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/>.
24 #include "ffmpeg_encoder.h"
30 #include "player_video.h"
31 #include "compose.hpp"
39 using std::make_shared;
40 using std::shared_ptr;
44 using boost::optional;
45 using namespace dcpomatic;
46 #if BOOST_VERSION >= 106100
47 using namespace boost::placeholders;
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,
65 , _split_reels (split_reels)
66 , _audio_stream_per_channel (audio_stream_per_channel)
67 , _x264_crf (x264_crf)
69 _player->set_always_burn_open_subtitles ();
70 _player->set_play_referenced ();
72 int const ch = film->audio_channels ();
75 if (mixdown_to_stereo) {
76 _output_audio_channels = 2;
77 map = AudioMapping (ch, 2);
78 float const overall_gain = 2 / (4 + sqrt(2));
79 float const minus_3dB = 1 / sqrt(2);
81 map.set (dcp::Channel::LEFT, 0, 1);
82 map.set (dcp::Channel::RIGHT, 1, 1);
84 map.set (dcp::Channel::LEFT, 0, overall_gain);
85 map.set (dcp::Channel::RIGHT, 1, overall_gain);
86 map.set (dcp::Channel::CENTRE, 0, overall_gain * minus_3dB);
87 map.set (dcp::Channel::CENTRE, 1, overall_gain * minus_3dB);
88 map.set (dcp::Channel::LS, 0, overall_gain);
90 map.set (dcp::Channel::LEFT, 0, overall_gain);
91 map.set (dcp::Channel::RIGHT, 1, overall_gain);
92 map.set (dcp::Channel::CENTRE, 0, overall_gain * minus_3dB);
93 map.set (dcp::Channel::CENTRE, 1, overall_gain * minus_3dB);
94 map.set (dcp::Channel::LS, 0, overall_gain);
95 map.set (dcp::Channel::RS, 1, overall_gain);
97 /* XXX: maybe we should do something better for >6 channel DCPs */
99 /* Our encoders don't really want to encode any channel count between 9 and 15 inclusive,
100 * so let's just use 16 channel exports for any project with more than 8 channels.
102 _output_audio_channels = ch > 8 ? 16 : ch;
103 map = AudioMapping (ch, _output_audio_channels);
104 for (int i = 0; i < ch; ++i) {
109 _butler = std::make_shared<Butler>(
113 _output_audio_channels,
114 bind(&PlayerVideo::force, FFmpegFileEncoder::pixel_format(format)),
118 Image::Alignment::PADDED,
121 Butler::Audio::ENABLED
130 auto job = _job.lock ();
131 DCPOMATIC_ASSERT (job);
132 job->sub (_("Encoding"));
137 list<FileEncoderSet> file_encoders;
139 int const files = _split_reels ? _film->reels().size() : 1;
140 for (int i = 0; i < files; ++i) {
142 boost::filesystem::path filename = _output;
143 string extension = boost::filesystem::extension (filename);
144 filename = boost::filesystem::change_extension (filename, "");
147 /// TRANSLATORS: _reel%1 here is to be added to an export filename to indicate
148 /// which reel it is. Preserve the %1; it will be replaced with the reel number.
149 filename = filename.string() + String::compose(_("_reel%1"), i + 1);
152 file_encoders.push_back (
155 _film->video_frame_rate(),
156 _film->audio_frame_rate(),
157 _output_audio_channels,
159 _audio_stream_per_channel,
168 auto reel_periods = _film->reels ();
169 auto reel = reel_periods.begin ();
170 auto encoder = file_encoders.begin ();
172 auto const video_frame = DCPTime::from_frames (1, _film->video_frame_rate ());
173 int const audio_frames = video_frame.frames_round(_film->audio_frame_rate());
174 std::vector<float> interleaved(_output_audio_channels * audio_frames);
175 auto deinterleaved = make_shared<AudioBuffers>(_output_audio_channels, audio_frames);
176 int const gets_per_frame = _film->three_d() ? 2 : 1;
177 for (DCPTime i; i < _film->length(); i += video_frame) {
179 if (file_encoders.size() > 1 && !reel->contains(i)) {
180 /* Next reel and file */
183 DCPOMATIC_ASSERT (reel != reel_periods.end());
184 DCPOMATIC_ASSERT (encoder != file_encoders.end());
187 for (int j = 0; j < gets_per_frame; ++j) {
189 auto v = _butler->get_video (Butler::Behaviour::BLOCKING, &e);
192 auto fe = encoder->get (v.first->eyes());
194 fe->video(v.first, v.second - reel->from);
197 if (e.code != Butler::Error::Code::FINISHED) {
198 throw DecodeError(String::compose("Error during decoding: %1", e.summary()));
206 boost::mutex::scoped_lock lm (_mutex);
210 auto job = _job.lock ();
212 job->set_progress (float(i.get()) / _film->length().get());
217 _butler->get_audio (Butler::Behaviour::BLOCKING, interleaved.data(), audio_frames);
218 /* XXX: inefficient; butler interleaves and we deinterleave again */
219 float* p = interleaved.data();
220 for (int j = 0; j < audio_frames; ++j) {
221 for (int k = 0; k < _output_audio_channels; ++k) {
222 deinterleaved->data(k)[j] = *p++;
225 encoder->audio (deinterleaved);
228 for (auto i: file_encoders) {
234 FFmpegEncoder::current_rate () const
236 return _history.rate ();
240 FFmpegEncoder::frames_done () const
242 boost::mutex::scoped_lock lm (_mutex);
243 return _last_time.frames_round (_film->video_frame_rate ());
246 FFmpegEncoder::FileEncoderSet::FileEncoderSet (
247 dcp::Size video_frame_size,
248 int video_frame_rate,
249 int audio_frame_rate,
252 bool audio_stream_per_channel,
255 boost::filesystem::path output,
260 /// TRANSLATORS: L here is an abbreviation for "left", to indicate the left-eye part of a 3D export
261 _encoders[Eyes::LEFT] = make_shared<FFmpegFileEncoder>(
262 video_frame_size, video_frame_rate, audio_frame_rate, channels, format,
263 audio_stream_per_channel, x264_crf, String::compose("%1_%2%3", output.string(), _("L"), extension)
265 /// TRANSLATORS: R here is an abbreviation for "right", to indicate the right-eye part of a 3D export
266 _encoders[Eyes::RIGHT] = make_shared<FFmpegFileEncoder>(
267 video_frame_size, video_frame_rate, audio_frame_rate, channels, format,
268 audio_stream_per_channel, x264_crf, String::compose("%1_%2%3", output.string(), _("R"), extension)
271 _encoders[Eyes::BOTH] = make_shared<FFmpegFileEncoder>(
272 video_frame_size, video_frame_rate, audio_frame_rate, channels, format,
273 audio_stream_per_channel, x264_crf, String::compose("%1%2", output.string(), extension)
278 shared_ptr<FFmpegFileEncoder>
279 FFmpegEncoder::FileEncoderSet::get (Eyes eyes) const
281 if (_encoders.size() == 1) {
282 /* We are doing a 2D export... */
283 if (eyes == Eyes::LEFT) {
284 /* ...but we got some 3D data; put the left eye into the output... */
286 } else if (eyes == Eyes::RIGHT) {
287 /* ...and ignore the right eye.*/
292 auto i = _encoders.find (eyes);
293 DCPOMATIC_ASSERT (i != _encoders.end());
298 FFmpegEncoder::FileEncoderSet::flush ()
300 for (auto& i: _encoders) {
306 FFmpegEncoder::FileEncoderSet::audio (shared_ptr<AudioBuffers> a)
308 for (auto& i: _encoders) {