Missed update to private test repo version.
[dcpomatic.git] / src / lib / ffmpeg_encoder.cc
1 /*
2     Copyright (C) 2017-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "ffmpeg_encoder.h"
22 #include "film.h"
23 #include "job.h"
24 #include "player.h"
25 #include "player_video.h"
26 #include "log.h"
27 #include "image.h"
28 #include "cross.h"
29 #include "butler.h"
30 #include "compose.hpp"
31 #include <iostream>
32
33 #include "i18n.h"
34
35 using std::string;
36 using std::runtime_error;
37 using std::cout;
38 using std::pair;
39 using std::list;
40 using std::map;
41 using boost::shared_ptr;
42 using boost::bind;
43 using boost::weak_ptr;
44 #if BOOST_VERSION >= 106100
45 using namespace boost::placeholders;
46 #endif
47
48 FFmpegEncoder::FFmpegEncoder (
49         shared_ptr<const Film> film,
50         weak_ptr<Job> job,
51         boost::filesystem::path output,
52         ExportFormat format,
53         bool mixdown_to_stereo,
54         bool split_reels,
55         int x264_crf
56         )
57         : Encoder (film, job)
58         , _history (1000)
59 {
60         _player->set_always_burn_open_subtitles ();
61         _player->set_play_referenced ();
62
63         int const ch = film->audio_channels ();
64
65         AudioMapping map;
66         if (mixdown_to_stereo) {
67                 _output_audio_channels = 2;
68                 map = AudioMapping (ch, 2);
69                 float const overall_gain = 2 / (4 + sqrt(2));
70                 float const minus_3dB = 1 / sqrt(2);
71                 if (ch == 2) {
72                         map.set (dcp::LEFT, 0, 1);
73                         map.set (dcp::RIGHT, 1, 1);
74                 } else if (ch == 4) {
75                         map.set (dcp::LEFT,   0, overall_gain);
76                         map.set (dcp::RIGHT,  1, overall_gain);
77                         map.set (dcp::CENTRE, 0, overall_gain * minus_3dB);
78                         map.set (dcp::CENTRE, 1, overall_gain * minus_3dB);
79                         map.set (dcp::LS,     0, overall_gain);
80                 } else if (ch >= 6) {
81                         map.set (dcp::LEFT,   0, overall_gain);
82                         map.set (dcp::RIGHT,  1, overall_gain);
83                         map.set (dcp::CENTRE, 0, overall_gain * minus_3dB);
84                         map.set (dcp::CENTRE, 1, overall_gain * minus_3dB);
85                         map.set (dcp::LS,     0, overall_gain);
86                         map.set (dcp::RS,     1, overall_gain);
87                 }
88         } else {
89                 _output_audio_channels = ch > 8 ? 16 : ch;
90                 map = AudioMapping (ch, _output_audio_channels);
91                 for (int i = 0; i < ch; ++i) {
92                         map.set (i, i, 1);
93                 }
94         }
95
96         _butler.reset (new Butler(_player, map, _output_audio_channels, bind(&PlayerVideo::force, _1, FFmpegFileEncoder::pixel_format(format)), true, false));
97
98         int const files = split_reels ? film->reels().size() : 1;
99         for (int i = 0; i < files; ++i) {
100
101                 boost::filesystem::path filename = output;
102                 string extension = boost::filesystem::extension (filename);
103                 filename = boost::filesystem::change_extension (filename, "");
104
105                 if (files > 1) {
106                         /// TRANSLATORS: _reel%1 here is to be added to an export filename to indicate
107                         /// which reel it is.  Preserve the %1; it will be replaced with the reel number.
108                         filename = filename.string() + String::compose(_("_reel%1"), i + 1);
109                 }
110
111                 _file_encoders.push_back (
112                         FileEncoderSet (
113                                 _film->frame_size(),
114                                 _film->video_frame_rate(),
115                                 _film->audio_frame_rate(),
116                                 _output_audio_channels,
117                                 format,
118                                 x264_crf,
119                                 _film->three_d(),
120                                 filename,
121                                 extension
122                                 )
123                         );
124         }
125 }
126
127 void
128 FFmpegEncoder::go ()
129 {
130         {
131                 shared_ptr<Job> job = _job.lock ();
132                 DCPOMATIC_ASSERT (job);
133                 job->sub (_("Encoding"));
134         }
135
136         Waker waker;
137
138         list<DCPTimePeriod> reel_periods = _film->reels ();
139         list<DCPTimePeriod>::const_iterator reel = reel_periods.begin ();
140         list<FileEncoderSet>::iterator encoder = _file_encoders.begin ();
141
142         DCPTime const video_frame = DCPTime::from_frames (1, _film->video_frame_rate ());
143         int const audio_frames = video_frame.frames_round(_film->audio_frame_rate());
144         float* interleaved = new float[_output_audio_channels * audio_frames];
145         shared_ptr<AudioBuffers> deinterleaved (new AudioBuffers (_output_audio_channels, audio_frames));
146         int const gets_per_frame = _film->three_d() ? 2 : 1;
147         for (DCPTime i; i < _film->length(); i += video_frame) {
148
149                 if (_file_encoders.size() > 1 && !reel->contains(i)) {
150                         /* Next reel and file */
151                         ++reel;
152                         ++encoder;
153                         DCPOMATIC_ASSERT (reel != reel_periods.end());
154                         DCPOMATIC_ASSERT (encoder != _file_encoders.end());
155                 }
156
157                 for (int j = 0; j < gets_per_frame; ++j) {
158                         Butler::Error e;
159                         pair<shared_ptr<PlayerVideo>, DCPTime> v = _butler->get_video (&e);
160                         if (!v.first) {
161                                 throw ProgrammingError(__FILE__, __LINE__, String::compose("butler returned no video; error was %1", static_cast<int>(e)));
162                         }
163                         shared_ptr<FFmpegFileEncoder> fe = encoder->get (v.first->eyes());
164                         if (fe) {
165                                 fe->video(v.first, v.second);
166                         }
167                 }
168
169                 _history.event ();
170
171                 {
172                         boost::mutex::scoped_lock lm (_mutex);
173                         _last_time = i;
174                 }
175
176                 shared_ptr<Job> job = _job.lock ();
177                 if (job) {
178                         job->set_progress (float(i.get()) / _film->length().get());
179                 }
180
181                 waker.nudge ();
182
183                 _butler->get_audio (interleaved, audio_frames);
184                 /* XXX: inefficient; butler interleaves and we deinterleave again */
185                 float* p = interleaved;
186                 for (int j = 0; j < audio_frames; ++j) {
187                         for (int k = 0; k < _output_audio_channels; ++k) {
188                                 deinterleaved->data(k)[j] = *p++;
189                         }
190                 }
191                 encoder->audio (deinterleaved);
192         }
193         delete[] interleaved;
194
195         BOOST_FOREACH (FileEncoderSet i, _file_encoders) {
196                 i.flush ();
197         }
198
199         _butler->rethrow ();
200 }
201
202 float
203 FFmpegEncoder::current_rate () const
204 {
205         return _history.rate ();
206 }
207
208 Frame
209 FFmpegEncoder::frames_done () const
210 {
211         boost::mutex::scoped_lock lm (_mutex);
212         return _last_time.frames_round (_film->video_frame_rate ());
213 }
214
215 FFmpegEncoder::FileEncoderSet::FileEncoderSet (
216         dcp::Size video_frame_size,
217         int video_frame_rate,
218         int audio_frame_rate,
219         int channels,
220         ExportFormat format,
221         int x264_crf,
222         bool three_d,
223         boost::filesystem::path output,
224         string extension
225         )
226 {
227         if (three_d) {
228                 /// TRANSLATORS: L here is an abbreviation for "left", to indicate the left-eye part of a 3D export
229                 _encoders[EYES_LEFT] = shared_ptr<FFmpegFileEncoder>(
230                         new FFmpegFileEncoder(video_frame_size, video_frame_rate, audio_frame_rate, channels, format, x264_crf, String::compose("%1_%2%3", output.string(), _("L"), extension))
231                         );
232                 /// TRANSLATORS: R here is an abbreviation for "left", to indicate the left-eye part of a 3D export
233                 _encoders[EYES_RIGHT] = shared_ptr<FFmpegFileEncoder>(
234                         new FFmpegFileEncoder(video_frame_size, video_frame_rate, audio_frame_rate, channels, format, x264_crf, String::compose("%1_%2%3", output.string(), _("R"), extension))
235                         );
236         } else {
237                 _encoders[EYES_BOTH]  = shared_ptr<FFmpegFileEncoder>(
238                         new FFmpegFileEncoder(video_frame_size, video_frame_rate, audio_frame_rate, channels, format, x264_crf, String::compose("%1%2", output.string(), extension))
239                         );
240         }
241 }
242
243 shared_ptr<FFmpegFileEncoder>
244 FFmpegEncoder::FileEncoderSet::get (Eyes eyes) const
245 {
246         if (_encoders.size() == 1) {
247                 /* We are doing a 2D export... */
248                 if (eyes == EYES_LEFT) {
249                         /* ...but we got some 3D data; put the left eye into the output... */
250                         eyes = EYES_BOTH;
251                 } else if (eyes == EYES_RIGHT) {
252                         /* ...and ignore the right eye.*/
253                         return shared_ptr<FFmpegFileEncoder>();
254                 }
255         }
256
257         map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::const_iterator i = _encoders.find (eyes);
258         DCPOMATIC_ASSERT (i != _encoders.end());
259         return i->second;
260 }
261
262 void
263 FFmpegEncoder::FileEncoderSet::flush ()
264 {
265         for (map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::iterator i = _encoders.begin(); i != _encoders.end(); ++i) {
266                 i->second->flush ();
267         }
268 }
269
270 void
271 FFmpegEncoder::FileEncoderSet::audio (shared_ptr<AudioBuffers> a)
272 {
273         for (map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::iterator i = _encoders.begin(); i != _encoders.end(); ++i) {
274                 i->second->audio (a);
275         }
276 }