Fix crash on exporting multi-reel to a single file (#1388).
[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 boost::shared_ptr;
41 using boost::bind;
42 using boost::weak_ptr;
43
44 FFmpegEncoder::FFmpegEncoder (
45         shared_ptr<const Film> film,
46         weak_ptr<Job> job,
47         boost::filesystem::path output,
48         ExportFormat format,
49         bool mixdown_to_stereo,
50         bool split_reels,
51         int x264_crf
52         )
53         : Encoder (film, job)
54         , _history (1000)
55 {
56         int const files = split_reels ? film->reels().size() : 1;
57         for (int i = 0; i < files; ++i) {
58
59                 boost::filesystem::path filename = output;
60                 if (files > 1) {
61                         string extension = boost::filesystem::extension (filename);
62                         filename = boost::filesystem::change_extension (filename, "");
63                         /// TRANSLATORS: _reel%1.%2 here is to be added to an export filename to indicate
64                         /// which reel it is.  Preserve the %1 and %2; %1 will be replaced with the reel number
65                         /// and %2 with the file extension.
66                         filename = filename.string() + String::compose(_("_reel%1%2"), i + 1, extension);
67                 }
68
69                 _file_encoders.push_back (
70                         shared_ptr<FFmpegFileEncoder>(
71                                 new FFmpegFileEncoder(
72                                         _film->frame_size(),
73                                         _film->video_frame_rate(),
74                                         _film->audio_frame_rate(),
75                                         mixdown_to_stereo ? 2 : film->audio_channels(),
76                                         _film->log(),
77                                         format,
78                                         x264_crf,
79                                         filename
80                                         )
81                                 )
82                         );
83         }
84
85         _player->set_always_burn_open_subtitles ();
86         _player->set_play_referenced ();
87
88         int const ch = film->audio_channels ();
89
90         AudioMapping map;
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);
96                 map.set (dcp::LEFT,   0, overall_gain);
97                 map.set (dcp::RIGHT,  1, overall_gain);
98                 map.set (dcp::CENTRE, 0, overall_gain * minus_3dB);
99                 map.set (dcp::CENTRE, 1, overall_gain * minus_3dB);
100                 map.set (dcp::LS,     0, overall_gain);
101                 map.set (dcp::RS,     1, overall_gain);
102         } else {
103                 _output_audio_channels = ch;
104                 map = AudioMapping (ch, ch);
105                 for (int i = 0; i < ch; ++i) {
106                         map.set (i, i, 1);
107                 }
108         }
109
110         _butler.reset (new Butler (_player, film->log(), map, _output_audio_channels));
111 }
112
113 void
114 FFmpegEncoder::go ()
115 {
116         {
117                 shared_ptr<Job> job = _job.lock ();
118                 DCPOMATIC_ASSERT (job);
119                 job->sub (_("Encoding"));
120         }
121
122         list<DCPTimePeriod> reel_periods = _film->reels ();
123         list<DCPTimePeriod>::const_iterator reel = reel_periods.begin ();
124         list<shared_ptr<FFmpegFileEncoder> >::iterator encoder = _file_encoders.begin ();
125
126         DCPTime const video_frame = DCPTime::from_frames (1, _film->video_frame_rate ());
127         int const audio_frames = video_frame.frames_round(_film->audio_frame_rate());
128         float* interleaved = new float[_output_audio_channels * audio_frames];
129         shared_ptr<AudioBuffers> deinterleaved (new AudioBuffers (_output_audio_channels, audio_frames));
130         for (DCPTime i; i < _film->length(); i += video_frame) {
131
132                 if (_file_encoders.size() > 1 && !reel->contains(i)) {
133                         /* Next reel and file */
134                         ++reel;
135                         ++encoder;
136                         DCPOMATIC_ASSERT (reel != reel_periods.end());
137                         DCPOMATIC_ASSERT (encoder != _file_encoders.end());
138                 }
139
140                 pair<shared_ptr<PlayerVideo>, DCPTime> v = _butler->get_video ();
141                 (*encoder)->video (v.first, v.second);
142
143                 _history.event ();
144
145                 {
146                         boost::mutex::scoped_lock lm (_mutex);
147                         _last_time = i;
148                 }
149
150                 shared_ptr<Job> job = _job.lock ();
151                 if (job) {
152                         job->set_progress (float(i.get()) / _film->length().get());
153                 }
154
155                 _butler->get_audio (interleaved, audio_frames);
156                 /* XXX: inefficient; butler interleaves and we deinterleave again */
157                 float* p = interleaved;
158                 for (int j = 0; j < audio_frames; ++j) {
159                         for (int k = 0; k < _output_audio_channels; ++k) {
160                                 deinterleaved->data(k)[j] = *p++;
161                         }
162                 }
163                 (*encoder)->audio (deinterleaved);
164         }
165         delete[] interleaved;
166
167         BOOST_FOREACH (shared_ptr<FFmpegFileEncoder> i, _file_encoders) {
168                 i->flush ();
169         }
170 }
171
172 float
173 FFmpegEncoder::current_rate () const
174 {
175         return _history.rate ();
176 }
177
178 Frame
179 FFmpegEncoder::frames_done () const
180 {
181         boost::mutex::scoped_lock lm (_mutex);
182         return _last_time.frames_round (_film->video_frame_rate ());
183 }