write: Fix type size for mdcv luminance
[L-SMASH.git] / codecs / h264.c
blob3ae05c68b85ca5a7a91e0138f8b201181ac173c0
1 /*****************************************************************************
2 * h264.c
3 *****************************************************************************
4 * Copyright (C) 2012-2017 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
21 /* This file is available under an ISC license. */
23 #include "common/internal.h" /* must be placed first */
25 #include <string.h>
26 #include <stdlib.h>
27 #include <inttypes.h>
29 #include "core/box.h"
31 /***************************************************************************
32 ITU-T Recommendation H.264 (04/13)
33 ISO/IEC 14496-15:2010
34 ***************************************************************************/
35 #include "h264.h"
36 #include "nalu.h"
38 #define IF_EXCEED_INT32( x ) if( (x) < INT32_MIN || (x) > INT32_MAX )
39 #define H264_REQUIRES_AVCC_EXTENSION( x ) ((x) == 100 || (x) == 110 || (x) == 122 || (x) == 144)
40 #define H264_POC_DEBUG_PRINT 0
42 typedef enum
44 H264_SLICE_TYPE_P = 0,
45 H264_SLICE_TYPE_B = 1,
46 H264_SLICE_TYPE_I = 2,
47 H264_SLICE_TYPE_SP = 3,
48 H264_SLICE_TYPE_SI = 4
49 } h264_slice_type;
51 static lsmash_h264_parameter_sets_t *h264_allocate_parameter_sets( void )
53 lsmash_h264_parameter_sets_t *parameter_sets = lsmash_malloc_zero( sizeof(lsmash_h264_parameter_sets_t) );
54 if( !parameter_sets )
55 return NULL;
56 lsmash_list_init( parameter_sets->sps_list, isom_remove_dcr_ps );
57 lsmash_list_init( parameter_sets->pps_list, isom_remove_dcr_ps );
58 lsmash_list_init( parameter_sets->spsext_list, isom_remove_dcr_ps );
59 return parameter_sets;
62 static void h264_deallocate_parameter_sets
64 lsmash_h264_specific_parameters_t *param
67 if( !param || !param->parameter_sets )
68 return;
69 lsmash_list_remove_entries( param->parameter_sets->sps_list );
70 lsmash_list_remove_entries( param->parameter_sets->pps_list );
71 lsmash_list_remove_entries( param->parameter_sets->spsext_list );
72 lsmash_freep( &param->parameter_sets );
75 void lsmash_destroy_h264_parameter_sets
77 lsmash_h264_specific_parameters_t *param
80 h264_deallocate_parameter_sets( param );
83 void h264_destruct_specific_data
85 void *data
88 if( !data )
89 return;
90 h264_deallocate_parameter_sets( data );
91 lsmash_free( data );
94 void h264_cleanup_parser
96 h264_info_t *info
99 if( !info )
100 return;
101 lsmash_list_remove_entries( info->sps_list );
102 lsmash_list_remove_entries( info->pps_list );
103 lsmash_list_remove_entries( info->slice_list );
104 h264_deallocate_parameter_sets( &info->avcC_param );
105 h264_deallocate_parameter_sets( &info->avcC_param_next );
106 lsmash_destroy_multiple_buffers( info->buffer.bank );
107 lsmash_bits_adhoc_cleanup( info->bits );
108 info->bits = NULL;
111 int h264_setup_parser
113 h264_info_t *info,
114 int parse_only
117 assert( info );
118 memset( info, 0, sizeof(h264_info_t) );
119 info->avcC_param .lengthSizeMinusOne = NALU_DEFAULT_NALU_LENGTH_SIZE - 1;
120 info->avcC_param_next.lengthSizeMinusOne = NALU_DEFAULT_NALU_LENGTH_SIZE - 1;
121 h264_stream_buffer_t *sb = &info->buffer;
122 sb->bank = lsmash_create_multiple_buffers( parse_only ? 1 : 3, NALU_DEFAULT_BUFFER_SIZE );
123 if( !sb->bank )
124 return LSMASH_ERR_MEMORY_ALLOC;
125 sb->rbsp = lsmash_withdraw_buffer( sb->bank, 1 );
126 if( !parse_only )
128 info->au.data = lsmash_withdraw_buffer( sb->bank, 2 );
129 info->au.incomplete_data = lsmash_withdraw_buffer( sb->bank, 3 );
131 info->bits = lsmash_bits_adhoc_create();
132 if( !info->bits )
134 lsmash_destroy_multiple_buffers( sb->bank );
135 return LSMASH_ERR_MEMORY_ALLOC;
137 lsmash_list_init_simple( info->sps_list );
138 lsmash_list_init_simple( info->pps_list );
139 lsmash_list_init_simple( info->slice_list );
140 return 0;
143 static int h264_check_nalu_header
145 lsmash_bs_t *bs,
146 h264_nalu_header_t *nuh,
147 int use_long_start_code
150 uint8_t temp8 = lsmash_bs_show_byte( bs, use_long_start_code ? NALU_LONG_START_CODE_LENGTH : NALU_SHORT_START_CODE_LENGTH );
151 nuh->forbidden_zero_bit = (temp8 >> 7) & 0x01;
152 nuh->nal_ref_idc = (temp8 >> 5) & 0x03;
153 nuh->nal_unit_type = temp8 & 0x1f;
154 nuh->length = 1;
155 if( nuh->nal_unit_type == H264_NALU_TYPE_PREFIX
156 || nuh->nal_unit_type == H264_NALU_TYPE_SLICE_EXT
157 || nuh->nal_unit_type == H264_NALU_TYPE_SLICE_EXT_DVC )
159 /* We don't support these types of NALU. */
160 //nuh->length += 3;
161 return LSMASH_ERR_PATCH_WELCOME;
163 if( nuh->forbidden_zero_bit )
164 return LSMASH_ERR_INVALID_DATA;
165 /* SPS and PPS require long start code (0x00000001).
166 * Also AU delimiter requires it too because this type of NALU shall be the first NALU of any AU if present. */
167 if( !use_long_start_code
168 && (nuh->nal_unit_type == H264_NALU_TYPE_SPS
169 || nuh->nal_unit_type == H264_NALU_TYPE_PPS
170 || nuh->nal_unit_type == H264_NALU_TYPE_AUD) )
171 return LSMASH_ERR_INVALID_DATA;
172 if( nuh->nal_ref_idc )
174 /* nal_ref_idc shall be equal to 0 for all NALUs having nal_unit_type equal to 6, 9, 10, 11, or 12. */
175 if( nuh->nal_unit_type == H264_NALU_TYPE_SEI
176 || nuh->nal_unit_type == H264_NALU_TYPE_AUD
177 || nuh->nal_unit_type == H264_NALU_TYPE_EOS
178 || nuh->nal_unit_type == H264_NALU_TYPE_EOB
179 || nuh->nal_unit_type == H264_NALU_TYPE_FD )
180 return LSMASH_ERR_INVALID_DATA;
182 else
183 /* nal_ref_idc shall not be equal to 0 for NALUs with nal_unit_type equal to 5. */
184 if( nuh->nal_unit_type == H264_NALU_TYPE_SLICE_IDR )
185 return LSMASH_ERR_INVALID_DATA;
186 return 0;
189 uint64_t h264_find_next_start_code
191 lsmash_bs_t *bs,
192 h264_nalu_header_t *nuh,
193 uint64_t *start_code_length,
194 uint64_t *trailing_zero_bytes
197 uint64_t length = 0; /* the length of the latest NALU */
198 uint64_t count = 0; /* the number of the trailing zero bytes after the latest NALU */
199 /* Check the type of the current start code. */
200 int long_start_code
201 = (!lsmash_bs_is_end( bs, NALU_LONG_START_CODE_LENGTH ) && 0x00000001 == lsmash_bs_show_be32( bs, 0 )) ? 1
202 : (!lsmash_bs_is_end( bs, NALU_SHORT_START_CODE_LENGTH ) && 0x000001 == lsmash_bs_show_be24( bs, 0 )) ? 0
203 : -1;
204 if( long_start_code >= 0 && h264_check_nalu_header( bs, nuh, long_start_code ) == 0 )
206 *start_code_length = long_start_code ? NALU_LONG_START_CODE_LENGTH : NALU_SHORT_START_CODE_LENGTH;
207 uint64_t distance = *start_code_length + nuh->length;
208 /* Find the start code of the next NALU and get the distance from the start code of the latest NALU. */
209 if( !lsmash_bs_is_end( bs, distance + NALU_SHORT_START_CODE_LENGTH ) )
211 uint32_t sync_bytes = lsmash_bs_show_be24( bs, distance );
212 while( 0x000001 != sync_bytes )
214 if( lsmash_bs_is_end( bs, ++distance + NALU_SHORT_START_CODE_LENGTH ) )
216 distance = lsmash_bs_get_remaining_buffer_size( bs );
217 break;
219 sync_bytes <<= 8;
220 sync_bytes |= lsmash_bs_show_byte( bs, distance + NALU_SHORT_START_CODE_LENGTH - 1 );
221 sync_bytes &= 0xFFFFFF;
224 else
225 distance = lsmash_bs_get_remaining_buffer_size( bs );
226 /* Any NALU has no consecutive zero bytes at the end. */
227 while( 0x00 == lsmash_bs_show_byte( bs, distance - 1 ) )
229 --distance;
230 ++count;
232 /* Remove the length of the start code. */
233 length = distance - *start_code_length;
234 /* If there are one or more trailing zero bytes, we treat the last one byte as a part of the next start code.
235 * This makes the next start code a long start code. */
236 if( count )
237 --count;
239 else
241 /* No start code. */
242 nuh->forbidden_zero_bit = 1; /* shall be 0, so invalid */
243 nuh->nal_ref_idc = 0; /* arbitrary */
244 nuh->nal_unit_type = H264_NALU_TYPE_UNSPECIFIED0;
245 nuh->length = 0;
246 *start_code_length = 0;
247 length = NALU_NO_START_CODE_FOUND;
249 *trailing_zero_bytes = count;
250 return length;
253 static h264_sps_t *h264_get_sps
255 lsmash_entry_list_t *sps_list,
256 uint8_t sps_id
259 if( !sps_list || sps_id > 31 )
260 return NULL;
261 for( lsmash_entry_t *entry = sps_list->head; entry; entry = entry->next )
263 h264_sps_t *sps = (h264_sps_t *)entry->data;
264 if( !sps )
265 return NULL;
266 if( sps->seq_parameter_set_id == sps_id )
267 return sps;
269 h264_sps_t *sps = lsmash_malloc_zero( sizeof(h264_sps_t) );
270 if( !sps )
271 return NULL;
272 sps->seq_parameter_set_id = sps_id;
273 if( lsmash_list_add_entry( sps_list, sps ) < 0 )
275 lsmash_free( sps );
276 return NULL;
278 return sps;
281 static h264_pps_t *h264_get_pps
283 lsmash_entry_list_t *pps_list,
284 uint8_t pps_id
287 if( !pps_list )
288 return NULL;
289 for( lsmash_entry_t *entry = pps_list->head; entry; entry = entry->next )
291 h264_pps_t *pps = (h264_pps_t *)entry->data;
292 if( !pps )
293 return NULL;
294 if( pps->pic_parameter_set_id == pps_id )
295 return pps;
297 h264_pps_t *pps = lsmash_malloc_zero( sizeof(h264_pps_t) );
298 if( !pps )
299 return NULL;
300 pps->pic_parameter_set_id = pps_id;
301 if( lsmash_list_add_entry( pps_list, pps ) < 0 )
303 lsmash_free( pps );
304 return NULL;
306 return pps;
309 static h264_slice_info_t *h264_get_slice_info
311 lsmash_entry_list_t *slice_list,
312 uint8_t slice_id
315 if( !slice_list )
316 return NULL;
317 for( lsmash_entry_t *entry = slice_list->head; entry; entry = entry->next )
319 h264_slice_info_t *slice = (h264_slice_info_t *)entry->data;
320 if( !slice )
321 return NULL;
322 if( slice->slice_id == slice_id )
323 return slice;
325 h264_slice_info_t *slice = lsmash_malloc_zero( sizeof(h264_slice_info_t) );
326 if( !slice )
327 return NULL;
328 slice->slice_id = slice_id;
329 if( lsmash_list_add_entry( slice_list, slice ) < 0 )
331 lsmash_free( slice );
332 return NULL;
334 return slice;
337 int h264_calculate_poc
339 h264_info_t *info,
340 h264_picture_info_t *picture,
341 h264_picture_info_t *prev_picture
344 #if H264_POC_DEBUG_PRINT
345 fprintf( stderr, "PictureOrderCount\n" );
346 #endif
347 h264_pps_t *pps = h264_get_pps( info->pps_list, picture->pic_parameter_set_id );
348 if( !pps )
349 return LSMASH_ERR_NAMELESS;
350 h264_sps_t *sps = h264_get_sps( info->sps_list, pps->seq_parameter_set_id );
351 if( !sps )
352 return LSMASH_ERR_NAMELESS;
353 int64_t TopFieldOrderCnt = 0;
354 int64_t BottomFieldOrderCnt = 0;
355 if( sps->pic_order_cnt_type == 0 )
357 int32_t prevPicOrderCntMsb;
358 int32_t prevPicOrderCntLsb;
359 if( picture->idr )
361 prevPicOrderCntMsb = 0;
362 prevPicOrderCntLsb = 0;
364 else if( prev_picture->ref_pic_has_mmco5 )
366 prevPicOrderCntMsb = 0;
367 prevPicOrderCntLsb = prev_picture->ref_pic_bottom_field_flag ? 0 : prev_picture->ref_pic_TopFieldOrderCnt;
369 else
371 prevPicOrderCntMsb = prev_picture->ref_pic_PicOrderCntMsb;
372 prevPicOrderCntLsb = prev_picture->ref_pic_PicOrderCntLsb;
374 int64_t PicOrderCntMsb;
375 int32_t pic_order_cnt_lsb = picture->pic_order_cnt_lsb;
376 uint64_t MaxPicOrderCntLsb = sps->MaxPicOrderCntLsb;
377 if( (pic_order_cnt_lsb < prevPicOrderCntLsb)
378 && ((prevPicOrderCntLsb - pic_order_cnt_lsb) >= (MaxPicOrderCntLsb / 2)) )
379 PicOrderCntMsb = prevPicOrderCntMsb + MaxPicOrderCntLsb;
380 else if( (pic_order_cnt_lsb > prevPicOrderCntLsb)
381 && ((pic_order_cnt_lsb - prevPicOrderCntLsb) > (MaxPicOrderCntLsb / 2)) )
382 PicOrderCntMsb = prevPicOrderCntMsb - MaxPicOrderCntLsb;
383 else
384 PicOrderCntMsb = prevPicOrderCntMsb;
385 IF_EXCEED_INT32( PicOrderCntMsb )
386 return LSMASH_ERR_INVALID_DATA;
387 BottomFieldOrderCnt = TopFieldOrderCnt = PicOrderCntMsb + pic_order_cnt_lsb;
388 if( !picture->field_pic_flag )
389 BottomFieldOrderCnt += picture->delta_pic_order_cnt_bottom;
390 IF_EXCEED_INT32( TopFieldOrderCnt )
391 return LSMASH_ERR_INVALID_DATA;
392 IF_EXCEED_INT32( BottomFieldOrderCnt )
393 return LSMASH_ERR_INVALID_DATA;
394 if( !picture->disposable )
396 picture->ref_pic_has_mmco5 = picture->has_mmco5;
397 picture->ref_pic_bottom_field_flag = picture->bottom_field_flag;
398 picture->ref_pic_TopFieldOrderCnt = TopFieldOrderCnt;
399 picture->ref_pic_PicOrderCntMsb = PicOrderCntMsb;
400 picture->ref_pic_PicOrderCntLsb = pic_order_cnt_lsb;
402 #if H264_POC_DEBUG_PRINT
403 fprintf( stderr, " prevPicOrderCntMsb: %"PRId32"\n", prevPicOrderCntMsb );
404 fprintf( stderr, " prevPicOrderCntLsb: %"PRId32"\n", prevPicOrderCntLsb );
405 fprintf( stderr, " PicOrderCntMsb: %"PRId64"\n", PicOrderCntMsb );
406 fprintf( stderr, " pic_order_cnt_lsb: %"PRId32"\n", pic_order_cnt_lsb );
407 fprintf( stderr, " MaxPicOrderCntLsb: %"PRIu64"\n", MaxPicOrderCntLsb );
408 #endif
410 else if( sps->pic_order_cnt_type == 1 )
412 uint32_t frame_num = picture->frame_num;
413 uint32_t prevFrameNum = prev_picture->has_mmco5 ? 0 : prev_picture->frame_num;
414 uint32_t prevFrameNumOffset = prev_picture->has_mmco5 ? 0 : prev_picture->FrameNumOffset;
415 uint64_t FrameNumOffset = picture->idr ? 0 : prevFrameNumOffset + (prevFrameNum > frame_num ? sps->MaxFrameNum : 0);
416 if( FrameNumOffset > INT32_MAX )
417 return LSMASH_ERR_INVALID_DATA;
418 int64_t expectedPicOrderCnt;
419 if( sps->num_ref_frames_in_pic_order_cnt_cycle )
421 uint64_t absFrameNum = FrameNumOffset + frame_num;
422 absFrameNum -= picture->disposable && absFrameNum > 0;
423 if( absFrameNum )
425 uint64_t picOrderCntCycleCnt = (absFrameNum - 1) / sps->num_ref_frames_in_pic_order_cnt_cycle;
426 uint8_t frameNumInPicOrderCntCycle = (absFrameNum - 1) % sps->num_ref_frames_in_pic_order_cnt_cycle;
427 expectedPicOrderCnt = picOrderCntCycleCnt * sps->ExpectedDeltaPerPicOrderCntCycle;
428 for( uint8_t i = 0; i <= frameNumInPicOrderCntCycle; i++ )
429 expectedPicOrderCnt += sps->offset_for_ref_frame[i];
431 else
432 expectedPicOrderCnt = 0;
434 else
435 expectedPicOrderCnt = 0;
436 if( picture->disposable )
437 expectedPicOrderCnt += sps->offset_for_non_ref_pic;
438 TopFieldOrderCnt = expectedPicOrderCnt + picture->delta_pic_order_cnt[0];
439 BottomFieldOrderCnt = TopFieldOrderCnt + sps->offset_for_top_to_bottom_field;
440 if( !picture->field_pic_flag )
441 BottomFieldOrderCnt += picture->delta_pic_order_cnt[1];
442 IF_EXCEED_INT32( TopFieldOrderCnt )
443 return LSMASH_ERR_INVALID_DATA;
444 IF_EXCEED_INT32( BottomFieldOrderCnt )
445 return LSMASH_ERR_INVALID_DATA;
446 picture->FrameNumOffset = FrameNumOffset;
448 else if( sps->pic_order_cnt_type == 2 )
450 uint32_t frame_num = picture->frame_num;
451 uint32_t prevFrameNum = prev_picture->has_mmco5 ? 0 : prev_picture->frame_num;
452 int32_t prevFrameNumOffset = prev_picture->has_mmco5 ? 0 : prev_picture->FrameNumOffset;
453 int64_t FrameNumOffset;
454 int64_t tempPicOrderCnt;
455 if( picture->idr )
457 FrameNumOffset = 0;
458 tempPicOrderCnt = 0;
460 else
462 FrameNumOffset = prevFrameNumOffset + (prevFrameNum > frame_num ? sps->MaxFrameNum : 0);
463 tempPicOrderCnt = 2 * (FrameNumOffset + frame_num) - picture->disposable;
464 IF_EXCEED_INT32( FrameNumOffset )
465 return LSMASH_ERR_INVALID_DATA;
466 IF_EXCEED_INT32( tempPicOrderCnt )
467 return LSMASH_ERR_INVALID_DATA;
469 TopFieldOrderCnt = tempPicOrderCnt;
470 BottomFieldOrderCnt = tempPicOrderCnt;
471 picture->FrameNumOffset = FrameNumOffset;
473 if( !picture->field_pic_flag )
474 picture->PicOrderCnt = LSMASH_MIN( TopFieldOrderCnt, BottomFieldOrderCnt );
475 else
476 picture->PicOrderCnt = picture->bottom_field_flag ? BottomFieldOrderCnt : TopFieldOrderCnt;
477 #if H264_POC_DEBUG_PRINT
478 if( picture->field_pic_flag )
480 if( !picture->bottom_field_flag )
481 fprintf( stderr, " TopFieldOrderCnt: %"PRId64"\n", TopFieldOrderCnt );
482 else
483 fprintf( stderr, " BottomFieldOrderCnt: %"PRId64"\n", BottomFieldOrderCnt );
485 fprintf( stderr, " POC: %"PRId32"\n", picture->PicOrderCnt );
486 #endif
487 return 0;
490 static int h264_parse_scaling_list
492 lsmash_bits_t *bits,
493 int sizeOfScalingList
496 /* scaling_list( scalingList, sizeOfScalingList, useDefaultScalingMatrixFlag ) */
497 int nextScale = 8;
498 for( int i = 0; i < sizeOfScalingList; i++ )
500 int64_t delta_scale = nalu_get_exp_golomb_se( bits );
501 if( delta_scale < -128 || delta_scale > 127 )
502 return LSMASH_ERR_INVALID_DATA;
503 nextScale = (nextScale + delta_scale + 256) % 256;
504 if( nextScale == 0 )
505 break;
507 return 0;
510 static int h264_parse_hrd_parameters
512 lsmash_bits_t *bits,
513 h264_hrd_t *hrd
516 /* hrd_parameters() */
517 uint64_t cpb_cnt_minus1 = nalu_get_exp_golomb_ue( bits );
518 if( cpb_cnt_minus1 > 31 )
519 return LSMASH_ERR_INVALID_DATA;
520 lsmash_bits_get( bits, 4 ); /* bit_rate_scale */
521 lsmash_bits_get( bits, 4 ); /* cpb_size_scale */
522 for( uint64_t SchedSelIdx = 0; SchedSelIdx <= cpb_cnt_minus1; SchedSelIdx++ )
524 nalu_get_exp_golomb_ue( bits ); /* bit_rate_value_minus1[ SchedSelIdx ] */
525 nalu_get_exp_golomb_ue( bits ); /* cpb_size_value_minus1[ SchedSelIdx ] */
526 lsmash_bits_get( bits, 1 ); /* cbr_flag [ SchedSelIdx ] */
528 lsmash_bits_get( bits, 5 ); /* initial_cpb_removal_delay_length_minus1 */
529 hrd->cpb_removal_delay_length = lsmash_bits_get( bits, 5 ) + 1;
530 hrd->dpb_output_delay_length = lsmash_bits_get( bits, 5 ) + 1;
531 lsmash_bits_get( bits, 5 ); /* time_offset_length */
532 return 0;
535 static int h264_parse_sps_minimally
537 lsmash_bits_t *bits,
538 h264_sps_t *sps,
539 uint8_t *rbsp_buffer,
540 uint8_t *ebsp,
541 uint64_t ebsp_size
544 uint64_t rbsp_size;
545 int err = nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, &rbsp_size, ebsp, ebsp_size );
546 if( err < 0 )
547 return err;
548 memset( sps, 0, sizeof(h264_sps_t) );
549 sps->profile_idc = lsmash_bits_get( bits, 8 );
550 sps->constraint_set_flags = lsmash_bits_get( bits, 8 );
551 sps->level_idc = lsmash_bits_get( bits, 8 );
552 uint64_t seq_parameter_set_id = nalu_get_exp_golomb_ue( bits );
553 if( seq_parameter_set_id > 31 )
554 return LSMASH_ERR_INVALID_DATA;
555 sps->seq_parameter_set_id = seq_parameter_set_id;
556 if( sps->profile_idc == 100 || sps->profile_idc == 110 || sps->profile_idc == 122
557 || sps->profile_idc == 244 || sps->profile_idc == 44 || sps->profile_idc == 83
558 || sps->profile_idc == 86 || sps->profile_idc == 118 || sps->profile_idc == 128
559 || sps->profile_idc == 138 )
561 sps->chroma_format_idc = nalu_get_exp_golomb_ue( bits );
562 if( sps->chroma_format_idc == 3 )
563 sps->separate_colour_plane_flag = lsmash_bits_get( bits, 1 );
564 uint64_t bit_depth_luma_minus8 = nalu_get_exp_golomb_ue( bits );
565 if( bit_depth_luma_minus8 > 6 )
566 return LSMASH_ERR_INVALID_DATA;
567 uint64_t bit_depth_chroma_minus8 = nalu_get_exp_golomb_ue( bits );
568 if( bit_depth_chroma_minus8 > 6 )
569 return LSMASH_ERR_INVALID_DATA;
570 sps->bit_depth_luma_minus8 = bit_depth_luma_minus8;
571 sps->bit_depth_chroma_minus8 = bit_depth_chroma_minus8;
572 lsmash_bits_get( bits, 1 ); /* qpprime_y_zero_transform_bypass_flag */
573 if( lsmash_bits_get( bits, 1 ) ) /* seq_scaling_matrix_present_flag */
575 int num_loops = sps->chroma_format_idc != 3 ? 8 : 12;
576 for( int i = 0; i < num_loops; i++ )
577 if( lsmash_bits_get( bits, 1 ) /* seq_scaling_list_present_flag[i] */
578 && (err = h264_parse_scaling_list( bits, i < 6 ? 16 : 64 )) < 0 )
579 return err;
582 else
584 sps->chroma_format_idc = 1;
585 sps->separate_colour_plane_flag = 0;
586 sps->bit_depth_luma_minus8 = 0;
587 sps->bit_depth_chroma_minus8 = 0;
589 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
592 int h264_parse_sps
594 h264_info_t *info,
595 uint8_t *rbsp_buffer,
596 uint8_t *ebsp,
597 uint64_t ebsp_size
600 lsmash_bits_t *bits = info->bits;
601 /* seq_parameter_set_data() */
602 h264_sps_t temp_sps;
603 int err = h264_parse_sps_minimally( bits, &temp_sps, rbsp_buffer, ebsp, ebsp_size );
604 if( err < 0 )
605 return err;
606 h264_sps_t *sps = h264_get_sps( info->sps_list, temp_sps.seq_parameter_set_id );
607 if( !sps )
608 return LSMASH_ERR_NAMELESS;
609 memset( sps, 0, sizeof(h264_sps_t) );
610 sps->profile_idc = temp_sps.profile_idc;
611 sps->constraint_set_flags = temp_sps.constraint_set_flags;
612 sps->level_idc = temp_sps.level_idc;
613 sps->seq_parameter_set_id = temp_sps.seq_parameter_set_id;
614 sps->chroma_format_idc = temp_sps.chroma_format_idc;
615 sps->separate_colour_plane_flag = temp_sps.separate_colour_plane_flag;
616 sps->bit_depth_luma_minus8 = temp_sps.bit_depth_luma_minus8;
617 sps->bit_depth_chroma_minus8 = temp_sps.bit_depth_chroma_minus8;
618 sps->ChromaArrayType = sps->separate_colour_plane_flag ? 0 : sps->chroma_format_idc;
619 uint64_t log2_max_frame_num_minus4 = nalu_get_exp_golomb_ue( bits );
620 if( log2_max_frame_num_minus4 > 12 )
621 return LSMASH_ERR_INVALID_DATA;
622 sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4;
623 sps->MaxFrameNum = 1 << sps->log2_max_frame_num;
624 uint64_t pic_order_cnt_type = nalu_get_exp_golomb_ue( bits );
625 if( pic_order_cnt_type > 2 )
626 return LSMASH_ERR_INVALID_DATA;
627 sps->pic_order_cnt_type = pic_order_cnt_type;
628 if( sps->pic_order_cnt_type == 0 )
630 uint64_t log2_max_pic_order_cnt_lsb_minus4 = nalu_get_exp_golomb_ue( bits );
631 if( log2_max_pic_order_cnt_lsb_minus4 > 12 )
632 return LSMASH_ERR_INVALID_DATA;
633 sps->log2_max_pic_order_cnt_lsb = log2_max_pic_order_cnt_lsb_minus4 + 4;
634 sps->MaxPicOrderCntLsb = 1 << sps->log2_max_pic_order_cnt_lsb;
636 else if( sps->pic_order_cnt_type == 1 )
638 sps->delta_pic_order_always_zero_flag = lsmash_bits_get( bits, 1 );
639 static const int64_t max_value = (signed)(((uint64_t)1 << 31) - 1);
640 static const int64_t min_value = -(signed)(((uint64_t)1 << 31) - 1);
641 int64_t offset_for_non_ref_pic = nalu_get_exp_golomb_se( bits );
642 if( offset_for_non_ref_pic < min_value || offset_for_non_ref_pic > max_value )
643 return LSMASH_ERR_INVALID_DATA;
644 sps->offset_for_non_ref_pic = offset_for_non_ref_pic;
645 int64_t offset_for_top_to_bottom_field = nalu_get_exp_golomb_se( bits );
646 if( offset_for_top_to_bottom_field < min_value || offset_for_top_to_bottom_field > max_value )
647 return LSMASH_ERR_INVALID_DATA;
648 sps->offset_for_top_to_bottom_field = offset_for_top_to_bottom_field;
649 uint64_t num_ref_frames_in_pic_order_cnt_cycle = nalu_get_exp_golomb_ue( bits );
650 if( num_ref_frames_in_pic_order_cnt_cycle > 255 )
651 return LSMASH_ERR_INVALID_DATA;
652 sps->num_ref_frames_in_pic_order_cnt_cycle = num_ref_frames_in_pic_order_cnt_cycle;
653 sps->ExpectedDeltaPerPicOrderCntCycle = 0;
654 for( int i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++ )
656 int64_t offset_for_ref_frame = nalu_get_exp_golomb_se( bits );
657 if( offset_for_ref_frame < min_value || offset_for_ref_frame > max_value )
658 return LSMASH_ERR_INVALID_DATA;
659 sps->offset_for_ref_frame[i] = offset_for_ref_frame;
660 sps->ExpectedDeltaPerPicOrderCntCycle += offset_for_ref_frame;
663 sps->max_num_ref_frames = nalu_get_exp_golomb_ue( bits );
664 lsmash_bits_get( bits, 1 ); /* gaps_in_frame_num_value_allowed_flag */
665 uint64_t pic_width_in_mbs_minus1 = nalu_get_exp_golomb_ue( bits );
666 uint64_t pic_height_in_map_units_minus1 = nalu_get_exp_golomb_ue( bits );
667 sps->frame_mbs_only_flag = lsmash_bits_get( bits, 1 );
668 if( !sps->frame_mbs_only_flag )
669 lsmash_bits_get( bits, 1 ); /* mb_adaptive_frame_field_flag */
670 lsmash_bits_get( bits, 1 ); /* direct_8x8_inference_flag */
671 uint64_t PicWidthInMbs = pic_width_in_mbs_minus1 + 1;
672 uint64_t PicHeightInMapUnits = pic_height_in_map_units_minus1 + 1;
673 sps->PicSizeInMapUnits = PicWidthInMbs * PicHeightInMapUnits;
674 sps->cropped_width = PicWidthInMbs * 16;
675 sps->cropped_height = (2 - sps->frame_mbs_only_flag) * PicHeightInMapUnits * 16;
676 if( lsmash_bits_get( bits, 1 ) ) /* frame_cropping_flag */
678 uint8_t CropUnitX;
679 uint8_t CropUnitY;
680 if( sps->ChromaArrayType == 0 )
682 CropUnitX = 1;
683 CropUnitY = 2 - sps->frame_mbs_only_flag;
685 else
687 static const int SubWidthC [] = { 0, 2, 2, 1 };
688 static const int SubHeightC[] = { 0, 2, 1, 1 };
689 CropUnitX = SubWidthC [ sps->chroma_format_idc ];
690 CropUnitY = SubHeightC[ sps->chroma_format_idc ] * (2 - sps->frame_mbs_only_flag);
692 uint64_t frame_crop_left_offset = nalu_get_exp_golomb_ue( bits );
693 uint64_t frame_crop_right_offset = nalu_get_exp_golomb_ue( bits );
694 uint64_t frame_crop_top_offset = nalu_get_exp_golomb_ue( bits );
695 uint64_t frame_crop_bottom_offset = nalu_get_exp_golomb_ue( bits );
696 sps->cropped_width -= (frame_crop_left_offset + frame_crop_right_offset) * CropUnitX;
697 sps->cropped_height -= (frame_crop_top_offset + frame_crop_bottom_offset) * CropUnitY;
699 if( lsmash_bits_get( bits, 1 ) ) /* vui_parameters_present_flag */
701 /* vui_parameters() */
702 if( lsmash_bits_get( bits, 1 ) ) /* aspect_ratio_info_present_flag */
704 uint8_t aspect_ratio_idc = lsmash_bits_get( bits, 8 );
705 if( aspect_ratio_idc == 255 )
707 /* Extended_SAR */
708 sps->vui.sar_width = lsmash_bits_get( bits, 16 );
709 sps->vui.sar_height = lsmash_bits_get( bits, 16 );
711 else
713 static const struct
715 uint16_t sar_width;
716 uint16_t sar_height;
717 } pre_defined_sar[]
719 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
720 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 }, { 80, 33 },
721 { 18, 11 }, { 15, 11 }, { 64, 33 }, { 160, 99 }, { 4, 3 },
722 { 3, 2 }, { 2, 1 }
724 if( aspect_ratio_idc < (sizeof(pre_defined_sar) / sizeof(pre_defined_sar[0])) )
726 sps->vui.sar_width = pre_defined_sar[ aspect_ratio_idc ].sar_width;
727 sps->vui.sar_height = pre_defined_sar[ aspect_ratio_idc ].sar_height;
729 else
731 /* Behavior when unknown aspect_ratio_idc is detected is not specified in the specification. */
732 sps->vui.sar_width = 0;
733 sps->vui.sar_height = 0;
737 if( lsmash_bits_get( bits, 1 ) ) /* overscan_info_present_flag */
738 lsmash_bits_get( bits, 1 ); /* overscan_appropriate_flag */
739 if( lsmash_bits_get( bits, 1 ) ) /* video_signal_type_present_flag */
741 lsmash_bits_get( bits, 3 ); /* video_format */
742 sps->vui.video_full_range_flag = lsmash_bits_get( bits, 1 );
743 if( lsmash_bits_get( bits, 1 ) ) /* colour_description_present_flag */
745 sps->vui.colour_primaries = lsmash_bits_get( bits, 8 );
746 sps->vui.transfer_characteristics = lsmash_bits_get( bits, 8 );
747 sps->vui.matrix_coefficients = lsmash_bits_get( bits, 8 );
750 if( lsmash_bits_get( bits, 1 ) ) /* chroma_loc_info_present_flag */
752 nalu_get_exp_golomb_ue( bits ); /* chroma_sample_loc_type_top_field */
753 nalu_get_exp_golomb_ue( bits ); /* chroma_sample_loc_type_bottom_field */
755 if( lsmash_bits_get( bits, 1 ) ) /* timing_info_present_flag */
757 sps->vui.num_units_in_tick = lsmash_bits_get( bits, 32 );
758 sps->vui.time_scale = lsmash_bits_get( bits, 32 );
759 sps->vui.fixed_frame_rate_flag = lsmash_bits_get( bits, 1 );
761 else
763 sps->vui.num_units_in_tick = 1; /* arbitrary */
764 sps->vui.time_scale = 50; /* arbitrary */
765 sps->vui.fixed_frame_rate_flag = 0;
767 int nal_hrd_parameters_present_flag = lsmash_bits_get( bits, 1 );
768 if( nal_hrd_parameters_present_flag
769 && (err = h264_parse_hrd_parameters( bits, &sps->vui.hrd )) < 0 )
770 return err;
771 int vcl_hrd_parameters_present_flag = lsmash_bits_get( bits, 1 );
772 if( vcl_hrd_parameters_present_flag
773 && (err = h264_parse_hrd_parameters( bits, &sps->vui.hrd )) < 0 )
774 return err;
775 if( nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag )
777 sps->vui.hrd.present = 1;
778 sps->vui.hrd.CpbDpbDelaysPresentFlag = 1;
779 lsmash_bits_get( bits, 1 ); /* low_delay_hrd_flag */
781 sps->vui.pic_struct_present_flag = lsmash_bits_get( bits, 1 );
782 if( lsmash_bits_get( bits, 1 ) ) /* bitstream_restriction_flag */
784 lsmash_bits_get( bits, 1 ); /* motion_vectors_over_pic_boundaries_flag */
785 nalu_get_exp_golomb_ue( bits ); /* max_bytes_per_pic_denom */
786 nalu_get_exp_golomb_ue( bits ); /* max_bits_per_mb_denom */
787 nalu_get_exp_golomb_ue( bits ); /* log2_max_mv_length_horizontal */
788 nalu_get_exp_golomb_ue( bits ); /* log2_max_mv_length_vertical */
789 nalu_get_exp_golomb_ue( bits ); /* max_num_reorder_frames */
790 nalu_get_exp_golomb_ue( bits ); /* max_dec_frame_buffering */
793 else
795 sps->vui.video_full_range_flag = 0;
796 sps->vui.num_units_in_tick = 1; /* arbitrary */
797 sps->vui.time_scale = 50; /* arbitrary */
798 sps->vui.fixed_frame_rate_flag = 0;
800 /* rbsp_trailing_bits() */
801 if( !lsmash_bits_get( bits, 1 ) ) /* rbsp_stop_one_bit */
802 return LSMASH_ERR_INVALID_DATA;
803 lsmash_bits_empty( bits );
804 if( bits->bs->error )
805 return LSMASH_ERR_NAMELESS;
806 sps->present = 1;
807 info->sps = *sps;
808 return 0;
811 static int h264_parse_pps_minimally
813 lsmash_bits_t *bits,
814 h264_pps_t *pps,
815 uint8_t *rbsp_buffer,
816 uint8_t *ebsp,
817 uint64_t ebsp_size
820 uint64_t rbsp_size;
821 int err = nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, &rbsp_size, ebsp, ebsp_size );
822 if( err < 0 )
823 return err;
824 memset( pps, 0, sizeof(h264_pps_t) );
825 uint64_t pic_parameter_set_id = nalu_get_exp_golomb_ue( bits );
826 if( pic_parameter_set_id > 255 )
827 return LSMASH_ERR_INVALID_DATA;
828 pps->pic_parameter_set_id = pic_parameter_set_id;
829 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
832 int h264_parse_pps
834 h264_info_t *info,
835 uint8_t *rbsp_buffer,
836 uint8_t *ebsp,
837 uint64_t ebsp_size
840 lsmash_bits_t *bits = info->bits;
841 /* pic_parameter_set_rbsp */
842 h264_pps_t temp_pps;
843 int err = h264_parse_pps_minimally( bits, &temp_pps, rbsp_buffer, ebsp, ebsp_size );
844 if( err < 0 )
845 return err;
846 h264_pps_t *pps = h264_get_pps( info->pps_list, temp_pps.pic_parameter_set_id );
847 if( !pps )
848 return LSMASH_ERR_NAMELESS;
849 memset( pps, 0, sizeof(h264_pps_t) );
850 pps->pic_parameter_set_id = temp_pps.pic_parameter_set_id;
851 uint64_t seq_parameter_set_id = nalu_get_exp_golomb_ue( bits );
852 if( seq_parameter_set_id > 31 )
853 return LSMASH_ERR_INVALID_DATA;
854 h264_sps_t *sps = h264_get_sps( info->sps_list, seq_parameter_set_id );
855 if( !sps )
856 return LSMASH_ERR_NAMELESS;
857 pps->seq_parameter_set_id = seq_parameter_set_id;
858 pps->entropy_coding_mode_flag = lsmash_bits_get( bits, 1 );
859 pps->bottom_field_pic_order_in_frame_present_flag = lsmash_bits_get( bits, 1 );
860 uint64_t num_slice_groups_minus1 = nalu_get_exp_golomb_ue( bits );
861 if( num_slice_groups_minus1 > 7 )
862 return LSMASH_ERR_INVALID_DATA;
863 pps->num_slice_groups_minus1 = num_slice_groups_minus1;
864 if( num_slice_groups_minus1 ) /* num_slice_groups_minus1 */
866 uint64_t slice_group_map_type = nalu_get_exp_golomb_ue( bits );
867 if( slice_group_map_type > 6 )
868 return LSMASH_ERR_INVALID_DATA;
869 pps->slice_group_map_type = slice_group_map_type;
870 if( slice_group_map_type == 0 )
871 for( uint64_t iGroup = 0; iGroup <= num_slice_groups_minus1; iGroup++ )
872 nalu_get_exp_golomb_ue( bits ); /* run_length_minus1[ iGroup ] */
873 else if( slice_group_map_type == 2 )
874 for( uint64_t iGroup = 0; iGroup < num_slice_groups_minus1; iGroup++ )
876 nalu_get_exp_golomb_ue( bits ); /* top_left [ iGroup ] */
877 nalu_get_exp_golomb_ue( bits ); /* bottom_right[ iGroup ] */
879 else if( slice_group_map_type == 3
880 || slice_group_map_type == 4
881 || slice_group_map_type == 5 )
883 lsmash_bits_get( bits, 1 ); /* slice_group_change_direction_flag */
884 uint64_t slice_group_change_rate_minus1 = nalu_get_exp_golomb_ue( bits );
885 if( slice_group_change_rate_minus1 > (sps->PicSizeInMapUnits - 1) )
886 return LSMASH_ERR_INVALID_DATA;
887 pps->SliceGroupChangeRate = slice_group_change_rate_minus1 + 1;
889 else if( slice_group_map_type == 6 )
891 uint64_t pic_size_in_map_units_minus1 = nalu_get_exp_golomb_ue( bits );
892 int length = lsmash_ceil_log2( num_slice_groups_minus1 + 1 );
893 for( uint64_t i = 0; i <= pic_size_in_map_units_minus1; i++ )
894 /* slice_group_id */
895 if( lsmash_bits_get( bits, length ) > num_slice_groups_minus1 )
896 return LSMASH_ERR_INVALID_DATA;
899 pps->num_ref_idx_l0_default_active_minus1 = nalu_get_exp_golomb_ue( bits );
900 pps->num_ref_idx_l1_default_active_minus1 = nalu_get_exp_golomb_ue( bits );
901 pps->weighted_pred_flag = lsmash_bits_get( bits, 1 );
902 pps->weighted_bipred_idc = lsmash_bits_get( bits, 2 );
903 nalu_get_exp_golomb_se( bits ); /* pic_init_qp_minus26 */
904 nalu_get_exp_golomb_se( bits ); /* pic_init_qs_minus26 */
905 nalu_get_exp_golomb_se( bits ); /* chroma_qp_index_offset */
906 pps->deblocking_filter_control_present_flag = lsmash_bits_get( bits, 1 );
907 lsmash_bits_get( bits, 1 ); /* constrained_intra_pred_flag */
908 pps->redundant_pic_cnt_present_flag = lsmash_bits_get( bits, 1 );
909 if( nalu_check_more_rbsp_data( bits ) )
911 int transform_8x8_mode_flag = lsmash_bits_get( bits, 1 );
912 if( lsmash_bits_get( bits, 1 ) ) /* pic_scaling_matrix_present_flag */
914 int num_loops = 6 + (sps->chroma_format_idc != 3 ? 2 : 6) * transform_8x8_mode_flag;
915 for( int i = 0; i < num_loops; i++ )
916 if( lsmash_bits_get( bits, 1 ) /* pic_scaling_list_present_flag[i] */
917 && (err = h264_parse_scaling_list( bits, i < 6 ? 16 : 64 )) < 0 )
918 return err;
920 nalu_get_exp_golomb_se( bits ); /* second_chroma_qp_index_offset */
922 /* rbsp_trailing_bits() */
923 if( !lsmash_bits_get( bits, 1 ) ) /* rbsp_stop_one_bit */
924 return LSMASH_ERR_INVALID_DATA;
925 lsmash_bits_empty( bits );
926 if( bits->bs->error )
927 return LSMASH_ERR_NAMELESS;
928 pps->present = 1;
929 info->sps = *sps;
930 info->pps = *pps;
931 return 0;
934 int h264_parse_sei
936 lsmash_bits_t *bits,
937 h264_sps_t *sps,
938 h264_sei_t *sei,
939 uint8_t *rbsp_buffer,
940 uint8_t *ebsp,
941 uint64_t ebsp_size
944 uint64_t rbsp_size;
945 int err = nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, &rbsp_size, ebsp, ebsp_size );
946 if( err < 0 )
947 return err;
948 uint8_t *rbsp_start = rbsp_buffer;
949 uint64_t rbsp_pos = 0;
952 /* sei_message() */
953 uint32_t payloadType = 0;
954 for( uint8_t temp = lsmash_bits_get( bits, 8 ); ; temp = lsmash_bits_get( bits, 8 ) )
956 /* 0xff : ff_byte
957 * otherwise: last_payload_type_byte */
958 payloadType += temp;
959 ++rbsp_pos;
960 if( temp != 0xff )
961 break;
963 uint32_t payloadSize = 0;
964 for( uint8_t temp = lsmash_bits_get( bits, 8 ); ; temp = lsmash_bits_get( bits, 8 ) )
966 /* 0xff : ff_byte
967 * otherwise: last_payload_size_byte */
968 payloadSize += temp;
969 ++rbsp_pos;
970 if( temp != 0xff )
971 break;
973 if( payloadType == 1 )
975 /* pic_timing */
976 h264_hrd_t *hrd = sps ? &sps->vui.hrd : NULL;
977 if( !hrd )
978 goto skip_sei_message; /* Any active SPS is not found. */
979 sei->pic_timing.present = 1;
980 if( hrd->CpbDpbDelaysPresentFlag )
982 lsmash_bits_get( bits, hrd->cpb_removal_delay_length ); /* cpb_removal_delay */
983 lsmash_bits_get( bits, hrd->dpb_output_delay_length ); /* dpb_output_delay */
985 if( sps->vui.pic_struct_present_flag )
987 sei->pic_timing.pic_struct = lsmash_bits_get( bits, 4 );
988 /* Skip the remaining bits. */
989 uint32_t remaining_bits = payloadSize * 8 - 4;
990 if( hrd->CpbDpbDelaysPresentFlag )
991 remaining_bits -= hrd->cpb_removal_delay_length
992 + hrd->dpb_output_delay_length;
993 lsmash_bits_get( bits, remaining_bits );
996 else if( payloadType == 3 )
998 /* filler_payload
999 * 'avc1' and 'avc2' samples are forbidden to contain this. */
1000 return LSMASH_ERR_PATCH_WELCOME;
1002 else if( payloadType == 6 )
1004 /* recovery_point */
1005 sei->recovery_point.present = 1;
1006 sei->recovery_point.random_accessible = 1;
1007 sei->recovery_point.recovery_frame_cnt = nalu_get_exp_golomb_ue( bits );
1008 lsmash_bits_get( bits, 1 ); /* exact_match_flag */
1009 sei->recovery_point.broken_link_flag = lsmash_bits_get( bits, 1 );
1010 lsmash_bits_get( bits, 2 ); /* changing_slice_group_idc */
1012 else
1014 skip_sei_message:
1015 lsmash_bits_get( bits, payloadSize * 8 );
1017 lsmash_bits_get_align( bits );
1018 rbsp_pos += payloadSize;
1019 if( rbsp_pos > rbsp_size )
1021 rbsp_pos = rbsp_size;
1022 if( *(rbsp_start + rbsp_pos) != 0x80 )
1024 lsmash_log( NULL, LSMASH_LOG_ERROR, "Invalid payloadSize is there within H.264/AVC sei_message().\n" );
1025 return LSMASH_ERR_INVALID_DATA;
1027 else
1029 /* The last payloadSize is invalid but it probably can do recovery, so just log warning and break loop here. */
1030 lsmash_log( NULL, LSMASH_LOG_WARNING, "Invalid payloadSize is there within H.264/AVC sei_message().\n" );
1031 break; /* redundant but for readability */
1034 } while( *(rbsp_start + rbsp_pos) != 0x80 ); /* All SEI messages are byte aligned at their end.
1035 * Therefore, 0x80 shall be rbsp_trailing_bits(). */
1036 lsmash_bits_empty( bits );
1037 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
1040 static int h264_parse_slice_header
1042 h264_info_t *info,
1043 h264_nalu_header_t *nuh
1046 h264_slice_info_t *slice = &info->slice;
1047 memset( slice, 0, sizeof(h264_slice_info_t) );
1048 /* slice_header() */
1049 lsmash_bits_t *bits = info->bits;
1050 nalu_get_exp_golomb_ue( bits ); /* first_mb_in_slice */
1051 uint8_t slice_type = slice->type = nalu_get_exp_golomb_ue( bits );
1052 if( (uint64_t)slice->type > 9 )
1053 return LSMASH_ERR_INVALID_DATA;
1054 if( slice_type > 4 )
1055 slice_type = slice->type -= 5;
1056 uint64_t pic_parameter_set_id = nalu_get_exp_golomb_ue( bits );
1057 if( pic_parameter_set_id > 255 )
1058 return LSMASH_ERR_INVALID_DATA;
1059 slice->pic_parameter_set_id = pic_parameter_set_id;
1060 h264_pps_t *pps = h264_get_pps( info->pps_list, pic_parameter_set_id );
1061 if( !pps )
1062 return LSMASH_ERR_NAMELESS;
1063 h264_sps_t *sps = h264_get_sps( info->sps_list, pps->seq_parameter_set_id );
1064 if( !sps )
1065 return LSMASH_ERR_NAMELESS;
1066 slice->seq_parameter_set_id = pps->seq_parameter_set_id;
1067 slice->nal_ref_idc = nuh->nal_ref_idc;
1068 slice->IdrPicFlag = (nuh->nal_unit_type == H264_NALU_TYPE_SLICE_IDR);
1069 slice->pic_order_cnt_type = sps->pic_order_cnt_type;
1070 if( (slice->IdrPicFlag || sps->max_num_ref_frames == 0) && slice_type != 2 && slice_type != 4 )
1071 return LSMASH_ERR_INVALID_DATA;
1072 if( sps->separate_colour_plane_flag )
1073 lsmash_bits_get( bits, 2 ); /* colour_plane_id */
1074 uint64_t frame_num = lsmash_bits_get( bits, sps->log2_max_frame_num );
1075 if( frame_num >= (1ULL << sps->log2_max_frame_num) || (slice->IdrPicFlag && frame_num) )
1076 return LSMASH_ERR_INVALID_DATA;
1077 slice->frame_num = frame_num;
1078 if( !sps->frame_mbs_only_flag )
1080 slice->field_pic_flag = lsmash_bits_get( bits, 1 );
1081 if( slice->field_pic_flag )
1082 slice->bottom_field_flag = lsmash_bits_get( bits, 1 );
1084 if( slice->IdrPicFlag )
1086 uint64_t idr_pic_id = nalu_get_exp_golomb_ue( bits );
1087 if( idr_pic_id > 65535 )
1088 return LSMASH_ERR_INVALID_DATA;
1089 slice->idr_pic_id = idr_pic_id;
1091 if( sps->pic_order_cnt_type == 0 )
1093 uint64_t pic_order_cnt_lsb = lsmash_bits_get( bits, sps->log2_max_pic_order_cnt_lsb );
1094 if( pic_order_cnt_lsb >= sps->MaxPicOrderCntLsb )
1095 return LSMASH_ERR_INVALID_DATA;
1096 slice->pic_order_cnt_lsb = pic_order_cnt_lsb;
1097 if( pps->bottom_field_pic_order_in_frame_present_flag && !slice->field_pic_flag )
1098 slice->delta_pic_order_cnt_bottom = nalu_get_exp_golomb_se( bits );
1100 else if( sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag )
1102 slice->delta_pic_order_cnt[0] = nalu_get_exp_golomb_se( bits );
1103 if( pps->bottom_field_pic_order_in_frame_present_flag && !slice->field_pic_flag )
1104 slice->delta_pic_order_cnt[1] = nalu_get_exp_golomb_se( bits );
1106 if( pps->redundant_pic_cnt_present_flag )
1108 uint64_t redundant_pic_cnt = nalu_get_exp_golomb_ue( bits );
1109 if( redundant_pic_cnt > 127 )
1110 return LSMASH_ERR_INVALID_DATA;
1111 slice->has_redundancy = !!redundant_pic_cnt;
1113 if( slice_type == H264_SLICE_TYPE_B )
1114 lsmash_bits_get( bits, 1 );
1115 uint64_t num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
1116 uint64_t num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
1117 if( slice_type == H264_SLICE_TYPE_P || slice_type == H264_SLICE_TYPE_SP || slice_type == H264_SLICE_TYPE_B )
1119 if( lsmash_bits_get( bits, 1 ) ) /* num_ref_idx_active_override_flag */
1121 num_ref_idx_l0_active_minus1 = nalu_get_exp_golomb_ue( bits );
1122 if( num_ref_idx_l0_active_minus1 > 31 )
1123 return LSMASH_ERR_INVALID_DATA;
1124 if( slice_type == H264_SLICE_TYPE_B )
1126 num_ref_idx_l1_active_minus1 = nalu_get_exp_golomb_ue( bits );
1127 if( num_ref_idx_l1_active_minus1 > 31 )
1128 return LSMASH_ERR_INVALID_DATA;
1132 if( nuh->nal_unit_type == H264_NALU_TYPE_SLICE_EXT
1133 || nuh->nal_unit_type == H264_NALU_TYPE_SLICE_EXT_DVC )
1135 return LSMASH_ERR_PATCH_WELCOME; /* No support of MVC yet */
1136 #if 0
1137 /* ref_pic_list_mvc_modification() */
1138 if( slice_type == H264_SLICE_TYPE_P || slice_type == H264_SLICE_TYPE_B || slice_type == H264_SLICE_TYPE_SP )
1140 for( int i = 0; i < 1 + (slice_type == H264_SLICE_TYPE_B); i++ )
1142 if( lsmash_bits_get( bits, 1 ) ) /* (S)P and B: ref_pic_list_modification_flag_l0
1143 * B: ref_pic_list_modification_flag_l1 */
1145 uint64_t modification_of_pic_nums_idc;
1148 modification_of_pic_nums_idc = nalu_get_exp_golomb_ue( bits );
1149 #if 0
1150 if( modification_of_pic_nums_idc == 0 || modification_of_pic_nums_idc == 1 )
1151 nalu_get_exp_golomb_ue( bits ); /* abs_diff_pic_num_minus1 */
1152 else if( modification_of_pic_nums_idc == 2 )
1153 nalu_get_exp_golomb_ue( bits ); /* long_term_pic_num */
1154 else if( modification_of_pic_nums_idc == 4 || modification_of_pic_nums_idc == 5 )
1155 nalu_get_exp_golomb_ue( bits ); /* abs_diff_view_idx_minus1 */
1156 #else
1157 if( modification_of_pic_nums_idc != 3 )
1158 nalu_get_exp_golomb_ue( bits ); /* abs_diff_pic_num_minus1, long_term_pic_num or abs_diff_view_idx_minus1 */
1159 #endif
1160 } while( modification_of_pic_nums_idc != 3 );
1164 #endif
1166 else
1168 /* ref_pic_list_modification() */
1169 if( slice_type == H264_SLICE_TYPE_P || slice_type == H264_SLICE_TYPE_B || slice_type == H264_SLICE_TYPE_SP )
1171 for( int i = 0; i < 1 + (slice_type == H264_SLICE_TYPE_B); i++ )
1173 if( lsmash_bits_get( bits, 1 ) ) /* (S)P and B: ref_pic_list_modification_flag_l0
1174 * B: ref_pic_list_modification_flag_l1 */
1176 uint64_t modification_of_pic_nums_idc;
1179 modification_of_pic_nums_idc = nalu_get_exp_golomb_ue( bits );
1180 #if 0
1181 if( modification_of_pic_nums_idc == 0 || modification_of_pic_nums_idc == 1 )
1182 nalu_get_exp_golomb_ue( bits ); /* abs_diff_pic_num_minus1 */
1183 else if( modification_of_pic_nums_idc == 2 )
1184 nalu_get_exp_golomb_ue( bits ); /* long_term_pic_num */
1185 #else
1186 if( modification_of_pic_nums_idc != 3 )
1187 nalu_get_exp_golomb_ue( bits ); /* abs_diff_pic_num_minus1 or long_term_pic_num */
1188 #endif
1189 } while( modification_of_pic_nums_idc != 3 );
1194 if( (pps->weighted_pred_flag && (slice_type == H264_SLICE_TYPE_P || slice_type == H264_SLICE_TYPE_SP))
1195 || (pps->weighted_bipred_idc == 1 && slice_type == H264_SLICE_TYPE_B) )
1197 /* pred_weight_table() */
1198 nalu_get_exp_golomb_ue( bits ); /* luma_log2_weight_denom */
1199 if( sps->ChromaArrayType )
1200 nalu_get_exp_golomb_ue( bits ); /* chroma_log2_weight_denom */
1201 for( uint8_t i = 0; i <= num_ref_idx_l0_active_minus1; i++ )
1203 if( lsmash_bits_get( bits, 1 ) ) /* luma_weight_l0_flag */
1205 nalu_get_exp_golomb_se( bits ); /* luma_weight_l0[i] */
1206 nalu_get_exp_golomb_se( bits ); /* luma_offset_l0[i] */
1208 if( sps->ChromaArrayType
1209 && lsmash_bits_get( bits, 1 ) /* chroma_weight_l0_flag */ )
1210 for( int j = 0; j < 2; j++ )
1212 nalu_get_exp_golomb_se( bits ); /* chroma_weight_l0[i][j]*/
1213 nalu_get_exp_golomb_se( bits ); /* chroma_offset_l0[i][j] */
1216 if( slice_type == H264_SLICE_TYPE_B )
1217 for( uint8_t i = 0; i <= num_ref_idx_l1_active_minus1; i++ )
1219 if( lsmash_bits_get( bits, 1 ) ) /* luma_weight_l1_flag */
1221 nalu_get_exp_golomb_se( bits ); /* luma_weight_l1[i] */
1222 nalu_get_exp_golomb_se( bits ); /* luma_offset_l1[i] */
1224 if( sps->ChromaArrayType
1225 && lsmash_bits_get( bits, 1 ) /* chroma_weight_l1_flag */ )
1226 for( int j = 0; j < 2; j++ )
1228 nalu_get_exp_golomb_se( bits ); /* chroma_weight_l1[i][j]*/
1229 nalu_get_exp_golomb_se( bits ); /* chroma_offset_l1[i][j] */
1233 if( nuh->nal_ref_idc )
1235 /* dec_ref_pic_marking() */
1236 if( slice->IdrPicFlag )
1238 lsmash_bits_get( bits, 1 ); /* no_output_of_prior_pics_flag */
1239 lsmash_bits_get( bits, 1 ); /* long_term_reference_flag */
1241 else if( lsmash_bits_get( bits, 1 ) ) /* adaptive_ref_pic_marking_mode_flag */
1243 uint64_t memory_management_control_operation;
1246 memory_management_control_operation = nalu_get_exp_golomb_ue( bits );
1247 if( memory_management_control_operation )
1249 if( memory_management_control_operation == 5 )
1250 slice->has_mmco5 = 1;
1251 else
1253 nalu_get_exp_golomb_ue( bits );
1254 if( memory_management_control_operation == 3 )
1255 nalu_get_exp_golomb_ue( bits );
1258 } while( memory_management_control_operation );
1261 /* We needn't read more if not slice data partition A.
1262 * Skip slice_data() and rbsp_slice_trailing_bits(). */
1263 if( nuh->nal_unit_type == H264_NALU_TYPE_SLICE_DP_A )
1265 if( pps->entropy_coding_mode_flag && slice_type != H264_SLICE_TYPE_I && slice_type != H264_SLICE_TYPE_SI )
1266 nalu_get_exp_golomb_ue( bits ); /* cabac_init_idc */
1267 nalu_get_exp_golomb_se( bits ); /* slice_qp_delta */
1268 if( slice_type == H264_SLICE_TYPE_SP || slice_type == H264_SLICE_TYPE_SI )
1270 if( slice_type == H264_SLICE_TYPE_SP )
1271 lsmash_bits_get( bits, 1 ); /* sp_for_switch_flag */
1272 nalu_get_exp_golomb_se( bits ); /* slice_qs_delta */
1274 if( pps->deblocking_filter_control_present_flag
1275 && nalu_get_exp_golomb_ue( bits ) != 1 /* disable_deblocking_filter_idc */ )
1277 int64_t slice_alpha_c0_offset_div2 = nalu_get_exp_golomb_se( bits );
1278 if( slice_alpha_c0_offset_div2 < -6 || slice_alpha_c0_offset_div2 > 6 )
1279 return LSMASH_ERR_INVALID_DATA;
1280 int64_t slice_beta_offset_div2 = nalu_get_exp_golomb_se( bits );
1281 if( slice_beta_offset_div2 < -6 || slice_beta_offset_div2 > 6 )
1282 return LSMASH_ERR_INVALID_DATA;
1284 if( pps->num_slice_groups_minus1
1285 && (pps->slice_group_map_type == 3 || pps->slice_group_map_type == 4 || pps->slice_group_map_type == 5) )
1287 uint64_t temp = ((uint64_t)sps->PicSizeInMapUnits - 1) / pps->SliceGroupChangeRate + 1;
1288 uint64_t slice_group_change_cycle = lsmash_bits_get( bits, lsmash_ceil_log2( temp + 1 ) );
1289 if( slice_group_change_cycle > temp )
1290 return LSMASH_ERR_INVALID_DATA;
1292 /* end of slice_header() */
1293 slice->slice_id = nalu_get_exp_golomb_ue( bits );
1294 h264_slice_info_t *slice_part = h264_get_slice_info( info->slice_list, slice->slice_id );
1295 if( !slice_part )
1296 return LSMASH_ERR_NAMELESS;
1297 *slice_part = *slice;
1299 lsmash_bits_empty( bits );
1300 if( bits->bs->error )
1301 return LSMASH_ERR_NAMELESS;
1302 info->sps = *sps;
1303 info->pps = *pps;
1304 return 0;
1307 int h264_parse_slice
1309 h264_info_t *info,
1310 h264_nalu_header_t *nuh,
1311 uint8_t *rbsp_buffer,
1312 uint8_t *ebsp,
1313 uint64_t ebsp_size
1316 lsmash_bits_t *bits = info->bits;
1317 uint64_t size = nuh->nal_unit_type == H264_NALU_TYPE_SLICE_IDR || nuh->nal_ref_idc == 0
1318 ? LSMASH_MIN( ebsp_size, 100 )
1319 : LSMASH_MIN( ebsp_size, 1000 );
1320 uint64_t rbsp_size;
1321 int err = nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, &rbsp_size, ebsp, size );
1322 if( err < 0 )
1323 return err;
1324 if( nuh->nal_unit_type != H264_NALU_TYPE_SLICE_DP_B
1325 && nuh->nal_unit_type != H264_NALU_TYPE_SLICE_DP_C )
1326 return h264_parse_slice_header( info, nuh );
1327 /* slice_data_partition_b_layer_rbsp() or slice_data_partition_c_layer_rbsp() */
1328 uint64_t slice_id = nalu_get_exp_golomb_ue( bits );
1329 h264_slice_info_t *slice = h264_get_slice_info( info->slice_list, slice_id );
1330 if( !slice )
1331 return LSMASH_ERR_NAMELESS;
1332 h264_pps_t *pps = h264_get_pps( info->pps_list, slice->pic_parameter_set_id );
1333 if( !pps )
1334 return LSMASH_ERR_NAMELESS;
1335 h264_sps_t *sps = h264_get_sps( info->sps_list, pps->seq_parameter_set_id );
1336 if( !sps )
1337 return LSMASH_ERR_NAMELESS;
1338 slice->seq_parameter_set_id = pps->seq_parameter_set_id;
1339 if( sps->separate_colour_plane_flag )
1340 lsmash_bits_get( bits, 2 ); /* colour_plane_id */
1341 if( pps->redundant_pic_cnt_present_flag )
1343 uint64_t redundant_pic_cnt = nalu_get_exp_golomb_ue( bits );
1344 if( redundant_pic_cnt > 127 )
1345 return LSMASH_ERR_INVALID_DATA;
1346 slice->has_redundancy = !!redundant_pic_cnt;
1348 /* Skip slice_data() and rbsp_slice_trailing_bits(). */
1349 lsmash_bits_empty( bits );
1350 if( bits->bs->error )
1351 return LSMASH_ERR_NAMELESS;
1352 info->sps = *sps;
1353 info->pps = *pps;
1354 return 0;
1357 static int h264_get_sps_id
1359 uint8_t *ps_ebsp,
1360 uint32_t ps_ebsp_length,
1361 uint8_t *ps_id
1364 /* max number of bits of sps_id = 11: 0b000001XXXXX
1365 * (24 + 11 - 1) / 8 + 1 = 5 bytes
1366 * Why +1? Because there might be an emulation_prevention_three_byte. */
1367 lsmash_bits_t bits = { 0 };
1368 lsmash_bs_t bs = { 0 };
1369 uint8_t rbsp_buffer[6];
1370 uint8_t buffer [6];
1371 bs.buffer.data = buffer;
1372 bs.buffer.alloc = 6;
1373 lsmash_bits_init( &bits, &bs );
1374 uint64_t rbsp_size;
1375 int err = nalu_import_rbsp_from_ebsp( &bits, rbsp_buffer, &rbsp_size, ps_ebsp, LSMASH_MIN( ps_ebsp_length, 6 ) );
1376 if( err < 0 )
1377 return err;
1378 lsmash_bits_get( &bits, 24 ); /* profile_idc, constraint_set_flags and level_idc */
1379 uint64_t sec_parameter_set_id = nalu_get_exp_golomb_ue( &bits );
1380 if( sec_parameter_set_id > 31 )
1381 return LSMASH_ERR_INVALID_DATA;
1382 *ps_id = sec_parameter_set_id;
1383 return bs.error ? LSMASH_ERR_NAMELESS : 0;
1386 static int h264_get_pps_id
1388 uint8_t *ps_ebsp,
1389 uint32_t ps_ebsp_length,
1390 uint8_t *ps_id
1393 /* max number of bits of pps_id = 17: 0b000000001XXXXXXXX
1394 * (17 - 1) / 8 + 1 = 3 bytes
1395 * Why +1? Because there might be an emulation_prevention_three_byte. */
1396 lsmash_bits_t bits = { 0 };
1397 lsmash_bs_t bs = { 0 };
1398 uint8_t rbsp_buffer[4];
1399 uint8_t buffer [4];
1400 bs.buffer.data = buffer;
1401 bs.buffer.alloc = 4;
1402 lsmash_bits_init( &bits, &bs );
1403 uint64_t rbsp_size;
1404 int err = nalu_import_rbsp_from_ebsp( &bits, rbsp_buffer, &rbsp_size, ps_ebsp, LSMASH_MIN( ps_ebsp_length, 4 ) );
1405 if( err < 0 )
1406 return err;
1407 uint64_t pic_parameter_set_id = nalu_get_exp_golomb_ue( &bits );
1408 if( pic_parameter_set_id > 255 )
1409 return LSMASH_ERR_INVALID_DATA;
1410 *ps_id = pic_parameter_set_id;
1411 return bs.error ? LSMASH_ERR_NAMELESS : 0;
1414 static inline int h264_get_ps_id
1416 uint8_t *ps_ebsp,
1417 uint32_t ps_ebsp_length,
1418 uint8_t *ps_id,
1419 lsmash_h264_parameter_set_type ps_type
1422 int (*get_ps_id)( uint8_t *ps_ebsp, uint32_t ps_ebsp_length, uint8_t *ps_id )
1423 = ps_type == H264_PARAMETER_SET_TYPE_SPS ? h264_get_sps_id
1424 : ps_type == H264_PARAMETER_SET_TYPE_PPS ? h264_get_pps_id
1425 : NULL;
1426 return get_ps_id ? get_ps_id( ps_ebsp, ps_ebsp_length, ps_id ) : LSMASH_ERR_INVALID_DATA;
1429 static inline lsmash_entry_list_t *h264_get_parameter_set_list
1431 lsmash_h264_specific_parameters_t *param,
1432 lsmash_h264_parameter_set_type ps_type
1435 if( !param->parameter_sets )
1436 return NULL;
1437 return ps_type == H264_PARAMETER_SET_TYPE_SPS ? param->parameter_sets->sps_list
1438 : ps_type == H264_PARAMETER_SET_TYPE_PPS ? param->parameter_sets->pps_list
1439 : ps_type == H264_PARAMETER_SET_TYPE_SPSEXT ? param->parameter_sets->spsext_list
1440 : NULL;
1443 static lsmash_entry_t *h264_get_ps_entry_from_param
1445 lsmash_h264_specific_parameters_t *param,
1446 lsmash_h264_parameter_set_type ps_type,
1447 uint8_t ps_id
1450 int (*get_ps_id)( uint8_t *ps_ebsp, uint32_t ps_ebsp_length, uint8_t *ps_id )
1451 = ps_type == H264_PARAMETER_SET_TYPE_SPS ? h264_get_sps_id
1452 : ps_type == H264_PARAMETER_SET_TYPE_PPS ? h264_get_pps_id
1453 : NULL;
1454 if( !get_ps_id )
1455 return NULL;
1456 lsmash_entry_list_t *ps_list = h264_get_parameter_set_list( param, ps_type );
1457 if( !ps_list )
1458 return NULL;
1459 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
1461 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1462 if( !ps )
1463 return NULL;
1464 uint8_t param_ps_id;
1465 if( get_ps_id( ps->nalUnit + 1, ps->nalUnitLength - 1, &param_ps_id ) < 0 )
1466 return NULL;
1467 if( ps_id == param_ps_id )
1468 return entry;
1470 return NULL;
1473 static inline void h264_update_picture_type
1475 h264_picture_info_t *picture,
1476 h264_slice_info_t *slice
1479 if( picture->type == H264_PICTURE_TYPE_I_P )
1481 if( slice->type == H264_SLICE_TYPE_B )
1482 picture->type = H264_PICTURE_TYPE_I_P_B;
1483 else if( slice->type == H264_SLICE_TYPE_SI || slice->type == H264_SLICE_TYPE_SP )
1484 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1486 else if( picture->type == H264_PICTURE_TYPE_I_P_B )
1488 if( slice->type != H264_SLICE_TYPE_P && slice->type != H264_SLICE_TYPE_B && slice->type != H264_SLICE_TYPE_I )
1489 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1491 else if( picture->type == H264_PICTURE_TYPE_I )
1493 if( slice->type == H264_SLICE_TYPE_P )
1494 picture->type = H264_PICTURE_TYPE_I_P;
1495 else if( slice->type == H264_SLICE_TYPE_B )
1496 picture->type = H264_PICTURE_TYPE_I_P_B;
1497 else if( slice->type == H264_SLICE_TYPE_SI )
1498 picture->type = H264_PICTURE_TYPE_I_SI;
1499 else if( slice->type == H264_SLICE_TYPE_SP )
1500 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1502 else if( picture->type == H264_PICTURE_TYPE_SI_SP )
1504 if( slice->type == H264_SLICE_TYPE_P || slice->type == H264_SLICE_TYPE_I )
1505 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1506 else if( slice->type == H264_SLICE_TYPE_B )
1507 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1509 else if( picture->type == H264_PICTURE_TYPE_SI )
1511 if( slice->type == H264_SLICE_TYPE_P )
1512 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1513 else if( slice->type == H264_SLICE_TYPE_B )
1514 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1515 else if( slice->type != H264_SLICE_TYPE_I )
1516 picture->type = H264_PICTURE_TYPE_I_SI;
1517 else if( slice->type == H264_SLICE_TYPE_SP )
1518 picture->type = H264_PICTURE_TYPE_SI_SP;
1520 else if( picture->type == H264_PICTURE_TYPE_I_SI )
1522 if( slice->type == H264_SLICE_TYPE_P || slice->type == H264_SLICE_TYPE_SP )
1523 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1524 else if( slice->type == H264_SLICE_TYPE_B )
1525 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1527 else if( picture->type == H264_PICTURE_TYPE_I_SI_P_SP )
1529 if( slice->type == H264_SLICE_TYPE_B )
1530 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1532 else if( picture->type == H264_PICTURE_TYPE_NONE )
1534 if( slice->type == H264_SLICE_TYPE_P )
1535 picture->type = H264_PICTURE_TYPE_I_P;
1536 else if( slice->type == H264_SLICE_TYPE_B )
1537 picture->type = H264_PICTURE_TYPE_I_P_B;
1538 else if( slice->type == H264_SLICE_TYPE_I )
1539 picture->type = H264_PICTURE_TYPE_I;
1540 else if( slice->type == H264_SLICE_TYPE_SI )
1541 picture->type = H264_PICTURE_TYPE_SI;
1542 else if( slice->type == H264_SLICE_TYPE_SP )
1543 picture->type = H264_PICTURE_TYPE_SI_SP;
1545 #if 0
1546 fprintf( stderr, "Picture type = %s\n", picture->type == H264_PICTURE_TYPE_I_P ? "P"
1547 : picture->type == H264_PICTURE_TYPE_I_P_B ? "B"
1548 : picture->type == H264_PICTURE_TYPE_I ? "I"
1549 : picture->type == H264_PICTURE_TYPE_SI ? "SI"
1550 : picture->type == H264_PICTURE_TYPE_I_SI ? "SI"
1551 : "SP" );
1552 #endif
1555 /* Shall be called at least once per picture. */
1556 void h264_update_picture_info_for_slice
1558 h264_info_t *info,
1559 h264_picture_info_t *picture,
1560 h264_slice_info_t *slice
1563 assert( info );
1564 picture->has_mmco5 |= slice->has_mmco5;
1565 picture->has_redundancy |= slice->has_redundancy;
1566 picture->has_primary |= !slice->has_redundancy;
1567 h264_update_picture_type( picture, slice );
1568 /* Mark 'used' on active parameter sets. */
1569 uint8_t ps_id[2] = { slice->seq_parameter_set_id, slice->pic_parameter_set_id };
1570 for( int i = 0; i < 2; i++ )
1572 lsmash_h264_parameter_set_type ps_type = (lsmash_h264_parameter_set_type)i;
1573 lsmash_entry_t *entry = h264_get_ps_entry_from_param( &info->avcC_param, ps_type, ps_id[i] );
1574 if( entry && entry->data )
1576 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1577 if( ps->unused )
1578 lsmash_append_h264_parameter_set( &info->avcC_param, ps_type, ps->nalUnit, ps->nalUnitLength );
1581 /* Discard this slice info. */
1582 slice->present = 0;
1585 /* Shall be called exactly once per picture. */
1586 void h264_update_picture_info
1588 h264_info_t *info,
1589 h264_picture_info_t *picture,
1590 h264_slice_info_t *slice,
1591 h264_sei_t *sei
1594 picture->frame_num = slice->frame_num;
1595 picture->pic_order_cnt_lsb = slice->pic_order_cnt_lsb;
1596 picture->delta_pic_order_cnt_bottom = slice->delta_pic_order_cnt_bottom;
1597 picture->delta_pic_order_cnt[0] = slice->delta_pic_order_cnt[0];
1598 picture->delta_pic_order_cnt[1] = slice->delta_pic_order_cnt[1];
1599 picture->field_pic_flag = slice->field_pic_flag;
1600 picture->bottom_field_flag = slice->bottom_field_flag;
1601 picture->idr = slice->IdrPicFlag;
1602 picture->pic_parameter_set_id = slice->pic_parameter_set_id;
1603 picture->disposable = (slice->nal_ref_idc == 0);
1604 picture->random_accessible = slice->IdrPicFlag;
1605 h264_update_picture_info_for_slice( info, picture, slice );
1606 picture->independent = picture->type == H264_PICTURE_TYPE_I || picture->type == H264_PICTURE_TYPE_I_SI;
1607 if( sei->pic_timing.present )
1609 if( sei->pic_timing.pic_struct < 9 )
1611 static const uint8_t DeltaTfiDivisor[9] = { 2, 1, 1, 2, 2, 3, 3, 4, 6 };
1612 picture->delta = DeltaTfiDivisor[ sei->pic_timing.pic_struct ];
1614 else
1615 /* Reserved values in the spec we refer to. */
1616 picture->delta = picture->field_pic_flag ? 1 : 2;
1617 sei->pic_timing.present = 0;
1619 else
1620 picture->delta = picture->field_pic_flag ? 1 : 2;
1621 if( sei->recovery_point.present )
1623 picture->random_accessible |= sei->recovery_point.random_accessible;
1624 picture->broken_link_flag |= sei->recovery_point.broken_link_flag;
1625 picture->recovery_frame_cnt = sei->recovery_point.recovery_frame_cnt;
1626 sei->recovery_point.present = 0;
1630 int h264_find_au_delimit_by_slice_info
1632 h264_slice_info_t *slice,
1633 h264_slice_info_t *prev_slice
1636 if( slice->frame_num != prev_slice->frame_num
1637 || ((slice->pic_order_cnt_type == 0 && prev_slice->pic_order_cnt_type == 0)
1638 && (slice->pic_order_cnt_lsb != prev_slice->pic_order_cnt_lsb
1639 || slice->delta_pic_order_cnt_bottom != prev_slice->delta_pic_order_cnt_bottom))
1640 || ((slice->pic_order_cnt_type == 1 && prev_slice->pic_order_cnt_type == 1)
1641 && (slice->delta_pic_order_cnt[0] != prev_slice->delta_pic_order_cnt[0]
1642 || slice->delta_pic_order_cnt[1] != prev_slice->delta_pic_order_cnt[1]))
1643 || slice->field_pic_flag != prev_slice->field_pic_flag
1644 || slice->bottom_field_flag != prev_slice->bottom_field_flag
1645 || slice->IdrPicFlag != prev_slice->IdrPicFlag
1646 || slice->pic_parameter_set_id != prev_slice->pic_parameter_set_id
1647 || ((slice->nal_ref_idc == 0 || prev_slice->nal_ref_idc == 0)
1648 && (slice->nal_ref_idc != prev_slice->nal_ref_idc))
1649 || (slice->IdrPicFlag == 1 && prev_slice->IdrPicFlag == 1
1650 && slice->idr_pic_id != prev_slice->idr_pic_id) )
1651 return 1;
1652 return 0;
1655 int h264_find_au_delimit_by_nalu_type
1657 uint8_t nalu_type,
1658 uint8_t prev_nalu_type
1661 return ((nalu_type >= H264_NALU_TYPE_SEI && nalu_type <= H264_NALU_TYPE_AUD)
1662 || (nalu_type >= H264_NALU_TYPE_PREFIX && nalu_type <= H264_NALU_TYPE_RSV_NVCL18))
1663 && ((prev_nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && prev_nalu_type <= H264_NALU_TYPE_SLICE_IDR)
1664 || prev_nalu_type == H264_NALU_TYPE_FD || prev_nalu_type == H264_NALU_TYPE_SLICE_AUX);
1667 int h264_supplement_buffer
1669 h264_stream_buffer_t *sb,
1670 h264_access_unit_t *au,
1671 uint32_t size
1674 lsmash_multiple_buffers_t *bank = lsmash_resize_multiple_buffers( sb->bank, size );
1675 if( !bank )
1676 return LSMASH_ERR_MEMORY_ALLOC;
1677 sb->bank = bank;
1678 sb->rbsp = lsmash_withdraw_buffer( bank, 1 );
1679 if( au && bank->number_of_buffers == 3 )
1681 au->data = lsmash_withdraw_buffer( bank, 2 );
1682 au->incomplete_data = lsmash_withdraw_buffer( bank, 3 );
1684 return 0;
1687 static void h264_bs_put_parameter_sets
1689 lsmash_bs_t *bs,
1690 lsmash_entry_list_t *ps_list,
1691 uint32_t max_ps_count
1694 uint32_t ps_count = 0;
1695 for( lsmash_entry_t *entry = ps_list->head; entry && ps_count < max_ps_count; entry = entry->next )
1697 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1698 if( ps && !ps->unused )
1700 lsmash_bs_put_be16( bs, ps->nalUnitLength );
1701 lsmash_bs_put_bytes( bs, ps->nalUnitLength, ps->nalUnit );
1703 else
1704 continue;
1705 ++ps_count;
1709 uint8_t *lsmash_create_h264_specific_info
1711 lsmash_h264_specific_parameters_t *param,
1712 uint32_t *data_length
1715 if( !param || !param->parameter_sets || !data_length )
1716 return NULL;
1717 if( param->lengthSizeMinusOne != 0 && param->lengthSizeMinusOne != 1 && param->lengthSizeMinusOne != 3 )
1718 return NULL;
1719 static const uint32_t max_ps_count[3] = { 31, 255, 255 };
1720 lsmash_entry_list_t *ps_list[3] =
1722 param->parameter_sets->sps_list, /* SPS */
1723 param->parameter_sets->pps_list, /* PPS */
1724 param->parameter_sets->spsext_list /* SPSExt */
1726 uint32_t ps_count[3] = { 0, 0, 0 };
1727 /* SPS and PPS are mandatory. */
1728 if( !ps_list[0] || !ps_list[0]->head || ps_list[0]->entry_count == 0
1729 || !ps_list[1] || !ps_list[1]->head || ps_list[1]->entry_count == 0 )
1730 return NULL;
1731 for( int i = 0; i < 3; i++ )
1732 if( ps_list[i] )
1733 for( lsmash_entry_t *entry = ps_list[i]->head; entry && ps_count[i] < max_ps_count[i]; entry = entry->next )
1735 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1736 if( !ps )
1737 return NULL;
1738 if( ps->unused )
1739 continue;
1740 ++ps_count[i];
1742 /* Create an AVCConfigurationBox */
1743 lsmash_bs_t *bs = lsmash_bs_create();
1744 if( !bs )
1745 return NULL;
1746 lsmash_bs_put_be32( bs, 0 ); /* box size */
1747 lsmash_bs_put_be32( bs, ISOM_BOX_TYPE_AVCC.fourcc ); /* box type: 'avcC' */
1748 lsmash_bs_put_byte( bs, 1 ); /* configurationVersion */
1749 lsmash_bs_put_byte( bs, param->AVCProfileIndication ); /* AVCProfileIndication */
1750 lsmash_bs_put_byte( bs, param->profile_compatibility ); /* profile_compatibility */
1751 lsmash_bs_put_byte( bs, param->AVCLevelIndication ); /* AVCLevelIndication */
1752 lsmash_bs_put_byte( bs, param->lengthSizeMinusOne | 0xfc ); /* lengthSizeMinusOne */
1753 lsmash_bs_put_byte( bs, ps_count[0] | 0xe0 ); /* numOfSequenceParameterSets */
1754 h264_bs_put_parameter_sets( bs, ps_list[0], ps_count[0] ); /* sequenceParameterSetLength
1755 * sequenceParameterSetNALUnit */
1756 lsmash_bs_put_byte( bs, ps_count[1] ); /* numOfPictureParameterSets */
1757 h264_bs_put_parameter_sets( bs, ps_list[1], ps_count[1] ); /* pictureParameterSetLength
1758 * pictureParameterSetNALUnit */
1759 if( H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication ) )
1761 lsmash_bs_put_byte( bs, param->chroma_format | 0xfc ); /* chroma_format */
1762 lsmash_bs_put_byte( bs, param->bit_depth_luma_minus8 | 0xf8 ); /* bit_depth_luma_minus8 */
1763 lsmash_bs_put_byte( bs, param->bit_depth_chroma_minus8 | 0xf8 ); /* bit_depth_chroma_minus8 */
1764 if( ps_list[2] )
1766 lsmash_bs_put_byte( bs, ps_count[2] ); /* numOfSequenceParameterSetExt */
1767 h264_bs_put_parameter_sets( bs, ps_list[2], ps_count[2] ); /* sequenceParameterSetExtLength
1768 * sequenceParameterSetExtNALUnit */
1770 else /* no sequence parameter set extensions */
1771 lsmash_bs_put_byte( bs, 0 ); /* numOfSequenceParameterSetExt */
1773 uint8_t *data = lsmash_bs_export_data( bs, data_length );
1774 lsmash_bs_cleanup( bs );
1775 /* Update box size. */
1776 LSMASH_SET_BE32( data, *data_length );
1777 return data;
1780 static inline int h264_validate_ps_type
1782 lsmash_h264_parameter_set_type ps_type,
1783 void *ps_data,
1784 uint32_t ps_length
1787 if( !ps_data || ps_length < 2 )
1788 return LSMASH_ERR_INVALID_DATA;
1789 if( ps_type != H264_PARAMETER_SET_TYPE_SPS
1790 && ps_type != H264_PARAMETER_SET_TYPE_PPS
1791 && ps_type != H264_PARAMETER_SET_TYPE_SPSEXT )
1792 return LSMASH_ERR_INVALID_DATA;
1793 uint8_t nalu_type = *((uint8_t *)ps_data) & 0x1f;
1794 if( nalu_type != H264_NALU_TYPE_SPS
1795 && nalu_type != H264_NALU_TYPE_PPS
1796 && nalu_type != H264_NALU_TYPE_SPS_EXT )
1797 return LSMASH_ERR_INVALID_DATA;
1798 if( (ps_type == H264_PARAMETER_SET_TYPE_SPS && nalu_type != H264_NALU_TYPE_SPS)
1799 || (ps_type == H264_PARAMETER_SET_TYPE_PPS && nalu_type != H264_NALU_TYPE_PPS)
1800 || (ps_type == H264_PARAMETER_SET_TYPE_SPSEXT && nalu_type != H264_NALU_TYPE_SPS_EXT) )
1801 return LSMASH_ERR_INVALID_DATA;
1802 return 0;
1805 static lsmash_dcr_nalu_appendable h264_check_sps_appendable
1807 lsmash_bits_t *bits,
1808 uint8_t *rbsp_buffer,
1809 lsmash_h264_specific_parameters_t *param,
1810 uint8_t *ps_data,
1811 uint32_t ps_length,
1812 lsmash_entry_list_t *ps_list
1815 h264_sps_t sps;
1816 if( h264_parse_sps_minimally( bits, &sps, rbsp_buffer, ps_data + 1, ps_length - 1 ) < 0 )
1817 return DCR_NALU_APPEND_ERROR;
1818 lsmash_bits_empty( bits );
1819 /* FIXME; If the sequence parameter sets are marked with different profiles,
1820 * and the relevant profile compatibility flags are all zero,
1821 * then the stream may need examination to determine which profile, if any, the stream conforms to.
1822 * If the stream is not examined, or the examination reveals that there is no profile to which the stream conforms,
1823 * then the stream must be split into two or more sub-streams with separate configuration records in which these rules can be met. */
1824 #if 0
1825 if( sps.profile_idc != param->AVCProfileIndication && (sps->constraint_set_flags & param->profile_compatibility) )
1826 #else
1827 if( sps.profile_idc != param->AVCProfileIndication )
1828 #endif
1829 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
1830 /* The values of chroma_format_idc, bit_depth_luma_minus8 and bit_depth_chroma_minus8
1831 * must be identical in all SPSs in a single AVC configuration record. */
1832 if( H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication )
1833 && (sps.chroma_format_idc != param->chroma_format
1834 || sps.bit_depth_luma_minus8 != param->bit_depth_luma_minus8
1835 || sps.bit_depth_chroma_minus8 != param->bit_depth_chroma_minus8) )
1836 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
1837 /* Forbidden to duplicate SPS that has the same seq_parameter_set_id with different form within the same configuration record. */
1838 uint8_t sps_id = sps.seq_parameter_set_id;
1839 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
1841 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1842 if( !ps )
1843 return DCR_NALU_APPEND_ERROR;
1844 if( ps->unused )
1845 continue;
1846 uint8_t param_sps_id;
1847 if( h264_get_sps_id( ps->nalUnit + 1, ps->nalUnitLength - 1, &param_sps_id ) < 0 )
1848 return DCR_NALU_APPEND_ERROR;
1849 if( sps_id == param_sps_id )
1850 /* SPS that has the same seq_parameter_set_id already exists with different form. */
1851 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
1852 if( entry == ps_list->head )
1854 /* Check if the visual presentation sizes are different. */
1855 h264_sps_t first_sps;
1856 if( h264_parse_sps_minimally( bits, &first_sps, rbsp_buffer,
1857 ps->nalUnit + 1,
1858 ps->nalUnitLength - 1 ) < 0 )
1859 return DCR_NALU_APPEND_ERROR;
1860 if( sps.cropped_width != first_sps.cropped_width
1861 || sps.cropped_height != first_sps.cropped_height )
1862 return DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED;
1865 return DCR_NALU_APPEND_POSSIBLE;
1868 static lsmash_dcr_nalu_appendable h264_check_pps_appendable
1870 uint8_t *ps_data,
1871 uint32_t ps_length,
1872 lsmash_entry_list_t *ps_list
1875 uint8_t pps_id;
1876 if( h264_get_pps_id( ps_data + 1, ps_length - 1, &pps_id ) < 0 )
1877 return DCR_NALU_APPEND_ERROR;
1878 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
1880 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1881 if( !ps )
1882 return DCR_NALU_APPEND_ERROR;
1883 if( ps->unused )
1884 continue;
1885 uint8_t param_pps_id;
1886 if( h264_get_pps_id( ps->nalUnit + 1, ps->nalUnitLength - 1, &param_pps_id ) < 0 )
1887 return DCR_NALU_APPEND_ERROR;
1888 if( pps_id == param_pps_id )
1889 /* PPS that has the same pic_parameter_set_id already exists with different form. */
1890 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
1892 return DCR_NALU_APPEND_POSSIBLE;
1895 lsmash_dcr_nalu_appendable lsmash_check_h264_parameter_set_appendable
1897 lsmash_h264_specific_parameters_t *param,
1898 lsmash_h264_parameter_set_type ps_type,
1899 void *_ps_data,
1900 uint32_t ps_length
1903 uint8_t *ps_data = _ps_data;
1904 if( !param )
1905 return DCR_NALU_APPEND_ERROR;
1906 if( h264_validate_ps_type( ps_type, ps_data, ps_length ) < 0 )
1907 return DCR_NALU_APPEND_ERROR;
1908 if( ps_type == H264_PARAMETER_SET_TYPE_SPSEXT
1909 && !H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication ) )
1910 return DCR_NALU_APPEND_ERROR;
1911 /* Check whether the same parameter set already exsits or not. */
1912 lsmash_entry_list_t *ps_list = h264_get_parameter_set_list( param, ps_type );
1913 if( !ps_list || !ps_list->head )
1914 return DCR_NALU_APPEND_POSSIBLE; /* No parameter set */
1915 switch( nalu_check_same_ps_existence( ps_list, ps_data, ps_length ) )
1917 case 0 : break;
1918 case 1 : return DCR_NALU_APPEND_DUPLICATED; /* The same parameter set already exists. */
1919 default : return DCR_NALU_APPEND_ERROR; /* An error occured. */
1921 uint32_t ps_count;
1922 if( nalu_get_ps_count( ps_list, &ps_count ) )
1923 return DCR_NALU_APPEND_ERROR;
1924 if( (ps_type == H264_PARAMETER_SET_TYPE_SPS && ps_count >= 31)
1925 || (ps_type == H264_PARAMETER_SET_TYPE_PPS && ps_count >= 255)
1926 || (ps_type == H264_PARAMETER_SET_TYPE_SPSEXT && ps_count >= 255) )
1927 return DCR_NALU_APPEND_NEW_DCR_REQUIRED; /* No more appendable parameter sets. */
1928 if( ps_type == H264_PARAMETER_SET_TYPE_SPSEXT )
1929 return DCR_NALU_APPEND_POSSIBLE;
1930 /* Check whether a new specific info is needed or not. */
1931 if( ps_type == H264_PARAMETER_SET_TYPE_PPS )
1932 /* PPS */
1933 return h264_check_pps_appendable( ps_data, ps_length, ps_list );
1934 else
1936 /* SPS
1937 * Set up bitstream handler for parse parameter sets. */
1938 lsmash_bits_t *bits = lsmash_bits_adhoc_create();
1939 if( !bits )
1940 return DCR_NALU_APPEND_ERROR;
1941 uint32_t max_ps_length;
1942 uint8_t *rbsp_buffer;
1943 if( nalu_get_max_ps_length( ps_list, &max_ps_length ) < 0
1944 || (rbsp_buffer = lsmash_malloc( LSMASH_MAX( max_ps_length, ps_length ) )) == NULL )
1946 lsmash_bits_adhoc_cleanup( bits );
1947 return DCR_NALU_APPEND_ERROR;
1949 lsmash_dcr_nalu_appendable appendable = h264_check_sps_appendable( bits, rbsp_buffer, param, ps_data, ps_length, ps_list );
1950 lsmash_bits_adhoc_cleanup( bits );
1951 lsmash_free( rbsp_buffer );
1952 return appendable;
1956 static inline void h264_reorder_parameter_set_ascending_id
1958 lsmash_h264_specific_parameters_t *param,
1959 lsmash_h264_parameter_set_type ps_type,
1960 lsmash_entry_list_t *ps_list,
1961 uint8_t ps_id
1964 lsmash_entry_t *entry = NULL;
1965 if( ps_id )
1966 for( int i = ps_id - 1; i; i-- )
1968 entry = h264_get_ps_entry_from_param( param, ps_type, i );
1969 if( entry )
1970 break;
1972 int append_head = 0;
1973 if( !entry )
1975 /* Couldn't find any parameter set with lower identifier.
1976 * Next, find parameter set with upper identifier. */
1977 int max_ps_id = ps_type == H264_PARAMETER_SET_TYPE_SPS ? 31 : 255;
1978 for( int i = ps_id + 1; i <= max_ps_id; i++ )
1980 entry = h264_get_ps_entry_from_param( param, ps_type, i );
1981 if( entry )
1982 break;
1984 if( entry )
1985 append_head = 1;
1987 if( !entry )
1988 return; /* The new entry was appended to the tail. */
1989 lsmash_entry_t *new_entry = ps_list->tail;
1990 if( append_head )
1992 /* before: entry[i > ps_id] ... -> prev_entry -> new_entry[ps_id]
1993 * after: new_entry[ps_id] -> entry[i > ps_id] -> ... -> prev_entry */
1994 if( new_entry->prev )
1995 new_entry->prev->next = NULL;
1996 new_entry->prev = NULL;
1997 entry->prev = new_entry;
1998 new_entry->next = entry;
1999 return;
2001 /* before: entry[i < ps_id] -> next_entry -> ... -> prev_entry -> new_entry[ps_id]
2002 * after: entry[i < ps_id] -> new_entry[ps_id] -> next_entry -> ... -> prev_entry */
2003 if( new_entry->prev )
2004 new_entry->prev->next = NULL;
2005 new_entry->prev = entry;
2006 new_entry->next = entry->next;
2007 if( entry->next )
2008 entry->next->prev = new_entry;
2009 entry->next = new_entry;
2012 int lsmash_append_h264_parameter_set
2014 lsmash_h264_specific_parameters_t *param,
2015 lsmash_h264_parameter_set_type ps_type,
2016 void *_ps_data,
2017 uint32_t ps_length
2020 uint8_t *ps_data = _ps_data;
2021 if( !param || !ps_data || ps_length < 2 )
2022 return LSMASH_ERR_FUNCTION_PARAM;
2023 if( ps_type != H264_PARAMETER_SET_TYPE_SPS
2024 && ps_type != H264_PARAMETER_SET_TYPE_PPS
2025 && ps_type != H264_PARAMETER_SET_TYPE_SPSEXT )
2026 return LSMASH_ERR_FUNCTION_PARAM;
2027 if( !param->parameter_sets )
2029 param->parameter_sets = h264_allocate_parameter_sets();
2030 if( !param->parameter_sets )
2031 return LSMASH_ERR_MEMORY_ALLOC;
2033 lsmash_entry_list_t *ps_list = h264_get_parameter_set_list( param, ps_type );
2034 if( !ps_list )
2035 return LSMASH_ERR_NAMELESS;
2036 if( ps_type == H264_PARAMETER_SET_TYPE_SPSEXT )
2038 if( !H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication ) )
2039 return 0;
2040 isom_dcr_ps_entry_t *ps = isom_create_ps_entry( ps_data, ps_length );
2041 if( !ps )
2042 return LSMASH_ERR_MEMORY_ALLOC;
2043 if( lsmash_list_add_entry( ps_list, ps ) < 0 )
2045 isom_remove_dcr_ps( ps );
2046 return LSMASH_ERR_MEMORY_ALLOC;
2048 return 0;
2050 /* Check if the same parameter set identifier already exists. */
2051 uint8_t ps_id;
2052 int err = h264_get_ps_id( ps_data + 1, ps_length - 1, &ps_id, ps_type );
2053 if( err < 0 )
2054 return err;
2055 lsmash_entry_t *entry = h264_get_ps_entry_from_param( param, ps_type, ps_id );
2056 isom_dcr_ps_entry_t *ps = entry ? (isom_dcr_ps_entry_t *)entry->data : NULL;
2057 if( ps && !ps->unused )
2058 /* The same parameter set identifier already exists. */
2059 return LSMASH_ERR_FUNCTION_PARAM;
2060 int invoke_reorder;
2061 if( ps )
2063 /* Reuse an already existed parameter set in the list. */
2064 ps->unused = 0;
2065 if( ps->nalUnit != ps_data )
2067 /* The same address could be given when called by h264_update_picture_info_for_slice(). */
2068 lsmash_free( ps->nalUnit );
2069 ps->nalUnit = ps_data;
2071 ps->nalUnitLength = ps_length;
2072 invoke_reorder = 0;
2074 else
2076 /* Create a new parameter set and append it into the list. */
2077 ps = isom_create_ps_entry( ps_data, ps_length );
2078 if( !ps )
2079 return LSMASH_ERR_MEMORY_ALLOC;
2080 if( lsmash_list_add_entry( ps_list, ps ) < 0 )
2082 isom_remove_dcr_ps( ps );
2083 return LSMASH_ERR_MEMORY_ALLOC;
2085 invoke_reorder = 1;
2087 if( ps_type == H264_PARAMETER_SET_TYPE_SPS )
2089 /* Update specific info with SPS. */
2090 lsmash_bits_t *bits = lsmash_bits_adhoc_create();
2091 if( !bits )
2092 return LSMASH_ERR_MEMORY_ALLOC;
2093 uint8_t *rbsp_buffer = lsmash_malloc( ps_length );
2094 if( !rbsp_buffer )
2096 lsmash_bits_adhoc_cleanup( bits );
2097 return LSMASH_ERR_MEMORY_ALLOC;
2099 h264_sps_t sps;
2100 err = h264_parse_sps_minimally( bits, &sps, rbsp_buffer, ps_data + 1, ps_length - 1 );
2101 lsmash_bits_adhoc_cleanup( bits );
2102 lsmash_free( rbsp_buffer );
2103 if( err < 0 )
2105 lsmash_list_remove_entry_tail( ps_list );
2106 return err;
2108 if( ps_list->entry_count == 1 )
2109 param->profile_compatibility = 0xff;
2110 param->AVCProfileIndication = sps.profile_idc;
2111 param->profile_compatibility &= sps.constraint_set_flags;
2112 param->AVCLevelIndication = LSMASH_MAX( param->AVCLevelIndication, sps.level_idc );
2113 param->chroma_format = sps.chroma_format_idc;
2114 param->bit_depth_luma_minus8 = sps.bit_depth_luma_minus8;
2115 param->bit_depth_chroma_minus8 = sps.bit_depth_chroma_minus8;
2117 if( invoke_reorder )
2118 /* Add a new parameter set in order of ascending parameter set identifier. */
2119 h264_reorder_parameter_set_ascending_id( param, ps_type, ps_list, ps_id );
2120 return 0;
2123 int h264_try_to_append_parameter_set
2125 h264_info_t *info,
2126 lsmash_h264_parameter_set_type ps_type,
2127 void *_ps_data,
2128 uint32_t ps_length
2131 uint8_t *ps_data = _ps_data;
2132 lsmash_dcr_nalu_appendable ret = lsmash_check_h264_parameter_set_appendable( &info->avcC_param, ps_type, ps_data, ps_length );
2133 lsmash_h264_specific_parameters_t *param;
2134 switch( ret )
2136 case DCR_NALU_APPEND_ERROR : /* Error */
2137 return LSMASH_ERR_NAMELESS;
2138 case DCR_NALU_APPEND_NEW_DCR_REQUIRED : /* Mulitiple sample description is needed. */
2139 case DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED : /* Mulitiple sample description is needed. */
2140 param = &info->avcC_param_next;
2141 info->avcC_pending = 1;
2142 break;
2143 case DCR_NALU_APPEND_POSSIBLE : /* Appendable */
2144 param = info->avcC_pending ? &info->avcC_param_next : &info->avcC_param;
2145 break;
2146 default : /* No need to append */
2147 return 0;
2149 int err;
2150 switch( ps_type )
2152 case H264_PARAMETER_SET_TYPE_SPS :
2153 if( (err = h264_parse_sps( info, info->buffer.rbsp, ps_data + 1, ps_length - 1 )) < 0 )
2154 return err;
2155 break;
2156 case H264_PARAMETER_SET_TYPE_PPS :
2157 if( (err = h264_parse_pps( info, info->buffer.rbsp, ps_data + 1, ps_length - 1 )) < 0 )
2158 return err;
2159 break;
2160 default :
2161 break;
2163 return lsmash_append_h264_parameter_set( param, ps_type, ps_data, ps_length );
2166 static inline int h264_move_dcr_nalu_entry
2168 lsmash_h264_specific_parameters_t *dst_data,
2169 lsmash_h264_specific_parameters_t *src_data,
2170 lsmash_h264_parameter_set_type ps_type
2173 lsmash_entry_list_t *src_ps_list = h264_get_parameter_set_list( src_data, ps_type );
2174 lsmash_entry_list_t *dst_ps_list = h264_get_parameter_set_list( dst_data, ps_type );
2175 assert( src_ps_list && dst_ps_list );
2176 for( lsmash_entry_t *src_entry = src_ps_list->head; src_entry; src_entry = src_entry->next )
2178 isom_dcr_ps_entry_t *src_ps = (isom_dcr_ps_entry_t *)src_entry->data;
2179 if( !src_ps )
2180 continue;
2181 int err;
2182 uint8_t src_ps_id;
2183 if( (err = h264_get_ps_id( src_ps->nalUnit + 1, src_ps->nalUnitLength - 1, &src_ps_id, ps_type )) < 0 )
2184 return err;
2185 lsmash_entry_t *dst_entry;
2186 for( dst_entry = dst_ps_list->head; dst_entry; dst_entry = dst_entry->next )
2188 isom_dcr_ps_entry_t *dst_ps = (isom_dcr_ps_entry_t *)dst_entry->data;
2189 if( !dst_ps )
2190 continue;
2191 uint8_t dst_ps_id;
2192 if( (err = h264_get_ps_id( dst_ps->nalUnit + 1, dst_ps->nalUnitLength - 1, &dst_ps_id, ps_type )) < 0 )
2193 return err;
2194 if( dst_ps_id == src_ps_id )
2196 /* Replace the old parameter set with the new one. */
2197 assert( dst_entry->data != src_entry->data );
2198 isom_remove_dcr_ps( dst_ps );
2199 dst_entry->data = src_entry->data;
2200 src_entry->data = NULL;
2201 break;
2204 if( !dst_entry )
2206 /* Move the parameter set. */
2207 if( lsmash_list_add_entry( dst_ps_list, src_ps ) < 0 )
2208 return LSMASH_ERR_MEMORY_ALLOC;
2209 src_entry->data = NULL;
2212 return 0;
2215 int h264_move_pending_avcC_param
2217 h264_info_t *info
2220 assert( info );
2221 if( !info->avcC_pending )
2222 return 0;
2223 /* Mark 'unused' on parameter sets within the decoder configuration record. */
2224 for( int i = 0; i < H264_PARAMETER_SET_TYPE_NUM; i++ )
2226 lsmash_entry_list_t *ps_list = h264_get_parameter_set_list( &info->avcC_param, i );
2227 assert( ps_list );
2228 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
2230 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
2231 if( !ps )
2232 continue;
2233 ps->unused = 1;
2236 /* Move the new parameter sets. */
2237 int err;
2238 if( (err = h264_move_dcr_nalu_entry( &info->avcC_param, &info->avcC_param_next, H264_PARAMETER_SET_TYPE_SPS )) < 0
2239 || (err = h264_move_dcr_nalu_entry( &info->avcC_param, &info->avcC_param_next, H264_PARAMETER_SET_TYPE_PPS )) < 0 )
2240 return err;
2241 /* Move to the pending. */
2242 lsmash_h264_parameter_sets_t *parameter_sets = info->avcC_param.parameter_sets; /* Back up parameter sets. */
2243 info->avcC_param = info->avcC_param_next;
2244 info->avcC_param.parameter_sets = parameter_sets;
2245 /* No pending avcC. */
2246 h264_deallocate_parameter_sets( &info->avcC_param_next );
2247 uint8_t lengthSizeMinusOne = info->avcC_param_next.lengthSizeMinusOne;
2248 memset( &info->avcC_param_next, 0, sizeof(lsmash_h264_specific_parameters_t) );
2249 info->avcC_param_next.lengthSizeMinusOne = lengthSizeMinusOne;
2250 info->avcC_pending = 0;
2251 return 0;
2254 static int h264_parse_succeeded
2256 h264_info_t *info,
2257 lsmash_h264_specific_parameters_t *param
2260 int ret;
2261 if( info->sps.present && info->pps.present )
2263 *param = info->avcC_param;
2264 /* Avoid freeing parameter sets. */
2265 info->avcC_param.parameter_sets = NULL;
2266 ret = 0;
2268 else
2269 ret = LSMASH_ERR_INVALID_DATA;
2270 h264_cleanup_parser( info );
2271 return ret;
2274 static inline int h264_parse_failed
2276 h264_info_t *info,
2277 int ret
2280 h264_cleanup_parser( info );
2281 return ret;
2284 int lsmash_setup_h264_specific_parameters_from_access_unit
2286 lsmash_h264_specific_parameters_t *param,
2287 uint8_t *data,
2288 uint32_t data_length
2291 if( !param || !data || data_length == 0 )
2292 return LSMASH_ERR_FUNCTION_PARAM;
2293 h264_info_t *info = &(h264_info_t){ { 0 } };
2294 lsmash_bs_t *bs = &(lsmash_bs_t){ 0 };
2295 int err = lsmash_bs_set_empty_stream( bs, data, data_length );
2296 if( err < 0 )
2297 return err;
2298 uint64_t sc_head_pos = nalu_find_first_start_code( bs );
2299 if( sc_head_pos == NALU_NO_START_CODE_FOUND )
2300 return LSMASH_ERR_INVALID_DATA;
2301 else if( sc_head_pos == NALU_IO_ERROR )
2302 return LSMASH_ERR_IO;
2303 if( (err = h264_setup_parser( info, 1 )) < 0 )
2304 return h264_parse_failed( info, err );
2305 h264_stream_buffer_t *sb = &info->buffer;
2306 h264_slice_info_t *slice = &info->slice;
2307 while( 1 )
2309 h264_nalu_header_t nuh;
2310 uint64_t start_code_length;
2311 uint64_t trailing_zero_bytes;
2312 uint64_t nalu_length = h264_find_next_start_code( bs, &nuh, &start_code_length, &trailing_zero_bytes );
2313 if( nalu_length == NALU_NO_START_CODE_FOUND )
2314 /* For the last NALU. This NALU already has been parsed. */
2315 return h264_parse_succeeded( info, param );
2316 uint8_t nalu_type = nuh.nal_unit_type;
2317 uint64_t next_sc_head_pos = sc_head_pos
2318 + start_code_length
2319 + nalu_length
2320 + trailing_zero_bytes;
2321 if( nalu_type == H264_NALU_TYPE_FD )
2323 /* We don't support streams with both filler and HRD yet.
2324 * Otherwise, just skip filler because elemental streams defined in 14496-15 are forbidden to use filler. */
2325 if( info->sps.vui.hrd.present )
2326 return h264_parse_failed( info, LSMASH_ERR_PATCH_WELCOME );
2328 else if( (nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && nalu_type <= H264_NALU_TYPE_SPS_EXT)
2329 || nalu_type == H264_NALU_TYPE_SLICE_AUX )
2331 /* Increase the buffer if needed. */
2332 uint64_t possible_au_length = NALU_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
2333 if( sb->bank->buffer_size < possible_au_length
2334 && (err = h264_supplement_buffer( sb, NULL, 2 * possible_au_length )) < 0 )
2335 return h264_parse_failed( info, err );
2336 /* Get the EBSP of the current NALU here.
2337 * AVC elemental stream defined in 14496-15 can recognize from 0 to 13, and 19 of nal_unit_type.
2338 * We don't support SVC and MVC elemental stream defined in 14496-15 yet. */
2339 uint8_t *nalu = lsmash_bs_get_buffer_data( bs ) + start_code_length;
2340 if( nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && nalu_type <= H264_NALU_TYPE_SLICE_IDR )
2342 /* VCL NALU (slice) */
2343 h264_slice_info_t prev_slice = *slice;
2344 if( (err = h264_parse_slice( info, &nuh, sb->rbsp, nalu + nuh.length, nalu_length - nuh.length )) < 0 )
2345 return h264_parse_failed( info, err );
2346 if( prev_slice.present )
2348 /* Check whether the AU that contains the previous VCL NALU completed or not. */
2349 if( h264_find_au_delimit_by_slice_info( slice, &prev_slice ) )
2350 /* The current NALU is the first VCL NALU of the primary coded picture of an new AU.
2351 * Therefore, the previous slice belongs to that new AU. */
2352 return h264_parse_succeeded( info, param );
2354 slice->present = 1;
2356 else
2358 if( h264_find_au_delimit_by_nalu_type( nalu_type, info->prev_nalu_type ) )
2359 /* The last slice belongs to the AU you want at this time. */
2360 return h264_parse_succeeded( info, param );
2361 switch( nalu_type )
2363 case H264_NALU_TYPE_SPS :
2364 if( (err = h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_SPS, nalu, nalu_length )) < 0 )
2365 return h264_parse_failed( info, err );
2366 break;
2367 case H264_NALU_TYPE_PPS :
2368 if( (err = h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_PPS, nalu, nalu_length )) < 0 )
2369 return h264_parse_failed( info, err );
2370 break;
2371 case H264_NALU_TYPE_SPS_EXT :
2372 if( (err = h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_SPSEXT, nalu, nalu_length )) < 0 )
2373 return h264_parse_failed( info, err );
2374 break;
2375 default :
2376 break;
2380 /* Move to the first byte of the next start code. */
2381 info->prev_nalu_type = nalu_type;
2382 if( lsmash_bs_read_seek( bs, next_sc_head_pos, SEEK_SET ) != next_sc_head_pos )
2383 return h264_parse_failed( info, LSMASH_ERR_NAMELESS );
2384 /* Check if no more data to read from the stream. */
2385 if( !lsmash_bs_is_end( bs, NALU_SHORT_START_CODE_LENGTH ) )
2386 sc_head_pos = next_sc_head_pos;
2387 else
2388 return h264_parse_succeeded( info, param );
2392 int h264_construct_specific_parameters
2394 lsmash_codec_specific_t *dst,
2395 lsmash_codec_specific_t *src
2398 assert( dst && dst->data.structured && src && src->data.unstructured );
2399 if( src->size < ISOM_BASEBOX_COMMON_SIZE + 7 )
2400 return LSMASH_ERR_INVALID_DATA;
2401 lsmash_h264_specific_parameters_t *param = (lsmash_h264_specific_parameters_t *)dst->data.structured;
2402 uint8_t *data = src->data.unstructured;
2403 uint64_t size = LSMASH_GET_BE32( data );
2404 data += ISOM_BASEBOX_COMMON_SIZE;
2405 if( size == 1 )
2407 size = LSMASH_GET_BE64( data );
2408 data += 8;
2410 if( size != src->size )
2411 return LSMASH_ERR_INVALID_DATA;
2412 if( !param->parameter_sets )
2414 param->parameter_sets = h264_allocate_parameter_sets();
2415 if( !param->parameter_sets )
2416 return LSMASH_ERR_MEMORY_ALLOC;
2418 lsmash_bs_t *bs = lsmash_bs_create();
2419 if( !bs )
2420 return LSMASH_ERR_MEMORY_ALLOC;
2421 int err = lsmash_bs_import_data( bs, data, src->size - (data - src->data.unstructured) );
2422 if( err < 0 )
2423 goto fail;
2424 if( lsmash_bs_get_byte( bs ) != 1 )
2426 /* We don't support configurationVersion other than 1. */
2427 err = LSMASH_ERR_INVALID_DATA;
2428 goto fail;
2430 param->AVCProfileIndication = lsmash_bs_get_byte( bs );
2431 param->profile_compatibility = lsmash_bs_get_byte( bs );
2432 param->AVCLevelIndication = lsmash_bs_get_byte( bs );
2433 param->lengthSizeMinusOne = lsmash_bs_get_byte( bs ) & 0x03;
2434 uint8_t numOfSequenceParameterSets = lsmash_bs_get_byte( bs ) & 0x1F;
2435 if( numOfSequenceParameterSets
2436 && (err = nalu_get_dcr_ps( bs, param->parameter_sets->sps_list, numOfSequenceParameterSets )) < 0 )
2437 goto fail;
2438 uint8_t numOfPictureParameterSets = lsmash_bs_get_byte( bs );
2439 if( numOfPictureParameterSets
2440 && (err = nalu_get_dcr_ps( bs, param->parameter_sets->pps_list, numOfPictureParameterSets )) < 0 )
2441 goto fail;
2442 if( H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication ) )
2444 param->chroma_format = lsmash_bs_get_byte( bs ) & 0x03;
2445 param->bit_depth_luma_minus8 = lsmash_bs_get_byte( bs ) & 0x07;
2446 param->bit_depth_chroma_minus8 = lsmash_bs_get_byte( bs ) & 0x07;
2447 uint8_t numOfSequenceParameterSetExt = lsmash_bs_get_byte( bs );
2448 if( numOfSequenceParameterSetExt
2449 && (err = nalu_get_dcr_ps( bs, param->parameter_sets->spsext_list, numOfSequenceParameterSetExt )) < 0 )
2450 goto fail;
2452 lsmash_bs_cleanup( bs );
2453 return 0;
2454 fail:
2455 lsmash_bs_cleanup( bs );
2456 return err;
2459 int h264_print_codec_specific
2461 FILE *fp,
2462 lsmash_file_t *file,
2463 isom_box_t *box,
2464 int level
2467 assert( box->manager & LSMASH_BINARY_CODED_BOX );
2468 int indent = level;
2469 lsmash_ifprintf( fp, indent++, "[%s: AVC Configuration Box]\n", isom_4cc2str( box->type.fourcc ) );
2470 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
2471 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
2472 uint8_t *data = box->binary;
2473 uint32_t offset = isom_skip_box_common( &data );
2474 lsmash_bs_t *bs = lsmash_bs_create();
2475 if( !bs )
2476 return LSMASH_ERR_MEMORY_ALLOC;
2477 int err = lsmash_bs_import_data( bs, data, box->size - offset );
2478 if( err < 0 )
2480 lsmash_bs_cleanup( bs );
2481 return err;
2483 lsmash_ifprintf( fp, indent, "configurationVersion = %"PRIu8"\n", lsmash_bs_get_byte( bs ) );
2484 uint8_t AVCProfileIndication = lsmash_bs_get_byte( bs );
2485 lsmash_ifprintf( fp, indent, "AVCProfileIndication = %"PRIu8"\n", AVCProfileIndication );
2486 lsmash_ifprintf( fp, indent, "profile_compatibility = 0x%02"PRIx8"\n", lsmash_bs_get_byte( bs ) );
2487 lsmash_ifprintf( fp, indent, "AVCLevelIndication = %"PRIu8"\n", lsmash_bs_get_byte( bs ) );
2488 uint8_t temp8 = lsmash_bs_get_byte( bs );
2489 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 2) & 0x3F );
2490 lsmash_ifprintf( fp, indent, "lengthSizeMinusOne = %"PRIu8"\n", temp8 & 0x03 );
2491 temp8 = lsmash_bs_get_byte( bs );
2492 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 5) & 0x07 );
2493 uint8_t numOfSequenceParameterSets = temp8 & 0x1f;
2494 lsmash_ifprintf( fp, indent, "numOfSequenceParameterSets = %"PRIu8"\n", numOfSequenceParameterSets );
2495 for( uint8_t i = 0; i < numOfSequenceParameterSets; i++ )
2497 uint16_t nalUnitLength = lsmash_bs_get_be16( bs );
2498 lsmash_bs_skip_bytes( bs, nalUnitLength );
2500 uint8_t numOfPictureParameterSets = lsmash_bs_get_byte( bs );
2501 lsmash_ifprintf( fp, indent, "numOfPictureParameterSets = %"PRIu8"\n", numOfPictureParameterSets );
2502 for( uint8_t i = 0; i < numOfPictureParameterSets; i++ )
2504 uint16_t nalUnitLength = lsmash_bs_get_be16( bs );
2505 lsmash_bs_skip_bytes( bs, nalUnitLength );
2507 /* Note: there are too many files, in the world, that don't contain the following fields. */
2508 if( H264_REQUIRES_AVCC_EXTENSION( AVCProfileIndication )
2509 && (lsmash_bs_get_pos( bs ) < (box->size - offset)) )
2511 temp8 = lsmash_bs_get_byte( bs );
2512 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 2) & 0x3F );
2513 lsmash_ifprintf( fp, indent, "chroma_format = %"PRIu8"\n", temp8 & 0x03 );
2514 temp8 = lsmash_bs_get_byte( bs );
2515 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 3) & 0x1F );
2516 lsmash_ifprintf( fp, indent, "bit_depth_luma_minus8 = %"PRIu8"\n", temp8 & 0x7 );
2517 temp8 = lsmash_bs_get_byte( bs );
2518 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 3) & 0x1F );
2519 lsmash_ifprintf( fp, indent, "bit_depth_chroma_minus8 = %"PRIu8"\n", temp8 & 0x7 );
2520 lsmash_ifprintf( fp, indent, "numOfSequenceParameterSetExt = %"PRIu8"\n", lsmash_bs_get_byte( bs ) );
2522 lsmash_bs_cleanup( bs );
2523 return 0;
2526 int h264_copy_codec_specific
2528 lsmash_codec_specific_t *dst,
2529 lsmash_codec_specific_t *src
2532 assert( src && src->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && src->data.structured );
2533 assert( dst && dst->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && dst->data.structured );
2534 lsmash_h264_specific_parameters_t *src_data = (lsmash_h264_specific_parameters_t *)src->data.structured;
2535 lsmash_h264_specific_parameters_t *dst_data = (lsmash_h264_specific_parameters_t *)dst->data.structured;
2536 h264_deallocate_parameter_sets( dst_data );
2537 *dst_data = *src_data;
2538 if( !src_data->parameter_sets )
2539 return 0;
2540 dst_data->parameter_sets = h264_allocate_parameter_sets();
2541 if( !dst_data->parameter_sets )
2542 return LSMASH_ERR_MEMORY_ALLOC;
2543 for( int i = 0; i < 3; i++ )
2545 lsmash_entry_list_t *src_ps_list = h264_get_parameter_set_list( src_data, i );
2546 lsmash_entry_list_t *dst_ps_list = h264_get_parameter_set_list( dst_data, i );
2547 assert( src_ps_list && dst_ps_list );
2548 for( lsmash_entry_t *entry = src_ps_list->head; entry; entry = entry->next )
2550 isom_dcr_ps_entry_t *src_ps = (isom_dcr_ps_entry_t *)entry->data;
2551 if( !src_ps || src_ps->unused )
2552 continue;
2553 isom_dcr_ps_entry_t *dst_ps = isom_create_ps_entry( src_ps->nalUnit, src_ps->nalUnitLength );
2554 if( !dst_ps )
2556 h264_deallocate_parameter_sets( dst_data );
2557 return LSMASH_ERR_MEMORY_ALLOC;
2559 if( lsmash_list_add_entry( dst_ps_list, dst_ps ) < 0 )
2561 h264_deallocate_parameter_sets( dst_data );
2562 isom_remove_dcr_ps( dst_ps );
2563 return LSMASH_ERR_MEMORY_ALLOC;
2567 return 0;
2570 int h264_print_bitrate
2572 FILE *fp,
2573 lsmash_file_t *file,
2574 isom_box_t *box,
2575 int level
2578 assert( fp && LSMASH_IS_EXISTING_BOX( file ) && LSMASH_IS_EXISTING_BOX( box ) );
2579 int indent = level;
2580 lsmash_ifprintf( fp, indent++, "[%s: MPEG-4 Bit Rate Box]\n", isom_4cc2str( box->type.fourcc ) );
2581 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
2582 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
2583 isom_btrt_t *btrt = (isom_btrt_t *)box;
2584 lsmash_ifprintf( fp, indent, "bufferSizeDB = %"PRIu32"\n", btrt->bufferSizeDB );
2585 lsmash_ifprintf( fp, indent, "maxBitrate = %"PRIu32"\n", btrt->maxBitrate );
2586 lsmash_ifprintf( fp, indent, "avgBitrate = %"PRIu32"\n", btrt->avgBitrate );
2587 return 0;