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