f352f5a5246d4c48aa95fa6ba2b7233e4a46b449
[dcpomatic.git] / src / lib / encoder.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file src/encoder.h
21  *  @brief Parent class for classes which can encode video and audio frames.
22  */
23
24 #include <boost/filesystem.hpp>
25 #include "encoder.h"
26 #include "util.h"
27 #include "options.h"
28 #include "film.h"
29 #include "log.h"
30 #include "exceptions.h"
31
32 using std::pair;
33 using std::stringstream;
34 using std::vector;
35 using namespace boost;
36
37 int const Encoder::_history_size = 25;
38
39 /** @param f Film that we are encoding.
40  *  @param o Options.
41  */
42 Encoder::Encoder (shared_ptr<const Film> f, shared_ptr<const EncodeOptions> o)
43         : _film (f)
44         , _opt (o)
45         , _just_skipped (false)
46         , _video_frame (0)
47         , _audio_frame (0)
48 #ifdef HAVE_SWRESAMPLE    
49         , _swr_context (0)
50 #endif    
51         , _audio_frames_written (0)
52 {
53         if (_film->audio_stream()) {
54                 /* Create sound output files with .tmp suffixes; we will rename
55                    them if and when we complete.
56                 */
57                 for (int i = 0; i < _film->audio_channels(); ++i) {
58                         SF_INFO sf_info;
59                         sf_info.samplerate = dcp_audio_sample_rate (_film->audio_stream()->sample_rate());
60                         /* We write mono files */
61                         sf_info.channels = 1;
62                         sf_info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_24;
63                         SNDFILE* f = sf_open (_opt->multichannel_audio_out_path (i, true).c_str (), SFM_WRITE, &sf_info);
64                         if (f == 0) {
65                                 throw CreateFileError (_opt->multichannel_audio_out_path (i, true));
66                         }
67                         _sound_files.push_back (f);
68                 }
69         }
70 }
71
72 Encoder::~Encoder ()
73 {
74         close_sound_files ();
75 }
76
77 void
78 Encoder::process_begin ()
79 {
80         if (_film->audio_stream() && _film->audio_stream()->sample_rate() != _film->target_audio_sample_rate()) {
81 #ifdef HAVE_SWRESAMPLE
82
83                 stringstream s;
84                 s << "Will resample audio from " << _film->audio_stream()->sample_rate() << " to " << _film->target_audio_sample_rate();
85                 _film->log()->log (s.str ());
86
87                 /* We will be using planar float data when we call the resampler */
88                 _swr_context = swr_alloc_set_opts (
89                         0,
90                         _film->audio_stream()->channel_layout(),
91                         AV_SAMPLE_FMT_FLTP,
92                         _film->target_audio_sample_rate(),
93                         _film->audio_stream()->channel_layout(),
94                         AV_SAMPLE_FMT_FLTP,
95                         _film->audio_stream()->sample_rate(),
96                         0, 0
97                         );
98                 
99                 swr_init (_swr_context);
100 #else
101                 throw EncodeError ("Cannot resample audio as libswresample is not present");
102 #endif
103         } else {
104 #ifdef HAVE_SWRESAMPLE
105                 _swr_context = 0;
106 #endif          
107         }
108 }
109
110
111 void
112 Encoder::process_end ()
113 {
114 #if HAVE_SWRESAMPLE     
115         if (_film->audio_stream() && _swr_context) {
116
117                 shared_ptr<AudioBuffers> out (new AudioBuffers (_film->audio_stream()->channels(), 256));
118                         
119                 while (1) {
120                         int const frames = swr_convert (_swr_context, (uint8_t **) out->data(), 256, 0, 0);
121
122                         if (frames < 0) {
123                                 throw EncodeError ("could not run sample-rate converter");
124                         }
125
126                         if (frames == 0) {
127                                 break;
128                         }
129
130                         out->set_frames (frames);
131                         write_audio (out);
132                 }
133
134                 swr_free (&_swr_context);
135         }
136 #endif
137
138         if (_film->audio_stream()) {
139                 close_sound_files ();
140                 
141                 /* Rename .wav.tmp files to .wav */
142                 for (int i = 0; i < _film->audio_channels(); ++i) {
143                         if (boost::filesystem::exists (_opt->multichannel_audio_out_path (i, false))) {
144                                 boost::filesystem::remove (_opt->multichannel_audio_out_path (i, false));
145                         }
146                         boost::filesystem::rename (_opt->multichannel_audio_out_path (i, true), _opt->multichannel_audio_out_path (i, false));
147                 }
148         }
149 }       
150
151 /** @return an estimate of the current number of frames we are encoding per second,
152  *  or 0 if not known.
153  */
154 float
155 Encoder::current_frames_per_second () const
156 {
157         boost::mutex::scoped_lock lock (_history_mutex);
158         if (int (_time_history.size()) < _history_size) {
159                 return 0;
160         }
161
162         struct timeval now;
163         gettimeofday (&now, 0);
164
165         return _history_size / (seconds (now) - seconds (_time_history.back ()));
166 }
167
168 /** @return true if the last frame to be processed was skipped as it already existed */
169 bool
170 Encoder::skipping () const
171 {
172         boost::mutex::scoped_lock (_history_mutex);
173         return _just_skipped;
174 }
175
176 /** @return Number of video frames that have been received */
177 SourceFrame
178 Encoder::video_frame () const
179 {
180         boost::mutex::scoped_lock (_history_mutex);
181         return _video_frame;
182 }
183
184 /** Should be called when a frame has been encoded successfully.
185  *  @param n Source frame index.
186  */
187 void
188 Encoder::frame_done ()
189 {
190         boost::mutex::scoped_lock lock (_history_mutex);
191         _just_skipped = false;
192         
193         struct timeval tv;
194         gettimeofday (&tv, 0);
195         _time_history.push_front (tv);
196         if (int (_time_history.size()) > _history_size) {
197                 _time_history.pop_back ();
198         }
199 }
200
201 /** Called by a subclass when it has just skipped the processing
202     of a frame because it has already been done.
203 */
204 void
205 Encoder::frame_skipped ()
206 {
207         boost::mutex::scoped_lock lock (_history_mutex);
208         _just_skipped = true;
209 }
210
211 void
212 Encoder::process_video (shared_ptr<Image> i, boost::shared_ptr<Subtitle> s)
213 {
214         if (_opt->video_skip != 0 && (_video_frame % _opt->video_skip) != 0) {
215                 ++_video_frame;
216                 return;
217         }
218
219         if (_opt->video_range) {
220                 pair<SourceFrame, SourceFrame> const r = _opt->video_range.get();
221                 if (_video_frame < r.first || _video_frame >= r.second) {
222                         ++_video_frame;
223                         return;
224                 }
225         }
226
227         do_process_video (i, s);
228         ++_video_frame;
229 }
230
231 void
232 Encoder::process_audio (shared_ptr<AudioBuffers> data)
233 {
234         if (_opt->audio_range) {
235
236                 shared_ptr<AudioBuffers> trimmed (new AudioBuffers (*data.get ()));
237                 
238                 /* Range that we are encoding */
239                 pair<int64_t, int64_t> required_range = _opt->audio_range.get();
240                 /* Range of this block of data */
241                 pair<int64_t, int64_t> this_range (_audio_frame, _audio_frame + trimmed->frames());
242
243                 if (this_range.second < required_range.first || required_range.second < this_range.first) {
244                         /* No part of this audio is within the required range */
245                         return;
246                 } else if (required_range.first >= this_range.first && required_range.first < this_range.second) {
247                         /* Trim start */
248                         int64_t const shift = required_range.first - this_range.first;
249                         trimmed->move (shift, 0, trimmed->frames() - shift);
250                         trimmed->set_frames (trimmed->frames() - shift);
251                 } else if (required_range.second >= this_range.first && required_range.second < this_range.second) {
252                         /* Trim end */
253                         trimmed->set_frames (required_range.second - this_range.first);
254                 }
255
256                 data = trimmed;
257         }
258
259 #if HAVE_SWRESAMPLE
260         /* Maybe sample-rate convert */
261         if (_swr_context) {
262
263                 /* Compute the resampled frames count and add 32 for luck */
264                 int const max_resampled_frames = ceil ((int64_t) data->frames() * _film->target_audio_sample_rate() / _film->audio_stream()->sample_rate()) + 32;
265
266                 shared_ptr<AudioBuffers> resampled (new AudioBuffers (_film->audio_stream()->channels(), max_resampled_frames));
267
268                 /* Resample audio */
269                 int const resampled_frames = swr_convert (
270                         _swr_context, (uint8_t **) resampled->data(), max_resampled_frames, (uint8_t const **) data->data(), data->frames()
271                         );
272                 
273                 if (resampled_frames < 0) {
274                         throw EncodeError ("could not run sample-rate converter");
275                 }
276
277                 resampled->set_frames (resampled_frames);
278                 
279                 /* And point our variables at the resampled audio */
280                 data = resampled;
281         }
282 #endif
283
284         write_audio (data);
285         
286         _audio_frame += data->frames ();
287 }
288
289 void
290 Encoder::write_audio (shared_ptr<const AudioBuffers> audio)
291 {
292         for (int i = 0; i < _film->audio_channels(); ++i) {
293                 sf_write_float (_sound_files[i], audio->data(i), audio->frames());
294         }
295
296         _audio_frames_written += audio->frames ();
297 }
298
299 void
300 Encoder::close_sound_files ()
301 {
302         for (vector<SNDFILE*>::iterator i = _sound_files.begin(); i != _sound_files.end(); ++i) {
303                 sf_close (*i);
304         }
305
306         _sound_files.clear ();
307 }       
308