#ifdef-y hacks to build with Ubuntu 12.04's ffmpeg.
[dcpomatic.git] / src / lib / decoder.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/decoder.cc
21  *  @brief Parent class for decoders of content.
22  */
23
24 #include <iostream>
25 #include <stdint.h>
26 extern "C" {
27 #include <libavfilter/avfiltergraph.h>
28 #include <libavfilter/vsrc_buffer.h>
29 #include <libavformat/avio.h>
30 }
31 #include "film.h"
32 #include "format.h"
33 #include "job.h"
34 #include "film_state.h"
35 #include "options.h"
36 #include "exceptions.h"
37 #include "image.h"
38 #include "util.h"
39 #include "log.h"
40 #include "decoder.h"
41 #include "filter.h"
42 #include "delay_line.h"
43 #include "ffmpeg_compatibility.h"
44
45 using namespace std;
46 using namespace boost;
47
48 /** @param s FilmState of the Film.
49  *  @param o Options.
50  *  @param j Job that we are running within, or 0
51  *  @param l Log to use.
52  *  @param minimal true to do the bare minimum of work; just run through the content.  Useful for acquiring
53  *  accurate frame counts as quickly as possible.  This generates no video or audio output.
54  *  @param ignore_length Ignore the content's claimed length when computing progress.
55  */
56 Decoder::Decoder (boost::shared_ptr<const FilmState> s, boost::shared_ptr<const Options> o, Job* j, Log* l, bool minimal, bool ignore_length)
57         : _fs (s)
58         , _opt (o)
59         , _job (j)
60         , _log (l)
61         , _minimal (minimal)
62         , _ignore_length (ignore_length)
63         , _video_frame (0)
64         , _buffer_src_context (0)
65         , _buffer_sink_context (0)
66         , _have_setup_video_filters (false)
67         , _delay_line (0)
68         , _delay_in_bytes (0)
69 {
70         if (_opt->decode_video_frequency != 0 && _fs->length == 0) {
71                 throw DecodeError ("cannot do a partial decode if length == 0");
72         }
73 }
74
75 Decoder::~Decoder ()
76 {
77         delete _delay_line;
78 }
79
80 void
81 Decoder::process_begin ()
82 {
83         /* This assumes 2 bytes per sample */
84         _delay_in_bytes = _fs->audio_delay * _fs->audio_sample_rate * _fs->audio_channels * 2 / 1000;
85         delete _delay_line;
86         _delay_line = new DelayLine (_delay_in_bytes);
87 }
88
89 void
90 Decoder::process_end ()
91 {
92         if (_delay_in_bytes < 0) {
93                 uint8_t remainder[-_delay_in_bytes];
94                 _delay_line->get_remaining (remainder);
95                 Audio (remainder, _delay_in_bytes);
96         }
97 }
98
99 /** Start decoding */
100 void
101 Decoder::go ()
102 {
103         process_begin ();
104
105         if (_job && _ignore_length) {
106                 _job->set_progress_unknown ();
107         }
108
109         while (pass () == false) {
110                 if (_job && !_ignore_length) {
111                         _job->set_progress (float (_video_frame) / decoding_frames ());
112                 }
113         }
114
115         process_end ();
116 }
117
118 /** @return Number of frames that we will be decoding */
119 int
120 Decoder::decoding_frames () const
121 {
122         if (_opt->num_frames > 0) {
123                 return _opt->num_frames;
124         }
125         
126         return _fs->length;
127 }
128
129 /** Run one pass.  This may or may not generate any actual video / audio data;
130  *  some decoders may require several passes to generate a single frame.
131  *  @return true if we have finished processing all data; otherwise false.
132  */
133 bool
134 Decoder::pass ()
135 {
136         if (!_have_setup_video_filters) {
137                 setup_video_filters ();
138                 _have_setup_video_filters = true;
139         }
140         
141         if (_opt->num_frames != 0 && _video_frame >= _opt->num_frames) {
142                 return true;
143         }
144
145         return do_pass ();
146 }
147
148 /** Called by subclasses to tell the world that some audio data is ready */
149 void
150 Decoder::process_audio (uint8_t* data, int channels, int size)
151 {
152         if (_fs->audio_gain != 0) {
153                 float const linear_gain = pow (10, _fs->audio_gain / 20);
154                 uint8_t* p = data;
155                 int const samples = size / 2;
156                 switch (_fs->audio_sample_format) {
157                 case AV_SAMPLE_FMT_S16:
158                         for (int i = 0; i < samples; ++i) {
159                                 /* XXX: assumes little-endian; also we should probably be dithering here */
160                                 int const ou = p[0] | (p[1] << 8);
161                                 int const os = ou >= 0x8000 ? (- 0x10000 + ou) : ou;
162                                 int const gs = int (os * linear_gain);
163                                 int const gu = gs > 0 ? gs : (0x10000 + gs);
164                                 p[0] = gu & 0xff;
165                                 p[1] = (gu & 0xff00) >> 8;
166                                 p += 2;
167                         }
168                         break;
169                 default:
170                         assert (false);
171                 }
172         }
173
174         int available = _delay_line->feed (data, size);
175         Audio (data, available);
176 }
177
178 /** Called by subclasses to tell the world that some video data is ready.
179  *  We do some post-processing / filtering then emit it for listeners.
180  *  @param frame to decode; caller manages memory.
181  */
182 void
183 Decoder::process_video (AVFrame* frame)
184 {
185         if (_minimal) {
186                 ++_video_frame;
187                 return;
188         }
189
190         /* Use FilmState::length here as our one may be wrong */
191
192         int gap = 0;
193         if (_opt->decode_video_frequency != 0) {
194                 gap = _fs->length / _opt->decode_video_frequency;
195         }
196
197         if (_opt->decode_video_frequency != 0 && gap != 0 && (_video_frame % gap) != 0) {
198                 ++_video_frame;
199                 return;
200         }
201
202 #ifdef DVDOMATIC_FFMPEG_0_8_3
203         
204         AVRational par;
205         par.num = sample_aspect_ratio_numerator ();
206         par.den = sample_aspect_ratio_denominator ();
207
208         if (av_vsrc_buffer_add_frame (_buffer_src_context, frame, 0, par) < 0) {
209                 throw DecodeError ("could not push buffer into filter chain.");
210         }
211
212 #else
213
214         if (av_vsrc_buffer_add_frame (_buffer_src_context, frame, 0) < 0) {
215                 throw DecodeError ("could not push buffer into filter chain.");
216         }
217
218 #endif  
219         
220         while (avfilter_poll_frame (_buffer_sink_context->inputs[0])) {
221
222 #ifdef DVDOMATIC_FFMPEG_0_8_3
223
224                 int r = avfilter_request_frame (_buffer_sink_context->inputs[0]);
225                 if (r < 0) {
226                         throw DecodeError ("could not request filtered frame");
227                 }
228                 
229                 AVFilterBufferRef* filter_buffer = _buffer_sink_context->inputs[0]->cur_buf;
230                 
231 #else
232
233                 AVFilterBufferRef* filter_buffer;
234                 if (avbuffersink_get_buffer_ref (_buffer_sink_context, &filter_buffer, 0) < 0) {
235                         filter_buffer = 0;
236                 }
237
238 #endif          
239                 
240                 if (filter_buffer) {
241                         /* This takes ownership of filter_buffer */
242                         shared_ptr<Image> image (new FilterBufferImage ((PixelFormat) frame->format, filter_buffer));
243
244                         if (_opt->black_after > 0 && _video_frame > _opt->black_after) {
245                                 image->make_black ();
246                         }
247
248                         Video (image, _video_frame);
249                         ++_video_frame;
250                 }
251         }
252 }
253
254 void
255 Decoder::setup_video_filters ()
256 {
257         stringstream fs;
258         Size size_after_crop;
259         
260         if (_opt->apply_crop) {
261                 size_after_crop = _fs->cropped_size (native_size ());
262                 fs << crop_string (Position (_fs->left_crop, _fs->top_crop), size_after_crop);
263         } else {
264                 size_after_crop = native_size ();
265                 fs << crop_string (Position (0, 0), size_after_crop);
266         }
267
268         string filters = Filter::ffmpeg_strings (_fs->filters).first;
269         if (!filters.empty ()) {
270                 filters += ",";
271         }
272
273         filters += fs.str ();
274
275         avfilter_register_all ();
276         
277         AVFilterGraph* graph = avfilter_graph_alloc();
278         if (graph == 0) {
279                 throw DecodeError ("Could not create filter graph.");
280         }
281
282         AVFilter* buffer_src = avfilter_get_by_name("buffer");
283         if (buffer_src == 0) {
284                 throw DecodeError ("Could not find buffer src filter");
285         }
286
287         AVFilter* buffer_sink = get_sink ();
288
289         stringstream a;
290         a << native_size().width << ":"
291           << native_size().height << ":"
292           << pixel_format() << ":"
293           << time_base_numerator() << ":"
294           << time_base_denominator() << ":"
295           << sample_aspect_ratio_numerator() << ":"
296           << sample_aspect_ratio_denominator();
297
298         int r;
299         if ((r = avfilter_graph_create_filter (&_buffer_src_context, buffer_src, "in", a.str().c_str(), 0, graph)) < 0) {
300                 throw DecodeError ("could not create buffer source");
301         }
302
303         enum PixelFormat pixel_formats[] = { pixel_format(), PIX_FMT_NONE };
304         if (avfilter_graph_create_filter (&_buffer_sink_context, buffer_sink, "out", 0, pixel_formats, graph) < 0) {
305                 throw DecodeError ("could not create buffer sink.");
306         }
307
308         AVFilterInOut* outputs = avfilter_inout_alloc ();
309         outputs->name = av_strdup("in");
310         outputs->filter_ctx = _buffer_src_context;
311         outputs->pad_idx = 0;
312         outputs->next = 0;
313
314         AVFilterInOut* inputs = avfilter_inout_alloc ();
315         inputs->name = av_strdup("out");
316         inputs->filter_ctx = _buffer_sink_context;
317         inputs->pad_idx = 0;
318         inputs->next = 0;
319
320         _log->log ("Using filter chain `" + filters + "'");
321 #ifdef DVDOMATIC_FFMPEG_0_8_3   
322         if (avfilter_graph_parse (graph, filters.c_str(), inputs, outputs, 0) < 0) {
323 #else
324         if (avfilter_graph_parse (graph, filters.c_str(), &inputs, &outputs, 0) < 0) {
325 #endif          
326                 
327                 throw DecodeError ("could not set up filter graph.");
328         }
329
330         if (avfilter_graph_config (graph, 0) < 0) {
331                 throw DecodeError ("could not configure filter graph.");
332         }
333
334         /* XXX: leaking `inputs' / `outputs' ? */
335 }
336