]> git.sesse.net Git - vlc/blobdiff - modules/packetizer/h264.c
Do not loose pts on stream properties changes.
[vlc] / modules / packetizer / h264.c
index 3baac0cbf150cba3117b0b8367d5101c67b5a821..1f8ed09efdcc0176f88178d179526d71748a66c7 100644 (file)
@@ -31,6 +31,7 @@
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
+#include <assert.h>
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
@@ -38,8 +39,8 @@
 #include <vlc_codec.h>
 #include <vlc_block.h>
 
-#include "vlc_block_helper.h"
-#include "vlc_bits.h"
+#include <vlc_block_helper.h>
+#include <vlc_bits.h>
 #include "../codec/cc.h"
 #include "packetizer_helper.h"
 
@@ -151,6 +152,8 @@ enum nal_priority_e
     NAL_PRIORITY_HIGHEST    = 3,
 };
 
+#define BLOCK_FLAG_PRIVATE_AUD (1 << BLOCK_FLAG_PRIVATE_SHIFT)
+
 static block_t *Packetize( decoder_t *, block_t ** );
 static block_t *PacketizeAVC1( decoder_t *, block_t ** );
 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] );
@@ -221,8 +224,8 @@ static int Open( vlc_object_t *p_this )
     p_sys->slice.i_pic_order_cnt_lsb = -1;
     p_sys->slice.i_delta_pic_order_cnt_bottom = -1;
 
-    p_sys->i_frame_dts = -1;
-    p_sys->i_frame_pts = -1;
+    p_sys->i_frame_dts = VLC_TS_INVALID;
+    p_sys->i_frame_pts = VLC_TS_INVALID;
 
     /* Setup properties */
     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
@@ -340,8 +343,8 @@ static int Open( vlc_object_t *p_this )
         p_dec->pf_get_cc = GetCc;
 
         /* */
-        p_sys->i_cc_pts = 0;
-        p_sys->i_cc_dts = 0;
+        p_sys->i_cc_pts = VLC_TS_INVALID;
+        p_sys->i_cc_dts = VLC_TS_INVALID;
         p_sys->i_cc_flags = 0;
         cc_Init( &p_sys->cc );
         cc_Init( &p_sys->cc_next );
@@ -502,8 +505,8 @@ static void PacketizeReset( void *p_private, bool b_broken )
         p_sys->slice.i_frame_type = 0;
         p_sys->b_slice = false;
     }
-    p_sys->i_frame_pts = -1;
-    p_sys->i_frame_dts = -1;
+    p_sys->i_frame_pts = VLC_TS_INVALID;
+    p_sys->i_frame_dts = VLC_TS_INVALID;
 }
 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t *p_block )
 {
@@ -663,7 +666,21 @@ static block_t *ParseNALBlock( decoder_t *p_dec, bool *pb_used_ts, block_t *p_fr
 
         /* Parse SEI for CC support */
         if( i_nal_type == NAL_SEI )
+        {
             ParseSei( p_dec, p_frag );
+        }
+        else if( i_nal_type == NAL_AU_DELIMITER )
+        {
+            if( p_sys->p_frame && (p_sys->p_frame->i_flags & BLOCK_FLAG_PRIVATE_AUD) )
+            {
+                block_Release( p_frag );
+                p_frag = NULL;
+            }
+            else
+            {
+                p_frag->i_flags |= BLOCK_FLAG_PRIVATE_AUD;
+            }
+        }
     }
 
     /* Append the block */
@@ -671,7 +688,8 @@ static block_t *ParseNALBlock( decoder_t *p_dec, bool *pb_used_ts, block_t *p_fr
         block_ChainAppend( &p_sys->p_frame, p_frag );
 
     *pb_used_ts = false;
-    if( p_sys->i_frame_dts < 0 && p_sys->i_frame_pts < 0 )
+    if( p_sys->i_frame_dts <= VLC_TS_INVALID && 
+        p_sys->i_frame_pts <= VLC_TS_INVALID )
     {
         p_sys->i_frame_dts = i_frag_dts;
         p_sys->i_frame_pts = i_frag_pts;
@@ -690,15 +708,20 @@ static block_t *OutputPicture( decoder_t *p_dec )
 
     if( p_sys->slice.i_frame_type == BLOCK_FLAG_TYPE_I && p_sys->b_sps && p_sys->b_pps )
     {
-        block_t *p_list = NULL;
-        int i;
+        block_t *p_head = NULL;
+        if( p_sys->p_frame->i_flags & BLOCK_FLAG_PRIVATE_AUD )
+        {
+            p_head = p_sys->p_frame;
+            p_sys->p_frame = p_sys->p_frame->p_next;
+        }
 
-        for( i = 0; i < SPS_MAX; i++ )
+        block_t *p_list = NULL;
+        for( int i = 0; i < SPS_MAX; i++ )
         {
             if( p_sys->pp_sps[i] )
                 block_ChainAppend( &p_list, block_Duplicate( p_sys->pp_sps[i] ) );
         }
-        for( i = 0; i < PPS_MAX; i++ )
+        for( int i = 0; i < PPS_MAX; i++ )
         {
             if( p_sys->pp_pps[i] )
                 block_ChainAppend( &p_list, block_Duplicate( p_sys->pp_pps[i] ) );
@@ -706,8 +729,13 @@ static block_t *OutputPicture( decoder_t *p_dec )
         if( p_list )
             p_sys->b_header = true;
 
-        block_ChainAppend( &p_list, p_sys->p_frame );
-        p_pic = block_ChainGather( p_list );
+        if( p_head )
+            p_head->p_next = p_list;
+        else
+            p_head = p_list;
+        block_ChainAppend( &p_head, p_sys->p_frame );
+
+        p_pic = block_ChainGather( p_head );
     }
     else
     {
@@ -717,11 +745,12 @@ static block_t *OutputPicture( decoder_t *p_dec )
     p_pic->i_pts = p_sys->i_frame_pts;
     p_pic->i_length = 0;    /* FIXME */
     p_pic->i_flags |= p_sys->slice.i_frame_type;
+    p_pic->i_flags &= ~BLOCK_FLAG_PRIVATE_AUD;
 
     p_sys->slice.i_frame_type = 0;
     p_sys->p_frame = NULL;
-    p_sys->i_frame_dts = -1;
-    p_sys->i_frame_pts = -1;
+    p_sys->i_frame_dts = VLC_TS_INVALID;
+    p_sys->i_frame_pts = VLC_TS_INVALID;
     p_sys->b_slice = false;
 
     /* CC */
@@ -754,8 +783,10 @@ static void PutSPS( decoder_t *p_dec, block_t *p_frag )
 
     bs_init( &s, pb_dec, i_dec );
     int i_profile_idc = bs_read( &s, 8 );
-    /* Skip constraint_set0123, reserved(4), level(8) */
-    bs_skip( &s, 1+1+1+1 + 4 + 8 );
+    p_dec->fmt_out.i_profile = i_profile_idc;
+    /* Skip constraint_set0123, reserved(4) */
+    bs_skip( &s, 1+1+1+1 + 4 );
+    p_dec->fmt_out.i_level = bs_read( &s, 8 );
     /* sps id */
     i_sps_id = bs_read_ue( &s );
     if( i_sps_id >= SPS_MAX )
@@ -910,12 +941,16 @@ static void PutSPS( decoder_t *p_dec, block_t *p_frag )
                 h = 0;
             }
 
-            if( h != 0 )
-                p_dec->fmt_out.video.i_aspect = (int64_t)VOUT_ASPECT_FACTOR *
-                        ( w * p_dec->fmt_out.video.i_width ) /
-                        ( h * p_dec->fmt_out.video.i_height);
+            if( w != 0 && h != 0 )
+            {
+                p_dec->fmt_out.video.i_sar_num = w;
+                p_dec->fmt_out.video.i_sar_den = h;
+            }
             else
-                p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR;
+            {
+                p_dec->fmt_out.video.i_sar_num = 1;
+                p_dec->fmt_out.video.i_sar_den = 1;
+            }
         }
     }
 
@@ -1122,7 +1157,7 @@ static void ParseSei( decoder_t *p_dec, block_t *p_frag )
             if( i_t35 >= 5 &&
                 !memcmp( p_t35, p_dvb1_data_start_code, sizeof(p_dvb1_data_start_code) ) )
             {
-                cc_Extract( &p_sys->cc_next, &p_t35[3], i_t35 - 3 );
+                cc_Extract( &p_sys->cc_next, true, &p_t35[3], i_t35 - 3 );
             }
         }
         i_used += i_size;