]> git.sesse.net Git - vlc/blobdiff - modules/packetizer/h264.c
HACK for minimalView to go around a Qt bug/feature
[vlc] / modules / packetizer / h264.c
index 23e8ff948dbb76ac71ff298a54ed322111010ec7..abffa1f6cd8c43b04808ca810062faf4deb057ff 100644 (file)
@@ -83,6 +83,8 @@ typedef struct
     int i_delta_pic_order_cnt1;
 } slice_t;
 
+#define SPS_MAX (32)
+#define PPS_MAX (256)
 struct decoder_sys_t
 {
     block_bytestream_t bytestream;
@@ -91,17 +93,17 @@ struct decoder_sys_t
     size_t  i_offset;
     uint8_t startcode[4];
 
-    bool b_slice;
-    block_t    *p_frame;
+    bool    b_slice;
+    block_t *p_frame;
 
+    bool   b_header;
     bool   b_sps;
     bool   b_pps;
-    bool   b_header;
+    block_t *pp_sps[SPS_MAX];
+    block_t *pp_pps[PPS_MAX];
 
     /* avcC data */
     int i_avcC_length_size;
-    block_t *p_sps;
-    block_t *p_pps;
 
     /* Useful values of the Sequence Parameter Set */
     int i_log2_max_frame_num;
@@ -148,7 +150,7 @@ enum nal_priority_e
 
 static block_t *ParseNALBlock( decoder_t *, block_t * );
 
-static block_t *CreateAnnexbNAL( decoder_t *, uint8_t *p, int );
+static block_t *CreateAnnexbNAL( decoder_t *, const uint8_t *p, int );
 
 static block_t *OutputPicture( decoder_t *p_dec );
 static void PutSPS( decoder_t *p_dec, block_t *p_frag );
@@ -166,6 +168,7 @@ static int Open( vlc_object_t *p_this )
 {
     decoder_t     *p_dec = (decoder_t*)p_this;
     decoder_sys_t *p_sys;
+    int i;
 
     if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'h', '2', '6', '4') &&
         p_dec->fmt_in.i_codec != VLC_FOURCC( 'H', '2', '6', '4') &&
@@ -192,11 +195,13 @@ static int Open( vlc_object_t *p_this )
     p_sys->bytestream = block_BytestreamInit();
     p_sys->b_slice = false;
     p_sys->p_frame = NULL;
+    p_sys->b_header= false;
     p_sys->b_sps   = false;
     p_sys->b_pps   = false;
-    p_sys->p_sps   = 0;
-    p_sys->p_pps   = 0;
-    p_sys->b_header= false;
+    for( i = 0; i < SPS_MAX; i++ )
+        p_sys->pp_sps[i] = NULL;
+    for( i = 0; i < PPS_MAX; i++ )
+        p_sys->pp_pps[i] = NULL;
 
     p_sys->slice.i_nal_type = -1;
     p_sys->slice.i_nal_ref_idc = -1;
@@ -261,26 +266,53 @@ static int Open( vlc_object_t *p_this )
         msg_Dbg( p_dec, "avcC length size=%d, sps=%d, pps=%d",
                  p_sys->i_avcC_length_size, i_sps, i_pps );
 
-        if( !p_sys->p_sps || p_sys->p_pps )
+        if( !p_sys->b_sps || !p_sys->b_pps )
             return VLC_EGENERIC;
 
         /* FIXME: FFMPEG isn't happy at all if you leave this */
-        if( p_dec->fmt_out.i_extra > 0 ) free( p_dec->fmt_out.p_extra );
+        if( p_dec->fmt_out.i_extra > 0 )
+            free( p_dec->fmt_out.p_extra );
         p_dec->fmt_out.i_extra = 0;
         p_dec->fmt_out.p_extra = NULL;
 
         /* Set the new extradata */
-        p_dec->fmt_out.i_extra = p_sys->p_pps->i_buffer + p_sys->p_sps->i_buffer;
+        for( i = 0; i < SPS_MAX; i++ )
+        {
+            if( p_sys->pp_sps[i] )
+                p_dec->fmt_out.i_extra += p_sys->pp_sps[i]->i_buffer;
+        }
+        for( i = 0; i < PPS_MAX; i++ )
+        {
+            if( p_sys->pp_pps[i] )
+                p_dec->fmt_out.i_extra += p_sys->pp_pps[i]->i_buffer;
+        }
         p_dec->fmt_out.p_extra = malloc( p_dec->fmt_out.i_extra );
         if( p_dec->fmt_out.p_extra )
         {
-            memcpy( (uint8_t*)p_dec->fmt_out.p_extra,
-                    p_sys->p_sps->p_buffer, p_sys->p_sps->i_buffer);
-            memcpy( (uint8_t*)p_dec->fmt_out.p_extra+p_sys->p_sps->i_buffer,
-                    p_sys->p_pps->p_buffer, p_sys->p_pps->i_buffer);
+            uint8_t *p_dst = p_dec->fmt_out.p_extra;
+
+            for( i = 0; i < SPS_MAX; i++ )
+            {
+                if( p_sys->pp_sps[i] )
+                {
+                    memcpy( p_dst, p_sys->pp_sps[i]->p_buffer, p_sys->pp_sps[i]->i_buffer );
+                    p_dst += p_sys->pp_sps[i]->i_buffer;
+                }
+            }
+            for( i = 0; i < PPS_MAX; i++ )
+            {
+                if( p_sys->pp_pps[i] )
+                {
+                    memcpy( p_dst, p_sys->pp_pps[i]->p_buffer, p_sys->pp_pps[i]->i_buffer );
+                    p_dst += p_sys->pp_pps[i]->i_buffer;
+                }
+            }
             p_sys->b_header = true;
         }
-        else p_dec->fmt_out.i_extra = 0;
+        else
+        {
+            p_dec->fmt_out.i_extra = 0;
+        }
 
         /* Set callback */
         p_dec->pf_packetize = PacketizeAVC1;
@@ -321,10 +353,19 @@ static void Close( vlc_object_t *p_this )
 {
     decoder_t *p_dec = (decoder_t*)p_this;
     decoder_sys_t *p_sys = p_dec->p_sys;
+    int i;
 
     if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
-    if( p_sys->p_sps ) block_Release( p_sys->p_sps );
-    if( p_sys->p_pps ) block_Release( p_sys->p_pps );
+    for( i = 0; i < SPS_MAX; i++ )
+    {
+        if( p_sys->pp_sps[i] )
+            block_Release( p_sys->pp_sps[i] );
+    }
+    for( i = 0; i < PPS_MAX; i++ )
+    {
+        if( p_sys->pp_pps[i] )
+            block_Release( p_sys->pp_pps[i] );
+    }
     block_BytestreamRelease( &p_sys->bytestream );
     free( p_sys );
 }
@@ -496,7 +537,7 @@ static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block )
 /****************************************************************************
  * Helpers
  ****************************************************************************/
-static block_t *CreateAnnexbNAL( decoder_t *p_dec, uint8_t *p, int i_size )
+static block_t *CreateAnnexbNAL( decoder_t *p_dec, const uint8_t *p, int i_size )
 {
     block_t *p_nal;
 
@@ -517,9 +558,9 @@ static block_t *CreateAnnexbNAL( decoder_t *p_dec, uint8_t *p, int i_size )
 }
 
 static void CreateDecodedNAL( uint8_t **pp_ret, int *pi_ret,
-                             uint8_t *src, int i_src )
+                              const uint8_t *src, int i_src )
 {
-    uint8_t *end = &src[i_src];
+    const uint8_t *end = &src[i_src];
     uint8_t *dst = malloc( i_src );
 
     *pp_ret = dst;
@@ -631,6 +672,8 @@ static block_t *ParseNALBlock( decoder_t *p_dec, block_t *p_frag )
     {
         if( p_sys->b_slice )
             p_pic = OutputPicture( p_dec );
+
+        /* TODO parse SEI for CC support */
     }
 
     /* Append the block */
@@ -647,16 +690,29 @@ static block_t *OutputPicture( decoder_t *p_dec )
     if( !p_sys->b_header && p_sys->slice.i_frame_type != BLOCK_FLAG_TYPE_I)
         return NULL;
 
-    if( p_sys->slice.i_frame_type == BLOCK_FLAG_TYPE_I && p_sys->p_sps && p_sys->p_pps )
+    if( p_sys->slice.i_frame_type == BLOCK_FLAG_TYPE_I && p_sys->b_sps && p_sys->b_pps )
     {
-        block_t *p_sps = block_Duplicate( p_sys->p_sps );
-        block_t *p_pps = block_Duplicate( p_sys->p_pps );
-        p_sps->i_dts = p_sys->p_frame->i_dts;
-        p_sps->i_pts = p_sys->p_frame->i_pts;
-        block_ChainAppend( &p_sps, p_pps );
-        block_ChainAppend( &p_sps, p_sys->p_frame );
-        p_sys->b_header = true;
-        p_pic = block_ChainGather( p_sps );
+        block_t *p_list = NULL;
+        int i;
+
+        for( 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++ )
+        {
+            if( p_sys->pp_pps[i] )
+                block_ChainAppend( &p_list, block_Duplicate( p_sys->pp_pps[i] ) );
+        }
+        if( p_list )
+        {
+            p_list->i_dts = p_sys->p_frame->i_dts;
+            p_list->i_pts = p_sys->p_frame->i_pts;
+            p_sys->b_header = true;
+        }
+        block_ChainAppend( &p_list, p_sys->p_frame );
+        p_pic = block_ChainGather( p_list );
     }
     else
     {
@@ -676,24 +732,28 @@ static void PutSPS( decoder_t *p_dec, block_t *p_frag )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
 
-    uint8_t *dec = NULL;
+    uint8_t *pb_dec = NULL;
     int     i_dec = 0;
     bs_t s;
     int i_tmp;
+    int i_sps_id;
 
-    if( !p_sys->b_sps )
-        msg_Dbg( p_dec, "found NAL_SPS" );
-
-    p_sys->b_sps = true;
-
-    CreateDecodedNAL( &dec, &i_dec, &p_frag->p_buffer[5],
+    CreateDecodedNAL( &pb_dec, &i_dec, &p_frag->p_buffer[5],
                      p_frag->i_buffer - 5 );
 
-    bs_init( &s, dec, i_dec );
+    bs_init( &s, pb_dec, i_dec );
     /* Skip profile(8), constraint_set012, reserver(5), level(8) */
     bs_skip( &s, 8 + 1+1+1 + 5 + 8 );
     /* sps id */
-    bs_read_ue( &s );
+    i_sps_id = bs_read_ue( &s );
+    if( i_sps_id >= SPS_MAX )
+    {
+        msg_Warn( p_dec, "invalid SPS (sps_id=%d)", i_sps_id );
+        free( pb_dec );
+        block_Release( p_frag );
+        return;
+    }
+
     /* Skip i_log2_max_frame_num */
     p_sys->i_log2_max_frame_num = bs_read_ue( &s );
     if( p_sys->i_log2_max_frame_num > 12)
@@ -765,26 +825,32 @@ static void PutSPS( decoder_t *p_dec, block_t *p_frag )
         i_tmp = bs_read( &s, 1 );
         if( i_tmp )
         {
-            static const struct { int w, h; } sar[14] =
+            static const struct { int w, h; } sar[17] =
             {
                 { 0,   0 }, { 1,   1 }, { 12, 11 }, { 10, 11 },
                 { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
                 { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
-                { 64, 33 }, { 160,99 },
+                { 64, 33 }, { 160,99 }, {  4,  3 }, {  3,  2 },
+                {  2,  1 },
             };
             int i_sar = bs_read( &s, 8 );
             int w, h;
 
-            if( i_sar < 14 )
+            if( i_sar < 17 )
             {
                 w = sar[i_sar].w;
                 h = sar[i_sar].h;
             }
-            else
+            else if( i_sar == 255 )
             {
                 w = bs_read( &s, 16 );
                 h = bs_read( &s, 16 );
             }
+            else
+            {
+                w = 0;
+                h = 0;
+            }
             if( h != 0 )
                 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * w /
                     h * p_dec->fmt_out.video.i_width /
@@ -794,35 +860,46 @@ static void PutSPS( decoder_t *p_dec, block_t *p_frag )
         }
     }
 
-    free( dec );
+    free( pb_dec );
 
     /* We have a new SPS */
-    if( p_sys->p_sps )
-        block_Release( p_sys->p_sps );
-    p_sys->p_sps = p_frag;
+    if( !p_sys->b_sps )
+        msg_Dbg( p_dec, "found NAL_SPS (sps_id=%d)", i_sps_id );
+    p_sys->b_sps = true;
+
+    if( p_sys->pp_sps[i_sps_id] )
+        block_Release( p_sys->pp_sps[i_sps_id] );
+    p_sys->pp_sps[i_sps_id] = p_frag;
 }
 
 static void PutPPS( decoder_t *p_dec, block_t *p_frag )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
     bs_t s;
+    int i_pps_id;
+    int i_sps_id;
 
     bs_init( &s, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
-    bs_read_ue( &s ); // pps id
-    bs_read_ue( &s ); // sps id
+    i_pps_id = bs_read_ue( &s ); // pps id
+    i_sps_id = bs_read_ue( &s ); // sps id
+    if( i_pps_id >= PPS_MAX || i_sps_id >= SPS_MAX )
+    {
+        msg_Warn( p_dec, "invalid PPS (pps_id=%d sps_id=%d)", i_pps_id, i_sps_id );
+        block_Release( p_frag );
+        return;
+    }
     bs_skip( &s, 1 ); // entropy coding mode flag
     p_sys->i_pic_order_present_flag = bs_read( &s, 1 );
+    /* TODO */
 
+    /* We have a new PPS */
     if( !p_sys->b_pps )
-        msg_Dbg( p_dec, "found NAL_PPS" );
+        msg_Dbg( p_dec, "found NAL_PPS (pps_id=%d sps_id=%d)", i_pps_id, i_sps_id );
     p_sys->b_pps = true;
 
-    /* TODO */
-
-    /* We have a new PPS */
-    if( p_sys->p_pps )
-        block_Release( p_sys->p_pps );
-    p_sys->p_pps = p_frag;
+    if( p_sys->pp_pps[i_pps_id] )
+        block_Release( p_sys->pp_pps[i_pps_id] );
+    p_sys->pp_pps[i_pps_id] = p_frag;
 }
 
 static void ParseSlice( decoder_t *p_dec, bool *pb_new_picture, slice_t *p_slice,