Report better errors when the butler dies.
[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 #include "ffmpeg_encoder.h"
22 #include "film.h"
23 #include "job.h"
24 #include "player.h"
25 #include "player_video.h"
26 #include "log.h"
27 #include "image.h"
28 #include "cross.h"
29 #include "butler.h"
30 #include "compose.hpp"
31 #include <iostream>
32
33 #include "i18n.h"
34
35 using std::string;
36 using std::runtime_error;
37 using std::cout;
38 using std::pair;
39 using std::list;
40 using std::map;
41 using boost::shared_ptr;
42 using boost::bind;
43 using boost::weak_ptr;
44 using boost::optional;
45 using namespace dcpomatic;
46
47 /** @param key Key to use to encrypt MP4 outputs */
48 FFmpegEncoder::FFmpegEncoder (
49         shared_ptr<const Film> film,
50         weak_ptr<Job> job,
51         boost::filesystem::path output,
52         ExportFormat format,
53         bool mixdown_to_stereo,
54         bool split_reels,
55         bool audio_stream_per_channel,
56         int x264_crf
57 #ifdef DCPOMATIC_VARIANT_SWAROOP
58         , optional<dcp::Key> key
59         , optional<string> id
60 #endif
61         )
62         : Encoder (film, job)
63         , _history (200)
64 {
65         _player->set_always_burn_open_subtitles ();
66         _player->set_play_referenced ();
67
68         int const ch = film->audio_channels ();
69
70         AudioMapping map;
71         if (mixdown_to_stereo) {
72                 _output_audio_channels = 2;
73                 map = AudioMapping (ch, 2);
74                 float const overall_gain = 2 / (4 + sqrt(2));
75                 float const minus_3dB = 1 / sqrt(2);
76                 if (ch == 2) {
77                         map.set (dcp::LEFT, 0, 1);
78                         map.set (dcp::RIGHT, 1, 1);
79                 } else if (ch == 4) {
80                         map.set (dcp::LEFT,   0, overall_gain);
81                         map.set (dcp::RIGHT,  1, overall_gain);
82                         map.set (dcp::CENTRE, 0, overall_gain * minus_3dB);
83                         map.set (dcp::CENTRE, 1, overall_gain * minus_3dB);
84                         map.set (dcp::LS,     0, overall_gain);
85                 } else if (ch >= 6) {
86                         map.set (dcp::LEFT,   0, overall_gain);
87                         map.set (dcp::RIGHT,  1, overall_gain);
88                         map.set (dcp::CENTRE, 0, overall_gain * minus_3dB);
89                         map.set (dcp::CENTRE, 1, overall_gain * minus_3dB);
90                         map.set (dcp::LS,     0, overall_gain);
91                         map.set (dcp::RS,     1, overall_gain);
92                 }
93                 /* XXX: maybe we should do something better for >6 channel DCPs */
94         } else {
95                 /* Our encoders don't really want to encode any channel count between 9 and 15 inclusive,
96                  * so let's just use 16 channel exports for any project with more than 8 channels.
97                  */
98                 _output_audio_channels = ch > 8 ? 16 : ch;
99                 map = AudioMapping (ch, _output_audio_channels);
100                 for (int i = 0; i < ch; ++i) {
101                         map.set (i, i, 1);
102                 }
103         }
104
105         _butler.reset (new Butler(_player, map, _output_audio_channels, bind(&PlayerVideo::force, _1, FFmpegFileEncoder::pixel_format(format)), true, false));
106
107         int const files = split_reels ? film->reels().size() : 1;
108         for (int i = 0; i < files; ++i) {
109
110                 boost::filesystem::path filename = output;
111                 string extension = boost::filesystem::extension (filename);
112                 filename = boost::filesystem::change_extension (filename, "");
113
114                 if (files > 1) {
115                         /// TRANSLATORS: _reel%1 here is to be added to an export filename to indicate
116                         /// which reel it is.  Preserve the %1; it will be replaced with the reel number.
117                         filename = filename.string() + String::compose(_("_reel%1"), i + 1);
118                 }
119
120                 _file_encoders.push_back (
121                         FileEncoderSet (
122                                 _film->frame_size(),
123                                 _film->video_frame_rate(),
124                                 _film->audio_frame_rate(),
125                                 _output_audio_channels,
126                                 format,
127                                 audio_stream_per_channel,
128                                 x264_crf,
129                                 _film->three_d(),
130                                 filename,
131                                 extension
132 #ifdef DCPOMATIC_VARIANT_SWAROOP
133                                 , key
134                                 , id
135 #endif
136                                 )
137                         );
138         }
139 }
140
141
142 void
143 FFmpegEncoder::go ()
144 {
145         {
146                 shared_ptr<Job> job = _job.lock ();
147                 DCPOMATIC_ASSERT (job);
148                 job->sub (_("Encoding"));
149         }
150
151         Waker waker;
152
153         list<DCPTimePeriod> reel_periods = _film->reels ();
154         list<DCPTimePeriod>::const_iterator reel = reel_periods.begin ();
155         list<FileEncoderSet>::iterator encoder = _file_encoders.begin ();
156
157         DCPTime const video_frame = DCPTime::from_frames (1, _film->video_frame_rate ());
158         int const audio_frames = video_frame.frames_round(_film->audio_frame_rate());
159         float* interleaved = new float[_output_audio_channels * audio_frames];
160         shared_ptr<AudioBuffers> deinterleaved (new AudioBuffers (_output_audio_channels, audio_frames));
161         int const gets_per_frame = _film->three_d() ? 2 : 1;
162         for (DCPTime i; i < _film->length(); i += video_frame) {
163
164                 if (_file_encoders.size() > 1 && !reel->contains(i)) {
165                         /* Next reel and file */
166                         ++reel;
167                         ++encoder;
168                         DCPOMATIC_ASSERT (reel != reel_periods.end());
169                         DCPOMATIC_ASSERT (encoder != _file_encoders.end());
170                 }
171
172                 for (int j = 0; j < gets_per_frame; ++j) {
173                         Butler::Error e;
174                         pair<shared_ptr<PlayerVideo>, DCPTime> v = _butler->get_video (true, &e);
175                         _butler->rethrow ();
176                         if (!v.first) {
177                                 throw ProgrammingError(__FILE__, __LINE__, String::compose("butler returned no video; error was %1", e.summary()));
178                         }
179                         shared_ptr<FFmpegFileEncoder> fe = encoder->get (v.first->eyes());
180                         if (fe) {
181                                 fe->video(v.first, v.second);
182                         }
183                 }
184
185                 _history.event ();
186
187                 {
188                         boost::mutex::scoped_lock lm (_mutex);
189                         _last_time = i;
190                 }
191
192                 shared_ptr<Job> job = _job.lock ();
193                 if (job) {
194                         job->set_progress (float(i.get()) / _film->length().get());
195                 }
196
197                 waker.nudge ();
198
199                 _butler->get_audio (interleaved, audio_frames);
200                 /* XXX: inefficient; butler interleaves and we deinterleave again */
201                 float* p = interleaved;
202                 for (int j = 0; j < audio_frames; ++j) {
203                         for (int k = 0; k < _output_audio_channels; ++k) {
204                                 deinterleaved->data(k)[j] = *p++;
205                         }
206                 }
207                 encoder->audio (deinterleaved);
208         }
209         delete[] interleaved;
210
211         BOOST_FOREACH (FileEncoderSet i, _file_encoders) {
212                 i.flush ();
213         }
214 }
215
216 optional<float>
217 FFmpegEncoder::current_rate () const
218 {
219         return _history.rate ();
220 }
221
222 Frame
223 FFmpegEncoder::frames_done () const
224 {
225         boost::mutex::scoped_lock lm (_mutex);
226         return _last_time.frames_round (_film->video_frame_rate ());
227 }
228
229 FFmpegEncoder::FileEncoderSet::FileEncoderSet (
230         dcp::Size video_frame_size,
231         int video_frame_rate,
232         int audio_frame_rate,
233         int channels,
234         ExportFormat format,
235         bool audio_stream_per_channel,
236         int x264_crf,
237         bool three_d,
238         boost::filesystem::path output,
239         string extension
240 #ifdef DCPOMATIC_VARIANT_SWAROOP
241         , optional<dcp::Key> key
242         , optional<string> id
243 #endif
244         )
245 {
246         if (three_d) {
247                 /// TRANSLATORS: L here is an abbreviation for "left", to indicate the left-eye part of a 3D export
248                 _encoders[EYES_LEFT] = shared_ptr<FFmpegFileEncoder>(
249                         new FFmpegFileEncoder(
250                                 video_frame_size, video_frame_rate, audio_frame_rate, channels, format,
251                                 audio_stream_per_channel, x264_crf, String::compose("%1_%2%3", output.string(), _("L"), extension)
252 #ifdef DCPOMATIC_VARIANT_SWAROOP
253                                               , key, id
254 #endif
255                                 )
256                         );
257                 /// TRANSLATORS: R here is an abbreviation for "right", to indicate the right-eye part of a 3D export
258                 _encoders[EYES_RIGHT] = shared_ptr<FFmpegFileEncoder>(
259                         new 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(), _("R"), extension)
262 #ifdef DCPOMATIC_VARIANT_SWAROOP
263                                               , key, id
264 #endif
265                                 )
266                         );
267         } else {
268                 _encoders[EYES_BOTH]  = shared_ptr<FFmpegFileEncoder>(
269                         new 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 #ifdef DCPOMATIC_VARIANT_SWAROOP
273                                               , key, id
274 #endif
275                                 )
276                         );
277         }
278 }
279
280 shared_ptr<FFmpegFileEncoder>
281 FFmpegEncoder::FileEncoderSet::get (Eyes eyes) const
282 {
283         if (_encoders.size() == 1) {
284                 /* We are doing a 2D export... */
285                 if (eyes == EYES_LEFT) {
286                         /* ...but we got some 3D data; put the left eye into the output... */
287                         eyes = EYES_BOTH;
288                 } else if (eyes == EYES_RIGHT) {
289                         /* ...and ignore the right eye.*/
290                         return shared_ptr<FFmpegFileEncoder>();
291                 }
292         }
293
294         map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::const_iterator i = _encoders.find (eyes);
295         DCPOMATIC_ASSERT (i != _encoders.end());
296         return i->second;
297 }
298
299 void
300 FFmpegEncoder::FileEncoderSet::flush ()
301 {
302         for (map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::iterator i = _encoders.begin(); i != _encoders.end(); ++i) {
303                 i->second->flush ();
304         }
305 }
306
307 void
308 FFmpegEncoder::FileEncoderSet::audio (shared_ptr<AudioBuffers> a)
309 {
310         for (map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::iterator i = _encoders.begin(); i != _encoders.end(); ++i) {
311                 i->second->audio (a);
312         }
313 }