print: Avoid Variable Length Arrays.
[L-SMASH.git] / importer / nalu_imp.c
blobe36c1cce8fa20970fdf27f8d40b490b54b434dd0
1 /*****************************************************************************
2 * nalu_imp.c
3 *****************************************************************************
4 * Copyright (C) 2011-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 <stdlib.h>
26 #include <string.h>
27 #include <inttypes.h>
29 #define LSMASH_IMPORTER_INTERNAL
30 #include "importer.h"
32 /***************************************************************************
33 H.264 importer
34 ITU-T Recommendation H.264 (04/13)
35 ISO/IEC 14496-15:2010
36 ***************************************************************************/
37 #include "codecs/h264.h"
38 #include "codecs/nalu.h"
40 typedef struct
42 h264_info_t info;
43 lsmash_entry_list_t avcC_list[1]; /* stored as lsmash_codec_specific_t */
44 lsmash_media_ts_list_t ts_list;
45 uint32_t max_au_length;
46 uint32_t num_undecodable;
47 uint32_t avcC_number;
48 uint32_t last_delta;
49 uint64_t last_intra_cts;
50 uint64_t sc_head_pos;
51 uint8_t composition_reordering_present;
52 uint8_t field_pic_present;
53 } h264_importer_t;
55 typedef struct
57 int64_t poc;
58 uint32_t delta;
59 uint16_t poc_delta;
60 uint16_t reset;
61 } nal_pic_timing_t;
63 static void remove_h264_importer( h264_importer_t *h264_imp )
65 if( !h264_imp )
66 return;
67 lsmash_remove_entries( h264_imp->avcC_list, lsmash_destroy_codec_specific_data );
68 h264_cleanup_parser( &h264_imp->info );
69 lsmash_free( h264_imp->ts_list.timestamp );
70 lsmash_free( h264_imp );
73 static void h264_importer_cleanup( importer_t *importer )
75 debug_if( importer && importer->info )
76 remove_h264_importer( importer->info );
79 static h264_importer_t *create_h264_importer( importer_t *importer )
81 h264_importer_t *h264_imp = lsmash_malloc_zero( sizeof(h264_importer_t) );
82 if( !h264_imp )
83 return NULL;
84 if( h264_setup_parser( &h264_imp->info, 0 ) < 0 )
86 remove_h264_importer( h264_imp );
87 return NULL;
89 lsmash_init_entry_list( h264_imp->avcC_list );
90 return h264_imp;
93 static inline int h264_complete_au( h264_access_unit_t *au, int probe )
95 if( !au->picture.has_primary || au->incomplete_length == 0 )
96 return 0;
97 if( !probe )
98 memcpy( au->data, au->incomplete_data, au->incomplete_length );
99 au->length = au->incomplete_length;
100 au->incomplete_length = 0;
101 au->picture.has_primary = 0;
102 return 1;
105 static void h264_append_nalu_to_au( h264_access_unit_t *au, uint8_t *src_nalu, uint32_t nalu_length, int probe )
107 if( !probe )
109 uint8_t *dst_nalu = au->incomplete_data + au->incomplete_length + NALU_DEFAULT_NALU_LENGTH_SIZE;
110 for( int i = NALU_DEFAULT_NALU_LENGTH_SIZE; i; i-- )
111 *(dst_nalu - i) = (nalu_length >> ((i - 1) * 8)) & 0xff;
112 memcpy( dst_nalu, src_nalu, nalu_length );
114 /* Note: au->incomplete_length shall be 0 immediately after AU has completed.
115 * Therefore, possible_au_length in h264_get_access_unit_internal() can't be used here
116 * to avoid increasing AU length monotonously through the entire stream. */
117 au->incomplete_length += NALU_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
120 static int h264_get_au_internal_succeeded( h264_importer_t *h264_imp, h264_access_unit_t *au )
122 au->number += 1;
123 return 0;
126 static int h264_get_au_internal_failed( h264_importer_t *h264_imp, h264_access_unit_t *au, int complete_au, int ret )
128 if( complete_au )
129 au->number += 1;
130 return ret;
133 static lsmash_video_summary_t *h264_create_summary
135 lsmash_h264_specific_parameters_t *param,
136 h264_sps_t *sps,
137 uint32_t max_au_length
140 lsmash_video_summary_t *summary = (lsmash_video_summary_t *)lsmash_create_summary( LSMASH_SUMMARY_TYPE_VIDEO );
141 if( !summary )
142 return NULL;
143 /* Update summary here.
144 * max_au_length is set at the last of mp4sys_h264_probe function. */
145 lsmash_codec_specific_t *cs = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264,
146 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED );
147 cs->data.unstructured = lsmash_create_h264_specific_info( param, &cs->size );
148 if( !cs->data.unstructured
149 || lsmash_add_entry( &summary->opaque->list, cs ) < 0 )
151 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
152 lsmash_destroy_codec_specific_data( cs );
153 return NULL;
155 summary->sample_type = ISOM_CODEC_TYPE_AVC1_VIDEO;
156 summary->max_au_length = max_au_length;
157 summary->timescale = sps->vui.time_scale;
158 summary->timebase = sps->vui.num_units_in_tick;
159 summary->vfr = !sps->vui.fixed_frame_rate_flag;
160 summary->sample_per_field = 0;
161 summary->width = sps->cropped_width;
162 summary->height = sps->cropped_height;
163 summary->par_h = sps->vui.sar_width;
164 summary->par_v = sps->vui.sar_height;
165 summary->color.primaries_index = sps->vui.colour_primaries;
166 summary->color.transfer_index = sps->vui.transfer_characteristics;
167 summary->color.matrix_index = sps->vui.matrix_coefficients;
168 summary->color.full_range = sps->vui.video_full_range_flag;
169 return summary;
172 static int h264_store_codec_specific
174 h264_importer_t *h264_imp,
175 lsmash_h264_specific_parameters_t *avcC_param
178 lsmash_codec_specific_t *src_cs = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264,
179 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED );
180 if( !src_cs )
181 return LSMASH_ERR_NAMELESS;
182 lsmash_h264_specific_parameters_t *src_param = (lsmash_h264_specific_parameters_t *)src_cs->data.structured;
183 *src_param = *avcC_param;
184 lsmash_codec_specific_t *dst_cs = lsmash_convert_codec_specific_format( src_cs, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED );
185 src_param->parameter_sets = NULL; /* Avoid freeing parameter sets within avcC_param. */
186 lsmash_destroy_codec_specific_data( src_cs );
187 if( !dst_cs )
189 lsmash_destroy_codec_specific_data( dst_cs );
190 return LSMASH_ERR_NAMELESS;
192 if( lsmash_add_entry( h264_imp->avcC_list, dst_cs ) < 0 )
194 lsmash_destroy_codec_specific_data( dst_cs );
195 return LSMASH_ERR_MEMORY_ALLOC;
197 return 0;
200 static inline void h264_new_access_unit
202 h264_access_unit_t *au
205 au->length = 0;
206 au->picture.type = H264_PICTURE_TYPE_NONE;
207 au->picture.random_accessible = 0;
208 au->picture.recovery_frame_cnt = 0;
209 au->picture.has_mmco5 = 0;
210 au->picture.has_redundancy = 0;
211 au->picture.broken_link_flag = 0;
214 /* If probe equals 0, don't get the actual data (EBPS) of an access unit and only parse NALU.
215 * Currently, you can get AU of AVC video elemental stream only, not AVC parameter set elemental stream defined in 14496-15. */
216 static int h264_get_access_unit_internal
218 importer_t *importer,
219 int probe
222 h264_importer_t *h264_imp = (h264_importer_t *)importer->info;
223 h264_info_t *info = &h264_imp->info;
224 h264_slice_info_t *slice = &info->slice;
225 h264_access_unit_t *au = &info->au;
226 h264_picture_info_t *picture = &au->picture;
227 h264_stream_buffer_t *sb = &info->buffer;
228 lsmash_bs_t *bs = importer->bs;
229 int complete_au = 0;
230 h264_new_access_unit( au );
231 while( 1 )
233 h264_nalu_header_t nuh;
234 uint64_t start_code_length;
235 uint64_t trailing_zero_bytes;
236 uint64_t nalu_length = h264_find_next_start_code( bs, &nuh, &start_code_length, &trailing_zero_bytes );
237 if( start_code_length <= NALU_SHORT_START_CODE_LENGTH && lsmash_bs_is_end( bs, nalu_length ) )
239 /* For the last NALU.
240 * This NALU already has been appended into the latest access unit and parsed. */
241 h264_update_picture_info( info, picture, slice, &info->sei );
242 complete_au = h264_complete_au( au, probe );
243 if( complete_au )
244 return h264_get_au_internal_succeeded( h264_imp, au );
245 else
246 return h264_get_au_internal_failed( h264_imp, au, complete_au, LSMASH_ERR_INVALID_DATA );
248 uint8_t nalu_type = nuh.nal_unit_type;
249 uint64_t next_sc_head_pos = h264_imp->sc_head_pos
250 + start_code_length
251 + nalu_length
252 + trailing_zero_bytes;
253 #if 0
254 if( probe )
256 fprintf( stderr, "NALU type: %"PRIu8" \n", nalu_type );
257 fprintf( stderr, " NALU header position: %"PRIx64" \n", h264_imp->sc_head_pos + start_code_length );
258 fprintf( stderr, " EBSP position: %"PRIx64" \n", h264_imp->sc_head_pos + start_code_length + nuh.length );
259 fprintf( stderr, " EBSP length: %"PRIx64" (%"PRIu64") \n", nalu_length - nuh.length, nalu_length - nuh.length );
260 fprintf( stderr, " trailing_zero_bytes: %"PRIx64" \n", trailing_zero_bytes );
261 fprintf( stderr, " Next start code position: %"PRIx64"\n", next_sc_head_pos );
263 #endif
264 if( nalu_type == H264_NALU_TYPE_FD )
266 /* We don't support streams with both filler and HRD yet.
267 * Otherwise, just skip filler because 'avc1' and 'avc2' samples are forbidden to use filler. */
268 if( info->sps.vui.hrd.present )
269 return h264_get_au_internal_failed( h264_imp, au, complete_au, LSMASH_ERR_PATCH_WELCOME );
271 else if( (nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && nalu_type <= H264_NALU_TYPE_SPS_EXT)
272 || nalu_type == H264_NALU_TYPE_SLICE_AUX )
274 int err;
275 /* Increase the buffer if needed. */
276 uint64_t possible_au_length = au->incomplete_length + NALU_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
277 if( sb->bank->buffer_size < possible_au_length
278 && (err = h264_supplement_buffer( sb, au, 2 * possible_au_length )) < 0 )
280 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to increase the buffer size.\n" );
281 return h264_get_au_internal_failed( h264_imp, au, complete_au, err );
283 /* Get the EBSP of the current NALU here.
284 * AVC elemental stream defined in 14496-15 can recognizes from 0 to 13, and 19 of nal_unit_type.
285 * We don't support SVC and MVC elemental stream defined in 14496-15 yet. */
286 uint8_t *nalu = lsmash_bs_get_buffer_data( bs ) + start_code_length;
287 if( nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && nalu_type <= H264_NALU_TYPE_SLICE_IDR )
289 /* VCL NALU (slice) */
290 h264_slice_info_t prev_slice = *slice;
291 if( (err = h264_parse_slice( info, &nuh, sb->rbsp, nalu + nuh.length, nalu_length - nuh.length )) < 0 )
292 return h264_get_au_internal_failed( h264_imp, au, complete_au, err );
293 if( probe && info->avcC_pending )
295 /* Copy and append a Codec Specific info. */
296 if( (err = h264_store_codec_specific( h264_imp, &info->avcC_param )) < 0 )
297 return err;
299 if( (err = h264_move_pending_avcC_param( info )) < 0 )
300 return err;
301 if( prev_slice.present )
303 /* Check whether the AU that contains the previous VCL NALU completed or not. */
304 if( h264_find_au_delimit_by_slice_info( slice, &prev_slice ) )
306 /* The current NALU is the first VCL NALU of the primary coded picture of an new AU.
307 * Therefore, the previous slice belongs to the AU you want at this time. */
308 h264_update_picture_info( info, picture, &prev_slice, &info->sei );
309 complete_au = h264_complete_au( au, probe );
311 else
312 h264_update_picture_info_for_slice( info, picture, &prev_slice );
314 h264_append_nalu_to_au( au, nalu, nalu_length, probe );
315 slice->present = 1;
317 else
319 if( h264_find_au_delimit_by_nalu_type( nalu_type, info->prev_nalu_type ) )
321 /* The last slice belongs to the AU you want at this time. */
322 h264_update_picture_info( info, picture, slice, &info->sei );
323 complete_au = h264_complete_au( au, probe );
325 switch( nalu_type )
327 case H264_NALU_TYPE_SEI :
329 if( (err = h264_parse_sei( info->bits, &info->sps, &info->sei, sb->rbsp,
330 nalu + nuh.length,
331 nalu_length - nuh.length )) < 0 )
332 return h264_get_au_internal_failed( h264_imp, au, complete_au, err );
333 h264_append_nalu_to_au( au, nalu, nalu_length, probe );
334 break;
336 case H264_NALU_TYPE_SPS :
337 if( (err = h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_SPS, nalu, nalu_length )) < 0 )
338 return h264_get_au_internal_failed( h264_imp, au, complete_au, err );
339 break;
340 case H264_NALU_TYPE_PPS :
341 if( (err = h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_PPS, nalu, nalu_length )) < 0 )
342 return h264_get_au_internal_failed( h264_imp, au, complete_au, err );
343 break;
344 case H264_NALU_TYPE_AUD : /* We drop access unit delimiters. */
345 break;
346 case H264_NALU_TYPE_SPS_EXT :
347 if( (err = h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_SPSEXT, nalu, nalu_length )) < 0 )
348 return h264_get_au_internal_failed( h264_imp, au, complete_au, err );
349 break;
350 default :
351 h264_append_nalu_to_au( au, nalu, nalu_length, probe );
352 break;
354 if( info->avcC_pending )
355 importer->status = IMPORTER_CHANGE;
358 /* Move to the first byte of the next start code. */
359 info->prev_nalu_type = nalu_type;
360 if( lsmash_bs_read_seek( bs, next_sc_head_pos, SEEK_SET ) != next_sc_head_pos )
362 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to seek the next start code.\n" );
363 return h264_get_au_internal_failed( h264_imp, au, complete_au, LSMASH_ERR_NAMELESS );
365 /* Check if no more data to read from the stream. */
366 if( !lsmash_bs_is_end( bs, NALU_SHORT_START_CODE_LENGTH ) )
367 h264_imp->sc_head_pos = next_sc_head_pos;
368 /* If there is no more data in the stream, and flushed chunk of NALUs, flush it as complete AU here. */
369 else if( au->incomplete_length && au->length == 0 )
371 h264_update_picture_info( info, picture, slice, &info->sei );
372 h264_complete_au( au, probe );
373 return h264_get_au_internal_succeeded( h264_imp, au );
375 if( complete_au )
376 return h264_get_au_internal_succeeded( h264_imp, au );
380 static inline void h264_importer_check_eof( importer_t *importer, h264_access_unit_t *au )
382 if( lsmash_bs_is_end( importer->bs, 0 ) && au->incomplete_length == 0 )
383 importer->status = IMPORTER_EOF;
384 else if( importer->status != IMPORTER_CHANGE )
385 importer->status = IMPORTER_OK;
388 static int h264_importer_get_accessunit
390 importer_t *importer,
391 uint32_t track_number,
392 lsmash_sample_t **p_sample
395 if( !importer->info )
396 return LSMASH_ERR_NAMELESS;
397 if( track_number != 1 )
398 return LSMASH_ERR_FUNCTION_PARAM;
399 h264_importer_t *h264_imp = (h264_importer_t *)importer->info;
400 h264_info_t *info = &h264_imp->info;
401 importer_status current_status = importer->status;
402 if( current_status == IMPORTER_ERROR )
403 return LSMASH_ERR_NAMELESS;
404 if( current_status == IMPORTER_EOF )
405 return IMPORTER_EOF;
406 int err = h264_get_access_unit_internal( importer, 0 );
407 if( err < 0 )
409 importer->status = IMPORTER_ERROR;
410 return err;
412 h264_importer_check_eof( importer, &info->au );
413 if( importer->status == IMPORTER_CHANGE && !info->avcC_pending )
414 current_status = IMPORTER_CHANGE;
415 if( current_status == IMPORTER_CHANGE )
417 /* Update the active summary. */
418 lsmash_codec_specific_t *cs = (lsmash_codec_specific_t *)lsmash_get_entry_data( h264_imp->avcC_list, ++ h264_imp->avcC_number );
419 if( !cs )
420 return LSMASH_ERR_NAMELESS;
421 lsmash_h264_specific_parameters_t *avcC_param = (lsmash_h264_specific_parameters_t *)cs->data.structured;
422 lsmash_video_summary_t *summary = h264_create_summary( avcC_param, &info->sps, h264_imp->max_au_length );
423 if( !summary )
424 return LSMASH_ERR_NAMELESS;
425 lsmash_remove_entry( importer->summaries, track_number, lsmash_cleanup_summary );
426 if( lsmash_add_entry( importer->summaries, summary ) < 0 )
428 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
429 return LSMASH_ERR_MEMORY_ALLOC;
431 importer->status = IMPORTER_OK;
433 lsmash_sample_t *sample = lsmash_create_sample( h264_imp->max_au_length );
434 if( !sample )
435 return LSMASH_ERR_MEMORY_ALLOC;
436 *p_sample = sample;
437 h264_access_unit_t *au = &info->au;
438 h264_picture_info_t *picture = &au->picture;
439 sample->dts = h264_imp->ts_list.timestamp[ au->number - 1 ].dts;
440 sample->cts = h264_imp->ts_list.timestamp[ au->number - 1 ].cts;
441 if( au->number < h264_imp->num_undecodable )
442 sample->prop.leading = ISOM_SAMPLE_IS_UNDECODABLE_LEADING;
443 else
444 sample->prop.leading = picture->independent || sample->cts >= h264_imp->last_intra_cts
445 ? ISOM_SAMPLE_IS_NOT_LEADING : ISOM_SAMPLE_IS_UNDECODABLE_LEADING;
446 if( picture->independent )
447 h264_imp->last_intra_cts = sample->cts;
448 if( h264_imp->composition_reordering_present && !picture->disposable && !picture->idr )
449 sample->prop.allow_earlier = QT_SAMPLE_EARLIER_PTS_ALLOWED;
450 sample->prop.independent = picture->independent ? ISOM_SAMPLE_IS_INDEPENDENT : ISOM_SAMPLE_IS_NOT_INDEPENDENT;
451 sample->prop.disposable = picture->disposable ? ISOM_SAMPLE_IS_DISPOSABLE : ISOM_SAMPLE_IS_NOT_DISPOSABLE;
452 sample->prop.redundant = picture->has_redundancy ? ISOM_SAMPLE_HAS_REDUNDANCY : ISOM_SAMPLE_HAS_NO_REDUNDANCY;
453 sample->prop.post_roll.identifier = picture->frame_num;
454 if( picture->random_accessible )
456 if( picture->idr )
457 sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
458 else if( picture->recovery_frame_cnt )
460 sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_POST_ROLL_START;
461 sample->prop.post_roll.complete = (picture->frame_num + picture->recovery_frame_cnt) % info->sps.MaxFrameNum;
463 else
465 sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP;
466 if( !picture->broken_link_flag )
467 sample->prop.ra_flags |= QT_SAMPLE_RANDOM_ACCESS_FLAG_PARTIAL_SYNC;
470 sample->length = au->length;
471 memcpy( sample->data, au->data, au->length );
472 return current_status;
475 static void nalu_deduplicate_poc
477 nal_pic_timing_t *npt,
478 uint32_t *max_composition_delay,
479 uint32_t num_access_units,
480 uint32_t max_num_reorder_pics
483 /* Deduplicate POCs. */
484 int64_t poc_offset = 0;
485 int64_t poc_min = 0;
486 int64_t invalid_poc_min = 0;
487 uint32_t last_poc_reset = UINT32_MAX;
488 uint32_t invalid_poc_start = 0;
489 int invalid_poc_present = 0;
490 for( uint32_t i = 0; ; i++ )
492 if( i < num_access_units && npt[i].poc != 0 && !npt[i].reset )
494 /* poc_offset is not added to each POC here.
495 * It is done when we encounter the next coded video sequence. */
496 if( npt[i].poc < 0 )
498 /* Pictures with negative POC shall precede IDR-picture in composition order.
499 * The minimum POC is added to poc_offset when we encounter the next coded video sequence. */
500 if( last_poc_reset == UINT32_MAX || i > last_poc_reset + max_num_reorder_pics )
502 if( !invalid_poc_present )
504 invalid_poc_present = 1;
505 invalid_poc_start = i;
507 if( invalid_poc_min > npt[i].poc )
508 invalid_poc_min = npt[i].poc;
510 else if( poc_min > npt[i].poc )
512 poc_min = npt[i].poc;
513 *max_composition_delay = LSMASH_MAX( *max_composition_delay, i - last_poc_reset );
516 continue;
518 /* Encountered a new coded video sequence or no more POCs.
519 * Add poc_offset to each POC of the previous coded video sequence. */
520 poc_offset -= poc_min;
521 int64_t poc_max = 0;
522 for( uint32_t j = last_poc_reset; j < i + !!npt[i].reset; j++ )
523 if( npt[j].poc >= 0 || (j <= last_poc_reset + max_num_reorder_pics) )
525 npt[j].poc += poc_offset;
526 if( poc_max < npt[j].poc )
527 poc_max = npt[j].poc;
529 poc_offset = poc_max + 1;
530 if( invalid_poc_present )
532 /* Pictures with invalid negative POC is probably supposed to be composited
533 * both before the next coded video sequence and after the current one. */
534 poc_offset -= invalid_poc_min;
535 for( uint32_t j = invalid_poc_start; j < i + !!npt[i].reset; j++ )
536 if( npt[j].poc < 0 )
538 npt[j].poc += poc_offset;
539 if( poc_max < npt[j].poc )
540 poc_max = npt[j].poc;
542 invalid_poc_present = 0;
543 invalid_poc_start = 0;
544 invalid_poc_min = 0;
545 poc_offset = poc_max + 1;
547 if( i < num_access_units )
549 if( npt[i].reset )
550 npt[i].poc = 0;
551 poc_min = 0;
552 last_poc_reset = i;
554 else
555 break; /* no more POCs */
559 static void nalu_generate_timestamps_from_poc
561 importer_t *importer,
562 lsmash_media_ts_t *timestamp,
563 nal_pic_timing_t *npt,
564 uint8_t *composition_reordering_present,
565 uint32_t *last_delta,
566 uint32_t max_composition_delay,
567 uint32_t num_access_units
570 /* Check if composition delay derived from reordering is present. */
571 if( max_composition_delay == 0 )
573 for( uint32_t i = 1; i < num_access_units; i++ )
574 if( npt[i].poc < npt[i - 1].poc )
576 *composition_reordering_present = 1;
577 break;
580 else
581 *composition_reordering_present = 1;
582 /* Generate timestamps. */
583 if( *composition_reordering_present )
585 /* Generate timestamps.
586 * Here, DTSs and CTSs are temporary values for sort. */
587 for( uint32_t i = 0; i < num_access_units; i++ )
589 timestamp[i].cts = (uint64_t)npt[i].poc;
590 timestamp[i].dts = (uint64_t)i;
592 qsort( timestamp, num_access_units, sizeof(lsmash_media_ts_t), (int(*)( const void *, const void * ))lsmash_compare_cts );
593 /* Check POC gap in output order. */
594 lsmash_class_t *logger = &(lsmash_class_t){ .name = importer->class->name };
595 for( uint32_t i = 1; i < num_access_units; i++ )
596 if( timestamp[i].cts > timestamp[i - 1].cts + npt[i - 1].poc_delta )
597 lsmash_log( &logger, LSMASH_LOG_WARNING,
598 "POC gap is detected at picture %"PRIu64". Maybe some pictures are lost.\n", timestamp[i].dts );
599 /* Get the maximum composition delay derived from reordering. */
600 for( uint32_t i = 0; i < num_access_units; i++ )
601 if( i < timestamp[i].dts )
603 uint32_t composition_delay = timestamp[i].dts - i;
604 max_composition_delay = LSMASH_MAX( max_composition_delay, composition_delay );
606 uint64_t *ts_buffer = (uint64_t *)lsmash_malloc( (num_access_units + max_composition_delay) * sizeof(uint64_t) );
607 if( !ts_buffer )
609 /* It seems that there is no enough memory to generate more appropriate timestamps.
610 * Anyway, generate CTSs and DTSs. */
611 for( uint32_t i = 0; i < num_access_units; i++ )
612 timestamp[i].cts = i + max_composition_delay;
613 qsort( timestamp, num_access_units, sizeof(lsmash_media_ts_t), (int(*)( const void *, const void * ))lsmash_compare_dts );
614 *last_delta = 1;
615 return;
617 uint64_t *reorder_cts = ts_buffer;
618 uint64_t *prev_reorder_cts = ts_buffer + num_access_units;
619 *last_delta = npt[num_access_units - 1].delta;
620 /* Generate CTSs. */
621 timestamp[0].cts = 0;
622 for( uint32_t i = 1; i < num_access_units; i++ )
623 timestamp[i].cts = timestamp[i - 1].cts + npt[i - 1].delta;
624 int64_t composition_delay_time = timestamp[max_composition_delay].cts;
625 for( uint32_t i = 0; i < num_access_units; i++ )
627 timestamp[i].cts += composition_delay_time;
628 reorder_cts[i] = timestamp[i].cts;
630 /* Generate DTSs. */
631 qsort( timestamp, num_access_units, sizeof(lsmash_media_ts_t), (int(*)( const void *, const void * ))lsmash_compare_dts );
632 for( uint32_t i = 0; i < num_access_units; i++ )
634 timestamp[i].dts = i <= max_composition_delay
635 ? reorder_cts[i] - composition_delay_time
636 : prev_reorder_cts[(i - max_composition_delay) % max_composition_delay];
637 prev_reorder_cts[i % max_composition_delay] = reorder_cts[i];
639 lsmash_free( ts_buffer );
640 #if 0
641 fprintf( stderr, "max_composition_delay=%"PRIu32", composition_delay_time=%"PRIu64"\n",
642 max_composition_delay, composition_delay_time );
643 #endif
645 else
647 timestamp[0].dts = 0;
648 timestamp[0].cts = 0;
649 for( uint32_t i = 1; i < num_access_units; i++ )
651 timestamp[i].dts = timestamp[i - 1].dts + npt[i - 1].delta;
652 timestamp[i].cts = timestamp[i - 1].cts + npt[i - 1].delta;
654 *last_delta = npt[num_access_units - 1].delta;
658 static void nalu_reduce_timescale
660 lsmash_media_ts_t *timestamp,
661 nal_pic_timing_t *npt,
662 uint32_t *last_delta,
663 uint32_t *timescale,
664 uint32_t num_access_units
667 uint64_t gcd_delta = *timescale;
668 for( uint32_t i = 0; i < num_access_units && gcd_delta > 1; i++ )
669 gcd_delta = lsmash_get_gcd( gcd_delta, npt[i].delta );
670 if( gcd_delta > 1 )
672 for( uint32_t i = 0; i < num_access_units; i++ )
674 timestamp[i].dts /= gcd_delta;
675 timestamp[i].cts /= gcd_delta;
677 *last_delta /= gcd_delta;
678 *timescale /= gcd_delta;
680 #if 0
681 for( uint32_t i = 0; i < num_access_units; i++ )
682 fprintf( stderr, "Timestamp[%"PRIu32"]: POC=%"PRId64", DTS=%"PRIu64", CTS=%"PRIu64"\n",
683 i, npt[i].poc, timestamp[i].dts, timestamp[i].cts );
684 #endif
687 static lsmash_video_summary_t *h264_setup_first_summary
689 importer_t *importer
692 h264_importer_t *h264_imp = (h264_importer_t *)importer->info;
693 lsmash_codec_specific_t *cs = (lsmash_codec_specific_t *)lsmash_get_entry_data( h264_imp->avcC_list, ++ h264_imp->avcC_number );
694 if( !cs || !cs->data.structured )
696 lsmash_destroy_codec_specific_data( cs );
697 return NULL;
699 lsmash_video_summary_t *summary = h264_create_summary( (lsmash_h264_specific_parameters_t *)cs->data.structured,
700 &h264_imp->info.sps, h264_imp->max_au_length );
701 if( !summary )
703 lsmash_destroy_codec_specific_data( cs );
704 return NULL;
706 if( lsmash_add_entry( importer->summaries, summary ) < 0 )
708 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
709 return NULL;
711 summary->sample_per_field = h264_imp->field_pic_present;
712 return summary;
715 static int h264_analyze_whole_stream
717 importer_t *importer
720 /* Parse all NALU in the stream for preparation of calculating timestamps. */
721 uint32_t npt_alloc = (1 << 12) * sizeof(nal_pic_timing_t);
722 nal_pic_timing_t *npt = lsmash_malloc( npt_alloc );
723 if( !npt )
724 return LSMASH_ERR_MEMORY_ALLOC;
725 uint32_t picture_stats[H264_PICTURE_TYPE_NONE + 1] = { 0 };
726 uint32_t num_access_units = 0;
727 lsmash_class_t *logger = &(lsmash_class_t){ "H.264" };
728 lsmash_log( &logger, LSMASH_LOG_INFO, "Analyzing stream as H.264\r" );
729 h264_importer_t *h264_imp = (h264_importer_t *)importer->info;
730 h264_info_t *info = &h264_imp->info;
731 importer->status = IMPORTER_OK;
732 int err = LSMASH_ERR_MEMORY_ALLOC;
733 while( importer->status != IMPORTER_EOF )
735 #if 0
736 lsmash_log( &logger, LSMASH_LOG_INFO, "Analyzing stream as H.264: %"PRIu32"\n", num_access_units + 1 );
737 #endif
738 h264_picture_info_t *picture = &info->au.picture;
739 h264_picture_info_t prev_picture = *picture;
740 if( (err = h264_get_access_unit_internal( importer, 1 )) < 0
741 || (err = h264_calculate_poc( info, picture, &prev_picture )) < 0 )
742 goto fail;
743 h264_importer_check_eof( importer, &info->au );
744 if( npt_alloc <= num_access_units * sizeof(nal_pic_timing_t) )
746 uint32_t alloc = 2 * num_access_units * sizeof(nal_pic_timing_t);
747 nal_pic_timing_t *temp = (nal_pic_timing_t *)lsmash_realloc( npt, alloc );
748 if( !temp )
749 goto fail;
750 npt = temp;
751 npt_alloc = alloc;
753 h264_imp->field_pic_present |= picture->field_pic_flag;
754 npt[num_access_units].poc = picture->PicOrderCnt;
755 npt[num_access_units].delta = picture->delta;
756 npt[num_access_units].poc_delta = picture->field_pic_flag ? 1 : 2;
757 npt[num_access_units].reset = picture->has_mmco5;
758 ++num_access_units;
759 h264_imp->max_au_length = LSMASH_MAX( info->au.length, h264_imp->max_au_length );
760 if( picture->idr )
761 ++picture_stats[H264_PICTURE_TYPE_IDR];
762 else if( picture->type >= H264_PICTURE_TYPE_NONE )
763 ++picture_stats[H264_PICTURE_TYPE_NONE];
764 else
765 ++picture_stats[ picture->type ];
767 lsmash_log_refresh_line( &logger );
768 lsmash_log( &logger, LSMASH_LOG_INFO,
769 "IDR: %"PRIu32", I: %"PRIu32", P: %"PRIu32", B: %"PRIu32", "
770 "SI: %"PRIu32", SP: %"PRIu32", Unknown: %"PRIu32"\n",
771 picture_stats[H264_PICTURE_TYPE_IDR ],
772 picture_stats[H264_PICTURE_TYPE_I ],
773 picture_stats[H264_PICTURE_TYPE_I_P ],
774 picture_stats[H264_PICTURE_TYPE_I_P_B ],
775 picture_stats[H264_PICTURE_TYPE_SI ]
776 + picture_stats[H264_PICTURE_TYPE_I_SI ],
777 picture_stats[H264_PICTURE_TYPE_SI_SP ]
778 + picture_stats[H264_PICTURE_TYPE_I_SI_P_SP ]
779 + picture_stats[H264_PICTURE_TYPE_I_SI_P_SP_B],
780 picture_stats[H264_PICTURE_TYPE_NONE ] );
781 /* Copy and append the last Codec Specific info. */
782 if( (err = h264_store_codec_specific( h264_imp, &info->avcC_param )) < 0 )
783 goto fail;
784 /* Set up the first summary. */
785 lsmash_video_summary_t *summary = h264_setup_first_summary( importer );
786 if( !summary )
788 err = LSMASH_ERR_NAMELESS;
789 goto fail;
791 /* Allocate timestamps. */
792 lsmash_media_ts_t *timestamp = lsmash_malloc( num_access_units * sizeof(lsmash_media_ts_t) );
793 if( !timestamp )
794 goto fail;
795 /* Count leading samples that are undecodable. */
796 for( uint32_t i = 0; i < num_access_units; i++ )
798 if( npt[i].poc == 0 )
799 break;
800 ++ h264_imp->num_undecodable;
802 /* Deduplicate POCs. */
803 uint32_t max_composition_delay = 0;
804 nalu_deduplicate_poc( npt, &max_composition_delay, num_access_units, 32 );
805 /* Generate timestamps. */
806 nalu_generate_timestamps_from_poc( importer, timestamp, npt,
807 &h264_imp->composition_reordering_present,
808 &h264_imp->last_delta,
809 max_composition_delay, num_access_units );
810 nalu_reduce_timescale( timestamp, npt, &h264_imp->last_delta, &summary->timescale, num_access_units );
811 lsmash_free( npt );
812 h264_imp->ts_list.sample_count = num_access_units;
813 h264_imp->ts_list.timestamp = timestamp;
814 return 0;
815 fail:
816 lsmash_log_refresh_line( &logger );
817 lsmash_free( npt );
818 return err;
821 static int h264_importer_probe( importer_t *importer )
823 /* Find the first start code. */
824 h264_importer_t *h264_imp = create_h264_importer( importer );
825 if( !h264_imp )
826 return LSMASH_ERR_MEMORY_ALLOC;
827 lsmash_bs_t *bs = importer->bs;
828 uint64_t first_sc_head_pos = nalu_find_first_start_code( bs );
829 int err;
830 if( first_sc_head_pos == NALU_NO_START_CODE_FOUND )
832 err = LSMASH_ERR_INVALID_DATA;
833 goto fail;
835 /* OK. It seems the stream has a long start code of H.264. */
836 importer->info = h264_imp;
837 h264_info_t *info = &h264_imp->info;
838 lsmash_bs_read_seek( bs, first_sc_head_pos, SEEK_SET );
839 h264_imp->sc_head_pos = first_sc_head_pos;
840 if( (err = h264_analyze_whole_stream( importer )) < 0 )
841 goto fail;
842 /* Go back to the start code of the first NALU. */
843 importer->status = IMPORTER_OK;
844 lsmash_bs_read_seek( bs, first_sc_head_pos, SEEK_SET );
845 h264_imp->sc_head_pos = first_sc_head_pos;
846 info->prev_nalu_type = H264_NALU_TYPE_UNSPECIFIED0;
847 uint8_t *temp_au = info->au.data;
848 uint8_t *temp_incomplete_au = info->au.incomplete_data;
849 memset( &info->au, 0, sizeof(h264_access_unit_t) );
850 info->au.data = temp_au;
851 info->au.incomplete_data = temp_incomplete_au;
852 memset( &info->slice, 0, sizeof(h264_slice_info_t) );
853 memset( &info->sps, 0, sizeof(h264_sps_t) );
854 memset( &info->pps, 0, sizeof(h264_pps_t) );
855 lsmash_remove_entries( info->avcC_param.parameter_sets->sps_list, isom_remove_dcr_ps );
856 lsmash_remove_entries( info->avcC_param.parameter_sets->pps_list, isom_remove_dcr_ps );
857 lsmash_remove_entries( info->avcC_param.parameter_sets->spsext_list, isom_remove_dcr_ps );
858 lsmash_destroy_h264_parameter_sets( &info->avcC_param_next );
859 return 0;
860 fail:
861 remove_h264_importer( h264_imp );
862 importer->info = NULL;
863 lsmash_remove_entries( importer->summaries, lsmash_cleanup_summary );
864 return err;
867 static uint32_t h264_importer_get_last_delta( importer_t *importer, uint32_t track_number )
869 debug_if( !importer || !importer->info )
870 return 0;
871 h264_importer_t *h264_imp = (h264_importer_t *)importer->info;
872 if( !h264_imp || track_number != 1 || importer->status != IMPORTER_EOF )
873 return 0;
874 return h264_imp->ts_list.sample_count
875 ? h264_imp->last_delta
876 : UINT32_MAX; /* arbitrary */
879 const importer_functions h264_importer =
881 { "H.264", offsetof( importer_t, log_level ) },
883 h264_importer_probe,
884 h264_importer_get_accessunit,
885 h264_importer_get_last_delta,
886 h264_importer_cleanup
889 /***************************************************************************
890 HEVC importer
891 ITU-T Recommendation H.265 (04/13)
892 ISO/IEC 14496-15:2014
893 ***************************************************************************/
894 #include "codecs/hevc.h"
896 typedef struct
898 hevc_info_t info;
899 lsmash_entry_list_t hvcC_list[1]; /* stored as lsmash_codec_specific_t */
900 lsmash_media_ts_list_t ts_list;
901 uint32_t max_au_length;
902 uint32_t num_undecodable;
903 uint32_t hvcC_number;
904 uint32_t last_delta;
905 uint64_t last_intra_cts;
906 uint64_t sc_head_pos;
907 uint8_t composition_reordering_present;
908 uint8_t field_pic_present;
909 uint8_t max_TemporalId;
910 } hevc_importer_t;
912 static void remove_hevc_importer( hevc_importer_t *hevc_imp )
914 if( !hevc_imp )
915 return;
916 lsmash_remove_entries( hevc_imp->hvcC_list, lsmash_destroy_codec_specific_data );
917 hevc_cleanup_parser( &hevc_imp->info );
918 lsmash_free( hevc_imp->ts_list.timestamp );
919 lsmash_free( hevc_imp );
922 static void hevc_importer_cleanup( importer_t *importer )
924 debug_if( importer && importer->info )
925 remove_hevc_importer( importer->info );
928 static hevc_importer_t *create_hevc_importer( importer_t *importer )
930 hevc_importer_t *hevc_imp = lsmash_malloc_zero( sizeof(hevc_importer_t) );
931 if( !hevc_imp )
932 return NULL;
933 if( hevc_setup_parser( &hevc_imp->info, 0 ) < 0 )
935 remove_hevc_importer( hevc_imp );
936 return NULL;
938 lsmash_init_entry_list( hevc_imp->hvcC_list );
939 hevc_imp->info.eos = 1;
940 return hevc_imp;
943 static inline int hevc_complete_au( hevc_access_unit_t *au, int probe )
945 if( !au->picture.has_primary || au->incomplete_length == 0 )
946 return 0;
947 if( !probe )
948 memcpy( au->data, au->incomplete_data, au->incomplete_length );
949 au->TemporalId = au->picture.TemporalId;
950 au->length = au->incomplete_length;
951 au->incomplete_length = 0;
952 au->picture.has_primary = 0;
953 return 1;
956 static void hevc_append_nalu_to_au( hevc_access_unit_t *au, uint8_t *src_nalu, uint32_t nalu_length, int probe )
958 if( !probe )
960 uint8_t *dst_nalu = au->incomplete_data + au->incomplete_length + NALU_DEFAULT_NALU_LENGTH_SIZE;
961 for( int i = NALU_DEFAULT_NALU_LENGTH_SIZE; i; i-- )
962 *(dst_nalu - i) = (nalu_length >> ((i - 1) * 8)) & 0xff;
963 memcpy( dst_nalu, src_nalu, nalu_length );
965 /* Note: picture->incomplete_au_length shall be 0 immediately after AU has completed.
966 * Therefore, possible_au_length in hevc_get_access_unit_internal() can't be used here
967 * to avoid increasing AU length monotonously through the entire stream. */
968 au->incomplete_length += NALU_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
971 static int hevc_get_au_internal_succeeded( hevc_importer_t *hevc_imp, hevc_access_unit_t *au )
973 au->number += 1;
974 return 0;
977 static int hevc_get_au_internal_failed( hevc_importer_t *hevc_imp, hevc_access_unit_t *au, int complete_au, int ret )
979 if( complete_au )
980 au->number += 1;
981 return ret;
984 static lsmash_video_summary_t *hevc_create_summary
986 lsmash_hevc_specific_parameters_t *param,
987 hevc_sps_t *sps,
988 uint32_t max_au_length
991 lsmash_video_summary_t *summary = (lsmash_video_summary_t *)lsmash_create_summary( LSMASH_SUMMARY_TYPE_VIDEO );
992 if( !summary )
993 return NULL;
994 /* Update summary here.
995 * max_au_length is set at the last of hevc_importer_probe function. */
996 lsmash_codec_specific_t *specific = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_HEVC,
997 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED );
998 specific->data.unstructured = lsmash_create_hevc_specific_info( param, &specific->size );
999 if( !specific->data.unstructured
1000 || lsmash_add_entry( &summary->opaque->list, specific ) < 0 )
1002 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
1003 lsmash_destroy_codec_specific_data( specific );
1004 return NULL;
1006 summary->sample_type = ISOM_CODEC_TYPE_HVC1_VIDEO;
1007 summary->max_au_length = max_au_length;
1008 summary->timescale = sps->vui.time_scale;
1009 summary->timebase = sps->vui.num_units_in_tick;
1010 summary->vfr = (param->constantFrameRate == 0);
1011 summary->sample_per_field = 0;
1012 summary->width = sps->cropped_width;
1013 summary->height = sps->cropped_height;
1014 summary->par_h = sps->vui.sar_width;
1015 summary->par_v = sps->vui.sar_height;
1016 summary->color.primaries_index = sps->vui.colour_primaries != 2 ? sps->vui.colour_primaries : 0;
1017 summary->color.transfer_index = sps->vui.transfer_characteristics != 2 ? sps->vui.transfer_characteristics : 0;
1018 summary->color.matrix_index = sps->vui.matrix_coeffs != 2 ? sps->vui.matrix_coeffs : 0;
1019 summary->color.full_range = sps->vui.video_full_range_flag;
1020 lsmash_convert_crop_into_clap( sps->vui.def_disp_win_offset, summary->width, summary->height, &summary->clap );
1021 return summary;
1024 static int hevc_store_codec_specific
1026 hevc_importer_t *hevc_imp,
1027 lsmash_hevc_specific_parameters_t *hvcC_param
1030 lsmash_codec_specific_t *src_cs = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_HEVC,
1031 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED );
1032 if( !src_cs )
1033 return LSMASH_ERR_NAMELESS;
1034 lsmash_hevc_specific_parameters_t *src_param = (lsmash_hevc_specific_parameters_t *)src_cs->data.structured;
1035 *src_param = *hvcC_param;
1036 lsmash_codec_specific_t *dst_cs = lsmash_convert_codec_specific_format( src_cs, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED );
1037 src_param->parameter_arrays = NULL; /* Avoid freeing parameter arrays within hvcC_param. */
1038 lsmash_destroy_codec_specific_data( src_cs );
1039 if( !dst_cs )
1041 lsmash_destroy_codec_specific_data( dst_cs );
1042 return LSMASH_ERR_NAMELESS;
1044 if( lsmash_add_entry( hevc_imp->hvcC_list, dst_cs ) < 0 )
1046 lsmash_destroy_codec_specific_data( dst_cs );
1047 return LSMASH_ERR_MEMORY_ALLOC;
1049 return 0;
1052 static inline void hevc_new_access_unit( hevc_access_unit_t *au )
1054 au->length = 0;
1055 au->picture.type = HEVC_PICTURE_TYPE_NONE;
1056 au->picture.random_accessible = 0;
1057 au->picture.recovery_poc_cnt = 0;
1060 /* If probe equals 0, don't get the actual data (EBPS) of an access unit and only parse NALU. */
1061 static int hevc_get_access_unit_internal
1063 importer_t *importer,
1064 int probe
1067 hevc_importer_t *hevc_imp = (hevc_importer_t *)importer->info;
1068 hevc_info_t *info = &hevc_imp->info;
1069 hevc_slice_info_t *slice = &info->slice;
1070 hevc_access_unit_t *au = &info->au;
1071 hevc_picture_info_t *picture = &au->picture;
1072 hevc_stream_buffer_t *sb = &info->buffer;
1073 lsmash_bs_t *bs = importer->bs;
1074 int complete_au = 0;
1075 hevc_new_access_unit( au );
1076 while( 1 )
1078 hevc_nalu_header_t nuh;
1079 uint64_t start_code_length;
1080 uint64_t trailing_zero_bytes;
1081 uint64_t nalu_length = hevc_find_next_start_code( bs, &nuh, &start_code_length, &trailing_zero_bytes );
1082 if( start_code_length <= NALU_SHORT_START_CODE_LENGTH && lsmash_bs_is_end( bs, nalu_length ) )
1084 /* For the last NALU.
1085 * This NALU already has been appended into the latest access unit and parsed. */
1086 hevc_update_picture_info( info, picture, slice, &info->sps, &info->sei );
1087 complete_au = hevc_complete_au( au, probe );
1088 if( complete_au )
1089 return hevc_get_au_internal_succeeded( hevc_imp, au );
1090 else
1091 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, LSMASH_ERR_INVALID_DATA );
1093 uint8_t nalu_type = nuh.nal_unit_type;
1094 uint64_t next_sc_head_pos = hevc_imp->sc_head_pos
1095 + start_code_length
1096 + nalu_length
1097 + trailing_zero_bytes;
1098 #if 0
1099 if( probe )
1101 fprintf( stderr, "NALU type: %"PRIu8" \n", nalu_type );
1102 fprintf( stderr, " NALU header position: %"PRIx64" \n", hevc_imp->sc_head_pos + start_code_length );
1103 fprintf( stderr, " EBSP position: %"PRIx64" \n", hevc_imp->sc_head_pos + start_code_length + nuh.length );
1104 fprintf( stderr, " EBSP length: %"PRIx64" (%"PRIu64") \n", nalu_length - nuh.length, nalu_length - nuh.length );
1105 fprintf( stderr, " trailing_zero_bytes: %"PRIx64" \n", trailing_zero_bytes );
1106 fprintf( stderr, " Next start code position: %"PRIx64"\n", next_sc_head_pos );
1108 #endif
1109 /* Check if the end of sequence. Used for POC calculation. */
1110 info->eos |= info->prev_nalu_type == HEVC_NALU_TYPE_EOS
1111 || info->prev_nalu_type == HEVC_NALU_TYPE_EOB;
1112 /* Process the current NALU by its type. */
1113 if( nalu_type == HEVC_NALU_TYPE_FD )
1115 /* We don't support streams with both filler and HRD yet. Otherwise, just skip filler. */
1116 if( info->sps.vui.hrd.present )
1117 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, LSMASH_ERR_PATCH_WELCOME );
1119 else if( nalu_type <= HEVC_NALU_TYPE_RASL_R
1120 || (nalu_type >= HEVC_NALU_TYPE_BLA_W_LP && nalu_type <= HEVC_NALU_TYPE_CRA)
1121 || (nalu_type >= HEVC_NALU_TYPE_VPS && nalu_type <= HEVC_NALU_TYPE_SUFFIX_SEI) )
1123 int err;
1124 /* Increase the buffer if needed. */
1125 uint64_t possible_au_length = au->incomplete_length + NALU_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
1126 if( sb->bank->buffer_size < possible_au_length
1127 && (err = hevc_supplement_buffer( sb, au, 2 * possible_au_length )) < 0 )
1129 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to increase the buffer size.\n" );
1130 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, err );
1132 /* Get the EBSP of the current NALU here. */
1133 uint8_t *nalu = lsmash_bs_get_buffer_data( bs ) + start_code_length;
1134 if( nalu_type <= HEVC_NALU_TYPE_RSV_VCL31 )
1136 /* VCL NALU (slice) */
1137 hevc_slice_info_t prev_slice = *slice;
1138 if( (err = hevc_parse_slice_segment_header( info, &nuh, sb->rbsp,
1139 nalu + nuh.length,
1140 nalu_length - nuh.length )) < 0 )
1141 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, err );
1142 if( probe && info->hvcC_pending )
1144 /* Copy and append a Codec Specific info. */
1145 if( (err = hevc_store_codec_specific( hevc_imp, &info->hvcC_param )) < 0 )
1146 return err;
1148 if( (err = hevc_move_pending_hvcC_param( info )) < 0 )
1149 return err;
1150 if( prev_slice.present )
1152 /* Check whether the AU that contains the previous VCL NALU completed or not. */
1153 if( hevc_find_au_delimit_by_slice_info( info, slice, &prev_slice ) )
1155 /* The current NALU is the first VCL NALU of the primary coded picture of a new AU.
1156 * Therefore, the previous slice belongs to the AU you want at this time. */
1157 hevc_update_picture_info( info, picture, &prev_slice, &info->sps, &info->sei );
1158 complete_au = hevc_complete_au( au, probe );
1160 else
1161 hevc_update_picture_info_for_slice( info, picture, &prev_slice );
1163 hevc_append_nalu_to_au( au, nalu, nalu_length, probe );
1164 slice->present = 1;
1166 else
1168 if( hevc_find_au_delimit_by_nalu_type( nalu_type, info->prev_nalu_type ) )
1170 /* The last slice belongs to the AU you want at this time. */
1171 hevc_update_picture_info( info, picture, slice, &info->sps, &info->sei );
1172 complete_au = hevc_complete_au( au, probe );
1174 switch( nalu_type )
1176 case HEVC_NALU_TYPE_PREFIX_SEI :
1177 case HEVC_NALU_TYPE_SUFFIX_SEI :
1179 if( (err = hevc_parse_sei( info->bits, &info->vps, &info->sps, &info->sei, &nuh,
1180 sb->rbsp, nalu + nuh.length, nalu_length - nuh.length )) < 0 )
1181 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, err );
1182 hevc_append_nalu_to_au( au, nalu, nalu_length, probe );
1183 break;
1185 case HEVC_NALU_TYPE_VPS :
1186 if( (err = hevc_try_to_append_dcr_nalu( info, HEVC_DCR_NALU_TYPE_VPS, nalu, nalu_length )) < 0 )
1187 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, err );
1188 break;
1189 case HEVC_NALU_TYPE_SPS :
1190 if( (err = hevc_try_to_append_dcr_nalu( info, HEVC_DCR_NALU_TYPE_SPS, nalu, nalu_length )) < 0 )
1191 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, err );
1192 break;
1193 case HEVC_NALU_TYPE_PPS :
1194 if( (err = hevc_try_to_append_dcr_nalu( info, HEVC_DCR_NALU_TYPE_PPS, nalu, nalu_length )) < 0 )
1195 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, err );
1196 break;
1197 case HEVC_NALU_TYPE_AUD : /* We drop access unit delimiters. */
1198 break;
1199 default :
1200 hevc_append_nalu_to_au( au, nalu, nalu_length, probe );
1201 break;
1203 if( info->hvcC_pending )
1204 importer->status = IMPORTER_CHANGE;
1207 /* Move to the first byte of the next start code. */
1208 info->prev_nalu_type = nalu_type;
1209 if( lsmash_bs_read_seek( bs, next_sc_head_pos, SEEK_SET ) != next_sc_head_pos )
1211 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to seek the next start code.\n" );
1212 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, LSMASH_ERR_NAMELESS );
1214 if( !lsmash_bs_is_end( bs, NALU_SHORT_START_CODE_LENGTH ) )
1215 hevc_imp->sc_head_pos = next_sc_head_pos;
1216 /* If there is no more data in the stream, and flushed chunk of NALUs, flush it as complete AU here. */
1217 else if( au->incomplete_length && au->length == 0 )
1219 hevc_update_picture_info( info, picture, slice, &info->sps, &info->sei );
1220 hevc_complete_au( au, probe );
1221 return hevc_get_au_internal_succeeded( hevc_imp, au );
1223 if( complete_au )
1224 return hevc_get_au_internal_succeeded( hevc_imp, au );
1228 static inline void hevc_importer_check_eof( importer_t *importer, hevc_access_unit_t *au )
1230 if( lsmash_bs_is_end( importer->bs, 0 ) && au->incomplete_length == 0 )
1231 importer->status = IMPORTER_EOF;
1232 else if( importer->status != IMPORTER_CHANGE )
1233 importer->status = IMPORTER_OK;
1236 static int hevc_importer_get_accessunit( importer_t *importer, uint32_t track_number, lsmash_sample_t **p_sample )
1238 if( !importer->info )
1239 return LSMASH_ERR_NAMELESS;
1240 if( track_number != 1 )
1241 return LSMASH_ERR_FUNCTION_PARAM;
1242 hevc_importer_t *hevc_imp = (hevc_importer_t *)importer->info;
1243 hevc_info_t *info = &hevc_imp->info;
1244 importer_status current_status = importer->status;
1245 if( current_status == IMPORTER_ERROR )
1246 return LSMASH_ERR_NAMELESS;
1247 if( current_status == IMPORTER_EOF )
1248 return IMPORTER_EOF;
1249 int err = hevc_get_access_unit_internal( importer, 0 );
1250 if( err < 0 )
1252 importer->status = IMPORTER_ERROR;
1253 return err;
1255 hevc_importer_check_eof( importer, &info->au );
1256 if( importer->status == IMPORTER_CHANGE && !info->hvcC_pending )
1257 current_status = IMPORTER_CHANGE;
1258 if( current_status == IMPORTER_CHANGE )
1260 /* Update the active summary. */
1261 lsmash_codec_specific_t *cs = (lsmash_codec_specific_t *)lsmash_get_entry_data( hevc_imp->hvcC_list, ++ hevc_imp->hvcC_number );
1262 if( !cs )
1263 return LSMASH_ERR_NAMELESS;
1264 lsmash_hevc_specific_parameters_t *hvcC_param = (lsmash_hevc_specific_parameters_t *)cs->data.structured;
1265 lsmash_video_summary_t *summary = hevc_create_summary( hvcC_param, &info->sps, hevc_imp->max_au_length );
1266 if( !summary )
1267 return LSMASH_ERR_NAMELESS;
1268 lsmash_remove_entry( importer->summaries, track_number, lsmash_cleanup_summary );
1269 if( lsmash_add_entry( importer->summaries, summary ) < 0 )
1271 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
1272 return LSMASH_ERR_MEMORY_ALLOC;
1274 importer->status = IMPORTER_OK;
1276 lsmash_sample_t *sample = lsmash_create_sample( hevc_imp->max_au_length );
1277 if( !sample )
1278 return LSMASH_ERR_MEMORY_ALLOC;
1279 *p_sample = sample;
1280 hevc_access_unit_t *au = &info->au;
1281 hevc_picture_info_t *picture = &au->picture;
1282 sample->dts = hevc_imp->ts_list.timestamp[ au->number - 1 ].dts;
1283 sample->cts = hevc_imp->ts_list.timestamp[ au->number - 1 ].cts;
1284 /* Set property of disposability. */
1285 if( picture->sublayer_nonref && au->TemporalId == hevc_imp->max_TemporalId )
1286 /* Sub-layer non-reference pictures are not referenced by subsequent pictures of
1287 * the same sub-layer in decoding order. */
1288 sample->prop.disposable = ISOM_SAMPLE_IS_DISPOSABLE;
1289 else
1290 sample->prop.disposable = ISOM_SAMPLE_IS_NOT_DISPOSABLE;
1291 /* Set property of leading. */
1292 if( picture->radl || picture->rasl )
1293 sample->prop.leading = picture->radl ? ISOM_SAMPLE_IS_DECODABLE_LEADING : ISOM_SAMPLE_IS_UNDECODABLE_LEADING;
1294 else
1296 if( au->number < hevc_imp->num_undecodable )
1297 sample->prop.leading = ISOM_SAMPLE_IS_UNDECODABLE_LEADING;
1298 else
1300 if( picture->independent || sample->cts >= hevc_imp->last_intra_cts )
1301 sample->prop.leading = ISOM_SAMPLE_IS_NOT_LEADING;
1302 else
1303 sample->prop.leading = ISOM_SAMPLE_IS_UNDECODABLE_LEADING;
1306 if( picture->independent )
1307 hevc_imp->last_intra_cts = sample->cts;
1308 /* Set property of independence. */
1309 sample->prop.independent = picture->independent ? ISOM_SAMPLE_IS_INDEPENDENT : ISOM_SAMPLE_IS_NOT_INDEPENDENT;
1310 sample->prop.redundant = ISOM_SAMPLE_HAS_NO_REDUNDANCY;
1311 sample->prop.post_roll.identifier = picture->poc;
1312 if( picture->random_accessible )
1314 if( picture->irap )
1316 sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
1317 if( picture->closed_rap )
1318 sample->prop.ra_flags |= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_CLOSED_RAP;
1319 else
1320 sample->prop.ra_flags |= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP;
1322 else if( picture->recovery_poc_cnt )
1324 sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_POST_ROLL_START;
1325 sample->prop.post_roll.complete = picture->poc + picture->recovery_poc_cnt;
1327 else
1328 sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP;
1330 sample->length = au->length;
1331 memcpy( sample->data, au->data, au->length );
1332 return current_status;
1335 static lsmash_video_summary_t *hevc_setup_first_summary
1337 importer_t *importer
1340 hevc_importer_t *hevc_imp = (hevc_importer_t *)importer->info;
1341 lsmash_codec_specific_t *cs = (lsmash_codec_specific_t *)lsmash_get_entry_data( hevc_imp->hvcC_list, ++ hevc_imp->hvcC_number );
1342 if( !cs || !cs->data.structured )
1344 lsmash_destroy_codec_specific_data( cs );
1345 return NULL;
1347 lsmash_video_summary_t *summary = hevc_create_summary( (lsmash_hevc_specific_parameters_t *)cs->data.structured,
1348 &hevc_imp->info.sps, hevc_imp->max_au_length );
1349 if( !summary )
1351 lsmash_destroy_codec_specific_data( cs );
1352 return NULL;
1354 if( lsmash_add_entry( importer->summaries, summary ) < 0 )
1356 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
1357 return NULL;
1359 summary->sample_per_field = hevc_imp->field_pic_present;
1360 return summary;
1363 static int hevc_analyze_whole_stream
1365 importer_t *importer
1368 /* Parse all NALU in the stream for preparation of calculating timestamps. */
1369 uint32_t npt_alloc = (1 << 12) * sizeof(nal_pic_timing_t);
1370 nal_pic_timing_t *npt = (nal_pic_timing_t *)lsmash_malloc( npt_alloc );
1371 if( !npt )
1372 return LSMASH_ERR_MEMORY_ALLOC;
1373 uint32_t picture_stats[HEVC_PICTURE_TYPE_NONE + 1] = { 0 };
1374 uint32_t num_access_units = 0;
1375 lsmash_class_t *logger = &(lsmash_class_t){ "HEVC" };
1376 lsmash_log( &logger, LSMASH_LOG_INFO, "Analyzing stream as HEVC\r" );
1377 hevc_importer_t *hevc_imp = (hevc_importer_t *)importer->info;
1378 hevc_info_t *info = &hevc_imp->info;
1379 importer->status = IMPORTER_OK;
1380 int err = LSMASH_ERR_MEMORY_ALLOC;
1381 while( importer->status != IMPORTER_EOF )
1383 #if 0
1384 lsmash_log( &logger, LSMASH_LOG_INFO, "Analyzing stream as HEVC: %"PRIu32"\n", num_access_units + 1 );
1385 #endif
1386 hevc_picture_info_t *picture = &info->au.picture;
1387 hevc_picture_info_t prev_picture = *picture;
1388 if( (err = hevc_get_access_unit_internal( importer, 1 )) < 0
1389 || (err = hevc_calculate_poc( info, &info->au.picture, &prev_picture )) < 0 )
1390 goto fail;
1391 hevc_importer_check_eof( importer, &info->au );
1392 if( npt_alloc <= num_access_units * sizeof(nal_pic_timing_t) )
1394 uint32_t alloc = 2 * num_access_units * sizeof(nal_pic_timing_t);
1395 nal_pic_timing_t *temp = (nal_pic_timing_t *)lsmash_realloc( npt, alloc );
1396 if( !temp )
1397 goto fail;
1398 npt = temp;
1399 npt_alloc = alloc;
1401 hevc_imp->field_pic_present |= picture->field_coded;
1402 npt[num_access_units].poc = picture->poc;
1403 npt[num_access_units].delta = picture->delta;
1404 npt[num_access_units].poc_delta = 1;
1405 npt[num_access_units].reset = 0;
1406 ++num_access_units;
1407 hevc_imp->max_au_length = LSMASH_MAX( hevc_imp->max_au_length, info->au.length );
1408 hevc_imp->max_TemporalId = LSMASH_MAX( hevc_imp->max_TemporalId, info->au.TemporalId );
1409 if( picture->idr )
1410 ++picture_stats[HEVC_PICTURE_TYPE_IDR];
1411 else if( picture->irap )
1412 ++picture_stats[ picture->broken_link ? HEVC_PICTURE_TYPE_BLA : HEVC_PICTURE_TYPE_CRA ];
1413 else if( picture->type >= HEVC_PICTURE_TYPE_NONE )
1414 ++picture_stats[HEVC_PICTURE_TYPE_NONE];
1415 else
1416 ++picture_stats[ picture->type ];
1418 lsmash_log_refresh_line( &logger );
1419 lsmash_log( &logger, LSMASH_LOG_INFO,
1420 "IDR: %"PRIu32", CRA: %"PRIu32", BLA: %"PRIu32", I: %"PRIu32", P: %"PRIu32", B: %"PRIu32", Unknown: %"PRIu32"\n",
1421 picture_stats[HEVC_PICTURE_TYPE_IDR], picture_stats[HEVC_PICTURE_TYPE_CRA],
1422 picture_stats[HEVC_PICTURE_TYPE_BLA], picture_stats[HEVC_PICTURE_TYPE_I],
1423 picture_stats[HEVC_PICTURE_TYPE_I_P], picture_stats[HEVC_PICTURE_TYPE_I_P_B],
1424 picture_stats[HEVC_PICTURE_TYPE_NONE]);
1425 /* Copy and append the last Codec Specific info. */
1426 if( (err = hevc_store_codec_specific( hevc_imp, &info->hvcC_param )) < 0 )
1427 goto fail;
1428 /* Set up the first summary. */
1429 lsmash_video_summary_t *summary = hevc_setup_first_summary( importer );
1430 if( !summary )
1432 err = LSMASH_ERR_NAMELESS;
1433 goto fail;
1435 /* */
1436 lsmash_media_ts_t *timestamp = lsmash_malloc( num_access_units * sizeof(lsmash_media_ts_t) );
1437 if( !timestamp )
1438 goto fail;
1439 /* Count leading samples that are undecodable. */
1440 for( uint32_t i = 0; i < num_access_units; i++ )
1442 if( npt[i].poc == 0 )
1443 break;
1444 ++ hevc_imp->num_undecodable;
1446 /* Deduplicate POCs. */
1447 uint32_t max_composition_delay = 0;
1448 nalu_deduplicate_poc( npt, &max_composition_delay, num_access_units, 15 );
1449 /* Generate timestamps. */
1450 nalu_generate_timestamps_from_poc( importer, timestamp, npt,
1451 &hevc_imp->composition_reordering_present,
1452 &hevc_imp->last_delta,
1453 max_composition_delay, num_access_units );
1454 summary->timescale *= 2; /* We assume that picture timing is in field level.
1455 * For HEVC, it seems time_scale is set in frame level basically.
1456 * So multiply by 2 for reducing timebase and timescale. */
1457 nalu_reduce_timescale( timestamp, npt, &hevc_imp->last_delta, &summary->timescale, num_access_units );
1458 lsmash_free( npt );
1459 hevc_imp->ts_list.sample_count = num_access_units;
1460 hevc_imp->ts_list.timestamp = timestamp;
1461 return 0;
1462 fail:
1463 lsmash_log_refresh_line( &logger );
1464 lsmash_free( npt );
1465 return err;
1468 static int hevc_importer_probe( importer_t *importer )
1470 /* Find the first start code. */
1471 hevc_importer_t *hevc_imp = create_hevc_importer( importer );
1472 if( !hevc_imp )
1473 return LSMASH_ERR_MEMORY_ALLOC;
1474 lsmash_bs_t *bs = importer->bs;
1475 uint64_t first_sc_head_pos = nalu_find_first_start_code( bs );
1476 int err;
1477 if( first_sc_head_pos == NALU_NO_START_CODE_FOUND )
1479 err = LSMASH_ERR_INVALID_DATA;
1480 goto fail;
1482 /* OK. It seems the stream has a long start code of HEVC. */
1483 importer->info = hevc_imp;
1484 hevc_info_t *info = &hevc_imp->info;
1485 lsmash_bs_read_seek( bs, first_sc_head_pos, SEEK_SET );
1486 hevc_imp->sc_head_pos = first_sc_head_pos;
1487 if( (err = hevc_analyze_whole_stream( importer )) < 0 )
1488 goto fail;
1489 /* Go back to the start code of the first NALU. */
1490 importer->status = IMPORTER_OK;
1491 lsmash_bs_read_seek( bs, first_sc_head_pos, SEEK_SET );
1492 hevc_imp->sc_head_pos = first_sc_head_pos;
1493 info->prev_nalu_type = HEVC_NALU_TYPE_UNKNOWN;
1494 uint8_t *temp_au = info->au.data;
1495 uint8_t *temp_incomplete_au = info->au.incomplete_data;
1496 memset( &info->au, 0, sizeof(hevc_access_unit_t) );
1497 info->au.data = temp_au;
1498 info->au.incomplete_data = temp_incomplete_au;
1499 memset( &info->slice, 0, sizeof(hevc_slice_info_t) );
1500 memset( &info->vps, 0, sizeof(hevc_vps_t) );
1501 memset( &info->sps, 0, sizeof(hevc_sps_t) );
1502 memset( &info->pps, 0, SIZEOF_PPS_EXCLUDING_HEAP );
1503 for( int i = 0; i < HEVC_DCR_NALU_TYPE_NUM; i++ )
1504 lsmash_remove_entries( info->hvcC_param.parameter_arrays->ps_array[i].list, isom_remove_dcr_ps );
1505 lsmash_destroy_hevc_parameter_arrays( &info->hvcC_param_next );
1506 return 0;
1507 fail:
1508 remove_hevc_importer( hevc_imp );
1509 importer->info = NULL;
1510 lsmash_remove_entries( importer->summaries, lsmash_cleanup_summary );
1511 return err;
1514 static uint32_t hevc_importer_get_last_delta( importer_t *importer, uint32_t track_number )
1516 debug_if( !importer || !importer->info )
1517 return 0;
1518 hevc_importer_t *hevc_imp = (hevc_importer_t *)importer->info;
1519 if( !hevc_imp || track_number != 1 || importer->status != IMPORTER_EOF )
1520 return 0;
1521 return hevc_imp->ts_list.sample_count
1522 ? hevc_imp->last_delta
1523 : UINT32_MAX; /* arbitrary */
1526 const importer_functions hevc_importer =
1528 { "HEVC", offsetof( importer_t, log_level ) },
1530 hevc_importer_probe,
1531 hevc_importer_get_accessunit,
1532 hevc_importer_get_last_delta,
1533 hevc_importer_cleanup