isom: Evaluate return value properly.
[L-SMASH.git] / codecs / h264.c
blob8f02e4d17167787619046221256d375482f9011f
1 /*****************************************************************************
2 * h264.c:
3 *****************************************************************************
4 * Copyright (C) 2012-2014 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
21 /* This file is available under an ISC license. */
23 #include "common/internal.h" /* must be placed first */
25 #include <string.h>
26 #include <stdlib.h>
27 #include <inttypes.h>
29 #include "core/box.h"
31 /***************************************************************************
32 ITU-T Recommendation H.264 (04/13)
33 ISO/IEC 14496-15:2010
34 ***************************************************************************/
35 #include "h264.h"
36 #include "nalu.h"
38 #define IF_INVALID_VALUE( x ) if( x )
39 #define IF_EXCEED_INT32( x ) if( (x) < INT32_MIN || (x) > INT32_MAX )
40 #define H264_REQUIRES_AVCC_EXTENSION( x ) ((x) == 100 || (x) == 110 || (x) == 122 || (x) == 144)
41 #define H264_POC_DEBUG_PRINT 0
43 typedef enum
45 H264_SLICE_TYPE_P = 0,
46 H264_SLICE_TYPE_B = 1,
47 H264_SLICE_TYPE_I = 2,
48 H264_SLICE_TYPE_SP = 3,
49 H264_SLICE_TYPE_SI = 4
50 } h264_slice_type;
52 void lsmash_destroy_h264_parameter_sets
54 lsmash_h264_specific_parameters_t *param
57 if( !param || !param->parameter_sets )
58 return;
59 lsmash_remove_entries( param->parameter_sets->sps_list, isom_remove_dcr_ps );
60 lsmash_remove_entries( param->parameter_sets->pps_list, isom_remove_dcr_ps );
61 lsmash_remove_entries( param->parameter_sets->spsext_list, isom_remove_dcr_ps );
62 lsmash_free( param->parameter_sets );
63 param->parameter_sets = NULL;
66 void h264_destruct_specific_data
68 void *data
71 if( !data )
72 return;
73 lsmash_destroy_h264_parameter_sets( data );
74 lsmash_free( data );
77 void h264_cleanup_parser
79 h264_info_t *info
82 if( !info )
83 return;
84 lsmash_remove_entries( info->sps_list, NULL );
85 lsmash_remove_entries( info->pps_list, NULL );
86 lsmash_remove_entries( info->slice_list, NULL );
87 lsmash_destroy_h264_parameter_sets( &info->avcC_param );
88 lsmash_destroy_h264_parameter_sets( &info->avcC_param_next );
89 lsmash_destroy_multiple_buffers( info->buffer.bank );
90 lsmash_bits_adhoc_cleanup( info->bits );
91 info->bits = NULL;
94 int h264_setup_parser
96 h264_info_t *info,
97 int parse_only
100 if( !info )
101 return -1;
102 memset( info, 0, sizeof(h264_info_t) );
103 info->avcC_param .lengthSizeMinusOne = NALU_DEFAULT_NALU_LENGTH_SIZE - 1;
104 info->avcC_param_next.lengthSizeMinusOne = NALU_DEFAULT_NALU_LENGTH_SIZE - 1;
105 h264_stream_buffer_t *sb = &info->buffer;
106 sb->bank = lsmash_create_multiple_buffers( parse_only ? 1 : 3, NALU_DEFAULT_BUFFER_SIZE );
107 if( !sb->bank )
108 return -1;
109 sb->rbsp = lsmash_withdraw_buffer( sb->bank, 1 );
110 if( !parse_only )
112 info->au.data = lsmash_withdraw_buffer( sb->bank, 2 );
113 info->au.incomplete_data = lsmash_withdraw_buffer( sb->bank, 3 );
115 info->bits = lsmash_bits_adhoc_create();
116 if( !info->bits )
118 lsmash_destroy_multiple_buffers( sb->bank );
119 return -1;
121 lsmash_init_entry_list( info->sps_list );
122 lsmash_init_entry_list( info->pps_list );
123 lsmash_init_entry_list( info->slice_list );
124 return 0;
127 static int h264_check_nalu_header
129 lsmash_bs_t *bs,
130 h264_nalu_header_t *nuh,
131 int use_long_start_code
134 uint8_t temp8 = lsmash_bs_show_byte( bs, use_long_start_code ? NALU_LONG_START_CODE_LENGTH : NALU_SHORT_START_CODE_LENGTH );
135 nuh->forbidden_zero_bit = (temp8 >> 7) & 0x01;
136 nuh->nal_ref_idc = (temp8 >> 5) & 0x03;
137 nuh->nal_unit_type = temp8 & 0x1f;
138 nuh->length = 1;
139 if( nuh->nal_unit_type == H264_NALU_TYPE_PREFIX
140 || nuh->nal_unit_type == H264_NALU_TYPE_SLICE_EXT
141 || nuh->nal_unit_type == H264_NALU_TYPE_SLICE_EXT_DVC )
143 /* We don't support these types of NALU. */
144 //nuh->length += 3;
145 return -1;
147 IF_INVALID_VALUE( nuh->forbidden_zero_bit )
148 return -1;
149 /* SPS and PPS require long start code (0x00000001).
150 * Also AU delimiter requires it too because this type of NALU shall be the first NALU of any AU if present. */
151 IF_INVALID_VALUE( !use_long_start_code
152 && (nuh->nal_unit_type == H264_NALU_TYPE_SPS
153 || nuh->nal_unit_type == H264_NALU_TYPE_PPS
154 || nuh->nal_unit_type == H264_NALU_TYPE_AUD) )
155 return -1;
156 if( nuh->nal_ref_idc )
158 /* nal_ref_idc shall be equal to 0 for all NALUs having nal_unit_type equal to 6, 9, 10, 11, or 12. */
159 IF_INVALID_VALUE( nuh->nal_unit_type == H264_NALU_TYPE_SEI
160 || nuh->nal_unit_type == H264_NALU_TYPE_AUD
161 || nuh->nal_unit_type == H264_NALU_TYPE_EOS
162 || nuh->nal_unit_type == H264_NALU_TYPE_EOB
163 || nuh->nal_unit_type == H264_NALU_TYPE_FD )
164 return -1;
166 else
167 /* nal_ref_idc shall not be equal to 0 for NALUs with nal_unit_type equal to 5. */
168 IF_INVALID_VALUE( nuh->nal_unit_type == H264_NALU_TYPE_SLICE_IDR )
169 return -1;
170 return 0;
173 uint64_t h264_find_next_start_code
175 lsmash_bs_t *bs,
176 h264_nalu_header_t *nuh,
177 uint64_t *start_code_length,
178 uint64_t *trailing_zero_bytes
181 uint64_t length = 0; /* the length of the latest NALU */
182 uint64_t count = 0; /* the number of the trailing zero bytes after the latest NALU */
183 /* Check the type of the current start code. */
184 int long_start_code
185 = (!lsmash_bs_is_end( bs, NALU_LONG_START_CODE_LENGTH ) && 0x00000001 == lsmash_bs_show_be32( bs, 0 )) ? 1
186 : (!lsmash_bs_is_end( bs, NALU_SHORT_START_CODE_LENGTH ) && 0x000001 == lsmash_bs_show_be24( bs, 0 )) ? 0
187 : -1;
188 if( long_start_code >= 0 && h264_check_nalu_header( bs, nuh, long_start_code ) == 0 )
190 *start_code_length = long_start_code ? NALU_LONG_START_CODE_LENGTH : NALU_SHORT_START_CODE_LENGTH;
191 uint64_t distance = *start_code_length + nuh->length;
192 /* Find the start code of the next NALU and get the distance from the start code of the latest NALU. */
193 if( !lsmash_bs_is_end( bs, distance + NALU_SHORT_START_CODE_LENGTH ) )
195 uint32_t sync_bytes = lsmash_bs_show_be24( bs, distance );
196 while( 0x000001 != sync_bytes )
198 if( lsmash_bs_is_end( bs, ++distance + NALU_SHORT_START_CODE_LENGTH ) )
200 distance = lsmash_bs_get_remaining_buffer_size( bs );
201 break;
203 sync_bytes <<= 8;
204 sync_bytes |= lsmash_bs_show_byte( bs, distance + NALU_SHORT_START_CODE_LENGTH - 1 );
205 sync_bytes &= 0xFFFFFF;
208 else
209 distance = lsmash_bs_get_remaining_buffer_size( bs );
210 /* Any NALU has no consecutive zero bytes at the end. */
211 while( 0x00 == lsmash_bs_show_byte( bs, distance - 1 ) )
213 --distance;
214 ++count;
216 /* Remove the length of the start code. */
217 length = distance - *start_code_length;
218 /* If there are one or more trailing zero bytes, we treat the last one byte as a part of the next start code.
219 * This makes the next start code a long start code. */
220 if( count )
221 --count;
223 else
225 /* No start code. */
226 nuh->forbidden_zero_bit = 1; /* shall be 0, so invalid */
227 nuh->nal_ref_idc = 0; /* arbitrary */
228 nuh->nal_unit_type = H264_NALU_TYPE_UNSPECIFIED0;
229 nuh->length = 0;
230 *start_code_length = 0;
232 *trailing_zero_bytes = count;
233 return length;
236 static h264_sps_t *h264_get_sps
238 lsmash_entry_list_t *sps_list,
239 uint8_t sps_id
242 if( !sps_list || sps_id > 31 )
243 return NULL;
244 for( lsmash_entry_t *entry = sps_list->head; entry; entry = entry->next )
246 h264_sps_t *sps = (h264_sps_t *)entry->data;
247 if( !sps )
248 return NULL;
249 if( sps->seq_parameter_set_id == sps_id )
250 return sps;
252 h264_sps_t *sps = lsmash_malloc_zero( sizeof(h264_sps_t) );
253 if( !sps )
254 return NULL;
255 sps->seq_parameter_set_id = sps_id;
256 if( lsmash_add_entry( sps_list, sps ) )
258 lsmash_free( sps );
259 return NULL;
261 return sps;
264 static h264_pps_t *h264_get_pps
266 lsmash_entry_list_t *pps_list,
267 uint8_t pps_id
270 if( !pps_list )
271 return NULL;
272 for( lsmash_entry_t *entry = pps_list->head; entry; entry = entry->next )
274 h264_pps_t *pps = (h264_pps_t *)entry->data;
275 if( !pps )
276 return NULL;
277 if( pps->pic_parameter_set_id == pps_id )
278 return pps;
280 h264_pps_t *pps = lsmash_malloc_zero( sizeof(h264_pps_t) );
281 if( !pps )
282 return NULL;
283 pps->pic_parameter_set_id = pps_id;
284 if( lsmash_add_entry( pps_list, pps ) )
286 lsmash_free( pps );
287 return NULL;
289 return pps;
292 static h264_slice_info_t *h264_get_slice_info
294 lsmash_entry_list_t *slice_list,
295 uint8_t slice_id
298 if( !slice_list )
299 return NULL;
300 for( lsmash_entry_t *entry = slice_list->head; entry; entry = entry->next )
302 h264_slice_info_t *slice = (h264_slice_info_t *)entry->data;
303 if( !slice )
304 return NULL;
305 if( slice->slice_id == slice_id )
306 return slice;
308 h264_slice_info_t *slice = lsmash_malloc_zero( sizeof(h264_slice_info_t) );
309 if( !slice )
310 return NULL;
311 slice->slice_id = slice_id;
312 if( lsmash_add_entry( slice_list, slice ) )
314 lsmash_free( slice );
315 return NULL;
317 return slice;
320 int h264_calculate_poc
322 h264_info_t *info,
323 h264_picture_info_t *picture,
324 h264_picture_info_t *prev_picture
327 #if H264_POC_DEBUG_PRINT
328 fprintf( stderr, "PictureOrderCount\n" );
329 #endif
330 h264_pps_t *pps = h264_get_pps( info->pps_list, picture->pic_parameter_set_id );
331 if( !pps )
332 return -1;
333 h264_sps_t *sps = h264_get_sps( info->sps_list, pps->seq_parameter_set_id );
334 if( !sps )
335 return -1;
336 int64_t TopFieldOrderCnt = 0;
337 int64_t BottomFieldOrderCnt = 0;
338 if( sps->pic_order_cnt_type == 0 )
340 int32_t prevPicOrderCntMsb;
341 int32_t prevPicOrderCntLsb;
342 if( picture->idr )
344 prevPicOrderCntMsb = 0;
345 prevPicOrderCntLsb = 0;
347 else if( prev_picture->ref_pic_has_mmco5 )
349 prevPicOrderCntMsb = 0;
350 prevPicOrderCntLsb = prev_picture->ref_pic_bottom_field_flag ? 0 : prev_picture->ref_pic_TopFieldOrderCnt;
352 else
354 prevPicOrderCntMsb = prev_picture->ref_pic_PicOrderCntMsb;
355 prevPicOrderCntLsb = prev_picture->ref_pic_PicOrderCntLsb;
357 int64_t PicOrderCntMsb;
358 int32_t pic_order_cnt_lsb = picture->pic_order_cnt_lsb;
359 uint64_t MaxPicOrderCntLsb = sps->MaxPicOrderCntLsb;
360 if( (pic_order_cnt_lsb < prevPicOrderCntLsb)
361 && ((prevPicOrderCntLsb - pic_order_cnt_lsb) >= (MaxPicOrderCntLsb / 2)) )
362 PicOrderCntMsb = prevPicOrderCntMsb + MaxPicOrderCntLsb;
363 else if( (pic_order_cnt_lsb > prevPicOrderCntLsb)
364 && ((pic_order_cnt_lsb - prevPicOrderCntLsb) > (MaxPicOrderCntLsb / 2)) )
365 PicOrderCntMsb = prevPicOrderCntMsb - MaxPicOrderCntLsb;
366 else
367 PicOrderCntMsb = prevPicOrderCntMsb;
368 IF_EXCEED_INT32( PicOrderCntMsb )
369 return -1;
370 BottomFieldOrderCnt = TopFieldOrderCnt = PicOrderCntMsb + pic_order_cnt_lsb;
371 if( !picture->field_pic_flag )
372 BottomFieldOrderCnt += picture->delta_pic_order_cnt_bottom;
373 IF_EXCEED_INT32( TopFieldOrderCnt )
374 return -1;
375 IF_EXCEED_INT32( BottomFieldOrderCnt )
376 return -1;
377 if( !picture->disposable )
379 picture->ref_pic_has_mmco5 = picture->has_mmco5;
380 picture->ref_pic_bottom_field_flag = picture->bottom_field_flag;
381 picture->ref_pic_TopFieldOrderCnt = TopFieldOrderCnt;
382 picture->ref_pic_PicOrderCntMsb = PicOrderCntMsb;
383 picture->ref_pic_PicOrderCntLsb = pic_order_cnt_lsb;
385 #if H264_POC_DEBUG_PRINT
386 fprintf( stderr, " prevPicOrderCntMsb: %"PRId32"\n", prevPicOrderCntMsb );
387 fprintf( stderr, " prevPicOrderCntLsb: %"PRId32"\n", prevPicOrderCntLsb );
388 fprintf( stderr, " PicOrderCntMsb: %"PRId64"\n", PicOrderCntMsb );
389 fprintf( stderr, " pic_order_cnt_lsb: %"PRId32"\n", pic_order_cnt_lsb );
390 fprintf( stderr, " MaxPicOrderCntLsb: %"PRIu64"\n", MaxPicOrderCntLsb );
391 #endif
393 else if( sps->pic_order_cnt_type == 1 )
395 uint32_t frame_num = picture->frame_num;
396 uint32_t prevFrameNum = prev_picture->has_mmco5 ? 0 : prev_picture->frame_num;
397 uint32_t prevFrameNumOffset = prev_picture->has_mmco5 ? 0 : prev_picture->FrameNumOffset;
398 uint64_t FrameNumOffset = picture->idr ? 0 : prevFrameNumOffset + (prevFrameNum > frame_num ? sps->MaxFrameNum : 0);
399 IF_INVALID_VALUE( FrameNumOffset > INT32_MAX )
400 return -1;
401 int64_t expectedPicOrderCnt;
402 if( sps->num_ref_frames_in_pic_order_cnt_cycle )
404 uint64_t absFrameNum = FrameNumOffset + frame_num;
405 absFrameNum -= picture->disposable && absFrameNum > 0;
406 if( absFrameNum )
408 uint64_t picOrderCntCycleCnt = (absFrameNum - 1) / sps->num_ref_frames_in_pic_order_cnt_cycle;
409 uint8_t frameNumInPicOrderCntCycle = (absFrameNum - 1) % sps->num_ref_frames_in_pic_order_cnt_cycle;
410 expectedPicOrderCnt = picOrderCntCycleCnt * sps->ExpectedDeltaPerPicOrderCntCycle;
411 for( uint8_t i = 0; i <= frameNumInPicOrderCntCycle; i++ )
412 expectedPicOrderCnt += sps->offset_for_ref_frame[i];
414 else
415 expectedPicOrderCnt = 0;
417 else
418 expectedPicOrderCnt = 0;
419 if( picture->disposable )
420 expectedPicOrderCnt += sps->offset_for_non_ref_pic;
421 TopFieldOrderCnt = expectedPicOrderCnt + picture->delta_pic_order_cnt[0];
422 BottomFieldOrderCnt = TopFieldOrderCnt + sps->offset_for_top_to_bottom_field;
423 if( !picture->field_pic_flag )
424 BottomFieldOrderCnt += picture->delta_pic_order_cnt[1];
425 IF_EXCEED_INT32( TopFieldOrderCnt )
426 return -1;
427 IF_EXCEED_INT32( BottomFieldOrderCnt )
428 return -1;
429 picture->FrameNumOffset = FrameNumOffset;
431 else if( sps->pic_order_cnt_type == 2 )
433 uint32_t frame_num = picture->frame_num;
434 uint32_t prevFrameNum = prev_picture->has_mmco5 ? 0 : prev_picture->frame_num;
435 int32_t prevFrameNumOffset = prev_picture->has_mmco5 ? 0 : prev_picture->FrameNumOffset;
436 int64_t FrameNumOffset;
437 int64_t tempPicOrderCnt;
438 if( picture->idr )
440 FrameNumOffset = 0;
441 tempPicOrderCnt = 0;
443 else
445 FrameNumOffset = prevFrameNumOffset + (prevFrameNum > frame_num ? sps->MaxFrameNum : 0);
446 tempPicOrderCnt = 2 * (FrameNumOffset + frame_num) - picture->disposable;
447 IF_EXCEED_INT32( FrameNumOffset )
448 return -1;
449 IF_EXCEED_INT32( tempPicOrderCnt )
450 return -1;
452 TopFieldOrderCnt = tempPicOrderCnt;
453 BottomFieldOrderCnt = tempPicOrderCnt;
454 picture->FrameNumOffset = FrameNumOffset;
456 if( !picture->field_pic_flag )
457 picture->PicOrderCnt = LSMASH_MIN( TopFieldOrderCnt, BottomFieldOrderCnt );
458 else
459 picture->PicOrderCnt = picture->bottom_field_flag ? BottomFieldOrderCnt : TopFieldOrderCnt;
460 #if H264_POC_DEBUG_PRINT
461 if( picture->field_pic_flag )
463 if( !picture->bottom_field_flag )
464 fprintf( stderr, " TopFieldOrderCnt: %"PRId64"\n", TopFieldOrderCnt );
465 else
466 fprintf( stderr, " BottomFieldOrderCnt: %"PRId64"\n", BottomFieldOrderCnt );
468 fprintf( stderr, " POC: %"PRId32"\n", picture->PicOrderCnt );
469 #endif
470 return 0;
473 static int h264_parse_scaling_list
475 lsmash_bits_t *bits,
476 int sizeOfScalingList
479 /* scaling_list( scalingList, sizeOfScalingList, useDefaultScalingMatrixFlag ) */
480 int nextScale = 8;
481 for( int i = 0; i < sizeOfScalingList; i++ )
483 int64_t delta_scale = nalu_get_exp_golomb_se( bits );
484 IF_INVALID_VALUE( delta_scale < -128 || delta_scale > 127 )
485 return -1;
486 nextScale = (nextScale + delta_scale + 256) % 256;
487 if( nextScale == 0 )
488 break;
490 return 0;
493 static int h264_parse_hrd_parameters
495 lsmash_bits_t *bits,
496 h264_hrd_t *hrd
499 /* hrd_parameters() */
500 uint64_t cpb_cnt_minus1 = nalu_get_exp_golomb_ue( bits );
501 IF_INVALID_VALUE( cpb_cnt_minus1 > 31 )
502 return -1;
503 lsmash_bits_get( bits, 4 ); /* bit_rate_scale */
504 lsmash_bits_get( bits, 4 ); /* cpb_size_scale */
505 for( uint64_t SchedSelIdx = 0; SchedSelIdx <= cpb_cnt_minus1; SchedSelIdx++ )
507 nalu_get_exp_golomb_ue( bits ); /* bit_rate_value_minus1[ SchedSelIdx ] */
508 nalu_get_exp_golomb_ue( bits ); /* cpb_size_value_minus1[ SchedSelIdx ] */
509 lsmash_bits_get( bits, 1 ); /* cbr_flag [ SchedSelIdx ] */
511 lsmash_bits_get( bits, 5 ); /* initial_cpb_removal_delay_length_minus1 */
512 hrd->cpb_removal_delay_length = lsmash_bits_get( bits, 5 ) + 1;
513 hrd->dpb_output_delay_length = lsmash_bits_get( bits, 5 ) + 1;
514 lsmash_bits_get( bits, 5 ); /* time_offset_length */
515 return 0;
518 static int h264_parse_sps_minimally
520 lsmash_bits_t *bits,
521 h264_sps_t *sps,
522 uint8_t *rbsp_buffer,
523 uint8_t *ebsp,
524 uint64_t ebsp_size
527 if( nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, ebsp_size ) )
528 return -1;
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_INVALID_VALUE( seq_parameter_set_id > 31 )
535 return -1;
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_INVALID_VALUE( bit_depth_luma_minus8 > 6 )
547 return -1;
548 uint64_t bit_depth_chroma_minus8 = nalu_get_exp_golomb_ue( bits );
549 IF_INVALID_VALUE( bit_depth_chroma_minus8 > 6 )
550 return -1;
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 && h264_parse_scaling_list( bits, i < 6 ? 16 : 64 ) )
560 return -1;
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 ? -1 : 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 if( h264_parse_sps_minimally( bits, &temp_sps, rbsp_buffer, ebsp, ebsp_size ) )
585 return -1;
586 h264_sps_t *sps = h264_get_sps( info->sps_list, temp_sps.seq_parameter_set_id );
587 if( !sps )
588 return -1;
589 memset( sps, 0, sizeof(h264_sps_t) );
590 sps->profile_idc = temp_sps.profile_idc;
591 sps->constraint_set_flags = temp_sps.constraint_set_flags;
592 sps->level_idc = temp_sps.level_idc;
593 sps->seq_parameter_set_id = temp_sps.seq_parameter_set_id;
594 sps->chroma_format_idc = temp_sps.chroma_format_idc;
595 sps->separate_colour_plane_flag = temp_sps.separate_colour_plane_flag;
596 sps->bit_depth_luma_minus8 = temp_sps.bit_depth_luma_minus8;
597 sps->bit_depth_chroma_minus8 = temp_sps.bit_depth_chroma_minus8;
598 sps->ChromaArrayType = sps->separate_colour_plane_flag ? 0 : sps->chroma_format_idc;
599 uint64_t log2_max_frame_num_minus4 = nalu_get_exp_golomb_ue( bits );
600 IF_INVALID_VALUE( log2_max_frame_num_minus4 > 12 )
601 return -1;
602 sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4;
603 sps->MaxFrameNum = 1 << sps->log2_max_frame_num;
604 uint64_t pic_order_cnt_type = nalu_get_exp_golomb_ue( bits );
605 IF_INVALID_VALUE( pic_order_cnt_type > 2 )
606 return -1;
607 sps->pic_order_cnt_type = pic_order_cnt_type;
608 if( sps->pic_order_cnt_type == 0 )
610 uint64_t log2_max_pic_order_cnt_lsb_minus4 = nalu_get_exp_golomb_ue( bits );
611 IF_INVALID_VALUE( log2_max_pic_order_cnt_lsb_minus4 > 12 )
612 return -1;
613 sps->log2_max_pic_order_cnt_lsb = log2_max_pic_order_cnt_lsb_minus4 + 4;
614 sps->MaxPicOrderCntLsb = 1 << sps->log2_max_pic_order_cnt_lsb;
616 else if( sps->pic_order_cnt_type == 1 )
618 sps->delta_pic_order_always_zero_flag = lsmash_bits_get( bits, 1 );
619 int64_t max_value = ((uint64_t)1 << 31) - 1;
620 int64_t min_value = -((uint64_t)1 << 31) + 1;
621 int64_t offset_for_non_ref_pic = nalu_get_exp_golomb_se( bits );
622 if( offset_for_non_ref_pic < min_value || offset_for_non_ref_pic > max_value )
623 return -1;
624 sps->offset_for_non_ref_pic = offset_for_non_ref_pic;
625 int64_t offset_for_top_to_bottom_field = nalu_get_exp_golomb_se( bits );
626 if( offset_for_top_to_bottom_field < min_value || offset_for_top_to_bottom_field > max_value )
627 return -1;
628 sps->offset_for_top_to_bottom_field = offset_for_top_to_bottom_field;
629 uint64_t num_ref_frames_in_pic_order_cnt_cycle = nalu_get_exp_golomb_ue( bits );
630 IF_INVALID_VALUE( num_ref_frames_in_pic_order_cnt_cycle > 255 )
631 return -1;
632 sps->num_ref_frames_in_pic_order_cnt_cycle = num_ref_frames_in_pic_order_cnt_cycle;
633 sps->ExpectedDeltaPerPicOrderCntCycle = 0;
634 for( int i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++ )
636 int64_t offset_for_ref_frame = nalu_get_exp_golomb_se( bits );
637 if( offset_for_ref_frame < min_value || offset_for_ref_frame > max_value )
638 return -1;
639 sps->offset_for_ref_frame[i] = offset_for_ref_frame;
640 sps->ExpectedDeltaPerPicOrderCntCycle += offset_for_ref_frame;
643 sps->max_num_ref_frames = nalu_get_exp_golomb_ue( bits );
644 lsmash_bits_get( bits, 1 ); /* gaps_in_frame_num_value_allowed_flag */
645 uint64_t pic_width_in_mbs_minus1 = nalu_get_exp_golomb_ue( bits );
646 uint64_t pic_height_in_map_units_minus1 = nalu_get_exp_golomb_ue( bits );
647 sps->frame_mbs_only_flag = lsmash_bits_get( bits, 1 );
648 if( !sps->frame_mbs_only_flag )
649 lsmash_bits_get( bits, 1 ); /* mb_adaptive_frame_field_flag */
650 lsmash_bits_get( bits, 1 ); /* direct_8x8_inference_flag */
651 uint64_t PicWidthInMbs = pic_width_in_mbs_minus1 + 1;
652 uint64_t PicHeightInMapUnits = pic_height_in_map_units_minus1 + 1;
653 sps->PicSizeInMapUnits = PicWidthInMbs * PicHeightInMapUnits;
654 sps->cropped_width = PicWidthInMbs * 16;
655 sps->cropped_height = (2 - sps->frame_mbs_only_flag) * PicHeightInMapUnits * 16;
656 if( lsmash_bits_get( bits, 1 ) ) /* frame_cropping_flag */
658 uint8_t CropUnitX;
659 uint8_t CropUnitY;
660 if( sps->ChromaArrayType == 0 )
662 CropUnitX = 1;
663 CropUnitY = 2 - sps->frame_mbs_only_flag;
665 else
667 static const int SubWidthC [] = { 0, 2, 2, 1 };
668 static const int SubHeightC[] = { 0, 2, 1, 1 };
669 CropUnitX = SubWidthC [ sps->chroma_format_idc ];
670 CropUnitY = SubHeightC[ sps->chroma_format_idc ] * (2 - sps->frame_mbs_only_flag);
672 uint64_t frame_crop_left_offset = nalu_get_exp_golomb_ue( bits );
673 uint64_t frame_crop_right_offset = nalu_get_exp_golomb_ue( bits );
674 uint64_t frame_crop_top_offset = nalu_get_exp_golomb_ue( bits );
675 uint64_t frame_crop_bottom_offset = nalu_get_exp_golomb_ue( bits );
676 sps->cropped_width -= (frame_crop_left_offset + frame_crop_right_offset) * CropUnitX;
677 sps->cropped_height -= (frame_crop_top_offset + frame_crop_bottom_offset) * CropUnitY;
679 if( lsmash_bits_get( bits, 1 ) ) /* vui_parameters_present_flag */
681 /* vui_parameters() */
682 if( lsmash_bits_get( bits, 1 ) ) /* aspect_ratio_info_present_flag */
684 uint8_t aspect_ratio_idc = lsmash_bits_get( bits, 8 );
685 if( aspect_ratio_idc == 255 )
687 /* Extended_SAR */
688 sps->vui.sar_width = lsmash_bits_get( bits, 16 );
689 sps->vui.sar_height = lsmash_bits_get( bits, 16 );
691 else
693 static const struct
695 uint16_t sar_width;
696 uint16_t sar_height;
697 } pre_defined_sar[]
699 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
700 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 }, { 80, 33 },
701 { 18, 11 }, { 15, 11 }, { 64, 33 }, { 160, 99 }, { 4, 3 },
702 { 3, 2 }, { 2, 1 }
704 if( aspect_ratio_idc < (sizeof(pre_defined_sar) / sizeof(pre_defined_sar[0])) )
706 sps->vui.sar_width = pre_defined_sar[ aspect_ratio_idc ].sar_width;
707 sps->vui.sar_height = pre_defined_sar[ aspect_ratio_idc ].sar_height;
709 else
711 /* Behavior when unknown aspect_ratio_idc is detected is not specified in the specification. */
712 sps->vui.sar_width = 0;
713 sps->vui.sar_height = 0;
717 if( lsmash_bits_get( bits, 1 ) ) /* overscan_info_present_flag */
718 lsmash_bits_get( bits, 1 ); /* overscan_appropriate_flag */
719 if( lsmash_bits_get( bits, 1 ) ) /* video_signal_type_present_flag */
721 lsmash_bits_get( bits, 3 ); /* video_format */
722 sps->vui.video_full_range_flag = lsmash_bits_get( bits, 1 );
723 if( lsmash_bits_get( bits, 1 ) ) /* colour_description_present_flag */
725 sps->vui.colour_primaries = lsmash_bits_get( bits, 8 );
726 sps->vui.transfer_characteristics = lsmash_bits_get( bits, 8 );
727 sps->vui.matrix_coefficients = lsmash_bits_get( bits, 8 );
730 if( lsmash_bits_get( bits, 1 ) ) /* chroma_loc_info_present_flag */
732 nalu_get_exp_golomb_ue( bits ); /* chroma_sample_loc_type_top_field */
733 nalu_get_exp_golomb_ue( bits ); /* chroma_sample_loc_type_bottom_field */
735 if( lsmash_bits_get( bits, 1 ) ) /* timing_info_present_flag */
737 sps->vui.num_units_in_tick = lsmash_bits_get( bits, 32 );
738 sps->vui.time_scale = lsmash_bits_get( bits, 32 );
739 sps->vui.fixed_frame_rate_flag = lsmash_bits_get( bits, 1 );
741 else
743 sps->vui.num_units_in_tick = 1; /* arbitrary */
744 sps->vui.time_scale = 50; /* arbitrary */
745 sps->vui.fixed_frame_rate_flag = 0;
747 int nal_hrd_parameters_present_flag = lsmash_bits_get( bits, 1 );
748 if( nal_hrd_parameters_present_flag
749 && h264_parse_hrd_parameters( bits, &sps->vui.hrd ) )
750 return -1;
751 int vcl_hrd_parameters_present_flag = lsmash_bits_get( bits, 1 );
752 if( vcl_hrd_parameters_present_flag
753 && h264_parse_hrd_parameters( bits, &sps->vui.hrd ) )
754 return -1;
755 if( nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag )
757 sps->vui.hrd.present = 1;
758 sps->vui.hrd.CpbDpbDelaysPresentFlag = 1;
759 lsmash_bits_get( bits, 1 ); /* low_delay_hrd_flag */
761 sps->vui.pic_struct_present_flag = lsmash_bits_get( bits, 1 );
762 if( lsmash_bits_get( bits, 1 ) ) /* bitstream_restriction_flag */
764 lsmash_bits_get( bits, 1 ); /* motion_vectors_over_pic_boundaries_flag */
765 nalu_get_exp_golomb_ue( bits ); /* max_bytes_per_pic_denom */
766 nalu_get_exp_golomb_ue( bits ); /* max_bits_per_mb_denom */
767 nalu_get_exp_golomb_ue( bits ); /* log2_max_mv_length_horizontal */
768 nalu_get_exp_golomb_ue( bits ); /* log2_max_mv_length_vertical */
769 nalu_get_exp_golomb_ue( bits ); /* max_num_reorder_frames */
770 nalu_get_exp_golomb_ue( bits ); /* max_dec_frame_buffering */
773 else
775 sps->vui.video_full_range_flag = 0;
776 sps->vui.num_units_in_tick = 1; /* arbitrary */
777 sps->vui.time_scale = 50; /* arbitrary */
778 sps->vui.fixed_frame_rate_flag = 0;
780 /* rbsp_trailing_bits() */
781 IF_INVALID_VALUE( !lsmash_bits_get( bits, 1 ) ) /* rbsp_stop_one_bit */
782 return -1;
783 lsmash_bits_empty( bits );
784 if( bits->bs->error )
785 return -1;
786 sps->present = 1;
787 info->sps = *sps;
788 return 0;
791 static int h264_parse_pps_minimally
793 lsmash_bits_t *bits,
794 h264_pps_t *pps,
795 uint8_t *rbsp_buffer,
796 uint8_t *ebsp,
797 uint64_t ebsp_size
800 if( nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, ebsp_size ) )
801 return -1;
802 memset( pps, 0, sizeof(h264_pps_t) );
803 uint64_t pic_parameter_set_id = nalu_get_exp_golomb_ue( bits );
804 IF_INVALID_VALUE( pic_parameter_set_id > 255 )
805 return -1;
806 pps->pic_parameter_set_id = pic_parameter_set_id;
807 return bits->bs->error ? -1 : 0;
810 int h264_parse_pps
812 h264_info_t *info,
813 uint8_t *rbsp_buffer,
814 uint8_t *ebsp,
815 uint64_t ebsp_size
818 lsmash_bits_t *bits = info->bits;
819 /* pic_parameter_set_rbsp */
820 h264_pps_t temp_pps;
821 if( h264_parse_pps_minimally( bits, &temp_pps, rbsp_buffer, ebsp, ebsp_size ) )
822 return -1;
823 h264_pps_t *pps = h264_get_pps( info->pps_list, temp_pps.pic_parameter_set_id );
824 if( !pps )
825 return -1;
826 memset( pps, 0, sizeof(h264_pps_t) );
827 pps->pic_parameter_set_id = temp_pps.pic_parameter_set_id;
828 uint64_t seq_parameter_set_id = nalu_get_exp_golomb_ue( bits );
829 IF_INVALID_VALUE( seq_parameter_set_id > 31 )
830 return -1;
831 h264_sps_t *sps = h264_get_sps( info->sps_list, seq_parameter_set_id );
832 if( !sps )
833 return -1;
834 pps->seq_parameter_set_id = seq_parameter_set_id;
835 pps->entropy_coding_mode_flag = lsmash_bits_get( bits, 1 );
836 pps->bottom_field_pic_order_in_frame_present_flag = lsmash_bits_get( bits, 1 );
837 uint64_t num_slice_groups_minus1 = nalu_get_exp_golomb_ue( bits );
838 IF_INVALID_VALUE( num_slice_groups_minus1 > 7 )
839 return -1;
840 pps->num_slice_groups_minus1 = num_slice_groups_minus1;
841 if( num_slice_groups_minus1 ) /* num_slice_groups_minus1 */
843 uint64_t slice_group_map_type = nalu_get_exp_golomb_ue( bits );
844 IF_INVALID_VALUE( slice_group_map_type > 6 )
845 return -1;
846 pps->slice_group_map_type = slice_group_map_type;
847 if( slice_group_map_type == 0 )
848 for( uint64_t iGroup = 0; iGroup <= num_slice_groups_minus1; iGroup++ )
849 nalu_get_exp_golomb_ue( bits ); /* run_length_minus1[ iGroup ] */
850 else if( slice_group_map_type == 2 )
851 for( uint64_t iGroup = 0; iGroup < num_slice_groups_minus1; iGroup++ )
853 nalu_get_exp_golomb_ue( bits ); /* top_left [ iGroup ] */
854 nalu_get_exp_golomb_ue( bits ); /* bottom_right[ iGroup ] */
856 else if( slice_group_map_type == 3
857 || slice_group_map_type == 4
858 || slice_group_map_type == 5 )
860 lsmash_bits_get( bits, 1 ); /* slice_group_change_direction_flag */
861 uint64_t slice_group_change_rate_minus1 = nalu_get_exp_golomb_ue( bits );
862 IF_INVALID_VALUE( slice_group_change_rate_minus1 > (sps->PicSizeInMapUnits - 1) )
863 return -1;
864 pps->SliceGroupChangeRate = slice_group_change_rate_minus1 + 1;
866 else if( slice_group_map_type == 6 )
868 uint64_t pic_size_in_map_units_minus1 = nalu_get_exp_golomb_ue( bits );
869 int length = lsmash_ceil_log2( num_slice_groups_minus1 + 1 );
870 for( uint64_t i = 0; i <= pic_size_in_map_units_minus1; i++ )
871 /* slice_group_id */
872 IF_INVALID_VALUE( lsmash_bits_get( bits, length ) > num_slice_groups_minus1 )
873 return -1;
876 pps->num_ref_idx_l0_default_active_minus1 = nalu_get_exp_golomb_ue( bits );
877 pps->num_ref_idx_l1_default_active_minus1 = nalu_get_exp_golomb_ue( bits );
878 pps->weighted_pred_flag = lsmash_bits_get( bits, 1 );
879 pps->weighted_bipred_idc = lsmash_bits_get( bits, 2 );
880 nalu_get_exp_golomb_se( bits ); /* pic_init_qp_minus26 */
881 nalu_get_exp_golomb_se( bits ); /* pic_init_qs_minus26 */
882 nalu_get_exp_golomb_se( bits ); /* chroma_qp_index_offset */
883 pps->deblocking_filter_control_present_flag = lsmash_bits_get( bits, 1 );
884 lsmash_bits_get( bits, 1 ); /* constrained_intra_pred_flag */
885 pps->redundant_pic_cnt_present_flag = lsmash_bits_get( bits, 1 );
886 if( nalu_check_more_rbsp_data( bits ) )
888 int transform_8x8_mode_flag = lsmash_bits_get( bits, 1 );
889 if( lsmash_bits_get( bits, 1 ) ) /* pic_scaling_matrix_present_flag */
891 int num_loops = 6 + (sps->chroma_format_idc != 3 ? 2 : 6) * transform_8x8_mode_flag;
892 for( int i = 0; i < num_loops; i++ )
893 if( lsmash_bits_get( bits, 1 ) /* pic_scaling_list_present_flag[i] */
894 && h264_parse_scaling_list( bits, i < 6 ? 16 : 64 ) )
895 return -1;
897 nalu_get_exp_golomb_se( bits ); /* second_chroma_qp_index_offset */
899 /* rbsp_trailing_bits() */
900 IF_INVALID_VALUE( !lsmash_bits_get( bits, 1 ) ) /* rbsp_stop_one_bit */
901 return -1;
902 lsmash_bits_empty( bits );
903 if( bits->bs->error )
904 return -1;
905 pps->present = 1;
906 info->sps = *sps;
907 info->pps = *pps;
908 return 0;
911 int h264_parse_sei
913 lsmash_bits_t *bits,
914 h264_sps_t *sps,
915 h264_sei_t *sei,
916 uint8_t *rbsp_buffer,
917 uint8_t *ebsp,
918 uint64_t ebsp_size
921 if( nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, ebsp_size ) )
922 return -1;
923 uint8_t *rbsp_start = rbsp_buffer;
924 uint64_t rbsp_pos = 0;
927 /* sei_message() */
928 uint32_t payloadType = 0;
929 for( uint8_t temp = lsmash_bits_get( bits, 8 ); ; temp = lsmash_bits_get( bits, 8 ) )
931 /* 0xff : ff_byte
932 * otherwise: last_payload_type_byte */
933 payloadType += temp;
934 ++rbsp_pos;
935 if( temp != 0xff )
936 break;
938 uint32_t payloadSize = 0;
939 for( uint8_t temp = lsmash_bits_get( bits, 8 ); ; temp = lsmash_bits_get( bits, 8 ) )
941 /* 0xff : ff_byte
942 * otherwise: last_payload_size_byte */
943 payloadSize += temp;
944 ++rbsp_pos;
945 if( temp != 0xff )
946 break;
948 if( payloadType == 1 )
950 /* pic_timing */
951 h264_hrd_t *hrd = sps ? &sps->vui.hrd : NULL;
952 if( !hrd )
953 goto skip_sei_message; /* Any active SPS is not found. */
954 sei->pic_timing.present = 1;
955 if( hrd->CpbDpbDelaysPresentFlag )
957 lsmash_bits_get( bits, hrd->cpb_removal_delay_length ); /* cpb_removal_delay */
958 lsmash_bits_get( bits, hrd->dpb_output_delay_length ); /* dpb_output_delay */
960 if( sps->vui.pic_struct_present_flag )
962 sei->pic_timing.pic_struct = lsmash_bits_get( bits, 4 );
963 /* Skip the remaining bits. */
964 uint32_t remaining_bits = payloadSize * 8 - 4;
965 if( hrd->CpbDpbDelaysPresentFlag )
966 remaining_bits -= hrd->cpb_removal_delay_length
967 + hrd->dpb_output_delay_length;
968 lsmash_bits_get( bits, remaining_bits );
971 else if( payloadType == 3 )
973 /* filler_payload
974 * AVC file format is forbidden to contain this. */
975 return -1;
977 else if( payloadType == 6 )
979 /* recovery_point */
980 sei->recovery_point.present = 1;
981 sei->recovery_point.random_accessible = 1;
982 sei->recovery_point.recovery_frame_cnt = nalu_get_exp_golomb_ue( bits );
983 lsmash_bits_get( bits, 1 ); /* exact_match_flag */
984 sei->recovery_point.broken_link_flag = lsmash_bits_get( bits, 1 );
985 lsmash_bits_get( bits, 2 ); /* changing_slice_group_idc */
987 else
989 skip_sei_message:
990 lsmash_bits_get( bits, payloadSize * 8 );
992 lsmash_bits_get_align( bits );
993 rbsp_pos += payloadSize;
994 } while( *(rbsp_start + rbsp_pos) != 0x80 ); /* All SEI messages are byte aligned at their end.
995 * Therefore, 0x80 shall be rbsp_trailing_bits(). */
996 lsmash_bits_empty( bits );
997 return bits->bs->error ? -1 : 0;
1000 static int h264_parse_slice_header
1002 h264_info_t *info,
1003 h264_nalu_header_t *nuh
1006 h264_slice_info_t *slice = &info->slice;
1007 memset( slice, 0, sizeof(h264_slice_info_t) );
1008 /* slice_header() */
1009 lsmash_bits_t *bits = info->bits;
1010 nalu_get_exp_golomb_ue( bits ); /* first_mb_in_slice */
1011 uint8_t slice_type = slice->type = nalu_get_exp_golomb_ue( bits );
1012 IF_INVALID_VALUE( (uint64_t)slice->type > 9 )
1013 return -1;
1014 if( slice_type > 4 )
1015 slice_type = slice->type -= 5;
1016 uint64_t pic_parameter_set_id = nalu_get_exp_golomb_ue( bits );
1017 IF_INVALID_VALUE( pic_parameter_set_id > 255 )
1018 return -1;
1019 slice->pic_parameter_set_id = pic_parameter_set_id;
1020 h264_pps_t *pps = h264_get_pps( info->pps_list, pic_parameter_set_id );
1021 if( !pps )
1022 return -1;
1023 h264_sps_t *sps = h264_get_sps( info->sps_list, pps->seq_parameter_set_id );
1024 if( !sps )
1025 return -1;
1026 slice->seq_parameter_set_id = pps->seq_parameter_set_id;
1027 slice->nal_ref_idc = nuh->nal_ref_idc;
1028 slice->IdrPicFlag = (nuh->nal_unit_type == H264_NALU_TYPE_SLICE_IDR);
1029 slice->pic_order_cnt_type = sps->pic_order_cnt_type;
1030 IF_INVALID_VALUE( (slice->IdrPicFlag || sps->max_num_ref_frames == 0) && slice_type != 2 && slice_type != 4 )
1031 return -1;
1032 if( sps->separate_colour_plane_flag )
1033 lsmash_bits_get( bits, 2 ); /* colour_plane_id */
1034 uint64_t frame_num = lsmash_bits_get( bits, sps->log2_max_frame_num );
1035 IF_INVALID_VALUE( frame_num >= (1 << sps->log2_max_frame_num) || (slice->IdrPicFlag && frame_num) )
1036 return -1;
1037 slice->frame_num = frame_num;
1038 if( !sps->frame_mbs_only_flag )
1040 slice->field_pic_flag = lsmash_bits_get( bits, 1 );
1041 if( slice->field_pic_flag )
1042 slice->bottom_field_flag = lsmash_bits_get( bits, 1 );
1044 if( slice->IdrPicFlag )
1046 uint64_t idr_pic_id = nalu_get_exp_golomb_ue( bits );
1047 IF_INVALID_VALUE( idr_pic_id > 65535 )
1048 return -1;
1049 slice->idr_pic_id = idr_pic_id;
1051 if( sps->pic_order_cnt_type == 0 )
1053 uint64_t pic_order_cnt_lsb = lsmash_bits_get( bits, sps->log2_max_pic_order_cnt_lsb );
1054 IF_INVALID_VALUE( pic_order_cnt_lsb >= sps->MaxPicOrderCntLsb )
1055 return -1;
1056 slice->pic_order_cnt_lsb = pic_order_cnt_lsb;
1057 if( pps->bottom_field_pic_order_in_frame_present_flag && !slice->field_pic_flag )
1058 slice->delta_pic_order_cnt_bottom = nalu_get_exp_golomb_se( bits );
1060 else if( sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag )
1062 slice->delta_pic_order_cnt[0] = nalu_get_exp_golomb_se( bits );
1063 if( pps->bottom_field_pic_order_in_frame_present_flag && !slice->field_pic_flag )
1064 slice->delta_pic_order_cnt[1] = nalu_get_exp_golomb_se( bits );
1066 if( pps->redundant_pic_cnt_present_flag )
1068 uint64_t redundant_pic_cnt = nalu_get_exp_golomb_ue( bits );
1069 IF_INVALID_VALUE( redundant_pic_cnt > 127 )
1070 return -1;
1071 slice->has_redundancy = !!redundant_pic_cnt;
1073 if( slice_type == H264_SLICE_TYPE_B )
1074 lsmash_bits_get( bits, 1 );
1075 uint64_t num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1;
1076 uint64_t num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1;
1077 if( slice_type == H264_SLICE_TYPE_P || slice_type == H264_SLICE_TYPE_SP || slice_type == H264_SLICE_TYPE_B )
1079 if( lsmash_bits_get( bits, 1 ) ) /* num_ref_idx_active_override_flag */
1081 num_ref_idx_l0_active_minus1 = nalu_get_exp_golomb_ue( bits );
1082 IF_INVALID_VALUE( num_ref_idx_l0_active_minus1 > 31 )
1083 return -1;
1084 if( slice_type == H264_SLICE_TYPE_B )
1086 num_ref_idx_l1_active_minus1 = nalu_get_exp_golomb_ue( bits );
1087 IF_INVALID_VALUE( num_ref_idx_l1_active_minus1 > 31 )
1088 return -1;
1092 if( nuh->nal_unit_type == H264_NALU_TYPE_SLICE_EXT
1093 || nuh->nal_unit_type == H264_NALU_TYPE_SLICE_EXT_DVC )
1095 return -1; /* No support of MVC yet */
1096 #if 0
1097 /* ref_pic_list_mvc_modification() */
1098 if( slice_type == H264_SLICE_TYPE_P || slice_type == H264_SLICE_TYPE_B || slice_type == H264_SLICE_TYPE_SP )
1100 for( int i = 0; i < 1 + (slice_type == H264_SLICE_TYPE_B); i++ )
1102 if( lsmash_bits_get( bits, 1 ) ) /* (S)P and B: ref_pic_list_modification_flag_l0
1103 * B: ref_pic_list_modification_flag_l1 */
1105 uint64_t modification_of_pic_nums_idc;
1108 modification_of_pic_nums_idc = nalu_get_exp_golomb_ue( bits );
1109 #if 0
1110 if( modification_of_pic_nums_idc == 0 || modification_of_pic_nums_idc == 1 )
1111 nalu_get_exp_golomb_ue( bits ); /* abs_diff_pic_num_minus1 */
1112 else if( modification_of_pic_nums_idc == 2 )
1113 nalu_get_exp_golomb_ue( bits ); /* long_term_pic_num */
1114 else if( modification_of_pic_nums_idc == 4 || modification_of_pic_nums_idc == 5 )
1115 nalu_get_exp_golomb_ue( bits ); /* abs_diff_view_idx_minus1 */
1116 #else
1117 if( modification_of_pic_nums_idc != 3 )
1118 nalu_get_exp_golomb_ue( bits ); /* abs_diff_pic_num_minus1, long_term_pic_num or abs_diff_view_idx_minus1 */
1119 #endif
1120 } while( modification_of_pic_nums_idc != 3 );
1124 #endif
1126 else
1128 /* ref_pic_list_modification() */
1129 if( slice_type == H264_SLICE_TYPE_P || slice_type == H264_SLICE_TYPE_B || slice_type == H264_SLICE_TYPE_SP )
1131 for( int i = 0; i < 1 + (slice_type == H264_SLICE_TYPE_B); i++ )
1133 if( lsmash_bits_get( bits, 1 ) ) /* (S)P and B: ref_pic_list_modification_flag_l0
1134 * B: ref_pic_list_modification_flag_l1 */
1136 uint64_t modification_of_pic_nums_idc;
1139 modification_of_pic_nums_idc = nalu_get_exp_golomb_ue( bits );
1140 #if 0
1141 if( modification_of_pic_nums_idc == 0 || modification_of_pic_nums_idc == 1 )
1142 nalu_get_exp_golomb_ue( bits ); /* abs_diff_pic_num_minus1 */
1143 else if( modification_of_pic_nums_idc == 2 )
1144 nalu_get_exp_golomb_ue( bits ); /* long_term_pic_num */
1145 #else
1146 if( modification_of_pic_nums_idc != 3 )
1147 nalu_get_exp_golomb_ue( bits ); /* abs_diff_pic_num_minus1 or long_term_pic_num */
1148 #endif
1149 } while( modification_of_pic_nums_idc != 3 );
1154 if( (pps->weighted_pred_flag && (slice_type == H264_SLICE_TYPE_P || slice_type == H264_SLICE_TYPE_SP))
1155 || (pps->weighted_bipred_idc == 1 && slice_type == H264_SLICE_TYPE_B) )
1157 /* pred_weight_table() */
1158 nalu_get_exp_golomb_ue( bits ); /* luma_log2_weight_denom */
1159 if( sps->ChromaArrayType )
1160 nalu_get_exp_golomb_ue( bits ); /* chroma_log2_weight_denom */
1161 for( uint8_t i = 0; i <= num_ref_idx_l0_active_minus1; i++ )
1163 if( lsmash_bits_get( bits, 1 ) ) /* luma_weight_l0_flag */
1165 nalu_get_exp_golomb_se( bits ); /* luma_weight_l0[i] */
1166 nalu_get_exp_golomb_se( bits ); /* luma_offset_l0[i] */
1168 if( sps->ChromaArrayType
1169 && lsmash_bits_get( bits, 1 ) /* chroma_weight_l0_flag */ )
1170 for( int j = 0; j < 2; j++ )
1172 nalu_get_exp_golomb_se( bits ); /* chroma_weight_l0[i][j]*/
1173 nalu_get_exp_golomb_se( bits ); /* chroma_offset_l0[i][j] */
1176 if( slice_type == H264_SLICE_TYPE_B )
1177 for( uint8_t i = 0; i <= num_ref_idx_l1_active_minus1; i++ )
1179 if( lsmash_bits_get( bits, 1 ) ) /* luma_weight_l1_flag */
1181 nalu_get_exp_golomb_se( bits ); /* luma_weight_l1[i] */
1182 nalu_get_exp_golomb_se( bits ); /* luma_offset_l1[i] */
1184 if( sps->ChromaArrayType
1185 && lsmash_bits_get( bits, 1 ) /* chroma_weight_l1_flag */ )
1186 for( int j = 0; j < 2; j++ )
1188 nalu_get_exp_golomb_se( bits ); /* chroma_weight_l1[i][j]*/
1189 nalu_get_exp_golomb_se( bits ); /* chroma_offset_l1[i][j] */
1193 if( nuh->nal_ref_idc )
1195 /* dec_ref_pic_marking() */
1196 if( slice->IdrPicFlag )
1198 lsmash_bits_get( bits, 1 ); /* no_output_of_prior_pics_flag */
1199 lsmash_bits_get( bits, 1 ); /* long_term_reference_flag */
1201 else if( lsmash_bits_get( bits, 1 ) ) /* adaptive_ref_pic_marking_mode_flag */
1203 uint64_t memory_management_control_operation;
1206 memory_management_control_operation = nalu_get_exp_golomb_ue( bits );
1207 if( memory_management_control_operation )
1209 if( memory_management_control_operation == 5 )
1210 slice->has_mmco5 = 1;
1211 else
1213 nalu_get_exp_golomb_ue( bits );
1214 if( memory_management_control_operation == 3 )
1215 nalu_get_exp_golomb_ue( bits );
1218 } while( memory_management_control_operation );
1221 /* We needn't read more if not slice data partition A.
1222 * Skip slice_data() and rbsp_slice_trailing_bits(). */
1223 if( nuh->nal_unit_type == H264_NALU_TYPE_SLICE_DP_A )
1225 if( pps->entropy_coding_mode_flag && slice_type != H264_SLICE_TYPE_I && slice_type != H264_SLICE_TYPE_SI )
1226 nalu_get_exp_golomb_ue( bits ); /* cabac_init_idc */
1227 nalu_get_exp_golomb_se( bits ); /* slice_qp_delta */
1228 if( slice_type == H264_SLICE_TYPE_SP || slice_type == H264_SLICE_TYPE_SI )
1230 if( slice_type == H264_SLICE_TYPE_SP )
1231 lsmash_bits_get( bits, 1 ); /* sp_for_switch_flag */
1232 nalu_get_exp_golomb_se( bits ); /* slice_qs_delta */
1234 if( pps->deblocking_filter_control_present_flag
1235 && nalu_get_exp_golomb_ue( bits ) != 1 /* disable_deblocking_filter_idc */ )
1237 int64_t slice_alpha_c0_offset_div2 = nalu_get_exp_golomb_se( bits );
1238 IF_INVALID_VALUE( slice_alpha_c0_offset_div2 < -6 || slice_alpha_c0_offset_div2 > 6 )
1239 return -1;
1240 int64_t slice_beta_offset_div2 = nalu_get_exp_golomb_se( bits );
1241 IF_INVALID_VALUE( slice_beta_offset_div2 < -6 || slice_beta_offset_div2 > 6 )
1242 return -1;
1244 if( pps->num_slice_groups_minus1
1245 && (pps->slice_group_map_type == 3 || pps->slice_group_map_type == 4 || pps->slice_group_map_type == 5) )
1247 uint64_t temp = ((uint64_t)sps->PicSizeInMapUnits - 1) / pps->SliceGroupChangeRate + 1;
1248 uint64_t slice_group_change_cycle = lsmash_bits_get( bits, lsmash_ceil_log2( temp + 1 ) );
1249 IF_INVALID_VALUE( slice_group_change_cycle > temp )
1250 return -1;
1252 /* end of slice_header() */
1253 slice->slice_id = nalu_get_exp_golomb_ue( bits );
1254 h264_slice_info_t *slice_part = h264_get_slice_info( info->slice_list, slice->slice_id );
1255 if( !slice_part )
1256 return -1;
1257 *slice_part = *slice;
1259 lsmash_bits_empty( bits );
1260 if( bits->bs->error )
1261 return -1;
1262 info->sps = *sps;
1263 info->pps = *pps;
1264 return 0;
1267 int h264_parse_slice
1269 h264_info_t *info,
1270 h264_nalu_header_t *nuh,
1271 uint8_t *rbsp_buffer,
1272 uint8_t *ebsp,
1273 uint64_t ebsp_size
1276 lsmash_bits_t *bits = info->bits;
1277 uint64_t size = nuh->nal_unit_type == H264_NALU_TYPE_SLICE_IDR || nuh->nal_ref_idc == 0
1278 ? LSMASH_MIN( ebsp_size, 100 )
1279 : LSMASH_MIN( ebsp_size, 1000 );
1280 if( nalu_import_rbsp_from_ebsp( bits, rbsp_buffer, ebsp, size ) )
1281 return -1;
1282 if( nuh->nal_unit_type != H264_NALU_TYPE_SLICE_DP_B
1283 && nuh->nal_unit_type != H264_NALU_TYPE_SLICE_DP_C )
1284 return h264_parse_slice_header( info, nuh );
1285 /* slice_data_partition_b_layer_rbsp() or slice_data_partition_c_layer_rbsp() */
1286 uint64_t slice_id = nalu_get_exp_golomb_ue( bits );
1287 h264_slice_info_t *slice = h264_get_slice_info( info->slice_list, slice_id );
1288 if( !slice )
1289 return -1;
1290 h264_pps_t *pps = h264_get_pps( info->pps_list, slice->pic_parameter_set_id );
1291 if( !pps )
1292 return -1;
1293 h264_sps_t *sps = h264_get_sps( info->sps_list, pps->seq_parameter_set_id );
1294 if( !sps )
1295 return -1;
1296 slice->seq_parameter_set_id = pps->seq_parameter_set_id;
1297 if( sps->separate_colour_plane_flag )
1298 lsmash_bits_get( bits, 2 ); /* colour_plane_id */
1299 if( pps->redundant_pic_cnt_present_flag )
1301 uint64_t redundant_pic_cnt = nalu_get_exp_golomb_ue( bits );
1302 IF_INVALID_VALUE( redundant_pic_cnt > 127 )
1303 return -1;
1304 slice->has_redundancy = !!redundant_pic_cnt;
1306 /* Skip slice_data() and rbsp_slice_trailing_bits(). */
1307 lsmash_bits_empty( bits );
1308 if( bits->bs->error )
1309 return -1;
1310 info->sps = *sps;
1311 info->pps = *pps;
1312 return 0;
1315 static int h264_get_sps_id
1317 uint8_t *ps_ebsp,
1318 uint32_t ps_ebsp_length,
1319 uint8_t *ps_id
1322 /* max number of bits of sps_id = 11: 0b000001XXXXX
1323 * (24 + 11 - 1) / 8 + 1 = 5 bytes
1324 * Why +1? Because there might be an emulation_prevention_three_byte. */
1325 lsmash_bits_t bits = { 0 };
1326 lsmash_bs_t bs = { 0 };
1327 uint8_t rbsp_buffer[6];
1328 uint8_t buffer [6];
1329 bs.buffer.data = buffer;
1330 bs.buffer.alloc = 6;
1331 lsmash_bits_init( &bits, &bs );
1332 if( nalu_import_rbsp_from_ebsp( &bits, rbsp_buffer, ps_ebsp, LSMASH_MIN( ps_ebsp_length, 6 ) ) )
1333 return -1;
1334 lsmash_bits_get( &bits, 24 ); /* profile_idc, constraint_set_flags and level_idc */
1335 uint64_t sec_parameter_set_id = nalu_get_exp_golomb_ue( &bits );
1336 IF_INVALID_VALUE( sec_parameter_set_id > 31 )
1337 return -1;
1338 *ps_id = sec_parameter_set_id;
1339 return bs.error ? -1 : 0;
1342 static int h264_get_pps_id
1344 uint8_t *ps_ebsp,
1345 uint32_t ps_ebsp_length,
1346 uint8_t *ps_id
1349 /* max number of bits of pps_id = 17: 0b000000001XXXXXXXX
1350 * (17 - 1) / 8 + 1 = 3 bytes
1351 * Why +1? Because there might be an emulation_prevention_three_byte. */
1352 lsmash_bits_t bits = { 0 };
1353 lsmash_bs_t bs = { 0 };
1354 uint8_t rbsp_buffer[4];
1355 uint8_t buffer [4];
1356 bs.buffer.data = buffer;
1357 bs.buffer.alloc = 4;
1358 lsmash_bits_init( &bits, &bs );
1359 if( nalu_import_rbsp_from_ebsp( &bits, rbsp_buffer, ps_ebsp, LSMASH_MIN( ps_ebsp_length, 4 ) ) )
1360 return -1;
1361 uint64_t pic_parameter_set_id = nalu_get_exp_golomb_ue( &bits );
1362 IF_INVALID_VALUE( pic_parameter_set_id > 255 )
1363 return -1;
1364 *ps_id = pic_parameter_set_id;
1365 return bs.error ? -1 : 0;
1368 static inline int h264_get_ps_id
1370 uint8_t *ps_ebsp,
1371 uint32_t ps_ebsp_length,
1372 uint8_t *ps_id,
1373 lsmash_h264_parameter_set_type ps_type
1376 int (*get_ps_id)( uint8_t *ps_ebsp, uint32_t ps_ebsp_length, uint8_t *ps_id )
1377 = ps_type == H264_PARAMETER_SET_TYPE_SPS ? h264_get_sps_id
1378 : ps_type == H264_PARAMETER_SET_TYPE_PPS ? h264_get_pps_id
1379 : NULL;
1380 return get_ps_id ? get_ps_id( ps_ebsp, ps_ebsp_length, ps_id ) : -1;
1383 static inline lsmash_entry_list_t *h264_get_parameter_set_list
1385 lsmash_h264_specific_parameters_t *param,
1386 lsmash_h264_parameter_set_type ps_type
1389 if( !param->parameter_sets )
1390 return NULL;
1391 return ps_type == H264_PARAMETER_SET_TYPE_SPS ? param->parameter_sets->sps_list
1392 : ps_type == H264_PARAMETER_SET_TYPE_PPS ? param->parameter_sets->pps_list
1393 : ps_type == H264_PARAMETER_SET_TYPE_SPSEXT ? param->parameter_sets->spsext_list
1394 : NULL;
1397 static lsmash_entry_t *h264_get_ps_entry_from_param
1399 lsmash_h264_specific_parameters_t *param,
1400 lsmash_h264_parameter_set_type ps_type,
1401 uint8_t ps_id
1404 int (*get_ps_id)( uint8_t *ps_ebsp, uint32_t ps_ebsp_length, uint8_t *ps_id )
1405 = ps_type == H264_PARAMETER_SET_TYPE_SPS ? h264_get_sps_id
1406 : ps_type == H264_PARAMETER_SET_TYPE_PPS ? h264_get_pps_id
1407 : NULL;
1408 if( !get_ps_id )
1409 return NULL;
1410 lsmash_entry_list_t *ps_list = h264_get_parameter_set_list( param, ps_type );
1411 if( !ps_list )
1412 return NULL;
1413 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
1415 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1416 if( !ps )
1417 return NULL;
1418 uint8_t param_ps_id;
1419 if( get_ps_id( ps->nalUnit + 1, ps->nalUnitLength - 1, &param_ps_id ) )
1420 return NULL;
1421 if( ps_id == param_ps_id )
1422 return entry;
1424 return NULL;
1427 static inline void h264_update_picture_type
1429 h264_picture_info_t *picture,
1430 h264_slice_info_t *slice
1433 if( picture->type == H264_PICTURE_TYPE_I_P )
1435 if( slice->type == H264_SLICE_TYPE_B )
1436 picture->type = H264_PICTURE_TYPE_I_P_B;
1437 else if( slice->type == H264_SLICE_TYPE_SI || slice->type == H264_SLICE_TYPE_SP )
1438 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1440 else if( picture->type == H264_PICTURE_TYPE_I_P_B )
1442 if( slice->type != H264_SLICE_TYPE_P && slice->type != H264_SLICE_TYPE_B && slice->type != H264_SLICE_TYPE_I )
1443 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1445 else if( picture->type == H264_PICTURE_TYPE_I )
1447 if( slice->type == H264_SLICE_TYPE_P )
1448 picture->type = H264_PICTURE_TYPE_I_P;
1449 else if( slice->type == H264_SLICE_TYPE_B )
1450 picture->type = H264_PICTURE_TYPE_I_P_B;
1451 else if( slice->type == H264_SLICE_TYPE_SI )
1452 picture->type = H264_PICTURE_TYPE_I_SI;
1453 else if( slice->type == H264_SLICE_TYPE_SP )
1454 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1456 else if( picture->type == H264_PICTURE_TYPE_SI_SP )
1458 if( slice->type == H264_SLICE_TYPE_P || slice->type == H264_SLICE_TYPE_I )
1459 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1460 else if( slice->type == H264_SLICE_TYPE_B )
1461 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1463 else if( picture->type == H264_PICTURE_TYPE_SI )
1465 if( slice->type == H264_SLICE_TYPE_P )
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;
1469 else if( slice->type != H264_SLICE_TYPE_I )
1470 picture->type = H264_PICTURE_TYPE_I_SI;
1471 else if( slice->type == H264_SLICE_TYPE_SP )
1472 picture->type = H264_PICTURE_TYPE_SI_SP;
1474 else if( picture->type == H264_PICTURE_TYPE_I_SI )
1476 if( slice->type == H264_SLICE_TYPE_P || slice->type == H264_SLICE_TYPE_SP )
1477 picture->type = H264_PICTURE_TYPE_I_SI_P_SP;
1478 else if( slice->type == H264_SLICE_TYPE_B )
1479 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1481 else if( picture->type == H264_PICTURE_TYPE_I_SI_P_SP )
1483 if( slice->type == H264_SLICE_TYPE_B )
1484 picture->type = H264_PICTURE_TYPE_I_SI_P_SP_B;
1486 else if( picture->type == H264_PICTURE_TYPE_NONE )
1488 if( slice->type == H264_SLICE_TYPE_P )
1489 picture->type = H264_PICTURE_TYPE_I_P;
1490 else if( slice->type == H264_SLICE_TYPE_B )
1491 picture->type = H264_PICTURE_TYPE_I_P_B;
1492 else if( slice->type == H264_SLICE_TYPE_I )
1493 picture->type = H264_PICTURE_TYPE_I;
1494 else if( slice->type == H264_SLICE_TYPE_SI )
1495 picture->type = H264_PICTURE_TYPE_SI;
1496 else if( slice->type == H264_SLICE_TYPE_SP )
1497 picture->type = H264_PICTURE_TYPE_SI_SP;
1499 #if 0
1500 fprintf( stderr, "Picture type = %s\n", picture->type == H264_PICTURE_TYPE_I_P ? "P"
1501 : picture->type == H264_PICTURE_TYPE_I_P_B ? "B"
1502 : picture->type == H264_PICTURE_TYPE_I ? "I"
1503 : picture->type == H264_PICTURE_TYPE_SI ? "SI"
1504 : picture->type == H264_PICTURE_TYPE_I_SI ? "SI"
1505 : "SP" );
1506 #endif
1509 /* Shall be called at least once per picture. */
1510 void h264_update_picture_info_for_slice
1512 h264_info_t *info,
1513 h264_picture_info_t *picture,
1514 h264_slice_info_t *slice
1517 assert( info );
1518 picture->has_mmco5 |= slice->has_mmco5;
1519 picture->has_redundancy |= slice->has_redundancy;
1520 picture->has_primary |= !slice->has_redundancy;
1521 h264_update_picture_type( picture, slice );
1522 /* Mark 'used' on active parameter sets. */
1523 uint8_t ps_id[2] = { slice->seq_parameter_set_id, slice->pic_parameter_set_id };
1524 for( int i = 0; i < 2; i++ )
1526 lsmash_h264_parameter_set_type ps_type = (lsmash_h264_parameter_set_type)i;
1527 lsmash_entry_t *entry = h264_get_ps_entry_from_param( &info->avcC_param, ps_type, ps_id[i] );
1528 if( entry && entry->data )
1530 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1531 if( ps->unused )
1532 lsmash_append_h264_parameter_set( &info->avcC_param, ps_type, ps->nalUnit, ps->nalUnitLength );
1535 /* Discard this slice info. */
1536 slice->present = 0;
1539 /* Shall be called exactly once per picture. */
1540 void h264_update_picture_info
1542 h264_info_t *info,
1543 h264_picture_info_t *picture,
1544 h264_slice_info_t *slice,
1545 h264_sei_t *sei
1548 picture->frame_num = slice->frame_num;
1549 picture->pic_order_cnt_lsb = slice->pic_order_cnt_lsb;
1550 picture->delta_pic_order_cnt_bottom = slice->delta_pic_order_cnt_bottom;
1551 picture->delta_pic_order_cnt[0] = slice->delta_pic_order_cnt[0];
1552 picture->delta_pic_order_cnt[1] = slice->delta_pic_order_cnt[1];
1553 picture->field_pic_flag = slice->field_pic_flag;
1554 picture->bottom_field_flag = slice->bottom_field_flag;
1555 picture->idr = slice->IdrPicFlag;
1556 picture->pic_parameter_set_id = slice->pic_parameter_set_id;
1557 picture->disposable = (slice->nal_ref_idc == 0);
1558 picture->random_accessible = slice->IdrPicFlag;
1559 h264_update_picture_info_for_slice( info, picture, slice );
1560 picture->independent = picture->type == H264_PICTURE_TYPE_I || picture->type == H264_PICTURE_TYPE_I_SI;
1561 if( sei->pic_timing.present )
1563 if( sei->pic_timing.pic_struct < 9 )
1565 static const uint8_t DeltaTfiDivisor[9] = { 2, 1, 1, 2, 2, 3, 3, 4, 6 };
1566 picture->delta = DeltaTfiDivisor[ sei->pic_timing.pic_struct ];
1568 else
1569 /* Reserved values in the spec we refer to. */
1570 picture->delta = picture->field_pic_flag ? 1 : 2;
1571 sei->pic_timing.present = 0;
1573 else
1574 picture->delta = picture->field_pic_flag ? 1 : 2;
1575 if( sei->recovery_point.present )
1577 picture->random_accessible |= sei->recovery_point.random_accessible;
1578 picture->broken_link_flag |= sei->recovery_point.broken_link_flag;
1579 picture->recovery_frame_cnt = sei->recovery_point.recovery_frame_cnt;
1580 sei->recovery_point.present = 0;
1584 int h264_find_au_delimit_by_slice_info
1586 h264_slice_info_t *slice,
1587 h264_slice_info_t *prev_slice
1590 if( slice->frame_num != prev_slice->frame_num
1591 || ((slice->pic_order_cnt_type == 0 && prev_slice->pic_order_cnt_type == 0)
1592 && (slice->pic_order_cnt_lsb != prev_slice->pic_order_cnt_lsb
1593 || slice->delta_pic_order_cnt_bottom != prev_slice->delta_pic_order_cnt_bottom))
1594 || ((slice->pic_order_cnt_type == 1 && prev_slice->pic_order_cnt_type == 1)
1595 && (slice->delta_pic_order_cnt[0] != prev_slice->delta_pic_order_cnt[0]
1596 || slice->delta_pic_order_cnt[1] != prev_slice->delta_pic_order_cnt[1]))
1597 || slice->field_pic_flag != prev_slice->field_pic_flag
1598 || slice->bottom_field_flag != prev_slice->bottom_field_flag
1599 || slice->IdrPicFlag != prev_slice->IdrPicFlag
1600 || slice->pic_parameter_set_id != prev_slice->pic_parameter_set_id
1601 || ((slice->nal_ref_idc == 0 || prev_slice->nal_ref_idc == 0)
1602 && (slice->nal_ref_idc != prev_slice->nal_ref_idc))
1603 || (slice->IdrPicFlag == 1 && prev_slice->IdrPicFlag == 1
1604 && slice->idr_pic_id != prev_slice->idr_pic_id) )
1605 return 1;
1606 return 0;
1609 int h264_find_au_delimit_by_nalu_type
1611 uint8_t nalu_type,
1612 uint8_t prev_nalu_type
1615 return ((nalu_type >= H264_NALU_TYPE_SEI && nalu_type <= H264_NALU_TYPE_AUD)
1616 || (nalu_type >= H264_NALU_TYPE_PREFIX && nalu_type <= H264_NALU_TYPE_RSV_NVCL18))
1617 && ((prev_nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && prev_nalu_type <= H264_NALU_TYPE_SLICE_IDR)
1618 || prev_nalu_type == H264_NALU_TYPE_FD || prev_nalu_type == H264_NALU_TYPE_SLICE_AUX);
1621 int h264_supplement_buffer
1623 h264_stream_buffer_t *sb,
1624 h264_access_unit_t *au,
1625 uint32_t size
1628 lsmash_multiple_buffers_t *bank = lsmash_resize_multiple_buffers( sb->bank, size );
1629 if( !bank )
1630 return -1;
1631 sb->bank = bank;
1632 sb->rbsp = lsmash_withdraw_buffer( bank, 1 );
1633 if( au && bank->number_of_buffers == 3 )
1635 au->data = lsmash_withdraw_buffer( bank, 2 );
1636 au->incomplete_data = lsmash_withdraw_buffer( bank, 3 );
1638 return 0;
1641 static void h264_bs_put_parameter_sets
1643 lsmash_bs_t *bs,
1644 lsmash_entry_list_t *ps_list,
1645 uint32_t max_ps_count
1648 uint32_t ps_count = 0;
1649 for( lsmash_entry_t *entry = ps_list->head; entry && ps_count < max_ps_count; entry = entry->next )
1651 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1652 if( ps && !ps->unused )
1654 lsmash_bs_put_be16( bs, ps->nalUnitLength );
1655 lsmash_bs_put_bytes( bs, ps->nalUnitLength, ps->nalUnit );
1657 else
1658 continue;
1659 ++ps_count;
1663 uint8_t *lsmash_create_h264_specific_info
1665 lsmash_h264_specific_parameters_t *param,
1666 uint32_t *data_length
1669 if( !param || !param->parameter_sets || !data_length )
1670 return NULL;
1671 if( param->lengthSizeMinusOne != 0 && param->lengthSizeMinusOne != 1 && param->lengthSizeMinusOne != 3 )
1672 return NULL;
1673 static const uint32_t max_ps_count[3] = { 31, 255, 255 };
1674 lsmash_entry_list_t *ps_list[3] =
1676 param->parameter_sets->sps_list, /* SPS */
1677 param->parameter_sets->pps_list, /* PPS */
1678 param->parameter_sets->spsext_list /* SPSExt */
1680 uint32_t ps_count[3] = { 0, 0, 0 };
1681 /* SPS and PPS are mandatory. */
1682 if( !ps_list[0] || !ps_list[0]->head || ps_list[0]->entry_count == 0
1683 || !ps_list[1] || !ps_list[1]->head || ps_list[1]->entry_count == 0 )
1684 return NULL;
1685 /* Calculate enough buffer size. */
1686 uint32_t buffer_size = ISOM_BASEBOX_COMMON_SIZE + 11;
1687 for( int i = 0; i < 3; i++ )
1688 if( ps_list[i] )
1689 for( lsmash_entry_t *entry = ps_list[i]->head; entry && ps_count[i] < max_ps_count[i]; entry = entry->next )
1691 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1692 if( !ps )
1693 return NULL;
1694 if( ps->unused )
1695 continue;
1696 buffer_size += 2 + ps->nalUnitLength;
1697 ++ps_count[i];
1699 /* Set up bytestream writer. */
1700 uint8_t buffer[buffer_size];
1701 lsmash_bs_t bs = { 0 };
1702 bs.buffer.data = buffer;
1703 bs.buffer.alloc = buffer_size;
1704 /* Create an AVCConfigurationBox */
1705 lsmash_bs_put_be32( &bs, 0 ); /* box size */
1706 lsmash_bs_put_be32( &bs, ISOM_BOX_TYPE_AVCC.fourcc ); /* box type: 'avcC' */
1707 lsmash_bs_put_byte( &bs, 1 ); /* configurationVersion */
1708 lsmash_bs_put_byte( &bs, param->AVCProfileIndication ); /* AVCProfileIndication */
1709 lsmash_bs_put_byte( &bs, param->profile_compatibility ); /* profile_compatibility */
1710 lsmash_bs_put_byte( &bs, param->AVCLevelIndication ); /* AVCLevelIndication */
1711 lsmash_bs_put_byte( &bs, param->lengthSizeMinusOne | 0xfc ); /* lengthSizeMinusOne */
1712 lsmash_bs_put_byte( &bs, ps_count[0] | 0xe0 ); /* numOfSequenceParameterSets */
1713 h264_bs_put_parameter_sets( &bs, ps_list[0], ps_count[0] ); /* sequenceParameterSetLength
1714 * sequenceParameterSetNALUnit */
1715 lsmash_bs_put_byte( &bs, ps_count[1] ); /* numOfPictureParameterSets */
1716 h264_bs_put_parameter_sets( &bs, ps_list[1], ps_count[1] ); /* pictureParameterSetLength
1717 * pictureParameterSetNALUnit */
1718 if( H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication ) )
1720 lsmash_bs_put_byte( &bs, param->chroma_format | 0xfc ); /* chroma_format */
1721 lsmash_bs_put_byte( &bs, param->bit_depth_luma_minus8 | 0xf8 ); /* bit_depth_luma_minus8 */
1722 lsmash_bs_put_byte( &bs, param->bit_depth_chroma_minus8 | 0xf8 ); /* bit_depth_chroma_minus8 */
1723 if( ps_list[2] )
1725 lsmash_bs_put_byte( &bs, ps_count[2] ); /* numOfSequenceParameterSetExt */
1726 h264_bs_put_parameter_sets( &bs, ps_list[2], ps_count[2] ); /* sequenceParameterSetExtLength
1727 * sequenceParameterSetExtNALUnit */
1729 else /* no sequence parameter set extensions */
1730 lsmash_bs_put_byte( &bs, 0 ); /* numOfSequenceParameterSetExt */
1732 uint8_t *data = lsmash_bs_export_data( &bs, data_length );
1733 /* Update box size. */
1734 LSMASH_SET_BE32( data, *data_length );
1735 return data;
1738 static inline int h264_validate_ps_type
1740 lsmash_h264_parameter_set_type ps_type,
1741 void *ps_data,
1742 uint32_t ps_length
1745 if( !ps_data || ps_length < 2 )
1746 return -1;
1747 if( ps_type != H264_PARAMETER_SET_TYPE_SPS
1748 && ps_type != H264_PARAMETER_SET_TYPE_PPS
1749 && ps_type != H264_PARAMETER_SET_TYPE_SPSEXT )
1750 return -1;
1751 uint8_t nalu_type = *((uint8_t *)ps_data) & 0x1f;
1752 if( nalu_type != H264_NALU_TYPE_SPS
1753 && nalu_type != H264_NALU_TYPE_PPS
1754 && nalu_type != H264_NALU_TYPE_SPS_EXT )
1755 return -1;
1756 if( (ps_type == H264_PARAMETER_SET_TYPE_SPS && nalu_type != H264_NALU_TYPE_SPS)
1757 || (ps_type == H264_PARAMETER_SET_TYPE_PPS && nalu_type != H264_NALU_TYPE_PPS)
1758 || (ps_type == H264_PARAMETER_SET_TYPE_SPSEXT && nalu_type != H264_NALU_TYPE_SPS_EXT) )
1759 return -1;
1760 return 0;
1763 lsmash_dcr_nalu_appendable lsmash_check_h264_parameter_set_appendable
1765 lsmash_h264_specific_parameters_t *param,
1766 lsmash_h264_parameter_set_type ps_type,
1767 void *_ps_data,
1768 uint32_t ps_length
1771 uint8_t *ps_data = _ps_data;
1772 if( !param )
1773 return DCR_NALU_APPEND_ERROR;
1774 if( h264_validate_ps_type( ps_type, ps_data, ps_length ) )
1775 return DCR_NALU_APPEND_ERROR;
1776 if( ps_type == H264_PARAMETER_SET_TYPE_SPSEXT
1777 && !H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication ) )
1778 return DCR_NALU_APPEND_ERROR;
1779 /* Check whether the same parameter set already exsits or not. */
1780 lsmash_entry_list_t *ps_list = h264_get_parameter_set_list( param, ps_type );
1781 if( !ps_list || !ps_list->head )
1782 return DCR_NALU_APPEND_POSSIBLE; /* No parameter set */
1783 switch( nalu_check_same_ps_existence( ps_list, ps_data, ps_length ) )
1785 case 0 : break;
1786 case 1 : return DCR_NALU_APPEND_DUPLICATED; /* The same parameter set already exists. */
1787 default : return DCR_NALU_APPEND_ERROR; /* An error occured. */
1789 uint32_t max_ps_length;
1790 if( nalu_get_max_ps_length( ps_list, &max_ps_length ) )
1791 return DCR_NALU_APPEND_ERROR;
1792 max_ps_length = LSMASH_MAX( max_ps_length, ps_length );
1793 uint32_t ps_count;
1794 if( nalu_get_ps_count( ps_list, &ps_count ) )
1795 return DCR_NALU_APPEND_ERROR;
1796 if( (ps_type == H264_PARAMETER_SET_TYPE_SPS && ps_count >= 31)
1797 || (ps_type == H264_PARAMETER_SET_TYPE_PPS && ps_count >= 255)
1798 || (ps_type == H264_PARAMETER_SET_TYPE_SPSEXT && ps_count >= 255) )
1799 return DCR_NALU_APPEND_NEW_DCR_REQUIRED; /* No more appendable parameter sets. */
1800 if( ps_type == H264_PARAMETER_SET_TYPE_SPSEXT )
1801 return DCR_NALU_APPEND_POSSIBLE;
1802 /* Check whether a new specific info is needed or not. */
1803 lsmash_bits_t bits = { 0 };
1804 lsmash_bs_t bs = { 0 };
1805 uint8_t rbsp_buffer[max_ps_length];
1806 uint8_t buffer [max_ps_length];
1807 bs.buffer.data = buffer;
1808 bs.buffer.alloc = max_ps_length;
1809 lsmash_bits_init( &bits, &bs );
1810 if( ps_type == H264_PARAMETER_SET_TYPE_PPS )
1812 /* PPS */
1813 uint8_t pps_id;
1814 if( h264_get_pps_id( ps_data + 1, ps_length - 1, &pps_id ) )
1815 return DCR_NALU_APPEND_ERROR;
1816 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
1818 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1819 if( !ps )
1820 return DCR_NALU_APPEND_ERROR;
1821 if( ps->unused )
1822 continue;
1823 uint8_t param_pps_id;
1824 if( h264_get_pps_id( ps->nalUnit + 1, ps->nalUnitLength - 1, &param_pps_id ) )
1825 return DCR_NALU_APPEND_ERROR;
1826 if( pps_id == param_pps_id )
1827 /* PPS that has the same pic_parameter_set_id already exists with different form. */
1828 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
1830 return DCR_NALU_APPEND_POSSIBLE;
1832 /* SPS */
1833 h264_sps_t sps;
1834 if( h264_parse_sps_minimally( &bits, &sps, rbsp_buffer, ps_data + 1, ps_length - 1 ) )
1835 return DCR_NALU_APPEND_ERROR;
1836 lsmash_bits_empty( &bits );
1837 /* FIXME; If the sequence parameter sets are marked with different profiles,
1838 * and the relevant profile compatibility flags are all zero,
1839 * then the stream may need examination to determine which profile, if any, the stream conforms to.
1840 * If the stream is not examined, or the examination reveals that there is no profile to which the stream conforms,
1841 * then the stream must be split into two or more sub-streams with separate configuration records in which these rules can be met. */
1842 #if 0
1843 if( sps.profile_idc != param->AVCProfileIndication && (sps->constraint_set_flags & param->profile_compatibility) )
1844 #else
1845 if( sps.profile_idc != param->AVCProfileIndication )
1846 #endif
1847 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
1848 /* The values of chroma_format_idc, bit_depth_luma_minus8 and bit_depth_chroma_minus8
1849 * must be identical in all SPSs in a single AVC configuration record. */
1850 if( H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication )
1851 && (sps.chroma_format_idc != param->chroma_format
1852 || sps.bit_depth_luma_minus8 != param->bit_depth_luma_minus8
1853 || sps.bit_depth_chroma_minus8 != param->bit_depth_chroma_minus8) )
1854 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
1855 /* Forbidden to duplicate SPS that has the same seq_parameter_set_id with different form within the same configuration record. */
1856 uint8_t sps_id = sps.seq_parameter_set_id;
1857 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
1859 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
1860 if( !ps )
1861 return DCR_NALU_APPEND_ERROR;
1862 if( ps->unused )
1863 continue;
1864 uint8_t param_sps_id;
1865 if( h264_get_sps_id( ps->nalUnit + 1, ps->nalUnitLength - 1, &param_sps_id ) )
1866 return DCR_NALU_APPEND_ERROR;
1867 if( sps_id == param_sps_id )
1868 /* SPS that has the same seq_parameter_set_id already exists with different form. */
1869 return DCR_NALU_APPEND_NEW_DCR_REQUIRED;
1870 if( entry == ps_list->head )
1872 /* Check if the visual presentation sizes are different. */
1873 h264_sps_t first_sps;
1874 if( h264_parse_sps_minimally( &bits, &first_sps, rbsp_buffer,
1875 ps->nalUnit + 1,
1876 ps->nalUnitLength - 1 ) )
1877 return DCR_NALU_APPEND_ERROR;
1878 if( sps.cropped_width != first_sps.cropped_width
1879 || sps.cropped_height != first_sps.cropped_height )
1880 return DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED;
1883 return DCR_NALU_APPEND_POSSIBLE;
1886 static inline void h264_reorder_parameter_set_ascending_id
1888 lsmash_h264_specific_parameters_t *param,
1889 lsmash_h264_parameter_set_type ps_type,
1890 lsmash_entry_list_t *ps_list,
1891 uint8_t ps_id
1894 lsmash_entry_t *entry = NULL;
1895 if( ps_id )
1896 for( int i = ps_id - 1; i; i-- )
1898 entry = h264_get_ps_entry_from_param( param, ps_type, i );
1899 if( entry )
1900 break;
1902 int append_head = 0;
1903 if( !entry )
1905 /* Couldn't find any parameter set with lower identifier.
1906 * Next, find parameter set with upper identifier. */
1907 int max_ps_id = ps_type == H264_PARAMETER_SET_TYPE_SPS ? 31 : 255;
1908 for( int i = ps_id + 1; i <= max_ps_id; i++ )
1910 entry = h264_get_ps_entry_from_param( param, ps_type, i );
1911 if( entry )
1912 break;
1914 if( entry )
1915 append_head = 1;
1917 if( !entry )
1918 return; /* The new entry was appended to the tail. */
1919 lsmash_entry_t *new_entry = ps_list->tail;
1920 if( append_head )
1922 /* before: entry[i > ps_id] ... -> prev_entry -> new_entry[ps_id]
1923 * after: new_entry[ps_id] -> entry[i > ps_id] -> ... -> prev_entry */
1924 if( new_entry->prev )
1925 new_entry->prev->next = NULL;
1926 new_entry->prev = NULL;
1927 entry->prev = new_entry;
1928 new_entry->next = entry;
1929 return;
1931 /* before: entry[i < ps_id] -> next_entry -> ... -> prev_entry -> new_entry[ps_id]
1932 * after: entry[i < ps_id] -> new_entry[ps_id] -> next_entry -> ... -> prev_entry */
1933 if( new_entry->prev )
1934 new_entry->prev->next = NULL;
1935 new_entry->prev = entry;
1936 new_entry->next = entry->next;
1937 if( entry->next )
1938 entry->next->prev = new_entry;
1939 entry->next = new_entry;
1942 int lsmash_append_h264_parameter_set
1944 lsmash_h264_specific_parameters_t *param,
1945 lsmash_h264_parameter_set_type ps_type,
1946 void *_ps_data,
1947 uint32_t ps_length
1950 uint8_t *ps_data = _ps_data;
1951 if( !param || !ps_data || ps_length < 2 )
1952 return -1;
1953 if( !param->parameter_sets )
1955 param->parameter_sets = lsmash_malloc_zero( sizeof(lsmash_h264_parameter_sets_t) );
1956 if( !param->parameter_sets )
1957 return -1;
1959 lsmash_entry_list_t *ps_list = h264_get_parameter_set_list( param, ps_type );
1960 if( !ps_list )
1961 return -1;
1962 if( ps_type != H264_PARAMETER_SET_TYPE_SPS
1963 && ps_type != H264_PARAMETER_SET_TYPE_PPS
1964 && ps_type != H264_PARAMETER_SET_TYPE_SPSEXT )
1965 return -1;
1966 if( ps_type == H264_PARAMETER_SET_TYPE_SPSEXT )
1968 if( !H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication ) )
1969 return 0;
1970 isom_dcr_ps_entry_t *ps = isom_create_ps_entry( ps_data, ps_length );
1971 if( !ps )
1972 return -1;
1973 if( lsmash_add_entry( ps_list, ps ) )
1975 isom_remove_dcr_ps( ps );
1976 return -1;
1978 return 0;
1980 /* Check if the same parameter set identifier already exists. */
1981 uint8_t ps_id;
1982 if( h264_get_ps_id( ps_data + 1, ps_length - 1, &ps_id, ps_type ) )
1983 return -1;
1984 lsmash_entry_t *entry = h264_get_ps_entry_from_param( param, ps_type, ps_id );
1985 isom_dcr_ps_entry_t *ps = entry ? (isom_dcr_ps_entry_t *)entry->data : NULL;
1986 if( ps && !ps->unused )
1987 /* The same parameter set identifier already exists. */
1988 return -1;
1989 int invoke_reorder;
1990 if( ps )
1992 /* Reuse an already existed parameter set in the list. */
1993 ps->unused = 0;
1994 if( ps->nalUnit != ps_data )
1996 /* The same address could be given when called by h264_update_picture_info_for_slice(). */
1997 lsmash_free( ps->nalUnit );
1998 ps->nalUnit = ps_data;
2000 ps->nalUnitLength = ps_length;
2001 invoke_reorder = 0;
2003 else
2005 /* Create a new parameter set and append it into the list. */
2006 ps = isom_create_ps_entry( ps_data, ps_length );
2007 if( !ps )
2008 return -1;
2009 if( lsmash_add_entry( ps_list, ps ) )
2011 isom_remove_dcr_ps( ps );
2012 return -1;
2014 invoke_reorder = 1;
2016 if( ps_type == H264_PARAMETER_SET_TYPE_SPS )
2018 /* Update specific info with SPS. */
2019 lsmash_bits_t bits = { 0 };
2020 lsmash_bs_t bs = { 0 };
2021 uint8_t rbsp_buffer[ps_length];
2022 uint8_t buffer [ps_length];
2023 bs.buffer.data = buffer;
2024 bs.buffer.alloc = ps_length;
2025 lsmash_bits_init( &bits, &bs );
2026 h264_sps_t sps;
2027 if( h264_parse_sps_minimally( &bits, &sps, rbsp_buffer, ps_data + 1, ps_length - 1 ) )
2029 lsmash_remove_entry_tail( ps_list, isom_remove_dcr_ps );
2030 return -1;
2032 if( ps_list->entry_count == 1 )
2033 param->profile_compatibility = 0xff;
2034 param->AVCProfileIndication = sps.profile_idc;
2035 param->profile_compatibility &= sps.constraint_set_flags;
2036 param->AVCLevelIndication = LSMASH_MAX( param->AVCLevelIndication, sps.level_idc );
2037 param->chroma_format = sps.chroma_format_idc;
2038 param->bit_depth_luma_minus8 = sps.bit_depth_luma_minus8;
2039 param->bit_depth_chroma_minus8 = sps.bit_depth_chroma_minus8;
2041 if( invoke_reorder )
2042 /* Add a new parameter set in order of ascending parameter set identifier. */
2043 h264_reorder_parameter_set_ascending_id( param, ps_type, ps_list, ps_id );
2044 return 0;
2047 int h264_try_to_append_parameter_set
2049 h264_info_t *info,
2050 lsmash_h264_parameter_set_type ps_type,
2051 void *_ps_data,
2052 uint32_t ps_length
2055 uint8_t *ps_data = _ps_data;
2056 lsmash_dcr_nalu_appendable ret = lsmash_check_h264_parameter_set_appendable( &info->avcC_param, ps_type, ps_data, ps_length );
2057 lsmash_h264_specific_parameters_t *param;
2058 switch( ret )
2060 case DCR_NALU_APPEND_ERROR : /* Error */
2061 return -1;
2062 case DCR_NALU_APPEND_NEW_DCR_REQUIRED : /* Mulitiple sample description is needed. */
2063 case DCR_NALU_APPEND_NEW_SAMPLE_ENTRY_REQUIRED : /* Mulitiple sample description is needed. */
2064 param = &info->avcC_param_next;
2065 info->avcC_pending = 1;
2066 break;
2067 case DCR_NALU_APPEND_POSSIBLE : /* Appendable */
2068 param = info->avcC_pending ? &info->avcC_param_next : &info->avcC_param;
2069 break;
2070 default : /* No need to append */
2071 return 0;
2073 switch( ps_type )
2075 case H264_PARAMETER_SET_TYPE_SPS :
2076 if( h264_parse_sps( info, info->buffer.rbsp, ps_data + 1, ps_length - 1 ) < 0 )
2077 return -1;
2078 break;
2079 case H264_PARAMETER_SET_TYPE_PPS :
2080 if( h264_parse_pps( info, info->buffer.rbsp, ps_data + 1, ps_length - 1 ) < 0 )
2081 return -1;
2082 break;
2083 default :
2084 break;
2086 return lsmash_append_h264_parameter_set( param, ps_type, ps_data, ps_length );
2089 static inline int h264_move_dcr_nalu_entry
2091 lsmash_h264_specific_parameters_t *dst_data,
2092 lsmash_h264_specific_parameters_t *src_data,
2093 lsmash_h264_parameter_set_type ps_type
2096 lsmash_entry_list_t *src_ps_list = h264_get_parameter_set_list( src_data, ps_type );
2097 lsmash_entry_list_t *dst_ps_list = h264_get_parameter_set_list( dst_data, ps_type );
2098 assert( src_ps_list && dst_ps_list );
2099 for( lsmash_entry_t *src_entry = src_ps_list->head; src_entry; src_entry = src_entry->next )
2101 isom_dcr_ps_entry_t *src_ps = (isom_dcr_ps_entry_t *)src_entry->data;
2102 if( !src_ps )
2103 continue;
2104 uint8_t src_ps_id;
2105 if( h264_get_ps_id( src_ps->nalUnit + 1, src_ps->nalUnitLength - 1, &src_ps_id, ps_type ) < 0 )
2106 return -1;
2107 lsmash_entry_t *dst_entry;
2108 for( dst_entry = dst_ps_list->head; dst_entry; dst_entry = dst_entry->next )
2110 isom_dcr_ps_entry_t *dst_ps = (isom_dcr_ps_entry_t *)dst_entry->data;
2111 if( !dst_ps )
2112 continue;
2113 uint8_t dst_ps_id;
2114 if( h264_get_ps_id( dst_ps->nalUnit + 1, dst_ps->nalUnitLength - 1, &dst_ps_id, ps_type ) < 0 )
2115 return -1;
2116 if( dst_ps_id == src_ps_id )
2118 /* Replace the old parameter set with the new one. */
2119 assert( dst_entry->data != src_entry->data );
2120 isom_remove_dcr_ps( dst_ps );
2121 dst_entry->data = src_entry->data;
2122 src_entry->data = NULL;
2123 break;
2126 if( !dst_entry )
2128 /* Move the parameter set. */
2129 if( lsmash_add_entry( dst_ps_list, src_ps ) )
2130 return -1;
2131 src_entry->data = NULL;
2134 return 0;
2137 int h264_move_pending_avcC_param
2139 h264_info_t *info
2142 assert( info );
2143 if( !info->avcC_pending )
2144 return 0;
2145 /* Mark 'unused' on parameter sets within the decoder configuration record. */
2146 for( int i = 0; i < H264_PARAMETER_SET_TYPE_NUM; i++ )
2148 lsmash_entry_list_t *ps_list = h264_get_parameter_set_list( &info->avcC_param, i );
2149 assert( ps_list );
2150 for( lsmash_entry_t *entry = ps_list->head; entry; entry = entry->next )
2152 isom_dcr_ps_entry_t *ps = (isom_dcr_ps_entry_t *)entry->data;
2153 if( !ps )
2154 continue;
2155 ps->unused = 1;
2158 /* Move the new parameter sets. */
2159 if( h264_move_dcr_nalu_entry( &info->avcC_param, &info->avcC_param_next, H264_PARAMETER_SET_TYPE_SPS ) < 0
2160 || h264_move_dcr_nalu_entry( &info->avcC_param, &info->avcC_param_next, H264_PARAMETER_SET_TYPE_PPS ) < 0 )
2161 return -1;
2162 /* Move to the pending. */
2163 lsmash_h264_parameter_sets_t *parameter_sets = info->avcC_param.parameter_sets; /* Back up parameter sets. */
2164 info->avcC_param = info->avcC_param_next;
2165 info->avcC_param.parameter_sets = parameter_sets;
2166 /* No pending avcC. */
2167 lsmash_destroy_h264_parameter_sets( &info->avcC_param_next );
2168 memset( &info->avcC_param_next, 0, sizeof(lsmash_h264_specific_parameters_t) );
2169 info->avcC_pending = 0;
2170 return 0;
2173 static int h264_parse_succeeded
2175 h264_info_t *info,
2176 lsmash_h264_specific_parameters_t *param
2179 int ret;
2180 if( info->sps.present && info->pps.present )
2182 *param = info->avcC_param;
2183 /* Avoid freeing parameter sets. */
2184 info->avcC_param.parameter_sets = NULL;
2185 ret = 0;
2187 else
2188 ret = -1;
2189 h264_cleanup_parser( info );
2190 return ret;
2193 static inline int h264_parse_failed
2195 h264_info_t *info
2198 h264_cleanup_parser( info );
2199 return -1;
2202 int lsmash_setup_h264_specific_parameters_from_access_unit
2204 lsmash_h264_specific_parameters_t *param,
2205 uint8_t *data,
2206 uint32_t data_length
2209 if( !param || !data || data_length == 0 )
2210 return -1;
2211 h264_info_t *info = &(h264_info_t){ { 0 } };
2212 lsmash_bs_t *bs = &(lsmash_bs_t){ 0 };
2213 if( lsmash_bs_set_empty_stream( bs, data, data_length ) < 0 )
2214 return -1;
2215 uint64_t sc_head_pos = nalu_find_first_start_code( bs );
2216 if( sc_head_pos == NALU_NO_START_CODE_FOUND )
2217 return -1;
2218 if( h264_setup_parser( info, 1 ) < 0 )
2219 return h264_parse_failed( info );
2220 h264_stream_buffer_t *sb = &info->buffer;
2221 h264_slice_info_t *slice = &info->slice;
2222 while( 1 )
2224 h264_nalu_header_t nuh;
2225 uint64_t start_code_length;
2226 uint64_t trailing_zero_bytes;
2227 uint64_t nalu_length = h264_find_next_start_code( bs, &nuh, &start_code_length, &trailing_zero_bytes );
2228 if( start_code_length <= NALU_SHORT_START_CODE_LENGTH && lsmash_bs_is_end( bs, nalu_length ) )
2229 /* For the last NALU. This NALU already has been parsed. */
2230 return h264_parse_succeeded( info, param );
2231 uint8_t nalu_type = nuh.nal_unit_type;
2232 uint64_t next_sc_head_pos = sc_head_pos
2233 + start_code_length
2234 + nalu_length
2235 + trailing_zero_bytes;
2236 if( nalu_type == H264_NALU_TYPE_FD )
2238 /* We don't support streams with both filler and HRD yet.
2239 * Otherwise, just skip filler because elemental streams defined in 14496-15 are forbidden to use filler. */
2240 if( info->sps.vui.hrd.present )
2241 return h264_parse_failed( info );
2243 else if( (nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && nalu_type <= H264_NALU_TYPE_SPS_EXT)
2244 || nalu_type == H264_NALU_TYPE_SLICE_AUX )
2246 /* Increase the buffer if needed. */
2247 uint64_t possible_au_length = NALU_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
2248 if( sb->bank->buffer_size < possible_au_length
2249 && h264_supplement_buffer( sb, NULL, 2 * possible_au_length ) < 0 )
2250 return h264_parse_failed( info );
2251 /* Get the EBSP of the current NALU here.
2252 * AVC elemental stream defined in 14496-15 can recognize from 0 to 13, and 19 of nal_unit_type.
2253 * We don't support SVC and MVC elemental stream defined in 14496-15 yet. */
2254 uint8_t *nalu = lsmash_bs_get_buffer_data( bs ) + start_code_length;
2255 if( nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && nalu_type <= H264_NALU_TYPE_SLICE_IDR )
2257 /* VCL NALU (slice) */
2258 h264_slice_info_t prev_slice = *slice;
2259 if( h264_parse_slice( info, &nuh, sb->rbsp, nalu + nuh.length, nalu_length - nuh.length ) )
2260 return h264_parse_failed( info );
2261 if( prev_slice.present )
2263 /* Check whether the AU that contains the previous VCL NALU completed or not. */
2264 if( h264_find_au_delimit_by_slice_info( slice, &prev_slice ) )
2265 /* The current NALU is the first VCL NALU of the primary coded picture of an new AU.
2266 * Therefore, the previous slice belongs to that new AU. */
2267 return h264_parse_succeeded( info, param );
2269 slice->present = 1;
2271 else
2273 if( h264_find_au_delimit_by_nalu_type( nalu_type, info->prev_nalu_type ) )
2274 /* The last slice belongs to the AU you want at this time. */
2275 return h264_parse_succeeded( info, param );
2276 switch( nalu_type )
2278 case H264_NALU_TYPE_SPS :
2279 if( h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_SPS, nalu, nalu_length ) )
2280 return h264_parse_failed( info );
2281 break;
2282 case H264_NALU_TYPE_PPS :
2283 if( h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_PPS, nalu, nalu_length ) )
2284 return h264_parse_failed( info );
2285 break;
2286 case H264_NALU_TYPE_SPS_EXT :
2287 if( h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_SPSEXT, nalu, nalu_length ) )
2288 return h264_parse_failed( info );
2289 break;
2290 default :
2291 break;
2295 /* Move to the first byte of the next start code. */
2296 info->prev_nalu_type = nalu_type;
2297 if( lsmash_bs_read_seek( bs, next_sc_head_pos, SEEK_SET ) != next_sc_head_pos )
2298 return h264_parse_failed( info );
2299 /* Check if no more data to read from the stream. */
2300 if( !lsmash_bs_is_end( bs, NALU_SHORT_START_CODE_LENGTH ) )
2301 sc_head_pos = next_sc_head_pos;
2302 else
2303 return h264_parse_succeeded( info, param );
2307 int h264_construct_specific_parameters
2309 lsmash_codec_specific_t *dst,
2310 lsmash_codec_specific_t *src
2313 assert( dst && dst->data.structured && src && src->data.unstructured );
2314 if( src->size < ISOM_BASEBOX_COMMON_SIZE + 7 )
2315 return -1;
2316 lsmash_h264_specific_parameters_t *param = (lsmash_h264_specific_parameters_t *)dst->data.structured;
2317 uint8_t *data = src->data.unstructured;
2318 uint64_t size = LSMASH_GET_BE32( data );
2319 data += ISOM_BASEBOX_COMMON_SIZE;
2320 if( size == 1 )
2322 size = LSMASH_GET_BE64( data );
2323 data += 8;
2325 if( size != src->size )
2326 return -1;
2327 if( !param->parameter_sets )
2329 param->parameter_sets = lsmash_malloc_zero( sizeof(lsmash_h264_parameter_sets_t) );
2330 if( !param->parameter_sets )
2331 return -1;
2333 lsmash_bs_t *bs = lsmash_bs_create();
2334 if( !bs )
2335 return -1;
2336 if( lsmash_bs_import_data( bs, data, src->size - (data - src->data.unstructured) ) )
2337 goto fail;
2338 if( lsmash_bs_get_byte( bs ) != 1 )
2339 goto fail; /* We don't support configurationVersion other than 1. */
2340 param->AVCProfileIndication = lsmash_bs_get_byte( bs );
2341 param->profile_compatibility = lsmash_bs_get_byte( bs );
2342 param->AVCLevelIndication = lsmash_bs_get_byte( bs );
2343 param->lengthSizeMinusOne = lsmash_bs_get_byte( bs ) & 0x03;
2344 uint8_t numOfSequenceParameterSets = lsmash_bs_get_byte( bs ) & 0x1F;
2345 if( numOfSequenceParameterSets
2346 && nalu_get_dcr_ps( bs, param->parameter_sets->sps_list, numOfSequenceParameterSets ) )
2347 goto fail;
2348 uint8_t numOfPictureParameterSets = lsmash_bs_get_byte( bs );
2349 if( numOfPictureParameterSets
2350 && nalu_get_dcr_ps( bs, param->parameter_sets->pps_list, numOfPictureParameterSets ) )
2351 goto fail;
2352 if( H264_REQUIRES_AVCC_EXTENSION( param->AVCProfileIndication ) )
2354 param->chroma_format = lsmash_bs_get_byte( bs ) & 0x03;
2355 param->bit_depth_luma_minus8 = lsmash_bs_get_byte( bs ) & 0x07;
2356 param->bit_depth_chroma_minus8 = lsmash_bs_get_byte( bs ) & 0x07;
2357 uint8_t numOfSequenceParameterSetExt = lsmash_bs_get_byte( bs );
2358 if( numOfSequenceParameterSetExt
2359 && nalu_get_dcr_ps( bs, param->parameter_sets->spsext_list, numOfSequenceParameterSetExt ) )
2360 goto fail;
2362 lsmash_bs_cleanup( bs );
2363 return 0;
2364 fail:
2365 lsmash_bs_cleanup( bs );
2366 return -1;
2369 int h264_print_codec_specific
2371 FILE *fp,
2372 lsmash_file_t *file,
2373 isom_box_t *box,
2374 int level
2377 assert( fp && file && box && (box->manager & LSMASH_BINARY_CODED_BOX) );
2378 int indent = level;
2379 lsmash_ifprintf( fp, indent++, "[%s: AVC Configuration Box]\n", isom_4cc2str( box->type.fourcc ) );
2380 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
2381 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
2382 uint8_t *data = box->binary;
2383 uint32_t offset = isom_skip_box_common( &data );
2384 lsmash_bs_t *bs = lsmash_bs_create();
2385 if( !bs )
2386 return -1;
2387 if( lsmash_bs_import_data( bs, data, box->size - offset ) )
2389 lsmash_bs_cleanup( bs );
2390 return -1;
2392 lsmash_ifprintf( fp, indent, "configurationVersion = %"PRIu8"\n", lsmash_bs_get_byte( bs ) );
2393 uint8_t AVCProfileIndication = lsmash_bs_get_byte( bs );
2394 lsmash_ifprintf( fp, indent, "AVCProfileIndication = %"PRIu8"\n", AVCProfileIndication );
2395 lsmash_ifprintf( fp, indent, "profile_compatibility = 0x%02"PRIx8"\n", lsmash_bs_get_byte( bs ) );
2396 lsmash_ifprintf( fp, indent, "AVCLevelIndication = %"PRIu8"\n", lsmash_bs_get_byte( bs ) );
2397 uint8_t temp8 = lsmash_bs_get_byte( bs );
2398 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 2) & 0x3F );
2399 lsmash_ifprintf( fp, indent, "lengthSizeMinusOne = %"PRIu8"\n", temp8 & 0x03 );
2400 temp8 = lsmash_bs_get_byte( bs );
2401 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 5) & 0x07 );
2402 uint8_t numOfSequenceParameterSets = temp8 & 0x1f;
2403 lsmash_ifprintf( fp, indent, "numOfSequenceParameterSets = %"PRIu8"\n", numOfSequenceParameterSets );
2404 for( uint8_t i = 0; i < numOfSequenceParameterSets; i++ )
2406 uint16_t nalUnitLength = lsmash_bs_get_be16( bs );
2407 lsmash_bs_skip_bytes( bs, nalUnitLength );
2409 uint8_t numOfPictureParameterSets = lsmash_bs_get_byte( bs );
2410 lsmash_ifprintf( fp, indent, "numOfPictureParameterSets = %"PRIu8"\n", numOfPictureParameterSets );
2411 for( uint8_t i = 0; i < numOfPictureParameterSets; i++ )
2413 uint16_t nalUnitLength = lsmash_bs_get_be16( bs );
2414 lsmash_bs_skip_bytes( bs, nalUnitLength );
2416 /* Note: there are too many files, in the world, that don't contain the following fields. */
2417 if( H264_REQUIRES_AVCC_EXTENSION( AVCProfileIndication )
2418 && (lsmash_bs_get_pos( bs ) < (box->size - offset)) )
2420 temp8 = lsmash_bs_get_byte( bs );
2421 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 2) & 0x3F );
2422 lsmash_ifprintf( fp, indent, "chroma_format = %"PRIu8"\n", temp8 & 0x03 );
2423 temp8 = lsmash_bs_get_byte( bs );
2424 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 3) & 0x1F );
2425 lsmash_ifprintf( fp, indent, "bit_depth_luma_minus8 = %"PRIu8"\n", temp8 & 0x7 );
2426 temp8 = lsmash_bs_get_byte( bs );
2427 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", (temp8 >> 3) & 0x1F );
2428 lsmash_ifprintf( fp, indent, "bit_depth_chroma_minus8 = %"PRIu8"\n", temp8 & 0x7 );
2429 lsmash_ifprintf( fp, indent, "numOfSequenceParameterSetExt = %"PRIu8"\n", lsmash_bs_get_byte( bs ) );
2431 lsmash_bs_cleanup( bs );
2432 return 0;
2435 int h264_copy_codec_specific
2437 lsmash_codec_specific_t *dst,
2438 lsmash_codec_specific_t *src
2441 assert( src && src->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && src->data.structured );
2442 assert( dst && dst->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && dst->data.structured );
2443 lsmash_h264_specific_parameters_t *src_data = (lsmash_h264_specific_parameters_t *)src->data.structured;
2444 lsmash_h264_specific_parameters_t *dst_data = (lsmash_h264_specific_parameters_t *)dst->data.structured;
2445 lsmash_destroy_h264_parameter_sets( dst_data );
2446 *dst_data = *src_data;
2447 if( !src_data->parameter_sets )
2448 return 0;
2449 dst_data->parameter_sets = lsmash_malloc_zero( sizeof(lsmash_h264_parameter_sets_t) );
2450 if( !dst_data->parameter_sets )
2451 return -1;
2452 for( int i = 0; i < 3; i++ )
2454 lsmash_entry_list_t *src_ps_list = h264_get_parameter_set_list( src_data, i );
2455 lsmash_entry_list_t *dst_ps_list = h264_get_parameter_set_list( dst_data, i );
2456 assert( src_ps_list && dst_ps_list );
2457 for( lsmash_entry_t *entry = src_ps_list->head; entry; entry = entry->next )
2459 isom_dcr_ps_entry_t *src_ps = (isom_dcr_ps_entry_t *)entry->data;
2460 if( !src_ps || src_ps->unused )
2461 continue;
2462 isom_dcr_ps_entry_t *dst_ps = isom_create_ps_entry( src_ps->nalUnit, src_ps->nalUnitLength );
2463 if( !dst_ps )
2465 lsmash_destroy_h264_parameter_sets( dst_data );
2466 return -1;
2468 if( lsmash_add_entry( dst_ps_list, dst_ps ) )
2470 lsmash_destroy_h264_parameter_sets( dst_data );
2471 isom_remove_dcr_ps( dst_ps );
2472 return -1;
2476 return 0;
2479 int h264_print_bitrate
2481 FILE *fp,
2482 lsmash_file_t *file,
2483 isom_box_t *box,
2484 int level
2487 assert( fp && file && box );
2488 int indent = level;
2489 lsmash_ifprintf( fp, indent++, "[%s: MPEG-4 Bit Rate Box]\n", isom_4cc2str( box->type.fourcc ) );
2490 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
2491 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
2492 isom_btrt_t *btrt = (isom_btrt_t *)box;
2493 lsmash_ifprintf( fp, indent, "bufferSizeDB = %"PRIu32"\n", btrt->bufferSizeDB );
2494 lsmash_ifprintf( fp, indent, "maxBitrate = %"PRIu32"\n", btrt->maxBitrate );
2495 lsmash_ifprintf( fp, indent, "avgBitrate = %"PRIu32"\n", btrt->avgBitrate );
2496 return 0;