remuxer: Add --max-chunk-size.
[L-SMASH.git] / codecs / h264.c
blobb183a0e955a507ed6b5c05722d7829ae8992149c
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 void lsmash_destroy_h264_parameter_sets
53 lsmash_h264_specific_parameters_t *param
56 if( !param || !param->parameter_sets )
57 return;
58 lsmash_remove_entries( param->parameter_sets->sps_list, isom_remove_dcr_ps );
59 lsmash_remove_entries( param->parameter_sets->pps_list, isom_remove_dcr_ps );
60 lsmash_remove_entries( param->parameter_sets->spsext_list, isom_remove_dcr_ps );
61 lsmash_free( param->parameter_sets );
62 param->parameter_sets = NULL;
65 void h264_destruct_specific_data
67 void *data
70 if( !data )
71 return;
72 lsmash_destroy_h264_parameter_sets( data );
73 lsmash_free( data );
76 void h264_cleanup_parser
78 h264_info_t *info
81 if( !info )
82 return;
83 lsmash_remove_entries( info->sps_list, NULL );
84 lsmash_remove_entries( info->pps_list, NULL );
85 lsmash_remove_entries( info->slice_list, NULL );
86 lsmash_destroy_h264_parameter_sets( &info->avcC_param );
87 lsmash_destroy_h264_parameter_sets( &info->avcC_param_next );
88 lsmash_destroy_multiple_buffers( info->buffer.bank );
89 lsmash_bits_adhoc_cleanup( info->bits );
90 info->bits = NULL;
93 int h264_setup_parser
95 h264_info_t *info,
96 int parse_only
99 assert( info );
100 memset( info, 0, sizeof(h264_info_t) );
101 info->avcC_param .lengthSizeMinusOne = NALU_DEFAULT_NALU_LENGTH_SIZE - 1;
102 info->avcC_param_next.lengthSizeMinusOne = NALU_DEFAULT_NALU_LENGTH_SIZE - 1;
103 h264_stream_buffer_t *sb = &info->buffer;
104 sb->bank = lsmash_create_multiple_buffers( parse_only ? 1 : 3, NALU_DEFAULT_BUFFER_SIZE );
105 if( !sb->bank )
106 return LSMASH_ERR_MEMORY_ALLOC;
107 sb->rbsp = lsmash_withdraw_buffer( sb->bank, 1 );
108 if( !parse_only )
110 info->au.data = lsmash_withdraw_buffer( sb->bank, 2 );
111 info->au.incomplete_data = lsmash_withdraw_buffer( sb->bank, 3 );
113 info->bits = lsmash_bits_adhoc_create();
114 if( !info->bits )
116 lsmash_destroy_multiple_buffers( sb->bank );
117 return LSMASH_ERR_MEMORY_ALLOC;
119 lsmash_init_entry_list( info->sps_list );
120 lsmash_init_entry_list( info->pps_list );
121 lsmash_init_entry_list( info->slice_list );
122 return 0;
125 static int h264_check_nalu_header
127 lsmash_bs_t *bs,
128 h264_nalu_header_t *nuh,
129 int use_long_start_code
132 uint8_t temp8 = lsmash_bs_show_byte( bs, use_long_start_code ? NALU_LONG_START_CODE_LENGTH : NALU_SHORT_START_CODE_LENGTH );
133 nuh->forbidden_zero_bit = (temp8 >> 7) & 0x01;
134 nuh->nal_ref_idc = (temp8 >> 5) & 0x03;
135 nuh->nal_unit_type = temp8 & 0x1f;
136 nuh->length = 1;
137 if( nuh->nal_unit_type == H264_NALU_TYPE_PREFIX
138 || nuh->nal_unit_type == H264_NALU_TYPE_SLICE_EXT
139 || nuh->nal_unit_type == H264_NALU_TYPE_SLICE_EXT_DVC )
141 /* We don't support these types of NALU. */
142 //nuh->length += 3;
143 return LSMASH_ERR_PATCH_WELCOME;
145 if( nuh->forbidden_zero_bit )
146 return LSMASH_ERR_INVALID_DATA;
147 /* SPS and PPS require long start code (0x00000001).
148 * Also AU delimiter requires it too because this type of NALU shall be the first NALU of any AU if present. */
149 if( !use_long_start_code
150 && (nuh->nal_unit_type == H264_NALU_TYPE_SPS
151 || nuh->nal_unit_type == H264_NALU_TYPE_PPS
152 || nuh->nal_unit_type == H264_NALU_TYPE_AUD) )
153 return LSMASH_ERR_INVALID_DATA;
154 if( nuh->nal_ref_idc )
156 /* nal_ref_idc shall be equal to 0 for all NALUs having nal_unit_type equal to 6, 9, 10, 11, or 12. */
157 if( nuh->nal_unit_type == H264_NALU_TYPE_SEI
158 || nuh->nal_unit_type == H264_NALU_TYPE_AUD
159 || nuh->nal_unit_type == H264_NALU_TYPE_EOS
160 || nuh->nal_unit_type == H264_NALU_TYPE_EOB
161 || nuh->nal_unit_type == H264_NALU_TYPE_FD )
162 return LSMASH_ERR_INVALID_DATA;
164 else
165 /* nal_ref_idc shall not be equal to 0 for NALUs with nal_unit_type equal to 5. */
166 if( nuh->nal_unit_type == H264_NALU_TYPE_SLICE_IDR )
167 return LSMASH_ERR_INVALID_DATA;
168 return 0;
171 uint64_t h264_find_next_start_code
173 lsmash_bs_t *bs,
174 h264_nalu_header_t *nuh,
175 uint64_t *start_code_length,
176 uint64_t *trailing_zero_bytes
179 uint64_t length = 0; /* the length of the latest NALU */
180 uint64_t count = 0; /* the number of the trailing zero bytes after the latest NALU */
181 /* Check the type of the current start code. */
182 int long_start_code
183 = (!lsmash_bs_is_end( bs, NALU_LONG_START_CODE_LENGTH ) && 0x00000001 == lsmash_bs_show_be32( bs, 0 )) ? 1
184 : (!lsmash_bs_is_end( bs, NALU_SHORT_START_CODE_LENGTH ) && 0x000001 == lsmash_bs_show_be24( bs, 0 )) ? 0
185 : -1;
186 if( long_start_code >= 0 && h264_check_nalu_header( bs, nuh, long_start_code ) == 0 )
188 *start_code_length = long_start_code ? NALU_LONG_START_CODE_LENGTH : NALU_SHORT_START_CODE_LENGTH;
189 uint64_t distance = *start_code_length + nuh->length;
190 /* Find the start code of the next NALU and get the distance from the start code of the latest NALU. */
191 if( !lsmash_bs_is_end( bs, distance + NALU_SHORT_START_CODE_LENGTH ) )
193 uint32_t sync_bytes = lsmash_bs_show_be24( bs, distance );
194 while( 0x000001 != sync_bytes )
196 if( lsmash_bs_is_end( bs, ++distance + NALU_SHORT_START_CODE_LENGTH ) )
198 distance = lsmash_bs_get_remaining_buffer_size( bs );
199 break;
201 sync_bytes <<= 8;
202 sync_bytes |= lsmash_bs_show_byte( bs, distance + NALU_SHORT_START_CODE_LENGTH - 1 );
203 sync_bytes &= 0xFFFFFF;
206 else
207 distance = lsmash_bs_get_remaining_buffer_size( bs );
208 /* Any NALU has no consecutive zero bytes at the end. */
209 while( 0x00 == lsmash_bs_show_byte( bs, distance - 1 ) )
211 --distance;
212 ++count;
214 /* Remove the length of the start code. */
215 length = distance - *start_code_length;
216 /* If there are one or more trailing zero bytes, we treat the last one byte as a part of the next start code.
217 * This makes the next start code a long start code. */
218 if( count )
219 --count;
221 else
223 /* No start code. */
224 nuh->forbidden_zero_bit = 1; /* shall be 0, so invalid */
225 nuh->nal_ref_idc = 0; /* arbitrary */
226 nuh->nal_unit_type = H264_NALU_TYPE_UNSPECIFIED0;
227 nuh->length = 0;
228 *start_code_length = 0;
229 length = NALU_NO_START_CODE_FOUND;
231 *trailing_zero_bytes = count;
232 return length;
235 static h264_sps_t *h264_get_sps
237 lsmash_entry_list_t *sps_list,
238 uint8_t sps_id
241 if( !sps_list || sps_id > 31 )
242 return NULL;
243 for( lsmash_entry_t *entry = sps_list->head; entry; entry = entry->next )
245 h264_sps_t *sps = (h264_sps_t *)entry->data;
246 if( !sps )
247 return NULL;
248 if( sps->seq_parameter_set_id == sps_id )
249 return sps;
251 h264_sps_t *sps = lsmash_malloc_zero( sizeof(h264_sps_t) );
252 if( !sps )
253 return NULL;
254 sps->seq_parameter_set_id = sps_id;
255 if( lsmash_add_entry( sps_list, sps ) < 0 )
257 lsmash_free( sps );
258 return NULL;
260 return sps;
263 static h264_pps_t *h264_get_pps
265 lsmash_entry_list_t *pps_list,
266 uint8_t pps_id
269 if( !pps_list )
270 return NULL;
271 for( lsmash_entry_t *entry = pps_list->head; entry; entry = entry->next )
273 h264_pps_t *pps = (h264_pps_t *)entry->data;
274 if( !pps )
275 return NULL;
276 if( pps->pic_parameter_set_id == pps_id )
277 return pps;
279 h264_pps_t *pps = lsmash_malloc_zero( sizeof(h264_pps_t) );
280 if( !pps )
281 return NULL;
282 pps->pic_parameter_set_id = pps_id;
283 if( lsmash_add_entry( pps_list, pps ) < 0 )
285 lsmash_free( pps );
286 return NULL;
288 return pps;
291 static h264_slice_info_t *h264_get_slice_info
293 lsmash_entry_list_t *slice_list,
294 uint8_t slice_id
297 if( !slice_list )
298 return NULL;
299 for( lsmash_entry_t *entry = slice_list->head; entry; entry = entry->next )
301 h264_slice_info_t *slice = (h264_slice_info_t *)entry->data;
302 if( !slice )
303 return NULL;
304 if( slice->slice_id == slice_id )
305 return slice;
307 h264_slice_info_t *slice = lsmash_malloc_zero( sizeof(h264_slice_info_t) );
308 if( !slice )
309 return NULL;
310 slice->slice_id = slice_id;
311 if( lsmash_add_entry( slice_list, slice ) < 0 )
313 lsmash_free( slice );
314 return NULL;
316 return slice;
319 int h264_calculate_poc
321 h264_info_t *info,
322 h264_picture_info_t *picture,
323 h264_picture_info_t *prev_picture
326 #if H264_POC_DEBUG_PRINT
327 fprintf( stderr, "PictureOrderCount\n" );
328 #endif
329 h264_pps_t *pps = h264_get_pps( info->pps_list, picture->pic_parameter_set_id );
330 if( !pps )
331 return LSMASH_ERR_NAMELESS;
332 h264_sps_t *sps = h264_get_sps( info->sps_list, pps->seq_parameter_set_id );
333 if( !sps )
334 return LSMASH_ERR_NAMELESS;
335 int64_t TopFieldOrderCnt = 0;
336 int64_t BottomFieldOrderCnt = 0;
337 if( sps->pic_order_cnt_type == 0 )
339 int32_t prevPicOrderCntMsb;
340 int32_t prevPicOrderCntLsb;
341 if( picture->idr )
343 prevPicOrderCntMsb = 0;
344 prevPicOrderCntLsb = 0;
346 else if( prev_picture->ref_pic_has_mmco5 )
348 prevPicOrderCntMsb = 0;
349 prevPicOrderCntLsb = prev_picture->ref_pic_bottom_field_flag ? 0 : prev_picture->ref_pic_TopFieldOrderCnt;
351 else
353 prevPicOrderCntMsb = prev_picture->ref_pic_PicOrderCntMsb;
354 prevPicOrderCntLsb = prev_picture->ref_pic_PicOrderCntLsb;
356 int64_t PicOrderCntMsb;
357 int32_t pic_order_cnt_lsb = picture->pic_order_cnt_lsb;
358 uint64_t MaxPicOrderCntLsb = sps->MaxPicOrderCntLsb;
359 if( (pic_order_cnt_lsb < prevPicOrderCntLsb)
360 && ((prevPicOrderCntLsb - pic_order_cnt_lsb) >= (MaxPicOrderCntLsb / 2)) )
361 PicOrderCntMsb = prevPicOrderCntMsb + MaxPicOrderCntLsb;
362 else if( (pic_order_cnt_lsb > prevPicOrderCntLsb)
363 && ((pic_order_cnt_lsb - prevPicOrderCntLsb) > (MaxPicOrderCntLsb / 2)) )
364 PicOrderCntMsb = prevPicOrderCntMsb - MaxPicOrderCntLsb;
365 else
366 PicOrderCntMsb = prevPicOrderCntMsb;
367 IF_EXCEED_INT32( PicOrderCntMsb )
368 return LSMASH_ERR_INVALID_DATA;
369 BottomFieldOrderCnt = TopFieldOrderCnt = PicOrderCntMsb + pic_order_cnt_lsb;
370 if( !picture->field_pic_flag )
371 BottomFieldOrderCnt += picture->delta_pic_order_cnt_bottom;
372 IF_EXCEED_INT32( TopFieldOrderCnt )
373 return LSMASH_ERR_INVALID_DATA;
374 IF_EXCEED_INT32( BottomFieldOrderCnt )
375 return LSMASH_ERR_INVALID_DATA;
376 if( !picture->disposable )
378 picture->ref_pic_has_mmco5 = picture->has_mmco5;
379 picture->ref_pic_bottom_field_flag = picture->bottom_field_flag;
380 picture->ref_pic_TopFieldOrderCnt = TopFieldOrderCnt;
381 picture->ref_pic_PicOrderCntMsb = PicOrderCntMsb;
382 picture->ref_pic_PicOrderCntLsb = pic_order_cnt_lsb;
384 #if H264_POC_DEBUG_PRINT
385 fprintf( stderr, " prevPicOrderCntMsb: %"PRId32"\n", prevPicOrderCntMsb );
386 fprintf( stderr, " prevPicOrderCntLsb: %"PRId32"\n", prevPicOrderCntLsb );
387 fprintf( stderr, " PicOrderCntMsb: %"PRId64"\n", PicOrderCntMsb );
388 fprintf( stderr, " pic_order_cnt_lsb: %"PRId32"\n", pic_order_cnt_lsb );
389 fprintf( stderr, " MaxPicOrderCntLsb: %"PRIu64"\n", MaxPicOrderCntLsb );
390 #endif
392 else if( sps->pic_order_cnt_type == 1 )
394 uint32_t frame_num = picture->frame_num;
395 uint32_t prevFrameNum = prev_picture->has_mmco5 ? 0 : prev_picture->frame_num;
396 uint32_t prevFrameNumOffset = prev_picture->has_mmco5 ? 0 : prev_picture->FrameNumOffset;
397 uint64_t FrameNumOffset = picture->idr ? 0 : prevFrameNumOffset + (prevFrameNum > frame_num ? sps->MaxFrameNum : 0);
398 if( FrameNumOffset > INT32_MAX )
399 return LSMASH_ERR_INVALID_DATA;
400 int64_t expectedPicOrderCnt;
401 if( sps->num_ref_frames_in_pic_order_cnt_cycle )
403 uint64_t absFrameNum = FrameNumOffset + frame_num;
404 absFrameNum -= picture->disposable && absFrameNum > 0;
405 if( absFrameNum )
407 uint64_t picOrderCntCycleCnt = (absFrameNum - 1) / sps->num_ref_frames_in_pic_order_cnt_cycle;
408 uint8_t frameNumInPicOrderCntCycle = (absFrameNum - 1) % sps->num_ref_frames_in_pic_order_cnt_cycle;
409 expectedPicOrderCnt = picOrderCntCycleCnt * sps->ExpectedDeltaPerPicOrderCntCycle;
410 for( uint8_t i = 0; i <= frameNumInPicOrderCntCycle; i++ )
411 expectedPicOrderCnt += sps->offset_for_ref_frame[i];
413 else
414 expectedPicOrderCnt = 0;
416 else
417 expectedPicOrderCnt = 0;
418 if( picture->disposable )
419 expectedPicOrderCnt += sps->offset_for_non_ref_pic;
420 TopFieldOrderCnt = expectedPicOrderCnt + picture->delta_pic_order_cnt[0];
421 BottomFieldOrderCnt = TopFieldOrderCnt + sps->offset_for_top_to_bottom_field;
422 if( !picture->field_pic_flag )
423 BottomFieldOrderCnt += picture->delta_pic_order_cnt[1];
424 IF_EXCEED_INT32( TopFieldOrderCnt )
425 return LSMASH_ERR_INVALID_DATA;
426 IF_EXCEED_INT32( BottomFieldOrderCnt )
427 return LSMASH_ERR_INVALID_DATA;
428 picture->FrameNumOffset = FrameNumOffset;
430 else if( sps->pic_order_cnt_type == 2 )
432 uint32_t frame_num = picture->frame_num;
433 uint32_t prevFrameNum = prev_picture->has_mmco5 ? 0 : prev_picture->frame_num;
434 int32_t prevFrameNumOffset = prev_picture->has_mmco5 ? 0 : prev_picture->FrameNumOffset;
435 int64_t FrameNumOffset;
436 int64_t tempPicOrderCnt;
437 if( picture->idr )
439 FrameNumOffset = 0;
440 tempPicOrderCnt = 0;
442 else
444 FrameNumOffset = prevFrameNumOffset + (prevFrameNum > frame_num ? sps->MaxFrameNum : 0);
445 tempPicOrderCnt = 2 * (FrameNumOffset + frame_num) - picture->disposable;
446 IF_EXCEED_INT32( FrameNumOffset )
447 return LSMASH_ERR_INVALID_DATA;
448 IF_EXCEED_INT32( tempPicOrderCnt )
449 return LSMASH_ERR_INVALID_DATA;
451 TopFieldOrderCnt = tempPicOrderCnt;
452 BottomFieldOrderCnt = tempPicOrderCnt;
453 picture->FrameNumOffset = FrameNumOffset;
455 if( !picture->field_pic_flag )
456 picture->PicOrderCnt = LSMASH_MIN( TopFieldOrderCnt, BottomFieldOrderCnt );
457 else
458 picture->PicOrderCnt = picture->bottom_field_flag ? BottomFieldOrderCnt : TopFieldOrderCnt;
459 #if H264_POC_DEBUG_PRINT
460 if( picture->field_pic_flag )
462 if( !picture->bottom_field_flag )
463 fprintf( stderr, " TopFieldOrderCnt: %"PRId64"\n", TopFieldOrderCnt );
464 else
465 fprintf( stderr, " BottomFieldOrderCnt: %"PRId64"\n", BottomFieldOrderCnt );
467 fprintf( stderr, " POC: %"PRId32"\n", picture->PicOrderCnt );
468 #endif
469 return 0;
472 static int h264_parse_scaling_list
474 lsmash_bits_t *bits,
475 int sizeOfScalingList
478 /* scaling_list( scalingList, sizeOfScalingList, useDefaultScalingMatrixFlag ) */
479 int nextScale = 8;
480 for( int i = 0; i < sizeOfScalingList; i++ )
482 int64_t delta_scale = nalu_get_exp_golomb_se( bits );
483 if( delta_scale < -128 || delta_scale > 127 )
484 return LSMASH_ERR_INVALID_DATA;
485 nextScale = (nextScale + delta_scale + 256) % 256;
486 if( nextScale == 0 )
487 break;
489 return 0;
492 static int h264_parse_hrd_parameters
494 lsmash_bits_t *bits,
495 h264_hrd_t *hrd
498 /* hrd_parameters() */
499 uint64_t cpb_cnt_minus1 = nalu_get_exp_golomb_ue( bits );
500 if( cpb_cnt_minus1 > 31 )
501 return LSMASH_ERR_INVALID_DATA;
502 lsmash_bits_get( bits, 4 ); /* bit_rate_scale */
503 lsmash_bits_get( bits, 4 ); /* cpb_size_scale */
504 for( uint64_t SchedSelIdx = 0; SchedSelIdx <= cpb_cnt_minus1; SchedSelIdx++ )
506 nalu_get_exp_golomb_ue( bits ); /* bit_rate_value_minus1[ SchedSelIdx ] */
507 nalu_get_exp_golomb_ue( bits ); /* cpb_size_value_minus1[ SchedSelIdx ] */
508 lsmash_bits_get( bits, 1 ); /* cbr_flag [ SchedSelIdx ] */
510 lsmash_bits_get( bits, 5 ); /* initial_cpb_removal_delay_length_minus1 */
511 hrd->cpb_removal_delay_length = lsmash_bits_get( bits, 5 ) + 1;
512 hrd->dpb_output_delay_length = lsmash_bits_get( bits, 5 ) + 1;
513 lsmash_bits_get( bits, 5 ); /* time_offset_length */
514 return 0;
517 static int h264_parse_sps_minimally
519 lsmash_bits_t *bits,
520 h264_sps_t *sps,
521 uint8_t *rbsp_buffer,
522 uint8_t *ebsp,
523 uint64_t ebsp_size
526 int err = nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, ebsp_size );
527 if( err < 0 )
528 return err;
529 memset( sps, 0, sizeof(h264_sps_t) );
530 sps->profile_idc = lsmash_bits_get( bits, 8 );
531 sps->constraint_set_flags = lsmash_bits_get( bits, 8 );
532 sps->level_idc = lsmash_bits_get( bits, 8 );
533 uint64_t seq_parameter_set_id = nalu_get_exp_golomb_ue( bits );
534 if( seq_parameter_set_id > 31 )
535 return LSMASH_ERR_INVALID_DATA;
536 sps->seq_parameter_set_id = seq_parameter_set_id;
537 if( sps->profile_idc == 100 || sps->profile_idc == 110 || sps->profile_idc == 122
538 || sps->profile_idc == 244 || sps->profile_idc == 44 || sps->profile_idc == 83
539 || sps->profile_idc == 86 || sps->profile_idc == 118 || sps->profile_idc == 128
540 || sps->profile_idc == 138 )
542 sps->chroma_format_idc = nalu_get_exp_golomb_ue( bits );
543 if( sps->chroma_format_idc == 3 )
544 sps->separate_colour_plane_flag = lsmash_bits_get( bits, 1 );
545 uint64_t bit_depth_luma_minus8 = nalu_get_exp_golomb_ue( bits );
546 if( bit_depth_luma_minus8 > 6 )
547 return LSMASH_ERR_INVALID_DATA;
548 uint64_t bit_depth_chroma_minus8 = nalu_get_exp_golomb_ue( bits );
549 if( bit_depth_chroma_minus8 > 6 )
550 return LSMASH_ERR_INVALID_DATA;
551 sps->bit_depth_luma_minus8 = bit_depth_luma_minus8;
552 sps->bit_depth_chroma_minus8 = bit_depth_chroma_minus8;
553 lsmash_bits_get( bits, 1 ); /* qpprime_y_zero_transform_bypass_flag */
554 if( lsmash_bits_get( bits, 1 ) ) /* seq_scaling_matrix_present_flag */
556 int num_loops = sps->chroma_format_idc != 3 ? 8 : 12;
557 for( int i = 0; i < num_loops; i++ )
558 if( lsmash_bits_get( bits, 1 ) /* seq_scaling_list_present_flag[i] */
559 && (err = h264_parse_scaling_list( bits, i < 6 ? 16 : 64 )) < 0 )
560 return err;
563 else
565 sps->chroma_format_idc = 1;
566 sps->separate_colour_plane_flag = 0;
567 sps->bit_depth_luma_minus8 = 0;
568 sps->bit_depth_chroma_minus8 = 0;
570 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
573 int h264_parse_sps
575 h264_info_t *info,
576 uint8_t *rbsp_buffer,
577 uint8_t *ebsp,
578 uint64_t ebsp_size
581 lsmash_bits_t *bits = info->bits;
582 /* seq_parameter_set_data() */
583 h264_sps_t temp_sps;
584 int err = h264_parse_sps_minimally( bits, &temp_sps, rbsp_buffer, ebsp, ebsp_size );
585 if( err < 0 )
586 return err;
587 h264_sps_t *sps = h264_get_sps( info->sps_list, temp_sps.seq_parameter_set_id );
588 if( !sps )
589 return LSMASH_ERR_NAMELESS;
590 memset( sps, 0, sizeof(h264_sps_t) );
591 sps->profile_idc = temp_sps.profile_idc;
592 sps->constraint_set_flags = temp_sps.constraint_set_flags;
593 sps->level_idc = temp_sps.level_idc;
594 sps->seq_parameter_set_id = temp_sps.seq_parameter_set_id;
595 sps->chroma_format_idc = temp_sps.chroma_format_idc;
596 sps->separate_colour_plane_flag = temp_sps.separate_colour_plane_flag;
597 sps->bit_depth_luma_minus8 = temp_sps.bit_depth_luma_minus8;
598 sps->bit_depth_chroma_minus8 = temp_sps.bit_depth_chroma_minus8;
599 sps->ChromaArrayType = sps->separate_colour_plane_flag ? 0 : sps->chroma_format_idc;
600 uint64_t log2_max_frame_num_minus4 = nalu_get_exp_golomb_ue( bits );
601 if( log2_max_frame_num_minus4 > 12 )
602 return LSMASH_ERR_INVALID_DATA;
603 sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4;
604 sps->MaxFrameNum = 1 << sps->log2_max_frame_num;
605 uint64_t pic_order_cnt_type = nalu_get_exp_golomb_ue( bits );
606 if( pic_order_cnt_type > 2 )
607 return LSMASH_ERR_INVALID_DATA;
608 sps->pic_order_cnt_type = pic_order_cnt_type;
609 if( sps->pic_order_cnt_type == 0 )
611 uint64_t log2_max_pic_order_cnt_lsb_minus4 = nalu_get_exp_golomb_ue( bits );
612 if( log2_max_pic_order_cnt_lsb_minus4 > 12 )
613 return LSMASH_ERR_INVALID_DATA;
614 sps->log2_max_pic_order_cnt_lsb = log2_max_pic_order_cnt_lsb_minus4 + 4;
615 sps->MaxPicOrderCntLsb = 1 << sps->log2_max_pic_order_cnt_lsb;
617 else if( sps->pic_order_cnt_type == 1 )
619 sps->delta_pic_order_always_zero_flag = lsmash_bits_get( bits, 1 );
620 static const int64_t max_value = (signed)(((uint64_t)1 << 31) - 1);
621 static const int64_t min_value = -(signed)(((uint64_t)1 << 31) - 1);
622 int64_t offset_for_non_ref_pic = nalu_get_exp_golomb_se( bits );
623 if( offset_for_non_ref_pic < min_value || offset_for_non_ref_pic > max_value )
624 return LSMASH_ERR_INVALID_DATA;
625 sps->offset_for_non_ref_pic = offset_for_non_ref_pic;
626 int64_t offset_for_top_to_bottom_field = nalu_get_exp_golomb_se( bits );
627 if( offset_for_top_to_bottom_field < min_value || offset_for_top_to_bottom_field > max_value )
628 return LSMASH_ERR_INVALID_DATA;
629 sps->offset_for_top_to_bottom_field = offset_for_top_to_bottom_field;
630 uint64_t num_ref_frames_in_pic_order_cnt_cycle = nalu_get_exp_golomb_ue( bits );
631 if( num_ref_frames_in_pic_order_cnt_cycle > 255 )
632 return LSMASH_ERR_INVALID_DATA;
633 sps->num_ref_frames_in_pic_order_cnt_cycle = num_ref_frames_in_pic_order_cnt_cycle;
634 sps->ExpectedDeltaPerPicOrderCntCycle = 0;
635 for( int i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++ )
637 int64_t offset_for_ref_frame = nalu_get_exp_golomb_se( bits );
638 if( offset_for_ref_frame < min_value || offset_for_ref_frame > max_value )
639 return LSMASH_ERR_INVALID_DATA;
640 sps->offset_for_ref_frame[i] = offset_for_ref_frame;
641 sps->ExpectedDeltaPerPicOrderCntCycle += offset_for_ref_frame;
644 sps->max_num_ref_frames = nalu_get_exp_golomb_ue( bits );
645 lsmash_bits_get( bits, 1 ); /* gaps_in_frame_num_value_allowed_flag */
646 uint64_t pic_width_in_mbs_minus1 = nalu_get_exp_golomb_ue( bits );
647 uint64_t pic_height_in_map_units_minus1 = nalu_get_exp_golomb_ue( bits );
648 sps->frame_mbs_only_flag = lsmash_bits_get( bits, 1 );
649 if( !sps->frame_mbs_only_flag )
650 lsmash_bits_get( bits, 1 ); /* mb_adaptive_frame_field_flag */
651 lsmash_bits_get( bits, 1 ); /* direct_8x8_inference_flag */
652 uint64_t PicWidthInMbs = pic_width_in_mbs_minus1 + 1;
653 uint64_t PicHeightInMapUnits = pic_height_in_map_units_minus1 + 1;
654 sps->PicSizeInMapUnits = PicWidthInMbs * PicHeightInMapUnits;
655 sps->cropped_width = PicWidthInMbs * 16;
656 sps->cropped_height = (2 - sps->frame_mbs_only_flag) * PicHeightInMapUnits * 16;
657 if( lsmash_bits_get( bits, 1 ) ) /* frame_cropping_flag */
659 uint8_t CropUnitX;
660 uint8_t CropUnitY;
661 if( sps->ChromaArrayType == 0 )
663 CropUnitX = 1;
664 CropUnitY = 2 - sps->frame_mbs_only_flag;
666 else
668 static const int SubWidthC [] = { 0, 2, 2, 1 };
669 static const int SubHeightC[] = { 0, 2, 1, 1 };
670 CropUnitX = SubWidthC [ sps->chroma_format_idc ];
671 CropUnitY = SubHeightC[ sps->chroma_format_idc ] * (2 - sps->frame_mbs_only_flag);
673 uint64_t frame_crop_left_offset = nalu_get_exp_golomb_ue( bits );
674 uint64_t frame_crop_right_offset = nalu_get_exp_golomb_ue( bits );
675 uint64_t frame_crop_top_offset = nalu_get_exp_golomb_ue( bits );
676 uint64_t frame_crop_bottom_offset = nalu_get_exp_golomb_ue( bits );
677 sps->cropped_width -= (frame_crop_left_offset + frame_crop_right_offset) * CropUnitX;
678 sps->cropped_height -= (frame_crop_top_offset + frame_crop_bottom_offset) * CropUnitY;
680 if( lsmash_bits_get( bits, 1 ) ) /* vui_parameters_present_flag */
682 /* vui_parameters() */
683 if( lsmash_bits_get( bits, 1 ) ) /* aspect_ratio_info_present_flag */
685 uint8_t aspect_ratio_idc = lsmash_bits_get( bits, 8 );
686 if( aspect_ratio_idc == 255 )
688 /* Extended_SAR */
689 sps->vui.sar_width = lsmash_bits_get( bits, 16 );
690 sps->vui.sar_height = lsmash_bits_get( bits, 16 );
692 else
694 static const struct
696 uint16_t sar_width;
697 uint16_t sar_height;
698 } pre_defined_sar[]
700 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
701 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 }, { 80, 33 },
702 { 18, 11 }, { 15, 11 }, { 64, 33 }, { 160, 99 }, { 4, 3 },
703 { 3, 2 }, { 2, 1 }
705 if( aspect_ratio_idc < (sizeof(pre_defined_sar) / sizeof(pre_defined_sar[0])) )
707 sps->vui.sar_width = pre_defined_sar[ aspect_ratio_idc ].sar_width;
708 sps->vui.sar_height = pre_defined_sar[ aspect_ratio_idc ].sar_height;
710 else
712 /* Behavior when unknown aspect_ratio_idc is detected is not specified in the specification. */
713 sps->vui.sar_width = 0;
714 sps->vui.sar_height = 0;
718 if( lsmash_bits_get( bits, 1 ) ) /* overscan_info_present_flag */
719 lsmash_bits_get( bits, 1 ); /* overscan_appropriate_flag */
720 if( lsmash_bits_get( bits, 1 ) ) /* video_signal_type_present_flag */
722 lsmash_bits_get( bits, 3 ); /* video_format */
723 sps->vui.video_full_range_flag = lsmash_bits_get( bits, 1 );
724 if( lsmash_bits_get( bits, 1 ) ) /* colour_description_present_flag */
726 sps->vui.colour_primaries = lsmash_bits_get( bits, 8 );
727 sps->vui.transfer_characteristics = lsmash_bits_get( bits, 8 );
728 sps->vui.matrix_coefficients = lsmash_bits_get( bits, 8 );
731 if( lsmash_bits_get( bits, 1 ) ) /* chroma_loc_info_present_flag */
733 nalu_get_exp_golomb_ue( bits ); /* chroma_sample_loc_type_top_field */
734 nalu_get_exp_golomb_ue( bits ); /* chroma_sample_loc_type_bottom_field */
736 if( lsmash_bits_get( bits, 1 ) ) /* timing_info_present_flag */
738 sps->vui.num_units_in_tick = lsmash_bits_get( bits, 32 );
739 sps->vui.time_scale = lsmash_bits_get( bits, 32 );
740 sps->vui.fixed_frame_rate_flag = lsmash_bits_get( bits, 1 );
742 else
744 sps->vui.num_units_in_tick = 1; /* arbitrary */
745 sps->vui.time_scale = 50; /* arbitrary */
746 sps->vui.fixed_frame_rate_flag = 0;
748 int nal_hrd_parameters_present_flag = lsmash_bits_get( bits, 1 );
749 if( nal_hrd_parameters_present_flag
750 && (err = h264_parse_hrd_parameters( bits, &sps->vui.hrd )) < 0 )
751 return err;
752 int vcl_hrd_parameters_present_flag = lsmash_bits_get( bits, 1 );
753 if( vcl_hrd_parameters_present_flag
754 && (err = h264_parse_hrd_parameters( bits, &sps->vui.hrd )) < 0 )
755 return err;
756 if( nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag )
758 sps->vui.hrd.present = 1;
759 sps->vui.hrd.CpbDpbDelaysPresentFlag = 1;
760 lsmash_bits_get( bits, 1 ); /* low_delay_hrd_flag */
762 sps->vui.pic_struct_present_flag = lsmash_bits_get( bits, 1 );
763 if( lsmash_bits_get( bits, 1 ) ) /* bitstream_restriction_flag */
765 lsmash_bits_get( bits, 1 ); /* motion_vectors_over_pic_boundaries_flag */
766 nalu_get_exp_golomb_ue( bits ); /* max_bytes_per_pic_denom */
767 nalu_get_exp_golomb_ue( bits ); /* max_bits_per_mb_denom */
768 nalu_get_exp_golomb_ue( bits ); /* log2_max_mv_length_horizontal */
769 nalu_get_exp_golomb_ue( bits ); /* log2_max_mv_length_vertical */
770 nalu_get_exp_golomb_ue( bits ); /* max_num_reorder_frames */
771 nalu_get_exp_golomb_ue( bits ); /* max_dec_frame_buffering */
774 else
776 sps->vui.video_full_range_flag = 0;
777 sps->vui.num_units_in_tick = 1; /* arbitrary */
778 sps->vui.time_scale = 50; /* arbitrary */
779 sps->vui.fixed_frame_rate_flag = 0;
781 /* rbsp_trailing_bits() */
782 if( !lsmash_bits_get( bits, 1 ) ) /* rbsp_stop_one_bit */
783 return LSMASH_ERR_INVALID_DATA;
784 lsmash_bits_empty( bits );
785 if( bits->bs->error )
786 return LSMASH_ERR_NAMELESS;
787 sps->present = 1;
788 info->sps = *sps;
789 return 0;
792 static int h264_parse_pps_minimally
794 lsmash_bits_t *bits,
795 h264_pps_t *pps,
796 uint8_t *rbsp_buffer,
797 uint8_t *ebsp,
798 uint64_t ebsp_size
801 int err = nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, ebsp_size );
802 if( err < 0 )
803 return err;
804 memset( pps, 0, sizeof(h264_pps_t) );
805 uint64_t pic_parameter_set_id = nalu_get_exp_golomb_ue( bits );
806 if( pic_parameter_set_id > 255 )
807 return LSMASH_ERR_INVALID_DATA;
808 pps->pic_parameter_set_id = pic_parameter_set_id;
809 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
812 int h264_parse_pps
814 h264_info_t *info,
815 uint8_t *rbsp_buffer,
816 uint8_t *ebsp,
817 uint64_t ebsp_size
820 lsmash_bits_t *bits = info->bits;
821 /* pic_parameter_set_rbsp */
822 h264_pps_t temp_pps;
823 int err = h264_parse_pps_minimally( bits, &temp_pps, rbsp_buffer, ebsp, ebsp_size );
824 if( err < 0 )
825 return err;
826 h264_pps_t *pps = h264_get_pps( info->pps_list, temp_pps.pic_parameter_set_id );
827 if( !pps )
828 return LSMASH_ERR_NAMELESS;
829 memset( pps, 0, sizeof(h264_pps_t) );
830 pps->pic_parameter_set_id = temp_pps.pic_parameter_set_id;
831 uint64_t seq_parameter_set_id = nalu_get_exp_golomb_ue( bits );
832 if( seq_parameter_set_id > 31 )
833 return LSMASH_ERR_INVALID_DATA;
834 h264_sps_t *sps = h264_get_sps( info->sps_list, seq_parameter_set_id );
835 if( !sps )
836 return LSMASH_ERR_NAMELESS;
837 pps->seq_parameter_set_id = seq_parameter_set_id;
838 pps->entropy_coding_mode_flag = lsmash_bits_get( bits, 1 );
839 pps->bottom_field_pic_order_in_frame_present_flag = lsmash_bits_get( bits, 1 );
840 uint64_t num_slice_groups_minus1 = nalu_get_exp_golomb_ue( bits );
841 if( num_slice_groups_minus1 > 7 )
842 return LSMASH_ERR_INVALID_DATA;
843 pps->num_slice_groups_minus1 = num_slice_groups_minus1;
844 if( num_slice_groups_minus1 ) /* num_slice_groups_minus1 */
846 uint64_t slice_group_map_type = nalu_get_exp_golomb_ue( bits );
847 if( slice_group_map_type > 6 )
848 return LSMASH_ERR_INVALID_DATA;
849 pps->slice_group_map_type = slice_group_map_type;
850 if( slice_group_map_type == 0 )
851 for( uint64_t iGroup = 0; iGroup <= num_slice_groups_minus1; iGroup++ )
852 nalu_get_exp_golomb_ue( bits ); /* run_length_minus1[ iGroup ] */
853 else if( slice_group_map_type == 2 )
854 for( uint64_t iGroup = 0; iGroup < num_slice_groups_minus1; iGroup++ )
856 nalu_get_exp_golomb_ue( bits ); /* top_left [ iGroup ] */
857 nalu_get_exp_golomb_ue( bits ); /* bottom_right[ iGroup ] */
859 else if( slice_group_map_type == 3
860 || slice_group_map_type == 4
861 || slice_group_map_type == 5 )
863 lsmash_bits_get( bits, 1 ); /* slice_group_change_direction_flag */
864 uint64_t slice_group_change_rate_minus1 = nalu_get_exp_golomb_ue( bits );
865 if( slice_group_change_rate_minus1 > (sps->PicSizeInMapUnits - 1) )
866 return LSMASH_ERR_INVALID_DATA;
867 pps->SliceGroupChangeRate = slice_group_change_rate_minus1 + 1;
869 else if( slice_group_map_type == 6 )
871 uint64_t pic_size_in_map_units_minus1 = nalu_get_exp_golomb_ue( bits );
872 int length = lsmash_ceil_log2( num_slice_groups_minus1 + 1 );
873 for( uint64_t i = 0; i <= pic_size_in_map_units_minus1; i++ )
874 /* slice_group_id */
875 if( lsmash_bits_get( bits, length ) > num_slice_groups_minus1 )
876 return LSMASH_ERR_INVALID_DATA;
879 pps->num_ref_idx_l0_default_active_minus1 = nalu_get_exp_golomb_ue( bits );
880 pps->num_ref_idx_l1_default_active_minus1 = nalu_get_exp_golomb_ue( bits );
881 pps->weighted_pred_flag = lsmash_bits_get( bits, 1 );
882 pps->weighted_bipred_idc = lsmash_bits_get( bits, 2 );
883 nalu_get_exp_golomb_se( bits ); /* pic_init_qp_minus26 */
884 nalu_get_exp_golomb_se( bits ); /* pic_init_qs_minus26 */
885 nalu_get_exp_golomb_se( bits ); /* chroma_qp_index_offset */
886 pps->deblocking_filter_control_present_flag = lsmash_bits_get( bits, 1 );
887 lsmash_bits_get( bits, 1 ); /* constrained_intra_pred_flag */
888 pps->redundant_pic_cnt_present_flag = lsmash_bits_get( bits, 1 );
889 if( nalu_check_more_rbsp_data( bits ) )
891 int transform_8x8_mode_flag = lsmash_bits_get( bits, 1 );
892 if( lsmash_bits_get( bits, 1 ) ) /* pic_scaling_matrix_present_flag */
894 int num_loops = 6 + (sps->chroma_format_idc != 3 ? 2 : 6) * transform_8x8_mode_flag;
895 for( int i = 0; i < num_loops; i++ )
896 if( lsmash_bits_get( bits, 1 ) /* pic_scaling_list_present_flag[i] */
897 && (err = h264_parse_scaling_list( bits, i < 6 ? 16 : 64 )) < 0 )
898 return err;
900 nalu_get_exp_golomb_se( bits ); /* second_chroma_qp_index_offset */
902 /* rbsp_trailing_bits() */
903 if( !lsmash_bits_get( bits, 1 ) ) /* rbsp_stop_one_bit */
904 return LSMASH_ERR_INVALID_DATA;
905 lsmash_bits_empty( bits );
906 if( bits->bs->error )
907 return LSMASH_ERR_NAMELESS;
908 pps->present = 1;
909 info->sps = *sps;
910 info->pps = *pps;
911 return 0;
914 int h264_parse_sei
916 lsmash_bits_t *bits,
917 h264_sps_t *sps,
918 h264_sei_t *sei,
919 uint8_t *rbsp_buffer,
920 uint8_t *ebsp,
921 uint64_t ebsp_size
924 int err = nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, ebsp_size );
925 if( err < 0 )
926 return err;
927 uint8_t *rbsp_start = rbsp_buffer;
928 uint64_t rbsp_pos = 0;
931 /* sei_message() */
932 uint32_t payloadType = 0;
933 for( uint8_t temp = lsmash_bits_get( bits, 8 ); ; temp = lsmash_bits_get( bits, 8 ) )
935 /* 0xff : ff_byte
936 * otherwise: last_payload_type_byte */
937 payloadType += temp;
938 ++rbsp_pos;
939 if( temp != 0xff )
940 break;
942 uint32_t payloadSize = 0;
943 for( uint8_t temp = lsmash_bits_get( bits, 8 ); ; temp = lsmash_bits_get( bits, 8 ) )
945 /* 0xff : ff_byte
946 * otherwise: last_payload_size_byte */
947 payloadSize += temp;
948 ++rbsp_pos;
949 if( temp != 0xff )
950 break;
952 if( payloadType == 1 )
954 /* pic_timing */
955 h264_hrd_t *hrd = sps ? &sps->vui.hrd : NULL;
956 if( !hrd )
957 goto skip_sei_message; /* Any active SPS is not found. */
958 sei->pic_timing.present = 1;
959 if( hrd->CpbDpbDelaysPresentFlag )
961 lsmash_bits_get( bits, hrd->cpb_removal_delay_length ); /* cpb_removal_delay */
962 lsmash_bits_get( bits, hrd->dpb_output_delay_length ); /* dpb_output_delay */
964 if( sps->vui.pic_struct_present_flag )
966 sei->pic_timing.pic_struct = lsmash_bits_get( bits, 4 );
967 /* Skip the remaining bits. */
968 uint32_t remaining_bits = payloadSize * 8 - 4;
969 if( hrd->CpbDpbDelaysPresentFlag )
970 remaining_bits -= hrd->cpb_removal_delay_length
971 + hrd->dpb_output_delay_length;
972 lsmash_bits_get( bits, remaining_bits );
975 else if( payloadType == 3 )
977 /* filler_payload
978 * 'avc1' and 'avc2' samples are forbidden to contain this. */
979 return LSMASH_ERR_PATCH_WELCOME;
981 else if( payloadType == 6 )
983 /* recovery_point */
984 sei->recovery_point.present = 1;
985 sei->recovery_point.random_accessible = 1;
986 sei->recovery_point.recovery_frame_cnt = nalu_get_exp_golomb_ue( bits );
987 lsmash_bits_get( bits, 1 ); /* exact_match_flag */
988 sei->recovery_point.broken_link_flag = lsmash_bits_get( bits, 1 );
989 lsmash_bits_get( bits, 2 ); /* changing_slice_group_idc */
991 else
993 skip_sei_message:
994 lsmash_bits_get( bits, payloadSize * 8 );
996 lsmash_bits_get_align( bits );
997 rbsp_pos += payloadSize;
998 } while( *(rbsp_start + rbsp_pos) != 0x80 ); /* All SEI messages are byte aligned at their end.
999 * Therefore, 0x80 shall be rbsp_trailing_bits(). */
1000 lsmash_bits_empty( bits );
1001 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
1004 static int h264_parse_slice_header
1006 h264_info_t *info,
1007 h264_nalu_header_t *nuh
1010 h264_slice_info_t *slice = &info->slice;
1011 memset( slice, 0, sizeof(h264_slice_info_t) );
1012 /* slice_header() */
1013 lsmash_bits_t *bits = info->bits;
1014 nalu_get_exp_golomb_ue( bits ); /* first_mb_in_slice */
1015 uint8_t slice_type = slice->type = nalu_get_exp_golomb_ue( bits );
1016 if( (uint64_t)slice->type > 9 )
1017 return LSMASH_ERR_INVALID_DATA;
1018 if( slice_type > 4 )
1019 slice_type = slice->type -= 5;
1020 uint64_t pic_parameter_set_id = nalu_get_exp_golomb_ue( bits );
1021 if( pic_parameter_set_id > 255 )
1022 return LSMASH_ERR_INVALID_DATA;
1023 slice->pic_parameter_set_id = pic_parameter_set_id;
1024 h264_pps_t *pps = h264_get_pps( info->pps_list, pic_parameter_set_id );
1025 if( !pps )
1026 return LSMASH_ERR_NAMELESS;
1027 h264_sps_t *sps = h264_get_sps( info->sps_list, pps->seq_parameter_set_id );
1028 if( !sps )
1029 return LSMASH_ERR_NAMELESS;
1030 slice->seq_parameter_set_id = pps->seq_parameter_set_id;
1031 slice->nal_ref_idc = nuh->nal_ref_idc;
1032 slice->IdrPicFlag = (nuh->nal_unit_type == H264_NALU_TYPE_SLICE_IDR);
1033 slice->pic_order_cnt_type = sps->pic_order_cnt_type;
1034 if( (slice->IdrPicFlag || sps->max_num_ref_frames == 0) && slice_type != 2 && slice_type != 4 )
1035 return LSMASH_ERR_INVALID_DATA;
1036 if( sps->separate_colour_plane_flag )
1037 lsmash_bits_get( bits, 2 ); /* colour_plane_id */
1038 uint64_t frame_num = lsmash_bits_get( bits, sps->log2_max_frame_num );
1039 if( frame_num >= (1ULL << sps->log2_max_frame_num) || (slice->IdrPicFlag && frame_num) )
1040 return LSMASH_ERR_INVALID_DATA;
1041 slice->frame_num = frame_num;
1042 if( !sps->frame_mbs_only_flag )
1044 slice->field_pic_flag = lsmash_bits_get( bits, 1 );
1045 if( slice->field_pic_flag )
1046 slice->bottom_field_flag = lsmash_bits_get( bits, 1 );
1048 if( slice->IdrPicFlag )
1050 uint64_t idr_pic_id = nalu_get_exp_golomb_ue( bits );
1051 if( idr_pic_id > 65535 )
1052 return LSMASH_ERR_INVALID_DATA;
1053 slice->idr_pic_id = idr_pic_id;
1055 if( sps->pic_order_cnt_type == 0 )
1057 uint64_t pic_order_cnt_lsb = lsmash_bits_get( bits, sps->log2_max_pic_order_cnt_lsb );
1058 if( pic_order_cnt_lsb >= sps->MaxPicOrderCntLsb )
1059 return LSMASH_ERR_INVALID_DATA;
1060 slice->pic_order_cnt_lsb = pic_order_cnt_lsb;
1061 if( pps->bottom_field_pic_order_in_frame_present_flag && !slice->field_pic_flag )
1062 slice->delta_pic_order_cnt_bottom = nalu_get_exp_golomb_se( bits );
1064 else if( sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag )
1066 slice->delta_pic_order_cnt[0] = nalu_get_exp_golomb_se( bits );
1067 if( pps->bottom_field_pic_order_in_frame_present_flag && !slice->field_pic_flag )
1068 slice->delta_pic_order_cnt[1] = nalu_get_exp_golomb_se( bits );
1070 if( pps->redundant_pic_cnt_present_flag )
1072 uint64_t redundant_pic_cnt = nalu_get_exp_golomb_ue( bits );
1073 if( redundant_pic_cnt > 127 )
1074 return LSMASH_ERR_INVALID_DATA;
1075 slice->has_redundancy = !!redundant_pic_cnt;
1077 if( slice_type == H264_SLICE_TYPE_B )
1078 lsmash_bits_get( bits, 1 );
1079 uint64_t num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
1080 uint64_t num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
1081 if( slice_type == H264_SLICE_TYPE_P || slice_type == H264_SLICE_TYPE_SP || slice_type == H264_SLICE_TYPE_B )
1083 if( lsmash_bits_get( bits, 1 ) ) /* num_ref_idx_active_override_flag */
1085 num_ref_idx_l0_active_minus1 = nalu_get_exp_golomb_ue( bits );
1086 if( num_ref_idx_l0_active_minus1 > 31 )
1087 return LSMASH_ERR_INVALID_DATA;
1088 if( slice_type == H264_SLICE_TYPE_B )
1090 num_ref_idx_l1_active_minus1 = nalu_get_exp_golomb_ue( bits );
1091 if( num_ref_idx_l1_active_minus1 > 31 )
1092 return LSMASH_ERR_INVALID_DATA;
1096 if( nuh->nal_unit_type == H264_NALU_TYPE_SLICE_EXT
1097 || nuh->nal_unit_type == H264_NALU_TYPE_SLICE_EXT_DVC )
1099 return LSMASH_ERR_PATCH_WELCOME; /* No support of MVC yet */
1100 #if 0
1101 /* ref_pic_list_mvc_modification() */
1102 if( slice_type == H264_SLICE_TYPE_P || slice_type == H264_SLICE_TYPE_B || slice_type == H264_SLICE_TYPE_SP )
1104 for( int i = 0; i < 1 + (slice_type == H264_SLICE_TYPE_B); i++ )
1106 if( lsmash_bits_get( bits, 1 ) ) /* (S)P and B: ref_pic_list_modification_flag_l0
1107 * B: ref_pic_list_modification_flag_l1 */
1109 uint64_t modification_of_pic_nums_idc;
1112 modification_of_pic_nums_idc = nalu_get_exp_golomb_ue( bits );
1113 #if 0
1114 if( modification_of_pic_nums_idc == 0 || modification_of_pic_nums_idc == 1 )
1115 nalu_get_exp_golomb_ue( bits ); /* abs_diff_pic_num_minus1 */
1116 else if( modification_of_pic_nums_idc == 2 )
1117 nalu_get_exp_golomb_ue( bits ); /* long_term_pic_num */
1118 else if( modification_of_pic_nums_idc == 4 || modification_of_pic_nums_idc == 5 )
1119 nalu_get_exp_golomb_ue( bits ); /* abs_diff_view_idx_minus1 */
1120 #else
1121 if( modification_of_pic_nums_idc != 3 )
1122 nalu_get_exp_golomb_ue( bits ); /* abs_diff_pic_num_minus1, long_term_pic_num or abs_diff_view_idx_minus1 */
1123 #endif
1124 } while( modification_of_pic_nums_idc != 3 );
1128 #endif
1130 else
1132 /* ref_pic_list_modification() */
1133 if( slice_type == H264_SLICE_TYPE_P || slice_type == H264_SLICE_TYPE_B || slice_type == H264_SLICE_TYPE_SP )
1135 for( int i = 0; i < 1 + (slice_type == H264_SLICE_TYPE_B); i++ )
1137 if( lsmash_bits_get( bits, 1 ) ) /* (S)P and B: ref_pic_list_modification_flag_l0
1138 * B: ref_pic_list_modification_flag_l1 */
1140 uint64_t modification_of_pic_nums_idc;
1143 modification_of_pic_nums_idc = nalu_get_exp_golomb_ue( bits );
1144 #if 0
1145 if( modification_of_pic_nums_idc == 0 || modification_of_pic_nums_idc == 1 )
1146 nalu_get_exp_golomb_ue( bits ); /* abs_diff_pic_num_minus1 */
1147 else if( modification_of_pic_nums_idc == 2 )
1148 nalu_get_exp_golomb_ue( bits ); /* long_term_pic_num */
1149 #else
1150 if( modification_of_pic_nums_idc != 3 )
1151 nalu_get_exp_golomb_ue( bits ); /* abs_diff_pic_num_minus1 or long_term_pic_num */
1152 #endif
1153 } while( modification_of_pic_nums_idc != 3 );
1158 if( (pps->weighted_pred_flag && (slice_type == H264_SLICE_TYPE_P || slice_type == H264_SLICE_TYPE_SP))
1159 || (pps->weighted_bipred_idc == 1 && slice_type == H264_SLICE_TYPE_B) )
1161 /* pred_weight_table() */
1162 nalu_get_exp_golomb_ue( bits ); /* luma_log2_weight_denom */
1163 if( sps->ChromaArrayType )
1164 nalu_get_exp_golomb_ue( bits ); /* chroma_log2_weight_denom */
1165 for( uint8_t i = 0; i <= num_ref_idx_l0_active_minus1; i++ )
1167 if( lsmash_bits_get( bits, 1 ) ) /* luma_weight_l0_flag */
1169 nalu_get_exp_golomb_se( bits ); /* luma_weight_l0[i] */
1170 nalu_get_exp_golomb_se( bits ); /* luma_offset_l0[i] */
1172 if( sps->ChromaArrayType
1173 && lsmash_bits_get( bits, 1 ) /* chroma_weight_l0_flag */ )
1174 for( int j = 0; j < 2; j++ )
1176 nalu_get_exp_golomb_se( bits ); /* chroma_weight_l0[i][j]*/
1177 nalu_get_exp_golomb_se( bits ); /* chroma_offset_l0[i][j] */
1180 if( slice_type == H264_SLICE_TYPE_B )
1181 for( uint8_t i = 0; i <= num_ref_idx_l1_active_minus1; i++ )
1183 if( lsmash_bits_get( bits, 1 ) ) /* luma_weight_l1_flag */
1185 nalu_get_exp_golomb_se( bits ); /* luma_weight_l1[i] */
1186 nalu_get_exp_golomb_se( bits ); /* luma_offset_l1[i] */
1188 if( sps->ChromaArrayType
1189 && lsmash_bits_get( bits, 1 ) /* chroma_weight_l1_flag */ )
1190 for( int j = 0; j < 2; j++ )
1192 nalu_get_exp_golomb_se( bits ); /* chroma_weight_l1[i][j]*/
1193 nalu_get_exp_golomb_se( bits ); /* chroma_offset_l1[i][j] */
1197 if( nuh->nal_ref_idc )
1199 /* dec_ref_pic_marking() */
1200 if( slice->IdrPicFlag )
1202 lsmash_bits_get( bits, 1 ); /* no_output_of_prior_pics_flag */
1203 lsmash_bits_get( bits, 1 ); /* long_term_reference_flag */
1205 else if( lsmash_bits_get( bits, 1 ) ) /* adaptive_ref_pic_marking_mode_flag */
1207 uint64_t memory_management_control_operation;
1210 memory_management_control_operation = nalu_get_exp_golomb_ue( bits );
1211 if( memory_management_control_operation )
1213 if( memory_management_control_operation == 5 )
1214 slice->has_mmco5 = 1;
1215 else
1217 nalu_get_exp_golomb_ue( bits );
1218 if( memory_management_control_operation == 3 )
1219 nalu_get_exp_golomb_ue( bits );
1222 } while( memory_management_control_operation );
1225 /* We needn't read more if not slice data partition A.
1226 * Skip slice_data() and rbsp_slice_trailing_bits(). */
1227 if( nuh->nal_unit_type == H264_NALU_TYPE_SLICE_DP_A )
1229 if( pps->entropy_coding_mode_flag && slice_type != H264_SLICE_TYPE_I && slice_type != H264_SLICE_TYPE_SI )
1230 nalu_get_exp_golomb_ue( bits ); /* cabac_init_idc */
1231 nalu_get_exp_golomb_se( bits ); /* slice_qp_delta */
1232 if( slice_type == H264_SLICE_TYPE_SP || slice_type == H264_SLICE_TYPE_SI )
1234 if( slice_type == H264_SLICE_TYPE_SP )
1235 lsmash_bits_get( bits, 1 ); /* sp_for_switch_flag */
1236 nalu_get_exp_golomb_se( bits ); /* slice_qs_delta */
1238 if( pps->deblocking_filter_control_present_flag
1239 && nalu_get_exp_golomb_ue( bits ) != 1 /* disable_deblocking_filter_idc */ )
1241 int64_t slice_alpha_c0_offset_div2 = nalu_get_exp_golomb_se( bits );
1242 if( slice_alpha_c0_offset_div2 < -6 || slice_alpha_c0_offset_div2 > 6 )
1243 return LSMASH_ERR_INVALID_DATA;
1244 int64_t slice_beta_offset_div2 = nalu_get_exp_golomb_se( bits );
1245 if( slice_beta_offset_div2 < -6 || slice_beta_offset_div2 > 6 )
1246 return LSMASH_ERR_INVALID_DATA;
1248 if( pps->num_slice_groups_minus1
1249 && (pps->slice_group_map_type == 3 || pps->slice_group_map_type == 4 || pps->slice_group_map_type == 5) )
1251 uint64_t temp = ((uint64_t)sps->PicSizeInMapUnits - 1) / pps->SliceGroupChangeRate + 1;
1252 uint64_t slice_group_change_cycle = lsmash_bits_get( bits, lsmash_ceil_log2( temp + 1 ) );
1253 if( slice_group_change_cycle > temp )
1254 return LSMASH_ERR_INVALID_DATA;
1256 /* end of slice_header() */
1257 slice->slice_id = nalu_get_exp_golomb_ue( bits );
1258 h264_slice_info_t *slice_part = h264_get_slice_info( info->slice_list, slice->slice_id );
1259 if( !slice_part )
1260 return LSMASH_ERR_NAMELESS;
1261 *slice_part = *slice;
1263 lsmash_bits_empty( bits );
1264 if( bits->bs->error )
1265 return LSMASH_ERR_NAMELESS;
1266 info->sps = *sps;
1267 info->pps = *pps;
1268 return 0;
1271 int h264_parse_slice
1273 h264_info_t *info,
1274 h264_nalu_header_t *nuh,
1275 uint8_t *rbsp_buffer,
1276 uint8_t *ebsp,
1277 uint64_t ebsp_size
1280 lsmash_bits_t *bits = info->bits;
1281 uint64_t size = nuh->nal_unit_type == H264_NALU_TYPE_SLICE_IDR || nuh->nal_ref_idc == 0
1282 ? LSMASH_MIN( ebsp_size, 100 )
1283 : LSMASH_MIN( ebsp_size, 1000 );
1284 int err = nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, size );
1285 if( err < 0 )
1286 return err;
1287 if( nuh->nal_unit_type != H264_NALU_TYPE_SLICE_DP_B
1288 && nuh->nal_unit_type != H264_NALU_TYPE_SLICE_DP_C )
1289 return h264_parse_slice_header( info, nuh );
1290 /* slice_data_partition_b_layer_rbsp() or slice_data_partition_c_layer_rbsp() */
1291 uint64_t slice_id = nalu_get_exp_golomb_ue( bits );
1292 h264_slice_info_t *slice = h264_get_slice_info( info->slice_list, slice_id );
1293 if( !slice )
1294 return LSMASH_ERR_NAMELESS;
1295 h264_pps_t *pps = h264_get_pps( info->pps_list, slice->pic_parameter_set_id );
1296 if( !pps )
1297 return LSMASH_ERR_NAMELESS;
1298 h264_sps_t *sps = h264_get_sps( info->sps_list, pps->seq_parameter_set_id );
1299 if( !sps )
1300 return LSMASH_ERR_NAMELESS;
1301 slice->seq_parameter_set_id = pps->seq_parameter_set_id;
1302 if( sps->separate_colour_plane_flag )
1303 lsmash_bits_get( bits, 2 ); /* colour_plane_id */
1304 if( pps->redundant_pic_cnt_present_flag )
1306 uint64_t redundant_pic_cnt = nalu_get_exp_golomb_ue( bits );
1307 if( redundant_pic_cnt > 127 )
1308 return LSMASH_ERR_INVALID_DATA;
1309 slice->has_redundancy = !!redundant_pic_cnt;
1311 /* Skip slice_data() and rbsp_slice_trailing_bits(). */
1312 lsmash_bits_empty( bits );
1313 if( bits->bs->error )
1314 return LSMASH_ERR_NAMELESS;
1315 info->sps = *sps;
1316 info->pps = *pps;
1317 return 0;
1320 static int h264_get_sps_id
1322 uint8_t *ps_ebsp,
1323 uint32_t ps_ebsp_length,
1324 uint8_t *ps_id
1327 /* max number of bits of sps_id = 11: 0b000001XXXXX
1328 * (24 + 11 - 1) / 8 + 1 = 5 bytes
1329 * Why +1? Because there might be an emulation_prevention_three_byte. */
1330 lsmash_bits_t bits = { 0 };
1331 lsmash_bs_t bs = { 0 };
1332 uint8_t rbsp_buffer[6];
1333 uint8_t buffer [6];
1334 bs.buffer.data = buffer;
1335 bs.buffer.alloc = 6;
1336 lsmash_bits_init( &bits, &bs );
1337 int err = nalu_import_rbsp_from_ebsp( &bits, rbsp_buffer, ps_ebsp, LSMASH_MIN( ps_ebsp_length, 6 ) );
1338 if( err < 0 )
1339 return err;
1340 lsmash_bits_get( &bits, 24 ); /* profile_idc, constraint_set_flags and level_idc */
1341 uint64_t sec_parameter_set_id = nalu_get_exp_golomb_ue( &bits );
1342 if( sec_parameter_set_id > 31 )
1343 return LSMASH_ERR_INVALID_DATA;
1344 *ps_id = sec_parameter_set_id;
1345 return bs.error ? LSMASH_ERR_NAMELESS : 0;
1348 static int h264_get_pps_id
1350 uint8_t *ps_ebsp,
1351 uint32_t ps_ebsp_length,
1352 uint8_t *ps_id
1355 /* max number of bits of pps_id = 17: 0b000000001XXXXXXXX
1356 * (17 - 1) / 8 + 1 = 3 bytes
1357 * Why +1? Because there might be an emulation_prevention_three_byte. */
1358 lsmash_bits_t bits = { 0 };
1359 lsmash_bs_t bs = { 0 };
1360 uint8_t rbsp_buffer[4];
1361 uint8_t buffer [4];
1362 bs.buffer.data = buffer;
1363 bs.buffer.alloc = 4;
1364 lsmash_bits_init( &bits, &bs );
1365 int err = nalu_import_rbsp_from_ebsp( &bits, rbsp_buffer, ps_ebsp, LSMASH_MIN( ps_ebsp_length, 4 ) );
1366 if( err < 0 )
1367 return err;
1368 uint64_t pic_parameter_set_id = nalu_get_exp_golomb_ue( &bits );
1369 if( pic_parameter_set_id > 255 )
1370 return LSMASH_ERR_INVALID_DATA;
1371 *ps_id = pic_parameter_set_id;
1372 return bs.error ? LSMASH_ERR_NAMELESS : 0;
1375 static inline int h264_get_ps_id
1377 uint8_t *ps_ebsp,
1378 uint32_t ps_ebsp_length,
1379 uint8_t *ps_id,
1380 lsmash_h264_parameter_set_type ps_type
1383 int (*get_ps_id)( uint8_t *ps_ebsp, uint32_t ps_ebsp_length, uint8_t *ps_id )
1384 = ps_type == H264_PARAMETER_SET_TYPE_SPS ? h264_get_sps_id
1385 : ps_type == H264_PARAMETER_SET_TYPE_PPS ? h264_get_pps_id
1386 : NULL;
1387 return get_ps_id ? get_ps_id( ps_ebsp, ps_ebsp_length, ps_id ) : LSMASH_ERR_INVALID_DATA;
1390 static inline lsmash_entry_list_t *h264_get_parameter_set_list
1392 lsmash_h264_specific_parameters_t *param,
1393 lsmash_h264_parameter_set_type ps_type
1396 if( !param->parameter_sets )
1397 return NULL;
1398 return ps_type == H264_PARAMETER_SET_TYPE_SPS ? param->parameter_sets->sps_list
1399 : ps_type == H264_PARAMETER_SET_TYPE_PPS ? param->parameter_sets->pps_list
1400 : ps_type == H264_PARAMETER_SET_TYPE_SPSEXT ? param->parameter_sets->spsext_list
1401 : NULL;
1404 static lsmash_entry_t *h264_get_ps_entry_from_param
1406 lsmash_h264_specific_parameters_t *param,
1407 lsmash_h264_parameter_set_type ps_type,
1408 uint8_t ps_id
1411 int (*get_ps_id)( uint8_t *ps_ebsp, uint32_t ps_ebsp_length, uint8_t *ps_id )
1412 = ps_type == H264_PARAMETER_SET_TYPE_SPS ? h264_get_sps_id
1413 : ps_type == H264_PARAMETER_SET_TYPE_PPS ? h264_get_pps_id
1414 : NULL;
1415 if( !get_ps_id )
1416 return NULL;
1417 lsmash_entry_list_t *ps_list = h264_get_parameter_set_list( param, ps_type );
1418 if( !ps_list )
1419 return NULL;
1420 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
1422 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1423 if( !ps )
1424 return NULL;
1425 uint8_t param_ps_id;
1426 if( get_ps_id( ps->nalUnit + 1, ps->nalUnitLength - 1, &param_ps_id ) < 0 )
1427 return NULL;
1428 if( ps_id == param_ps_id )
1429 return entry;
1431 return NULL;
1434 static inline void h264_update_picture_type
1436 h264_picture_info_t *picture,
1437 h264_slice_info_t *slice
1440 if( picture->type == H264_PICTURE_TYPE_I_P )
1442 if( slice->type == H264_SLICE_TYPE_B )
1443 picture->type = H264_PICTURE_TYPE_I_P_B;
1444 else if( slice->type == H264_SLICE_TYPE_SI || slice->type == H264_SLICE_TYPE_SP )
1445 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1447 else if( picture->type == H264_PICTURE_TYPE_I_P_B )
1449 if( slice->type != H264_SLICE_TYPE_P && slice->type != H264_SLICE_TYPE_B && slice->type != H264_SLICE_TYPE_I )
1450 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1452 else if( picture->type == H264_PICTURE_TYPE_I )
1454 if( slice->type == H264_SLICE_TYPE_P )
1455 picture->type = H264_PICTURE_TYPE_I_P;
1456 else if( slice->type == H264_SLICE_TYPE_B )
1457 picture->type = H264_PICTURE_TYPE_I_P_B;
1458 else if( slice->type == H264_SLICE_TYPE_SI )
1459 picture->type = H264_PICTURE_TYPE_I_SI;
1460 else if( slice->type == H264_SLICE_TYPE_SP )
1461 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1463 else if( picture->type == H264_PICTURE_TYPE_SI_SP )
1465 if( slice->type == H264_SLICE_TYPE_P || slice->type == H264_SLICE_TYPE_I )
1466 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1467 else if( slice->type == H264_SLICE_TYPE_B )
1468 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1470 else if( picture->type == H264_PICTURE_TYPE_SI )
1472 if( slice->type == H264_SLICE_TYPE_P )
1473 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1474 else if( slice->type == H264_SLICE_TYPE_B )
1475 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1476 else if( slice->type != H264_SLICE_TYPE_I )
1477 picture->type = H264_PICTURE_TYPE_I_SI;
1478 else if( slice->type == H264_SLICE_TYPE_SP )
1479 picture->type = H264_PICTURE_TYPE_SI_SP;
1481 else if( picture->type == H264_PICTURE_TYPE_I_SI )
1483 if( slice->type == H264_SLICE_TYPE_P || slice->type == H264_SLICE_TYPE_SP )
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_I_SI_P_SP )
1490 if( slice->type == H264_SLICE_TYPE_B )
1491 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1493 else if( picture->type == H264_PICTURE_TYPE_NONE )
1495 if( slice->type == H264_SLICE_TYPE_P )
1496 picture->type = H264_PICTURE_TYPE_I_P;
1497 else if( slice->type == H264_SLICE_TYPE_B )
1498 picture->type = H264_PICTURE_TYPE_I_P_B;
1499 else if( slice->type == H264_SLICE_TYPE_I )
1500 picture->type = H264_PICTURE_TYPE_I;
1501 else if( slice->type == H264_SLICE_TYPE_SI )
1502 picture->type = H264_PICTURE_TYPE_SI;
1503 else if( slice->type == H264_SLICE_TYPE_SP )
1504 picture->type = H264_PICTURE_TYPE_SI_SP;
1506 #if 0
1507 fprintf( stderr, "Picture type = %s\n", picture->type == H264_PICTURE_TYPE_I_P ? "P"
1508 : picture->type == H264_PICTURE_TYPE_I_P_B ? "B"
1509 : picture->type == H264_PICTURE_TYPE_I ? "I"
1510 : picture->type == H264_PICTURE_TYPE_SI ? "SI"
1511 : picture->type == H264_PICTURE_TYPE_I_SI ? "SI"
1512 : "SP" );
1513 #endif
1516 /* Shall be called at least once per picture. */
1517 void h264_update_picture_info_for_slice
1519 h264_info_t *info,
1520 h264_picture_info_t *picture,
1521 h264_slice_info_t *slice
1524 assert( info );
1525 picture->has_mmco5 |= slice->has_mmco5;
1526 picture->has_redundancy |= slice->has_redundancy;
1527 picture->has_primary |= !slice->has_redundancy;
1528 h264_update_picture_type( picture, slice );
1529 /* Mark 'used' on active parameter sets. */
1530 uint8_t ps_id[2] = { slice->seq_parameter_set_id, slice->pic_parameter_set_id };
1531 for( int i = 0; i < 2; i++ )
1533 lsmash_h264_parameter_set_type ps_type = (lsmash_h264_parameter_set_type)i;
1534 lsmash_entry_t *entry = h264_get_ps_entry_from_param( &info->avcC_param, ps_type, ps_id[i] );
1535 if( entry && entry->data )
1537 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1538 if( ps->unused )
1539 lsmash_append_h264_parameter_set( &info->avcC_param, ps_type, ps->nalUnit, ps->nalUnitLength );
1542 /* Discard this slice info. */
1543 slice->present = 0;
1546 /* Shall be called exactly once per picture. */
1547 void h264_update_picture_info
1549 h264_info_t *info,
1550 h264_picture_info_t *picture,
1551 h264_slice_info_t *slice,
1552 h264_sei_t *sei
1555 picture->frame_num = slice->frame_num;
1556 picture->pic_order_cnt_lsb = slice->pic_order_cnt_lsb;
1557 picture->delta_pic_order_cnt_bottom = slice->delta_pic_order_cnt_bottom;
1558 picture->delta_pic_order_cnt[0] = slice->delta_pic_order_cnt[0];
1559 picture->delta_pic_order_cnt[1] = slice->delta_pic_order_cnt[1];
1560 picture->field_pic_flag = slice->field_pic_flag;
1561 picture->bottom_field_flag = slice->bottom_field_flag;
1562 picture->idr = slice->IdrPicFlag;
1563 picture->pic_parameter_set_id = slice->pic_parameter_set_id;
1564 picture->disposable = (slice->nal_ref_idc == 0);
1565 picture->random_accessible = slice->IdrPicFlag;
1566 h264_update_picture_info_for_slice( info, picture, slice );
1567 picture->independent = picture->type == H264_PICTURE_TYPE_I || picture->type == H264_PICTURE_TYPE_I_SI;
1568 if( sei->pic_timing.present )
1570 if( sei->pic_timing.pic_struct < 9 )
1572 static const uint8_t DeltaTfiDivisor[9] = { 2, 1, 1, 2, 2, 3, 3, 4, 6 };
1573 picture->delta = DeltaTfiDivisor[ sei->pic_timing.pic_struct ];
1575 else
1576 /* Reserved values in the spec we refer to. */
1577 picture->delta = picture->field_pic_flag ? 1 : 2;
1578 sei->pic_timing.present = 0;
1580 else
1581 picture->delta = picture->field_pic_flag ? 1 : 2;
1582 if( sei->recovery_point.present )
1584 picture->random_accessible |= sei->recovery_point.random_accessible;
1585 picture->broken_link_flag |= sei->recovery_point.broken_link_flag;
1586 picture->recovery_frame_cnt = sei->recovery_point.recovery_frame_cnt;
1587 sei->recovery_point.present = 0;
1591 int h264_find_au_delimit_by_slice_info
1593 h264_slice_info_t *slice,
1594 h264_slice_info_t *prev_slice
1597 if( slice->frame_num != prev_slice->frame_num
1598 || ((slice->pic_order_cnt_type == 0 && prev_slice->pic_order_cnt_type == 0)
1599 && (slice->pic_order_cnt_lsb != prev_slice->pic_order_cnt_lsb
1600 || slice->delta_pic_order_cnt_bottom != prev_slice->delta_pic_order_cnt_bottom))
1601 || ((slice->pic_order_cnt_type == 1 && prev_slice->pic_order_cnt_type == 1)
1602 && (slice->delta_pic_order_cnt[0] != prev_slice->delta_pic_order_cnt[0]
1603 || slice->delta_pic_order_cnt[1] != prev_slice->delta_pic_order_cnt[1]))
1604 || slice->field_pic_flag != prev_slice->field_pic_flag
1605 || slice->bottom_field_flag != prev_slice->bottom_field_flag
1606 || slice->IdrPicFlag != prev_slice->IdrPicFlag
1607 || slice->pic_parameter_set_id != prev_slice->pic_parameter_set_id
1608 || ((slice->nal_ref_idc == 0 || prev_slice->nal_ref_idc == 0)
1609 && (slice->nal_ref_idc != prev_slice->nal_ref_idc))
1610 || (slice->IdrPicFlag == 1 && prev_slice->IdrPicFlag == 1
1611 && slice->idr_pic_id != prev_slice->idr_pic_id) )
1612 return 1;
1613 return 0;
1616 int h264_find_au_delimit_by_nalu_type
1618 uint8_t nalu_type,
1619 uint8_t prev_nalu_type
1622 return ((nalu_type >= H264_NALU_TYPE_SEI && nalu_type <= H264_NALU_TYPE_AUD)
1623 || (nalu_type >= H264_NALU_TYPE_PREFIX && nalu_type <= H264_NALU_TYPE_RSV_NVCL18))
1624 && ((prev_nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && prev_nalu_type <= H264_NALU_TYPE_SLICE_IDR)
1625 || prev_nalu_type == H264_NALU_TYPE_FD || prev_nalu_type == H264_NALU_TYPE_SLICE_AUX);
1628 int h264_supplement_buffer
1630 h264_stream_buffer_t *sb,
1631 h264_access_unit_t *au,
1632 uint32_t size
1635 lsmash_multiple_buffers_t *bank = lsmash_resize_multiple_buffers( sb->bank, size );
1636 if( !bank )
1637 return LSMASH_ERR_MEMORY_ALLOC;
1638 sb->bank = bank;
1639 sb->rbsp = lsmash_withdraw_buffer( bank, 1 );
1640 if( au && bank->number_of_buffers == 3 )
1642 au->data = lsmash_withdraw_buffer( bank, 2 );
1643 au->incomplete_data = lsmash_withdraw_buffer( bank, 3 );
1645 return 0;
1648 static void h264_bs_put_parameter_sets
1650 lsmash_bs_t *bs,
1651 lsmash_entry_list_t *ps_list,
1652 uint32_t max_ps_count
1655 uint32_t ps_count = 0;
1656 for( lsmash_entry_t *entry = ps_list->head; entry && ps_count < max_ps_count; entry = entry->next )
1658 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1659 if( ps && !ps->unused )
1661 lsmash_bs_put_be16( bs, ps->nalUnitLength );
1662 lsmash_bs_put_bytes( bs, ps->nalUnitLength, ps->nalUnit );
1664 else
1665 continue;
1666 ++ps_count;
1670 uint8_t *lsmash_create_h264_specific_info
1672 lsmash_h264_specific_parameters_t *param,
1673 uint32_t *data_length
1676 if( !param || !param->parameter_sets || !data_length )
1677 return NULL;
1678 if( param->lengthSizeMinusOne != 0 && param->lengthSizeMinusOne != 1 && param->lengthSizeMinusOne != 3 )
1679 return NULL;
1680 static const uint32_t max_ps_count[3] = { 31, 255, 255 };
1681 lsmash_entry_list_t *ps_list[3] =
1683 param->parameter_sets->sps_list, /* SPS */
1684 param->parameter_sets->pps_list, /* PPS */
1685 param->parameter_sets->spsext_list /* SPSExt */
1687 uint32_t ps_count[3] = { 0, 0, 0 };
1688 /* SPS and PPS are mandatory. */
1689 if( !ps_list[0] || !ps_list[0]->head || ps_list[0]->entry_count == 0
1690 || !ps_list[1] || !ps_list[1]->head || ps_list[1]->entry_count == 0 )
1691 return NULL;
1692 for( int i = 0; i < 3; i++ )
1693 if( ps_list[i] )
1694 for( lsmash_entry_t *entry = ps_list[i]->head; entry && ps_count[i] < max_ps_count[i]; entry = entry->next )
1696 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1697 if( !ps )
1698 return NULL;
1699 if( ps->unused )
1700 continue;
1701 ++ps_count[i];
1703 /* Create an AVCConfigurationBox */
1704 lsmash_bs_t *bs = lsmash_bs_create();
1705 if( !bs )
1706 return NULL;
1707 lsmash_bs_put_be32( bs, 0 ); /* box size */
1708 lsmash_bs_put_be32( bs, ISOM_BOX_TYPE_AVCC.fourcc ); /* box type: 'avcC' */
1709 lsmash_bs_put_byte( bs, 1 ); /* configurationVersion */
1710 lsmash_bs_put_byte( bs, param->AVCProfileIndication ); /* AVCProfileIndication */
1711 lsmash_bs_put_byte( bs, param->profile_compatibility ); /* profile_compatibility */
1712 lsmash_bs_put_byte( bs, param->AVCLevelIndication ); /* AVCLevelIndication */
1713 lsmash_bs_put_byte( bs, param->lengthSizeMinusOne | 0xfc ); /* lengthSizeMinusOne */
1714 lsmash_bs_put_byte( bs, ps_count[0] | 0xe0 ); /* numOfSequenceParameterSets */
1715 h264_bs_put_parameter_sets( bs, ps_list[0], ps_count[0] ); /* sequenceParameterSetLength
1716 * sequenceParameterSetNALUnit */
1717 lsmash_bs_put_byte( bs, ps_count[1] ); /* numOfPictureParameterSets */
1718 h264_bs_put_parameter_sets( bs, ps_list[1], ps_count[1] ); /* pictureParameterSetLength
1719 * pictureParameterSetNALUnit */
1720 if( H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication ) )
1722 lsmash_bs_put_byte( bs, param->chroma_format | 0xfc ); /* chroma_format */
1723 lsmash_bs_put_byte( bs, param->bit_depth_luma_minus8 | 0xf8 ); /* bit_depth_luma_minus8 */
1724 lsmash_bs_put_byte( bs, param->bit_depth_chroma_minus8 | 0xf8 ); /* bit_depth_chroma_minus8 */
1725 if( ps_list[2] )
1727 lsmash_bs_put_byte( bs, ps_count[2] ); /* numOfSequenceParameterSetExt */
1728 h264_bs_put_parameter_sets( bs, ps_list[2], ps_count[2] ); /* sequenceParameterSetExtLength
1729 * sequenceParameterSetExtNALUnit */
1731 else /* no sequence parameter set extensions */
1732 lsmash_bs_put_byte( bs, 0 ); /* numOfSequenceParameterSetExt */
1734 uint8_t *data = lsmash_bs_export_data( bs, data_length );
1735 lsmash_bs_cleanup( bs );
1736 /* Update box size. */
1737 LSMASH_SET_BE32( data, *data_length );
1738 return data;
1741 static inline int h264_validate_ps_type
1743 lsmash_h264_parameter_set_type ps_type,
1744 void *ps_data,
1745 uint32_t ps_length
1748 if( !ps_data || ps_length < 2 )
1749 return LSMASH_ERR_INVALID_DATA;
1750 if( ps_type != H264_PARAMETER_SET_TYPE_SPS
1751 && ps_type != H264_PARAMETER_SET_TYPE_PPS
1752 && ps_type != H264_PARAMETER_SET_TYPE_SPSEXT )
1753 return LSMASH_ERR_INVALID_DATA;
1754 uint8_t nalu_type = *((uint8_t *)ps_data) & 0x1f;
1755 if( nalu_type != H264_NALU_TYPE_SPS
1756 && nalu_type != H264_NALU_TYPE_PPS
1757 && nalu_type != H264_NALU_TYPE_SPS_EXT )
1758 return LSMASH_ERR_INVALID_DATA;
1759 if( (ps_type == H264_PARAMETER_SET_TYPE_SPS && nalu_type != H264_NALU_TYPE_SPS)
1760 || (ps_type == H264_PARAMETER_SET_TYPE_PPS && nalu_type != H264_NALU_TYPE_PPS)
1761 || (ps_type == H264_PARAMETER_SET_TYPE_SPSEXT && nalu_type != H264_NALU_TYPE_SPS_EXT) )
1762 return LSMASH_ERR_INVALID_DATA;
1763 return 0;
1766 static lsmash_dcr_nalu_appendable h264_check_sps_appendable
1768 lsmash_bits_t *bits,
1769 uint8_t *rbsp_buffer,
1770 lsmash_h264_specific_parameters_t *param,
1771 uint8_t *ps_data,
1772 uint32_t ps_length,
1773 lsmash_entry_list_t *ps_list
1776 h264_sps_t sps;
1777 if( h264_parse_sps_minimally( bits, &sps, rbsp_buffer, ps_data + 1, ps_length - 1 ) < 0 )
1778 return DCR_NALU_APPEND_ERROR;
1779 lsmash_bits_empty( bits );
1780 /* FIXME; If the sequence parameter sets are marked with different profiles,
1781 * and the relevant profile compatibility flags are all zero,
1782 * then the stream may need examination to determine which profile, if any, the stream conforms to.
1783 * If the stream is not examined, or the examination reveals that there is no profile to which the stream conforms,
1784 * then the stream must be split into two or more sub-streams with separate configuration records in which these rules can be met. */
1785 #if 0
1786 if( sps.profile_idc != param->AVCProfileIndication && (sps->constraint_set_flags & param->profile_compatibility) )
1787 #else
1788 if( sps.profile_idc != param->AVCProfileIndication )
1789 #endif
1790 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
1791 /* The values of chroma_format_idc, bit_depth_luma_minus8 and bit_depth_chroma_minus8
1792 * must be identical in all SPSs in a single AVC configuration record. */
1793 if( H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication )
1794 && (sps.chroma_format_idc != param->chroma_format
1795 || sps.bit_depth_luma_minus8 != param->bit_depth_luma_minus8
1796 || sps.bit_depth_chroma_minus8 != param->bit_depth_chroma_minus8) )
1797 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
1798 /* Forbidden to duplicate SPS that has the same seq_parameter_set_id with different form within the same configuration record. */
1799 uint8_t sps_id = sps.seq_parameter_set_id;
1800 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
1802 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1803 if( !ps )
1804 return DCR_NALU_APPEND_ERROR;
1805 if( ps->unused )
1806 continue;
1807 uint8_t param_sps_id;
1808 if( h264_get_sps_id( ps->nalUnit + 1, ps->nalUnitLength - 1, &param_sps_id ) < 0 )
1809 return DCR_NALU_APPEND_ERROR;
1810 if( sps_id == param_sps_id )
1811 /* SPS that has the same seq_parameter_set_id already exists with different form. */
1812 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
1813 if( entry == ps_list->head )
1815 /* Check if the visual presentation sizes are different. */
1816 h264_sps_t first_sps;
1817 if( h264_parse_sps_minimally( bits, &first_sps, rbsp_buffer,
1818 ps->nalUnit + 1,
1819 ps->nalUnitLength - 1 ) < 0 )
1820 return DCR_NALU_APPEND_ERROR;
1821 if( sps.cropped_width != first_sps.cropped_width
1822 || sps.cropped_height != first_sps.cropped_height )
1823 return DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED;
1826 return DCR_NALU_APPEND_POSSIBLE;
1829 static lsmash_dcr_nalu_appendable h264_check_pps_appendable
1831 uint8_t *ps_data,
1832 uint32_t ps_length,
1833 lsmash_entry_list_t *ps_list
1836 uint8_t pps_id;
1837 if( h264_get_pps_id( ps_data + 1, ps_length - 1, &pps_id ) < 0 )
1838 return DCR_NALU_APPEND_ERROR;
1839 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
1841 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1842 if( !ps )
1843 return DCR_NALU_APPEND_ERROR;
1844 if( ps->unused )
1845 continue;
1846 uint8_t param_pps_id;
1847 if( h264_get_pps_id( ps->nalUnit + 1, ps->nalUnitLength - 1, &param_pps_id ) < 0 )
1848 return DCR_NALU_APPEND_ERROR;
1849 if( pps_id == param_pps_id )
1850 /* PPS that has the same pic_parameter_set_id already exists with different form. */
1851 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
1853 return DCR_NALU_APPEND_POSSIBLE;
1856 lsmash_dcr_nalu_appendable lsmash_check_h264_parameter_set_appendable
1858 lsmash_h264_specific_parameters_t *param,
1859 lsmash_h264_parameter_set_type ps_type,
1860 void *_ps_data,
1861 uint32_t ps_length
1864 uint8_t *ps_data = _ps_data;
1865 if( !param )
1866 return DCR_NALU_APPEND_ERROR;
1867 if( h264_validate_ps_type( ps_type, ps_data, ps_length ) < 0 )
1868 return DCR_NALU_APPEND_ERROR;
1869 if( ps_type == H264_PARAMETER_SET_TYPE_SPSEXT
1870 && !H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication ) )
1871 return DCR_NALU_APPEND_ERROR;
1872 /* Check whether the same parameter set already exsits or not. */
1873 lsmash_entry_list_t *ps_list = h264_get_parameter_set_list( param, ps_type );
1874 if( !ps_list || !ps_list->head )
1875 return DCR_NALU_APPEND_POSSIBLE; /* No parameter set */
1876 switch( nalu_check_same_ps_existence( ps_list, ps_data, ps_length ) )
1878 case 0 : break;
1879 case 1 : return DCR_NALU_APPEND_DUPLICATED; /* The same parameter set already exists. */
1880 default : return DCR_NALU_APPEND_ERROR; /* An error occured. */
1882 uint32_t ps_count;
1883 if( nalu_get_ps_count( ps_list, &ps_count ) )
1884 return DCR_NALU_APPEND_ERROR;
1885 if( (ps_type == H264_PARAMETER_SET_TYPE_SPS && ps_count >= 31)
1886 || (ps_type == H264_PARAMETER_SET_TYPE_PPS && ps_count >= 255)
1887 || (ps_type == H264_PARAMETER_SET_TYPE_SPSEXT && ps_count >= 255) )
1888 return DCR_NALU_APPEND_NEW_DCR_REQUIRED; /* No more appendable parameter sets. */
1889 if( ps_type == H264_PARAMETER_SET_TYPE_SPSEXT )
1890 return DCR_NALU_APPEND_POSSIBLE;
1891 /* Check whether a new specific info is needed or not. */
1892 if( ps_type == H264_PARAMETER_SET_TYPE_PPS )
1893 /* PPS */
1894 return h264_check_pps_appendable( ps_data, ps_length, ps_list );
1895 else
1897 /* SPS
1898 * Set up bitstream handler for parse parameter sets. */
1899 lsmash_bits_t *bits = lsmash_bits_adhoc_create();
1900 if( !bits )
1901 return DCR_NALU_APPEND_ERROR;
1902 uint32_t max_ps_length;
1903 uint8_t *rbsp_buffer;
1904 if( nalu_get_max_ps_length( ps_list, &max_ps_length ) < 0
1905 || (rbsp_buffer = lsmash_malloc( LSMASH_MAX( max_ps_length, ps_length ) )) == NULL )
1907 lsmash_bits_adhoc_cleanup( bits );
1908 return DCR_NALU_APPEND_ERROR;
1910 lsmash_dcr_nalu_appendable appendable = h264_check_sps_appendable( bits, rbsp_buffer, param, ps_data, ps_length, ps_list );
1911 lsmash_bits_adhoc_cleanup( bits );
1912 lsmash_free( rbsp_buffer );
1913 return appendable;
1917 static inline void h264_reorder_parameter_set_ascending_id
1919 lsmash_h264_specific_parameters_t *param,
1920 lsmash_h264_parameter_set_type ps_type,
1921 lsmash_entry_list_t *ps_list,
1922 uint8_t ps_id
1925 lsmash_entry_t *entry = NULL;
1926 if( ps_id )
1927 for( int i = ps_id - 1; i; i-- )
1929 entry = h264_get_ps_entry_from_param( param, ps_type, i );
1930 if( entry )
1931 break;
1933 int append_head = 0;
1934 if( !entry )
1936 /* Couldn't find any parameter set with lower identifier.
1937 * Next, find parameter set with upper identifier. */
1938 int max_ps_id = ps_type == H264_PARAMETER_SET_TYPE_SPS ? 31 : 255;
1939 for( int i = ps_id + 1; i <= max_ps_id; i++ )
1941 entry = h264_get_ps_entry_from_param( param, ps_type, i );
1942 if( entry )
1943 break;
1945 if( entry )
1946 append_head = 1;
1948 if( !entry )
1949 return; /* The new entry was appended to the tail. */
1950 lsmash_entry_t *new_entry = ps_list->tail;
1951 if( append_head )
1953 /* before: entry[i > ps_id] ... -> prev_entry -> new_entry[ps_id]
1954 * after: new_entry[ps_id] -> entry[i > ps_id] -> ... -> prev_entry */
1955 if( new_entry->prev )
1956 new_entry->prev->next = NULL;
1957 new_entry->prev = NULL;
1958 entry->prev = new_entry;
1959 new_entry->next = entry;
1960 return;
1962 /* before: entry[i < ps_id] -> next_entry -> ... -> prev_entry -> new_entry[ps_id]
1963 * after: entry[i < ps_id] -> new_entry[ps_id] -> next_entry -> ... -> prev_entry */
1964 if( new_entry->prev )
1965 new_entry->prev->next = NULL;
1966 new_entry->prev = entry;
1967 new_entry->next = entry->next;
1968 if( entry->next )
1969 entry->next->prev = new_entry;
1970 entry->next = new_entry;
1973 int lsmash_append_h264_parameter_set
1975 lsmash_h264_specific_parameters_t *param,
1976 lsmash_h264_parameter_set_type ps_type,
1977 void *_ps_data,
1978 uint32_t ps_length
1981 uint8_t *ps_data = _ps_data;
1982 if( !param || !ps_data || ps_length < 2 )
1983 return LSMASH_ERR_FUNCTION_PARAM;
1984 if( ps_type != H264_PARAMETER_SET_TYPE_SPS
1985 && ps_type != H264_PARAMETER_SET_TYPE_PPS
1986 && ps_type != H264_PARAMETER_SET_TYPE_SPSEXT )
1987 return LSMASH_ERR_FUNCTION_PARAM;
1988 if( !param->parameter_sets )
1990 param->parameter_sets = lsmash_malloc_zero( sizeof(lsmash_h264_parameter_sets_t) );
1991 if( !param->parameter_sets )
1992 return LSMASH_ERR_MEMORY_ALLOC;
1994 lsmash_entry_list_t *ps_list = h264_get_parameter_set_list( param, ps_type );
1995 if( !ps_list )
1996 return LSMASH_ERR_NAMELESS;
1997 if( ps_type == H264_PARAMETER_SET_TYPE_SPSEXT )
1999 if( !H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication ) )
2000 return 0;
2001 isom_dcr_ps_entry_t *ps = isom_create_ps_entry( ps_data, ps_length );
2002 if( !ps )
2003 return LSMASH_ERR_MEMORY_ALLOC;
2004 if( lsmash_add_entry( ps_list, ps ) < 0 )
2006 isom_remove_dcr_ps( ps );
2007 return LSMASH_ERR_MEMORY_ALLOC;
2009 return 0;
2011 /* Check if the same parameter set identifier already exists. */
2012 uint8_t ps_id;
2013 int err = h264_get_ps_id( ps_data + 1, ps_length - 1, &ps_id, ps_type );
2014 if( err < 0 )
2015 return err;
2016 lsmash_entry_t *entry = h264_get_ps_entry_from_param( param, ps_type, ps_id );
2017 isom_dcr_ps_entry_t *ps = entry ? (isom_dcr_ps_entry_t *)entry->data : NULL;
2018 if( ps && !ps->unused )
2019 /* The same parameter set identifier already exists. */
2020 return LSMASH_ERR_FUNCTION_PARAM;
2021 int invoke_reorder;
2022 if( ps )
2024 /* Reuse an already existed parameter set in the list. */
2025 ps->unused = 0;
2026 if( ps->nalUnit != ps_data )
2028 /* The same address could be given when called by h264_update_picture_info_for_slice(). */
2029 lsmash_free( ps->nalUnit );
2030 ps->nalUnit = ps_data;
2032 ps->nalUnitLength = ps_length;
2033 invoke_reorder = 0;
2035 else
2037 /* Create a new parameter set and append it into the list. */
2038 ps = isom_create_ps_entry( ps_data, ps_length );
2039 if( !ps )
2040 return LSMASH_ERR_MEMORY_ALLOC;
2041 if( lsmash_add_entry( ps_list, ps ) < 0 )
2043 isom_remove_dcr_ps( ps );
2044 return LSMASH_ERR_MEMORY_ALLOC;
2046 invoke_reorder = 1;
2048 if( ps_type == H264_PARAMETER_SET_TYPE_SPS )
2050 /* Update specific info with SPS. */
2051 lsmash_bits_t *bits = lsmash_bits_adhoc_create();
2052 if( !bits )
2053 return LSMASH_ERR_MEMORY_ALLOC;
2054 uint8_t *rbsp_buffer = lsmash_malloc( ps_length );
2055 if( !rbsp_buffer )
2057 lsmash_bits_adhoc_cleanup( bits );
2058 return LSMASH_ERR_MEMORY_ALLOC;
2060 h264_sps_t sps;
2061 err = h264_parse_sps_minimally( bits, &sps, rbsp_buffer, ps_data + 1, ps_length - 1 );
2062 lsmash_bits_adhoc_cleanup( bits );
2063 lsmash_free( rbsp_buffer );
2064 if( err < 0 )
2066 lsmash_remove_entry_tail( ps_list, isom_remove_dcr_ps );
2067 return err;
2069 if( ps_list->entry_count == 1 )
2070 param->profile_compatibility = 0xff;
2071 param->AVCProfileIndication = sps.profile_idc;
2072 param->profile_compatibility &= sps.constraint_set_flags;
2073 param->AVCLevelIndication = LSMASH_MAX( param->AVCLevelIndication, sps.level_idc );
2074 param->chroma_format = sps.chroma_format_idc;
2075 param->bit_depth_luma_minus8 = sps.bit_depth_luma_minus8;
2076 param->bit_depth_chroma_minus8 = sps.bit_depth_chroma_minus8;
2078 if( invoke_reorder )
2079 /* Add a new parameter set in order of ascending parameter set identifier. */
2080 h264_reorder_parameter_set_ascending_id( param, ps_type, ps_list, ps_id );
2081 return 0;
2084 int h264_try_to_append_parameter_set
2086 h264_info_t *info,
2087 lsmash_h264_parameter_set_type ps_type,
2088 void *_ps_data,
2089 uint32_t ps_length
2092 uint8_t *ps_data = _ps_data;
2093 lsmash_dcr_nalu_appendable ret = lsmash_check_h264_parameter_set_appendable( &info->avcC_param, ps_type, ps_data, ps_length );
2094 lsmash_h264_specific_parameters_t *param;
2095 switch( ret )
2097 case DCR_NALU_APPEND_ERROR : /* Error */
2098 return LSMASH_ERR_NAMELESS;
2099 case DCR_NALU_APPEND_NEW_DCR_REQUIRED : /* Mulitiple sample description is needed. */
2100 case DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED : /* Mulitiple sample description is needed. */
2101 param = &info->avcC_param_next;
2102 info->avcC_pending = 1;
2103 break;
2104 case DCR_NALU_APPEND_POSSIBLE : /* Appendable */
2105 param = info->avcC_pending ? &info->avcC_param_next : &info->avcC_param;
2106 break;
2107 default : /* No need to append */
2108 return 0;
2110 int err;
2111 switch( ps_type )
2113 case H264_PARAMETER_SET_TYPE_SPS :
2114 if( (err = h264_parse_sps( info, info->buffer.rbsp, ps_data + 1, ps_length - 1 )) < 0 )
2115 return err;
2116 break;
2117 case H264_PARAMETER_SET_TYPE_PPS :
2118 if( (err = h264_parse_pps( info, info->buffer.rbsp, ps_data + 1, ps_length - 1 )) < 0 )
2119 return err;
2120 break;
2121 default :
2122 break;
2124 return lsmash_append_h264_parameter_set( param, ps_type, ps_data, ps_length );
2127 static inline int h264_move_dcr_nalu_entry
2129 lsmash_h264_specific_parameters_t *dst_data,
2130 lsmash_h264_specific_parameters_t *src_data,
2131 lsmash_h264_parameter_set_type ps_type
2134 lsmash_entry_list_t *src_ps_list = h264_get_parameter_set_list( src_data, ps_type );
2135 lsmash_entry_list_t *dst_ps_list = h264_get_parameter_set_list( dst_data, ps_type );
2136 assert( src_ps_list && dst_ps_list );
2137 for( lsmash_entry_t *src_entry = src_ps_list->head; src_entry; src_entry = src_entry->next )
2139 isom_dcr_ps_entry_t *src_ps = (isom_dcr_ps_entry_t *)src_entry->data;
2140 if( !src_ps )
2141 continue;
2142 int err;
2143 uint8_t src_ps_id;
2144 if( (err = h264_get_ps_id( src_ps->nalUnit + 1, src_ps->nalUnitLength - 1, &src_ps_id, ps_type )) < 0 )
2145 return err;
2146 lsmash_entry_t *dst_entry;
2147 for( dst_entry = dst_ps_list->head; dst_entry; dst_entry = dst_entry->next )
2149 isom_dcr_ps_entry_t *dst_ps = (isom_dcr_ps_entry_t *)dst_entry->data;
2150 if( !dst_ps )
2151 continue;
2152 uint8_t dst_ps_id;
2153 if( (err = h264_get_ps_id( dst_ps->nalUnit + 1, dst_ps->nalUnitLength - 1, &dst_ps_id, ps_type )) < 0 )
2154 return err;
2155 if( dst_ps_id == src_ps_id )
2157 /* Replace the old parameter set with the new one. */
2158 assert( dst_entry->data != src_entry->data );
2159 isom_remove_dcr_ps( dst_ps );
2160 dst_entry->data = src_entry->data;
2161 src_entry->data = NULL;
2162 break;
2165 if( !dst_entry )
2167 /* Move the parameter set. */
2168 if( lsmash_add_entry( dst_ps_list, src_ps ) < 0 )
2169 return LSMASH_ERR_MEMORY_ALLOC;
2170 src_entry->data = NULL;
2173 return 0;
2176 int h264_move_pending_avcC_param
2178 h264_info_t *info
2181 assert( info );
2182 if( !info->avcC_pending )
2183 return 0;
2184 /* Mark 'unused' on parameter sets within the decoder configuration record. */
2185 for( int i = 0; i < H264_PARAMETER_SET_TYPE_NUM; i++ )
2187 lsmash_entry_list_t *ps_list = h264_get_parameter_set_list( &info->avcC_param, i );
2188 assert( ps_list );
2189 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
2191 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
2192 if( !ps )
2193 continue;
2194 ps->unused = 1;
2197 /* Move the new parameter sets. */
2198 int err;
2199 if( (err = h264_move_dcr_nalu_entry( &info->avcC_param, &info->avcC_param_next, H264_PARAMETER_SET_TYPE_SPS )) < 0
2200 || (err = h264_move_dcr_nalu_entry( &info->avcC_param, &info->avcC_param_next, H264_PARAMETER_SET_TYPE_PPS )) < 0 )
2201 return err;
2202 /* Move to the pending. */
2203 lsmash_h264_parameter_sets_t *parameter_sets = info->avcC_param.parameter_sets; /* Back up parameter sets. */
2204 info->avcC_param = info->avcC_param_next;
2205 info->avcC_param.parameter_sets = parameter_sets;
2206 /* No pending avcC. */
2207 lsmash_destroy_h264_parameter_sets( &info->avcC_param_next );
2208 uint8_t lengthSizeMinusOne = info->avcC_param_next.lengthSizeMinusOne;
2209 memset( &info->avcC_param_next, 0, sizeof(lsmash_h264_specific_parameters_t) );
2210 info->avcC_param_next.lengthSizeMinusOne = lengthSizeMinusOne;
2211 info->avcC_pending = 0;
2212 return 0;
2215 static int h264_parse_succeeded
2217 h264_info_t *info,
2218 lsmash_h264_specific_parameters_t *param
2221 int ret;
2222 if( info->sps.present && info->pps.present )
2224 *param = info->avcC_param;
2225 /* Avoid freeing parameter sets. */
2226 info->avcC_param.parameter_sets = NULL;
2227 ret = 0;
2229 else
2230 ret = LSMASH_ERR_INVALID_DATA;
2231 h264_cleanup_parser( info );
2232 return ret;
2235 static inline int h264_parse_failed
2237 h264_info_t *info,
2238 int ret
2241 h264_cleanup_parser( info );
2242 return ret;
2245 int lsmash_setup_h264_specific_parameters_from_access_unit
2247 lsmash_h264_specific_parameters_t *param,
2248 uint8_t *data,
2249 uint32_t data_length
2252 if( !param || !data || data_length == 0 )
2253 return LSMASH_ERR_FUNCTION_PARAM;
2254 h264_info_t *info = &(h264_info_t){ { 0 } };
2255 lsmash_bs_t *bs = &(lsmash_bs_t){ 0 };
2256 int err = lsmash_bs_set_empty_stream( bs, data, data_length );
2257 if( err < 0 )
2258 return err;
2259 uint64_t sc_head_pos = nalu_find_first_start_code( bs );
2260 if( sc_head_pos == NALU_NO_START_CODE_FOUND )
2261 return LSMASH_ERR_INVALID_DATA;
2262 else if( sc_head_pos == NALU_IO_ERROR )
2263 return LSMASH_ERR_IO;
2264 if( (err = h264_setup_parser( info, 1 )) < 0 )
2265 return h264_parse_failed( info, err );
2266 h264_stream_buffer_t *sb = &info->buffer;
2267 h264_slice_info_t *slice = &info->slice;
2268 while( 1 )
2270 h264_nalu_header_t nuh;
2271 uint64_t start_code_length;
2272 uint64_t trailing_zero_bytes;
2273 uint64_t nalu_length = h264_find_next_start_code( bs, &nuh, &start_code_length, &trailing_zero_bytes );
2274 if( nalu_length == NALU_NO_START_CODE_FOUND )
2275 /* For the last NALU. This NALU already has been parsed. */
2276 return h264_parse_succeeded( info, param );
2277 uint8_t nalu_type = nuh.nal_unit_type;
2278 uint64_t next_sc_head_pos = sc_head_pos
2279 + start_code_length
2280 + nalu_length
2281 + trailing_zero_bytes;
2282 if( nalu_type == H264_NALU_TYPE_FD )
2284 /* We don't support streams with both filler and HRD yet.
2285 * Otherwise, just skip filler because elemental streams defined in 14496-15 are forbidden to use filler. */
2286 if( info->sps.vui.hrd.present )
2287 return h264_parse_failed( info, LSMASH_ERR_PATCH_WELCOME );
2289 else if( (nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && nalu_type <= H264_NALU_TYPE_SPS_EXT)
2290 || nalu_type == H264_NALU_TYPE_SLICE_AUX )
2292 /* Increase the buffer if needed. */
2293 uint64_t possible_au_length = NALU_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
2294 if( sb->bank->buffer_size < possible_au_length
2295 && (err = h264_supplement_buffer( sb, NULL, 2 * possible_au_length )) < 0 )
2296 return h264_parse_failed( info, err );
2297 /* Get the EBSP of the current NALU here.
2298 * AVC elemental stream defined in 14496-15 can recognize from 0 to 13, and 19 of nal_unit_type.
2299 * We don't support SVC and MVC elemental stream defined in 14496-15 yet. */
2300 uint8_t *nalu = lsmash_bs_get_buffer_data( bs ) + start_code_length;
2301 if( nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && nalu_type <= H264_NALU_TYPE_SLICE_IDR )
2303 /* VCL NALU (slice) */
2304 h264_slice_info_t prev_slice = *slice;
2305 if( (err = h264_parse_slice( info, &nuh, sb->rbsp, nalu + nuh.length, nalu_length - nuh.length )) < 0 )
2306 return h264_parse_failed( info, err );
2307 if( prev_slice.present )
2309 /* Check whether the AU that contains the previous VCL NALU completed or not. */
2310 if( h264_find_au_delimit_by_slice_info( slice, &prev_slice ) )
2311 /* The current NALU is the first VCL NALU of the primary coded picture of an new AU.
2312 * Therefore, the previous slice belongs to that new AU. */
2313 return h264_parse_succeeded( info, param );
2315 slice->present = 1;
2317 else
2319 if( h264_find_au_delimit_by_nalu_type( nalu_type, info->prev_nalu_type ) )
2320 /* The last slice belongs to the AU you want at this time. */
2321 return h264_parse_succeeded( info, param );
2322 switch( nalu_type )
2324 case H264_NALU_TYPE_SPS :
2325 if( (err = h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_SPS, nalu, nalu_length )) < 0 )
2326 return h264_parse_failed( info, err );
2327 break;
2328 case H264_NALU_TYPE_PPS :
2329 if( (err = h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_PPS, nalu, nalu_length )) < 0 )
2330 return h264_parse_failed( info, err );
2331 break;
2332 case H264_NALU_TYPE_SPS_EXT :
2333 if( (err = h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_SPSEXT, nalu, nalu_length )) < 0 )
2334 return h264_parse_failed( info, err );
2335 break;
2336 default :
2337 break;
2341 /* Move to the first byte of the next start code. */
2342 info->prev_nalu_type = nalu_type;
2343 if( lsmash_bs_read_seek( bs, next_sc_head_pos, SEEK_SET ) != next_sc_head_pos )
2344 return h264_parse_failed( info, LSMASH_ERR_NAMELESS );
2345 /* Check if no more data to read from the stream. */
2346 if( !lsmash_bs_is_end( bs, NALU_SHORT_START_CODE_LENGTH ) )
2347 sc_head_pos = next_sc_head_pos;
2348 else
2349 return h264_parse_succeeded( info, param );
2353 int h264_construct_specific_parameters
2355 lsmash_codec_specific_t *dst,
2356 lsmash_codec_specific_t *src
2359 assert( dst && dst->data.structured && src && src->data.unstructured );
2360 if( src->size < ISOM_BASEBOX_COMMON_SIZE + 7 )
2361 return LSMASH_ERR_INVALID_DATA;
2362 lsmash_h264_specific_parameters_t *param = (lsmash_h264_specific_parameters_t *)dst->data.structured;
2363 uint8_t *data = src->data.unstructured;
2364 uint64_t size = LSMASH_GET_BE32( data );
2365 data += ISOM_BASEBOX_COMMON_SIZE;
2366 if( size == 1 )
2368 size = LSMASH_GET_BE64( data );
2369 data += 8;
2371 if( size != src->size )
2372 return LSMASH_ERR_INVALID_DATA;
2373 if( !param->parameter_sets )
2375 param->parameter_sets = lsmash_malloc_zero( sizeof(lsmash_h264_parameter_sets_t) );
2376 if( !param->parameter_sets )
2377 return LSMASH_ERR_MEMORY_ALLOC;
2379 lsmash_bs_t *bs = lsmash_bs_create();
2380 if( !bs )
2381 return LSMASH_ERR_MEMORY_ALLOC;
2382 int err = lsmash_bs_import_data( bs, data, src->size - (data - src->data.unstructured) );
2383 if( err < 0 )
2384 goto fail;
2385 if( lsmash_bs_get_byte( bs ) != 1 )
2387 /* We don't support configurationVersion other than 1. */
2388 err = LSMASH_ERR_INVALID_DATA;
2389 goto fail;
2391 param->AVCProfileIndication = lsmash_bs_get_byte( bs );
2392 param->profile_compatibility = lsmash_bs_get_byte( bs );
2393 param->AVCLevelIndication = lsmash_bs_get_byte( bs );
2394 param->lengthSizeMinusOne = lsmash_bs_get_byte( bs ) & 0x03;
2395 uint8_t numOfSequenceParameterSets = lsmash_bs_get_byte( bs ) & 0x1F;
2396 if( numOfSequenceParameterSets
2397 && (err = nalu_get_dcr_ps( bs, param->parameter_sets->sps_list, numOfSequenceParameterSets )) < 0 )
2398 goto fail;
2399 uint8_t numOfPictureParameterSets = lsmash_bs_get_byte( bs );
2400 if( numOfPictureParameterSets
2401 && (err = nalu_get_dcr_ps( bs, param->parameter_sets->pps_list, numOfPictureParameterSets )) < 0 )
2402 goto fail;
2403 if( H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication ) )
2405 param->chroma_format = lsmash_bs_get_byte( bs ) & 0x03;
2406 param->bit_depth_luma_minus8 = lsmash_bs_get_byte( bs ) & 0x07;
2407 param->bit_depth_chroma_minus8 = lsmash_bs_get_byte( bs ) & 0x07;
2408 uint8_t numOfSequenceParameterSetExt = lsmash_bs_get_byte( bs );
2409 if( numOfSequenceParameterSetExt
2410 && (err = nalu_get_dcr_ps( bs, param->parameter_sets->spsext_list, numOfSequenceParameterSetExt )) < 0 )
2411 goto fail;
2413 lsmash_bs_cleanup( bs );
2414 return 0;
2415 fail:
2416 lsmash_bs_cleanup( bs );
2417 return err;
2420 int h264_print_codec_specific
2422 FILE *fp,
2423 lsmash_file_t *file,
2424 isom_box_t *box,
2425 int level
2428 assert( fp && file && box && (box->manager & LSMASH_BINARY_CODED_BOX) );
2429 int indent = level;
2430 lsmash_ifprintf( fp, indent++, "[%s: AVC Configuration Box]\n", isom_4cc2str( box->type.fourcc ) );
2431 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
2432 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
2433 uint8_t *data = box->binary;
2434 uint32_t offset = isom_skip_box_common( &data );
2435 lsmash_bs_t *bs = lsmash_bs_create();
2436 if( !bs )
2437 return LSMASH_ERR_MEMORY_ALLOC;
2438 int err = lsmash_bs_import_data( bs, data, box->size - offset );
2439 if( err < 0 )
2441 lsmash_bs_cleanup( bs );
2442 return err;
2444 lsmash_ifprintf( fp, indent, "configurationVersion = %"PRIu8"\n", lsmash_bs_get_byte( bs ) );
2445 uint8_t AVCProfileIndication = lsmash_bs_get_byte( bs );
2446 lsmash_ifprintf( fp, indent, "AVCProfileIndication = %"PRIu8"\n", AVCProfileIndication );
2447 lsmash_ifprintf( fp, indent, "profile_compatibility = 0x%02"PRIx8"\n", lsmash_bs_get_byte( bs ) );
2448 lsmash_ifprintf( fp, indent, "AVCLevelIndication = %"PRIu8"\n", lsmash_bs_get_byte( bs ) );
2449 uint8_t temp8 = lsmash_bs_get_byte( bs );
2450 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 2) & 0x3F );
2451 lsmash_ifprintf( fp, indent, "lengthSizeMinusOne = %"PRIu8"\n", temp8 & 0x03 );
2452 temp8 = lsmash_bs_get_byte( bs );
2453 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 5) & 0x07 );
2454 uint8_t numOfSequenceParameterSets = temp8 & 0x1f;
2455 lsmash_ifprintf( fp, indent, "numOfSequenceParameterSets = %"PRIu8"\n", numOfSequenceParameterSets );
2456 for( uint8_t i = 0; i < numOfSequenceParameterSets; i++ )
2458 uint16_t nalUnitLength = lsmash_bs_get_be16( bs );
2459 lsmash_bs_skip_bytes( bs, nalUnitLength );
2461 uint8_t numOfPictureParameterSets = lsmash_bs_get_byte( bs );
2462 lsmash_ifprintf( fp, indent, "numOfPictureParameterSets = %"PRIu8"\n", numOfPictureParameterSets );
2463 for( uint8_t i = 0; i < numOfPictureParameterSets; i++ )
2465 uint16_t nalUnitLength = lsmash_bs_get_be16( bs );
2466 lsmash_bs_skip_bytes( bs, nalUnitLength );
2468 /* Note: there are too many files, in the world, that don't contain the following fields. */
2469 if( H264_REQUIRES_AVCC_EXTENSION( AVCProfileIndication )
2470 && (lsmash_bs_get_pos( bs ) < (box->size - offset)) )
2472 temp8 = lsmash_bs_get_byte( bs );
2473 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 2) & 0x3F );
2474 lsmash_ifprintf( fp, indent, "chroma_format = %"PRIu8"\n", temp8 & 0x03 );
2475 temp8 = lsmash_bs_get_byte( bs );
2476 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 3) & 0x1F );
2477 lsmash_ifprintf( fp, indent, "bit_depth_luma_minus8 = %"PRIu8"\n", temp8 & 0x7 );
2478 temp8 = lsmash_bs_get_byte( bs );
2479 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 3) & 0x1F );
2480 lsmash_ifprintf( fp, indent, "bit_depth_chroma_minus8 = %"PRIu8"\n", temp8 & 0x7 );
2481 lsmash_ifprintf( fp, indent, "numOfSequenceParameterSetExt = %"PRIu8"\n", lsmash_bs_get_byte( bs ) );
2483 lsmash_bs_cleanup( bs );
2484 return 0;
2487 int h264_copy_codec_specific
2489 lsmash_codec_specific_t *dst,
2490 lsmash_codec_specific_t *src
2493 assert( src && src->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && src->data.structured );
2494 assert( dst && dst->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && dst->data.structured );
2495 lsmash_h264_specific_parameters_t *src_data = (lsmash_h264_specific_parameters_t *)src->data.structured;
2496 lsmash_h264_specific_parameters_t *dst_data = (lsmash_h264_specific_parameters_t *)dst->data.structured;
2497 lsmash_destroy_h264_parameter_sets( dst_data );
2498 *dst_data = *src_data;
2499 if( !src_data->parameter_sets )
2500 return 0;
2501 dst_data->parameter_sets = lsmash_malloc_zero( sizeof(lsmash_h264_parameter_sets_t) );
2502 if( !dst_data->parameter_sets )
2503 return LSMASH_ERR_MEMORY_ALLOC;
2504 for( int i = 0; i < 3; i++ )
2506 lsmash_entry_list_t *src_ps_list = h264_get_parameter_set_list( src_data, i );
2507 lsmash_entry_list_t *dst_ps_list = h264_get_parameter_set_list( dst_data, i );
2508 assert( src_ps_list && dst_ps_list );
2509 for( lsmash_entry_t *entry = src_ps_list->head; entry; entry = entry->next )
2511 isom_dcr_ps_entry_t *src_ps = (isom_dcr_ps_entry_t *)entry->data;
2512 if( !src_ps || src_ps->unused )
2513 continue;
2514 isom_dcr_ps_entry_t *dst_ps = isom_create_ps_entry( src_ps->nalUnit, src_ps->nalUnitLength );
2515 if( !dst_ps )
2517 lsmash_destroy_h264_parameter_sets( dst_data );
2518 return LSMASH_ERR_MEMORY_ALLOC;
2520 if( lsmash_add_entry( dst_ps_list, dst_ps ) < 0 )
2522 lsmash_destroy_h264_parameter_sets( dst_data );
2523 isom_remove_dcr_ps( dst_ps );
2524 return LSMASH_ERR_MEMORY_ALLOC;
2528 return 0;
2531 int h264_print_bitrate
2533 FILE *fp,
2534 lsmash_file_t *file,
2535 isom_box_t *box,
2536 int level
2539 assert( fp && file && box );
2540 int indent = level;
2541 lsmash_ifprintf( fp, indent++, "[%s: MPEG-4 Bit Rate Box]\n", isom_4cc2str( box->type.fourcc ) );
2542 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
2543 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
2544 isom_btrt_t *btrt = (isom_btrt_t *)box;
2545 lsmash_ifprintf( fp, indent, "bufferSizeDB = %"PRIu32"\n", btrt->bufferSizeDB );
2546 lsmash_ifprintf( fp, indent, "maxBitrate = %"PRIu32"\n", btrt->maxBitrate );
2547 lsmash_ifprintf( fp, indent, "avgBitrate = %"PRIu32"\n", btrt->avgBitrate );
2548 return 0;