]> git.sesse.net Git - vlc/blobdiff - modules/packetizer/mpegvideo.c
Improvements to preferences
[vlc] / modules / packetizer / mpegvideo.c
index 8dd35d6d6dc1409e686620eaee5b3dc30b19ceb6..6e26d00ad3e91e7fdc2cb1802685a125ca2c1438 100644 (file)
@@ -2,11 +2,11 @@
  * mpegvideo.c: parse and packetize an MPEG1/2 video stream
  *****************************************************************************
  * Copyright (C) 2001, 2002 VideoLAN
- * $Id: mpegvideo.c,v 1.26 2004/01/25 17:41:30 murray Exp $
+ * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Eric Petit <titer@videolan.org>
- *          Gildas Bazin <gbazin@netcourrier.com>
+ *          Gildas Bazin <gbazin@videolan.org>
  *
  * 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
@@ -55,7 +55,9 @@ static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
 vlc_module_begin();
-    set_description( _("MPEG-I/II Video Packetizer") );
+    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_end();
@@ -76,12 +78,14 @@ struct decoder_sys_t
     int i_offset;
     uint8_t p_startcode[3];
 
-    /* Sequence header and extention */
+    /* Sequence header and extension */
     block_t *p_seq;
     block_t *p_ext;
 
     /* Current frame being built */
     block_t    *p_frame;
+    block_t    **pp_last;
+
     vlc_bool_t b_frame_slice;
     mtime_t i_pts;
     mtime_t i_dts;
@@ -92,6 +96,7 @@ struct decoder_sys_t
     vlc_bool_t  b_seq_progressive;
     vlc_bool_t  b_low_delay;
     int         i_aspect_ratio_info;
+    vlc_bool_t  b_inited;
 
     /* Picture properties */
     int i_temporal_ref;
@@ -105,7 +110,7 @@ struct decoder_sys_t
     mtime_t i_old_duration;
     mtime_t i_last_ref_pts;
 
-    /* Number of pictues since last sequence header */
+    /* Number of pictures since last sequence header */
     int i_seq_old;
 
 };
@@ -146,6 +151,7 @@ static int Open( vlc_object_t *p_this )
     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->i_dts = p_sys->i_pts = 0;
@@ -162,6 +168,7 @@ 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;
@@ -209,12 +216,15 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
         return NULL;
     }
 
-    if( (*pp_block)->b_discontinuity )
+    if( (*pp_block)->i_flags & BLOCK_FLAG_DISCONTINUITY )
     {
         p_sys->i_state = STATE_NOSYNC;
         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;
+        block_Release( *pp_block );
+        return NULL;
     }
 
     block_BytestreamPush( &p_sys->bytestream, *pp_block );
@@ -260,6 +270,7 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
 
             /* 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;
 
@@ -294,6 +305,10 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
                 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 );
 
@@ -324,6 +339,7 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
         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 = VLC_FALSE;
 
     }
@@ -410,6 +426,19 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
             p_sys->i_old_duration = i_duration;
         }
 
+        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
@@ -419,6 +448,7 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
 
         /* Reset context */
         p_sys->p_frame = NULL;
+        p_sys->pp_last = &p_sys->p_frame;
         p_sys->b_frame_slice = VLC_FALSE;
     }
 
@@ -432,12 +462,10 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
             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 ) );
+            block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_seq ) );
             if( p_sys->p_ext )
             {
-                block_ChainAppend( &p_sys->p_frame,
-                                   block_Duplicate( p_sys->p_ext ) );
+                block_ChainLastAppend( &p_sys->pp_last, block_Duplicate( p_sys->p_ext ) );
             }
 
             p_sys->i_seq_old = 0;
@@ -477,14 +505,19 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
         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 = VLC_TRUE;
         p_sys->b_low_delay = VLC_TRUE;
 
-#if 0
-        msg_Dbg( p_dec, "Size %dx%d fps=%.3f",
+        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 );
-#endif
+            p_sys->b_inited = 1;
+        }
     }
     else if( p_frag->p_buffer[3] == 0xb5 )
     {
@@ -549,7 +582,7 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
     }
 
     /* Append the block */
-    block_ChainAppend( &p_sys->p_frame, p_frag );
+    block_ChainLastAppend( &p_sys->pp_last, p_frag );
 
     return p_pic;
 }