From 6fd70950e721181920603d90ad52ed58883f2806 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Tue, 9 Oct 2012 02:07:07 +0100 Subject: [PATCH] Various subtitle fixes. --- src/lib/decoder.cc | 2 ++ src/lib/decoder.h | 1 + src/lib/ffmpeg_decoder.cc | 55 ++++++++++++++++++++++----------------- src/lib/ffmpeg_decoder.h | 1 + 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/lib/decoder.cc b/src/lib/decoder.cc index 8aa5f77c6..44079edf7 100644 --- a/src/lib/decoder.cc +++ b/src/lib/decoder.cc @@ -314,6 +314,8 @@ Decoder::process_video (AVFrame* frame) image->make_black (); } + overlay (image); + TIMING ("Decoder emits %1", _video_frame); Video (image, _video_frame); ++_video_frame; diff --git a/src/lib/decoder.h b/src/lib/decoder.h index 19ef25ede..08d961233 100644 --- a/src/lib/decoder.h +++ b/src/lib/decoder.h @@ -99,6 +99,7 @@ protected: virtual int time_base_denominator () const = 0; virtual int sample_aspect_ratio_numerator () const = 0; virtual int sample_aspect_ratio_denominator () const = 0; + virtual void overlay (boost::shared_ptr image) const {} void process_video (AVFrame *); void process_audio (uint8_t *, int); diff --git a/src/lib/ffmpeg_decoder.cc b/src/lib/ffmpeg_decoder.cc index cf17bbfb7..808e5ac9b 100644 --- a/src/lib/ffmpeg_decoder.cc +++ b/src/lib/ffmpeg_decoder.cc @@ -27,6 +27,7 @@ #include #include #include +#include extern "C" { #include #include @@ -231,7 +232,6 @@ FFmpegDecoder::do_pass () int frame_finished; if (avcodec_decode_video2 (_video_codec_context, _frame, &frame_finished, &_packet) >= 0 && frame_finished) { - maybe_add_subtitle (); process_video (_frame); } @@ -355,7 +355,7 @@ FFmpegDecoder::sample_aspect_ratio_denominator () const } void -FFmpegDecoder::maybe_add_subtitle () +FFmpegDecoder::overlay (shared_ptr image) const { if (!_have_subtitle) { return; @@ -368,32 +368,34 @@ FFmpegDecoder::maybe_add_subtitle () float const to = packet_time + (float (_subtitle.end_display_time) / 1e3); float const video_frame_time = float (last_video_frame ()) / rint (_fs->frames_per_second); - - if (from < video_frame_time || video_frame_time > to) { + + if (from > video_frame_time || video_frame_time < to) { return; } - + for (unsigned int i = 0; i < _subtitle.num_rects; ++i) { AVSubtitleRect* rect = _subtitle.rects[i]; if (rect->type != SUBTITLE_BITMAP) { throw DecodeError ("non-bitmap subtitles not yet supported"); } - - /* XXX: all this assumes YUV420 in _frame */ + + /* XXX: all this assumes YUV420 in image */ assert (rect->pict.data[0]); - /* Start of the first line in the target frame */ - uint8_t* frame_y_p = _frame->data[0] + rect->y * _frame->linesize[0]; - uint8_t* frame_u_p = _frame->data[1] + (rect->y / 2) * _frame->linesize[1]; - uint8_t* frame_v_p = _frame->data[2] + (rect->y / 2) * _frame->linesize[2]; + /* Start of the first line in the target image */ + uint8_t* frame_y_p = image->data()[0] + rect->y * image->line_size()[0]; + uint8_t* frame_u_p = image->data()[1] + (rect->y / 2) * image->line_size()[1]; + uint8_t* frame_v_p = image->data()[2] + (rect->y / 2) * image->line_size()[2]; + + int const hlim = min (rect->y + rect->h, image->size().height) - rect->y; /* Start of the first line in the subtitle */ uint8_t* sub_p = rect->pict.data[0]; /* sub_p looks up into a RGB palette which is here */ uint32_t const * palette = (uint32_t *) rect->pict.data[1]; - for (int sub_y = 0; sub_y < rect->h; ++sub_y) { + for (int sub_y = 0; sub_y < hlim; ++sub_y) { /* Pointers to the start of this line */ uint8_t* sub_line_p = sub_p; uint8_t* frame_line_y_p = frame_y_p + rect->x; @@ -401,8 +403,8 @@ FFmpegDecoder::maybe_add_subtitle () uint8_t* frame_line_v_p = frame_v_p + (rect->x / 2); /* U and V are subsampled */ - uint8_t current_u = 0; - uint8_t current_v = 0; + uint8_t next_u = 0; + uint8_t next_v = 0; int subsample_step = 0; for (int sub_x = 0; sub_x < rect->w; ++sub_x) { @@ -420,29 +422,34 @@ FFmpegDecoder::maybe_add_subtitle () *frame_line_y_p++ = int (cy * (1 - alpha)) + int (RGB_TO_Y_CCIR (red, green, blue) * alpha); /* Store up U and V */ - current_u |= ((RGB_TO_U_CCIR (red, green, blue, 0) & 0xf0) >> 4) << (4 * subsample_step); - current_v |= ((RGB_TO_V_CCIR (red, green, blue, 0) & 0xf0) >> 4) << (4 * subsample_step); + next_u |= ((RGB_TO_U_CCIR (red, green, blue, 0) & 0xf0) >> 4) << (4 * subsample_step); + next_v |= ((RGB_TO_V_CCIR (red, green, blue, 0) & 0xf0) >> 4) << (4 * subsample_step); if (subsample_step == 1 && (sub_y % 2) == 0) { - /* We have complete U and V bytes, so alpha-blend them into the frame */ int const cu = *frame_line_u_p; int const cv = *frame_line_v_p; - *frame_line_u_p++ = int (cu * (1 - alpha)) + int (current_u * alpha); - *frame_line_v_p++ = int (cv * (1 - alpha)) + int (current_v * alpha); - current_u = current_v = 0; + + *frame_line_u_p++ = + int (((cu & 0x0f) * (1 - alpha) + (next_u & 0x0f) * alpha)) | + int (((cu & 0xf0) * (1 - alpha) + (next_u & 0xf0) * alpha)); + + *frame_line_v_p++ = + int (((cv & 0x0f) * (1 - alpha) + (next_v & 0x0f) * alpha)) | + int (((cv & 0xf0) * (1 - alpha) + (next_v & 0xf0) * alpha)); + + next_u = next_v = 0; } subsample_step = (subsample_step + 1) % 2; } sub_p += rect->pict.linesize[0]; - frame_y_p += _frame->linesize[0]; + frame_y_p += image->line_size()[0]; if ((sub_y % 2) == 0) { - frame_u_p += _frame->linesize[1]; - frame_v_p += _frame->linesize[2]; + frame_u_p += image->line_size()[1]; + frame_v_p += image->line_size()[2]; } } } } - diff --git a/src/lib/ffmpeg_decoder.h b/src/lib/ffmpeg_decoder.h index 739502d50..18c2e2aeb 100644 --- a/src/lib/ffmpeg_decoder.h +++ b/src/lib/ffmpeg_decoder.h @@ -72,6 +72,7 @@ private: int time_base_denominator () const; int sample_aspect_ratio_numerator () const; int sample_aspect_ratio_denominator () const; + void overlay (boost::shared_ptr image) const; void setup_general (); void setup_video (); -- 2.30.2