1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2012-2014 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
21 /* This file is available under an ISC license. */
23 #include "common/internal.h" /* must be placed first */
31 /***************************************************************************
32 ITU-T Recommendation H.264 (04/13)
34 ***************************************************************************/
38 #define IF_EXCEED_INT32( x ) if( (x) < INT32_MIN || (x) > INT32_MAX )
39 #define H264_REQUIRES_AVCC_EXTENSION( x ) ((x) == 100 || (x) == 110 || (x) == 122 || (x) == 144)
40 #define H264_POC_DEBUG_PRINT 0
44 H264_SLICE_TYPE_P
= 0,
45 H264_SLICE_TYPE_B
= 1,
46 H264_SLICE_TYPE_I
= 2,
47 H264_SLICE_TYPE_SP
= 3,
48 H264_SLICE_TYPE_SI
= 4
51 void lsmash_destroy_h264_parameter_sets
53 lsmash_h264_specific_parameters_t
*param
56 if( !param
|| !param
->parameter_sets
)
58 lsmash_remove_entries( param
->parameter_sets
->sps_list
, isom_remove_dcr_ps
);
59 lsmash_remove_entries( param
->parameter_sets
->pps_list
, isom_remove_dcr_ps
);
60 lsmash_remove_entries( param
->parameter_sets
->spsext_list
, isom_remove_dcr_ps
);
61 lsmash_free( param
->parameter_sets
);
62 param
->parameter_sets
= NULL
;
65 void h264_destruct_specific_data
72 lsmash_destroy_h264_parameter_sets( data
);
76 void h264_cleanup_parser
83 lsmash_remove_entries( info
->sps_list
, NULL
);
84 lsmash_remove_entries( info
->pps_list
, NULL
);
85 lsmash_remove_entries( info
->slice_list
, NULL
);
86 lsmash_destroy_h264_parameter_sets( &info
->avcC_param
);
87 lsmash_destroy_h264_parameter_sets( &info
->avcC_param_next
);
88 lsmash_destroy_multiple_buffers( info
->buffer
.bank
);
89 lsmash_bits_adhoc_cleanup( info
->bits
);
100 memset( info
, 0, sizeof(h264_info_t
) );
101 info
->avcC_param
.lengthSizeMinusOne
= NALU_DEFAULT_NALU_LENGTH_SIZE
- 1;
102 info
->avcC_param_next
.lengthSizeMinusOne
= NALU_DEFAULT_NALU_LENGTH_SIZE
- 1;
103 h264_stream_buffer_t
*sb
= &info
->buffer
;
104 sb
->bank
= lsmash_create_multiple_buffers( parse_only
? 1 : 3, NALU_DEFAULT_BUFFER_SIZE
);
106 return LSMASH_ERR_MEMORY_ALLOC
;
107 sb
->rbsp
= lsmash_withdraw_buffer( sb
->bank
, 1 );
110 info
->au
.data
= lsmash_withdraw_buffer( sb
->bank
, 2 );
111 info
->au
.incomplete_data
= lsmash_withdraw_buffer( sb
->bank
, 3 );
113 info
->bits
= lsmash_bits_adhoc_create();
116 lsmash_destroy_multiple_buffers( sb
->bank
);
117 return LSMASH_ERR_MEMORY_ALLOC
;
119 lsmash_init_entry_list( info
->sps_list
);
120 lsmash_init_entry_list( info
->pps_list
);
121 lsmash_init_entry_list( info
->slice_list
);
125 static int h264_check_nalu_header
128 h264_nalu_header_t
*nuh
,
129 int use_long_start_code
132 uint8_t temp8
= lsmash_bs_show_byte( bs
, use_long_start_code
? NALU_LONG_START_CODE_LENGTH
: NALU_SHORT_START_CODE_LENGTH
);
133 nuh
->forbidden_zero_bit
= (temp8
>> 7) & 0x01;
134 nuh
->nal_ref_idc
= (temp8
>> 5) & 0x03;
135 nuh
->nal_unit_type
= temp8
& 0x1f;
137 if( nuh
->nal_unit_type
== H264_NALU_TYPE_PREFIX
138 || nuh
->nal_unit_type
== H264_NALU_TYPE_SLICE_EXT
139 || nuh
->nal_unit_type
== H264_NALU_TYPE_SLICE_EXT_DVC
)
141 /* We don't support these types of NALU. */
143 return LSMASH_ERR_PATCH_WELCOME
;
145 if( nuh
->forbidden_zero_bit
)
146 return LSMASH_ERR_INVALID_DATA
;
147 /* SPS and PPS require long start code (0x00000001).
148 * Also AU delimiter requires it too because this type of NALU shall be the first NALU of any AU if present. */
149 if( !use_long_start_code
150 && (nuh
->nal_unit_type
== H264_NALU_TYPE_SPS
151 || nuh
->nal_unit_type
== H264_NALU_TYPE_PPS
152 || nuh
->nal_unit_type
== H264_NALU_TYPE_AUD
) )
153 return LSMASH_ERR_INVALID_DATA
;
154 if( nuh
->nal_ref_idc
)
156 /* nal_ref_idc shall be equal to 0 for all NALUs having nal_unit_type equal to 6, 9, 10, 11, or 12. */
157 if( nuh
->nal_unit_type
== H264_NALU_TYPE_SEI
158 || nuh
->nal_unit_type
== H264_NALU_TYPE_AUD
159 || nuh
->nal_unit_type
== H264_NALU_TYPE_EOS
160 || nuh
->nal_unit_type
== H264_NALU_TYPE_EOB
161 || nuh
->nal_unit_type
== H264_NALU_TYPE_FD
)
162 return LSMASH_ERR_INVALID_DATA
;
165 /* nal_ref_idc shall not be equal to 0 for NALUs with nal_unit_type equal to 5. */
166 if( nuh
->nal_unit_type
== H264_NALU_TYPE_SLICE_IDR
)
167 return LSMASH_ERR_INVALID_DATA
;
171 uint64_t h264_find_next_start_code
174 h264_nalu_header_t
*nuh
,
175 uint64_t *start_code_length
,
176 uint64_t *trailing_zero_bytes
179 uint64_t length
= 0; /* the length of the latest NALU */
180 uint64_t count
= 0; /* the number of the trailing zero bytes after the latest NALU */
181 /* Check the type of the current start code. */
183 = (!lsmash_bs_is_end( bs
, NALU_LONG_START_CODE_LENGTH
) && 0x00000001 == lsmash_bs_show_be32( bs
, 0 )) ? 1
184 : (!lsmash_bs_is_end( bs
, NALU_SHORT_START_CODE_LENGTH
) && 0x000001 == lsmash_bs_show_be24( bs
, 0 )) ? 0
186 if( long_start_code
>= 0 && h264_check_nalu_header( bs
, nuh
, long_start_code
) == 0 )
188 *start_code_length
= long_start_code
? NALU_LONG_START_CODE_LENGTH
: NALU_SHORT_START_CODE_LENGTH
;
189 uint64_t distance
= *start_code_length
+ nuh
->length
;
190 /* Find the start code of the next NALU and get the distance from the start code of the latest NALU. */
191 if( !lsmash_bs_is_end( bs
, distance
+ NALU_SHORT_START_CODE_LENGTH
) )
193 uint32_t sync_bytes
= lsmash_bs_show_be24( bs
, distance
);
194 while( 0x000001 != sync_bytes
)
196 if( lsmash_bs_is_end( bs
, ++distance
+ NALU_SHORT_START_CODE_LENGTH
) )
198 distance
= lsmash_bs_get_remaining_buffer_size( bs
);
202 sync_bytes
|= lsmash_bs_show_byte( bs
, distance
+ NALU_SHORT_START_CODE_LENGTH
- 1 );
203 sync_bytes
&= 0xFFFFFF;
207 distance
= lsmash_bs_get_remaining_buffer_size( bs
);
208 /* Any NALU has no consecutive zero bytes at the end. */
209 while( 0x00 == lsmash_bs_show_byte( bs
, distance
- 1 ) )
214 /* Remove the length of the start code. */
215 length
= distance
- *start_code_length
;
216 /* If there are one or more trailing zero bytes, we treat the last one byte as a part of the next start code.
217 * This makes the next start code a long start code. */
224 nuh
->forbidden_zero_bit
= 1; /* shall be 0, so invalid */
225 nuh
->nal_ref_idc
= 0; /* arbitrary */
226 nuh
->nal_unit_type
= H264_NALU_TYPE_UNSPECIFIED0
;
228 *start_code_length
= 0;
230 *trailing_zero_bytes
= count
;
234 static h264_sps_t
*h264_get_sps
236 lsmash_entry_list_t
*sps_list
,
240 if( !sps_list
|| sps_id
> 31 )
242 for( lsmash_entry_t
*entry
= sps_list
->head
; entry
; entry
= entry
->next
)
244 h264_sps_t
*sps
= (h264_sps_t
*)entry
->data
;
247 if( sps
->seq_parameter_set_id
== sps_id
)
250 h264_sps_t
*sps
= lsmash_malloc_zero( sizeof(h264_sps_t
) );
253 sps
->seq_parameter_set_id
= sps_id
;
254 if( lsmash_add_entry( sps_list
, sps
) < 0 )
262 static h264_pps_t
*h264_get_pps
264 lsmash_entry_list_t
*pps_list
,
270 for( lsmash_entry_t
*entry
= pps_list
->head
; entry
; entry
= entry
->next
)
272 h264_pps_t
*pps
= (h264_pps_t
*)entry
->data
;
275 if( pps
->pic_parameter_set_id
== pps_id
)
278 h264_pps_t
*pps
= lsmash_malloc_zero( sizeof(h264_pps_t
) );
281 pps
->pic_parameter_set_id
= pps_id
;
282 if( lsmash_add_entry( pps_list
, pps
) < 0 )
290 static h264_slice_info_t
*h264_get_slice_info
292 lsmash_entry_list_t
*slice_list
,
298 for( lsmash_entry_t
*entry
= slice_list
->head
; entry
; entry
= entry
->next
)
300 h264_slice_info_t
*slice
= (h264_slice_info_t
*)entry
->data
;
303 if( slice
->slice_id
== slice_id
)
306 h264_slice_info_t
*slice
= lsmash_malloc_zero( sizeof(h264_slice_info_t
) );
309 slice
->slice_id
= slice_id
;
310 if( lsmash_add_entry( slice_list
, slice
) < 0 )
312 lsmash_free( slice
);
318 int h264_calculate_poc
321 h264_picture_info_t
*picture
,
322 h264_picture_info_t
*prev_picture
325 #if H264_POC_DEBUG_PRINT
326 fprintf( stderr
, "PictureOrderCount\n" );
328 h264_pps_t
*pps
= h264_get_pps( info
->pps_list
, picture
->pic_parameter_set_id
);
330 return LSMASH_ERR_NAMELESS
;
331 h264_sps_t
*sps
= h264_get_sps( info
->sps_list
, pps
->seq_parameter_set_id
);
333 return LSMASH_ERR_NAMELESS
;
334 int64_t TopFieldOrderCnt
= 0;
335 int64_t BottomFieldOrderCnt
= 0;
336 if( sps
->pic_order_cnt_type
== 0 )
338 int32_t prevPicOrderCntMsb
;
339 int32_t prevPicOrderCntLsb
;
342 prevPicOrderCntMsb
= 0;
343 prevPicOrderCntLsb
= 0;
345 else if( prev_picture
->ref_pic_has_mmco5
)
347 prevPicOrderCntMsb
= 0;
348 prevPicOrderCntLsb
= prev_picture
->ref_pic_bottom_field_flag
? 0 : prev_picture
->ref_pic_TopFieldOrderCnt
;
352 prevPicOrderCntMsb
= prev_picture
->ref_pic_PicOrderCntMsb
;
353 prevPicOrderCntLsb
= prev_picture
->ref_pic_PicOrderCntLsb
;
355 int64_t PicOrderCntMsb
;
356 int32_t pic_order_cnt_lsb
= picture
->pic_order_cnt_lsb
;
357 uint64_t MaxPicOrderCntLsb
= sps
->MaxPicOrderCntLsb
;
358 if( (pic_order_cnt_lsb
< prevPicOrderCntLsb
)
359 && ((prevPicOrderCntLsb
- pic_order_cnt_lsb
) >= (MaxPicOrderCntLsb
/ 2)) )
360 PicOrderCntMsb
= prevPicOrderCntMsb
+ MaxPicOrderCntLsb
;
361 else if( (pic_order_cnt_lsb
> prevPicOrderCntLsb
)
362 && ((pic_order_cnt_lsb
- prevPicOrderCntLsb
) > (MaxPicOrderCntLsb
/ 2)) )
363 PicOrderCntMsb
= prevPicOrderCntMsb
- MaxPicOrderCntLsb
;
365 PicOrderCntMsb
= prevPicOrderCntMsb
;
366 IF_EXCEED_INT32( PicOrderCntMsb
)
367 return LSMASH_ERR_INVALID_DATA
;
368 BottomFieldOrderCnt
= TopFieldOrderCnt
= PicOrderCntMsb
+ pic_order_cnt_lsb
;
369 if( !picture
->field_pic_flag
)
370 BottomFieldOrderCnt
+= picture
->delta_pic_order_cnt_bottom
;
371 IF_EXCEED_INT32( TopFieldOrderCnt
)
372 return LSMASH_ERR_INVALID_DATA
;
373 IF_EXCEED_INT32( BottomFieldOrderCnt
)
374 return LSMASH_ERR_INVALID_DATA
;
375 if( !picture
->disposable
)
377 picture
->ref_pic_has_mmco5
= picture
->has_mmco5
;
378 picture
->ref_pic_bottom_field_flag
= picture
->bottom_field_flag
;
379 picture
->ref_pic_TopFieldOrderCnt
= TopFieldOrderCnt
;
380 picture
->ref_pic_PicOrderCntMsb
= PicOrderCntMsb
;
381 picture
->ref_pic_PicOrderCntLsb
= pic_order_cnt_lsb
;
383 #if H264_POC_DEBUG_PRINT
384 fprintf( stderr
, " prevPicOrderCntMsb: %"PRId32
"\n", prevPicOrderCntMsb
);
385 fprintf( stderr
, " prevPicOrderCntLsb: %"PRId32
"\n", prevPicOrderCntLsb
);
386 fprintf( stderr
, " PicOrderCntMsb: %"PRId64
"\n", PicOrderCntMsb
);
387 fprintf( stderr
, " pic_order_cnt_lsb: %"PRId32
"\n", pic_order_cnt_lsb
);
388 fprintf( stderr
, " MaxPicOrderCntLsb: %"PRIu64
"\n", MaxPicOrderCntLsb
);
391 else if( sps
->pic_order_cnt_type
== 1 )
393 uint32_t frame_num
= picture
->frame_num
;
394 uint32_t prevFrameNum
= prev_picture
->has_mmco5
? 0 : prev_picture
->frame_num
;
395 uint32_t prevFrameNumOffset
= prev_picture
->has_mmco5
? 0 : prev_picture
->FrameNumOffset
;
396 uint64_t FrameNumOffset
= picture
->idr
? 0 : prevFrameNumOffset
+ (prevFrameNum
> frame_num
? sps
->MaxFrameNum
: 0);
397 if( FrameNumOffset
> INT32_MAX
)
398 return LSMASH_ERR_INVALID_DATA
;
399 int64_t expectedPicOrderCnt
;
400 if( sps
->num_ref_frames_in_pic_order_cnt_cycle
)
402 uint64_t absFrameNum
= FrameNumOffset
+ frame_num
;
403 absFrameNum
-= picture
->disposable
&& absFrameNum
> 0;
406 uint64_t picOrderCntCycleCnt
= (absFrameNum
- 1) / sps
->num_ref_frames_in_pic_order_cnt_cycle
;
407 uint8_t frameNumInPicOrderCntCycle
= (absFrameNum
- 1) % sps
->num_ref_frames_in_pic_order_cnt_cycle
;
408 expectedPicOrderCnt
= picOrderCntCycleCnt
* sps
->ExpectedDeltaPerPicOrderCntCycle
;
409 for( uint8_t i
= 0; i
<= frameNumInPicOrderCntCycle
; i
++ )
410 expectedPicOrderCnt
+= sps
->offset_for_ref_frame
[i
];
413 expectedPicOrderCnt
= 0;
416 expectedPicOrderCnt
= 0;
417 if( picture
->disposable
)
418 expectedPicOrderCnt
+= sps
->offset_for_non_ref_pic
;
419 TopFieldOrderCnt
= expectedPicOrderCnt
+ picture
->delta_pic_order_cnt
[0];
420 BottomFieldOrderCnt
= TopFieldOrderCnt
+ sps
->offset_for_top_to_bottom_field
;
421 if( !picture
->field_pic_flag
)
422 BottomFieldOrderCnt
+= picture
->delta_pic_order_cnt
[1];
423 IF_EXCEED_INT32( TopFieldOrderCnt
)
424 return LSMASH_ERR_INVALID_DATA
;
425 IF_EXCEED_INT32( BottomFieldOrderCnt
)
426 return LSMASH_ERR_INVALID_DATA
;
427 picture
->FrameNumOffset
= FrameNumOffset
;
429 else if( sps
->pic_order_cnt_type
== 2 )
431 uint32_t frame_num
= picture
->frame_num
;
432 uint32_t prevFrameNum
= prev_picture
->has_mmco5
? 0 : prev_picture
->frame_num
;
433 int32_t prevFrameNumOffset
= prev_picture
->has_mmco5
? 0 : prev_picture
->FrameNumOffset
;
434 int64_t FrameNumOffset
;
435 int64_t tempPicOrderCnt
;
443 FrameNumOffset
= prevFrameNumOffset
+ (prevFrameNum
> frame_num
? sps
->MaxFrameNum
: 0);
444 tempPicOrderCnt
= 2 * (FrameNumOffset
+ frame_num
) - picture
->disposable
;
445 IF_EXCEED_INT32( FrameNumOffset
)
446 return LSMASH_ERR_INVALID_DATA
;
447 IF_EXCEED_INT32( tempPicOrderCnt
)
448 return LSMASH_ERR_INVALID_DATA
;
450 TopFieldOrderCnt
= tempPicOrderCnt
;
451 BottomFieldOrderCnt
= tempPicOrderCnt
;
452 picture
->FrameNumOffset
= FrameNumOffset
;
454 if( !picture
->field_pic_flag
)
455 picture
->PicOrderCnt
= LSMASH_MIN( TopFieldOrderCnt
, BottomFieldOrderCnt
);
457 picture
->PicOrderCnt
= picture
->bottom_field_flag
? BottomFieldOrderCnt
: TopFieldOrderCnt
;
458 #if H264_POC_DEBUG_PRINT
459 if( picture
->field_pic_flag
)
461 if( !picture
->bottom_field_flag
)
462 fprintf( stderr
, " TopFieldOrderCnt: %"PRId64
"\n", TopFieldOrderCnt
);
464 fprintf( stderr
, " BottomFieldOrderCnt: %"PRId64
"\n", BottomFieldOrderCnt
);
466 fprintf( stderr
, " POC: %"PRId32
"\n", picture
->PicOrderCnt
);
471 static int h264_parse_scaling_list
474 int sizeOfScalingList
477 /* scaling_list( scalingList, sizeOfScalingList, useDefaultScalingMatrixFlag ) */
479 for( int i
= 0; i
< sizeOfScalingList
; i
++ )
481 int64_t delta_scale
= nalu_get_exp_golomb_se( bits
);
482 if( delta_scale
< -128 || delta_scale
> 127 )
483 return LSMASH_ERR_INVALID_DATA
;
484 nextScale
= (nextScale
+ delta_scale
+ 256) % 256;
491 static int h264_parse_hrd_parameters
497 /* hrd_parameters() */
498 uint64_t cpb_cnt_minus1
= nalu_get_exp_golomb_ue( bits
);
499 if( cpb_cnt_minus1
> 31 )
500 return LSMASH_ERR_INVALID_DATA
;
501 lsmash_bits_get( bits
, 4 ); /* bit_rate_scale */
502 lsmash_bits_get( bits
, 4 ); /* cpb_size_scale */
503 for( uint64_t SchedSelIdx
= 0; SchedSelIdx
<= cpb_cnt_minus1
; SchedSelIdx
++ )
505 nalu_get_exp_golomb_ue( bits
); /* bit_rate_value_minus1[ SchedSelIdx ] */
506 nalu_get_exp_golomb_ue( bits
); /* cpb_size_value_minus1[ SchedSelIdx ] */
507 lsmash_bits_get( bits
, 1 ); /* cbr_flag [ SchedSelIdx ] */
509 lsmash_bits_get( bits
, 5 ); /* initial_cpb_removal_delay_length_minus1 */
510 hrd
->cpb_removal_delay_length
= lsmash_bits_get( bits
, 5 ) + 1;
511 hrd
->dpb_output_delay_length
= lsmash_bits_get( bits
, 5 ) + 1;
512 lsmash_bits_get( bits
, 5 ); /* time_offset_length */
516 static int h264_parse_sps_minimally
520 uint8_t *rbsp_buffer
,
525 int err
= nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, ebsp
, ebsp_size
);
528 memset( sps
, 0, sizeof(h264_sps_t
) );
529 sps
->profile_idc
= lsmash_bits_get( bits
, 8 );
530 sps
->constraint_set_flags
= lsmash_bits_get( bits
, 8 );
531 sps
->level_idc
= lsmash_bits_get( bits
, 8 );
532 uint64_t seq_parameter_set_id
= nalu_get_exp_golomb_ue( bits
);
533 if( seq_parameter_set_id
> 31 )
534 return LSMASH_ERR_INVALID_DATA
;
535 sps
->seq_parameter_set_id
= seq_parameter_set_id
;
536 if( sps
->profile_idc
== 100 || sps
->profile_idc
== 110 || sps
->profile_idc
== 122
537 || sps
->profile_idc
== 244 || sps
->profile_idc
== 44 || sps
->profile_idc
== 83
538 || sps
->profile_idc
== 86 || sps
->profile_idc
== 118 || sps
->profile_idc
== 128
539 || sps
->profile_idc
== 138 )
541 sps
->chroma_format_idc
= nalu_get_exp_golomb_ue( bits
);
542 if( sps
->chroma_format_idc
== 3 )
543 sps
->separate_colour_plane_flag
= lsmash_bits_get( bits
, 1 );
544 uint64_t bit_depth_luma_minus8
= nalu_get_exp_golomb_ue( bits
);
545 if( bit_depth_luma_minus8
> 6 )
546 return LSMASH_ERR_INVALID_DATA
;
547 uint64_t bit_depth_chroma_minus8
= nalu_get_exp_golomb_ue( bits
);
548 if( bit_depth_chroma_minus8
> 6 )
549 return LSMASH_ERR_INVALID_DATA
;
550 sps
->bit_depth_luma_minus8
= bit_depth_luma_minus8
;
551 sps
->bit_depth_chroma_minus8
= bit_depth_chroma_minus8
;
552 lsmash_bits_get( bits
, 1 ); /* qpprime_y_zero_transform_bypass_flag */
553 if( lsmash_bits_get( bits
, 1 ) ) /* seq_scaling_matrix_present_flag */
555 int num_loops
= sps
->chroma_format_idc
!= 3 ? 8 : 12;
556 for( int i
= 0; i
< num_loops
; i
++ )
557 if( lsmash_bits_get( bits
, 1 ) /* seq_scaling_list_present_flag[i] */
558 && (err
= h264_parse_scaling_list( bits
, i
< 6 ? 16 : 64 )) < 0 )
564 sps
->chroma_format_idc
= 1;
565 sps
->separate_colour_plane_flag
= 0;
566 sps
->bit_depth_luma_minus8
= 0;
567 sps
->bit_depth_chroma_minus8
= 0;
569 return bits
->bs
->error
? LSMASH_ERR_NAMELESS
: 0;
575 uint8_t *rbsp_buffer
,
580 lsmash_bits_t
*bits
= info
->bits
;
581 /* seq_parameter_set_data() */
583 int err
= h264_parse_sps_minimally( bits
, &temp_sps
, rbsp_buffer
, ebsp
, ebsp_size
);
586 h264_sps_t
*sps
= h264_get_sps( info
->sps_list
, temp_sps
.seq_parameter_set_id
);
588 return LSMASH_ERR_NAMELESS
;
589 memset( sps
, 0, sizeof(h264_sps_t
) );
590 sps
->profile_idc
= temp_sps
.profile_idc
;
591 sps
->constraint_set_flags
= temp_sps
.constraint_set_flags
;
592 sps
->level_idc
= temp_sps
.level_idc
;
593 sps
->seq_parameter_set_id
= temp_sps
.seq_parameter_set_id
;
594 sps
->chroma_format_idc
= temp_sps
.chroma_format_idc
;
595 sps
->separate_colour_plane_flag
= temp_sps
.separate_colour_plane_flag
;
596 sps
->bit_depth_luma_minus8
= temp_sps
.bit_depth_luma_minus8
;
597 sps
->bit_depth_chroma_minus8
= temp_sps
.bit_depth_chroma_minus8
;
598 sps
->ChromaArrayType
= sps
->separate_colour_plane_flag
? 0 : sps
->chroma_format_idc
;
599 uint64_t log2_max_frame_num_minus4
= nalu_get_exp_golomb_ue( bits
);
600 if( log2_max_frame_num_minus4
> 12 )
601 return LSMASH_ERR_INVALID_DATA
;
602 sps
->log2_max_frame_num
= log2_max_frame_num_minus4
+ 4;
603 sps
->MaxFrameNum
= 1 << sps
->log2_max_frame_num
;
604 uint64_t pic_order_cnt_type
= nalu_get_exp_golomb_ue( bits
);
605 if( pic_order_cnt_type
> 2 )
606 return LSMASH_ERR_INVALID_DATA
;
607 sps
->pic_order_cnt_type
= pic_order_cnt_type
;
608 if( sps
->pic_order_cnt_type
== 0 )
610 uint64_t log2_max_pic_order_cnt_lsb_minus4
= nalu_get_exp_golomb_ue( bits
);
611 if( log2_max_pic_order_cnt_lsb_minus4
> 12 )
612 return LSMASH_ERR_INVALID_DATA
;
613 sps
->log2_max_pic_order_cnt_lsb
= log2_max_pic_order_cnt_lsb_minus4
+ 4;
614 sps
->MaxPicOrderCntLsb
= 1 << sps
->log2_max_pic_order_cnt_lsb
;
616 else if( sps
->pic_order_cnt_type
== 1 )
618 sps
->delta_pic_order_always_zero_flag
= lsmash_bits_get( bits
, 1 );
619 static const int64_t max_value
= ((uint64_t)1 << 31) - 1;
620 static const int64_t min_value
= -((uint64_t)1 << 31) + 1;
621 int64_t offset_for_non_ref_pic
= nalu_get_exp_golomb_se( bits
);
622 if( offset_for_non_ref_pic
< min_value
|| offset_for_non_ref_pic
> max_value
)
623 return LSMASH_ERR_INVALID_DATA
;
624 sps
->offset_for_non_ref_pic
= offset_for_non_ref_pic
;
625 int64_t offset_for_top_to_bottom_field
= nalu_get_exp_golomb_se( bits
);
626 if( offset_for_top_to_bottom_field
< min_value
|| offset_for_top_to_bottom_field
> max_value
)
627 return LSMASH_ERR_INVALID_DATA
;
628 sps
->offset_for_top_to_bottom_field
= offset_for_top_to_bottom_field
;
629 uint64_t num_ref_frames_in_pic_order_cnt_cycle
= nalu_get_exp_golomb_ue( bits
);
630 if( num_ref_frames_in_pic_order_cnt_cycle
> 255 )
631 return LSMASH_ERR_INVALID_DATA
;
632 sps
->num_ref_frames_in_pic_order_cnt_cycle
= num_ref_frames_in_pic_order_cnt_cycle
;
633 sps
->ExpectedDeltaPerPicOrderCntCycle
= 0;
634 for( int i
= 0; i
< num_ref_frames_in_pic_order_cnt_cycle
; i
++ )
636 int64_t offset_for_ref_frame
= nalu_get_exp_golomb_se( bits
);
637 if( offset_for_ref_frame
< min_value
|| offset_for_ref_frame
> max_value
)
638 return LSMASH_ERR_INVALID_DATA
;
639 sps
->offset_for_ref_frame
[i
] = offset_for_ref_frame
;
640 sps
->ExpectedDeltaPerPicOrderCntCycle
+= offset_for_ref_frame
;
643 sps
->max_num_ref_frames
= nalu_get_exp_golomb_ue( bits
);
644 lsmash_bits_get( bits
, 1 ); /* gaps_in_frame_num_value_allowed_flag */
645 uint64_t pic_width_in_mbs_minus1
= nalu_get_exp_golomb_ue( bits
);
646 uint64_t pic_height_in_map_units_minus1
= nalu_get_exp_golomb_ue( bits
);
647 sps
->frame_mbs_only_flag
= lsmash_bits_get( bits
, 1 );
648 if( !sps
->frame_mbs_only_flag
)
649 lsmash_bits_get( bits
, 1 ); /* mb_adaptive_frame_field_flag */
650 lsmash_bits_get( bits
, 1 ); /* direct_8x8_inference_flag */
651 uint64_t PicWidthInMbs
= pic_width_in_mbs_minus1
+ 1;
652 uint64_t PicHeightInMapUnits
= pic_height_in_map_units_minus1
+ 1;
653 sps
->PicSizeInMapUnits
= PicWidthInMbs
* PicHeightInMapUnits
;
654 sps
->cropped_width
= PicWidthInMbs
* 16;
655 sps
->cropped_height
= (2 - sps
->frame_mbs_only_flag
) * PicHeightInMapUnits
* 16;
656 if( lsmash_bits_get( bits
, 1 ) ) /* frame_cropping_flag */
660 if( sps
->ChromaArrayType
== 0 )
663 CropUnitY
= 2 - sps
->frame_mbs_only_flag
;
667 static const int SubWidthC
[] = { 0, 2, 2, 1 };
668 static const int SubHeightC
[] = { 0, 2, 1, 1 };
669 CropUnitX
= SubWidthC
[ sps
->chroma_format_idc
];
670 CropUnitY
= SubHeightC
[ sps
->chroma_format_idc
] * (2 - sps
->frame_mbs_only_flag
);
672 uint64_t frame_crop_left_offset
= nalu_get_exp_golomb_ue( bits
);
673 uint64_t frame_crop_right_offset
= nalu_get_exp_golomb_ue( bits
);
674 uint64_t frame_crop_top_offset
= nalu_get_exp_golomb_ue( bits
);
675 uint64_t frame_crop_bottom_offset
= nalu_get_exp_golomb_ue( bits
);
676 sps
->cropped_width
-= (frame_crop_left_offset
+ frame_crop_right_offset
) * CropUnitX
;
677 sps
->cropped_height
-= (frame_crop_top_offset
+ frame_crop_bottom_offset
) * CropUnitY
;
679 if( lsmash_bits_get( bits
, 1 ) ) /* vui_parameters_present_flag */
681 /* vui_parameters() */
682 if( lsmash_bits_get( bits
, 1 ) ) /* aspect_ratio_info_present_flag */
684 uint8_t aspect_ratio_idc
= lsmash_bits_get( bits
, 8 );
685 if( aspect_ratio_idc
== 255 )
688 sps
->vui
.sar_width
= lsmash_bits_get( bits
, 16 );
689 sps
->vui
.sar_height
= lsmash_bits_get( bits
, 16 );
699 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
700 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 }, { 80, 33 },
701 { 18, 11 }, { 15, 11 }, { 64, 33 }, { 160, 99 }, { 4, 3 },
704 if( aspect_ratio_idc
< (sizeof(pre_defined_sar
) / sizeof(pre_defined_sar
[0])) )
706 sps
->vui
.sar_width
= pre_defined_sar
[ aspect_ratio_idc
].sar_width
;
707 sps
->vui
.sar_height
= pre_defined_sar
[ aspect_ratio_idc
].sar_height
;
711 /* Behavior when unknown aspect_ratio_idc is detected is not specified in the specification. */
712 sps
->vui
.sar_width
= 0;
713 sps
->vui
.sar_height
= 0;
717 if( lsmash_bits_get( bits
, 1 ) ) /* overscan_info_present_flag */
718 lsmash_bits_get( bits
, 1 ); /* overscan_appropriate_flag */
719 if( lsmash_bits_get( bits
, 1 ) ) /* video_signal_type_present_flag */
721 lsmash_bits_get( bits
, 3 ); /* video_format */
722 sps
->vui
.video_full_range_flag
= lsmash_bits_get( bits
, 1 );
723 if( lsmash_bits_get( bits
, 1 ) ) /* colour_description_present_flag */
725 sps
->vui
.colour_primaries
= lsmash_bits_get( bits
, 8 );
726 sps
->vui
.transfer_characteristics
= lsmash_bits_get( bits
, 8 );
727 sps
->vui
.matrix_coefficients
= lsmash_bits_get( bits
, 8 );
730 if( lsmash_bits_get( bits
, 1 ) ) /* chroma_loc_info_present_flag */
732 nalu_get_exp_golomb_ue( bits
); /* chroma_sample_loc_type_top_field */
733 nalu_get_exp_golomb_ue( bits
); /* chroma_sample_loc_type_bottom_field */
735 if( lsmash_bits_get( bits
, 1 ) ) /* timing_info_present_flag */
737 sps
->vui
.num_units_in_tick
= lsmash_bits_get( bits
, 32 );
738 sps
->vui
.time_scale
= lsmash_bits_get( bits
, 32 );
739 sps
->vui
.fixed_frame_rate_flag
= lsmash_bits_get( bits
, 1 );
743 sps
->vui
.num_units_in_tick
= 1; /* arbitrary */
744 sps
->vui
.time_scale
= 50; /* arbitrary */
745 sps
->vui
.fixed_frame_rate_flag
= 0;
747 int nal_hrd_parameters_present_flag
= lsmash_bits_get( bits
, 1 );
748 if( nal_hrd_parameters_present_flag
749 && (err
= h264_parse_hrd_parameters( bits
, &sps
->vui
.hrd
)) < 0 )
751 int vcl_hrd_parameters_present_flag
= lsmash_bits_get( bits
, 1 );
752 if( vcl_hrd_parameters_present_flag
753 && (err
= h264_parse_hrd_parameters( bits
, &sps
->vui
.hrd
)) < 0 )
755 if( nal_hrd_parameters_present_flag
|| vcl_hrd_parameters_present_flag
)
757 sps
->vui
.hrd
.present
= 1;
758 sps
->vui
.hrd
.CpbDpbDelaysPresentFlag
= 1;
759 lsmash_bits_get( bits
, 1 ); /* low_delay_hrd_flag */
761 sps
->vui
.pic_struct_present_flag
= lsmash_bits_get( bits
, 1 );
762 if( lsmash_bits_get( bits
, 1 ) ) /* bitstream_restriction_flag */
764 lsmash_bits_get( bits
, 1 ); /* motion_vectors_over_pic_boundaries_flag */
765 nalu_get_exp_golomb_ue( bits
); /* max_bytes_per_pic_denom */
766 nalu_get_exp_golomb_ue( bits
); /* max_bits_per_mb_denom */
767 nalu_get_exp_golomb_ue( bits
); /* log2_max_mv_length_horizontal */
768 nalu_get_exp_golomb_ue( bits
); /* log2_max_mv_length_vertical */
769 nalu_get_exp_golomb_ue( bits
); /* max_num_reorder_frames */
770 nalu_get_exp_golomb_ue( bits
); /* max_dec_frame_buffering */
775 sps
->vui
.video_full_range_flag
= 0;
776 sps
->vui
.num_units_in_tick
= 1; /* arbitrary */
777 sps
->vui
.time_scale
= 50; /* arbitrary */
778 sps
->vui
.fixed_frame_rate_flag
= 0;
780 /* rbsp_trailing_bits() */
781 if( !lsmash_bits_get( bits
, 1 ) ) /* rbsp_stop_one_bit */
782 return LSMASH_ERR_INVALID_DATA
;
783 lsmash_bits_empty( bits
);
784 if( bits
->bs
->error
)
785 return LSMASH_ERR_NAMELESS
;
791 static int h264_parse_pps_minimally
795 uint8_t *rbsp_buffer
,
800 int err
= nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, ebsp
, ebsp_size
);
803 memset( pps
, 0, sizeof(h264_pps_t
) );
804 uint64_t pic_parameter_set_id
= nalu_get_exp_golomb_ue( bits
);
805 if( pic_parameter_set_id
> 255 )
806 return LSMASH_ERR_INVALID_DATA
;
807 pps
->pic_parameter_set_id
= pic_parameter_set_id
;
808 return bits
->bs
->error
? LSMASH_ERR_NAMELESS
: 0;
814 uint8_t *rbsp_buffer
,
819 lsmash_bits_t
*bits
= info
->bits
;
820 /* pic_parameter_set_rbsp */
822 int err
= h264_parse_pps_minimally( bits
, &temp_pps
, rbsp_buffer
, ebsp
, ebsp_size
);
825 h264_pps_t
*pps
= h264_get_pps( info
->pps_list
, temp_pps
.pic_parameter_set_id
);
827 return LSMASH_ERR_NAMELESS
;
828 memset( pps
, 0, sizeof(h264_pps_t
) );
829 pps
->pic_parameter_set_id
= temp_pps
.pic_parameter_set_id
;
830 uint64_t seq_parameter_set_id
= nalu_get_exp_golomb_ue( bits
);
831 if( seq_parameter_set_id
> 31 )
832 return LSMASH_ERR_INVALID_DATA
;
833 h264_sps_t
*sps
= h264_get_sps( info
->sps_list
, seq_parameter_set_id
);
835 return LSMASH_ERR_NAMELESS
;
836 pps
->seq_parameter_set_id
= seq_parameter_set_id
;
837 pps
->entropy_coding_mode_flag
= lsmash_bits_get( bits
, 1 );
838 pps
->bottom_field_pic_order_in_frame_present_flag
= lsmash_bits_get( bits
, 1 );
839 uint64_t num_slice_groups_minus1
= nalu_get_exp_golomb_ue( bits
);
840 if( num_slice_groups_minus1
> 7 )
841 return LSMASH_ERR_INVALID_DATA
;
842 pps
->num_slice_groups_minus1
= num_slice_groups_minus1
;
843 if( num_slice_groups_minus1
) /* num_slice_groups_minus1 */
845 uint64_t slice_group_map_type
= nalu_get_exp_golomb_ue( bits
);
846 if( slice_group_map_type
> 6 )
847 return LSMASH_ERR_INVALID_DATA
;
848 pps
->slice_group_map_type
= slice_group_map_type
;
849 if( slice_group_map_type
== 0 )
850 for( uint64_t iGroup
= 0; iGroup
<= num_slice_groups_minus1
; iGroup
++ )
851 nalu_get_exp_golomb_ue( bits
); /* run_length_minus1[ iGroup ] */
852 else if( slice_group_map_type
== 2 )
853 for( uint64_t iGroup
= 0; iGroup
< num_slice_groups_minus1
; iGroup
++ )
855 nalu_get_exp_golomb_ue( bits
); /* top_left [ iGroup ] */
856 nalu_get_exp_golomb_ue( bits
); /* bottom_right[ iGroup ] */
858 else if( slice_group_map_type
== 3
859 || slice_group_map_type
== 4
860 || slice_group_map_type
== 5 )
862 lsmash_bits_get( bits
, 1 ); /* slice_group_change_direction_flag */
863 uint64_t slice_group_change_rate_minus1
= nalu_get_exp_golomb_ue( bits
);
864 if( slice_group_change_rate_minus1
> (sps
->PicSizeInMapUnits
- 1) )
865 return LSMASH_ERR_INVALID_DATA
;
866 pps
->SliceGroupChangeRate
= slice_group_change_rate_minus1
+ 1;
868 else if( slice_group_map_type
== 6 )
870 uint64_t pic_size_in_map_units_minus1
= nalu_get_exp_golomb_ue( bits
);
871 int length
= lsmash_ceil_log2( num_slice_groups_minus1
+ 1 );
872 for( uint64_t i
= 0; i
<= pic_size_in_map_units_minus1
; i
++ )
874 if( lsmash_bits_get( bits
, length
) > num_slice_groups_minus1
)
875 return LSMASH_ERR_INVALID_DATA
;
878 pps
->num_ref_idx_l0_default_active_minus1
= nalu_get_exp_golomb_ue( bits
);
879 pps
->num_ref_idx_l1_default_active_minus1
= nalu_get_exp_golomb_ue( bits
);
880 pps
->weighted_pred_flag
= lsmash_bits_get( bits
, 1 );
881 pps
->weighted_bipred_idc
= lsmash_bits_get( bits
, 2 );
882 nalu_get_exp_golomb_se( bits
); /* pic_init_qp_minus26 */
883 nalu_get_exp_golomb_se( bits
); /* pic_init_qs_minus26 */
884 nalu_get_exp_golomb_se( bits
); /* chroma_qp_index_offset */
885 pps
->deblocking_filter_control_present_flag
= lsmash_bits_get( bits
, 1 );
886 lsmash_bits_get( bits
, 1 ); /* constrained_intra_pred_flag */
887 pps
->redundant_pic_cnt_present_flag
= lsmash_bits_get( bits
, 1 );
888 if( nalu_check_more_rbsp_data( bits
) )
890 int transform_8x8_mode_flag
= lsmash_bits_get( bits
, 1 );
891 if( lsmash_bits_get( bits
, 1 ) ) /* pic_scaling_matrix_present_flag */
893 int num_loops
= 6 + (sps
->chroma_format_idc
!= 3 ? 2 : 6) * transform_8x8_mode_flag
;
894 for( int i
= 0; i
< num_loops
; i
++ )
895 if( lsmash_bits_get( bits
, 1 ) /* pic_scaling_list_present_flag[i] */
896 && (err
= h264_parse_scaling_list( bits
, i
< 6 ? 16 : 64 )) < 0 )
899 nalu_get_exp_golomb_se( bits
); /* second_chroma_qp_index_offset */
901 /* rbsp_trailing_bits() */
902 if( !lsmash_bits_get( bits
, 1 ) ) /* rbsp_stop_one_bit */
903 return LSMASH_ERR_INVALID_DATA
;
904 lsmash_bits_empty( bits
);
905 if( bits
->bs
->error
)
906 return LSMASH_ERR_NAMELESS
;
918 uint8_t *rbsp_buffer
,
923 int err
= nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, ebsp
, ebsp_size
);
926 uint8_t *rbsp_start
= rbsp_buffer
;
927 uint64_t rbsp_pos
= 0;
931 uint32_t payloadType
= 0;
932 for( uint8_t temp
= lsmash_bits_get( bits
, 8 ); ; temp
= lsmash_bits_get( bits
, 8 ) )
935 * otherwise: last_payload_type_byte */
941 uint32_t payloadSize
= 0;
942 for( uint8_t temp
= lsmash_bits_get( bits
, 8 ); ; temp
= lsmash_bits_get( bits
, 8 ) )
945 * otherwise: last_payload_size_byte */
951 if( payloadType
== 1 )
954 h264_hrd_t
*hrd
= sps
? &sps
->vui
.hrd
: NULL
;
956 goto skip_sei_message
; /* Any active SPS is not found. */
957 sei
->pic_timing
.present
= 1;
958 if( hrd
->CpbDpbDelaysPresentFlag
)
960 lsmash_bits_get( bits
, hrd
->cpb_removal_delay_length
); /* cpb_removal_delay */
961 lsmash_bits_get( bits
, hrd
->dpb_output_delay_length
); /* dpb_output_delay */
963 if( sps
->vui
.pic_struct_present_flag
)
965 sei
->pic_timing
.pic_struct
= lsmash_bits_get( bits
, 4 );
966 /* Skip the remaining bits. */
967 uint32_t remaining_bits
= payloadSize
* 8 - 4;
968 if( hrd
->CpbDpbDelaysPresentFlag
)
969 remaining_bits
-= hrd
->cpb_removal_delay_length
970 + hrd
->dpb_output_delay_length
;
971 lsmash_bits_get( bits
, remaining_bits
);
974 else if( payloadType
== 3 )
977 * 'avc1' and 'avc2' samples are forbidden to contain this. */
978 return LSMASH_ERR_PATCH_WELCOME
;
980 else if( payloadType
== 6 )
983 sei
->recovery_point
.present
= 1;
984 sei
->recovery_point
.random_accessible
= 1;
985 sei
->recovery_point
.recovery_frame_cnt
= nalu_get_exp_golomb_ue( bits
);
986 lsmash_bits_get( bits
, 1 ); /* exact_match_flag */
987 sei
->recovery_point
.broken_link_flag
= lsmash_bits_get( bits
, 1 );
988 lsmash_bits_get( bits
, 2 ); /* changing_slice_group_idc */
993 lsmash_bits_get( bits
, payloadSize
* 8 );
995 lsmash_bits_get_align( bits
);
996 rbsp_pos
+= payloadSize
;
997 } while( *(rbsp_start
+ rbsp_pos
) != 0x80 ); /* All SEI messages are byte aligned at their end.
998 * Therefore, 0x80 shall be rbsp_trailing_bits(). */
999 lsmash_bits_empty( bits
);
1000 return bits
->bs
->error
? LSMASH_ERR_NAMELESS
: 0;
1003 static int h264_parse_slice_header
1006 h264_nalu_header_t
*nuh
1009 h264_slice_info_t
*slice
= &info
->slice
;
1010 memset( slice
, 0, sizeof(h264_slice_info_t
) );
1011 /* slice_header() */
1012 lsmash_bits_t
*bits
= info
->bits
;
1013 nalu_get_exp_golomb_ue( bits
); /* first_mb_in_slice */
1014 uint8_t slice_type
= slice
->type
= nalu_get_exp_golomb_ue( bits
);
1015 if( (uint64_t)slice
->type
> 9 )
1016 return LSMASH_ERR_INVALID_DATA
;
1017 if( slice_type
> 4 )
1018 slice_type
= slice
->type
-= 5;
1019 uint64_t pic_parameter_set_id
= nalu_get_exp_golomb_ue( bits
);
1020 if( pic_parameter_set_id
> 255 )
1021 return LSMASH_ERR_INVALID_DATA
;
1022 slice
->pic_parameter_set_id
= pic_parameter_set_id
;
1023 h264_pps_t
*pps
= h264_get_pps( info
->pps_list
, pic_parameter_set_id
);
1025 return LSMASH_ERR_NAMELESS
;
1026 h264_sps_t
*sps
= h264_get_sps( info
->sps_list
, pps
->seq_parameter_set_id
);
1028 return LSMASH_ERR_NAMELESS
;
1029 slice
->seq_parameter_set_id
= pps
->seq_parameter_set_id
;
1030 slice
->nal_ref_idc
= nuh
->nal_ref_idc
;
1031 slice
->IdrPicFlag
= (nuh
->nal_unit_type
== H264_NALU_TYPE_SLICE_IDR
);
1032 slice
->pic_order_cnt_type
= sps
->pic_order_cnt_type
;
1033 if( (slice
->IdrPicFlag
|| sps
->max_num_ref_frames
== 0) && slice_type
!= 2 && slice_type
!= 4 )
1034 return LSMASH_ERR_INVALID_DATA
;
1035 if( sps
->separate_colour_plane_flag
)
1036 lsmash_bits_get( bits
, 2 ); /* colour_plane_id */
1037 uint64_t frame_num
= lsmash_bits_get( bits
, sps
->log2_max_frame_num
);
1038 if( frame_num
>= (1 << sps
->log2_max_frame_num
) || (slice
->IdrPicFlag
&& frame_num
) )
1039 return LSMASH_ERR_INVALID_DATA
;
1040 slice
->frame_num
= frame_num
;
1041 if( !sps
->frame_mbs_only_flag
)
1043 slice
->field_pic_flag
= lsmash_bits_get( bits
, 1 );
1044 if( slice
->field_pic_flag
)
1045 slice
->bottom_field_flag
= lsmash_bits_get( bits
, 1 );
1047 if( slice
->IdrPicFlag
)
1049 uint64_t idr_pic_id
= nalu_get_exp_golomb_ue( bits
);
1050 if( idr_pic_id
> 65535 )
1051 return LSMASH_ERR_INVALID_DATA
;
1052 slice
->idr_pic_id
= idr_pic_id
;
1054 if( sps
->pic_order_cnt_type
== 0 )
1056 uint64_t pic_order_cnt_lsb
= lsmash_bits_get( bits
, sps
->log2_max_pic_order_cnt_lsb
);
1057 if( pic_order_cnt_lsb
>= sps
->MaxPicOrderCntLsb
)
1058 return LSMASH_ERR_INVALID_DATA
;
1059 slice
->pic_order_cnt_lsb
= pic_order_cnt_lsb
;
1060 if( pps
->bottom_field_pic_order_in_frame_present_flag
&& !slice
->field_pic_flag
)
1061 slice
->delta_pic_order_cnt_bottom
= nalu_get_exp_golomb_se( bits
);
1063 else if( sps
->pic_order_cnt_type
== 1 && !sps
->delta_pic_order_always_zero_flag
)
1065 slice
->delta_pic_order_cnt
[0] = nalu_get_exp_golomb_se( bits
);
1066 if( pps
->bottom_field_pic_order_in_frame_present_flag
&& !slice
->field_pic_flag
)
1067 slice
->delta_pic_order_cnt
[1] = nalu_get_exp_golomb_se( bits
);
1069 if( pps
->redundant_pic_cnt_present_flag
)
1071 uint64_t redundant_pic_cnt
= nalu_get_exp_golomb_ue( bits
);
1072 if( redundant_pic_cnt
> 127 )
1073 return LSMASH_ERR_INVALID_DATA
;
1074 slice
->has_redundancy
= !!redundant_pic_cnt
;
1076 if( slice_type
== H264_SLICE_TYPE_B
)
1077 lsmash_bits_get( bits
, 1 );
1078 uint64_t num_ref_idx_l0_active_minus1
= pps
->num_ref_idx_l0_default_active_minus1
;
1079 uint64_t num_ref_idx_l1_active_minus1
= pps
->num_ref_idx_l1_default_active_minus1
;
1080 if( slice_type
== H264_SLICE_TYPE_P
|| slice_type
== H264_SLICE_TYPE_SP
|| slice_type
== H264_SLICE_TYPE_B
)
1082 if( lsmash_bits_get( bits
, 1 ) ) /* num_ref_idx_active_override_flag */
1084 num_ref_idx_l0_active_minus1
= nalu_get_exp_golomb_ue( bits
);
1085 if( num_ref_idx_l0_active_minus1
> 31 )
1086 return LSMASH_ERR_INVALID_DATA
;
1087 if( slice_type
== H264_SLICE_TYPE_B
)
1089 num_ref_idx_l1_active_minus1
= nalu_get_exp_golomb_ue( bits
);
1090 if( num_ref_idx_l1_active_minus1
> 31 )
1091 return LSMASH_ERR_INVALID_DATA
;
1095 if( nuh
->nal_unit_type
== H264_NALU_TYPE_SLICE_EXT
1096 || nuh
->nal_unit_type
== H264_NALU_TYPE_SLICE_EXT_DVC
)
1098 return LSMASH_ERR_PATCH_WELCOME
; /* No support of MVC yet */
1100 /* ref_pic_list_mvc_modification() */
1101 if( slice_type
== H264_SLICE_TYPE_P
|| slice_type
== H264_SLICE_TYPE_B
|| slice_type
== H264_SLICE_TYPE_SP
)
1103 for( int i
= 0; i
< 1 + (slice_type
== H264_SLICE_TYPE_B
); i
++ )
1105 if( lsmash_bits_get( bits
, 1 ) ) /* (S)P and B: ref_pic_list_modification_flag_l0
1106 * B: ref_pic_list_modification_flag_l1 */
1108 uint64_t modification_of_pic_nums_idc
;
1111 modification_of_pic_nums_idc
= nalu_get_exp_golomb_ue( bits
);
1113 if( modification_of_pic_nums_idc
== 0 || modification_of_pic_nums_idc
== 1 )
1114 nalu_get_exp_golomb_ue( bits
); /* abs_diff_pic_num_minus1 */
1115 else if( modification_of_pic_nums_idc
== 2 )
1116 nalu_get_exp_golomb_ue( bits
); /* long_term_pic_num */
1117 else if( modification_of_pic_nums_idc
== 4 || modification_of_pic_nums_idc
== 5 )
1118 nalu_get_exp_golomb_ue( bits
); /* abs_diff_view_idx_minus1 */
1120 if( modification_of_pic_nums_idc
!= 3 )
1121 nalu_get_exp_golomb_ue( bits
); /* abs_diff_pic_num_minus1, long_term_pic_num or abs_diff_view_idx_minus1 */
1123 } while( modification_of_pic_nums_idc
!= 3 );
1131 /* ref_pic_list_modification() */
1132 if( slice_type
== H264_SLICE_TYPE_P
|| slice_type
== H264_SLICE_TYPE_B
|| slice_type
== H264_SLICE_TYPE_SP
)
1134 for( int i
= 0; i
< 1 + (slice_type
== H264_SLICE_TYPE_B
); i
++ )
1136 if( lsmash_bits_get( bits
, 1 ) ) /* (S)P and B: ref_pic_list_modification_flag_l0
1137 * B: ref_pic_list_modification_flag_l1 */
1139 uint64_t modification_of_pic_nums_idc
;
1142 modification_of_pic_nums_idc
= nalu_get_exp_golomb_ue( bits
);
1144 if( modification_of_pic_nums_idc
== 0 || modification_of_pic_nums_idc
== 1 )
1145 nalu_get_exp_golomb_ue( bits
); /* abs_diff_pic_num_minus1 */
1146 else if( modification_of_pic_nums_idc
== 2 )
1147 nalu_get_exp_golomb_ue( bits
); /* long_term_pic_num */
1149 if( modification_of_pic_nums_idc
!= 3 )
1150 nalu_get_exp_golomb_ue( bits
); /* abs_diff_pic_num_minus1 or long_term_pic_num */
1152 } while( modification_of_pic_nums_idc
!= 3 );
1157 if( (pps
->weighted_pred_flag
&& (slice_type
== H264_SLICE_TYPE_P
|| slice_type
== H264_SLICE_TYPE_SP
))
1158 || (pps
->weighted_bipred_idc
== 1 && slice_type
== H264_SLICE_TYPE_B
) )
1160 /* pred_weight_table() */
1161 nalu_get_exp_golomb_ue( bits
); /* luma_log2_weight_denom */
1162 if( sps
->ChromaArrayType
)
1163 nalu_get_exp_golomb_ue( bits
); /* chroma_log2_weight_denom */
1164 for( uint8_t i
= 0; i
<= num_ref_idx_l0_active_minus1
; i
++ )
1166 if( lsmash_bits_get( bits
, 1 ) ) /* luma_weight_l0_flag */
1168 nalu_get_exp_golomb_se( bits
); /* luma_weight_l0[i] */
1169 nalu_get_exp_golomb_se( bits
); /* luma_offset_l0[i] */
1171 if( sps
->ChromaArrayType
1172 && lsmash_bits_get( bits
, 1 ) /* chroma_weight_l0_flag */ )
1173 for( int j
= 0; j
< 2; j
++ )
1175 nalu_get_exp_golomb_se( bits
); /* chroma_weight_l0[i][j]*/
1176 nalu_get_exp_golomb_se( bits
); /* chroma_offset_l0[i][j] */
1179 if( slice_type
== H264_SLICE_TYPE_B
)
1180 for( uint8_t i
= 0; i
<= num_ref_idx_l1_active_minus1
; i
++ )
1182 if( lsmash_bits_get( bits
, 1 ) ) /* luma_weight_l1_flag */
1184 nalu_get_exp_golomb_se( bits
); /* luma_weight_l1[i] */
1185 nalu_get_exp_golomb_se( bits
); /* luma_offset_l1[i] */
1187 if( sps
->ChromaArrayType
1188 && lsmash_bits_get( bits
, 1 ) /* chroma_weight_l1_flag */ )
1189 for( int j
= 0; j
< 2; j
++ )
1191 nalu_get_exp_golomb_se( bits
); /* chroma_weight_l1[i][j]*/
1192 nalu_get_exp_golomb_se( bits
); /* chroma_offset_l1[i][j] */
1196 if( nuh
->nal_ref_idc
)
1198 /* dec_ref_pic_marking() */
1199 if( slice
->IdrPicFlag
)
1201 lsmash_bits_get( bits
, 1 ); /* no_output_of_prior_pics_flag */
1202 lsmash_bits_get( bits
, 1 ); /* long_term_reference_flag */
1204 else if( lsmash_bits_get( bits
, 1 ) ) /* adaptive_ref_pic_marking_mode_flag */
1206 uint64_t memory_management_control_operation
;
1209 memory_management_control_operation
= nalu_get_exp_golomb_ue( bits
);
1210 if( memory_management_control_operation
)
1212 if( memory_management_control_operation
== 5 )
1213 slice
->has_mmco5
= 1;
1216 nalu_get_exp_golomb_ue( bits
);
1217 if( memory_management_control_operation
== 3 )
1218 nalu_get_exp_golomb_ue( bits
);
1221 } while( memory_management_control_operation
);
1224 /* We needn't read more if not slice data partition A.
1225 * Skip slice_data() and rbsp_slice_trailing_bits(). */
1226 if( nuh
->nal_unit_type
== H264_NALU_TYPE_SLICE_DP_A
)
1228 if( pps
->entropy_coding_mode_flag
&& slice_type
!= H264_SLICE_TYPE_I
&& slice_type
!= H264_SLICE_TYPE_SI
)
1229 nalu_get_exp_golomb_ue( bits
); /* cabac_init_idc */
1230 nalu_get_exp_golomb_se( bits
); /* slice_qp_delta */
1231 if( slice_type
== H264_SLICE_TYPE_SP
|| slice_type
== H264_SLICE_TYPE_SI
)
1233 if( slice_type
== H264_SLICE_TYPE_SP
)
1234 lsmash_bits_get( bits
, 1 ); /* sp_for_switch_flag */
1235 nalu_get_exp_golomb_se( bits
); /* slice_qs_delta */
1237 if( pps
->deblocking_filter_control_present_flag
1238 && nalu_get_exp_golomb_ue( bits
) != 1 /* disable_deblocking_filter_idc */ )
1240 int64_t slice_alpha_c0_offset_div2
= nalu_get_exp_golomb_se( bits
);
1241 if( slice_alpha_c0_offset_div2
< -6 || slice_alpha_c0_offset_div2
> 6 )
1242 return LSMASH_ERR_INVALID_DATA
;
1243 int64_t slice_beta_offset_div2
= nalu_get_exp_golomb_se( bits
);
1244 if( slice_beta_offset_div2
< -6 || slice_beta_offset_div2
> 6 )
1245 return LSMASH_ERR_INVALID_DATA
;
1247 if( pps
->num_slice_groups_minus1
1248 && (pps
->slice_group_map_type
== 3 || pps
->slice_group_map_type
== 4 || pps
->slice_group_map_type
== 5) )
1250 uint64_t temp
= ((uint64_t)sps
->PicSizeInMapUnits
- 1) / pps
->SliceGroupChangeRate
+ 1;
1251 uint64_t slice_group_change_cycle
= lsmash_bits_get( bits
, lsmash_ceil_log2( temp
+ 1 ) );
1252 if( slice_group_change_cycle
> temp
)
1253 return LSMASH_ERR_INVALID_DATA
;
1255 /* end of slice_header() */
1256 slice
->slice_id
= nalu_get_exp_golomb_ue( bits
);
1257 h264_slice_info_t
*slice_part
= h264_get_slice_info( info
->slice_list
, slice
->slice_id
);
1259 return LSMASH_ERR_NAMELESS
;
1260 *slice_part
= *slice
;
1262 lsmash_bits_empty( bits
);
1263 if( bits
->bs
->error
)
1264 return LSMASH_ERR_NAMELESS
;
1270 int h264_parse_slice
1273 h264_nalu_header_t
*nuh
,
1274 uint8_t *rbsp_buffer
,
1279 lsmash_bits_t
*bits
= info
->bits
;
1280 uint64_t size
= nuh
->nal_unit_type
== H264_NALU_TYPE_SLICE_IDR
|| nuh
->nal_ref_idc
== 0
1281 ? LSMASH_MIN( ebsp_size
, 100 )
1282 : LSMASH_MIN( ebsp_size
, 1000 );
1283 int err
= nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, ebsp
, size
);
1286 if( nuh
->nal_unit_type
!= H264_NALU_TYPE_SLICE_DP_B
1287 && nuh
->nal_unit_type
!= H264_NALU_TYPE_SLICE_DP_C
)
1288 return h264_parse_slice_header( info
, nuh
);
1289 /* slice_data_partition_b_layer_rbsp() or slice_data_partition_c_layer_rbsp() */
1290 uint64_t slice_id
= nalu_get_exp_golomb_ue( bits
);
1291 h264_slice_info_t
*slice
= h264_get_slice_info( info
->slice_list
, slice_id
);
1293 return LSMASH_ERR_NAMELESS
;
1294 h264_pps_t
*pps
= h264_get_pps( info
->pps_list
, slice
->pic_parameter_set_id
);
1296 return LSMASH_ERR_NAMELESS
;
1297 h264_sps_t
*sps
= h264_get_sps( info
->sps_list
, pps
->seq_parameter_set_id
);
1299 return LSMASH_ERR_NAMELESS
;
1300 slice
->seq_parameter_set_id
= pps
->seq_parameter_set_id
;
1301 if( sps
->separate_colour_plane_flag
)
1302 lsmash_bits_get( bits
, 2 ); /* colour_plane_id */
1303 if( pps
->redundant_pic_cnt_present_flag
)
1305 uint64_t redundant_pic_cnt
= nalu_get_exp_golomb_ue( bits
);
1306 if( redundant_pic_cnt
> 127 )
1307 return LSMASH_ERR_INVALID_DATA
;
1308 slice
->has_redundancy
= !!redundant_pic_cnt
;
1310 /* Skip slice_data() and rbsp_slice_trailing_bits(). */
1311 lsmash_bits_empty( bits
);
1312 if( bits
->bs
->error
)
1313 return LSMASH_ERR_NAMELESS
;
1319 static int h264_get_sps_id
1322 uint32_t ps_ebsp_length
,
1326 /* max number of bits of sps_id = 11: 0b000001XXXXX
1327 * (24 + 11 - 1) / 8 + 1 = 5 bytes
1328 * Why +1? Because there might be an emulation_prevention_three_byte. */
1329 lsmash_bits_t bits
= { 0 };
1330 lsmash_bs_t bs
= { 0 };
1331 uint8_t rbsp_buffer
[6];
1333 bs
.buffer
.data
= buffer
;
1334 bs
.buffer
.alloc
= 6;
1335 lsmash_bits_init( &bits
, &bs
);
1336 int err
= nalu_import_rbsp_from_ebsp( &bits
, rbsp_buffer
, ps_ebsp
, LSMASH_MIN( ps_ebsp_length
, 6 ) );
1339 lsmash_bits_get( &bits
, 24 ); /* profile_idc, constraint_set_flags and level_idc */
1340 uint64_t sec_parameter_set_id
= nalu_get_exp_golomb_ue( &bits
);
1341 if( sec_parameter_set_id
> 31 )
1342 return LSMASH_ERR_INVALID_DATA
;
1343 *ps_id
= sec_parameter_set_id
;
1344 return bs
.error
? LSMASH_ERR_NAMELESS
: 0;
1347 static int h264_get_pps_id
1350 uint32_t ps_ebsp_length
,
1354 /* max number of bits of pps_id = 17: 0b000000001XXXXXXXX
1355 * (17 - 1) / 8 + 1 = 3 bytes
1356 * Why +1? Because there might be an emulation_prevention_three_byte. */
1357 lsmash_bits_t bits
= { 0 };
1358 lsmash_bs_t bs
= { 0 };
1359 uint8_t rbsp_buffer
[4];
1361 bs
.buffer
.data
= buffer
;
1362 bs
.buffer
.alloc
= 4;
1363 lsmash_bits_init( &bits
, &bs
);
1364 int err
= nalu_import_rbsp_from_ebsp( &bits
, rbsp_buffer
, ps_ebsp
, LSMASH_MIN( ps_ebsp_length
, 4 ) );
1367 uint64_t pic_parameter_set_id
= nalu_get_exp_golomb_ue( &bits
);
1368 if( pic_parameter_set_id
> 255 )
1369 return LSMASH_ERR_INVALID_DATA
;
1370 *ps_id
= pic_parameter_set_id
;
1371 return bs
.error
? LSMASH_ERR_NAMELESS
: 0;
1374 static inline int h264_get_ps_id
1377 uint32_t ps_ebsp_length
,
1379 lsmash_h264_parameter_set_type ps_type
1382 int (*get_ps_id
)( uint8_t *ps_ebsp
, uint32_t ps_ebsp_length
, uint8_t *ps_id
)
1383 = ps_type
== H264_PARAMETER_SET_TYPE_SPS
? h264_get_sps_id
1384 : ps_type
== H264_PARAMETER_SET_TYPE_PPS
? h264_get_pps_id
1386 return get_ps_id
? get_ps_id( ps_ebsp
, ps_ebsp_length
, ps_id
) : LSMASH_ERR_INVALID_DATA
;
1389 static inline lsmash_entry_list_t
*h264_get_parameter_set_list
1391 lsmash_h264_specific_parameters_t
*param
,
1392 lsmash_h264_parameter_set_type ps_type
1395 if( !param
->parameter_sets
)
1397 return ps_type
== H264_PARAMETER_SET_TYPE_SPS
? param
->parameter_sets
->sps_list
1398 : ps_type
== H264_PARAMETER_SET_TYPE_PPS
? param
->parameter_sets
->pps_list
1399 : ps_type
== H264_PARAMETER_SET_TYPE_SPSEXT
? param
->parameter_sets
->spsext_list
1403 static lsmash_entry_t
*h264_get_ps_entry_from_param
1405 lsmash_h264_specific_parameters_t
*param
,
1406 lsmash_h264_parameter_set_type ps_type
,
1410 int (*get_ps_id
)( uint8_t *ps_ebsp
, uint32_t ps_ebsp_length
, uint8_t *ps_id
)
1411 = ps_type
== H264_PARAMETER_SET_TYPE_SPS
? h264_get_sps_id
1412 : ps_type
== H264_PARAMETER_SET_TYPE_PPS
? h264_get_pps_id
1416 lsmash_entry_list_t
*ps_list
= h264_get_parameter_set_list( param
, ps_type
);
1419 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
; entry
= entry
->next
)
1421 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1424 uint8_t param_ps_id
;
1425 if( get_ps_id( ps
->nalUnit
+ 1, ps
->nalUnitLength
- 1, ¶m_ps_id
) < 0 )
1427 if( ps_id
== param_ps_id
)
1433 static inline void h264_update_picture_type
1435 h264_picture_info_t
*picture
,
1436 h264_slice_info_t
*slice
1439 if( picture
->type
== H264_PICTURE_TYPE_I_P
)
1441 if( slice
->type
== H264_SLICE_TYPE_B
)
1442 picture
->type
= H264_PICTURE_TYPE_I_P_B
;
1443 else if( slice
->type
== H264_SLICE_TYPE_SI
|| slice
->type
== H264_SLICE_TYPE_SP
)
1444 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP
;
1446 else if( picture
->type
== H264_PICTURE_TYPE_I_P_B
)
1448 if( slice
->type
!= H264_SLICE_TYPE_P
&& slice
->type
!= H264_SLICE_TYPE_B
&& slice
->type
!= H264_SLICE_TYPE_I
)
1449 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP_B
;
1451 else if( picture
->type
== H264_PICTURE_TYPE_I
)
1453 if( slice
->type
== H264_SLICE_TYPE_P
)
1454 picture
->type
= H264_PICTURE_TYPE_I_P
;
1455 else if( slice
->type
== H264_SLICE_TYPE_B
)
1456 picture
->type
= H264_PICTURE_TYPE_I_P_B
;
1457 else if( slice
->type
== H264_SLICE_TYPE_SI
)
1458 picture
->type
= H264_PICTURE_TYPE_I_SI
;
1459 else if( slice
->type
== H264_SLICE_TYPE_SP
)
1460 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP
;
1462 else if( picture
->type
== H264_PICTURE_TYPE_SI_SP
)
1464 if( slice
->type
== H264_SLICE_TYPE_P
|| slice
->type
== H264_SLICE_TYPE_I
)
1465 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP
;
1466 else if( slice
->type
== H264_SLICE_TYPE_B
)
1467 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP_B
;
1469 else if( picture
->type
== H264_PICTURE_TYPE_SI
)
1471 if( slice
->type
== H264_SLICE_TYPE_P
)
1472 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP
;
1473 else if( slice
->type
== H264_SLICE_TYPE_B
)
1474 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP_B
;
1475 else if( slice
->type
!= H264_SLICE_TYPE_I
)
1476 picture
->type
= H264_PICTURE_TYPE_I_SI
;
1477 else if( slice
->type
== H264_SLICE_TYPE_SP
)
1478 picture
->type
= H264_PICTURE_TYPE_SI_SP
;
1480 else if( picture
->type
== H264_PICTURE_TYPE_I_SI
)
1482 if( slice
->type
== H264_SLICE_TYPE_P
|| slice
->type
== H264_SLICE_TYPE_SP
)
1483 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP
;
1484 else if( slice
->type
== H264_SLICE_TYPE_B
)
1485 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP_B
;
1487 else if( picture
->type
== H264_PICTURE_TYPE_I_SI_P_SP
)
1489 if( slice
->type
== H264_SLICE_TYPE_B
)
1490 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP_B
;
1492 else if( picture
->type
== H264_PICTURE_TYPE_NONE
)
1494 if( slice
->type
== H264_SLICE_TYPE_P
)
1495 picture
->type
= H264_PICTURE_TYPE_I_P
;
1496 else if( slice
->type
== H264_SLICE_TYPE_B
)
1497 picture
->type
= H264_PICTURE_TYPE_I_P_B
;
1498 else if( slice
->type
== H264_SLICE_TYPE_I
)
1499 picture
->type
= H264_PICTURE_TYPE_I
;
1500 else if( slice
->type
== H264_SLICE_TYPE_SI
)
1501 picture
->type
= H264_PICTURE_TYPE_SI
;
1502 else if( slice
->type
== H264_SLICE_TYPE_SP
)
1503 picture
->type
= H264_PICTURE_TYPE_SI_SP
;
1506 fprintf( stderr
, "Picture type = %s\n", picture
->type
== H264_PICTURE_TYPE_I_P
? "P"
1507 : picture
->type
== H264_PICTURE_TYPE_I_P_B
? "B"
1508 : picture
->type
== H264_PICTURE_TYPE_I
? "I"
1509 : picture
->type
== H264_PICTURE_TYPE_SI
? "SI"
1510 : picture
->type
== H264_PICTURE_TYPE_I_SI
? "SI"
1515 /* Shall be called at least once per picture. */
1516 void h264_update_picture_info_for_slice
1519 h264_picture_info_t
*picture
,
1520 h264_slice_info_t
*slice
1524 picture
->has_mmco5
|= slice
->has_mmco5
;
1525 picture
->has_redundancy
|= slice
->has_redundancy
;
1526 picture
->has_primary
|= !slice
->has_redundancy
;
1527 h264_update_picture_type( picture
, slice
);
1528 /* Mark 'used' on active parameter sets. */
1529 uint8_t ps_id
[2] = { slice
->seq_parameter_set_id
, slice
->pic_parameter_set_id
};
1530 for( int i
= 0; i
< 2; i
++ )
1532 lsmash_h264_parameter_set_type ps_type
= (lsmash_h264_parameter_set_type
)i
;
1533 lsmash_entry_t
*entry
= h264_get_ps_entry_from_param( &info
->avcC_param
, ps_type
, ps_id
[i
] );
1534 if( entry
&& entry
->data
)
1536 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1538 lsmash_append_h264_parameter_set( &info
->avcC_param
, ps_type
, ps
->nalUnit
, ps
->nalUnitLength
);
1541 /* Discard this slice info. */
1545 /* Shall be called exactly once per picture. */
1546 void h264_update_picture_info
1549 h264_picture_info_t
*picture
,
1550 h264_slice_info_t
*slice
,
1554 picture
->frame_num
= slice
->frame_num
;
1555 picture
->pic_order_cnt_lsb
= slice
->pic_order_cnt_lsb
;
1556 picture
->delta_pic_order_cnt_bottom
= slice
->delta_pic_order_cnt_bottom
;
1557 picture
->delta_pic_order_cnt
[0] = slice
->delta_pic_order_cnt
[0];
1558 picture
->delta_pic_order_cnt
[1] = slice
->delta_pic_order_cnt
[1];
1559 picture
->field_pic_flag
= slice
->field_pic_flag
;
1560 picture
->bottom_field_flag
= slice
->bottom_field_flag
;
1561 picture
->idr
= slice
->IdrPicFlag
;
1562 picture
->pic_parameter_set_id
= slice
->pic_parameter_set_id
;
1563 picture
->disposable
= (slice
->nal_ref_idc
== 0);
1564 picture
->random_accessible
= slice
->IdrPicFlag
;
1565 h264_update_picture_info_for_slice( info
, picture
, slice
);
1566 picture
->independent
= picture
->type
== H264_PICTURE_TYPE_I
|| picture
->type
== H264_PICTURE_TYPE_I_SI
;
1567 if( sei
->pic_timing
.present
)
1569 if( sei
->pic_timing
.pic_struct
< 9 )
1571 static const uint8_t DeltaTfiDivisor
[9] = { 2, 1, 1, 2, 2, 3, 3, 4, 6 };
1572 picture
->delta
= DeltaTfiDivisor
[ sei
->pic_timing
.pic_struct
];
1575 /* Reserved values in the spec we refer to. */
1576 picture
->delta
= picture
->field_pic_flag
? 1 : 2;
1577 sei
->pic_timing
.present
= 0;
1580 picture
->delta
= picture
->field_pic_flag
? 1 : 2;
1581 if( sei
->recovery_point
.present
)
1583 picture
->random_accessible
|= sei
->recovery_point
.random_accessible
;
1584 picture
->broken_link_flag
|= sei
->recovery_point
.broken_link_flag
;
1585 picture
->recovery_frame_cnt
= sei
->recovery_point
.recovery_frame_cnt
;
1586 sei
->recovery_point
.present
= 0;
1590 int h264_find_au_delimit_by_slice_info
1592 h264_slice_info_t
*slice
,
1593 h264_slice_info_t
*prev_slice
1596 if( slice
->frame_num
!= prev_slice
->frame_num
1597 || ((slice
->pic_order_cnt_type
== 0 && prev_slice
->pic_order_cnt_type
== 0)
1598 && (slice
->pic_order_cnt_lsb
!= prev_slice
->pic_order_cnt_lsb
1599 || slice
->delta_pic_order_cnt_bottom
!= prev_slice
->delta_pic_order_cnt_bottom
))
1600 || ((slice
->pic_order_cnt_type
== 1 && prev_slice
->pic_order_cnt_type
== 1)
1601 && (slice
->delta_pic_order_cnt
[0] != prev_slice
->delta_pic_order_cnt
[0]
1602 || slice
->delta_pic_order_cnt
[1] != prev_slice
->delta_pic_order_cnt
[1]))
1603 || slice
->field_pic_flag
!= prev_slice
->field_pic_flag
1604 || slice
->bottom_field_flag
!= prev_slice
->bottom_field_flag
1605 || slice
->IdrPicFlag
!= prev_slice
->IdrPicFlag
1606 || slice
->pic_parameter_set_id
!= prev_slice
->pic_parameter_set_id
1607 || ((slice
->nal_ref_idc
== 0 || prev_slice
->nal_ref_idc
== 0)
1608 && (slice
->nal_ref_idc
!= prev_slice
->nal_ref_idc
))
1609 || (slice
->IdrPicFlag
== 1 && prev_slice
->IdrPicFlag
== 1
1610 && slice
->idr_pic_id
!= prev_slice
->idr_pic_id
) )
1615 int h264_find_au_delimit_by_nalu_type
1618 uint8_t prev_nalu_type
1621 return ((nalu_type
>= H264_NALU_TYPE_SEI
&& nalu_type
<= H264_NALU_TYPE_AUD
)
1622 || (nalu_type
>= H264_NALU_TYPE_PREFIX
&& nalu_type
<= H264_NALU_TYPE_RSV_NVCL18
))
1623 && ((prev_nalu_type
>= H264_NALU_TYPE_SLICE_N_IDR
&& prev_nalu_type
<= H264_NALU_TYPE_SLICE_IDR
)
1624 || prev_nalu_type
== H264_NALU_TYPE_FD
|| prev_nalu_type
== H264_NALU_TYPE_SLICE_AUX
);
1627 int h264_supplement_buffer
1629 h264_stream_buffer_t
*sb
,
1630 h264_access_unit_t
*au
,
1634 lsmash_multiple_buffers_t
*bank
= lsmash_resize_multiple_buffers( sb
->bank
, size
);
1636 return LSMASH_ERR_MEMORY_ALLOC
;
1638 sb
->rbsp
= lsmash_withdraw_buffer( bank
, 1 );
1639 if( au
&& bank
->number_of_buffers
== 3 )
1641 au
->data
= lsmash_withdraw_buffer( bank
, 2 );
1642 au
->incomplete_data
= lsmash_withdraw_buffer( bank
, 3 );
1647 static void h264_bs_put_parameter_sets
1650 lsmash_entry_list_t
*ps_list
,
1651 uint32_t max_ps_count
1654 uint32_t ps_count
= 0;
1655 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
&& ps_count
< max_ps_count
; entry
= entry
->next
)
1657 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1658 if( ps
&& !ps
->unused
)
1660 lsmash_bs_put_be16( bs
, ps
->nalUnitLength
);
1661 lsmash_bs_put_bytes( bs
, ps
->nalUnitLength
, ps
->nalUnit
);
1669 uint8_t *lsmash_create_h264_specific_info
1671 lsmash_h264_specific_parameters_t
*param
,
1672 uint32_t *data_length
1675 if( !param
|| !param
->parameter_sets
|| !data_length
)
1677 if( param
->lengthSizeMinusOne
!= 0 && param
->lengthSizeMinusOne
!= 1 && param
->lengthSizeMinusOne
!= 3 )
1679 static const uint32_t max_ps_count
[3] = { 31, 255, 255 };
1680 lsmash_entry_list_t
*ps_list
[3] =
1682 param
->parameter_sets
->sps_list
, /* SPS */
1683 param
->parameter_sets
->pps_list
, /* PPS */
1684 param
->parameter_sets
->spsext_list
/* SPSExt */
1686 uint32_t ps_count
[3] = { 0, 0, 0 };
1687 /* SPS and PPS are mandatory. */
1688 if( !ps_list
[0] || !ps_list
[0]->head
|| ps_list
[0]->entry_count
== 0
1689 || !ps_list
[1] || !ps_list
[1]->head
|| ps_list
[1]->entry_count
== 0 )
1691 /* Calculate enough buffer size. */
1692 uint32_t buffer_size
= ISOM_BASEBOX_COMMON_SIZE
+ 11;
1693 for( int i
= 0; i
< 3; i
++ )
1695 for( lsmash_entry_t
*entry
= ps_list
[i
]->head
; entry
&& ps_count
[i
] < max_ps_count
[i
]; entry
= entry
->next
)
1697 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1702 buffer_size
+= 2 + ps
->nalUnitLength
;
1705 /* Set up bytestream writer. */
1706 uint8_t buffer
[buffer_size
];
1707 lsmash_bs_t bs
= { 0 };
1708 bs
.buffer
.data
= buffer
;
1709 bs
.buffer
.alloc
= buffer_size
;
1710 /* Create an AVCConfigurationBox */
1711 lsmash_bs_put_be32( &bs
, 0 ); /* box size */
1712 lsmash_bs_put_be32( &bs
, ISOM_BOX_TYPE_AVCC
.fourcc
); /* box type: 'avcC' */
1713 lsmash_bs_put_byte( &bs
, 1 ); /* configurationVersion */
1714 lsmash_bs_put_byte( &bs
, param
->AVCProfileIndication
); /* AVCProfileIndication */
1715 lsmash_bs_put_byte( &bs
, param
->profile_compatibility
); /* profile_compatibility */
1716 lsmash_bs_put_byte( &bs
, param
->AVCLevelIndication
); /* AVCLevelIndication */
1717 lsmash_bs_put_byte( &bs
, param
->lengthSizeMinusOne
| 0xfc ); /* lengthSizeMinusOne */
1718 lsmash_bs_put_byte( &bs
, ps_count
[0] | 0xe0 ); /* numOfSequenceParameterSets */
1719 h264_bs_put_parameter_sets( &bs
, ps_list
[0], ps_count
[0] ); /* sequenceParameterSetLength
1720 * sequenceParameterSetNALUnit */
1721 lsmash_bs_put_byte( &bs
, ps_count
[1] ); /* numOfPictureParameterSets */
1722 h264_bs_put_parameter_sets( &bs
, ps_list
[1], ps_count
[1] ); /* pictureParameterSetLength
1723 * pictureParameterSetNALUnit */
1724 if( H264_REQUIRES_AVCC_EXTENSION( param
->AVCProfileIndication
) )
1726 lsmash_bs_put_byte( &bs
, param
->chroma_format
| 0xfc ); /* chroma_format */
1727 lsmash_bs_put_byte( &bs
, param
->bit_depth_luma_minus8
| 0xf8 ); /* bit_depth_luma_minus8 */
1728 lsmash_bs_put_byte( &bs
, param
->bit_depth_chroma_minus8
| 0xf8 ); /* bit_depth_chroma_minus8 */
1731 lsmash_bs_put_byte( &bs
, ps_count
[2] ); /* numOfSequenceParameterSetExt */
1732 h264_bs_put_parameter_sets( &bs
, ps_list
[2], ps_count
[2] ); /* sequenceParameterSetExtLength
1733 * sequenceParameterSetExtNALUnit */
1735 else /* no sequence parameter set extensions */
1736 lsmash_bs_put_byte( &bs
, 0 ); /* numOfSequenceParameterSetExt */
1738 uint8_t *data
= lsmash_bs_export_data( &bs
, data_length
);
1739 /* Update box size. */
1740 LSMASH_SET_BE32( data
, *data_length
);
1744 static inline int h264_validate_ps_type
1746 lsmash_h264_parameter_set_type ps_type
,
1751 if( !ps_data
|| ps_length
< 2 )
1752 return LSMASH_ERR_INVALID_DATA
;
1753 if( ps_type
!= H264_PARAMETER_SET_TYPE_SPS
1754 && ps_type
!= H264_PARAMETER_SET_TYPE_PPS
1755 && ps_type
!= H264_PARAMETER_SET_TYPE_SPSEXT
)
1756 return LSMASH_ERR_INVALID_DATA
;
1757 uint8_t nalu_type
= *((uint8_t *)ps_data
) & 0x1f;
1758 if( nalu_type
!= H264_NALU_TYPE_SPS
1759 && nalu_type
!= H264_NALU_TYPE_PPS
1760 && nalu_type
!= H264_NALU_TYPE_SPS_EXT
)
1761 return LSMASH_ERR_INVALID_DATA
;
1762 if( (ps_type
== H264_PARAMETER_SET_TYPE_SPS
&& nalu_type
!= H264_NALU_TYPE_SPS
)
1763 || (ps_type
== H264_PARAMETER_SET_TYPE_PPS
&& nalu_type
!= H264_NALU_TYPE_PPS
)
1764 || (ps_type
== H264_PARAMETER_SET_TYPE_SPSEXT
&& nalu_type
!= H264_NALU_TYPE_SPS_EXT
) )
1765 return LSMASH_ERR_INVALID_DATA
;
1769 lsmash_dcr_nalu_appendable lsmash_check_h264_parameter_set_appendable
1771 lsmash_h264_specific_parameters_t
*param
,
1772 lsmash_h264_parameter_set_type ps_type
,
1777 uint8_t *ps_data
= _ps_data
;
1779 return DCR_NALU_APPEND_ERROR
;
1780 if( h264_validate_ps_type( ps_type
, ps_data
, ps_length
) < 0 )
1781 return DCR_NALU_APPEND_ERROR
;
1782 if( ps_type
== H264_PARAMETER_SET_TYPE_SPSEXT
1783 && !H264_REQUIRES_AVCC_EXTENSION( param
->AVCProfileIndication
) )
1784 return DCR_NALU_APPEND_ERROR
;
1785 /* Check whether the same parameter set already exsits or not. */
1786 lsmash_entry_list_t
*ps_list
= h264_get_parameter_set_list( param
, ps_type
);
1787 if( !ps_list
|| !ps_list
->head
)
1788 return DCR_NALU_APPEND_POSSIBLE
; /* No parameter set */
1789 switch( nalu_check_same_ps_existence( ps_list
, ps_data
, ps_length
) )
1792 case 1 : return DCR_NALU_APPEND_DUPLICATED
; /* The same parameter set already exists. */
1793 default : return DCR_NALU_APPEND_ERROR
; /* An error occured. */
1795 uint32_t max_ps_length
;
1796 if( nalu_get_max_ps_length( ps_list
, &max_ps_length
) < 0 )
1797 return DCR_NALU_APPEND_ERROR
;
1798 max_ps_length
= LSMASH_MAX( max_ps_length
, ps_length
);
1800 if( nalu_get_ps_count( ps_list
, &ps_count
) )
1801 return DCR_NALU_APPEND_ERROR
;
1802 if( (ps_type
== H264_PARAMETER_SET_TYPE_SPS
&& ps_count
>= 31)
1803 || (ps_type
== H264_PARAMETER_SET_TYPE_PPS
&& ps_count
>= 255)
1804 || (ps_type
== H264_PARAMETER_SET_TYPE_SPSEXT
&& ps_count
>= 255) )
1805 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
; /* No more appendable parameter sets. */
1806 if( ps_type
== H264_PARAMETER_SET_TYPE_SPSEXT
)
1807 return DCR_NALU_APPEND_POSSIBLE
;
1808 /* Check whether a new specific info is needed or not. */
1809 lsmash_bits_t bits
= { 0 };
1810 lsmash_bs_t bs
= { 0 };
1811 uint8_t rbsp_buffer
[max_ps_length
];
1812 uint8_t buffer
[max_ps_length
];
1813 bs
.buffer
.data
= buffer
;
1814 bs
.buffer
.alloc
= max_ps_length
;
1815 lsmash_bits_init( &bits
, &bs
);
1816 if( ps_type
== H264_PARAMETER_SET_TYPE_PPS
)
1820 if( h264_get_pps_id( ps_data
+ 1, ps_length
- 1, &pps_id
) < 0 )
1821 return DCR_NALU_APPEND_ERROR
;
1822 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
; entry
= entry
->next
)
1824 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1826 return DCR_NALU_APPEND_ERROR
;
1829 uint8_t param_pps_id
;
1830 if( h264_get_pps_id( ps
->nalUnit
+ 1, ps
->nalUnitLength
- 1, ¶m_pps_id
) < 0 )
1831 return DCR_NALU_APPEND_ERROR
;
1832 if( pps_id
== param_pps_id
)
1833 /* PPS that has the same pic_parameter_set_id already exists with different form. */
1834 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
1836 return DCR_NALU_APPEND_POSSIBLE
;
1840 if( h264_parse_sps_minimally( &bits
, &sps
, rbsp_buffer
, ps_data
+ 1, ps_length
- 1 ) < 0 )
1841 return DCR_NALU_APPEND_ERROR
;
1842 lsmash_bits_empty( &bits
);
1843 /* FIXME; If the sequence parameter sets are marked with different profiles,
1844 * and the relevant profile compatibility flags are all zero,
1845 * then the stream may need examination to determine which profile, if any, the stream conforms to.
1846 * If the stream is not examined, or the examination reveals that there is no profile to which the stream conforms,
1847 * then the stream must be split into two or more sub-streams with separate configuration records in which these rules can be met. */
1849 if( sps
.profile_idc
!= param
->AVCProfileIndication
&& (sps
->constraint_set_flags
& param
->profile_compatibility
) )
1851 if( sps
.profile_idc
!= param
->AVCProfileIndication
)
1853 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
1854 /* The values of chroma_format_idc, bit_depth_luma_minus8 and bit_depth_chroma_minus8
1855 * must be identical in all SPSs in a single AVC configuration record. */
1856 if( H264_REQUIRES_AVCC_EXTENSION( param
->AVCProfileIndication
)
1857 && (sps
.chroma_format_idc
!= param
->chroma_format
1858 || sps
.bit_depth_luma_minus8
!= param
->bit_depth_luma_minus8
1859 || sps
.bit_depth_chroma_minus8
!= param
->bit_depth_chroma_minus8
) )
1860 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
1861 /* Forbidden to duplicate SPS that has the same seq_parameter_set_id with different form within the same configuration record. */
1862 uint8_t sps_id
= sps
.seq_parameter_set_id
;
1863 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
; entry
= entry
->next
)
1865 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1867 return DCR_NALU_APPEND_ERROR
;
1870 uint8_t param_sps_id
;
1871 if( h264_get_sps_id( ps
->nalUnit
+ 1, ps
->nalUnitLength
- 1, ¶m_sps_id
) < 0 )
1872 return DCR_NALU_APPEND_ERROR
;
1873 if( sps_id
== param_sps_id
)
1874 /* SPS that has the same seq_parameter_set_id already exists with different form. */
1875 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
1876 if( entry
== ps_list
->head
)
1878 /* Check if the visual presentation sizes are different. */
1879 h264_sps_t first_sps
;
1880 if( h264_parse_sps_minimally( &bits
, &first_sps
, rbsp_buffer
,
1882 ps
->nalUnitLength
- 1 ) < 0 )
1883 return DCR_NALU_APPEND_ERROR
;
1884 if( sps
.cropped_width
!= first_sps
.cropped_width
1885 || sps
.cropped_height
!= first_sps
.cropped_height
)
1886 return DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED
;
1889 return DCR_NALU_APPEND_POSSIBLE
;
1892 static inline void h264_reorder_parameter_set_ascending_id
1894 lsmash_h264_specific_parameters_t
*param
,
1895 lsmash_h264_parameter_set_type ps_type
,
1896 lsmash_entry_list_t
*ps_list
,
1900 lsmash_entry_t
*entry
= NULL
;
1902 for( int i
= ps_id
- 1; i
; i
-- )
1904 entry
= h264_get_ps_entry_from_param( param
, ps_type
, i
);
1908 int append_head
= 0;
1911 /* Couldn't find any parameter set with lower identifier.
1912 * Next, find parameter set with upper identifier. */
1913 int max_ps_id
= ps_type
== H264_PARAMETER_SET_TYPE_SPS
? 31 : 255;
1914 for( int i
= ps_id
+ 1; i
<= max_ps_id
; i
++ )
1916 entry
= h264_get_ps_entry_from_param( param
, ps_type
, i
);
1924 return; /* The new entry was appended to the tail. */
1925 lsmash_entry_t
*new_entry
= ps_list
->tail
;
1928 /* before: entry[i > ps_id] ... -> prev_entry -> new_entry[ps_id]
1929 * after: new_entry[ps_id] -> entry[i > ps_id] -> ... -> prev_entry */
1930 if( new_entry
->prev
)
1931 new_entry
->prev
->next
= NULL
;
1932 new_entry
->prev
= NULL
;
1933 entry
->prev
= new_entry
;
1934 new_entry
->next
= entry
;
1937 /* before: entry[i < ps_id] -> next_entry -> ... -> prev_entry -> new_entry[ps_id]
1938 * after: entry[i < ps_id] -> new_entry[ps_id] -> next_entry -> ... -> prev_entry */
1939 if( new_entry
->prev
)
1940 new_entry
->prev
->next
= NULL
;
1941 new_entry
->prev
= entry
;
1942 new_entry
->next
= entry
->next
;
1944 entry
->next
->prev
= new_entry
;
1945 entry
->next
= new_entry
;
1948 int lsmash_append_h264_parameter_set
1950 lsmash_h264_specific_parameters_t
*param
,
1951 lsmash_h264_parameter_set_type ps_type
,
1956 uint8_t *ps_data
= _ps_data
;
1957 if( !param
|| !ps_data
|| ps_length
< 2 )
1958 return LSMASH_ERR_FUNCTION_PARAM
;
1959 if( ps_type
!= H264_PARAMETER_SET_TYPE_SPS
1960 && ps_type
!= H264_PARAMETER_SET_TYPE_PPS
1961 && ps_type
!= H264_PARAMETER_SET_TYPE_SPSEXT
)
1962 return LSMASH_ERR_FUNCTION_PARAM
;
1963 if( !param
->parameter_sets
)
1965 param
->parameter_sets
= lsmash_malloc_zero( sizeof(lsmash_h264_parameter_sets_t
) );
1966 if( !param
->parameter_sets
)
1967 return LSMASH_ERR_MEMORY_ALLOC
;
1969 lsmash_entry_list_t
*ps_list
= h264_get_parameter_set_list( param
, ps_type
);
1971 return LSMASH_ERR_NAMELESS
;
1972 if( ps_type
== H264_PARAMETER_SET_TYPE_SPSEXT
)
1974 if( !H264_REQUIRES_AVCC_EXTENSION( param
->AVCProfileIndication
) )
1976 isom_dcr_ps_entry_t
*ps
= isom_create_ps_entry( ps_data
, ps_length
);
1978 return LSMASH_ERR_MEMORY_ALLOC
;
1979 if( lsmash_add_entry( ps_list
, ps
) < 0 )
1981 isom_remove_dcr_ps( ps
);
1982 return LSMASH_ERR_MEMORY_ALLOC
;
1986 /* Check if the same parameter set identifier already exists. */
1988 int err
= h264_get_ps_id( ps_data
+ 1, ps_length
- 1, &ps_id
, ps_type
);
1991 lsmash_entry_t
*entry
= h264_get_ps_entry_from_param( param
, ps_type
, ps_id
);
1992 isom_dcr_ps_entry_t
*ps
= entry
? (isom_dcr_ps_entry_t
*)entry
->data
: NULL
;
1993 if( ps
&& !ps
->unused
)
1994 /* The same parameter set identifier already exists. */
1995 return LSMASH_ERR_FUNCTION_PARAM
;
1999 /* Reuse an already existed parameter set in the list. */
2001 if( ps
->nalUnit
!= ps_data
)
2003 /* The same address could be given when called by h264_update_picture_info_for_slice(). */
2004 lsmash_free( ps
->nalUnit
);
2005 ps
->nalUnit
= ps_data
;
2007 ps
->nalUnitLength
= ps_length
;
2012 /* Create a new parameter set and append it into the list. */
2013 ps
= isom_create_ps_entry( ps_data
, ps_length
);
2015 return LSMASH_ERR_MEMORY_ALLOC
;
2016 if( lsmash_add_entry( ps_list
, ps
) < 0 )
2018 isom_remove_dcr_ps( ps
);
2019 return LSMASH_ERR_MEMORY_ALLOC
;
2023 if( ps_type
== H264_PARAMETER_SET_TYPE_SPS
)
2025 /* Update specific info with SPS. */
2026 lsmash_bits_t bits
= { 0 };
2027 lsmash_bs_t bs
= { 0 };
2028 uint8_t rbsp_buffer
[ps_length
];
2029 uint8_t buffer
[ps_length
];
2030 bs
.buffer
.data
= buffer
;
2031 bs
.buffer
.alloc
= ps_length
;
2032 lsmash_bits_init( &bits
, &bs
);
2034 if( (err
= h264_parse_sps_minimally( &bits
, &sps
, rbsp_buffer
, ps_data
+ 1, ps_length
- 1 )) < 0 )
2036 lsmash_remove_entry_tail( ps_list
, isom_remove_dcr_ps
);
2039 if( ps_list
->entry_count
== 1 )
2040 param
->profile_compatibility
= 0xff;
2041 param
->AVCProfileIndication
= sps
.profile_idc
;
2042 param
->profile_compatibility
&= sps
.constraint_set_flags
;
2043 param
->AVCLevelIndication
= LSMASH_MAX( param
->AVCLevelIndication
, sps
.level_idc
);
2044 param
->chroma_format
= sps
.chroma_format_idc
;
2045 param
->bit_depth_luma_minus8
= sps
.bit_depth_luma_minus8
;
2046 param
->bit_depth_chroma_minus8
= sps
.bit_depth_chroma_minus8
;
2048 if( invoke_reorder
)
2049 /* Add a new parameter set in order of ascending parameter set identifier. */
2050 h264_reorder_parameter_set_ascending_id( param
, ps_type
, ps_list
, ps_id
);
2054 int h264_try_to_append_parameter_set
2057 lsmash_h264_parameter_set_type ps_type
,
2062 uint8_t *ps_data
= _ps_data
;
2063 lsmash_dcr_nalu_appendable ret
= lsmash_check_h264_parameter_set_appendable( &info
->avcC_param
, ps_type
, ps_data
, ps_length
);
2064 lsmash_h264_specific_parameters_t
*param
;
2067 case DCR_NALU_APPEND_ERROR
: /* Error */
2068 return LSMASH_ERR_NAMELESS
;
2069 case DCR_NALU_APPEND_NEW_DCR_REQUIRED
: /* Mulitiple sample description is needed. */
2070 case DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED
: /* Mulitiple sample description is needed. */
2071 param
= &info
->avcC_param_next
;
2072 info
->avcC_pending
= 1;
2074 case DCR_NALU_APPEND_POSSIBLE
: /* Appendable */
2075 param
= info
->avcC_pending
? &info
->avcC_param_next
: &info
->avcC_param
;
2077 default : /* No need to append */
2083 case H264_PARAMETER_SET_TYPE_SPS
:
2084 if( (err
= h264_parse_sps( info
, info
->buffer
.rbsp
, ps_data
+ 1, ps_length
- 1 )) < 0 )
2087 case H264_PARAMETER_SET_TYPE_PPS
:
2088 if( (err
= h264_parse_pps( info
, info
->buffer
.rbsp
, ps_data
+ 1, ps_length
- 1 )) < 0 )
2094 return lsmash_append_h264_parameter_set( param
, ps_type
, ps_data
, ps_length
);
2097 static inline int h264_move_dcr_nalu_entry
2099 lsmash_h264_specific_parameters_t
*dst_data
,
2100 lsmash_h264_specific_parameters_t
*src_data
,
2101 lsmash_h264_parameter_set_type ps_type
2104 lsmash_entry_list_t
*src_ps_list
= h264_get_parameter_set_list( src_data
, ps_type
);
2105 lsmash_entry_list_t
*dst_ps_list
= h264_get_parameter_set_list( dst_data
, ps_type
);
2106 assert( src_ps_list
&& dst_ps_list
);
2107 for( lsmash_entry_t
*src_entry
= src_ps_list
->head
; src_entry
; src_entry
= src_entry
->next
)
2109 isom_dcr_ps_entry_t
*src_ps
= (isom_dcr_ps_entry_t
*)src_entry
->data
;
2114 if( (err
= h264_get_ps_id( src_ps
->nalUnit
+ 1, src_ps
->nalUnitLength
- 1, &src_ps_id
, ps_type
)) < 0 )
2116 lsmash_entry_t
*dst_entry
;
2117 for( dst_entry
= dst_ps_list
->head
; dst_entry
; dst_entry
= dst_entry
->next
)
2119 isom_dcr_ps_entry_t
*dst_ps
= (isom_dcr_ps_entry_t
*)dst_entry
->data
;
2123 if( (err
= h264_get_ps_id( dst_ps
->nalUnit
+ 1, dst_ps
->nalUnitLength
- 1, &dst_ps_id
, ps_type
)) < 0 )
2125 if( dst_ps_id
== src_ps_id
)
2127 /* Replace the old parameter set with the new one. */
2128 assert( dst_entry
->data
!= src_entry
->data
);
2129 isom_remove_dcr_ps( dst_ps
);
2130 dst_entry
->data
= src_entry
->data
;
2131 src_entry
->data
= NULL
;
2137 /* Move the parameter set. */
2138 if( lsmash_add_entry( dst_ps_list
, src_ps
) < 0 )
2139 return LSMASH_ERR_MEMORY_ALLOC
;
2140 src_entry
->data
= NULL
;
2146 int h264_move_pending_avcC_param
2152 if( !info
->avcC_pending
)
2154 /* Mark 'unused' on parameter sets within the decoder configuration record. */
2155 for( int i
= 0; i
< H264_PARAMETER_SET_TYPE_NUM
; i
++ )
2157 lsmash_entry_list_t
*ps_list
= h264_get_parameter_set_list( &info
->avcC_param
, i
);
2159 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
; entry
= entry
->next
)
2161 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
2167 /* Move the new parameter sets. */
2169 if( (err
= h264_move_dcr_nalu_entry( &info
->avcC_param
, &info
->avcC_param_next
, H264_PARAMETER_SET_TYPE_SPS
)) < 0
2170 || (err
= h264_move_dcr_nalu_entry( &info
->avcC_param
, &info
->avcC_param_next
, H264_PARAMETER_SET_TYPE_PPS
)) < 0 )
2172 /* Move to the pending. */
2173 lsmash_h264_parameter_sets_t
*parameter_sets
= info
->avcC_param
.parameter_sets
; /* Back up parameter sets. */
2174 info
->avcC_param
= info
->avcC_param_next
;
2175 info
->avcC_param
.parameter_sets
= parameter_sets
;
2176 /* No pending avcC. */
2177 lsmash_destroy_h264_parameter_sets( &info
->avcC_param_next
);
2178 memset( &info
->avcC_param_next
, 0, sizeof(lsmash_h264_specific_parameters_t
) );
2179 info
->avcC_pending
= 0;
2183 static int h264_parse_succeeded
2186 lsmash_h264_specific_parameters_t
*param
2190 if( info
->sps
.present
&& info
->pps
.present
)
2192 *param
= info
->avcC_param
;
2193 /* Avoid freeing parameter sets. */
2194 info
->avcC_param
.parameter_sets
= NULL
;
2198 ret
= LSMASH_ERR_INVALID_DATA
;
2199 h264_cleanup_parser( info
);
2203 static inline int h264_parse_failed
2209 h264_cleanup_parser( info
);
2213 int lsmash_setup_h264_specific_parameters_from_access_unit
2215 lsmash_h264_specific_parameters_t
*param
,
2217 uint32_t data_length
2220 if( !param
|| !data
|| data_length
== 0 )
2221 return LSMASH_ERR_FUNCTION_PARAM
;
2222 h264_info_t
*info
= &(h264_info_t
){ { 0 } };
2223 lsmash_bs_t
*bs
= &(lsmash_bs_t
){ 0 };
2224 int err
= lsmash_bs_set_empty_stream( bs
, data
, data_length
);
2227 uint64_t sc_head_pos
= nalu_find_first_start_code( bs
);
2228 if( sc_head_pos
== NALU_NO_START_CODE_FOUND
)
2229 return LSMASH_ERR_INVALID_DATA
;
2230 if( (err
= h264_setup_parser( info
, 1 )) < 0 )
2231 return h264_parse_failed( info
, err
);
2232 h264_stream_buffer_t
*sb
= &info
->buffer
;
2233 h264_slice_info_t
*slice
= &info
->slice
;
2236 h264_nalu_header_t nuh
;
2237 uint64_t start_code_length
;
2238 uint64_t trailing_zero_bytes
;
2239 uint64_t nalu_length
= h264_find_next_start_code( bs
, &nuh
, &start_code_length
, &trailing_zero_bytes
);
2240 if( start_code_length
<= NALU_SHORT_START_CODE_LENGTH
&& lsmash_bs_is_end( bs
, nalu_length
) )
2241 /* For the last NALU. This NALU already has been parsed. */
2242 return h264_parse_succeeded( info
, param
);
2243 uint8_t nalu_type
= nuh
.nal_unit_type
;
2244 uint64_t next_sc_head_pos
= sc_head_pos
2247 + trailing_zero_bytes
;
2248 if( nalu_type
== H264_NALU_TYPE_FD
)
2250 /* We don't support streams with both filler and HRD yet.
2251 * Otherwise, just skip filler because elemental streams defined in 14496-15 are forbidden to use filler. */
2252 if( info
->sps
.vui
.hrd
.present
)
2253 return h264_parse_failed( info
, LSMASH_ERR_PATCH_WELCOME
);
2255 else if( (nalu_type
>= H264_NALU_TYPE_SLICE_N_IDR
&& nalu_type
<= H264_NALU_TYPE_SPS_EXT
)
2256 || nalu_type
== H264_NALU_TYPE_SLICE_AUX
)
2258 /* Increase the buffer if needed. */
2259 uint64_t possible_au_length
= NALU_DEFAULT_NALU_LENGTH_SIZE
+ nalu_length
;
2260 if( sb
->bank
->buffer_size
< possible_au_length
2261 && (err
= h264_supplement_buffer( sb
, NULL
, 2 * possible_au_length
)) < 0 )
2262 return h264_parse_failed( info
, err
);
2263 /* Get the EBSP of the current NALU here.
2264 * AVC elemental stream defined in 14496-15 can recognize from 0 to 13, and 19 of nal_unit_type.
2265 * We don't support SVC and MVC elemental stream defined in 14496-15 yet. */
2266 uint8_t *nalu
= lsmash_bs_get_buffer_data( bs
) + start_code_length
;
2267 if( nalu_type
>= H264_NALU_TYPE_SLICE_N_IDR
&& nalu_type
<= H264_NALU_TYPE_SLICE_IDR
)
2269 /* VCL NALU (slice) */
2270 h264_slice_info_t prev_slice
= *slice
;
2271 if( (err
= h264_parse_slice( info
, &nuh
, sb
->rbsp
, nalu
+ nuh
.length
, nalu_length
- nuh
.length
)) < 0 )
2272 return h264_parse_failed( info
, err
);
2273 if( prev_slice
.present
)
2275 /* Check whether the AU that contains the previous VCL NALU completed or not. */
2276 if( h264_find_au_delimit_by_slice_info( slice
, &prev_slice
) )
2277 /* The current NALU is the first VCL NALU of the primary coded picture of an new AU.
2278 * Therefore, the previous slice belongs to that new AU. */
2279 return h264_parse_succeeded( info
, param
);
2285 if( h264_find_au_delimit_by_nalu_type( nalu_type
, info
->prev_nalu_type
) )
2286 /* The last slice belongs to the AU you want at this time. */
2287 return h264_parse_succeeded( info
, param
);
2290 case H264_NALU_TYPE_SPS
:
2291 if( (err
= h264_try_to_append_parameter_set( info
, H264_PARAMETER_SET_TYPE_SPS
, nalu
, nalu_length
)) < 0 )
2292 return h264_parse_failed( info
, err
);
2294 case H264_NALU_TYPE_PPS
:
2295 if( (err
= h264_try_to_append_parameter_set( info
, H264_PARAMETER_SET_TYPE_PPS
, nalu
, nalu_length
)) < 0 )
2296 return h264_parse_failed( info
, err
);
2298 case H264_NALU_TYPE_SPS_EXT
:
2299 if( (err
= h264_try_to_append_parameter_set( info
, H264_PARAMETER_SET_TYPE_SPSEXT
, nalu
, nalu_length
)) < 0 )
2300 return h264_parse_failed( info
, err
);
2307 /* Move to the first byte of the next start code. */
2308 info
->prev_nalu_type
= nalu_type
;
2309 if( lsmash_bs_read_seek( bs
, next_sc_head_pos
, SEEK_SET
) != next_sc_head_pos
)
2310 return h264_parse_failed( info
, LSMASH_ERR_NAMELESS
);
2311 /* Check if no more data to read from the stream. */
2312 if( !lsmash_bs_is_end( bs
, NALU_SHORT_START_CODE_LENGTH
) )
2313 sc_head_pos
= next_sc_head_pos
;
2315 return h264_parse_succeeded( info
, param
);
2319 int h264_construct_specific_parameters
2321 lsmash_codec_specific_t
*dst
,
2322 lsmash_codec_specific_t
*src
2325 assert( dst
&& dst
->data
.structured
&& src
&& src
->data
.unstructured
);
2326 if( src
->size
< ISOM_BASEBOX_COMMON_SIZE
+ 7 )
2327 return LSMASH_ERR_INVALID_DATA
;
2328 lsmash_h264_specific_parameters_t
*param
= (lsmash_h264_specific_parameters_t
*)dst
->data
.structured
;
2329 uint8_t *data
= src
->data
.unstructured
;
2330 uint64_t size
= LSMASH_GET_BE32( data
);
2331 data
+= ISOM_BASEBOX_COMMON_SIZE
;
2334 size
= LSMASH_GET_BE64( data
);
2337 if( size
!= src
->size
)
2338 return LSMASH_ERR_INVALID_DATA
;
2339 if( !param
->parameter_sets
)
2341 param
->parameter_sets
= lsmash_malloc_zero( sizeof(lsmash_h264_parameter_sets_t
) );
2342 if( !param
->parameter_sets
)
2343 return LSMASH_ERR_MEMORY_ALLOC
;
2345 lsmash_bs_t
*bs
= lsmash_bs_create();
2347 return LSMASH_ERR_MEMORY_ALLOC
;
2348 int err
= lsmash_bs_import_data( bs
, data
, src
->size
- (data
- src
->data
.unstructured
) );
2351 if( lsmash_bs_get_byte( bs
) != 1 )
2353 /* We don't support configurationVersion other than 1. */
2354 err
= LSMASH_ERR_INVALID_DATA
;
2357 param
->AVCProfileIndication
= lsmash_bs_get_byte( bs
);
2358 param
->profile_compatibility
= lsmash_bs_get_byte( bs
);
2359 param
->AVCLevelIndication
= lsmash_bs_get_byte( bs
);
2360 param
->lengthSizeMinusOne
= lsmash_bs_get_byte( bs
) & 0x03;
2361 uint8_t numOfSequenceParameterSets
= lsmash_bs_get_byte( bs
) & 0x1F;
2362 if( numOfSequenceParameterSets
2363 && (err
= nalu_get_dcr_ps( bs
, param
->parameter_sets
->sps_list
, numOfSequenceParameterSets
)) < 0 )
2365 uint8_t numOfPictureParameterSets
= lsmash_bs_get_byte( bs
);
2366 if( numOfPictureParameterSets
2367 && (err
= nalu_get_dcr_ps( bs
, param
->parameter_sets
->pps_list
, numOfPictureParameterSets
)) < 0 )
2369 if( H264_REQUIRES_AVCC_EXTENSION( param
->AVCProfileIndication
) )
2371 param
->chroma_format
= lsmash_bs_get_byte( bs
) & 0x03;
2372 param
->bit_depth_luma_minus8
= lsmash_bs_get_byte( bs
) & 0x07;
2373 param
->bit_depth_chroma_minus8
= lsmash_bs_get_byte( bs
) & 0x07;
2374 uint8_t numOfSequenceParameterSetExt
= lsmash_bs_get_byte( bs
);
2375 if( numOfSequenceParameterSetExt
2376 && (err
= nalu_get_dcr_ps( bs
, param
->parameter_sets
->spsext_list
, numOfSequenceParameterSetExt
)) < 0 )
2379 lsmash_bs_cleanup( bs
);
2382 lsmash_bs_cleanup( bs
);
2386 int h264_print_codec_specific
2389 lsmash_file_t
*file
,
2394 assert( fp
&& file
&& box
&& (box
->manager
& LSMASH_BINARY_CODED_BOX
) );
2396 lsmash_ifprintf( fp
, indent
++, "[%s: AVC Configuration Box]\n", isom_4cc2str( box
->type
.fourcc
) );
2397 lsmash_ifprintf( fp
, indent
, "position = %"PRIu64
"\n", box
->pos
);
2398 lsmash_ifprintf( fp
, indent
, "size = %"PRIu64
"\n", box
->size
);
2399 uint8_t *data
= box
->binary
;
2400 uint32_t offset
= isom_skip_box_common( &data
);
2401 lsmash_bs_t
*bs
= lsmash_bs_create();
2403 return LSMASH_ERR_MEMORY_ALLOC
;
2404 int err
= lsmash_bs_import_data( bs
, data
, box
->size
- offset
);
2407 lsmash_bs_cleanup( bs
);
2410 lsmash_ifprintf( fp
, indent
, "configurationVersion = %"PRIu8
"\n", lsmash_bs_get_byte( bs
) );
2411 uint8_t AVCProfileIndication
= lsmash_bs_get_byte( bs
);
2412 lsmash_ifprintf( fp
, indent
, "AVCProfileIndication = %"PRIu8
"\n", AVCProfileIndication
);
2413 lsmash_ifprintf( fp
, indent
, "profile_compatibility = 0x%02"PRIx8
"\n", lsmash_bs_get_byte( bs
) );
2414 lsmash_ifprintf( fp
, indent
, "AVCLevelIndication = %"PRIu8
"\n", lsmash_bs_get_byte( bs
) );
2415 uint8_t temp8
= lsmash_bs_get_byte( bs
);
2416 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 2) & 0x3F );
2417 lsmash_ifprintf( fp
, indent
, "lengthSizeMinusOne = %"PRIu8
"\n", temp8
& 0x03 );
2418 temp8
= lsmash_bs_get_byte( bs
);
2419 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 5) & 0x07 );
2420 uint8_t numOfSequenceParameterSets
= temp8
& 0x1f;
2421 lsmash_ifprintf( fp
, indent
, "numOfSequenceParameterSets = %"PRIu8
"\n", numOfSequenceParameterSets
);
2422 for( uint8_t i
= 0; i
< numOfSequenceParameterSets
; i
++ )
2424 uint16_t nalUnitLength
= lsmash_bs_get_be16( bs
);
2425 lsmash_bs_skip_bytes( bs
, nalUnitLength
);
2427 uint8_t numOfPictureParameterSets
= lsmash_bs_get_byte( bs
);
2428 lsmash_ifprintf( fp
, indent
, "numOfPictureParameterSets = %"PRIu8
"\n", numOfPictureParameterSets
);
2429 for( uint8_t i
= 0; i
< numOfPictureParameterSets
; i
++ )
2431 uint16_t nalUnitLength
= lsmash_bs_get_be16( bs
);
2432 lsmash_bs_skip_bytes( bs
, nalUnitLength
);
2434 /* Note: there are too many files, in the world, that don't contain the following fields. */
2435 if( H264_REQUIRES_AVCC_EXTENSION( AVCProfileIndication
)
2436 && (lsmash_bs_get_pos( bs
) < (box
->size
- offset
)) )
2438 temp8
= lsmash_bs_get_byte( bs
);
2439 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 2) & 0x3F );
2440 lsmash_ifprintf( fp
, indent
, "chroma_format = %"PRIu8
"\n", temp8
& 0x03 );
2441 temp8
= lsmash_bs_get_byte( bs
);
2442 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 3) & 0x1F );
2443 lsmash_ifprintf( fp
, indent
, "bit_depth_luma_minus8 = %"PRIu8
"\n", temp8
& 0x7 );
2444 temp8
= lsmash_bs_get_byte( bs
);
2445 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 3) & 0x1F );
2446 lsmash_ifprintf( fp
, indent
, "bit_depth_chroma_minus8 = %"PRIu8
"\n", temp8
& 0x7 );
2447 lsmash_ifprintf( fp
, indent
, "numOfSequenceParameterSetExt = %"PRIu8
"\n", lsmash_bs_get_byte( bs
) );
2449 lsmash_bs_cleanup( bs
);
2453 int h264_copy_codec_specific
2455 lsmash_codec_specific_t
*dst
,
2456 lsmash_codec_specific_t
*src
2459 assert( src
&& src
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
&& src
->data
.structured
);
2460 assert( dst
&& dst
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
&& dst
->data
.structured
);
2461 lsmash_h264_specific_parameters_t
*src_data
= (lsmash_h264_specific_parameters_t
*)src
->data
.structured
;
2462 lsmash_h264_specific_parameters_t
*dst_data
= (lsmash_h264_specific_parameters_t
*)dst
->data
.structured
;
2463 lsmash_destroy_h264_parameter_sets( dst_data
);
2464 *dst_data
= *src_data
;
2465 if( !src_data
->parameter_sets
)
2467 dst_data
->parameter_sets
= lsmash_malloc_zero( sizeof(lsmash_h264_parameter_sets_t
) );
2468 if( !dst_data
->parameter_sets
)
2469 return LSMASH_ERR_MEMORY_ALLOC
;
2470 for( int i
= 0; i
< 3; i
++ )
2472 lsmash_entry_list_t
*src_ps_list
= h264_get_parameter_set_list( src_data
, i
);
2473 lsmash_entry_list_t
*dst_ps_list
= h264_get_parameter_set_list( dst_data
, i
);
2474 assert( src_ps_list
&& dst_ps_list
);
2475 for( lsmash_entry_t
*entry
= src_ps_list
->head
; entry
; entry
= entry
->next
)
2477 isom_dcr_ps_entry_t
*src_ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
2478 if( !src_ps
|| src_ps
->unused
)
2480 isom_dcr_ps_entry_t
*dst_ps
= isom_create_ps_entry( src_ps
->nalUnit
, src_ps
->nalUnitLength
);
2483 lsmash_destroy_h264_parameter_sets( dst_data
);
2484 return LSMASH_ERR_MEMORY_ALLOC
;
2486 if( lsmash_add_entry( dst_ps_list
, dst_ps
) < 0 )
2488 lsmash_destroy_h264_parameter_sets( dst_data
);
2489 isom_remove_dcr_ps( dst_ps
);
2490 return LSMASH_ERR_MEMORY_ALLOC
;
2497 int h264_print_bitrate
2500 lsmash_file_t
*file
,
2505 assert( fp
&& file
&& box
);
2507 lsmash_ifprintf( fp
, indent
++, "[%s: MPEG-4 Bit Rate Box]\n", isom_4cc2str( box
->type
.fourcc
) );
2508 lsmash_ifprintf( fp
, indent
, "position = %"PRIu64
"\n", box
->pos
);
2509 lsmash_ifprintf( fp
, indent
, "size = %"PRIu64
"\n", box
->size
);
2510 isom_btrt_t
*btrt
= (isom_btrt_t
*)box
;
2511 lsmash_ifprintf( fp
, indent
, "bufferSizeDB = %"PRIu32
"\n", btrt
->bufferSizeDB
);
2512 lsmash_ifprintf( fp
, indent
, "maxBitrate = %"PRIu32
"\n", btrt
->maxBitrate
);
2513 lsmash_ifprintf( fp
, indent
, "avgBitrate = %"PRIu32
"\n", btrt
->avgBitrate
);