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.
32 #include <libavcodec/avcodec.h>
33 #include <libavformat/avformat.h>
34 #include <libswscale/swscale.h>
35 #include <libpostproc/postprocess.h>
40 #include "transcoder.h"
43 #include "film_state.h"
45 #include "exceptions.h"
49 #include "ffmpeg_decoder.h"
52 using namespace boost;
54 FFmpegDecoder::FFmpegDecoder (boost::shared_ptr<const FilmState> s, boost::shared_ptr<const Options> o, Job* j, Log* l, bool minimal, bool ignore_length)
55 : Decoder (s, o, j, l, minimal, ignore_length)
59 , _subtitle_stream (-1)
61 , _video_codec_context (0)
63 , _audio_codec_context (0)
65 , _subtitle_codec_context (0)
67 , _have_subtitle (false)
75 FFmpegDecoder::~FFmpegDecoder ()
77 if (_audio_codec_context) {
78 avcodec_close (_audio_codec_context);
81 if (_video_codec_context) {
82 avcodec_close (_video_codec_context);
86 avsubtitle_free (&_subtitle);
89 if (_subtitle_codec_context) {
90 avcodec_close (_subtitle_codec_context);
94 avformat_close_input (&_format_context);
98 FFmpegDecoder::setup_general ()
104 if ((r = avformat_open_input (&_format_context, _fs->content_path().c_str(), 0, 0)) != 0) {
105 throw OpenFileError (_fs->content_path ());
108 if (avformat_find_stream_info (_format_context, 0) < 0) {
109 throw DecodeError ("could not find stream information");
112 for (uint32_t i = 0; i < _format_context->nb_streams; ++i) {
113 if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
115 } else if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
117 } else if (_format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
118 _subtitle_stream = i;
122 if (_video_stream < 0) {
123 throw DecodeError ("could not find video stream");
126 _frame = avcodec_alloc_frame ();
128 throw DecodeError ("could not allocate frame");
133 FFmpegDecoder::setup_video ()
135 _video_codec_context = _format_context->streams[_video_stream]->codec;
136 _video_codec = avcodec_find_decoder (_video_codec_context->codec_id);
138 if (_video_codec == 0) {
139 throw DecodeError ("could not find video decoder");
142 if (avcodec_open2 (_video_codec_context, _video_codec, 0) < 0) {
143 throw DecodeError ("could not open video decoder");
148 FFmpegDecoder::setup_audio ()
150 if (_audio_stream < 0) {
154 _audio_codec_context = _format_context->streams[_audio_stream]->codec;
155 _audio_codec = avcodec_find_decoder (_audio_codec_context->codec_id);
157 if (_audio_codec == 0) {
158 throw DecodeError ("could not find audio decoder");
161 if (avcodec_open2 (_audio_codec_context, _audio_codec, 0) < 0) {
162 throw DecodeError ("could not open audio decoder");
165 /* This is a hack; sometimes it seems that _audio_codec_context->channel_layout isn't set up,
166 so bodge it here. No idea why we should have to do this.
169 if (_audio_codec_context->channel_layout == 0) {
170 _audio_codec_context->channel_layout = av_get_default_channel_layout (audio_channels ());
175 FFmpegDecoder::setup_subtitle ()
177 if (_subtitle_stream < 0) {
181 _subtitle_codec_context = _format_context->streams[_subtitle_stream]->codec;
182 _subtitle_codec = avcodec_find_decoder (_subtitle_codec_context->codec_id);
184 if (_subtitle_codec == 0) {
185 throw DecodeError ("could not find subtitle decoder");
188 if (avcodec_open2 (_subtitle_codec_context, _subtitle_codec, 0) < 0) {
189 throw DecodeError ("could not open subtitle decoder");
195 FFmpegDecoder::do_pass ()
197 int r = av_read_frame (_format_context, &_packet);
199 if (r != AVERROR_EOF) {
200 throw DecodeError ("error on av_read_frame");
203 /* Get any remaining frames */
210 if (_opt->decode_video) {
211 while (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
212 process_video (_frame);
216 if (_audio_stream >= 0 && _opt->decode_audio) {
217 while (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
218 int const data_size = av_samples_get_buffer_size (
219 0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
222 assert (_audio_codec_context->channels == _fs->audio_channels);
223 process_audio (_frame->data[0], data_size);
230 if (_packet.stream_index == _video_stream && _opt->decode_video) {
233 if (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
234 maybe_add_subtitle ();
235 process_video (_frame);
238 } else if (_audio_stream >= 0 && _packet.stream_index == _audio_stream && _opt->decode_audio) {
240 avcodec_get_frame_defaults (_frame);
243 if (avcodec_decode_audio4 (_audio_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) {
244 int const data_size = av_samples_get_buffer_size (
245 0, _audio_codec_context->channels, _frame->nb_samples, audio_sample_format (), 1
248 assert (_audio_codec_context->channels == _fs->audio_channels);
249 process_audio (_frame->data[0], data_size);
252 } else if (_subtitle_stream >= 0 && _packet.stream_index == _subtitle_stream) {
254 if (_have_subtitle) {
255 avsubtitle_free (&_subtitle);
256 _have_subtitle = false;
260 if (avcodec_decode_subtitle2 (_subtitle_codec_context, &_subtitle, &got_subtitle, &_packet) && got_subtitle) {
261 _have_subtitle = true;
265 av_free_packet (&_packet);
270 FFmpegDecoder::length_in_frames () const
272 return (_format_context->duration / AV_TIME_BASE) * frames_per_second ();
276 FFmpegDecoder::frames_per_second () const
278 return av_q2d (_format_context->streams[_video_stream]->avg_frame_rate);
282 FFmpegDecoder::audio_channels () const
284 if (_audio_codec_context == 0) {
288 return _audio_codec_context->channels;
292 FFmpegDecoder::audio_sample_rate () const
294 if (_audio_codec_context == 0) {
298 return _audio_codec_context->sample_rate;
302 FFmpegDecoder::audio_sample_format () const
304 if (_audio_codec_context == 0) {
305 return (AVSampleFormat) 0;
308 return _audio_codec_context->sample_fmt;
312 FFmpegDecoder::audio_channel_layout () const
314 if (_audio_codec_context == 0) {
318 return _audio_codec_context->channel_layout;
322 FFmpegDecoder::native_size () const
324 return Size (_video_codec_context->width, _video_codec_context->height);
328 FFmpegDecoder::pixel_format () const
330 return _video_codec_context->pix_fmt;
334 FFmpegDecoder::time_base_numerator () const
336 return _video_codec_context->time_base.num;
340 FFmpegDecoder::time_base_denominator () const
342 return _video_codec_context->time_base.den;
346 FFmpegDecoder::sample_aspect_ratio_numerator () const
348 return _video_codec_context->sample_aspect_ratio.num;
352 FFmpegDecoder::sample_aspect_ratio_denominator () const
354 return _video_codec_context->sample_aspect_ratio.den;
358 FFmpegDecoder::maybe_add_subtitle ()
360 if (!_have_subtitle) {
364 /* subtitle PTS in seconds */
365 float const packet_time = (_subtitle.pts / AV_TIME_BASE) + float (_subtitle.pts % AV_TIME_BASE) / 1e6;
366 /* hence start time for this sub */
367 float const from = packet_time + (float (_subtitle.start_display_time) / 1e3);
368 float const to = packet_time + (float (_subtitle.end_display_time) / 1e3);
370 float const video_frame_time = float (last_video_frame ()) / rint (_fs->frames_per_second);
372 if (from < video_frame_time || video_frame_time > to) {
376 for (unsigned int i = 0; i < _subtitle.num_rects; ++i) {
377 AVSubtitleRect* rect = _subtitle.rects[i];
378 if (rect->type != SUBTITLE_BITMAP) {
379 throw DecodeError ("non-bitmap subtitles not yet supported");
382 /* XXX: all this assumes YUV420 in _frame */
384 assert (rect->pict.data[0]);
386 /* Start of the first line in the target frame */
387 uint8_t* frame_y_p = _frame->data[0] + rect->y * _frame->linesize[0];
388 uint8_t* frame_u_p = _frame->data[1] + (rect->y / 2) * _frame->linesize[1];
389 uint8_t* frame_v_p = _frame->data[2] + (rect->y / 2) * _frame->linesize[2];
391 /* Start of the first line in the subtitle */
392 uint8_t* sub_p = rect->pict.data[0];
393 /* sub_p looks up into a RGB palette which is here */
394 uint32_t const * palette = (uint32_t *) rect->pict.data[1];
396 for (int sub_y = 0; sub_y < rect->h; ++sub_y) {
397 /* Pointers to the start of this line */
398 uint8_t* sub_line_p = sub_p;
399 uint8_t* frame_line_y_p = frame_y_p + rect->x;
400 uint8_t* frame_line_u_p = frame_u_p + (rect->x / 2);
401 uint8_t* frame_line_v_p = frame_v_p + (rect->x / 2);
403 /* U and V are subsampled */
404 uint8_t current_u = 0;
405 uint8_t current_v = 0;
406 int subsample_step = 0;
408 for (int sub_x = 0; sub_x < rect->w; ++sub_x) {
410 /* RGB value for this subtitle pixel */
411 uint32_t const val = palette[*sub_line_p++];
413 int const red = (val & 0xff);
414 int const green = (val & 0xff00) >> 8;
415 int const blue = (val & 0xff0000) >> 16;
416 float const alpha = ((val & 0xff000000) >> 24) / 255.0;
419 int const cy = *frame_line_y_p;
420 *frame_line_y_p++ = int (cy * (1 - alpha)) + int (RGB_TO_Y_CCIR (red, green, blue) * alpha);
422 /* Store up U and V */
423 current_u |= ((RGB_TO_U_CCIR (red, green, blue, 0) & 0xf0) >> 4) << (4 * subsample_step);
424 current_v |= ((RGB_TO_V_CCIR (red, green, blue, 0) & 0xf0) >> 4) << (4 * subsample_step);
426 if (subsample_step == 1 && (sub_y % 2) == 0) {
427 /* We have complete U and V bytes, so alpha-blend them into the frame */
428 int const cu = *frame_line_u_p;
429 int const cv = *frame_line_v_p;
430 *frame_line_u_p++ = int (cu * (1 - alpha)) + int (current_u * alpha);
431 *frame_line_v_p++ = int (cv * (1 - alpha)) + int (current_v * alpha);
432 current_u = current_v = 0;
435 subsample_step = (subsample_step + 1) % 2;
438 sub_p += rect->pict.linesize[0];
439 frame_y_p += _frame->linesize[0];
440 if ((sub_y % 2) == 0) {
441 frame_u_p += _frame->linesize[1];
442 frame_v_p += _frame->linesize[2];