2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file src/ffmpeg_decoder.cc
21 * @brief A decoder using FFmpeg to decode content.
30 #include <boost/lexical_cast.hpp>
33 #include <libavcodec/avcodec.h>
34 #include <libavformat/avformat.h>
35 #include <libswscale/swscale.h>
36 #include <libpostproc/postprocess.h>
41 #include "transcoder.h"
45 #include "exceptions.h"
49 #include "ffmpeg_decoder.h"
54 using std::stringstream;
55 using boost::shared_ptr;
57 FFmpegDecoder::FFmpegDecoder (shared_ptr<Film> f, shared_ptr<const Options> o, Job* j, bool minimal, bool ignore_length)
58 : Decoder (f, o, j, minimal, ignore_length)
62 , _subtitle_stream (-1)
64 , _video_codec_context (0)
66 , _audio_codec_context (0)
68 , _subtitle_codec_context (0)
77 FFmpegDecoder::~FFmpegDecoder ()
79 if (_audio_codec_context) {
80 avcodec_close (_audio_codec_context);
83 if (_video_codec_context) {
84 avcodec_close (_video_codec_context);
87 if (_subtitle_codec_context) {
88 avcodec_close (_subtitle_codec_context);
92 avformat_close_input (&_format_context);
96 FFmpegDecoder::setup_general ()
102 if ((r = avformat_open_input (&_format_context, _film->content_path().c_str(), 0, 0)) != 0) {
103 throw OpenFileError (_film->content_path ());
106 if (avformat_find_stream_info (_format_context, 0) < 0) {
107 throw DecodeError ("could not find stream information");
110 /* Find video, audio and subtitle streams and choose the first of each */
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) {
116 } else if (s->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
117 if (_audio_stream == -1) {
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;
125 _subtitle_streams.push_back (SubtitleStream (stream_name (s), i));
129 /* Now override audio and subtitle streams with those from the Film, if it has any */
131 if (_film->audio_stream_index() != -1) {
132 _audio_stream = _film->audio_stream().id();
135 if (_film->subtitle_stream_index() != -1) {
136 _subtitle_stream = _film->subtitle_stream().id ();
139 if (_video_stream < 0) {
140 throw DecodeError ("could not find video stream");
143 _frame = avcodec_alloc_frame ();
145 throw DecodeError ("could not allocate frame");
150 FFmpegDecoder::setup_video ()
152 _video_codec_context = _format_context->streams[_video_stream]->codec;
153 _video_codec = avcodec_find_decoder (_video_codec_context->codec_id);
155 if (_video_codec == 0) {
156 throw DecodeError ("could not find video decoder");
159 if (avcodec_open2 (_video_codec_context, _video_codec, 0) < 0) {
160 throw DecodeError ("could not open video decoder");
165 FFmpegDecoder::setup_audio ()
167 if (_audio_stream < 0) {
171 _audio_codec_context = _format_context->streams[_audio_stream]->codec;
172 _audio_codec = avcodec_find_decoder (_audio_codec_context->codec_id);
174 if (_audio_codec == 0) {
175 throw DecodeError ("could not find audio decoder");
178 if (avcodec_open2 (_audio_codec_context, _audio_codec, 0) < 0) {
179 throw DecodeError ("could not open audio decoder");
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.
186 if (_audio_codec_context->channel_layout == 0) {
187 _audio_codec_context->channel_layout = av_get_default_channel_layout (audio_channels ());
192 FFmpegDecoder::setup_subtitle ()
194 if (_subtitle_stream < 0) {
198 _subtitle_codec_context = _format_context->streams[_subtitle_stream]->codec;
199 _subtitle_codec = avcodec_find_decoder (_subtitle_codec_context->codec_id);
201 if (_subtitle_codec == 0) {
202 throw DecodeError ("could not find subtitle decoder");
205 if (avcodec_open2 (_subtitle_codec_context, _subtitle_codec, 0) < 0) {
206 throw DecodeError ("could not open subtitle decoder");
212 FFmpegDecoder::do_pass ()
214 int r = av_read_frame (_format_context, &_packet);
217 if (r != AVERROR_EOF) {
218 throw DecodeError ("error on av_read_frame");
221 /* Get any remaining frames */
226 /* XXX: should we reset _packet.data and size after each *_decode_* call? */
230 while (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
231 process_video (_frame);
234 if (_audio_stream >= 0 && _opt->decode_audio) {
235 while (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
236 int const data_size = av_samples_get_buffer_size (
237 0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
240 assert (_audio_codec_context->channels == _film->audio_channels());
241 process_audio (_frame->data[0], data_size);
248 double const pts_seconds = av_q2d (_format_context->streams[_packet.stream_index]->time_base) * _packet.pts;
250 if (_packet.stream_index == _video_stream) {
253 _first_video = pts_seconds;
257 if (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
258 process_video (_frame);
261 } else if (_audio_stream >= 0 && _packet.stream_index == _audio_stream && _opt->decode_audio && _first_video && _first_video.get() <= pts_seconds) {
263 /* Note: We only decode audio if we've had our first video packet through, and if it
264 was before this packet. Until then audio is thrown away.
268 _first_audio = pts_seconds;
270 /* This is our first audio packet, and if we've arrived here we must have had our
271 first video packet. Push some silence to make up the gap between our first
272 video packet and our first audio.
275 /* frames of silence that we must push */
276 int const s = rint ((_first_audio.get() - _first_video.get()) * audio_sample_rate ());
280 "First video at %1, first audio at %2, pushing %3 frames of silence for %4 channels (%5 bytes per sample)",
281 _first_video.get(), _first_audio.get(), s, audio_channels(), bytes_per_audio_sample()
286 int const b = s * audio_channels() * bytes_per_audio_sample();
288 /* XXX: this assumes that it won't be too much, and there are shaky assumptions
289 that all sound representations are silent with memset()ed zero data.
292 memset (silence, 0, b);
293 process_audio (silence, b);
296 avcodec_get_frame_defaults (_frame);
299 if (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
300 int const data_size = av_samples_get_buffer_size (
301 0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
304 assert (_audio_codec_context->channels == _film->audio_channels());
305 process_audio (_frame->data[0], data_size);
308 } else if (_subtitle_stream >= 0 && _packet.stream_index == _subtitle_stream && _opt->decode_subtitles && _first_video) {
312 if (avcodec_decode_subtitle2 (_subtitle_codec_context, &sub, &got_subtitle, &_packet) && got_subtitle) {
313 /* Sometimes we get an empty AVSubtitle, which is used by some codecs to
314 indicate that the previous subtitle should stop.
316 if (sub.num_rects > 0) {
317 process_subtitle (shared_ptr<TimedSubtitle> (new TimedSubtitle (sub, _first_video.get())));
319 process_subtitle (shared_ptr<TimedSubtitle> ());
321 avsubtitle_free (&sub);
325 av_free_packet (&_packet);
330 FFmpegDecoder::frames_per_second () const
332 AVStream* s = _format_context->streams[_video_stream];
334 if (s->avg_frame_rate.num && s->avg_frame_rate.den) {
335 return av_q2d (s->avg_frame_rate);
338 return av_q2d (s->r_frame_rate);
342 FFmpegDecoder::audio_channels () const
344 if (_audio_codec_context == 0) {
348 return _audio_codec_context->channels;
352 FFmpegDecoder::audio_sample_rate () const
354 if (_audio_codec_context == 0) {
358 return _audio_codec_context->sample_rate;
362 FFmpegDecoder::audio_sample_format () const
364 if (_audio_codec_context == 0) {
365 return (AVSampleFormat) 0;
368 return _audio_codec_context->sample_fmt;
372 FFmpegDecoder::audio_channel_layout () const
374 if (_audio_codec_context == 0) {
378 return _audio_codec_context->channel_layout;
382 FFmpegDecoder::native_size () const
384 return Size (_video_codec_context->width, _video_codec_context->height);
388 FFmpegDecoder::pixel_format () const
390 return _video_codec_context->pix_fmt;
394 FFmpegDecoder::time_base_numerator () const
396 return _video_codec_context->time_base.num;
400 FFmpegDecoder::time_base_denominator () const
402 return _video_codec_context->time_base.den;
406 FFmpegDecoder::sample_aspect_ratio_numerator () const
408 return _video_codec_context->sample_aspect_ratio.num;
412 FFmpegDecoder::sample_aspect_ratio_denominator () const
414 return _video_codec_context->sample_aspect_ratio.den;
418 FFmpegDecoder::has_subtitles () const
420 return (_subtitle_stream != -1);
424 FFmpegDecoder::audio_streams () const
426 return _audio_streams;
429 vector<SubtitleStream>
430 FFmpegDecoder::subtitle_streams () const
432 return _subtitle_streams;
436 FFmpegDecoder::stream_name (AVStream* s) const
440 AVDictionaryEntry const * lang = av_dict_get (s->metadata, "language", 0, 0);
445 AVDictionaryEntry const * title = av_dict_get (s->metadata, "title", 0, 0);
447 if (!n.str().empty()) {
453 if (n.str().empty()) {