]> git.sesse.net Git - vlc/blobdiff - modules/packetizer/mpegvideo.c
* modules/gui/wxwidgets/playlist.cpp: Fixed segfault. Patch by Brian Robb (vascy...
[vlc] / modules / packetizer / mpegvideo.c
index 4857e21a177d640fc69cd73cbe73cb6950f6cbf8..ac798f8803ccb9f8a9da9f719f908da998f20748 100644 (file)
@@ -1,12 +1,13 @@
 /*****************************************************************************
  * mpegvideo.c: parse and packetize an MPEG1/2 video stream
  *****************************************************************************
- * Copyright (C) 2001, 2002 VideoLAN
- * $Id: mpegvideo.c,v 1.31 2004/03/03 11:09:30 massiot Exp $
+ * Copyright (C) 2001-2005 VideoLAN (Centrale Réseaux) and its contributors
+ * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Eric Petit <titer@videolan.org>
- *          Gildas Bazin <gbazin@netcourrier.com>
+ *          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
 
 #include "vlc_block_helper.h"
 
+#define SYNC_INTRAFRAME_TEXT N_("Sync on intraframe")
+#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 intraframe found.")
+
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
@@ -55,9 +61,14 @@ 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 );
+
+    add_bool( "packetizer-mpegvideo-sync-iframe", 0, NULL, SYNC_INTRAFRAME_TEXT,
+              SYNC_INTRAFRAME_LONGTEXT, VLC_TRUE );
 vlc_module_end();
 
 /*****************************************************************************
@@ -82,6 +93,8 @@ struct decoder_sys_t
 
     /* 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;
@@ -108,7 +121,10 @@ struct decoder_sys_t
 
     /* 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;
 };
 
 enum {
@@ -147,6 +163,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;
@@ -169,6 +186,11 @@ static int Open( vlc_object_t *p_this )
     p_sys->i_old_duration = 0;
     p_sys->i_last_ref_pts = 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 happens on intraframe now." );
+        
     return VLC_SUCCESS;
 }
 
@@ -194,6 +216,8 @@ static void Close( vlc_object_t *p_this )
     {
         block_ChainRelease( p_sys->p_frame );
     }
+    
+    var_Destroy( p_dec, "packetizer-mpegvideo-sync-iframe" );
 
     free( p_sys );
 }
@@ -211,12 +235,17 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
         return NULL;
     }
 
-    if( (*pp_block)->i_flags&BLOCK_FLAG_DISCONTINUITY )
+    if( (*pp_block)->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
     {
         p_sys->i_state = STATE_NOSYNC;
-        if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
+        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;
+        block_Release( *pp_block );
+        return NULL;
     }
 
     block_BytestreamPush( &p_sys->bytestream, *pp_block );
@@ -262,6 +291,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;
 
@@ -285,6 +315,24 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
                 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
+                {
+                    p_sys->i_state = STATE_NOSYNC;
+                    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 &&
@@ -330,6 +378,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;
 
     }
@@ -438,6 +487,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;
     }
 
@@ -451,12 +501,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;
@@ -496,6 +544,9 @@ 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;
 
@@ -514,12 +565,14 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
         /* Extention start code */
         if( i_type == 0x01 )
         {
+#if 0
             static const int mpeg2_aspect[16][2] =
             {
                 {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
 
             /* sequence extention */
             if( p_sys->p_ext) block_Release( p_sys->p_ext );
@@ -533,10 +586,18 @@ static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
                     p_frag->p_buffer[9]&0x80 ? VLC_TRUE : VLC_FALSE;
             }
 
+            /* 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_aspect =
                 mpeg2_aspect[p_sys->i_aspect_ratio_info][0] *
                 VOUT_ASPECT_FACTOR /
                 mpeg2_aspect[p_sys->i_aspect_ratio_info][1];
+#endif
 
         }
         else if( i_type == 0x08 )
@@ -570,7 +631,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;
 }