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 void lsmash_destroy_hevc_parameter_arrays
57 lsmash_hevc_specific_parameters_t
*param
60 if( !param
|| !param
->parameter_arrays
)
62 for( int i
= 0; i
< HEVC_DCR_NALU_TYPE_NUM
; i
++ )
63 lsmash_remove_entries( param
->parameter_arrays
->ps_array
[i
].list
, isom_remove_dcr_ps
);
64 lsmash_free( param
->parameter_arrays
);
65 param
->parameter_arrays
= NULL
;
68 void hevc_destruct_specific_data
75 lsmash_destroy_hevc_parameter_arrays( data
);
79 static void hevc_remove_pps
86 lsmash_free( pps
->colWidth
);
87 lsmash_free( pps
->rowHeight
);
91 void hevc_cleanup_parser
98 lsmash_remove_entries( info
->vps_list
, NULL
);
99 lsmash_remove_entries( info
->sps_list
, NULL
);
100 lsmash_remove_entries( info
->pps_list
, hevc_remove_pps
);
101 lsmash_destroy_hevc_parameter_arrays( &info
->hvcC_param
);
102 lsmash_destroy_hevc_parameter_arrays( &info
->hvcC_param_next
);
103 lsmash_destroy_multiple_buffers( info
->buffer
.bank
);
104 lsmash_bits_adhoc_cleanup( info
->bits
);
108 int hevc_setup_parser
115 memset( info
, 0, sizeof(hevc_info_t
) );
116 info
->hvcC_param
.lengthSizeMinusOne
= NALU_DEFAULT_NALU_LENGTH_SIZE
- 1;
117 info
->hvcC_param_next
.lengthSizeMinusOne
= NALU_DEFAULT_NALU_LENGTH_SIZE
- 1;
118 hevc_stream_buffer_t
*sb
= &info
->buffer
;
119 sb
->bank
= lsmash_create_multiple_buffers( parse_only
? 1 : 3, NALU_DEFAULT_BUFFER_SIZE
);
121 return LSMASH_ERR_MEMORY_ALLOC
;
122 sb
->rbsp
= lsmash_withdraw_buffer( sb
->bank
, 1 );
125 info
->au
.data
= lsmash_withdraw_buffer( sb
->bank
, 2 );
126 info
->au
.incomplete_data
= lsmash_withdraw_buffer( sb
->bank
, 3 );
128 info
->bits
= lsmash_bits_adhoc_create();
131 lsmash_destroy_multiple_buffers( sb
->bank
);
132 return LSMASH_ERR_MEMORY_ALLOC
;
134 lsmash_init_entry_list( info
->vps_list
);
135 lsmash_init_entry_list( info
->sps_list
);
136 lsmash_init_entry_list( info
->pps_list
);
137 info
->prev_nalu_type
= HEVC_NALU_TYPE_UNKNOWN
;
141 static int hevc_check_nalu_header
144 hevc_nalu_header_t
*nuh
,
145 int use_long_start_code
148 /* Check if the enough length of NALU header on the buffer. */
149 int start_code_length
= use_long_start_code
? NALU_LONG_START_CODE_LENGTH
: NALU_SHORT_START_CODE_LENGTH
;
150 if( lsmash_bs_is_end( bs
, start_code_length
+ 1 ) )
151 return LSMASH_ERR_NAMELESS
;
152 /* Read NALU header. */
153 uint16_t temp16
= lsmash_bs_show_be16( bs
, start_code_length
);
154 nuh
->forbidden_zero_bit
= (temp16
>> 15) & 0x01;
155 nuh
->nal_unit_type
= (temp16
>> 9) & 0x3f;
156 nuh
->nuh_layer_id
= (temp16
>> 3) & 0x3f;
157 uint8_t nuh_temporal_id_plus1
= temp16
& 0x07;
158 if( nuh
->forbidden_zero_bit
|| nuh_temporal_id_plus1
== 0 )
159 return LSMASH_ERR_INVALID_DATA
;
160 nuh
->TemporalId
= nuh_temporal_id_plus1
- 1;
161 nuh
->length
= HEVC_MIN_NALU_HEADER_LENGTH
;
162 /* nuh_layer_id shall be 0 in the specification we refer to. */
163 if( nuh
->nuh_layer_id
)
164 return LSMASH_ERR_NAMELESS
;
165 if( nuh
->TemporalId
== 0 )
167 /* For TSA_N, TSA_R, STSA_N and STSA_R, TemporalId shall not be equal to 0. */
168 if( nuh
->nal_unit_type
>= HEVC_NALU_TYPE_TSA_N
169 && nuh
->nal_unit_type
<= HEVC_NALU_TYPE_STSA_R
)
170 return LSMASH_ERR_INVALID_DATA
;
174 /* For BLA_W_LP to RSV_IRAP_VCL23, TemporalId shall be equal to 0. */
175 if( nuh
->nal_unit_type
>= HEVC_NALU_TYPE_BLA_W_LP
176 && nuh
->nal_unit_type
<= HEVC_NALU_TYPE_RSV_IRAP_VCL23
)
177 return LSMASH_ERR_INVALID_DATA
;
178 /* For VPS, SPS, EOS and EOB, TemporalId shall be equal to 0. */
179 if( nuh
->nal_unit_type
>= HEVC_NALU_TYPE_VPS
180 && nuh
->nal_unit_type
<= HEVC_NALU_TYPE_EOB
181 && nuh
->nal_unit_type
!= HEVC_NALU_TYPE_PPS
182 && nuh
->nal_unit_type
!= HEVC_NALU_TYPE_AUD
)
183 return LSMASH_ERR_INVALID_DATA
;
185 /* VPS, SPS and PPS require long start code (0x00000001).
186 * Also AU delimiter requires it too because this type of NALU shall be the first NALU of any AU if present. */
187 if( !use_long_start_code
188 && nuh
->nal_unit_type
>= HEVC_NALU_TYPE_VPS
189 && nuh
->nal_unit_type
<= HEVC_NALU_TYPE_AUD
)
190 return LSMASH_ERR_INVALID_DATA
;
194 uint64_t hevc_find_next_start_code
197 hevc_nalu_header_t
*nuh
,
198 uint64_t *start_code_length
,
199 uint64_t *trailing_zero_bytes
202 uint64_t length
= 0; /* the length of the latest NALU */
203 uint64_t count
= 0; /* the number of the trailing zero bytes after the latest NALU */
204 /* Check the type of the current start code. */
206 = (!lsmash_bs_is_end( bs
, NALU_LONG_START_CODE_LENGTH
) && 0x00000001 == lsmash_bs_show_be32( bs
, 0 )) ? 1
207 : (!lsmash_bs_is_end( bs
, NALU_SHORT_START_CODE_LENGTH
) && 0x000001 == lsmash_bs_show_be24( bs
, 0 )) ? 0
209 if( long_start_code
>= 0 && hevc_check_nalu_header( bs
, nuh
, long_start_code
) == 0 )
211 *start_code_length
= long_start_code
? NALU_LONG_START_CODE_LENGTH
: NALU_SHORT_START_CODE_LENGTH
;
212 uint64_t distance
= *start_code_length
+ nuh
->length
;
213 /* Find the start code of the next NALU and get the distance from the start code of the latest NALU. */
214 if( !lsmash_bs_is_end( bs
, distance
+ NALU_SHORT_START_CODE_LENGTH
) )
216 uint32_t sync_bytes
= lsmash_bs_show_be24( bs
, distance
);
217 while( 0x000001 != sync_bytes
)
219 if( lsmash_bs_is_end( bs
, ++distance
+ NALU_SHORT_START_CODE_LENGTH
) )
221 distance
= lsmash_bs_get_remaining_buffer_size( bs
);
225 sync_bytes
|= lsmash_bs_show_byte( bs
, distance
+ NALU_SHORT_START_CODE_LENGTH
- 1 );
226 sync_bytes
&= 0xFFFFFF;
230 distance
= lsmash_bs_get_remaining_buffer_size( bs
);
231 /* Any NALU has no consecutive zero bytes at the end. */
232 while( 0x00 == lsmash_bs_show_byte( bs
, distance
- 1 ) )
237 /* Remove the length of the start code. */
238 length
= distance
- *start_code_length
;
239 /* If there are one or more trailing zero bytes, we treat the last one byte as a part of the next start code.
240 * This makes the next start code a long start code. */
247 nuh
->forbidden_zero_bit
= 1; /* shall be 0, so invalid */
248 nuh
->nal_unit_type
= HEVC_NALU_TYPE_UNKNOWN
;
249 nuh
->nuh_layer_id
= 0; /* arbitrary */
250 nuh
->TemporalId
= 0; /* arbitrary */
252 *start_code_length
= 0;
253 length
= NALU_NO_START_CODE_FOUND
;
255 *trailing_zero_bytes
= count
;
259 static hevc_vps_t
*hevc_get_vps
261 lsmash_entry_list_t
*vps_list
,
265 if( !vps_list
|| vps_id
> HEVC_MAX_VPS_ID
)
267 for( lsmash_entry_t
*entry
= vps_list
->head
; entry
; entry
= entry
->next
)
269 hevc_vps_t
*vps
= (hevc_vps_t
*)entry
->data
;
272 if( vps
->video_parameter_set_id
== vps_id
)
275 hevc_vps_t
*vps
= lsmash_malloc_zero( sizeof(hevc_vps_t
) );
278 vps
->video_parameter_set_id
= vps_id
;
279 if( lsmash_add_entry( vps_list
, vps
) < 0 )
287 static hevc_sps_t
*hevc_get_sps
289 lsmash_entry_list_t
*sps_list
,
293 if( !sps_list
|| sps_id
> HEVC_MAX_SPS_ID
)
295 for( lsmash_entry_t
*entry
= sps_list
->head
; entry
; entry
= entry
->next
)
297 hevc_sps_t
*sps
= (hevc_sps_t
*)entry
->data
;
300 if( sps
->seq_parameter_set_id
== sps_id
)
303 hevc_sps_t
*sps
= lsmash_malloc_zero( sizeof(hevc_sps_t
) );
306 sps
->seq_parameter_set_id
= sps_id
;
307 if( lsmash_add_entry( sps_list
, sps
) < 0 )
315 static hevc_pps_t
*hevc_get_pps
317 lsmash_entry_list_t
*pps_list
,
321 if( !pps_list
|| pps_id
> HEVC_MAX_PPS_ID
)
323 for( lsmash_entry_t
*entry
= pps_list
->head
; entry
; entry
= entry
->next
)
325 hevc_pps_t
*pps
= (hevc_pps_t
*)entry
->data
;
328 if( pps
->pic_parameter_set_id
== pps_id
)
331 hevc_pps_t
*pps
= lsmash_malloc_zero( sizeof(hevc_pps_t
) );
334 pps
->pic_parameter_set_id
= pps_id
;
335 if( lsmash_add_entry( pps_list
, pps
) < 0 )
343 int hevc_calculate_poc
346 hevc_picture_info_t
*picture
,
347 hevc_picture_info_t
*prev_picture
350 #if HEVC_POC_DEBUG_PRINT
351 fprintf( stderr
, "PictureOrderCount\n" );
353 hevc_pps_t
*pps
= hevc_get_pps( info
->pps_list
, picture
->pic_parameter_set_id
);
355 return LSMASH_ERR_NAMELESS
;
356 hevc_sps_t
*sps
= hevc_get_sps( info
->sps_list
, pps
->seq_parameter_set_id
);
358 return LSMASH_ERR_NAMELESS
;
359 /* 8.3.1 Decoding process for picture order count
360 * This process needs to be invoked only for the first slice segment of a picture. */
361 int NoRaslOutputFlag
;
364 /* 8.1 General decoding process
365 * If the current picture is an IDR picture, a BLA picture, the first picture in the
366 * bitstream in decoding order, or the first picture that follows an end of sequence
367 * NAL unit in decoding order, the variable NoRaslOutputFlag is set equal to 1.
369 * Note that not only the end of sequence NAL unit but the end of bistream NAL unit as
370 * well specify that the current access unit is the last access unit in the coded video
371 * sequence in decoding order. */
372 NoRaslOutputFlag
= picture
->idr
|| picture
->broken_link
|| info
->eos
;
377 NoRaslOutputFlag
= 0;
379 int32_t poc_lsb
= picture
->poc_lsb
;
380 if( picture
->irap
&& NoRaslOutputFlag
)
384 int32_t prev_poc_msb
= picture
->idr
? 0 : prev_picture
->tid0_poc_msb
;
385 int32_t prev_poc_lsb
= picture
->idr
? 0 : prev_picture
->tid0_poc_lsb
;
386 int32_t max_poc_lsb
= 1 << sps
->log2_max_pic_order_cnt_lsb
;
387 if( (poc_lsb
< prev_poc_lsb
)
388 && ((prev_poc_lsb
- poc_lsb
) >= (max_poc_lsb
/ 2)) )
389 poc_msb
= prev_poc_msb
+ max_poc_lsb
;
390 else if( (poc_lsb
> prev_poc_lsb
)
391 && ((poc_lsb
- prev_poc_lsb
) > (max_poc_lsb
/ 2)) )
392 poc_msb
= prev_poc_msb
- max_poc_lsb
;
394 poc_msb
= prev_poc_msb
;
396 picture
->poc
= poc_msb
+ poc_lsb
;
397 if( picture
->TemporalId
== 0 && (!picture
->radl
|| !picture
->rasl
|| !picture
->sublayer_nonref
) )
399 picture
->tid0_poc_msb
= poc_msb
;
400 picture
->tid0_poc_lsb
= poc_lsb
;
402 #if HEVC_POC_DEBUG_PRINT
403 fprintf( stderr
, " prevPicOrderCntMsb: %"PRId32
"\n", prev_poc_msb
);
404 fprintf( stderr
, " prevPicOrderCntLsb: %"PRId32
"\n", prev_poc_lsb
);
405 fprintf( stderr
, " PicOrderCntMsb: %"PRId64
"\n", poc_msb
);
406 fprintf( stderr
, " pic_order_cnt_lsb: %"PRId32
"\n", poc_lsb
);
407 fprintf( stderr
, " MaxPicOrderCntLsb: %"PRIu64
"\n", max_poc_lsb
);
408 fprintf( stderr
, " POC: %"PRId32
"\n", picture
->poc
);
413 static inline int hevc_activate_vps
416 uint8_t video_parameter_set_id
419 hevc_vps_t
*vps
= hevc_get_vps( info
->vps_list
, video_parameter_set_id
);
421 return LSMASH_ERR_NAMELESS
;
426 static inline int hevc_activate_sps
429 uint8_t seq_parameter_set_id
432 hevc_sps_t
*sps
= hevc_get_sps( info
->sps_list
, seq_parameter_set_id
);
434 return LSMASH_ERR_NAMELESS
;
439 static void hevc_parse_scaling_list_data
444 for( int sizeId
= 0; sizeId
< 4; sizeId
++ )
445 for( int matrixId
= 0; matrixId
< (sizeId
== 3 ? 2 : 6); matrixId
++ )
447 if( !lsmash_bits_get( bits
, 1 ) ) /* scaling_list_pred_mode_flag[sizeId][matrixId] */
448 nalu_get_exp_golomb_ue( bits
); /* scaling_list_pred_matrix_id_delta[sizeId][matrixId] */
451 int coefNum
= LSMASH_MIN( 64, 1 << (4 + (sizeId
<< 1)) );
453 nalu_get_exp_golomb_se( bits
); /* scaling_list_dc_coef_minus8[sizeId - 2][matrixId] */
454 for( int i
= 0; i
< coefNum
; i
++ )
455 nalu_get_exp_golomb_se( bits
); /* scaling_list_delta_coef */
460 static int hevc_short_term_ref_pic_set
467 int inter_ref_pic_set_prediction_flag
= stRpsIdx
!= 0 ? lsmash_bits_get( bits
, 1 ) : 0;
468 if( inter_ref_pic_set_prediction_flag
)
470 /* delta_idx_minus1 is always 0 in SPS since stRpsIdx must not be equal to num_short_term_ref_pic_sets. */
471 uint64_t delta_idx_minus1
= stRpsIdx
== sps
->num_short_term_ref_pic_sets
? nalu_get_exp_golomb_ue( bits
) : 0;
472 int delta_rps_sign
= lsmash_bits_get( bits
, 1 );
473 uint64_t abs_delta_rps_minus1
= nalu_get_exp_golomb_ue( bits
);
474 int RefRpsIdx
= stRpsIdx
- (delta_idx_minus1
+ 1);
475 int deltaRps
= (delta_rps_sign
? -1 : 1) * (abs_delta_rps_minus1
+ 1);
476 hevc_st_rps_t
*st_rps
= &sps
->st_rps
[stRpsIdx
];
477 hevc_st_rps_t
*ref_rps
= &sps
->st_rps
[RefRpsIdx
];
478 uint8_t used_by_curr_pic_flag
[32];
479 uint8_t use_delta_flag
[32];
480 for( int j
= 0; j
<= ref_rps
->NumDeltaPocs
; j
++ )
482 used_by_curr_pic_flag
[j
] = lsmash_bits_get( bits
, 1 );
483 use_delta_flag
[j
] = !used_by_curr_pic_flag
[j
] ? lsmash_bits_get( bits
, 1 ) : 1;
485 /* NumNegativePics */
487 for( int j
= ref_rps
->NumPositivePics
- 1; j
>= 0; j
-- )
489 int dPoc
= ref_rps
->DeltaPocS1
[j
] + deltaRps
;
490 if( dPoc
< 0 && use_delta_flag
[ ref_rps
->NumNegativePics
+ j
] )
492 st_rps
->DeltaPocS0
[i
] = dPoc
;
493 st_rps
->UsedByCurrPicS0
[i
++] = used_by_curr_pic_flag
[ ref_rps
->NumNegativePics
+ j
];
496 if( deltaRps
< 0 && use_delta_flag
[ ref_rps
->NumDeltaPocs
] )
498 st_rps
->DeltaPocS0
[i
] = deltaRps
;
499 st_rps
->UsedByCurrPicS0
[i
++] = used_by_curr_pic_flag
[ ref_rps
->NumDeltaPocs
];
501 for( int j
= 0; j
< ref_rps
->NumNegativePics
; j
++ )
503 int dPoc
= ref_rps
->DeltaPocS0
[j
] + deltaRps
;
504 if( dPoc
< 0 && use_delta_flag
[j
] )
506 st_rps
->DeltaPocS0
[i
] = dPoc
;
507 st_rps
->UsedByCurrPicS0
[i
++] = used_by_curr_pic_flag
[j
];
510 st_rps
->NumNegativePics
= i
;
511 /* NumPositivePics */
513 for( int j
= ref_rps
->NumNegativePics
- 1; j
>= 0; j
-- )
515 int dPoc
= ref_rps
->DeltaPocS0
[j
] + deltaRps
;
516 if( dPoc
> 0 && use_delta_flag
[j
] )
518 st_rps
->DeltaPocS1
[i
] = dPoc
;
519 st_rps
->UsedByCurrPicS1
[i
++] = used_by_curr_pic_flag
[j
];
522 if( deltaRps
> 0 && use_delta_flag
[ ref_rps
->NumDeltaPocs
] )
524 st_rps
->DeltaPocS1
[i
] = deltaRps
;
525 st_rps
->UsedByCurrPicS1
[i
++] = used_by_curr_pic_flag
[ ref_rps
->NumDeltaPocs
];
527 for( int j
= 0; j
< ref_rps
->NumPositivePics
; j
++ )
529 int dPoc
= ref_rps
->DeltaPocS1
[j
] + deltaRps
;
530 if( dPoc
> 0 && use_delta_flag
[ ref_rps
->NumNegativePics
+ j
] )
532 st_rps
->DeltaPocS1
[i
] = dPoc
;
533 st_rps
->UsedByCurrPicS1
[i
++] = used_by_curr_pic_flag
[ ref_rps
->NumNegativePics
+ j
];
536 st_rps
->NumPositivePics
= i
;
538 st_rps
->NumDeltaPocs
= st_rps
->NumNegativePics
+ st_rps
->NumPositivePics
;
542 uint64_t num_negative_pics
= nalu_get_exp_golomb_ue( bits
);
543 uint64_t num_positive_pics
= nalu_get_exp_golomb_ue( bits
);
544 if( num_negative_pics
>= HEVC_MAX_DPB_SIZE
|| num_positive_pics
>= HEVC_MAX_DPB_SIZE
)
545 return LSMASH_ERR_INVALID_DATA
;
546 hevc_st_rps_t
*st_rps
= &sps
->st_rps
[stRpsIdx
];
547 st_rps
->NumNegativePics
= num_negative_pics
;
548 st_rps
->NumPositivePics
= num_positive_pics
;
549 st_rps
->NumDeltaPocs
= st_rps
->NumNegativePics
+ st_rps
->NumPositivePics
;
550 for( int i
= 0; i
< num_negative_pics
; i
++ )
552 uint64_t delta_poc_s0_minus1
= nalu_get_exp_golomb_ue( bits
);
554 st_rps
->DeltaPocS0
[i
] = -(signed)(delta_poc_s0_minus1
+ 1);
556 st_rps
->DeltaPocS0
[i
] = st_rps
->DeltaPocS0
[i
- 1] - (delta_poc_s0_minus1
+ 1);
557 st_rps
->UsedByCurrPicS0
[i
] = lsmash_bits_get( bits
, 1 ); /* used_by_curr_pic_s0_flag */
559 for( int i
= 0; i
< num_positive_pics
; i
++ )
561 uint64_t delta_poc_s1_minus1
= nalu_get_exp_golomb_ue( bits
);
563 st_rps
->DeltaPocS1
[i
] = +(delta_poc_s1_minus1
+ 1);
565 st_rps
->DeltaPocS1
[i
] = st_rps
->DeltaPocS1
[i
- 1] + (delta_poc_s1_minus1
+ 1);
566 st_rps
->UsedByCurrPicS0
[i
] = lsmash_bits_get( bits
, 1 ); /* used_by_curr_pic_s1_flag */
572 static inline void hevc_parse_sub_layer_hrd_parameters
576 int sub_pic_hrd_params_present_flag
579 for( int i
= 0; i
<= CpbCnt
; i
++ )
581 nalu_get_exp_golomb_ue( bits
); /* bit_rate_value_minus1[i] */
582 nalu_get_exp_golomb_ue( bits
); /* cpb_size_value_minus1[i] */
583 if( sub_pic_hrd_params_present_flag
)
585 nalu_get_exp_golomb_ue( bits
); /* cpb_size_du_value_minus1[i] */
586 nalu_get_exp_golomb_ue( bits
); /* bit_rate_du_value_minus1[i] */
588 lsmash_bits_get( bits
, 1 ); /* cbr_flag[i] */
592 static void hevc_parse_hrd_parameters
596 int commonInfPresentFlag
,
597 int maxNumSubLayersMinus1
600 /* The specification we refer to doesn't define the implicit value of some fields.
601 * According to JCTVC-HM reference software,
602 * the implicit value of nal_hrd_parameters_present_flag is to be equal to 0,
603 * the implicit value of vcl_hrd_parameters_present_flag is to be equal to 0. */
604 int nal_hrd_parameters_present_flag
= 0;
605 int vcl_hrd_parameters_present_flag
= 0;
606 memset( hrd
, 0, sizeof(hevc_hrd_t
) );
607 if( commonInfPresentFlag
)
609 nal_hrd_parameters_present_flag
= lsmash_bits_get( bits
, 1 );
610 vcl_hrd_parameters_present_flag
= lsmash_bits_get( bits
, 1 );
611 if( nal_hrd_parameters_present_flag
612 || vcl_hrd_parameters_present_flag
)
614 hrd
->CpbDpbDelaysPresentFlag
= 1;
615 hrd
->sub_pic_hrd_params_present_flag
= lsmash_bits_get( bits
, 1 );
616 if( hrd
->sub_pic_hrd_params_present_flag
)
618 lsmash_bits_get( bits
, 8 ); /* tick_divisor_minus2 */
619 hrd
->du_cpb_removal_delay_increment_length
= lsmash_bits_get( bits
, 5 ) + 1;
620 hrd
->sub_pic_cpb_params_in_pic_timing_sei_flag
= lsmash_bits_get( bits
, 1 );
621 hrd
->dpb_output_delay_du_length
= lsmash_bits_get( bits
, 5 ) + 1;
623 lsmash_bits_get( bits
, 4 ); /* bit_rate_scale */
624 lsmash_bits_get( bits
, 4 ); /* cpb_size_scale */
625 if( hrd
->sub_pic_hrd_params_present_flag
)
626 lsmash_bits_get( bits
, 4 ); /* cpb_size_du_scale */
627 lsmash_bits_get( bits
, 5 ); /* initial_cpb_removal_delay_length_minus1 */
628 hrd
->au_cpb_removal_delay_length
= lsmash_bits_get( bits
, 5 ) + 1;
629 hrd
->dpb_output_delay_length
= lsmash_bits_get( bits
, 5 ) + 1;
632 for( int i
= 0; i
<= maxNumSubLayersMinus1
; i
++ )
634 hrd
->fixed_pic_rate_general_flag
[i
] = lsmash_bits_get( bits
, 1 );
635 uint8_t fixed_pic_rate_within_cvs_flag
= !hrd
->fixed_pic_rate_general_flag
[i
] ? lsmash_bits_get( bits
, 1 ) : 1;
636 uint8_t low_delay_hrd_flag
= !fixed_pic_rate_within_cvs_flag
? lsmash_bits_get( bits
, 1 ) : 0;
637 hrd
->elemental_duration_in_tc
[i
] = fixed_pic_rate_within_cvs_flag
? nalu_get_exp_golomb_ue( bits
) + 1 : 0;
638 uint8_t cpb_cnt_minus1
= !low_delay_hrd_flag
? nalu_get_exp_golomb_ue( bits
) : 0;
639 if( nal_hrd_parameters_present_flag
)
640 hevc_parse_sub_layer_hrd_parameters( bits
, cpb_cnt_minus1
, hrd
->sub_pic_hrd_params_present_flag
);
641 if( vcl_hrd_parameters_present_flag
)
642 hevc_parse_sub_layer_hrd_parameters( bits
, cpb_cnt_minus1
, hrd
->sub_pic_hrd_params_present_flag
);
646 static inline void hevc_parse_profile_tier_level_common
649 hevc_ptl_common_t
*ptlc
,
654 if( profile_present
)
656 ptlc
->profile_space
= lsmash_bits_get( bits
, 2 );
657 ptlc
->tier_flag
= lsmash_bits_get( bits
, 1 );
658 ptlc
->profile_idc
= lsmash_bits_get( bits
, 5 );
659 ptlc
->profile_compatibility_flags
= lsmash_bits_get( bits
, 32 );
660 ptlc
->progressive_source_flag
= lsmash_bits_get( bits
, 1 );
661 ptlc
->interlaced_source_flag
= lsmash_bits_get( bits
, 1 );
662 ptlc
->non_packed_constraint_flag
= lsmash_bits_get( bits
, 1 );
663 ptlc
->frame_only_constraint_flag
= lsmash_bits_get( bits
, 1 );
664 ptlc
->reserved_zero_44bits
= lsmash_bits_get( bits
, 44 );
667 ptlc
->level_idc
= lsmash_bits_get( bits
, 8 );
670 static void hevc_parse_profile_tier_level
674 int maxNumSubLayersMinus1
677 hevc_parse_profile_tier_level_common( bits
, &ptl
->general
, 1, 1 );
678 if( maxNumSubLayersMinus1
== 0 )
680 assert( maxNumSubLayersMinus1
<= 6 );
681 int sub_layer_profile_present_flag
[6] = { 0 };
682 int sub_layer_level_present_flag
[6] = { 0 };
683 for( int i
= 0; i
< maxNumSubLayersMinus1
; i
++ )
685 sub_layer_profile_present_flag
[i
] = lsmash_bits_get( bits
, 1 );
686 sub_layer_level_present_flag
[i
] = lsmash_bits_get( bits
, 1 );
688 for( int i
= maxNumSubLayersMinus1
; i
< 8; i
++ )
689 lsmash_bits_get( bits
, 2 ); /* reserved_zero_2bits[i] */
690 for( int i
= 0; i
< maxNumSubLayersMinus1
; i
++ )
691 hevc_parse_profile_tier_level_common( bits
, &ptl
->sub_layer
[i
], sub_layer_profile_present_flag
[i
], sub_layer_level_present_flag
[i
] );
694 static int hevc_parse_vps_minimally
698 uint8_t *rbsp_buffer
,
703 int err
= nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, ebsp
, ebsp_size
);
706 memset( vps
, 0, sizeof(hevc_vps_t
) );
707 vps
->video_parameter_set_id
= lsmash_bits_get( bits
, 4 );
708 /* vps_reserved_three_2bits shall be 3 in the specification we refer to. */
709 if( lsmash_bits_get( bits
, 2 ) != 3 )
710 return LSMASH_ERR_NAMELESS
;
711 /* vps_max_layers_minus1 shall be 0 in the specification we refer to. */
712 if( lsmash_bits_get( bits
, 6 ) != 0 )
713 return LSMASH_ERR_NAMELESS
;
714 vps
->max_sub_layers_minus1
= lsmash_bits_get( bits
, 3 );
715 vps
->temporal_id_nesting_flag
= lsmash_bits_get( bits
, 1 );
716 /* When vps_max_sub_layers_minus1 is equal to 0, vps_temporal_id_nesting_flag shall be equal to 1. */
717 if( (vps
->max_sub_layers_minus1
| vps
->temporal_id_nesting_flag
) == 0 )
718 return LSMASH_ERR_INVALID_DATA
;
719 /* vps_reserved_0xffff_16bits shall be 0xFFFF in the specification we refer to. */
720 if( lsmash_bits_get( bits
, 16 ) != 0xFFFF )
721 return LSMASH_ERR_NAMELESS
;
722 hevc_parse_profile_tier_level( bits
, &vps
->ptl
, vps
->max_sub_layers_minus1
);
723 vps
->frame_field_info_present_flag
= vps
->ptl
.general
.progressive_source_flag
724 && vps
->ptl
.general
.interlaced_source_flag
;
725 int sub_layer_ordering_info_present_flag
= lsmash_bits_get( bits
, 1 );
726 for( int i
= sub_layer_ordering_info_present_flag
? 0 : vps
->max_sub_layers_minus1
; i
<= vps
->max_sub_layers_minus1
; i
++ )
728 nalu_get_exp_golomb_ue( bits
); /* max_dec_pic_buffering_minus1[i] */
729 nalu_get_exp_golomb_ue( bits
); /* max_num_reorder_pics [i] */
730 nalu_get_exp_golomb_ue( bits
); /* max_latency_increase_plus1 [i] */
732 uint8_t max_layer_id
= lsmash_bits_get( bits
, 6 );
733 uint16_t num_layer_sets_minus1
= nalu_get_exp_golomb_ue( bits
);
734 for( int i
= 1; i
<= num_layer_sets_minus1
; i
++ )
735 for( int j
= 0; j
<= max_layer_id
; j
++ )
736 lsmash_bits_get( bits
, 1 ); /* layer_id_included_flag[i][j] */
737 return bits
->bs
->error
? LSMASH_ERR_NAMELESS
: 0;
743 uint8_t *rbsp_buffer
,
748 lsmash_bits_t
*bits
= info
->bits
;
751 /* Parse VPS minimally for configuration records. */
753 int err
= hevc_parse_vps_minimally( bits
, &min_vps
, rbsp_buffer
, ebsp
, ebsp_size
);
756 vps
= hevc_get_vps( info
->vps_list
, min_vps
.video_parameter_set_id
);
758 return LSMASH_ERR_NAMELESS
;
761 vps
->timing_info_present_flag
= lsmash_bits_get( bits
, 1 );
762 if( vps
->timing_info_present_flag
)
764 lsmash_bits_get( bits
, 32 ); /* num_units_in_tick */
765 lsmash_bits_get( bits
, 32 ); /* time_scale */
766 if( lsmash_bits_get( bits
, 1 ) ) /* poc_proportional_to_timing_flag */
767 nalu_get_exp_golomb_ue( bits
); /* num_ticks_poc_diff_one_minus1 */
768 vps
->num_hrd_parameters
= nalu_get_exp_golomb_ue( bits
);
769 for( int i
= 0; i
< vps
->num_hrd_parameters
; i
++ )
771 nalu_get_exp_golomb_ue( bits
); /* hrd_layer_set_idx[i] */
772 int cprms_present_flag
= i
> 0 ? lsmash_bits_get( bits
, 1 ) : 1;
773 /* Although the value of vps_num_hrd_parameters is required to be less than or equal to 1 in the spec
774 * we refer to, decoders shall allow other values of vps_num_hrd_parameters in the range of 0 to 1024,
775 * inclusive, to appear in the syntax. */
777 hevc_parse_hrd_parameters( bits
, &vps
->hrd
[i
], cprms_present_flag
, vps
->max_sub_layers_minus1
);
780 hevc_hrd_t dummy_hrd
;
781 hevc_parse_hrd_parameters( bits
, &dummy_hrd
, cprms_present_flag
, vps
->max_sub_layers_minus1
);
785 /* Skip VPS extension. */
786 lsmash_bits_empty( bits
);
787 if( bits
->bs
->error
)
788 return LSMASH_ERR_NAMELESS
;
794 static int hevc_parse_sps_minimally
798 uint8_t *rbsp_buffer
,
803 int err
= nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, ebsp
, ebsp_size
);
806 memset( sps
, 0, sizeof(hevc_sps_t
) );
807 sps
->video_parameter_set_id
= lsmash_bits_get( bits
, 4 );
808 sps
->max_sub_layers_minus1
= lsmash_bits_get( bits
, 3 );
809 sps
->temporal_id_nesting_flag
= lsmash_bits_get( bits
, 1 );
810 hevc_parse_profile_tier_level( bits
, &sps
->ptl
, sps
->max_sub_layers_minus1
);
811 sps
->seq_parameter_set_id
= nalu_get_exp_golomb_ue( bits
);
812 sps
->chroma_format_idc
= nalu_get_exp_golomb_ue( bits
);
813 if( sps
->chroma_format_idc
== 3 )
814 sps
->separate_colour_plane_flag
= lsmash_bits_get( bits
, 1 );
815 static const int SubWidthC
[] = { 1, 2, 2, 1 };
816 static const int SubHeightC
[] = { 1, 2, 1, 1 };
817 uint64_t pic_width_in_luma_samples
= nalu_get_exp_golomb_ue( bits
);
818 uint64_t pic_height_in_luma_samples
= nalu_get_exp_golomb_ue( bits
);
819 sps
->cropped_width
= pic_width_in_luma_samples
;
820 sps
->cropped_height
= pic_height_in_luma_samples
;
821 if( lsmash_bits_get( bits
, 1 ) ) /* conformance_window_flag */
823 uint64_t conf_win_left_offset
= nalu_get_exp_golomb_ue( bits
);
824 uint64_t conf_win_right_offset
= nalu_get_exp_golomb_ue( bits
);
825 uint64_t conf_win_top_offset
= nalu_get_exp_golomb_ue( bits
);
826 uint64_t conf_win_bottom_offset
= nalu_get_exp_golomb_ue( bits
);
827 sps
->cropped_width
-= (conf_win_left_offset
+ conf_win_right_offset
) * SubWidthC
[ sps
->chroma_format_idc
];
828 sps
->cropped_height
-= (conf_win_top_offset
+ conf_win_bottom_offset
) * SubHeightC
[ sps
->chroma_format_idc
];
830 sps
->bit_depth_luma_minus8
= nalu_get_exp_golomb_ue( bits
);
831 sps
->bit_depth_chroma_minus8
= nalu_get_exp_golomb_ue( bits
);
832 sps
->log2_max_pic_order_cnt_lsb
= nalu_get_exp_golomb_ue( bits
) + 4;
833 int sub_layer_ordering_info_present_flag
= lsmash_bits_get( bits
, 1 );
834 for( int i
= sub_layer_ordering_info_present_flag
? 0 : sps
->max_sub_layers_minus1
; i
<= sps
->max_sub_layers_minus1
; i
++ )
836 nalu_get_exp_golomb_ue( bits
); /* max_dec_pic_buffering_minus1[i] */
837 nalu_get_exp_golomb_ue( bits
); /* max_num_reorder_pics [i] */
838 nalu_get_exp_golomb_ue( bits
); /* max_latency_increase_plus1 [i] */
840 uint64_t log2_min_luma_coding_block_size_minus3
= nalu_get_exp_golomb_ue( bits
);
841 uint64_t log2_diff_max_min_luma_coding_block_size
= nalu_get_exp_golomb_ue( bits
);
842 nalu_get_exp_golomb_ue( bits
); /* log2_min_transform_block_size_minus2 */
843 nalu_get_exp_golomb_ue( bits
); /* log2_diff_max_min_transform_block_size */
844 nalu_get_exp_golomb_ue( bits
); /* max_transform_hierarchy_depth_inter */
845 nalu_get_exp_golomb_ue( bits
); /* max_transform_hierarchy_depth_intra */
847 int MinCbLog2SizeY
= log2_min_luma_coding_block_size_minus3
+ 3;
848 int MinCbSizeY
= 1 << MinCbLog2SizeY
;
849 if( pic_width_in_luma_samples
== 0 || pic_width_in_luma_samples
% MinCbSizeY
850 || pic_height_in_luma_samples
== 0 || pic_height_in_luma_samples
% MinCbSizeY
)
851 return LSMASH_ERR_INVALID_DATA
; /* Both shall be an integer multiple of MinCbSizeY. */
852 int CtbLog2SizeY
= MinCbLog2SizeY
+ log2_diff_max_min_luma_coding_block_size
;
853 int CtbSizeY
= 1 << CtbLog2SizeY
;
854 sps
->PicWidthInCtbsY
= (pic_width_in_luma_samples
- 1) / CtbSizeY
+ 1;
855 sps
->PicHeightInCtbsY
= (pic_height_in_luma_samples
- 1) / CtbSizeY
+ 1;
856 sps
->PicSizeInCtbsY
= sps
->PicWidthInCtbsY
* sps
->PicHeightInCtbsY
;
858 if( lsmash_bits_get( bits
, 1 ) /* scaling_list_enabled_flag */
859 && lsmash_bits_get( bits
, 1 ) ) /* sps_scaling_list_data_present_flag */
860 hevc_parse_scaling_list_data( bits
);
861 lsmash_bits_get( bits
, 1 ); /* amp_enabled_flag */
862 lsmash_bits_get( bits
, 1 ); /* sample_adaptive_offset_enabled_flag */
863 if( lsmash_bits_get( bits
, 1 ) ) /* pcm_enabled_flag */
865 lsmash_bits_get( bits
, 4 ); /* pcm_sample_bit_depth_luma_minus1 */
866 lsmash_bits_get( bits
, 4 ); /* pcm_sample_bit_depth_chroma_minus1 */
867 nalu_get_exp_golomb_ue( bits
); /* log2_min_pcm_luma_coding_block_size_minus3 */
868 nalu_get_exp_golomb_ue( bits
); /* log2_diff_max_min_pcm_luma_coding_block_size */
869 lsmash_bits_get( bits
, 1 ); /* pcm_loop_filter_disabled_flag */
871 sps
->num_short_term_ref_pic_sets
= nalu_get_exp_golomb_ue( bits
);
872 for( int i
= 0; i
< sps
->num_short_term_ref_pic_sets
; i
++ )
873 if( (err
= hevc_short_term_ref_pic_set( bits
, sps
, i
)) < 0 )
875 sps
->long_term_ref_pics_present_flag
= lsmash_bits_get( bits
, 1 );
876 if( sps
->long_term_ref_pics_present_flag
)
878 sps
->num_long_term_ref_pics_sps
= nalu_get_exp_golomb_ue( bits
);
879 for( int i
= 0; i
< sps
->num_long_term_ref_pics_sps
; i
++ )
881 lsmash_bits_get( bits
, sps
->log2_max_pic_order_cnt_lsb
); /* lt_ref_pic_poc_lsb_sps [i] */
882 lsmash_bits_get( bits
, 1 ); /* used_by_curr_pic_lt_sps_flag[i] */
885 sps
->temporal_mvp_enabled_flag
= lsmash_bits_get( bits
, 1 );
886 lsmash_bits_get( bits
, 1 ); /* strong_intra_smoothing_enabled_flag */
887 sps
->vui
.present
= lsmash_bits_get( bits
, 1 ); /* vui_parameters_present_flag */
888 if( sps
->vui
.present
)
890 /* vui_parameters() */
891 if( lsmash_bits_get( bits
, 1 ) ) /* aspect_ratio_info_present_flag */
893 uint8_t aspect_ratio_idc
= lsmash_bits_get( bits
, 8 );
894 if( aspect_ratio_idc
== 255 )
897 sps
->vui
.sar_width
= lsmash_bits_get( bits
, 16 );
898 sps
->vui
.sar_height
= lsmash_bits_get( bits
, 16 );
906 } pre_defined_sar
[] =
908 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
909 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 }, { 80, 33 },
910 { 18, 11 }, { 15, 11 }, { 64, 33 }, { 160, 99 }, { 4, 3 },
913 if( aspect_ratio_idc
< (sizeof(pre_defined_sar
) / sizeof(pre_defined_sar
[0])) )
915 sps
->vui
.sar_width
= pre_defined_sar
[ aspect_ratio_idc
].sar_width
;
916 sps
->vui
.sar_height
= pre_defined_sar
[ aspect_ratio_idc
].sar_height
;
920 /* Behavior when unknown aspect_ratio_idc is detected is not specified in the specification. */
921 sps
->vui
.sar_width
= 0;
922 sps
->vui
.sar_height
= 0;
928 sps
->vui
.sar_width
= 0;
929 sps
->vui
.sar_height
= 0;
931 if( lsmash_bits_get( bits
, 1 ) ) /* overscan_info_present_flag */
932 lsmash_bits_get( bits
, 1 ); /* overscan_appropriate_flag */
933 if( lsmash_bits_get( bits
, 1 ) ) /* video_signal_type_present_flag */
935 lsmash_bits_get( bits
, 3 ); /* video_format */
936 sps
->vui
.video_full_range_flag
= lsmash_bits_get( bits
, 1 );
937 sps
->vui
.colour_description_present_flag
= lsmash_bits_get( bits
, 1 );
938 if( sps
->vui
.colour_description_present_flag
)
940 sps
->vui
.colour_primaries
= lsmash_bits_get( bits
, 8 );
941 sps
->vui
.transfer_characteristics
= lsmash_bits_get( bits
, 8 );
942 sps
->vui
.matrix_coeffs
= lsmash_bits_get( bits
, 8 );
946 sps
->vui
.colour_primaries
= 2;
947 sps
->vui
.transfer_characteristics
= 2;
948 sps
->vui
.matrix_coeffs
= 2;
951 if( lsmash_bits_get( bits
, 1 ) ) /* chroma_loc_info_present_flag */
953 nalu_get_exp_golomb_ue( bits
); /* chroma_sample_loc_type_top_field */
954 nalu_get_exp_golomb_ue( bits
); /* chroma_sample_loc_type_bottom_field */
956 lsmash_bits_get( bits
, 1 ); /* neutral_chroma_indication_flag */
957 sps
->vui
.field_seq_flag
= lsmash_bits_get( bits
, 1 );
958 sps
->vui
.frame_field_info_present_flag
= lsmash_bits_get( bits
, 1 );
959 if( sps
->vui
.field_seq_flag
)
960 /* cropped_height indicates in a frame. */
961 sps
->cropped_height
*= 2;
962 if( lsmash_bits_get( bits
, 1 ) ) /* default_display_window_flag */
964 /* default display window
965 * A rectangular region for display specified by these values is not considered
966 * as cropped visual presentation size which decoder delivers.
967 * Maybe, these values shall be indicated by the clean aperture on container level. */
968 sps
->vui
.def_disp_win_offset
.left
= (lsmash_rational_u32_t
){ nalu_get_exp_golomb_ue( bits
) * SubWidthC
[ sps
->chroma_format_idc
], 1 };
969 sps
->vui
.def_disp_win_offset
.right
= (lsmash_rational_u32_t
){ nalu_get_exp_golomb_ue( bits
) * SubWidthC
[ sps
->chroma_format_idc
], 1 };
970 sps
->vui
.def_disp_win_offset
.top
= (lsmash_rational_u32_t
){ nalu_get_exp_golomb_ue( bits
) * SubHeightC
[ sps
->chroma_format_idc
], 1 };
971 sps
->vui
.def_disp_win_offset
.bottom
= (lsmash_rational_u32_t
){ nalu_get_exp_golomb_ue( bits
) * SubHeightC
[ sps
->chroma_format_idc
], 1 };
973 if( lsmash_bits_get( bits
, 1 ) ) /* vui_timing_info_present_flag */
975 sps
->vui
.num_units_in_tick
= lsmash_bits_get( bits
, 32 );
976 sps
->vui
.time_scale
= lsmash_bits_get( bits
, 32 );
977 if( lsmash_bits_get( bits
, 1 ) ) /* vui_poc_proportional_to_timing_flag */
978 nalu_get_exp_golomb_ue( bits
); /* vui_num_ticks_poc_diff_one_minus1 */
979 if( lsmash_bits_get( bits
, 1 ) ) /* vui_hrd_parameters_present_flag */
980 hevc_parse_hrd_parameters( bits
, &sps
->vui
.hrd
, 1, sps
->max_sub_layers_minus1
);
984 sps
->vui
.num_units_in_tick
= 1; /* arbitrary */
985 sps
->vui
.time_scale
= 25; /* arbitrary */
987 if( lsmash_bits_get( bits
, 1 ) ) /* bitstream_restriction_flag */
989 lsmash_bits_get( bits
, 1 ); /* tiles_fixed_structure_flag */
990 lsmash_bits_get( bits
, 1 ); /* motion_vectors_over_pic_boundaries_flag */
991 lsmash_bits_get( bits
, 1 ); /* restricted_ref_pic_lists_flag */
992 sps
->vui
.min_spatial_segmentation_idc
= nalu_get_exp_golomb_ue( bits
);
993 nalu_get_exp_golomb_ue( bits
); /* max_bytes_per_pic_denom */
994 nalu_get_exp_golomb_ue( bits
); /* max_bits_per_min_cu_denom */
995 nalu_get_exp_golomb_ue( bits
); /* log2_max_mv_length_horizontal */
996 nalu_get_exp_golomb_ue( bits
); /* log2_max_mv_length_vertical */
999 sps
->vui
.min_spatial_segmentation_idc
= 0;
1003 sps
->vui
.sar_width
= 0;
1004 sps
->vui
.sar_height
= 0;
1005 sps
->vui
.colour_primaries
= 2;
1006 sps
->vui
.transfer_characteristics
= 2;
1007 sps
->vui
.matrix_coeffs
= 2;
1008 sps
->vui
.field_seq_flag
= 0;
1009 sps
->vui
.frame_field_info_present_flag
= sps
->ptl
.general
.progressive_source_flag
1010 && sps
->ptl
.general
.interlaced_source_flag
;
1011 sps
->vui
.num_units_in_tick
= 1; /* arbitrary */
1012 sps
->vui
.time_scale
= 25; /* arbitrary */
1013 sps
->vui
.min_spatial_segmentation_idc
= 0;
1015 return bits
->bs
->error
? LSMASH_ERR_NAMELESS
: 0;
1021 uint8_t *rbsp_buffer
,
1026 lsmash_bits_t
*bits
= info
->bits
;
1029 /* Parse SPS minimally for configuration records. */
1031 int err
= hevc_parse_sps_minimally( bits
, &min_sps
, rbsp_buffer
, ebsp
, ebsp_size
);
1034 sps
= hevc_get_sps( info
->sps_list
, min_sps
.seq_parameter_set_id
);
1036 return LSMASH_ERR_NAMELESS
;
1039 /* Skip SPS extension. */
1040 lsmash_bits_empty( bits
);
1041 if( bits
->bs
->error
)
1042 return LSMASH_ERR_NAMELESS
;
1045 hevc_activate_vps( info
, info
->sps
.video_parameter_set_id
);
1049 static int hevc_allocate_tile_sizes
1052 uint32_t num_tile_columns
,
1053 uint32_t num_tile_rows
1056 /* Allocate columns and rows of tiles. */
1057 size_t col_alloc_size
= 2 * num_tile_columns
* sizeof(uint32_t);
1058 if( col_alloc_size
> pps
->col_alloc_size
)
1060 void *temp
= lsmash_realloc( pps
->colWidth
, col_alloc_size
);
1062 return LSMASH_ERR_MEMORY_ALLOC
;
1063 pps
->col_alloc_size
= col_alloc_size
;
1064 pps
->colWidth
= temp
;
1066 size_t row_alloc_size
= 2 * num_tile_rows
* sizeof(uint32_t);
1067 if( row_alloc_size
> pps
->row_alloc_size
)
1069 void *temp
= lsmash_realloc( pps
->rowHeight
, row_alloc_size
);
1071 return LSMASH_ERR_MEMORY_ALLOC
;
1072 pps
->row_alloc_size
= row_alloc_size
;
1073 pps
->rowHeight
= temp
;
1075 pps
->colBd
= pps
->colWidth
+ num_tile_columns
;
1076 pps
->rowBd
= pps
->rowHeight
+ num_tile_rows
;
1080 static int hevc_parse_pps_minimally
1082 lsmash_bits_t
*bits
,
1084 uint8_t *rbsp_buffer
,
1089 int err
= nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, ebsp
, ebsp_size
);
1092 memset( pps
, 0, SIZEOF_PPS_EXCLUDING_HEAP
);
1093 pps
->pic_parameter_set_id
= nalu_get_exp_golomb_ue( bits
);
1094 pps
->seq_parameter_set_id
= nalu_get_exp_golomb_ue( bits
);
1095 pps
->dependent_slice_segments_enabled_flag
= lsmash_bits_get( bits
, 1 );
1096 pps
->output_flag_present_flag
= lsmash_bits_get( bits
, 1 );
1097 pps
->num_extra_slice_header_bits
= lsmash_bits_get( bits
, 3 );
1098 lsmash_bits_get( bits
, 1 ); /* sign_data_hiding_enabled_flag */
1099 lsmash_bits_get( bits
, 1 ); /* cabac_init_present_flag */
1100 nalu_get_exp_golomb_ue( bits
); /* num_ref_idx_l0_default_active_minus1 */
1101 nalu_get_exp_golomb_ue( bits
); /* num_ref_idx_l1_default_active_minus1 */
1102 nalu_get_exp_golomb_se( bits
); /* init_qp_minus26 */
1103 lsmash_bits_get( bits
, 1 ); /* constrained_intra_pred_flag */
1104 lsmash_bits_get( bits
, 1 ); /* transform_skip_enabled_flag */
1105 if( lsmash_bits_get( bits
, 1 ) ) /* cu_qp_delta_enabled_flag */
1106 nalu_get_exp_golomb_ue( bits
); /* diff_cu_qp_delta_depth */
1107 nalu_get_exp_golomb_se( bits
); /* cb_qp_offset */
1108 nalu_get_exp_golomb_se( bits
); /* cr_qp_offset */
1109 lsmash_bits_get( bits
, 1 ); /* slice_chroma_qp_offsets_present_flag */
1110 lsmash_bits_get( bits
, 1 ); /* weighted_pred_flag */
1111 lsmash_bits_get( bits
, 1 ); /* weighted_bipred_flag */
1112 lsmash_bits_get( bits
, 1 ) /* transquant_bypass_enabled_flag */;
1113 pps
->tiles_enabled_flag
= lsmash_bits_get( bits
, 1 );
1114 pps
->entropy_coding_sync_enabled_flag
= lsmash_bits_get( bits
, 1 );
1115 return bits
->bs
->error
? LSMASH_ERR_NAMELESS
: 0;
1121 uint8_t *rbsp_buffer
,
1127 lsmash_bits_t
*bits
= info
->bits
;
1130 /* Parse PPS minimally for configuration records. */
1132 if( (err
= hevc_parse_pps_minimally( bits
, &min_pps
, rbsp_buffer
, ebsp
, ebsp_size
)) < 0 )
1134 pps
= hevc_get_pps( info
->pps_list
, min_pps
.pic_parameter_set_id
);
1136 return LSMASH_ERR_NAMELESS
;
1137 memcpy( pps
, &min_pps
, SIZEOF_PPS_EXCLUDING_HEAP
);
1139 hevc_sps_t temp_sps
= info
->sps
;
1140 if( (err
= hevc_activate_sps( info
, pps
->seq_parameter_set_id
)) < 0 )
1142 hevc_sps_t
*sps
= &info
->sps
;
1143 if( pps
->tiles_enabled_flag
)
1145 pps
->num_tile_columns_minus1
= nalu_get_exp_golomb_ue( bits
);
1146 pps
->num_tile_rows_minus1
= nalu_get_exp_golomb_ue( bits
);
1147 if( pps
->num_tile_columns_minus1
>= sps
->PicWidthInCtbsY
1148 || pps
->num_tile_rows_minus1
>= sps
->PicHeightInCtbsY
)
1150 err
= LSMASH_ERR_INVALID_DATA
;
1153 if( (err
= hevc_allocate_tile_sizes( pps
, pps
->num_tile_columns_minus1
+ 1, pps
->num_tile_rows_minus1
+ 1 )) < 0 )
1155 if( lsmash_bits_get( bits
, 1 ) ) /* uniform_spacing_flag */
1157 for( int i
= 0; i
<= pps
->num_tile_columns_minus1
; i
++ )
1158 pps
->colWidth
[i
] = ((i
+ 1) * sps
->PicWidthInCtbsY
) / (pps
->num_tile_columns_minus1
+ 1)
1159 - ( i
* sps
->PicWidthInCtbsY
) / (pps
->num_tile_columns_minus1
+ 1);
1160 for( int j
= 0; j
<= pps
->num_tile_rows_minus1
; j
++ )
1161 pps
->rowHeight
[j
] = ((j
+ 1) * sps
->PicHeightInCtbsY
) / (pps
->num_tile_rows_minus1
+ 1)
1162 - ( j
* sps
->PicHeightInCtbsY
) / (pps
->num_tile_rows_minus1
+ 1);
1166 pps
->colWidth
[ pps
->num_tile_columns_minus1
] = sps
->PicWidthInCtbsY
;
1167 for( uint64_t i
= 0; i
< pps
->num_tile_columns_minus1
; i
++ )
1169 pps
->colWidth
[i
] = nalu_get_exp_golomb_ue( bits
) + 1; /* column_width_minus1[i] */
1170 pps
->colWidth
[ pps
->num_tile_columns_minus1
] -= pps
->colWidth
[i
];
1172 pps
->rowHeight
[ pps
->num_tile_rows_minus1
] = sps
->PicHeightInCtbsY
;
1173 for( uint64_t j
= 0; j
< pps
->num_tile_rows_minus1
; j
++ )
1175 pps
->rowHeight
[j
] = nalu_get_exp_golomb_ue( bits
) + 1; /* row_height_minus1 [j] */
1176 pps
->rowHeight
[ pps
->num_tile_rows_minus1
] -= pps
->rowHeight
[j
];
1180 for( uint64_t i
= 0; i
< pps
->num_tile_columns_minus1
; i
++ )
1181 pps
->colBd
[i
+ 1] = pps
->colBd
[i
] + pps
->colWidth
[i
];
1183 for( uint64_t j
= 0; j
< pps
->num_tile_rows_minus1
; j
++ )
1184 pps
->rowBd
[j
+ 1] = pps
->rowBd
[j
] + pps
->rowHeight
[j
];
1185 lsmash_bits_get( bits
, 1 ); /* loop_filter_across_tiles_enabled_flag */
1189 pps
->num_tile_columns_minus1
= 0;
1190 pps
->num_tile_rows_minus1
= 0;
1191 if( (err
= hevc_allocate_tile_sizes( pps
, 1, 1 )) < 0 )
1193 pps
->colWidth
[0] = sps
->PicWidthInCtbsY
;
1194 pps
->rowHeight
[0] = sps
->PicHeightInCtbsY
;
1199 /* Skip PPS extension. */
1200 lsmash_bits_empty( bits
);
1201 if( bits
->bs
->error
)
1205 hevc_activate_vps( info
, info
->sps
.video_parameter_set_id
);
1209 info
->sps
= temp_sps
;
1215 lsmash_bits_t
*bits
,
1219 hevc_nalu_header_t
*nuh
,
1220 uint8_t *rbsp_buffer
,
1225 int err
= nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, ebsp
, ebsp_size
);
1228 uint8_t *rbsp_start
= rbsp_buffer
;
1229 uint64_t rbsp_pos
= 0;
1233 uint32_t payloadType
= 0;
1234 for( uint8_t temp
= lsmash_bits_get( bits
, 8 ); ; temp
= lsmash_bits_get( bits
, 8 ) )
1237 * otherwise: last_payload_type_byte */
1238 payloadType
+= temp
;
1243 uint32_t payloadSize
= 0;
1244 for( uint8_t temp
= lsmash_bits_get( bits
, 8 ); ; temp
= lsmash_bits_get( bits
, 8 ) )
1247 * otherwise: last_payload_size_byte */
1248 payloadSize
+= temp
;
1253 if( nuh
->nal_unit_type
== HEVC_NALU_TYPE_PREFIX_SEI
)
1255 if( payloadType
== 1 )
1258 hevc_hrd_t
*hrd
= sps
? &sps
->vui
.hrd
: vps
? &vps
->hrd
[0] : NULL
;
1260 goto skip_sei_message
; /* Any active VPS or SPS is not found. */
1261 sei
->pic_timing
.present
= 1;
1262 if( (sps
&& sps
->vui
.frame_field_info_present_flag
) || vps
->frame_field_info_present_flag
)
1264 sei
->pic_timing
.pic_struct
= lsmash_bits_get( bits
, 4 );
1265 lsmash_bits_get( bits
, 2 ); /* source_scan_type */
1266 lsmash_bits_get( bits
, 1 ); /* duplicate_flag */
1268 if( hrd
->CpbDpbDelaysPresentFlag
)
1270 lsmash_bits_get( bits
, hrd
->au_cpb_removal_delay_length
); /* au_cpb_removal_delay_minus1 */
1271 lsmash_bits_get( bits
, hrd
->dpb_output_delay_length
); /* pic_dpb_output_delay */
1272 if( hrd
->sub_pic_hrd_params_present_flag
)
1274 lsmash_bits_get( bits
, hrd
->dpb_output_delay_du_length
); /* pic_dpb_output_du_delay */
1275 if( hrd
->sub_pic_cpb_params_in_pic_timing_sei_flag
)
1277 uint64_t num_decoding_units_minus1
= nalu_get_exp_golomb_ue( bits
);
1278 int du_common_cpb_removal_delay_flag
= lsmash_bits_get( bits
, 1 );
1279 if( du_common_cpb_removal_delay_flag
)
1280 /* du_common_cpb_removal_delay_increment_minus1 */
1281 lsmash_bits_get( bits
, hrd
->du_cpb_removal_delay_increment_length
);
1282 for( uint64_t i
= 0; i
<= num_decoding_units_minus1
; i
++ )
1284 nalu_get_exp_golomb_ue( bits
); /* num_nalus_in_du_minus1 */
1285 if( !du_common_cpb_removal_delay_flag
&& i
< num_decoding_units_minus1
)
1286 nalu_get_exp_golomb_ue( bits
); /* du_cpb_removal_delay_increment_minus1 */
1292 else if( payloadType
== 3 )
1295 * FIXME: remove if array_completeness equal to 1. */
1296 return LSMASH_ERR_PATCH_WELCOME
;
1298 else if( payloadType
== 6 )
1300 /* recovery_point */
1301 sei
->recovery_point
.present
= 1;
1302 sei
->recovery_point
.recovery_poc_cnt
= nalu_get_exp_golomb_se( bits
);
1303 lsmash_bits_get( bits
, 1 ); /* exact_match_flag */
1304 sei
->recovery_point
.broken_link_flag
= lsmash_bits_get( bits
, 1 );
1307 goto skip_sei_message
;
1309 else if( nuh
->nal_unit_type
== HEVC_NALU_TYPE_SUFFIX_SEI
)
1311 if( payloadType
== 3 )
1314 * FIXME: remove if array_completeness equal to 1. */
1315 return LSMASH_ERR_PATCH_WELCOME
;
1318 goto skip_sei_message
;
1323 lsmash_bits_get( bits
, payloadSize
* 8 );
1325 lsmash_bits_get_align( bits
);
1326 rbsp_pos
+= payloadSize
;
1327 } while( *(rbsp_start
+ rbsp_pos
) != 0x80 ); /* All SEI messages are byte aligned at their end.
1328 * Therefore, 0x80 shall be rbsp_trailing_bits(). */
1329 lsmash_bits_empty( bits
);
1330 return bits
->bs
->error
? LSMASH_ERR_NAMELESS
: 0;
1333 int hevc_parse_slice_segment_header
1336 hevc_nalu_header_t
*nuh
,
1337 uint8_t *rbsp_buffer
,
1342 lsmash_bits_t
*bits
= info
->bits
;
1343 int err
= nalu_import_rbsp_from_ebsp( bits
, rbsp_buffer
, ebsp
, LSMASH_MIN( ebsp_size
, 50 ) );
1346 hevc_slice_info_t
*slice
= &info
->slice
;
1347 memset( slice
, 0, sizeof(hevc_slice_info_t
) );
1348 slice
->nalu_type
= nuh
->nal_unit_type
;
1349 slice
->TemporalId
= nuh
->TemporalId
;
1350 slice
->first_slice_segment_in_pic_flag
= lsmash_bits_get( bits
, 1 );
1351 if( nuh
->nal_unit_type
>= HEVC_NALU_TYPE_BLA_W_LP
1352 && nuh
->nal_unit_type
<= HEVC_NALU_TYPE_RSV_IRAP_VCL23
)
1353 lsmash_bits_get( bits
, 1 ); /* no_output_of_prior_pics_flag */
1354 slice
->pic_parameter_set_id
= nalu_get_exp_golomb_ue( bits
);
1355 /* Get PPS by slice_pic_parameter_set_id. */
1356 hevc_pps_t
*pps
= hevc_get_pps( info
->pps_list
, slice
->pic_parameter_set_id
);
1358 return LSMASH_ERR_NAMELESS
;
1359 /* Get SPS by pps_seq_parameter_set_id. */
1360 hevc_sps_t
*sps
= hevc_get_sps( info
->sps_list
, pps
->seq_parameter_set_id
);
1362 return LSMASH_ERR_NAMELESS
;
1363 slice
->video_parameter_set_id
= sps
->video_parameter_set_id
;
1364 slice
->seq_parameter_set_id
= pps
->seq_parameter_set_id
;
1365 if( !slice
->first_slice_segment_in_pic_flag
)
1367 slice
->dependent_slice_segment_flag
= pps
->dependent_slice_segments_enabled_flag
? lsmash_bits_get( bits
, 1 ) : 0;
1368 slice
->segment_address
= lsmash_bits_get( bits
, lsmash_ceil_log2( sps
->PicSizeInCtbsY
) );
1372 slice
->dependent_slice_segment_flag
= 0;
1373 slice
->segment_address
= 0;
1375 if( !slice
->dependent_slice_segment_flag
)
1377 /* independent slice segment
1378 * The values of the slice segment header of dependent slice segment are inferred from the values
1379 * for the preceding independent slice segment in decoding order, if some of the values are not present. */
1380 for( int i
= 0; i
< pps
->num_extra_slice_header_bits
; i
++ )
1381 lsmash_bits_get( bits
, 1 ); /* slice_reserved_flag[i] */
1382 slice
->type
= nalu_get_exp_golomb_ue( bits
);
1383 if( pps
->output_flag_present_flag
)
1384 lsmash_bits_get( bits
, 1 ); /* pic_output_flag */
1385 if( sps
->separate_colour_plane_flag
)
1386 lsmash_bits_get( bits
, 1 ); /* colour_plane_id */
1387 if( nuh
->nal_unit_type
!= HEVC_NALU_TYPE_IDR_W_RADL
1388 && nuh
->nal_unit_type
!= HEVC_NALU_TYPE_IDR_N_LP
)
1390 slice
->pic_order_cnt_lsb
= lsmash_bits_get( bits
, sps
->log2_max_pic_order_cnt_lsb
);
1391 if( !lsmash_bits_get( bits
, 1 ) ) /* short_term_ref_pic_set_sps_flag */
1393 if( (err
= hevc_short_term_ref_pic_set( bits
, sps
, sps
->num_short_term_ref_pic_sets
)) < 0 )
1398 int length
= lsmash_ceil_log2( sps
->num_short_term_ref_pic_sets
);
1400 lsmash_bits_get( bits
, length
); /* short_term_ref_pic_set_idx */
1402 if( sps
->long_term_ref_pics_present_flag
)
1404 uint64_t num_long_term_sps
= sps
->num_long_term_ref_pics_sps
> 0 ? nalu_get_exp_golomb_ue( bits
) : 0;
1405 uint64_t num_long_term_pics
= nalu_get_exp_golomb_ue( bits
);
1406 for( uint64_t i
= 0; i
< num_long_term_sps
+ num_long_term_pics
; i
++ )
1408 if( i
< num_long_term_sps
)
1410 int length
= lsmash_ceil_log2( sps
->num_long_term_ref_pics_sps
);
1412 lsmash_bits_get( bits
, length
); /* lt_idx_sps[i] */
1416 lsmash_bits_get( bits
, sps
->log2_max_pic_order_cnt_lsb
); /* poc_lsb_lt [i] */
1417 lsmash_bits_get( bits
, 1 ); /* used_by_curr_pic_lt_flag[i] */
1419 if( lsmash_bits_get( bits
, 1 ) ) /* delta_poc_msb_present_flag[i] */
1420 nalu_get_exp_golomb_ue( bits
); /* delta_poc_msb_cycle_lt [i] */
1423 if( sps
->temporal_mvp_enabled_flag
)
1424 lsmash_bits_get( bits
, 1 ); /* slice_temporal_mvp_enabled_flag */
1427 /* For IDR-pictures, slice_pic_order_cnt_lsb is inferred to be 0. */
1428 slice
->pic_order_cnt_lsb
= 0;
1430 lsmash_bits_empty( bits
);
1431 if( bits
->bs
->error
)
1432 return LSMASH_ERR_NAMELESS
;
1438 static int hevc_get_vps_id
1441 uint32_t ps_ebsp_length
,
1445 /* the number of bits of vps_id = 4
1446 * (4 - 1) / 8 + 1 = 1 bytes */
1447 *ps_id
= (*ps_ebsp
>> 4) & 0x0F; /* vps_video_parameter_set_id */
1451 static int hevc_get_sps_id
1454 uint32_t ps_ebsp_length
,
1458 /* the maximum number of bits of sps_id = 9: 0b00001XXXX
1459 * (8 + 688 + 9 - 1) / 8 + 1 = 89 bytes
1460 * Here more additional bytes because there might be emulation_prevention_three_byte(s). */
1461 lsmash_bits_t bits
= { 0 };
1462 lsmash_bs_t bs
= { 0 };
1463 uint8_t rbsp_buffer
[128];
1464 uint8_t buffer
[128];
1465 bs
.buffer
.data
= buffer
;
1466 bs
.buffer
.alloc
= 128;
1467 lsmash_bits_init( &bits
, &bs
);
1468 int err
= nalu_import_rbsp_from_ebsp( &bits
, rbsp_buffer
, ps_ebsp
, LSMASH_MIN( ps_ebsp_length
, 128 ) );
1471 /* Skip sps_video_parameter_set_id and sps_temporal_id_nesting_flag. */
1472 uint8_t sps_max_sub_layers_minus1
= (lsmash_bits_get( &bits
, 8 ) >> 1) & 0x07;
1473 /* profile_tier_level() costs at most 688 bits. */
1475 hevc_parse_profile_tier_level( &bits
, &sps_ptl
, sps_max_sub_layers_minus1
);
1476 uint64_t sps_seq_parameter_set_id
= nalu_get_exp_golomb_ue( &bits
);
1477 if( sps_seq_parameter_set_id
> HEVC_MAX_SPS_ID
)
1478 return LSMASH_ERR_INVALID_DATA
;
1479 *ps_id
= sps_seq_parameter_set_id
;
1480 return bs
.error
? LSMASH_ERR_NAMELESS
: 0;
1483 static int hevc_get_pps_id
1486 uint32_t ps_ebsp_length
,
1490 /* the maximum number of bits of pps_id = 13: 0b0000001XXXXXX
1491 * (13 - 1) / 8 + 1 = 2 bytes
1492 * Why +1? Because there might be an emulation_prevention_three_byte. */
1493 lsmash_bits_t bits
= { 0 };
1494 lsmash_bs_t bs
= { 0 };
1495 uint8_t rbsp_buffer
[3];
1497 bs
.buffer
.data
= buffer
;
1498 bs
.buffer
.alloc
= 3;
1499 lsmash_bits_init( &bits
, &bs
);
1500 int err
= nalu_import_rbsp_from_ebsp( &bits
, rbsp_buffer
, ps_ebsp
, LSMASH_MIN( ps_ebsp_length
, 3 ) );
1503 uint64_t pic_parameter_set_id
= nalu_get_exp_golomb_ue( &bits
);
1504 if( pic_parameter_set_id
> HEVC_MAX_PPS_ID
)
1505 return LSMASH_ERR_INVALID_DATA
;
1506 *ps_id
= pic_parameter_set_id
;
1507 return bs
.error
? LSMASH_ERR_NAMELESS
: 0;
1510 static inline int hevc_get_ps_id
1513 uint32_t ps_ebsp_length
,
1515 lsmash_hevc_dcr_nalu_type ps_type
1518 int (*get_ps_id
)( uint8_t *ps_ebsp
, uint32_t ps_ebsp_length
, uint8_t *ps_id
)
1519 = ps_type
== HEVC_DCR_NALU_TYPE_VPS
? hevc_get_vps_id
1520 : ps_type
== HEVC_DCR_NALU_TYPE_SPS
? hevc_get_sps_id
1521 : ps_type
== HEVC_DCR_NALU_TYPE_PPS
? hevc_get_pps_id
1523 return get_ps_id
? get_ps_id( ps_ebsp
, ps_ebsp_length
, ps_id
) : LSMASH_ERR_INVALID_DATA
;
1526 static inline hevc_parameter_array_t
*hevc_get_parameter_set_array
1528 lsmash_hevc_specific_parameters_t
*param
,
1529 lsmash_hevc_dcr_nalu_type ps_type
1532 if( !param
->parameter_arrays
)
1534 if( ps_type
>= HEVC_DCR_NALU_TYPE_NUM
)
1536 return ¶m
->parameter_arrays
->ps_array
[ps_type
];
1539 static inline lsmash_entry_list_t
*hevc_get_parameter_set_list
1541 lsmash_hevc_specific_parameters_t
*param
,
1542 lsmash_hevc_dcr_nalu_type ps_type
1545 if( !param
->parameter_arrays
)
1547 if( ps_type
>= HEVC_DCR_NALU_TYPE_NUM
)
1549 return param
->parameter_arrays
->ps_array
[ps_type
].list
;
1552 static lsmash_entry_t
*hevc_get_ps_entry_from_param
1554 lsmash_hevc_specific_parameters_t
*param
,
1555 lsmash_hevc_dcr_nalu_type ps_type
,
1559 int (*get_ps_id
)( uint8_t *ps_ebsp
, uint32_t ps_ebsp_length
, uint8_t *ps_id
)
1560 = ps_type
== HEVC_DCR_NALU_TYPE_VPS
? hevc_get_vps_id
1561 : ps_type
== HEVC_DCR_NALU_TYPE_SPS
? hevc_get_sps_id
1562 : ps_type
== HEVC_DCR_NALU_TYPE_PPS
? hevc_get_pps_id
1566 lsmash_entry_list_t
*list
= hevc_get_parameter_set_list( param
, ps_type
);
1569 for( lsmash_entry_t
*entry
= list
->head
; entry
; entry
= entry
->next
)
1571 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1574 uint8_t param_ps_id
;
1575 if( get_ps_id( ps
->nalUnit
+ HEVC_MIN_NALU_HEADER_LENGTH
,
1576 ps
->nalUnitLength
- HEVC_MIN_NALU_HEADER_LENGTH
, ¶m_ps_id
) < 0 )
1578 if( ps_id
== param_ps_id
)
1584 static inline void hevc_update_picture_type
1586 hevc_picture_info_t
*picture
,
1587 hevc_slice_info_t
*slice
1590 if( picture
->type
== HEVC_PICTURE_TYPE_I_P
)
1592 if( slice
->type
== HEVC_SLICE_TYPE_B
)
1593 picture
->type
= HEVC_PICTURE_TYPE_I_P_B
;
1595 else if( picture
->type
== HEVC_PICTURE_TYPE_I
)
1597 if( slice
->type
== HEVC_SLICE_TYPE_P
)
1598 picture
->type
= HEVC_PICTURE_TYPE_I_P
;
1599 else if( slice
->type
== HEVC_SLICE_TYPE_B
)
1600 picture
->type
= HEVC_PICTURE_TYPE_I_P_B
;
1602 else if( picture
->type
== HEVC_PICTURE_TYPE_NONE
)
1604 if( slice
->type
== HEVC_SLICE_TYPE_P
)
1605 picture
->type
= HEVC_PICTURE_TYPE_I_P
;
1606 else if( slice
->type
== HEVC_SLICE_TYPE_B
)
1607 picture
->type
= HEVC_PICTURE_TYPE_I_P_B
;
1608 else if( slice
->type
== HEVC_SLICE_TYPE_I
)
1609 picture
->type
= HEVC_PICTURE_TYPE_I
;
1612 fprintf( stderr
, "Picture type = %s\n", picture
->type
== HEVC_PICTURE_TYPE_I_P
? "P"
1613 : picture
->type
== HEVC_PICTURE_TYPE_I_P_B
? "B"
1614 : picture
->type
== HEVC_PICTURE_TYPE_I
? "I" );
1618 /* Shall be called at least once per picture. */
1619 void hevc_update_picture_info_for_slice
1622 hevc_picture_info_t
*picture
,
1623 hevc_slice_info_t
*slice
1627 picture
->has_primary
|= !slice
->dependent_slice_segment_flag
;
1628 hevc_update_picture_type( picture
, slice
);
1629 /* Mark 'used' on active parameter sets. */
1630 uint8_t ps_id
[3] = { slice
->video_parameter_set_id
, slice
->seq_parameter_set_id
, slice
->pic_parameter_set_id
};
1631 for( int i
= 0; i
< 3; i
++ )
1633 lsmash_hevc_dcr_nalu_type ps_type
= (lsmash_hevc_dcr_nalu_type
)i
;
1634 lsmash_entry_t
*entry
= hevc_get_ps_entry_from_param( &info
->hvcC_param
, ps_type
, ps_id
[i
] );
1635 if( entry
&& entry
->data
)
1637 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1639 lsmash_append_hevc_dcr_nalu( &info
->hvcC_param
, ps_type
, ps
->nalUnit
, ps
->nalUnitLength
);
1642 /* Discard this slice info. */
1646 /* Shall be called exactly once per picture. */
1647 void hevc_update_picture_info
1650 hevc_picture_info_t
*picture
,
1651 hevc_slice_info_t
*slice
,
1656 picture
->irap
= slice
->nalu_type
>= HEVC_NALU_TYPE_BLA_W_LP
&& slice
->nalu_type
<= HEVC_NALU_TYPE_CRA
;
1657 picture
->idr
= slice
->nalu_type
== HEVC_NALU_TYPE_IDR_W_RADL
|| slice
->nalu_type
== HEVC_NALU_TYPE_IDR_N_LP
;
1658 picture
->broken_link
= slice
->nalu_type
>= HEVC_NALU_TYPE_BLA_W_LP
&& slice
->nalu_type
<= HEVC_NALU_TYPE_BLA_N_LP
;
1659 picture
->radl
= slice
->nalu_type
== HEVC_NALU_TYPE_RADL_N
|| slice
->nalu_type
== HEVC_NALU_TYPE_RADL_R
;
1660 picture
->rasl
= slice
->nalu_type
== HEVC_NALU_TYPE_RASL_N
|| slice
->nalu_type
== HEVC_NALU_TYPE_RASL_R
;
1661 picture
->sublayer_nonref
= slice
->nalu_type
<= HEVC_NALU_TYPE_RSV_VCL_R15
&& ((slice
->nalu_type
& 0x01) == 0);
1662 picture
->closed_rap
= slice
->nalu_type
>= HEVC_NALU_TYPE_BLA_W_RADL
&& slice
->nalu_type
<= HEVC_NALU_TYPE_IDR_N_LP
;
1663 picture
->random_accessible
= picture
->irap
;
1664 picture
->TemporalId
= slice
->TemporalId
;
1665 picture
->pic_parameter_set_id
= slice
->pic_parameter_set_id
;
1666 picture
->poc_lsb
= slice
->pic_order_cnt_lsb
;
1667 hevc_update_picture_info_for_slice( info
, picture
, slice
);
1668 picture
->independent
= (picture
->type
== HEVC_PICTURE_TYPE_I
);
1669 picture
->field_coded
= sps
->vui
.field_seq_flag
;
1670 if( sei
->pic_timing
.present
)
1672 if( sei
->pic_timing
.pic_struct
< 13 )
1674 static const uint8_t delta
[13] = { 2, 1, 1, 2, 2, 3, 3, 4, 6, 1, 1, 1, 1 };
1675 picture
->delta
= delta
[ sei
->pic_timing
.pic_struct
];
1678 /* Reserved values in the spec we refer to. */
1679 picture
->delta
= picture
->field_coded
? 1 : 2;
1680 sei
->pic_timing
.present
= 0;
1683 picture
->delta
= picture
->field_coded
? 1 : 2;
1684 if( sei
->recovery_point
.present
)
1686 picture
->random_accessible
|= sei
->recovery_point
.present
;
1687 picture
->recovery_poc_cnt
= sei
->recovery_point
.recovery_poc_cnt
;
1688 picture
->broken_link
|= sei
->recovery_point
.broken_link_flag
;
1689 sei
->recovery_point
.present
= 0;
1692 picture
->recovery_poc_cnt
= 0;
1695 static uint64_t hevc_get_ctb_address_in_tile_scan
1699 uint64_t segment_address
,
1703 uint64_t tbX
= segment_address
% sps
->PicWidthInCtbsY
;
1704 uint64_t tbY
= segment_address
/ sps
->PicWidthInCtbsY
;
1705 uint32_t tileX
= pps
->num_tile_columns_minus1
;
1706 for( uint32_t i
= 0; i
<= pps
->num_tile_columns_minus1
; i
++ )
1707 if( tbX
>= pps
->colBd
[i
] )
1709 uint32_t tileY
= pps
->num_tile_rows_minus1
;
1710 for( uint32_t j
= 0; j
<= pps
->num_tile_rows_minus1
; j
++ )
1711 if( tbY
>= pps
->rowBd
[j
] )
1713 uint64_t CtbAddrInTs
= 0;
1714 for( uint32_t i
= 0; i
< tileX
; i
++ )
1715 CtbAddrInTs
+= pps
->rowHeight
[tileY
] * pps
->colWidth
[i
];
1716 for( uint32_t j
= 0; j
< tileY
; j
++ )
1717 CtbAddrInTs
+= sps
->PicWidthInCtbsY
* pps
->rowHeight
[j
];
1718 CtbAddrInTs
+= (tbY
- pps
->rowBd
[tileY
]) * pps
->colWidth
[tileX
] + tbX
- pps
->colBd
[tileX
];
1719 *TileId
= (uint64_t)tileY
* (pps
->num_tile_columns_minus1
+ 1) + tileX
;
1723 int hevc_find_au_delimit_by_slice_info
1726 hevc_slice_info_t
*slice
,
1727 hevc_slice_info_t
*prev_slice
1730 /* 7.4.2.4.5 Order of VCL NAL units and association to coded pictures
1731 * - The first VCL NAL unit of the coded picture shall have first_slice_segment_in_pic_flag equal to 1. */
1732 if( slice
->first_slice_segment_in_pic_flag
)
1734 /* The value of TemporalId shall be the same for all VCL NAL units of an access unit. */
1735 if( slice
->TemporalId
!= prev_slice
->TemporalId
)
1737 /* 7.4.2.4.5 Order of VCL NAL units and association to coded pictures
1738 * When either of the following conditions is true, both the current and the previous coded slice segment NAL units
1739 * shall belong to the same coded picture.
1740 * - TileId[ CtbAddrRsToTs[ prev_slice->segment_address ] ] < TileId[ CtbAddrRsToTs[ slice->segment_address ] ]
1741 * - TileId[ CtbAddrRsToTs[ prev_slice->segment_address ] ] == TileId[ CtbAddrRsToTs[ slice->segment_address ] ]
1742 * && CtbAddrRsToTs[ prev_slice->segment_address ] < CtbAddrRsToTs[ slice->segment_address ]
1744 hevc_pps_t
*prev_pps
= hevc_get_pps( info
->pps_list
, prev_slice
->pic_parameter_set_id
);
1747 hevc_sps_t
*prev_sps
= hevc_get_sps( info
->sps_list
, prev_pps
->seq_parameter_set_id
);
1750 uint64_t currTileId
;
1751 uint64_t prevTileId
;
1752 uint64_t currCtbAddrInTs
= hevc_get_ctb_address_in_tile_scan( &info
->sps
, &info
->pps
, slice
->segment_address
, &currTileId
);
1753 uint64_t prevCtbAddrInTs
= hevc_get_ctb_address_in_tile_scan( prev_sps
, prev_pps
, prev_slice
->segment_address
, &prevTileId
);
1754 if( prevTileId
< currTileId
)
1756 if( prevTileId
== currTileId
&& prevCtbAddrInTs
< currCtbAddrInTs
)
1761 int hevc_find_au_delimit_by_nalu_type
1764 uint8_t prev_nalu_type
1767 /* 7.4.2.4.4 Order of NAL units and coded pictures and their association to access units */
1768 if( prev_nalu_type
<= HEVC_NALU_TYPE_RSV_VCL31
)
1769 /* The first of any of the following NAL units after the last VCL NAL unit of a coded picture
1770 * specifies the start of a new access unit:
1771 * - access unit delimiter NAL unit (when present)
1772 * - VPS NAL unit (when present)
1773 * - SPS NAL unit (when present)
1774 * - PPS NAL unit (when present)
1775 * - Prefix SEI NAL unit (when present)
1776 * - NAL units with nal_unit_type in the range of RSV_NVCL41..RSV_NVCL44 (when present)
1777 * - NAL units with nal_unit_type in the range of UNSPEC48..UNSPEC55 (when present)
1778 * - first VCL NAL unit of a coded picture (always present) */
1779 return (nalu_type
>= HEVC_NALU_TYPE_VPS
&& nalu_type
<= HEVC_NALU_TYPE_AUD
)
1780 || (nalu_type
== HEVC_NALU_TYPE_PREFIX_SEI
)
1781 || (nalu_type
>= HEVC_NALU_TYPE_RSV_NVCL41
&& nalu_type
<= HEVC_NALU_TYPE_RSV_NVCL44
)
1782 || (nalu_type
>= HEVC_NALU_TYPE_UNSPEC48
&& nalu_type
<= HEVC_NALU_TYPE_UNSPEC55
);
1783 else if( prev_nalu_type
== HEVC_NALU_TYPE_EOS
)
1784 /* An end of sequence NAL unit shall be the last NAL unit in the access unit unless the next
1785 * NAL unit is an end of bitstream NAL unit. */
1786 return (nalu_type
!= HEVC_NALU_TYPE_EOB
);
1788 /* An end of bitstream NAL unit shall be the last NAL unit in the access unit.
1789 * Thus, the next NAL unit shall be the first NAL unit in the next access unit. */
1790 return (prev_nalu_type
== HEVC_NALU_TYPE_EOB
);
1793 int hevc_supplement_buffer
1795 hevc_stream_buffer_t
*sb
,
1796 hevc_access_unit_t
*au
,
1800 lsmash_multiple_buffers_t
*bank
= lsmash_resize_multiple_buffers( sb
->bank
, size
);
1802 return LSMASH_ERR_MEMORY_ALLOC
;
1804 sb
->rbsp
= lsmash_withdraw_buffer( bank
, 1 );
1805 if( au
&& bank
->number_of_buffers
== 3 )
1807 au
->data
= lsmash_withdraw_buffer( bank
, 2 );
1808 au
->incomplete_data
= lsmash_withdraw_buffer( bank
, 3 );
1813 static void hevc_bs_put_parameter_sets
1816 lsmash_entry_list_t
*dcr_ps_list
,
1817 uint32_t max_dcr_ps_count
1820 uint32_t dcr_ps_count
= 0;
1821 for( lsmash_entry_t
*entry
= dcr_ps_list
->head
; entry
&& dcr_ps_count
< max_dcr_ps_count
; entry
= entry
->next
)
1823 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1824 if( ps
&& !ps
->unused
)
1826 lsmash_bs_put_be16( bs
, ps
->nalUnitLength
);
1827 lsmash_bs_put_bytes( bs
, ps
->nalUnitLength
, ps
->nalUnit
);
1835 uint8_t *lsmash_create_hevc_specific_info
1837 lsmash_hevc_specific_parameters_t
*param
,
1838 uint32_t *data_length
1841 if( !param
|| !param
->parameter_arrays
|| !data_length
)
1843 if( param
->lengthSizeMinusOne
!= 0
1844 && param
->lengthSizeMinusOne
!= 1
1845 && param
->lengthSizeMinusOne
!= 3 )
1847 hevc_parameter_array_t
*param_arrays
[HEVC_DCR_NALU_TYPE_NUM
];
1848 lsmash_entry_list_t
*dcr_ps_list
[HEVC_DCR_NALU_TYPE_NUM
];
1849 for( int i
= 0; i
< HEVC_DCR_NALU_TYPE_NUM
; i
++ )
1851 param_arrays
[i
] = ¶m
->parameter_arrays
->ps_array
[i
];
1852 dcr_ps_list
[i
] = param_arrays
[i
]->list
;
1854 /* VPS, SPS and PPS are mandatory. */
1855 if( !dcr_ps_list
[0] || !dcr_ps_list
[0]->head
|| dcr_ps_list
[0]->entry_count
== 0
1856 || !dcr_ps_list
[1] || !dcr_ps_list
[1]->head
|| dcr_ps_list
[1]->entry_count
== 0
1857 || !dcr_ps_list
[2] || !dcr_ps_list
[2]->head
|| dcr_ps_list
[2]->entry_count
== 0 )
1859 /* Calculate enough buffer size. */
1860 static const uint32_t max_dcr_ps_count
[HEVC_DCR_NALU_TYPE_NUM
] =
1862 HEVC_MAX_VPS_ID
+ 1,
1863 HEVC_MAX_SPS_ID
+ 1,
1864 HEVC_MAX_PPS_ID
+ 1,
1868 uint32_t ps_count
[HEVC_DCR_NALU_TYPE_NUM
] = { 0 };
1869 for( int i
= 0; i
< HEVC_DCR_NALU_TYPE_NUM
; i
++ )
1870 if( dcr_ps_list
[i
] )
1871 for( lsmash_entry_t
*entry
= dcr_ps_list
[i
]->head
; entry
&& ps_count
[i
] < max_dcr_ps_count
[i
]; entry
= entry
->next
)
1873 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1880 /* Create an HEVCConfigurationBox */
1881 lsmash_bs_t
*bs
= lsmash_bs_create();
1884 lsmash_bs_put_be32( bs
, 0 ); /* box size */
1885 lsmash_bs_put_be32( bs
, ISOM_BOX_TYPE_HVCC
.fourcc
); /* box type: 'hvcC' */
1886 lsmash_bs_put_byte( bs
, HVCC_CONFIGURATION_VERSION
); /* configurationVersion */
1887 uint8_t temp8
= (param
->general_profile_space
<< 6)
1888 | (param
->general_tier_flag
<< 5)
1889 | param
->general_profile_idc
;
1890 lsmash_bs_put_byte( bs
, temp8
);
1891 lsmash_bs_put_be32( bs
, param
->general_profile_compatibility_flags
);
1892 lsmash_bs_put_be32( bs
, param
->general_constraint_indicator_flags
>> 16 );
1893 lsmash_bs_put_be16( bs
, param
->general_constraint_indicator_flags
);
1894 lsmash_bs_put_byte( bs
, param
->general_level_idc
);
1895 lsmash_bs_put_be16( bs
, param
->min_spatial_segmentation_idc
| 0xF000 );
1896 lsmash_bs_put_byte( bs
, param
->parallelismType
| 0xFC );
1897 lsmash_bs_put_byte( bs
, param
->chromaFormat
| 0xFC );
1898 lsmash_bs_put_byte( bs
, param
->bitDepthLumaMinus8
| 0xF8 );
1899 lsmash_bs_put_byte( bs
, param
->bitDepthChromaMinus8
| 0xF8 );
1900 lsmash_bs_put_be16( bs
, param
->avgFrameRate
);
1901 temp8
= (param
->constantFrameRate
<< 6)
1902 | (param
->numTemporalLayers
<< 3)
1903 | (param
->temporalIdNested
<< 2)
1904 | param
->lengthSizeMinusOne
;
1905 lsmash_bs_put_byte( bs
, temp8
);
1906 uint8_t numOfArrays
= !!ps_count
[0]
1911 lsmash_bs_put_byte( bs
, numOfArrays
);
1912 for( uint8_t i
= 0; i
< numOfArrays
; i
++ )
1914 temp8
= (param_arrays
[i
]->array_completeness
<< 7) | param_arrays
[i
]->NAL_unit_type
;
1915 lsmash_bs_put_byte( bs
, temp8
);
1916 lsmash_bs_put_be16( bs
, ps_count
[i
] );
1917 hevc_bs_put_parameter_sets( bs
, dcr_ps_list
[i
], ps_count
[i
] );
1919 uint8_t *data
= lsmash_bs_export_data( bs
, data_length
);
1920 lsmash_bs_cleanup( bs
);
1921 /* Update box size. */
1922 LSMASH_SET_BE32( data
, *data_length
);
1926 static inline int hevc_validate_dcr_nalu_type
1928 lsmash_hevc_dcr_nalu_type ps_type
,
1933 if( !ps_data
|| ps_length
< 3 )
1934 return LSMASH_ERR_INVALID_DATA
;
1935 if( ps_type
!= HEVC_DCR_NALU_TYPE_VPS
1936 && ps_type
!= HEVC_DCR_NALU_TYPE_SPS
1937 && ps_type
!= HEVC_DCR_NALU_TYPE_PPS
1938 && ps_type
!= HEVC_DCR_NALU_TYPE_PREFIX_SEI
1939 && ps_type
!= HEVC_DCR_NALU_TYPE_SUFFIX_SEI
)
1940 return LSMASH_ERR_INVALID_DATA
;
1941 uint8_t nalu_type
= (*((uint8_t *)ps_data
) >> 1) & 0x3f;
1942 if( nalu_type
!= HEVC_NALU_TYPE_VPS
1943 && nalu_type
!= HEVC_NALU_TYPE_SPS
1944 && nalu_type
!= HEVC_NALU_TYPE_PPS
1945 && nalu_type
!= HEVC_NALU_TYPE_PREFIX_SEI
1946 && nalu_type
!= HEVC_NALU_TYPE_SUFFIX_SEI
)
1947 return LSMASH_ERR_INVALID_DATA
;
1948 if( (ps_type
== HEVC_DCR_NALU_TYPE_VPS
&& nalu_type
!= HEVC_NALU_TYPE_VPS
)
1949 || (ps_type
== HEVC_DCR_NALU_TYPE_SPS
&& nalu_type
!= HEVC_NALU_TYPE_SPS
)
1950 || (ps_type
== HEVC_DCR_NALU_TYPE_PPS
&& nalu_type
!= HEVC_NALU_TYPE_PPS
)
1951 || (ps_type
== HEVC_DCR_NALU_TYPE_PREFIX_SEI
&& nalu_type
!= HEVC_NALU_TYPE_PREFIX_SEI
)
1952 || (ps_type
== HEVC_DCR_NALU_TYPE_SUFFIX_SEI
&& nalu_type
!= HEVC_NALU_TYPE_SUFFIX_SEI
) )
1953 return LSMASH_ERR_INVALID_DATA
;
1957 static lsmash_dcr_nalu_appendable hevc_check_vps_appendable
1959 lsmash_bits_t
*bits
,
1960 uint8_t *rbsp_buffer
,
1961 lsmash_hevc_specific_parameters_t
*param
,
1964 lsmash_entry_list_t
*ps_list
1968 if( hevc_parse_vps_minimally( bits
, &vps
, rbsp_buffer
,
1969 ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
1970 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
) < 0 )
1971 return DCR_NALU_APPEND_ERROR
;
1972 /* The value of profile_space must be identical in all the parameter sets in a single HEVC Decoder Configuration Record. */
1973 if( vps
.ptl
.general
.profile_space
!= param
->general_profile_space
)
1974 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
1976 if( vps
.ptl
.general
.profile_idc
!= param
->general_profile_idc
)
1977 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
1978 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
; entry
= entry
->next
)
1980 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
1982 return DCR_NALU_APPEND_ERROR
;
1985 uint8_t param_vps_id
;
1986 if( hevc_get_vps_id( ps
->nalUnit
+ HEVC_MIN_NALU_HEADER_LENGTH
,
1987 ps
->nalUnitLength
- HEVC_MIN_NALU_HEADER_LENGTH
, ¶m_vps_id
) < 0 )
1988 return DCR_NALU_APPEND_ERROR
;
1989 if( param_vps_id
== vps
.video_parameter_set_id
)
1990 /* VPS that has the same video_parameter_set_id already exists with different form. */
1991 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
1993 return DCR_NALU_APPEND_POSSIBLE
;
1996 static lsmash_dcr_nalu_appendable hevc_check_sps_appendable
1998 lsmash_bits_t
*bits
,
1999 uint8_t *rbsp_buffer
,
2000 lsmash_hevc_specific_parameters_t
*param
,
2003 lsmash_entry_list_t
*ps_list
2007 if( hevc_parse_sps_minimally( bits
, &sps
, rbsp_buffer
,
2008 ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2009 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
) < 0 )
2010 return DCR_NALU_APPEND_ERROR
;
2011 lsmash_bits_empty( bits
);
2012 /* The values of profile_space, chromaFormat, bitDepthLumaMinus8 and bitDepthChromaMinus8
2013 * must be identical in all the parameter sets in a single HEVC Decoder Configuration Record. */
2014 if( sps
.ptl
.general
.profile_space
!= param
->general_profile_space
2015 || sps
.chroma_format_idc
!= param
->chromaFormat
2016 || sps
.bit_depth_luma_minus8
!= param
->bitDepthLumaMinus8
2017 || sps
.bit_depth_chroma_minus8
!= param
->bitDepthChromaMinus8
)
2018 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
2019 /* FIXME; If the sequence parameter sets are marked with different profiles,
2020 * and the relevant profile compatibility flags are all zero,
2021 * then the stream may need examination to determine which profile, if any, the stream conforms to.
2022 * If the stream is not examined, or the examination reveals that there is no profile to which the stream conforms,
2023 * then the stream must be split into two or more sub-streams with separate configuration records in which these rules can be met. */
2025 if( sps
.ptl
.general
.profile_idc
!= param
->general_profile_idc
2026 && (sps
.ptl
.general
.profile_compatibility_flags
& param
->general_profile_compatibility_flags
) )
2028 if( sps
.ptl
.general
.profile_idc
!= param
->general_profile_idc
)
2030 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
2031 /* Forbidden to duplicate SPS that has the same seq_parameter_set_id with different form within the same configuration record. */
2032 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
; entry
= entry
->next
)
2034 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
2036 return DCR_NALU_APPEND_ERROR
;
2039 uint8_t param_sps_id
;
2040 if( hevc_get_sps_id( ps
->nalUnit
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2041 ps
->nalUnitLength
- HEVC_MIN_NALU_HEADER_LENGTH
, ¶m_sps_id
) < 0 )
2042 return DCR_NALU_APPEND_ERROR
;
2043 if( param_sps_id
== sps
.seq_parameter_set_id
)
2044 /* SPS that has the same seq_parameter_set_id already exists with different form. */
2045 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
2046 if( entry
== ps_list
->head
)
2048 /* Check if the cropped visual presentation sizes, the sample aspect ratios, the colour descriptions and
2049 * the default display windows are different. */
2050 hevc_sps_t first_sps
;
2051 if( hevc_parse_sps_minimally( bits
, &first_sps
, rbsp_buffer
,
2052 ps
->nalUnit
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2053 ps
->nalUnitLength
- HEVC_MIN_NALU_HEADER_LENGTH
) < 0 )
2054 return DCR_NALU_APPEND_ERROR
;
2055 if( sps
.cropped_width
!= first_sps
.cropped_width
2056 || sps
.cropped_height
!= first_sps
.cropped_height
2057 || sps
.vui
.sar_width
!= first_sps
.vui
.sar_width
2058 || sps
.vui
.sar_height
!= first_sps
.vui
.sar_height
2059 || sps
.vui
.colour_primaries
!= first_sps
.vui
.colour_primaries
2060 || sps
.vui
.transfer_characteristics
!= first_sps
.vui
.transfer_characteristics
2061 || sps
.vui
.matrix_coeffs
!= first_sps
.vui
.matrix_coeffs
2062 || sps
.vui
.video_full_range_flag
!= first_sps
.vui
.video_full_range_flag
2063 || sps
.vui
.def_disp_win_offset
.left
.n
!= first_sps
.vui
.def_disp_win_offset
.left
.n
2064 || sps
.vui
.def_disp_win_offset
.right
.n
!= first_sps
.vui
.def_disp_win_offset
.right
.n
2065 || sps
.vui
.def_disp_win_offset
.top
.n
!= first_sps
.vui
.def_disp_win_offset
.top
.n
2066 || sps
.vui
.def_disp_win_offset
.bottom
.n
!= first_sps
.vui
.def_disp_win_offset
.bottom
.n
)
2067 return DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED
;
2070 return DCR_NALU_APPEND_POSSIBLE
;
2073 static lsmash_dcr_nalu_appendable hevc_check_pps_appendable
2077 lsmash_entry_list_t
*ps_list
2081 if( hevc_get_pps_id( ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2082 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
, &pps_id
) < 0 )
2083 return DCR_NALU_APPEND_ERROR
;
2084 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
; entry
= entry
->next
)
2086 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
2088 return DCR_NALU_APPEND_ERROR
;
2091 uint8_t param_pps_id
;
2092 if( hevc_get_pps_id( ps
->nalUnit
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2093 ps
->nalUnitLength
- HEVC_MIN_NALU_HEADER_LENGTH
, ¶m_pps_id
) < 0 )
2094 return DCR_NALU_APPEND_ERROR
;
2095 if( pps_id
== param_pps_id
)
2096 /* PPS that has the same pic_parameter_set_id already exists with different form. */
2097 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
;
2099 return DCR_NALU_APPEND_POSSIBLE
;
2102 lsmash_dcr_nalu_appendable lsmash_check_hevc_dcr_nalu_appendable
2104 lsmash_hevc_specific_parameters_t
*param
,
2105 lsmash_hevc_dcr_nalu_type ps_type
,
2110 uint8_t *ps_data
= _ps_data
;
2112 return DCR_NALU_APPEND_ERROR
;
2113 if( hevc_validate_dcr_nalu_type( ps_type
, ps_data
, ps_length
) < 0 )
2114 return DCR_NALU_APPEND_ERROR
;
2115 /* Check whether the same parameter set already exsits or not. */
2116 lsmash_entry_list_t
*ps_list
= hevc_get_parameter_set_list( param
, ps_type
);
2117 if( !ps_list
|| !ps_list
->head
)
2118 return DCR_NALU_APPEND_POSSIBLE
; /* No parameter set */
2119 switch( nalu_check_same_ps_existence( ps_list
, ps_data
, ps_length
) )
2122 case 1 : return DCR_NALU_APPEND_DUPLICATED
; /* The same parameter set already exists. */
2123 default : return DCR_NALU_APPEND_ERROR
; /* An error occured. */
2125 /* Check the number of parameter sets in HEVC Decoder Configuration Record. */
2127 if( nalu_get_ps_count( ps_list
, &ps_count
) < 0 )
2128 return DCR_NALU_APPEND_ERROR
;
2129 if( (ps_type
== HEVC_DCR_NALU_TYPE_VPS
&& ps_count
>= HEVC_MAX_VPS_ID
)
2130 || (ps_type
== HEVC_DCR_NALU_TYPE_SPS
&& ps_count
>= HEVC_MAX_SPS_ID
)
2131 || (ps_type
== HEVC_DCR_NALU_TYPE_PPS
&& ps_count
>= HEVC_MAX_PPS_ID
)
2132 || (ps_type
== HEVC_DCR_NALU_TYPE_PREFIX_SEI
&& ps_count
>= UINT16_MAX
)
2133 || (ps_type
== HEVC_DCR_NALU_TYPE_SUFFIX_SEI
&& ps_count
>= UINT16_MAX
) )
2134 return DCR_NALU_APPEND_NEW_DCR_REQUIRED
; /* No more appendable parameter sets. */
2135 if( ps_type
== HEVC_DCR_NALU_TYPE_PREFIX_SEI
2136 || ps_type
== HEVC_DCR_NALU_TYPE_SUFFIX_SEI
)
2137 return DCR_NALU_APPEND_POSSIBLE
;
2138 /* Check whether a new specific info is needed or not. */
2139 if( ps_type
== HEVC_DCR_NALU_TYPE_PPS
)
2141 return hevc_check_pps_appendable( ps_data
, ps_length
, ps_list
);
2145 * Set up bitstream handler for parse parameter sets. */
2146 lsmash_bits_t
*bits
= lsmash_bits_adhoc_create();
2148 return DCR_NALU_APPEND_ERROR
;
2149 uint32_t max_ps_length
;
2150 uint8_t *rbsp_buffer
;
2151 if( nalu_get_max_ps_length( ps_list
, &max_ps_length
) < 0
2152 || (rbsp_buffer
= lsmash_malloc( LSMASH_MAX( max_ps_length
, ps_length
) )) == NULL
)
2154 lsmash_bits_adhoc_cleanup( bits
);
2155 return DCR_NALU_APPEND_ERROR
;
2157 lsmash_dcr_nalu_appendable appendable
;
2158 if( ps_type
== HEVC_DCR_NALU_TYPE_VPS
)
2159 appendable
= hevc_check_vps_appendable( bits
, rbsp_buffer
, param
, ps_data
, ps_length
, ps_list
);
2161 appendable
= hevc_check_sps_appendable( bits
, rbsp_buffer
, param
, ps_data
, ps_length
, ps_list
);
2162 lsmash_bits_adhoc_cleanup( bits
);
2163 lsmash_free( rbsp_buffer
);
2168 static inline void hevc_specific_parameters_ready
2170 lsmash_hevc_specific_parameters_t
*param
2173 param
->general_profile_compatibility_flags
= ~UINT32_C(0);
2174 param
->general_constraint_indicator_flags
= 0x0000FFFFFFFFFFFF;
2175 param
->min_spatial_segmentation_idc
= 0x0FFF;
2176 param
->avgFrameRate
= 0; /* unspecified average frame rate */
2177 param
->constantFrameRate
= 2;
2178 param
->numTemporalLayers
= 0;
2179 param
->temporalIdNested
= 1;
2182 static inline void hevc_specific_parameters_update_ptl
2184 lsmash_hevc_specific_parameters_t
*param
,
2188 param
->general_profile_space
= ptl
->general
.profile_space
;
2189 param
->general_tier_flag
= LSMASH_MAX( param
->general_tier_flag
, ptl
->general
.tier_flag
);
2190 param
->general_profile_idc
= ptl
->general
.profile_idc
;
2191 param
->general_profile_compatibility_flags
&= ptl
->general
.profile_compatibility_flags
;
2192 param
->general_constraint_indicator_flags
&= ((uint64_t)ptl
->general
.progressive_source_flag
<< 47)
2193 | ((uint64_t)ptl
->general
.interlaced_source_flag
<< 46)
2194 | ((uint64_t)ptl
->general
.non_packed_constraint_flag
<< 45)
2195 | ((uint64_t)ptl
->general
.frame_only_constraint_flag
<< 44)
2196 | ptl
->general
.reserved_zero_44bits
;
2197 param
->general_level_idc
= LSMASH_MAX( param
->general_level_idc
, ptl
->general
.level_idc
);
2200 static inline void hevc_reorder_parameter_set_ascending_id
2202 lsmash_hevc_specific_parameters_t
*param
,
2203 lsmash_hevc_dcr_nalu_type ps_type
,
2204 lsmash_entry_list_t
*ps_list
,
2208 lsmash_entry_t
*entry
= NULL
;
2210 for( int i
= ps_id
- 1; i
; i
-- )
2212 entry
= hevc_get_ps_entry_from_param( param
, ps_type
, i
);
2216 int append_head
= 0;
2219 /* Couldn't find any parameter set with lower identifier.
2220 * Next, find parameter set with upper identifier. */
2221 int max_ps_id
= ps_type
== HEVC_DCR_NALU_TYPE_VPS
? HEVC_MAX_VPS_ID
2222 : ps_type
== HEVC_DCR_NALU_TYPE_SPS
? HEVC_MAX_SPS_ID
2224 for( int i
= ps_id
+ 1; i
<= max_ps_id
; i
++ )
2226 entry
= hevc_get_ps_entry_from_param( param
, ps_type
, i
);
2234 return; /* The new entry was appended to the tail. */
2235 lsmash_entry_t
*new_entry
= ps_list
->tail
;
2238 /* before: entry[i > ps_id] ... -> prev_entry -> new_entry[ps_id]
2239 * after: new_entry[ps_id] -> entry[i > ps_id] -> ... -> prev_entry */
2240 if( new_entry
->prev
)
2241 new_entry
->prev
->next
= NULL
;
2242 new_entry
->prev
= NULL
;
2243 entry
->prev
= new_entry
;
2244 new_entry
->next
= entry
;
2247 /* before: entry[i < ps_id] -> next_entry -> ... -> prev_entry -> new_entry[ps_id]
2248 * after: entry[i < ps_id] -> new_entry[ps_id] -> next_entry -> ... -> prev_entry */
2249 if( new_entry
->prev
)
2250 new_entry
->prev
->next
= NULL
;
2251 new_entry
->prev
= entry
;
2252 new_entry
->next
= entry
->next
;
2254 entry
->next
->prev
= new_entry
;
2255 entry
->next
= new_entry
;
2258 static inline int hevc_alloc_parameter_arrays
2260 lsmash_hevc_specific_parameters_t
*param
2264 if( param
->parameter_arrays
)
2266 lsmash_hevc_parameter_arrays_t
*parameter_arrays
= lsmash_malloc_zero( sizeof(lsmash_hevc_parameter_arrays_t
) );
2267 if( !parameter_arrays
)
2268 return LSMASH_ERR_MEMORY_ALLOC
;
2269 param
->parameter_arrays
= parameter_arrays
;
2270 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_VPS
].array_completeness
= 1;
2271 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_VPS
].NAL_unit_type
= HEVC_NALU_TYPE_VPS
;
2272 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_SPS
].array_completeness
= 1;
2273 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_SPS
].NAL_unit_type
= HEVC_NALU_TYPE_SPS
;
2274 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_PPS
].array_completeness
= 1;
2275 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_PPS
].NAL_unit_type
= HEVC_NALU_TYPE_PPS
;
2276 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_PREFIX_SEI
].array_completeness
= 0;
2277 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_PREFIX_SEI
].NAL_unit_type
= HEVC_NALU_TYPE_PREFIX_SEI
;
2278 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_SUFFIX_SEI
].array_completeness
= 0;
2279 parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_SUFFIX_SEI
].NAL_unit_type
= HEVC_NALU_TYPE_SUFFIX_SEI
;
2283 int lsmash_append_hevc_dcr_nalu
2285 lsmash_hevc_specific_parameters_t
*param
,
2286 lsmash_hevc_dcr_nalu_type ps_type
,
2291 uint8_t *ps_data
= _ps_data
;
2292 if( !param
|| !ps_data
|| ps_length
< 2 )
2293 return LSMASH_ERR_FUNCTION_PARAM
;
2294 int err
= hevc_alloc_parameter_arrays( param
);
2297 hevc_parameter_array_t
*ps_array
= hevc_get_parameter_set_array( param
, ps_type
);
2299 return LSMASH_ERR_FUNCTION_PARAM
;
2300 lsmash_entry_list_t
*ps_list
= ps_array
->list
;
2301 if( ps_type
== HEVC_DCR_NALU_TYPE_PREFIX_SEI
2302 || ps_type
== HEVC_DCR_NALU_TYPE_SUFFIX_SEI
)
2304 /* Append a SEI anyway. */
2305 isom_dcr_ps_entry_t
*ps
= isom_create_ps_entry( ps_data
, ps_length
);
2307 return LSMASH_ERR_MEMORY_ALLOC
;
2308 if( lsmash_add_entry( ps_list
, ps
) < 0 )
2310 isom_remove_dcr_ps( ps
);
2311 return LSMASH_ERR_MEMORY_ALLOC
;
2315 if( ps_type
!= HEVC_DCR_NALU_TYPE_VPS
2316 && ps_type
!= HEVC_DCR_NALU_TYPE_SPS
2317 && ps_type
!= HEVC_DCR_NALU_TYPE_PPS
)
2318 return LSMASH_ERR_FUNCTION_PARAM
;
2319 /* Check if the same parameter set identifier already exists. */
2321 if( (err
= hevc_get_ps_id( ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2322 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
, &ps_id
, ps_type
)) < 0 )
2324 lsmash_entry_t
*entry
= hevc_get_ps_entry_from_param( param
, ps_type
, ps_id
);
2325 isom_dcr_ps_entry_t
*ps
= entry
? (isom_dcr_ps_entry_t
*)entry
->data
: NULL
;
2326 if( ps
&& !ps
->unused
)
2327 /* The same parameter set identifier already exists. */
2328 return LSMASH_ERR_FUNCTION_PARAM
;
2332 /* Reuse an already existed parameter set in the list. */
2334 if( ps
->nalUnit
!= ps_data
)
2336 /* The same address could be given when called by hevc_update_picture_info_for_slice(). */
2337 lsmash_free( ps
->nalUnit
);
2338 ps
->nalUnit
= ps_data
;
2340 ps
->nalUnitLength
= ps_length
;
2345 /* Create a new parameter set and append it into the list. */
2346 ps
= isom_create_ps_entry( ps_data
, ps_length
);
2348 return LSMASH_ERR_MEMORY_ALLOC
;
2349 if( lsmash_add_entry( ps_list
, ps
) < 0 )
2351 isom_remove_dcr_ps( ps
);
2352 return LSMASH_ERR_MEMORY_ALLOC
;
2356 lsmash_bits_t
*bits
= NULL
;
2357 uint8_t *rbsp_buffer
= NULL
;
2359 if( (err
= nalu_get_ps_count( ps_list
, &ps_count
)) < 0 )
2361 if( (bits
= lsmash_bits_adhoc_create()) == NULL
2362 || (rbsp_buffer
= lsmash_malloc( ps_length
)) == NULL
)
2364 err
= LSMASH_ERR_MEMORY_ALLOC
;
2367 /* Update specific info with VPS, SPS or PPS. */
2369 if( ps_type
== HEVC_DCR_NALU_TYPE_VPS
)
2372 if( (err
= hevc_parse_vps_minimally( bits
, &vps
, rbsp_buffer
,
2373 ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2374 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
)) < 0 )
2378 /* Initialize if not initialized yet. */
2379 lsmash_entry_list_t
*sps_list
= hevc_get_parameter_set_list( param
, HEVC_DCR_NALU_TYPE_SPS
);
2381 if( (err
= nalu_get_ps_count( sps_list
, &sps_count
)) < 0 )
2383 if( sps_count
== 0 )
2384 hevc_specific_parameters_ready( param
);
2386 hevc_specific_parameters_update_ptl( param
, &vps
.ptl
);
2387 param
->numTemporalLayers
= LSMASH_MAX( param
->numTemporalLayers
, vps
.max_sub_layers_minus1
+ 1 );
2388 //param->temporalIdNested &= vps.temporal_id_nesting_flag;
2390 else if( ps_type
== HEVC_DCR_NALU_TYPE_SPS
)
2393 if( (err
= hevc_parse_sps_minimally( bits
, &sps
, rbsp_buffer
,
2394 ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2395 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
)) < 0 )
2399 /* Initialize if not initialized yet. */
2400 lsmash_entry_list_t
*vps_list
= hevc_get_parameter_set_list( param
, HEVC_DCR_NALU_TYPE_VPS
);
2402 if( (err
= nalu_get_ps_count( vps_list
, &vps_count
)) < 0 )
2404 if( vps_count
== 0 )
2405 hevc_specific_parameters_ready( param
);
2407 hevc_specific_parameters_update_ptl( param
, &sps
.ptl
);
2408 param
->min_spatial_segmentation_idc
= LSMASH_MIN( param
->min_spatial_segmentation_idc
, sps
.vui
.min_spatial_segmentation_idc
);
2409 param
->chromaFormat
= sps
.chroma_format_idc
;
2410 param
->bitDepthLumaMinus8
= sps
.bit_depth_luma_minus8
;
2411 param
->bitDepthChromaMinus8
= sps
.bit_depth_chroma_minus8
;
2412 param
->numTemporalLayers
= LSMASH_MAX( param
->numTemporalLayers
, sps
.max_sub_layers_minus1
+ 1 );
2413 param
->temporalIdNested
&= sps
.temporal_id_nesting_flag
;
2414 /* Check type of constant frame rate. */
2415 if( param
->constantFrameRate
)
2418 if( param
->constantFrameRate
== 2 )
2421 for( uint8_t i
= 0; i
<= sps
.max_sub_layers_minus1
; i
++ )
2422 cfr
&= sps
.vui
.hrd
.fixed_pic_rate_general_flag
[i
];
2427 param
->constantFrameRate
= 2;
2430 for( uint8_t i
= 0; i
<= sps
.max_sub_layers_minus1
; i
++ )
2431 cfr
|= sps
.vui
.hrd
.fixed_pic_rate_general_flag
[i
];
2432 param
->constantFrameRate
= cfr
;
2436 /* FIXME: probably, we can get average frame rate according to C.3.3 Picture output. */
2437 if( param
->constantFrameRate
)
2439 uint64_t interval
= 0;
2440 for( uint8_t i
= 0; i
<= sps
.max_sub_layers_minus1
; i
++ )
2441 interval
+= sps
.vui
.num_units_in_tick
* sps
.vui
.hrd
.elemental_duration_in_tc_minus1
[i
];
2442 uint64_t frame_rate
;
2444 frame_rate
= ((256 * (2 - sps
.vui
.field_seq_flag
) * (uint64_t)sps
.vui
.time_scale
)
2445 * (sps
.max_sub_layers_minus1
+ 1)) / interval
;
2448 if( frame_rate
!= param
->avgFrameRate
&& param
->avgFrameRate
)
2449 param
->constantFrameRate
= 0;
2450 param
->avgFrameRate
= frame_rate
;
2457 if( (err
= hevc_parse_pps_minimally( bits
, &pps
, rbsp_buffer
,
2458 ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2459 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
)) < 0 )
2461 uint8_t parallelismType
= 0;
2462 #if 1 /* Replace 1 with 0 if parallelismType shall be set to 0 when min_spatial_segmentation_idc equal to 0. */
2464 if( pps
.entropy_coding_sync_enabled_flag
)
2465 parallelismType
= pps
.tiles_enabled_flag
? 0 : 3;
2466 else if( pps
.tiles_enabled_flag
)
2467 parallelismType
= 2;
2469 parallelismType
= 1;
2471 /* Parse SPS and check the value of min_spatial_segmentation_idc is equal to zero or not.
2472 * If the value is not equal to zero, update parallelismType appropriately.
2473 * If corresponding SPS is not found, set 0 to parallelismType. */
2474 entry
= hevc_get_ps_entry_from_param( param
, HEVC_DCR_NALU_TYPE_SPS
, pps
.seq_parameter_set_id
);
2475 if( entry
&& entry
->data
)
2477 ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
2478 lsmash_bits_t
*sps_bits
= lsmash_bits_adhoc_create();
2481 err
= LSMASH_ERR_MEMORY_ALLOC
;
2484 uint8_t *sps_rbsp_buffer
= lsmash_malloc( ps
->nalUnitLength
);
2485 if( !sps_rbsp_buffer
)
2487 lsmash_bits_adhoc_cleanup( sps_bits
);
2488 err
= LSMASH_ERR_MEMORY_ALLOC
;
2492 if( hevc_parse_sps_minimally( sps_bits
, &sps
, sps_rbsp_buffer
,
2493 ps
->nalUnit
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2494 ps
->nalUnitLength
- HEVC_MIN_NALU_HEADER_LENGTH
) == 0 )
2496 if( sps
.vui
.min_spatial_segmentation_idc
)
2498 if( pps
.entropy_coding_sync_enabled_flag
)
2499 parallelismType
= pps
.tiles_enabled_flag
? 0 : 3;
2500 else if( pps
.tiles_enabled_flag
)
2501 parallelismType
= 2;
2503 parallelismType
= 1;
2506 parallelismType
= 0;
2508 lsmash_bits_adhoc_cleanup( sps_bits
);
2509 lsmash_free( sps_rbsp_buffer
);
2513 param
->parallelismType
= parallelismType
;
2514 else if( param
->parallelismType
!= parallelismType
)
2515 param
->parallelismType
= 0;
2518 if( invoke_reorder
)
2519 /* Add a new parameter set in order of ascending parameter set identifier. */
2520 hevc_reorder_parameter_set_ascending_id( param
, ps_type
, ps_list
, ps_id
);
2524 ps
= (isom_dcr_ps_entry_t
*)lsmash_get_entry_data( ps_list
, ps_list
->entry_count
);
2528 lsmash_bits_adhoc_cleanup( bits
);
2529 lsmash_free( rbsp_buffer
);
2533 int hevc_try_to_append_dcr_nalu
2536 lsmash_hevc_dcr_nalu_type ps_type
,
2541 uint8_t *ps_data
= _ps_data
;
2542 lsmash_dcr_nalu_appendable ret
= lsmash_check_hevc_dcr_nalu_appendable( &info
->hvcC_param
, ps_type
, ps_data
, ps_length
);
2543 lsmash_hevc_specific_parameters_t
*param
;
2546 case DCR_NALU_APPEND_ERROR
: /* Error */
2547 return LSMASH_ERR_NAMELESS
;
2548 case DCR_NALU_APPEND_NEW_DCR_REQUIRED
: /* Mulitiple sample description is needed. */
2549 case DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED
: /* Mulitiple sample description is needed. */
2550 param
= &info
->hvcC_param_next
;
2551 info
->hvcC_pending
= 1;
2553 case DCR_NALU_APPEND_POSSIBLE
: /* Appendable */
2554 param
= info
->hvcC_pending
? &info
->hvcC_param_next
: &info
->hvcC_param
;
2556 default : /* No need to append */
2557 return DCR_NALU_APPEND_DUPLICATED
;
2562 case HEVC_DCR_NALU_TYPE_VPS
:
2563 if( (err
= hevc_parse_vps( info
, info
->buffer
.rbsp
,
2564 ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2565 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
)) < 0 )
2568 case HEVC_DCR_NALU_TYPE_SPS
:
2569 if( (err
= hevc_parse_sps( info
, info
->buffer
.rbsp
,
2570 ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2571 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
)) < 0 )
2574 case HEVC_DCR_NALU_TYPE_PPS
:
2575 if( (err
= hevc_parse_pps( info
, info
->buffer
.rbsp
,
2576 ps_data
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2577 ps_length
- HEVC_MIN_NALU_HEADER_LENGTH
)) < 0 )
2583 return lsmash_append_hevc_dcr_nalu( param
, ps_type
, ps_data
, ps_length
);
2586 static int hevc_move_dcr_nalu_entry
2588 lsmash_hevc_specific_parameters_t
*dst_data
,
2589 lsmash_hevc_specific_parameters_t
*src_data
,
2590 lsmash_hevc_dcr_nalu_type ps_type
2593 lsmash_entry_list_t
*src_ps_list
= hevc_get_parameter_set_list( src_data
, ps_type
);
2594 lsmash_entry_list_t
*dst_ps_list
= hevc_get_parameter_set_list( dst_data
, ps_type
);
2595 assert( src_ps_list
&& dst_ps_list
);
2596 for( lsmash_entry_t
*src_entry
= src_ps_list
->head
; src_entry
; src_entry
= src_entry
->next
)
2598 isom_dcr_ps_entry_t
*src_ps
= (isom_dcr_ps_entry_t
*)src_entry
->data
;
2603 if( (err
= hevc_get_ps_id( src_ps
->nalUnit
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2604 src_ps
->nalUnitLength
- HEVC_MIN_NALU_HEADER_LENGTH
,
2605 &src_ps_id
, ps_type
)) < 0 )
2607 lsmash_entry_t
*dst_entry
;
2608 for( dst_entry
= dst_ps_list
->head
; dst_entry
; dst_entry
= dst_entry
->next
)
2610 isom_dcr_ps_entry_t
*dst_ps
= (isom_dcr_ps_entry_t
*)dst_entry
->data
;
2614 if( (err
= hevc_get_ps_id( dst_ps
->nalUnit
+ HEVC_MIN_NALU_HEADER_LENGTH
,
2615 dst_ps
->nalUnitLength
- HEVC_MIN_NALU_HEADER_LENGTH
,
2616 &dst_ps_id
, ps_type
)) < 0 )
2618 if( dst_ps_id
== src_ps_id
)
2620 /* Replace the old parameter set with the new one. */
2621 assert( dst_entry
->data
!= src_entry
->data
);
2622 isom_remove_dcr_ps( dst_ps
);
2623 dst_entry
->data
= src_entry
->data
;
2624 src_entry
->data
= NULL
;
2630 /* Move the parameter set. */
2631 if( lsmash_add_entry( dst_ps_list
, src_ps
) < 0 )
2632 return LSMASH_ERR_MEMORY_ALLOC
;
2633 src_entry
->data
= NULL
;
2639 int hevc_move_pending_hvcC_param
2645 if( !info
->hvcC_pending
)
2647 /* Mark 'unused' on parameter sets within the decoder configuration record. */
2648 for( int i
= 0; i
< HEVC_DCR_NALU_TYPE_NUM
; i
++ )
2650 lsmash_entry_list_t
*ps_list
= hevc_get_parameter_set_list( &info
->hvcC_param
, i
);
2652 for( lsmash_entry_t
*entry
= ps_list
->head
; entry
; entry
= entry
->next
)
2654 isom_dcr_ps_entry_t
*ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
2660 /* Move the new parameter sets. */
2662 if( (err
= hevc_move_dcr_nalu_entry( &info
->hvcC_param
, &info
->hvcC_param_next
, HEVC_DCR_NALU_TYPE_VPS
)) < 0
2663 || (err
= hevc_move_dcr_nalu_entry( &info
->hvcC_param
, &info
->hvcC_param_next
, HEVC_DCR_NALU_TYPE_SPS
)) < 0
2664 || (err
= hevc_move_dcr_nalu_entry( &info
->hvcC_param
, &info
->hvcC_param_next
, HEVC_DCR_NALU_TYPE_PPS
)) < 0
2665 || (err
= hevc_move_dcr_nalu_entry( &info
->hvcC_param
, &info
->hvcC_param_next
, HEVC_DCR_NALU_TYPE_PREFIX_SEI
)) < 0
2666 || (err
= hevc_move_dcr_nalu_entry( &info
->hvcC_param
, &info
->hvcC_param_next
, HEVC_DCR_NALU_TYPE_SUFFIX_SEI
)) < 0 )
2668 /* Move to the pending. */
2669 lsmash_hevc_parameter_arrays_t
*parameter_arrays
= info
->hvcC_param
.parameter_arrays
; /* Back up parameter arrays. */
2670 info
->hvcC_param
= info
->hvcC_param_next
;
2671 info
->hvcC_param
.parameter_arrays
= parameter_arrays
;
2672 /* No pending hvcC. */
2673 lsmash_destroy_hevc_parameter_arrays( &info
->hvcC_param_next
);
2674 uint8_t lengthSizeMinusOne
= info
->hvcC_param_next
.lengthSizeMinusOne
;
2675 memset( &info
->hvcC_param_next
, 0, sizeof(lsmash_hevc_specific_parameters_t
) );
2676 info
->hvcC_param_next
.lengthSizeMinusOne
= lengthSizeMinusOne
;
2677 info
->hvcC_pending
= 0;
2681 int lsmash_set_hevc_array_completeness
2683 lsmash_hevc_specific_parameters_t
*param
,
2684 lsmash_hevc_dcr_nalu_type ps_type
,
2685 int array_completeness
2688 if( hevc_alloc_parameter_arrays( param
) < 0 )
2689 return LSMASH_ERR_MEMORY_ALLOC
;
2690 hevc_parameter_array_t
*ps_array
= hevc_get_parameter_set_array( param
, ps_type
);
2692 return LSMASH_ERR_FUNCTION_PARAM
;
2693 ps_array
->array_completeness
= array_completeness
;
2697 int lsmash_get_hevc_array_completeness
2699 lsmash_hevc_specific_parameters_t
*param
,
2700 lsmash_hevc_dcr_nalu_type ps_type
,
2701 int *array_completeness
2704 if( hevc_alloc_parameter_arrays( param
) < 0 )
2705 return LSMASH_ERR_MEMORY_ALLOC
;
2706 hevc_parameter_array_t
*ps_array
= hevc_get_parameter_set_array( param
, ps_type
);
2708 return LSMASH_ERR_FUNCTION_PARAM
;
2709 *array_completeness
= ps_array
->array_completeness
;
2713 static int hevc_parse_succeeded
2716 lsmash_hevc_specific_parameters_t
*param
2720 if( info
->vps
.present
2721 && info
->sps
.present
2722 && info
->pps
.present
)
2724 *param
= info
->hvcC_param
;
2725 /* Avoid freeing parameter sets. */
2726 info
->hvcC_param
.parameter_arrays
= NULL
;
2730 ret
= LSMASH_ERR_INVALID_DATA
;
2731 hevc_cleanup_parser( info
);
2735 static inline int hevc_parse_failed
2741 hevc_cleanup_parser( info
);
2745 int lsmash_setup_hevc_specific_parameters_from_access_unit
2747 lsmash_hevc_specific_parameters_t
*param
,
2749 uint32_t data_length
2752 if( !param
|| !data
|| data_length
== 0 )
2753 return LSMASH_ERR_FUNCTION_PARAM
;
2754 hevc_info_t
*info
= &(hevc_info_t
){ { 0 } };
2755 lsmash_bs_t
*bs
= &(lsmash_bs_t
){ 0 };
2756 int err
= lsmash_bs_set_empty_stream( bs
, data
, data_length
);
2759 uint64_t sc_head_pos
= nalu_find_first_start_code( bs
);
2760 if( sc_head_pos
== NALU_NO_START_CODE_FOUND
)
2761 return LSMASH_ERR_INVALID_DATA
;
2762 else if( sc_head_pos
== NALU_IO_ERROR
)
2763 return LSMASH_ERR_IO
;
2764 if( (err
= hevc_setup_parser( info
, 1 )) < 0 )
2765 return hevc_parse_failed( info
, err
);
2766 hevc_stream_buffer_t
*sb
= &info
->buffer
;
2767 hevc_slice_info_t
*slice
= &info
->slice
;
2770 hevc_nalu_header_t nuh
;
2771 uint64_t start_code_length
;
2772 uint64_t trailing_zero_bytes
;
2773 uint64_t nalu_length
= hevc_find_next_start_code( bs
, &nuh
, &start_code_length
, &trailing_zero_bytes
);
2774 if( nalu_length
== NALU_NO_START_CODE_FOUND
)
2775 /* For the last NALU. This NALU already has been parsed. */
2776 return hevc_parse_succeeded( info
, param
);
2777 uint8_t nalu_type
= nuh
.nal_unit_type
;
2778 uint64_t next_sc_head_pos
= sc_head_pos
2781 + trailing_zero_bytes
;
2782 if( nalu_type
== HEVC_NALU_TYPE_FD
)
2784 /* We don't support streams with both filler and HRD yet. Otherwise, just skip filler. */
2785 if( info
->sps
.vui
.hrd
.present
)
2786 return hevc_parse_failed( info
, LSMASH_ERR_PATCH_WELCOME
);
2788 else if( nalu_type
<= HEVC_NALU_TYPE_RASL_R
2789 || (nalu_type
>= HEVC_NALU_TYPE_BLA_W_LP
&& nalu_type
<= HEVC_NALU_TYPE_CRA
)
2790 || (nalu_type
>= HEVC_NALU_TYPE_VPS
&& nalu_type
<= HEVC_NALU_TYPE_SUFFIX_SEI
) )
2792 /* Increase the buffer if needed. */
2793 uint64_t possible_au_length
= NALU_DEFAULT_NALU_LENGTH_SIZE
+ nalu_length
;
2794 if( sb
->bank
->buffer_size
< possible_au_length
2795 && (err
= hevc_supplement_buffer( sb
, NULL
, 2 * possible_au_length
)) < 0 )
2796 return hevc_parse_failed( info
, err
);
2797 /* Get the EBSP of the current NALU here. */
2798 uint8_t *nalu
= lsmash_bs_get_buffer_data( bs
) + start_code_length
;
2799 if( nalu_type
<= HEVC_NALU_TYPE_RSV_VCL31
)
2801 /* VCL NALU (slice) */
2802 hevc_slice_info_t prev_slice
= *slice
;
2803 if( (err
= hevc_parse_slice_segment_header( info
, &nuh
, sb
->rbsp
, nalu
+ nuh
.length
, nalu_length
- nuh
.length
)) < 0 )
2804 return hevc_parse_failed( info
, err
);
2805 if( prev_slice
.present
)
2807 /* Check whether the AU that contains the previous VCL NALU completed or not. */
2808 if( hevc_find_au_delimit_by_slice_info( info
, slice
, &prev_slice
) )
2809 /* The current NALU is the first VCL NALU of the primary coded picture of a new AU.
2810 * Therefore, the previous slice belongs to that new AU. */
2811 return hevc_parse_succeeded( info
, param
);
2817 if( hevc_find_au_delimit_by_nalu_type( nalu_type
, info
->prev_nalu_type
) )
2818 /* The last slice belongs to the AU you want at this time. */
2819 return hevc_parse_succeeded( info
, param
);
2822 case HEVC_NALU_TYPE_VPS
:
2823 if( (err
= hevc_try_to_append_dcr_nalu( info
, HEVC_DCR_NALU_TYPE_VPS
, nalu
, nalu_length
)) < 0 )
2824 return hevc_parse_failed( info
, err
);
2826 case HEVC_NALU_TYPE_SPS
:
2827 if( (err
= hevc_try_to_append_dcr_nalu( info
, HEVC_DCR_NALU_TYPE_SPS
, nalu
, nalu_length
)) < 0 )
2828 return hevc_parse_failed( info
, err
);
2830 case HEVC_NALU_TYPE_PPS
:
2831 if( (err
= hevc_try_to_append_dcr_nalu( info
, HEVC_DCR_NALU_TYPE_PPS
, nalu
, nalu_length
)) < 0 )
2832 return hevc_parse_failed( info
, err
);
2839 /* Move to the first byte of the next start code. */
2840 info
->prev_nalu_type
= nalu_type
;
2841 if( lsmash_bs_read_seek( bs
, next_sc_head_pos
, SEEK_SET
) != next_sc_head_pos
)
2842 return hevc_parse_failed( info
, LSMASH_ERR_NAMELESS
);
2843 /* Check if no more data to read from the stream. */
2844 if( !lsmash_bs_is_end( bs
, NALU_SHORT_START_CODE_LENGTH
) )
2845 sc_head_pos
= next_sc_head_pos
;
2847 return hevc_parse_succeeded( info
, param
);
2851 int hevc_construct_specific_parameters
2853 lsmash_codec_specific_t
*dst
,
2854 lsmash_codec_specific_t
*src
2857 assert( dst
&& dst
->data
.structured
&& src
&& src
->data
.unstructured
);
2858 if( src
->size
< ISOM_BASEBOX_COMMON_SIZE
+ 7 )
2859 return LSMASH_ERR_INVALID_DATA
;
2860 lsmash_hevc_specific_parameters_t
*param
= (lsmash_hevc_specific_parameters_t
*)dst
->data
.structured
;
2861 uint8_t *data
= src
->data
.unstructured
;
2862 uint64_t size
= LSMASH_GET_BE32( data
);
2863 data
+= ISOM_BASEBOX_COMMON_SIZE
;
2866 size
= LSMASH_GET_BE64( data
);
2869 if( size
!= src
->size
)
2870 return LSMASH_ERR_INVALID_DATA
;
2871 if( hevc_alloc_parameter_arrays( param
) < 0 )
2872 return LSMASH_ERR_MEMORY_ALLOC
;
2873 lsmash_bs_t
*bs
= lsmash_bs_create();
2875 return LSMASH_ERR_MEMORY_ALLOC
;
2876 int err
= lsmash_bs_import_data( bs
, data
, src
->size
- (data
- src
->data
.unstructured
) );
2879 if( lsmash_bs_get_byte( bs
) != HVCC_CONFIGURATION_VERSION
)
2881 err
= LSMASH_ERR_INVALID_DATA
;
2882 goto fail
; /* We don't support configurationVersion other than HVCC_CONFIGURATION_VERSION. */
2884 uint8_t temp8
= lsmash_bs_get_byte( bs
);
2885 param
->general_profile_space
= (temp8
>> 6) & 0x03;
2886 param
->general_tier_flag
= (temp8
>> 5) & 0x01;
2887 param
->general_profile_idc
= temp8
& 0x1F;
2888 param
->general_profile_compatibility_flags
= lsmash_bs_get_be32( bs
);
2889 uint32_t temp32
= lsmash_bs_get_be32( bs
);
2890 uint16_t temp16
= lsmash_bs_get_be16( bs
);
2891 param
->general_constraint_indicator_flags
= ((uint64_t)temp32
<< 16) | temp16
;
2892 param
->general_level_idc
= lsmash_bs_get_byte( bs
);
2893 param
->min_spatial_segmentation_idc
= lsmash_bs_get_be16( bs
) & 0x0FFF;
2894 param
->parallelismType
= lsmash_bs_get_byte( bs
) & 0x03;
2895 param
->chromaFormat
= lsmash_bs_get_byte( bs
) & 0x03;
2896 param
->bitDepthLumaMinus8
= lsmash_bs_get_byte( bs
) & 0x07;
2897 param
->bitDepthChromaMinus8
= lsmash_bs_get_byte( bs
) & 0x07;
2898 param
->avgFrameRate
= lsmash_bs_get_be16( bs
);
2899 temp8
= lsmash_bs_get_byte( bs
);
2900 param
->constantFrameRate
= (temp8
>> 6) & 0x03;
2901 param
->numTemporalLayers
= (temp8
>> 3) & 0x07;
2902 param
->temporalIdNested
= (temp8
>> 2) & 0x01;
2903 param
->lengthSizeMinusOne
= temp8
& 0x03;
2904 uint8_t numOfArrays
= lsmash_bs_get_byte( bs
);
2905 for( uint8_t i
= 0; i
< numOfArrays
; i
++ )
2907 hevc_parameter_array_t param_array
;
2908 memset( ¶m_array
, 0, sizeof(hevc_parameter_array_t
) );
2909 temp8
= lsmash_bs_get_byte( bs
);
2910 param_array
.array_completeness
= (temp8
>> 7) & 0x01;
2911 param_array
.NAL_unit_type
= temp8
& 0x3F;
2912 param_array
.list
->entry_count
= lsmash_bs_get_be16( bs
);
2913 if( param_array
.NAL_unit_type
== HEVC_NALU_TYPE_VPS
2914 || param_array
.NAL_unit_type
== HEVC_NALU_TYPE_SPS
2915 || param_array
.NAL_unit_type
== HEVC_NALU_TYPE_PPS
2916 || param_array
.NAL_unit_type
== HEVC_NALU_TYPE_PREFIX_SEI
2917 || param_array
.NAL_unit_type
== HEVC_NALU_TYPE_SUFFIX_SEI
)
2919 if( (err
= nalu_get_dcr_ps( bs
, param_array
.list
, param_array
.list
->entry_count
)) < 0 )
2923 for( uint16_t j
= 0; j
< param_array
.list
->entry_count
; j
++ )
2925 uint16_t nalUnitLength
= lsmash_bs_get_be16( bs
);
2926 lsmash_bs_skip_bytes( bs
, nalUnitLength
); /* nalUnit */
2928 switch( param_array
.NAL_unit_type
)
2930 case HEVC_NALU_TYPE_VPS
:
2931 param
->parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_VPS
] = param_array
;
2933 case HEVC_NALU_TYPE_SPS
:
2934 param
->parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_SPS
] = param_array
;
2936 case HEVC_NALU_TYPE_PPS
:
2937 param
->parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_PPS
] = param_array
;
2939 case HEVC_NALU_TYPE_PREFIX_SEI
:
2940 param
->parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_PREFIX_SEI
] = param_array
;
2942 case HEVC_NALU_TYPE_SUFFIX_SEI
:
2943 param
->parameter_arrays
->ps_array
[HEVC_DCR_NALU_TYPE_SUFFIX_SEI
] = param_array
;
2946 /* Discard unknown NALUs. */
2950 lsmash_bs_cleanup( bs
);
2953 lsmash_bs_cleanup( bs
);
2957 int hevc_print_codec_specific
2960 lsmash_file_t
*file
,
2965 assert( fp
&& file
&& box
&& (box
->manager
& LSMASH_BINARY_CODED_BOX
) );
2967 lsmash_ifprintf( fp
, indent
++, "[%s: HEVC Configuration Box]\n", isom_4cc2str( box
->type
.fourcc
) );
2968 lsmash_ifprintf( fp
, indent
, "position = %"PRIu64
"\n", box
->pos
);
2969 lsmash_ifprintf( fp
, indent
, "size = %"PRIu64
"\n", box
->size
);
2970 uint8_t *data
= box
->binary
;
2971 uint32_t offset
= isom_skip_box_common( &data
);
2972 lsmash_bs_t
*bs
= lsmash_bs_create();
2974 return LSMASH_ERR_MEMORY_ALLOC
;
2975 int err
= lsmash_bs_import_data( bs
, data
, box
->size
- offset
);
2978 lsmash_bs_cleanup( bs
);
2981 uint8_t configurationVersion
= lsmash_bs_get_byte( bs
);
2982 lsmash_ifprintf( fp
, indent
, "configurationVersion = %"PRIu8
"\n", configurationVersion
);
2983 if( configurationVersion
!= HVCC_CONFIGURATION_VERSION
)
2985 lsmash_bs_cleanup( bs
);
2988 uint8_t temp8
= lsmash_bs_get_byte( bs
);
2989 lsmash_ifprintf( fp
, indent
, "general_profile_space = %"PRIu8
"\n", (temp8
>> 6) & 0x03 );
2990 lsmash_ifprintf( fp
, indent
, "general_tier_flag = %"PRIu8
"\n", (temp8
>> 5) & 0x01 );
2991 lsmash_ifprintf( fp
, indent
, "general_profile_idc = %"PRIu8
"\n", temp8
& 0x1F );
2992 lsmash_ifprintf( fp
, indent
, "general_profile_compatibility_flags = 0x%08"PRIx32
"\n", lsmash_bs_get_be32( bs
) );
2993 uint32_t temp32
= lsmash_bs_get_be32( bs
);
2994 uint16_t temp16
= lsmash_bs_get_be16( bs
);
2995 lsmash_ifprintf( fp
, indent
, "general_constraint_indicator_flags = 0x%012"PRIx64
"\n", ((uint64_t)temp32
<< 16) | temp16
);
2996 uint8_t general_level_idc
= lsmash_bs_get_byte( bs
);
2997 lsmash_ifprintf( fp
, indent
, "general_level_idc = %"PRIu8
" (Level %g)\n", general_level_idc
, general_level_idc
/ 30.0 );
2998 temp16
= lsmash_bs_get_be16( bs
);
2999 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp16
>> 12) & 0x0F );
3000 lsmash_ifprintf( fp
, indent
, "min_spatial_segmentation_idc = %"PRIu16
"\n", temp16
& 0x0FFF );
3001 temp8
= lsmash_bs_get_byte( bs
);
3002 uint8_t parallelismType
= temp8
& 0x03;
3003 static const char *parallelism_table
[4] =
3005 "Mixed types or Unknown",
3008 "Entropy coding synchronization based / WPP: Wavefront Parallel Processing"
3010 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 2) & 0x3F );
3011 lsmash_ifprintf( fp
, indent
, "parallelismType = %"PRIu8
" (%s)\n", parallelismType
,
3012 parallelism_table
[parallelismType
] );
3013 temp8
= lsmash_bs_get_byte( bs
);
3014 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 2) & 0x3F );
3015 lsmash_ifprintf( fp
, indent
, "chromaFormat = %"PRIu8
"\n", temp8
& 0x03 );
3016 temp8
= lsmash_bs_get_byte( bs
);
3017 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 3) & 0x1F );
3018 lsmash_ifprintf( fp
, indent
, "bitDepthLumaMinus8 = %"PRIu8
"\n", temp8
& 0x07 );
3019 temp8
= lsmash_bs_get_byte( bs
);
3020 lsmash_ifprintf( fp
, indent
, "reserved = 0x%02"PRIx8
"\n", (temp8
>> 3) & 0x1F );
3021 lsmash_ifprintf( fp
, indent
, "bitDepthChromaMinus8 = %"PRIu8
"\n", temp8
& 0x07 );
3022 lsmash_ifprintf( fp
, indent
, "avgFrameRate = %"PRIu16
"\n", lsmash_bs_get_be16( bs
) );
3023 temp8
= lsmash_bs_get_byte( bs
);
3024 lsmash_ifprintf( fp
, indent
, "constantFrameRate = %"PRIu8
"\n", (temp8
>> 6) & 0x03 );
3025 lsmash_ifprintf( fp
, indent
, "numTemporalLayers = %"PRIu8
"\n", (temp8
>> 3) & 0x07 );
3026 lsmash_ifprintf( fp
, indent
, "temporalIdNested = %"PRIu8
"\n", (temp8
>> 2) & 0x01 );
3027 lsmash_ifprintf( fp
, indent
, "lengthSizeMinusOne = %"PRIu8
"\n", temp8
& 0x03 );
3028 uint8_t numOfArrays
= lsmash_bs_get_byte( bs
);
3029 lsmash_ifprintf( fp
, indent
, "numOfArrays = %"PRIu8
"\n", numOfArrays
);
3030 for( uint8_t i
= 0; i
< numOfArrays
; i
++ )
3032 int array_indent
= indent
+ 1;
3033 lsmash_ifprintf( fp
, array_indent
++, "array[%"PRIu8
"]\n", i
);
3034 temp8
= lsmash_bs_get_byte( bs
);
3035 lsmash_ifprintf( fp
, array_indent
, "array_completeness = %"PRIu8
"\n", (temp8
>> 7) & 0x01 );
3036 lsmash_ifprintf( fp
, array_indent
, "reserved = %"PRIu8
"\n", (temp8
>> 6) & 0x01 );
3037 lsmash_ifprintf( fp
, array_indent
, "NAL_unit_type = %"PRIu8
"\n", temp8
& 0x3F );
3038 uint16_t numNalus
= lsmash_bs_get_be16( bs
);
3039 lsmash_ifprintf( fp
, array_indent
, "numNalus = %"PRIu16
"\n", numNalus
);
3040 for( uint16_t j
= 0; j
< numNalus
; j
++ )
3042 uint16_t nalUnitLength
= lsmash_bs_get_be16( bs
);
3043 lsmash_bs_skip_bytes( bs
, nalUnitLength
);
3044 lsmash_ifprintf( fp
, array_indent
, "nalUnit[%"PRIu16
"]\n", j
);
3045 lsmash_ifprintf( fp
, array_indent
+ 1, "nalUnitLength = %"PRIu16
"\n", nalUnitLength
);
3048 lsmash_bs_cleanup( bs
);
3052 static inline int hevc_copy_dcr_nalu_array
3054 lsmash_hevc_specific_parameters_t
*dst_data
,
3055 lsmash_hevc_specific_parameters_t
*src_data
,
3056 lsmash_hevc_dcr_nalu_type ps_type
3059 hevc_parameter_array_t
*src_ps_array
= hevc_get_parameter_set_array( src_data
, ps_type
);
3060 hevc_parameter_array_t
*dst_ps_array
= hevc_get_parameter_set_array( dst_data
, ps_type
);
3061 assert( src_ps_array
&& dst_ps_array
);
3062 dst_ps_array
->array_completeness
= src_ps_array
->array_completeness
;
3063 dst_ps_array
->NAL_unit_type
= src_ps_array
->NAL_unit_type
;
3064 lsmash_entry_list_t
*src_ps_list
= src_ps_array
->list
;
3065 lsmash_entry_list_t
*dst_ps_list
= dst_ps_array
->list
;
3066 for( lsmash_entry_t
*entry
= src_ps_list
->head
; entry
; entry
= entry
->next
)
3068 isom_dcr_ps_entry_t
*src_ps
= (isom_dcr_ps_entry_t
*)entry
->data
;
3069 if( !src_ps
|| src_ps
->unused
)
3071 isom_dcr_ps_entry_t
*dst_ps
= isom_create_ps_entry( src_ps
->nalUnit
, src_ps
->nalUnitLength
);
3074 lsmash_destroy_hevc_parameter_arrays( dst_data
);
3075 return LSMASH_ERR_MEMORY_ALLOC
;
3077 if( lsmash_add_entry( dst_ps_list
, dst_ps
) < 0 )
3079 lsmash_destroy_hevc_parameter_arrays( dst_data
);
3080 isom_remove_dcr_ps( dst_ps
);
3081 return LSMASH_ERR_MEMORY_ALLOC
;
3087 int hevc_copy_codec_specific
3089 lsmash_codec_specific_t
*dst
,
3090 lsmash_codec_specific_t
*src
3093 assert( src
&& src
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
&& src
->data
.structured
);
3094 assert( dst
&& dst
->format
== LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
&& dst
->data
.structured
);
3095 lsmash_hevc_specific_parameters_t
*src_data
= (lsmash_hevc_specific_parameters_t
*)src
->data
.structured
;
3096 lsmash_hevc_specific_parameters_t
*dst_data
= (lsmash_hevc_specific_parameters_t
*)dst
->data
.structured
;
3097 lsmash_destroy_hevc_parameter_arrays( dst_data
);
3098 *dst_data
= *src_data
;
3099 if( !src_data
->parameter_arrays
)
3101 dst_data
->parameter_arrays
= lsmash_malloc_zero( sizeof(lsmash_hevc_parameter_arrays_t
) );
3102 if( !dst_data
->parameter_arrays
)
3103 return LSMASH_ERR_MEMORY_ALLOC
;
3104 for( int i
= 0; i
< HEVC_DCR_NALU_TYPE_NUM
; i
++ )
3106 int err
= hevc_copy_dcr_nalu_array( dst_data
, src_data
, (lsmash_hevc_dcr_nalu_type
)i
);