]> git.sesse.net Git - vlc/blobdiff - modules/packetizer/mpegvideo.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / modules / packetizer / mpegvideo.c
index ff3815a8ec9a150a068971e30358941e558b8e91..cad9545f6070dbcb1b08e67703468297005d4472 100644 (file)
@@ -1,11 +1,13 @@
 /*****************************************************************************
- * mpegvideo.c
+ * mpegvideo.c: parse and packetize an MPEG1/2 video stream
  *****************************************************************************
- * Copyright (C) 2001, 2002 VideoLAN
- * $Id: mpegvideo.c,v 1.23 2003/11/27 22:44:50 massiot Exp $
+ * Copyright (C) 2001-2006 the VideoLAN team
+ * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Eric Petit <titer@videolan.org>
+ *          Gildas Bazin <gbazin@videolan.org>
+ *          Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc(), free() */
 
-#include <vlc/vlc.h>
-#include <vlc/decoder.h>
-#include <vlc/input.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#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 " \
+    "sync on the next full frame. This flags instructs the packetizer " \
+    "to sync on the first Intra Frame found.")
 
 /*****************************************************************************
  * Module descriptor
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
-vlc_module_begin();
-    set_description( _("MPEG-I/II video packetizer") );
-    set_capability( "packetizer", 50 );
-    set_callbacks( Open, Close );
-vlc_module_end();
+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", false, NULL, SYNC_INTRAFRAME_TEXT,
+              SYNC_INTRAFRAME_LONGTEXT, true )
+vlc_module_end ()
 
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-static block_t *Packetize( decoder_t *, block_t ** );
-
-static int mpgv_FindStartCode( uint8_t **pp_start, uint8_t *p_end );
-
 struct decoder_sys_t
 {
-    /* sequence header and extention */
+    /*
+     * Input properties
+     */
+    packetizer_t packetizer;
+
+    /* Sequence header and extension */
     block_t *p_seq;
     block_t *p_ext;
 
-    /* current frame being building */
+    /* Current frame being built */
     block_t    *p_frame;
-    vlc_bool_t b_frame_slice;
-    vlc_bool_t b_frame_corrupted;
-    vlc_bool_t b_gop;
+    block_t    **pp_last;
 
-    /* pts of current picture */
+    bool b_frame_slice;
     mtime_t i_pts;
     mtime_t i_dts;
 
-    /* gathering buffer */
-    int         i_buffer;
-    int         i_buffer_size;
-    uint8_t     *p_buffer;
-    uint8_t     *p_start, *p_old;
-
-    /* */
+    /* 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;
+    bool  b_inited;
 
-    /* */
+    /* Picture properties */
     int i_temporal_ref;
     int i_picture_type;
     int i_picture_structure;
@@ -101,16 +115,36 @@ struct decoder_sys_t
     int i_repeat_first_field;
     int i_progressive_frame;
 
-    /* */
-    int     i_seq_old;  /* How many picture from last seq */
-
-    /* */
-
     mtime_t i_interpolated_dts;
-    mtime_t i_old_duration;
     mtime_t i_last_ref_pts;
+    bool b_second_field;
+
+    /* Number of pictures since last sequence header */
+    int i_seq_old;
+
+    /* Sync behaviour */
+    bool  b_sync_on_intra_frame;
+    bool  b_discontinuity;
+
+    /* */
+    bool b_cc_reset;
+    uint32_t i_cc_flags;
+    mtime_t i_cc_pts;
+    mtime_t i_cc_dts;
+    cc_data_t cc;
 };
 
+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:
  *****************************************************************************/
@@ -119,38 +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 */
+    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->b_frame_slice = VLC_FALSE;
-    p_sys->b_frame_corrupted = VLC_FALSE;
-    p_sys->b_gop = VLC_FALSE;
-
-    p_sys->i_buffer = 0;
-    p_sys->i_buffer_size = 10000;
-    p_sys->p_buffer = malloc( p_sys->i_buffer_size );
-    p_sys->p_start = p_sys->p_buffer;
-    p_sys->p_old = NULL;
+    p_sys->pp_last = &p_sys->p_frame;
+    p_sys->b_frame_slice = false;
 
-    p_sys->i_dts = 0;
-    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;
@@ -159,10 +193,22 @@ static int Open( vlc_object_t *p_this )
     p_sys->i_top_field_first = 0;
     p_sys->i_repeat_first_field = 0;
     p_sys->i_progressive_frame = 0;
+    p_sys->b_inited = 0;
 
-    p_sys->i_interpolated_dts = 0;
-    p_sys->i_old_duration = 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 = 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 = false;
+    p_sys->i_cc_pts = 0;
+    p_sys->i_cc_dts = 0;
+    p_sys->i_cc_flags = 0;
+    cc_Init( &p_sys->cc );
 
     return VLC_SUCCESS;
 }
@@ -185,9 +231,12 @@ static void Close( vlc_object_t *p_this )
     }
     if( p_sys->p_frame )
     {
-        block_Release( p_sys->p_frame );
+        block_ChainRelease( p_sys->p_frame );
     }
-    free( p_sys->p_buffer );
+    packetizer_Clean( &p_sys->packetizer );
+
+    var_Destroy( p_dec, "packetizer-mpegvideo-sync-iframe" );
+
     free( p_sys );
 }
 
@@ -197,323 +246,416 @@ 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_chain_out = NULL;
-    block_t       *p_block;
 
-    if( pp_block == NULL || *pp_block == NULL )
-    {
+    return packetizer_Packetize( &p_sys->packetizer, pp_block );
+}
+
+/*****************************************************************************
+ * GetCc:
+ *****************************************************************************/
+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;
+    int i;
+
+    for( i = 0; i < 4; i++ )
+        pb_present[i] = p_sys->cc.pb_present[i];
+
+    if( p_sys->cc.i_data <= 0 )
         return NULL;
-    }
-    p_block = *pp_block;
-    *pp_block = NULL;
 
-    if( p_block->b_discontinuity )
+    p_cc = block_New( p_dec, p_sys->cc.i_data);
+    if( p_cc )
     {
-        p_sys->b_frame_corrupted = VLC_TRUE;
+        memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
+        p_cc->i_dts = 
+        p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
+        p_cc->i_flags = ( p_sys->cc.b_reorder  ? p_sys->i_cc_flags : BLOCK_FLAG_TYPE_P ) & ( BLOCK_FLAG_TYPE_I|BLOCK_FLAG_TYPE_P|BLOCK_FLAG_TYPE_B);
     }
+    cc_Flush( &p_sys->cc );
+    return p_cc;
+}
 
-    /* Append data */
-    if( p_sys->i_buffer + p_block->i_buffer > p_sys->i_buffer_size )
+/*****************************************************************************
+ * 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 )
     {
-        uint8_t *p_buffer = p_sys->p_buffer;
+        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;
+}
 
-        p_sys->i_buffer_size += p_block->i_buffer + 1024;
-        p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
+static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t *p_block )
+{
+    decoder_t *p_dec = p_private;
 
-        if( p_sys->p_start )
-        {
-            p_sys->p_start = p_sys->p_start - p_buffer + p_sys->p_buffer;
-        }
-        if( p_sys->p_old )
+    /* 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 )
         {
-            p_sys->p_old = p_sys->p_old - p_buffer + p_sys->p_buffer;
+            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;
     }
-    memcpy( &p_sys->p_buffer[p_sys->i_buffer], p_block->p_buffer,
-            p_block->i_buffer );
-    p_sys->i_buffer += p_block->i_buffer;
 
+    /* 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;
 
-    if( p_sys->i_buffer > 10*1000000 )
+    return VLC_SUCCESS;
+}
+/*****************************************************************************
+ * ParseMPEGBlock: Re-assemble fragments into a block containing a picture
+ *****************************************************************************/
+static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
+{
+    decoder_sys_t *p_sys = p_dec->p_sys;
+    block_t *p_pic = NULL;
+
+    /*
+     * Check if previous picture is finished
+     */
+    if( ( p_sys->b_frame_slice &&
+          (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) ) &&
+          p_sys->p_seq == NULL )
     {
-        msg_Err( p_dec, "mmh reseting context" );
-        p_sys->i_buffer = 0;
-    }
+        /* We have a picture but without a sequence header we can't
+         * do anything */
+        msg_Dbg( p_dec, "waiting for sequence start" );
+        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;
 
-    /* Split data in block */
-    for( ;; )
+    }
+    else if( p_sys->b_frame_slice &&
+             (p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf) )
     {
-        if( mpgv_FindStartCode( &p_sys->p_start, &p_sys->p_buffer[p_sys->i_buffer] ) )
-        {
-            block_Release( p_block );
+        const bool b_eos = p_frag->p_buffer[3] == 0xb7;
 
-            if( p_sys->p_seq == NULL )
-            {
-                block_ChainRelease( p_chain_out );
-                return NULL;
-            }
-            return p_chain_out;
-        }
+        mtime_t i_duration;
 
-        if( p_sys->p_old )
+        if( b_eos )
         {
-            /* Extract the data */
-            int i_frag = p_sys->p_start - p_sys->p_old;
-            block_t *p_frag = block_New( p_dec, i_frag );
+            block_ChainLastAppend( &p_sys->pp_last, p_frag );
+            p_frag = NULL;
+        }
 
-            memcpy( p_frag->p_buffer, p_sys->p_old, i_frag );
-            if( i_frag < p_sys->i_buffer )
-            {
-                memmove( p_sys->p_buffer, &p_sys->p_buffer[i_frag],
-                         p_sys->i_buffer - i_frag );
-            }
-            p_sys->i_buffer -= i_frag;
-            p_sys->p_start -= i_frag;
-            p_sys->p_old   -= i_frag;
+        p_pic = block_ChainGather( p_sys->p_frame );
 
-            if( p_sys->b_frame_slice && ( p_frag->p_buffer[3] == 0x00 || p_frag->p_buffer[3] > 0xaf ) )
-            {
-                /* We have a complete picture output it */
-                if( p_sys->p_seq == NULL )
-                {
-                    msg_Dbg( p_dec, "waiting sequence start" );
-                    block_ChainRelease( p_sys->p_frame );
-                }
-                else 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" );
-                    block_ChainRelease( p_sys->p_frame );
-                }
-                else if( p_sys->b_frame_corrupted )
-                {
-                    msg_Warn( p_dec, "trashing a corrupted picture" );
-                    block_ChainRelease( p_sys->p_frame );
-                    p_sys->b_frame_corrupted = VLC_FALSE;
-                }
-                else
-                {
-                    block_t *p_pic = block_ChainGather( p_sys->p_frame );
-                    mtime_t i_duration = (mtime_t)( 1000000 * p_sys->i_frame_rate_base / p_sys->i_frame_rate);
-
-                    if( !p_sys->b_seq_progressive && p_sys->i_picture_structure != 0x03 )
-                    {
-                        i_duration /= 2;
-                    }
-
-                    if( p_sys->b_seq_progressive )
-                    {
-                        if( p_sys->i_top_field_first == 0 && p_sys->i_repeat_first_field == 1 )
-                        {
-                            i_duration *= 2;
-                        }
-                        else if( p_sys->i_top_field_first == 1 && p_sys->i_repeat_first_field == 1 )
-                        {
-                            i_duration *= 3;
-                        }
-                    }
-                    else
-                    {
-                        if( p_sys->i_picture_structure == 0x03 )
-                        {
-                            if( p_sys->i_progressive_frame && p_sys->i_repeat_first_field )
-                            {
-                                i_duration += i_duration / 2;
-                            }
-                        }
-                    }
-
-                    if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
-                    {
-                        /* 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;
-                    }
-                    else
-                    {
-                        /* Correct interpolated dts when we receive a new pts/dts */
-                        if( p_sys->i_last_ref_pts > 0 )
-                            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;
-
-                        p_sys->i_last_ref_pts = p_sys->i_pts;
-                    }
-
-                    p_pic->i_dts    = p_sys->i_interpolated_dts;
-                    /* Set PTS only if I frame or come from stream */
-                    if( p_sys->i_pts > 0 )
-                    {
-                        p_pic->i_pts    = p_sys->i_pts;
-                    }
-                    else if( p_sys->i_picture_type == 0x03 )
-                    {
-                        p_pic->i_pts = p_pic->i_dts;
-                    }
-                    else
-                    {
-                        p_pic->i_pts = -1;
-                    }
-
-                    if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
-                    {
-                        /* Trivial case (DTS == PTS) */
-                        p_sys->i_interpolated_dts += i_duration;
-                    }
-                    else
-                    {
-                        p_sys->i_interpolated_dts += p_sys->i_old_duration;
-                        p_sys->i_old_duration = i_duration;
-                    }
-
-                    p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts;
-
-                    //msg_Dbg( p_dec, "pic: type=%d dts=%lld pts-dts=%lld", p_sys->i_picture_type, p_pic->i_dts, p_pic->i_pts - p_pic->i_dts);
-
-                    block_ChainAppend( &p_chain_out, p_pic );
+        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 );
 
-                /* reset context */
-                p_sys->p_frame = NULL;
-                p_sys->b_frame_slice = VLC_FALSE;
-                p_sys->b_gop = VLC_FALSE;
-                p_sys->i_pts = 0;
-                p_sys->i_dts = 0;
-            }
+        if( !p_sys->b_seq_progressive && p_sys->i_picture_structure != 0x03 )
+        {
+            i_duration /= 2;
+        }
 
-            if( p_frag->p_buffer[3] == 0xb8 )
+        if( p_sys->b_seq_progressive )
+        {
+            if( p_sys->i_top_field_first == 0 &&
+                p_sys->i_repeat_first_field == 1 )
             {
-                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 */
-                    block_ChainAppend( &p_sys->p_frame,
-                                       block_Duplicate( p_sys->p_seq ) );
-                    if( p_sys->p_ext )
-                    {
-                        block_ChainAppend( &p_sys->p_frame,
-                                           block_Duplicate( p_sys->p_ext ) );
-                    }
-
-                    p_sys->i_seq_old = 0;
-                }
-                p_sys->b_gop = VLC_TRUE;
+                i_duration *= 2;
             }
-            else if( p_frag->p_buffer[3] == 0xb3 )
+            else if( p_sys->i_top_field_first == 1 &&
+                     p_sys->i_repeat_first_field == 1 )
             {
-                static const int code_to_frame_rate[16][2] =
-                {
-                    { 1, 1 },   /* invalid */
-                    { 24000, 1001 }, { 24, 1 }, { 25, 1 },       { 30000, 1001 },
-                    { 30, 1 },       { 50, 1 }, { 60000, 1001 }, { 60, 1 },
-                    { 1, 1 },        { 1, 1 },  { 1, 1 },        { 1, 1 },  /* invalid */
-                    { 1, 1 },        { 1, 1 },  { 1, 1 }                    /* invalid */
-                };
-
-                /* sequence header */
-                if( p_sys->p_seq )
-                {
-                    block_Release( p_sys->p_seq );
-                }
-                if( p_sys->p_ext )
+                i_duration *= 3;
+            }
+        }
+        else
+        {
+            if( p_sys->i_picture_structure == 0x03 )
+            {
+                if( p_sys->i_progressive_frame && p_sys->i_repeat_first_field )
                 {
-                    block_Release( p_sys->p_ext );
-                    p_sys->p_ext = NULL;
+                    i_duration += i_duration / 2;
                 }
-                p_sys->p_seq = block_Duplicate( p_frag );
-                p_sys->i_seq_old = 0;
+            }
+        }
 
-                p_dec->fmt_out.video.i_width = ( p_frag->p_buffer[4] << 4)|(p_frag->p_buffer[5] >> 4 );
-                p_dec->fmt_out.video.i_height= ( (p_frag->p_buffer[5]&0x0f) << 8 )|p_frag->p_buffer[6];
+        if( p_sys->b_low_delay || p_sys->i_picture_type == 0x03 )
+        {
+            /* Trivial case (DTS == PTS) */
+            /* Correct interpolated dts when we receive a new pts/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 > VLC_TS_INVALID && !p_sys->b_second_field)
+                p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
+            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;
+        }
 
-                p_sys->i_frame_rate = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][0];
-                p_sys->i_frame_rate_base = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][1];
+        p_pic->i_dts = p_sys->i_interpolated_dts;
+        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 > VLC_TS_INVALID )
+        {
+            p_pic->i_pts = p_sys->i_pts;
+        }
+        else if( p_sys->i_picture_type == 0x03 )
+        {
+            p_pic->i_pts = p_pic->i_dts;
+        }
+        else
+        {
+            p_pic->i_pts = VLC_TS_INVALID;
+        }
 
-                p_sys->b_seq_progressive = VLC_TRUE;
-                p_sys->b_low_delay = VLC_TRUE;
+        switch ( p_sys->i_picture_type )
+        {
+        case 0x01:
+            p_pic->i_flags |= BLOCK_FLAG_TYPE_I;
+            break;
+        case 0x02:
+            p_pic->i_flags |= BLOCK_FLAG_TYPE_P;
+            break;
+        case 0x03:
+            p_pic->i_flags |= BLOCK_FLAG_TYPE_B;
+            break;
+        }
+
+        p_pic->i_length = p_sys->i_interpolated_dts - p_pic->i_dts;
 
 #if 0
-                msg_Dbg( p_dec, "Size %dx%d fps=%.3f",
-                         p_dec->fmt_out.video.i_width,
-                         p_dec->fmt_out.video.i_height,
-                         (float)p_sys->i_frame_rate / (float)p_sys->i_frame_rate_base );
+        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
-            }
-            else if( p_frag->p_buffer[3] == 0xb5 )
+
+        /* Reset context */
+        p_sys->p_frame = NULL;
+        p_sys->pp_last = &p_sys->p_frame;
+        p_sys->b_frame_slice = false;
+
+        if( p_sys->i_picture_structure != 0x03 )
+        {
+            p_sys->b_second_field = !p_sys->b_second_field;
+        }
+        else
+        {
+            p_sys->b_second_field = 0;
+        }
+
+        /* CC */
+        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;
+    }
+
+    if( !p_pic && p_sys->b_cc_reset )
+    {
+        p_sys->b_cc_reset = false;
+        cc_Flush( &p_sys->cc );
+    }
+
+    if( !p_frag )
+        return p_pic;
+    /*
+     * Check info of current fragment
+     */
+    if( p_frag->p_buffer[3] == 0xb8 )
+    {
+        /* Group start code */
+        if( p_sys->p_seq &&
+            p_sys->i_seq_old > p_sys->i_frame_rate/p_sys->i_frame_rate_base )
+        {
+            /* 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 )
             {
-                int i_type = p_frag->p_buffer[4] >> 4;
-                /* extention start code */
-                if( i_type == 0x01 )
-                {
-                    /* sequence extention */
-                    if( p_sys->p_ext)
-                    {
-                        block_Release( p_sys->p_ext );
-                    }
-                    p_sys->p_ext = block_Duplicate( p_frag );
-
-                    if( p_frag->i_buffer >= 10 )
-                    {
-                        p_sys->b_seq_progressive = p_frag->p_buffer[5]&0x08 ? VLC_TRUE : VLC_FALSE;
-                        p_sys->b_low_delay = p_frag->p_buffer[9]&0x80 ? VLC_TRUE : VLC_FALSE;
-                    }
-                }
-                else if( i_type == 0x08 )
-                {
-                    /* picture extention */
-                    p_sys->i_picture_structure = p_frag->p_buffer[6]&0x03;
-                    p_sys->i_top_field_first   = p_frag->p_buffer[7] >> 7;
-                    p_sys->i_repeat_first_field= (p_frag->p_buffer[7]>>1)&0x01;
-                    p_sys->i_progressive_frame = p_frag->p_buffer[8] >> 7;
-                }
+                block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_ext ) );
             }
-            else if( p_frag->p_buffer[3] == 0x00 )
+
+            p_sys->i_seq_old = 0;
+        }
+    }
+    else if( p_frag->p_buffer[3] == 0xb3 && p_frag->i_buffer >= 8 )
+    {
+        /* Sequence header code */
+        static const int code_to_frame_rate[16][2] =
+        {
+            { 1, 1 },  /* invalid */
+            { 24000, 1001 }, { 24, 1 }, { 25, 1 },       { 30000, 1001 },
+            { 30, 1 },       { 50, 1 }, { 60000, 1001 }, { 60, 1 },
+            /* Unofficial 15fps from Xing*/
+            { 15, 1001 },
+            /* Unofficial economy rates from libmpeg3 */
+            { 5, 1001 }, { 10, 1001 }, { 12, 1001 }, { 15, 1001 },
+            { 1, 1 },  { 1, 1 }  /* invalid */
+        };
+
+        if( p_sys->p_seq ) block_Release( p_sys->p_seq );
+        if( p_sys->p_ext ) block_Release( p_sys->p_ext );
+
+        p_sys->p_seq = block_Duplicate( p_frag );
+        p_sys->i_seq_old = 0;
+        p_sys->p_ext = NULL;
+
+        p_dec->fmt_out.video.i_width =
+            ( p_frag->p_buffer[4] << 4)|(p_frag->p_buffer[5] >> 4 );
+        p_dec->fmt_out.video.i_height =
+            ( (p_frag->p_buffer[5]&0x0f) << 8 )|p_frag->p_buffer[6];
+        p_sys->i_aspect_ratio_info = p_frag->p_buffer[7] >> 4;
+
+        /* TODO: MPEG1 aspect ratio */
+
+        p_sys->i_frame_rate = code_to_frame_rate[p_frag->p_buffer[7]&0x0f][0];
+        p_sys->i_frame_rate_base =
+            code_to_frame_rate[p_frag->p_buffer[7]&0x0f][1];
+
+        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 = true;
+        p_sys->b_low_delay = true;
+
+        if ( !p_sys->b_inited )
+        {
+            msg_Dbg( p_dec, "size %dx%d fps=%.3f",
+                 p_dec->fmt_out.video.i_width, p_dec->fmt_out.video.i_height,
+                 p_sys->i_frame_rate / (float)p_sys->i_frame_rate_base );
+            p_sys->b_inited = 1;
+        }
+    }
+    else if( p_frag->p_buffer[3] == 0xb5 )
+    {
+        int i_type = p_frag->p_buffer[4] >> 4;
+
+        /* Extension start code */
+        if( i_type == 0x01 )
+        {
+#if 0
+            static const int mpeg2_aspect[16][2] =
             {
-                /* picture */
-                p_sys->i_seq_old++;
+                {0,1}, {1,1}, {4,3}, {16,9}, {221,100},
+                {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1}, {0,1},
+                {0,1}, {0,1}
+            };
+#endif
 
-                if( p_frag->i_buffer >= 6 )
-                {
-                    p_sys->i_temporal_ref = ( p_frag->p_buffer[4] << 2 )|(p_frag->p_buffer[5] >> 6);
-                    p_sys->i_picture_type = ( p_frag->p_buffer[5] >> 3 )&0x03;
-                }
-                if( !p_sys->b_frame_slice )
-                {
-                    p_sys->i_dts = p_block->i_dts; p_block->i_dts = 0;
-                    p_sys->i_pts = p_block->i_pts; p_block->i_pts = 0;
-                }
-            }
-            else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf )
+            /* sequence extension */
+            if( p_sys->p_ext) block_Release( p_sys->p_ext );
+            p_sys->p_ext = block_Duplicate( p_frag );
+
+            if( p_frag->i_buffer >= 10 )
             {
-                /* Slice */
-                p_sys->b_frame_slice = VLC_TRUE;
+                p_sys->b_seq_progressive =
+                    p_frag->p_buffer[5]&0x08 ? true : false;
+                p_sys->b_low_delay =
+                    p_frag->p_buffer[9]&0x80 ? true : false;
             }
 
-            /* Append the block */
-            block_ChainAppend( &p_sys->p_frame, p_frag );
+            /* Do not set aspect ratio : in case we're transcoding,
+             * transcode will take our fmt_out as a fmt_in to libmpeg2.
+             * libmpeg2.c will then believe that the user has requested
+             * a specific aspect ratio, which she hasn't. Thus in case
+             * of aspect ratio change, we're screwed. --Meuuh
+             */
+#if 0
+            p_dec->fmt_out.video.i_sar_num =
+                mpeg2_aspect[p_sys->i_aspect_ratio_info][0] *
+                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
+
+        }
+        else if( i_type == 0x08 )
+        {
+            /* picture extension */
+            p_sys->i_picture_structure = p_frag->p_buffer[6]&0x03;
+            p_sys->i_top_field_first   = p_frag->p_buffer[7] >> 7;
+            p_sys->i_repeat_first_field= (p_frag->p_buffer[7]>>1)&0x01;
+            p_sys->i_progressive_frame = p_frag->p_buffer[8] >> 7;
         }
-        p_sys->p_old = p_sys->p_start;
-        p_sys->p_start += 4;
     }
-}
-
-static int mpgv_FindStartCode( uint8_t **pp_start, uint8_t *p_end )
-{
-    uint8_t *p = *pp_start;
-
-    for( p = *pp_start; p < p_end - 4; p++ )
+    else if( p_frag->p_buffer[3] == 0xb2 && 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 )
     {
-        if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
+        /* Picture start code */
+        p_sys->i_seq_old++;
+
+        if( p_frag->i_buffer >= 6 )
         {
-            *pp_start = p;
-            return VLC_SUCCESS;
+            p_sys->i_temporal_ref =
+                ( p_frag->p_buffer[4] << 2 )|(p_frag->p_buffer[5] >> 6);
+            p_sys->i_picture_type = ( p_frag->p_buffer[5] >> 3 ) & 0x03;
         }
-    }
 
-    *pp_start = p;
-    return VLC_EGENERIC;
-}
+        p_sys->i_dts = p_frag->i_dts;
+        p_sys->i_pts = p_frag->i_pts;
+    }
+    else if( p_frag->p_buffer[3] >= 0x01 && p_frag->p_buffer[3] <= 0xaf )
+    {
+        /* Slice start code */
+        p_sys->b_frame_slice = true;
+    }
 
+    /* Append the block */
+    block_ChainLastAppend( &p_sys->pp_last, p_frag );
 
+    return p_pic;
+}