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_INVALID_VALUE( x ) if( x )
39 #define IF_EXCEED_INT32( x ) if( (x) < INT32_MIN || (x) > INT32_MAX )
40 #define H264_REQUIRES_AVCC_EXTENSION( x ) ((x) == 100 || (x) == 110 || (x) == 122 || (x) == 144)
41 #define H264_POC_DEBUG_PRINT 0
45 H264_SLICE_TYPE_P
= 0,
46 H264_SLICE_TYPE_B
= 1,
47 H264_SLICE_TYPE_I
= 2,
48 H264_SLICE_TYPE_SP
= 3,
49 H264_SLICE_TYPE_SI
= 4
52 void lsmash_destroy_h264_parameter_sets
54 lsmash_h264_specific_parameters_t
*param
57 if( !param
|| !param
->parameter_sets
)
59 lsmash_remove_entries( param
->parameter_sets
->sps_list
, isom_remove_dcr_ps
);
60 lsmash_remove_entries( param
->parameter_sets
->pps_list
, isom_remove_dcr_ps
);
61 lsmash_remove_entries( param
->parameter_sets
->spsext_list
, isom_remove_dcr_ps
);
62 lsmash_free( param
->parameter_sets
);
63 param
->parameter_sets
= NULL
;
66 void h264_destruct_specific_data
73 lsmash_destroy_h264_parameter_sets( data
);
77 void h264_cleanup_parser
84 lsmash_remove_entries( info
->sps_list
, NULL
);
85 lsmash_remove_entries( info
->pps_list
, NULL
);
86 lsmash_remove_entries( info
->slice_list
, NULL
);
87 lsmash_destroy_h264_parameter_sets( &info
->avcC_param
);
88 lsmash_destroy_h264_parameter_sets( &info
->avcC_param_next
);
89 lsmash_destroy_multiple_buffers( info
->buffer
.bank
);
90 lsmash_bits_adhoc_cleanup( info
->bits
);
102 memset( info
, 0, sizeof(h264_info_t
) );
103 info
->avcC_param
.lengthSizeMinusOne
= NALU_DEFAULT_NALU_LENGTH_SIZE
- 1;
104 info
->avcC_param_next
.lengthSizeMinusOne
= NALU_DEFAULT_NALU_LENGTH_SIZE
- 1;
105 h264_stream_buffer_t
*sb
= &info
->buffer
;
106 sb
->bank
= lsmash_create_multiple_buffers( parse_only
? 1 : 3, NALU_DEFAULT_BUFFER_SIZE
);
109 sb
->rbsp
= lsmash_withdraw_buffer( sb
->bank
, 1 );
112 info
->au
.data
= lsmash_withdraw_buffer( sb
->bank
, 2 );
113 info
->au
.incomplete_data
= lsmash_withdraw_buffer( sb
->bank
, 3 );
115 info
->bits
= lsmash_bits_adhoc_create();
118 lsmash_destroy_multiple_buffers( sb
->bank
);
121 lsmash_init_entry_list( info
->sps_list
);
122 lsmash_init_entry_list( info
->pps_list
);
123 lsmash_init_entry_list( info
->slice_list
);
127 static int h264_check_nalu_header
130 h264_nalu_header_t
*nuh
,
131 int use_long_start_code
134 uint8_t temp8
= lsmash_bs_show_byte( bs
, use_long_start_code
? NALU_LONG_START_CODE_LENGTH
: NALU_SHORT_START_CODE_LENGTH
);
135 nuh
->forbidden_zero_bit
= (temp8
>> 7) & 0x01;
136 nuh
->nal_ref_idc
= (temp8
>> 5) & 0x03;
137 nuh
->nal_unit_type
= temp8
& 0x1f;
139 if( nuh
->nal_unit_type
== H264_NALU_TYPE_PREFIX
140 || nuh
->nal_unit_type
== H264_NALU_TYPE_SLICE_EXT
141 || nuh
->nal_unit_type
== H264_NALU_TYPE_SLICE_EXT_DVC
)
143 /* We don't support these types of NALU. */
147 IF_INVALID_VALUE( nuh
->forbidden_zero_bit
)
149 /* SPS and PPS require long start code (0x00000001).
150 * Also AU delimiter requires it too because this type of NALU shall be the first NALU of any AU if present. */
151 IF_INVALID_VALUE( !use_long_start_code
152 && (nuh
->nal_unit_type
== H264_NALU_TYPE_SPS
153 || nuh
->nal_unit_type
== H264_NALU_TYPE_PPS
154 || nuh
->nal_unit_type
== H264_NALU_TYPE_AUD
) )
156 if( nuh
->nal_ref_idc
)
158 /* nal_ref_idc shall be equal to 0 for all NALUs having nal_unit_type equal to 6, 9, 10, 11, or 12. */
159 IF_INVALID_VALUE( nuh
->nal_unit_type
== H264_NALU_TYPE_SEI
160 || nuh
->nal_unit_type
== H264_NALU_TYPE_AUD
161 || nuh
->nal_unit_type
== H264_NALU_TYPE_EOS
162 || nuh
->nal_unit_type
== H264_NALU_TYPE_EOB
163 || nuh
->nal_unit_type
== H264_NALU_TYPE_FD
)
167 /* nal_ref_idc shall not be equal to 0 for NALUs with nal_unit_type equal to 5. */
168 IF_INVALID_VALUE( nuh
->nal_unit_type
== H264_NALU_TYPE_SLICE_IDR
)
173 uint64_t h264_find_next_start_code
176 h264_nalu_header_t
*nuh
,
177 uint64_t *start_code_length
,
178 uint64_t *trailing_zero_bytes
181 uint64_t length
= 0; /* the length of the latest NALU */
182 uint64_t count
= 0; /* the number of the trailing zero bytes after the latest NALU */
183 /* Check the type of the current start code. */
185 = (!lsmash_bs_is_end( bs
, NALU_LONG_START_CODE_LENGTH
) && 0x00000001 == lsmash_bs_show_be32( bs
, 0 )) ? 1
186 : (!lsmash_bs_is_end( bs
, NALU_SHORT_START_CODE_LENGTH
) && 0x000001 == lsmash_bs_show_be24( bs
, 0 )) ? 0
188 if( long_start_code
>= 0 && h264_check_nalu_header( bs
, nuh
, long_start_code
) == 0 )
190 *start_code_length
= long_start_code
? NALU_LONG_START_CODE_LENGTH
: NALU_SHORT_START_CODE_LENGTH
;
191 uint64_t distance
= *start_code_length
+ nuh
->length
;
192 /* Find the start code of the next NALU and get the distance from the start code of the latest NALU. */
193 if( !lsmash_bs_is_end( bs
, distance
+ NALU_SHORT_START_CODE_LENGTH
) )
195 uint32_t sync_bytes
= lsmash_bs_show_be24( bs
, distance
);
196 while( 0x000001 != sync_bytes
)
198 if( lsmash_bs_is_end( bs
, ++distance
+ NALU_SHORT_START_CODE_LENGTH
) )
200 distance
= lsmash_bs_get_remaining_buffer_size( bs
);
204 sync_bytes
|= lsmash_bs_show_byte( bs
, distance
+ NALU_SHORT_START_CODE_LENGTH
- 1 );
205 sync_bytes
&= 0xFFFFFF;
209 distance
= lsmash_bs_get_remaining_buffer_size( bs
);
210 /* Any NALU has no consecutive zero bytes at the end. */
211 while( 0x00 == lsmash_bs_show_byte( bs
, distance
- 1 ) )
216 /* Remove the length of the start code. */
217 length
= distance
- *start_code_length
;
218 /* If there are one or more trailing zero bytes, we treat the last one byte as a part of the next start code.
219 * This makes the next start code a long start code. */
226 nuh
->forbidden_zero_bit
= 1; /* shall be 0, so invalid */
227 nuh
->nal_ref_idc
= 0; /* arbitrary */
228 nuh
->nal_unit_type
= H264_NALU_TYPE_UNSPECIFIED0
;
230 *start_code_length
= 0;
232 *trailing_zero_bytes
= count
;
236 static h264_sps_t
*h264_get_sps
238 lsmash_entry_list_t
*sps_list
,
242 if( !sps_list
|| sps_id
> 31 )
244 for( lsmash_entry_t
*entry
= sps_list
->head
; entry
; entry
= entry
->next
)
246 h264_sps_t
*sps
= (h264_sps_t
*)entry
->data
;
249 if( sps
->seq_parameter_set_id
== sps_id
)
252 h264_sps_t
*sps
= lsmash_malloc_zero( sizeof(h264_sps_t
) );
255 sps
->seq_parameter_set_id
= sps_id
;
256 if( lsmash_add_entry( sps_list
, sps
) )
264 static h264_pps_t
*h264_get_pps
266 lsmash_entry_list_t
*pps_list
,
272 for( lsmash_entry_t
*entry
= pps_list
->head
; entry
; entry
= entry
->next
)
274 h264_pps_t
*pps
= (h264_pps_t
*)entry
->data
;
277 if( pps
->pic_parameter_set_id
== pps_id
)
280 h264_pps_t
*pps
= lsmash_malloc_zero( sizeof(h264_pps_t
) );
283 pps
->pic_parameter_set_id
= pps_id
;
284 if( lsmash_add_entry( pps_list
, pps
) )
292 static h264_slice_info_t
*h264_get_slice_info
294 lsmash_entry_list_t
*slice_list
,
300 for( lsmash_entry_t
*entry
= slice_list
->head
; entry
; entry
= entry
->next
)
302 h264_slice_info_t
*slice
= (h264_slice_info_t
*)entry
->data
;
305 if( slice
->slice_id
== slice_id
)
308 h264_slice_info_t
*slice
= lsmash_malloc_zero( sizeof(h264_slice_info_t
) );
311 slice
->slice_id
= slice_id
;
312 if( lsmash_add_entry( slice_list
, slice
) )
314 lsmash_free( slice
);
320 int h264_calculate_poc
323 h264_picture_info_t
*picture
,
324 h264_picture_info_t
*prev_picture
327 #if H264_POC_DEBUG_PRINT
328 fprintf( stderr
, "PictureOrderCount\n" );
330 h264_pps_t
*pps
= h264_get_pps( info
->pps_list
, picture
->pic_parameter_set_id
);
333 h264_sps_t
*sps
= h264_get_sps( info
->sps_list
, pps
->seq_parameter_set_id
);
336 int64_t TopFieldOrderCnt
= 0;
337 int64_t BottomFieldOrderCnt
= 0;
338 if( sps
->pic_order_cnt_type
== 0 )
340 int32_t prevPicOrderCntMsb
;
341 int32_t prevPicOrderCntLsb
;
344 prevPicOrderCntMsb
= 0;
345 prevPicOrderCntLsb
= 0;
347 else if( prev_picture
->ref_pic_has_mmco5
)
349 prevPicOrderCntMsb
= 0;
350 prevPicOrderCntLsb
= prev_picture
->ref_pic_bottom_field_flag
? 0 : prev_picture
->ref_pic_TopFieldOrderCnt
;
354 prevPicOrderCntMsb
= prev_picture
->ref_pic_PicOrderCntMsb
;
355 prevPicOrderCntLsb
= prev_picture
->ref_pic_PicOrderCntLsb
;
357 int64_t PicOrderCntMsb
;
358 int32_t pic_order_cnt_lsb
= picture
->pic_order_cnt_lsb
;
359 uint64_t MaxPicOrderCntLsb
= sps
->MaxPicOrderCntLsb
;
360 if( (pic_order_cnt_lsb
< prevPicOrderCntLsb
)
361 && ((prevPicOrderCntLsb
- pic_order_cnt_lsb
) >= (MaxPicOrderCntLsb
/ 2)) )
362 PicOrderCntMsb
= prevPicOrderCntMsb
+ MaxPicOrderCntLsb
;
363 else if( (pic_order_cnt_lsb
> prevPicOrderCntLsb
)
364 && ((pic_order_cnt_lsb
- prevPicOrderCntLsb
) > (MaxPicOrderCntLsb
/ 2)) )
365 PicOrderCntMsb
= prevPicOrderCntMsb
- MaxPicOrderCntLsb
;
367 PicOrderCntMsb
= prevPicOrderCntMsb
;
368 IF_EXCEED_INT32( PicOrderCntMsb
)
370 BottomFieldOrderCnt
= TopFieldOrderCnt
= PicOrderCntMsb
+ pic_order_cnt_lsb
;
371 if( !picture
->field_pic_flag
)
372 BottomFieldOrderCnt
+= picture
->delta_pic_order_cnt_bottom
;
373 IF_EXCEED_INT32( TopFieldOrderCnt
)
375 IF_EXCEED_INT32( BottomFieldOrderCnt
)
377 if( !picture
->disposable
)
379 picture
->ref_pic_has_mmco5
= picture
->has_mmco5
;
380 picture
->ref_pic_bottom_field_flag
= picture
->bottom_field_flag
;
381 picture
->ref_pic_TopFieldOrderCnt
= TopFieldOrderCnt
;
382 picture
->ref_pic_PicOrderCntMsb
= PicOrderCntMsb
;
383 picture
->ref_pic_PicOrderCntLsb
= pic_order_cnt_lsb
;
385 #if H264_POC_DEBUG_PRINT
386 fprintf( stderr
, " prevPicOrderCntMsb: %"PRId32
"\n", prevPicOrderCntMsb
);
387 fprintf( stderr
, " prevPicOrderCntLsb: %"PRId32
"\n", prevPicOrderCntLsb
);
388 fprintf( stderr
, " PicOrderCntMsb: %"PRId64
"\n", PicOrderCntMsb
);
389 fprintf( stderr
, " pic_order_cnt_lsb: %"PRId32
"\n", pic_order_cnt_lsb
);
390 fprintf( stderr
, " MaxPicOrderCntLsb: %"PRIu64
"\n", MaxPicOrderCntLsb
);
393 else if( sps
->pic_order_cnt_type
== 1 )
395 uint32_t frame_num
= picture
->frame_num
;
396 uint32_t prevFrameNum
= prev_picture
->has_mmco5
? 0 : prev_picture
->frame_num
;
397 uint32_t prevFrameNumOffset
= prev_picture
->has_mmco5
? 0 : prev_picture
->FrameNumOffset
;
398 uint64_t FrameNumOffset
= picture
->idr
? 0 : prevFrameNumOffset
+ (prevFrameNum
> frame_num
? sps
->MaxFrameNum
: 0);
399 IF_INVALID_VALUE( FrameNumOffset
> INT32_MAX
)
401 int64_t expectedPicOrderCnt
;
402 if( sps
->num_ref_frames_in_pic_order_cnt_cycle
)
404 uint64_t absFrameNum
= FrameNumOffset
+ frame_num
;
405 absFrameNum
-= picture
->disposable
&& absFrameNum
> 0;
408 uint64_t picOrderCntCycleCnt
= (absFrameNum
- 1) / sps
->num_ref_frames_in_pic_order_cnt_cycle
;
409 uint8_t frameNumInPicOrderCntCycle
= (absFrameNum
- 1) % sps
->num_ref_frames_in_pic_order_cnt_cycle
;
410 expectedPicOrderCnt
= picOrderCntCycleCnt
* sps
->ExpectedDeltaPerPicOrderCntCycle
;
411 for( uint8_t i
= 0; i
<= frameNumInPicOrderCntCycle
; i
++ )
412 expectedPicOrderCnt
+= sps
->offset_for_ref_frame
[i
];
415 expectedPicOrderCnt
= 0;
418 expectedPicOrderCnt
= 0;
419 if( picture
->disposable
)
420 expectedPicOrderCnt
+= sps
->offset_for_non_ref_pic
;
421 TopFieldOrderCnt
= expectedPicOrderCnt
+ picture
->delta_pic_order_cnt
[0];
422 BottomFieldOrderCnt
= TopFieldOrderCnt
+ sps
->offset_for_top_to_bottom_field
;
423 if( !picture
->field_pic_flag
)
424 BottomFieldOrderCnt
+= picture
->delta_pic_order_cnt
[1];
425 IF_EXCEED_INT32( TopFieldOrderCnt
)
427 IF_EXCEED_INT32( BottomFieldOrderCnt
)
429 picture
->FrameNumOffset
= FrameNumOffset
;
431 else if( sps
->pic_order_cnt_type
== 2 )
433 uint32_t frame_num
= picture
->frame_num
;
434 uint32_t prevFrameNum
= prev_picture
->has_mmco5
? 0 : prev_picture
->frame_num
;
435 int32_t prevFrameNumOffset
= prev_picture
->has_mmco5
? 0 : prev_picture
->FrameNumOffset
;
436 int64_t FrameNumOffset
;
437 int64_t tempPicOrderCnt
;
445 FrameNumOffset
= prevFrameNumOffset
+ (prevFrameNum
> frame_num
? sps
->MaxFrameNum
: 0);
446 tempPicOrderCnt
= 2 * (FrameNumOffset
+ frame_num
) - picture
->disposable
;
447 IF_EXCEED_INT32( FrameNumOffset
)
449 IF_EXCEED_INT32( tempPicOrderCnt
)
452 TopFieldOrderCnt
= tempPicOrderCnt
;
453 BottomFieldOrderCnt
= tempPicOrderCnt
;
454 picture
->FrameNumOffset
= FrameNumOffset
;
456 if( !picture
->field_pic_flag
)
457 picture
->PicOrderCnt
= LSMASH_MIN( TopFieldOrderCnt
, BottomFieldOrderCnt
);
459 picture
->PicOrderCnt
= picture
->bottom_field_flag
? BottomFieldOrderCnt
: TopFieldOrderCnt
;
460 #if H264_POC_DEBUG_PRINT
461 if( picture
->field_pic_flag
)
463 if( !picture
->bottom_field_flag
)
464 fprintf( stderr
, " TopFieldOrderCnt: %"PRId64
"\n", TopFieldOrderCnt
);
466 fprintf( stderr
, " BottomFieldOrderCnt: %"PRId64
"\n", BottomFieldOrderCnt
);
468 fprintf( stderr
, " POC: %"PRId32
"\n", picture
->PicOrderCnt
);
473 static int h264_parse_scaling_list
476 int sizeOfScalingList
479 /* scaling_list( scalingList, sizeOfScalingList, useDefaultScalingMatrixFlag ) */
481 for( int i
= 0; i
< sizeOfScalingList
; i
++ )
483 int64_t delta_scale
= nalu_get_exp_golomb_se( bits
);
484 IF_INVALID_VALUE( delta_scale
< -128 || delta_scale
> 127 )
486 nextScale
= (nextScale
+ delta_scale
+ 256) % 256;
493 static int h264_parse_hrd_parameters
499 /* hrd_parameters() */
500 uint64_t cpb_cnt_minus1
= nalu_get_exp_golomb_ue( bits
);
501 IF_INVALID_VALUE( cpb_cnt_minus1
> 31 )
503 lsmash_bits_get( bits
, 4 ); /* bit_rate_scale */
504 lsmash_bits_get( bits
, 4 ); /* cpb_size_scale */
505 for( uint64_t SchedSelIdx
= 0; SchedSelIdx
<= cpb_cnt_minus1
; SchedSelIdx
++ )
507 nalu_get_exp_golomb_ue( bits
); /* bit_rate_value_minus1[ SchedSelIdx ] */
508 nalu_get_exp_golomb_ue( bits
); /* cpb_size_value_minus1[ SchedSelIdx ] */
509 lsmash_bits_get( bits
, 1 ); /* cbr_flag [ SchedSelIdx ] */
511 lsmash_bits_get( bits
, 5 ); /* initial_cpb_removal_delay_length_minus1 */
512 hrd
->cpb_removal_delay_length
= lsmash_bits_get( bits
, 5 ) + 1;
513 hrd
->dpb_output_delay_length
= lsmash_bits_get( bits
, 5 ) + 1;
514 lsmash_bits_get( bits
, 5 ); /* time_offset_length */
518 static int h264_parse_sps_minimally
522 uint8_t *rbsp_buffer
,
527 if( nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, ebsp
, ebsp_size
) )
529 memset( sps
, 0, sizeof(h264_sps_t
) );
530 sps
->profile_idc
= lsmash_bits_get( bits
, 8 );
531 sps
->constraint_set_flags
= lsmash_bits_get( bits
, 8 );
532 sps
->level_idc
= lsmash_bits_get( bits
, 8 );
533 uint64_t seq_parameter_set_id
= nalu_get_exp_golomb_ue( bits
);
534 IF_INVALID_VALUE( seq_parameter_set_id
> 31 )
536 sps
->seq_parameter_set_id
= seq_parameter_set_id
;
537 if( sps
->profile_idc
== 100 || sps
->profile_idc
== 110 || sps
->profile_idc
== 122
538 || sps
->profile_idc
== 244 || sps
->profile_idc
== 44 || sps
->profile_idc
== 83
539 || sps
->profile_idc
== 86 || sps
->profile_idc
== 118 || sps
->profile_idc
== 128
540 || sps
->profile_idc
== 138 )
542 sps
->chroma_format_idc
= nalu_get_exp_golomb_ue( bits
);
543 if( sps
->chroma_format_idc
== 3 )
544 sps
->separate_colour_plane_flag
= lsmash_bits_get( bits
, 1 );
545 uint64_t bit_depth_luma_minus8
= nalu_get_exp_golomb_ue( bits
);
546 IF_INVALID_VALUE( bit_depth_luma_minus8
> 6 )
548 uint64_t bit_depth_chroma_minus8
= nalu_get_exp_golomb_ue( bits
);
549 IF_INVALID_VALUE( bit_depth_chroma_minus8
> 6 )
551 sps
->bit_depth_luma_minus8
= bit_depth_luma_minus8
;
552 sps
->bit_depth_chroma_minus8
= bit_depth_chroma_minus8
;
553 lsmash_bits_get( bits
, 1 ); /* qpprime_y_zero_transform_bypass_flag */
554 if( lsmash_bits_get( bits
, 1 ) ) /* seq_scaling_matrix_present_flag */
556 int num_loops
= sps
->chroma_format_idc
!= 3 ? 8 : 12;
557 for( int i
= 0; i
< num_loops
; i
++ )
558 if( lsmash_bits_get( bits
, 1 ) /* seq_scaling_list_present_flag[i] */
559 && h264_parse_scaling_list( bits
, i
< 6 ? 16 : 64 ) )
565 sps
->chroma_format_idc
= 1;
566 sps
->separate_colour_plane_flag
= 0;
567 sps
->bit_depth_luma_minus8
= 0;
568 sps
->bit_depth_chroma_minus8
= 0;
570 return bits
->bs
->error
? -1 : 0;
576 uint8_t *rbsp_buffer
,
581 lsmash_bits_t
*bits
= info
->bits
;
582 /* seq_parameter_set_data() */
584 if( 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
);
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_INVALID_VALUE( log2_max_frame_num_minus4
> 12 )
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_INVALID_VALUE( pic_order_cnt_type
> 2 )
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_INVALID_VALUE( log2_max_pic_order_cnt_lsb_minus4
> 12 )
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 int64_t max_value
= ((uint64_t)1 << 31) - 1;
620 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
)
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
)
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_INVALID_VALUE( num_ref_frames_in_pic_order_cnt_cycle
> 255 )
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
)
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 && h264_parse_hrd_parameters( bits
, &sps
->vui
.hrd
) )
751 int vcl_hrd_parameters_present_flag
= lsmash_bits_get( bits
, 1 );
752 if( vcl_hrd_parameters_present_flag
753 && h264_parse_hrd_parameters( bits
, &sps
->vui
.hrd
) )
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_INVALID_VALUE( !lsmash_bits_get( bits
, 1 ) ) /* rbsp_stop_one_bit */
783 lsmash_bits_empty( bits
);
784 if( bits
->bs
->error
)
791 static int h264_parse_pps_minimally
795 uint8_t *rbsp_buffer
,
800 if( nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, ebsp
, ebsp_size
) )
802 memset( pps
, 0, sizeof(h264_pps_t
) );
803 uint64_t pic_parameter_set_id
= nalu_get_exp_golomb_ue( bits
);
804 IF_INVALID_VALUE( pic_parameter_set_id
> 255 )
806 pps
->pic_parameter_set_id
= pic_parameter_set_id
;
807 return bits
->bs
->error
? -1 : 0;
813 uint8_t *rbsp_buffer
,
818 lsmash_bits_t
*bits
= info
->bits
;
819 /* pic_parameter_set_rbsp */
821 if( h264_parse_pps_minimally( bits
, &temp_pps
, rbsp_buffer
, ebsp
, ebsp_size
) )
823 h264_pps_t
*pps
= h264_get_pps( info
->pps_list
, temp_pps
.pic_parameter_set_id
);
826 memset( pps
, 0, sizeof(h264_pps_t
) );
827 pps
->pic_parameter_set_id
= temp_pps
.pic_parameter_set_id
;
828 uint64_t seq_parameter_set_id
= nalu_get_exp_golomb_ue( bits
);
829 IF_INVALID_VALUE( seq_parameter_set_id
> 31 )
831 h264_sps_t
*sps
= h264_get_sps( info
->sps_list
, seq_parameter_set_id
);
834 pps
->seq_parameter_set_id
= seq_parameter_set_id
;
835 pps
->entropy_coding_mode_flag
= lsmash_bits_get( bits
, 1 );
836 pps
->bottom_field_pic_order_in_frame_present_flag
= lsmash_bits_get( bits
, 1 );
837 uint64_t num_slice_groups_minus1
= nalu_get_exp_golomb_ue( bits
);
838 IF_INVALID_VALUE( num_slice_groups_minus1
> 7 )
840 pps
->num_slice_groups_minus1
= num_slice_groups_minus1
;
841 if( num_slice_groups_minus1
) /* num_slice_groups_minus1 */
843 uint64_t slice_group_map_type
= nalu_get_exp_golomb_ue( bits
);
844 IF_INVALID_VALUE( slice_group_map_type
> 6 )
846 pps
->slice_group_map_type
= slice_group_map_type
;
847 if( slice_group_map_type
== 0 )
848 for( uint64_t iGroup
= 0; iGroup
<= num_slice_groups_minus1
; iGroup
++ )
849 nalu_get_exp_golomb_ue( bits
); /* run_length_minus1[ iGroup ] */
850 else if( slice_group_map_type
== 2 )
851 for( uint64_t iGroup
= 0; iGroup
< num_slice_groups_minus1
; iGroup
++ )
853 nalu_get_exp_golomb_ue( bits
); /* top_left [ iGroup ] */
854 nalu_get_exp_golomb_ue( bits
); /* bottom_right[ iGroup ] */
856 else if( slice_group_map_type
== 3
857 || slice_group_map_type
== 4
858 || slice_group_map_type
== 5 )
860 lsmash_bits_get( bits
, 1 ); /* slice_group_change_direction_flag */
861 uint64_t slice_group_change_rate_minus1
= nalu_get_exp_golomb_ue( bits
);
862 IF_INVALID_VALUE( slice_group_change_rate_minus1
> (sps
->PicSizeInMapUnits
- 1) )
864 pps
->SliceGroupChangeRate
= slice_group_change_rate_minus1
+ 1;
866 else if( slice_group_map_type
== 6 )
868 uint64_t pic_size_in_map_units_minus1
= nalu_get_exp_golomb_ue( bits
);
869 int length
= lsmash_ceil_log2( num_slice_groups_minus1
+ 1 );
870 for( uint64_t i
= 0; i
<= pic_size_in_map_units_minus1
; i
++ )
872 IF_INVALID_VALUE( lsmash_bits_get( bits
, length
) > num_slice_groups_minus1
)
876 pps
->num_ref_idx_l0_default_active_minus1
= nalu_get_exp_golomb_ue( bits
);
877 pps
->num_ref_idx_l1_default_active_minus1
= nalu_get_exp_golomb_ue( bits
);
878 pps
->weighted_pred_flag
= lsmash_bits_get( bits
, 1 );
879 pps
->weighted_bipred_idc
= lsmash_bits_get( bits
, 2 );
880 nalu_get_exp_golomb_se( bits
); /* pic_init_qp_minus26 */
881 nalu_get_exp_golomb_se( bits
); /* pic_init_qs_minus26 */
882 nalu_get_exp_golomb_se( bits
); /* chroma_qp_index_offset */
883 pps
->deblocking_filter_control_present_flag
= lsmash_bits_get( bits
, 1 );
884 lsmash_bits_get( bits
, 1 ); /* constrained_intra_pred_flag */
885 pps
->redundant_pic_cnt_present_flag
= lsmash_bits_get( bits
, 1 );
886 if( nalu_check_more_rbsp_data( bits
) )
888 int transform_8x8_mode_flag
= lsmash_bits_get( bits
, 1 );
889 if( lsmash_bits_get( bits
, 1 ) ) /* pic_scaling_matrix_present_flag */
891 int num_loops
= 6 + (sps
->chroma_format_idc
!= 3 ? 2 : 6) * transform_8x8_mode_flag
;
892 for( int i
= 0; i
< num_loops
; i
++ )
893 if( lsmash_bits_get( bits
, 1 ) /* pic_scaling_list_present_flag[i] */
894 && h264_parse_scaling_list( bits
, i
< 6 ? 16 : 64 ) )
897 nalu_get_exp_golomb_se( bits
); /* second_chroma_qp_index_offset */
899 /* rbsp_trailing_bits() */
900 IF_INVALID_VALUE( !lsmash_bits_get( bits
, 1 ) ) /* rbsp_stop_one_bit */
902 lsmash_bits_empty( bits
);
903 if( bits
->bs
->error
)
916 uint8_t *rbsp_buffer
,
921 if( nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, ebsp
, ebsp_size
) )
923 uint8_t *rbsp_start
= rbsp_buffer
;
924 uint64_t rbsp_pos
= 0;
928 uint32_t payloadType
= 0;
929 for( uint8_t temp
= lsmash_bits_get( bits
, 8 ); ; temp
= lsmash_bits_get( bits
, 8 ) )
932 * otherwise: last_payload_type_byte */
938 uint32_t payloadSize
= 0;
939 for( uint8_t temp
= lsmash_bits_get( bits
, 8 ); ; temp
= lsmash_bits_get( bits
, 8 ) )
942 * otherwise: last_payload_size_byte */
948 if( payloadType
== 1 )
951 h264_hrd_t
*hrd
= sps
? &sps
->vui
.hrd
: NULL
;
953 goto skip_sei_message
; /* Any active SPS is not found. */
954 sei
->pic_timing
.present
= 1;
955 if( hrd
->CpbDpbDelaysPresentFlag
)
957 lsmash_bits_get( bits
, hrd
->cpb_removal_delay_length
); /* cpb_removal_delay */
958 lsmash_bits_get( bits
, hrd
->dpb_output_delay_length
); /* dpb_output_delay */
960 if( sps
->vui
.pic_struct_present_flag
)
962 sei
->pic_timing
.pic_struct
= lsmash_bits_get( bits
, 4 );
963 /* Skip the remaining bits. */
964 uint32_t remaining_bits
= payloadSize
* 8 - 4;
965 if( hrd
->CpbDpbDelaysPresentFlag
)
966 remaining_bits
-= hrd
->cpb_removal_delay_length
967 + hrd
->dpb_output_delay_length
;
968 lsmash_bits_get( bits
, remaining_bits
);
971 else if( payloadType
== 3 )
974 * AVC file format is forbidden to contain this. */
977 else if( payloadType
== 6 )
980 sei
->recovery_point
.present
= 1;
981 sei
->recovery_point
.random_accessible
= 1;
982 sei
->recovery_point
.recovery_frame_cnt
= nalu_get_exp_golomb_ue( bits
);
983 lsmash_bits_get( bits
, 1 ); /* exact_match_flag */
984 sei
->recovery_point
.broken_link_flag
= lsmash_bits_get( bits
, 1 );
985 lsmash_bits_get( bits
, 2 ); /* changing_slice_group_idc */
990 lsmash_bits_get( bits
, payloadSize
* 8 );
992 lsmash_bits_get_align( bits
);
993 rbsp_pos
+= payloadSize
;
994 } while( *(rbsp_start
+ rbsp_pos
) != 0x80 ); /* All SEI messages are byte aligned at their end.
995 * Therefore, 0x80 shall be rbsp_trailing_bits(). */
996 lsmash_bits_empty( bits
);
997 return bits
->bs
->error
? -1 : 0;
1000 static int h264_parse_slice_header
1003 h264_nalu_header_t
*nuh
1006 h264_slice_info_t
*slice
= &info
->slice
;
1007 memset( slice
, 0, sizeof(h264_slice_info_t
) );
1008 /* slice_header() */
1009 lsmash_bits_t
*bits
= info
->bits
;
1010 nalu_get_exp_golomb_ue( bits
); /* first_mb_in_slice */
1011 uint8_t slice_type
= slice
->type
= nalu_get_exp_golomb_ue( bits
);
1012 IF_INVALID_VALUE( (uint64_t)slice
->type
> 9 )
1014 if( slice_type
> 4 )
1015 slice_type
= slice
->type
-= 5;
1016 uint64_t pic_parameter_set_id
= nalu_get_exp_golomb_ue( bits
);
1017 IF_INVALID_VALUE( pic_parameter_set_id
> 255 )
1019 slice
->pic_parameter_set_id
= pic_parameter_set_id
;
1020 h264_pps_t
*pps
= h264_get_pps( info
->pps_list
, pic_parameter_set_id
);
1023 h264_sps_t
*sps
= h264_get_sps( info
->sps_list
, pps
->seq_parameter_set_id
);
1026 slice
->seq_parameter_set_id
= pps
->seq_parameter_set_id
;
1027 slice
->nal_ref_idc
= nuh
->nal_ref_idc
;
1028 slice
->IdrPicFlag
= (nuh
->nal_unit_type
== H264_NALU_TYPE_SLICE_IDR
);
1029 slice
->pic_order_cnt_type
= sps
->pic_order_cnt_type
;
1030 IF_INVALID_VALUE( (slice
->IdrPicFlag
|| sps
->max_num_ref_frames
== 0) && slice_type
!= 2 && slice_type
!= 4 )
1032 if( sps
->separate_colour_plane_flag
)
1033 lsmash_bits_get( bits
, 2 ); /* colour_plane_id */
1034 uint64_t frame_num
= lsmash_bits_get( bits
, sps
->log2_max_frame_num
);
1035 IF_INVALID_VALUE( frame_num
>= (1 << sps
->log2_max_frame_num
) || (slice
->IdrPicFlag
&& frame_num
) )
1037 slice
->frame_num
= frame_num
;
1038 if( !sps
->frame_mbs_only_flag
)
1040 slice
->field_pic_flag
= lsmash_bits_get( bits
, 1 );
1041 if( slice
->field_pic_flag
)
1042 slice
->bottom_field_flag
= lsmash_bits_get( bits
, 1 );
1044 if( slice
->IdrPicFlag
)
1046 uint64_t idr_pic_id
= nalu_get_exp_golomb_ue( bits
);
1047 IF_INVALID_VALUE( idr_pic_id
> 65535 )
1049 slice
->idr_pic_id
= idr_pic_id
;
1051 if( sps
->pic_order_cnt_type
== 0 )
1053 uint64_t pic_order_cnt_lsb
= lsmash_bits_get( bits
, sps
->log2_max_pic_order_cnt_lsb
);
1054 IF_INVALID_VALUE( pic_order_cnt_lsb
>= sps
->MaxPicOrderCntLsb
)
1056 slice
->pic_order_cnt_lsb
= pic_order_cnt_lsb
;
1057 if( pps
->bottom_field_pic_order_in_frame_present_flag
&& !slice
->field_pic_flag
)
1058 slice
->delta_pic_order_cnt_bottom
= nalu_get_exp_golomb_se( bits
);
1060 else if( sps
->pic_order_cnt_type
== 1 && !sps
->delta_pic_order_always_zero_flag
)
1062 slice
->delta_pic_order_cnt
[0] = nalu_get_exp_golomb_se( bits
);
1063 if( pps
->bottom_field_pic_order_in_frame_present_flag
&& !slice
->field_pic_flag
)
1064 slice
->delta_pic_order_cnt
[1] = nalu_get_exp_golomb_se( bits
);
1066 if( pps
->redundant_pic_cnt_present_flag
)
1068 uint64_t redundant_pic_cnt
= nalu_get_exp_golomb_ue( bits
);
1069 IF_INVALID_VALUE( redundant_pic_cnt
> 127 )
1071 slice
->has_redundancy
= !!redundant_pic_cnt
;
1073 if( slice_type
== H264_SLICE_TYPE_B
)
1074 lsmash_bits_get( bits
, 1 );
1075 uint64_t num_ref_idx_l0_active_minus1
= pps
->num_ref_idx_l0_default_active_minus1
;
1076 uint64_t num_ref_idx_l1_active_minus1
= pps
->num_ref_idx_l1_default_active_minus1
;
1077 if( slice_type
== H264_SLICE_TYPE_P
|| slice_type
== H264_SLICE_TYPE_SP
|| slice_type
== H264_SLICE_TYPE_B
)
1079 if( lsmash_bits_get( bits
, 1 ) ) /* num_ref_idx_active_override_flag */
1081 num_ref_idx_l0_active_minus1
= nalu_get_exp_golomb_ue( bits
);
1082 IF_INVALID_VALUE( num_ref_idx_l0_active_minus1
> 31 )
1084 if( slice_type
== H264_SLICE_TYPE_B
)
1086 num_ref_idx_l1_active_minus1
= nalu_get_exp_golomb_ue( bits
);
1087 IF_INVALID_VALUE( num_ref_idx_l1_active_minus1
> 31 )
1092 if( nuh
->nal_unit_type
== H264_NALU_TYPE_SLICE_EXT
1093 || nuh
->nal_unit_type
== H264_NALU_TYPE_SLICE_EXT_DVC
)
1095 return -1; /* No support of MVC yet */
1097 /* ref_pic_list_mvc_modification() */
1098 if( slice_type
== H264_SLICE_TYPE_P
|| slice_type
== H264_SLICE_TYPE_B
|| slice_type
== H264_SLICE_TYPE_SP
)
1100 for( int i
= 0; i
< 1 + (slice_type
== H264_SLICE_TYPE_B
); i
++ )
1102 if( lsmash_bits_get( bits
, 1 ) ) /* (S)P and B: ref_pic_list_modification_flag_l0
1103 * B: ref_pic_list_modification_flag_l1 */
1105 uint64_t modification_of_pic_nums_idc
;
1108 modification_of_pic_nums_idc
= nalu_get_exp_golomb_ue( bits
);
1110 if( modification_of_pic_nums_idc
== 0 || modification_of_pic_nums_idc
== 1 )
1111 nalu_get_exp_golomb_ue( bits
); /* abs_diff_pic_num_minus1 */
1112 else if( modification_of_pic_nums_idc
== 2 )
1113 nalu_get_exp_golomb_ue( bits
); /* long_term_pic_num */
1114 else if( modification_of_pic_nums_idc
== 4 || modification_of_pic_nums_idc
== 5 )
1115 nalu_get_exp_golomb_ue( bits
); /* abs_diff_view_idx_minus1 */
1117 if( modification_of_pic_nums_idc
!= 3 )
1118 nalu_get_exp_golomb_ue( bits
); /* abs_diff_pic_num_minus1, long_term_pic_num or abs_diff_view_idx_minus1 */
1120 } while( modification_of_pic_nums_idc
!= 3 );
1128 /* ref_pic_list_modification() */
1129 if( slice_type
== H264_SLICE_TYPE_P
|| slice_type
== H264_SLICE_TYPE_B
|| slice_type
== H264_SLICE_TYPE_SP
)
1131 for( int i
= 0; i
< 1 + (slice_type
== H264_SLICE_TYPE_B
); i
++ )
1133 if( lsmash_bits_get( bits
, 1 ) ) /* (S)P and B: ref_pic_list_modification_flag_l0
1134 * B: ref_pic_list_modification_flag_l1 */
1136 uint64_t modification_of_pic_nums_idc
;
1139 modification_of_pic_nums_idc
= nalu_get_exp_golomb_ue( bits
);
1141 if( modification_of_pic_nums_idc
== 0 || modification_of_pic_nums_idc
== 1 )
1142 nalu_get_exp_golomb_ue( bits
); /* abs_diff_pic_num_minus1 */
1143 else if( modification_of_pic_nums_idc
== 2 )
1144 nalu_get_exp_golomb_ue( bits
); /* long_term_pic_num */
1146 if( modification_of_pic_nums_idc
!= 3 )
1147 nalu_get_exp_golomb_ue( bits
); /* abs_diff_pic_num_minus1 or long_term_pic_num */
1149 } while( modification_of_pic_nums_idc
!= 3 );
1154 if( (pps
->weighted_pred_flag
&& (slice_type
== H264_SLICE_TYPE_P
|| slice_type
== H264_SLICE_TYPE_SP
))
1155 || (pps
->weighted_bipred_idc
== 1 && slice_type
== H264_SLICE_TYPE_B
) )
1157 /* pred_weight_table() */
1158 nalu_get_exp_golomb_ue( bits
); /* luma_log2_weight_denom */
1159 if( sps
->ChromaArrayType
)
1160 nalu_get_exp_golomb_ue( bits
); /* chroma_log2_weight_denom */
1161 for( uint8_t i
= 0; i
<= num_ref_idx_l0_active_minus1
; i
++ )
1163 if( lsmash_bits_get( bits
, 1 ) ) /* luma_weight_l0_flag */
1165 nalu_get_exp_golomb_se( bits
); /* luma_weight_l0[i] */
1166 nalu_get_exp_golomb_se( bits
); /* luma_offset_l0[i] */
1168 if( sps
->ChromaArrayType
1169 && lsmash_bits_get( bits
, 1 ) /* chroma_weight_l0_flag */ )
1170 for( int j
= 0; j
< 2; j
++ )
1172 nalu_get_exp_golomb_se( bits
); /* chroma_weight_l0[i][j]*/
1173 nalu_get_exp_golomb_se( bits
); /* chroma_offset_l0[i][j] */
1176 if( slice_type
== H264_SLICE_TYPE_B
)
1177 for( uint8_t i
= 0; i
<= num_ref_idx_l1_active_minus1
; i
++ )
1179 if( lsmash_bits_get( bits
, 1 ) ) /* luma_weight_l1_flag */
1181 nalu_get_exp_golomb_se( bits
); /* luma_weight_l1[i] */
1182 nalu_get_exp_golomb_se( bits
); /* luma_offset_l1[i] */
1184 if( sps
->ChromaArrayType
1185 && lsmash_bits_get( bits
, 1 ) /* chroma_weight_l1_flag */ )
1186 for( int j
= 0; j
< 2; j
++ )
1188 nalu_get_exp_golomb_se( bits
); /* chroma_weight_l1[i][j]*/
1189 nalu_get_exp_golomb_se( bits
); /* chroma_offset_l1[i][j] */
1193 if( nuh
->nal_ref_idc
)
1195 /* dec_ref_pic_marking() */
1196 if( slice
->IdrPicFlag
)
1198 lsmash_bits_get( bits
, 1 ); /* no_output_of_prior_pics_flag */
1199 lsmash_bits_get( bits
, 1 ); /* long_term_reference_flag */
1201 else if( lsmash_bits_get( bits
, 1 ) ) /* adaptive_ref_pic_marking_mode_flag */
1203 uint64_t memory_management_control_operation
;
1206 memory_management_control_operation
= nalu_get_exp_golomb_ue( bits
);
1207 if( memory_management_control_operation
)
1209 if( memory_management_control_operation
== 5 )
1210 slice
->has_mmco5
= 1;
1213 nalu_get_exp_golomb_ue( bits
);
1214 if( memory_management_control_operation
== 3 )
1215 nalu_get_exp_golomb_ue( bits
);
1218 } while( memory_management_control_operation
);
1221 /* We needn't read more if not slice data partition A.
1222 * Skip slice_data() and rbsp_slice_trailing_bits(). */
1223 if( nuh
->nal_unit_type
== H264_NALU_TYPE_SLICE_DP_A
)
1225 if( pps
->entropy_coding_mode_flag
&& slice_type
!= H264_SLICE_TYPE_I
&& slice_type
!= H264_SLICE_TYPE_SI
)
1226 nalu_get_exp_golomb_ue( bits
); /* cabac_init_idc */
1227 nalu_get_exp_golomb_se( bits
); /* slice_qp_delta */
1228 if( slice_type
== H264_SLICE_TYPE_SP
|| slice_type
== H264_SLICE_TYPE_SI
)
1230 if( slice_type
== H264_SLICE_TYPE_SP
)
1231 lsmash_bits_get( bits
, 1 ); /* sp_for_switch_flag */
1232 nalu_get_exp_golomb_se( bits
); /* slice_qs_delta */
1234 if( pps
->deblocking_filter_control_present_flag
1235 && nalu_get_exp_golomb_ue( bits
) != 1 /* disable_deblocking_filter_idc */ )
1237 int64_t slice_alpha_c0_offset_div2
= nalu_get_exp_golomb_se( bits
);
1238 IF_INVALID_VALUE( slice_alpha_c0_offset_div2
< -6 || slice_alpha_c0_offset_div2
> 6 )
1240 int64_t slice_beta_offset_div2
= nalu_get_exp_golomb_se( bits
);
1241 IF_INVALID_VALUE( slice_beta_offset_div2
< -6 || slice_beta_offset_div2
> 6 )
1244 if( pps
->num_slice_groups_minus1
1245 && (pps
->slice_group_map_type
== 3 || pps
->slice_group_map_type
== 4 || pps
->slice_group_map_type
== 5) )
1247 uint64_t temp
= ((uint64_t)sps
->PicSizeInMapUnits
- 1) / pps
->SliceGroupChangeRate
+ 1;
1248 uint64_t slice_group_change_cycle
= lsmash_bits_get( bits
, lsmash_ceil_log2( temp
+ 1 ) );
1249 IF_INVALID_VALUE( slice_group_change_cycle
> temp
)
1252 /* end of slice_header() */
1253 slice
->slice_id
= nalu_get_exp_golomb_ue( bits
);
1254 h264_slice_info_t
*slice_part
= h264_get_slice_info( info
->slice_list
, slice
->slice_id
);
1257 *slice_part
= *slice
;
1259 lsmash_bits_empty( bits
);
1260 if( bits
->bs
->error
)
1267 int h264_parse_slice
1270 h264_nalu_header_t
*nuh
,
1271 uint8_t *rbsp_buffer
,
1276 lsmash_bits_t
*bits
= info
->bits
;
1277 uint64_t size
= nuh
->nal_unit_type
== H264_NALU_TYPE_SLICE_IDR
|| nuh
->nal_ref_idc
== 0
1278 ? LSMASH_MIN( ebsp_size
, 100 )
1279 : LSMASH_MIN( ebsp_size
, 1000 );
1280 if( nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, ebsp
, size
) )
1282 if( nuh
->nal_unit_type
!= H264_NALU_TYPE_SLICE_DP_B
1283 && nuh
->nal_unit_type
!= H264_NALU_TYPE_SLICE_DP_C
)
1284 return h264_parse_slice_header( info
, nuh
);
1285 /* slice_data_partition_b_layer_rbsp() or slice_data_partition_c_layer_rbsp() */
1286 uint64_t slice_id
= nalu_get_exp_golomb_ue( bits
);
1287 h264_slice_info_t
*slice
= h264_get_slice_info( info
->slice_list
, slice_id
);
1290 h264_pps_t
*pps
= h264_get_pps( info
->pps_list
, slice
->pic_parameter_set_id
);
1293 h264_sps_t
*sps
= h264_get_sps( info
->sps_list
, pps
->seq_parameter_set_id
);
1296 slice
->seq_parameter_set_id
= pps
->seq_parameter_set_id
;
1297 if( sps
->separate_colour_plane_flag
)
1298 lsmash_bits_get( bits
, 2 ); /* colour_plane_id */
1299 if( pps
->redundant_pic_cnt_present_flag
)
1301 uint64_t redundant_pic_cnt
= nalu_get_exp_golomb_ue( bits
);
1302 IF_INVALID_VALUE( redundant_pic_cnt
> 127 )
1304 slice
->has_redundancy
= !!redundant_pic_cnt
;
1306 /* Skip slice_data() and rbsp_slice_trailing_bits(). */
1307 lsmash_bits_empty( bits
);
1308 if( bits
->bs
->error
)
1315 static int h264_get_sps_id
1318 uint32_t ps_ebsp_length
,
1322 /* max number of bits of sps_id = 11: 0b000001XXXXX
1323 * (24 + 11 - 1) / 8 + 1 = 5 bytes
1324 * Why +1? Because there might be an emulation_prevention_three_byte. */
1325 lsmash_bits_t bits
= { 0 };
1326 lsmash_bs_t bs
= { 0 };
1327 uint8_t rbsp_buffer
[6];
1329 bs
.buffer
.data
= buffer
;
1330 bs
.buffer
.alloc
= 6;
1331 lsmash_bits_init( &bits
, &bs
);
1332 if( nalu_import_rbsp_from_ebsp( &bits
, rbsp_buffer
, ps_ebsp
, LSMASH_MIN( ps_ebsp_length
, 6 ) ) )
1334 lsmash_bits_get( &bits
, 24 ); /* profile_idc, constraint_set_flags and level_idc */
1335 uint64_t sec_parameter_set_id
= nalu_get_exp_golomb_ue( &bits
);
1336 IF_INVALID_VALUE( sec_parameter_set_id
> 31 )
1338 *ps_id
= sec_parameter_set_id
;
1339 return bs
.error
? -1 : 0;
1342 static int h264_get_pps_id
1345 uint32_t ps_ebsp_length
,
1349 /* max number of bits of pps_id = 17: 0b000000001XXXXXXXX
1350 * (17 - 1) / 8 + 1 = 3 bytes
1351 * Why +1? Because there might be an emulation_prevention_three_byte. */
1352 lsmash_bits_t bits
= { 0 };
1353 lsmash_bs_t bs
= { 0 };
1354 uint8_t rbsp_buffer
[4];
1356 bs
.buffer
.data
= buffer
;
1357 bs
.buffer
.alloc
= 4;
1358 lsmash_bits_init( &bits
, &bs
);
1359 if( nalu_import_rbsp_from_ebsp( &bits
, rbsp_buffer
, ps_ebsp
, LSMASH_MIN( ps_ebsp_length
, 4 ) ) )
1361 uint64_t pic_parameter_set_id
= nalu_get_exp_golomb_ue( &bits
);
1362 IF_INVALID_VALUE( pic_parameter_set_id
> 255 )
1364 *ps_id
= pic_parameter_set_id
;
1365 return bs
.error
? -1 : 0;
1368 static inline int h264_get_ps_id
1371 uint32_t ps_ebsp_length
,
1373 lsmash_h264_parameter_set_type ps_type
1376 int (*get_ps_id
)( uint8_t *ps_ebsp
, uint32_t ps_ebsp_length
, uint8_t *ps_id
)
1377 = ps_type
== H264_PARAMETER_SET_TYPE_SPS
? h264_get_sps_id
1378 : ps_type
== H264_PARAMETER_SET_TYPE_PPS
? h264_get_pps_id
1380 return get_ps_id
? get_ps_id( ps_ebsp
, ps_ebsp_length
, ps_id
) : -1;
1383 static inline lsmash_entry_list_t
*h264_get_parameter_set_list
1385 lsmash_h264_specific_parameters_t
*param
,
1386 lsmash_h264_parameter_set_type ps_type
1389 if( !param
->parameter_sets
)
1391 return ps_type
== H264_PARAMETER_SET_TYPE_SPS
? param
->parameter_sets
->sps_list
1392 : ps_type
== H264_PARAMETER_SET_TYPE_PPS
? param
->parameter_sets
->pps_list
1393 : ps_type
== H264_PARAMETER_SET_TYPE_SPSEXT
? param
->parameter_sets
->spsext_list
1397 static lsmash_entry_t
*h264_get_ps_entry_from_param
1399 lsmash_h264_specific_parameters_t
*param
,
1400 lsmash_h264_parameter_set_type ps_type
,
1404 int (*get_ps_id
)( uint8_t *ps_ebsp
, uint32_t ps_ebsp_length
, uint8_t *ps_id
)
1405 = ps_type
== H264_PARAMETER_SET_TYPE_SPS
? h264_get_sps_id
1406 : ps_type
== H264_PARAMETER_SET_TYPE_PPS
? h264_get_pps_id
1410 lsmash_entry_list_t
*ps_list
= h264_get_parameter_set_list( param
, ps_type
);
1413 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
; entry
= entry
->next
)
1415 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1418 uint8_t param_ps_id
;
1419 if( get_ps_id( ps
->nalUnit
+ 1, ps
->nalUnitLength
- 1, ¶m_ps_id
) )
1421 if( ps_id
== param_ps_id
)
1427 static inline void h264_update_picture_type
1429 h264_picture_info_t
*picture
,
1430 h264_slice_info_t
*slice
1433 if( picture
->type
== H264_PICTURE_TYPE_I_P
)
1435 if( slice
->type
== H264_SLICE_TYPE_B
)
1436 picture
->type
= H264_PICTURE_TYPE_I_P_B
;
1437 else if( slice
->type
== H264_SLICE_TYPE_SI
|| slice
->type
== H264_SLICE_TYPE_SP
)
1438 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP
;
1440 else if( picture
->type
== H264_PICTURE_TYPE_I_P_B
)
1442 if( slice
->type
!= H264_SLICE_TYPE_P
&& slice
->type
!= H264_SLICE_TYPE_B
&& slice
->type
!= H264_SLICE_TYPE_I
)
1443 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP_B
;
1445 else if( picture
->type
== H264_PICTURE_TYPE_I
)
1447 if( slice
->type
== H264_SLICE_TYPE_P
)
1448 picture
->type
= H264_PICTURE_TYPE_I_P
;
1449 else if( slice
->type
== H264_SLICE_TYPE_B
)
1450 picture
->type
= H264_PICTURE_TYPE_I_P_B
;
1451 else if( slice
->type
== H264_SLICE_TYPE_SI
)
1452 picture
->type
= H264_PICTURE_TYPE_I_SI
;
1453 else if( slice
->type
== H264_SLICE_TYPE_SP
)
1454 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP
;
1456 else if( picture
->type
== H264_PICTURE_TYPE_SI_SP
)
1458 if( slice
->type
== H264_SLICE_TYPE_P
|| slice
->type
== H264_SLICE_TYPE_I
)
1459 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP
;
1460 else if( slice
->type
== H264_SLICE_TYPE_B
)
1461 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP_B
;
1463 else if( picture
->type
== H264_PICTURE_TYPE_SI
)
1465 if( slice
->type
== H264_SLICE_TYPE_P
)
1466 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP
;
1467 else if( slice
->type
== H264_SLICE_TYPE_B
)
1468 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP_B
;
1469 else if( slice
->type
!= H264_SLICE_TYPE_I
)
1470 picture
->type
= H264_PICTURE_TYPE_I_SI
;
1471 else if( slice
->type
== H264_SLICE_TYPE_SP
)
1472 picture
->type
= H264_PICTURE_TYPE_SI_SP
;
1474 else if( picture
->type
== H264_PICTURE_TYPE_I_SI
)
1476 if( slice
->type
== H264_SLICE_TYPE_P
|| slice
->type
== H264_SLICE_TYPE_SP
)
1477 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP
;
1478 else if( slice
->type
== H264_SLICE_TYPE_B
)
1479 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP_B
;
1481 else if( picture
->type
== H264_PICTURE_TYPE_I_SI_P_SP
)
1483 if( slice
->type
== H264_SLICE_TYPE_B
)
1484 picture
->type
= H264_PICTURE_TYPE_I_SI_P_SP_B
;
1486 else if( picture
->type
== H264_PICTURE_TYPE_NONE
)
1488 if( slice
->type
== H264_SLICE_TYPE_P
)
1489 picture
->type
= H264_PICTURE_TYPE_I_P
;
1490 else if( slice
->type
== H264_SLICE_TYPE_B
)
1491 picture
->type
= H264_PICTURE_TYPE_I_P_B
;
1492 else if( slice
->type
== H264_SLICE_TYPE_I
)
1493 picture
->type
= H264_PICTURE_TYPE_I
;
1494 else if( slice
->type
== H264_SLICE_TYPE_SI
)
1495 picture
->type
= H264_PICTURE_TYPE_SI
;
1496 else if( slice
->type
== H264_SLICE_TYPE_SP
)
1497 picture
->type
= H264_PICTURE_TYPE_SI_SP
;
1500 fprintf( stderr
, "Picture type = %s\n", picture
->type
== H264_PICTURE_TYPE_I_P
? "P"
1501 : picture
->type
== H264_PICTURE_TYPE_I_P_B
? "B"
1502 : picture
->type
== H264_PICTURE_TYPE_I
? "I"
1503 : picture
->type
== H264_PICTURE_TYPE_SI
? "SI"
1504 : picture
->type
== H264_PICTURE_TYPE_I_SI
? "SI"
1509 /* Shall be called at least once per picture. */
1510 void h264_update_picture_info_for_slice
1513 h264_picture_info_t
*picture
,
1514 h264_slice_info_t
*slice
1518 picture
->has_mmco5
|= slice
->has_mmco5
;
1519 picture
->has_redundancy
|= slice
->has_redundancy
;
1520 picture
->has_primary
|= !slice
->has_redundancy
;
1521 h264_update_picture_type( picture
, slice
);
1522 /* Mark 'used' on active parameter sets. */
1523 uint8_t ps_id
[2] = { slice
->seq_parameter_set_id
, slice
->pic_parameter_set_id
};
1524 for( int i
= 0; i
< 2; i
++ )
1526 lsmash_h264_parameter_set_type ps_type
= (lsmash_h264_parameter_set_type
)i
;
1527 lsmash_entry_t
*entry
= h264_get_ps_entry_from_param( &info
->avcC_param
, ps_type
, ps_id
[i
] );
1528 if( entry
&& entry
->data
)
1530 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1532 lsmash_append_h264_parameter_set( &info
->avcC_param
, ps_type
, ps
->nalUnit
, ps
->nalUnitLength
);
1535 /* Discard this slice info. */
1539 /* Shall be called exactly once per picture. */
1540 void h264_update_picture_info
1543 h264_picture_info_t
*picture
,
1544 h264_slice_info_t
*slice
,
1548 picture
->frame_num
= slice
->frame_num
;
1549 picture
->pic_order_cnt_lsb
= slice
->pic_order_cnt_lsb
;
1550 picture
->delta_pic_order_cnt_bottom
= slice
->delta_pic_order_cnt_bottom
;
1551 picture
->delta_pic_order_cnt
[0] = slice
->delta_pic_order_cnt
[0];
1552 picture
->delta_pic_order_cnt
[1] = slice
->delta_pic_order_cnt
[1];
1553 picture
->field_pic_flag
= slice
->field_pic_flag
;
1554 picture
->bottom_field_flag
= slice
->bottom_field_flag
;
1555 picture
->idr
= slice
->IdrPicFlag
;
1556 picture
->pic_parameter_set_id
= slice
->pic_parameter_set_id
;
1557 picture
->disposable
= (slice
->nal_ref_idc
== 0);
1558 picture
->random_accessible
= slice
->IdrPicFlag
;
1559 h264_update_picture_info_for_slice( info
, picture
, slice
);
1560 picture
->independent
= picture
->type
== H264_PICTURE_TYPE_I
|| picture
->type
== H264_PICTURE_TYPE_I_SI
;
1561 if( sei
->pic_timing
.present
)
1563 if( sei
->pic_timing
.pic_struct
< 9 )
1565 static const uint8_t DeltaTfiDivisor
[9] = { 2, 1, 1, 2, 2, 3, 3, 4, 6 };
1566 picture
->delta
= DeltaTfiDivisor
[ sei
->pic_timing
.pic_struct
];
1569 /* Reserved values in the spec we refer to. */
1570 picture
->delta
= picture
->field_pic_flag
? 1 : 2;
1571 sei
->pic_timing
.present
= 0;
1574 picture
->delta
= picture
->field_pic_flag
? 1 : 2;
1575 if( sei
->recovery_point
.present
)
1577 picture
->random_accessible
|= sei
->recovery_point
.random_accessible
;
1578 picture
->broken_link_flag
|= sei
->recovery_point
.broken_link_flag
;
1579 picture
->recovery_frame_cnt
= sei
->recovery_point
.recovery_frame_cnt
;
1580 sei
->recovery_point
.present
= 0;
1584 int h264_find_au_delimit_by_slice_info
1586 h264_slice_info_t
*slice
,
1587 h264_slice_info_t
*prev_slice
1590 if( slice
->frame_num
!= prev_slice
->frame_num
1591 || ((slice
->pic_order_cnt_type
== 0 && prev_slice
->pic_order_cnt_type
== 0)
1592 && (slice
->pic_order_cnt_lsb
!= prev_slice
->pic_order_cnt_lsb
1593 || slice
->delta_pic_order_cnt_bottom
!= prev_slice
->delta_pic_order_cnt_bottom
))
1594 || ((slice
->pic_order_cnt_type
== 1 && prev_slice
->pic_order_cnt_type
== 1)
1595 && (slice
->delta_pic_order_cnt
[0] != prev_slice
->delta_pic_order_cnt
[0]
1596 || slice
->delta_pic_order_cnt
[1] != prev_slice
->delta_pic_order_cnt
[1]))
1597 || slice
->field_pic_flag
!= prev_slice
->field_pic_flag
1598 || slice
->bottom_field_flag
!= prev_slice
->bottom_field_flag
1599 || slice
->IdrPicFlag
!= prev_slice
->IdrPicFlag
1600 || slice
->pic_parameter_set_id
!= prev_slice
->pic_parameter_set_id
1601 || ((slice
->nal_ref_idc
== 0 || prev_slice
->nal_ref_idc
== 0)
1602 && (slice
->nal_ref_idc
!= prev_slice
->nal_ref_idc
))
1603 || (slice
->IdrPicFlag
== 1 && prev_slice
->IdrPicFlag
== 1
1604 && slice
->idr_pic_id
!= prev_slice
->idr_pic_id
) )
1609 int h264_find_au_delimit_by_nalu_type
1612 uint8_t prev_nalu_type
1615 return ((nalu_type
>= H264_NALU_TYPE_SEI
&& nalu_type
<= H264_NALU_TYPE_AUD
)
1616 || (nalu_type
>= H264_NALU_TYPE_PREFIX
&& nalu_type
<= H264_NALU_TYPE_RSV_NVCL18
))
1617 && ((prev_nalu_type
>= H264_NALU_TYPE_SLICE_N_IDR
&& prev_nalu_type
<= H264_NALU_TYPE_SLICE_IDR
)
1618 || prev_nalu_type
== H264_NALU_TYPE_FD
|| prev_nalu_type
== H264_NALU_TYPE_SLICE_AUX
);
1621 int h264_supplement_buffer
1623 h264_stream_buffer_t
*sb
,
1624 h264_access_unit_t
*au
,
1628 lsmash_multiple_buffers_t
*bank
= lsmash_resize_multiple_buffers( sb
->bank
, size
);
1632 sb
->rbsp
= lsmash_withdraw_buffer( bank
, 1 );
1633 if( au
&& bank
->number_of_buffers
== 3 )
1635 au
->data
= lsmash_withdraw_buffer( bank
, 2 );
1636 au
->incomplete_data
= lsmash_withdraw_buffer( bank
, 3 );
1641 static void h264_bs_put_parameter_sets
1644 lsmash_entry_list_t
*ps_list
,
1645 uint32_t max_ps_count
1648 uint32_t ps_count
= 0;
1649 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
&& ps_count
< max_ps_count
; entry
= entry
->next
)
1651 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1652 if( ps
&& !ps
->unused
)
1654 lsmash_bs_put_be16( bs
, ps
->nalUnitLength
);
1655 lsmash_bs_put_bytes( bs
, ps
->nalUnitLength
, ps
->nalUnit
);
1663 uint8_t *lsmash_create_h264_specific_info
1665 lsmash_h264_specific_parameters_t
*param
,
1666 uint32_t *data_length
1669 if( !param
|| !param
->parameter_sets
|| !data_length
)
1671 if( param
->lengthSizeMinusOne
!= 0 && param
->lengthSizeMinusOne
!= 1 && param
->lengthSizeMinusOne
!= 3 )
1673 static const uint32_t max_ps_count
[3] = { 31, 255, 255 };
1674 lsmash_entry_list_t
*ps_list
[3] =
1676 param
->parameter_sets
->sps_list
, /* SPS */
1677 param
->parameter_sets
->pps_list
, /* PPS */
1678 param
->parameter_sets
->spsext_list
/* SPSExt */
1680 uint32_t ps_count
[3] = { 0, 0, 0 };
1681 /* SPS and PPS are mandatory. */
1682 if( !ps_list
[0] || !ps_list
[0]->head
|| ps_list
[0]->entry_count
== 0
1683 || !ps_list
[1] || !ps_list
[1]->head
|| ps_list
[1]->entry_count
== 0 )
1685 /* Calculate enough buffer size. */
1686 uint32_t buffer_size
= ISOM_BASEBOX_COMMON_SIZE
+ 11;
1687 for( int i
= 0; i
< 3; i
++ )
1689 for( lsmash_entry_t
*entry
= ps_list
[i
]->head
; entry
&& ps_count
[i
] < max_ps_count
[i
]; entry
= entry
->next
)
1691 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1696 buffer_size
+= 2 + ps
->nalUnitLength
;
1699 /* Set up bytestream writer. */
1700 uint8_t buffer
[buffer_size
];
1701 lsmash_bs_t bs
= { 0 };
1702 bs
.buffer
.data
= buffer
;
1703 bs
.buffer
.alloc
= buffer_size
;
1704 /* Create an AVCConfigurationBox */
1705 lsmash_bs_put_be32( &bs
, 0 ); /* box size */
1706 lsmash_bs_put_be32( &bs
, ISOM_BOX_TYPE_AVCC
.fourcc
); /* box type: 'avcC' */
1707 lsmash_bs_put_byte( &bs
, 1 ); /* configurationVersion */
1708 lsmash_bs_put_byte( &bs
, param
->AVCProfileIndication
); /* AVCProfileIndication */
1709 lsmash_bs_put_byte( &bs
, param
->profile_compatibility
); /* profile_compatibility */
1710 lsmash_bs_put_byte( &bs
, param
->AVCLevelIndication
); /* AVCLevelIndication */
1711 lsmash_bs_put_byte( &bs
, param
->lengthSizeMinusOne
| 0xfc ); /* lengthSizeMinusOne */
1712 lsmash_bs_put_byte( &bs
, ps_count
[0] | 0xe0 ); /* numOfSequenceParameterSets */
1713 h264_bs_put_parameter_sets( &bs
, ps_list
[0], ps_count
[0] ); /* sequenceParameterSetLength
1714 * sequenceParameterSetNALUnit */
1715 lsmash_bs_put_byte( &bs
, ps_count
[1] ); /* numOfPictureParameterSets */
1716 h264_bs_put_parameter_sets( &bs
, ps_list
[1], ps_count
[1] ); /* pictureParameterSetLength
1717 * pictureParameterSetNALUnit */
1718 if( H264_REQUIRES_AVCC_EXTENSION( param
->AVCProfileIndication
) )
1720 lsmash_bs_put_byte( &bs
, param
->chroma_format
| 0xfc ); /* chroma_format */
1721 lsmash_bs_put_byte( &bs
, param
->bit_depth_luma_minus8
| 0xf8 ); /* bit_depth_luma_minus8 */
1722 lsmash_bs_put_byte( &bs
, param
->bit_depth_chroma_minus8
| 0xf8 ); /* bit_depth_chroma_minus8 */
1725 lsmash_bs_put_byte( &bs
, ps_count
[2] ); /* numOfSequenceParameterSetExt */
1726 h264_bs_put_parameter_sets( &bs
, ps_list
[2], ps_count
[2] ); /* sequenceParameterSetExtLength
1727 * sequenceParameterSetExtNALUnit */
1729 else /* no sequence parameter set extensions */
1730 lsmash_bs_put_byte( &bs
, 0 ); /* numOfSequenceParameterSetExt */
1732 uint8_t *data
= lsmash_bs_export_data( &bs
, data_length
);
1733 /* Update box size. */
1734 LSMASH_SET_BE32( data
, *data_length
);
1738 static inline int h264_validate_ps_type
1740 lsmash_h264_parameter_set_type ps_type
,
1745 if( !ps_data
|| ps_length
< 2 )
1747 if( ps_type
!= H264_PARAMETER_SET_TYPE_SPS
1748 && ps_type
!= H264_PARAMETER_SET_TYPE_PPS
1749 && ps_type
!= H264_PARAMETER_SET_TYPE_SPSEXT
)
1751 uint8_t nalu_type
= *((uint8_t *)ps_data
) & 0x1f;
1752 if( nalu_type
!= H264_NALU_TYPE_SPS
1753 && nalu_type
!= H264_NALU_TYPE_PPS
1754 && nalu_type
!= H264_NALU_TYPE_SPS_EXT
)
1756 if( (ps_type
== H264_PARAMETER_SET_TYPE_SPS
&& nalu_type
!= H264_NALU_TYPE_SPS
)
1757 || (ps_type
== H264_PARAMETER_SET_TYPE_PPS
&& nalu_type
!= H264_NALU_TYPE_PPS
)
1758 || (ps_type
== H264_PARAMETER_SET_TYPE_SPSEXT
&& nalu_type
!= H264_NALU_TYPE_SPS_EXT
) )
1763 lsmash_dcr_nalu_appendable lsmash_check_h264_parameter_set_appendable
1765 lsmash_h264_specific_parameters_t
*param
,
1766 lsmash_h264_parameter_set_type ps_type
,
1771 uint8_t *ps_data
= _ps_data
;
1773 return DCR_NALU_APPEND_ERROR
;
1774 if( h264_validate_ps_type( ps_type
, ps_data
, ps_length
) )
1775 return DCR_NALU_APPEND_ERROR
;
1776 if( ps_type
== H264_PARAMETER_SET_TYPE_SPSEXT
1777 && !H264_REQUIRES_AVCC_EXTENSION( param
->AVCProfileIndication
) )
1778 return DCR_NALU_APPEND_ERROR
;
1779 /* Check whether the same parameter set already exsits or not. */
1780 lsmash_entry_list_t
*ps_list
= h264_get_parameter_set_list( param
, ps_type
);
1781 if( !ps_list
|| !ps_list
->head
)
1782 return DCR_NALU_APPEND_POSSIBLE
; /* No parameter set */
1783 switch( nalu_check_same_ps_existence( ps_list
, ps_data
, ps_length
) )
1786 case 1 : return DCR_NALU_APPEND_DUPLICATED
; /* The same parameter set already exists. */
1787 default : return DCR_NALU_APPEND_ERROR
; /* An error occured. */
1789 uint32_t max_ps_length
;
1790 if( nalu_get_max_ps_length( ps_list
, &max_ps_length
) )
1791 return DCR_NALU_APPEND_ERROR
;
1792 max_ps_length
= LSMASH_MAX( max_ps_length
, ps_length
);
1794 if( nalu_get_ps_count( ps_list
, &ps_count
) )
1795 return DCR_NALU_APPEND_ERROR
;
1796 if( (ps_type
== H264_PARAMETER_SET_TYPE_SPS
&& ps_count
>= 31)
1797 || (ps_type
== H264_PARAMETER_SET_TYPE_PPS
&& ps_count
>= 255)
1798 || (ps_type
== H264_PARAMETER_SET_TYPE_SPSEXT
&& ps_count
>= 255) )
1799 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
; /* No more appendable parameter sets. */
1800 if( ps_type
== H264_PARAMETER_SET_TYPE_SPSEXT
)
1801 return DCR_NALU_APPEND_POSSIBLE
;
1802 /* Check whether a new specific info is needed or not. */
1803 lsmash_bits_t bits
= { 0 };
1804 lsmash_bs_t bs
= { 0 };
1805 uint8_t rbsp_buffer
[max_ps_length
];
1806 uint8_t buffer
[max_ps_length
];
1807 bs
.buffer
.data
= buffer
;
1808 bs
.buffer
.alloc
= max_ps_length
;
1809 lsmash_bits_init( &bits
, &bs
);
1810 if( ps_type
== H264_PARAMETER_SET_TYPE_PPS
)
1814 if( h264_get_pps_id( ps_data
+ 1, ps_length
- 1, &pps_id
) )
1815 return DCR_NALU_APPEND_ERROR
;
1816 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
; entry
= entry
->next
)
1818 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1820 return DCR_NALU_APPEND_ERROR
;
1823 uint8_t param_pps_id
;
1824 if( h264_get_pps_id( ps
->nalUnit
+ 1, ps
->nalUnitLength
- 1, ¶m_pps_id
) )
1825 return DCR_NALU_APPEND_ERROR
;
1826 if( pps_id
== param_pps_id
)
1827 /* PPS that has the same pic_parameter_set_id already exists with different form. */
1828 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
1830 return DCR_NALU_APPEND_POSSIBLE
;
1834 if( h264_parse_sps_minimally( &bits
, &sps
, rbsp_buffer
, ps_data
+ 1, ps_length
- 1 ) )
1835 return DCR_NALU_APPEND_ERROR
;
1836 lsmash_bits_empty( &bits
);
1837 /* FIXME; If the sequence parameter sets are marked with different profiles,
1838 * and the relevant profile compatibility flags are all zero,
1839 * then the stream may need examination to determine which profile, if any, the stream conforms to.
1840 * If the stream is not examined, or the examination reveals that there is no profile to which the stream conforms,
1841 * then the stream must be split into two or more sub-streams with separate configuration records in which these rules can be met. */
1843 if( sps
.profile_idc
!= param
->AVCProfileIndication
&& (sps
->constraint_set_flags
& param
->profile_compatibility
) )
1845 if( sps
.profile_idc
!= param
->AVCProfileIndication
)
1847 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
1848 /* The values of chroma_format_idc, bit_depth_luma_minus8 and bit_depth_chroma_minus8
1849 * must be identical in all SPSs in a single AVC configuration record. */
1850 if( H264_REQUIRES_AVCC_EXTENSION( param
->AVCProfileIndication
)
1851 && (sps
.chroma_format_idc
!= param
->chroma_format
1852 || sps
.bit_depth_luma_minus8
!= param
->bit_depth_luma_minus8
1853 || sps
.bit_depth_chroma_minus8
!= param
->bit_depth_chroma_minus8
) )
1854 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
1855 /* Forbidden to duplicate SPS that has the same seq_parameter_set_id with different form within the same configuration record. */
1856 uint8_t sps_id
= sps
.seq_parameter_set_id
;
1857 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
; entry
= entry
->next
)
1859 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1861 return DCR_NALU_APPEND_ERROR
;
1864 uint8_t param_sps_id
;
1865 if( h264_get_sps_id( ps
->nalUnit
+ 1, ps
->nalUnitLength
- 1, ¶m_sps_id
) )
1866 return DCR_NALU_APPEND_ERROR
;
1867 if( sps_id
== param_sps_id
)
1868 /* SPS that has the same seq_parameter_set_id already exists with different form. */
1869 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
1870 if( entry
== ps_list
->head
)
1872 /* Check if the visual presentation sizes are different. */
1873 h264_sps_t first_sps
;
1874 if( h264_parse_sps_minimally( &bits
, &first_sps
, rbsp_buffer
,
1876 ps
->nalUnitLength
- 1 ) )
1877 return DCR_NALU_APPEND_ERROR
;
1878 if( sps
.cropped_width
!= first_sps
.cropped_width
1879 || sps
.cropped_height
!= first_sps
.cropped_height
)
1880 return DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED
;
1883 return DCR_NALU_APPEND_POSSIBLE
;
1886 static inline void h264_reorder_parameter_set_ascending_id
1888 lsmash_h264_specific_parameters_t
*param
,
1889 lsmash_h264_parameter_set_type ps_type
,
1890 lsmash_entry_list_t
*ps_list
,
1894 lsmash_entry_t
*entry
= NULL
;
1896 for( int i
= ps_id
- 1; i
; i
-- )
1898 entry
= h264_get_ps_entry_from_param( param
, ps_type
, i
);
1902 int append_head
= 0;
1905 /* Couldn't find any parameter set with lower identifier.
1906 * Next, find parameter set with upper identifier. */
1907 int max_ps_id
= ps_type
== H264_PARAMETER_SET_TYPE_SPS
? 31 : 255;
1908 for( int i
= ps_id
+ 1; i
<= max_ps_id
; i
++ )
1910 entry
= h264_get_ps_entry_from_param( param
, ps_type
, i
);
1918 return; /* The new entry was appended to the tail. */
1919 lsmash_entry_t
*new_entry
= ps_list
->tail
;
1922 /* before: entry[i > ps_id] ... -> prev_entry -> new_entry[ps_id]
1923 * after: new_entry[ps_id] -> entry[i > ps_id] -> ... -> prev_entry */
1924 if( new_entry
->prev
)
1925 new_entry
->prev
->next
= NULL
;
1926 new_entry
->prev
= NULL
;
1927 entry
->prev
= new_entry
;
1928 new_entry
->next
= entry
;
1931 /* before: entry[i < ps_id] -> next_entry -> ... -> prev_entry -> new_entry[ps_id]
1932 * after: entry[i < ps_id] -> new_entry[ps_id] -> next_entry -> ... -> prev_entry */
1933 if( new_entry
->prev
)
1934 new_entry
->prev
->next
= NULL
;
1935 new_entry
->prev
= entry
;
1936 new_entry
->next
= entry
->next
;
1938 entry
->next
->prev
= new_entry
;
1939 entry
->next
= new_entry
;
1942 int lsmash_append_h264_parameter_set
1944 lsmash_h264_specific_parameters_t
*param
,
1945 lsmash_h264_parameter_set_type ps_type
,
1950 uint8_t *ps_data
= _ps_data
;
1951 if( !param
|| !ps_data
|| ps_length
< 2 )
1953 if( !param
->parameter_sets
)
1955 param
->parameter_sets
= lsmash_malloc_zero( sizeof(lsmash_h264_parameter_sets_t
) );
1956 if( !param
->parameter_sets
)
1959 lsmash_entry_list_t
*ps_list
= h264_get_parameter_set_list( param
, ps_type
);
1962 if( ps_type
!= H264_PARAMETER_SET_TYPE_SPS
1963 && ps_type
!= H264_PARAMETER_SET_TYPE_PPS
1964 && ps_type
!= H264_PARAMETER_SET_TYPE_SPSEXT
)
1966 if( ps_type
== H264_PARAMETER_SET_TYPE_SPSEXT
)
1968 if( !H264_REQUIRES_AVCC_EXTENSION( param
->AVCProfileIndication
) )
1970 isom_dcr_ps_entry_t
*ps
= isom_create_ps_entry( ps_data
, ps_length
);
1973 if( lsmash_add_entry( ps_list
, ps
) )
1975 isom_remove_dcr_ps( ps
);
1980 /* Check if the same parameter set identifier already exists. */
1982 if( h264_get_ps_id( ps_data
+ 1, ps_length
- 1, &ps_id
, ps_type
) )
1984 lsmash_entry_t
*entry
= h264_get_ps_entry_from_param( param
, ps_type
, ps_id
);
1985 isom_dcr_ps_entry_t
*ps
= entry
? (isom_dcr_ps_entry_t
*)entry
->data
: NULL
;
1986 if( ps
&& !ps
->unused
)
1987 /* The same parameter set identifier already exists. */
1992 /* Reuse an already existed parameter set in the list. */
1994 if( ps
->nalUnit
!= ps_data
)
1996 /* The same address could be given when called by h264_update_picture_info_for_slice(). */
1997 lsmash_free( ps
->nalUnit
);
1998 ps
->nalUnit
= ps_data
;
2000 ps
->nalUnitLength
= ps_length
;
2005 /* Create a new parameter set and append it into the list. */
2006 ps
= isom_create_ps_entry( ps_data
, ps_length
);
2009 if( lsmash_add_entry( ps_list
, ps
) )
2011 isom_remove_dcr_ps( ps
);
2016 if( ps_type
== H264_PARAMETER_SET_TYPE_SPS
)
2018 /* Update specific info with SPS. */
2019 lsmash_bits_t bits
= { 0 };
2020 lsmash_bs_t bs
= { 0 };
2021 uint8_t rbsp_buffer
[ps_length
];
2022 uint8_t buffer
[ps_length
];
2023 bs
.buffer
.data
= buffer
;
2024 bs
.buffer
.alloc
= ps_length
;
2025 lsmash_bits_init( &bits
, &bs
);
2027 if( h264_parse_sps_minimally( &bits
, &sps
, rbsp_buffer
, ps_data
+ 1, ps_length
- 1 ) )
2029 lsmash_remove_entry_tail( ps_list
, isom_remove_dcr_ps
);
2032 if( ps_list
->entry_count
== 1 )
2033 param
->profile_compatibility
= 0xff;
2034 param
->AVCProfileIndication
= sps
.profile_idc
;
2035 param
->profile_compatibility
&= sps
.constraint_set_flags
;
2036 param
->AVCLevelIndication
= LSMASH_MAX( param
->AVCLevelIndication
, sps
.level_idc
);
2037 param
->chroma_format
= sps
.chroma_format_idc
;
2038 param
->bit_depth_luma_minus8
= sps
.bit_depth_luma_minus8
;
2039 param
->bit_depth_chroma_minus8
= sps
.bit_depth_chroma_minus8
;
2041 if( invoke_reorder
)
2042 /* Add a new parameter set in order of ascending parameter set identifier. */
2043 h264_reorder_parameter_set_ascending_id( param
, ps_type
, ps_list
, ps_id
);
2047 int h264_try_to_append_parameter_set
2050 lsmash_h264_parameter_set_type ps_type
,
2055 uint8_t *ps_data
= _ps_data
;
2056 lsmash_dcr_nalu_appendable ret
= lsmash_check_h264_parameter_set_appendable( &info
->avcC_param
, ps_type
, ps_data
, ps_length
);
2057 lsmash_h264_specific_parameters_t
*param
;
2060 case DCR_NALU_APPEND_ERROR
: /* Error */
2062 case DCR_NALU_APPEND_NEW_DCR_REQUIRED
: /* Mulitiple sample description is needed. */
2063 case DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED
: /* Mulitiple sample description is needed. */
2064 param
= &info
->avcC_param_next
;
2065 info
->avcC_pending
= 1;
2067 case DCR_NALU_APPEND_POSSIBLE
: /* Appendable */
2068 param
= info
->avcC_pending
? &info
->avcC_param_next
: &info
->avcC_param
;
2070 default : /* No need to append */
2075 case H264_PARAMETER_SET_TYPE_SPS
:
2076 if( h264_parse_sps( info
, info
->buffer
.rbsp
, ps_data
+ 1, ps_length
- 1 ) < 0 )
2079 case H264_PARAMETER_SET_TYPE_PPS
:
2080 if( h264_parse_pps( info
, info
->buffer
.rbsp
, ps_data
+ 1, ps_length
- 1 ) < 0 )
2086 return lsmash_append_h264_parameter_set( param
, ps_type
, ps_data
, ps_length
);
2089 static inline int h264_move_dcr_nalu_entry
2091 lsmash_h264_specific_parameters_t
*dst_data
,
2092 lsmash_h264_specific_parameters_t
*src_data
,
2093 lsmash_h264_parameter_set_type ps_type
2096 lsmash_entry_list_t
*src_ps_list
= h264_get_parameter_set_list( src_data
, ps_type
);
2097 lsmash_entry_list_t
*dst_ps_list
= h264_get_parameter_set_list( dst_data
, ps_type
);
2098 assert( src_ps_list
&& dst_ps_list
);
2099 for( lsmash_entry_t
*src_entry
= src_ps_list
->head
; src_entry
; src_entry
= src_entry
->next
)
2101 isom_dcr_ps_entry_t
*src_ps
= (isom_dcr_ps_entry_t
*)src_entry
->data
;
2105 if( h264_get_ps_id( src_ps
->nalUnit
+ 1, src_ps
->nalUnitLength
- 1, &src_ps_id
, ps_type
) < 0 )
2107 lsmash_entry_t
*dst_entry
;
2108 for( dst_entry
= dst_ps_list
->head
; dst_entry
; dst_entry
= dst_entry
->next
)
2110 isom_dcr_ps_entry_t
*dst_ps
= (isom_dcr_ps_entry_t
*)dst_entry
->data
;
2114 if( h264_get_ps_id( dst_ps
->nalUnit
+ 1, dst_ps
->nalUnitLength
- 1, &dst_ps_id
, ps_type
) < 0 )
2116 if( dst_ps_id
== src_ps_id
)
2118 /* Replace the old parameter set with the new one. */
2119 assert( dst_entry
->data
!= src_entry
->data
);
2120 isom_remove_dcr_ps( dst_ps
);
2121 dst_entry
->data
= src_entry
->data
;
2122 src_entry
->data
= NULL
;
2128 /* Move the parameter set. */
2129 if( lsmash_add_entry( dst_ps_list
, src_ps
) )
2131 src_entry
->data
= NULL
;
2137 int h264_move_pending_avcC_param
2143 if( !info
->avcC_pending
)
2145 /* Mark 'unused' on parameter sets within the decoder configuration record. */
2146 for( int i
= 0; i
< H264_PARAMETER_SET_TYPE_NUM
; i
++ )
2148 lsmash_entry_list_t
*ps_list
= h264_get_parameter_set_list( &info
->avcC_param
, i
);
2150 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
; entry
= entry
->next
)
2152 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
2158 /* Move the new parameter sets. */
2159 if( h264_move_dcr_nalu_entry( &info
->avcC_param
, &info
->avcC_param_next
, H264_PARAMETER_SET_TYPE_SPS
) < 0
2160 || h264_move_dcr_nalu_entry( &info
->avcC_param
, &info
->avcC_param_next
, H264_PARAMETER_SET_TYPE_PPS
) < 0 )
2162 /* Move to the pending. */
2163 lsmash_h264_parameter_sets_t
*parameter_sets
= info
->avcC_param
.parameter_sets
; /* Back up parameter sets. */
2164 info
->avcC_param
= info
->avcC_param_next
;
2165 info
->avcC_param
.parameter_sets
= parameter_sets
;
2166 /* No pending avcC. */
2167 lsmash_destroy_h264_parameter_sets( &info
->avcC_param_next
);
2168 memset( &info
->avcC_param_next
, 0, sizeof(lsmash_h264_specific_parameters_t
) );
2169 info
->avcC_pending
= 0;
2173 static int h264_parse_succeeded
2176 lsmash_h264_specific_parameters_t
*param
2180 if( info
->sps
.present
&& info
->pps
.present
)
2182 *param
= info
->avcC_param
;
2183 /* Avoid freeing parameter sets. */
2184 info
->avcC_param
.parameter_sets
= NULL
;
2189 h264_cleanup_parser( info
);
2193 static inline int h264_parse_failed
2198 h264_cleanup_parser( info
);
2202 int lsmash_setup_h264_specific_parameters_from_access_unit
2204 lsmash_h264_specific_parameters_t
*param
,
2206 uint32_t data_length
2209 if( !param
|| !data
|| data_length
== 0 )
2211 h264_info_t
*info
= &(h264_info_t
){ { 0 } };
2212 lsmash_bs_t
*bs
= &(lsmash_bs_t
){ 0 };
2213 if( lsmash_bs_set_empty_stream( bs
, data
, data_length
) < 0 )
2215 uint64_t sc_head_pos
= nalu_find_first_start_code( bs
);
2216 if( sc_head_pos
== NALU_NO_START_CODE_FOUND
)
2218 if( h264_setup_parser( info
, 1 ) < 0 )
2219 return h264_parse_failed( info
);
2220 h264_stream_buffer_t
*sb
= &info
->buffer
;
2221 h264_slice_info_t
*slice
= &info
->slice
;
2224 h264_nalu_header_t nuh
;
2225 uint64_t start_code_length
;
2226 uint64_t trailing_zero_bytes
;
2227 uint64_t nalu_length
= h264_find_next_start_code( bs
, &nuh
, &start_code_length
, &trailing_zero_bytes
);
2228 if( start_code_length
<= NALU_SHORT_START_CODE_LENGTH
&& lsmash_bs_is_end( bs
, nalu_length
) )
2229 /* For the last NALU. This NALU already has been parsed. */
2230 return h264_parse_succeeded( info
, param
);
2231 uint8_t nalu_type
= nuh
.nal_unit_type
;
2232 uint64_t next_sc_head_pos
= sc_head_pos
2235 + trailing_zero_bytes
;
2236 if( nalu_type
== H264_NALU_TYPE_FD
)
2238 /* We don't support streams with both filler and HRD yet.
2239 * Otherwise, just skip filler because elemental streams defined in 14496-15 are forbidden to use filler. */
2240 if( info
->sps
.vui
.hrd
.present
)
2241 return h264_parse_failed( info
);
2243 else if( (nalu_type
>= H264_NALU_TYPE_SLICE_N_IDR
&& nalu_type
<= H264_NALU_TYPE_SPS_EXT
)
2244 || nalu_type
== H264_NALU_TYPE_SLICE_AUX
)
2246 /* Increase the buffer if needed. */
2247 uint64_t possible_au_length
= NALU_DEFAULT_NALU_LENGTH_SIZE
+ nalu_length
;
2248 if( sb
->bank
->buffer_size
< possible_au_length
2249 && h264_supplement_buffer( sb
, NULL
, 2 * possible_au_length
) < 0 )
2250 return h264_parse_failed( info
);
2251 /* Get the EBSP of the current NALU here.
2252 * AVC elemental stream defined in 14496-15 can recognize from 0 to 13, and 19 of nal_unit_type.
2253 * We don't support SVC and MVC elemental stream defined in 14496-15 yet. */
2254 uint8_t *nalu
= lsmash_bs_get_buffer_data( bs
) + start_code_length
;
2255 if( nalu_type
>= H264_NALU_TYPE_SLICE_N_IDR
&& nalu_type
<= H264_NALU_TYPE_SLICE_IDR
)
2257 /* VCL NALU (slice) */
2258 h264_slice_info_t prev_slice
= *slice
;
2259 if( h264_parse_slice( info
, &nuh
, sb
->rbsp
, nalu
+ nuh
.length
, nalu_length
- nuh
.length
) )
2260 return h264_parse_failed( info
);
2261 if( prev_slice
.present
)
2263 /* Check whether the AU that contains the previous VCL NALU completed or not. */
2264 if( h264_find_au_delimit_by_slice_info( slice
, &prev_slice
) )
2265 /* The current NALU is the first VCL NALU of the primary coded picture of an new AU.
2266 * Therefore, the previous slice belongs to that new AU. */
2267 return h264_parse_succeeded( info
, param
);
2273 if( h264_find_au_delimit_by_nalu_type( nalu_type
, info
->prev_nalu_type
) )
2274 /* The last slice belongs to the AU you want at this time. */
2275 return h264_parse_succeeded( info
, param
);
2278 case H264_NALU_TYPE_SPS
:
2279 if( h264_try_to_append_parameter_set( info
, H264_PARAMETER_SET_TYPE_SPS
, nalu
, nalu_length
) )
2280 return h264_parse_failed( info
);
2282 case H264_NALU_TYPE_PPS
:
2283 if( h264_try_to_append_parameter_set( info
, H264_PARAMETER_SET_TYPE_PPS
, nalu
, nalu_length
) )
2284 return h264_parse_failed( info
);
2286 case H264_NALU_TYPE_SPS_EXT
:
2287 if( h264_try_to_append_parameter_set( info
, H264_PARAMETER_SET_TYPE_SPSEXT
, nalu
, nalu_length
) )
2288 return h264_parse_failed( info
);
2295 /* Move to the first byte of the next start code. */
2296 info
->prev_nalu_type
= nalu_type
;
2297 if( lsmash_bs_read_seek( bs
, next_sc_head_pos
, SEEK_SET
) != next_sc_head_pos
)
2298 return h264_parse_failed( info
);
2299 /* Check if no more data to read from the stream. */
2300 if( !lsmash_bs_is_end( bs
, NALU_SHORT_START_CODE_LENGTH
) )
2301 sc_head_pos
= next_sc_head_pos
;
2303 return h264_parse_succeeded( info
, param
);
2307 int h264_construct_specific_parameters
2309 lsmash_codec_specific_t
*dst
,
2310 lsmash_codec_specific_t
*src
2313 assert( dst
&& dst
->data
.structured
&& src
&& src
->data
.unstructured
);
2314 if( src
->size
< ISOM_BASEBOX_COMMON_SIZE
+ 7 )
2316 lsmash_h264_specific_parameters_t
*param
= (lsmash_h264_specific_parameters_t
*)dst
->data
.structured
;
2317 uint8_t *data
= src
->data
.unstructured
;
2318 uint64_t size
= LSMASH_GET_BE32( data
);
2319 data
+= ISOM_BASEBOX_COMMON_SIZE
;
2322 size
= LSMASH_GET_BE64( data
);
2325 if( size
!= src
->size
)
2327 if( !param
->parameter_sets
)
2329 param
->parameter_sets
= lsmash_malloc_zero( sizeof(lsmash_h264_parameter_sets_t
) );
2330 if( !param
->parameter_sets
)
2333 lsmash_bs_t
*bs
= lsmash_bs_create();
2336 if( lsmash_bs_import_data( bs
, data
, src
->size
- (data
- src
->data
.unstructured
) ) )
2338 if( lsmash_bs_get_byte( bs
) != 1 )
2339 goto fail
; /* We don't support configurationVersion other than 1. */
2340 param
->AVCProfileIndication
= lsmash_bs_get_byte( bs
);
2341 param
->profile_compatibility
= lsmash_bs_get_byte( bs
);
2342 param
->AVCLevelIndication
= lsmash_bs_get_byte( bs
);
2343 param
->lengthSizeMinusOne
= lsmash_bs_get_byte( bs
) & 0x03;
2344 uint8_t numOfSequenceParameterSets
= lsmash_bs_get_byte( bs
) & 0x1F;
2345 if( numOfSequenceParameterSets
2346 && nalu_get_dcr_ps( bs
, param
->parameter_sets
->sps_list
, numOfSequenceParameterSets
) )
2348 uint8_t numOfPictureParameterSets
= lsmash_bs_get_byte( bs
);
2349 if( numOfPictureParameterSets
2350 && nalu_get_dcr_ps( bs
, param
->parameter_sets
->pps_list
, numOfPictureParameterSets
) )
2352 if( H264_REQUIRES_AVCC_EXTENSION( param
->AVCProfileIndication
) )
2354 param
->chroma_format
= lsmash_bs_get_byte( bs
) & 0x03;
2355 param
->bit_depth_luma_minus8
= lsmash_bs_get_byte( bs
) & 0x07;
2356 param
->bit_depth_chroma_minus8
= lsmash_bs_get_byte( bs
) & 0x07;
2357 uint8_t numOfSequenceParameterSetExt
= lsmash_bs_get_byte( bs
);
2358 if( numOfSequenceParameterSetExt
2359 && nalu_get_dcr_ps( bs
, param
->parameter_sets
->spsext_list
, numOfSequenceParameterSetExt
) )
2362 lsmash_bs_cleanup( bs
);
2365 lsmash_bs_cleanup( bs
);
2369 int h264_print_codec_specific
2372 lsmash_file_t
*file
,
2377 assert( fp
&& file
&& box
&& (box
->manager
& LSMASH_BINARY_CODED_BOX
) );
2379 lsmash_ifprintf( fp
, indent
++, "[%s: AVC Configuration Box]\n", isom_4cc2str( box
->type
.fourcc
) );
2380 lsmash_ifprintf( fp
, indent
, "position = %"PRIu64
"\n", box
->pos
);
2381 lsmash_ifprintf( fp
, indent
, "size = %"PRIu64
"\n", box
->size
);
2382 uint8_t *data
= box
->binary
;
2383 uint32_t offset
= isom_skip_box_common( &data
);
2384 lsmash_bs_t
*bs
= lsmash_bs_create();
2387 if( lsmash_bs_import_data( bs
, data
, box
->size
- offset
) )
2389 lsmash_bs_cleanup( bs
);
2392 lsmash_ifprintf( fp
, indent
, "configurationVersion = %"PRIu8
"\n", lsmash_bs_get_byte( bs
) );
2393 uint8_t AVCProfileIndication
= lsmash_bs_get_byte( bs
);
2394 lsmash_ifprintf( fp
, indent
, "AVCProfileIndication = %"PRIu8
"\n", AVCProfileIndication
);
2395 lsmash_ifprintf( fp
, indent
, "profile_compatibility = 0x%02"PRIx8
"\n", lsmash_bs_get_byte( bs
) );
2396 lsmash_ifprintf( fp
, indent
, "AVCLevelIndication = %"PRIu8
"\n", lsmash_bs_get_byte( bs
) );
2397 uint8_t temp8
= lsmash_bs_get_byte( bs
);
2398 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 2) & 0x3F );
2399 lsmash_ifprintf( fp
, indent
, "lengthSizeMinusOne = %"PRIu8
"\n", temp8
& 0x03 );
2400 temp8
= lsmash_bs_get_byte( bs
);
2401 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 5) & 0x07 );
2402 uint8_t numOfSequenceParameterSets
= temp8
& 0x1f;
2403 lsmash_ifprintf( fp
, indent
, "numOfSequenceParameterSets = %"PRIu8
"\n", numOfSequenceParameterSets
);
2404 for( uint8_t i
= 0; i
< numOfSequenceParameterSets
; i
++ )
2406 uint16_t nalUnitLength
= lsmash_bs_get_be16( bs
);
2407 lsmash_bs_skip_bytes( bs
, nalUnitLength
);
2409 uint8_t numOfPictureParameterSets
= lsmash_bs_get_byte( bs
);
2410 lsmash_ifprintf( fp
, indent
, "numOfPictureParameterSets = %"PRIu8
"\n", numOfPictureParameterSets
);
2411 for( uint8_t i
= 0; i
< numOfPictureParameterSets
; i
++ )
2413 uint16_t nalUnitLength
= lsmash_bs_get_be16( bs
);
2414 lsmash_bs_skip_bytes( bs
, nalUnitLength
);
2416 /* Note: there are too many files, in the world, that don't contain the following fields. */
2417 if( H264_REQUIRES_AVCC_EXTENSION( AVCProfileIndication
)
2418 && (lsmash_bs_get_pos( bs
) < (box
->size
- offset
)) )
2420 temp8
= lsmash_bs_get_byte( bs
);
2421 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 2) & 0x3F );
2422 lsmash_ifprintf( fp
, indent
, "chroma_format = %"PRIu8
"\n", temp8
& 0x03 );
2423 temp8
= lsmash_bs_get_byte( bs
);
2424 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 3) & 0x1F );
2425 lsmash_ifprintf( fp
, indent
, "bit_depth_luma_minus8 = %"PRIu8
"\n", temp8
& 0x7 );
2426 temp8
= lsmash_bs_get_byte( bs
);
2427 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 3) & 0x1F );
2428 lsmash_ifprintf( fp
, indent
, "bit_depth_chroma_minus8 = %"PRIu8
"\n", temp8
& 0x7 );
2429 lsmash_ifprintf( fp
, indent
, "numOfSequenceParameterSetExt = %"PRIu8
"\n", lsmash_bs_get_byte( bs
) );
2431 lsmash_bs_cleanup( bs
);
2435 int h264_copy_codec_specific
2437 lsmash_codec_specific_t
*dst
,
2438 lsmash_codec_specific_t
*src
2441 assert( src
&& src
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
&& src
->data
.structured
);
2442 assert( dst
&& dst
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
&& dst
->data
.structured
);
2443 lsmash_h264_specific_parameters_t
*src_data
= (lsmash_h264_specific_parameters_t
*)src
->data
.structured
;
2444 lsmash_h264_specific_parameters_t
*dst_data
= (lsmash_h264_specific_parameters_t
*)dst
->data
.structured
;
2445 lsmash_destroy_h264_parameter_sets( dst_data
);
2446 *dst_data
= *src_data
;
2447 if( !src_data
->parameter_sets
)
2449 dst_data
->parameter_sets
= lsmash_malloc_zero( sizeof(lsmash_h264_parameter_sets_t
) );
2450 if( !dst_data
->parameter_sets
)
2452 for( int i
= 0; i
< 3; i
++ )
2454 lsmash_entry_list_t
*src_ps_list
= h264_get_parameter_set_list( src_data
, i
);
2455 lsmash_entry_list_t
*dst_ps_list
= h264_get_parameter_set_list( dst_data
, i
);
2456 assert( src_ps_list
&& dst_ps_list
);
2457 for( lsmash_entry_t
*entry
= src_ps_list
->head
; entry
; entry
= entry
->next
)
2459 isom_dcr_ps_entry_t
*src_ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
2460 if( !src_ps
|| src_ps
->unused
)
2462 isom_dcr_ps_entry_t
*dst_ps
= isom_create_ps_entry( src_ps
->nalUnit
, src_ps
->nalUnitLength
);
2465 lsmash_destroy_h264_parameter_sets( dst_data
);
2468 if( lsmash_add_entry( dst_ps_list
, dst_ps
) )
2470 lsmash_destroy_h264_parameter_sets( dst_data
);
2471 isom_remove_dcr_ps( dst_ps
);
2479 int h264_print_bitrate
2482 lsmash_file_t
*file
,
2487 assert( fp
&& file
&& box
);
2489 lsmash_ifprintf( fp
, indent
++, "[%s: MPEG-4 Bit Rate Box]\n", isom_4cc2str( box
->type
.fourcc
) );
2490 lsmash_ifprintf( fp
, indent
, "position = %"PRIu64
"\n", box
->pos
);
2491 lsmash_ifprintf( fp
, indent
, "size = %"PRIu64
"\n", box
->size
);
2492 isom_btrt_t
*btrt
= (isom_btrt_t
*)box
;
2493 lsmash_ifprintf( fp
, indent
, "bufferSizeDB = %"PRIu32
"\n", btrt
->bufferSizeDB
);
2494 lsmash_ifprintf( fp
, indent
, "maxBitrate = %"PRIu32
"\n", btrt
->maxBitrate
);
2495 lsmash_ifprintf( fp
, indent
, "avgBitrate = %"PRIu32
"\n", btrt
->avgBitrate
);