30701797affc9f3f74837378b79735d19b66e08a
[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 "options.h"
45 #include "exceptions.h"
46 #include "image.h"
47 #include "util.h"
48 #include "log.h"
49 #include "ffmpeg_decoder.h"
50 #include "subtitle.h"
51
52 using namespace std;
53 using namespace boost;
54
55 FFmpegDecoder::FFmpegDecoder (boost::shared_ptr<Film> f, boost::shared_ptr<const Options> o, Job* j, bool minimal, bool ignore_length)
56         : Decoder (f, o, j, minimal, ignore_length)
57         , _format_context (0)
58         , _video_stream (-1)
59         , _audio_stream (-1)
60         , _subtitle_stream (-1)
61         , _frame (0)
62         , _video_codec_context (0)
63         , _video_codec (0)
64         , _audio_codec_context (0)
65         , _audio_codec (0)
66         , _subtitle_codec_context (0)
67         , _subtitle_codec (0)
68 {
69         setup_general ();
70         setup_video ();
71         setup_audio ();
72         setup_subtitle ();
73 }
74
75 FFmpegDecoder::~FFmpegDecoder ()
76 {
77         if (_audio_codec_context) {
78                 avcodec_close (_audio_codec_context);
79         }
80         
81         if (_video_codec_context) {
82                 avcodec_close (_video_codec_context);
83         }
84
85         if (_subtitle_codec_context) {
86                 avcodec_close (_subtitle_codec_context);
87         }
88         
89         av_free (_frame);
90         avformat_close_input (&_format_context);
91 }       
92
93 void
94 FFmpegDecoder::setup_general ()
95 {
96         int r;
97         
98         av_register_all ();
99
100         if ((r = avformat_open_input (&_format_context, _film->content_path().c_str(), 0, 0)) != 0) {
101                 throw OpenFileError (_film->content_path ());
102         }
103
104         if (avformat_find_stream_info (_format_context, 0) < 0) {
105                 throw DecodeError ("could not find stream information");
106         }
107
108         /* Find video, audio and subtitle streams and choose the first of each */
109
110         for (uint32_t i = 0; i < _format_context->nb_streams; ++i) {
111                 AVStream* s = _format_context->streams[i];
112                 if (s->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
113                         _video_stream = i;
114                 } else if (s->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
115                         if (_audio_stream == -1) {
116                                 _audio_stream = i;
117                         }
118                         _audio_streams.push_back (AudioStream (stream_name (s), i, s->codec->channels));
119                 } else if (s->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
120                         if (_subtitle_stream == -1) {
121                                 _subtitle_stream = i;
122                         }
123                         _subtitle_streams.push_back (SubtitleStream (stream_name (s), i));
124                 }
125         }
126
127         /* Now override audio and subtitle streams with those from the Film, if it has any */
128
129         if (_film->audio_stream_index() != -1) {
130                 _audio_stream = _film->audio_stream().id();
131         }
132
133         if (_film->subtitle_stream_index() != -1) {
134                 _subtitle_stream = _film->subtitle_stream().id ();
135         }
136
137         if (_video_stream < 0) {
138                 throw DecodeError ("could not find video stream");
139         }
140
141         _frame = avcodec_alloc_frame ();
142         if (_frame == 0) {
143                 throw DecodeError ("could not allocate frame");
144         }
145 }
146
147 void
148 FFmpegDecoder::setup_video ()
149 {
150         _video_codec_context = _format_context->streams[_video_stream]->codec;
151         _video_codec = avcodec_find_decoder (_video_codec_context->codec_id);
152
153         if (_video_codec == 0) {
154                 throw DecodeError ("could not find video decoder");
155         }
156         
157         if (avcodec_open2 (_video_codec_context, _video_codec, 0) < 0) {
158                 throw DecodeError ("could not open video decoder");
159         }
160 }
161
162 void
163 FFmpegDecoder::setup_audio ()
164 {
165         if (_audio_stream < 0) {
166                 return;
167         }
168         
169         _audio_codec_context = _format_context->streams[_audio_stream]->codec;
170         _audio_codec = avcodec_find_decoder (_audio_codec_context->codec_id);
171
172         if (_audio_codec == 0) {
173                 throw DecodeError ("could not find audio decoder");
174         }
175
176         if (avcodec_open2 (_audio_codec_context, _audio_codec, 0) < 0) {
177                 throw DecodeError ("could not open audio decoder");
178         }
179
180         /* This is a hack; sometimes it seems that _audio_codec_context->channel_layout isn't set up,
181            so bodge it here.  No idea why we should have to do this.
182         */
183
184         if (_audio_codec_context->channel_layout == 0) {
185                 _audio_codec_context->channel_layout = av_get_default_channel_layout (audio_channels ());
186         }
187 }
188
189 void
190 FFmpegDecoder::setup_subtitle ()
191 {
192         if (_subtitle_stream < 0) {
193                 return;
194         }
195
196         _subtitle_codec_context = _format_context->streams[_subtitle_stream]->codec;
197         _subtitle_codec = avcodec_find_decoder (_subtitle_codec_context->codec_id);
198
199         if (_subtitle_codec == 0) {
200                 throw DecodeError ("could not find subtitle decoder");
201         }
202         
203         if (avcodec_open2 (_subtitle_codec_context, _subtitle_codec, 0) < 0) {
204                 throw DecodeError ("could not open subtitle decoder");
205         }
206 }
207
208
209 bool
210 FFmpegDecoder::do_pass ()
211 {
212         int r = av_read_frame (_format_context, &_packet);
213         
214         if (r < 0) {
215                 if (r != AVERROR_EOF) {
216                         throw DecodeError ("error on av_read_frame");
217                 }
218                 
219                 /* Get any remaining frames */
220                 
221                 _packet.data = 0;
222                 _packet.size = 0;
223
224                 /* XXX: should we reset _packet.data and size after each *_decode_* call? */
225
226                 int frame_finished;
227
228                 while (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
229                         process_video (_frame);
230                 }
231
232                 if (_audio_stream >= 0 && _opt->decode_audio) {
233                         while (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
234                                 int const data_size = av_samples_get_buffer_size (
235                                         0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
236                                         );
237
238                                 assert (_audio_codec_context->channels == _film->audio_channels());
239                                 process_audio (_frame->data[0], data_size);
240                         }
241                 }
242
243                 return true;
244         }
245
246         double const pts_seconds = av_q2d (_format_context->streams[_packet.stream_index]->time_base) * _packet.pts;
247         
248         if (_packet.stream_index == _video_stream) {
249
250                 if (!_first_video) {
251                         _first_video = pts_seconds;
252                 }
253                 
254                 int frame_finished;
255                 if (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
256                         process_video (_frame);
257                 }
258
259         } else if (_audio_stream >= 0 && _packet.stream_index == _audio_stream && _opt->decode_audio && _first_video && _first_video.get() <= pts_seconds) {
260
261                 /* Note: We only decode audio if we've had our first video packet through, and if it
262                    was before this packet.  Until then audio is thrown away.
263                 */
264                 
265                 if (!_first_audio) {
266                         _first_audio = pts_seconds;
267                         
268                         /* This is our first audio packet, and if we've arrived here we must have had our
269                            first video packet.  Push some silence to make up the gap between our first
270                            video packet and our first audio.
271                         */
272                         
273                         /* frames of silence that we must push */
274                         int const s = rint ((_first_audio.get() - _first_video.get()) * audio_sample_rate ());
275                         
276                         _log->log (
277                                 String::compose (
278                                         "First video at %1, first audio at %2, pushing %3 frames of silence for %4 channels (%5 bytes per sample)",
279                                         _first_video.get(), _first_audio.get(), s, audio_channels(), bytes_per_audio_sample()
280                                         )
281                                 );
282                         
283                         /* hence bytes */
284                         int const b = s * audio_channels() * bytes_per_audio_sample();
285                         
286                         /* XXX: this assumes that it won't be too much, and there are shaky assumptions
287                            that all sound representations are silent with memset()ed zero data.
288                         */
289                         uint8_t silence[b];
290                         memset (silence, 0, b);
291                         process_audio (silence, b);
292                 }
293                 
294                 avcodec_get_frame_defaults (_frame);
295                 
296                 int frame_finished;
297                 if (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
298                         int const data_size = av_samples_get_buffer_size (
299                                 0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
300                                 );
301                         
302                         assert (_audio_codec_context->channels == _film->audio_channels());
303                         process_audio (_frame->data[0], data_size);
304                 }
305                         
306         } else if (_subtitle_stream >= 0 && _packet.stream_index == _subtitle_stream && _opt->decode_subtitles && _first_video) {
307
308                 int got_subtitle;
309                 AVSubtitle sub;
310                 if (avcodec_decode_subtitle2 (_subtitle_codec_context, &sub, &got_subtitle, &_packet) && got_subtitle) {
311                         /* Sometimes we get an empty AVSubtitle, which is used by some codecs to
312                            indicate that the previous subtitle should stop.
313                         */
314                         if (sub.num_rects > 0) {
315                                 process_subtitle (shared_ptr<TimedSubtitle> (new TimedSubtitle (sub, _first_video.get())));
316                         } else {
317                                 process_subtitle (shared_ptr<TimedSubtitle> ());
318                         }
319                         avsubtitle_free (&sub);
320                 }
321         }
322         
323         av_free_packet (&_packet);
324         return false;
325 }
326
327 float
328 FFmpegDecoder::frames_per_second () const
329 {
330         AVStream* s = _format_context->streams[_video_stream];
331
332         if (s->avg_frame_rate.num && s->avg_frame_rate.den) {
333                 return av_q2d (s->avg_frame_rate);
334         }
335
336         return av_q2d (s->r_frame_rate);
337 }
338
339 int
340 FFmpegDecoder::audio_channels () const
341 {
342         if (_audio_codec_context == 0) {
343                 return 0;
344         }
345
346         return _audio_codec_context->channels;
347 }
348
349 int
350 FFmpegDecoder::audio_sample_rate () const
351 {
352         if (_audio_codec_context == 0) {
353                 return 0;
354         }
355         
356         return _audio_codec_context->sample_rate;
357 }
358
359 AVSampleFormat
360 FFmpegDecoder::audio_sample_format () const
361 {
362         if (_audio_codec_context == 0) {
363                 return (AVSampleFormat) 0;
364         }
365         
366         return _audio_codec_context->sample_fmt;
367 }
368
369 int64_t
370 FFmpegDecoder::audio_channel_layout () const
371 {
372         if (_audio_codec_context == 0) {
373                 return 0;
374         }
375         
376         return _audio_codec_context->channel_layout;
377 }
378
379 Size
380 FFmpegDecoder::native_size () const
381 {
382         return Size (_video_codec_context->width, _video_codec_context->height);
383 }
384
385 PixelFormat
386 FFmpegDecoder::pixel_format () const
387 {
388         return _video_codec_context->pix_fmt;
389 }
390
391 int
392 FFmpegDecoder::time_base_numerator () const
393 {
394         return _video_codec_context->time_base.num;
395 }
396
397 int
398 FFmpegDecoder::time_base_denominator () const
399 {
400         return _video_codec_context->time_base.den;
401 }
402
403 int
404 FFmpegDecoder::sample_aspect_ratio_numerator () const
405 {
406         return _video_codec_context->sample_aspect_ratio.num;
407 }
408
409 int
410 FFmpegDecoder::sample_aspect_ratio_denominator () const
411 {
412         return _video_codec_context->sample_aspect_ratio.den;
413 }
414
415 bool
416 FFmpegDecoder::has_subtitles () const
417 {
418         return (_subtitle_stream != -1);
419 }
420
421 vector<AudioStream>
422 FFmpegDecoder::audio_streams () const
423 {
424         return _audio_streams;
425 }
426
427 vector<SubtitleStream>
428 FFmpegDecoder::subtitle_streams () const
429 {
430         return _subtitle_streams;
431 }
432
433 string
434 FFmpegDecoder::stream_name (AVStream* s) const
435 {
436         stringstream n;
437         
438         AVDictionaryEntry const * lang = av_dict_get (s->metadata, "language", 0, 0);
439         if (lang) {
440                 n << lang->value;
441         }
442         
443         AVDictionaryEntry const * title = av_dict_get (s->metadata, "title", 0, 0);
444         if (title) {
445                 if (!n.str().empty()) {
446                         n << " ";
447                 }
448                 n << title->value;
449         }
450
451         if (n.str().empty()) {
452                 n << "unknown";
453         }
454
455         return n.str ();
456 }
457