]> git.sesse.net Git - vlc/blobdiff - modules/packetizer/mpegvideo.c
Qt4 - Simple Preferences, audio: you expect to deal with % for the volume not abstrac...
[vlc] / modules / packetizer / mpegvideo.c
index c5ae881f496a598b8ef0a0b1a79ce8f6474736dd..19fd9543e36763934dd5e265c6571bd6912839d3 100644 (file)
 /*****************************************************************************
  * 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_block_helper.h"
+#include <vlc/vlc.h>
+#include <vlc_block.h>
+#include <vlc_codec.h>
+#include <vlc_block_helper.h>
+#include "../codec/cc.h"
 
 #define SYNC_INTRAFRAME_TEXT N_("Sync on Intra Frame")
 #define SYNC_INTRAFRAME_LONGTEXT N_("Normally the packetizer would " \
@@ -76,6 +79,7 @@ vlc_module_end();
  *****************************************************************************/
 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
 {
@@ -84,7 +88,7 @@ struct decoder_sys_t
      */
     block_bytestream_t bytestream;
     int i_state;
-    int i_offset;
+    size_t i_offset;
     uint8_t p_startcode[3];
 
     /* Sequence header and extension */
@@ -116,8 +120,8 @@ struct decoder_sys_t
     int i_progressive_frame;
 
     mtime_t i_interpolated_dts;
-    mtime_t i_old_duration;
     mtime_t i_last_ref_pts;
+    vlc_bool_t b_second_field;
 
     /* Number of pictures since last sequence header */
     int i_seq_old;
@@ -125,6 +129,13 @@ struct decoder_sys_t
     /* Sync behaviour */
     vlc_bool_t  b_sync_on_intra_frame;
     vlc_bool_t  b_discontinuity;
+
+    /* */
+    vlc_bool_t b_cc_reset;
+    uint32_t i_cc_flags;
+    mtime_t i_cc_pts;
+    mtime_t i_cc_dts;
+    cc_data_t cc;
 };
 
 enum {
@@ -149,12 +160,13 @@ static int Open( vlc_object_t *p_this )
 
     es_format_Init( &p_dec->fmt_out, VIDEO_ES, VLC_FOURCC('m','p','g','v') );
     p_dec->pf_packetize = Packetize;
+    p_dec->pf_get_cc = GetCc;
 
     p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
 
     /* Misc init */
     p_sys->i_state = STATE_NOSYNC;
-    p_sys->bytestream = block_BytestreamInit( p_dec );
+    p_sys->bytestream = block_BytestreamInit();
     p_sys->p_startcode[0] = 0;
     p_sys->p_startcode[1] = 0;
     p_sys->p_startcode[2] = 1;
@@ -183,14 +195,20 @@ static int Open( vlc_object_t *p_this )
     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->b_second_field = 0;
 
     p_sys->b_discontinuity = VLC_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->i_cc_pts = 0;
+    p_sys->i_cc_dts = 0;
+    p_sys->i_cc_flags = 0;
+    cc_Init( &p_sys->cc );
+
     return VLC_SUCCESS;
 }
 
@@ -235,19 +253,28 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
         return NULL;
     }
 
-    if( (*pp_block)->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
+    if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
     {
-        p_sys->i_state = STATE_NOSYNC;
-        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;
+        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 )
@@ -360,6 +387,33 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
     }
 }
 
+/*****************************************************************************
+ * GetCc:
+ *****************************************************************************/
+static block_t *GetCc( decoder_t *p_dec, vlc_bool_t 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_cc = block_New( p_dec, p_sys->cc.i_data);
+    if( p_cc )
+    {
+        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;
+}
+
 /*****************************************************************************
  * ParseMPEGBlock: Re-assemble fragments into a block containing a picture
  *****************************************************************************/
@@ -433,14 +487,16 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
         else
         {
             /* Correct interpolated dts when we receive a new pts/dts */
-            if( p_sys->i_last_ref_pts > 0 )
+            if( p_sys->i_last_ref_pts > 0 && !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;
 
-            p_sys->i_last_ref_pts = p_sys->i_pts;
+            if( !p_sys->b_second_field )
+                p_sys->i_last_ref_pts = p_sys->i_pts;
         }
 
         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 > 0 )
@@ -456,17 +512,6 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
             p_pic->i_pts = 0;
         }
 
-        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;
-        }
-
         switch ( p_sys->i_picture_type )
         {
         case 0x01:
@@ -491,6 +536,27 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
         p_sys->p_frame = NULL;
         p_sys->pp_last = &p_sys->p_frame;
         p_sys->b_frame_slice = VLC_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 = VLC_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 = VLC_FALSE;
+        cc_Flush( &p_sys->cc );
     }
 
     /*
@@ -564,7 +630,7 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
     {
         int i_type = p_frag->p_buffer[4] >> 4;
 
-        /* Extention start code */
+        /* Extension start code */
         if( i_type == 0x01 )
         {
 #if 0
@@ -576,7 +642,7 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
             };
 #endif
 
-            /* sequence extention */
+            /* sequence extension */
             if( p_sys->p_ext) block_Release( p_sys->p_ext );
             p_sys->p_ext = block_Duplicate( p_frag );
 
@@ -604,13 +670,17 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
         }
         else if( i_type == 0x08 )
         {
-            /* picture extention */
+            /* 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;
         }
     }
+    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 );
+    }
     else if( p_frag->p_buffer[3] == 0x00 )
     {
         /* Picture start code */