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"
44 #include "film_state.h"
46 #include "exceptions.h"
50 #include "ffmpeg_decoder.h"
54 using namespace boost;
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)
61 , _subtitle_stream (-1)
63 , _video_codec_context (0)
65 , _audio_codec_context (0)
67 , _subtitle_codec_context (0)
76 FFmpegDecoder::~FFmpegDecoder ()
78 if (_audio_codec_context) {
79 avcodec_close (_audio_codec_context);
82 if (_video_codec_context) {
83 avcodec_close (_video_codec_context);
86 if (_subtitle_codec_context) {
87 avcodec_close (_subtitle_codec_context);
91 avformat_close_input (&_format_context);
95 FFmpegDecoder::setup_general ()
101 if ((r = avformat_open_input (&_format_context, _fs->content_path().c_str(), 0, 0)) != 0) {
102 throw OpenFileError (_fs->content_path ());
105 if (avformat_find_stream_info (_format_context, 0) < 0) {
106 throw DecodeError ("could not find stream information");
109 for (uint32_t i = 0; i < _format_context->nb_streams; ++i) {
110 if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
112 } else if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
114 } else if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
115 _subtitle_stream = i;
119 if (_video_stream < 0) {
120 throw DecodeError ("could not find video stream");
123 _frame = avcodec_alloc_frame ();
125 throw DecodeError ("could not allocate frame");
130 FFmpegDecoder::setup_video ()
132 _video_codec_context = _format_context->streams[_video_stream]->codec;
133 _video_codec = avcodec_find_decoder (_video_codec_context->codec_id);
135 if (_video_codec == 0) {
136 throw DecodeError ("could not find video decoder");
139 if (avcodec_open2 (_video_codec_context, _video_codec, 0) < 0) {
140 throw DecodeError ("could not open video decoder");
145 FFmpegDecoder::setup_audio ()
147 if (_audio_stream < 0) {
151 _audio_codec_context = _format_context->streams[_audio_stream]->codec;
152 _audio_codec = avcodec_find_decoder (_audio_codec_context->codec_id);
154 if (_audio_codec == 0) {
155 throw DecodeError ("could not find audio decoder");
158 if (avcodec_open2 (_audio_codec_context, _audio_codec, 0) < 0) {
159 throw DecodeError ("could not open audio decoder");
162 /* This is a hack; sometimes it seems that _audio_codec_context->channel_layout isn't set up,
163 so bodge it here. No idea why we should have to do this.
166 if (_audio_codec_context->channel_layout == 0) {
167 _audio_codec_context->channel_layout = av_get_default_channel_layout (audio_channels ());
172 FFmpegDecoder::setup_subtitle ()
174 if (_subtitle_stream < 0) {
178 _subtitle_codec_context = _format_context->streams[_subtitle_stream]->codec;
179 _subtitle_codec = avcodec_find_decoder (_subtitle_codec_context->codec_id);
181 if (_subtitle_codec == 0) {
182 throw DecodeError ("could not find subtitle decoder");
185 if (avcodec_open2 (_subtitle_codec_context, _subtitle_codec, 0) < 0) {
186 throw DecodeError ("could not open subtitle decoder");
192 FFmpegDecoder::do_pass ()
194 int r = av_read_frame (_format_context, &_packet);
196 if (r != AVERROR_EOF) {
197 throw DecodeError ("error on av_read_frame");
200 /* Get any remaining frames */
207 while (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
208 process_video (_frame);
211 if (_audio_stream >= 0 && _opt->decode_audio) {
212 while (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
213 int const data_size = av_samples_get_buffer_size (
214 0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
217 assert (_audio_codec_context->channels == _fs->audio_channels);
218 process_audio (_frame->data[0], data_size);
225 if (_packet.stream_index == _video_stream) {
228 if (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
229 process_video (_frame);
232 } else if (_audio_stream >= 0 && _packet.stream_index == _audio_stream && _opt->decode_audio) {
234 avcodec_get_frame_defaults (_frame);
237 if (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
238 int const data_size = av_samples_get_buffer_size (
239 0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
242 assert (_audio_codec_context->channels == _fs->audio_channels);
243 process_audio (_frame->data[0], data_size);
246 } else if (_subtitle_stream >= 0 && _packet.stream_index == _subtitle_stream && _opt->decode_subtitles) {
250 if (avcodec_decode_subtitle2 (_subtitle_codec_context, &sub, &got_subtitle, &_packet) && got_subtitle) {
251 process_subtitle (shared_ptr<Subtitle> (new Subtitle (sub)));
252 avsubtitle_free (&sub);
256 av_free_packet (&_packet);
261 FFmpegDecoder::length_in_frames () const
263 return (_format_context->duration / AV_TIME_BASE) * frames_per_second ();
267 FFmpegDecoder::frames_per_second () const
269 AVStream* s = _format_context->streams[_video_stream];
271 if (s->avg_frame_rate.num && s->avg_frame_rate.den) {
272 return av_q2d (s->avg_frame_rate);
275 return av_q2d (s->r_frame_rate);
279 FFmpegDecoder::audio_channels () const
281 if (_audio_codec_context == 0) {
285 return _audio_codec_context->channels;
289 FFmpegDecoder::audio_sample_rate () const
291 if (_audio_codec_context == 0) {
295 return _audio_codec_context->sample_rate;
299 FFmpegDecoder::audio_sample_format () const
301 if (_audio_codec_context == 0) {
302 return (AVSampleFormat) 0;
305 return _audio_codec_context->sample_fmt;
309 FFmpegDecoder::audio_channel_layout () const
311 if (_audio_codec_context == 0) {
315 return _audio_codec_context->channel_layout;
319 FFmpegDecoder::native_size () const
321 return Size (_video_codec_context->width, _video_codec_context->height);
325 FFmpegDecoder::pixel_format () const
327 return _video_codec_context->pix_fmt;
331 FFmpegDecoder::time_base_numerator () const
333 return _video_codec_context->time_base.num;
337 FFmpegDecoder::time_base_denominator () const
339 return _video_codec_context->time_base.den;
343 FFmpegDecoder::sample_aspect_ratio_numerator () const
345 return _video_codec_context->sample_aspect_ratio.num;
349 FFmpegDecoder::sample_aspect_ratio_denominator () const
351 return _video_codec_context->sample_aspect_ratio.den;
355 FFmpegDecoder::has_subtitles () const
357 return (_subtitle_stream != -1);