Partial fix to sync according to pts.
[dcpomatic.git] / src / lib / ffmpeg_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/ffmpeg_decoder.cc
21  *  @brief A decoder using FFmpeg to decode content.
22  */
23
24 #include <stdexcept>
25 #include <vector>
26 #include <sstream>
27 #include <iomanip>
28 #include <iostream>
29 #include <stdint.h>
30 #include <boost/lexical_cast.hpp>
31 extern "C" {
32 #include <tiffio.h>
33 #include <libavcodec/avcodec.h>
34 #include <libavformat/avformat.h>
35 #include <libswscale/swscale.h>
36 #include <libpostproc/postprocess.h>
37 }
38 #include <sndfile.h>
39 #include "film.h"
40 #include "format.h"
41 #include "transcoder.h"
42 #include "job.h"
43 #include "filter.h"
44 #include "film_state.h"
45 #include "options.h"
46 #include "exceptions.h"
47 #include "image.h"
48 #include "util.h"
49 #include "log.h"
50 #include "ffmpeg_decoder.h"
51 #include "subtitle.h"
52
53 using namespace std;
54 using namespace boost;
55
56 FFmpegDecoder::FFmpegDecoder (boost::shared_ptr<const FilmState> s, boost::shared_ptr<const Options> o, Job* j, Log* l, bool minimal, bool ignore_length)
57         : Decoder (s, o, j, l, minimal, ignore_length)
58         , _format_context (0)
59         , _video_stream (-1)
60         , _audio_stream (-1)
61         , _subtitle_stream (-1)
62         , _frame (0)
63         , _video_codec_context (0)
64         , _video_codec (0)
65         , _audio_codec_context (0)
66         , _audio_codec (0)
67         , _subtitle_codec_context (0)
68         , _subtitle_codec (0)
69         , _first_video_pts (-1)
70 {
71         setup_general ();
72         setup_video ();
73         setup_audio ();
74         setup_subtitle ();
75 }
76
77 FFmpegDecoder::~FFmpegDecoder ()
78 {
79         if (_audio_codec_context) {
80                 avcodec_close (_audio_codec_context);
81         }
82         
83         if (_video_codec_context) {
84                 avcodec_close (_video_codec_context);
85         }
86
87         if (_subtitle_codec_context) {
88                 avcodec_close (_subtitle_codec_context);
89         }
90         
91         av_free (_frame);
92         avformat_close_input (&_format_context);
93 }       
94
95 void
96 FFmpegDecoder::setup_general ()
97 {
98         int r;
99         
100         av_register_all ();
101
102         if ((r = avformat_open_input (&_format_context, _fs->content_path().c_str(), 0, 0)) != 0) {
103                 throw OpenFileError (_fs->content_path ());
104         }
105
106         if (avformat_find_stream_info (_format_context, 0) < 0) {
107                 throw DecodeError ("could not find stream information");
108         }
109
110         /* Find video, audio and subtitle streams and choose the first of each */
111
112         for (uint32_t i = 0; i < _format_context->nb_streams; ++i) {
113                 AVStream* s = _format_context->streams[i];
114                 if (s->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
115                         _video_stream = i;
116                 } else if (s->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
117                         if (_audio_stream == -1) {
118                                 _audio_stream = i;
119                         }
120                         _audio_streams.push_back (AudioStream (stream_name (s), i, s->codec->channels));
121                 } else if (s->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
122                         if (_subtitle_stream == -1) {
123                                 _subtitle_stream = i;
124                         }
125                         _subtitle_streams.push_back (SubtitleStream (stream_name (s), i));
126                 }
127         }
128
129         /* Now override audio and subtitle streams with those from the Film, if it has any */
130
131         if (_fs->audio_stream_index() != -1) {
132                 _audio_stream = _fs->audio_stream().id();
133         }
134
135         if (_fs->subtitle_stream_index() != -1) {
136                 _subtitle_stream = _fs->subtitle_stream().id ();
137         }
138
139         if (_video_stream < 0) {
140                 throw DecodeError ("could not find video stream");
141         }
142
143         _frame = avcodec_alloc_frame ();
144         if (_frame == 0) {
145                 throw DecodeError ("could not allocate frame");
146         }
147 }
148
149 void
150 FFmpegDecoder::setup_video ()
151 {
152         _video_codec_context = _format_context->streams[_video_stream]->codec;
153         _video_codec = avcodec_find_decoder (_video_codec_context->codec_id);
154
155         if (_video_codec == 0) {
156                 throw DecodeError ("could not find video decoder");
157         }
158         
159         if (avcodec_open2 (_video_codec_context, _video_codec, 0) < 0) {
160                 throw DecodeError ("could not open video decoder");
161         }
162 }
163
164 void
165 FFmpegDecoder::setup_audio ()
166 {
167         if (_audio_stream < 0) {
168                 return;
169         }
170         
171         _audio_codec_context = _format_context->streams[_audio_stream]->codec;
172         _audio_codec = avcodec_find_decoder (_audio_codec_context->codec_id);
173
174         if (_audio_codec == 0) {
175                 throw DecodeError ("could not find audio decoder");
176         }
177
178         if (avcodec_open2 (_audio_codec_context, _audio_codec, 0) < 0) {
179                 throw DecodeError ("could not open audio decoder");
180         }
181
182         /* This is a hack; sometimes it seems that _audio_codec_context->channel_layout isn't set up,
183            so bodge it here.  No idea why we should have to do this.
184         */
185
186         if (_audio_codec_context->channel_layout == 0) {
187                 _audio_codec_context->channel_layout = av_get_default_channel_layout (audio_channels ());
188         }
189 }
190
191 void
192 FFmpegDecoder::setup_subtitle ()
193 {
194         if (_subtitle_stream < 0) {
195                 return;
196         }
197
198         _subtitle_codec_context = _format_context->streams[_subtitle_stream]->codec;
199         _subtitle_codec = avcodec_find_decoder (_subtitle_codec_context->codec_id);
200
201         if (_subtitle_codec == 0) {
202                 throw DecodeError ("could not find subtitle decoder");
203         }
204         
205         if (avcodec_open2 (_subtitle_codec_context, _subtitle_codec, 0) < 0) {
206                 throw DecodeError ("could not open subtitle decoder");
207         }
208 }
209
210
211 bool
212 FFmpegDecoder::do_pass ()
213 {
214         int r = av_read_frame (_format_context, &_packet);
215         if (r < 0) {
216                 if (r != AVERROR_EOF) {
217                         throw DecodeError ("error on av_read_frame");
218                 }
219                 
220                 /* Get any remaining frames */
221                 
222                 _packet.data = 0;
223                 _packet.size = 0;
224
225                 /* XXX: should we reset _packet.data and size after each *_decode_* call? */
226
227                 int frame_finished;
228
229                 while (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
230                         process_video (_frame);
231                 }
232
233                 if (_audio_stream >= 0 && _opt->decode_audio) {
234                         while (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
235                                 int const data_size = av_samples_get_buffer_size (
236                                         0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
237                                         );
238
239                                 assert (_audio_codec_context->channels == _fs->audio_channels());
240                                 process_audio (_frame->data[0], data_size);
241                         }
242                 }
243
244                 return true;
245         }
246
247         if (_packet.stream_index == _video_stream) {
248
249                 int frame_finished;
250                 if (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
251                         if (_first_video_pts == -1) {
252                                 _first_video_pts = _packet.pts;
253                         }
254                         process_video (_frame);
255                 }
256
257         } else if (_audio_stream >= 0 && _packet.stream_index == _audio_stream && _opt->decode_audio) {
258                 
259                 avcodec_get_frame_defaults (_frame);
260                 
261                 int frame_finished;
262                 if (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
263                         int const data_size = av_samples_get_buffer_size (
264                                 0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
265                                 );
266
267                         assert (_audio_codec_context->channels == _fs->audio_channels());
268                         process_audio (_frame->data[0], data_size);
269                 }
270
271         } else if (_subtitle_stream >= 0 && _packet.stream_index == _subtitle_stream && _opt->decode_subtitles) {
272
273                 int got_subtitle;
274                 AVSubtitle sub;
275                 if (avcodec_decode_subtitle2 (_subtitle_codec_context, &sub, &got_subtitle, &_packet) && got_subtitle) {
276                         /* I'm not entirely sure why, but sometimes we get an AVSubtitle with
277                            no AVSubtitleRects.
278                         */
279                         if (sub.num_rects > 0) {
280                                 process_subtitle (shared_ptr<TimedSubtitle> (new TimedSubtitle (sub)));
281                         }
282                         avsubtitle_free (&sub);
283                 }
284         }
285         
286         av_free_packet (&_packet);
287         return false;
288 }
289
290 int
291 FFmpegDecoder::length_in_frames () const
292 {
293         return (_format_context->duration / AV_TIME_BASE) * frames_per_second ();
294 }
295
296 float
297 FFmpegDecoder::frames_per_second () const
298 {
299         AVStream* s = _format_context->streams[_video_stream];
300
301         if (s->avg_frame_rate.num && s->avg_frame_rate.den) {
302                 return av_q2d (s->avg_frame_rate);
303         }
304
305         return av_q2d (s->r_frame_rate);
306 }
307
308 int
309 FFmpegDecoder::audio_channels () const
310 {
311         if (_audio_codec_context == 0) {
312                 return 0;
313         }
314
315         return _audio_codec_context->channels;
316 }
317
318 int
319 FFmpegDecoder::audio_sample_rate () const
320 {
321         if (_audio_codec_context == 0) {
322                 return 0;
323         }
324         
325         return _audio_codec_context->sample_rate;
326 }
327
328 AVSampleFormat
329 FFmpegDecoder::audio_sample_format () const
330 {
331         if (_audio_codec_context == 0) {
332                 return (AVSampleFormat) 0;
333         }
334         
335         return _audio_codec_context->sample_fmt;
336 }
337
338 int64_t
339 FFmpegDecoder::audio_channel_layout () const
340 {
341         if (_audio_codec_context == 0) {
342                 return 0;
343         }
344         
345         return _audio_codec_context->channel_layout;
346 }
347
348 Size
349 FFmpegDecoder::native_size () const
350 {
351         return Size (_video_codec_context->width, _video_codec_context->height);
352 }
353
354 PixelFormat
355 FFmpegDecoder::pixel_format () const
356 {
357         return _video_codec_context->pix_fmt;
358 }
359
360 int
361 FFmpegDecoder::time_base_numerator () const
362 {
363         return _video_codec_context->time_base.num;
364 }
365
366 int
367 FFmpegDecoder::time_base_denominator () const
368 {
369         return _video_codec_context->time_base.den;
370 }
371
372 int
373 FFmpegDecoder::sample_aspect_ratio_numerator () const
374 {
375         return _video_codec_context->sample_aspect_ratio.num;
376 }
377
378 int
379 FFmpegDecoder::sample_aspect_ratio_denominator () const
380 {
381         return _video_codec_context->sample_aspect_ratio.den;
382 }
383
384 bool
385 FFmpegDecoder::has_subtitles () const
386 {
387         return (_subtitle_stream != -1);
388 }
389
390 vector<AudioStream>
391 FFmpegDecoder::audio_streams () const
392 {
393         return _audio_streams;
394 }
395
396 vector<SubtitleStream>
397 FFmpegDecoder::subtitle_streams () const
398 {
399         return _subtitle_streams;
400 }
401
402 string
403 FFmpegDecoder::stream_name (AVStream* s) const
404 {
405         stringstream n;
406         
407         AVDictionaryEntry const * lang = av_dict_get (s->metadata, "language", 0, 0);
408         if (lang) {
409                 n << lang->value;
410         }
411         
412         AVDictionaryEntry const * title = av_dict_get (s->metadata, "title", 0, 0);
413         if (title) {
414                 if (!n.str().empty()) {
415                         n << " ";
416                 }
417                 n << title->value;
418         }
419
420         if (n.str().empty()) {
421                 n << "unknown";
422         }
423
424         return n.str ();
425 }
426
427 int
428 FFmpegDecoder::audio_to_discard () const
429 {
430         AVStream* v = _format_context->streams[_video_stream];
431         AVStream* a = _format_context->streams[_audio_stream];
432
433         assert (v->time_base.num == a->time_base.num);
434         assert (v->time_base.den == a->time_base.den);
435
436         return rint (av_q2d (v->time_base) * 1000 * (_first_video_pts - _first_audio_pts));
437 }