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;
45 FFmpegEncoder::FFmpegEncoder (
46 shared_ptr<const Film> film,
48 boost::filesystem::path output,
50 bool mixdown_to_stereo,
57 int const files = split_reels ? film->reels().size() : 1;
58 for (int i = 0; i < files; ++i) {
60 boost::filesystem::path filename = output;
61 string extension = boost::filesystem::extension (filename);
62 filename = boost::filesystem::change_extension (filename, "");
65 /// TRANSLATORS: _reel%1 here is to be added to an export filename to indicate
66 /// which reel it is. Preserve the %1; it will be replaced with the reel number.
67 filename = filename.string() + String::compose(_("_reel%1"), i + 1);
70 _file_encoders.push_back (
73 _film->video_frame_rate(),
74 _film->audio_frame_rate(),
75 mixdown_to_stereo ? 2 : film->audio_channels(),
85 _player->set_always_burn_open_subtitles ();
86 _player->set_play_referenced ();
88 int const ch = film->audio_channels ();
91 if (mixdown_to_stereo) {
92 _output_audio_channels = 2;
93 map = AudioMapping (ch, 2);
94 float const overall_gain = 2 / (4 + sqrt(2));
95 float const minus_3dB = 1 / sqrt(2);
97 map.set (dcp::LEFT, 0, 1);
98 map.set (dcp::RIGHT, 1, 1);
100 map.set (dcp::LEFT, 0, overall_gain);
101 map.set (dcp::RIGHT, 1, overall_gain);
102 map.set (dcp::CENTRE, 0, overall_gain * minus_3dB);
103 map.set (dcp::CENTRE, 1, overall_gain * minus_3dB);
104 map.set (dcp::LS, 0, overall_gain);
105 } else if (ch >= 6) {
106 map.set (dcp::LEFT, 0, overall_gain);
107 map.set (dcp::RIGHT, 1, overall_gain);
108 map.set (dcp::CENTRE, 0, overall_gain * minus_3dB);
109 map.set (dcp::CENTRE, 1, overall_gain * minus_3dB);
110 map.set (dcp::LS, 0, overall_gain);
111 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"));
135 list<DCPTimePeriod> reel_periods = _film->reels ();
136 list<DCPTimePeriod>::const_iterator reel = reel_periods.begin ();
137 list<FileEncoderSet>::iterator encoder = _file_encoders.begin ();
139 DCPTime const video_frame = DCPTime::from_frames (1, _film->video_frame_rate ());
140 int const audio_frames = video_frame.frames_round(_film->audio_frame_rate());
141 float* interleaved = new float[_output_audio_channels * audio_frames];
142 shared_ptr<AudioBuffers> deinterleaved (new AudioBuffers (_output_audio_channels, audio_frames));
143 int const gets_per_frame = _film->three_d() ? 2 : 1;
144 for (DCPTime i; i < _film->length(); i += video_frame) {
146 if (_file_encoders.size() > 1 && !reel->contains(i)) {
147 /* Next reel and file */
150 DCPOMATIC_ASSERT (reel != reel_periods.end());
151 DCPOMATIC_ASSERT (encoder != _file_encoders.end());
154 for (int j = 0; j < gets_per_frame; ++j) {
156 pair<shared_ptr<PlayerVideo>, DCPTime> v = _butler->get_video (&e);
158 throw ProgrammingError(__FILE__, __LINE__, String::compose("butler returned no video; error was %1", static_cast<int>(e)));
160 shared_ptr<FFmpegFileEncoder> fe = encoder->get (v.first->eyes());
162 fe->video(v.first, v.second);
169 boost::mutex::scoped_lock lm (_mutex);
173 shared_ptr<Job> job = _job.lock ();
175 job->set_progress (float(i.get()) / _film->length().get());
180 _butler->get_audio (interleaved, audio_frames);
181 /* XXX: inefficient; butler interleaves and we deinterleave again */
182 float* p = interleaved;
183 for (int j = 0; j < audio_frames; ++j) {
184 for (int k = 0; k < _output_audio_channels; ++k) {
185 deinterleaved->data(k)[j] = *p++;
188 encoder->audio (deinterleaved);
190 delete[] interleaved;
192 BOOST_FOREACH (FileEncoderSet i, _file_encoders) {
200 FFmpegEncoder::current_rate () const
202 return _history.rate ();
206 FFmpegEncoder::frames_done () const
208 boost::mutex::scoped_lock lm (_mutex);
209 return _last_time.frames_round (_film->video_frame_rate ());
212 FFmpegEncoder::FileEncoderSet::FileEncoderSet (
213 dcp::Size video_frame_size,
214 int video_frame_rate,
215 int audio_frame_rate,
220 boost::filesystem::path output,
225 /// TRANSLATORS: L here is an abbreviation for "left", to indicate the left-eye part of a 3D export
226 _encoders[EYES_LEFT] = shared_ptr<FFmpegFileEncoder>(
227 new FFmpegFileEncoder(video_frame_size, video_frame_rate, audio_frame_rate, channels, format, x264_crf, String::compose("%1_%2%3", output.string(), _("L"), extension))
229 /// TRANSLATORS: R here is an abbreviation for "left", to indicate the left-eye part of a 3D export
230 _encoders[EYES_RIGHT] = shared_ptr<FFmpegFileEncoder>(
231 new FFmpegFileEncoder(video_frame_size, video_frame_rate, audio_frame_rate, channels, format, x264_crf, String::compose("%1_%2%3", output.string(), _("R"), extension))
234 _encoders[EYES_BOTH] = shared_ptr<FFmpegFileEncoder>(
235 new FFmpegFileEncoder(video_frame_size, video_frame_rate, audio_frame_rate, channels, format, x264_crf, String::compose("%1%2", output.string(), extension))
240 shared_ptr<FFmpegFileEncoder>
241 FFmpegEncoder::FileEncoderSet::get (Eyes eyes) const
243 if (_encoders.size() == 1) {
244 /* We are doing a 2D export... */
245 if (eyes == EYES_LEFT) {
246 /* ...but we got some 3D data; put the left eye into the output... */
248 } else if (eyes == EYES_RIGHT) {
249 /* ...and ignore the right eye.*/
250 return shared_ptr<FFmpegFileEncoder>();
254 map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::const_iterator i = _encoders.find (eyes);
255 DCPOMATIC_ASSERT (i != _encoders.end());
260 FFmpegEncoder::FileEncoderSet::flush ()
262 for (map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::iterator i = _encoders.begin(); i != _encoders.end(); ++i) {
268 FFmpegEncoder::FileEncoderSet::audio (shared_ptr<AudioBuffers> a)
270 for (map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::iterator i = _encoders.begin(); i != _encoders.end(); ++i) {
271 i->second->audio (a);