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