2 Copyright (C) 2017-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "ffmpeg_encoder.h"
25 #include "player_video.h"
30 #include "compose.hpp"
36 using std::runtime_error;
41 using boost::shared_ptr;
43 using boost::weak_ptr;
44 using boost::optional;
45 using namespace dcpomatic;
46 #if BOOST_VERSION >= 106100
47 using namespace boost::placeholders;
50 /** @param key Key to use to encrypt MP4 outputs */
51 FFmpegEncoder::FFmpegEncoder (
52 shared_ptr<const Film> film,
54 boost::filesystem::path output,
56 bool mixdown_to_stereo,
58 bool audio_stream_per_channel,
60 #ifdef DCPOMATIC_VARIANT_SWAROOP
61 , optional<dcp::Key> key
69 , _split_reels (split_reels)
70 , _audio_stream_per_channel (audio_stream_per_channel)
71 , _x264_crf (x264_crf)
73 _player->set_always_burn_open_subtitles ();
74 _player->set_play_referenced ();
76 int const ch = film->audio_channels ();
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);
85 map.set (dcp::LEFT, 0, 1);
86 map.set (dcp::RIGHT, 1, 1);
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);
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);
101 /* XXX: maybe we should do something better for >6 channel DCPs */
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.
106 _output_audio_channels = ch > 8 ? 16 : ch;
107 map = AudioMapping (ch, _output_audio_channels);
108 for (int i = 0; i < ch; ++i) {
113 _butler.reset (new Butler(_player, map, _output_audio_channels, bind(&PlayerVideo::force, _1, FFmpegFileEncoder::pixel_format(format)), true, false));
121 shared_ptr<Job> job = _job.lock ();
122 DCPOMATIC_ASSERT (job);
123 job->sub (_("Encoding"));
128 list<FileEncoderSet> file_encoders;
130 int const files = _split_reels ? _film->reels().size() : 1;
131 for (int i = 0; i < files; ++i) {
133 boost::filesystem::path filename = _output;
134 string extension = boost::filesystem::extension (filename);
135 filename = boost::filesystem::change_extension (filename, "");
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);
143 file_encoders.push_back (
146 _film->video_frame_rate(),
147 _film->audio_frame_rate(),
148 _output_audio_channels,
150 _audio_stream_per_channel,
155 #ifdef DCPOMATIC_VARIANT_SWAROOP
163 list<DCPTimePeriod> reel_periods = _film->reels ();
164 list<DCPTimePeriod>::const_iterator reel = reel_periods.begin ();
165 list<FileEncoderSet>::iterator encoder = file_encoders.begin ();
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) {
174 if (file_encoders.size() > 1 && !reel->contains(i)) {
175 /* Next reel and file */
178 DCPOMATIC_ASSERT (reel != reel_periods.end());
179 DCPOMATIC_ASSERT (encoder != file_encoders.end());
182 for (int j = 0; j < gets_per_frame; ++j) {
184 pair<shared_ptr<PlayerVideo>, DCPTime> v = _butler->get_video (true, &e);
187 throw DecodeError(String::compose("Error during decoding: %1", e.summary()));
189 shared_ptr<FFmpegFileEncoder> fe = encoder->get (v.first->eyes());
191 fe->video(v.first, v.second);
198 boost::mutex::scoped_lock lm (_mutex);
202 shared_ptr<Job> job = _job.lock ();
204 job->set_progress (float(i.get()) / _film->length().get());
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++;
217 encoder->audio (deinterleaved);
219 delete[] interleaved;
221 BOOST_FOREACH (FileEncoderSet i, file_encoders) {
227 FFmpegEncoder::current_rate () const
229 return _history.rate ();
233 FFmpegEncoder::frames_done () const
235 boost::mutex::scoped_lock lm (_mutex);
236 return _last_time.frames_round (_film->video_frame_rate ());
239 FFmpegEncoder::FileEncoderSet::FileEncoderSet (
240 dcp::Size video_frame_size,
241 int video_frame_rate,
242 int audio_frame_rate,
245 bool audio_stream_per_channel,
248 boost::filesystem::path output,
250 #ifdef DCPOMATIC_VARIANT_SWAROOP
251 , optional<dcp::Key> key
252 , optional<string> id
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
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
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
290 shared_ptr<FFmpegFileEncoder>
291 FFmpegEncoder::FileEncoderSet::get (Eyes eyes) const
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... */
298 } else if (eyes == EYES_RIGHT) {
299 /* ...and ignore the right eye.*/
300 return shared_ptr<FFmpegFileEncoder>();
304 map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::const_iterator i = _encoders.find (eyes);
305 DCPOMATIC_ASSERT (i != _encoders.end());
310 FFmpegEncoder::FileEncoderSet::flush ()
312 for (map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::iterator i = _encoders.begin(); i != _encoders.end(); ++i) {
318 FFmpegEncoder::FileEncoderSet::audio (shared_ptr<AudioBuffers> a)
320 for (map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::iterator i = _encoders.begin(); i != _encoders.end(); ++i) {
321 i->second->audio (a);