list: Decide the entry eliminator of list at its initialization.
[L-SMASH.git] / codecs / h264.c
blob183313a82fb372fde57199884de2116d3781ecd7
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 int err = nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, ebsp_size );
545 if( err < 0 )
546 return err;
547 memset( sps, 0, sizeof(h264_sps_t) );
548 sps->profile_idc = lsmash_bits_get( bits, 8 );
549 sps->constraint_set_flags = lsmash_bits_get( bits, 8 );
550 sps->level_idc = lsmash_bits_get( bits, 8 );
551 uint64_t seq_parameter_set_id = nalu_get_exp_golomb_ue( bits );
552 if( seq_parameter_set_id > 31 )
553 return LSMASH_ERR_INVALID_DATA;
554 sps->seq_parameter_set_id = seq_parameter_set_id;
555 if( sps->profile_idc == 100 || sps->profile_idc == 110 || sps->profile_idc == 122
556 || sps->profile_idc == 244 || sps->profile_idc == 44 || sps->profile_idc == 83
557 || sps->profile_idc == 86 || sps->profile_idc == 118 || sps->profile_idc == 128
558 || sps->profile_idc == 138 )
560 sps->chroma_format_idc = nalu_get_exp_golomb_ue( bits );
561 if( sps->chroma_format_idc == 3 )
562 sps->separate_colour_plane_flag = lsmash_bits_get( bits, 1 );
563 uint64_t bit_depth_luma_minus8 = nalu_get_exp_golomb_ue( bits );
564 if( bit_depth_luma_minus8 > 6 )
565 return LSMASH_ERR_INVALID_DATA;
566 uint64_t bit_depth_chroma_minus8 = nalu_get_exp_golomb_ue( bits );
567 if( bit_depth_chroma_minus8 > 6 )
568 return LSMASH_ERR_INVALID_DATA;
569 sps->bit_depth_luma_minus8 = bit_depth_luma_minus8;
570 sps->bit_depth_chroma_minus8 = bit_depth_chroma_minus8;
571 lsmash_bits_get( bits, 1 ); /* qpprime_y_zero_transform_bypass_flag */
572 if( lsmash_bits_get( bits, 1 ) ) /* seq_scaling_matrix_present_flag */
574 int num_loops = sps->chroma_format_idc != 3 ? 8 : 12;
575 for( int i = 0; i < num_loops; i++ )
576 if( lsmash_bits_get( bits, 1 ) /* seq_scaling_list_present_flag[i] */
577 && (err = h264_parse_scaling_list( bits, i < 6 ? 16 : 64 )) < 0 )
578 return err;
581 else
583 sps->chroma_format_idc = 1;
584 sps->separate_colour_plane_flag = 0;
585 sps->bit_depth_luma_minus8 = 0;
586 sps->bit_depth_chroma_minus8 = 0;
588 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
591 int h264_parse_sps
593 h264_info_t *info,
594 uint8_t *rbsp_buffer,
595 uint8_t *ebsp,
596 uint64_t ebsp_size
599 lsmash_bits_t *bits = info->bits;
600 /* seq_parameter_set_data() */
601 h264_sps_t temp_sps;
602 int err = h264_parse_sps_minimally( bits, &temp_sps, rbsp_buffer, ebsp, ebsp_size );
603 if( err < 0 )
604 return err;
605 h264_sps_t *sps = h264_get_sps( info->sps_list, temp_sps.seq_parameter_set_id );
606 if( !sps )
607 return LSMASH_ERR_NAMELESS;
608 memset( sps, 0, sizeof(h264_sps_t) );
609 sps->profile_idc = temp_sps.profile_idc;
610 sps->constraint_set_flags = temp_sps.constraint_set_flags;
611 sps->level_idc = temp_sps.level_idc;
612 sps->seq_parameter_set_id = temp_sps.seq_parameter_set_id;
613 sps->chroma_format_idc = temp_sps.chroma_format_idc;
614 sps->separate_colour_plane_flag = temp_sps.separate_colour_plane_flag;
615 sps->bit_depth_luma_minus8 = temp_sps.bit_depth_luma_minus8;
616 sps->bit_depth_chroma_minus8 = temp_sps.bit_depth_chroma_minus8;
617 sps->ChromaArrayType = sps->separate_colour_plane_flag ? 0 : sps->chroma_format_idc;
618 uint64_t log2_max_frame_num_minus4 = nalu_get_exp_golomb_ue( bits );
619 if( log2_max_frame_num_minus4 > 12 )
620 return LSMASH_ERR_INVALID_DATA;
621 sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4;
622 sps->MaxFrameNum = 1 << sps->log2_max_frame_num;
623 uint64_t pic_order_cnt_type = nalu_get_exp_golomb_ue( bits );
624 if( pic_order_cnt_type > 2 )
625 return LSMASH_ERR_INVALID_DATA;
626 sps->pic_order_cnt_type = pic_order_cnt_type;
627 if( sps->pic_order_cnt_type == 0 )
629 uint64_t log2_max_pic_order_cnt_lsb_minus4 = nalu_get_exp_golomb_ue( bits );
630 if( log2_max_pic_order_cnt_lsb_minus4 > 12 )
631 return LSMASH_ERR_INVALID_DATA;
632 sps->log2_max_pic_order_cnt_lsb = log2_max_pic_order_cnt_lsb_minus4 + 4;
633 sps->MaxPicOrderCntLsb = 1 << sps->log2_max_pic_order_cnt_lsb;
635 else if( sps->pic_order_cnt_type == 1 )
637 sps->delta_pic_order_always_zero_flag = lsmash_bits_get( bits, 1 );
638 static const int64_t max_value = (signed)(((uint64_t)1 << 31) - 1);
639 static const int64_t min_value = -(signed)(((uint64_t)1 << 31) - 1);
640 int64_t offset_for_non_ref_pic = nalu_get_exp_golomb_se( bits );
641 if( offset_for_non_ref_pic < min_value || offset_for_non_ref_pic > max_value )
642 return LSMASH_ERR_INVALID_DATA;
643 sps->offset_for_non_ref_pic = offset_for_non_ref_pic;
644 int64_t offset_for_top_to_bottom_field = nalu_get_exp_golomb_se( bits );
645 if( offset_for_top_to_bottom_field < min_value || offset_for_top_to_bottom_field > max_value )
646 return LSMASH_ERR_INVALID_DATA;
647 sps->offset_for_top_to_bottom_field = offset_for_top_to_bottom_field;
648 uint64_t num_ref_frames_in_pic_order_cnt_cycle = nalu_get_exp_golomb_ue( bits );
649 if( num_ref_frames_in_pic_order_cnt_cycle > 255 )
650 return LSMASH_ERR_INVALID_DATA;
651 sps->num_ref_frames_in_pic_order_cnt_cycle = num_ref_frames_in_pic_order_cnt_cycle;
652 sps->ExpectedDeltaPerPicOrderCntCycle = 0;
653 for( int i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++ )
655 int64_t offset_for_ref_frame = nalu_get_exp_golomb_se( bits );
656 if( offset_for_ref_frame < min_value || offset_for_ref_frame > max_value )
657 return LSMASH_ERR_INVALID_DATA;
658 sps->offset_for_ref_frame[i] = offset_for_ref_frame;
659 sps->ExpectedDeltaPerPicOrderCntCycle += offset_for_ref_frame;
662 sps->max_num_ref_frames = nalu_get_exp_golomb_ue( bits );
663 lsmash_bits_get( bits, 1 ); /* gaps_in_frame_num_value_allowed_flag */
664 uint64_t pic_width_in_mbs_minus1 = nalu_get_exp_golomb_ue( bits );
665 uint64_t pic_height_in_map_units_minus1 = nalu_get_exp_golomb_ue( bits );
666 sps->frame_mbs_only_flag = lsmash_bits_get( bits, 1 );
667 if( !sps->frame_mbs_only_flag )
668 lsmash_bits_get( bits, 1 ); /* mb_adaptive_frame_field_flag */
669 lsmash_bits_get( bits, 1 ); /* direct_8x8_inference_flag */
670 uint64_t PicWidthInMbs = pic_width_in_mbs_minus1 + 1;
671 uint64_t PicHeightInMapUnits = pic_height_in_map_units_minus1 + 1;
672 sps->PicSizeInMapUnits = PicWidthInMbs * PicHeightInMapUnits;
673 sps->cropped_width = PicWidthInMbs * 16;
674 sps->cropped_height = (2 - sps->frame_mbs_only_flag) * PicHeightInMapUnits * 16;
675 if( lsmash_bits_get( bits, 1 ) ) /* frame_cropping_flag */
677 uint8_t CropUnitX;
678 uint8_t CropUnitY;
679 if( sps->ChromaArrayType == 0 )
681 CropUnitX = 1;
682 CropUnitY = 2 - sps->frame_mbs_only_flag;
684 else
686 static const int SubWidthC [] = { 0, 2, 2, 1 };
687 static const int SubHeightC[] = { 0, 2, 1, 1 };
688 CropUnitX = SubWidthC [ sps->chroma_format_idc ];
689 CropUnitY = SubHeightC[ sps->chroma_format_idc ] * (2 - sps->frame_mbs_only_flag);
691 uint64_t frame_crop_left_offset = nalu_get_exp_golomb_ue( bits );
692 uint64_t frame_crop_right_offset = nalu_get_exp_golomb_ue( bits );
693 uint64_t frame_crop_top_offset = nalu_get_exp_golomb_ue( bits );
694 uint64_t frame_crop_bottom_offset = nalu_get_exp_golomb_ue( bits );
695 sps->cropped_width -= (frame_crop_left_offset + frame_crop_right_offset) * CropUnitX;
696 sps->cropped_height -= (frame_crop_top_offset + frame_crop_bottom_offset) * CropUnitY;
698 if( lsmash_bits_get( bits, 1 ) ) /* vui_parameters_present_flag */
700 /* vui_parameters() */
701 if( lsmash_bits_get( bits, 1 ) ) /* aspect_ratio_info_present_flag */
703 uint8_t aspect_ratio_idc = lsmash_bits_get( bits, 8 );
704 if( aspect_ratio_idc == 255 )
706 /* Extended_SAR */
707 sps->vui.sar_width = lsmash_bits_get( bits, 16 );
708 sps->vui.sar_height = lsmash_bits_get( bits, 16 );
710 else
712 static const struct
714 uint16_t sar_width;
715 uint16_t sar_height;
716 } pre_defined_sar[]
718 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
719 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 }, { 80, 33 },
720 { 18, 11 }, { 15, 11 }, { 64, 33 }, { 160, 99 }, { 4, 3 },
721 { 3, 2 }, { 2, 1 }
723 if( aspect_ratio_idc < (sizeof(pre_defined_sar) / sizeof(pre_defined_sar[0])) )
725 sps->vui.sar_width = pre_defined_sar[ aspect_ratio_idc ].sar_width;
726 sps->vui.sar_height = pre_defined_sar[ aspect_ratio_idc ].sar_height;
728 else
730 /* Behavior when unknown aspect_ratio_idc is detected is not specified in the specification. */
731 sps->vui.sar_width = 0;
732 sps->vui.sar_height = 0;
736 if( lsmash_bits_get( bits, 1 ) ) /* overscan_info_present_flag */
737 lsmash_bits_get( bits, 1 ); /* overscan_appropriate_flag */
738 if( lsmash_bits_get( bits, 1 ) ) /* video_signal_type_present_flag */
740 lsmash_bits_get( bits, 3 ); /* video_format */
741 sps->vui.video_full_range_flag = lsmash_bits_get( bits, 1 );
742 if( lsmash_bits_get( bits, 1 ) ) /* colour_description_present_flag */
744 sps->vui.colour_primaries = lsmash_bits_get( bits, 8 );
745 sps->vui.transfer_characteristics = lsmash_bits_get( bits, 8 );
746 sps->vui.matrix_coefficients = lsmash_bits_get( bits, 8 );
749 if( lsmash_bits_get( bits, 1 ) ) /* chroma_loc_info_present_flag */
751 nalu_get_exp_golomb_ue( bits ); /* chroma_sample_loc_type_top_field */
752 nalu_get_exp_golomb_ue( bits ); /* chroma_sample_loc_type_bottom_field */
754 if( lsmash_bits_get( bits, 1 ) ) /* timing_info_present_flag */
756 sps->vui.num_units_in_tick = lsmash_bits_get( bits, 32 );
757 sps->vui.time_scale = lsmash_bits_get( bits, 32 );
758 sps->vui.fixed_frame_rate_flag = lsmash_bits_get( bits, 1 );
760 else
762 sps->vui.num_units_in_tick = 1; /* arbitrary */
763 sps->vui.time_scale = 50; /* arbitrary */
764 sps->vui.fixed_frame_rate_flag = 0;
766 int nal_hrd_parameters_present_flag = lsmash_bits_get( bits, 1 );
767 if( nal_hrd_parameters_present_flag
768 && (err = h264_parse_hrd_parameters( bits, &sps->vui.hrd )) < 0 )
769 return err;
770 int vcl_hrd_parameters_present_flag = lsmash_bits_get( bits, 1 );
771 if( vcl_hrd_parameters_present_flag
772 && (err = h264_parse_hrd_parameters( bits, &sps->vui.hrd )) < 0 )
773 return err;
774 if( nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag )
776 sps->vui.hrd.present = 1;
777 sps->vui.hrd.CpbDpbDelaysPresentFlag = 1;
778 lsmash_bits_get( bits, 1 ); /* low_delay_hrd_flag */
780 sps->vui.pic_struct_present_flag = lsmash_bits_get( bits, 1 );
781 if( lsmash_bits_get( bits, 1 ) ) /* bitstream_restriction_flag */
783 lsmash_bits_get( bits, 1 ); /* motion_vectors_over_pic_boundaries_flag */
784 nalu_get_exp_golomb_ue( bits ); /* max_bytes_per_pic_denom */
785 nalu_get_exp_golomb_ue( bits ); /* max_bits_per_mb_denom */
786 nalu_get_exp_golomb_ue( bits ); /* log2_max_mv_length_horizontal */
787 nalu_get_exp_golomb_ue( bits ); /* log2_max_mv_length_vertical */
788 nalu_get_exp_golomb_ue( bits ); /* max_num_reorder_frames */
789 nalu_get_exp_golomb_ue( bits ); /* max_dec_frame_buffering */
792 else
794 sps->vui.video_full_range_flag = 0;
795 sps->vui.num_units_in_tick = 1; /* arbitrary */
796 sps->vui.time_scale = 50; /* arbitrary */
797 sps->vui.fixed_frame_rate_flag = 0;
799 /* rbsp_trailing_bits() */
800 if( !lsmash_bits_get( bits, 1 ) ) /* rbsp_stop_one_bit */
801 return LSMASH_ERR_INVALID_DATA;
802 lsmash_bits_empty( bits );
803 if( bits->bs->error )
804 return LSMASH_ERR_NAMELESS;
805 sps->present = 1;
806 info->sps = *sps;
807 return 0;
810 static int h264_parse_pps_minimally
812 lsmash_bits_t *bits,
813 h264_pps_t *pps,
814 uint8_t *rbsp_buffer,
815 uint8_t *ebsp,
816 uint64_t ebsp_size
819 int err = nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, ebsp_size );
820 if( err < 0 )
821 return err;
822 memset( pps, 0, sizeof(h264_pps_t) );
823 uint64_t pic_parameter_set_id = nalu_get_exp_golomb_ue( bits );
824 if( pic_parameter_set_id > 255 )
825 return LSMASH_ERR_INVALID_DATA;
826 pps->pic_parameter_set_id = pic_parameter_set_id;
827 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
830 int h264_parse_pps
832 h264_info_t *info,
833 uint8_t *rbsp_buffer,
834 uint8_t *ebsp,
835 uint64_t ebsp_size
838 lsmash_bits_t *bits = info->bits;
839 /* pic_parameter_set_rbsp */
840 h264_pps_t temp_pps;
841 int err = h264_parse_pps_minimally( bits, &temp_pps, rbsp_buffer, ebsp, ebsp_size );
842 if( err < 0 )
843 return err;
844 h264_pps_t *pps = h264_get_pps( info->pps_list, temp_pps.pic_parameter_set_id );
845 if( !pps )
846 return LSMASH_ERR_NAMELESS;
847 memset( pps, 0, sizeof(h264_pps_t) );
848 pps->pic_parameter_set_id = temp_pps.pic_parameter_set_id;
849 uint64_t seq_parameter_set_id = nalu_get_exp_golomb_ue( bits );
850 if( seq_parameter_set_id > 31 )
851 return LSMASH_ERR_INVALID_DATA;
852 h264_sps_t *sps = h264_get_sps( info->sps_list, seq_parameter_set_id );
853 if( !sps )
854 return LSMASH_ERR_NAMELESS;
855 pps->seq_parameter_set_id = seq_parameter_set_id;
856 pps->entropy_coding_mode_flag = lsmash_bits_get( bits, 1 );
857 pps->bottom_field_pic_order_in_frame_present_flag = lsmash_bits_get( bits, 1 );
858 uint64_t num_slice_groups_minus1 = nalu_get_exp_golomb_ue( bits );
859 if( num_slice_groups_minus1 > 7 )
860 return LSMASH_ERR_INVALID_DATA;
861 pps->num_slice_groups_minus1 = num_slice_groups_minus1;
862 if( num_slice_groups_minus1 ) /* num_slice_groups_minus1 */
864 uint64_t slice_group_map_type = nalu_get_exp_golomb_ue( bits );
865 if( slice_group_map_type > 6 )
866 return LSMASH_ERR_INVALID_DATA;
867 pps->slice_group_map_type = slice_group_map_type;
868 if( slice_group_map_type == 0 )
869 for( uint64_t iGroup = 0; iGroup <= num_slice_groups_minus1; iGroup++ )
870 nalu_get_exp_golomb_ue( bits ); /* run_length_minus1[ iGroup ] */
871 else if( slice_group_map_type == 2 )
872 for( uint64_t iGroup = 0; iGroup < num_slice_groups_minus1; iGroup++ )
874 nalu_get_exp_golomb_ue( bits ); /* top_left [ iGroup ] */
875 nalu_get_exp_golomb_ue( bits ); /* bottom_right[ iGroup ] */
877 else if( slice_group_map_type == 3
878 || slice_group_map_type == 4
879 || slice_group_map_type == 5 )
881 lsmash_bits_get( bits, 1 ); /* slice_group_change_direction_flag */
882 uint64_t slice_group_change_rate_minus1 = nalu_get_exp_golomb_ue( bits );
883 if( slice_group_change_rate_minus1 > (sps->PicSizeInMapUnits - 1) )
884 return LSMASH_ERR_INVALID_DATA;
885 pps->SliceGroupChangeRate = slice_group_change_rate_minus1 + 1;
887 else if( slice_group_map_type == 6 )
889 uint64_t pic_size_in_map_units_minus1 = nalu_get_exp_golomb_ue( bits );
890 int length = lsmash_ceil_log2( num_slice_groups_minus1 + 1 );
891 for( uint64_t i = 0; i <= pic_size_in_map_units_minus1; i++ )
892 /* slice_group_id */
893 if( lsmash_bits_get( bits, length ) > num_slice_groups_minus1 )
894 return LSMASH_ERR_INVALID_DATA;
897 pps->num_ref_idx_l0_default_active_minus1 = nalu_get_exp_golomb_ue( bits );
898 pps->num_ref_idx_l1_default_active_minus1 = nalu_get_exp_golomb_ue( bits );
899 pps->weighted_pred_flag = lsmash_bits_get( bits, 1 );
900 pps->weighted_bipred_idc = lsmash_bits_get( bits, 2 );
901 nalu_get_exp_golomb_se( bits ); /* pic_init_qp_minus26 */
902 nalu_get_exp_golomb_se( bits ); /* pic_init_qs_minus26 */
903 nalu_get_exp_golomb_se( bits ); /* chroma_qp_index_offset */
904 pps->deblocking_filter_control_present_flag = lsmash_bits_get( bits, 1 );
905 lsmash_bits_get( bits, 1 ); /* constrained_intra_pred_flag */
906 pps->redundant_pic_cnt_present_flag = lsmash_bits_get( bits, 1 );
907 if( nalu_check_more_rbsp_data( bits ) )
909 int transform_8x8_mode_flag = lsmash_bits_get( bits, 1 );
910 if( lsmash_bits_get( bits, 1 ) ) /* pic_scaling_matrix_present_flag */
912 int num_loops = 6 + (sps->chroma_format_idc != 3 ? 2 : 6) * transform_8x8_mode_flag;
913 for( int i = 0; i < num_loops; i++ )
914 if( lsmash_bits_get( bits, 1 ) /* pic_scaling_list_present_flag[i] */
915 && (err = h264_parse_scaling_list( bits, i < 6 ? 16 : 64 )) < 0 )
916 return err;
918 nalu_get_exp_golomb_se( bits ); /* second_chroma_qp_index_offset */
920 /* rbsp_trailing_bits() */
921 if( !lsmash_bits_get( bits, 1 ) ) /* rbsp_stop_one_bit */
922 return LSMASH_ERR_INVALID_DATA;
923 lsmash_bits_empty( bits );
924 if( bits->bs->error )
925 return LSMASH_ERR_NAMELESS;
926 pps->present = 1;
927 info->sps = *sps;
928 info->pps = *pps;
929 return 0;
932 int h264_parse_sei
934 lsmash_bits_t *bits,
935 h264_sps_t *sps,
936 h264_sei_t *sei,
937 uint8_t *rbsp_buffer,
938 uint8_t *ebsp,
939 uint64_t ebsp_size
942 int err = nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, ebsp_size );
943 if( err < 0 )
944 return err;
945 uint8_t *rbsp_start = rbsp_buffer;
946 uint64_t rbsp_pos = 0;
949 /* sei_message() */
950 uint32_t payloadType = 0;
951 for( uint8_t temp = lsmash_bits_get( bits, 8 ); ; temp = lsmash_bits_get( bits, 8 ) )
953 /* 0xff : ff_byte
954 * otherwise: last_payload_type_byte */
955 payloadType += temp;
956 ++rbsp_pos;
957 if( temp != 0xff )
958 break;
960 uint32_t payloadSize = 0;
961 for( uint8_t temp = lsmash_bits_get( bits, 8 ); ; temp = lsmash_bits_get( bits, 8 ) )
963 /* 0xff : ff_byte
964 * otherwise: last_payload_size_byte */
965 payloadSize += temp;
966 ++rbsp_pos;
967 if( temp != 0xff )
968 break;
970 if( payloadType == 1 )
972 /* pic_timing */
973 h264_hrd_t *hrd = sps ? &sps->vui.hrd : NULL;
974 if( !hrd )
975 goto skip_sei_message; /* Any active SPS is not found. */
976 sei->pic_timing.present = 1;
977 if( hrd->CpbDpbDelaysPresentFlag )
979 lsmash_bits_get( bits, hrd->cpb_removal_delay_length ); /* cpb_removal_delay */
980 lsmash_bits_get( bits, hrd->dpb_output_delay_length ); /* dpb_output_delay */
982 if( sps->vui.pic_struct_present_flag )
984 sei->pic_timing.pic_struct = lsmash_bits_get( bits, 4 );
985 /* Skip the remaining bits. */
986 uint32_t remaining_bits = payloadSize * 8 - 4;
987 if( hrd->CpbDpbDelaysPresentFlag )
988 remaining_bits -= hrd->cpb_removal_delay_length
989 + hrd->dpb_output_delay_length;
990 lsmash_bits_get( bits, remaining_bits );
993 else if( payloadType == 3 )
995 /* filler_payload
996 * 'avc1' and 'avc2' samples are forbidden to contain this. */
997 return LSMASH_ERR_PATCH_WELCOME;
999 else if( payloadType == 6 )
1001 /* recovery_point */
1002 sei->recovery_point.present = 1;
1003 sei->recovery_point.random_accessible = 1;
1004 sei->recovery_point.recovery_frame_cnt = nalu_get_exp_golomb_ue( bits );
1005 lsmash_bits_get( bits, 1 ); /* exact_match_flag */
1006 sei->recovery_point.broken_link_flag = lsmash_bits_get( bits, 1 );
1007 lsmash_bits_get( bits, 2 ); /* changing_slice_group_idc */
1009 else
1011 skip_sei_message:
1012 lsmash_bits_get( bits, payloadSize * 8 );
1014 lsmash_bits_get_align( bits );
1015 rbsp_pos += payloadSize;
1016 } while( *(rbsp_start + rbsp_pos) != 0x80 ); /* All SEI messages are byte aligned at their end.
1017 * Therefore, 0x80 shall be rbsp_trailing_bits(). */
1018 lsmash_bits_empty( bits );
1019 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
1022 static int h264_parse_slice_header
1024 h264_info_t *info,
1025 h264_nalu_header_t *nuh
1028 h264_slice_info_t *slice = &info->slice;
1029 memset( slice, 0, sizeof(h264_slice_info_t) );
1030 /* slice_header() */
1031 lsmash_bits_t *bits = info->bits;
1032 nalu_get_exp_golomb_ue( bits ); /* first_mb_in_slice */
1033 uint8_t slice_type = slice->type = nalu_get_exp_golomb_ue( bits );
1034 if( (uint64_t)slice->type > 9 )
1035 return LSMASH_ERR_INVALID_DATA;
1036 if( slice_type > 4 )
1037 slice_type = slice->type -= 5;
1038 uint64_t pic_parameter_set_id = nalu_get_exp_golomb_ue( bits );
1039 if( pic_parameter_set_id > 255 )
1040 return LSMASH_ERR_INVALID_DATA;
1041 slice->pic_parameter_set_id = pic_parameter_set_id;
1042 h264_pps_t *pps = h264_get_pps( info->pps_list, pic_parameter_set_id );
1043 if( !pps )
1044 return LSMASH_ERR_NAMELESS;
1045 h264_sps_t *sps = h264_get_sps( info->sps_list, pps->seq_parameter_set_id );
1046 if( !sps )
1047 return LSMASH_ERR_NAMELESS;
1048 slice->seq_parameter_set_id = pps->seq_parameter_set_id;
1049 slice->nal_ref_idc = nuh->nal_ref_idc;
1050 slice->IdrPicFlag = (nuh->nal_unit_type == H264_NALU_TYPE_SLICE_IDR);
1051 slice->pic_order_cnt_type = sps->pic_order_cnt_type;
1052 if( (slice->IdrPicFlag || sps->max_num_ref_frames == 0) && slice_type != 2 && slice_type != 4 )
1053 return LSMASH_ERR_INVALID_DATA;
1054 if( sps->separate_colour_plane_flag )
1055 lsmash_bits_get( bits, 2 ); /* colour_plane_id */
1056 uint64_t frame_num = lsmash_bits_get( bits, sps->log2_max_frame_num );
1057 if( frame_num >= (1ULL << sps->log2_max_frame_num) || (slice->IdrPicFlag && frame_num) )
1058 return LSMASH_ERR_INVALID_DATA;
1059 slice->frame_num = frame_num;
1060 if( !sps->frame_mbs_only_flag )
1062 slice->field_pic_flag = lsmash_bits_get( bits, 1 );
1063 if( slice->field_pic_flag )
1064 slice->bottom_field_flag = lsmash_bits_get( bits, 1 );
1066 if( slice->IdrPicFlag )
1068 uint64_t idr_pic_id = nalu_get_exp_golomb_ue( bits );
1069 if( idr_pic_id > 65535 )
1070 return LSMASH_ERR_INVALID_DATA;
1071 slice->idr_pic_id = idr_pic_id;
1073 if( sps->pic_order_cnt_type == 0 )
1075 uint64_t pic_order_cnt_lsb = lsmash_bits_get( bits, sps->log2_max_pic_order_cnt_lsb );
1076 if( pic_order_cnt_lsb >= sps->MaxPicOrderCntLsb )
1077 return LSMASH_ERR_INVALID_DATA;
1078 slice->pic_order_cnt_lsb = pic_order_cnt_lsb;
1079 if( pps->bottom_field_pic_order_in_frame_present_flag && !slice->field_pic_flag )
1080 slice->delta_pic_order_cnt_bottom = nalu_get_exp_golomb_se( bits );
1082 else if( sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag )
1084 slice->delta_pic_order_cnt[0] = nalu_get_exp_golomb_se( bits );
1085 if( pps->bottom_field_pic_order_in_frame_present_flag && !slice->field_pic_flag )
1086 slice->delta_pic_order_cnt[1] = nalu_get_exp_golomb_se( bits );
1088 if( pps->redundant_pic_cnt_present_flag )
1090 uint64_t redundant_pic_cnt = nalu_get_exp_golomb_ue( bits );
1091 if( redundant_pic_cnt > 127 )
1092 return LSMASH_ERR_INVALID_DATA;
1093 slice->has_redundancy = !!redundant_pic_cnt;
1095 if( slice_type == H264_SLICE_TYPE_B )
1096 lsmash_bits_get( bits, 1 );
1097 uint64_t num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
1098 uint64_t num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
1099 if( slice_type == H264_SLICE_TYPE_P || slice_type == H264_SLICE_TYPE_SP || slice_type == H264_SLICE_TYPE_B )
1101 if( lsmash_bits_get( bits, 1 ) ) /* num_ref_idx_active_override_flag */
1103 num_ref_idx_l0_active_minus1 = nalu_get_exp_golomb_ue( bits );
1104 if( num_ref_idx_l0_active_minus1 > 31 )
1105 return LSMASH_ERR_INVALID_DATA;
1106 if( slice_type == H264_SLICE_TYPE_B )
1108 num_ref_idx_l1_active_minus1 = nalu_get_exp_golomb_ue( bits );
1109 if( num_ref_idx_l1_active_minus1 > 31 )
1110 return LSMASH_ERR_INVALID_DATA;
1114 if( nuh->nal_unit_type == H264_NALU_TYPE_SLICE_EXT
1115 || nuh->nal_unit_type == H264_NALU_TYPE_SLICE_EXT_DVC )
1117 return LSMASH_ERR_PATCH_WELCOME; /* No support of MVC yet */
1118 #if 0
1119 /* ref_pic_list_mvc_modification() */
1120 if( slice_type == H264_SLICE_TYPE_P || slice_type == H264_SLICE_TYPE_B || slice_type == H264_SLICE_TYPE_SP )
1122 for( int i = 0; i < 1 + (slice_type == H264_SLICE_TYPE_B); i++ )
1124 if( lsmash_bits_get( bits, 1 ) ) /* (S)P and B: ref_pic_list_modification_flag_l0
1125 * B: ref_pic_list_modification_flag_l1 */
1127 uint64_t modification_of_pic_nums_idc;
1130 modification_of_pic_nums_idc = nalu_get_exp_golomb_ue( bits );
1131 #if 0
1132 if( modification_of_pic_nums_idc == 0 || modification_of_pic_nums_idc == 1 )
1133 nalu_get_exp_golomb_ue( bits ); /* abs_diff_pic_num_minus1 */
1134 else if( modification_of_pic_nums_idc == 2 )
1135 nalu_get_exp_golomb_ue( bits ); /* long_term_pic_num */
1136 else if( modification_of_pic_nums_idc == 4 || modification_of_pic_nums_idc == 5 )
1137 nalu_get_exp_golomb_ue( bits ); /* abs_diff_view_idx_minus1 */
1138 #else
1139 if( modification_of_pic_nums_idc != 3 )
1140 nalu_get_exp_golomb_ue( bits ); /* abs_diff_pic_num_minus1, long_term_pic_num or abs_diff_view_idx_minus1 */
1141 #endif
1142 } while( modification_of_pic_nums_idc != 3 );
1146 #endif
1148 else
1150 /* ref_pic_list_modification() */
1151 if( slice_type == H264_SLICE_TYPE_P || slice_type == H264_SLICE_TYPE_B || slice_type == H264_SLICE_TYPE_SP )
1153 for( int i = 0; i < 1 + (slice_type == H264_SLICE_TYPE_B); i++ )
1155 if( lsmash_bits_get( bits, 1 ) ) /* (S)P and B: ref_pic_list_modification_flag_l0
1156 * B: ref_pic_list_modification_flag_l1 */
1158 uint64_t modification_of_pic_nums_idc;
1161 modification_of_pic_nums_idc = nalu_get_exp_golomb_ue( bits );
1162 #if 0
1163 if( modification_of_pic_nums_idc == 0 || modification_of_pic_nums_idc == 1 )
1164 nalu_get_exp_golomb_ue( bits ); /* abs_diff_pic_num_minus1 */
1165 else if( modification_of_pic_nums_idc == 2 )
1166 nalu_get_exp_golomb_ue( bits ); /* long_term_pic_num */
1167 #else
1168 if( modification_of_pic_nums_idc != 3 )
1169 nalu_get_exp_golomb_ue( bits ); /* abs_diff_pic_num_minus1 or long_term_pic_num */
1170 #endif
1171 } while( modification_of_pic_nums_idc != 3 );
1176 if( (pps->weighted_pred_flag && (slice_type == H264_SLICE_TYPE_P || slice_type == H264_SLICE_TYPE_SP))
1177 || (pps->weighted_bipred_idc == 1 && slice_type == H264_SLICE_TYPE_B) )
1179 /* pred_weight_table() */
1180 nalu_get_exp_golomb_ue( bits ); /* luma_log2_weight_denom */
1181 if( sps->ChromaArrayType )
1182 nalu_get_exp_golomb_ue( bits ); /* chroma_log2_weight_denom */
1183 for( uint8_t i = 0; i <= num_ref_idx_l0_active_minus1; i++ )
1185 if( lsmash_bits_get( bits, 1 ) ) /* luma_weight_l0_flag */
1187 nalu_get_exp_golomb_se( bits ); /* luma_weight_l0[i] */
1188 nalu_get_exp_golomb_se( bits ); /* luma_offset_l0[i] */
1190 if( sps->ChromaArrayType
1191 && lsmash_bits_get( bits, 1 ) /* chroma_weight_l0_flag */ )
1192 for( int j = 0; j < 2; j++ )
1194 nalu_get_exp_golomb_se( bits ); /* chroma_weight_l0[i][j]*/
1195 nalu_get_exp_golomb_se( bits ); /* chroma_offset_l0[i][j] */
1198 if( slice_type == H264_SLICE_TYPE_B )
1199 for( uint8_t i = 0; i <= num_ref_idx_l1_active_minus1; i++ )
1201 if( lsmash_bits_get( bits, 1 ) ) /* luma_weight_l1_flag */
1203 nalu_get_exp_golomb_se( bits ); /* luma_weight_l1[i] */
1204 nalu_get_exp_golomb_se( bits ); /* luma_offset_l1[i] */
1206 if( sps->ChromaArrayType
1207 && lsmash_bits_get( bits, 1 ) /* chroma_weight_l1_flag */ )
1208 for( int j = 0; j < 2; j++ )
1210 nalu_get_exp_golomb_se( bits ); /* chroma_weight_l1[i][j]*/
1211 nalu_get_exp_golomb_se( bits ); /* chroma_offset_l1[i][j] */
1215 if( nuh->nal_ref_idc )
1217 /* dec_ref_pic_marking() */
1218 if( slice->IdrPicFlag )
1220 lsmash_bits_get( bits, 1 ); /* no_output_of_prior_pics_flag */
1221 lsmash_bits_get( bits, 1 ); /* long_term_reference_flag */
1223 else if( lsmash_bits_get( bits, 1 ) ) /* adaptive_ref_pic_marking_mode_flag */
1225 uint64_t memory_management_control_operation;
1228 memory_management_control_operation = nalu_get_exp_golomb_ue( bits );
1229 if( memory_management_control_operation )
1231 if( memory_management_control_operation == 5 )
1232 slice->has_mmco5 = 1;
1233 else
1235 nalu_get_exp_golomb_ue( bits );
1236 if( memory_management_control_operation == 3 )
1237 nalu_get_exp_golomb_ue( bits );
1240 } while( memory_management_control_operation );
1243 /* We needn't read more if not slice data partition A.
1244 * Skip slice_data() and rbsp_slice_trailing_bits(). */
1245 if( nuh->nal_unit_type == H264_NALU_TYPE_SLICE_DP_A )
1247 if( pps->entropy_coding_mode_flag && slice_type != H264_SLICE_TYPE_I && slice_type != H264_SLICE_TYPE_SI )
1248 nalu_get_exp_golomb_ue( bits ); /* cabac_init_idc */
1249 nalu_get_exp_golomb_se( bits ); /* slice_qp_delta */
1250 if( slice_type == H264_SLICE_TYPE_SP || slice_type == H264_SLICE_TYPE_SI )
1252 if( slice_type == H264_SLICE_TYPE_SP )
1253 lsmash_bits_get( bits, 1 ); /* sp_for_switch_flag */
1254 nalu_get_exp_golomb_se( bits ); /* slice_qs_delta */
1256 if( pps->deblocking_filter_control_present_flag
1257 && nalu_get_exp_golomb_ue( bits ) != 1 /* disable_deblocking_filter_idc */ )
1259 int64_t slice_alpha_c0_offset_div2 = nalu_get_exp_golomb_se( bits );
1260 if( slice_alpha_c0_offset_div2 < -6 || slice_alpha_c0_offset_div2 > 6 )
1261 return LSMASH_ERR_INVALID_DATA;
1262 int64_t slice_beta_offset_div2 = nalu_get_exp_golomb_se( bits );
1263 if( slice_beta_offset_div2 < -6 || slice_beta_offset_div2 > 6 )
1264 return LSMASH_ERR_INVALID_DATA;
1266 if( pps->num_slice_groups_minus1
1267 && (pps->slice_group_map_type == 3 || pps->slice_group_map_type == 4 || pps->slice_group_map_type == 5) )
1269 uint64_t temp = ((uint64_t)sps->PicSizeInMapUnits - 1) / pps->SliceGroupChangeRate + 1;
1270 uint64_t slice_group_change_cycle = lsmash_bits_get( bits, lsmash_ceil_log2( temp + 1 ) );
1271 if( slice_group_change_cycle > temp )
1272 return LSMASH_ERR_INVALID_DATA;
1274 /* end of slice_header() */
1275 slice->slice_id = nalu_get_exp_golomb_ue( bits );
1276 h264_slice_info_t *slice_part = h264_get_slice_info( info->slice_list, slice->slice_id );
1277 if( !slice_part )
1278 return LSMASH_ERR_NAMELESS;
1279 *slice_part = *slice;
1281 lsmash_bits_empty( bits );
1282 if( bits->bs->error )
1283 return LSMASH_ERR_NAMELESS;
1284 info->sps = *sps;
1285 info->pps = *pps;
1286 return 0;
1289 int h264_parse_slice
1291 h264_info_t *info,
1292 h264_nalu_header_t *nuh,
1293 uint8_t *rbsp_buffer,
1294 uint8_t *ebsp,
1295 uint64_t ebsp_size
1298 lsmash_bits_t *bits = info->bits;
1299 uint64_t size = nuh->nal_unit_type == H264_NALU_TYPE_SLICE_IDR || nuh->nal_ref_idc == 0
1300 ? LSMASH_MIN( ebsp_size, 100 )
1301 : LSMASH_MIN( ebsp_size, 1000 );
1302 int err = nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, size );
1303 if( err < 0 )
1304 return err;
1305 if( nuh->nal_unit_type != H264_NALU_TYPE_SLICE_DP_B
1306 && nuh->nal_unit_type != H264_NALU_TYPE_SLICE_DP_C )
1307 return h264_parse_slice_header( info, nuh );
1308 /* slice_data_partition_b_layer_rbsp() or slice_data_partition_c_layer_rbsp() */
1309 uint64_t slice_id = nalu_get_exp_golomb_ue( bits );
1310 h264_slice_info_t *slice = h264_get_slice_info( info->slice_list, slice_id );
1311 if( !slice )
1312 return LSMASH_ERR_NAMELESS;
1313 h264_pps_t *pps = h264_get_pps( info->pps_list, slice->pic_parameter_set_id );
1314 if( !pps )
1315 return LSMASH_ERR_NAMELESS;
1316 h264_sps_t *sps = h264_get_sps( info->sps_list, pps->seq_parameter_set_id );
1317 if( !sps )
1318 return LSMASH_ERR_NAMELESS;
1319 slice->seq_parameter_set_id = pps->seq_parameter_set_id;
1320 if( sps->separate_colour_plane_flag )
1321 lsmash_bits_get( bits, 2 ); /* colour_plane_id */
1322 if( pps->redundant_pic_cnt_present_flag )
1324 uint64_t redundant_pic_cnt = nalu_get_exp_golomb_ue( bits );
1325 if( redundant_pic_cnt > 127 )
1326 return LSMASH_ERR_INVALID_DATA;
1327 slice->has_redundancy = !!redundant_pic_cnt;
1329 /* Skip slice_data() and rbsp_slice_trailing_bits(). */
1330 lsmash_bits_empty( bits );
1331 if( bits->bs->error )
1332 return LSMASH_ERR_NAMELESS;
1333 info->sps = *sps;
1334 info->pps = *pps;
1335 return 0;
1338 static int h264_get_sps_id
1340 uint8_t *ps_ebsp,
1341 uint32_t ps_ebsp_length,
1342 uint8_t *ps_id
1345 /* max number of bits of sps_id = 11: 0b000001XXXXX
1346 * (24 + 11 - 1) / 8 + 1 = 5 bytes
1347 * Why +1? Because there might be an emulation_prevention_three_byte. */
1348 lsmash_bits_t bits = { 0 };
1349 lsmash_bs_t bs = { 0 };
1350 uint8_t rbsp_buffer[6];
1351 uint8_t buffer [6];
1352 bs.buffer.data = buffer;
1353 bs.buffer.alloc = 6;
1354 lsmash_bits_init( &bits, &bs );
1355 int err = nalu_import_rbsp_from_ebsp( &bits, rbsp_buffer, ps_ebsp, LSMASH_MIN( ps_ebsp_length, 6 ) );
1356 if( err < 0 )
1357 return err;
1358 lsmash_bits_get( &bits, 24 ); /* profile_idc, constraint_set_flags and level_idc */
1359 uint64_t sec_parameter_set_id = nalu_get_exp_golomb_ue( &bits );
1360 if( sec_parameter_set_id > 31 )
1361 return LSMASH_ERR_INVALID_DATA;
1362 *ps_id = sec_parameter_set_id;
1363 return bs.error ? LSMASH_ERR_NAMELESS : 0;
1366 static int h264_get_pps_id
1368 uint8_t *ps_ebsp,
1369 uint32_t ps_ebsp_length,
1370 uint8_t *ps_id
1373 /* max number of bits of pps_id = 17: 0b000000001XXXXXXXX
1374 * (17 - 1) / 8 + 1 = 3 bytes
1375 * Why +1? Because there might be an emulation_prevention_three_byte. */
1376 lsmash_bits_t bits = { 0 };
1377 lsmash_bs_t bs = { 0 };
1378 uint8_t rbsp_buffer[4];
1379 uint8_t buffer [4];
1380 bs.buffer.data = buffer;
1381 bs.buffer.alloc = 4;
1382 lsmash_bits_init( &bits, &bs );
1383 int err = nalu_import_rbsp_from_ebsp( &bits, rbsp_buffer, ps_ebsp, LSMASH_MIN( ps_ebsp_length, 4 ) );
1384 if( err < 0 )
1385 return err;
1386 uint64_t pic_parameter_set_id = nalu_get_exp_golomb_ue( &bits );
1387 if( pic_parameter_set_id > 255 )
1388 return LSMASH_ERR_INVALID_DATA;
1389 *ps_id = pic_parameter_set_id;
1390 return bs.error ? LSMASH_ERR_NAMELESS : 0;
1393 static inline int h264_get_ps_id
1395 uint8_t *ps_ebsp,
1396 uint32_t ps_ebsp_length,
1397 uint8_t *ps_id,
1398 lsmash_h264_parameter_set_type ps_type
1401 int (*get_ps_id)( uint8_t *ps_ebsp, uint32_t ps_ebsp_length, uint8_t *ps_id )
1402 = ps_type == H264_PARAMETER_SET_TYPE_SPS ? h264_get_sps_id
1403 : ps_type == H264_PARAMETER_SET_TYPE_PPS ? h264_get_pps_id
1404 : NULL;
1405 return get_ps_id ? get_ps_id( ps_ebsp, ps_ebsp_length, ps_id ) : LSMASH_ERR_INVALID_DATA;
1408 static inline lsmash_entry_list_t *h264_get_parameter_set_list
1410 lsmash_h264_specific_parameters_t *param,
1411 lsmash_h264_parameter_set_type ps_type
1414 if( !param->parameter_sets )
1415 return NULL;
1416 return ps_type == H264_PARAMETER_SET_TYPE_SPS ? param->parameter_sets->sps_list
1417 : ps_type == H264_PARAMETER_SET_TYPE_PPS ? param->parameter_sets->pps_list
1418 : ps_type == H264_PARAMETER_SET_TYPE_SPSEXT ? param->parameter_sets->spsext_list
1419 : NULL;
1422 static lsmash_entry_t *h264_get_ps_entry_from_param
1424 lsmash_h264_specific_parameters_t *param,
1425 lsmash_h264_parameter_set_type ps_type,
1426 uint8_t ps_id
1429 int (*get_ps_id)( uint8_t *ps_ebsp, uint32_t ps_ebsp_length, uint8_t *ps_id )
1430 = ps_type == H264_PARAMETER_SET_TYPE_SPS ? h264_get_sps_id
1431 : ps_type == H264_PARAMETER_SET_TYPE_PPS ? h264_get_pps_id
1432 : NULL;
1433 if( !get_ps_id )
1434 return NULL;
1435 lsmash_entry_list_t *ps_list = h264_get_parameter_set_list( param, ps_type );
1436 if( !ps_list )
1437 return NULL;
1438 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
1440 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1441 if( !ps )
1442 return NULL;
1443 uint8_t param_ps_id;
1444 if( get_ps_id( ps->nalUnit + 1, ps->nalUnitLength - 1, &param_ps_id ) < 0 )
1445 return NULL;
1446 if( ps_id == param_ps_id )
1447 return entry;
1449 return NULL;
1452 static inline void h264_update_picture_type
1454 h264_picture_info_t *picture,
1455 h264_slice_info_t *slice
1458 if( picture->type == H264_PICTURE_TYPE_I_P )
1460 if( slice->type == H264_SLICE_TYPE_B )
1461 picture->type = H264_PICTURE_TYPE_I_P_B;
1462 else if( slice->type == H264_SLICE_TYPE_SI || slice->type == H264_SLICE_TYPE_SP )
1463 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1465 else if( picture->type == H264_PICTURE_TYPE_I_P_B )
1467 if( slice->type != H264_SLICE_TYPE_P && slice->type != H264_SLICE_TYPE_B && slice->type != H264_SLICE_TYPE_I )
1468 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1470 else if( picture->type == H264_PICTURE_TYPE_I )
1472 if( slice->type == H264_SLICE_TYPE_P )
1473 picture->type = H264_PICTURE_TYPE_I_P;
1474 else if( slice->type == H264_SLICE_TYPE_B )
1475 picture->type = H264_PICTURE_TYPE_I_P_B;
1476 else if( slice->type == H264_SLICE_TYPE_SI )
1477 picture->type = H264_PICTURE_TYPE_I_SI;
1478 else if( slice->type == H264_SLICE_TYPE_SP )
1479 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1481 else if( picture->type == H264_PICTURE_TYPE_SI_SP )
1483 if( slice->type == H264_SLICE_TYPE_P || slice->type == H264_SLICE_TYPE_I )
1484 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1485 else if( slice->type == H264_SLICE_TYPE_B )
1486 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1488 else if( picture->type == H264_PICTURE_TYPE_SI )
1490 if( slice->type == H264_SLICE_TYPE_P )
1491 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1492 else if( slice->type == H264_SLICE_TYPE_B )
1493 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1494 else if( slice->type != H264_SLICE_TYPE_I )
1495 picture->type = H264_PICTURE_TYPE_I_SI;
1496 else if( slice->type == H264_SLICE_TYPE_SP )
1497 picture->type = H264_PICTURE_TYPE_SI_SP;
1499 else if( picture->type == H264_PICTURE_TYPE_I_SI )
1501 if( slice->type == H264_SLICE_TYPE_P || slice->type == H264_SLICE_TYPE_SP )
1502 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1503 else if( slice->type == H264_SLICE_TYPE_B )
1504 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1506 else if( picture->type == H264_PICTURE_TYPE_I_SI_P_SP )
1508 if( slice->type == H264_SLICE_TYPE_B )
1509 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1511 else if( picture->type == H264_PICTURE_TYPE_NONE )
1513 if( slice->type == H264_SLICE_TYPE_P )
1514 picture->type = H264_PICTURE_TYPE_I_P;
1515 else if( slice->type == H264_SLICE_TYPE_B )
1516 picture->type = H264_PICTURE_TYPE_I_P_B;
1517 else if( slice->type == H264_SLICE_TYPE_I )
1518 picture->type = H264_PICTURE_TYPE_I;
1519 else if( slice->type == H264_SLICE_TYPE_SI )
1520 picture->type = H264_PICTURE_TYPE_SI;
1521 else if( slice->type == H264_SLICE_TYPE_SP )
1522 picture->type = H264_PICTURE_TYPE_SI_SP;
1524 #if 0
1525 fprintf( stderr, "Picture type = %s\n", picture->type == H264_PICTURE_TYPE_I_P ? "P"
1526 : picture->type == H264_PICTURE_TYPE_I_P_B ? "B"
1527 : picture->type == H264_PICTURE_TYPE_I ? "I"
1528 : picture->type == H264_PICTURE_TYPE_SI ? "SI"
1529 : picture->type == H264_PICTURE_TYPE_I_SI ? "SI"
1530 : "SP" );
1531 #endif
1534 /* Shall be called at least once per picture. */
1535 void h264_update_picture_info_for_slice
1537 h264_info_t *info,
1538 h264_picture_info_t *picture,
1539 h264_slice_info_t *slice
1542 assert( info );
1543 picture->has_mmco5 |= slice->has_mmco5;
1544 picture->has_redundancy |= slice->has_redundancy;
1545 picture->has_primary |= !slice->has_redundancy;
1546 h264_update_picture_type( picture, slice );
1547 /* Mark 'used' on active parameter sets. */
1548 uint8_t ps_id[2] = { slice->seq_parameter_set_id, slice->pic_parameter_set_id };
1549 for( int i = 0; i < 2; i++ )
1551 lsmash_h264_parameter_set_type ps_type = (lsmash_h264_parameter_set_type)i;
1552 lsmash_entry_t *entry = h264_get_ps_entry_from_param( &info->avcC_param, ps_type, ps_id[i] );
1553 if( entry && entry->data )
1555 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1556 if( ps->unused )
1557 lsmash_append_h264_parameter_set( &info->avcC_param, ps_type, ps->nalUnit, ps->nalUnitLength );
1560 /* Discard this slice info. */
1561 slice->present = 0;
1564 /* Shall be called exactly once per picture. */
1565 void h264_update_picture_info
1567 h264_info_t *info,
1568 h264_picture_info_t *picture,
1569 h264_slice_info_t *slice,
1570 h264_sei_t *sei
1573 picture->frame_num = slice->frame_num;
1574 picture->pic_order_cnt_lsb = slice->pic_order_cnt_lsb;
1575 picture->delta_pic_order_cnt_bottom = slice->delta_pic_order_cnt_bottom;
1576 picture->delta_pic_order_cnt[0] = slice->delta_pic_order_cnt[0];
1577 picture->delta_pic_order_cnt[1] = slice->delta_pic_order_cnt[1];
1578 picture->field_pic_flag = slice->field_pic_flag;
1579 picture->bottom_field_flag = slice->bottom_field_flag;
1580 picture->idr = slice->IdrPicFlag;
1581 picture->pic_parameter_set_id = slice->pic_parameter_set_id;
1582 picture->disposable = (slice->nal_ref_idc == 0);
1583 picture->random_accessible = slice->IdrPicFlag;
1584 h264_update_picture_info_for_slice( info, picture, slice );
1585 picture->independent = picture->type == H264_PICTURE_TYPE_I || picture->type == H264_PICTURE_TYPE_I_SI;
1586 if( sei->pic_timing.present )
1588 if( sei->pic_timing.pic_struct < 9 )
1590 static const uint8_t DeltaTfiDivisor[9] = { 2, 1, 1, 2, 2, 3, 3, 4, 6 };
1591 picture->delta = DeltaTfiDivisor[ sei->pic_timing.pic_struct ];
1593 else
1594 /* Reserved values in the spec we refer to. */
1595 picture->delta = picture->field_pic_flag ? 1 : 2;
1596 sei->pic_timing.present = 0;
1598 else
1599 picture->delta = picture->field_pic_flag ? 1 : 2;
1600 if( sei->recovery_point.present )
1602 picture->random_accessible |= sei->recovery_point.random_accessible;
1603 picture->broken_link_flag |= sei->recovery_point.broken_link_flag;
1604 picture->recovery_frame_cnt = sei->recovery_point.recovery_frame_cnt;
1605 sei->recovery_point.present = 0;
1609 int h264_find_au_delimit_by_slice_info
1611 h264_slice_info_t *slice,
1612 h264_slice_info_t *prev_slice
1615 if( slice->frame_num != prev_slice->frame_num
1616 || ((slice->pic_order_cnt_type == 0 && prev_slice->pic_order_cnt_type == 0)
1617 && (slice->pic_order_cnt_lsb != prev_slice->pic_order_cnt_lsb
1618 || slice->delta_pic_order_cnt_bottom != prev_slice->delta_pic_order_cnt_bottom))
1619 || ((slice->pic_order_cnt_type == 1 && prev_slice->pic_order_cnt_type == 1)
1620 && (slice->delta_pic_order_cnt[0] != prev_slice->delta_pic_order_cnt[0]
1621 || slice->delta_pic_order_cnt[1] != prev_slice->delta_pic_order_cnt[1]))
1622 || slice->field_pic_flag != prev_slice->field_pic_flag
1623 || slice->bottom_field_flag != prev_slice->bottom_field_flag
1624 || slice->IdrPicFlag != prev_slice->IdrPicFlag
1625 || slice->pic_parameter_set_id != prev_slice->pic_parameter_set_id
1626 || ((slice->nal_ref_idc == 0 || prev_slice->nal_ref_idc == 0)
1627 && (slice->nal_ref_idc != prev_slice->nal_ref_idc))
1628 || (slice->IdrPicFlag == 1 && prev_slice->IdrPicFlag == 1
1629 && slice->idr_pic_id != prev_slice->idr_pic_id) )
1630 return 1;
1631 return 0;
1634 int h264_find_au_delimit_by_nalu_type
1636 uint8_t nalu_type,
1637 uint8_t prev_nalu_type
1640 return ((nalu_type >= H264_NALU_TYPE_SEI && nalu_type <= H264_NALU_TYPE_AUD)
1641 || (nalu_type >= H264_NALU_TYPE_PREFIX && nalu_type <= H264_NALU_TYPE_RSV_NVCL18))
1642 && ((prev_nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && prev_nalu_type <= H264_NALU_TYPE_SLICE_IDR)
1643 || prev_nalu_type == H264_NALU_TYPE_FD || prev_nalu_type == H264_NALU_TYPE_SLICE_AUX);
1646 int h264_supplement_buffer
1648 h264_stream_buffer_t *sb,
1649 h264_access_unit_t *au,
1650 uint32_t size
1653 lsmash_multiple_buffers_t *bank = lsmash_resize_multiple_buffers( sb->bank, size );
1654 if( !bank )
1655 return LSMASH_ERR_MEMORY_ALLOC;
1656 sb->bank = bank;
1657 sb->rbsp = lsmash_withdraw_buffer( bank, 1 );
1658 if( au && bank->number_of_buffers == 3 )
1660 au->data = lsmash_withdraw_buffer( bank, 2 );
1661 au->incomplete_data = lsmash_withdraw_buffer( bank, 3 );
1663 return 0;
1666 static void h264_bs_put_parameter_sets
1668 lsmash_bs_t *bs,
1669 lsmash_entry_list_t *ps_list,
1670 uint32_t max_ps_count
1673 uint32_t ps_count = 0;
1674 for( lsmash_entry_t *entry = ps_list->head; entry && ps_count < max_ps_count; entry = entry->next )
1676 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1677 if( ps && !ps->unused )
1679 lsmash_bs_put_be16( bs, ps->nalUnitLength );
1680 lsmash_bs_put_bytes( bs, ps->nalUnitLength, ps->nalUnit );
1682 else
1683 continue;
1684 ++ps_count;
1688 uint8_t *lsmash_create_h264_specific_info
1690 lsmash_h264_specific_parameters_t *param,
1691 uint32_t *data_length
1694 if( !param || !param->parameter_sets || !data_length )
1695 return NULL;
1696 if( param->lengthSizeMinusOne != 0 && param->lengthSizeMinusOne != 1 && param->lengthSizeMinusOne != 3 )
1697 return NULL;
1698 static const uint32_t max_ps_count[3] = { 31, 255, 255 };
1699 lsmash_entry_list_t *ps_list[3] =
1701 param->parameter_sets->sps_list, /* SPS */
1702 param->parameter_sets->pps_list, /* PPS */
1703 param->parameter_sets->spsext_list /* SPSExt */
1705 uint32_t ps_count[3] = { 0, 0, 0 };
1706 /* SPS and PPS are mandatory. */
1707 if( !ps_list[0] || !ps_list[0]->head || ps_list[0]->entry_count == 0
1708 || !ps_list[1] || !ps_list[1]->head || ps_list[1]->entry_count == 0 )
1709 return NULL;
1710 for( int i = 0; i < 3; i++ )
1711 if( ps_list[i] )
1712 for( lsmash_entry_t *entry = ps_list[i]->head; entry && ps_count[i] < max_ps_count[i]; entry = entry->next )
1714 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1715 if( !ps )
1716 return NULL;
1717 if( ps->unused )
1718 continue;
1719 ++ps_count[i];
1721 /* Create an AVCConfigurationBox */
1722 lsmash_bs_t *bs = lsmash_bs_create();
1723 if( !bs )
1724 return NULL;
1725 lsmash_bs_put_be32( bs, 0 ); /* box size */
1726 lsmash_bs_put_be32( bs, ISOM_BOX_TYPE_AVCC.fourcc ); /* box type: 'avcC' */
1727 lsmash_bs_put_byte( bs, 1 ); /* configurationVersion */
1728 lsmash_bs_put_byte( bs, param->AVCProfileIndication ); /* AVCProfileIndication */
1729 lsmash_bs_put_byte( bs, param->profile_compatibility ); /* profile_compatibility */
1730 lsmash_bs_put_byte( bs, param->AVCLevelIndication ); /* AVCLevelIndication */
1731 lsmash_bs_put_byte( bs, param->lengthSizeMinusOne | 0xfc ); /* lengthSizeMinusOne */
1732 lsmash_bs_put_byte( bs, ps_count[0] | 0xe0 ); /* numOfSequenceParameterSets */
1733 h264_bs_put_parameter_sets( bs, ps_list[0], ps_count[0] ); /* sequenceParameterSetLength
1734 * sequenceParameterSetNALUnit */
1735 lsmash_bs_put_byte( bs, ps_count[1] ); /* numOfPictureParameterSets */
1736 h264_bs_put_parameter_sets( bs, ps_list[1], ps_count[1] ); /* pictureParameterSetLength
1737 * pictureParameterSetNALUnit */
1738 if( H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication ) )
1740 lsmash_bs_put_byte( bs, param->chroma_format | 0xfc ); /* chroma_format */
1741 lsmash_bs_put_byte( bs, param->bit_depth_luma_minus8 | 0xf8 ); /* bit_depth_luma_minus8 */
1742 lsmash_bs_put_byte( bs, param->bit_depth_chroma_minus8 | 0xf8 ); /* bit_depth_chroma_minus8 */
1743 if( ps_list[2] )
1745 lsmash_bs_put_byte( bs, ps_count[2] ); /* numOfSequenceParameterSetExt */
1746 h264_bs_put_parameter_sets( bs, ps_list[2], ps_count[2] ); /* sequenceParameterSetExtLength
1747 * sequenceParameterSetExtNALUnit */
1749 else /* no sequence parameter set extensions */
1750 lsmash_bs_put_byte( bs, 0 ); /* numOfSequenceParameterSetExt */
1752 uint8_t *data = lsmash_bs_export_data( bs, data_length );
1753 lsmash_bs_cleanup( bs );
1754 /* Update box size. */
1755 LSMASH_SET_BE32( data, *data_length );
1756 return data;
1759 static inline int h264_validate_ps_type
1761 lsmash_h264_parameter_set_type ps_type,
1762 void *ps_data,
1763 uint32_t ps_length
1766 if( !ps_data || ps_length < 2 )
1767 return LSMASH_ERR_INVALID_DATA;
1768 if( ps_type != H264_PARAMETER_SET_TYPE_SPS
1769 && ps_type != H264_PARAMETER_SET_TYPE_PPS
1770 && ps_type != H264_PARAMETER_SET_TYPE_SPSEXT )
1771 return LSMASH_ERR_INVALID_DATA;
1772 uint8_t nalu_type = *((uint8_t *)ps_data) & 0x1f;
1773 if( nalu_type != H264_NALU_TYPE_SPS
1774 && nalu_type != H264_NALU_TYPE_PPS
1775 && nalu_type != H264_NALU_TYPE_SPS_EXT )
1776 return LSMASH_ERR_INVALID_DATA;
1777 if( (ps_type == H264_PARAMETER_SET_TYPE_SPS && nalu_type != H264_NALU_TYPE_SPS)
1778 || (ps_type == H264_PARAMETER_SET_TYPE_PPS && nalu_type != H264_NALU_TYPE_PPS)
1779 || (ps_type == H264_PARAMETER_SET_TYPE_SPSEXT && nalu_type != H264_NALU_TYPE_SPS_EXT) )
1780 return LSMASH_ERR_INVALID_DATA;
1781 return 0;
1784 static lsmash_dcr_nalu_appendable h264_check_sps_appendable
1786 lsmash_bits_t *bits,
1787 uint8_t *rbsp_buffer,
1788 lsmash_h264_specific_parameters_t *param,
1789 uint8_t *ps_data,
1790 uint32_t ps_length,
1791 lsmash_entry_list_t *ps_list
1794 h264_sps_t sps;
1795 if( h264_parse_sps_minimally( bits, &sps, rbsp_buffer, ps_data + 1, ps_length - 1 ) < 0 )
1796 return DCR_NALU_APPEND_ERROR;
1797 lsmash_bits_empty( bits );
1798 /* FIXME; If the sequence parameter sets are marked with different profiles,
1799 * and the relevant profile compatibility flags are all zero,
1800 * then the stream may need examination to determine which profile, if any, the stream conforms to.
1801 * If the stream is not examined, or the examination reveals that there is no profile to which the stream conforms,
1802 * then the stream must be split into two or more sub-streams with separate configuration records in which these rules can be met. */
1803 #if 0
1804 if( sps.profile_idc != param->AVCProfileIndication && (sps->constraint_set_flags & param->profile_compatibility) )
1805 #else
1806 if( sps.profile_idc != param->AVCProfileIndication )
1807 #endif
1808 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
1809 /* The values of chroma_format_idc, bit_depth_luma_minus8 and bit_depth_chroma_minus8
1810 * must be identical in all SPSs in a single AVC configuration record. */
1811 if( H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication )
1812 && (sps.chroma_format_idc != param->chroma_format
1813 || sps.bit_depth_luma_minus8 != param->bit_depth_luma_minus8
1814 || sps.bit_depth_chroma_minus8 != param->bit_depth_chroma_minus8) )
1815 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
1816 /* Forbidden to duplicate SPS that has the same seq_parameter_set_id with different form within the same configuration record. */
1817 uint8_t sps_id = sps.seq_parameter_set_id;
1818 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
1820 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1821 if( !ps )
1822 return DCR_NALU_APPEND_ERROR;
1823 if( ps->unused )
1824 continue;
1825 uint8_t param_sps_id;
1826 if( h264_get_sps_id( ps->nalUnit + 1, ps->nalUnitLength - 1, &param_sps_id ) < 0 )
1827 return DCR_NALU_APPEND_ERROR;
1828 if( sps_id == param_sps_id )
1829 /* SPS that has the same seq_parameter_set_id already exists with different form. */
1830 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
1831 if( entry == ps_list->head )
1833 /* Check if the visual presentation sizes are different. */
1834 h264_sps_t first_sps;
1835 if( h264_parse_sps_minimally( bits, &first_sps, rbsp_buffer,
1836 ps->nalUnit + 1,
1837 ps->nalUnitLength - 1 ) < 0 )
1838 return DCR_NALU_APPEND_ERROR;
1839 if( sps.cropped_width != first_sps.cropped_width
1840 || sps.cropped_height != first_sps.cropped_height )
1841 return DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED;
1844 return DCR_NALU_APPEND_POSSIBLE;
1847 static lsmash_dcr_nalu_appendable h264_check_pps_appendable
1849 uint8_t *ps_data,
1850 uint32_t ps_length,
1851 lsmash_entry_list_t *ps_list
1854 uint8_t pps_id;
1855 if( h264_get_pps_id( ps_data + 1, ps_length - 1, &pps_id ) < 0 )
1856 return DCR_NALU_APPEND_ERROR;
1857 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
1859 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1860 if( !ps )
1861 return DCR_NALU_APPEND_ERROR;
1862 if( ps->unused )
1863 continue;
1864 uint8_t param_pps_id;
1865 if( h264_get_pps_id( ps->nalUnit + 1, ps->nalUnitLength - 1, &param_pps_id ) < 0 )
1866 return DCR_NALU_APPEND_ERROR;
1867 if( pps_id == param_pps_id )
1868 /* PPS that has the same pic_parameter_set_id already exists with different form. */
1869 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
1871 return DCR_NALU_APPEND_POSSIBLE;
1874 lsmash_dcr_nalu_appendable lsmash_check_h264_parameter_set_appendable
1876 lsmash_h264_specific_parameters_t *param,
1877 lsmash_h264_parameter_set_type ps_type,
1878 void *_ps_data,
1879 uint32_t ps_length
1882 uint8_t *ps_data = _ps_data;
1883 if( !param )
1884 return DCR_NALU_APPEND_ERROR;
1885 if( h264_validate_ps_type( ps_type, ps_data, ps_length ) < 0 )
1886 return DCR_NALU_APPEND_ERROR;
1887 if( ps_type == H264_PARAMETER_SET_TYPE_SPSEXT
1888 && !H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication ) )
1889 return DCR_NALU_APPEND_ERROR;
1890 /* Check whether the same parameter set already exsits or not. */
1891 lsmash_entry_list_t *ps_list = h264_get_parameter_set_list( param, ps_type );
1892 if( !ps_list || !ps_list->head )
1893 return DCR_NALU_APPEND_POSSIBLE; /* No parameter set */
1894 switch( nalu_check_same_ps_existence( ps_list, ps_data, ps_length ) )
1896 case 0 : break;
1897 case 1 : return DCR_NALU_APPEND_DUPLICATED; /* The same parameter set already exists. */
1898 default : return DCR_NALU_APPEND_ERROR; /* An error occured. */
1900 uint32_t ps_count;
1901 if( nalu_get_ps_count( ps_list, &ps_count ) )
1902 return DCR_NALU_APPEND_ERROR;
1903 if( (ps_type == H264_PARAMETER_SET_TYPE_SPS && ps_count >= 31)
1904 || (ps_type == H264_PARAMETER_SET_TYPE_PPS && ps_count >= 255)
1905 || (ps_type == H264_PARAMETER_SET_TYPE_SPSEXT && ps_count >= 255) )
1906 return DCR_NALU_APPEND_NEW_DCR_REQUIRED; /* No more appendable parameter sets. */
1907 if( ps_type == H264_PARAMETER_SET_TYPE_SPSEXT )
1908 return DCR_NALU_APPEND_POSSIBLE;
1909 /* Check whether a new specific info is needed or not. */
1910 if( ps_type == H264_PARAMETER_SET_TYPE_PPS )
1911 /* PPS */
1912 return h264_check_pps_appendable( ps_data, ps_length, ps_list );
1913 else
1915 /* SPS
1916 * Set up bitstream handler for parse parameter sets. */
1917 lsmash_bits_t *bits = lsmash_bits_adhoc_create();
1918 if( !bits )
1919 return DCR_NALU_APPEND_ERROR;
1920 uint32_t max_ps_length;
1921 uint8_t *rbsp_buffer;
1922 if( nalu_get_max_ps_length( ps_list, &max_ps_length ) < 0
1923 || (rbsp_buffer = lsmash_malloc( LSMASH_MAX( max_ps_length, ps_length ) )) == NULL )
1925 lsmash_bits_adhoc_cleanup( bits );
1926 return DCR_NALU_APPEND_ERROR;
1928 lsmash_dcr_nalu_appendable appendable = h264_check_sps_appendable( bits, rbsp_buffer, param, ps_data, ps_length, ps_list );
1929 lsmash_bits_adhoc_cleanup( bits );
1930 lsmash_free( rbsp_buffer );
1931 return appendable;
1935 static inline void h264_reorder_parameter_set_ascending_id
1937 lsmash_h264_specific_parameters_t *param,
1938 lsmash_h264_parameter_set_type ps_type,
1939 lsmash_entry_list_t *ps_list,
1940 uint8_t ps_id
1943 lsmash_entry_t *entry = NULL;
1944 if( ps_id )
1945 for( int i = ps_id - 1; i; i-- )
1947 entry = h264_get_ps_entry_from_param( param, ps_type, i );
1948 if( entry )
1949 break;
1951 int append_head = 0;
1952 if( !entry )
1954 /* Couldn't find any parameter set with lower identifier.
1955 * Next, find parameter set with upper identifier. */
1956 int max_ps_id = ps_type == H264_PARAMETER_SET_TYPE_SPS ? 31 : 255;
1957 for( int i = ps_id + 1; i <= max_ps_id; i++ )
1959 entry = h264_get_ps_entry_from_param( param, ps_type, i );
1960 if( entry )
1961 break;
1963 if( entry )
1964 append_head = 1;
1966 if( !entry )
1967 return; /* The new entry was appended to the tail. */
1968 lsmash_entry_t *new_entry = ps_list->tail;
1969 if( append_head )
1971 /* before: entry[i > ps_id] ... -> prev_entry -> new_entry[ps_id]
1972 * after: new_entry[ps_id] -> entry[i > ps_id] -> ... -> prev_entry */
1973 if( new_entry->prev )
1974 new_entry->prev->next = NULL;
1975 new_entry->prev = NULL;
1976 entry->prev = new_entry;
1977 new_entry->next = entry;
1978 return;
1980 /* before: entry[i < ps_id] -> next_entry -> ... -> prev_entry -> new_entry[ps_id]
1981 * after: entry[i < ps_id] -> new_entry[ps_id] -> next_entry -> ... -> prev_entry */
1982 if( new_entry->prev )
1983 new_entry->prev->next = NULL;
1984 new_entry->prev = entry;
1985 new_entry->next = entry->next;
1986 if( entry->next )
1987 entry->next->prev = new_entry;
1988 entry->next = new_entry;
1991 int lsmash_append_h264_parameter_set
1993 lsmash_h264_specific_parameters_t *param,
1994 lsmash_h264_parameter_set_type ps_type,
1995 void *_ps_data,
1996 uint32_t ps_length
1999 uint8_t *ps_data = _ps_data;
2000 if( !param || !ps_data || ps_length < 2 )
2001 return LSMASH_ERR_FUNCTION_PARAM;
2002 if( ps_type != H264_PARAMETER_SET_TYPE_SPS
2003 && ps_type != H264_PARAMETER_SET_TYPE_PPS
2004 && ps_type != H264_PARAMETER_SET_TYPE_SPSEXT )
2005 return LSMASH_ERR_FUNCTION_PARAM;
2006 if( !param->parameter_sets )
2008 param->parameter_sets = h264_allocate_parameter_sets();
2009 if( !param->parameter_sets )
2010 return LSMASH_ERR_MEMORY_ALLOC;
2012 lsmash_entry_list_t *ps_list = h264_get_parameter_set_list( param, ps_type );
2013 if( !ps_list )
2014 return LSMASH_ERR_NAMELESS;
2015 if( ps_type == H264_PARAMETER_SET_TYPE_SPSEXT )
2017 if( !H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication ) )
2018 return 0;
2019 isom_dcr_ps_entry_t *ps = isom_create_ps_entry( ps_data, ps_length );
2020 if( !ps )
2021 return LSMASH_ERR_MEMORY_ALLOC;
2022 if( lsmash_list_add_entry( ps_list, ps ) < 0 )
2024 isom_remove_dcr_ps( ps );
2025 return LSMASH_ERR_MEMORY_ALLOC;
2027 return 0;
2029 /* Check if the same parameter set identifier already exists. */
2030 uint8_t ps_id;
2031 int err = h264_get_ps_id( ps_data + 1, ps_length - 1, &ps_id, ps_type );
2032 if( err < 0 )
2033 return err;
2034 lsmash_entry_t *entry = h264_get_ps_entry_from_param( param, ps_type, ps_id );
2035 isom_dcr_ps_entry_t *ps = entry ? (isom_dcr_ps_entry_t *)entry->data : NULL;
2036 if( ps && !ps->unused )
2037 /* The same parameter set identifier already exists. */
2038 return LSMASH_ERR_FUNCTION_PARAM;
2039 int invoke_reorder;
2040 if( ps )
2042 /* Reuse an already existed parameter set in the list. */
2043 ps->unused = 0;
2044 if( ps->nalUnit != ps_data )
2046 /* The same address could be given when called by h264_update_picture_info_for_slice(). */
2047 lsmash_free( ps->nalUnit );
2048 ps->nalUnit = ps_data;
2050 ps->nalUnitLength = ps_length;
2051 invoke_reorder = 0;
2053 else
2055 /* Create a new parameter set and append it into the list. */
2056 ps = isom_create_ps_entry( ps_data, ps_length );
2057 if( !ps )
2058 return LSMASH_ERR_MEMORY_ALLOC;
2059 if( lsmash_list_add_entry( ps_list, ps ) < 0 )
2061 isom_remove_dcr_ps( ps );
2062 return LSMASH_ERR_MEMORY_ALLOC;
2064 invoke_reorder = 1;
2066 if( ps_type == H264_PARAMETER_SET_TYPE_SPS )
2068 /* Update specific info with SPS. */
2069 lsmash_bits_t *bits = lsmash_bits_adhoc_create();
2070 if( !bits )
2071 return LSMASH_ERR_MEMORY_ALLOC;
2072 uint8_t *rbsp_buffer = lsmash_malloc( ps_length );
2073 if( !rbsp_buffer )
2075 lsmash_bits_adhoc_cleanup( bits );
2076 return LSMASH_ERR_MEMORY_ALLOC;
2078 h264_sps_t sps;
2079 err = h264_parse_sps_minimally( bits, &sps, rbsp_buffer, ps_data + 1, ps_length - 1 );
2080 lsmash_bits_adhoc_cleanup( bits );
2081 lsmash_free( rbsp_buffer );
2082 if( err < 0 )
2084 lsmash_list_remove_entry_tail( ps_list );
2085 return err;
2087 if( ps_list->entry_count == 1 )
2088 param->profile_compatibility = 0xff;
2089 param->AVCProfileIndication = sps.profile_idc;
2090 param->profile_compatibility &= sps.constraint_set_flags;
2091 param->AVCLevelIndication = LSMASH_MAX( param->AVCLevelIndication, sps.level_idc );
2092 param->chroma_format = sps.chroma_format_idc;
2093 param->bit_depth_luma_minus8 = sps.bit_depth_luma_minus8;
2094 param->bit_depth_chroma_minus8 = sps.bit_depth_chroma_minus8;
2096 if( invoke_reorder )
2097 /* Add a new parameter set in order of ascending parameter set identifier. */
2098 h264_reorder_parameter_set_ascending_id( param, ps_type, ps_list, ps_id );
2099 return 0;
2102 int h264_try_to_append_parameter_set
2104 h264_info_t *info,
2105 lsmash_h264_parameter_set_type ps_type,
2106 void *_ps_data,
2107 uint32_t ps_length
2110 uint8_t *ps_data = _ps_data;
2111 lsmash_dcr_nalu_appendable ret = lsmash_check_h264_parameter_set_appendable( &info->avcC_param, ps_type, ps_data, ps_length );
2112 lsmash_h264_specific_parameters_t *param;
2113 switch( ret )
2115 case DCR_NALU_APPEND_ERROR : /* Error */
2116 return LSMASH_ERR_NAMELESS;
2117 case DCR_NALU_APPEND_NEW_DCR_REQUIRED : /* Mulitiple sample description is needed. */
2118 case DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED : /* Mulitiple sample description is needed. */
2119 param = &info->avcC_param_next;
2120 info->avcC_pending = 1;
2121 break;
2122 case DCR_NALU_APPEND_POSSIBLE : /* Appendable */
2123 param = info->avcC_pending ? &info->avcC_param_next : &info->avcC_param;
2124 break;
2125 default : /* No need to append */
2126 return 0;
2128 int err;
2129 switch( ps_type )
2131 case H264_PARAMETER_SET_TYPE_SPS :
2132 if( (err = h264_parse_sps( info, info->buffer.rbsp, ps_data + 1, ps_length - 1 )) < 0 )
2133 return err;
2134 break;
2135 case H264_PARAMETER_SET_TYPE_PPS :
2136 if( (err = h264_parse_pps( info, info->buffer.rbsp, ps_data + 1, ps_length - 1 )) < 0 )
2137 return err;
2138 break;
2139 default :
2140 break;
2142 return lsmash_append_h264_parameter_set( param, ps_type, ps_data, ps_length );
2145 static inline int h264_move_dcr_nalu_entry
2147 lsmash_h264_specific_parameters_t *dst_data,
2148 lsmash_h264_specific_parameters_t *src_data,
2149 lsmash_h264_parameter_set_type ps_type
2152 lsmash_entry_list_t *src_ps_list = h264_get_parameter_set_list( src_data, ps_type );
2153 lsmash_entry_list_t *dst_ps_list = h264_get_parameter_set_list( dst_data, ps_type );
2154 assert( src_ps_list && dst_ps_list );
2155 for( lsmash_entry_t *src_entry = src_ps_list->head; src_entry; src_entry = src_entry->next )
2157 isom_dcr_ps_entry_t *src_ps = (isom_dcr_ps_entry_t *)src_entry->data;
2158 if( !src_ps )
2159 continue;
2160 int err;
2161 uint8_t src_ps_id;
2162 if( (err = h264_get_ps_id( src_ps->nalUnit + 1, src_ps->nalUnitLength - 1, &src_ps_id, ps_type )) < 0 )
2163 return err;
2164 lsmash_entry_t *dst_entry;
2165 for( dst_entry = dst_ps_list->head; dst_entry; dst_entry = dst_entry->next )
2167 isom_dcr_ps_entry_t *dst_ps = (isom_dcr_ps_entry_t *)dst_entry->data;
2168 if( !dst_ps )
2169 continue;
2170 uint8_t dst_ps_id;
2171 if( (err = h264_get_ps_id( dst_ps->nalUnit + 1, dst_ps->nalUnitLength - 1, &dst_ps_id, ps_type )) < 0 )
2172 return err;
2173 if( dst_ps_id == src_ps_id )
2175 /* Replace the old parameter set with the new one. */
2176 assert( dst_entry->data != src_entry->data );
2177 isom_remove_dcr_ps( dst_ps );
2178 dst_entry->data = src_entry->data;
2179 src_entry->data = NULL;
2180 break;
2183 if( !dst_entry )
2185 /* Move the parameter set. */
2186 if( lsmash_list_add_entry( dst_ps_list, src_ps ) < 0 )
2187 return LSMASH_ERR_MEMORY_ALLOC;
2188 src_entry->data = NULL;
2191 return 0;
2194 int h264_move_pending_avcC_param
2196 h264_info_t *info
2199 assert( info );
2200 if( !info->avcC_pending )
2201 return 0;
2202 /* Mark 'unused' on parameter sets within the decoder configuration record. */
2203 for( int i = 0; i < H264_PARAMETER_SET_TYPE_NUM; i++ )
2205 lsmash_entry_list_t *ps_list = h264_get_parameter_set_list( &info->avcC_param, i );
2206 assert( ps_list );
2207 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
2209 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
2210 if( !ps )
2211 continue;
2212 ps->unused = 1;
2215 /* Move the new parameter sets. */
2216 int err;
2217 if( (err = h264_move_dcr_nalu_entry( &info->avcC_param, &info->avcC_param_next, H264_PARAMETER_SET_TYPE_SPS )) < 0
2218 || (err = h264_move_dcr_nalu_entry( &info->avcC_param, &info->avcC_param_next, H264_PARAMETER_SET_TYPE_PPS )) < 0 )
2219 return err;
2220 /* Move to the pending. */
2221 lsmash_h264_parameter_sets_t *parameter_sets = info->avcC_param.parameter_sets; /* Back up parameter sets. */
2222 info->avcC_param = info->avcC_param_next;
2223 info->avcC_param.parameter_sets = parameter_sets;
2224 /* No pending avcC. */
2225 h264_deallocate_parameter_sets( &info->avcC_param_next );
2226 uint8_t lengthSizeMinusOne = info->avcC_param_next.lengthSizeMinusOne;
2227 memset( &info->avcC_param_next, 0, sizeof(lsmash_h264_specific_parameters_t) );
2228 info->avcC_param_next.lengthSizeMinusOne = lengthSizeMinusOne;
2229 info->avcC_pending = 0;
2230 return 0;
2233 static int h264_parse_succeeded
2235 h264_info_t *info,
2236 lsmash_h264_specific_parameters_t *param
2239 int ret;
2240 if( info->sps.present && info->pps.present )
2242 *param = info->avcC_param;
2243 /* Avoid freeing parameter sets. */
2244 info->avcC_param.parameter_sets = NULL;
2245 ret = 0;
2247 else
2248 ret = LSMASH_ERR_INVALID_DATA;
2249 h264_cleanup_parser( info );
2250 return ret;
2253 static inline int h264_parse_failed
2255 h264_info_t *info,
2256 int ret
2259 h264_cleanup_parser( info );
2260 return ret;
2263 int lsmash_setup_h264_specific_parameters_from_access_unit
2265 lsmash_h264_specific_parameters_t *param,
2266 uint8_t *data,
2267 uint32_t data_length
2270 if( !param || !data || data_length == 0 )
2271 return LSMASH_ERR_FUNCTION_PARAM;
2272 h264_info_t *info = &(h264_info_t){ { 0 } };
2273 lsmash_bs_t *bs = &(lsmash_bs_t){ 0 };
2274 int err = lsmash_bs_set_empty_stream( bs, data, data_length );
2275 if( err < 0 )
2276 return err;
2277 uint64_t sc_head_pos = nalu_find_first_start_code( bs );
2278 if( sc_head_pos == NALU_NO_START_CODE_FOUND )
2279 return LSMASH_ERR_INVALID_DATA;
2280 else if( sc_head_pos == NALU_IO_ERROR )
2281 return LSMASH_ERR_IO;
2282 if( (err = h264_setup_parser( info, 1 )) < 0 )
2283 return h264_parse_failed( info, err );
2284 h264_stream_buffer_t *sb = &info->buffer;
2285 h264_slice_info_t *slice = &info->slice;
2286 while( 1 )
2288 h264_nalu_header_t nuh;
2289 uint64_t start_code_length;
2290 uint64_t trailing_zero_bytes;
2291 uint64_t nalu_length = h264_find_next_start_code( bs, &nuh, &start_code_length, &trailing_zero_bytes );
2292 if( nalu_length == NALU_NO_START_CODE_FOUND )
2293 /* For the last NALU. This NALU already has been parsed. */
2294 return h264_parse_succeeded( info, param );
2295 uint8_t nalu_type = nuh.nal_unit_type;
2296 uint64_t next_sc_head_pos = sc_head_pos
2297 + start_code_length
2298 + nalu_length
2299 + trailing_zero_bytes;
2300 if( nalu_type == H264_NALU_TYPE_FD )
2302 /* We don't support streams with both filler and HRD yet.
2303 * Otherwise, just skip filler because elemental streams defined in 14496-15 are forbidden to use filler. */
2304 if( info->sps.vui.hrd.present )
2305 return h264_parse_failed( info, LSMASH_ERR_PATCH_WELCOME );
2307 else if( (nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && nalu_type <= H264_NALU_TYPE_SPS_EXT)
2308 || nalu_type == H264_NALU_TYPE_SLICE_AUX )
2310 /* Increase the buffer if needed. */
2311 uint64_t possible_au_length = NALU_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
2312 if( sb->bank->buffer_size < possible_au_length
2313 && (err = h264_supplement_buffer( sb, NULL, 2 * possible_au_length )) < 0 )
2314 return h264_parse_failed( info, err );
2315 /* Get the EBSP of the current NALU here.
2316 * AVC elemental stream defined in 14496-15 can recognize from 0 to 13, and 19 of nal_unit_type.
2317 * We don't support SVC and MVC elemental stream defined in 14496-15 yet. */
2318 uint8_t *nalu = lsmash_bs_get_buffer_data( bs ) + start_code_length;
2319 if( nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && nalu_type <= H264_NALU_TYPE_SLICE_IDR )
2321 /* VCL NALU (slice) */
2322 h264_slice_info_t prev_slice = *slice;
2323 if( (err = h264_parse_slice( info, &nuh, sb->rbsp, nalu + nuh.length, nalu_length - nuh.length )) < 0 )
2324 return h264_parse_failed( info, err );
2325 if( prev_slice.present )
2327 /* Check whether the AU that contains the previous VCL NALU completed or not. */
2328 if( h264_find_au_delimit_by_slice_info( slice, &prev_slice ) )
2329 /* The current NALU is the first VCL NALU of the primary coded picture of an new AU.
2330 * Therefore, the previous slice belongs to that new AU. */
2331 return h264_parse_succeeded( info, param );
2333 slice->present = 1;
2335 else
2337 if( h264_find_au_delimit_by_nalu_type( nalu_type, info->prev_nalu_type ) )
2338 /* The last slice belongs to the AU you want at this time. */
2339 return h264_parse_succeeded( info, param );
2340 switch( nalu_type )
2342 case H264_NALU_TYPE_SPS :
2343 if( (err = h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_SPS, nalu, nalu_length )) < 0 )
2344 return h264_parse_failed( info, err );
2345 break;
2346 case H264_NALU_TYPE_PPS :
2347 if( (err = h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_PPS, nalu, nalu_length )) < 0 )
2348 return h264_parse_failed( info, err );
2349 break;
2350 case H264_NALU_TYPE_SPS_EXT :
2351 if( (err = h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_SPSEXT, nalu, nalu_length )) < 0 )
2352 return h264_parse_failed( info, err );
2353 break;
2354 default :
2355 break;
2359 /* Move to the first byte of the next start code. */
2360 info->prev_nalu_type = nalu_type;
2361 if( lsmash_bs_read_seek( bs, next_sc_head_pos, SEEK_SET ) != next_sc_head_pos )
2362 return h264_parse_failed( info, LSMASH_ERR_NAMELESS );
2363 /* Check if no more data to read from the stream. */
2364 if( !lsmash_bs_is_end( bs, NALU_SHORT_START_CODE_LENGTH ) )
2365 sc_head_pos = next_sc_head_pos;
2366 else
2367 return h264_parse_succeeded( info, param );
2371 int h264_construct_specific_parameters
2373 lsmash_codec_specific_t *dst,
2374 lsmash_codec_specific_t *src
2377 assert( dst && dst->data.structured && src && src->data.unstructured );
2378 if( src->size < ISOM_BASEBOX_COMMON_SIZE + 7 )
2379 return LSMASH_ERR_INVALID_DATA;
2380 lsmash_h264_specific_parameters_t *param = (lsmash_h264_specific_parameters_t *)dst->data.structured;
2381 uint8_t *data = src->data.unstructured;
2382 uint64_t size = LSMASH_GET_BE32( data );
2383 data += ISOM_BASEBOX_COMMON_SIZE;
2384 if( size == 1 )
2386 size = LSMASH_GET_BE64( data );
2387 data += 8;
2389 if( size != src->size )
2390 return LSMASH_ERR_INVALID_DATA;
2391 if( !param->parameter_sets )
2393 param->parameter_sets = h264_allocate_parameter_sets();
2394 if( !param->parameter_sets )
2395 return LSMASH_ERR_MEMORY_ALLOC;
2397 lsmash_bs_t *bs = lsmash_bs_create();
2398 if( !bs )
2399 return LSMASH_ERR_MEMORY_ALLOC;
2400 int err = lsmash_bs_import_data( bs, data, src->size - (data - src->data.unstructured) );
2401 if( err < 0 )
2402 goto fail;
2403 if( lsmash_bs_get_byte( bs ) != 1 )
2405 /* We don't support configurationVersion other than 1. */
2406 err = LSMASH_ERR_INVALID_DATA;
2407 goto fail;
2409 param->AVCProfileIndication = lsmash_bs_get_byte( bs );
2410 param->profile_compatibility = lsmash_bs_get_byte( bs );
2411 param->AVCLevelIndication = lsmash_bs_get_byte( bs );
2412 param->lengthSizeMinusOne = lsmash_bs_get_byte( bs ) & 0x03;
2413 uint8_t numOfSequenceParameterSets = lsmash_bs_get_byte( bs ) & 0x1F;
2414 if( numOfSequenceParameterSets
2415 && (err = nalu_get_dcr_ps( bs, param->parameter_sets->sps_list, numOfSequenceParameterSets )) < 0 )
2416 goto fail;
2417 uint8_t numOfPictureParameterSets = lsmash_bs_get_byte( bs );
2418 if( numOfPictureParameterSets
2419 && (err = nalu_get_dcr_ps( bs, param->parameter_sets->pps_list, numOfPictureParameterSets )) < 0 )
2420 goto fail;
2421 if( H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication ) )
2423 param->chroma_format = lsmash_bs_get_byte( bs ) & 0x03;
2424 param->bit_depth_luma_minus8 = lsmash_bs_get_byte( bs ) & 0x07;
2425 param->bit_depth_chroma_minus8 = lsmash_bs_get_byte( bs ) & 0x07;
2426 uint8_t numOfSequenceParameterSetExt = lsmash_bs_get_byte( bs );
2427 if( numOfSequenceParameterSetExt
2428 && (err = nalu_get_dcr_ps( bs, param->parameter_sets->spsext_list, numOfSequenceParameterSetExt )) < 0 )
2429 goto fail;
2431 lsmash_bs_cleanup( bs );
2432 return 0;
2433 fail:
2434 lsmash_bs_cleanup( bs );
2435 return err;
2438 int h264_print_codec_specific
2440 FILE *fp,
2441 lsmash_file_t *file,
2442 isom_box_t *box,
2443 int level
2446 assert( box->manager & LSMASH_BINARY_CODED_BOX );
2447 int indent = level;
2448 lsmash_ifprintf( fp, indent++, "[%s: AVC Configuration Box]\n", isom_4cc2str( box->type.fourcc ) );
2449 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
2450 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
2451 uint8_t *data = box->binary;
2452 uint32_t offset = isom_skip_box_common( &data );
2453 lsmash_bs_t *bs = lsmash_bs_create();
2454 if( !bs )
2455 return LSMASH_ERR_MEMORY_ALLOC;
2456 int err = lsmash_bs_import_data( bs, data, box->size - offset );
2457 if( err < 0 )
2459 lsmash_bs_cleanup( bs );
2460 return err;
2462 lsmash_ifprintf( fp, indent, "configurationVersion = %"PRIu8"\n", lsmash_bs_get_byte( bs ) );
2463 uint8_t AVCProfileIndication = lsmash_bs_get_byte( bs );
2464 lsmash_ifprintf( fp, indent, "AVCProfileIndication = %"PRIu8"\n", AVCProfileIndication );
2465 lsmash_ifprintf( fp, indent, "profile_compatibility = 0x%02"PRIx8"\n", lsmash_bs_get_byte( bs ) );
2466 lsmash_ifprintf( fp, indent, "AVCLevelIndication = %"PRIu8"\n", lsmash_bs_get_byte( bs ) );
2467 uint8_t temp8 = lsmash_bs_get_byte( bs );
2468 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 2) & 0x3F );
2469 lsmash_ifprintf( fp, indent, "lengthSizeMinusOne = %"PRIu8"\n", temp8 & 0x03 );
2470 temp8 = lsmash_bs_get_byte( bs );
2471 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 5) & 0x07 );
2472 uint8_t numOfSequenceParameterSets = temp8 & 0x1f;
2473 lsmash_ifprintf( fp, indent, "numOfSequenceParameterSets = %"PRIu8"\n", numOfSequenceParameterSets );
2474 for( uint8_t i = 0; i < numOfSequenceParameterSets; i++ )
2476 uint16_t nalUnitLength = lsmash_bs_get_be16( bs );
2477 lsmash_bs_skip_bytes( bs, nalUnitLength );
2479 uint8_t numOfPictureParameterSets = lsmash_bs_get_byte( bs );
2480 lsmash_ifprintf( fp, indent, "numOfPictureParameterSets = %"PRIu8"\n", numOfPictureParameterSets );
2481 for( uint8_t i = 0; i < numOfPictureParameterSets; i++ )
2483 uint16_t nalUnitLength = lsmash_bs_get_be16( bs );
2484 lsmash_bs_skip_bytes( bs, nalUnitLength );
2486 /* Note: there are too many files, in the world, that don't contain the following fields. */
2487 if( H264_REQUIRES_AVCC_EXTENSION( AVCProfileIndication )
2488 && (lsmash_bs_get_pos( bs ) < (box->size - offset)) )
2490 temp8 = lsmash_bs_get_byte( bs );
2491 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 2) & 0x3F );
2492 lsmash_ifprintf( fp, indent, "chroma_format = %"PRIu8"\n", temp8 & 0x03 );
2493 temp8 = lsmash_bs_get_byte( bs );
2494 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 3) & 0x1F );
2495 lsmash_ifprintf( fp, indent, "bit_depth_luma_minus8 = %"PRIu8"\n", temp8 & 0x7 );
2496 temp8 = lsmash_bs_get_byte( bs );
2497 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 3) & 0x1F );
2498 lsmash_ifprintf( fp, indent, "bit_depth_chroma_minus8 = %"PRIu8"\n", temp8 & 0x7 );
2499 lsmash_ifprintf( fp, indent, "numOfSequenceParameterSetExt = %"PRIu8"\n", lsmash_bs_get_byte( bs ) );
2501 lsmash_bs_cleanup( bs );
2502 return 0;
2505 int h264_copy_codec_specific
2507 lsmash_codec_specific_t *dst,
2508 lsmash_codec_specific_t *src
2511 assert( src && src->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && src->data.structured );
2512 assert( dst && dst->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && dst->data.structured );
2513 lsmash_h264_specific_parameters_t *src_data = (lsmash_h264_specific_parameters_t *)src->data.structured;
2514 lsmash_h264_specific_parameters_t *dst_data = (lsmash_h264_specific_parameters_t *)dst->data.structured;
2515 h264_deallocate_parameter_sets( dst_data );
2516 *dst_data = *src_data;
2517 if( !src_data->parameter_sets )
2518 return 0;
2519 dst_data->parameter_sets = h264_allocate_parameter_sets();
2520 if( !dst_data->parameter_sets )
2521 return LSMASH_ERR_MEMORY_ALLOC;
2522 for( int i = 0; i < 3; i++ )
2524 lsmash_entry_list_t *src_ps_list = h264_get_parameter_set_list( src_data, i );
2525 lsmash_entry_list_t *dst_ps_list = h264_get_parameter_set_list( dst_data, i );
2526 assert( src_ps_list && dst_ps_list );
2527 for( lsmash_entry_t *entry = src_ps_list->head; entry; entry = entry->next )
2529 isom_dcr_ps_entry_t *src_ps = (isom_dcr_ps_entry_t *)entry->data;
2530 if( !src_ps || src_ps->unused )
2531 continue;
2532 isom_dcr_ps_entry_t *dst_ps = isom_create_ps_entry( src_ps->nalUnit, src_ps->nalUnitLength );
2533 if( !dst_ps )
2535 h264_deallocate_parameter_sets( dst_data );
2536 return LSMASH_ERR_MEMORY_ALLOC;
2538 if( lsmash_list_add_entry( dst_ps_list, dst_ps ) < 0 )
2540 h264_deallocate_parameter_sets( dst_data );
2541 isom_remove_dcr_ps( dst_ps );
2542 return LSMASH_ERR_MEMORY_ALLOC;
2546 return 0;
2549 int h264_print_bitrate
2551 FILE *fp,
2552 lsmash_file_t *file,
2553 isom_box_t *box,
2554 int level
2557 assert( fp && LSMASH_IS_EXISTING_BOX( file ) && LSMASH_IS_EXISTING_BOX( box ) );
2558 int indent = level;
2559 lsmash_ifprintf( fp, indent++, "[%s: MPEG-4 Bit Rate Box]\n", isom_4cc2str( box->type.fourcc ) );
2560 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
2561 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
2562 isom_btrt_t *btrt = (isom_btrt_t *)box;
2563 lsmash_ifprintf( fp, indent, "bufferSizeDB = %"PRIu32"\n", btrt->bufferSizeDB );
2564 lsmash_ifprintf( fp, indent, "maxBitrate = %"PRIu32"\n", btrt->maxBitrate );
2565 lsmash_ifprintf( fp, indent, "avgBitrate = %"PRIu32"\n", btrt->avgBitrate );
2566 return 0;