Missed update to private test repo version.
[dcpomatic.git] / src / lib / ffmpeg_encoder.cc
1 /*
2     Copyright (C) 2017-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "butler.h"
23 #include "cross.h"
24 #include "ffmpeg_encoder.h"
25 #include "film.h"
26 #include "image.h"
27 #include "job.h"
28 #include "log.h"
29 #include "player.h"
30 #include "player_video.h"
31 #include "compose.hpp"
32 #include <iostream>
33
34 #include "i18n.h"
35
36
37 using std::cout;
38 using std::list;
39 using std::make_shared;
40 using std::shared_ptr;
41 using std::string;
42 using std::weak_ptr;
43 using boost::bind;
44 using boost::optional;
45 using namespace dcpomatic;
46 #if BOOST_VERSION >= 106100
47 using namespace boost::placeholders;
48 #endif
49
50
51 FFmpegEncoder::FFmpegEncoder (
52         shared_ptr<const Film> film,
53         weak_ptr<Job> job,
54         boost::filesystem::path output,
55         ExportFormat format,
56         bool mixdown_to_stereo,
57         bool split_reels,
58         bool audio_stream_per_channel,
59         int x264_crf
60         )
61         : Encoder (film, job)
62         , _history (200)
63         , _output (output)
64         , _format (format)
65         , _split_reels (split_reels)
66         , _audio_stream_per_channel (audio_stream_per_channel)
67         , _x264_crf (x264_crf)
68 {
69         _player->set_always_burn_open_subtitles ();
70         _player->set_play_referenced ();
71
72         int const ch = film->audio_channels ();
73
74         AudioMapping map;
75         if (mixdown_to_stereo) {
76                 _output_audio_channels = 2;
77                 map = AudioMapping (ch, 2);
78                 float const overall_gain = 2 / (4 + sqrt(2));
79                 float const minus_3dB = 1 / sqrt(2);
80                 if (ch == 2) {
81                         map.set (dcp::Channel::LEFT, 0, 1);
82                         map.set (dcp::Channel::RIGHT, 1, 1);
83                 } else if (ch == 4) {
84                         map.set (dcp::Channel::LEFT,   0, overall_gain);
85                         map.set (dcp::Channel::RIGHT,  1, overall_gain);
86                         map.set (dcp::Channel::CENTRE, 0, overall_gain * minus_3dB);
87                         map.set (dcp::Channel::CENTRE, 1, overall_gain * minus_3dB);
88                         map.set (dcp::Channel::LS,     0, overall_gain);
89                 } else if (ch >= 6) {
90                         map.set (dcp::Channel::LEFT,   0, overall_gain);
91                         map.set (dcp::Channel::RIGHT,  1, overall_gain);
92                         map.set (dcp::Channel::CENTRE, 0, overall_gain * minus_3dB);
93                         map.set (dcp::Channel::CENTRE, 1, overall_gain * minus_3dB);
94                         map.set (dcp::Channel::LS,     0, overall_gain);
95                         map.set (dcp::Channel::RS,     1, overall_gain);
96                 }
97                 /* XXX: maybe we should do something better for >6 channel DCPs */
98         } else {
99                 /* Our encoders don't really want to encode any channel count between 9 and 15 inclusive,
100                  * so let's just use 16 channel exports for any project with more than 8 channels.
101                  */
102                 _output_audio_channels = ch > 8 ? 16 : ch;
103                 map = AudioMapping (ch, _output_audio_channels);
104                 for (int i = 0; i < ch; ++i) {
105                         map.set (i, i, 1);
106                 }
107         }
108
109         _butler = std::make_shared<Butler>(
110                 _film,
111                 _player,
112                 map,
113                 _output_audio_channels,
114                 bind(&PlayerVideo::force, FFmpegFileEncoder::pixel_format(format)),
115                 VideoRange::VIDEO,
116                 Image::Alignment::PADDED,
117                 false,
118                 false,
119                 Butler::Audio::ENABLED
120                 );
121 }
122
123
124 void
125 FFmpegEncoder::go ()
126 {
127         {
128                 auto job = _job.lock ();
129                 DCPOMATIC_ASSERT (job);
130                 job->sub (_("Encoding"));
131         }
132
133         Waker waker;
134
135         list<FileEncoderSet> file_encoders;
136
137         int const files = _split_reels ? _film->reels().size() : 1;
138         for (int i = 0; i < files; ++i) {
139
140                 boost::filesystem::path filename = _output;
141                 string extension = boost::filesystem::extension (filename);
142                 filename = boost::filesystem::change_extension (filename, "");
143
144                 if (files > 1) {
145                         /// TRANSLATORS: _reel%1 here is to be added to an export filename to indicate
146                         /// which reel it is.  Preserve the %1; it will be replaced with the reel number.
147                         filename = filename.string() + String::compose(_("_reel%1"), i + 1);
148                 }
149
150                 file_encoders.push_back (
151                         FileEncoderSet (
152                                 _film->frame_size(),
153                                 _film->video_frame_rate(),
154                                 _film->audio_frame_rate(),
155                                 _output_audio_channels,
156                                 _format,
157                                 _audio_stream_per_channel,
158                                 _x264_crf,
159                                 _film->three_d(),
160                                 filename,
161                                 extension
162                                 )
163                         );
164         }
165
166         auto reel_periods = _film->reels ();
167         auto reel = reel_periods.begin ();
168         auto encoder = file_encoders.begin ();
169
170         auto const video_frame = DCPTime::from_frames (1, _film->video_frame_rate ());
171         int const audio_frames = video_frame.frames_round(_film->audio_frame_rate());
172         std::vector<float> interleaved(_output_audio_channels * audio_frames);
173         auto deinterleaved = make_shared<AudioBuffers>(_output_audio_channels, audio_frames);
174         int const gets_per_frame = _film->three_d() ? 2 : 1;
175         for (DCPTime i; i < _film->length(); i += video_frame) {
176
177                 if (file_encoders.size() > 1 && !reel->contains(i)) {
178                         /* Next reel and file */
179                         ++reel;
180                         ++encoder;
181                         DCPOMATIC_ASSERT (reel != reel_periods.end());
182                         DCPOMATIC_ASSERT (encoder != file_encoders.end());
183                 }
184
185                 for (int j = 0; j < gets_per_frame; ++j) {
186                         Butler::Error e;
187                         auto v = _butler->get_video (Butler::Behaviour::BLOCKING, &e);
188                         _butler->rethrow ();
189                         if (v.first) {
190                                 auto fe = encoder->get (v.first->eyes());
191                                 if (fe) {
192                                         fe->video(v.first, v.second - reel->from);
193                                 }
194                         } else {
195                                 if (e.code != Butler::Error::Code::FINISHED) {
196                                         throw DecodeError(String::compose("Error during decoding: %1", e.summary()));
197                                 }
198                         }
199                 }
200
201                 _history.event ();
202
203                 {
204                         boost::mutex::scoped_lock lm (_mutex);
205                         _last_time = i;
206                 }
207
208                 auto job = _job.lock ();
209                 if (job) {
210                         job->set_progress (float(i.get()) / _film->length().get());
211                 }
212
213                 waker.nudge ();
214
215                 _butler->get_audio (Butler::Behaviour::BLOCKING, interleaved.data(), audio_frames);
216                 /* XXX: inefficient; butler interleaves and we deinterleave again */
217                 float* p = interleaved.data();
218                 for (int j = 0; j < audio_frames; ++j) {
219                         for (int k = 0; k < _output_audio_channels; ++k) {
220                                 deinterleaved->data(k)[j] = *p++;
221                         }
222                 }
223                 encoder->audio (deinterleaved);
224         }
225
226         for (auto i: file_encoders) {
227                 i.flush ();
228         }
229 }
230
231 optional<float>
232 FFmpegEncoder::current_rate () const
233 {
234         return _history.rate ();
235 }
236
237 Frame
238 FFmpegEncoder::frames_done () const
239 {
240         boost::mutex::scoped_lock lm (_mutex);
241         return _last_time.frames_round (_film->video_frame_rate ());
242 }
243
244 FFmpegEncoder::FileEncoderSet::FileEncoderSet (
245         dcp::Size video_frame_size,
246         int video_frame_rate,
247         int audio_frame_rate,
248         int channels,
249         ExportFormat format,
250         bool audio_stream_per_channel,
251         int x264_crf,
252         bool three_d,
253         boost::filesystem::path output,
254         string extension
255         )
256 {
257         if (three_d) {
258                 /// TRANSLATORS: L here is an abbreviation for "left", to indicate the left-eye part of a 3D export
259                 _encoders[Eyes::LEFT] = make_shared<FFmpegFileEncoder>(
260                         video_frame_size, video_frame_rate, audio_frame_rate, channels, format,
261                         audio_stream_per_channel, x264_crf, String::compose("%1_%2%3", output.string(), _("L"), extension)
262                         );
263                 /// TRANSLATORS: R here is an abbreviation for "right", to indicate the right-eye part of a 3D export
264                 _encoders[Eyes::RIGHT] = make_shared<FFmpegFileEncoder>(
265                         video_frame_size, video_frame_rate, audio_frame_rate, channels, format,
266                         audio_stream_per_channel, x264_crf, String::compose("%1_%2%3", output.string(), _("R"), extension)
267                         );
268         } else {
269                 _encoders[Eyes::BOTH] = make_shared<FFmpegFileEncoder>(
270                         video_frame_size, video_frame_rate, audio_frame_rate, channels, format,
271                         audio_stream_per_channel, x264_crf, String::compose("%1%2", output.string(), extension)
272                         );
273         }
274 }
275
276 shared_ptr<FFmpegFileEncoder>
277 FFmpegEncoder::FileEncoderSet::get (Eyes eyes) const
278 {
279         if (_encoders.size() == 1) {
280                 /* We are doing a 2D export... */
281                 if (eyes == Eyes::LEFT) {
282                         /* ...but we got some 3D data; put the left eye into the output... */
283                         eyes = Eyes::BOTH;
284                 } else if (eyes == Eyes::RIGHT) {
285                         /* ...and ignore the right eye.*/
286                         return {};
287                 }
288         }
289
290         auto i = _encoders.find (eyes);
291         DCPOMATIC_ASSERT (i != _encoders.end());
292         return i->second;
293 }
294
295 void
296 FFmpegEncoder::FileEncoderSet::flush ()
297 {
298         for (auto& i: _encoders) {
299                 i.second->flush ();
300         }
301 }
302
303 void
304 FFmpegEncoder::FileEncoderSet::audio (shared_ptr<AudioBuffers> a)
305 {
306         for (auto& i: _encoders) {
307                 i.second->audio (a);
308         }
309 }