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,
62 , _output_audio_channels(mixdown_to_stereo ? 2 : (_film->audio_channels() > 8 ? 16 : _film->audio_channels()))
66 , _split_reels (split_reels)
67 , _audio_stream_per_channel (audio_stream_per_channel)
68 , _x264_crf (x264_crf)
72 mixdown_to_stereo ? stereo_map() : many_channel_map(),
73 _output_audio_channels,
74 boost::bind(&PlayerVideo::force, FFmpegFileEncoder::pixel_format(format)),
76 Image::Alignment::PADDED,
79 Butler::Audio::ENABLED
82 _player.set_always_burn_open_subtitles();
83 _player.set_play_referenced();
88 FFmpegEncoder::stereo_map() const
90 auto map = AudioMapping(_film->audio_channels(), 2);
91 float const overall_gain = 2 / (4 + sqrt(2));
92 float const minus_3dB = 1 / sqrt(2);
93 switch (_film->audio_channels()) {
95 map.set(dcp::Channel::LEFT, 0, 1);
96 map.set(dcp::Channel::RIGHT, 1, 1);
99 map.set(dcp::Channel::LEFT, 0, overall_gain);
100 map.set(dcp::Channel::RIGHT, 1, overall_gain);
101 map.set(dcp::Channel::CENTRE, 0, overall_gain * minus_3dB);
102 map.set(dcp::Channel::CENTRE, 1, overall_gain * minus_3dB);
103 map.set(dcp::Channel::LS, 0, overall_gain);
106 map.set(dcp::Channel::LEFT, 0, overall_gain);
107 map.set(dcp::Channel::RIGHT, 1, overall_gain);
108 map.set(dcp::Channel::CENTRE, 0, overall_gain * minus_3dB);
109 map.set(dcp::Channel::CENTRE, 1, overall_gain * minus_3dB);
110 map.set(dcp::Channel::LS, 0, overall_gain);
111 map.set(dcp::Channel::RS, 1, overall_gain);
114 /* XXX: maybe we should do something better for >6 channel DCPs */
120 FFmpegEncoder::many_channel_map() const
122 auto map = AudioMapping(_film->audio_channels(), _output_audio_channels);
123 for (int i = 0; i < _film->audio_channels(); ++i) {
134 auto job = _job.lock ();
135 DCPOMATIC_ASSERT (job);
136 job->sub (_("Encoding"));
141 list<FileEncoderSet> file_encoders;
143 int const files = _split_reels ? _film->reels().size() : 1;
144 for (int i = 0; i < files; ++i) {
146 boost::filesystem::path filename = _output;
147 string extension = boost::filesystem::extension (filename);
148 filename = boost::filesystem::change_extension (filename, "");
151 /// TRANSLATORS: _reel%1 here is to be added to an export filename to indicate
152 /// which reel it is. Preserve the %1; it will be replaced with the reel number.
153 filename = filename.string() + String::compose(_("_reel%1"), i + 1);
156 file_encoders.push_back (
159 _film->video_frame_rate(),
160 _film->audio_frame_rate(),
161 _output_audio_channels,
163 _audio_stream_per_channel,
172 auto reel_periods = _film->reels ();
173 auto reel = reel_periods.begin ();
174 auto encoder = file_encoders.begin ();
176 auto const video_frame = DCPTime::from_frames (1, _film->video_frame_rate ());
177 int const audio_frames = video_frame.frames_round(_film->audio_frame_rate());
178 std::vector<float> interleaved(_output_audio_channels * audio_frames);
179 auto deinterleaved = make_shared<AudioBuffers>(_output_audio_channels, audio_frames);
180 int const gets_per_frame = _film->three_d() ? 2 : 1;
181 for (DCPTime i; i < _film->length(); i += video_frame) {
183 if (file_encoders.size() > 1 && !reel->contains(i)) {
184 /* Next reel and file */
187 DCPOMATIC_ASSERT (reel != reel_periods.end());
188 DCPOMATIC_ASSERT (encoder != file_encoders.end());
191 for (int j = 0; j < gets_per_frame; ++j) {
193 auto v = _butler.get_video(Butler::Behaviour::BLOCKING, &e);
196 auto fe = encoder->get (v.first->eyes());
198 fe->video(v.first, v.second - reel->from);
201 if (e.code != Butler::Error::Code::FINISHED) {
202 throw DecodeError(String::compose("Error during decoding: %1", e.summary()));
210 boost::mutex::scoped_lock lm (_mutex);
214 auto job = _job.lock ();
216 job->set_progress (float(i.get()) / _film->length().get());
221 _butler.get_audio(Butler::Behaviour::BLOCKING, interleaved.data(), audio_frames);
222 /* XXX: inefficient; butler interleaves and we deinterleave again */
223 float* p = interleaved.data();
224 for (int j = 0; j < audio_frames; ++j) {
225 for (int k = 0; k < _output_audio_channels; ++k) {
226 deinterleaved->data(k)[j] = *p++;
229 encoder->audio (deinterleaved);
232 for (auto i: file_encoders) {
238 FFmpegEncoder::current_rate () const
240 return _history.rate ();
244 FFmpegEncoder::frames_done () const
246 boost::mutex::scoped_lock lm (_mutex);
247 return _last_time.frames_round (_film->video_frame_rate ());
250 FFmpegEncoder::FileEncoderSet::FileEncoderSet (
251 dcp::Size video_frame_size,
252 int video_frame_rate,
253 int audio_frame_rate,
256 bool audio_stream_per_channel,
259 boost::filesystem::path output,
264 /// TRANSLATORS: L here is an abbreviation for "left", to indicate the left-eye part of a 3D export
265 _encoders[Eyes::LEFT] = make_shared<FFmpegFileEncoder>(
266 video_frame_size, video_frame_rate, audio_frame_rate, channels, format,
267 audio_stream_per_channel, x264_crf, String::compose("%1_%2%3", output.string(), _("L"), extension)
269 /// TRANSLATORS: R here is an abbreviation for "right", to indicate the right-eye part of a 3D export
270 _encoders[Eyes::RIGHT] = make_shared<FFmpegFileEncoder>(
271 video_frame_size, video_frame_rate, audio_frame_rate, channels, format,
272 audio_stream_per_channel, x264_crf, String::compose("%1_%2%3", output.string(), _("R"), extension)
275 _encoders[Eyes::BOTH] = make_shared<FFmpegFileEncoder>(
276 video_frame_size, video_frame_rate, audio_frame_rate, channels, format,
277 audio_stream_per_channel, x264_crf, String::compose("%1%2", output.string(), extension)
282 shared_ptr<FFmpegFileEncoder>
283 FFmpegEncoder::FileEncoderSet::get (Eyes eyes) const
285 if (_encoders.size() == 1) {
286 /* We are doing a 2D export... */
287 if (eyes == Eyes::LEFT) {
288 /* ...but we got some 3D data; put the left eye into the output... */
290 } else if (eyes == Eyes::RIGHT) {
291 /* ...and ignore the right eye.*/
296 auto i = _encoders.find (eyes);
297 DCPOMATIC_ASSERT (i != _encoders.end());
302 FFmpegEncoder::FileEncoderSet::flush ()
304 for (auto& i: _encoders) {
310 FFmpegEncoder::FileEncoderSet::audio (shared_ptr<AudioBuffers> a)
312 for (auto& i: _encoders) {