1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2013-2017 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.265 (04/13)
34 ***************************************************************************/
38 #define IF_EXCEED_INT32( x ) if( (x) < INT32_MIN || (x) > INT32_MAX )
39 #define HEVC_POC_DEBUG_PRINT 0
41 #define HEVC_MIN_NALU_HEADER_LENGTH 2
42 #define HEVC_MAX_VPS_ID 15
43 #define HEVC_MAX_SPS_ID 15
44 #define HEVC_MAX_PPS_ID 63
45 #define HEVC_MAX_DPB_SIZE 16
46 #define HVCC_CONFIGURATION_VERSION 1
50 HEVC_SLICE_TYPE_B
= 0,
51 HEVC_SLICE_TYPE_P
= 1,
52 HEVC_SLICE_TYPE_I
= 2,
55 static lsmash_hevc_parameter_arrays_t
*hevc_alloc_parameter_arrays( void )
57 lsmash_hevc_parameter_arrays_t
*parameter_arrays
= lsmash_malloc_zero( sizeof(lsmash_hevc_parameter_arrays_t
) );
58 if( !parameter_arrays
)
60 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_VPS
].array_completeness
= 1;
61 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_VPS
].NAL_unit_type
= HEVC_NALU_TYPE_VPS
;
62 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_SPS
].array_completeness
= 1;
63 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_SPS
].NAL_unit_type
= HEVC_NALU_TYPE_SPS
;
64 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_PPS
].array_completeness
= 1;
65 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_PPS
].NAL_unit_type
= HEVC_NALU_TYPE_PPS
;
66 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_PREFIX_SEI
].array_completeness
= 0;
67 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_PREFIX_SEI
].NAL_unit_type
= HEVC_NALU_TYPE_PREFIX_SEI
;
68 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_SUFFIX_SEI
].array_completeness
= 0;
69 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_SUFFIX_SEI
].NAL_unit_type
= HEVC_NALU_TYPE_SUFFIX_SEI
;
70 for( int i
= 0; i
< HEVC_DCR_NALU_TYPE_NUM
; i
++ )
71 lsmash_list_init( parameter_arrays
->ps_array
[i
].list
, isom_remove_dcr_ps
);
72 return parameter_arrays
;
75 static int hevc_alloc_parameter_arrays_if_needed
77 lsmash_hevc_specific_parameters_t
*param
81 if( param
->parameter_arrays
)
83 lsmash_hevc_parameter_arrays_t
*parameter_arrays
= hevc_alloc_parameter_arrays();
84 if( !parameter_arrays
)
85 return LSMASH_ERR_MEMORY_ALLOC
;
86 param
->parameter_arrays
= parameter_arrays
;
90 static void hevc_deallocate_parameter_arrays
92 lsmash_hevc_specific_parameters_t
*param
95 if( !param
|| !param
->parameter_arrays
)
97 for( int i
= 0; i
< HEVC_DCR_NALU_TYPE_NUM
; i
++ )
98 lsmash_list_remove_entries( param
->parameter_arrays
->ps_array
[i
].list
);
99 lsmash_freep( ¶m
->parameter_arrays
);
102 void lsmash_destroy_hevc_parameter_arrays
104 lsmash_hevc_specific_parameters_t
*param
107 hevc_deallocate_parameter_arrays( param
);
110 void hevc_destruct_specific_data
117 hevc_deallocate_parameter_arrays( data
);
121 static void hevc_remove_pps
128 lsmash_free( pps
->colWidth
);
129 lsmash_free( pps
->rowHeight
);
133 void hevc_cleanup_parser
140 lsmash_list_remove_entries( info
->vps_list
);
141 lsmash_list_remove_entries( info
->sps_list
);
142 lsmash_list_remove_entries( info
->pps_list
);
143 hevc_deallocate_parameter_arrays( &info
->hvcC_param
);
144 hevc_deallocate_parameter_arrays( &info
->hvcC_param_next
);
145 lsmash_destroy_multiple_buffers( info
->buffer
.bank
);
146 lsmash_bits_adhoc_cleanup( info
->bits
);
150 int hevc_setup_parser
157 memset( info
, 0, sizeof(hevc_info_t
) );
158 info
->hvcC_param
.lengthSizeMinusOne
= NALU_DEFAULT_NALU_LENGTH_SIZE
- 1;
159 info
->hvcC_param_next
.lengthSizeMinusOne
= NALU_DEFAULT_NALU_LENGTH_SIZE
- 1;
160 hevc_stream_buffer_t
*sb
= &info
->buffer
;
161 sb
->bank
= lsmash_create_multiple_buffers( parse_only
? 1 : 3, NALU_DEFAULT_BUFFER_SIZE
);
163 return LSMASH_ERR_MEMORY_ALLOC
;
164 sb
->rbsp
= lsmash_withdraw_buffer( sb
->bank
, 1 );
167 info
->au
.data
= lsmash_withdraw_buffer( sb
->bank
, 2 );
168 info
->au
.incomplete_data
= lsmash_withdraw_buffer( sb
->bank
, 3 );
170 info
->bits
= lsmash_bits_adhoc_create();
173 lsmash_destroy_multiple_buffers( sb
->bank
);
174 return LSMASH_ERR_MEMORY_ALLOC
;
176 lsmash_list_init_simple( info
->vps_list
);
177 lsmash_list_init_simple( info
->sps_list
);
178 lsmash_list_init( info
->pps_list
, hevc_remove_pps
);
179 info
->prev_nalu_type
= HEVC_NALU_TYPE_UNKNOWN
;
183 static int hevc_check_nalu_header
186 hevc_nalu_header_t
*nuh
,
187 int use_long_start_code
190 /* Check if the enough length of NALU header on the buffer. */
191 int start_code_length
= use_long_start_code
? NALU_LONG_START_CODE_LENGTH
: NALU_SHORT_START_CODE_LENGTH
;
192 if( lsmash_bs_is_end( bs
, start_code_length
+ 1 ) )
193 return LSMASH_ERR_NAMELESS
;
194 /* Read NALU header. */
195 uint16_t temp16
= lsmash_bs_show_be16( bs
, start_code_length
);
196 nuh
->forbidden_zero_bit
= (temp16
>> 15) & 0x01;
197 nuh
->nal_unit_type
= (temp16
>> 9) & 0x3f;
198 nuh
->nuh_layer_id
= (temp16
>> 3) & 0x3f;
199 uint8_t nuh_temporal_id_plus1
= temp16
& 0x07;
200 if( nuh
->forbidden_zero_bit
|| nuh_temporal_id_plus1
== 0 )
201 return LSMASH_ERR_INVALID_DATA
;
202 nuh
->TemporalId
= nuh_temporal_id_plus1
- 1;
203 nuh
->length
= HEVC_MIN_NALU_HEADER_LENGTH
;
204 /* nuh_layer_id shall be 0 in the specification we refer to. */
205 if( nuh
->nuh_layer_id
)
206 return LSMASH_ERR_NAMELESS
;
207 if( nuh
->TemporalId
== 0 )
209 /* For TSA_N, TSA_R, STSA_N and STSA_R, TemporalId shall not be equal to 0. */
210 if( nuh
->nal_unit_type
>= HEVC_NALU_TYPE_TSA_N
211 && nuh
->nal_unit_type
<= HEVC_NALU_TYPE_STSA_R
)
212 return LSMASH_ERR_INVALID_DATA
;
216 /* For BLA_W_LP to RSV_IRAP_VCL23, TemporalId shall be equal to 0. */
217 if( nuh
->nal_unit_type
>= HEVC_NALU_TYPE_BLA_W_LP
218 && nuh
->nal_unit_type
<= HEVC_NALU_TYPE_RSV_IRAP_VCL23
)
219 return LSMASH_ERR_INVALID_DATA
;
220 /* For VPS, SPS, EOS and EOB, TemporalId shall be equal to 0. */
221 if( nuh
->nal_unit_type
>= HEVC_NALU_TYPE_VPS
222 && nuh
->nal_unit_type
<= HEVC_NALU_TYPE_EOB
223 && nuh
->nal_unit_type
!= HEVC_NALU_TYPE_PPS
224 && nuh
->nal_unit_type
!= HEVC_NALU_TYPE_AUD
)
225 return LSMASH_ERR_INVALID_DATA
;
227 /* VPS, SPS and PPS require long start code (0x00000001).
228 * Also AU delimiter requires it too because this type of NALU shall be the first NALU of any AU if present. */
229 if( !use_long_start_code
230 && nuh
->nal_unit_type
>= HEVC_NALU_TYPE_VPS
231 && nuh
->nal_unit_type
<= HEVC_NALU_TYPE_AUD
)
232 return LSMASH_ERR_INVALID_DATA
;
236 uint64_t hevc_find_next_start_code
239 hevc_nalu_header_t
*nuh
,
240 uint64_t *start_code_length
,
241 uint64_t *trailing_zero_bytes
244 uint64_t length
= 0; /* the length of the latest NALU */
245 uint64_t count
= 0; /* the number of the trailing zero bytes after the latest NALU */
246 /* Check the type of the current start code. */
248 = (!lsmash_bs_is_end( bs
, NALU_LONG_START_CODE_LENGTH
) && 0x00000001 == lsmash_bs_show_be32( bs
, 0 )) ? 1
249 : (!lsmash_bs_is_end( bs
, NALU_SHORT_START_CODE_LENGTH
) && 0x000001 == lsmash_bs_show_be24( bs
, 0 )) ? 0
251 if( long_start_code
>= 0 && hevc_check_nalu_header( bs
, nuh
, long_start_code
) == 0 )
253 *start_code_length
= long_start_code
? NALU_LONG_START_CODE_LENGTH
: NALU_SHORT_START_CODE_LENGTH
;
254 uint64_t distance
= *start_code_length
+ nuh
->length
;
255 /* Find the start code of the next NALU and get the distance from the start code of the latest NALU. */
256 if( !lsmash_bs_is_end( bs
, distance
+ NALU_SHORT_START_CODE_LENGTH
) )
258 uint32_t sync_bytes
= lsmash_bs_show_be24( bs
, distance
);
259 while( 0x000001 != sync_bytes
)
261 if( lsmash_bs_is_end( bs
, ++distance
+ NALU_SHORT_START_CODE_LENGTH
) )
263 distance
= lsmash_bs_get_remaining_buffer_size( bs
);
267 sync_bytes
|= lsmash_bs_show_byte( bs
, distance
+ NALU_SHORT_START_CODE_LENGTH
- 1 );
268 sync_bytes
&= 0xFFFFFF;
272 distance
= lsmash_bs_get_remaining_buffer_size( bs
);
273 /* Any NALU has no consecutive zero bytes at the end. */
274 while( 0x00 == lsmash_bs_show_byte( bs
, distance
- 1 ) )
279 /* Remove the length of the start code. */
280 length
= distance
- *start_code_length
;
281 /* If there are one or more trailing zero bytes, we treat the last one byte as a part of the next start code.
282 * This makes the next start code a long start code. */
289 nuh
->forbidden_zero_bit
= 1; /* shall be 0, so invalid */
290 nuh
->nal_unit_type
= HEVC_NALU_TYPE_UNKNOWN
;
291 nuh
->nuh_layer_id
= 0; /* arbitrary */
292 nuh
->TemporalId
= 0; /* arbitrary */
294 *start_code_length
= 0;
295 length
= NALU_NO_START_CODE_FOUND
;
297 *trailing_zero_bytes
= count
;
301 static hevc_vps_t
*hevc_get_vps
303 lsmash_entry_list_t
*vps_list
,
307 if( !vps_list
|| vps_id
> HEVC_MAX_VPS_ID
)
309 for( lsmash_entry_t
*entry
= vps_list
->head
; entry
; entry
= entry
->next
)
311 hevc_vps_t
*vps
= (hevc_vps_t
*)entry
->data
;
314 if( vps
->video_parameter_set_id
== vps_id
)
317 hevc_vps_t
*vps
= lsmash_malloc_zero( sizeof(hevc_vps_t
) );
320 vps
->video_parameter_set_id
= vps_id
;
321 if( lsmash_list_add_entry( vps_list
, vps
) < 0 )
329 static hevc_sps_t
*hevc_get_sps
331 lsmash_entry_list_t
*sps_list
,
335 if( !sps_list
|| sps_id
> HEVC_MAX_SPS_ID
)
337 for( lsmash_entry_t
*entry
= sps_list
->head
; entry
; entry
= entry
->next
)
339 hevc_sps_t
*sps
= (hevc_sps_t
*)entry
->data
;
342 if( sps
->seq_parameter_set_id
== sps_id
)
345 hevc_sps_t
*sps
= lsmash_malloc_zero( sizeof(hevc_sps_t
) );
348 sps
->seq_parameter_set_id
= sps_id
;
349 if( lsmash_list_add_entry( sps_list
, sps
) < 0 )
357 static hevc_pps_t
*hevc_get_pps
359 lsmash_entry_list_t
*pps_list
,
363 if( !pps_list
|| pps_id
> HEVC_MAX_PPS_ID
)
365 for( lsmash_entry_t
*entry
= pps_list
->head
; entry
; entry
= entry
->next
)
367 hevc_pps_t
*pps
= (hevc_pps_t
*)entry
->data
;
370 if( pps
->pic_parameter_set_id
== pps_id
)
373 hevc_pps_t
*pps
= lsmash_malloc_zero( sizeof(hevc_pps_t
) );
376 pps
->pic_parameter_set_id
= pps_id
;
377 if( lsmash_list_add_entry( pps_list
, pps
) < 0 )
385 int hevc_calculate_poc
388 hevc_picture_info_t
*picture
,
389 hevc_picture_info_t
*prev_picture
392 #if HEVC_POC_DEBUG_PRINT
393 fprintf( stderr
, "PictureOrderCount\n" );
395 hevc_pps_t
*pps
= hevc_get_pps( info
->pps_list
, picture
->pic_parameter_set_id
);
397 return LSMASH_ERR_NAMELESS
;
398 hevc_sps_t
*sps
= hevc_get_sps( info
->sps_list
, pps
->seq_parameter_set_id
);
400 return LSMASH_ERR_NAMELESS
;
401 /* 8.3.1 Decoding process for picture order count
402 * This process needs to be invoked only for the first slice segment of a picture. */
403 int NoRaslOutputFlag
;
406 /* 8.1 General decoding process
407 * If the current picture is an IDR picture, a BLA picture, the first picture in the
408 * bitstream in decoding order, or the first picture that follows an end of sequence
409 * NAL unit in decoding order, the variable NoRaslOutputFlag is set equal to 1.
411 * Note that not only the end of sequence NAL unit but the end of bistream NAL unit as
412 * well specify that the current access unit is the last access unit in the coded video
413 * sequence in decoding order. */
414 NoRaslOutputFlag
= picture
->idr
|| picture
->broken_link
|| info
->eos
;
419 NoRaslOutputFlag
= 0;
421 int32_t poc_lsb
= picture
->poc_lsb
;
422 if( picture
->irap
&& NoRaslOutputFlag
)
426 int32_t prev_poc_msb
= picture
->idr
? 0 : prev_picture
->tid0_poc_msb
;
427 int32_t prev_poc_lsb
= picture
->idr
? 0 : prev_picture
->tid0_poc_lsb
;
428 int32_t max_poc_lsb
= 1 << sps
->log2_max_pic_order_cnt_lsb
;
429 if( (poc_lsb
< prev_poc_lsb
)
430 && ((prev_poc_lsb
- poc_lsb
) >= (max_poc_lsb
/ 2)) )
431 poc_msb
= prev_poc_msb
+ max_poc_lsb
;
432 else if( (poc_lsb
> prev_poc_lsb
)
433 && ((poc_lsb
- prev_poc_lsb
) > (max_poc_lsb
/ 2)) )
434 poc_msb
= prev_poc_msb
- max_poc_lsb
;
436 poc_msb
= prev_poc_msb
;
438 picture
->poc
= poc_msb
+ poc_lsb
;
439 if( picture
->TemporalId
== 0 && (!picture
->radl
|| !picture
->rasl
|| !picture
->sublayer_nonref
) )
441 picture
->tid0_poc_msb
= poc_msb
;
442 picture
->tid0_poc_lsb
= poc_lsb
;
444 #if HEVC_POC_DEBUG_PRINT
445 fprintf( stderr
, " prevPicOrderCntMsb: %"PRId32
"\n", prev_poc_msb
);
446 fprintf( stderr
, " prevPicOrderCntLsb: %"PRId32
"\n", prev_poc_lsb
);
447 fprintf( stderr
, " PicOrderCntMsb: %"PRId64
"\n", poc_msb
);
448 fprintf( stderr
, " pic_order_cnt_lsb: %"PRId32
"\n", poc_lsb
);
449 fprintf( stderr
, " MaxPicOrderCntLsb: %"PRIu64
"\n", max_poc_lsb
);
450 fprintf( stderr
, " POC: %"PRId32
"\n", picture
->poc
);
455 static inline int hevc_activate_vps
458 uint8_t video_parameter_set_id
461 hevc_vps_t
*vps
= hevc_get_vps( info
->vps_list
, video_parameter_set_id
);
463 return LSMASH_ERR_NAMELESS
;
468 static inline int hevc_activate_sps
471 uint8_t seq_parameter_set_id
474 hevc_sps_t
*sps
= hevc_get_sps( info
->sps_list
, seq_parameter_set_id
);
476 return LSMASH_ERR_NAMELESS
;
481 static void hevc_parse_scaling_list_data
486 for( int sizeId
= 0; sizeId
< 4; sizeId
++ )
487 for( int matrixId
= 0; matrixId
< (sizeId
== 3 ? 2 : 6); matrixId
++ )
489 if( !lsmash_bits_get( bits
, 1 ) ) /* scaling_list_pred_mode_flag[sizeId][matrixId] */
490 nalu_get_exp_golomb_ue( bits
); /* scaling_list_pred_matrix_id_delta[sizeId][matrixId] */
493 int coefNum
= LSMASH_MIN( 64, 1 << (4 + (sizeId
<< 1)) );
495 nalu_get_exp_golomb_se( bits
); /* scaling_list_dc_coef_minus8[sizeId - 2][matrixId] */
496 for( int i
= 0; i
< coefNum
; i
++ )
497 nalu_get_exp_golomb_se( bits
); /* scaling_list_delta_coef */
502 static int hevc_short_term_ref_pic_set
509 int inter_ref_pic_set_prediction_flag
= stRpsIdx
!= 0 ? lsmash_bits_get( bits
, 1 ) : 0;
510 if( inter_ref_pic_set_prediction_flag
)
512 /* delta_idx_minus1 is always 0 in SPS since stRpsIdx must not be equal to num_short_term_ref_pic_sets. */
513 uint64_t delta_idx_minus1
= stRpsIdx
== sps
->num_short_term_ref_pic_sets
? nalu_get_exp_golomb_ue( bits
) : 0;
514 int delta_rps_sign
= lsmash_bits_get( bits
, 1 );
515 uint64_t abs_delta_rps_minus1
= nalu_get_exp_golomb_ue( bits
);
516 int RefRpsIdx
= stRpsIdx
- (delta_idx_minus1
+ 1);
517 int deltaRps
= (delta_rps_sign
? -1 : 1) * (abs_delta_rps_minus1
+ 1);
518 hevc_st_rps_t
*st_rps
= &sps
->st_rps
[stRpsIdx
];
519 hevc_st_rps_t
*ref_rps
= &sps
->st_rps
[RefRpsIdx
];
520 uint8_t used_by_curr_pic_flag
[32];
521 uint8_t use_delta_flag
[32];
522 for( int j
= 0; j
<= ref_rps
->NumDeltaPocs
; j
++ )
524 used_by_curr_pic_flag
[j
] = lsmash_bits_get( bits
, 1 );
525 use_delta_flag
[j
] = !used_by_curr_pic_flag
[j
] ? lsmash_bits_get( bits
, 1 ) : 1;
527 /* NumNegativePics */
529 for( int j
= ref_rps
->NumPositivePics
- 1; j
>= 0; j
-- )
531 int dPoc
= ref_rps
->DeltaPocS1
[j
] + deltaRps
;
532 if( dPoc
< 0 && use_delta_flag
[ ref_rps
->NumNegativePics
+ j
] )
534 st_rps
->DeltaPocS0
[i
] = dPoc
;
535 st_rps
->UsedByCurrPicS0
[i
++] = used_by_curr_pic_flag
[ ref_rps
->NumNegativePics
+ j
];
538 if( deltaRps
< 0 && use_delta_flag
[ ref_rps
->NumDeltaPocs
] )
540 st_rps
->DeltaPocS0
[i
] = deltaRps
;
541 st_rps
->UsedByCurrPicS0
[i
++] = used_by_curr_pic_flag
[ ref_rps
->NumDeltaPocs
];
543 for( int j
= 0; j
< ref_rps
->NumNegativePics
; j
++ )
545 int dPoc
= ref_rps
->DeltaPocS0
[j
] + deltaRps
;
546 if( dPoc
< 0 && use_delta_flag
[j
] )
548 st_rps
->DeltaPocS0
[i
] = dPoc
;
549 st_rps
->UsedByCurrPicS0
[i
++] = used_by_curr_pic_flag
[j
];
552 st_rps
->NumNegativePics
= i
;
553 /* NumPositivePics */
555 for( int j
= ref_rps
->NumNegativePics
- 1; j
>= 0; j
-- )
557 int dPoc
= ref_rps
->DeltaPocS0
[j
] + deltaRps
;
558 if( dPoc
> 0 && use_delta_flag
[j
] )
560 st_rps
->DeltaPocS1
[i
] = dPoc
;
561 st_rps
->UsedByCurrPicS1
[i
++] = used_by_curr_pic_flag
[j
];
564 if( deltaRps
> 0 && use_delta_flag
[ ref_rps
->NumDeltaPocs
] )
566 st_rps
->DeltaPocS1
[i
] = deltaRps
;
567 st_rps
->UsedByCurrPicS1
[i
++] = used_by_curr_pic_flag
[ ref_rps
->NumDeltaPocs
];
569 for( int j
= 0; j
< ref_rps
->NumPositivePics
; j
++ )
571 int dPoc
= ref_rps
->DeltaPocS1
[j
] + deltaRps
;
572 if( dPoc
> 0 && use_delta_flag
[ ref_rps
->NumNegativePics
+ j
] )
574 st_rps
->DeltaPocS1
[i
] = dPoc
;
575 st_rps
->UsedByCurrPicS1
[i
++] = used_by_curr_pic_flag
[ ref_rps
->NumNegativePics
+ j
];
578 st_rps
->NumPositivePics
= i
;
580 st_rps
->NumDeltaPocs
= st_rps
->NumNegativePics
+ st_rps
->NumPositivePics
;
584 uint64_t num_negative_pics
= nalu_get_exp_golomb_ue( bits
);
585 uint64_t num_positive_pics
= nalu_get_exp_golomb_ue( bits
);
586 if( num_negative_pics
>= HEVC_MAX_DPB_SIZE
|| num_positive_pics
>= HEVC_MAX_DPB_SIZE
)
587 return LSMASH_ERR_INVALID_DATA
;
588 hevc_st_rps_t
*st_rps
= &sps
->st_rps
[stRpsIdx
];
589 st_rps
->NumNegativePics
= num_negative_pics
;
590 st_rps
->NumPositivePics
= num_positive_pics
;
591 st_rps
->NumDeltaPocs
= st_rps
->NumNegativePics
+ st_rps
->NumPositivePics
;
592 for( int i
= 0; i
< num_negative_pics
; i
++ )
594 uint64_t delta_poc_s0_minus1
= nalu_get_exp_golomb_ue( bits
);
596 st_rps
->DeltaPocS0
[i
] = -(signed)(delta_poc_s0_minus1
+ 1);
598 st_rps
->DeltaPocS0
[i
] = st_rps
->DeltaPocS0
[i
- 1] - (delta_poc_s0_minus1
+ 1);
599 st_rps
->UsedByCurrPicS0
[i
] = lsmash_bits_get( bits
, 1 ); /* used_by_curr_pic_s0_flag */
601 for( int i
= 0; i
< num_positive_pics
; i
++ )
603 uint64_t delta_poc_s1_minus1
= nalu_get_exp_golomb_ue( bits
);
605 st_rps
->DeltaPocS1
[i
] = +(delta_poc_s1_minus1
+ 1);
607 st_rps
->DeltaPocS1
[i
] = st_rps
->DeltaPocS1
[i
- 1] + (delta_poc_s1_minus1
+ 1);
608 st_rps
->UsedByCurrPicS0
[i
] = lsmash_bits_get( bits
, 1 ); /* used_by_curr_pic_s1_flag */
614 static inline void hevc_parse_sub_layer_hrd_parameters
618 int sub_pic_hrd_params_present_flag
621 for( int i
= 0; i
<= CpbCnt
; i
++ )
623 nalu_get_exp_golomb_ue( bits
); /* bit_rate_value_minus1[i] */
624 nalu_get_exp_golomb_ue( bits
); /* cpb_size_value_minus1[i] */
625 if( sub_pic_hrd_params_present_flag
)
627 nalu_get_exp_golomb_ue( bits
); /* cpb_size_du_value_minus1[i] */
628 nalu_get_exp_golomb_ue( bits
); /* bit_rate_du_value_minus1[i] */
630 lsmash_bits_get( bits
, 1 ); /* cbr_flag[i] */
634 static void hevc_parse_hrd_parameters
638 int commonInfPresentFlag
,
639 int maxNumSubLayersMinus1
642 /* The specification we refer to doesn't define the implicit value of some fields.
643 * According to JCTVC-HM reference software,
644 * the implicit value of nal_hrd_parameters_present_flag is to be equal to 0,
645 * the implicit value of vcl_hrd_parameters_present_flag is to be equal to 0. */
646 int nal_hrd_parameters_present_flag
= 0;
647 int vcl_hrd_parameters_present_flag
= 0;
648 memset( hrd
, 0, sizeof(hevc_hrd_t
) );
649 if( commonInfPresentFlag
)
651 nal_hrd_parameters_present_flag
= lsmash_bits_get( bits
, 1 );
652 vcl_hrd_parameters_present_flag
= lsmash_bits_get( bits
, 1 );
653 if( nal_hrd_parameters_present_flag
654 || vcl_hrd_parameters_present_flag
)
656 hrd
->CpbDpbDelaysPresentFlag
= 1;
657 hrd
->sub_pic_hrd_params_present_flag
= lsmash_bits_get( bits
, 1 );
658 if( hrd
->sub_pic_hrd_params_present_flag
)
660 lsmash_bits_get( bits
, 8 ); /* tick_divisor_minus2 */
661 hrd
->du_cpb_removal_delay_increment_length
= lsmash_bits_get( bits
, 5 ) + 1;
662 hrd
->sub_pic_cpb_params_in_pic_timing_sei_flag
= lsmash_bits_get( bits
, 1 );
663 hrd
->dpb_output_delay_du_length
= lsmash_bits_get( bits
, 5 ) + 1;
665 lsmash_bits_get( bits
, 4 ); /* bit_rate_scale */
666 lsmash_bits_get( bits
, 4 ); /* cpb_size_scale */
667 if( hrd
->sub_pic_hrd_params_present_flag
)
668 lsmash_bits_get( bits
, 4 ); /* cpb_size_du_scale */
669 lsmash_bits_get( bits
, 5 ); /* initial_cpb_removal_delay_length_minus1 */
670 hrd
->au_cpb_removal_delay_length
= lsmash_bits_get( bits
, 5 ) + 1;
671 hrd
->dpb_output_delay_length
= lsmash_bits_get( bits
, 5 ) + 1;
674 for( int i
= 0; i
<= maxNumSubLayersMinus1
; i
++ )
676 hrd
->fixed_pic_rate_general_flag
[i
] = lsmash_bits_get( bits
, 1 );
677 uint8_t fixed_pic_rate_within_cvs_flag
= !hrd
->fixed_pic_rate_general_flag
[i
] ? lsmash_bits_get( bits
, 1 ) : 1;
678 uint8_t low_delay_hrd_flag
= !fixed_pic_rate_within_cvs_flag
? lsmash_bits_get( bits
, 1 ) : 0;
679 hrd
->elemental_duration_in_tc
[i
] = fixed_pic_rate_within_cvs_flag
? nalu_get_exp_golomb_ue( bits
) + 1 : 0;
680 uint8_t cpb_cnt_minus1
= !low_delay_hrd_flag
? nalu_get_exp_golomb_ue( bits
) : 0;
681 if( nal_hrd_parameters_present_flag
)
682 hevc_parse_sub_layer_hrd_parameters( bits
, cpb_cnt_minus1
, hrd
->sub_pic_hrd_params_present_flag
);
683 if( vcl_hrd_parameters_present_flag
)
684 hevc_parse_sub_layer_hrd_parameters( bits
, cpb_cnt_minus1
, hrd
->sub_pic_hrd_params_present_flag
);
688 static inline void hevc_parse_profile_tier_level_common
691 hevc_ptl_common_t
*ptlc
,
696 if( profile_present
)
698 ptlc
->profile_space
= lsmash_bits_get( bits
, 2 );
699 ptlc
->tier_flag
= lsmash_bits_get( bits
, 1 );
700 ptlc
->profile_idc
= lsmash_bits_get( bits
, 5 );
701 ptlc
->profile_compatibility_flags
= lsmash_bits_get( bits
, 32 );
702 ptlc
->progressive_source_flag
= lsmash_bits_get( bits
, 1 );
703 ptlc
->interlaced_source_flag
= lsmash_bits_get( bits
, 1 );
704 ptlc
->non_packed_constraint_flag
= lsmash_bits_get( bits
, 1 );
705 ptlc
->frame_only_constraint_flag
= lsmash_bits_get( bits
, 1 );
706 ptlc
->reserved_zero_44bits
= lsmash_bits_get( bits
, 44 );
709 ptlc
->level_idc
= lsmash_bits_get( bits
, 8 );
712 static void hevc_parse_profile_tier_level
716 int maxNumSubLayersMinus1
719 hevc_parse_profile_tier_level_common( bits
, &ptl
->general
, 1, 1 );
720 if( maxNumSubLayersMinus1
== 0 )
722 assert( maxNumSubLayersMinus1
<= 6 );
723 int sub_layer_profile_present_flag
[6] = { 0 };
724 int sub_layer_level_present_flag
[6] = { 0 };
725 for( int i
= 0; i
< maxNumSubLayersMinus1
; i
++ )
727 sub_layer_profile_present_flag
[i
] = lsmash_bits_get( bits
, 1 );
728 sub_layer_level_present_flag
[i
] = lsmash_bits_get( bits
, 1 );
730 for( int i
= maxNumSubLayersMinus1
; i
< 8; i
++ )
731 lsmash_bits_get( bits
, 2 ); /* reserved_zero_2bits[i] */
732 for( int i
= 0; i
< maxNumSubLayersMinus1
; i
++ )
733 hevc_parse_profile_tier_level_common( bits
, &ptl
->sub_layer
[i
], sub_layer_profile_present_flag
[i
], sub_layer_level_present_flag
[i
] );
736 static int hevc_parse_vps_minimally
740 uint8_t *rbsp_buffer
,
746 int err
= nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, &rbsp_size
, ebsp
, ebsp_size
);
749 memset( vps
, 0, sizeof(hevc_vps_t
) );
750 vps
->video_parameter_set_id
= lsmash_bits_get( bits
, 4 );
751 /* vps_reserved_three_2bits shall be 3 in the specification we refer to. */
752 if( lsmash_bits_get( bits
, 2 ) != 3 )
753 return LSMASH_ERR_NAMELESS
;
754 /* vps_max_layers_minus1 shall be 0 in the specification we refer to. */
755 if( lsmash_bits_get( bits
, 6 ) != 0 )
756 return LSMASH_ERR_NAMELESS
;
757 vps
->max_sub_layers_minus1
= lsmash_bits_get( bits
, 3 );
758 vps
->temporal_id_nesting_flag
= lsmash_bits_get( bits
, 1 );
759 /* When vps_max_sub_layers_minus1 is equal to 0, vps_temporal_id_nesting_flag shall be equal to 1. */
760 if( (vps
->max_sub_layers_minus1
| vps
->temporal_id_nesting_flag
) == 0 )
761 return LSMASH_ERR_INVALID_DATA
;
762 /* vps_reserved_0xffff_16bits shall be 0xFFFF in the specification we refer to. */
763 if( lsmash_bits_get( bits
, 16 ) != 0xFFFF )
764 return LSMASH_ERR_NAMELESS
;
765 hevc_parse_profile_tier_level( bits
, &vps
->ptl
, vps
->max_sub_layers_minus1
);
766 vps
->frame_field_info_present_flag
= vps
->ptl
.general
.progressive_source_flag
767 && vps
->ptl
.general
.interlaced_source_flag
;
768 int sub_layer_ordering_info_present_flag
= lsmash_bits_get( bits
, 1 );
769 for( int i
= sub_layer_ordering_info_present_flag
? 0 : vps
->max_sub_layers_minus1
; i
<= vps
->max_sub_layers_minus1
; i
++ )
771 nalu_get_exp_golomb_ue( bits
); /* max_dec_pic_buffering_minus1[i] */
772 nalu_get_exp_golomb_ue( bits
); /* max_num_reorder_pics [i] */
773 nalu_get_exp_golomb_ue( bits
); /* max_latency_increase_plus1 [i] */
775 uint8_t max_layer_id
= lsmash_bits_get( bits
, 6 );
776 uint16_t num_layer_sets_minus1
= nalu_get_exp_golomb_ue( bits
);
777 for( int i
= 1; i
<= num_layer_sets_minus1
; i
++ )
778 for( int j
= 0; j
<= max_layer_id
; j
++ )
779 lsmash_bits_get( bits
, 1 ); /* layer_id_included_flag[i][j] */
780 return bits
->bs
->error
? LSMASH_ERR_NAMELESS
: 0;
786 uint8_t *rbsp_buffer
,
791 lsmash_bits_t
*bits
= info
->bits
;
794 /* Parse VPS minimally for configuration records. */
796 int err
= hevc_parse_vps_minimally( bits
, &min_vps
, rbsp_buffer
, ebsp
, ebsp_size
);
799 vps
= hevc_get_vps( info
->vps_list
, min_vps
.video_parameter_set_id
);
801 return LSMASH_ERR_NAMELESS
;
804 vps
->timing_info_present_flag
= lsmash_bits_get( bits
, 1 );
805 if( vps
->timing_info_present_flag
)
807 lsmash_bits_get( bits
, 32 ); /* num_units_in_tick */
808 lsmash_bits_get( bits
, 32 ); /* time_scale */
809 if( lsmash_bits_get( bits
, 1 ) ) /* poc_proportional_to_timing_flag */
810 nalu_get_exp_golomb_ue( bits
); /* num_ticks_poc_diff_one_minus1 */
811 vps
->num_hrd_parameters
= nalu_get_exp_golomb_ue( bits
);
812 for( int i
= 0; i
< vps
->num_hrd_parameters
; i
++ )
814 nalu_get_exp_golomb_ue( bits
); /* hrd_layer_set_idx[i] */
815 int cprms_present_flag
= i
> 0 ? lsmash_bits_get( bits
, 1 ) : 1;
816 /* Although the value of vps_num_hrd_parameters is required to be less than or equal to 1 in the spec
817 * we refer to, decoders shall allow other values of vps_num_hrd_parameters in the range of 0 to 1024,
818 * inclusive, to appear in the syntax. */
820 hevc_parse_hrd_parameters( bits
, &vps
->hrd
[i
], cprms_present_flag
, vps
->max_sub_layers_minus1
);
823 hevc_hrd_t dummy_hrd
;
824 hevc_parse_hrd_parameters( bits
, &dummy_hrd
, cprms_present_flag
, vps
->max_sub_layers_minus1
);
828 /* Skip VPS extension. */
829 lsmash_bits_empty( bits
);
830 if( bits
->bs
->error
)
831 return LSMASH_ERR_NAMELESS
;
837 static int hevc_parse_sps_minimally
841 uint8_t *rbsp_buffer
,
847 int err
= nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, &rbsp_size
, ebsp
, ebsp_size
);
850 memset( sps
, 0, sizeof(hevc_sps_t
) );
851 sps
->video_parameter_set_id
= lsmash_bits_get( bits
, 4 );
852 sps
->max_sub_layers_minus1
= lsmash_bits_get( bits
, 3 );
853 sps
->temporal_id_nesting_flag
= lsmash_bits_get( bits
, 1 );
854 hevc_parse_profile_tier_level( bits
, &sps
->ptl
, sps
->max_sub_layers_minus1
);
855 sps
->seq_parameter_set_id
= nalu_get_exp_golomb_ue( bits
);
856 sps
->chroma_format_idc
= nalu_get_exp_golomb_ue( bits
);
857 if( sps
->chroma_format_idc
== 3 )
858 sps
->separate_colour_plane_flag
= lsmash_bits_get( bits
, 1 );
859 static const int SubWidthC
[] = { 1, 2, 2, 1 };
860 static const int SubHeightC
[] = { 1, 2, 1, 1 };
861 uint64_t pic_width_in_luma_samples
= nalu_get_exp_golomb_ue( bits
);
862 uint64_t pic_height_in_luma_samples
= nalu_get_exp_golomb_ue( bits
);
863 sps
->cropped_width
= pic_width_in_luma_samples
;
864 sps
->cropped_height
= pic_height_in_luma_samples
;
865 if( lsmash_bits_get( bits
, 1 ) ) /* conformance_window_flag */
867 uint64_t conf_win_left_offset
= nalu_get_exp_golomb_ue( bits
);
868 uint64_t conf_win_right_offset
= nalu_get_exp_golomb_ue( bits
);
869 uint64_t conf_win_top_offset
= nalu_get_exp_golomb_ue( bits
);
870 uint64_t conf_win_bottom_offset
= nalu_get_exp_golomb_ue( bits
);
871 sps
->cropped_width
-= (conf_win_left_offset
+ conf_win_right_offset
) * SubWidthC
[ sps
->chroma_format_idc
];
872 sps
->cropped_height
-= (conf_win_top_offset
+ conf_win_bottom_offset
) * SubHeightC
[ sps
->chroma_format_idc
];
874 sps
->bit_depth_luma_minus8
= nalu_get_exp_golomb_ue( bits
);
875 sps
->bit_depth_chroma_minus8
= nalu_get_exp_golomb_ue( bits
);
876 sps
->log2_max_pic_order_cnt_lsb
= nalu_get_exp_golomb_ue( bits
) + 4;
877 int sub_layer_ordering_info_present_flag
= lsmash_bits_get( bits
, 1 );
878 for( int i
= sub_layer_ordering_info_present_flag
? 0 : sps
->max_sub_layers_minus1
; i
<= sps
->max_sub_layers_minus1
; i
++ )
880 nalu_get_exp_golomb_ue( bits
); /* max_dec_pic_buffering_minus1[i] */
881 nalu_get_exp_golomb_ue( bits
); /* max_num_reorder_pics [i] */
882 nalu_get_exp_golomb_ue( bits
); /* max_latency_increase_plus1 [i] */
884 uint64_t log2_min_luma_coding_block_size_minus3
= nalu_get_exp_golomb_ue( bits
);
885 uint64_t log2_diff_max_min_luma_coding_block_size
= nalu_get_exp_golomb_ue( bits
);
886 nalu_get_exp_golomb_ue( bits
); /* log2_min_transform_block_size_minus2 */
887 nalu_get_exp_golomb_ue( bits
); /* log2_diff_max_min_transform_block_size */
888 nalu_get_exp_golomb_ue( bits
); /* max_transform_hierarchy_depth_inter */
889 nalu_get_exp_golomb_ue( bits
); /* max_transform_hierarchy_depth_intra */
891 int MinCbLog2SizeY
= log2_min_luma_coding_block_size_minus3
+ 3;
892 int MinCbSizeY
= 1 << MinCbLog2SizeY
;
893 if( pic_width_in_luma_samples
== 0 || pic_width_in_luma_samples
% MinCbSizeY
894 || pic_height_in_luma_samples
== 0 || pic_height_in_luma_samples
% MinCbSizeY
)
895 return LSMASH_ERR_INVALID_DATA
; /* Both shall be an integer multiple of MinCbSizeY. */
896 int CtbLog2SizeY
= MinCbLog2SizeY
+ log2_diff_max_min_luma_coding_block_size
;
897 int CtbSizeY
= 1 << CtbLog2SizeY
;
898 sps
->PicWidthInCtbsY
= (pic_width_in_luma_samples
- 1) / CtbSizeY
+ 1;
899 sps
->PicHeightInCtbsY
= (pic_height_in_luma_samples
- 1) / CtbSizeY
+ 1;
900 sps
->PicSizeInCtbsY
= sps
->PicWidthInCtbsY
* sps
->PicHeightInCtbsY
;
902 if( lsmash_bits_get( bits
, 1 ) /* scaling_list_enabled_flag */
903 && lsmash_bits_get( bits
, 1 ) ) /* sps_scaling_list_data_present_flag */
904 hevc_parse_scaling_list_data( bits
);
905 lsmash_bits_get( bits
, 1 ); /* amp_enabled_flag */
906 lsmash_bits_get( bits
, 1 ); /* sample_adaptive_offset_enabled_flag */
907 if( lsmash_bits_get( bits
, 1 ) ) /* pcm_enabled_flag */
909 lsmash_bits_get( bits
, 4 ); /* pcm_sample_bit_depth_luma_minus1 */
910 lsmash_bits_get( bits
, 4 ); /* pcm_sample_bit_depth_chroma_minus1 */
911 nalu_get_exp_golomb_ue( bits
); /* log2_min_pcm_luma_coding_block_size_minus3 */
912 nalu_get_exp_golomb_ue( bits
); /* log2_diff_max_min_pcm_luma_coding_block_size */
913 lsmash_bits_get( bits
, 1 ); /* pcm_loop_filter_disabled_flag */
915 sps
->num_short_term_ref_pic_sets
= nalu_get_exp_golomb_ue( bits
);
916 for( int i
= 0; i
< sps
->num_short_term_ref_pic_sets
; i
++ )
917 if( (err
= hevc_short_term_ref_pic_set( bits
, sps
, i
)) < 0 )
919 sps
->long_term_ref_pics_present_flag
= lsmash_bits_get( bits
, 1 );
920 if( sps
->long_term_ref_pics_present_flag
)
922 sps
->num_long_term_ref_pics_sps
= nalu_get_exp_golomb_ue( bits
);
923 for( int i
= 0; i
< sps
->num_long_term_ref_pics_sps
; i
++ )
925 lsmash_bits_get( bits
, sps
->log2_max_pic_order_cnt_lsb
); /* lt_ref_pic_poc_lsb_sps [i] */
926 lsmash_bits_get( bits
, 1 ); /* used_by_curr_pic_lt_sps_flag[i] */
929 sps
->temporal_mvp_enabled_flag
= lsmash_bits_get( bits
, 1 );
930 lsmash_bits_get( bits
, 1 ); /* strong_intra_smoothing_enabled_flag */
931 sps
->vui
.present
= lsmash_bits_get( bits
, 1 ); /* vui_parameters_present_flag */
932 if( sps
->vui
.present
)
934 /* vui_parameters() */
935 if( lsmash_bits_get( bits
, 1 ) ) /* aspect_ratio_info_present_flag */
937 uint8_t aspect_ratio_idc
= lsmash_bits_get( bits
, 8 );
938 if( aspect_ratio_idc
== 255 )
941 sps
->vui
.sar_width
= lsmash_bits_get( bits
, 16 );
942 sps
->vui
.sar_height
= lsmash_bits_get( bits
, 16 );
950 } pre_defined_sar
[] =
952 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
953 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 }, { 80, 33 },
954 { 18, 11 }, { 15, 11 }, { 64, 33 }, { 160, 99 }, { 4, 3 },
957 if( aspect_ratio_idc
< (sizeof(pre_defined_sar
) / sizeof(pre_defined_sar
[0])) )
959 sps
->vui
.sar_width
= pre_defined_sar
[ aspect_ratio_idc
].sar_width
;
960 sps
->vui
.sar_height
= pre_defined_sar
[ aspect_ratio_idc
].sar_height
;
964 /* Behavior when unknown aspect_ratio_idc is detected is not specified in the specification. */
965 sps
->vui
.sar_width
= 0;
966 sps
->vui
.sar_height
= 0;
972 sps
->vui
.sar_width
= 0;
973 sps
->vui
.sar_height
= 0;
975 if( lsmash_bits_get( bits
, 1 ) ) /* overscan_info_present_flag */
976 lsmash_bits_get( bits
, 1 ); /* overscan_appropriate_flag */
977 if( lsmash_bits_get( bits
, 1 ) ) /* video_signal_type_present_flag */
979 lsmash_bits_get( bits
, 3 ); /* video_format */
980 sps
->vui
.video_full_range_flag
= lsmash_bits_get( bits
, 1 );
981 sps
->vui
.colour_description_present_flag
= lsmash_bits_get( bits
, 1 );
982 if( sps
->vui
.colour_description_present_flag
)
984 sps
->vui
.colour_primaries
= lsmash_bits_get( bits
, 8 );
985 sps
->vui
.transfer_characteristics
= lsmash_bits_get( bits
, 8 );
986 sps
->vui
.matrix_coeffs
= lsmash_bits_get( bits
, 8 );
990 sps
->vui
.colour_primaries
= 2;
991 sps
->vui
.transfer_characteristics
= 2;
992 sps
->vui
.matrix_coeffs
= 2;
995 if( lsmash_bits_get( bits
, 1 ) ) /* chroma_loc_info_present_flag */
997 nalu_get_exp_golomb_ue( bits
); /* chroma_sample_loc_type_top_field */
998 nalu_get_exp_golomb_ue( bits
); /* chroma_sample_loc_type_bottom_field */
1000 lsmash_bits_get( bits
, 1 ); /* neutral_chroma_indication_flag */
1001 sps
->vui
.field_seq_flag
= lsmash_bits_get( bits
, 1 );
1002 sps
->vui
.frame_field_info_present_flag
= lsmash_bits_get( bits
, 1 );
1003 if( sps
->vui
.field_seq_flag
)
1004 /* cropped_height indicates in a frame. */
1005 sps
->cropped_height
*= 2;
1006 if( lsmash_bits_get( bits
, 1 ) ) /* default_display_window_flag */
1008 /* default display window
1009 * A rectangular region for display specified by these values is not considered
1010 * as cropped visual presentation size which decoder delivers.
1011 * Maybe, these values shall be indicated by the clean aperture on container level. */
1012 sps
->vui
.def_disp_win_offset
.left
= (lsmash_rational_u32_t
){ nalu_get_exp_golomb_ue( bits
) * SubWidthC
[ sps
->chroma_format_idc
], 1 };
1013 sps
->vui
.def_disp_win_offset
.right
= (lsmash_rational_u32_t
){ nalu_get_exp_golomb_ue( bits
) * SubWidthC
[ sps
->chroma_format_idc
], 1 };
1014 sps
->vui
.def_disp_win_offset
.top
= (lsmash_rational_u32_t
){ nalu_get_exp_golomb_ue( bits
) * SubHeightC
[ sps
->chroma_format_idc
], 1 };
1015 sps
->vui
.def_disp_win_offset
.bottom
= (lsmash_rational_u32_t
){ nalu_get_exp_golomb_ue( bits
) * SubHeightC
[ sps
->chroma_format_idc
], 1 };
1017 if( lsmash_bits_get( bits
, 1 ) ) /* vui_timing_info_present_flag */
1019 sps
->vui
.num_units_in_tick
= lsmash_bits_get( bits
, 32 );
1020 sps
->vui
.time_scale
= lsmash_bits_get( bits
, 32 );
1021 if( lsmash_bits_get( bits
, 1 ) ) /* vui_poc_proportional_to_timing_flag */
1022 nalu_get_exp_golomb_ue( bits
); /* vui_num_ticks_poc_diff_one_minus1 */
1023 if( lsmash_bits_get( bits
, 1 ) ) /* vui_hrd_parameters_present_flag */
1024 hevc_parse_hrd_parameters( bits
, &sps
->vui
.hrd
, 1, sps
->max_sub_layers_minus1
);
1028 sps
->vui
.num_units_in_tick
= 1; /* arbitrary */
1029 sps
->vui
.time_scale
= 25; /* arbitrary */
1031 if( lsmash_bits_get( bits
, 1 ) ) /* bitstream_restriction_flag */
1033 lsmash_bits_get( bits
, 1 ); /* tiles_fixed_structure_flag */
1034 lsmash_bits_get( bits
, 1 ); /* motion_vectors_over_pic_boundaries_flag */
1035 lsmash_bits_get( bits
, 1 ); /* restricted_ref_pic_lists_flag */
1036 sps
->vui
.min_spatial_segmentation_idc
= nalu_get_exp_golomb_ue( bits
);
1037 nalu_get_exp_golomb_ue( bits
); /* max_bytes_per_pic_denom */
1038 nalu_get_exp_golomb_ue( bits
); /* max_bits_per_min_cu_denom */
1039 nalu_get_exp_golomb_ue( bits
); /* log2_max_mv_length_horizontal */
1040 nalu_get_exp_golomb_ue( bits
); /* log2_max_mv_length_vertical */
1043 sps
->vui
.min_spatial_segmentation_idc
= 0;
1047 sps
->vui
.sar_width
= 0;
1048 sps
->vui
.sar_height
= 0;
1049 sps
->vui
.colour_primaries
= 2;
1050 sps
->vui
.transfer_characteristics
= 2;
1051 sps
->vui
.matrix_coeffs
= 2;
1052 sps
->vui
.field_seq_flag
= 0;
1053 sps
->vui
.frame_field_info_present_flag
= sps
->ptl
.general
.progressive_source_flag
1054 && sps
->ptl
.general
.interlaced_source_flag
;
1055 sps
->vui
.num_units_in_tick
= 1; /* arbitrary */
1056 sps
->vui
.time_scale
= 25; /* arbitrary */
1057 sps
->vui
.min_spatial_segmentation_idc
= 0;
1059 return bits
->bs
->error
? LSMASH_ERR_NAMELESS
: 0;
1065 uint8_t *rbsp_buffer
,
1070 lsmash_bits_t
*bits
= info
->bits
;
1073 /* Parse SPS minimally for configuration records. */
1075 int err
= hevc_parse_sps_minimally( bits
, &min_sps
, rbsp_buffer
, ebsp
, ebsp_size
);
1078 sps
= hevc_get_sps( info
->sps_list
, min_sps
.seq_parameter_set_id
);
1080 return LSMASH_ERR_NAMELESS
;
1083 /* Skip SPS extension. */
1084 lsmash_bits_empty( bits
);
1085 if( bits
->bs
->error
)
1086 return LSMASH_ERR_NAMELESS
;
1089 hevc_activate_vps( info
, info
->sps
.video_parameter_set_id
);
1093 static int hevc_allocate_tile_sizes
1096 uint32_t num_tile_columns
,
1097 uint32_t num_tile_rows
1100 /* Allocate columns and rows of tiles. */
1101 size_t col_alloc_size
= 2 * num_tile_columns
* sizeof(uint32_t);
1102 if( col_alloc_size
> pps
->col_alloc_size
)
1104 void *temp
= lsmash_realloc( pps
->colWidth
, col_alloc_size
);
1106 return LSMASH_ERR_MEMORY_ALLOC
;
1107 pps
->col_alloc_size
= col_alloc_size
;
1108 pps
->colWidth
= temp
;
1110 size_t row_alloc_size
= 2 * num_tile_rows
* sizeof(uint32_t);
1111 if( row_alloc_size
> pps
->row_alloc_size
)
1113 void *temp
= lsmash_realloc( pps
->rowHeight
, row_alloc_size
);
1115 return LSMASH_ERR_MEMORY_ALLOC
;
1116 pps
->row_alloc_size
= row_alloc_size
;
1117 pps
->rowHeight
= temp
;
1119 pps
->colBd
= pps
->colWidth
+ num_tile_columns
;
1120 pps
->rowBd
= pps
->rowHeight
+ num_tile_rows
;
1124 static int hevc_parse_pps_minimally
1126 lsmash_bits_t
*bits
,
1128 uint8_t *rbsp_buffer
,
1134 int err
= nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, &rbsp_size
, ebsp
, ebsp_size
);
1137 memset( pps
, 0, SIZEOF_PPS_EXCLUDING_HEAP
);
1138 pps
->pic_parameter_set_id
= nalu_get_exp_golomb_ue( bits
);
1139 pps
->seq_parameter_set_id
= nalu_get_exp_golomb_ue( bits
);
1140 pps
->dependent_slice_segments_enabled_flag
= lsmash_bits_get( bits
, 1 );
1141 pps
->output_flag_present_flag
= lsmash_bits_get( bits
, 1 );
1142 pps
->num_extra_slice_header_bits
= lsmash_bits_get( bits
, 3 );
1143 lsmash_bits_get( bits
, 1 ); /* sign_data_hiding_enabled_flag */
1144 lsmash_bits_get( bits
, 1 ); /* cabac_init_present_flag */
1145 nalu_get_exp_golomb_ue( bits
); /* num_ref_idx_l0_default_active_minus1 */
1146 nalu_get_exp_golomb_ue( bits
); /* num_ref_idx_l1_default_active_minus1 */
1147 nalu_get_exp_golomb_se( bits
); /* init_qp_minus26 */
1148 lsmash_bits_get( bits
, 1 ); /* constrained_intra_pred_flag */
1149 lsmash_bits_get( bits
, 1 ); /* transform_skip_enabled_flag */
1150 if( lsmash_bits_get( bits
, 1 ) ) /* cu_qp_delta_enabled_flag */
1151 nalu_get_exp_golomb_ue( bits
); /* diff_cu_qp_delta_depth */
1152 nalu_get_exp_golomb_se( bits
); /* cb_qp_offset */
1153 nalu_get_exp_golomb_se( bits
); /* cr_qp_offset */
1154 lsmash_bits_get( bits
, 1 ); /* slice_chroma_qp_offsets_present_flag */
1155 lsmash_bits_get( bits
, 1 ); /* weighted_pred_flag */
1156 lsmash_bits_get( bits
, 1 ); /* weighted_bipred_flag */
1157 lsmash_bits_get( bits
, 1 ) /* transquant_bypass_enabled_flag */;
1158 pps
->tiles_enabled_flag
= lsmash_bits_get( bits
, 1 );
1159 pps
->entropy_coding_sync_enabled_flag
= lsmash_bits_get( bits
, 1 );
1160 return bits
->bs
->error
? LSMASH_ERR_NAMELESS
: 0;
1166 uint8_t *rbsp_buffer
,
1172 lsmash_bits_t
*bits
= info
->bits
;
1175 /* Parse PPS minimally for configuration records. */
1177 if( (err
= hevc_parse_pps_minimally( bits
, &min_pps
, rbsp_buffer
, ebsp
, ebsp_size
)) < 0 )
1179 pps
= hevc_get_pps( info
->pps_list
, min_pps
.pic_parameter_set_id
);
1181 return LSMASH_ERR_NAMELESS
;
1182 memcpy( pps
, &min_pps
, SIZEOF_PPS_EXCLUDING_HEAP
);
1184 hevc_sps_t temp_sps
= info
->sps
;
1185 if( (err
= hevc_activate_sps( info
, pps
->seq_parameter_set_id
)) < 0 )
1187 hevc_sps_t
*sps
= &info
->sps
;
1188 if( pps
->tiles_enabled_flag
)
1190 pps
->num_tile_columns_minus1
= nalu_get_exp_golomb_ue( bits
);
1191 pps
->num_tile_rows_minus1
= nalu_get_exp_golomb_ue( bits
);
1192 if( pps
->num_tile_columns_minus1
>= sps
->PicWidthInCtbsY
1193 || pps
->num_tile_rows_minus1
>= sps
->PicHeightInCtbsY
)
1195 err
= LSMASH_ERR_INVALID_DATA
;
1198 if( (err
= hevc_allocate_tile_sizes( pps
, pps
->num_tile_columns_minus1
+ 1, pps
->num_tile_rows_minus1
+ 1 )) < 0 )
1200 if( lsmash_bits_get( bits
, 1 ) ) /* uniform_spacing_flag */
1202 for( int i
= 0; i
<= pps
->num_tile_columns_minus1
; i
++ )
1203 pps
->colWidth
[i
] = ((i
+ 1) * sps
->PicWidthInCtbsY
) / (pps
->num_tile_columns_minus1
+ 1)
1204 - ( i
* sps
->PicWidthInCtbsY
) / (pps
->num_tile_columns_minus1
+ 1);
1205 for( int j
= 0; j
<= pps
->num_tile_rows_minus1
; j
++ )
1206 pps
->rowHeight
[j
] = ((j
+ 1) * sps
->PicHeightInCtbsY
) / (pps
->num_tile_rows_minus1
+ 1)
1207 - ( j
* sps
->PicHeightInCtbsY
) / (pps
->num_tile_rows_minus1
+ 1);
1211 pps
->colWidth
[ pps
->num_tile_columns_minus1
] = sps
->PicWidthInCtbsY
;
1212 for( uint64_t i
= 0; i
< pps
->num_tile_columns_minus1
; i
++ )
1214 pps
->colWidth
[i
] = nalu_get_exp_golomb_ue( bits
) + 1; /* column_width_minus1[i] */
1215 pps
->colWidth
[ pps
->num_tile_columns_minus1
] -= pps
->colWidth
[i
];
1217 pps
->rowHeight
[ pps
->num_tile_rows_minus1
] = sps
->PicHeightInCtbsY
;
1218 for( uint64_t j
= 0; j
< pps
->num_tile_rows_minus1
; j
++ )
1220 pps
->rowHeight
[j
] = nalu_get_exp_golomb_ue( bits
) + 1; /* row_height_minus1 [j] */
1221 pps
->rowHeight
[ pps
->num_tile_rows_minus1
] -= pps
->rowHeight
[j
];
1225 for( uint64_t i
= 0; i
< pps
->num_tile_columns_minus1
; i
++ )
1226 pps
->colBd
[i
+ 1] = pps
->colBd
[i
] + pps
->colWidth
[i
];
1228 for( uint64_t j
= 0; j
< pps
->num_tile_rows_minus1
; j
++ )
1229 pps
->rowBd
[j
+ 1] = pps
->rowBd
[j
] + pps
->rowHeight
[j
];
1230 lsmash_bits_get( bits
, 1 ); /* loop_filter_across_tiles_enabled_flag */
1234 pps
->num_tile_columns_minus1
= 0;
1235 pps
->num_tile_rows_minus1
= 0;
1236 if( (err
= hevc_allocate_tile_sizes( pps
, 1, 1 )) < 0 )
1238 pps
->colWidth
[0] = sps
->PicWidthInCtbsY
;
1239 pps
->rowHeight
[0] = sps
->PicHeightInCtbsY
;
1244 /* Skip PPS extension. */
1245 lsmash_bits_empty( bits
);
1246 if( bits
->bs
->error
)
1250 hevc_activate_vps( info
, info
->sps
.video_parameter_set_id
);
1254 info
->sps
= temp_sps
;
1260 lsmash_bits_t
*bits
,
1264 hevc_nalu_header_t
*nuh
,
1265 uint8_t *rbsp_buffer
,
1271 int err
= nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, &rbsp_size
, ebsp
, ebsp_size
);
1274 uint8_t *rbsp_start
= rbsp_buffer
;
1275 uint64_t rbsp_pos
= 0;
1279 uint32_t payloadType
= 0;
1280 for( uint8_t temp
= lsmash_bits_get( bits
, 8 ); ; temp
= lsmash_bits_get( bits
, 8 ) )
1283 * otherwise: last_payload_type_byte */
1284 payloadType
+= temp
;
1289 uint32_t payloadSize
= 0;
1290 for( uint8_t temp
= lsmash_bits_get( bits
, 8 ); ; temp
= lsmash_bits_get( bits
, 8 ) )
1293 * otherwise: last_payload_size_byte */
1294 payloadSize
+= temp
;
1299 if( nuh
->nal_unit_type
== HEVC_NALU_TYPE_PREFIX_SEI
)
1301 if( payloadType
== 1 )
1304 hevc_hrd_t
*hrd
= sps
? &sps
->vui
.hrd
: vps
? &vps
->hrd
[0] : NULL
;
1306 goto skip_sei_message
; /* Any active VPS or SPS is not found. */
1307 sei
->pic_timing
.present
= 1;
1308 if( (sps
&& sps
->vui
.frame_field_info_present_flag
) || vps
->frame_field_info_present_flag
)
1310 sei
->pic_timing
.pic_struct
= lsmash_bits_get( bits
, 4 );
1311 lsmash_bits_get( bits
, 2 ); /* source_scan_type */
1312 lsmash_bits_get( bits
, 1 ); /* duplicate_flag */
1314 if( hrd
->CpbDpbDelaysPresentFlag
)
1316 lsmash_bits_get( bits
, hrd
->au_cpb_removal_delay_length
); /* au_cpb_removal_delay_minus1 */
1317 lsmash_bits_get( bits
, hrd
->dpb_output_delay_length
); /* pic_dpb_output_delay */
1318 if( hrd
->sub_pic_hrd_params_present_flag
)
1320 lsmash_bits_get( bits
, hrd
->dpb_output_delay_du_length
); /* pic_dpb_output_du_delay */
1321 if( hrd
->sub_pic_cpb_params_in_pic_timing_sei_flag
)
1323 uint64_t num_decoding_units_minus1
= nalu_get_exp_golomb_ue( bits
);
1324 int du_common_cpb_removal_delay_flag
= lsmash_bits_get( bits
, 1 );
1325 if( du_common_cpb_removal_delay_flag
)
1326 /* du_common_cpb_removal_delay_increment_minus1 */
1327 lsmash_bits_get( bits
, hrd
->du_cpb_removal_delay_increment_length
);
1328 for( uint64_t i
= 0; i
<= num_decoding_units_minus1
; i
++ )
1330 nalu_get_exp_golomb_ue( bits
); /* num_nalus_in_du_minus1 */
1331 if( !du_common_cpb_removal_delay_flag
&& i
< num_decoding_units_minus1
)
1332 nalu_get_exp_golomb_ue( bits
); /* du_cpb_removal_delay_increment_minus1 */
1338 else if( payloadType
== 3 )
1341 * FIXME: remove if array_completeness equal to 1. */
1342 return LSMASH_ERR_PATCH_WELCOME
;
1344 else if( payloadType
== 6 )
1346 /* recovery_point */
1347 sei
->recovery_point
.present
= 1;
1348 sei
->recovery_point
.recovery_poc_cnt
= nalu_get_exp_golomb_se( bits
);
1349 lsmash_bits_get( bits
, 1 ); /* exact_match_flag */
1350 sei
->recovery_point
.broken_link_flag
= lsmash_bits_get( bits
, 1 );
1353 goto skip_sei_message
;
1355 else if( nuh
->nal_unit_type
== HEVC_NALU_TYPE_SUFFIX_SEI
)
1357 if( payloadType
== 3 )
1360 * FIXME: remove if array_completeness equal to 1. */
1361 return LSMASH_ERR_PATCH_WELCOME
;
1364 goto skip_sei_message
;
1369 lsmash_bits_get( bits
, payloadSize
* 8 );
1371 lsmash_bits_get_align( bits
);
1372 rbsp_pos
+= payloadSize
;
1373 if( rbsp_pos
> rbsp_size
)
1375 rbsp_pos
= rbsp_size
;
1376 if( *(rbsp_start
+ rbsp_pos
) != 0x80 )
1378 lsmash_log( NULL
, LSMASH_LOG_ERROR
, "Invalid payloadSize is there within H.265/HEVC sei_message().\n" );
1379 return LSMASH_ERR_INVALID_DATA
;
1383 /* The last payloadSize is invalid but it probably can do recovery, so just log warning and break loop here. */
1384 lsmash_log( NULL
, LSMASH_LOG_WARNING
, "Invalid payloadSize is there within H.265/HEVC sei_message().\n" );
1385 break; /* redundant but for readability */
1388 } while( *(rbsp_start
+ rbsp_pos
) != 0x80 ); /* All SEI messages are byte aligned at their end.
1389 * Therefore, 0x80 shall be rbsp_trailing_bits(). */
1390 lsmash_bits_empty( bits
);
1391 return bits
->bs
->error
? LSMASH_ERR_NAMELESS
: 0;
1394 int hevc_parse_slice_segment_header
1397 hevc_nalu_header_t
*nuh
,
1398 uint8_t *rbsp_buffer
,
1403 lsmash_bits_t
*bits
= info
->bits
;
1405 int err
= nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, &rbsp_size
, ebsp
, LSMASH_MIN( ebsp_size
, 50 ) );
1408 hevc_slice_info_t
*slice
= &info
->slice
;
1409 memset( slice
, 0, sizeof(hevc_slice_info_t
) );
1410 slice
->nalu_type
= nuh
->nal_unit_type
;
1411 slice
->TemporalId
= nuh
->TemporalId
;
1412 slice
->first_slice_segment_in_pic_flag
= lsmash_bits_get( bits
, 1 );
1413 if( nuh
->nal_unit_type
>= HEVC_NALU_TYPE_BLA_W_LP
1414 && nuh
->nal_unit_type
<= HEVC_NALU_TYPE_RSV_IRAP_VCL23
)
1415 lsmash_bits_get( bits
, 1 ); /* no_output_of_prior_pics_flag */
1416 slice
->pic_parameter_set_id
= nalu_get_exp_golomb_ue( bits
);
1417 /* Get PPS by slice_pic_parameter_set_id. */
1418 hevc_pps_t
*pps
= hevc_get_pps( info
->pps_list
, slice
->pic_parameter_set_id
);
1420 return LSMASH_ERR_NAMELESS
;
1421 /* Get SPS by pps_seq_parameter_set_id. */
1422 hevc_sps_t
*sps
= hevc_get_sps( info
->sps_list
, pps
->seq_parameter_set_id
);
1424 return LSMASH_ERR_NAMELESS
;
1425 slice
->video_parameter_set_id
= sps
->video_parameter_set_id
;
1426 slice
->seq_parameter_set_id
= pps
->seq_parameter_set_id
;
1427 if( !slice
->first_slice_segment_in_pic_flag
)
1429 slice
->dependent_slice_segment_flag
= pps
->dependent_slice_segments_enabled_flag
? lsmash_bits_get( bits
, 1 ) : 0;
1430 slice
->segment_address
= lsmash_bits_get( bits
, lsmash_ceil_log2( sps
->PicSizeInCtbsY
) );
1434 slice
->dependent_slice_segment_flag
= 0;
1435 slice
->segment_address
= 0;
1437 if( !slice
->dependent_slice_segment_flag
)
1439 /* independent slice segment
1440 * The values of the slice segment header of dependent slice segment are inferred from the values
1441 * for the preceding independent slice segment in decoding order, if some of the values are not present. */
1442 for( int i
= 0; i
< pps
->num_extra_slice_header_bits
; i
++ )
1443 lsmash_bits_get( bits
, 1 ); /* slice_reserved_flag[i] */
1444 slice
->type
= nalu_get_exp_golomb_ue( bits
);
1445 if( pps
->output_flag_present_flag
)
1446 lsmash_bits_get( bits
, 1 ); /* pic_output_flag */
1447 if( sps
->separate_colour_plane_flag
)
1448 lsmash_bits_get( bits
, 1 ); /* colour_plane_id */
1449 if( nuh
->nal_unit_type
!= HEVC_NALU_TYPE_IDR_W_RADL
1450 && nuh
->nal_unit_type
!= HEVC_NALU_TYPE_IDR_N_LP
)
1452 slice
->pic_order_cnt_lsb
= lsmash_bits_get( bits
, sps
->log2_max_pic_order_cnt_lsb
);
1453 if( !lsmash_bits_get( bits
, 1 ) ) /* short_term_ref_pic_set_sps_flag */
1455 if( (err
= hevc_short_term_ref_pic_set( bits
, sps
, sps
->num_short_term_ref_pic_sets
)) < 0 )
1460 int length
= lsmash_ceil_log2( sps
->num_short_term_ref_pic_sets
);
1462 lsmash_bits_get( bits
, length
); /* short_term_ref_pic_set_idx */
1464 if( sps
->long_term_ref_pics_present_flag
)
1466 uint64_t num_long_term_sps
= sps
->num_long_term_ref_pics_sps
> 0 ? nalu_get_exp_golomb_ue( bits
) : 0;
1467 uint64_t num_long_term_pics
= nalu_get_exp_golomb_ue( bits
);
1468 for( uint64_t i
= 0; i
< num_long_term_sps
+ num_long_term_pics
; i
++ )
1470 if( i
< num_long_term_sps
)
1472 int length
= lsmash_ceil_log2( sps
->num_long_term_ref_pics_sps
);
1474 lsmash_bits_get( bits
, length
); /* lt_idx_sps[i] */
1478 lsmash_bits_get( bits
, sps
->log2_max_pic_order_cnt_lsb
); /* poc_lsb_lt [i] */
1479 lsmash_bits_get( bits
, 1 ); /* used_by_curr_pic_lt_flag[i] */
1481 if( lsmash_bits_get( bits
, 1 ) ) /* delta_poc_msb_present_flag[i] */
1482 nalu_get_exp_golomb_ue( bits
); /* delta_poc_msb_cycle_lt [i] */
1485 if( sps
->temporal_mvp_enabled_flag
)
1486 lsmash_bits_get( bits
, 1 ); /* slice_temporal_mvp_enabled_flag */
1489 /* For IDR-pictures, slice_pic_order_cnt_lsb is inferred to be 0. */
1490 slice
->pic_order_cnt_lsb
= 0;
1492 lsmash_bits_empty( bits
);
1493 if( bits
->bs
->error
)
1494 return LSMASH_ERR_NAMELESS
;
1500 static int hevc_get_vps_id
1503 uint32_t ps_ebsp_length
,
1507 /* the number of bits of vps_id = 4
1508 * (4 - 1) / 8 + 1 = 1 bytes */
1509 *ps_id
= (*ps_ebsp
>> 4) & 0x0F; /* vps_video_parameter_set_id */
1513 static int hevc_get_sps_id
1516 uint32_t ps_ebsp_length
,
1520 /* the maximum number of bits of sps_id = 9: 0b00001XXXX
1521 * (8 + 688 + 9 - 1) / 8 + 1 = 89 bytes
1522 * Here more additional bytes because there might be emulation_prevention_three_byte(s). */
1523 lsmash_bits_t bits
= { 0 };
1524 lsmash_bs_t bs
= { 0 };
1525 uint8_t rbsp_buffer
[128];
1526 uint8_t buffer
[128];
1527 bs
.buffer
.data
= buffer
;
1528 bs
.buffer
.alloc
= 128;
1529 lsmash_bits_init( &bits
, &bs
);
1531 int err
= nalu_import_rbsp_from_ebsp( &bits
, rbsp_buffer
, &rbsp_size
, ps_ebsp
, LSMASH_MIN( ps_ebsp_length
, 128 ) );
1534 /* Skip sps_video_parameter_set_id and sps_temporal_id_nesting_flag. */
1535 uint8_t sps_max_sub_layers_minus1
= (lsmash_bits_get( &bits
, 8 ) >> 1) & 0x07;
1536 /* profile_tier_level() costs at most 688 bits. */
1538 hevc_parse_profile_tier_level( &bits
, &sps_ptl
, sps_max_sub_layers_minus1
);
1539 uint64_t sps_seq_parameter_set_id
= nalu_get_exp_golomb_ue( &bits
);
1540 if( sps_seq_parameter_set_id
> HEVC_MAX_SPS_ID
)
1541 return LSMASH_ERR_INVALID_DATA
;
1542 *ps_id
= sps_seq_parameter_set_id
;
1543 return bs
.error
? LSMASH_ERR_NAMELESS
: 0;
1546 static int hevc_get_pps_id
1549 uint32_t ps_ebsp_length
,
1553 /* the maximum number of bits of pps_id = 13: 0b0000001XXXXXX
1554 * (13 - 1) / 8 + 1 = 2 bytes
1555 * Why +1? Because there might be an emulation_prevention_three_byte. */
1556 lsmash_bits_t bits
= { 0 };
1557 lsmash_bs_t bs
= { 0 };
1558 uint8_t rbsp_buffer
[3];
1560 bs
.buffer
.data
= buffer
;
1561 bs
.buffer
.alloc
= 3;
1562 lsmash_bits_init( &bits
, &bs
);
1564 int err
= nalu_import_rbsp_from_ebsp( &bits
, rbsp_buffer
, &rbsp_size
, ps_ebsp
, LSMASH_MIN( ps_ebsp_length
, 3 ) );
1567 uint64_t pic_parameter_set_id
= nalu_get_exp_golomb_ue( &bits
);
1568 if( pic_parameter_set_id
> HEVC_MAX_PPS_ID
)
1569 return LSMASH_ERR_INVALID_DATA
;
1570 *ps_id
= pic_parameter_set_id
;
1571 return bs
.error
? LSMASH_ERR_NAMELESS
: 0;
1574 static inline int hevc_get_ps_id
1577 uint32_t ps_ebsp_length
,
1579 lsmash_hevc_dcr_nalu_type ps_type
1582 int (*get_ps_id
)( uint8_t *ps_ebsp
, uint32_t ps_ebsp_length
, uint8_t *ps_id
)
1583 = ps_type
== HEVC_DCR_NALU_TYPE_VPS
? hevc_get_vps_id
1584 : ps_type
== HEVC_DCR_NALU_TYPE_SPS
? hevc_get_sps_id
1585 : ps_type
== HEVC_DCR_NALU_TYPE_PPS
? hevc_get_pps_id
1587 return get_ps_id
? get_ps_id( ps_ebsp
, ps_ebsp_length
, ps_id
) : LSMASH_ERR_INVALID_DATA
;
1590 static inline hevc_parameter_array_t
*hevc_get_parameter_set_array
1592 lsmash_hevc_specific_parameters_t
*param
,
1593 lsmash_hevc_dcr_nalu_type ps_type
1596 if( !param
->parameter_arrays
)
1598 if( ps_type
>= HEVC_DCR_NALU_TYPE_NUM
)
1600 return ¶m
->parameter_arrays
->ps_array
[ps_type
];
1603 static inline lsmash_entry_list_t
*hevc_get_parameter_set_list
1605 lsmash_hevc_specific_parameters_t
*param
,
1606 lsmash_hevc_dcr_nalu_type ps_type
1609 if( !param
->parameter_arrays
)
1611 if( ps_type
>= HEVC_DCR_NALU_TYPE_NUM
)
1613 return param
->parameter_arrays
->ps_array
[ps_type
].list
;
1616 static lsmash_entry_t
*hevc_get_ps_entry_from_param
1618 lsmash_hevc_specific_parameters_t
*param
,
1619 lsmash_hevc_dcr_nalu_type ps_type
,
1623 int (*get_ps_id
)( uint8_t *ps_ebsp
, uint32_t ps_ebsp_length
, uint8_t *ps_id
)
1624 = ps_type
== HEVC_DCR_NALU_TYPE_VPS
? hevc_get_vps_id
1625 : ps_type
== HEVC_DCR_NALU_TYPE_SPS
? hevc_get_sps_id
1626 : ps_type
== HEVC_DCR_NALU_TYPE_PPS
? hevc_get_pps_id
1630 lsmash_entry_list_t
*list
= hevc_get_parameter_set_list( param
, ps_type
);
1633 for( lsmash_entry_t
*entry
= list
->head
; entry
; entry
= entry
->next
)
1635 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1638 uint8_t param_ps_id
;
1639 if( get_ps_id( ps
->nalUnit
+ HEVC_MIN_NALU_HEADER_LENGTH
,
1640 ps
->nalUnitLength
- HEVC_MIN_NALU_HEADER_LENGTH
, ¶m_ps_id
) < 0 )
1642 if( ps_id
== param_ps_id
)
1648 static inline void hevc_update_picture_type
1650 hevc_picture_info_t
*picture
,
1651 hevc_slice_info_t
*slice
1654 if( picture
->type
== HEVC_PICTURE_TYPE_I_P
)
1656 if( slice
->type
== HEVC_SLICE_TYPE_B
)
1657 picture
->type
= HEVC_PICTURE_TYPE_I_P_B
;
1659 else if( picture
->type
== HEVC_PICTURE_TYPE_I
)
1661 if( slice
->type
== HEVC_SLICE_TYPE_P
)
1662 picture
->type
= HEVC_PICTURE_TYPE_I_P
;
1663 else if( slice
->type
== HEVC_SLICE_TYPE_B
)
1664 picture
->type
= HEVC_PICTURE_TYPE_I_P_B
;
1666 else if( picture
->type
== HEVC_PICTURE_TYPE_NONE
)
1668 if( slice
->type
== HEVC_SLICE_TYPE_P
)
1669 picture
->type
= HEVC_PICTURE_TYPE_I_P
;
1670 else if( slice
->type
== HEVC_SLICE_TYPE_B
)
1671 picture
->type
= HEVC_PICTURE_TYPE_I_P_B
;
1672 else if( slice
->type
== HEVC_SLICE_TYPE_I
)
1673 picture
->type
= HEVC_PICTURE_TYPE_I
;
1676 fprintf( stderr
, "Picture type = %s\n", picture
->type
== HEVC_PICTURE_TYPE_I_P
? "P"
1677 : picture
->type
== HEVC_PICTURE_TYPE_I_P_B
? "B"
1678 : picture
->type
== HEVC_PICTURE_TYPE_I
? "I" );
1682 /* Shall be called at least once per picture. */
1683 void hevc_update_picture_info_for_slice
1686 hevc_picture_info_t
*picture
,
1687 hevc_slice_info_t
*slice
1691 picture
->has_primary
|= !slice
->dependent_slice_segment_flag
;
1692 hevc_update_picture_type( picture
, slice
);
1693 /* Mark 'used' on active parameter sets. */
1694 uint8_t ps_id
[3] = { slice
->video_parameter_set_id
, slice
->seq_parameter_set_id
, slice
->pic_parameter_set_id
};
1695 for( int i
= 0; i
< 3; i
++ )
1697 lsmash_hevc_dcr_nalu_type ps_type
= (lsmash_hevc_dcr_nalu_type
)i
;
1698 lsmash_entry_t
*entry
= hevc_get_ps_entry_from_param( &info
->hvcC_param
, ps_type
, ps_id
[i
] );
1699 if( entry
&& entry
->data
)
1701 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1703 lsmash_append_hevc_dcr_nalu( &info
->hvcC_param
, ps_type
, ps
->nalUnit
, ps
->nalUnitLength
);
1706 /* Discard this slice info. */
1710 /* Shall be called exactly once per picture. */
1711 void hevc_update_picture_info
1714 hevc_picture_info_t
*picture
,
1715 hevc_slice_info_t
*slice
,
1720 picture
->irap
= slice
->nalu_type
>= HEVC_NALU_TYPE_BLA_W_LP
&& slice
->nalu_type
<= HEVC_NALU_TYPE_CRA
;
1721 picture
->idr
= slice
->nalu_type
== HEVC_NALU_TYPE_IDR_W_RADL
|| slice
->nalu_type
== HEVC_NALU_TYPE_IDR_N_LP
;
1722 picture
->broken_link
= slice
->nalu_type
>= HEVC_NALU_TYPE_BLA_W_LP
&& slice
->nalu_type
<= HEVC_NALU_TYPE_BLA_N_LP
;
1723 picture
->radl
= slice
->nalu_type
== HEVC_NALU_TYPE_RADL_N
|| slice
->nalu_type
== HEVC_NALU_TYPE_RADL_R
;
1724 picture
->rasl
= slice
->nalu_type
== HEVC_NALU_TYPE_RASL_N
|| slice
->nalu_type
== HEVC_NALU_TYPE_RASL_R
;
1725 picture
->sublayer_nonref
= slice
->nalu_type
<= HEVC_NALU_TYPE_RSV_VCL_R15
&& ((slice
->nalu_type
& 0x01) == 0);
1726 picture
->closed_rap
= slice
->nalu_type
>= HEVC_NALU_TYPE_BLA_W_RADL
&& slice
->nalu_type
<= HEVC_NALU_TYPE_IDR_N_LP
;
1727 picture
->random_accessible
= picture
->irap
;
1728 picture
->TemporalId
= slice
->TemporalId
;
1729 picture
->pic_parameter_set_id
= slice
->pic_parameter_set_id
;
1730 picture
->poc_lsb
= slice
->pic_order_cnt_lsb
;
1731 hevc_update_picture_info_for_slice( info
, picture
, slice
);
1732 picture
->independent
= (picture
->type
== HEVC_PICTURE_TYPE_I
);
1733 picture
->field_coded
= sps
->vui
.field_seq_flag
;
1734 if( sei
->pic_timing
.present
)
1736 if( sei
->pic_timing
.pic_struct
< 13 )
1738 static const uint8_t delta
[13] = { 2, 1, 1, 2, 2, 3, 3, 4, 6, 1, 1, 1, 1 };
1739 picture
->delta
= delta
[ sei
->pic_timing
.pic_struct
];
1742 /* Reserved values in the spec we refer to. */
1743 picture
->delta
= picture
->field_coded
? 1 : 2;
1744 sei
->pic_timing
.present
= 0;
1747 picture
->delta
= picture
->field_coded
? 1 : 2;
1748 if( sei
->recovery_point
.present
)
1750 picture
->random_accessible
|= sei
->recovery_point
.present
;
1751 picture
->recovery_poc_cnt
= sei
->recovery_point
.recovery_poc_cnt
;
1752 picture
->broken_link
|= sei
->recovery_point
.broken_link_flag
;
1753 sei
->recovery_point
.present
= 0;
1756 picture
->recovery_poc_cnt
= 0;
1759 static uint64_t hevc_get_ctb_address_in_tile_scan
1763 uint64_t segment_address
,
1767 uint64_t tbX
= segment_address
% sps
->PicWidthInCtbsY
;
1768 uint64_t tbY
= segment_address
/ sps
->PicWidthInCtbsY
;
1769 uint32_t tileX
= pps
->num_tile_columns_minus1
;
1770 for( uint32_t i
= 0; i
<= pps
->num_tile_columns_minus1
; i
++ )
1771 if( tbX
>= pps
->colBd
[i
] )
1773 uint32_t tileY
= pps
->num_tile_rows_minus1
;
1774 for( uint32_t j
= 0; j
<= pps
->num_tile_rows_minus1
; j
++ )
1775 if( tbY
>= pps
->rowBd
[j
] )
1777 uint64_t CtbAddrInTs
= 0;
1778 for( uint32_t i
= 0; i
< tileX
; i
++ )
1779 CtbAddrInTs
+= pps
->rowHeight
[tileY
] * pps
->colWidth
[i
];
1780 for( uint32_t j
= 0; j
< tileY
; j
++ )
1781 CtbAddrInTs
+= sps
->PicWidthInCtbsY
* pps
->rowHeight
[j
];
1782 CtbAddrInTs
+= (tbY
- pps
->rowBd
[tileY
]) * pps
->colWidth
[tileX
] + tbX
- pps
->colBd
[tileX
];
1783 *TileId
= (uint64_t)tileY
* (pps
->num_tile_columns_minus1
+ 1) + tileX
;
1787 int hevc_find_au_delimit_by_slice_info
1790 hevc_slice_info_t
*slice
,
1791 hevc_slice_info_t
*prev_slice
1794 /* 7.4.2.4.5 Order of VCL NAL units and association to coded pictures
1795 * - The first VCL NAL unit of the coded picture shall have first_slice_segment_in_pic_flag equal to 1. */
1796 if( slice
->first_slice_segment_in_pic_flag
)
1798 /* The value of TemporalId shall be the same for all VCL NAL units of an access unit. */
1799 if( slice
->TemporalId
!= prev_slice
->TemporalId
)
1801 /* 7.4.2.4.5 Order of VCL NAL units and association to coded pictures
1802 * When either of the following conditions is true, both the current and the previous coded slice segment NAL units
1803 * shall belong to the same coded picture.
1804 * - TileId[ CtbAddrRsToTs[ prev_slice->segment_address ] ] < TileId[ CtbAddrRsToTs[ slice->segment_address ] ]
1805 * - TileId[ CtbAddrRsToTs[ prev_slice->segment_address ] ] == TileId[ CtbAddrRsToTs[ slice->segment_address ] ]
1806 * && CtbAddrRsToTs[ prev_slice->segment_address ] < CtbAddrRsToTs[ slice->segment_address ]
1808 hevc_pps_t
*prev_pps
= hevc_get_pps( info
->pps_list
, prev_slice
->pic_parameter_set_id
);
1811 hevc_sps_t
*prev_sps
= hevc_get_sps( info
->sps_list
, prev_pps
->seq_parameter_set_id
);
1814 uint64_t currTileId
;
1815 uint64_t prevTileId
;
1816 uint64_t currCtbAddrInTs
= hevc_get_ctb_address_in_tile_scan( &info
->sps
, &info
->pps
, slice
->segment_address
, &currTileId
);
1817 uint64_t prevCtbAddrInTs
= hevc_get_ctb_address_in_tile_scan( prev_sps
, prev_pps
, prev_slice
->segment_address
, &prevTileId
);
1818 if( prevTileId
< currTileId
)
1820 if( prevTileId
== currTileId
&& prevCtbAddrInTs
< currCtbAddrInTs
)
1825 int hevc_find_au_delimit_by_nalu_type
1828 uint8_t prev_nalu_type
1831 /* 7.4.2.4.4 Order of NAL units and coded pictures and their association to access units */
1832 if( prev_nalu_type
<= HEVC_NALU_TYPE_RSV_VCL31
)
1833 /* The first of any of the following NAL units after the last VCL NAL unit of a coded picture
1834 * specifies the start of a new access unit:
1835 * - access unit delimiter NAL unit (when present)
1836 * - VPS NAL unit (when present)
1837 * - SPS NAL unit (when present)
1838 * - PPS NAL unit (when present)
1839 * - Prefix SEI NAL unit (when present)
1840 * - NAL units with nal_unit_type in the range of RSV_NVCL41..RSV_NVCL44 (when present)
1841 * - NAL units with nal_unit_type in the range of UNSPEC48..UNSPEC55 (when present)
1842 * - first VCL NAL unit of a coded picture (always present) */
1843 return (nalu_type
>= HEVC_NALU_TYPE_VPS
&& nalu_type
<= HEVC_NALU_TYPE_AUD
)
1844 || (nalu_type
== HEVC_NALU_TYPE_PREFIX_SEI
)
1845 || (nalu_type
>= HEVC_NALU_TYPE_RSV_NVCL41
&& nalu_type
<= HEVC_NALU_TYPE_RSV_NVCL44
)
1846 || (nalu_type
>= HEVC_NALU_TYPE_UNSPEC48
&& nalu_type
<= HEVC_NALU_TYPE_UNSPEC55
);
1847 else if( prev_nalu_type
== HEVC_NALU_TYPE_EOS
)
1848 /* An end of sequence NAL unit shall be the last NAL unit in the access unit unless the next
1849 * NAL unit is an end of bitstream NAL unit. */
1850 return (nalu_type
!= HEVC_NALU_TYPE_EOB
);
1852 /* An end of bitstream NAL unit shall be the last NAL unit in the access unit.
1853 * Thus, the next NAL unit shall be the first NAL unit in the next access unit. */
1854 return (prev_nalu_type
== HEVC_NALU_TYPE_EOB
);
1857 int hevc_supplement_buffer
1859 hevc_stream_buffer_t
*sb
,
1860 hevc_access_unit_t
*au
,
1864 lsmash_multiple_buffers_t
*bank
= lsmash_resize_multiple_buffers( sb
->bank
, size
);
1866 return LSMASH_ERR_MEMORY_ALLOC
;
1868 sb
->rbsp
= lsmash_withdraw_buffer( bank
, 1 );
1869 if( au
&& bank
->number_of_buffers
== 3 )
1871 au
->data
= lsmash_withdraw_buffer( bank
, 2 );
1872 au
->incomplete_data
= lsmash_withdraw_buffer( bank
, 3 );
1877 static void hevc_bs_put_parameter_sets
1880 lsmash_entry_list_t
*dcr_ps_list
,
1881 uint32_t max_dcr_ps_count
1884 uint32_t dcr_ps_count
= 0;
1885 for( lsmash_entry_t
*entry
= dcr_ps_list
->head
; entry
&& dcr_ps_count
< max_dcr_ps_count
; entry
= entry
->next
)
1887 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1888 if( ps
&& !ps
->unused
)
1890 lsmash_bs_put_be16( bs
, ps
->nalUnitLength
);
1891 lsmash_bs_put_bytes( bs
, ps
->nalUnitLength
, ps
->nalUnit
);
1899 uint8_t *lsmash_create_hevc_specific_info
1901 lsmash_hevc_specific_parameters_t
*param
,
1902 uint32_t *data_length
1905 if( !param
|| !param
->parameter_arrays
|| !data_length
)
1907 if( param
->lengthSizeMinusOne
!= 0
1908 && param
->lengthSizeMinusOne
!= 1
1909 && param
->lengthSizeMinusOne
!= 3 )
1911 hevc_parameter_array_t
*param_arrays
[HEVC_DCR_NALU_TYPE_NUM
];
1912 lsmash_entry_list_t
*dcr_ps_list
[HEVC_DCR_NALU_TYPE_NUM
];
1913 for( int i
= 0; i
< HEVC_DCR_NALU_TYPE_NUM
; i
++ )
1915 param_arrays
[i
] = ¶m
->parameter_arrays
->ps_array
[i
];
1916 dcr_ps_list
[i
] = param_arrays
[i
]->list
;
1918 /* VPS, SPS and PPS are mandatory. */
1919 if( !dcr_ps_list
[0] || !dcr_ps_list
[0]->head
|| dcr_ps_list
[0]->entry_count
== 0
1920 || !dcr_ps_list
[1] || !dcr_ps_list
[1]->head
|| dcr_ps_list
[1]->entry_count
== 0
1921 || !dcr_ps_list
[2] || !dcr_ps_list
[2]->head
|| dcr_ps_list
[2]->entry_count
== 0 )
1923 /* Calculate enough buffer size. */
1924 static const uint32_t max_dcr_ps_count
[HEVC_DCR_NALU_TYPE_NUM
] =
1926 HEVC_MAX_VPS_ID
+ 1,
1927 HEVC_MAX_SPS_ID
+ 1,
1928 HEVC_MAX_PPS_ID
+ 1,
1932 uint32_t ps_count
[HEVC_DCR_NALU_TYPE_NUM
] = { 0 };
1933 for( int i
= 0; i
< HEVC_DCR_NALU_TYPE_NUM
; i
++ )
1934 if( dcr_ps_list
[i
] )
1935 for( lsmash_entry_t
*entry
= dcr_ps_list
[i
]->head
; entry
&& ps_count
[i
] < max_dcr_ps_count
[i
]; entry
= entry
->next
)
1937 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1944 /* Create an HEVCConfigurationBox */
1945 lsmash_bs_t
*bs
= lsmash_bs_create();
1948 lsmash_bs_put_be32( bs
, 0 ); /* box size */
1949 lsmash_bs_put_be32( bs
, ISOM_BOX_TYPE_HVCC
.fourcc
); /* box type: 'hvcC' */
1950 lsmash_bs_put_byte( bs
, HVCC_CONFIGURATION_VERSION
); /* configurationVersion */
1951 uint8_t temp8
= (param
->general_profile_space
<< 6)
1952 | (param
->general_tier_flag
<< 5)
1953 | param
->general_profile_idc
;
1954 lsmash_bs_put_byte( bs
, temp8
);
1955 lsmash_bs_put_be32( bs
, param
->general_profile_compatibility_flags
);
1956 lsmash_bs_put_be32( bs
, param
->general_constraint_indicator_flags
>> 16 );
1957 lsmash_bs_put_be16( bs
, param
->general_constraint_indicator_flags
);
1958 lsmash_bs_put_byte( bs
, param
->general_level_idc
);
1959 lsmash_bs_put_be16( bs
, param
->min_spatial_segmentation_idc
| 0xF000 );
1960 lsmash_bs_put_byte( bs
, param
->parallelismType
| 0xFC );
1961 lsmash_bs_put_byte( bs
, param
->chromaFormat
| 0xFC );
1962 lsmash_bs_put_byte( bs
, param
->bitDepthLumaMinus8
| 0xF8 );
1963 lsmash_bs_put_byte( bs
, param
->bitDepthChromaMinus8
| 0xF8 );
1964 lsmash_bs_put_be16( bs
, param
->avgFrameRate
);
1965 temp8
= (param
->constantFrameRate
<< 6)
1966 | (param
->numTemporalLayers
<< 3)
1967 | (param
->temporalIdNested
<< 2)
1968 | param
->lengthSizeMinusOne
;
1969 lsmash_bs_put_byte( bs
, temp8
);
1970 uint8_t numOfArrays
= !!ps_count
[0]
1975 lsmash_bs_put_byte( bs
, numOfArrays
);
1976 for( uint8_t i
= 0; i
< numOfArrays
; i
++ )
1978 temp8
= (param_arrays
[i
]->array_completeness
<< 7) | param_arrays
[i
]->NAL_unit_type
;
1979 lsmash_bs_put_byte( bs
, temp8
);
1980 lsmash_bs_put_be16( bs
, ps_count
[i
] );
1981 hevc_bs_put_parameter_sets( bs
, dcr_ps_list
[i
], ps_count
[i
] );
1983 uint8_t *data
= lsmash_bs_export_data( bs
, data_length
);
1984 lsmash_bs_cleanup( bs
);
1985 /* Update box size. */
1986 LSMASH_SET_BE32( data
, *data_length
);
1990 static inline int hevc_validate_dcr_nalu_type
1992 lsmash_hevc_dcr_nalu_type ps_type
,
1997 if( !ps_data
|| ps_length
< 3 )
1998 return LSMASH_ERR_INVALID_DATA
;
1999 if( ps_type
!= HEVC_DCR_NALU_TYPE_VPS
2000 && ps_type
!= HEVC_DCR_NALU_TYPE_SPS
2001 && ps_type
!= HEVC_DCR_NALU_TYPE_PPS
2002 && ps_type
!= HEVC_DCR_NALU_TYPE_PREFIX_SEI
2003 && ps_type
!= HEVC_DCR_NALU_TYPE_SUFFIX_SEI
)
2004 return LSMASH_ERR_INVALID_DATA
;
2005 uint8_t nalu_type
= (*((uint8_t *)ps_data
) >> 1) & 0x3f;
2006 if( nalu_type
!= HEVC_NALU_TYPE_VPS
2007 && nalu_type
!= HEVC_NALU_TYPE_SPS
2008 && nalu_type
!= HEVC_NALU_TYPE_PPS
2009 && nalu_type
!= HEVC_NALU_TYPE_PREFIX_SEI
2010 && nalu_type
!= HEVC_NALU_TYPE_SUFFIX_SEI
)
2011 return LSMASH_ERR_INVALID_DATA
;
2012 if( (ps_type
== HEVC_DCR_NALU_TYPE_VPS
&& nalu_type
!= HEVC_NALU_TYPE_VPS
)
2013 || (ps_type
== HEVC_DCR_NALU_TYPE_SPS
&& nalu_type
!= HEVC_NALU_TYPE_SPS
)
2014 || (ps_type
== HEVC_DCR_NALU_TYPE_PPS
&& nalu_type
!= HEVC_NALU_TYPE_PPS
)
2015 || (ps_type
== HEVC_DCR_NALU_TYPE_PREFIX_SEI
&& nalu_type
!= HEVC_NALU_TYPE_PREFIX_SEI
)
2016 || (ps_type
== HEVC_DCR_NALU_TYPE_SUFFIX_SEI
&& nalu_type
!= HEVC_NALU_TYPE_SUFFIX_SEI
) )
2017 return LSMASH_ERR_INVALID_DATA
;
2021 static lsmash_dcr_nalu_appendable hevc_check_vps_appendable
2023 lsmash_bits_t
*bits
,
2024 uint8_t *rbsp_buffer
,
2025 lsmash_hevc_specific_parameters_t
*param
,
2028 lsmash_entry_list_t
*ps_list
2032 if( hevc_parse_vps_minimally( bits
, &vps
, rbsp_buffer
,
2033 ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2034 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
) < 0 )
2035 return DCR_NALU_APPEND_ERROR
;
2036 /* The value of profile_space must be identical in all the parameter sets in a single HEVC Decoder Configuration Record. */
2037 if( vps
.ptl
.general
.profile_space
!= param
->general_profile_space
)
2038 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
2040 if( vps
.ptl
.general
.profile_idc
!= param
->general_profile_idc
)
2041 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
2042 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
; entry
= entry
->next
)
2044 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
2046 return DCR_NALU_APPEND_ERROR
;
2049 uint8_t param_vps_id
;
2050 if( hevc_get_vps_id( ps
->nalUnit
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2051 ps
->nalUnitLength
- HEVC_MIN_NALU_HEADER_LENGTH
, ¶m_vps_id
) < 0 )
2052 return DCR_NALU_APPEND_ERROR
;
2053 if( param_vps_id
== vps
.video_parameter_set_id
)
2054 /* VPS that has the same video_parameter_set_id already exists with different form. */
2055 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
2057 return DCR_NALU_APPEND_POSSIBLE
;
2060 static lsmash_dcr_nalu_appendable hevc_check_sps_appendable
2062 lsmash_bits_t
*bits
,
2063 uint8_t *rbsp_buffer
,
2064 lsmash_hevc_specific_parameters_t
*param
,
2067 lsmash_entry_list_t
*ps_list
2071 if( hevc_parse_sps_minimally( bits
, &sps
, rbsp_buffer
,
2072 ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2073 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
) < 0 )
2074 return DCR_NALU_APPEND_ERROR
;
2075 lsmash_bits_empty( bits
);
2076 /* The values of profile_space, chromaFormat, bitDepthLumaMinus8 and bitDepthChromaMinus8
2077 * must be identical in all the parameter sets in a single HEVC Decoder Configuration Record. */
2078 if( sps
.ptl
.general
.profile_space
!= param
->general_profile_space
2079 || sps
.chroma_format_idc
!= param
->chromaFormat
2080 || sps
.bit_depth_luma_minus8
!= param
->bitDepthLumaMinus8
2081 || sps
.bit_depth_chroma_minus8
!= param
->bitDepthChromaMinus8
)
2082 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
2083 /* FIXME; If the sequence parameter sets are marked with different profiles,
2084 * and the relevant profile compatibility flags are all zero,
2085 * then the stream may need examination to determine which profile, if any, the stream conforms to.
2086 * If the stream is not examined, or the examination reveals that there is no profile to which the stream conforms,
2087 * then the stream must be split into two or more sub-streams with separate configuration records in which these rules can be met. */
2089 if( sps
.ptl
.general
.profile_idc
!= param
->general_profile_idc
2090 && (sps
.ptl
.general
.profile_compatibility_flags
& param
->general_profile_compatibility_flags
) )
2092 if( sps
.ptl
.general
.profile_idc
!= param
->general_profile_idc
)
2094 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
2095 /* Forbidden to duplicate SPS that has the same seq_parameter_set_id with different form within the same configuration record. */
2096 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
; entry
= entry
->next
)
2098 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
2100 return DCR_NALU_APPEND_ERROR
;
2103 uint8_t param_sps_id
;
2104 if( hevc_get_sps_id( ps
->nalUnit
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2105 ps
->nalUnitLength
- HEVC_MIN_NALU_HEADER_LENGTH
, ¶m_sps_id
) < 0 )
2106 return DCR_NALU_APPEND_ERROR
;
2107 if( param_sps_id
== sps
.seq_parameter_set_id
)
2108 /* SPS that has the same seq_parameter_set_id already exists with different form. */
2109 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
2110 if( entry
== ps_list
->head
)
2112 /* Check if the cropped visual presentation sizes, the sample aspect ratios, the colour descriptions and
2113 * the default display windows are different. */
2114 hevc_sps_t first_sps
;
2115 if( hevc_parse_sps_minimally( bits
, &first_sps
, rbsp_buffer
,
2116 ps
->nalUnit
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2117 ps
->nalUnitLength
- HEVC_MIN_NALU_HEADER_LENGTH
) < 0 )
2118 return DCR_NALU_APPEND_ERROR
;
2119 if( sps
.cropped_width
!= first_sps
.cropped_width
2120 || sps
.cropped_height
!= first_sps
.cropped_height
2121 || sps
.vui
.sar_width
!= first_sps
.vui
.sar_width
2122 || sps
.vui
.sar_height
!= first_sps
.vui
.sar_height
2123 || sps
.vui
.colour_primaries
!= first_sps
.vui
.colour_primaries
2124 || sps
.vui
.transfer_characteristics
!= first_sps
.vui
.transfer_characteristics
2125 || sps
.vui
.matrix_coeffs
!= first_sps
.vui
.matrix_coeffs
2126 || sps
.vui
.video_full_range_flag
!= first_sps
.vui
.video_full_range_flag
2127 || sps
.vui
.def_disp_win_offset
.left
.n
!= first_sps
.vui
.def_disp_win_offset
.left
.n
2128 || sps
.vui
.def_disp_win_offset
.right
.n
!= first_sps
.vui
.def_disp_win_offset
.right
.n
2129 || sps
.vui
.def_disp_win_offset
.top
.n
!= first_sps
.vui
.def_disp_win_offset
.top
.n
2130 || sps
.vui
.def_disp_win_offset
.bottom
.n
!= first_sps
.vui
.def_disp_win_offset
.bottom
.n
)
2131 return DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED
;
2134 return DCR_NALU_APPEND_POSSIBLE
;
2137 static lsmash_dcr_nalu_appendable hevc_check_pps_appendable
2141 lsmash_entry_list_t
*ps_list
2145 if( hevc_get_pps_id( ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2146 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
, &pps_id
) < 0 )
2147 return DCR_NALU_APPEND_ERROR
;
2148 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
; entry
= entry
->next
)
2150 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
2152 return DCR_NALU_APPEND_ERROR
;
2155 uint8_t param_pps_id
;
2156 if( hevc_get_pps_id( ps
->nalUnit
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2157 ps
->nalUnitLength
- HEVC_MIN_NALU_HEADER_LENGTH
, ¶m_pps_id
) < 0 )
2158 return DCR_NALU_APPEND_ERROR
;
2159 if( pps_id
== param_pps_id
)
2160 /* PPS that has the same pic_parameter_set_id already exists with different form. */
2161 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
2163 return DCR_NALU_APPEND_POSSIBLE
;
2166 lsmash_dcr_nalu_appendable lsmash_check_hevc_dcr_nalu_appendable
2168 lsmash_hevc_specific_parameters_t
*param
,
2169 lsmash_hevc_dcr_nalu_type ps_type
,
2174 uint8_t *ps_data
= _ps_data
;
2176 return DCR_NALU_APPEND_ERROR
;
2177 if( hevc_validate_dcr_nalu_type( ps_type
, ps_data
, ps_length
) < 0 )
2178 return DCR_NALU_APPEND_ERROR
;
2179 /* Check whether the same parameter set already exsits or not. */
2180 lsmash_entry_list_t
*ps_list
= hevc_get_parameter_set_list( param
, ps_type
);
2181 if( !ps_list
|| !ps_list
->head
)
2182 return DCR_NALU_APPEND_POSSIBLE
; /* No parameter set */
2183 switch( nalu_check_same_ps_existence( ps_list
, ps_data
, ps_length
) )
2186 case 1 : return DCR_NALU_APPEND_DUPLICATED
; /* The same parameter set already exists. */
2187 default : return DCR_NALU_APPEND_ERROR
; /* An error occured. */
2189 /* Check the number of parameter sets in HEVC Decoder Configuration Record. */
2191 if( nalu_get_ps_count( ps_list
, &ps_count
) < 0 )
2192 return DCR_NALU_APPEND_ERROR
;
2193 if( (ps_type
== HEVC_DCR_NALU_TYPE_VPS
&& ps_count
>= HEVC_MAX_VPS_ID
)
2194 || (ps_type
== HEVC_DCR_NALU_TYPE_SPS
&& ps_count
>= HEVC_MAX_SPS_ID
)
2195 || (ps_type
== HEVC_DCR_NALU_TYPE_PPS
&& ps_count
>= HEVC_MAX_PPS_ID
)
2196 || (ps_type
== HEVC_DCR_NALU_TYPE_PREFIX_SEI
&& ps_count
>= UINT16_MAX
)
2197 || (ps_type
== HEVC_DCR_NALU_TYPE_SUFFIX_SEI
&& ps_count
>= UINT16_MAX
) )
2198 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
; /* No more appendable parameter sets. */
2199 if( ps_type
== HEVC_DCR_NALU_TYPE_PREFIX_SEI
2200 || ps_type
== HEVC_DCR_NALU_TYPE_SUFFIX_SEI
)
2201 return DCR_NALU_APPEND_POSSIBLE
;
2202 /* Check whether a new specific info is needed or not. */
2203 if( ps_type
== HEVC_DCR_NALU_TYPE_PPS
)
2205 return hevc_check_pps_appendable( ps_data
, ps_length
, ps_list
);
2209 * Set up bitstream handler for parse parameter sets. */
2210 lsmash_bits_t
*bits
= lsmash_bits_adhoc_create();
2212 return DCR_NALU_APPEND_ERROR
;
2213 uint32_t max_ps_length
;
2214 uint8_t *rbsp_buffer
;
2215 if( nalu_get_max_ps_length( ps_list
, &max_ps_length
) < 0
2216 || (rbsp_buffer
= lsmash_malloc( LSMASH_MAX( max_ps_length
, ps_length
) )) == NULL
)
2218 lsmash_bits_adhoc_cleanup( bits
);
2219 return DCR_NALU_APPEND_ERROR
;
2221 lsmash_dcr_nalu_appendable appendable
;
2222 if( ps_type
== HEVC_DCR_NALU_TYPE_VPS
)
2223 appendable
= hevc_check_vps_appendable( bits
, rbsp_buffer
, param
, ps_data
, ps_length
, ps_list
);
2225 appendable
= hevc_check_sps_appendable( bits
, rbsp_buffer
, param
, ps_data
, ps_length
, ps_list
);
2226 lsmash_bits_adhoc_cleanup( bits
);
2227 lsmash_free( rbsp_buffer
);
2232 static inline void hevc_specific_parameters_ready
2234 lsmash_hevc_specific_parameters_t
*param
2237 param
->general_profile_compatibility_flags
= ~UINT32_C(0);
2238 param
->general_constraint_indicator_flags
= 0x0000FFFFFFFFFFFF;
2239 param
->min_spatial_segmentation_idc
= 0x0FFF;
2240 param
->avgFrameRate
= 0; /* unspecified average frame rate */
2241 param
->constantFrameRate
= 2;
2242 param
->numTemporalLayers
= 0;
2243 param
->temporalIdNested
= 1;
2246 static inline void hevc_specific_parameters_update_ptl
2248 lsmash_hevc_specific_parameters_t
*param
,
2252 param
->general_profile_space
= ptl
->general
.profile_space
;
2253 param
->general_tier_flag
= LSMASH_MAX( param
->general_tier_flag
, ptl
->general
.tier_flag
);
2254 param
->general_profile_idc
= ptl
->general
.profile_idc
;
2255 param
->general_profile_compatibility_flags
&= ptl
->general
.profile_compatibility_flags
;
2256 param
->general_constraint_indicator_flags
&= ((uint64_t)ptl
->general
.progressive_source_flag
<< 47)
2257 | ((uint64_t)ptl
->general
.interlaced_source_flag
<< 46)
2258 | ((uint64_t)ptl
->general
.non_packed_constraint_flag
<< 45)
2259 | ((uint64_t)ptl
->general
.frame_only_constraint_flag
<< 44)
2260 | ptl
->general
.reserved_zero_44bits
;
2261 param
->general_level_idc
= LSMASH_MAX( param
->general_level_idc
, ptl
->general
.level_idc
);
2264 static inline void hevc_reorder_parameter_set_ascending_id
2266 lsmash_hevc_specific_parameters_t
*param
,
2267 lsmash_hevc_dcr_nalu_type ps_type
,
2268 lsmash_entry_list_t
*ps_list
,
2272 lsmash_entry_t
*entry
= NULL
;
2274 for( int i
= ps_id
- 1; i
; i
-- )
2276 entry
= hevc_get_ps_entry_from_param( param
, ps_type
, i
);
2280 int append_head
= 0;
2283 /* Couldn't find any parameter set with lower identifier.
2284 * Next, find parameter set with upper identifier. */
2285 int max_ps_id
= ps_type
== HEVC_DCR_NALU_TYPE_VPS
? HEVC_MAX_VPS_ID
2286 : ps_type
== HEVC_DCR_NALU_TYPE_SPS
? HEVC_MAX_SPS_ID
2288 for( int i
= ps_id
+ 1; i
<= max_ps_id
; i
++ )
2290 entry
= hevc_get_ps_entry_from_param( param
, ps_type
, i
);
2298 return; /* The new entry was appended to the tail. */
2299 lsmash_entry_t
*new_entry
= ps_list
->tail
;
2302 /* before: entry[i > ps_id] ... -> prev_entry -> new_entry[ps_id]
2303 * after: new_entry[ps_id] -> entry[i > ps_id] -> ... -> prev_entry */
2304 if( new_entry
->prev
)
2305 new_entry
->prev
->next
= NULL
;
2306 new_entry
->prev
= NULL
;
2307 entry
->prev
= new_entry
;
2308 new_entry
->next
= entry
;
2311 /* before: entry[i < ps_id] -> next_entry -> ... -> prev_entry -> new_entry[ps_id]
2312 * after: entry[i < ps_id] -> new_entry[ps_id] -> next_entry -> ... -> prev_entry */
2313 if( new_entry
->prev
)
2314 new_entry
->prev
->next
= NULL
;
2315 new_entry
->prev
= entry
;
2316 new_entry
->next
= entry
->next
;
2318 entry
->next
->prev
= new_entry
;
2319 entry
->next
= new_entry
;
2322 int lsmash_append_hevc_dcr_nalu
2324 lsmash_hevc_specific_parameters_t
*param
,
2325 lsmash_hevc_dcr_nalu_type ps_type
,
2330 uint8_t *ps_data
= _ps_data
;
2331 if( !param
|| !ps_data
|| ps_length
< 2 )
2332 return LSMASH_ERR_FUNCTION_PARAM
;
2333 int err
= hevc_alloc_parameter_arrays_if_needed( param
);
2336 hevc_parameter_array_t
*ps_array
= hevc_get_parameter_set_array( param
, ps_type
);
2338 return LSMASH_ERR_FUNCTION_PARAM
;
2339 lsmash_entry_list_t
*ps_list
= ps_array
->list
;
2340 if( ps_type
== HEVC_DCR_NALU_TYPE_PREFIX_SEI
2341 || ps_type
== HEVC_DCR_NALU_TYPE_SUFFIX_SEI
)
2343 /* Append a SEI anyway. */
2344 isom_dcr_ps_entry_t
*ps
= isom_create_ps_entry( ps_data
, ps_length
);
2346 return LSMASH_ERR_MEMORY_ALLOC
;
2347 if( lsmash_list_add_entry( ps_list
, ps
) < 0 )
2349 isom_remove_dcr_ps( ps
);
2350 return LSMASH_ERR_MEMORY_ALLOC
;
2354 if( ps_type
!= HEVC_DCR_NALU_TYPE_VPS
2355 && ps_type
!= HEVC_DCR_NALU_TYPE_SPS
2356 && ps_type
!= HEVC_DCR_NALU_TYPE_PPS
)
2357 return LSMASH_ERR_FUNCTION_PARAM
;
2358 /* Check if the same parameter set identifier already exists. */
2360 if( (err
= hevc_get_ps_id( ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2361 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
, &ps_id
, ps_type
)) < 0 )
2363 lsmash_entry_t
*entry
= hevc_get_ps_entry_from_param( param
, ps_type
, ps_id
);
2364 isom_dcr_ps_entry_t
*ps
= entry
? (isom_dcr_ps_entry_t
*)entry
->data
: NULL
;
2365 if( ps
&& !ps
->unused
)
2366 /* The same parameter set identifier already exists. */
2367 return LSMASH_ERR_FUNCTION_PARAM
;
2371 /* Reuse an already existed parameter set in the list. */
2373 if( ps
->nalUnit
!= ps_data
)
2375 /* The same address could be given when called by hevc_update_picture_info_for_slice(). */
2376 lsmash_free( ps
->nalUnit
);
2377 ps
->nalUnit
= ps_data
;
2379 ps
->nalUnitLength
= ps_length
;
2384 /* Create a new parameter set and append it into the list. */
2385 ps
= isom_create_ps_entry( ps_data
, ps_length
);
2387 return LSMASH_ERR_MEMORY_ALLOC
;
2388 if( lsmash_list_add_entry( ps_list
, ps
) < 0 )
2390 isom_remove_dcr_ps( ps
);
2391 return LSMASH_ERR_MEMORY_ALLOC
;
2395 lsmash_bits_t
*bits
= NULL
;
2396 uint8_t *rbsp_buffer
= NULL
;
2398 if( (err
= nalu_get_ps_count( ps_list
, &ps_count
)) < 0 )
2400 if( (bits
= lsmash_bits_adhoc_create()) == NULL
2401 || (rbsp_buffer
= lsmash_malloc( ps_length
)) == NULL
)
2403 err
= LSMASH_ERR_MEMORY_ALLOC
;
2406 /* Update specific info with VPS, SPS or PPS. */
2408 if( ps_type
== HEVC_DCR_NALU_TYPE_VPS
)
2411 if( (err
= hevc_parse_vps_minimally( bits
, &vps
, rbsp_buffer
,
2412 ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2413 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
)) < 0 )
2417 /* Initialize if not initialized yet. */
2418 lsmash_entry_list_t
*sps_list
= hevc_get_parameter_set_list( param
, HEVC_DCR_NALU_TYPE_SPS
);
2420 if( (err
= nalu_get_ps_count( sps_list
, &sps_count
)) < 0 )
2422 if( sps_count
== 0 )
2423 hevc_specific_parameters_ready( param
);
2425 hevc_specific_parameters_update_ptl( param
, &vps
.ptl
);
2426 param
->numTemporalLayers
= LSMASH_MAX( param
->numTemporalLayers
, vps
.max_sub_layers_minus1
+ 1 );
2427 //param->temporalIdNested &= vps.temporal_id_nesting_flag;
2429 else if( ps_type
== HEVC_DCR_NALU_TYPE_SPS
)
2432 if( (err
= hevc_parse_sps_minimally( bits
, &sps
, rbsp_buffer
,
2433 ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2434 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
)) < 0 )
2438 /* Initialize if not initialized yet. */
2439 lsmash_entry_list_t
*vps_list
= hevc_get_parameter_set_list( param
, HEVC_DCR_NALU_TYPE_VPS
);
2441 if( (err
= nalu_get_ps_count( vps_list
, &vps_count
)) < 0 )
2443 if( vps_count
== 0 )
2444 hevc_specific_parameters_ready( param
);
2446 hevc_specific_parameters_update_ptl( param
, &sps
.ptl
);
2447 param
->min_spatial_segmentation_idc
= LSMASH_MIN( param
->min_spatial_segmentation_idc
, sps
.vui
.min_spatial_segmentation_idc
);
2448 param
->chromaFormat
= sps
.chroma_format_idc
;
2449 param
->bitDepthLumaMinus8
= sps
.bit_depth_luma_minus8
;
2450 param
->bitDepthChromaMinus8
= sps
.bit_depth_chroma_minus8
;
2451 param
->numTemporalLayers
= LSMASH_MAX( param
->numTemporalLayers
, sps
.max_sub_layers_minus1
+ 1 );
2452 param
->temporalIdNested
&= sps
.temporal_id_nesting_flag
;
2453 /* Check type of constant frame rate. */
2454 if( param
->constantFrameRate
)
2457 if( param
->constantFrameRate
== 2 )
2460 for( uint8_t i
= 0; i
<= sps
.max_sub_layers_minus1
; i
++ )
2461 cfr
&= sps
.vui
.hrd
.fixed_pic_rate_general_flag
[i
];
2466 param
->constantFrameRate
= 2;
2469 for( uint8_t i
= 0; i
<= sps
.max_sub_layers_minus1
; i
++ )
2470 cfr
|= sps
.vui
.hrd
.fixed_pic_rate_general_flag
[i
];
2471 param
->constantFrameRate
= cfr
;
2475 /* FIXME: probably, we can get average frame rate according to C.3.3 Picture output. */
2476 if( param
->constantFrameRate
)
2478 uint64_t interval
= 0;
2479 for( uint8_t i
= 0; i
<= sps
.max_sub_layers_minus1
; i
++ )
2480 interval
+= sps
.vui
.num_units_in_tick
* sps
.vui
.hrd
.elemental_duration_in_tc_minus1
[i
];
2481 uint64_t frame_rate
;
2483 frame_rate
= ((256 * (2 - sps
.vui
.field_seq_flag
) * (uint64_t)sps
.vui
.time_scale
)
2484 * (sps
.max_sub_layers_minus1
+ 1)) / interval
;
2487 if( frame_rate
!= param
->avgFrameRate
&& param
->avgFrameRate
)
2488 param
->constantFrameRate
= 0;
2489 param
->avgFrameRate
= frame_rate
;
2496 if( (err
= hevc_parse_pps_minimally( bits
, &pps
, rbsp_buffer
,
2497 ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2498 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
)) < 0 )
2500 uint8_t parallelismType
= 0;
2501 #if 1 /* Replace 1 with 0 if parallelismType shall be set to 0 when min_spatial_segmentation_idc equal to 0. */
2503 if( pps
.entropy_coding_sync_enabled_flag
)
2504 parallelismType
= pps
.tiles_enabled_flag
? 0 : 3;
2505 else if( pps
.tiles_enabled_flag
)
2506 parallelismType
= 2;
2508 parallelismType
= 1;
2510 /* Parse SPS and check the value of min_spatial_segmentation_idc is equal to zero or not.
2511 * If the value is not equal to zero, update parallelismType appropriately.
2512 * If corresponding SPS is not found, set 0 to parallelismType. */
2513 entry
= hevc_get_ps_entry_from_param( param
, HEVC_DCR_NALU_TYPE_SPS
, pps
.seq_parameter_set_id
);
2514 if( entry
&& entry
->data
)
2516 ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
2517 lsmash_bits_t
*sps_bits
= lsmash_bits_adhoc_create();
2520 err
= LSMASH_ERR_MEMORY_ALLOC
;
2523 uint8_t *sps_rbsp_buffer
= lsmash_malloc( ps
->nalUnitLength
);
2524 if( !sps_rbsp_buffer
)
2526 lsmash_bits_adhoc_cleanup( sps_bits
);
2527 err
= LSMASH_ERR_MEMORY_ALLOC
;
2531 if( hevc_parse_sps_minimally( sps_bits
, &sps
, sps_rbsp_buffer
,
2532 ps
->nalUnit
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2533 ps
->nalUnitLength
- HEVC_MIN_NALU_HEADER_LENGTH
) == 0 )
2535 if( sps
.vui
.min_spatial_segmentation_idc
)
2537 if( pps
.entropy_coding_sync_enabled_flag
)
2538 parallelismType
= pps
.tiles_enabled_flag
? 0 : 3;
2539 else if( pps
.tiles_enabled_flag
)
2540 parallelismType
= 2;
2542 parallelismType
= 1;
2545 parallelismType
= 0;
2547 lsmash_bits_adhoc_cleanup( sps_bits
);
2548 lsmash_free( sps_rbsp_buffer
);
2552 param
->parallelismType
= parallelismType
;
2553 else if( param
->parallelismType
!= parallelismType
)
2554 param
->parallelismType
= 0;
2557 if( invoke_reorder
)
2558 /* Add a new parameter set in order of ascending parameter set identifier. */
2559 hevc_reorder_parameter_set_ascending_id( param
, ps_type
, ps_list
, ps_id
);
2563 ps
= (isom_dcr_ps_entry_t
*)lsmash_list_get_entry_data( ps_list
, ps_list
->entry_count
);
2567 lsmash_bits_adhoc_cleanup( bits
);
2568 lsmash_free( rbsp_buffer
);
2572 int hevc_try_to_append_dcr_nalu
2575 lsmash_hevc_dcr_nalu_type ps_type
,
2580 uint8_t *ps_data
= _ps_data
;
2581 lsmash_dcr_nalu_appendable ret
= lsmash_check_hevc_dcr_nalu_appendable( &info
->hvcC_param
, ps_type
, ps_data
, ps_length
);
2582 lsmash_hevc_specific_parameters_t
*param
;
2585 case DCR_NALU_APPEND_ERROR
: /* Error */
2586 return LSMASH_ERR_NAMELESS
;
2587 case DCR_NALU_APPEND_NEW_DCR_REQUIRED
: /* Mulitiple sample description is needed. */
2588 case DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED
: /* Mulitiple sample description is needed. */
2589 param
= &info
->hvcC_param_next
;
2590 info
->hvcC_pending
= 1;
2592 case DCR_NALU_APPEND_POSSIBLE
: /* Appendable */
2593 param
= info
->hvcC_pending
? &info
->hvcC_param_next
: &info
->hvcC_param
;
2595 default : /* No need to append */
2596 return DCR_NALU_APPEND_DUPLICATED
;
2601 case HEVC_DCR_NALU_TYPE_VPS
:
2602 if( (err
= hevc_parse_vps( info
, info
->buffer
.rbsp
,
2603 ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2604 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
)) < 0 )
2607 case HEVC_DCR_NALU_TYPE_SPS
:
2608 if( (err
= hevc_parse_sps( info
, info
->buffer
.rbsp
,
2609 ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2610 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
)) < 0 )
2613 case HEVC_DCR_NALU_TYPE_PPS
:
2614 if( (err
= hevc_parse_pps( info
, info
->buffer
.rbsp
,
2615 ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2616 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
)) < 0 )
2622 return lsmash_append_hevc_dcr_nalu( param
, ps_type
, ps_data
, ps_length
);
2625 static int hevc_move_dcr_nalu_entry
2627 lsmash_hevc_specific_parameters_t
*dst_data
,
2628 lsmash_hevc_specific_parameters_t
*src_data
,
2629 lsmash_hevc_dcr_nalu_type ps_type
2632 lsmash_entry_list_t
*src_ps_list
= hevc_get_parameter_set_list( src_data
, ps_type
);
2633 lsmash_entry_list_t
*dst_ps_list
= hevc_get_parameter_set_list( dst_data
, ps_type
);
2634 assert( src_ps_list
&& dst_ps_list
);
2635 for( lsmash_entry_t
*src_entry
= src_ps_list
->head
; src_entry
; src_entry
= src_entry
->next
)
2637 isom_dcr_ps_entry_t
*src_ps
= (isom_dcr_ps_entry_t
*)src_entry
->data
;
2642 if( (err
= hevc_get_ps_id( src_ps
->nalUnit
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2643 src_ps
->nalUnitLength
- HEVC_MIN_NALU_HEADER_LENGTH
,
2644 &src_ps_id
, ps_type
)) < 0 )
2646 lsmash_entry_t
*dst_entry
;
2647 for( dst_entry
= dst_ps_list
->head
; dst_entry
; dst_entry
= dst_entry
->next
)
2649 isom_dcr_ps_entry_t
*dst_ps
= (isom_dcr_ps_entry_t
*)dst_entry
->data
;
2653 if( (err
= hevc_get_ps_id( dst_ps
->nalUnit
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2654 dst_ps
->nalUnitLength
- HEVC_MIN_NALU_HEADER_LENGTH
,
2655 &dst_ps_id
, ps_type
)) < 0 )
2657 if( dst_ps_id
== src_ps_id
)
2659 /* Replace the old parameter set with the new one. */
2660 assert( dst_entry
->data
!= src_entry
->data
);
2661 isom_remove_dcr_ps( dst_ps
);
2662 dst_entry
->data
= src_entry
->data
;
2663 src_entry
->data
= NULL
;
2669 /* Move the parameter set. */
2670 if( lsmash_list_add_entry( dst_ps_list
, src_ps
) < 0 )
2671 return LSMASH_ERR_MEMORY_ALLOC
;
2672 src_entry
->data
= NULL
;
2678 int hevc_move_pending_hvcC_param
2684 if( !info
->hvcC_pending
)
2686 /* Mark 'unused' on parameter sets within the decoder configuration record. */
2687 for( int i
= 0; i
< HEVC_DCR_NALU_TYPE_NUM
; i
++ )
2689 lsmash_entry_list_t
*ps_list
= hevc_get_parameter_set_list( &info
->hvcC_param
, i
);
2691 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
; entry
= entry
->next
)
2693 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
2699 /* Move the new parameter sets. */
2701 if( (err
= hevc_move_dcr_nalu_entry( &info
->hvcC_param
, &info
->hvcC_param_next
, HEVC_DCR_NALU_TYPE_VPS
)) < 0
2702 || (err
= hevc_move_dcr_nalu_entry( &info
->hvcC_param
, &info
->hvcC_param_next
, HEVC_DCR_NALU_TYPE_SPS
)) < 0
2703 || (err
= hevc_move_dcr_nalu_entry( &info
->hvcC_param
, &info
->hvcC_param_next
, HEVC_DCR_NALU_TYPE_PPS
)) < 0
2704 || (err
= hevc_move_dcr_nalu_entry( &info
->hvcC_param
, &info
->hvcC_param_next
, HEVC_DCR_NALU_TYPE_PREFIX_SEI
)) < 0
2705 || (err
= hevc_move_dcr_nalu_entry( &info
->hvcC_param
, &info
->hvcC_param_next
, HEVC_DCR_NALU_TYPE_SUFFIX_SEI
)) < 0 )
2707 /* Move to the pending. */
2708 lsmash_hevc_parameter_arrays_t
*parameter_arrays
= info
->hvcC_param
.parameter_arrays
; /* Back up parameter arrays. */
2709 info
->hvcC_param
= info
->hvcC_param_next
;
2710 info
->hvcC_param
.parameter_arrays
= parameter_arrays
;
2711 /* No pending hvcC. */
2712 hevc_deallocate_parameter_arrays( &info
->hvcC_param_next
);
2713 uint8_t lengthSizeMinusOne
= info
->hvcC_param_next
.lengthSizeMinusOne
;
2714 memset( &info
->hvcC_param_next
, 0, sizeof(lsmash_hevc_specific_parameters_t
) );
2715 info
->hvcC_param_next
.lengthSizeMinusOne
= lengthSizeMinusOne
;
2716 info
->hvcC_pending
= 0;
2720 int lsmash_set_hevc_array_completeness
2722 lsmash_hevc_specific_parameters_t
*param
,
2723 lsmash_hevc_dcr_nalu_type ps_type
,
2724 int array_completeness
2727 if( hevc_alloc_parameter_arrays_if_needed( param
) < 0 )
2728 return LSMASH_ERR_MEMORY_ALLOC
;
2729 hevc_parameter_array_t
*ps_array
= hevc_get_parameter_set_array( param
, ps_type
);
2731 return LSMASH_ERR_FUNCTION_PARAM
;
2732 ps_array
->array_completeness
= array_completeness
;
2736 int lsmash_get_hevc_array_completeness
2738 lsmash_hevc_specific_parameters_t
*param
,
2739 lsmash_hevc_dcr_nalu_type ps_type
,
2740 int *array_completeness
2743 if( hevc_alloc_parameter_arrays_if_needed( param
) < 0 )
2744 return LSMASH_ERR_MEMORY_ALLOC
;
2745 hevc_parameter_array_t
*ps_array
= hevc_get_parameter_set_array( param
, ps_type
);
2747 return LSMASH_ERR_FUNCTION_PARAM
;
2748 *array_completeness
= ps_array
->array_completeness
;
2752 static int hevc_parse_succeeded
2755 lsmash_hevc_specific_parameters_t
*param
2759 if( info
->vps
.present
2760 && info
->sps
.present
2761 && info
->pps
.present
)
2763 *param
= info
->hvcC_param
;
2764 /* Avoid freeing parameter sets. */
2765 info
->hvcC_param
.parameter_arrays
= NULL
;
2769 ret
= LSMASH_ERR_INVALID_DATA
;
2770 hevc_cleanup_parser( info
);
2774 static inline int hevc_parse_failed
2780 hevc_cleanup_parser( info
);
2784 int lsmash_setup_hevc_specific_parameters_from_access_unit
2786 lsmash_hevc_specific_parameters_t
*param
,
2788 uint32_t data_length
2791 if( !param
|| !data
|| data_length
== 0 )
2792 return LSMASH_ERR_FUNCTION_PARAM
;
2793 hevc_info_t
*info
= &(hevc_info_t
){ { 0 } };
2794 lsmash_bs_t
*bs
= &(lsmash_bs_t
){ 0 };
2795 int err
= lsmash_bs_set_empty_stream( bs
, data
, data_length
);
2798 uint64_t sc_head_pos
= nalu_find_first_start_code( bs
);
2799 if( sc_head_pos
== NALU_NO_START_CODE_FOUND
)
2800 return LSMASH_ERR_INVALID_DATA
;
2801 else if( sc_head_pos
== NALU_IO_ERROR
)
2802 return LSMASH_ERR_IO
;
2803 if( (err
= hevc_setup_parser( info
, 1 )) < 0 )
2804 return hevc_parse_failed( info
, err
);
2805 hevc_stream_buffer_t
*sb
= &info
->buffer
;
2806 hevc_slice_info_t
*slice
= &info
->slice
;
2809 hevc_nalu_header_t nuh
;
2810 uint64_t start_code_length
;
2811 uint64_t trailing_zero_bytes
;
2812 uint64_t nalu_length
= hevc_find_next_start_code( bs
, &nuh
, &start_code_length
, &trailing_zero_bytes
);
2813 if( nalu_length
== NALU_NO_START_CODE_FOUND
)
2814 /* For the last NALU. This NALU already has been parsed. */
2815 return hevc_parse_succeeded( info
, param
);
2816 uint8_t nalu_type
= nuh
.nal_unit_type
;
2817 uint64_t next_sc_head_pos
= sc_head_pos
2820 + trailing_zero_bytes
;
2821 if( nalu_type
== HEVC_NALU_TYPE_FD
)
2823 /* We don't support streams with both filler and HRD yet. Otherwise, just skip filler. */
2824 if( info
->sps
.vui
.hrd
.present
)
2825 return hevc_parse_failed( info
, LSMASH_ERR_PATCH_WELCOME
);
2827 else if( nalu_type
<= HEVC_NALU_TYPE_RASL_R
2828 || (nalu_type
>= HEVC_NALU_TYPE_BLA_W_LP
&& nalu_type
<= HEVC_NALU_TYPE_CRA
)
2829 || (nalu_type
>= HEVC_NALU_TYPE_VPS
&& nalu_type
<= HEVC_NALU_TYPE_SUFFIX_SEI
) )
2831 /* Increase the buffer if needed. */
2832 uint64_t possible_au_length
= NALU_DEFAULT_NALU_LENGTH_SIZE
+ nalu_length
;
2833 if( sb
->bank
->buffer_size
< possible_au_length
2834 && (err
= hevc_supplement_buffer( sb
, NULL
, 2 * possible_au_length
)) < 0 )
2835 return hevc_parse_failed( info
, err
);
2836 /* Get the EBSP of the current NALU here. */
2837 uint8_t *nalu
= lsmash_bs_get_buffer_data( bs
) + start_code_length
;
2838 if( nalu_type
<= HEVC_NALU_TYPE_RSV_VCL31
)
2840 /* VCL NALU (slice) */
2841 hevc_slice_info_t prev_slice
= *slice
;
2842 if( (err
= hevc_parse_slice_segment_header( info
, &nuh
, sb
->rbsp
, nalu
+ nuh
.length
, nalu_length
- nuh
.length
)) < 0 )
2843 return hevc_parse_failed( info
, err
);
2844 if( prev_slice
.present
)
2846 /* Check whether the AU that contains the previous VCL NALU completed or not. */
2847 if( hevc_find_au_delimit_by_slice_info( info
, slice
, &prev_slice
) )
2848 /* The current NALU is the first VCL NALU of the primary coded picture of a new AU.
2849 * Therefore, the previous slice belongs to that new AU. */
2850 return hevc_parse_succeeded( info
, param
);
2856 if( hevc_find_au_delimit_by_nalu_type( nalu_type
, info
->prev_nalu_type
) )
2857 /* The last slice belongs to the AU you want at this time. */
2858 return hevc_parse_succeeded( info
, param
);
2861 case HEVC_NALU_TYPE_VPS
:
2862 if( (err
= hevc_try_to_append_dcr_nalu( info
, HEVC_DCR_NALU_TYPE_VPS
, nalu
, nalu_length
)) < 0 )
2863 return hevc_parse_failed( info
, err
);
2865 case HEVC_NALU_TYPE_SPS
:
2866 if( (err
= hevc_try_to_append_dcr_nalu( info
, HEVC_DCR_NALU_TYPE_SPS
, nalu
, nalu_length
)) < 0 )
2867 return hevc_parse_failed( info
, err
);
2869 case HEVC_NALU_TYPE_PPS
:
2870 if( (err
= hevc_try_to_append_dcr_nalu( info
, HEVC_DCR_NALU_TYPE_PPS
, nalu
, nalu_length
)) < 0 )
2871 return hevc_parse_failed( info
, err
);
2878 /* Move to the first byte of the next start code. */
2879 info
->prev_nalu_type
= nalu_type
;
2880 if( lsmash_bs_read_seek( bs
, next_sc_head_pos
, SEEK_SET
) != next_sc_head_pos
)
2881 return hevc_parse_failed( info
, LSMASH_ERR_NAMELESS
);
2882 /* Check if no more data to read from the stream. */
2883 if( !lsmash_bs_is_end( bs
, NALU_SHORT_START_CODE_LENGTH
) )
2884 sc_head_pos
= next_sc_head_pos
;
2886 return hevc_parse_succeeded( info
, param
);
2890 int hevc_construct_specific_parameters
2892 lsmash_codec_specific_t
*dst
,
2893 lsmash_codec_specific_t
*src
2896 assert( dst
&& dst
->data
.structured
&& src
&& src
->data
.unstructured
);
2897 if( src
->size
< ISOM_BASEBOX_COMMON_SIZE
+ 7 )
2898 return LSMASH_ERR_INVALID_DATA
;
2899 lsmash_hevc_specific_parameters_t
*param
= (lsmash_hevc_specific_parameters_t
*)dst
->data
.structured
;
2900 uint8_t *data
= src
->data
.unstructured
;
2901 uint64_t size
= LSMASH_GET_BE32( data
);
2902 data
+= ISOM_BASEBOX_COMMON_SIZE
;
2905 size
= LSMASH_GET_BE64( data
);
2908 if( size
!= src
->size
)
2909 return LSMASH_ERR_INVALID_DATA
;
2910 if( hevc_alloc_parameter_arrays_if_needed( param
) < 0 )
2911 return LSMASH_ERR_MEMORY_ALLOC
;
2912 lsmash_bs_t
*bs
= lsmash_bs_create();
2914 return LSMASH_ERR_MEMORY_ALLOC
;
2915 int err
= lsmash_bs_import_data( bs
, data
, src
->size
- (data
- src
->data
.unstructured
) );
2918 if( lsmash_bs_get_byte( bs
) != HVCC_CONFIGURATION_VERSION
)
2920 err
= LSMASH_ERR_INVALID_DATA
;
2921 goto fail
; /* We don't support configurationVersion other than HVCC_CONFIGURATION_VERSION. */
2923 uint8_t temp8
= lsmash_bs_get_byte( bs
);
2924 param
->general_profile_space
= (temp8
>> 6) & 0x03;
2925 param
->general_tier_flag
= (temp8
>> 5) & 0x01;
2926 param
->general_profile_idc
= temp8
& 0x1F;
2927 param
->general_profile_compatibility_flags
= lsmash_bs_get_be32( bs
);
2928 uint32_t temp32
= lsmash_bs_get_be32( bs
);
2929 uint16_t temp16
= lsmash_bs_get_be16( bs
);
2930 param
->general_constraint_indicator_flags
= ((uint64_t)temp32
<< 16) | temp16
;
2931 param
->general_level_idc
= lsmash_bs_get_byte( bs
);
2932 param
->min_spatial_segmentation_idc
= lsmash_bs_get_be16( bs
) & 0x0FFF;
2933 param
->parallelismType
= lsmash_bs_get_byte( bs
) & 0x03;
2934 param
->chromaFormat
= lsmash_bs_get_byte( bs
) & 0x03;
2935 param
->bitDepthLumaMinus8
= lsmash_bs_get_byte( bs
) & 0x07;
2936 param
->bitDepthChromaMinus8
= lsmash_bs_get_byte( bs
) & 0x07;
2937 param
->avgFrameRate
= lsmash_bs_get_be16( bs
);
2938 temp8
= lsmash_bs_get_byte( bs
);
2939 param
->constantFrameRate
= (temp8
>> 6) & 0x03;
2940 param
->numTemporalLayers
= (temp8
>> 3) & 0x07;
2941 param
->temporalIdNested
= (temp8
>> 2) & 0x01;
2942 param
->lengthSizeMinusOne
= temp8
& 0x03;
2943 uint8_t numOfArrays
= lsmash_bs_get_byte( bs
);
2944 for( uint8_t i
= 0; i
< numOfArrays
; i
++ )
2946 hevc_parameter_array_t param_array
;
2947 memset( ¶m_array
, 0, sizeof(hevc_parameter_array_t
) );
2948 temp8
= lsmash_bs_get_byte( bs
);
2949 param_array
.array_completeness
= (temp8
>> 7) & 0x01;
2950 param_array
.NAL_unit_type
= temp8
& 0x3F;
2951 param_array
.list
->entry_count
= lsmash_bs_get_be16( bs
);
2952 if( param_array
.NAL_unit_type
== HEVC_NALU_TYPE_VPS
2953 || param_array
.NAL_unit_type
== HEVC_NALU_TYPE_SPS
2954 || param_array
.NAL_unit_type
== HEVC_NALU_TYPE_PPS
2955 || param_array
.NAL_unit_type
== HEVC_NALU_TYPE_PREFIX_SEI
2956 || param_array
.NAL_unit_type
== HEVC_NALU_TYPE_SUFFIX_SEI
)
2958 if( (err
= nalu_get_dcr_ps( bs
, param_array
.list
, param_array
.list
->entry_count
)) < 0 )
2962 for( uint16_t j
= 0; j
< param_array
.list
->entry_count
; j
++ )
2964 uint16_t nalUnitLength
= lsmash_bs_get_be16( bs
);
2965 lsmash_bs_skip_bytes( bs
, nalUnitLength
); /* nalUnit */
2967 switch( param_array
.NAL_unit_type
)
2969 case HEVC_NALU_TYPE_VPS
:
2970 param
->parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_VPS
] = param_array
;
2972 case HEVC_NALU_TYPE_SPS
:
2973 param
->parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_SPS
] = param_array
;
2975 case HEVC_NALU_TYPE_PPS
:
2976 param
->parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_PPS
] = param_array
;
2978 case HEVC_NALU_TYPE_PREFIX_SEI
:
2979 param
->parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_PREFIX_SEI
] = param_array
;
2981 case HEVC_NALU_TYPE_SUFFIX_SEI
:
2982 param
->parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_SUFFIX_SEI
] = param_array
;
2985 /* Discard unknown NALUs. */
2989 lsmash_bs_cleanup( bs
);
2992 lsmash_bs_cleanup( bs
);
2996 int hevc_print_codec_specific
2999 lsmash_file_t
*file
,
3004 assert( box
->manager
& LSMASH_BINARY_CODED_BOX
);
3006 lsmash_ifprintf( fp
, indent
++, "[%s: HEVC Configuration Box]\n", isom_4cc2str( box
->type
.fourcc
) );
3007 lsmash_ifprintf( fp
, indent
, "position = %"PRIu64
"\n", box
->pos
);
3008 lsmash_ifprintf( fp
, indent
, "size = %"PRIu64
"\n", box
->size
);
3009 uint8_t *data
= box
->binary
;
3010 uint32_t offset
= isom_skip_box_common( &data
);
3011 lsmash_bs_t
*bs
= lsmash_bs_create();
3013 return LSMASH_ERR_MEMORY_ALLOC
;
3014 int err
= lsmash_bs_import_data( bs
, data
, box
->size
- offset
);
3017 lsmash_bs_cleanup( bs
);
3020 uint8_t configurationVersion
= lsmash_bs_get_byte( bs
);
3021 lsmash_ifprintf( fp
, indent
, "configurationVersion = %"PRIu8
"\n", configurationVersion
);
3022 if( configurationVersion
!= HVCC_CONFIGURATION_VERSION
)
3024 lsmash_bs_cleanup( bs
);
3027 uint8_t temp8
= lsmash_bs_get_byte( bs
);
3028 lsmash_ifprintf( fp
, indent
, "general_profile_space = %"PRIu8
"\n", (temp8
>> 6) & 0x03 );
3029 lsmash_ifprintf( fp
, indent
, "general_tier_flag = %"PRIu8
"\n", (temp8
>> 5) & 0x01 );
3030 lsmash_ifprintf( fp
, indent
, "general_profile_idc = %"PRIu8
"\n", temp8
& 0x1F );
3031 lsmash_ifprintf( fp
, indent
, "general_profile_compatibility_flags = 0x%08"PRIx32
"\n", lsmash_bs_get_be32( bs
) );
3032 uint32_t temp32
= lsmash_bs_get_be32( bs
);
3033 uint16_t temp16
= lsmash_bs_get_be16( bs
);
3034 lsmash_ifprintf( fp
, indent
, "general_constraint_indicator_flags = 0x%012"PRIx64
"\n", ((uint64_t)temp32
<< 16) | temp16
);
3035 uint8_t general_level_idc
= lsmash_bs_get_byte( bs
);
3036 lsmash_ifprintf( fp
, indent
, "general_level_idc = %"PRIu8
" (Level %g)\n", general_level_idc
, general_level_idc
/ 30.0 );
3037 temp16
= lsmash_bs_get_be16( bs
);
3038 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp16
>> 12) & 0x0F );
3039 lsmash_ifprintf( fp
, indent
, "min_spatial_segmentation_idc = %"PRIu16
"\n", temp16
& 0x0FFF );
3040 temp8
= lsmash_bs_get_byte( bs
);
3041 uint8_t parallelismType
= temp8
& 0x03;
3042 static const char *parallelism_table
[4] =
3044 "Mixed types or Unknown",
3047 "Entropy coding synchronization based / WPP: Wavefront Parallel Processing"
3049 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 2) & 0x3F );
3050 lsmash_ifprintf( fp
, indent
, "parallelismType = %"PRIu8
" (%s)\n", parallelismType
,
3051 parallelism_table
[parallelismType
] );
3052 temp8
= lsmash_bs_get_byte( bs
);
3053 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 2) & 0x3F );
3054 lsmash_ifprintf( fp
, indent
, "chromaFormat = %"PRIu8
"\n", temp8
& 0x03 );
3055 temp8
= lsmash_bs_get_byte( bs
);
3056 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 3) & 0x1F );
3057 lsmash_ifprintf( fp
, indent
, "bitDepthLumaMinus8 = %"PRIu8
"\n", temp8
& 0x07 );
3058 temp8
= lsmash_bs_get_byte( bs
);
3059 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 3) & 0x1F );
3060 lsmash_ifprintf( fp
, indent
, "bitDepthChromaMinus8 = %"PRIu8
"\n", temp8
& 0x07 );
3061 lsmash_ifprintf( fp
, indent
, "avgFrameRate = %"PRIu16
"\n", lsmash_bs_get_be16( bs
) );
3062 temp8
= lsmash_bs_get_byte( bs
);
3063 lsmash_ifprintf( fp
, indent
, "constantFrameRate = %"PRIu8
"\n", (temp8
>> 6) & 0x03 );
3064 lsmash_ifprintf( fp
, indent
, "numTemporalLayers = %"PRIu8
"\n", (temp8
>> 3) & 0x07 );
3065 lsmash_ifprintf( fp
, indent
, "temporalIdNested = %"PRIu8
"\n", (temp8
>> 2) & 0x01 );
3066 lsmash_ifprintf( fp
, indent
, "lengthSizeMinusOne = %"PRIu8
"\n", temp8
& 0x03 );
3067 uint8_t numOfArrays
= lsmash_bs_get_byte( bs
);
3068 lsmash_ifprintf( fp
, indent
, "numOfArrays = %"PRIu8
"\n", numOfArrays
);
3069 for( uint8_t i
= 0; i
< numOfArrays
; i
++ )
3071 int array_indent
= indent
+ 1;
3072 lsmash_ifprintf( fp
, array_indent
++, "array[%"PRIu8
"]\n", i
);
3073 temp8
= lsmash_bs_get_byte( bs
);
3074 lsmash_ifprintf( fp
, array_indent
, "array_completeness = %"PRIu8
"\n", (temp8
>> 7) & 0x01 );
3075 lsmash_ifprintf( fp
, array_indent
, "reserved = %"PRIu8
"\n", (temp8
>> 6) & 0x01 );
3076 lsmash_ifprintf( fp
, array_indent
, "NAL_unit_type = %"PRIu8
"\n", temp8
& 0x3F );
3077 uint16_t numNalus
= lsmash_bs_get_be16( bs
);
3078 lsmash_ifprintf( fp
, array_indent
, "numNalus = %"PRIu16
"\n", numNalus
);
3079 for( uint16_t j
= 0; j
< numNalus
; j
++ )
3081 uint16_t nalUnitLength
= lsmash_bs_get_be16( bs
);
3082 lsmash_bs_skip_bytes( bs
, nalUnitLength
);
3083 lsmash_ifprintf( fp
, array_indent
, "nalUnit[%"PRIu16
"]\n", j
);
3084 lsmash_ifprintf( fp
, array_indent
+ 1, "nalUnitLength = %"PRIu16
"\n", nalUnitLength
);
3087 lsmash_bs_cleanup( bs
);
3091 static inline int hevc_copy_dcr_nalu_array
3093 lsmash_hevc_specific_parameters_t
*dst_data
,
3094 lsmash_hevc_specific_parameters_t
*src_data
,
3095 lsmash_hevc_dcr_nalu_type ps_type
3098 hevc_parameter_array_t
*src_ps_array
= hevc_get_parameter_set_array( src_data
, ps_type
);
3099 hevc_parameter_array_t
*dst_ps_array
= hevc_get_parameter_set_array( dst_data
, ps_type
);
3100 assert( src_ps_array
&& dst_ps_array
);
3101 dst_ps_array
->array_completeness
= src_ps_array
->array_completeness
;
3102 dst_ps_array
->NAL_unit_type
= src_ps_array
->NAL_unit_type
;
3103 lsmash_entry_list_t
*src_ps_list
= src_ps_array
->list
;
3104 lsmash_entry_list_t
*dst_ps_list
= dst_ps_array
->list
;
3105 for( lsmash_entry_t
*entry
= src_ps_list
->head
; entry
; entry
= entry
->next
)
3107 isom_dcr_ps_entry_t
*src_ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
3108 if( !src_ps
|| src_ps
->unused
)
3110 isom_dcr_ps_entry_t
*dst_ps
= isom_create_ps_entry( src_ps
->nalUnit
, src_ps
->nalUnitLength
);
3113 hevc_deallocate_parameter_arrays( dst_data
);
3114 return LSMASH_ERR_MEMORY_ALLOC
;
3116 if( lsmash_list_add_entry( dst_ps_list
, dst_ps
) < 0 )
3118 hevc_deallocate_parameter_arrays( dst_data
);
3119 isom_remove_dcr_ps( dst_ps
);
3120 return LSMASH_ERR_MEMORY_ALLOC
;
3126 int hevc_copy_codec_specific
3128 lsmash_codec_specific_t
*dst
,
3129 lsmash_codec_specific_t
*src
3132 assert( src
&& src
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
&& src
->data
.structured
);
3133 assert( dst
&& dst
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
&& dst
->data
.structured
);
3134 lsmash_hevc_specific_parameters_t
*src_data
= (lsmash_hevc_specific_parameters_t
*)src
->data
.structured
;
3135 lsmash_hevc_specific_parameters_t
*dst_data
= (lsmash_hevc_specific_parameters_t
*)dst
->data
.structured
;
3136 hevc_deallocate_parameter_arrays( dst_data
);
3137 *dst_data
= *src_data
;
3138 if( !src_data
->parameter_arrays
)
3140 dst_data
->parameter_arrays
= hevc_alloc_parameter_arrays();
3141 if( !dst_data
->parameter_arrays
)
3142 return LSMASH_ERR_MEMORY_ALLOC
;
3143 for( int i
= 0; i
< HEVC_DCR_NALU_TYPE_NUM
; i
++ )
3145 int err
= hevc_copy_dcr_nalu_array( dst_data
, src_data
, (lsmash_hevc_dcr_nalu_type
)i
);