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;
47 /** @param key Key to use to encrypt MP4 outputs */
48 FFmpegEncoder::FFmpegEncoder (
49 shared_ptr<const Film> film,
51 boost::filesystem::path output,
53 bool mixdown_to_stereo,
56 #ifdef DCPOMATIC_VARIANT_SWAROOP
57 , optional<dcp::Key> key
64 int const files = split_reels ? film->reels().size() : 1;
65 for (int i = 0; i < files; ++i) {
67 boost::filesystem::path filename = output;
68 string extension = boost::filesystem::extension (filename);
69 filename = boost::filesystem::change_extension (filename, "");
72 /// TRANSLATORS: _reel%1 here is to be added to an export filename to indicate
73 /// which reel it is. Preserve the %1; it will be replaced with the reel number.
74 filename = filename.string() + String::compose(_("_reel%1"), i + 1);
77 _file_encoders.push_back (
80 _film->video_frame_rate(),
81 _film->audio_frame_rate(),
82 mixdown_to_stereo ? 2 : film->audio_channels(),
88 #ifdef DCPOMATIC_VARIANT_SWAROOP
96 _player->set_always_burn_open_subtitles ();
97 _player->set_play_referenced ();
99 int const ch = film->audio_channels ();
102 if (mixdown_to_stereo) {
103 _output_audio_channels = 2;
104 map = AudioMapping (ch, 2);
105 float const overall_gain = 2 / (4 + sqrt(2));
106 float const minus_3dB = 1 / sqrt(2);
107 map.set (dcp::LEFT, 0, overall_gain);
108 map.set (dcp::RIGHT, 1, overall_gain);
109 map.set (dcp::CENTRE, 0, overall_gain * minus_3dB);
110 map.set (dcp::CENTRE, 1, overall_gain * minus_3dB);
111 map.set (dcp::LS, 0, overall_gain);
112 map.set (dcp::RS, 1, overall_gain);
114 _output_audio_channels = ch;
115 map = AudioMapping (ch, ch);
116 for (int i = 0; i < ch; ++i) {
121 _butler.reset (new Butler(_player, map, _output_audio_channels, bind(&PlayerVideo::force, _1, FFmpegFileEncoder::pixel_format(format)), true, false));
128 shared_ptr<Job> job = _job.lock ();
129 DCPOMATIC_ASSERT (job);
130 job->sub (_("Encoding"));
133 list<DCPTimePeriod> reel_periods = _film->reels ();
134 list<DCPTimePeriod>::const_iterator reel = reel_periods.begin ();
135 list<FileEncoderSet>::iterator encoder = _file_encoders.begin ();
137 DCPTime const video_frame = DCPTime::from_frames (1, _film->video_frame_rate ());
138 int const audio_frames = video_frame.frames_round(_film->audio_frame_rate());
139 float* interleaved = new float[_output_audio_channels * audio_frames];
140 shared_ptr<AudioBuffers> deinterleaved (new AudioBuffers (_output_audio_channels, audio_frames));
141 int const gets_per_frame = _film->three_d() ? 2 : 1;
142 for (DCPTime i; i < _film->length(); i += video_frame) {
144 if (_file_encoders.size() > 1 && !reel->contains(i)) {
145 /* Next reel and file */
148 DCPOMATIC_ASSERT (reel != reel_periods.end());
149 DCPOMATIC_ASSERT (encoder != _file_encoders.end());
152 for (int j = 0; j < gets_per_frame; ++j) {
153 pair<shared_ptr<PlayerVideo>, DCPTime> v = _butler->get_video (true, 0);
154 encoder->get(v.first->eyes())->video(v.first, v.second);
160 boost::mutex::scoped_lock lm (_mutex);
164 shared_ptr<Job> job = _job.lock ();
166 job->set_progress (float(i.get()) / _film->length().get());
169 _butler->get_audio (interleaved, audio_frames);
170 /* XXX: inefficient; butler interleaves and we deinterleave again */
171 float* p = interleaved;
172 for (int j = 0; j < audio_frames; ++j) {
173 for (int k = 0; k < _output_audio_channels; ++k) {
174 deinterleaved->data(k)[j] = *p++;
177 encoder->audio (deinterleaved);
179 delete[] interleaved;
181 BOOST_FOREACH (FileEncoderSet i, _file_encoders) {
187 FFmpegEncoder::current_rate () const
189 return _history.rate ();
193 FFmpegEncoder::frames_done () const
195 boost::mutex::scoped_lock lm (_mutex);
196 return _last_time.frames_round (_film->video_frame_rate ());
199 FFmpegEncoder::FileEncoderSet::FileEncoderSet (
200 dcp::Size video_frame_size,
201 int video_frame_rate,
202 int audio_frame_rate,
207 boost::filesystem::path output,
209 #ifdef DCPOMATIC_VARIANT_SWAROOP
210 , optional<dcp::Key> key
211 , optional<string> id
216 /// TRANSLATORS: L here is an abbreviation for "left", to indicate the left-eye part of a 3D export
217 _encoders[EYES_LEFT] = shared_ptr<FFmpegFileEncoder>(
218 new FFmpegFileEncoder(video_frame_size, video_frame_rate, audio_frame_rate, channels, format, x264_crf, String::compose("%1_%2%3", output.string(), _("L"), extension)
219 #ifdef DCPOMATIC_VARIANT_SWAROOP
224 /// TRANSLATORS: R here is an abbreviation for "right", to indicate the right-eye part of a 3D export
225 _encoders[EYES_RIGHT] = shared_ptr<FFmpegFileEncoder>(
226 new FFmpegFileEncoder(video_frame_size, video_frame_rate, audio_frame_rate, channels, format, x264_crf, String::compose("%1_%2%3", output.string(), _("R"), extension)
227 #ifdef DCPOMATIC_VARIANT_SWAROOP
233 _encoders[EYES_BOTH] = shared_ptr<FFmpegFileEncoder>(
234 new FFmpegFileEncoder(video_frame_size, video_frame_rate, audio_frame_rate, channels, format, x264_crf, String::compose("%1%2", output.string(), extension)
235 #ifdef DCPOMATIC_VARIANT_SWAROOP
243 shared_ptr<FFmpegFileEncoder>
244 FFmpegEncoder::FileEncoderSet::get (Eyes eyes) const
246 map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::const_iterator i = _encoders.find (eyes);
247 DCPOMATIC_ASSERT (i != _encoders.end());
252 FFmpegEncoder::FileEncoderSet::flush ()
254 for (map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::iterator i = _encoders.begin(); i != _encoders.end(); ++i) {
260 FFmpegEncoder::FileEncoderSet::audio (shared_ptr<AudioBuffers> a)
262 for (map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::iterator i = _encoders.begin(); i != _encoders.end(); ++i) {
263 i->second->audio (a);