Various subtitle fixes.
authorCarl Hetherington <cth@carlh.net>
Tue, 9 Oct 2012 01:07:07 +0000 (02:07 +0100)
committerCarl Hetherington <cth@carlh.net>
Tue, 9 Oct 2012 01:07:07 +0000 (02:07 +0100)
src/lib/decoder.cc
src/lib/decoder.h
src/lib/ffmpeg_decoder.cc
src/lib/ffmpeg_decoder.h

index 8aa5f77c6e9f7b2916fba1eba3f9b20688b9580b..44079edf7bf4d1f846e481f350c54f0b73525ef8 100644 (file)
@@ -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;
index 19ef25ede0836eea85926956131c6ce9db1e15fc..08d96123380701e94f5ece131c6d3e54cf5321ad 100644 (file)
@@ -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> image) const {}
        
        void process_video (AVFrame *);
        void process_audio (uint8_t *, int);
index cf17bbfb7e83932c363ca9e217ba3c62e55dfa1a..808e5ac9b573b82f98734c7eaa1cccf577c212bd 100644 (file)
@@ -27,6 +27,7 @@
 #include <iomanip>
 #include <iostream>
 #include <stdint.h>
+#include <boost/lexical_cast.hpp>
 extern "C" {
 #include <tiffio.h>
 #include <libavcodec/avcodec.h>
@@ -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> 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];
                        }
                }
        }
 }
-
     
index 739502d50205a87ecb5f11500767e88b3cf4bf4f..18c2e2aeb316fa937a253a5dbc70a9bdd7535f96 100644 (file)
@@ -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> image) const;
 
        void setup_general ();
        void setup_video ();