@@ -49,23 +49,23 @@ typedef struct H264BSFContext {
4949
5050static int alloc_and_copy (uint8_t * * poutbuf , int * poutbuf_size ,
5151 const uint8_t * sps_pps , uint32_t sps_pps_size ,
52- const uint8_t * in , uint32_t in_size )
52+ const uint8_t * in , uint32_t in_size , int ps )
5353{
5454 uint32_t offset = * poutbuf_size ;
55- uint8_t nal_header_size = offset ? 3 : 4 ;
55+ uint8_t start_code_size = offset == 0 || ps ? 4 : 3 ;
5656 int err ;
5757
58- * poutbuf_size += sps_pps_size + in_size + nal_header_size ;
58+ * poutbuf_size += sps_pps_size + in_size + start_code_size ;
5959 if ((err = av_reallocp (poutbuf ,
6060 * poutbuf_size + AV_INPUT_BUFFER_PADDING_SIZE )) < 0 ) {
6161 * poutbuf_size = 0 ;
6262 return err ;
6363 }
6464 if (sps_pps )
6565 memcpy (* poutbuf + offset , sps_pps , sps_pps_size );
66- memcpy (* poutbuf + sps_pps_size + nal_header_size + offset , in , in_size );
67- if (! offset ) {
68- AV_WB32 (* poutbuf + sps_pps_size , 1 );
66+ memcpy (* poutbuf + sps_pps_size + start_code_size + offset , in , in_size );
67+ if (start_code_size == 4 ) {
68+ AV_WB32 (* poutbuf + offset + sps_pps_size , 1 );
6969 } else {
7070 (* poutbuf + offset + sps_pps_size )[0 ] =
7171 (* poutbuf + offset + sps_pps_size )[1 ] = 0 ;
@@ -216,7 +216,7 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc,
216216 if ((ret = alloc_and_copy (poutbuf , poutbuf_size ,
217217 ctx -> spspps_buf + ctx -> sps_offset ,
218218 ctx -> pps_offset != -1 ? ctx -> pps_offset : ctx -> spspps_size - ctx -> sps_offset ,
219- buf , nal_size )) < 0 )
219+ buf , nal_size , 1 )) < 0 )
220220 goto fail ;
221221 ctx -> idr_sps_seen = 1 ;
222222 goto next_nal ;
@@ -234,23 +234,23 @@ static int h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc,
234234 if (ctx -> new_idr && unit_type == 5 && !ctx -> idr_sps_seen && !ctx -> idr_pps_seen ) {
235235 if ((ret = alloc_and_copy (poutbuf , poutbuf_size ,
236236 ctx -> spspps_buf , ctx -> spspps_size ,
237- buf , nal_size )) < 0 )
237+ buf , nal_size , 1 )) < 0 )
238238 goto fail ;
239239 ctx -> new_idr = 0 ;
240240 /* if only SPS has been seen, also insert PPS */
241241 } else if (ctx -> new_idr && unit_type == 5 && ctx -> idr_sps_seen && !ctx -> idr_pps_seen ) {
242242 if (ctx -> pps_offset == -1 ) {
243243 av_log (avctx , AV_LOG_WARNING , "PPS not present in the stream, nor in AVCC, stream may be unreadable\n" );
244244 if ((ret = alloc_and_copy (poutbuf , poutbuf_size ,
245- NULL , 0 , buf , nal_size )) < 0 )
245+ NULL , 0 , buf , nal_size , 0 )) < 0 )
246246 goto fail ;
247247 } else if ((ret = alloc_and_copy (poutbuf , poutbuf_size ,
248248 ctx -> spspps_buf + ctx -> pps_offset , ctx -> spspps_size - ctx -> pps_offset ,
249- buf , nal_size )) < 0 )
249+ buf , nal_size , 1 )) < 0 )
250250 goto fail ;
251251 } else {
252252 if ((ret = alloc_and_copy (poutbuf , poutbuf_size ,
253- NULL , 0 , buf , nal_size )) < 0 )
253+ NULL , 0 , buf , nal_size , unit_type == 7 || unit_type == 8 )) < 0 )
254254 goto fail ;
255255 if (!ctx -> new_idr && unit_type == 1 ) {
256256 ctx -> new_idr = 1 ;
0 commit comments