]> git.sesse.net Git - vlc/blobdiff - modules/packetizer/mpegvideo.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / modules / packetizer / mpegvideo.c
index 19fd9543e36763934dd5e265c6571bd6912839d3..cad9545f6070dbcb1b08e67703468297005d4472 100644 (file)
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <vlc_block.h>
 #include <vlc_codec.h>
 #include <vlc_block_helper.h>
 #include "../codec/cc.h"
+#include "packetizer_helper.h"
 
 #define SYNC_INTRAFRAME_TEXT N_("Sync on Intra Frame")
 #define SYNC_INTRAFRAME_LONGTEXT N_("Normally the packetizer would " \
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
-vlc_module_begin();
-    set_category( CAT_SOUT );
-    set_subcategory( SUBCAT_SOUT_PACKETIZER );
-    set_description( _("MPEG-I/II video packetizer") );
-    set_capability( "packetizer", 50 );
-    set_callbacks( Open, Close );
+vlc_module_begin ()
+    set_category( CAT_SOUT )
+    set_subcategory( SUBCAT_SOUT_PACKETIZER )
+    set_description( N_("MPEG-I/II video packetizer") )
+    set_shortname( N_("MPEG Video") )
+    set_capability( "packetizer", 50 )
+    set_callbacks( Open, Close )
 
-    add_bool( "packetizer-mpegvideo-sync-iframe", 0, NULL, SYNC_INTRAFRAME_TEXT,
-              SYNC_INTRAFRAME_LONGTEXT, VLC_TRUE );
-vlc_module_end();
+    add_bool( "packetizer-mpegvideo-sync-iframe", false, NULL, SYNC_INTRAFRAME_TEXT,
+              SYNC_INTRAFRAME_LONGTEXT, true )
+vlc_module_end ()
 
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-static block_t *Packetize( decoder_t *, block_t ** );
-static block_t *ParseMPEGBlock( decoder_t *, block_t * );
-static block_t *GetCc( decoder_t *p_dec, vlc_bool_t pb_present[4] );
-
 struct decoder_sys_t
 {
     /*
      * Input properties
      */
-    block_bytestream_t bytestream;
-    int i_state;
-    size_t i_offset;
-    uint8_t p_startcode[3];
+    packetizer_t packetizer;
 
     /* Sequence header and extension */
     block_t *p_seq;
@@ -99,17 +95,17 @@ struct decoder_sys_t
     block_t    *p_frame;
     block_t    **pp_last;
 
-    vlc_bool_t b_frame_slice;
+    bool b_frame_slice;
     mtime_t i_pts;
     mtime_t i_dts;
 
     /* Sequence properties */
     int         i_frame_rate;
     int         i_frame_rate_base;
-    vlc_bool_t  b_seq_progressive;
-    vlc_bool_t  b_low_delay;
+    bool  b_seq_progressive;
+    bool  b_low_delay;
     int         i_aspect_ratio_info;
-    vlc_bool_t  b_inited;
+    bool  b_inited;
 
     /* Picture properties */
     int i_temporal_ref;
@@ -121,27 +117,33 @@ struct decoder_sys_t
 
     mtime_t i_interpolated_dts;
     mtime_t i_last_ref_pts;
-    vlc_bool_t b_second_field;
+    bool b_second_field;
 
     /* Number of pictures since last sequence header */
     int i_seq_old;
 
     /* Sync behaviour */
-    vlc_bool_t  b_sync_on_intra_frame;
-    vlc_bool_t  b_discontinuity;
+    bool  b_sync_on_intra_frame;
+    bool  b_discontinuity;
 
     /* */
-    vlc_bool_t b_cc_reset;
+    bool b_cc_reset;
     uint32_t i_cc_flags;
     mtime_t i_cc_pts;
     mtime_t i_cc_dts;
     cc_data_t cc;
 };
 
-enum {
-    STATE_NOSYNC,
-    STATE_NEXT_SYNC
-};
+static block_t *Packetize( decoder_t *, block_t ** );
+static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] );
+
+static void PacketizeReset( void *p_private, bool b_broken );
+static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t * );
+static int PacketizeValidate( void *p_private, block_t * );
+
+static block_t *ParseMPEGBlock( decoder_t *, block_t * );
+
+static const uint8_t p_mp2v_startcode[3] = { 0x00, 0x00, 0x01 };
 
 /*****************************************************************************
  * Open:
@@ -151,39 +153,38 @@ static int Open( vlc_object_t *p_this )
     decoder_t *p_dec = (decoder_t*)p_this;
     decoder_sys_t *p_sys;
 
-    if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '1' ) &&
-        p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', '2' ) &&
-        p_dec->fmt_in.i_codec != VLC_FOURCC( 'm', 'p', 'g', 'v' ) )
-    {
+    if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGV )
         return VLC_EGENERIC;
-    }
 
-    es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC('m','p','g','v') );
+    es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_CODEC_MPGV );
+    p_dec->fmt_out.i_original_fourcc = p_dec->fmt_in.i_original_fourcc;
+
     p_dec->pf_packetize = Packetize;
     p_dec->pf_get_cc = GetCc;
 
     p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
+    if( !p_dec->p_sys )
+        return VLC_ENOMEM;
+    memset( p_dec->p_sys, 0, sizeof( decoder_sys_t ) );
 
     /* Misc init */
-    p_sys->i_state = STATE_NOSYNC;
-    p_sys->bytestream = block_BytestreamInit();
-    p_sys->p_startcode[0] = 0;
-    p_sys->p_startcode[1] = 0;
-    p_sys->p_startcode[2] = 1;
-    p_sys->i_offset = 0;
+    packetizer_Init( &p_sys->packetizer,
+                     p_mp2v_startcode, sizeof(p_mp2v_startcode),
+                     NULL, 0, 4,
+                     PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
 
     p_sys->p_seq = NULL;
     p_sys->p_ext = NULL;
     p_sys->p_frame = NULL;
     p_sys->pp_last = &p_sys->p_frame;
-    p_sys->b_frame_slice = VLC_FALSE;
+    p_sys->b_frame_slice = false;
 
-    p_sys->i_dts = p_sys->i_pts = 0;
+    p_sys->i_dts = p_sys->i_pts = VLC_TS_INVALID;
 
     p_sys->i_frame_rate = 1;
     p_sys->i_frame_rate_base = 1;
-    p_sys->b_seq_progressive = VLC_TRUE;
-    p_sys->b_low_delay = VLC_TRUE;
+    p_sys->b_seq_progressive = true;
+    p_sys->b_low_delay = true;
     p_sys->i_seq_old = 0;
 
     p_sys->i_temporal_ref = 0;
@@ -194,16 +195,16 @@ static int Open( vlc_object_t *p_this )
     p_sys->i_progressive_frame = 0;
     p_sys->b_inited = 0;
 
-    p_sys->i_interpolated_dts = 0;
-    p_sys->i_last_ref_pts = 0;
+    p_sys->i_interpolated_dts = VLC_TS_INVALID;
+    p_sys->i_last_ref_pts = VLC_TS_INVALID;
     p_sys->b_second_field = 0;
 
-    p_sys->b_discontinuity = VLC_FALSE;
+    p_sys->b_discontinuity = false;
     p_sys->b_sync_on_intra_frame = var_CreateGetBool( p_dec, "packetizer-mpegvideo-sync-iframe" );
     if( p_sys->b_sync_on_intra_frame )
         msg_Dbg( p_dec, "syncing on intra frame now" );
 
-    p_sys->b_cc_reset = VLC_FALSE;
+    p_sys->b_cc_reset = false;
     p_sys->i_cc_pts = 0;
     p_sys->i_cc_dts = 0;
     p_sys->i_cc_flags = 0;
@@ -220,8 +221,6 @@ static void Close( vlc_object_t *p_this )
     decoder_t     *p_dec = (decoder_t*)p_this;
     decoder_sys_t *p_sys = p_dec->p_sys;
 
-    block_BytestreamRelease( &p_sys->bytestream );
-
     if( p_sys->p_seq )
     {
         block_Release( p_sys->p_seq );
@@ -234,6 +233,7 @@ static void Close( vlc_object_t *p_this )
     {
         block_ChainRelease( p_sys->p_frame );
     }
+    packetizer_Clean( &p_sys->packetizer );
 
     var_Destroy( p_dec, "packetizer-mpegvideo-sync-iframe" );
 
@@ -246,151 +246,14 @@ static void Close( vlc_object_t *p_this )
 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
-    block_t       *p_pic;
-
-    if( pp_block == NULL || *pp_block == NULL )
-    {
-        return NULL;
-    }
-
-    if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
-    {
-        if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
-        {
-            p_sys->i_state = STATE_NOSYNC;
-            block_BytestreamFlush( &p_sys->bytestream );
-
-            p_sys->b_discontinuity = VLC_TRUE;
-            if( p_sys->p_frame )
-                block_ChainRelease( p_sys->p_frame );
-            p_sys->p_frame = NULL;
-            p_sys->pp_last = &p_sys->p_frame;
-            p_sys->b_frame_slice = VLC_FALSE;
-        }
-//        p_sys->i_interpolated_dts =
-//        p_sys->i_last_ref_pts = 0;
-
-        block_Release( *pp_block );
-        return NULL;
-    }
-
-
-    block_BytestreamPush( &p_sys->bytestream, *pp_block );
-
-    while( 1 )
-    {
-        switch( p_sys->i_state )
-        {
-
-        case STATE_NOSYNC:
-            if( block_FindStartcodeFromOffset( &p_sys->bytestream,
-                    &p_sys->i_offset, p_sys->p_startcode, 3 ) == VLC_SUCCESS )
-            {
-                p_sys->i_state = STATE_NEXT_SYNC;
-            }
-
-            if( p_sys->i_offset )
-            {
-                block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
-                p_sys->i_offset = 0;
-                block_BytestreamFlush( &p_sys->bytestream );
-            }
-
-            if( p_sys->i_state != STATE_NEXT_SYNC )
-            {
-                /* Need more data */
-                return NULL;
-            }
-
-            p_sys->i_offset = 1; /* To find next startcode */
-
-        case STATE_NEXT_SYNC:
-            /* TODO: If p_block == NULL, flush the buffer without checking the
-             * next sync word */
-
-            /* Find the next startcode */
-            if( block_FindStartcodeFromOffset( &p_sys->bytestream,
-                    &p_sys->i_offset, p_sys->p_startcode, 3 ) != VLC_SUCCESS )
-            {
-                /* Need more data */
-                return NULL;
-            }
-
-            /* Get the new fragment and set the pts/dts */
-            p_pic = block_New( p_dec, p_sys->i_offset );
-            block_BytestreamFlush( &p_sys->bytestream );
-            p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
-            p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
-
-            block_GetBytes( &p_sys->bytestream, p_pic->p_buffer,
-                            p_pic->i_buffer );
-
-            /* don't reuse the same timestamps several times */
-            if( p_pic->i_buffer >= 4 && p_pic->p_buffer[3] == 0x00 )
-            {
-                /* We have a picture start code */
-                p_sys->bytestream.p_block->i_pts = 0;
-                p_sys->bytestream.p_block->i_dts = 0;
-            }
-
-            p_sys->i_offset = 0;
-
-            /* Get picture if any */
-            if( !( p_pic = ParseMPEGBlock( p_dec, p_pic ) ) )
-            {
-                p_sys->i_state = STATE_NOSYNC;
-                break;
-            }
 
-            /* If a discontinuity has been encountered, then wait till
-             * the next Intra frame before continuing with packetizing */
-            if( p_sys->b_discontinuity &&
-                p_sys->b_sync_on_intra_frame )
-            {
-                if( p_pic->i_flags & BLOCK_FLAG_TYPE_I )
-                {
-                    msg_Dbg( p_dec, "synced on intra frame" );
-                    p_sys->b_discontinuity = VLC_FALSE;
-                    p_pic->i_flags |= BLOCK_FLAG_DISCONTINUITY;
-                }
-                else
-                {
-                    msg_Dbg( p_dec, "waiting on intra frame" );
-                    p_sys->i_state = STATE_NOSYNC;
-                    block_Release( p_pic );
-                    break;
-                }
-            }
-
-            /* We've just started the stream, wait for the first PTS.
-             * We discard here so we can still get the sequence header. */
-            if( p_sys->i_dts <= 0 && p_sys->i_pts <= 0 &&
-                p_sys->i_interpolated_dts <= 0 )
-            {
-                msg_Dbg( p_dec, "need a starting pts/dts" );
-                p_sys->i_state = STATE_NOSYNC;
-                block_Release( p_pic );
-                break;
-            }
-
-            /* When starting the stream we can have the first frame with
-             * a null DTS (i_interpolated_pts is initialized to 0) */
-            if( !p_pic->i_dts ) p_pic->i_dts = p_pic->i_pts;
-
-            /* So p_block doesn't get re-added several times */
-            *pp_block = block_BytestreamPop( &p_sys->bytestream );
-
-            p_sys->i_state = STATE_NOSYNC;
-
-            return p_pic;
-        }
-    }
+    return packetizer_Packetize( &p_sys->packetizer, pp_block );
 }
 
 /*****************************************************************************
  * GetCc:
  *****************************************************************************/
-static block_t *GetCc( decoder_t *p_dec, vlc_bool_t pb_present[4] )
+static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
     block_t *p_cc;
@@ -414,6 +277,76 @@ static block_t *GetCc( decoder_t *p_dec, vlc_bool_t pb_present[4] )
     return p_cc;
 }
 
+/*****************************************************************************
+ * Helpers:
+ *****************************************************************************/
+static void PacketizeReset( void *p_private, bool b_broken )
+{
+    decoder_t *p_dec = p_private;
+    decoder_sys_t *p_sys = p_dec->p_sys;
+
+    if( b_broken )
+    {
+        p_sys->b_discontinuity = true;
+        if( p_sys->p_frame )
+            block_ChainRelease( p_sys->p_frame );
+        p_sys->p_frame = NULL;
+        p_sys->pp_last = &p_sys->p_frame;
+        p_sys->b_frame_slice = false;
+    }
+    p_sys->i_dts =
+    p_sys->i_pts =
+    p_sys->i_interpolated_dts =
+    p_sys->i_last_ref_pts = VLC_TS_INVALID;
+}
+
+static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t *p_block )
+{
+    decoder_t *p_dec = p_private;
+
+    /* Check if we have a picture start code */
+    *pb_ts_used = p_block->p_buffer[3] == 0x00;
+
+    return ParseMPEGBlock( p_dec, p_block );
+}
+
+
+static int PacketizeValidate( void *p_private, block_t *p_au )
+{
+    decoder_t *p_dec = p_private;
+    decoder_sys_t *p_sys = p_dec->p_sys;
+
+    /* If a discontinuity has been encountered, then wait till
+     * the next Intra frame before continuing with packetizing */
+    if( p_sys->b_discontinuity &&
+        p_sys->b_sync_on_intra_frame )
+    {
+        if( (p_au->i_flags & BLOCK_FLAG_TYPE_I) == 0 )
+        {
+            msg_Dbg( p_dec, "waiting on intra frame" );
+            return VLC_EGENERIC;
+        }
+        msg_Dbg( p_dec, "synced on intra frame" );
+        p_sys->b_discontinuity = false;
+        p_au->i_flags |= BLOCK_FLAG_DISCONTINUITY;
+    }
+
+    /* We've just started the stream, wait for the first PTS.
+     * We discard here so we can still get the sequence header. */
+    if( p_sys->i_dts <= VLC_TS_INVALID && p_sys->i_pts <= VLC_TS_INVALID &&
+        p_sys->i_interpolated_dts <= VLC_TS_INVALID )
+    {
+        msg_Dbg( p_dec, "need a starting pts/dts" );
+        return VLC_EGENERIC;
+    }
+
+    /* When starting the stream we can have the first frame with
+     * an invalid DTS (i_interpolated_pts is initialized to VLC_TS_INVALID) */
+    if( p_au->i_dts <= VLC_TS_INVALID )
+        p_au->i_dts = p_au->i_pts;
+
+    return VLC_SUCCESS;
+}
 /*****************************************************************************
  * ParseMPEGBlock: Re-assemble fragments into a block containing a picture
  *****************************************************************************/
@@ -435,16 +368,27 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
         if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
         p_sys->p_frame = NULL;
         p_sys->pp_last = &p_sys->p_frame;
-        p_sys->b_frame_slice = VLC_FALSE;
+        p_sys->b_frame_slice = false;
 
     }
     else if( p_sys->b_frame_slice &&
              (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) )
     {
+        const bool b_eos = p_frag->p_buffer[3] == 0xb7;
+
         mtime_t i_duration;
 
+        if( b_eos )
+        {
+            block_ChainLastAppend( &p_sys->pp_last, p_frag );
+            p_frag = NULL;
+        }
+
         p_pic = block_ChainGather( p_sys->p_frame );
 
+        if( b_eos )
+            p_pic->i_flags |= BLOCK_FLAG_END_OF_SEQUENCE;
+
         i_duration = (mtime_t)( 1000000 * p_sys->i_frame_rate_base /
                                 p_sys->i_frame_rate );
 
@@ -481,15 +425,18 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
         {
             /* Trivial case (DTS == PTS) */
             /* Correct interpolated dts when we receive a new pts/dts */
-            if( p_sys->i_pts > 0 ) p_sys->i_interpolated_dts = p_sys->i_pts;
-            if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
+            if( p_sys->i_pts > VLC_TS_INVALID )
+                p_sys->i_interpolated_dts = p_sys->i_pts;
+            if( p_sys->i_dts > VLC_TS_INVALID )
+                p_sys->i_interpolated_dts = p_sys->i_dts;
         }
         else
         {
             /* Correct interpolated dts when we receive a new pts/dts */
-            if( p_sys->i_last_ref_pts > 0 && !p_sys->b_second_field )
+            if(p_sys->i_last_ref_pts > VLC_TS_INVALID && !p_sys->b_second_field)
                 p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
-            if( p_sys->i_dts > 0 ) p_sys->i_interpolated_dts = p_sys->i_dts;
+            if( p_sys->i_dts > VLC_TS_INVALID )
+                p_sys->i_interpolated_dts = p_sys->i_dts;
 
             if( !p_sys->b_second_field )
                 p_sys->i_last_ref_pts = p_sys->i_pts;
@@ -499,7 +446,7 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
         p_sys->i_interpolated_dts += i_duration;
 
         /* Set PTS only if we have a B frame or if it comes from the stream */
-        if( p_sys->i_pts > 0 )
+        if( p_sys->i_pts > VLC_TS_INVALID )
         {
             p_pic->i_pts = p_sys->i_pts;
         }
@@ -509,7 +456,7 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
         }
         else
         {
-            p_pic->i_pts = 0;
+            p_pic->i_pts = VLC_TS_INVALID;
         }
 
         switch ( p_sys->i_picture_type )
@@ -528,14 +475,14 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
         p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts;
 
 #if 0
-        msg_Dbg( p_dec, "pic: type=%d dts="I64Fd" pts-dts="I64Fd,
+        msg_Dbg( p_dec, "pic: type=%d dts=%"PRId64" pts-dts=%"PRId64,
         p_sys->i_picture_type, p_pic->i_dts, p_pic->i_pts - p_pic->i_dts);
 #endif
 
         /* Reset context */
         p_sys->p_frame = NULL;
         p_sys->pp_last = &p_sys->p_frame;
-        p_sys->b_frame_slice = VLC_FALSE;
+        p_sys->b_frame_slice = false;
 
         if( p_sys->i_picture_structure != 0x03 )
         {
@@ -547,7 +494,7 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
         }
 
         /* CC */
-        p_sys->b_cc_reset = VLC_TRUE;
+        p_sys->b_cc_reset = true;
         p_sys->i_cc_pts = p_pic->i_pts;
         p_sys->i_cc_dts = p_pic->i_dts;
         p_sys->i_cc_flags = p_pic->i_flags;
@@ -555,10 +502,12 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
 
     if( !p_pic && p_sys->b_cc_reset )
     {
-        p_sys->b_cc_reset = VLC_FALSE;
+        p_sys->b_cc_reset = false;
         cc_Flush( &p_sys->cc );
     }
 
+    if( !p_frag )
+        return p_pic;
     /*
      * Check info of current fragment
      */
@@ -568,7 +517,7 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
         if( p_sys->p_seq &&
             p_sys->i_seq_old > p_sys->i_frame_rate/p_sys->i_frame_rate_base )
         {
-            /* Usefull for mpeg1: repeat sequence header every second */
+            /* Useful for mpeg1: repeat sequence header every second */
             block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_seq ) );
             if( p_sys->p_ext )
             {
@@ -615,8 +564,8 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
         p_dec->fmt_out.video.i_frame_rate = p_sys->i_frame_rate;
         p_dec->fmt_out.video.i_frame_rate_base = p_sys->i_frame_rate_base;
 
-        p_sys->b_seq_progressive = VLC_TRUE;
-        p_sys->b_low_delay = VLC_TRUE;
+        p_sys->b_seq_progressive = true;
+        p_sys->b_low_delay = true;
 
         if ( !p_sys->b_inited )
         {
@@ -649,9 +598,9 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
             if( p_frag->i_buffer >= 10 )
             {
                 p_sys->b_seq_progressive =
-                    p_frag->p_buffer[5]&0x08 ? VLC_TRUE : VLC_FALSE;
+                    p_frag->p_buffer[5]&0x08 ? true : false;
                 p_sys->b_low_delay =
-                    p_frag->p_buffer[9]&0x80 ? VLC_TRUE : VLC_FALSE;
+                    p_frag->p_buffer[9]&0x80 ? true : false;
             }
 
             /* Do not set aspect ratio : in case we're transcoding,
@@ -661,10 +610,12 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
              * of aspect ratio change, we're screwed. --Meuuh
              */
 #if 0
-            p_dec->fmt_out.video.i_aspect =
+            p_dec->fmt_out.video.i_sar_num =
                 mpeg2_aspect[p_sys->i_aspect_ratio_info][0] *
-                VOUT_ASPECT_FACTOR /
-                mpeg2_aspect[p_sys->i_aspect_ratio_info][1];
+                p_dec->fmt_out.video.i_height;
+            p_dec->fmt_out.video.i_sar_den =
+                mpeg2_aspect[p_sys->i_aspect_ratio_info][1] *
+                p_dec->fmt_out.video.i_width;
 #endif
 
         }
@@ -679,7 +630,8 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
     }
     else if( p_frag->p_buffer[3] == 0xb2 && p_frag->i_buffer > 4 )
     {
-        cc_Extract( &p_sys->cc, &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
+        cc_Extract( &p_sys->cc, p_sys->i_top_field_first,
+                    &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
     }
     else if( p_frag->p_buffer[3] == 0x00 )
     {
@@ -699,7 +651,7 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
     else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf )
     {
         /* Slice start code */
-        p_sys->b_frame_slice = VLC_TRUE;
+        p_sys->b_frame_slice = true;
     }
 
     /* Append the block */