timeline: Utilize lsmash_freep().
[L-SMASH.git] / codecs / hevc.c
blob3ceaf73d7adc26af79c67753a849ed08f6f6bdb2
1 /*****************************************************************************
2 * hevc.c:
3 *****************************************************************************
4 * Copyright (C) 2013-2014 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
21 /* This file is available under an ISC license. */
23 #include "common/internal.h" /* must be placed first */
25 #include <string.h>
26 #include <stdlib.h>
27 #include <inttypes.h>
29 #include "core/box.h"
31 /***************************************************************************
32 ITU-T Recommendation H.265 (04/13)
33 ISO/IEC 14496-15:2014
34 ***************************************************************************/
35 #include "hevc.h"
36 #include "nalu.h"
38 #define IF_INVALID_VALUE( x ) if( x )
39 #define IF_EXCEED_INT32( x ) if( (x) < INT32_MIN || (x) > INT32_MAX )
40 #define HEVC_POC_DEBUG_PRINT 0
42 #define HEVC_MIN_NALU_HEADER_LENGTH 2
43 #define HEVC_MAX_VPS_ID 15
44 #define HEVC_MAX_SPS_ID 15
45 #define HEVC_MAX_PPS_ID 63
46 #define HEVC_MAX_DPB_SIZE 16
47 #define HVCC_CONFIGURATION_VERSION 1
49 typedef enum
51 HEVC_SLICE_TYPE_B = 0,
52 HEVC_SLICE_TYPE_P = 1,
53 HEVC_SLICE_TYPE_I = 2,
54 } hevc_slice_type;
56 void lsmash_destroy_hevc_parameter_arrays
58 lsmash_hevc_specific_parameters_t *param
61 if( !param || !param->parameter_arrays )
62 return;
63 for( int i = 0; i < HEVC_DCR_NALU_TYPE_NUM; i++ )
64 lsmash_remove_entries( param->parameter_arrays->ps_array[i].list, isom_remove_dcr_ps );
65 lsmash_free( param->parameter_arrays );
66 param->parameter_arrays = NULL;
69 void hevc_destruct_specific_data
71 void *data
74 if( !data )
75 return;
76 lsmash_destroy_hevc_parameter_arrays( data );
77 lsmash_free( data );
80 static void hevc_remove_pps
82 hevc_pps_t *pps
85 if( !pps )
86 return;
87 lsmash_free( pps->colWidth );
88 lsmash_free( pps->rowHeight );
89 lsmash_free( pps );
92 void hevc_cleanup_parser
94 hevc_info_t *info
97 if( !info )
98 return;
99 lsmash_remove_entries( info->vps_list, NULL );
100 lsmash_remove_entries( info->sps_list, NULL );
101 lsmash_remove_entries( info->pps_list, hevc_remove_pps );
102 lsmash_destroy_hevc_parameter_arrays( &info->hvcC_param );
103 lsmash_destroy_hevc_parameter_arrays( &info->hvcC_param_next );
104 lsmash_destroy_multiple_buffers( info->buffer.bank );
105 lsmash_bits_adhoc_cleanup( info->bits );
106 info->bits = NULL;
109 int hevc_setup_parser
111 hevc_info_t *info,
112 int parse_only
115 if( !info )
116 return -1;
117 memset( info, 0, sizeof(hevc_info_t) );
118 info->hvcC_param .lengthSizeMinusOne = NALU_DEFAULT_NALU_LENGTH_SIZE - 1;
119 info->hvcC_param_next.lengthSizeMinusOne = NALU_DEFAULT_NALU_LENGTH_SIZE - 1;
120 hevc_stream_buffer_t *sb = &info->buffer;
121 sb->bank = lsmash_create_multiple_buffers( parse_only ? 1 : 3, NALU_DEFAULT_BUFFER_SIZE );
122 if( !sb->bank )
123 return -1;
124 sb->rbsp = lsmash_withdraw_buffer( sb->bank, 1 );
125 if( !parse_only )
127 info->au.data = lsmash_withdraw_buffer( sb->bank, 2 );
128 info->au.incomplete_data = lsmash_withdraw_buffer( sb->bank, 3 );
130 info->bits = lsmash_bits_adhoc_create();
131 if( !info->bits )
133 lsmash_destroy_multiple_buffers( sb->bank );
134 return -1;
136 lsmash_init_entry_list( info->vps_list );
137 lsmash_init_entry_list( info->sps_list );
138 lsmash_init_entry_list( info->pps_list );
139 info->prev_nalu_type = HEVC_NALU_TYPE_UNKNOWN;
140 return 0;
143 static int hevc_check_nalu_header
145 lsmash_bs_t *bs,
146 hevc_nalu_header_t *nuh,
147 int use_long_start_code
150 /* Check if the enough length of NALU header on the buffer. */
151 int start_code_length = use_long_start_code ? NALU_LONG_START_CODE_LENGTH : NALU_SHORT_START_CODE_LENGTH;
152 if( lsmash_bs_is_end( bs, start_code_length + 1 ) )
153 return -1;
154 /* Read NALU header. */
155 uint16_t temp16 = lsmash_bs_show_be16( bs, start_code_length );
156 nuh->forbidden_zero_bit = (temp16 >> 15) & 0x01;
157 nuh->nal_unit_type = (temp16 >> 9) & 0x3f;
158 nuh->nuh_layer_id = (temp16 >> 3) & 0x3f;
159 uint8_t nuh_temporal_id_plus1 = temp16 & 0x07;
160 IF_INVALID_VALUE( nuh->forbidden_zero_bit || nuh_temporal_id_plus1 == 0 )
161 return -1;
162 nuh->TemporalId = nuh_temporal_id_plus1 - 1;
163 nuh->length = HEVC_MIN_NALU_HEADER_LENGTH;
164 /* nuh_layer_id shall be 0 in the specification we refer to. */
165 if( nuh->nuh_layer_id )
166 return -1;
167 if( nuh->TemporalId == 0 )
169 /* For TSA_N, TSA_R, STSA_N and STSA_R, TemporalId shall not be equal to 0. */
170 IF_INVALID_VALUE( nuh->nal_unit_type >= HEVC_NALU_TYPE_TSA_N
171 && nuh->nal_unit_type <= HEVC_NALU_TYPE_STSA_R )
172 return -1;
174 else
176 /* For BLA_W_LP to RSV_IRAP_VCL23, TemporalId shall be equal to 0. */
177 IF_INVALID_VALUE( nuh->nal_unit_type >= HEVC_NALU_TYPE_BLA_W_LP
178 && nuh->nal_unit_type <= HEVC_NALU_TYPE_RSV_IRAP_VCL23 )
179 return -1;
180 /* For VPS, SPS, EOS and EOB, TemporalId shall be equal to 0. */
181 IF_INVALID_VALUE( nuh->nal_unit_type >= HEVC_NALU_TYPE_VPS
182 && nuh->nal_unit_type <= HEVC_NALU_TYPE_EOB
183 && nuh->nal_unit_type != HEVC_NALU_TYPE_PPS
184 && nuh->nal_unit_type != HEVC_NALU_TYPE_AUD )
185 return -1;
187 /* VPS, SPS and PPS require long start code (0x00000001).
188 * Also AU delimiter requires it too because this type of NALU shall be the first NALU of any AU if present. */
189 IF_INVALID_VALUE( !use_long_start_code
190 && nuh->nal_unit_type >= HEVC_NALU_TYPE_VPS
191 && nuh->nal_unit_type <= HEVC_NALU_TYPE_AUD )
192 return -1;
193 return 0;
196 uint64_t hevc_find_next_start_code
198 lsmash_bs_t *bs,
199 hevc_nalu_header_t *nuh,
200 uint64_t *start_code_length,
201 uint64_t *trailing_zero_bytes
204 uint64_t length = 0; /* the length of the latest NALU */
205 uint64_t count = 0; /* the number of the trailing zero bytes after the latest NALU */
206 /* Check the type of the current start code. */
207 int long_start_code
208 = (!lsmash_bs_is_end( bs, NALU_LONG_START_CODE_LENGTH ) && 0x00000001 == lsmash_bs_show_be32( bs, 0 )) ? 1
209 : (!lsmash_bs_is_end( bs, NALU_SHORT_START_CODE_LENGTH ) && 0x000001 == lsmash_bs_show_be24( bs, 0 )) ? 0
210 : -1;
211 if( long_start_code >= 0 && hevc_check_nalu_header( bs, nuh, long_start_code ) == 0 )
213 *start_code_length = long_start_code ? NALU_LONG_START_CODE_LENGTH : NALU_SHORT_START_CODE_LENGTH;
214 uint64_t distance = *start_code_length + nuh->length;
215 /* Find the start code of the next NALU and get the distance from the start code of the latest NALU. */
216 if( !lsmash_bs_is_end( bs, distance + NALU_SHORT_START_CODE_LENGTH ) )
218 uint32_t sync_bytes = lsmash_bs_show_be24( bs, distance );
219 while( 0x000001 != sync_bytes )
221 if( lsmash_bs_is_end( bs, ++distance + NALU_SHORT_START_CODE_LENGTH ) )
223 distance = lsmash_bs_get_remaining_buffer_size( bs );
224 break;
226 sync_bytes <<= 8;
227 sync_bytes |= lsmash_bs_show_byte( bs, distance + NALU_SHORT_START_CODE_LENGTH - 1 );
228 sync_bytes &= 0xFFFFFF;
231 else
232 distance = lsmash_bs_get_remaining_buffer_size( bs );
233 /* Any NALU has no consecutive zero bytes at the end. */
234 while( 0x00 == lsmash_bs_show_byte( bs, distance - 1 ) )
236 --distance;
237 ++count;
239 /* Remove the length of the start code. */
240 length = distance - *start_code_length;
241 /* If there are one or more trailing zero bytes, we treat the last one byte as a part of the next start code.
242 * This makes the next start code a long start code. */
243 if( count )
244 --count;
246 else
248 /* No start code. */
249 nuh->forbidden_zero_bit = 1; /* shall be 0, so invalid */
250 nuh->nal_unit_type = HEVC_NALU_TYPE_UNKNOWN;
251 nuh->nuh_layer_id = 0; /* arbitrary */
252 nuh->TemporalId = 0; /* arbitrary */
253 nuh->length = 0;
254 *start_code_length = 0;
256 *trailing_zero_bytes = count;
257 return length;
260 static hevc_vps_t *hevc_get_vps
262 lsmash_entry_list_t *vps_list,
263 uint8_t vps_id
266 if( !vps_list || vps_id > HEVC_MAX_VPS_ID )
267 return NULL;
268 for( lsmash_entry_t *entry = vps_list->head; entry; entry = entry->next )
270 hevc_vps_t *vps = (hevc_vps_t *)entry->data;
271 if( !vps )
272 return NULL;
273 if( vps->video_parameter_set_id == vps_id )
274 return vps;
276 hevc_vps_t *vps = lsmash_malloc_zero( sizeof(hevc_vps_t) );
277 if( !vps )
278 return NULL;
279 vps->video_parameter_set_id = vps_id;
280 if( lsmash_add_entry( vps_list, vps ) )
282 lsmash_free( vps );
283 return NULL;
285 return vps;
288 static hevc_sps_t *hevc_get_sps
290 lsmash_entry_list_t *sps_list,
291 uint8_t sps_id
294 if( !sps_list || sps_id > HEVC_MAX_SPS_ID )
295 return NULL;
296 for( lsmash_entry_t *entry = sps_list->head; entry; entry = entry->next )
298 hevc_sps_t *sps = (hevc_sps_t *)entry->data;
299 if( !sps )
300 return NULL;
301 if( sps->seq_parameter_set_id == sps_id )
302 return sps;
304 hevc_sps_t *sps = lsmash_malloc_zero( sizeof(hevc_sps_t) );
305 if( !sps )
306 return NULL;
307 sps->seq_parameter_set_id = sps_id;
308 if( lsmash_add_entry( sps_list, sps ) )
310 lsmash_free( sps );
311 return NULL;
313 return sps;
316 static hevc_pps_t *hevc_get_pps
318 lsmash_entry_list_t *pps_list,
319 uint8_t pps_id
322 if( !pps_list || pps_id > HEVC_MAX_PPS_ID )
323 return NULL;
324 for( lsmash_entry_t *entry = pps_list->head; entry; entry = entry->next )
326 hevc_pps_t *pps = (hevc_pps_t *)entry->data;
327 if( !pps )
328 return NULL;
329 if( pps->pic_parameter_set_id == pps_id )
330 return pps;
332 hevc_pps_t *pps = lsmash_malloc_zero( sizeof(hevc_pps_t) );
333 if( !pps )
334 return NULL;
335 pps->pic_parameter_set_id = pps_id;
336 if( lsmash_add_entry( pps_list, pps ) )
338 lsmash_free( pps );
339 return NULL;
341 return pps;
344 int hevc_calculate_poc
346 hevc_info_t *info,
347 hevc_picture_info_t *picture,
348 hevc_picture_info_t *prev_picture
351 #if HEVC_POC_DEBUG_PRINT
352 fprintf( stderr, "PictureOrderCount\n" );
353 #endif
354 hevc_pps_t *pps = hevc_get_pps( info->pps_list, picture->pic_parameter_set_id );
355 if( !pps )
356 return -1;
357 hevc_sps_t *sps = hevc_get_sps( info->sps_list, pps->seq_parameter_set_id );
358 if( !sps )
359 return -1;
360 /* 8.3.1 Decoding process for picture order count
361 * This process needs to be invoked only for the first slice segment of a picture. */
362 int NoRaslOutputFlag;
363 if( picture->irap )
365 /* 8.1 General decoding process
366 * If the current picture is an IDR picture, a BLA picture, the first picture in the
367 * bitstream in decoding order, or the first picture that follows an end of sequence
368 * NAL unit in decoding order, the variable NoRaslOutputFlag is set equal to 1.
370 * Note that not only the end of sequence NAL unit but the end of bistream NAL unit as
371 * well specify that the current access unit is the last access unit in the coded video
372 * sequence in decoding order. */
373 NoRaslOutputFlag = picture->idr || picture->broken_link || info->eos;
374 if( info->eos )
375 info->eos = 0;
377 else
378 NoRaslOutputFlag = 0;
379 int64_t poc_msb;
380 int32_t poc_lsb = picture->poc_lsb;
381 if( picture->irap && NoRaslOutputFlag )
382 poc_msb = 0;
383 else
385 int32_t prev_poc_msb = picture->idr ? 0 : prev_picture->tid0_poc_msb;
386 int32_t prev_poc_lsb = picture->idr ? 0 : prev_picture->tid0_poc_lsb;
387 int32_t max_poc_lsb = 1 << sps->log2_max_pic_order_cnt_lsb;
388 if( (poc_lsb < prev_poc_lsb)
389 && ((prev_poc_lsb - poc_lsb) >= (max_poc_lsb / 2)) )
390 poc_msb = prev_poc_msb + max_poc_lsb;
391 else if( (poc_lsb > prev_poc_lsb)
392 && ((poc_lsb - prev_poc_lsb) > (max_poc_lsb / 2)) )
393 poc_msb = prev_poc_msb - max_poc_lsb;
394 else
395 poc_msb = prev_poc_msb;
397 picture->poc = poc_msb + poc_lsb;
398 if( picture->TemporalId == 0 && (!picture->radl || !picture->rasl || !picture->sublayer_nonref) )
400 picture->tid0_poc_msb = poc_msb;
401 picture->tid0_poc_lsb = poc_lsb;
403 #if HEVC_POC_DEBUG_PRINT
404 fprintf( stderr, " prevPicOrderCntMsb: %"PRId32"\n", prev_poc_msb );
405 fprintf( stderr, " prevPicOrderCntLsb: %"PRId32"\n", prev_poc_lsb );
406 fprintf( stderr, " PicOrderCntMsb: %"PRId64"\n", poc_msb );
407 fprintf( stderr, " pic_order_cnt_lsb: %"PRId32"\n", poc_lsb );
408 fprintf( stderr, " MaxPicOrderCntLsb: %"PRIu64"\n", max_poc_lsb );
409 fprintf( stderr, " POC: %"PRId32"\n", picture->poc );
410 #endif
411 return 0;
414 static inline int hevc_activate_vps
416 hevc_info_t *info,
417 uint8_t video_parameter_set_id
420 hevc_vps_t *vps = hevc_get_vps( info->vps_list, video_parameter_set_id );
421 if( !vps )
422 return -1;
423 info->vps = *vps;
424 return 0;
427 static inline int hevc_activate_sps
429 hevc_info_t *info,
430 uint8_t seq_parameter_set_id
433 hevc_sps_t *sps = hevc_get_sps( info->sps_list, seq_parameter_set_id );
434 if( !sps )
435 return -1;
436 info->sps = *sps;
437 return 0;
440 static void hevc_parse_scaling_list_data
442 lsmash_bits_t *bits
445 for( int sizeId = 0; sizeId < 4; sizeId++ )
446 for( int matrixId = 0; matrixId < (sizeId == 3 ? 2 : 6); matrixId++ )
448 if( !lsmash_bits_get( bits, 1 ) ) /* scaling_list_pred_mode_flag[sizeId][matrixId] */
449 nalu_get_exp_golomb_ue( bits ); /* scaling_list_pred_matrix_id_delta[sizeId][matrixId] */
450 else
452 int coefNum = LSMASH_MIN( 64, 1 << (4 + (sizeId << 1)) );
453 if( sizeId > 1 )
454 nalu_get_exp_golomb_se( bits ); /* scaling_list_dc_coef_minus8[sizeId - 2][matrixId] */
455 for( int i = 0; i < coefNum; i++ )
456 nalu_get_exp_golomb_se( bits ); /* scaling_list_delta_coef */
461 static int hevc_short_term_ref_pic_set
463 lsmash_bits_t *bits,
464 hevc_sps_t *sps,
465 int stRpsIdx
468 int inter_ref_pic_set_prediction_flag = stRpsIdx != 0 ? lsmash_bits_get( bits, 1 ) : 0;
469 if( inter_ref_pic_set_prediction_flag )
471 /* delta_idx_minus1 is always 0 in SPS since stRpsIdx must not be equal to num_short_term_ref_pic_sets. */
472 uint64_t delta_idx_minus1 = stRpsIdx == sps->num_short_term_ref_pic_sets ? nalu_get_exp_golomb_ue( bits ) : 0;
473 int delta_rps_sign = lsmash_bits_get( bits, 1 );
474 uint64_t abs_delta_rps_minus1 = nalu_get_exp_golomb_ue( bits );
475 int RefRpsIdx = stRpsIdx - (delta_idx_minus1 + 1);
476 int deltaRps = (delta_rps_sign ? -1 : 1) * (abs_delta_rps_minus1 + 1);
477 hevc_st_rps_t *st_rps = &sps->st_rps[stRpsIdx];
478 hevc_st_rps_t *ref_rps = &sps->st_rps[RefRpsIdx];
479 uint8_t used_by_curr_pic_flag[32];
480 uint8_t use_delta_flag [32];
481 for( int j = 0; j <= ref_rps->NumDeltaPocs; j++ )
483 used_by_curr_pic_flag[j] = lsmash_bits_get( bits, 1 );
484 use_delta_flag [j] = !used_by_curr_pic_flag[j] ? lsmash_bits_get( bits, 1 ) : 1;
486 /* NumNegativePics */
487 int i = 0;
488 for( int j = ref_rps->NumPositivePics - 1; j >= 0; j-- )
490 int dPoc = ref_rps->DeltaPocS1[j] + deltaRps;
491 if( dPoc < 0 && use_delta_flag[ ref_rps->NumNegativePics + j ] )
493 st_rps->DeltaPocS0 [i ] = dPoc;
494 st_rps->UsedByCurrPicS0[i++] = used_by_curr_pic_flag[ ref_rps->NumNegativePics + j ];
497 if( deltaRps < 0 && use_delta_flag[ ref_rps->NumDeltaPocs ] )
499 st_rps->DeltaPocS0 [i ] = deltaRps;
500 st_rps->UsedByCurrPicS0[i++] = used_by_curr_pic_flag[ ref_rps->NumDeltaPocs ];
502 for( int j = 0; j < ref_rps->NumNegativePics; j++ )
504 int dPoc = ref_rps->DeltaPocS0[j] + deltaRps;
505 if( dPoc < 0 && use_delta_flag[j] )
507 st_rps->DeltaPocS0 [i ] = dPoc;
508 st_rps->UsedByCurrPicS0[i++] = used_by_curr_pic_flag[j];
511 st_rps->NumNegativePics = i;
512 /* NumPositivePics */
513 i = 0;
514 for( int j = ref_rps->NumNegativePics - 1; j >= 0; j-- )
516 int dPoc = ref_rps->DeltaPocS0[j] + deltaRps;
517 if( dPoc > 0 && use_delta_flag[j] )
519 st_rps->DeltaPocS1 [i ] = dPoc;
520 st_rps->UsedByCurrPicS1[i++] = used_by_curr_pic_flag[j];
523 if( deltaRps > 0 && use_delta_flag[ ref_rps->NumDeltaPocs ] )
525 st_rps->DeltaPocS1 [i ] = deltaRps;
526 st_rps->UsedByCurrPicS1[i++] = used_by_curr_pic_flag[ ref_rps->NumDeltaPocs ];
528 for( int j = 0; j < ref_rps->NumPositivePics; j++ )
530 int dPoc = ref_rps->DeltaPocS1[j] + deltaRps;
531 if( dPoc > 0 && use_delta_flag[ ref_rps->NumNegativePics + j ] )
533 st_rps->DeltaPocS1 [i ] = dPoc;
534 st_rps->UsedByCurrPicS1[i++] = used_by_curr_pic_flag[ ref_rps->NumNegativePics + j ];
537 st_rps->NumPositivePics = i;
538 /* NumDeltaPocs */
539 st_rps->NumDeltaPocs = st_rps->NumNegativePics + st_rps->NumPositivePics;
541 else
543 uint64_t num_negative_pics = nalu_get_exp_golomb_ue( bits );
544 uint64_t num_positive_pics = nalu_get_exp_golomb_ue( bits );
545 IF_INVALID_VALUE( num_negative_pics >= HEVC_MAX_DPB_SIZE || num_positive_pics >= HEVC_MAX_DPB_SIZE )
546 return -1;
547 hevc_st_rps_t *st_rps = &sps->st_rps[stRpsIdx];
548 st_rps->NumNegativePics = num_negative_pics;
549 st_rps->NumPositivePics = num_positive_pics;
550 st_rps->NumDeltaPocs = st_rps->NumNegativePics + st_rps->NumPositivePics;
551 for( int i = 0; i < num_negative_pics; i++ )
553 uint64_t delta_poc_s0_minus1 = nalu_get_exp_golomb_ue( bits );
554 if( i == 0 )
555 st_rps->DeltaPocS0[i] = -(delta_poc_s0_minus1 + 1);
556 else
557 st_rps->DeltaPocS0[i] = st_rps->DeltaPocS0[i - 1] - (delta_poc_s0_minus1 + 1);
558 st_rps->UsedByCurrPicS0[i] = lsmash_bits_get( bits, 1 ); /* used_by_curr_pic_s0_flag */
560 for( int i = 0; i < num_positive_pics; i++ )
562 uint64_t delta_poc_s1_minus1 = nalu_get_exp_golomb_ue( bits );
563 if( i == 0 )
564 st_rps->DeltaPocS1[i] = +(delta_poc_s1_minus1 + 1);
565 else
566 st_rps->DeltaPocS1[i] = st_rps->DeltaPocS1[i - 1] + (delta_poc_s1_minus1 + 1);
567 st_rps->UsedByCurrPicS0[i] = lsmash_bits_get( bits, 1 ); /* used_by_curr_pic_s1_flag */
570 return 0;
573 static inline void hevc_parse_sub_layer_hrd_parameters
575 lsmash_bits_t *bits,
576 int CpbCnt,
577 int sub_pic_hrd_params_present_flag
580 for( int i = 0; i <= CpbCnt; i++ )
582 nalu_get_exp_golomb_ue( bits ); /* bit_rate_value_minus1[i] */
583 nalu_get_exp_golomb_ue( bits ); /* cpb_size_value_minus1[i] */
584 if( sub_pic_hrd_params_present_flag )
586 nalu_get_exp_golomb_ue( bits ); /* cpb_size_du_value_minus1[i] */
587 nalu_get_exp_golomb_ue( bits ); /* bit_rate_du_value_minus1[i] */
589 lsmash_bits_get( bits, 1 ); /* cbr_flag[i] */
593 static void hevc_parse_hrd_parameters
595 lsmash_bits_t *bits,
596 hevc_hrd_t *hrd,
597 int commonInfPresentFlag,
598 int maxNumSubLayersMinus1
601 /* The specification we refer to doesn't define the implicit value of some fields.
602 * According to JCTVC-HM reference software,
603 * the implicit value of nal_hrd_parameters_present_flag is to be equal to 0,
604 * the implicit value of vcl_hrd_parameters_present_flag is to be equal to 0. */
605 int nal_hrd_parameters_present_flag = 0;
606 int vcl_hrd_parameters_present_flag = 0;
607 memset( hrd, 0, sizeof(hevc_hrd_t) );
608 if( commonInfPresentFlag )
610 nal_hrd_parameters_present_flag = lsmash_bits_get( bits, 1 );
611 vcl_hrd_parameters_present_flag = lsmash_bits_get( bits, 1 );
612 if( nal_hrd_parameters_present_flag
613 || vcl_hrd_parameters_present_flag )
615 hrd->CpbDpbDelaysPresentFlag = 1;
616 hrd->sub_pic_hrd_params_present_flag = lsmash_bits_get( bits, 1 );
617 if( hrd->sub_pic_hrd_params_present_flag )
619 lsmash_bits_get( bits, 8 ); /* tick_divisor_minus2 */
620 hrd->du_cpb_removal_delay_increment_length = lsmash_bits_get( bits, 5 ) + 1;
621 hrd->sub_pic_cpb_params_in_pic_timing_sei_flag = lsmash_bits_get( bits, 1 );
622 hrd->dpb_output_delay_du_length = lsmash_bits_get( bits, 5 ) + 1;
624 lsmash_bits_get( bits, 4 ); /* bit_rate_scale */
625 lsmash_bits_get( bits, 4 ); /* cpb_size_scale */
626 if( hrd->sub_pic_hrd_params_present_flag )
627 lsmash_bits_get( bits, 4 ); /* cpb_size_du_scale */
628 lsmash_bits_get( bits, 5 ); /* initial_cpb_removal_delay_length_minus1 */
629 hrd->au_cpb_removal_delay_length = lsmash_bits_get( bits, 5 ) + 1;
630 hrd->dpb_output_delay_length = lsmash_bits_get( bits, 5 ) + 1;
633 for( int i = 0; i <= maxNumSubLayersMinus1; i++ )
635 hrd->fixed_pic_rate_general_flag[i] = lsmash_bits_get( bits, 1 );
636 uint8_t fixed_pic_rate_within_cvs_flag = !hrd->fixed_pic_rate_general_flag[i] ? lsmash_bits_get( bits, 1 ) : 1;
637 uint8_t low_delay_hrd_flag = !fixed_pic_rate_within_cvs_flag ? lsmash_bits_get( bits, 1 ) : 0;
638 hrd->elemental_duration_in_tc[i] = fixed_pic_rate_within_cvs_flag ? nalu_get_exp_golomb_ue( bits ) + 1 : 0;
639 uint8_t cpb_cnt_minus1 = !low_delay_hrd_flag ? nalu_get_exp_golomb_ue( bits ) : 0;
640 if( nal_hrd_parameters_present_flag )
641 hevc_parse_sub_layer_hrd_parameters( bits, cpb_cnt_minus1, hrd->sub_pic_hrd_params_present_flag );
642 if( vcl_hrd_parameters_present_flag )
643 hevc_parse_sub_layer_hrd_parameters( bits, cpb_cnt_minus1, hrd->sub_pic_hrd_params_present_flag );
647 static inline void hevc_parse_profile_tier_level_common
649 lsmash_bits_t *bits,
650 hevc_ptl_common_t *ptlc,
651 int profile_present,
652 int level_present
655 if( profile_present )
657 ptlc->profile_space = lsmash_bits_get( bits, 2 );
658 ptlc->tier_flag = lsmash_bits_get( bits, 1 );
659 ptlc->profile_idc = lsmash_bits_get( bits, 5 );
660 ptlc->profile_compatibility_flags = lsmash_bits_get( bits, 32 );
661 ptlc->progressive_source_flag = lsmash_bits_get( bits, 1 );
662 ptlc->interlaced_source_flag = lsmash_bits_get( bits, 1 );
663 ptlc->non_packed_constraint_flag = lsmash_bits_get( bits, 1 );
664 ptlc->frame_only_constraint_flag = lsmash_bits_get( bits, 1 );
665 ptlc->reserved_zero_44bits = lsmash_bits_get( bits, 44 );
667 if( level_present )
668 ptlc->level_idc = lsmash_bits_get( bits, 8 );
671 static void hevc_parse_profile_tier_level
673 lsmash_bits_t *bits,
674 hevc_ptl_t *ptl,
675 int maxNumSubLayersMinus1
678 hevc_parse_profile_tier_level_common( bits, &ptl->general, 1, 1 );
679 if( maxNumSubLayersMinus1 == 0 )
680 return;
681 assert( maxNumSubLayersMinus1 <= 6 );
682 int sub_layer_profile_present_flag[6] = { 0 };
683 int sub_layer_level_present_flag [6] = { 0 };
684 for( int i = 0; i < maxNumSubLayersMinus1; i++ )
686 sub_layer_profile_present_flag[i] = lsmash_bits_get( bits, 1 );
687 sub_layer_level_present_flag [i] = lsmash_bits_get( bits, 1 );
689 for( int i = maxNumSubLayersMinus1; i < 8; i++ )
690 lsmash_bits_get( bits, 2 ); /* reserved_zero_2bits[i] */
691 for( int i = 0; i < maxNumSubLayersMinus1; i++ )
692 hevc_parse_profile_tier_level_common( bits, &ptl->sub_layer[i], sub_layer_profile_present_flag[i], sub_layer_level_present_flag[i] );
695 static int hevc_parse_vps_minimally
697 lsmash_bits_t *bits,
698 hevc_vps_t *vps,
699 uint8_t *rbsp_buffer,
700 uint8_t *ebsp,
701 uint64_t ebsp_size
704 if( nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, ebsp_size ) )
705 return -1;
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 -1;
711 /* vps_max_layers_minus1 shall be 0 in the specification we refer to. */
712 if( lsmash_bits_get( bits, 6 ) != 0 )
713 return -1;
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 -1;
719 /* vps_reserved_0xffff_16bits shall be 0xFFFF in the specification we refer to. */
720 if( lsmash_bits_get( bits, 16 ) != 0xFFFF )
721 return -1;
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 ? -1 : 0;
740 int hevc_parse_vps
742 hevc_info_t *info,
743 uint8_t *rbsp_buffer,
744 uint8_t *ebsp,
745 uint64_t ebsp_size
748 lsmash_bits_t *bits = info->bits;
749 hevc_vps_t *vps;
751 /* Parse VPS minimally for configuration records. */
752 hevc_vps_t min_vps;
753 if( hevc_parse_vps_minimally( bits, &min_vps, rbsp_buffer, ebsp, ebsp_size ) )
754 return -1;
755 vps = hevc_get_vps( info->vps_list, min_vps.video_parameter_set_id );
756 if( !vps )
757 return -1;
758 *vps = min_vps;
760 vps->timing_info_present_flag = lsmash_bits_get( bits, 1 );
761 if( vps->timing_info_present_flag )
763 lsmash_bits_get( bits, 32 ); /* num_units_in_tick */
764 lsmash_bits_get( bits, 32 ); /* time_scale */
765 if( lsmash_bits_get( bits, 1 ) ) /* poc_proportional_to_timing_flag */
766 nalu_get_exp_golomb_ue( bits ); /* num_ticks_poc_diff_one_minus1 */
767 vps->num_hrd_parameters = nalu_get_exp_golomb_ue( bits );
768 for( int i = 0; i < vps->num_hrd_parameters; i++ )
770 nalu_get_exp_golomb_ue( bits ); /* hrd_layer_set_idx[i] */
771 int cprms_present_flag = i > 0 ? lsmash_bits_get( bits, 1 ) : 1;
772 /* Although the value of vps_num_hrd_parameters is required to be less than or equal to 1 in the spec
773 * we refer to, decoders shall allow other values of vps_num_hrd_parameters in the range of 0 to 1024,
774 * inclusive, to appear in the syntax. */
775 if( i <= 1 )
776 hevc_parse_hrd_parameters( bits, &vps->hrd[i], cprms_present_flag, vps->max_sub_layers_minus1 );
777 else
779 hevc_hrd_t dummy_hrd;
780 hevc_parse_hrd_parameters( bits, &dummy_hrd, cprms_present_flag, vps->max_sub_layers_minus1 );
784 /* Skip VPS extension. */
785 lsmash_bits_empty( bits );
786 if( bits->bs->error )
787 return -1;
788 vps->present = 1;
789 info->vps = *vps;
790 return 0;
793 static int hevc_parse_sps_minimally
795 lsmash_bits_t *bits,
796 hevc_sps_t *sps,
797 uint8_t *rbsp_buffer,
798 uint8_t *ebsp,
799 uint64_t ebsp_size
802 if( nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, ebsp_size ) )
803 return -1;
804 memset( sps, 0, sizeof(hevc_sps_t) );
805 sps->video_parameter_set_id = lsmash_bits_get( bits, 4 );
806 sps->max_sub_layers_minus1 = lsmash_bits_get( bits, 3 );
807 sps->temporal_id_nesting_flag = lsmash_bits_get( bits, 1 );
808 hevc_parse_profile_tier_level( bits, &sps->ptl, sps->max_sub_layers_minus1 );
809 sps->seq_parameter_set_id = nalu_get_exp_golomb_ue( bits );
810 sps->chroma_format_idc = nalu_get_exp_golomb_ue( bits );
811 if( sps->chroma_format_idc == 3 )
812 sps->separate_colour_plane_flag = lsmash_bits_get( bits, 1 );
813 static const int SubWidthC [] = { 1, 2, 2, 1 };
814 static const int SubHeightC[] = { 1, 2, 1, 1 };
815 uint64_t pic_width_in_luma_samples = nalu_get_exp_golomb_ue( bits );
816 uint64_t pic_height_in_luma_samples = nalu_get_exp_golomb_ue( bits );
817 sps->cropped_width = pic_width_in_luma_samples;
818 sps->cropped_height = pic_height_in_luma_samples;
819 if( lsmash_bits_get( bits, 1 ) ) /* conformance_window_flag */
821 uint64_t conf_win_left_offset = nalu_get_exp_golomb_ue( bits );
822 uint64_t conf_win_right_offset = nalu_get_exp_golomb_ue( bits );
823 uint64_t conf_win_top_offset = nalu_get_exp_golomb_ue( bits );
824 uint64_t conf_win_bottom_offset = nalu_get_exp_golomb_ue( bits );
825 sps->cropped_width -= (conf_win_left_offset + conf_win_right_offset) * SubWidthC [ sps->chroma_format_idc ];
826 sps->cropped_height -= (conf_win_top_offset + conf_win_bottom_offset) * SubHeightC[ sps->chroma_format_idc ];
828 sps->bit_depth_luma_minus8 = nalu_get_exp_golomb_ue( bits );
829 sps->bit_depth_chroma_minus8 = nalu_get_exp_golomb_ue( bits );
830 sps->log2_max_pic_order_cnt_lsb = nalu_get_exp_golomb_ue( bits ) + 4;
831 int sub_layer_ordering_info_present_flag = lsmash_bits_get( bits, 1 );
832 for( int i = sub_layer_ordering_info_present_flag ? 0 : sps->max_sub_layers_minus1; i <= sps->max_sub_layers_minus1; i++ )
834 nalu_get_exp_golomb_ue( bits ); /* max_dec_pic_buffering_minus1[i] */
835 nalu_get_exp_golomb_ue( bits ); /* max_num_reorder_pics [i] */
836 nalu_get_exp_golomb_ue( bits ); /* max_latency_increase_plus1 [i] */
838 uint64_t log2_min_luma_coding_block_size_minus3 = nalu_get_exp_golomb_ue( bits );
839 uint64_t log2_diff_max_min_luma_coding_block_size = nalu_get_exp_golomb_ue( bits );
840 nalu_get_exp_golomb_ue( bits ); /* log2_min_transform_block_size_minus2 */
841 nalu_get_exp_golomb_ue( bits ); /* log2_diff_max_min_transform_block_size */
842 nalu_get_exp_golomb_ue( bits ); /* max_transform_hierarchy_depth_inter */
843 nalu_get_exp_golomb_ue( bits ); /* max_transform_hierarchy_depth_intra */
845 int MinCbLog2SizeY = log2_min_luma_coding_block_size_minus3 + 3;
846 int MinCbSizeY = 1 << MinCbLog2SizeY;
847 if( pic_width_in_luma_samples == 0 || pic_width_in_luma_samples % MinCbSizeY
848 || pic_height_in_luma_samples == 0 || pic_height_in_luma_samples % MinCbSizeY )
849 return -1; /* Both shall be an integer multiple of MinCbSizeY. */
850 int CtbLog2SizeY = MinCbLog2SizeY + log2_diff_max_min_luma_coding_block_size;
851 int CtbSizeY = 1 << CtbLog2SizeY;
852 sps->PicWidthInCtbsY = (pic_width_in_luma_samples - 1) / CtbSizeY + 1;
853 sps->PicHeightInCtbsY = (pic_height_in_luma_samples - 1) / CtbSizeY + 1;
854 sps->PicSizeInCtbsY = sps->PicWidthInCtbsY * sps->PicHeightInCtbsY;
856 if( lsmash_bits_get( bits, 1 ) /* scaling_list_enabled_flag */
857 && lsmash_bits_get( bits, 1 ) ) /* sps_scaling_list_data_present_flag */
858 hevc_parse_scaling_list_data( bits );
859 lsmash_bits_get( bits, 1 ); /* amp_enabled_flag */
860 lsmash_bits_get( bits, 1 ); /* sample_adaptive_offset_enabled_flag */
861 if( lsmash_bits_get( bits, 1 ) ) /* pcm_enabled_flag */
863 lsmash_bits_get( bits, 4 ); /* pcm_sample_bit_depth_luma_minus1 */
864 lsmash_bits_get( bits, 4 ); /* pcm_sample_bit_depth_chroma_minus1 */
865 nalu_get_exp_golomb_ue( bits ); /* log2_min_pcm_luma_coding_block_size_minus3 */
866 nalu_get_exp_golomb_ue( bits ); /* log2_diff_max_min_pcm_luma_coding_block_size */
867 lsmash_bits_get( bits, 1 ); /* pcm_loop_filter_disabled_flag */
869 sps->num_short_term_ref_pic_sets = nalu_get_exp_golomb_ue( bits );
870 for( int i = 0; i < sps->num_short_term_ref_pic_sets; i++ )
871 if( hevc_short_term_ref_pic_set( bits, sps, i ) < 0 )
872 return -1;
873 sps->long_term_ref_pics_present_flag = lsmash_bits_get( bits, 1 );
874 if( sps->long_term_ref_pics_present_flag )
876 sps->num_long_term_ref_pics_sps = nalu_get_exp_golomb_ue( bits );
877 for( int i = 0; i < sps->num_long_term_ref_pics_sps; i++ )
879 lsmash_bits_get( bits, sps->log2_max_pic_order_cnt_lsb ); /* lt_ref_pic_poc_lsb_sps [i] */
880 lsmash_bits_get( bits, 1 ); /* used_by_curr_pic_lt_sps_flag[i] */
883 sps->temporal_mvp_enabled_flag = lsmash_bits_get( bits, 1 );
884 lsmash_bits_get( bits, 1 ); /* strong_intra_smoothing_enabled_flag */
885 sps->vui.present = lsmash_bits_get( bits, 1 ); /* vui_parameters_present_flag */
886 if( sps->vui.present )
888 /* vui_parameters() */
889 if( lsmash_bits_get( bits, 1 ) ) /* aspect_ratio_info_present_flag */
891 uint8_t aspect_ratio_idc = lsmash_bits_get( bits, 8 );
892 if( aspect_ratio_idc == 255 )
894 /* EXTENDED_SAR */
895 sps->vui.sar_width = lsmash_bits_get( bits, 16 );
896 sps->vui.sar_height = lsmash_bits_get( bits, 16 );
898 else
900 static const struct
902 uint16_t sar_width;
903 uint16_t sar_height;
904 } pre_defined_sar[] =
906 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
907 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 }, { 80, 33 },
908 { 18, 11 }, { 15, 11 }, { 64, 33 }, { 160, 99 }, { 4, 3 },
909 { 3, 2 }, { 2, 1 }
911 if( aspect_ratio_idc < (sizeof(pre_defined_sar) / sizeof(pre_defined_sar[0])) )
913 sps->vui.sar_width = pre_defined_sar[ aspect_ratio_idc ].sar_width;
914 sps->vui.sar_height = pre_defined_sar[ aspect_ratio_idc ].sar_height;
916 else
918 /* Behavior when unknown aspect_ratio_idc is detected is not specified in the specification. */
919 sps->vui.sar_width = 0;
920 sps->vui.sar_height = 0;
924 else
926 sps->vui.sar_width = 0;
927 sps->vui.sar_height = 0;
929 if( lsmash_bits_get( bits, 1 ) ) /* overscan_info_present_flag */
930 lsmash_bits_get( bits, 1 ); /* overscan_appropriate_flag */
931 if( lsmash_bits_get( bits, 1 ) ) /* video_signal_type_present_flag */
933 lsmash_bits_get( bits, 3 ); /* video_format */
934 sps->vui.video_full_range_flag = lsmash_bits_get( bits, 1 );
935 sps->vui.colour_description_present_flag = lsmash_bits_get( bits, 1 );
936 if( sps->vui.colour_description_present_flag )
938 sps->vui.colour_primaries = lsmash_bits_get( bits, 8 );
939 sps->vui.transfer_characteristics = lsmash_bits_get( bits, 8 );
940 sps->vui.matrix_coeffs = lsmash_bits_get( bits, 8 );
942 else
944 sps->vui.colour_primaries = 2;
945 sps->vui.transfer_characteristics = 2;
946 sps->vui.matrix_coeffs = 2;
949 if( lsmash_bits_get( bits, 1 ) ) /* chroma_loc_info_present_flag */
951 nalu_get_exp_golomb_ue( bits ); /* chroma_sample_loc_type_top_field */
952 nalu_get_exp_golomb_ue( bits ); /* chroma_sample_loc_type_bottom_field */
954 lsmash_bits_get( bits, 1 ); /* neutral_chroma_indication_flag */
955 sps->vui.field_seq_flag = lsmash_bits_get( bits, 1 );
956 sps->vui.frame_field_info_present_flag = lsmash_bits_get( bits, 1 );
957 if( sps->vui.field_seq_flag )
958 /* cropped_height indicates in a frame. */
959 sps->cropped_height *= 2;
960 if( lsmash_bits_get( bits, 1 ) ) /* default_display_window_flag */
962 /* default display window
963 * A rectangular region for display specified by these values is not considered
964 * as cropped visual presentation size which decoder delivers.
965 * Maybe, these values shall be indicated by the clean aperture on container level. */
966 sps->vui.def_disp_win_offset.left = (lsmash_rational_u32_t){ nalu_get_exp_golomb_ue( bits ) * SubWidthC [ sps->chroma_format_idc ], 1 };
967 sps->vui.def_disp_win_offset.right = (lsmash_rational_u32_t){ nalu_get_exp_golomb_ue( bits ) * SubWidthC [ sps->chroma_format_idc ], 1 };
968 sps->vui.def_disp_win_offset.top = (lsmash_rational_u32_t){ nalu_get_exp_golomb_ue( bits ) * SubHeightC[ sps->chroma_format_idc ], 1 };
969 sps->vui.def_disp_win_offset.bottom = (lsmash_rational_u32_t){ nalu_get_exp_golomb_ue( bits ) * SubHeightC[ sps->chroma_format_idc ], 1 };
971 if( lsmash_bits_get( bits, 1 ) ) /* vui_timing_info_present_flag */
973 sps->vui.num_units_in_tick = lsmash_bits_get( bits, 32 );
974 sps->vui.time_scale = lsmash_bits_get( bits, 32 );
975 if( lsmash_bits_get( bits, 1 ) ) /* vui_poc_proportional_to_timing_flag */
976 nalu_get_exp_golomb_ue( bits ); /* vui_num_ticks_poc_diff_one_minus1 */
977 if( lsmash_bits_get( bits, 1 ) ) /* vui_hrd_parameters_present_flag */
978 hevc_parse_hrd_parameters( bits, &sps->vui.hrd, 1, sps->max_sub_layers_minus1 );
980 else
982 sps->vui.num_units_in_tick = 1; /* arbitrary */
983 sps->vui.time_scale = 25; /* arbitrary */
985 if( lsmash_bits_get( bits, 1 ) ) /* bitstream_restriction_flag */
987 lsmash_bits_get( bits, 1 ); /* tiles_fixed_structure_flag */
988 lsmash_bits_get( bits, 1 ); /* motion_vectors_over_pic_boundaries_flag */
989 lsmash_bits_get( bits, 1 ); /* restricted_ref_pic_lists_flag */
990 sps->vui.min_spatial_segmentation_idc = nalu_get_exp_golomb_ue( bits );
991 nalu_get_exp_golomb_ue( bits ); /* max_bytes_per_pic_denom */
992 nalu_get_exp_golomb_ue( bits ); /* max_bits_per_min_cu_denom */
993 nalu_get_exp_golomb_ue( bits ); /* log2_max_mv_length_horizontal */
994 nalu_get_exp_golomb_ue( bits ); /* log2_max_mv_length_vertical */
996 else
997 sps->vui.min_spatial_segmentation_idc = 0;
999 else
1001 sps->vui.sar_width = 0;
1002 sps->vui.sar_height = 0;
1003 sps->vui.colour_primaries = 2;
1004 sps->vui.transfer_characteristics = 2;
1005 sps->vui.matrix_coeffs = 2;
1006 sps->vui.field_seq_flag = 0;
1007 sps->vui.frame_field_info_present_flag = sps->ptl.general.progressive_source_flag
1008 && sps->ptl.general.interlaced_source_flag;
1009 sps->vui.num_units_in_tick = 1; /* arbitrary */
1010 sps->vui.time_scale = 25; /* arbitrary */
1011 sps->vui.min_spatial_segmentation_idc = 0;
1013 return bits->bs->error ? -1 : 0;
1016 int hevc_parse_sps
1018 hevc_info_t *info,
1019 uint8_t *rbsp_buffer,
1020 uint8_t *ebsp,
1021 uint64_t ebsp_size
1024 lsmash_bits_t *bits = info->bits;
1025 hevc_sps_t *sps;
1027 /* Parse SPS minimally for configuration records. */
1028 hevc_sps_t min_sps;
1029 if( hevc_parse_sps_minimally( bits, &min_sps, rbsp_buffer, ebsp, ebsp_size ) )
1030 return -1;
1031 sps = hevc_get_sps( info->sps_list, min_sps.seq_parameter_set_id );
1032 if( !sps )
1033 return -1;
1034 *sps = min_sps;
1036 /* Skip SPS extension. */
1037 lsmash_bits_empty( bits );
1038 if( bits->bs->error )
1039 return -1;
1040 sps->present = 1;
1041 info->sps = *sps;
1042 hevc_activate_vps( info, info->sps.video_parameter_set_id );
1043 return 0;
1046 static int hevc_allocate_tile_sizes
1048 hevc_pps_t *pps,
1049 uint32_t num_tile_columns,
1050 uint32_t num_tile_rows
1053 /* Allocate columns and rows of tiles. */
1054 size_t col_alloc_size = 2 * num_tile_columns * sizeof(uint32_t);
1055 if( col_alloc_size > pps->col_alloc_size )
1057 void *temp = lsmash_realloc( pps->colWidth, col_alloc_size );
1058 if( !temp )
1059 return -1;
1060 pps->col_alloc_size = col_alloc_size;
1061 pps->colWidth = temp;
1063 size_t row_alloc_size = 2 * num_tile_rows * sizeof(uint32_t);
1064 if( row_alloc_size > pps->row_alloc_size )
1066 void *temp = lsmash_realloc( pps->rowHeight, row_alloc_size );
1067 if( !temp )
1068 return -1;
1069 pps->row_alloc_size = row_alloc_size;
1070 pps->rowHeight = temp;
1072 pps->colBd = pps->colWidth + num_tile_columns;
1073 pps->rowBd = pps->rowHeight + num_tile_rows;
1074 return 0;
1077 static int hevc_parse_pps_minimally
1079 lsmash_bits_t *bits,
1080 hevc_pps_t *pps,
1081 uint8_t *rbsp_buffer,
1082 uint8_t *ebsp,
1083 uint64_t ebsp_size
1086 if( nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, ebsp_size ) )
1087 return -1;
1088 memset( pps, 0, SIZEOF_PPS_EXCLUDING_HEAP );
1089 pps->pic_parameter_set_id = nalu_get_exp_golomb_ue( bits );
1090 pps->seq_parameter_set_id = nalu_get_exp_golomb_ue( bits );
1091 pps->dependent_slice_segments_enabled_flag = lsmash_bits_get( bits, 1 );
1092 pps->output_flag_present_flag = lsmash_bits_get( bits, 1 );
1093 pps->num_extra_slice_header_bits = lsmash_bits_get( bits, 3 );
1094 lsmash_bits_get( bits, 1 ); /* sign_data_hiding_enabled_flag */
1095 lsmash_bits_get( bits, 1 ); /* cabac_init_present_flag */
1096 nalu_get_exp_golomb_ue( bits ); /* num_ref_idx_l0_default_active_minus1 */
1097 nalu_get_exp_golomb_ue( bits ); /* num_ref_idx_l1_default_active_minus1 */
1098 nalu_get_exp_golomb_se( bits ); /* init_qp_minus26 */
1099 lsmash_bits_get( bits, 1 ); /* constrained_intra_pred_flag */
1100 lsmash_bits_get( bits, 1 ); /* transform_skip_enabled_flag */
1101 if( lsmash_bits_get( bits, 1 ) ) /* cu_qp_delta_enabled_flag */
1102 nalu_get_exp_golomb_ue( bits ); /* diff_cu_qp_delta_depth */
1103 nalu_get_exp_golomb_se( bits ); /* cb_qp_offset */
1104 nalu_get_exp_golomb_se( bits ); /* cr_qp_offset */
1105 lsmash_bits_get( bits, 1 ); /* slice_chroma_qp_offsets_present_flag */
1106 lsmash_bits_get( bits, 1 ); /* weighted_pred_flag */
1107 lsmash_bits_get( bits, 1 ); /* weighted_bipred_flag */
1108 lsmash_bits_get( bits, 1 ) /* transquant_bypass_enabled_flag */;
1109 pps->tiles_enabled_flag = lsmash_bits_get( bits, 1 );
1110 pps->entropy_coding_sync_enabled_flag = lsmash_bits_get( bits, 1 );
1111 return bits->bs->error ? -1 : 0;
1114 int hevc_parse_pps
1116 hevc_info_t *info,
1117 uint8_t *rbsp_buffer,
1118 uint8_t *ebsp,
1119 uint64_t ebsp_size
1122 lsmash_bits_t *bits = info->bits;
1123 hevc_pps_t *pps;
1125 /* Parse PPS minimally for configuration records. */
1126 hevc_pps_t min_pps;
1127 if( hevc_parse_pps_minimally( bits, &min_pps, rbsp_buffer, ebsp, ebsp_size ) )
1128 return -1;
1129 pps = hevc_get_pps( info->pps_list, min_pps.pic_parameter_set_id );
1130 if( !pps )
1131 return -1;
1132 memcpy( pps, &min_pps, SIZEOF_PPS_EXCLUDING_HEAP );
1134 hevc_sps_t temp_sps = info->sps;
1135 if( hevc_activate_sps( info, pps->seq_parameter_set_id ) < 0 )
1136 return -1;
1137 hevc_sps_t *sps = &info->sps;
1138 if( pps->tiles_enabled_flag )
1140 pps->num_tile_columns_minus1 = nalu_get_exp_golomb_ue( bits );
1141 pps->num_tile_rows_minus1 = nalu_get_exp_golomb_ue( bits );
1142 IF_INVALID_VALUE( pps->num_tile_columns_minus1 >= sps->PicWidthInCtbsY
1143 || pps->num_tile_rows_minus1 >= sps->PicHeightInCtbsY )
1144 goto fail;
1145 if( hevc_allocate_tile_sizes( pps, pps->num_tile_columns_minus1 + 1, pps->num_tile_rows_minus1 + 1 ) < 0 )
1146 goto fail;
1147 if( lsmash_bits_get( bits, 1 ) ) /* uniform_spacing_flag */
1149 for( int i = 0; i <= pps->num_tile_columns_minus1; i++ )
1150 pps->colWidth[i] = ((i + 1) * sps->PicWidthInCtbsY) / (pps->num_tile_columns_minus1 + 1)
1151 - ( i * sps->PicWidthInCtbsY) / (pps->num_tile_columns_minus1 + 1);
1152 for( int j = 0; j <= pps->num_tile_rows_minus1; j++ )
1153 pps->rowHeight[j] = ((j + 1) * sps->PicHeightInCtbsY) / (pps->num_tile_rows_minus1 + 1)
1154 - ( j * sps->PicHeightInCtbsY) / (pps->num_tile_rows_minus1 + 1);
1156 else
1158 pps->colWidth[ pps->num_tile_columns_minus1 ] = sps->PicWidthInCtbsY;
1159 for( uint64_t i = 0; i < pps->num_tile_columns_minus1; i++ )
1161 pps->colWidth[i] = nalu_get_exp_golomb_ue( bits ) + 1; /* column_width_minus1[i] */
1162 pps->colWidth[ pps->num_tile_columns_minus1 ] -= pps->colWidth[i];
1164 pps->rowHeight[ pps->num_tile_rows_minus1 ] = sps->PicHeightInCtbsY;
1165 for( uint64_t j = 0; j < pps->num_tile_rows_minus1; j++ )
1167 pps->rowHeight[j] = nalu_get_exp_golomb_ue( bits ) + 1; /* row_height_minus1 [j] */
1168 pps->rowHeight[ pps->num_tile_rows_minus1 ] -= pps->rowHeight[j];
1171 pps->colBd[0] = 0;
1172 for( uint64_t i = 0; i < pps->num_tile_columns_minus1; i++ )
1173 pps->colBd[i + 1] = pps->colBd[i] + pps->colWidth[i];
1174 pps->rowBd[0] = 0;
1175 for( uint64_t j = 0; j < pps->num_tile_rows_minus1; j++ )
1176 pps->rowBd[j + 1] = pps->rowBd[j] + pps->rowHeight[j];
1177 lsmash_bits_get( bits, 1 ); /* loop_filter_across_tiles_enabled_flag */
1179 else
1181 pps->num_tile_columns_minus1 = 0;
1182 pps->num_tile_rows_minus1 = 0;
1183 if( hevc_allocate_tile_sizes( pps, 1, 1 ) < 0 )
1184 goto fail;
1185 pps->colWidth [0] = sps->PicWidthInCtbsY;
1186 pps->rowHeight[0] = sps->PicHeightInCtbsY;
1187 pps->colBd [0] = 0;
1188 pps->rowBd [0] = 0;
1190 /* */
1191 /* Skip PPS extension. */
1192 lsmash_bits_empty( bits );
1193 if( bits->bs->error )
1194 return -1;
1195 pps->present = 1;
1196 info->pps = *pps;
1197 hevc_activate_vps( info, info->sps.video_parameter_set_id );
1198 return 0;
1199 fail:
1200 /* Revert SPS. */
1201 info->sps = temp_sps;
1202 return 0;
1205 int hevc_parse_sei
1207 lsmash_bits_t *bits,
1208 hevc_vps_t *vps,
1209 hevc_sps_t *sps,
1210 hevc_sei_t *sei,
1211 hevc_nalu_header_t *nuh,
1212 uint8_t *rbsp_buffer,
1213 uint8_t *ebsp,
1214 uint64_t ebsp_size
1217 if( nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, ebsp_size ) )
1218 return -1;
1219 uint8_t *rbsp_start = rbsp_buffer;
1220 uint64_t rbsp_pos = 0;
1223 /* sei_message() */
1224 uint32_t payloadType = 0;
1225 for( uint8_t temp = lsmash_bits_get( bits, 8 ); ; temp = lsmash_bits_get( bits, 8 ) )
1227 /* 0xff : ff_byte
1228 * otherwise: last_payload_type_byte */
1229 payloadType += temp;
1230 ++rbsp_pos;
1231 if( temp != 0xff )
1232 break;
1234 uint32_t payloadSize = 0;
1235 for( uint8_t temp = lsmash_bits_get( bits, 8 ); ; temp = lsmash_bits_get( bits, 8 ) )
1237 /* 0xff : ff_byte
1238 * otherwise: last_payload_size_byte */
1239 payloadSize += temp;
1240 ++rbsp_pos;
1241 if( temp != 0xff )
1242 break;
1244 if( nuh->nal_unit_type == HEVC_NALU_TYPE_PREFIX_SEI )
1246 if( payloadType == 1 )
1248 /* pic_timing */
1249 hevc_hrd_t *hrd = sps ? &sps->vui.hrd : vps ? &vps->hrd[0] : NULL;
1250 if( !hrd )
1251 goto skip_sei_message; /* Any active VPS or SPS is not found. */
1252 sei->pic_timing.present = 1;
1253 if( (sps && sps->vui.frame_field_info_present_flag) || vps->frame_field_info_present_flag )
1255 sei->pic_timing.pic_struct = lsmash_bits_get( bits, 4 );
1256 lsmash_bits_get( bits, 2 ); /* source_scan_type */
1257 lsmash_bits_get( bits, 1 ); /* duplicate_flag */
1259 if( hrd->CpbDpbDelaysPresentFlag )
1261 lsmash_bits_get( bits, hrd->au_cpb_removal_delay_length ); /* au_cpb_removal_delay_minus1 */
1262 lsmash_bits_get( bits, hrd->dpb_output_delay_length ); /* pic_dpb_output_delay */
1263 if( hrd->sub_pic_hrd_params_present_flag )
1265 lsmash_bits_get( bits, hrd->dpb_output_delay_du_length ); /* pic_dpb_output_du_delay */
1266 if( hrd->sub_pic_cpb_params_in_pic_timing_sei_flag )
1268 uint64_t num_decoding_units_minus1 = nalu_get_exp_golomb_ue( bits );
1269 int du_common_cpb_removal_delay_flag = lsmash_bits_get( bits, 1 );
1270 if( du_common_cpb_removal_delay_flag )
1271 /* du_common_cpb_removal_delay_increment_minus1 */
1272 lsmash_bits_get( bits, hrd->du_cpb_removal_delay_increment_length );
1273 for( uint64_t i = 0; i <= num_decoding_units_minus1; i++ )
1275 nalu_get_exp_golomb_ue( bits ); /* num_nalus_in_du_minus1 */
1276 if( !du_common_cpb_removal_delay_flag && i < num_decoding_units_minus1 )
1277 nalu_get_exp_golomb_ue( bits ); /* du_cpb_removal_delay_increment_minus1 */
1283 else if( payloadType == 3 )
1285 /* filler_payload
1286 * FIXME: remove if array_completeness equal to 1. */
1287 return -1;
1289 else if( payloadType == 6 )
1291 /* recovery_point */
1292 sei->recovery_point.present = 1;
1293 sei->recovery_point.recovery_poc_cnt = nalu_get_exp_golomb_se( bits );
1294 lsmash_bits_get( bits, 1 ); /* exact_match_flag */
1295 sei->recovery_point.broken_link_flag = lsmash_bits_get( bits, 1 );
1297 else
1298 goto skip_sei_message;
1300 else if( nuh->nal_unit_type == HEVC_NALU_TYPE_SUFFIX_SEI )
1302 if( payloadType == 3 )
1304 /* filler_payload
1305 * FIXME: remove if array_completeness equal to 1. */
1306 return -1;
1308 else
1309 goto skip_sei_message;
1311 else
1313 skip_sei_message:
1314 lsmash_bits_get( bits, payloadSize * 8 );
1316 lsmash_bits_get_align( bits );
1317 rbsp_pos += payloadSize;
1318 } while( *(rbsp_start + rbsp_pos) != 0x80 ); /* All SEI messages are byte aligned at their end.
1319 * Therefore, 0x80 shall be rbsp_trailing_bits(). */
1320 lsmash_bits_empty( bits );
1321 return bits->bs->error ? -1 : 0;
1324 int hevc_parse_slice_segment_header
1326 hevc_info_t *info,
1327 hevc_nalu_header_t *nuh,
1328 uint8_t *rbsp_buffer,
1329 uint8_t *ebsp,
1330 uint64_t ebsp_size
1333 lsmash_bits_t *bits = info->bits;
1334 if( nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, LSMASH_MIN( ebsp_size, 50 ) ) )
1335 return -1;
1336 hevc_slice_info_t *slice = &info->slice;
1337 memset( slice, 0, sizeof(hevc_slice_info_t) );
1338 slice->nalu_type = nuh->nal_unit_type;
1339 slice->TemporalId = nuh->TemporalId;
1340 slice->first_slice_segment_in_pic_flag = lsmash_bits_get( bits, 1 );
1341 if( nuh->nal_unit_type >= HEVC_NALU_TYPE_BLA_W_LP
1342 && nuh->nal_unit_type <= HEVC_NALU_TYPE_RSV_IRAP_VCL23 )
1343 lsmash_bits_get( bits, 1 ); /* no_output_of_prior_pics_flag */
1344 slice->pic_parameter_set_id = nalu_get_exp_golomb_ue( bits );
1345 /* Get PPS by slice_pic_parameter_set_id. */
1346 hevc_pps_t *pps = hevc_get_pps( info->pps_list, slice->pic_parameter_set_id );
1347 if( !pps )
1348 return -1;
1349 /* Get SPS by pps_seq_parameter_set_id. */
1350 hevc_sps_t *sps = hevc_get_sps( info->sps_list, pps->seq_parameter_set_id );
1351 if( !sps )
1352 return -1;
1353 slice->video_parameter_set_id = sps->video_parameter_set_id;
1354 slice->seq_parameter_set_id = pps->seq_parameter_set_id;
1355 if( !slice->first_slice_segment_in_pic_flag )
1357 slice->dependent_slice_segment_flag = pps->dependent_slice_segments_enabled_flag ? lsmash_bits_get( bits, 1 ) : 0;
1358 slice->segment_address = lsmash_bits_get( bits, lsmash_ceil_log2( sps->PicSizeInCtbsY ) );
1360 else
1362 slice->dependent_slice_segment_flag = 0;
1363 slice->segment_address = 0;
1365 if( !slice->dependent_slice_segment_flag )
1367 /* independent slice segment
1368 * The values of the slice segment header of dependent slice segment are inferred from the values
1369 * for the preceding independent slice segment in decoding order, if some of the values are not present. */
1370 for( int i = 0; i < pps->num_extra_slice_header_bits; i++ )
1371 lsmash_bits_get( bits, 1 ); /* slice_reserved_flag[i] */
1372 slice->type = nalu_get_exp_golomb_ue( bits );
1373 if( pps->output_flag_present_flag )
1374 lsmash_bits_get( bits, 1 ); /* pic_output_flag */
1375 if( sps->separate_colour_plane_flag )
1376 lsmash_bits_get( bits, 1 ); /* colour_plane_id */
1377 if( nuh->nal_unit_type != HEVC_NALU_TYPE_IDR_W_RADL
1378 && nuh->nal_unit_type != HEVC_NALU_TYPE_IDR_N_LP )
1380 slice->pic_order_cnt_lsb = lsmash_bits_get( bits, sps->log2_max_pic_order_cnt_lsb );
1381 if( !lsmash_bits_get( bits, 1 ) ) /* short_term_ref_pic_set_sps_flag */
1383 if( hevc_short_term_ref_pic_set( bits, sps, sps->num_short_term_ref_pic_sets ) < 0 )
1384 return -1;
1386 else
1388 int length = lsmash_ceil_log2( sps->num_short_term_ref_pic_sets );
1389 if( length > 0 )
1390 lsmash_bits_get( bits, length ); /* short_term_ref_pic_set_idx */
1392 if( sps->long_term_ref_pics_present_flag )
1394 uint64_t num_long_term_sps = sps->num_long_term_ref_pics_sps > 0 ? nalu_get_exp_golomb_ue( bits ) : 0;
1395 uint64_t num_long_term_pics = nalu_get_exp_golomb_ue( bits );
1396 for( uint64_t i = 0; i < num_long_term_sps + num_long_term_pics; i++ )
1398 if( i < num_long_term_sps )
1400 int length = lsmash_ceil_log2( sps->num_long_term_ref_pics_sps );
1401 if( length > 0 )
1402 lsmash_bits_get( bits, length ); /* lt_idx_sps[i] */
1404 else
1406 lsmash_bits_get( bits, sps->log2_max_pic_order_cnt_lsb ); /* poc_lsb_lt [i] */
1407 lsmash_bits_get( bits, 1 ); /* used_by_curr_pic_lt_flag[i] */
1409 if( lsmash_bits_get( bits, 1 ) ) /* delta_poc_msb_present_flag[i] */
1410 nalu_get_exp_golomb_ue( bits ); /* delta_poc_msb_cycle_lt [i] */
1413 if( sps->temporal_mvp_enabled_flag )
1414 lsmash_bits_get( bits, 1 ); /* slice_temporal_mvp_enabled_flag */
1416 else
1417 /* For IDR-pictures, slice_pic_order_cnt_lsb is inferred to be 0. */
1418 slice->pic_order_cnt_lsb = 0;
1420 lsmash_bits_empty( bits );
1421 if( bits->bs->error )
1422 return -1;
1423 info->sps = *sps;
1424 info->pps = *pps;
1425 return 0;
1428 static int hevc_get_vps_id
1430 uint8_t *ps_ebsp,
1431 uint32_t ps_ebsp_length,
1432 uint8_t *ps_id
1435 /* the number of bits of vps_id = 4
1436 * (4 - 1) / 8 + 1 = 1 bytes */
1437 *ps_id = (*ps_ebsp >> 4) & 0x0F; /* vps_video_parameter_set_id */
1438 return 0;
1441 static int hevc_get_sps_id
1443 uint8_t *ps_ebsp,
1444 uint32_t ps_ebsp_length,
1445 uint8_t *ps_id
1448 /* the maximum number of bits of sps_id = 9: 0b00001XXXX
1449 * (8 + 688 + 9 - 1) / 8 + 1 = 89 bytes
1450 * Here more additional bytes because there might be emulation_prevention_three_byte(s). */
1451 lsmash_bits_t bits = { 0 };
1452 lsmash_bs_t bs = { 0 };
1453 uint8_t rbsp_buffer[128];
1454 uint8_t buffer [128];
1455 bs.buffer.data = buffer;
1456 bs.buffer.alloc = 128;
1457 lsmash_bits_init( &bits, &bs );
1458 if( nalu_import_rbsp_from_ebsp( &bits, rbsp_buffer, ps_ebsp, LSMASH_MIN( ps_ebsp_length, 128 ) ) )
1459 return -1;
1460 /* Skip sps_video_parameter_set_id and sps_temporal_id_nesting_flag. */
1461 uint8_t sps_max_sub_layers_minus1 = (lsmash_bits_get( &bits, 8 ) >> 1) & 0x07;
1462 /* profile_tier_level() costs at most 688 bits. */
1463 hevc_ptl_t sps_ptl;
1464 hevc_parse_profile_tier_level( &bits, &sps_ptl, sps_max_sub_layers_minus1 );
1465 uint64_t sps_seq_parameter_set_id = nalu_get_exp_golomb_ue( &bits );
1466 IF_INVALID_VALUE( sps_seq_parameter_set_id > HEVC_MAX_SPS_ID )
1467 return -1;
1468 *ps_id = sps_seq_parameter_set_id;
1469 return bs.error ? -1 : 0;
1472 static int hevc_get_pps_id
1474 uint8_t *ps_ebsp,
1475 uint32_t ps_ebsp_length,
1476 uint8_t *ps_id
1479 /* the maximum number of bits of pps_id = 13: 0b0000001XXXXXX
1480 * (13 - 1) / 8 + 1 = 2 bytes
1481 * Why +1? Because there might be an emulation_prevention_three_byte. */
1482 lsmash_bits_t bits = { 0 };
1483 lsmash_bs_t bs = { 0 };
1484 uint8_t rbsp_buffer[3];
1485 uint8_t buffer [3];
1486 bs.buffer.data = buffer;
1487 bs.buffer.alloc = 3;
1488 lsmash_bits_init( &bits, &bs );
1489 if( nalu_import_rbsp_from_ebsp( &bits, rbsp_buffer, ps_ebsp, LSMASH_MIN( ps_ebsp_length, 3 ) ) )
1490 return -1;
1491 uint64_t pic_parameter_set_id = nalu_get_exp_golomb_ue( &bits );
1492 IF_INVALID_VALUE( pic_parameter_set_id > HEVC_MAX_PPS_ID )
1493 return -1;
1494 *ps_id = pic_parameter_set_id;
1495 return bs.error ? -1 : 0;
1498 static inline int hevc_get_ps_id
1500 uint8_t *ps_ebsp,
1501 uint32_t ps_ebsp_length,
1502 uint8_t *ps_id,
1503 lsmash_hevc_dcr_nalu_type ps_type
1506 int (*get_ps_id)( uint8_t *ps_ebsp, uint32_t ps_ebsp_length, uint8_t *ps_id )
1507 = ps_type == HEVC_DCR_NALU_TYPE_VPS ? hevc_get_vps_id
1508 : ps_type == HEVC_DCR_NALU_TYPE_SPS ? hevc_get_sps_id
1509 : ps_type == HEVC_DCR_NALU_TYPE_PPS ? hevc_get_pps_id
1510 : NULL;
1511 return get_ps_id ? get_ps_id( ps_ebsp, ps_ebsp_length, ps_id ) : -1;
1514 static inline hevc_parameter_array_t *hevc_get_parameter_set_array
1516 lsmash_hevc_specific_parameters_t *param,
1517 lsmash_hevc_dcr_nalu_type ps_type
1520 if( !param->parameter_arrays )
1521 return NULL;
1522 if( ps_type >= HEVC_DCR_NALU_TYPE_NUM )
1523 return NULL;
1524 return &param->parameter_arrays->ps_array[ps_type];
1527 static inline lsmash_entry_list_t *hevc_get_parameter_set_list
1529 lsmash_hevc_specific_parameters_t *param,
1530 lsmash_hevc_dcr_nalu_type ps_type
1533 if( !param->parameter_arrays )
1534 return NULL;
1535 if( ps_type >= HEVC_DCR_NALU_TYPE_NUM )
1536 return NULL;
1537 return param->parameter_arrays->ps_array[ps_type].list;
1540 static lsmash_entry_t *hevc_get_ps_entry_from_param
1542 lsmash_hevc_specific_parameters_t *param,
1543 lsmash_hevc_dcr_nalu_type ps_type,
1544 uint8_t ps_id
1547 int (*get_ps_id)( uint8_t *ps_ebsp, uint32_t ps_ebsp_length, uint8_t *ps_id )
1548 = ps_type == HEVC_DCR_NALU_TYPE_VPS ? hevc_get_vps_id
1549 : ps_type == HEVC_DCR_NALU_TYPE_SPS ? hevc_get_sps_id
1550 : ps_type == HEVC_DCR_NALU_TYPE_PPS ? hevc_get_pps_id
1551 : NULL;
1552 if( !get_ps_id )
1553 return NULL;
1554 lsmash_entry_list_t *list = hevc_get_parameter_set_list( param, ps_type );
1555 if( !list )
1556 return NULL;
1557 for( lsmash_entry_t *entry = list->head; entry; entry = entry->next )
1559 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1560 if( !ps )
1561 return NULL;
1562 uint8_t param_ps_id;
1563 if( get_ps_id( ps->nalUnit + HEVC_MIN_NALU_HEADER_LENGTH,
1564 ps->nalUnitLength - HEVC_MIN_NALU_HEADER_LENGTH, &param_ps_id ) )
1565 return NULL;
1566 if( ps_id == param_ps_id )
1567 return entry;
1569 return NULL;
1572 static inline void hevc_update_picture_type
1574 hevc_picture_info_t *picture,
1575 hevc_slice_info_t *slice
1578 if( picture->type == HEVC_PICTURE_TYPE_I_P )
1580 if( slice->type == HEVC_SLICE_TYPE_B )
1581 picture->type = HEVC_PICTURE_TYPE_I_P_B;
1583 else if( picture->type == HEVC_PICTURE_TYPE_I )
1585 if( slice->type == HEVC_SLICE_TYPE_P )
1586 picture->type = HEVC_PICTURE_TYPE_I_P;
1587 else if( slice->type == HEVC_SLICE_TYPE_B )
1588 picture->type = HEVC_PICTURE_TYPE_I_P_B;
1590 else if( picture->type == HEVC_PICTURE_TYPE_NONE )
1592 if( slice->type == HEVC_SLICE_TYPE_P )
1593 picture->type = HEVC_PICTURE_TYPE_I_P;
1594 else if( slice->type == HEVC_SLICE_TYPE_B )
1595 picture->type = HEVC_PICTURE_TYPE_I_P_B;
1596 else if( slice->type == HEVC_SLICE_TYPE_I )
1597 picture->type = HEVC_PICTURE_TYPE_I;
1599 #if 0
1600 fprintf( stderr, "Picture type = %s\n", picture->type == HEVC_PICTURE_TYPE_I_P ? "P"
1601 : picture->type == HEVC_PICTURE_TYPE_I_P_B ? "B"
1602 : picture->type == HEVC_PICTURE_TYPE_I ? "I" );
1603 #endif
1606 /* Shall be called at least once per picture. */
1607 void hevc_update_picture_info_for_slice
1609 hevc_info_t *info,
1610 hevc_picture_info_t *picture,
1611 hevc_slice_info_t *slice
1614 assert( info );
1615 picture->has_primary |= !slice->dependent_slice_segment_flag;
1616 hevc_update_picture_type( picture, slice );
1617 /* Mark 'used' on active parameter sets. */
1618 uint8_t ps_id[3] = { slice->video_parameter_set_id, slice->seq_parameter_set_id, slice->pic_parameter_set_id };
1619 for( int i = 0; i < 3; i++ )
1621 lsmash_hevc_dcr_nalu_type ps_type = (lsmash_hevc_dcr_nalu_type)i;
1622 lsmash_entry_t *entry = hevc_get_ps_entry_from_param( &info->hvcC_param, ps_type, ps_id[i] );
1623 if( entry && entry->data )
1625 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1626 if( ps->unused )
1627 lsmash_append_hevc_dcr_nalu( &info->hvcC_param, ps_type, ps->nalUnit, ps->nalUnitLength );
1630 /* Discard this slice info. */
1631 slice->present = 0;
1634 /* Shall be called exactly once per picture. */
1635 void hevc_update_picture_info
1637 hevc_info_t *info,
1638 hevc_picture_info_t *picture,
1639 hevc_slice_info_t *slice,
1640 hevc_sps_t *sps,
1641 hevc_sei_t *sei
1644 picture->irap = slice->nalu_type >= HEVC_NALU_TYPE_BLA_W_LP && slice->nalu_type <= HEVC_NALU_TYPE_CRA;
1645 picture->idr = slice->nalu_type == HEVC_NALU_TYPE_IDR_W_RADL || slice->nalu_type == HEVC_NALU_TYPE_IDR_N_LP;
1646 picture->broken_link = slice->nalu_type >= HEVC_NALU_TYPE_BLA_W_LP && slice->nalu_type <= HEVC_NALU_TYPE_BLA_N_LP;
1647 picture->radl = slice->nalu_type == HEVC_NALU_TYPE_RADL_N || slice->nalu_type == HEVC_NALU_TYPE_RADL_R;
1648 picture->rasl = slice->nalu_type == HEVC_NALU_TYPE_RASL_N || slice->nalu_type == HEVC_NALU_TYPE_RASL_R;
1649 picture->sublayer_nonref = slice->nalu_type <= HEVC_NALU_TYPE_RSV_VCL_R15 && ((slice->nalu_type & 0x01) == 0);
1650 picture->closed_rap = slice->nalu_type >= HEVC_NALU_TYPE_BLA_W_RADL && slice->nalu_type <= HEVC_NALU_TYPE_IDR_N_LP;
1651 picture->random_accessible = picture->irap;
1652 picture->TemporalId = slice->TemporalId;
1653 picture->pic_parameter_set_id = slice->pic_parameter_set_id;
1654 picture->poc_lsb = slice->pic_order_cnt_lsb;
1655 hevc_update_picture_info_for_slice( info, picture, slice );
1656 picture->independent = (picture->type == HEVC_PICTURE_TYPE_I);
1657 picture->field_coded = sps->vui.field_seq_flag;
1658 if( sei->pic_timing.present )
1660 if( sei->pic_timing.pic_struct < 13 )
1662 static const uint8_t delta[13] = { 2, 1, 1, 2, 2, 3, 3, 4, 6, 1, 1, 1, 1 };
1663 picture->delta = delta[ sei->pic_timing.pic_struct ];
1665 else
1666 /* Reserved values in the spec we refer to. */
1667 picture->delta = picture->field_coded ? 1 : 2;
1668 sei->pic_timing.present = 0;
1670 else
1671 picture->delta = picture->field_coded ? 1 : 2;
1672 if( sei->recovery_point.present )
1674 picture->random_accessible |= sei->recovery_point.present;
1675 picture->recovery_poc_cnt = sei->recovery_point.recovery_poc_cnt;
1676 picture->broken_link |= sei->recovery_point.broken_link_flag;
1677 sei->recovery_point.present = 0;
1679 else
1680 picture->recovery_poc_cnt = 0;
1683 static uint64_t hevc_get_ctb_address_in_tile_scan
1685 hevc_sps_t *sps,
1686 hevc_pps_t *pps,
1687 uint64_t segment_address,
1688 uint64_t *TileId
1691 uint64_t tbX = segment_address % sps->PicWidthInCtbsY;
1692 uint64_t tbY = segment_address / sps->PicWidthInCtbsY;
1693 uint32_t tileX = pps->num_tile_columns_minus1;
1694 for( uint32_t i = 0; i <= pps->num_tile_columns_minus1; i++ )
1695 if( tbX >= pps->colBd[i] )
1696 tileX = i;
1697 uint32_t tileY = pps->num_tile_rows_minus1;
1698 for( uint32_t j = 0; j <= pps->num_tile_rows_minus1; j++ )
1699 if( tbY >= pps->rowBd[j] )
1700 tileY = j;
1701 uint64_t CtbAddrInTs = 0;
1702 for( uint32_t i = 0; i < tileX; i++ )
1703 CtbAddrInTs += pps->rowHeight[tileY] * pps->colWidth[i];
1704 for( uint32_t j = 0; j < tileY; j++ )
1705 CtbAddrInTs += sps->PicWidthInCtbsY * pps->rowHeight[j];
1706 CtbAddrInTs += (tbY - pps->rowBd[tileY]) * pps->colWidth[tileX] + tbX - pps->colBd[tileX];
1707 *TileId = (uint64_t)tileY * (pps->num_tile_columns_minus1 + 1) + tileX;
1708 return CtbAddrInTs;
1711 int hevc_find_au_delimit_by_slice_info
1713 hevc_info_t *info,
1714 hevc_slice_info_t *slice,
1715 hevc_slice_info_t *prev_slice
1718 /* 7.4.2.4.5 Order of VCL NAL units and association to coded pictures
1719 * - The first VCL NAL unit of the coded picture shall have first_slice_segment_in_pic_flag equal to 1. */
1720 if( slice->first_slice_segment_in_pic_flag )
1721 return 1;
1722 /* The value of TemporalId shall be the same for all VCL NAL units of an access unit. */
1723 if( slice->TemporalId != prev_slice->TemporalId )
1724 return 1;
1725 /* 7.4.2.4.5 Order of VCL NAL units and association to coded pictures
1726 * - if( TileId[ CtbAddrRsToTs[ slice->segment_address ] ] <= TileId[ CtbAddrRsToTs[ prev_slice->segment_address ] ]
1727 * || CtbAddrRsToTs[ slice->segment_address ] <= CtbAddrRsToTs[ prev_slice->segment_address ] )
1728 * return 1;
1730 hevc_pps_t *prev_pps = hevc_get_pps( info->pps_list, prev_slice->pic_parameter_set_id );
1731 if( !prev_pps )
1732 return 0;
1733 hevc_sps_t *prev_sps = hevc_get_sps( info->sps_list, prev_pps->seq_parameter_set_id );
1734 if( !prev_sps )
1735 return 0;
1736 uint64_t currTileId;
1737 uint64_t prevTileId;
1738 uint64_t currCtbAddrInTs = hevc_get_ctb_address_in_tile_scan( &info->sps, &info->pps, slice->segment_address, &currTileId );
1739 uint64_t prevCtbAddrInTs = hevc_get_ctb_address_in_tile_scan( prev_sps, prev_pps, prev_slice->segment_address, &prevTileId );
1740 if( currTileId <= prevTileId
1741 || currCtbAddrInTs <= prevCtbAddrInTs )
1742 return 1;
1743 return 0;
1746 int hevc_find_au_delimit_by_nalu_type
1748 uint8_t nalu_type,
1749 uint8_t prev_nalu_type
1752 /* 7.4.2.4.4 Order of NAL units and coded pictures and their association to access units */
1753 if( prev_nalu_type <= HEVC_NALU_TYPE_RSV_VCL31 )
1754 /* The first of any of the following NAL units after the last VCL NAL unit of a coded picture
1755 * specifies the start of a new access unit:
1756 * - access unit delimiter NAL unit (when present)
1757 * - VPS NAL unit (when present)
1758 * - SPS NAL unit (when present)
1759 * - PPS NAL unit (when present)
1760 * - Prefix SEI NAL unit (when present)
1761 * - NAL units with nal_unit_type in the range of RSV_NVCL41..RSV_NVCL44 (when present)
1762 * - NAL units with nal_unit_type in the range of UNSPEC48..UNSPEC55 (when present)
1763 * - first VCL NAL unit of a coded picture (always present) */
1764 return (nalu_type >= HEVC_NALU_TYPE_VPS && nalu_type <= HEVC_NALU_TYPE_AUD)
1765 || (nalu_type == HEVC_NALU_TYPE_PREFIX_SEI)
1766 || (nalu_type >= HEVC_NALU_TYPE_RSV_NVCL41 && nalu_type <= HEVC_NALU_TYPE_RSV_NVCL44)
1767 || (nalu_type >= HEVC_NALU_TYPE_UNSPEC48 && nalu_type <= HEVC_NALU_TYPE_UNSPEC55);
1768 else if( prev_nalu_type == HEVC_NALU_TYPE_EOS )
1769 /* An end of sequence NAL unit shall be the last NAL unit in the access unit unless the next
1770 * NAL unit is an end of bitstream NAL unit. */
1771 return (nalu_type != HEVC_NALU_TYPE_EOB);
1772 else
1773 /* An end of bitstream NAL unit shall be the last NAL unit in the access unit.
1774 * Thus, the next NAL unit shall be the first NAL unit in the next access unit. */
1775 return (prev_nalu_type == HEVC_NALU_TYPE_EOB);
1778 int hevc_supplement_buffer
1780 hevc_stream_buffer_t *sb,
1781 hevc_access_unit_t *au,
1782 uint32_t size
1785 lsmash_multiple_buffers_t *bank = lsmash_resize_multiple_buffers( sb->bank, size );
1786 if( !bank )
1787 return -1;
1788 sb->bank = bank;
1789 sb->rbsp = lsmash_withdraw_buffer( bank, 1 );
1790 if( au && bank->number_of_buffers == 3 )
1792 au->data = lsmash_withdraw_buffer( bank, 2 );
1793 au->incomplete_data = lsmash_withdraw_buffer( bank, 3 );
1795 return 0;
1798 static void hevc_bs_put_parameter_sets
1800 lsmash_bs_t *bs,
1801 lsmash_entry_list_t *dcr_ps_list,
1802 uint32_t max_dcr_ps_count
1805 uint32_t dcr_ps_count = 0;
1806 for( lsmash_entry_t *entry = dcr_ps_list->head; entry && dcr_ps_count < max_dcr_ps_count; entry = entry->next )
1808 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1809 if( ps && !ps->unused )
1811 lsmash_bs_put_be16( bs, ps->nalUnitLength );
1812 lsmash_bs_put_bytes( bs, ps->nalUnitLength, ps->nalUnit );
1814 else
1815 continue;
1816 ++dcr_ps_count;
1820 uint8_t *lsmash_create_hevc_specific_info
1822 lsmash_hevc_specific_parameters_t *param,
1823 uint32_t *data_length
1826 if( !param || !param->parameter_arrays || !data_length )
1827 return NULL;
1828 if( param->lengthSizeMinusOne != 0
1829 && param->lengthSizeMinusOne != 1
1830 && param->lengthSizeMinusOne != 3 )
1831 return NULL;
1832 hevc_parameter_array_t *param_arrays[HEVC_DCR_NALU_TYPE_NUM];
1833 lsmash_entry_list_t *dcr_ps_list [HEVC_DCR_NALU_TYPE_NUM];
1834 for( int i = 0; i < HEVC_DCR_NALU_TYPE_NUM; i++ )
1836 param_arrays[i] = &param->parameter_arrays->ps_array[i];
1837 dcr_ps_list [i] = param_arrays[i]->list;
1839 /* VPS, SPS and PPS are mandatory. */
1840 if( !dcr_ps_list[0] || !dcr_ps_list[0]->head || dcr_ps_list[0]->entry_count == 0
1841 || !dcr_ps_list[1] || !dcr_ps_list[1]->head || dcr_ps_list[1]->entry_count == 0
1842 || !dcr_ps_list[2] || !dcr_ps_list[2]->head || dcr_ps_list[2]->entry_count == 0 )
1843 return NULL;
1844 /* Calculate enough buffer size. */
1845 static const uint32_t max_dcr_ps_count[HEVC_DCR_NALU_TYPE_NUM] =
1847 HEVC_MAX_VPS_ID + 1,
1848 HEVC_MAX_SPS_ID + 1,
1849 HEVC_MAX_PPS_ID + 1,
1850 UINT16_MAX,
1851 UINT16_MAX
1853 uint32_t ps_count[HEVC_DCR_NALU_TYPE_NUM] = { 0 };
1854 uint32_t buffer_size = ISOM_BASEBOX_COMMON_SIZE + 23;
1855 for( int i = 0; i < HEVC_DCR_NALU_TYPE_NUM; i++ )
1856 if( dcr_ps_list[i] )
1858 for( lsmash_entry_t *entry = dcr_ps_list[i]->head; entry && ps_count[i] < max_dcr_ps_count[i]; entry = entry->next )
1860 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1861 if( !ps )
1862 return NULL;
1863 if( ps->unused )
1864 continue;
1865 buffer_size += 2 + ps->nalUnitLength;
1866 ++ps_count[i];
1868 buffer_size += 3;
1870 /* Set up bytestream writer. */
1871 uint8_t buffer[buffer_size];
1872 lsmash_bs_t bs = { 0 };
1873 bs.buffer.data = buffer;
1874 bs.buffer.alloc = buffer_size;
1875 /* Create an HEVCConfigurationBox */
1876 lsmash_bs_put_be32( &bs, 0 ); /* box size */
1877 lsmash_bs_put_be32( &bs, ISOM_BOX_TYPE_HVCC.fourcc ); /* box type: 'hvcC' */
1878 lsmash_bs_put_byte( &bs, HVCC_CONFIGURATION_VERSION ); /* configurationVersion */
1879 uint8_t temp8 = (param->general_profile_space << 6)
1880 | (param->general_tier_flag << 5)
1881 | param->general_profile_idc;
1882 lsmash_bs_put_byte( &bs, temp8 );
1883 lsmash_bs_put_be32( &bs, param->general_profile_compatibility_flags );
1884 lsmash_bs_put_be32( &bs, param->general_constraint_indicator_flags >> 16 );
1885 lsmash_bs_put_be16( &bs, param->general_constraint_indicator_flags );
1886 lsmash_bs_put_byte( &bs, param->general_level_idc );
1887 lsmash_bs_put_be16( &bs, param->min_spatial_segmentation_idc | 0xF000 );
1888 lsmash_bs_put_byte( &bs, param->parallelismType | 0xFC );
1889 lsmash_bs_put_byte( &bs, param->chromaFormat | 0xFC );
1890 lsmash_bs_put_byte( &bs, param->bitDepthLumaMinus8 | 0xF8 );
1891 lsmash_bs_put_byte( &bs, param->bitDepthChromaMinus8 | 0xF8 );
1892 lsmash_bs_put_be16( &bs, param->avgFrameRate );
1893 temp8 = (param->constantFrameRate << 6)
1894 | (param->numTemporalLayers << 3)
1895 | (param->temporalIdNested << 2)
1896 | param->lengthSizeMinusOne;
1897 lsmash_bs_put_byte( &bs, temp8 );
1898 uint8_t numOfArrays = !!ps_count[0]
1899 + !!ps_count[1]
1900 + !!ps_count[2]
1901 + !!ps_count[3]
1902 + !!ps_count[4];
1903 lsmash_bs_put_byte( &bs, numOfArrays );
1904 for( uint8_t i = 0; i < numOfArrays; i++ )
1906 temp8 = (param_arrays[i]->array_completeness << 7) | param_arrays[i]->NAL_unit_type;
1907 lsmash_bs_put_byte( &bs, temp8 );
1908 lsmash_bs_put_be16( &bs, ps_count[i] );
1909 hevc_bs_put_parameter_sets( &bs, dcr_ps_list[i], ps_count[i] );
1911 uint8_t *data = lsmash_bs_export_data( &bs, data_length );
1912 /* Update box size. */
1913 LSMASH_SET_BE32( data, *data_length );
1914 return data;
1917 static inline int hevc_validate_dcr_nalu_type
1919 lsmash_hevc_dcr_nalu_type ps_type,
1920 void *ps_data,
1921 uint32_t ps_length
1924 if( !ps_data || ps_length < 3 )
1925 return -1;
1926 if( ps_type != HEVC_DCR_NALU_TYPE_VPS
1927 && ps_type != HEVC_DCR_NALU_TYPE_SPS
1928 && ps_type != HEVC_DCR_NALU_TYPE_PPS
1929 && ps_type != HEVC_DCR_NALU_TYPE_PREFIX_SEI
1930 && ps_type != HEVC_DCR_NALU_TYPE_SUFFIX_SEI )
1931 return -1;
1932 uint8_t nalu_type = (*((uint8_t *)ps_data) >> 1) & 0x3f;
1933 if( nalu_type != HEVC_NALU_TYPE_VPS
1934 && nalu_type != HEVC_NALU_TYPE_SPS
1935 && nalu_type != HEVC_NALU_TYPE_PPS
1936 && nalu_type != HEVC_NALU_TYPE_PREFIX_SEI
1937 && nalu_type != HEVC_NALU_TYPE_SUFFIX_SEI )
1938 return -1;
1939 if( (ps_type == HEVC_DCR_NALU_TYPE_VPS && nalu_type != HEVC_NALU_TYPE_VPS)
1940 || (ps_type == HEVC_DCR_NALU_TYPE_SPS && nalu_type != HEVC_NALU_TYPE_SPS)
1941 || (ps_type == HEVC_DCR_NALU_TYPE_PPS && nalu_type != HEVC_NALU_TYPE_PPS)
1942 || (ps_type == HEVC_DCR_NALU_TYPE_PREFIX_SEI && nalu_type != HEVC_NALU_TYPE_PREFIX_SEI)
1943 || (ps_type == HEVC_DCR_NALU_TYPE_SUFFIX_SEI && nalu_type != HEVC_NALU_TYPE_SUFFIX_SEI) )
1944 return -1;
1945 return 0;
1948 lsmash_dcr_nalu_appendable lsmash_check_hevc_dcr_nalu_appendable
1950 lsmash_hevc_specific_parameters_t *param,
1951 lsmash_hevc_dcr_nalu_type ps_type,
1952 void *_ps_data,
1953 uint32_t ps_length
1956 uint8_t *ps_data = _ps_data;
1957 if( !param )
1958 return DCR_NALU_APPEND_ERROR;
1959 if( hevc_validate_dcr_nalu_type( ps_type, ps_data, ps_length ) )
1960 return DCR_NALU_APPEND_ERROR;
1961 /* Check whether the same parameter set already exsits or not. */
1962 lsmash_entry_list_t *ps_list = hevc_get_parameter_set_list( param, ps_type );
1963 if( !ps_list || !ps_list->head )
1964 return DCR_NALU_APPEND_POSSIBLE; /* No parameter set */
1965 switch( nalu_check_same_ps_existence( ps_list, ps_data, ps_length ) )
1967 case 0 : break;
1968 case 1 : return DCR_NALU_APPEND_DUPLICATED; /* The same parameter set already exists. */
1969 default : return DCR_NALU_APPEND_ERROR; /* An error occured. */
1971 /* Check the number of parameter sets in HEVC Decoder Configuration Record. */
1972 uint32_t ps_count;
1973 if( nalu_get_ps_count( ps_list, &ps_count ) )
1974 return DCR_NALU_APPEND_ERROR;
1975 if( (ps_type == HEVC_DCR_NALU_TYPE_VPS && ps_count >= HEVC_MAX_VPS_ID)
1976 || (ps_type == HEVC_DCR_NALU_TYPE_SPS && ps_count >= HEVC_MAX_SPS_ID)
1977 || (ps_type == HEVC_DCR_NALU_TYPE_PPS && ps_count >= HEVC_MAX_PPS_ID)
1978 || (ps_type == HEVC_DCR_NALU_TYPE_PREFIX_SEI && ps_count >= UINT16_MAX)
1979 || (ps_type == HEVC_DCR_NALU_TYPE_SUFFIX_SEI && ps_count >= UINT16_MAX) )
1980 return DCR_NALU_APPEND_NEW_DCR_REQUIRED; /* No more appendable parameter sets. */
1981 if( ps_type == HEVC_DCR_NALU_TYPE_PREFIX_SEI
1982 || ps_type == HEVC_DCR_NALU_TYPE_SUFFIX_SEI )
1983 return DCR_NALU_APPEND_POSSIBLE;
1984 /* Check the maximum length of parameter sets in HEVC Decoder Configuration Record. */
1985 uint32_t max_ps_length;
1986 if( nalu_get_max_ps_length( ps_list, &max_ps_length ) )
1987 return DCR_NALU_APPEND_ERROR;
1988 max_ps_length = LSMASH_MAX( max_ps_length, ps_length );
1989 /* Check whether a new specific info is needed or not. */
1990 lsmash_bits_t bits = { 0 };
1991 lsmash_bs_t bs = { 0 };
1992 uint8_t rbsp_buffer[max_ps_length];
1993 uint8_t bs_buffer [max_ps_length];
1994 bs.buffer.data = bs_buffer;
1995 bs.buffer.alloc = max_ps_length;
1996 lsmash_bits_init( &bits, &bs );
1997 if( ps_type == HEVC_DCR_NALU_TYPE_PPS )
1999 /* PPS */
2000 uint8_t pps_id;
2001 if( hevc_get_pps_id( ps_data + HEVC_MIN_NALU_HEADER_LENGTH,
2002 ps_length - HEVC_MIN_NALU_HEADER_LENGTH, &pps_id ) )
2003 return DCR_NALU_APPEND_ERROR;
2004 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
2006 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
2007 if( !ps )
2008 return DCR_NALU_APPEND_ERROR;
2009 if( ps->unused )
2010 continue;
2011 uint8_t param_pps_id;
2012 if( hevc_get_pps_id( ps->nalUnit + HEVC_MIN_NALU_HEADER_LENGTH,
2013 ps->nalUnitLength - HEVC_MIN_NALU_HEADER_LENGTH, &param_pps_id ) )
2014 return DCR_NALU_APPEND_ERROR;
2015 if( pps_id == param_pps_id )
2016 /* PPS that has the same pic_parameter_set_id already exists with different form. */
2017 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
2019 return DCR_NALU_APPEND_POSSIBLE;
2021 else if( ps_type == HEVC_DCR_NALU_TYPE_VPS )
2023 /* VPS */
2024 hevc_vps_t vps;
2025 if( hevc_parse_vps_minimally( &bits, &vps, rbsp_buffer,
2026 ps_data + HEVC_MIN_NALU_HEADER_LENGTH,
2027 ps_length - HEVC_MIN_NALU_HEADER_LENGTH ) )
2028 return DCR_NALU_APPEND_ERROR;
2029 /* The value of profile_space must be identical in all the parameter sets in a single HEVC Decoder Configuration Record. */
2030 if( vps.ptl.general.profile_space != param->general_profile_space )
2031 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
2032 /* FIXME */
2033 if( vps.ptl.general.profile_idc != param->general_profile_idc )
2034 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
2035 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
2037 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
2038 if( !ps )
2039 return DCR_NALU_APPEND_ERROR;
2040 if( ps->unused )
2041 continue;
2042 uint8_t param_vps_id;
2043 if( hevc_get_vps_id( ps->nalUnit + HEVC_MIN_NALU_HEADER_LENGTH,
2044 ps->nalUnitLength - HEVC_MIN_NALU_HEADER_LENGTH, &param_vps_id ) )
2045 return DCR_NALU_APPEND_ERROR;
2046 if( param_vps_id == vps.video_parameter_set_id )
2047 /* VPS that has the same video_parameter_set_id already exists with different form. */
2048 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
2050 return DCR_NALU_APPEND_POSSIBLE;
2052 /* SPS */
2053 hevc_sps_t sps;
2054 if( hevc_parse_sps_minimally( &bits, &sps, rbsp_buffer,
2055 ps_data + HEVC_MIN_NALU_HEADER_LENGTH,
2056 ps_length - HEVC_MIN_NALU_HEADER_LENGTH ) )
2057 return DCR_NALU_APPEND_ERROR;
2058 lsmash_bits_empty( &bits );
2059 /* The values of profile_space, chromaFormat, bitDepthLumaMinus8 and bitDepthChromaMinus8
2060 * must be identical in all the parameter sets in a single HEVC Decoder Configuration Record. */
2061 if( sps.ptl.general.profile_space != param->general_profile_space
2062 || sps.chroma_format_idc != param->chromaFormat
2063 || sps.bit_depth_luma_minus8 != param->bitDepthLumaMinus8
2064 || sps.bit_depth_chroma_minus8 != param->bitDepthChromaMinus8 )
2065 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
2066 /* FIXME; If the sequence parameter sets are marked with different profiles,
2067 * and the relevant profile compatibility flags are all zero,
2068 * then the stream may need examination to determine which profile, if any, the stream conforms to.
2069 * If the stream is not examined, or the examination reveals that there is no profile to which the stream conforms,
2070 * then the stream must be split into two or more sub-streams with separate configuration records in which these rules can be met. */
2071 #if 0
2072 if( sps.ptl.general.profile_idc != param->general_profile_idc
2073 && (sps.ptl.general.profile_compatibility_flags & param->general_profile_compatibility_flags) )
2074 #else
2075 if( sps.ptl.general.profile_idc != param->general_profile_idc )
2076 #endif
2077 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
2078 /* Forbidden to duplicate SPS that has the same seq_parameter_set_id with different form within the same configuration record. */
2079 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
2081 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
2082 if( !ps )
2083 return DCR_NALU_APPEND_ERROR;
2084 if( ps->unused )
2085 continue;
2086 uint8_t param_sps_id;
2087 if( hevc_get_sps_id( ps->nalUnit + HEVC_MIN_NALU_HEADER_LENGTH,
2088 ps->nalUnitLength - HEVC_MIN_NALU_HEADER_LENGTH, &param_sps_id ) )
2089 return DCR_NALU_APPEND_ERROR;
2090 if( param_sps_id == sps.seq_parameter_set_id )
2091 /* SPS that has the same seq_parameter_set_id already exists with different form. */
2092 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
2093 if( entry == ps_list->head )
2095 /* Check if the cropped visual presentation sizes, the sample aspect ratios, the colour descriptions and
2096 * the default display windows are different. */
2097 hevc_sps_t first_sps;
2098 if( hevc_parse_sps_minimally( &bits, &first_sps, rbsp_buffer,
2099 ps->nalUnit + HEVC_MIN_NALU_HEADER_LENGTH,
2100 ps->nalUnitLength - HEVC_MIN_NALU_HEADER_LENGTH ) )
2101 return DCR_NALU_APPEND_ERROR;
2102 if( sps.cropped_width != first_sps.cropped_width
2103 || sps.cropped_height != first_sps.cropped_height
2104 || sps.vui.sar_width != first_sps.vui.sar_width
2105 || sps.vui.sar_height != first_sps.vui.sar_height
2106 || sps.vui.colour_primaries != first_sps.vui.colour_primaries
2107 || sps.vui.transfer_characteristics != first_sps.vui.transfer_characteristics
2108 || sps.vui.matrix_coeffs != first_sps.vui.matrix_coeffs
2109 || sps.vui.video_full_range_flag != first_sps.vui.video_full_range_flag
2110 || sps.vui.def_disp_win_offset.left .n != first_sps.vui.def_disp_win_offset.left .n
2111 || sps.vui.def_disp_win_offset.right .n != first_sps.vui.def_disp_win_offset.right .n
2112 || sps.vui.def_disp_win_offset.top .n != first_sps.vui.def_disp_win_offset.top .n
2113 || sps.vui.def_disp_win_offset.bottom.n != first_sps.vui.def_disp_win_offset.bottom.n )
2114 return DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED;
2117 return DCR_NALU_APPEND_POSSIBLE;
2120 static inline void hevc_specific_parameters_ready
2122 lsmash_hevc_specific_parameters_t *param
2125 param->general_profile_compatibility_flags = ~UINT32_C(0);
2126 param->general_constraint_indicator_flags = 0x0000FFFFFFFFFFFF;
2127 param->min_spatial_segmentation_idc = 0x0FFF;
2128 param->avgFrameRate = 0; /* unspecified average frame rate */
2129 param->constantFrameRate = 2;
2130 param->numTemporalLayers = 0;
2131 param->temporalIdNested = 1;
2134 static inline void hevc_specific_parameters_update_ptl
2136 lsmash_hevc_specific_parameters_t *param,
2137 hevc_ptl_t *ptl
2140 param->general_profile_space = ptl->general.profile_space;
2141 param->general_tier_flag = LSMASH_MAX( param->general_tier_flag, ptl->general.tier_flag );
2142 param->general_profile_idc = ptl->general.profile_idc;
2143 param->general_profile_compatibility_flags &= ptl->general.profile_compatibility_flags;
2144 param->general_constraint_indicator_flags &= ((uint64_t)ptl->general.progressive_source_flag << 47)
2145 | ((uint64_t)ptl->general.interlaced_source_flag << 46)
2146 | ((uint64_t)ptl->general.non_packed_constraint_flag << 45)
2147 | ((uint64_t)ptl->general.frame_only_constraint_flag << 44)
2148 | ptl->general.reserved_zero_44bits;
2149 param->general_level_idc = LSMASH_MAX( param->general_level_idc, ptl->general.level_idc );
2152 static inline void hevc_reorder_parameter_set_ascending_id
2154 lsmash_hevc_specific_parameters_t *param,
2155 lsmash_hevc_dcr_nalu_type ps_type,
2156 lsmash_entry_list_t *ps_list,
2157 uint8_t ps_id
2160 lsmash_entry_t *entry = NULL;
2161 if( ps_id )
2162 for( int i = ps_id - 1; i; i-- )
2164 entry = hevc_get_ps_entry_from_param( param, ps_type, i );
2165 if( entry )
2166 break;
2168 int append_head = 0;
2169 if( !entry )
2171 /* Couldn't find any parameter set with lower identifier.
2172 * Next, find parameter set with upper identifier. */
2173 int max_ps_id = ps_type == HEVC_DCR_NALU_TYPE_VPS ? HEVC_MAX_VPS_ID
2174 : ps_type == HEVC_DCR_NALU_TYPE_SPS ? HEVC_MAX_SPS_ID
2175 : HEVC_MAX_PPS_ID;
2176 for( int i = ps_id + 1; i <= max_ps_id; i++ )
2178 entry = hevc_get_ps_entry_from_param( param, ps_type, i );
2179 if( entry )
2180 break;
2182 if( entry )
2183 append_head = 1;
2185 if( !entry )
2186 return; /* The new entry was appended to the tail. */
2187 lsmash_entry_t *new_entry = ps_list->tail;
2188 if( append_head )
2190 /* before: entry[i > ps_id] ... -> prev_entry -> new_entry[ps_id]
2191 * after: new_entry[ps_id] -> entry[i > ps_id] -> ... -> prev_entry */
2192 if( new_entry->prev )
2193 new_entry->prev->next = NULL;
2194 new_entry->prev = NULL;
2195 entry->prev = new_entry;
2196 new_entry->next = entry;
2197 return;
2199 /* before: entry[i < ps_id] -> next_entry -> ... -> prev_entry -> new_entry[ps_id]
2200 * after: entry[i < ps_id] -> new_entry[ps_id] -> next_entry -> ... -> prev_entry */
2201 if( new_entry->prev )
2202 new_entry->prev->next = NULL;
2203 new_entry->prev = entry;
2204 new_entry->next = entry->next;
2205 if( entry->next )
2206 entry->next->prev = new_entry;
2207 entry->next = new_entry;
2210 static inline int hevc_alloc_parameter_arrays
2212 lsmash_hevc_specific_parameters_t *param
2215 assert( param );
2216 if( param->parameter_arrays )
2217 return 0;
2218 lsmash_hevc_parameter_arrays_t *parameter_arrays = lsmash_malloc_zero( sizeof(lsmash_hevc_parameter_arrays_t) );
2219 if( !parameter_arrays )
2220 return -1;
2221 param->parameter_arrays = parameter_arrays;
2222 parameter_arrays->ps_array[HEVC_DCR_NALU_TYPE_VPS ].array_completeness = 1;
2223 parameter_arrays->ps_array[HEVC_DCR_NALU_TYPE_VPS ].NAL_unit_type = HEVC_NALU_TYPE_VPS;
2224 parameter_arrays->ps_array[HEVC_DCR_NALU_TYPE_SPS ].array_completeness = 1;
2225 parameter_arrays->ps_array[HEVC_DCR_NALU_TYPE_SPS ].NAL_unit_type = HEVC_NALU_TYPE_SPS;
2226 parameter_arrays->ps_array[HEVC_DCR_NALU_TYPE_PPS ].array_completeness = 1;
2227 parameter_arrays->ps_array[HEVC_DCR_NALU_TYPE_PPS ].NAL_unit_type = HEVC_NALU_TYPE_PPS;
2228 parameter_arrays->ps_array[HEVC_DCR_NALU_TYPE_PREFIX_SEI].array_completeness = 0;
2229 parameter_arrays->ps_array[HEVC_DCR_NALU_TYPE_PREFIX_SEI].NAL_unit_type = HEVC_NALU_TYPE_PREFIX_SEI;
2230 parameter_arrays->ps_array[HEVC_DCR_NALU_TYPE_SUFFIX_SEI].array_completeness = 0;
2231 parameter_arrays->ps_array[HEVC_DCR_NALU_TYPE_SUFFIX_SEI].NAL_unit_type = HEVC_NALU_TYPE_SUFFIX_SEI;
2232 return 0;
2235 int lsmash_append_hevc_dcr_nalu
2237 lsmash_hevc_specific_parameters_t *param,
2238 lsmash_hevc_dcr_nalu_type ps_type,
2239 void *_ps_data,
2240 uint32_t ps_length
2243 uint8_t *ps_data = _ps_data;
2244 if( !param || !ps_data || ps_length < 2 )
2245 return -1;
2246 if( hevc_alloc_parameter_arrays( param ) < 0 )
2247 return -1;
2248 hevc_parameter_array_t *ps_array = hevc_get_parameter_set_array( param, ps_type );
2249 if( !ps_array )
2250 return -1;
2251 lsmash_entry_list_t *ps_list = ps_array->list;
2252 if( ps_type == HEVC_DCR_NALU_TYPE_PREFIX_SEI
2253 || ps_type == HEVC_DCR_NALU_TYPE_SUFFIX_SEI )
2255 /* Append a SEI anyway. */
2256 isom_dcr_ps_entry_t *ps = isom_create_ps_entry( ps_data, ps_length );
2257 if( !ps )
2258 return -1;
2259 if( lsmash_add_entry( ps_list, ps ) )
2261 isom_remove_dcr_ps( ps );
2262 return -1;
2264 return 0;
2266 if( ps_type != HEVC_DCR_NALU_TYPE_VPS
2267 && ps_type != HEVC_DCR_NALU_TYPE_SPS
2268 && ps_type != HEVC_DCR_NALU_TYPE_PPS )
2269 return -1;
2270 /* Check if the same parameter set identifier already exists. */
2271 uint8_t ps_id;
2272 if( hevc_get_ps_id( ps_data + HEVC_MIN_NALU_HEADER_LENGTH,
2273 ps_length - HEVC_MIN_NALU_HEADER_LENGTH, &ps_id, ps_type ) )
2274 return -1;
2275 lsmash_entry_t *entry = hevc_get_ps_entry_from_param( param, ps_type, ps_id );
2276 isom_dcr_ps_entry_t *ps = entry ? (isom_dcr_ps_entry_t *)entry->data : NULL;
2277 if( ps && !ps->unused )
2278 /* The same parameter set identifier already exists. */
2279 return -1;
2280 int invoke_reorder;
2281 if( ps )
2283 /* Reuse an already existed parameter set in the list. */
2284 ps->unused = 0;
2285 if( ps->nalUnit != ps_data )
2287 /* The same address could be given when called by hevc_update_picture_info_for_slice(). */
2288 lsmash_free( ps->nalUnit );
2289 ps->nalUnit = ps_data;
2291 ps->nalUnitLength = ps_length;
2292 invoke_reorder = 0;
2294 else
2296 /* Create a new parameter set and append it into the list. */
2297 ps = isom_create_ps_entry( ps_data, ps_length );
2298 if( !ps )
2299 return -1;
2300 if( lsmash_add_entry( ps_list, ps ) )
2302 isom_remove_dcr_ps( ps );
2303 return -1;
2305 invoke_reorder = 1;
2307 uint32_t ps_count;
2308 if( nalu_get_ps_count( ps_list, &ps_count ) < 0 )
2309 goto fail;
2310 /* Update specific info with VPS, SPS or PPS. */
2312 lsmash_bits_t bits = { 0 };
2313 lsmash_bs_t bs = { 0 };
2314 uint8_t rbsp_buffer[ps_length];
2315 uint8_t buffer [ps_length];
2316 bs.buffer.data = buffer;
2317 bs.buffer.alloc = ps_length;
2318 lsmash_bits_init( &bits, &bs );
2319 if( ps_type == HEVC_DCR_NALU_TYPE_VPS )
2321 hevc_vps_t vps;
2322 if( hevc_parse_vps_minimally( &bits, &vps, rbsp_buffer,
2323 ps_data + HEVC_MIN_NALU_HEADER_LENGTH,
2324 ps_length - HEVC_MIN_NALU_HEADER_LENGTH ) < 0 )
2325 goto fail;
2326 if( ps_count == 1 )
2328 /* Initialize if not initialized yet. */
2329 lsmash_entry_list_t *sps_list = hevc_get_parameter_set_list( param, HEVC_DCR_NALU_TYPE_SPS );
2330 uint32_t sps_count;
2331 if( nalu_get_ps_count( sps_list, &sps_count ) < 0 )
2332 goto fail;
2333 if( sps_count == 0 )
2334 hevc_specific_parameters_ready( param );
2336 hevc_specific_parameters_update_ptl( param, &vps.ptl );
2337 param->numTemporalLayers = LSMASH_MAX( param->numTemporalLayers, vps.max_sub_layers_minus1 + 1 );
2338 //param->temporalIdNested &= vps.temporal_id_nesting_flag;
2340 else if( ps_type == HEVC_DCR_NALU_TYPE_SPS )
2342 hevc_sps_t sps;
2343 if( hevc_parse_sps_minimally( &bits, &sps, rbsp_buffer,
2344 ps_data + HEVC_MIN_NALU_HEADER_LENGTH,
2345 ps_length - HEVC_MIN_NALU_HEADER_LENGTH ) < 0 )
2346 goto fail;
2347 if( ps_count == 1 )
2349 /* Initialize if not initialized yet. */
2350 lsmash_entry_list_t *vps_list = hevc_get_parameter_set_list( param, HEVC_DCR_NALU_TYPE_VPS );
2351 uint32_t vps_count;
2352 if( nalu_get_ps_count( vps_list, &vps_count ) < 0 )
2353 goto fail;
2354 if( vps_count == 0 )
2355 hevc_specific_parameters_ready( param );
2357 hevc_specific_parameters_update_ptl( param, &sps.ptl );
2358 param->min_spatial_segmentation_idc = LSMASH_MIN( param->min_spatial_segmentation_idc, sps.vui.min_spatial_segmentation_idc );
2359 param->chromaFormat = sps.chroma_format_idc;
2360 param->bitDepthLumaMinus8 = sps.bit_depth_luma_minus8;
2361 param->bitDepthChromaMinus8 = sps.bit_depth_chroma_minus8;
2362 param->numTemporalLayers = LSMASH_MAX( param->numTemporalLayers, sps.max_sub_layers_minus1 + 1 );
2363 param->temporalIdNested &= sps.temporal_id_nesting_flag;
2364 /* Check type of constant frame rate. */
2365 if( param->constantFrameRate )
2367 int cfr;
2368 if( param->constantFrameRate == 2 )
2370 cfr = 1;
2371 for( uint8_t i = 0; i <= sps.max_sub_layers_minus1; i++ )
2372 cfr &= sps.vui.hrd.fixed_pic_rate_general_flag[i];
2374 else
2375 cfr = 0;
2376 if( cfr )
2377 param->constantFrameRate = 2;
2378 else
2380 for( uint8_t i = 0; i <= sps.max_sub_layers_minus1; i++ )
2381 cfr |= sps.vui.hrd.fixed_pic_rate_general_flag[i];
2382 param->constantFrameRate = cfr;
2385 #if 0
2386 /* FIXME: probably, we can get average frame rate according to C.3.3 Picture output. */
2387 if( param->constantFrameRate )
2389 uint64_t interval = 0;
2390 for( uint8_t i = 0; i <= sps.max_sub_layers_minus1; i++ )
2391 interval += sps.vui.num_units_in_tick * sps.vui.hrd.elemental_duration_in_tc_minus1[i];
2392 uint64_t frame_rate;
2393 if( interval )
2394 frame_rate = ((256 * (2 - sps.vui.field_seq_flag) * (uint64_t)sps.vui.time_scale)
2395 * (sps.max_sub_layers_minus1 + 1)) / interval;
2396 else
2397 frame_rate = 0;
2398 if( frame_rate != param->avgFrameRate && param->avgFrameRate )
2399 param->constantFrameRate = 0;
2400 param->avgFrameRate = frame_rate;
2402 #endif
2404 else
2406 hevc_pps_t pps;
2407 if( hevc_parse_pps_minimally( &bits, &pps, rbsp_buffer,
2408 ps_data + HEVC_MIN_NALU_HEADER_LENGTH,
2409 ps_length - HEVC_MIN_NALU_HEADER_LENGTH ) < 0 )
2410 goto fail;
2411 uint8_t parallelismType = 0;
2412 #if 1 /* Replace 1 with 0 if parallelismType shall be set to 0 when min_spatial_segmentation_idc equal to 0. */
2414 if( pps.entropy_coding_sync_enabled_flag )
2415 parallelismType = pps.tiles_enabled_flag ? 0 : 3;
2416 else if( pps.tiles_enabled_flag )
2417 parallelismType = 2;
2418 else
2419 parallelismType = 1;
2420 #else
2421 /* Parse SPS and check the value of min_spatial_segmentation_idc is equal to zero or not.
2422 * If the value is not equal to zero, update parallelismType appropriately.
2423 * If corresponding SPS is not found, set 0 to parallelismType. */
2424 entry = hevc_get_ps_entry_from_param( param, HEVC_DCR_NALU_TYPE_SPS, pps.seq_parameter_set_id );
2425 if( entry && entry->data )
2427 ps = (isom_dcr_ps_entry_t *)entry->data;
2428 lsmash_bits_t sps_bits = { 0 };
2429 lsmash_bs_t sps_bs = { 0 };
2430 uint8_t sps_rbsp_buffer[ ps->nalUnitLength ];
2431 uint8_t sps_buffer [ ps->nalUnitLength ];
2432 sps_bs.buffer.data = sps_buffer;
2433 sps_bs.buffer.alloc = ps->nalUnitLength;
2434 lsmash_bits_init( &sps_bits, &sps_bs );
2435 hevc_sps_t sps;
2436 if( hevc_parse_sps_minimally( &sps_bits, &sps, sps_rbsp_buffer,
2437 ps->nalUnit + HEVC_MIN_NALU_HEADER_LENGTH,
2438 ps->nalUnitLength - HEVC_MIN_NALU_HEADER_LENGTH ) == 0 )
2440 if( sps.vui.min_spatial_segmentation_idc )
2442 if( pps.entropy_coding_sync_enabled_flag )
2443 parallelismType = pps.tiles_enabled_flag ? 0 : 3;
2444 else if( pps.tiles_enabled_flag )
2445 parallelismType = 2;
2446 else
2447 parallelismType = 1;
2449 else
2450 parallelismType = 0;
2453 #endif
2454 if( ps_count == 1 )
2455 param->parallelismType = parallelismType;
2456 else if( param->parallelismType != parallelismType )
2457 param->parallelismType = 0;
2460 if( invoke_reorder )
2461 /* Add a new parameter set in order of ascending parameter set identifier. */
2462 hevc_reorder_parameter_set_ascending_id( param, ps_type, ps_list, ps_id );
2463 return 0;
2464 fail:
2465 ps = (isom_dcr_ps_entry_t *)lsmash_get_entry_data( ps_list, ps_list->entry_count );
2466 if( ps )
2467 ps->unused = 1;
2468 return -1;
2471 int hevc_try_to_append_dcr_nalu
2473 hevc_info_t *info,
2474 lsmash_hevc_dcr_nalu_type ps_type,
2475 void *_ps_data,
2476 uint32_t ps_length
2479 uint8_t *ps_data = _ps_data;
2480 lsmash_dcr_nalu_appendable ret = lsmash_check_hevc_dcr_nalu_appendable( &info->hvcC_param, ps_type, ps_data, ps_length );
2481 lsmash_hevc_specific_parameters_t *param;
2482 switch( ret )
2484 case DCR_NALU_APPEND_ERROR : /* Error */
2485 return -1;
2486 case DCR_NALU_APPEND_NEW_DCR_REQUIRED : /* Mulitiple sample description is needed. */
2487 case DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED : /* Mulitiple sample description is needed. */
2488 param = &info->hvcC_param_next;
2489 info->hvcC_pending = 1;
2490 break;
2491 case DCR_NALU_APPEND_POSSIBLE : /* Appendable */
2492 param = info->hvcC_pending ? &info->hvcC_param_next : &info->hvcC_param;
2493 break;
2494 default : /* No need to append */
2495 return DCR_NALU_APPEND_DUPLICATED;
2497 switch( ps_type )
2499 case HEVC_DCR_NALU_TYPE_VPS :
2500 if( hevc_parse_vps( info, info->buffer.rbsp,
2501 ps_data + HEVC_MIN_NALU_HEADER_LENGTH,
2502 ps_length - HEVC_MIN_NALU_HEADER_LENGTH ) )
2503 return -1;
2504 break;
2505 case HEVC_DCR_NALU_TYPE_SPS :
2506 if( hevc_parse_sps( info, info->buffer.rbsp,
2507 ps_data + HEVC_MIN_NALU_HEADER_LENGTH,
2508 ps_length - HEVC_MIN_NALU_HEADER_LENGTH ) )
2509 return -1;
2510 break;
2511 case HEVC_DCR_NALU_TYPE_PPS :
2512 if( hevc_parse_pps( info, info->buffer.rbsp,
2513 ps_data + HEVC_MIN_NALU_HEADER_LENGTH,
2514 ps_length - HEVC_MIN_NALU_HEADER_LENGTH ) )
2515 return -1;
2516 break;
2517 default :
2518 break;
2520 return lsmash_append_hevc_dcr_nalu( param, ps_type, ps_data, ps_length );
2523 static int hevc_move_dcr_nalu_entry
2525 lsmash_hevc_specific_parameters_t *dst_data,
2526 lsmash_hevc_specific_parameters_t *src_data,
2527 lsmash_hevc_dcr_nalu_type ps_type
2530 lsmash_entry_list_t *src_ps_list = hevc_get_parameter_set_list( src_data, ps_type );
2531 lsmash_entry_list_t *dst_ps_list = hevc_get_parameter_set_list( dst_data, ps_type );
2532 assert( src_ps_list && dst_ps_list );
2533 for( lsmash_entry_t *src_entry = src_ps_list->head; src_entry; src_entry = src_entry->next )
2535 isom_dcr_ps_entry_t *src_ps = (isom_dcr_ps_entry_t *)src_entry->data;
2536 if( !src_ps )
2537 continue;
2538 uint8_t src_ps_id;
2539 if( hevc_get_ps_id( src_ps->nalUnit + HEVC_MIN_NALU_HEADER_LENGTH,
2540 src_ps->nalUnitLength - HEVC_MIN_NALU_HEADER_LENGTH,
2541 &src_ps_id, ps_type ) < 0 )
2542 return -1;
2543 lsmash_entry_t *dst_entry;
2544 for( dst_entry = dst_ps_list->head; dst_entry; dst_entry = dst_entry->next )
2546 isom_dcr_ps_entry_t *dst_ps = (isom_dcr_ps_entry_t *)dst_entry->data;
2547 if( !dst_ps )
2548 continue;
2549 uint8_t dst_ps_id;
2550 if( hevc_get_ps_id( dst_ps->nalUnit + HEVC_MIN_NALU_HEADER_LENGTH,
2551 dst_ps->nalUnitLength - HEVC_MIN_NALU_HEADER_LENGTH,
2552 &dst_ps_id, ps_type ) < 0 )
2553 return -1;
2554 if( dst_ps_id == src_ps_id )
2556 /* Replace the old parameter set with the new one. */
2557 assert( dst_entry->data != src_entry->data );
2558 isom_remove_dcr_ps( dst_ps );
2559 dst_entry->data = src_entry->data;
2560 src_entry->data = NULL;
2561 break;
2564 if( !dst_entry )
2566 /* Move the parameter set. */
2567 if( lsmash_add_entry( dst_ps_list, src_ps ) )
2568 return -1;
2569 src_entry->data = NULL;
2572 return 0;
2575 int hevc_move_pending_hvcC_param
2577 hevc_info_t *info
2580 assert( info );
2581 if( !info->hvcC_pending )
2582 return 0;
2583 /* Mark 'unused' on parameter sets within the decoder configuration record. */
2584 for( int i = 0; i < HEVC_DCR_NALU_TYPE_NUM; i++ )
2586 lsmash_entry_list_t *ps_list = hevc_get_parameter_set_list( &info->hvcC_param, i );
2587 assert( ps_list );
2588 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
2590 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
2591 if( !ps )
2592 continue;
2593 ps->unused = 1;
2596 /* Move the new parameter sets. */
2597 if( hevc_move_dcr_nalu_entry( &info->hvcC_param, &info->hvcC_param_next, HEVC_DCR_NALU_TYPE_VPS ) < 0
2598 || hevc_move_dcr_nalu_entry( &info->hvcC_param, &info->hvcC_param_next, HEVC_DCR_NALU_TYPE_SPS ) < 0
2599 || hevc_move_dcr_nalu_entry( &info->hvcC_param, &info->hvcC_param_next, HEVC_DCR_NALU_TYPE_PPS ) < 0
2600 || hevc_move_dcr_nalu_entry( &info->hvcC_param, &info->hvcC_param_next, HEVC_DCR_NALU_TYPE_PREFIX_SEI ) < 0
2601 || hevc_move_dcr_nalu_entry( &info->hvcC_param, &info->hvcC_param_next, HEVC_DCR_NALU_TYPE_SUFFIX_SEI ) < 0 )
2602 return -1;
2603 /* Move to the pending. */
2604 lsmash_hevc_parameter_arrays_t *parameter_arrays = info->hvcC_param.parameter_arrays; /* Back up parameter arrays. */
2605 info->hvcC_param = info->hvcC_param_next;
2606 info->hvcC_param.parameter_arrays = parameter_arrays;
2607 /* No pending hvcC. */
2608 lsmash_destroy_hevc_parameter_arrays( &info->hvcC_param_next );
2609 memset( &info->hvcC_param_next, 0, sizeof(lsmash_hevc_specific_parameters_t) );
2610 info->hvcC_pending = 0;
2611 return 0;
2614 int lsmash_set_hevc_array_completeness
2616 lsmash_hevc_specific_parameters_t *param,
2617 lsmash_hevc_dcr_nalu_type ps_type,
2618 int array_completeness
2621 if( hevc_alloc_parameter_arrays( param ) < 0 )
2622 return -1;
2623 hevc_parameter_array_t *ps_array = hevc_get_parameter_set_array( param, ps_type );
2624 if( !ps_array )
2625 return -1;
2626 ps_array->array_completeness = array_completeness;
2627 return 0;
2630 int lsmash_get_hevc_array_completeness
2632 lsmash_hevc_specific_parameters_t *param,
2633 lsmash_hevc_dcr_nalu_type ps_type,
2634 int *array_completeness
2637 if( hevc_alloc_parameter_arrays( param ) < 0 )
2638 return -1;
2639 hevc_parameter_array_t *ps_array = hevc_get_parameter_set_array( param, ps_type );
2640 if( !ps_array )
2641 return -1;
2642 *array_completeness = ps_array->array_completeness;
2643 return 0;
2646 static int hevc_parse_succeeded
2648 hevc_info_t *info,
2649 lsmash_hevc_specific_parameters_t *param
2652 int ret;
2653 if( info->vps.present
2654 && info->sps.present
2655 && info->pps.present )
2657 *param = info->hvcC_param;
2658 /* Avoid freeing parameter sets. */
2659 info->hvcC_param.parameter_arrays = NULL;
2660 ret = 0;
2662 else
2663 ret = -1;
2664 hevc_cleanup_parser( info );
2665 return ret;
2668 static inline int hevc_parse_failed
2670 hevc_info_t *info
2673 hevc_cleanup_parser( info );
2674 return -1;
2677 int lsmash_setup_hevc_specific_parameters_from_access_unit
2679 lsmash_hevc_specific_parameters_t *param,
2680 uint8_t *data,
2681 uint32_t data_length
2684 if( !param || !data || data_length == 0 )
2685 return -1;
2686 hevc_info_t *info = &(hevc_info_t){ { 0 } };
2687 lsmash_bs_t *bs = &(lsmash_bs_t){ 0 };
2688 if( lsmash_bs_set_empty_stream( bs, data, data_length ) < 0 )
2689 return -1;
2690 uint64_t sc_head_pos = nalu_find_first_start_code( bs );
2691 if( sc_head_pos == NALU_NO_START_CODE_FOUND )
2692 return -1;
2693 if( hevc_setup_parser( info, 1 ) < 0 )
2694 return hevc_parse_failed( info );
2695 hevc_stream_buffer_t *sb = &info->buffer;
2696 hevc_slice_info_t *slice = &info->slice;
2697 while( 1 )
2699 hevc_nalu_header_t nuh;
2700 uint64_t start_code_length;
2701 uint64_t trailing_zero_bytes;
2702 uint64_t nalu_length = hevc_find_next_start_code( bs, &nuh, &start_code_length, &trailing_zero_bytes );
2703 if( start_code_length <= NALU_SHORT_START_CODE_LENGTH && lsmash_bs_is_end( bs, nalu_length ) )
2704 /* For the last NALU. This NALU already has been parsed. */
2705 return hevc_parse_succeeded( info, param );
2706 uint8_t nalu_type = nuh.nal_unit_type;
2707 uint64_t next_sc_head_pos = sc_head_pos
2708 + start_code_length
2709 + nalu_length
2710 + trailing_zero_bytes;
2711 if( nalu_type == HEVC_NALU_TYPE_FD )
2713 /* We don't support streams with both filler and HRD yet.
2714 * Otherwise, just skip filler because elemental streams defined in 14496-15 are forbidden to use filler. */
2715 if( info->sps.vui.hrd.present )
2716 return hevc_parse_failed( info );
2718 else if( nalu_type <= HEVC_NALU_TYPE_RASL_R
2719 || (nalu_type >= HEVC_NALU_TYPE_BLA_W_LP && nalu_type <= HEVC_NALU_TYPE_CRA)
2720 || (nalu_type >= HEVC_NALU_TYPE_VPS && nalu_type <= HEVC_NALU_TYPE_SUFFIX_SEI) )
2722 /* Increase the buffer if needed. */
2723 uint64_t possible_au_length = NALU_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
2724 if( sb->bank->buffer_size < possible_au_length
2725 && hevc_supplement_buffer( sb, NULL, 2 * possible_au_length ) < 0 )
2726 return hevc_parse_failed( info );
2727 /* Get the EBSP of the current NALU here. */
2728 uint8_t *nalu = lsmash_bs_get_buffer_data( bs ) + start_code_length;
2729 if( nalu_type <= HEVC_NALU_TYPE_RSV_VCL31 )
2731 /* VCL NALU (slice) */
2732 hevc_slice_info_t prev_slice = *slice;
2733 if( hevc_parse_slice_segment_header( info, &nuh, sb->rbsp, nalu + nuh.length, nalu_length - nuh.length ) < 0 )
2734 return hevc_parse_failed( info );
2735 if( prev_slice.present )
2737 /* Check whether the AU that contains the previous VCL NALU completed or not. */
2738 if( hevc_find_au_delimit_by_slice_info( info, slice, &prev_slice ) )
2739 /* The current NALU is the first VCL NALU of the primary coded picture of a new AU.
2740 * Therefore, the previous slice belongs to that new AU. */
2741 return hevc_parse_succeeded( info, param );
2743 slice->present = 1;
2745 else
2747 if( hevc_find_au_delimit_by_nalu_type( nalu_type, info->prev_nalu_type ) )
2748 /* The last slice belongs to the AU you want at this time. */
2749 return hevc_parse_succeeded( info, param );
2750 switch( nalu_type )
2752 case HEVC_NALU_TYPE_VPS :
2753 if( hevc_try_to_append_dcr_nalu( info, HEVC_DCR_NALU_TYPE_VPS, nalu, nalu_length ) < 0 )
2754 return hevc_parse_failed( info );
2755 break;
2756 case HEVC_NALU_TYPE_SPS :
2757 if( hevc_try_to_append_dcr_nalu( info, HEVC_DCR_NALU_TYPE_SPS, nalu, nalu_length ) < 0 )
2758 return hevc_parse_failed( info );
2759 break;
2760 case HEVC_NALU_TYPE_PPS :
2761 if( hevc_try_to_append_dcr_nalu( info, HEVC_DCR_NALU_TYPE_PPS, nalu, nalu_length ) < 0 )
2762 return hevc_parse_failed( info );
2763 break;
2764 default :
2765 break;
2769 /* Move to the first byte of the next start code. */
2770 info->prev_nalu_type = nalu_type;
2771 if( lsmash_bs_read_seek( bs, next_sc_head_pos, SEEK_SET ) != next_sc_head_pos )
2772 return hevc_parse_failed( info );
2773 /* Check if no more data to read from the stream. */
2774 if( !lsmash_bs_is_end( bs, NALU_SHORT_START_CODE_LENGTH ) )
2775 sc_head_pos = next_sc_head_pos;
2776 else
2777 return hevc_parse_succeeded( info, param );
2781 int hevc_construct_specific_parameters
2783 lsmash_codec_specific_t *dst,
2784 lsmash_codec_specific_t *src
2787 assert( dst && dst->data.structured && src && src->data.unstructured );
2788 if( src->size < ISOM_BASEBOX_COMMON_SIZE + 7 )
2789 return -1;
2790 lsmash_hevc_specific_parameters_t *param = (lsmash_hevc_specific_parameters_t *)dst->data.structured;
2791 uint8_t *data = src->data.unstructured;
2792 uint64_t size = LSMASH_GET_BE32( data );
2793 data += ISOM_BASEBOX_COMMON_SIZE;
2794 if( size == 1 )
2796 size = LSMASH_GET_BE64( data );
2797 data += 8;
2799 if( size != src->size )
2800 return -1;
2801 if( hevc_alloc_parameter_arrays( param ) < 0 )
2802 return -1;
2803 lsmash_bs_t *bs = lsmash_bs_create();
2804 if( !bs )
2805 return -1;
2806 if( lsmash_bs_import_data( bs, data, src->size - (data - src->data.unstructured) ) )
2807 goto fail;
2808 if( lsmash_bs_get_byte( bs ) != HVCC_CONFIGURATION_VERSION )
2809 goto fail; /* We don't support configurationVersion other than HVCC_CONFIGURATION_VERSION. */
2810 uint8_t temp8 = lsmash_bs_get_byte( bs );
2811 param->general_profile_space = (temp8 >> 6) & 0x03;
2812 param->general_tier_flag = (temp8 >> 5) & 0x01;
2813 param->general_profile_idc = temp8 & 0x1F;
2814 param->general_profile_compatibility_flags = lsmash_bs_get_be32( bs );
2815 uint32_t temp32 = lsmash_bs_get_be32( bs );
2816 uint16_t temp16 = lsmash_bs_get_be16( bs );
2817 param->general_constraint_indicator_flags = ((uint64_t)temp32 << 16) | temp16;
2818 param->general_level_idc = lsmash_bs_get_byte( bs );
2819 param->min_spatial_segmentation_idc = lsmash_bs_get_be16( bs ) & 0x0FFF;
2820 param->parallelismType = lsmash_bs_get_byte( bs ) & 0x03;
2821 param->chromaFormat = lsmash_bs_get_byte( bs ) & 0x03;
2822 param->bitDepthLumaMinus8 = lsmash_bs_get_byte( bs ) & 0x07;
2823 param->bitDepthChromaMinus8 = lsmash_bs_get_byte( bs ) & 0x07;
2824 param->avgFrameRate = lsmash_bs_get_be16( bs );
2825 temp8 = lsmash_bs_get_byte( bs );
2826 param->constantFrameRate = (temp8 >> 6) & 0x03;
2827 param->numTemporalLayers = (temp8 >> 3) & 0x07;
2828 param->temporalIdNested = (temp8 >> 2) & 0x01;
2829 param->lengthSizeMinusOne = temp8 & 0x03;
2830 uint8_t numOfArrays = lsmash_bs_get_byte( bs );
2831 for( uint8_t i = 0; i < numOfArrays; i++ )
2833 hevc_parameter_array_t param_array;
2834 memset( &param_array, 0, sizeof(hevc_parameter_array_t) );
2835 temp8 = lsmash_bs_get_byte( bs );
2836 param_array.array_completeness = (temp8 >> 7) & 0x01;
2837 param_array.NAL_unit_type = temp8 & 0x3F;
2838 param_array.list->entry_count = lsmash_bs_get_be16( bs );
2839 if( param_array.NAL_unit_type == HEVC_NALU_TYPE_VPS
2840 || param_array.NAL_unit_type == HEVC_NALU_TYPE_SPS
2841 || param_array.NAL_unit_type == HEVC_NALU_TYPE_PPS
2842 || param_array.NAL_unit_type == HEVC_NALU_TYPE_PREFIX_SEI
2843 || param_array.NAL_unit_type == HEVC_NALU_TYPE_SUFFIX_SEI )
2845 if( nalu_get_dcr_ps( bs, param_array.list, param_array.list->entry_count ) < 0 )
2846 goto fail;
2848 else
2849 for( uint16_t j = 0; j < param_array.list->entry_count; j++ )
2851 uint16_t nalUnitLength = lsmash_bs_get_be16( bs );
2852 lsmash_bs_skip_bytes( bs, nalUnitLength ); /* nalUnit */
2854 switch( param_array.NAL_unit_type )
2856 case HEVC_NALU_TYPE_VPS :
2857 param->parameter_arrays->ps_array[HEVC_DCR_NALU_TYPE_VPS] = param_array;
2858 break;
2859 case HEVC_NALU_TYPE_SPS :
2860 param->parameter_arrays->ps_array[HEVC_DCR_NALU_TYPE_SPS] = param_array;
2861 break;
2862 case HEVC_NALU_TYPE_PPS :
2863 param->parameter_arrays->ps_array[HEVC_DCR_NALU_TYPE_PPS] = param_array;
2864 break;
2865 case HEVC_NALU_TYPE_PREFIX_SEI :
2866 param->parameter_arrays->ps_array[HEVC_DCR_NALU_TYPE_PREFIX_SEI] = param_array;
2867 break;
2868 case HEVC_NALU_TYPE_SUFFIX_SEI :
2869 param->parameter_arrays->ps_array[HEVC_DCR_NALU_TYPE_SUFFIX_SEI] = param_array;
2870 break;
2871 default :
2872 /* Discard unknown NALUs. */
2873 break;
2876 lsmash_bs_cleanup( bs );
2877 return 0;
2878 fail:
2879 lsmash_bs_cleanup( bs );
2880 return -1;
2883 int hevc_print_codec_specific
2885 FILE *fp,
2886 lsmash_file_t *file,
2887 isom_box_t *box,
2888 int level
2891 assert( fp && file && box && (box->manager & LSMASH_BINARY_CODED_BOX) );
2892 int indent = level;
2893 lsmash_ifprintf( fp, indent++, "[%s: HEVC Configuration Box]\n", isom_4cc2str( box->type.fourcc ) );
2894 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
2895 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
2896 uint8_t *data = box->binary;
2897 uint32_t offset = isom_skip_box_common( &data );
2898 lsmash_bs_t *bs = lsmash_bs_create();
2899 if( !bs )
2900 return -1;
2901 if( lsmash_bs_import_data( bs, data, box->size - offset ) )
2903 lsmash_bs_cleanup( bs );
2904 return -1;
2906 uint8_t configurationVersion = lsmash_bs_get_byte( bs );
2907 lsmash_ifprintf( fp, indent, "configurationVersion = %"PRIu8"\n", configurationVersion );
2908 if( configurationVersion != HVCC_CONFIGURATION_VERSION )
2910 lsmash_bs_cleanup( bs );
2911 return 0;
2913 uint8_t temp8 = lsmash_bs_get_byte( bs );
2914 lsmash_ifprintf( fp, indent, "general_profile_space = %"PRIu8"\n", (temp8 >> 6) & 0x03 );
2915 lsmash_ifprintf( fp, indent, "general_tier_flag = %"PRIu8"\n", (temp8 >> 5) & 0x01 );
2916 lsmash_ifprintf( fp, indent, "general_profile_idc = %"PRIu8"\n", temp8 & 0x1F );
2917 lsmash_ifprintf( fp, indent, "general_profile_compatibility_flags = 0x%08"PRIx32"\n", lsmash_bs_get_be32( bs ) );
2918 uint32_t temp32 = lsmash_bs_get_be32( bs );
2919 uint16_t temp16 = lsmash_bs_get_be16( bs );
2920 lsmash_ifprintf( fp, indent, "general_constraint_indicator_flags = 0x%012"PRIx64"\n", ((uint64_t)temp32 << 16) | temp16 );
2921 uint8_t general_level_idc = lsmash_bs_get_byte( bs );
2922 lsmash_ifprintf( fp, indent, "general_level_idc = %"PRIu8" (Level %g)\n", general_level_idc, general_level_idc / 30.0 );
2923 temp16 = lsmash_bs_get_be16( bs );
2924 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp16 >> 12) & 0x0F );
2925 lsmash_ifprintf( fp, indent, "min_spatial_segmentation_idc = %"PRIu16"\n", temp16 & 0x0FFF );
2926 temp8 = lsmash_bs_get_byte( bs );
2927 uint8_t parallelismType = temp8 & 0x03;
2928 static const char *parallelism_table[4] =
2930 "Mixed types or Unknown",
2931 "Slice based",
2932 "Tile based",
2933 "Entropy coding synchronization based / WPP: Wavefront Parallel Processing"
2935 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 2) & 0x3F );
2936 lsmash_ifprintf( fp, indent, "parallelismType = %"PRIu8" (%s)\n", parallelismType,
2937 parallelism_table[parallelismType] );
2938 temp8 = lsmash_bs_get_byte( bs );
2939 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 2) & 0x3F );
2940 lsmash_ifprintf( fp, indent, "chromaFormat = %"PRIu8"\n", temp8 & 0x03 );
2941 temp8 = lsmash_bs_get_byte( bs );
2942 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 3) & 0x1F );
2943 lsmash_ifprintf( fp, indent, "bitDepthLumaMinus8 = %"PRIu8"\n", temp8 & 0x07 );
2944 temp8 = lsmash_bs_get_byte( bs );
2945 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 3) & 0x1F );
2946 lsmash_ifprintf( fp, indent, "bitDepthChromaMinus8 = %"PRIu8"\n", temp8 & 0x07 );
2947 lsmash_ifprintf( fp, indent, "avgFrameRate = %"PRIu16"\n", lsmash_bs_get_be16( bs ) );
2948 temp8 = lsmash_bs_get_byte( bs );
2949 lsmash_ifprintf( fp, indent, "constantFrameRate = %"PRIu8"\n", (temp8 >> 6) & 0x03 );
2950 lsmash_ifprintf( fp, indent, "numTemporalLayers = %"PRIu8"\n", (temp8 >> 3) & 0x07 );
2951 lsmash_ifprintf( fp, indent, "temporalIdNested = %"PRIu8"\n", (temp8 >> 2) & 0x01 );
2952 lsmash_ifprintf( fp, indent, "lengthSizeMinusOne = %"PRIu8"\n", temp8 & 0x03 );
2953 uint8_t numOfArrays = lsmash_bs_get_byte( bs );
2954 lsmash_ifprintf( fp, indent, "numOfArrays = %"PRIu8"\n", numOfArrays );
2955 for( uint8_t i = 0; i < numOfArrays; i++ )
2957 int array_indent = indent + 1;
2958 lsmash_ifprintf( fp, array_indent++, "array[%"PRIu8"]\n", i );
2959 temp8 = lsmash_bs_get_byte( bs );
2960 lsmash_ifprintf( fp, array_indent, "array_completeness = %"PRIu8"\n", (temp8 >> 7) & 0x01 );
2961 lsmash_ifprintf( fp, array_indent, "reserved = %"PRIu8"\n", (temp8 >> 6) & 0x01 );
2962 lsmash_ifprintf( fp, array_indent, "NAL_unit_type = %"PRIu8"\n", temp8 & 0x3F );
2963 uint16_t numNalus = lsmash_bs_get_be16( bs );
2964 lsmash_ifprintf( fp, array_indent, "numNalus = %"PRIu16"\n", numNalus );
2965 for( uint16_t j = 0; j < numNalus; j++ )
2967 uint16_t nalUnitLength = lsmash_bs_get_be16( bs );
2968 lsmash_bs_skip_bytes( bs, nalUnitLength );
2971 lsmash_bs_cleanup( bs );
2972 return 0;
2975 static inline int hevc_copy_dcr_nalu_array
2977 lsmash_hevc_specific_parameters_t *dst_data,
2978 lsmash_hevc_specific_parameters_t *src_data,
2979 lsmash_hevc_dcr_nalu_type ps_type
2982 hevc_parameter_array_t *src_ps_array = hevc_get_parameter_set_array( src_data, ps_type );
2983 hevc_parameter_array_t *dst_ps_array = hevc_get_parameter_set_array( dst_data, ps_type );
2984 assert( src_ps_array && dst_ps_array );
2985 dst_ps_array->array_completeness = src_ps_array->array_completeness;
2986 dst_ps_array->NAL_unit_type = src_ps_array->NAL_unit_type;
2987 lsmash_entry_list_t *src_ps_list = src_ps_array->list;
2988 lsmash_entry_list_t *dst_ps_list = dst_ps_array->list;
2989 for( lsmash_entry_t *entry = src_ps_list->head; entry; entry = entry->next )
2991 isom_dcr_ps_entry_t *src_ps = (isom_dcr_ps_entry_t *)entry->data;
2992 if( !src_ps || src_ps->unused )
2993 continue;
2994 isom_dcr_ps_entry_t *dst_ps = isom_create_ps_entry( src_ps->nalUnit, src_ps->nalUnitLength );
2995 if( !dst_ps )
2997 lsmash_destroy_hevc_parameter_arrays( dst_data );
2998 return -1;
3000 if( lsmash_add_entry( dst_ps_list, dst_ps ) )
3002 lsmash_destroy_hevc_parameter_arrays( dst_data );
3003 isom_remove_dcr_ps( dst_ps );
3004 return -1;
3007 return 0;
3010 int hevc_copy_codec_specific
3012 lsmash_codec_specific_t *dst,
3013 lsmash_codec_specific_t *src
3016 assert( src && src->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && src->data.structured );
3017 assert( dst && dst->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && dst->data.structured );
3018 lsmash_hevc_specific_parameters_t *src_data = (lsmash_hevc_specific_parameters_t *)src->data.structured;
3019 lsmash_hevc_specific_parameters_t *dst_data = (lsmash_hevc_specific_parameters_t *)dst->data.structured;
3020 lsmash_destroy_hevc_parameter_arrays( dst_data );
3021 *dst_data = *src_data;
3022 if( !src_data->parameter_arrays )
3023 return 0;
3024 dst_data->parameter_arrays = lsmash_malloc_zero( sizeof(lsmash_hevc_parameter_arrays_t) );
3025 if( !dst_data->parameter_arrays )
3026 return -1;
3027 for( int i = 0; i < HEVC_DCR_NALU_TYPE_NUM; i++ )
3028 if( hevc_copy_dcr_nalu_array( dst_data, src_data, (lsmash_hevc_dcr_nalu_type)i ) < 0 )
3029 return -1;
3030 return 0;