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