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 namespace dcpomatic;
46 FFmpegEncoder::FFmpegEncoder (
47 shared_ptr<const Film> film,
49 boost::filesystem::path output,
51 bool mixdown_to_stereo,
58 int const files = split_reels ? film->reels().size() : 1;
59 for (int i = 0; i < files; ++i) {
61 boost::filesystem::path filename = output;
62 string extension = boost::filesystem::extension (filename);
63 filename = boost::filesystem::change_extension (filename, "");
66 /// TRANSLATORS: _reel%1 here is to be added to an export filename to indicate
67 /// which reel it is. Preserve the %1; it will be replaced with the reel number.
68 filename = filename.string() + String::compose(_("_reel%1"), i + 1);
71 _file_encoders.push_back (
74 _film->video_frame_rate(),
75 _film->audio_frame_rate(),
76 mixdown_to_stereo ? 2 : film->audio_channels(),
86 _player->set_always_burn_open_subtitles ();
87 _player->set_play_referenced ();
89 int const ch = film->audio_channels ();
92 if (mixdown_to_stereo) {
93 _output_audio_channels = 2;
94 map = AudioMapping (ch, 2);
95 float const overall_gain = 2 / (4 + sqrt(2));
96 float const minus_3dB = 1 / sqrt(2);
97 map.set (dcp::LEFT, 0, overall_gain);
98 map.set (dcp::RIGHT, 1, overall_gain);
99 map.set (dcp::CENTRE, 0, overall_gain * minus_3dB);
100 map.set (dcp::CENTRE, 1, overall_gain * minus_3dB);
101 map.set (dcp::LS, 0, overall_gain);
102 map.set (dcp::RS, 1, overall_gain);
104 _output_audio_channels = ch;
105 map = AudioMapping (ch, ch);
106 for (int i = 0; i < ch; ++i) {
111 _butler.reset (new Butler(_player, map, _output_audio_channels, bind(&PlayerVideo::force, _1, FFmpegFileEncoder::pixel_format(format)), true, false));
118 shared_ptr<Job> job = _job.lock ();
119 DCPOMATIC_ASSERT (job);
120 job->sub (_("Encoding"));
123 list<DCPTimePeriod> reel_periods = _film->reels ();
124 list<DCPTimePeriod>::const_iterator reel = reel_periods.begin ();
125 list<FileEncoderSet>::iterator encoder = _file_encoders.begin ();
127 DCPTime const video_frame = DCPTime::from_frames (1, _film->video_frame_rate ());
128 int const audio_frames = video_frame.frames_round(_film->audio_frame_rate());
129 float* interleaved = new float[_output_audio_channels * audio_frames];
130 shared_ptr<AudioBuffers> deinterleaved (new AudioBuffers (_output_audio_channels, audio_frames));
131 int const gets_per_frame = _film->three_d() ? 2 : 1;
132 for (DCPTime i; i < _film->length(); i += video_frame) {
134 if (_file_encoders.size() > 1 && !reel->contains(i)) {
135 /* Next reel and file */
138 DCPOMATIC_ASSERT (reel != reel_periods.end());
139 DCPOMATIC_ASSERT (encoder != _file_encoders.end());
142 for (int j = 0; j < gets_per_frame; ++j) {
143 pair<shared_ptr<PlayerVideo>, DCPTime> v = _butler->get_video ();
144 encoder->get(v.first->eyes())->video(v.first, v.second);
150 boost::mutex::scoped_lock lm (_mutex);
154 shared_ptr<Job> job = _job.lock ();
156 job->set_progress (float(i.get()) / _film->length().get());
159 _butler->get_audio (interleaved, audio_frames);
160 /* XXX: inefficient; butler interleaves and we deinterleave again */
161 float* p = interleaved;
162 for (int j = 0; j < audio_frames; ++j) {
163 for (int k = 0; k < _output_audio_channels; ++k) {
164 deinterleaved->data(k)[j] = *p++;
167 encoder->audio (deinterleaved);
169 delete[] interleaved;
171 BOOST_FOREACH (FileEncoderSet i, _file_encoders) {
177 FFmpegEncoder::current_rate () const
179 return _history.rate ();
183 FFmpegEncoder::frames_done () const
185 boost::mutex::scoped_lock lm (_mutex);
186 return _last_time.frames_round (_film->video_frame_rate ());
189 FFmpegEncoder::FileEncoderSet::FileEncoderSet (
190 dcp::Size video_frame_size,
191 int video_frame_rate,
192 int audio_frame_rate,
197 boost::filesystem::path output,
202 /// TRANSLATORS: L here is an abbreviation for "left", to indicate the left-eye part of a 3D export
203 _encoders[EYES_LEFT] = shared_ptr<FFmpegFileEncoder>(
204 new FFmpegFileEncoder(video_frame_size, video_frame_rate, audio_frame_rate, channels, format, x264_crf, String::compose("%1_%2%3", output.string(), _("L"), extension))
206 /// TRANSLATORS: R here is an abbreviation for "left", to indicate the left-eye part of a 3D export
207 _encoders[EYES_RIGHT] = shared_ptr<FFmpegFileEncoder>(
208 new FFmpegFileEncoder(video_frame_size, video_frame_rate, audio_frame_rate, channels, format, x264_crf, String::compose("%1_%2%3", output.string(), _("R"), extension))
211 _encoders[EYES_BOTH] = shared_ptr<FFmpegFileEncoder>(
212 new FFmpegFileEncoder(video_frame_size, video_frame_rate, audio_frame_rate, channels, format, x264_crf, String::compose("%1%2", output.string(), extension))
217 shared_ptr<FFmpegFileEncoder>
218 FFmpegEncoder::FileEncoderSet::get (Eyes eyes) const
220 map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::const_iterator i = _encoders.find (eyes);
221 DCPOMATIC_ASSERT (i != _encoders.end());
226 FFmpegEncoder::FileEncoderSet::flush ()
228 for (map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::iterator i = _encoders.begin(); i != _encoders.end(); ++i) {
234 FFmpegEncoder::FileEncoderSet::audio (shared_ptr<AudioBuffers> a)
236 for (map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::iterator i = _encoders.begin(); i != _encoders.end(); ++i) {
237 i->second->audio (a);