]> git.sesse.net Git - vlc/blobdiff - modules/packetizer/h264.c
Some more format strings
[vlc] / modules / packetizer / h264.c
index f56f189f6e672c25828ef5995464126fe248e499..158bdea2f6619af8d5eeb76a9a156ced3960bca0 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * h264.c: h264/avc video packetizer
  *****************************************************************************
- * Copyright (C) 2001, 2002 the VideoLAN team
+ * Copyright (C) 2001, 2002, 2006 the VideoLAN team
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
@@ -20,7 +20,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -44,7 +44,7 @@ static void Close( vlc_object_t * );
 vlc_module_begin();
     set_category( CAT_SOUT );
     set_subcategory( SUBCAT_SOUT_PACKETIZER );
-    set_description( _("H264 video packetizer") );
+    set_description( _("H.264 video packetizer") );
     set_capability( "packetizer", 50 );
     set_callbacks( Open, Close );
 vlc_module_end();
@@ -72,6 +72,8 @@ struct decoder_sys_t
 
     /* 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;
@@ -82,6 +84,7 @@ struct decoder_sys_t
     int i_nal_ref_idc;
     int i_idr_pic_id;
     int i_frame_num;
+    int i_frame_type;
 };
 
 enum
@@ -119,6 +122,8 @@ static block_t *nal_get_annexeb( decoder_t *, uint8_t *p, int );
 
 /*****************************************************************************
  * Open: probe the packetizer and return score
+ * When opening after demux, the packetizer is only loaded AFTER the decoder
+ * That means that what you set in fmt_out is ignored by the decoder in this special case
  *****************************************************************************/
 static int Open( vlc_object_t *p_this )
 {
@@ -129,6 +134,7 @@ static int Open( vlc_object_t *p_this )
         p_dec->fmt_in.i_codec != VLC_FOURCC( 'H', '2', '6', '4') &&
         p_dec->fmt_in.i_codec != VLC_FOURCC( 'V', 'S', 'S', 'H') &&
         p_dec->fmt_in.i_codec != VLC_FOURCC( 'v', 's', 's', 'h') &&
+        p_dec->fmt_in.i_codec != VLC_FOURCC( 'D', 'A', 'V', 'C') &&
         ( p_dec->fmt_in.i_codec != VLC_FOURCC( 'a', 'v', 'c', '1') ||
           p_dec->fmt_in.i_extra < 7 ) )
     {
@@ -152,21 +158,25 @@ static int Open( vlc_object_t *p_this )
     p_sys->p_frame = NULL;
     p_sys->b_sps   = VLC_FALSE;
     p_sys->b_pps   = VLC_FALSE;
+    p_sys->p_sps   = 0;
+    p_sys->p_pps   = 0;
 
     p_sys->i_nal_type = -1;
     p_sys->i_nal_ref_idc = -1;
     p_sys->i_idr_pic_id = -1;
     p_sys->i_frame_num = -1;
+    p_sys->i_frame_type = 0;
 
     /* Setup properties */
     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
     p_dec->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
-    /* FIXME: FFMPEG isn't happy at all if you leave this */
-    if( p_dec->fmt_out.i_extra ) free( p_dec->fmt_out.p_extra );
-    p_dec->fmt_out.i_extra = 0; p_dec->fmt_out.p_extra = 0;
 
     if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'a', 'v', 'c', '1' ) )
     {
+        /* This type of stream is produced by mp4 and matroska
+         * when we want to store it in another streamformat, you need to convert
+         * The fmt_in.p_extra should ALWAYS contain the avcC
+         * The fmt_out.p_extra should contain all the SPS and PPS with 4 byte startcodes */
         uint8_t *p = &((uint8_t*)p_dec->fmt_in.p_extra)[4];
         int i_sps, i_pps;
         int i;
@@ -176,12 +186,12 @@ static int Open( vlc_object_t *p_this )
 
         /* Read SPS */
         i_sps = (*p++)&0x1f;
-
         for( i = 0; i < i_sps; i++ )
         {
             int i_length = GetWBE( p );
             block_t *p_sps = nal_get_annexeb( p_dec, p + 2, i_length );
 
+            p_sys->p_sps = block_Duplicate( p_sps );
             p_sps->i_pts = p_sps->i_dts = mdate();
             ParseNALBlock( p_dec, p_sps );
             p += 2 + i_length;
@@ -193,18 +203,33 @@ static int Open( vlc_object_t *p_this )
             int i_length = GetWBE( p );
             block_t *p_pps = nal_get_annexeb( p_dec, p + 2, i_length );
 
+            p_sys->p_pps = block_Duplicate( p_pps );
             p_pps->i_pts = p_pps->i_dts = mdate();
             ParseNALBlock( p_dec, p_pps );
             p += 2 + i_length;
         }
-        msg_Dbg( p_dec, "avcC length size=%d sps=%d pps=%d",
+        msg_Dbg( p_dec, "avcC length size=%d, sps=%d, pps=%d",
                  p_sys->i_avcC_length_size, i_sps, i_pps );
 
+        /* FIXME: FFMPEG isn't happy at all if you leave this */
+        if( p_dec->fmt_out.i_extra ) 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;
+        p_dec->fmt_out.p_extra = (uint8_t*)malloc( p_dec->fmt_out.i_extra );
+        memcpy( p_dec->fmt_out.p_extra, p_sys->p_sps->p_buffer, p_sys->p_sps->i_buffer);
+        memcpy( p_dec->fmt_out.p_extra+p_sys->p_sps->i_buffer, p_sys->p_pps->p_buffer, p_sys->p_pps->i_buffer);
+
         /* Set callback */
         p_dec->pf_packetize = PacketizeAVC1;
     }
     else
     {
+        /* This type of stream contains data with 3 of 4 byte startcodes 
+         * The fmt_in.p_extra MAY contain SPS/PPS with 4 byte startcodes
+         * The fmt_out.p_extra should be the same */
+         
         /* Set callback */
         p_dec->pf_packetize = Packetize;
 
@@ -236,12 +261,17 @@ 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;
 
+    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 );
     block_BytestreamRelease( &p_sys->bytestream );
     free( p_sys );
 }
 
 /****************************************************************************
  * Packetize: the whole thing
+ * Search for the startcodes 3 or more bytes
+ * Feed ParseNALBlock ALWAYS with 4 byte startcode prepended NALs
  ****************************************************************************/
 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
 {
@@ -257,6 +287,7 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
         switch( p_sys->i_state )
         {
             case STATE_NOSYNC:
+                /* Skip until 3 byte startcode 0 0 1 */
                 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
                       &p_sys->i_offset, p_sys->startcode+1, 3 ) == VLC_SUCCESS)
                 {
@@ -265,6 +296,7 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
 
                 if( p_sys->i_offset )
                 {
+                    /* skip the data */
                     block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
                     p_sys->i_offset = 0;
                     block_BytestreamFlush( &p_sys->bytestream );
@@ -279,27 +311,26 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
                 p_sys->i_offset = 1; /* To find next startcode */
 
             case STATE_NEXT_SYNC:
-                /* Find the next startcode */
+                /* Find the next 3 byte startcode 0 0 1*/
                 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
-                      &p_sys->i_offset, p_sys->startcode, 3 ) != VLC_SUCCESS)
+                      &p_sys->i_offset, p_sys->startcode+1, 3 ) != VLC_SUCCESS)
                 {
-                    if( block_FindStartcodeFromOffset( &p_sys->bytestream,
-                          &p_sys->i_offset, p_sys->startcode+1, 3 ) !=
-                        VLC_SUCCESS )
-                    {
-                        /* Need more data */
-                        return NULL;
-                    }
+                    /* Need more data */
+                    return NULL;
                 }
 
                 /* Get the new fragment and set the pts/dts */
-                p_pic = block_New( p_dec, p_sys->i_offset );
+                p_pic = block_New( p_dec, p_sys->i_offset +1 );
                 p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
                 p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
+                /* Force 4 byte startcode 0 0 0 1 */
+                p_pic->p_buffer[0] = 0;
 
-                block_GetBytes( &p_sys->bytestream, p_pic->p_buffer,
-                                p_pic->i_buffer );
+                block_GetBytes( &p_sys->bytestream, &p_pic->p_buffer[1],
+                                p_pic->i_buffer-1 );
 
+                /* Remove trailing 0 bytes */
+                while( p_pic->i_buffer && (!p_pic->p_buffer[p_pic->i_buffer-1] ) ) p_pic->i_buffer--;
                 p_sys->i_offset = 0;
 
                 /* Parse the NAL */
@@ -324,7 +355,9 @@ static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
 }
 
 /****************************************************************************
- * PacketizeAVC1: the whole thing
+ * PacketizeAVC1: Takes VCL blocks of data and creates annexe B type NAL stream
+ * Will always use 4 byte 0 0 0 1 startcodes
+ * Will prepend a SPS and PPS before each keyframe
  ****************************************************************************/
 static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block )
 {
@@ -364,6 +397,7 @@ static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block )
         }
         p += i_size;
     }
+    block_Release( p_block );
 
     return p_ret;
 }
@@ -372,15 +406,16 @@ static block_t *nal_get_annexeb( decoder_t *p_dec, uint8_t *p, int i_size )
 {
     block_t *p_nal;
 
-    p_nal = block_New( p_dec, 3 + i_size );
+    p_nal = block_New( p_dec, 4 + i_size );
 
     /* Add start code */
     p_nal->p_buffer[0] = 0x00;
     p_nal->p_buffer[1] = 0x00;
-    p_nal->p_buffer[2] = 0x01;
+    p_nal->p_buffer[2] = 0x00;
+    p_nal->p_buffer[3] = 0x01;
 
     /* Copy nalu */
-    memcpy( &p_nal->p_buffer[3], p, i_size );
+    memcpy( &p_nal->p_buffer[4], p, i_size );
 
     return p_nal;
 }
@@ -429,21 +464,39 @@ static inline int bs_read_se( bs_t *s )
 }
 
 
+/*****************************************************************************
+ * ParseNALBlock: parses annexB type NALs
+ * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode 
+ *****************************************************************************/
 static block_t *ParseNALBlock( decoder_t *p_dec, block_t *p_frag )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
     block_t *p_pic = NULL;
 
-    const int i_nal_ref_idc = (p_frag->p_buffer[3] >> 5)&0x03;
-    const int i_nal_type = p_frag->p_buffer[3]&0x1f;
+    const int i_nal_ref_idc = (p_frag->p_buffer[4] >> 5)&0x03;
+    const int i_nal_type = p_frag->p_buffer[4]&0x1f;
 
 #define OUTPUT \
-    do {                                                \
-        p_pic = block_ChainGather( p_sys->p_frame );    \
-        p_pic->i_length = 0;    /* FIXME */             \
-                                                        \
-        p_sys->p_frame = NULL;                          \
-        p_sys->b_slice = VLC_FALSE;                     \
+    do {                                                      \
+        p_pic = block_ChainGather( p_sys->p_frame );          \
+        p_pic->i_length = 0;    /* FIXME */                   \
+        p_pic->i_flags |= p_sys->i_frame_type;                \
+                                                              \
+        p_sys->i_frame_type = 0;                              \
+        p_sys->p_frame = NULL;                                \
+        p_sys->b_slice = VLC_FALSE;                           \
+                                                              \
+        if( ( p_pic->i_flags & BLOCK_FLAG_TYPE_I ) &&         \
+              p_sys->p_sps && p_sys->p_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_pps->i_dts = p_pic->i_dts;       \
+            p_sps->i_pts = p_pps->i_pts = p_pic->i_pts;       \
+            block_ChainAppend( &p_sps, p_pps );               \
+            block_ChainAppend( &p_sps, p_pic );               \
+            p_pic = block_ChainGather( p_sps );               \
+        }                                                     \
     } while(0)
 
 
@@ -466,13 +519,13 @@ static block_t *ParseNALBlock( decoder_t *p_dec, block_t *p_frag )
     else if( i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
     {
         uint8_t *dec;
-        int i_dec, i_first_mb, i_slice_type, i_frame_num, i_pic_flags = 0;
+        int i_dec, i_first_mb, i_slice_type, i_frame_num;
         vlc_bool_t b_pic = VLC_FALSE;
         bs_t s;
 
         /* do not convert the whole frame */
-        nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[4],
-                         __MIN( p_frag->i_buffer - 4, 60 ) );
+        nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[5],
+                         __MIN( p_frag->i_buffer - 5, 60 ) );
         bs_init( &s, dec, i_dec );
 
         /* first_mb_in_slice */
@@ -481,21 +534,21 @@ static block_t *ParseNALBlock( decoder_t *p_dec, block_t *p_frag )
         /* slice_type */
         switch( (i_slice_type = bs_read_ue( &s )) )
         {
-            case 0: case 5:
-                i_pic_flags = BLOCK_FLAG_TYPE_P;
-                break;
-            case 1: case 6:
-                i_pic_flags = BLOCK_FLAG_TYPE_B;
-                break;
-            case 2: case 7:
-                i_pic_flags = BLOCK_FLAG_TYPE_I;
-                break;
-            case 3: case 8: /* SP */
-                i_pic_flags = BLOCK_FLAG_TYPE_P;
-                break;
-            case 4: case 9:
-                i_pic_flags = BLOCK_FLAG_TYPE_I;
-                break;
+        case 0: case 5:
+            p_sys->i_frame_type = BLOCK_FLAG_TYPE_P;
+            break;
+        case 1: case 6:
+            p_sys->i_frame_type = BLOCK_FLAG_TYPE_B;
+            break;
+        case 2: case 7:
+            p_sys->i_frame_type = BLOCK_FLAG_TYPE_I;
+            break;
+        case 3: case 8: /* SP */
+            p_sys->i_frame_type = BLOCK_FLAG_TYPE_P;
+            break;
+        case 4: case 9:
+            p_sys->i_frame_type = BLOCK_FLAG_TYPE_I;
+            break;
         }
 
         /* pic_parameter_set_id */
@@ -534,8 +587,7 @@ static block_t *ParseNALBlock( decoder_t *p_dec, block_t *p_frag )
         }
         p_sys->i_nal_type = i_nal_type;
 
-        if( b_pic && p_sys->b_slice )
-            OUTPUT;
+        if( b_pic && p_sys->b_slice ) OUTPUT;
 
         p_sys->b_slice = VLC_TRUE;
 
@@ -548,13 +600,12 @@ static block_t *ParseNALBlock( decoder_t *p_dec, block_t *p_frag )
         bs_t s;
         int i_tmp;
 
-        if( !p_sys->b_sps )
-            msg_Dbg( p_dec, "found NAL_SPS" );
+        if( !p_sys->b_sps ) msg_Dbg( p_dec, "found NAL_SPS" );
 
         p_sys->b_sps = VLC_TRUE;
 
-        nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[4],
-                         p_frag->i_buffer - 4 );
+        nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[5],
+                         p_frag->i_buffer - 5 );
 
         bs_init( &s, dec, i_dec );
         /* Skip profile(8), constraint_set012, reserver(5), level(8) */
@@ -649,8 +700,8 @@ static block_t *ParseNALBlock( decoder_t *p_dec, block_t *p_frag )
                     h = bs_read( &s, 16 );
                 }
                 if( h != 0 )
-                    p_dec->fmt_out.video.i_aspect =
-                        VOUT_ASPECT_FACTOR * w / h * p_dec->fmt_out.video.i_width /
+                    p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * w /
+                        h * p_dec->fmt_out.video.i_width /
                         p_dec->fmt_out.video.i_height;
                 else
                     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR;
@@ -659,30 +710,39 @@ static block_t *ParseNALBlock( decoder_t *p_dec, block_t *p_frag )
 
         free( dec );
 
+        if( p_sys->b_slice ) OUTPUT;
+
+        /* 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_slice )
-            OUTPUT;
+        /* Do not append the SPS because we will insert it on keyframes */
+        return p_pic;
     }
     else if( i_nal_type == NAL_PPS )
     {
         bs_t s;
-        bs_init( &s, &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
+        bs_init( &s, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
 
-        if( !p_sys->b_pps )
-            msg_Dbg( p_dec, "found NAL_PPS" );
+        if( !p_sys->b_pps ) msg_Dbg( p_dec, "found NAL_PPS" );
         p_sys->b_pps = VLC_TRUE;
 
         /* TODO */
 
-        if( p_sys->b_slice )
-            OUTPUT;
+        if( p_sys->b_slice ) OUTPUT;
+
+        /* We have a new PPS */
+        if( p_sys->p_pps ) block_Release( p_sys->p_pps );
+        p_sys->p_pps = p_frag;
+
+        /* Do not append the PPS because we will insert it on keyframes */
+        return p_pic;
     }
     else if( i_nal_type == NAL_AU_DELIMITER ||
              i_nal_type == NAL_SEI ||
              ( i_nal_type >= 13 && i_nal_type <= 18 ) )
     {
-        if( p_sys->b_slice )
-            OUTPUT;
+        if( p_sys->b_slice ) OUTPUT;
     }
 
 #undef OUTPUT