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