Fix comment.
[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 using namespace dcpomatic;
45
46 FFmpegEncoder::FFmpegEncoder (
47         shared_ptr<const Film> film,
48         weak_ptr<Job> job,
49         boost::filesystem::path output,
50         ExportFormat format,
51         bool mixdown_to_stereo,
52         bool split_reels,
53         int x264_crf
54         )
55         : Encoder (film, job)
56         , _history (1000)
57 {
58         int const files = split_reels ? film->reels().size() : 1;
59         for (int i = 0; i < files; ++i) {
60
61                 boost::filesystem::path filename = output;
62                 string extension = boost::filesystem::extension (filename);
63                 filename = boost::filesystem::change_extension (filename, "");
64
65                 if (files > 1) {
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);
69                 }
70
71                 _file_encoders.push_back (
72                         FileEncoderSet (
73                                 _film->frame_size(),
74                                 _film->video_frame_rate(),
75                                 _film->audio_frame_rate(),
76                                 mixdown_to_stereo ? 2 : film->audio_channels(),
77                                 format,
78                                 x264_crf,
79                                 _film->three_d(),
80                                 filename,
81                                 extension
82                                 )
83                         );
84         }
85
86         _player->set_always_burn_open_subtitles ();
87         _player->set_play_referenced ();
88
89         int const ch = film->audio_channels ();
90
91         AudioMapping map;
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);
103         } else {
104                 _output_audio_channels = ch;
105                 map = AudioMapping (ch, ch);
106                 for (int i = 0; i < ch; ++i) {
107                         map.set (i, i, 1);
108                 }
109         }
110
111         _butler.reset (new Butler(_player, map, _output_audio_channels, bind(&PlayerVideo::force, _1, FFmpegFileEncoder::pixel_format(format)), true, false));
112 }
113
114 void
115 FFmpegEncoder::go ()
116 {
117         {
118                 shared_ptr<Job> job = _job.lock ();
119                 DCPOMATIC_ASSERT (job);
120                 job->sub (_("Encoding"));
121         }
122
123         list<DCPTimePeriod> reel_periods = _film->reels ();
124         list<DCPTimePeriod>::const_iterator reel = reel_periods.begin ();
125         list<FileEncoderSet>::iterator encoder = _file_encoders.begin ();
126
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) {
133
134                 if (_file_encoders.size() > 1 && !reel->contains(i)) {
135                         /* Next reel and file */
136                         ++reel;
137                         ++encoder;
138                         DCPOMATIC_ASSERT (reel != reel_periods.end());
139                         DCPOMATIC_ASSERT (encoder != _file_encoders.end());
140                 }
141
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);
145                 }
146
147                 _history.event ();
148
149                 {
150                         boost::mutex::scoped_lock lm (_mutex);
151                         _last_time = i;
152                 }
153
154                 shared_ptr<Job> job = _job.lock ();
155                 if (job) {
156                         job->set_progress (float(i.get()) / _film->length().get());
157                 }
158
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++;
165                         }
166                 }
167                 encoder->audio (deinterleaved);
168         }
169         delete[] interleaved;
170
171         BOOST_FOREACH (FileEncoderSet i, _file_encoders) {
172                 i.flush ();
173         }
174 }
175
176 float
177 FFmpegEncoder::current_rate () const
178 {
179         return _history.rate ();
180 }
181
182 Frame
183 FFmpegEncoder::frames_done () const
184 {
185         boost::mutex::scoped_lock lm (_mutex);
186         return _last_time.frames_round (_film->video_frame_rate ());
187 }
188
189 FFmpegEncoder::FileEncoderSet::FileEncoderSet (
190         dcp::Size video_frame_size,
191         int video_frame_rate,
192         int audio_frame_rate,
193         int channels,
194         ExportFormat format,
195         int x264_crf,
196         bool three_d,
197         boost::filesystem::path output,
198         string extension
199         )
200 {
201         if (three_d) {
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))
205                         );
206                 /// TRANSLATORS: R here is an abbreviation for "right", to indicate the right-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))
209                         );
210         } else {
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))
213                         );
214         }
215 }
216
217 shared_ptr<FFmpegFileEncoder>
218 FFmpegEncoder::FileEncoderSet::get (Eyes eyes) const
219 {
220         map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::const_iterator i = _encoders.find (eyes);
221         DCPOMATIC_ASSERT (i != _encoders.end());
222         return i->second;
223 }
224
225 void
226 FFmpegEncoder::FileEncoderSet::flush ()
227 {
228         for (map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::iterator i = _encoders.begin(); i != _encoders.end(); ++i) {
229                 i->second->flush ();
230         }
231 }
232
233 void
234 FFmpegEncoder::FileEncoderSet::audio (shared_ptr<AudioBuffers> a)
235 {
236         for (map<Eyes, boost::shared_ptr<FFmpegFileEncoder> >::iterator i = _encoders.begin(); i != _encoders.end(); ++i) {
237                 i->second->audio (a);
238         }
239 }