write: Fix type size for mdcv luminance
[L-SMASH.git] / importer / nalu_imp.c
blob409d22dc11e201f8b059872fc224c588bc489dd7
1 /*****************************************************************************
2 * nalu_imp.c
3 *****************************************************************************
4 * Copyright (C) 2011-2017 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
21 /* This file is available under an ISC license. */
23 #include "common/internal.h" /* must be placed first */
25 #include <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 last_sync_cts;
51 uint64_t sc_head_pos;
52 uint8_t composition_reordering_present;
53 uint8_t field_pic_present;
54 } h264_importer_t;
56 typedef struct
58 int64_t poc;
59 uint32_t delta;
60 uint16_t poc_delta;
61 uint16_t reset;
62 } nal_pic_timing_t;
64 static void remove_h264_importer( h264_importer_t *h264_imp )
66 if( !h264_imp )
67 return;
68 lsmash_list_remove_entries( h264_imp->avcC_list );
69 h264_cleanup_parser( &h264_imp->info );
70 lsmash_free( h264_imp->ts_list.timestamp );
71 lsmash_free( h264_imp );
74 static void h264_importer_cleanup( importer_t *importer )
76 debug_if( importer && importer->info )
77 remove_h264_importer( importer->info );
80 static h264_importer_t *create_h264_importer( importer_t *importer )
82 h264_importer_t *h264_imp = lsmash_malloc_zero( sizeof(h264_importer_t) );
83 if( !h264_imp )
84 return NULL;
85 if( h264_setup_parser( &h264_imp->info, 0 ) < 0 )
87 remove_h264_importer( h264_imp );
88 return NULL;
90 lsmash_list_init( h264_imp->avcC_list, lsmash_destroy_codec_specific_data );
91 return h264_imp;
94 static inline int h264_complete_au( h264_access_unit_t *au, int probe )
96 if( !au->picture.has_primary || au->incomplete_length == 0 )
97 return 0;
98 if( !probe )
99 memcpy( au->data, au->incomplete_data, au->incomplete_length );
100 au->length = au->incomplete_length;
101 au->incomplete_length = 0;
102 au->picture.has_primary = 0;
103 return 1;
106 static void h264_append_nalu_to_au( h264_access_unit_t *au, uint8_t *src_nalu, uint32_t nalu_length, int probe )
108 if( !probe )
110 uint8_t *dst_nalu = au->incomplete_data + au->incomplete_length + NALU_DEFAULT_NALU_LENGTH_SIZE;
111 for( int i = NALU_DEFAULT_NALU_LENGTH_SIZE; i; i-- )
112 *(dst_nalu - i) = (nalu_length >> ((i - 1) * 8)) & 0xff;
113 memcpy( dst_nalu, src_nalu, nalu_length );
115 /* Note: au->incomplete_length shall be 0 immediately after AU has completed.
116 * Therefore, possible_au_length in h264_get_access_unit_internal() can't be used here
117 * to avoid increasing AU length monotonously through the entire stream. */
118 au->incomplete_length += NALU_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
121 static int h264_get_au_internal_succeeded( h264_importer_t *h264_imp, h264_access_unit_t *au )
123 au->number += 1;
124 return 0;
127 static int h264_get_au_internal_failed( h264_importer_t *h264_imp, h264_access_unit_t *au, int complete_au, int ret )
129 if( complete_au )
130 au->number += 1;
131 return ret;
134 static lsmash_video_summary_t *h264_create_summary
136 lsmash_h264_specific_parameters_t *param,
137 h264_sps_t *sps,
138 uint32_t max_au_length
141 lsmash_video_summary_t *summary = (lsmash_video_summary_t *)lsmash_create_summary( LSMASH_SUMMARY_TYPE_VIDEO );
142 if( !summary )
143 return NULL;
144 /* Update summary here.
145 * max_au_length is set at the last of mp4sys_h264_probe function. */
146 lsmash_codec_specific_t *cs = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264,
147 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED );
148 if( !cs )
150 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
151 return NULL;
153 cs->data.unstructured = lsmash_create_h264_specific_info( param, &cs->size );
154 if( !cs->data.unstructured
155 || lsmash_list_add_entry( &summary->opaque->list, cs ) < 0 )
157 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
158 lsmash_destroy_codec_specific_data( cs );
159 return NULL;
161 summary->sample_type = ISOM_CODEC_TYPE_AVC1_VIDEO;
162 summary->max_au_length = max_au_length;
163 summary->timescale = sps->vui.time_scale;
164 summary->timebase = sps->vui.num_units_in_tick;
165 summary->vfr = !sps->vui.fixed_frame_rate_flag;
166 summary->sample_per_field = 0;
167 summary->width = sps->cropped_width;
168 summary->height = sps->cropped_height;
169 summary->par_h = sps->vui.sar_width;
170 summary->par_v = sps->vui.sar_height;
171 summary->color.primaries_index = sps->vui.colour_primaries;
172 summary->color.transfer_index = sps->vui.transfer_characteristics;
173 summary->color.matrix_index = sps->vui.matrix_coefficients;
174 summary->color.full_range = sps->vui.video_full_range_flag;
175 return summary;
178 static int h264_store_codec_specific
180 h264_importer_t *h264_imp,
181 lsmash_h264_specific_parameters_t *avcC_param
184 lsmash_codec_specific_t *src_cs = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264,
185 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED );
186 if( !src_cs )
187 return LSMASH_ERR_NAMELESS;
188 lsmash_h264_specific_parameters_t *src_param = (lsmash_h264_specific_parameters_t *)src_cs->data.structured;
189 *src_param = *avcC_param;
190 lsmash_codec_specific_t *dst_cs = lsmash_convert_codec_specific_format( src_cs, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED );
191 src_param->parameter_sets = NULL; /* Avoid freeing parameter sets within avcC_param. */
192 lsmash_destroy_codec_specific_data( src_cs );
193 if( !dst_cs )
195 lsmash_destroy_codec_specific_data( dst_cs );
196 return LSMASH_ERR_NAMELESS;
198 if( lsmash_list_add_entry( h264_imp->avcC_list, dst_cs ) < 0 )
200 lsmash_destroy_codec_specific_data( dst_cs );
201 return LSMASH_ERR_MEMORY_ALLOC;
203 return 0;
206 static inline void h264_new_access_unit
208 h264_access_unit_t *au
211 au->length = 0;
212 au->picture.type = H264_PICTURE_TYPE_NONE;
213 au->picture.random_accessible = 0;
214 au->picture.recovery_frame_cnt = 0;
215 au->picture.has_mmco5 = 0;
216 au->picture.has_redundancy = 0;
217 au->picture.broken_link_flag = 0;
220 /* If probe equals 0, don't get the actual data (EBPS) of an access unit and only parse NALU.
221 * Currently, you can get AU of AVC video elemental stream only, not AVC parameter set elemental stream defined in 14496-15. */
222 static int h264_get_access_unit_internal
224 importer_t *importer,
225 int probe
228 h264_importer_t *h264_imp = (h264_importer_t *)importer->info;
229 h264_info_t *info = &h264_imp->info;
230 h264_slice_info_t *slice = &info->slice;
231 h264_access_unit_t *au = &info->au;
232 h264_picture_info_t *picture = &au->picture;
233 h264_stream_buffer_t *sb = &info->buffer;
234 lsmash_bs_t *bs = importer->bs;
235 int complete_au = 0;
236 h264_new_access_unit( au );
237 while( 1 )
239 h264_nalu_header_t nuh;
240 uint64_t start_code_length;
241 uint64_t trailing_zero_bytes;
242 uint64_t nalu_length = h264_find_next_start_code( bs, &nuh, &start_code_length, &trailing_zero_bytes );
243 if( nalu_length == NALU_NO_START_CODE_FOUND )
245 /* For the last NALU.
246 * This NALU already has been appended into the latest access unit and parsed. */
247 h264_update_picture_info( info, picture, slice, &info->sei );
248 complete_au = h264_complete_au( au, probe );
249 if( complete_au )
250 return h264_get_au_internal_succeeded( h264_imp, au );
251 else
252 return h264_get_au_internal_failed( h264_imp, au, complete_au, LSMASH_ERR_INVALID_DATA );
254 uint8_t nalu_type = nuh.nal_unit_type;
255 uint64_t next_sc_head_pos = h264_imp->sc_head_pos
256 + start_code_length
257 + nalu_length
258 + trailing_zero_bytes;
259 #if 0
260 if( probe )
262 fprintf( stderr, "NALU type: %"PRIu8" \n", nalu_type );
263 fprintf( stderr, " NALU header position: %"PRIx64" \n", h264_imp->sc_head_pos + start_code_length );
264 fprintf( stderr, " EBSP position: %"PRIx64" \n", h264_imp->sc_head_pos + start_code_length + nuh.length );
265 fprintf( stderr, " EBSP length: %"PRIx64" (%"PRIu64") \n", nalu_length - nuh.length, nalu_length - nuh.length );
266 fprintf( stderr, " trailing_zero_bytes: %"PRIx64" \n", trailing_zero_bytes );
267 fprintf( stderr, " Next start code position: %"PRIx64"\n", next_sc_head_pos );
269 #endif
270 if( nalu_type == H264_NALU_TYPE_FD )
272 /* We don't support streams with both filler and HRD yet.
273 * Otherwise, just skip filler because 'avc1' and 'avc2' samples are forbidden to use filler. */
274 if( info->sps.vui.hrd.present )
275 return h264_get_au_internal_failed( h264_imp, au, complete_au, LSMASH_ERR_PATCH_WELCOME );
277 else if( (nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && nalu_type <= H264_NALU_TYPE_SPS_EXT)
278 || nalu_type == H264_NALU_TYPE_SLICE_AUX )
280 int err;
281 /* Increase the buffer if needed. */
282 uint64_t possible_au_length = au->incomplete_length + NALU_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
283 if( sb->bank->buffer_size < possible_au_length
284 && (err = h264_supplement_buffer( sb, au, 2 * possible_au_length )) < 0 )
286 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to increase the buffer size.\n" );
287 return h264_get_au_internal_failed( h264_imp, au, complete_au, err );
289 /* Get the EBSP of the current NALU here.
290 * AVC elemental stream defined in 14496-15 can recognizes from 0 to 13, and 19 of nal_unit_type.
291 * We don't support SVC and MVC elemental stream defined in 14496-15 yet. */
292 uint8_t *nalu = lsmash_bs_get_buffer_data( bs ) + start_code_length;
293 if( nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && nalu_type <= H264_NALU_TYPE_SLICE_IDR )
295 /* VCL NALU (slice) */
296 h264_slice_info_t prev_slice = *slice;
297 if( (err = h264_parse_slice( info, &nuh, sb->rbsp, nalu + nuh.length, nalu_length - nuh.length )) < 0 )
298 return h264_get_au_internal_failed( h264_imp, au, complete_au, err );
299 if( probe && info->avcC_pending )
301 /* Copy and append a Codec Specific info. */
302 if( (err = h264_store_codec_specific( h264_imp, &info->avcC_param )) < 0 )
303 return err;
305 if( (err = h264_move_pending_avcC_param( info )) < 0 )
306 return err;
307 if( prev_slice.present )
309 /* Check whether the AU that contains the previous VCL NALU completed or not. */
310 if( h264_find_au_delimit_by_slice_info( slice, &prev_slice ) )
312 /* The current NALU is the first VCL NALU of the primary coded picture of an new AU.
313 * Therefore, the previous slice belongs to the AU you want at this time. */
314 h264_update_picture_info( info, picture, &prev_slice, &info->sei );
315 complete_au = h264_complete_au( au, probe );
317 else
318 h264_update_picture_info_for_slice( info, picture, &prev_slice );
320 h264_append_nalu_to_au( au, nalu, nalu_length, probe );
321 slice->present = 1;
323 else
325 if( h264_find_au_delimit_by_nalu_type( nalu_type, info->prev_nalu_type ) )
327 /* The last slice belongs to the AU you want at this time. */
328 h264_update_picture_info( info, picture, slice, &info->sei );
329 complete_au = h264_complete_au( au, probe );
331 switch( nalu_type )
333 case H264_NALU_TYPE_SEI :
335 if( (err = h264_parse_sei( info->bits, &info->sps, &info->sei, sb->rbsp,
336 nalu + nuh.length,
337 nalu_length - nuh.length )) < 0 )
338 return h264_get_au_internal_failed( h264_imp, au, complete_au, err );
339 h264_append_nalu_to_au( au, nalu, nalu_length, probe );
340 break;
342 case H264_NALU_TYPE_SPS :
343 if( (err = h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_SPS, nalu, nalu_length )) < 0 )
344 return h264_get_au_internal_failed( h264_imp, au, complete_au, err );
345 break;
346 case H264_NALU_TYPE_PPS :
347 if( (err = h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_PPS, nalu, nalu_length )) < 0 )
348 return h264_get_au_internal_failed( h264_imp, au, complete_au, err );
349 break;
350 case H264_NALU_TYPE_AUD : /* We drop access unit delimiters. */
351 break;
352 case H264_NALU_TYPE_SPS_EXT :
353 if( (err = h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_SPSEXT, nalu, nalu_length )) < 0 )
354 return h264_get_au_internal_failed( h264_imp, au, complete_au, err );
355 break;
356 default :
357 h264_append_nalu_to_au( au, nalu, nalu_length, probe );
358 break;
360 if( info->avcC_pending )
361 importer->status = IMPORTER_CHANGE;
364 /* Move to the first byte of the next start code. */
365 info->prev_nalu_type = nalu_type;
366 if( lsmash_bs_read_seek( bs, next_sc_head_pos, SEEK_SET ) != next_sc_head_pos )
368 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to seek the next start code.\n" );
369 return h264_get_au_internal_failed( h264_imp, au, complete_au, LSMASH_ERR_NAMELESS );
371 /* Check if no more data to read from the stream. */
372 if( !lsmash_bs_is_end( bs, NALU_SHORT_START_CODE_LENGTH ) )
373 h264_imp->sc_head_pos = next_sc_head_pos;
374 /* If there is no more data in the stream, and flushed chunk of NALUs, flush it as complete AU here. */
375 else if( au->incomplete_length && au->length == 0 )
377 h264_update_picture_info( info, picture, slice, &info->sei );
378 h264_complete_au( au, probe );
379 return h264_get_au_internal_succeeded( h264_imp, au );
381 if( complete_au )
382 return h264_get_au_internal_succeeded( h264_imp, au );
386 static inline void h264_importer_check_eof( importer_t *importer, h264_access_unit_t *au )
388 /* AVC byte stream NALU consists of at least 4 bytes (start-code + NALU-header). */
389 if( lsmash_bs_is_end( importer->bs, NALU_SHORT_START_CODE_LENGTH ) && au->incomplete_length == 0 )
390 importer->status = IMPORTER_EOF;
391 else if( importer->status != IMPORTER_CHANGE )
392 importer->status = IMPORTER_OK;
395 static int h264_importer_get_accessunit
397 importer_t *importer,
398 uint32_t track_number,
399 lsmash_sample_t **p_sample
402 if( !importer->info )
403 return LSMASH_ERR_NAMELESS;
404 if( track_number != 1 )
405 return LSMASH_ERR_FUNCTION_PARAM;
406 h264_importer_t *h264_imp = (h264_importer_t *)importer->info;
407 h264_info_t *info = &h264_imp->info;
408 importer_status current_status = importer->status;
409 if( current_status == IMPORTER_ERROR )
410 return LSMASH_ERR_NAMELESS;
411 if( current_status == IMPORTER_EOF )
412 return IMPORTER_EOF;
413 int err = h264_get_access_unit_internal( importer, 0 );
414 if( err < 0 )
416 importer->status = IMPORTER_ERROR;
417 return err;
419 h264_importer_check_eof( importer, &info->au );
420 if( importer->status == IMPORTER_CHANGE && !info->avcC_pending )
421 current_status = IMPORTER_CHANGE;
422 if( current_status == IMPORTER_CHANGE )
424 /* Update the active summary. */
425 lsmash_codec_specific_t *cs = (lsmash_codec_specific_t *)lsmash_list_get_entry_data( h264_imp->avcC_list, ++ h264_imp->avcC_number );
426 if( !cs )
427 return LSMASH_ERR_NAMELESS;
428 lsmash_h264_specific_parameters_t *avcC_param = (lsmash_h264_specific_parameters_t *)cs->data.structured;
429 lsmash_video_summary_t *summary = h264_create_summary( avcC_param, &info->sps, h264_imp->max_au_length );
430 if( !summary )
431 return LSMASH_ERR_NAMELESS;
432 lsmash_list_remove_entry( importer->summaries, track_number );
433 if( lsmash_list_add_entry( importer->summaries, summary ) < 0 )
435 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
436 return LSMASH_ERR_MEMORY_ALLOC;
438 importer->status = IMPORTER_OK;
440 lsmash_sample_t *sample = lsmash_create_sample( h264_imp->max_au_length );
441 if( !sample )
442 return LSMASH_ERR_MEMORY_ALLOC;
443 *p_sample = sample;
444 h264_access_unit_t *au = &info->au;
445 h264_picture_info_t *picture = &au->picture;
446 sample->dts = h264_imp->ts_list.timestamp[ au->number - 1 ].dts;
447 sample->cts = h264_imp->ts_list.timestamp[ au->number - 1 ].cts;
448 if( au->number < h264_imp->num_undecodable )
449 sample->prop.leading = ISOM_SAMPLE_IS_UNDECODABLE_LEADING;
450 else
451 sample->prop.leading =
452 picture->independent ? ISOM_SAMPLE_IS_NOT_LEADING
453 : sample->cts >= h264_imp->last_intra_cts ? ISOM_SAMPLE_IS_NOT_LEADING
454 : sample->cts < h264_imp->last_sync_cts ? ISOM_SAMPLE_IS_DECODABLE_LEADING
455 : ISOM_SAMPLE_IS_UNDECODABLE_LEADING;
456 if( h264_imp->composition_reordering_present && !picture->disposable && !picture->idr )
457 sample->prop.allow_earlier = QT_SAMPLE_EARLIER_PTS_ALLOWED;
458 sample->prop.independent = picture->independent ? ISOM_SAMPLE_IS_INDEPENDENT : ISOM_SAMPLE_IS_NOT_INDEPENDENT;
459 sample->prop.disposable = picture->disposable ? ISOM_SAMPLE_IS_DISPOSABLE : ISOM_SAMPLE_IS_NOT_DISPOSABLE;
460 sample->prop.redundant = picture->has_redundancy ? ISOM_SAMPLE_HAS_REDUNDANCY : ISOM_SAMPLE_HAS_NO_REDUNDANCY;
461 sample->prop.post_roll.identifier = picture->frame_num;
462 if( picture->random_accessible )
464 if( picture->idr )
465 sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
466 else if( picture->recovery_frame_cnt )
468 sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_POST_ROLL_START;
469 sample->prop.post_roll.complete = (picture->frame_num + picture->recovery_frame_cnt) % info->sps.MaxFrameNum;
471 else
473 sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP;
474 if( !picture->broken_link_flag )
475 sample->prop.ra_flags |= QT_SAMPLE_RANDOM_ACCESS_FLAG_PARTIAL_SYNC;
478 if( picture->independent )
479 h264_imp->last_intra_cts = sample->cts;
480 if( picture->idr )
481 h264_imp->last_sync_cts = sample->cts;
482 sample->length = au->length;
483 memcpy( sample->data, au->data, au->length );
484 return current_status;
487 static void nalu_deduplicate_poc
489 nal_pic_timing_t *npt,
490 uint32_t *max_composition_delay,
491 uint32_t num_access_units,
492 uint32_t max_num_reorder_pics
495 /* Deduplicate POCs. */
496 int64_t poc_offset = 0;
497 int64_t poc_min = 0;
498 int64_t invalid_poc_min = 0;
499 uint32_t last_poc_reset = UINT32_MAX;
500 uint32_t invalid_poc_start = 0;
501 int invalid_poc_present = 0;
502 for( uint32_t i = 0; ; i++ )
504 if( i < num_access_units && npt[i].poc != 0 && !npt[i].reset )
506 /* poc_offset is not added to each POC here.
507 * It is done when we encounter the next coded video sequence. */
508 if( npt[i].poc < 0 )
510 /* Pictures with negative POC shall precede IDR-picture in composition order.
511 * The minimum POC is added to poc_offset when we encounter the next coded video sequence. */
512 if( last_poc_reset == UINT32_MAX || i > last_poc_reset + max_num_reorder_pics )
514 if( !invalid_poc_present )
516 invalid_poc_present = 1;
517 invalid_poc_start = i;
519 if( invalid_poc_min > npt[i].poc )
520 invalid_poc_min = npt[i].poc;
522 else if( poc_min > npt[i].poc )
524 poc_min = npt[i].poc;
525 *max_composition_delay = LSMASH_MAX( *max_composition_delay, i - last_poc_reset );
528 continue;
530 /* Encountered a new coded video sequence or no more POCs.
531 * Add poc_offset to each POC of the previous coded video sequence. */
532 poc_offset -= poc_min;
533 int64_t poc_max = 0;
534 for( uint32_t j = last_poc_reset; j < i + !!npt[i].reset; j++ )
535 if( npt[j].poc >= 0 || (j <= last_poc_reset + max_num_reorder_pics) )
537 npt[j].poc += poc_offset;
538 if( poc_max < npt[j].poc )
539 poc_max = npt[j].poc;
541 poc_offset = poc_max + 1;
542 if( invalid_poc_present )
544 /* Pictures with invalid negative POC is probably supposed to be composited
545 * both before the next coded video sequence and after the current one. */
546 poc_offset -= invalid_poc_min;
547 for( uint32_t j = invalid_poc_start; j < i + !!npt[i].reset; j++ )
548 if( npt[j].poc < 0 )
550 npt[j].poc += poc_offset;
551 if( poc_max < npt[j].poc )
552 poc_max = npt[j].poc;
554 invalid_poc_present = 0;
555 invalid_poc_start = 0;
556 invalid_poc_min = 0;
557 poc_offset = poc_max + 1;
559 if( i < num_access_units )
561 if( npt[i].reset )
562 npt[i].poc = 0;
563 poc_min = 0;
564 last_poc_reset = i;
566 else
567 break; /* no more POCs */
571 static void nalu_generate_timestamps_from_poc
573 importer_t *importer,
574 lsmash_media_ts_t *timestamp,
575 nal_pic_timing_t *npt,
576 uint8_t *composition_reordering_present,
577 uint32_t *last_delta,
578 uint32_t max_composition_delay,
579 uint32_t num_access_units
582 /* Check if composition delay derived from reordering is present. */
583 if( max_composition_delay == 0 )
585 *composition_reordering_present = 0;
586 for( uint32_t i = 1; i < num_access_units; i++ )
587 if( npt[i].poc < npt[i - 1].poc )
589 *composition_reordering_present = 1;
590 break;
593 else
594 *composition_reordering_present = 1;
595 if( *composition_reordering_present )
597 /* Generate timestamps.
598 * Here, DTSs and CTSs are temporary values for sort. */
599 for( uint32_t i = 0; i < num_access_units; i++ )
601 timestamp[i].cts = (uint64_t)npt[i].poc;
602 timestamp[i].dts = (uint64_t)i;
604 qsort( timestamp, num_access_units, sizeof(lsmash_media_ts_t), (int(*)( const void *, const void * ))lsmash_compare_cts );
605 /* Check POC gap in output order. */
606 lsmash_class_t *logger = &(lsmash_class_t){ .name = importer->class->name };
607 for( uint32_t i = 1; i < num_access_units; i++ )
608 if( timestamp[i].cts > timestamp[i - 1].cts + npt[i - 1].poc_delta )
609 lsmash_log( &logger, LSMASH_LOG_WARNING,
610 "POC gap is detected at picture %"PRIu64". Maybe some pictures are lost.\n", timestamp[i].dts );
611 /* Get the maximum composition delay derived from reordering. */
612 for( uint32_t i = 0; i < num_access_units; i++ )
613 if( i < timestamp[i].dts )
615 uint32_t composition_delay = timestamp[i].dts - i;
616 max_composition_delay = LSMASH_MAX( max_composition_delay, composition_delay );
619 /* Generate timestamps. */
620 if( max_composition_delay )
622 uint64_t *ts_buffer = (uint64_t *)lsmash_malloc( (num_access_units + max_composition_delay) * sizeof(uint64_t) );
623 if( !ts_buffer )
625 /* It seems that there is no enough memory to generate more appropriate timestamps.
626 * Anyway, generate CTSs and DTSs. */
627 for( uint32_t i = 0; i < num_access_units; i++ )
628 timestamp[i].cts = i + max_composition_delay;
629 qsort( timestamp, num_access_units, sizeof(lsmash_media_ts_t), (int(*)( const void *, const void * ))lsmash_compare_dts );
630 *last_delta = 1;
631 return;
633 uint64_t *reorder_cts = ts_buffer;
634 uint64_t *prev_reorder_cts = ts_buffer + num_access_units;
635 *last_delta = npt[num_access_units - 1].delta;
636 /* Generate CTSs. */
637 timestamp[0].cts = 0;
638 for( uint32_t i = 1; i < num_access_units; i++ )
639 timestamp[i].cts = timestamp[i - 1].cts + npt[i - 1].delta;
640 int64_t composition_delay_time = timestamp[max_composition_delay].cts;
641 for( uint32_t i = 0; i < num_access_units; i++ )
643 timestamp[i].cts += composition_delay_time;
644 reorder_cts[i] = timestamp[i].cts;
646 /* Generate DTSs. */
647 qsort( timestamp, num_access_units, sizeof(lsmash_media_ts_t), (int(*)( const void *, const void * ))lsmash_compare_dts );
648 for( uint32_t i = 0; i < num_access_units; i++ )
650 timestamp[i].dts = i <= max_composition_delay
651 ? reorder_cts[i] - composition_delay_time
652 : prev_reorder_cts[(i - max_composition_delay) % max_composition_delay];
653 prev_reorder_cts[i % max_composition_delay] = reorder_cts[i];
655 lsmash_free( ts_buffer );
656 #if 0
657 fprintf( stderr, "max_composition_delay=%"PRIu32", composition_delay_time=%"PRIu64"\n",
658 max_composition_delay, composition_delay_time );
659 #endif
661 else
663 timestamp[0].dts = 0;
664 timestamp[0].cts = 0;
665 for( uint32_t i = 1; i < num_access_units; i++ )
667 timestamp[i].dts = timestamp[i - 1].dts + npt[i - 1].delta;
668 timestamp[i].cts = timestamp[i - 1].cts + npt[i - 1].delta;
670 *last_delta = npt[num_access_units - 1].delta;
674 static void nalu_reduce_timescale
676 lsmash_media_ts_t *timestamp,
677 nal_pic_timing_t *npt,
678 uint32_t *last_delta,
679 uint32_t *timescale,
680 uint32_t num_access_units
683 uint64_t gcd_delta = *timescale;
684 for( uint32_t i = 0; i < num_access_units && gcd_delta > 1; i++ )
685 gcd_delta = lsmash_get_gcd( gcd_delta, npt[i].delta );
686 if( gcd_delta > 1 )
688 for( uint32_t i = 0; i < num_access_units; i++ )
690 timestamp[i].dts /= gcd_delta;
691 timestamp[i].cts /= gcd_delta;
693 *last_delta /= gcd_delta;
694 *timescale /= gcd_delta;
696 #if 0
697 for( uint32_t i = 0; i < num_access_units; i++ )
698 fprintf( stderr, "Timestamp[%"PRIu32"]: POC=%"PRId64", DTS=%"PRIu64", CTS=%"PRIu64"\n",
699 i, npt[i].poc, timestamp[i].dts, timestamp[i].cts );
700 #endif
703 static lsmash_video_summary_t *h264_setup_first_summary
705 importer_t *importer
708 h264_importer_t *h264_imp = (h264_importer_t *)importer->info;
709 lsmash_codec_specific_t *cs = (lsmash_codec_specific_t *)lsmash_list_get_entry_data( h264_imp->avcC_list, ++ h264_imp->avcC_number );
710 if( !cs || !cs->data.structured )
712 lsmash_destroy_codec_specific_data( cs );
713 return NULL;
715 lsmash_video_summary_t *summary = h264_create_summary( (lsmash_h264_specific_parameters_t *)cs->data.structured,
716 &h264_imp->info.sps, h264_imp->max_au_length );
717 if( !summary )
719 lsmash_destroy_codec_specific_data( cs );
720 return NULL;
722 if( lsmash_list_add_entry( importer->summaries, summary ) < 0 )
724 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
725 return NULL;
727 summary->sample_per_field = h264_imp->field_pic_present;
728 return summary;
731 static int h264_analyze_whole_stream
733 importer_t *importer
736 /* Parse all NALU in the stream for preparation of calculating timestamps. */
737 uint32_t npt_alloc = (1 << 12) * sizeof(nal_pic_timing_t);
738 nal_pic_timing_t *npt = lsmash_malloc( npt_alloc );
739 if( !npt )
740 return LSMASH_ERR_MEMORY_ALLOC;
741 uint32_t picture_stats[H264_PICTURE_TYPE_NONE + 1] = { 0 };
742 uint32_t num_access_units = 0;
743 lsmash_class_t *logger = &(lsmash_class_t){ "H.264" };
744 lsmash_log( &logger, LSMASH_LOG_INFO, "Analyzing stream as H.264\r" );
745 h264_importer_t *h264_imp = (h264_importer_t *)importer->info;
746 h264_info_t *info = &h264_imp->info;
747 importer->status = IMPORTER_OK;
748 int err = LSMASH_ERR_MEMORY_ALLOC;
749 while( importer->status != IMPORTER_EOF )
751 #if 0
752 lsmash_log( &logger, LSMASH_LOG_INFO, "Analyzing stream as H.264: %"PRIu32"\n", num_access_units + 1 );
753 #endif
754 h264_picture_info_t *picture = &info->au.picture;
755 h264_picture_info_t prev_picture = *picture;
756 if( (err = h264_get_access_unit_internal( importer, 1 )) < 0
757 || (err = h264_calculate_poc( info, picture, &prev_picture )) < 0 )
758 goto fail;
759 h264_importer_check_eof( importer, &info->au );
760 if( npt_alloc <= num_access_units * sizeof(nal_pic_timing_t) )
762 uint32_t alloc = 2 * num_access_units * sizeof(nal_pic_timing_t);
763 nal_pic_timing_t *temp = (nal_pic_timing_t *)lsmash_realloc( npt, alloc );
764 if( !temp )
765 goto fail;
766 npt = temp;
767 npt_alloc = alloc;
769 h264_imp->field_pic_present |= picture->field_pic_flag;
770 npt[num_access_units].poc = picture->PicOrderCnt;
771 npt[num_access_units].delta = picture->delta;
772 npt[num_access_units].poc_delta = picture->field_pic_flag ? 1 : 2;
773 npt[num_access_units].reset = picture->has_mmco5;
774 ++num_access_units;
775 h264_imp->max_au_length = LSMASH_MAX( info->au.length, h264_imp->max_au_length );
776 if( picture->idr )
777 ++picture_stats[H264_PICTURE_TYPE_IDR];
778 else if( picture->type >= H264_PICTURE_TYPE_NONE )
779 ++picture_stats[H264_PICTURE_TYPE_NONE];
780 else
781 ++picture_stats[ picture->type ];
783 lsmash_log_refresh_line( &logger );
784 lsmash_log( &logger, LSMASH_LOG_INFO,
785 "IDR: %"PRIu32", I: %"PRIu32", P: %"PRIu32", B: %"PRIu32", "
786 "SI: %"PRIu32", SP: %"PRIu32", Unknown: %"PRIu32"\n",
787 picture_stats[H264_PICTURE_TYPE_IDR ],
788 picture_stats[H264_PICTURE_TYPE_I ],
789 picture_stats[H264_PICTURE_TYPE_I_P ],
790 picture_stats[H264_PICTURE_TYPE_I_P_B ],
791 picture_stats[H264_PICTURE_TYPE_SI ]
792 + picture_stats[H264_PICTURE_TYPE_I_SI ],
793 picture_stats[H264_PICTURE_TYPE_SI_SP ]
794 + picture_stats[H264_PICTURE_TYPE_I_SI_P_SP ]
795 + picture_stats[H264_PICTURE_TYPE_I_SI_P_SP_B],
796 picture_stats[H264_PICTURE_TYPE_NONE ] );
797 /* Copy and append the last Codec Specific info. */
798 if( (err = h264_store_codec_specific( h264_imp, &info->avcC_param )) < 0 )
799 goto fail;
800 /* Set up the first summary. */
801 lsmash_video_summary_t *summary = h264_setup_first_summary( importer );
802 if( !summary )
804 err = LSMASH_ERR_NAMELESS;
805 goto fail;
807 /* Allocate timestamps. */
808 lsmash_media_ts_t *timestamp = lsmash_malloc( num_access_units * sizeof(lsmash_media_ts_t) );
809 if( !timestamp )
810 goto fail;
811 /* Count leading samples that are undecodable. */
812 for( uint32_t i = 0; i < num_access_units; i++ )
814 if( npt[i].poc == 0 )
815 break;
816 ++ h264_imp->num_undecodable;
818 /* Deduplicate POCs. */
819 uint32_t max_composition_delay = 0;
820 nalu_deduplicate_poc( npt, &max_composition_delay, num_access_units, 32 );
821 /* Generate timestamps. */
822 nalu_generate_timestamps_from_poc( importer, timestamp, npt,
823 &h264_imp->composition_reordering_present,
824 &h264_imp->last_delta,
825 max_composition_delay, num_access_units );
826 nalu_reduce_timescale( timestamp, npt, &h264_imp->last_delta, &summary->timescale, num_access_units );
827 lsmash_free( npt );
828 h264_imp->ts_list.sample_count = num_access_units;
829 h264_imp->ts_list.timestamp = timestamp;
830 return 0;
831 fail:
832 lsmash_log_refresh_line( &logger );
833 lsmash_free( npt );
834 return err;
837 static int h264_importer_probe( importer_t *importer )
839 /* Find the first start code. */
840 h264_importer_t *h264_imp = create_h264_importer( importer );
841 if( !h264_imp )
842 return LSMASH_ERR_MEMORY_ALLOC;
843 lsmash_bs_t *bs = importer->bs;
844 uint64_t first_sc_head_pos = nalu_find_first_start_code( bs );
845 int err;
846 if( first_sc_head_pos == NALU_NO_START_CODE_FOUND )
848 err = LSMASH_ERR_INVALID_DATA;
849 goto fail;
851 else if( first_sc_head_pos == NALU_IO_ERROR )
853 err = LSMASH_ERR_IO;
854 goto fail;
856 /* OK. It seems the stream has a long start code of H.264. */
857 importer->info = h264_imp;
858 h264_info_t *info = &h264_imp->info;
859 lsmash_bs_read_seek( bs, first_sc_head_pos, SEEK_SET );
860 h264_imp->sc_head_pos = first_sc_head_pos;
861 if( (err = h264_analyze_whole_stream( importer )) < 0 )
862 goto fail;
863 /* Go back to the start code of the first NALU. */
864 importer->status = IMPORTER_OK;
865 lsmash_bs_read_seek( bs, first_sc_head_pos, SEEK_SET );
866 h264_imp->sc_head_pos = first_sc_head_pos;
867 info->prev_nalu_type = H264_NALU_TYPE_UNSPECIFIED0;
868 uint8_t *temp_au = info->au.data;
869 uint8_t *temp_incomplete_au = info->au.incomplete_data;
870 memset( &info->au, 0, sizeof(h264_access_unit_t) );
871 info->au.data = temp_au;
872 info->au.incomplete_data = temp_incomplete_au;
873 memset( &info->slice, 0, sizeof(h264_slice_info_t) );
874 memset( &info->sps, 0, sizeof(h264_sps_t) );
875 memset( &info->pps, 0, sizeof(h264_pps_t) );
876 lsmash_list_remove_entries( info->avcC_param.parameter_sets->sps_list );
877 lsmash_list_remove_entries( info->avcC_param.parameter_sets->pps_list );
878 lsmash_list_remove_entries( info->avcC_param.parameter_sets->spsext_list );
879 lsmash_destroy_h264_parameter_sets( &info->avcC_param_next );
880 return 0;
881 fail:
882 remove_h264_importer( h264_imp );
883 importer->info = NULL;
884 lsmash_list_remove_entries( importer->summaries );
885 return err;
888 static uint32_t h264_importer_get_last_delta( importer_t *importer, uint32_t track_number )
890 debug_if( !importer || !importer->info )
891 return 0;
892 h264_importer_t *h264_imp = (h264_importer_t *)importer->info;
893 if( !h264_imp || track_number != 1 || importer->status != IMPORTER_EOF )
894 return 0;
895 return h264_imp->ts_list.sample_count
896 ? h264_imp->last_delta
897 : UINT32_MAX; /* arbitrary */
900 const importer_functions h264_importer =
902 { "H.264", offsetof( importer_t, log_level ) },
904 h264_importer_probe,
905 h264_importer_get_accessunit,
906 h264_importer_get_last_delta,
907 h264_importer_cleanup
910 /***************************************************************************
911 HEVC importer
912 ITU-T Recommendation H.265 (04/13)
913 ISO/IEC 14496-15:2014
914 ***************************************************************************/
915 #include "codecs/hevc.h"
917 typedef struct
919 hevc_info_t info;
920 lsmash_entry_list_t hvcC_list[1]; /* stored as lsmash_codec_specific_t */
921 lsmash_media_ts_list_t ts_list;
922 uint32_t max_au_length;
923 uint32_t num_undecodable;
924 uint32_t hvcC_number;
925 uint32_t last_delta;
926 uint64_t last_intra_cts;
927 uint64_t sc_head_pos;
928 uint8_t composition_reordering_present;
929 uint8_t field_pic_present;
930 uint8_t max_TemporalId;
931 } hevc_importer_t;
933 static void remove_hevc_importer( hevc_importer_t *hevc_imp )
935 if( !hevc_imp )
936 return;
937 lsmash_list_remove_entries( hevc_imp->hvcC_list );
938 hevc_cleanup_parser( &hevc_imp->info );
939 lsmash_free( hevc_imp->ts_list.timestamp );
940 lsmash_free( hevc_imp );
943 static void hevc_importer_cleanup( importer_t *importer )
945 debug_if( importer && importer->info )
946 remove_hevc_importer( importer->info );
949 static hevc_importer_t *create_hevc_importer( importer_t *importer )
951 hevc_importer_t *hevc_imp = lsmash_malloc_zero( sizeof(hevc_importer_t) );
952 if( !hevc_imp )
953 return NULL;
954 if( hevc_setup_parser( &hevc_imp->info, 0 ) < 0 )
956 remove_hevc_importer( hevc_imp );
957 return NULL;
959 lsmash_list_init( hevc_imp->hvcC_list, lsmash_destroy_codec_specific_data );
960 hevc_imp->info.eos = 1;
961 return hevc_imp;
964 static inline int hevc_complete_au( hevc_access_unit_t *au, int probe )
966 if( !au->picture.has_primary || au->incomplete_length == 0 )
967 return 0;
968 if( !probe )
969 memcpy( au->data, au->incomplete_data, au->incomplete_length );
970 au->TemporalId = au->picture.TemporalId;
971 au->length = au->incomplete_length;
972 au->incomplete_length = 0;
973 au->picture.has_primary = 0;
974 return 1;
977 static void hevc_append_nalu_to_au( hevc_access_unit_t *au, uint8_t *src_nalu, uint32_t nalu_length, int probe )
979 if( !probe )
981 uint8_t *dst_nalu = au->incomplete_data + au->incomplete_length + NALU_DEFAULT_NALU_LENGTH_SIZE;
982 for( int i = NALU_DEFAULT_NALU_LENGTH_SIZE; i; i-- )
983 *(dst_nalu - i) = (nalu_length >> ((i - 1) * 8)) & 0xff;
984 memcpy( dst_nalu, src_nalu, nalu_length );
986 /* Note: picture->incomplete_au_length shall be 0 immediately after AU has completed.
987 * Therefore, possible_au_length in hevc_get_access_unit_internal() can't be used here
988 * to avoid increasing AU length monotonously through the entire stream. */
989 au->incomplete_length += NALU_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
992 static int hevc_get_au_internal_succeeded( hevc_importer_t *hevc_imp, hevc_access_unit_t *au )
994 au->number += 1;
995 return 0;
998 static int hevc_get_au_internal_failed( hevc_importer_t *hevc_imp, hevc_access_unit_t *au, int complete_au, int ret )
1000 if( complete_au )
1001 au->number += 1;
1002 return ret;
1005 static lsmash_video_summary_t *hevc_create_summary
1007 lsmash_hevc_specific_parameters_t *param,
1008 hevc_sps_t *sps,
1009 uint32_t max_au_length
1012 lsmash_video_summary_t *summary = (lsmash_video_summary_t *)lsmash_create_summary( LSMASH_SUMMARY_TYPE_VIDEO );
1013 if( !summary )
1014 return NULL;
1015 /* Update summary here.
1016 * max_au_length is set at the last of hevc_importer_probe function. */
1017 lsmash_codec_specific_t *specific = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_HEVC,
1018 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED );
1019 if( !specific )
1021 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
1022 return NULL;
1024 specific->data.unstructured = lsmash_create_hevc_specific_info( param, &specific->size );
1025 if( !specific->data.unstructured
1026 || lsmash_list_add_entry( &summary->opaque->list, specific ) < 0 )
1028 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
1029 lsmash_destroy_codec_specific_data( specific );
1030 return NULL;
1032 summary->sample_type = ISOM_CODEC_TYPE_HVC1_VIDEO;
1033 summary->max_au_length = max_au_length;
1034 summary->timescale = sps->vui.time_scale;
1035 summary->timebase = sps->vui.num_units_in_tick;
1036 summary->vfr = (param->constantFrameRate == 0);
1037 summary->sample_per_field = 0;
1038 summary->width = sps->cropped_width;
1039 summary->height = sps->cropped_height;
1040 summary->par_h = sps->vui.sar_width;
1041 summary->par_v = sps->vui.sar_height;
1042 summary->color.primaries_index = sps->vui.colour_primaries != 2 ? sps->vui.colour_primaries : 0;
1043 summary->color.transfer_index = sps->vui.transfer_characteristics != 2 ? sps->vui.transfer_characteristics : 0;
1044 summary->color.matrix_index = sps->vui.matrix_coeffs != 2 ? sps->vui.matrix_coeffs : 0;
1045 summary->color.full_range = sps->vui.video_full_range_flag;
1046 lsmash_convert_crop_into_clap( sps->vui.def_disp_win_offset, summary->width, summary->height, &summary->clap );
1047 return summary;
1050 static int hevc_store_codec_specific
1052 hevc_importer_t *hevc_imp,
1053 lsmash_hevc_specific_parameters_t *hvcC_param
1056 lsmash_codec_specific_t *src_cs = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_HEVC,
1057 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED );
1058 if( !src_cs )
1059 return LSMASH_ERR_NAMELESS;
1060 lsmash_hevc_specific_parameters_t *src_param = (lsmash_hevc_specific_parameters_t *)src_cs->data.structured;
1061 *src_param = *hvcC_param;
1062 lsmash_codec_specific_t *dst_cs = lsmash_convert_codec_specific_format( src_cs, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED );
1063 src_param->parameter_arrays = NULL; /* Avoid freeing parameter arrays within hvcC_param. */
1064 lsmash_destroy_codec_specific_data( src_cs );
1065 if( !dst_cs )
1067 lsmash_destroy_codec_specific_data( dst_cs );
1068 return LSMASH_ERR_NAMELESS;
1070 if( lsmash_list_add_entry( hevc_imp->hvcC_list, dst_cs ) < 0 )
1072 lsmash_destroy_codec_specific_data( dst_cs );
1073 return LSMASH_ERR_MEMORY_ALLOC;
1075 return 0;
1078 static inline void hevc_new_access_unit( hevc_access_unit_t *au )
1080 au->length = 0;
1081 au->picture.type = HEVC_PICTURE_TYPE_NONE;
1082 au->picture.random_accessible = 0;
1083 au->picture.recovery_poc_cnt = 0;
1086 /* If probe equals 0, don't get the actual data (EBPS) of an access unit and only parse NALU. */
1087 static int hevc_get_access_unit_internal
1089 importer_t *importer,
1090 int probe
1093 hevc_importer_t *hevc_imp = (hevc_importer_t *)importer->info;
1094 hevc_info_t *info = &hevc_imp->info;
1095 hevc_slice_info_t *slice = &info->slice;
1096 hevc_access_unit_t *au = &info->au;
1097 hevc_picture_info_t *picture = &au->picture;
1098 hevc_stream_buffer_t *sb = &info->buffer;
1099 lsmash_bs_t *bs = importer->bs;
1100 int complete_au = 0;
1101 hevc_new_access_unit( au );
1102 while( 1 )
1104 hevc_nalu_header_t nuh;
1105 uint64_t start_code_length;
1106 uint64_t trailing_zero_bytes;
1107 uint64_t nalu_length = hevc_find_next_start_code( bs, &nuh, &start_code_length, &trailing_zero_bytes );
1108 if( nalu_length == NALU_NO_START_CODE_FOUND )
1110 /* For the last NALU.
1111 * This NALU already has been appended into the latest access unit and parsed. */
1112 hevc_update_picture_info( info, picture, slice, &info->sps, &info->sei );
1113 complete_au = hevc_complete_au( au, probe );
1114 if( complete_au )
1115 return hevc_get_au_internal_succeeded( hevc_imp, au );
1116 else
1117 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, LSMASH_ERR_INVALID_DATA );
1119 uint8_t nalu_type = nuh.nal_unit_type;
1120 uint64_t next_sc_head_pos = hevc_imp->sc_head_pos
1121 + start_code_length
1122 + nalu_length
1123 + trailing_zero_bytes;
1124 #if 0
1125 if( probe )
1127 fprintf( stderr, "NALU type: %"PRIu8" \n", nalu_type );
1128 fprintf( stderr, " NALU header position: %"PRIx64" \n", hevc_imp->sc_head_pos + start_code_length );
1129 fprintf( stderr, " EBSP position: %"PRIx64" \n", hevc_imp->sc_head_pos + start_code_length + nuh.length );
1130 fprintf( stderr, " EBSP length: %"PRIx64" (%"PRIu64") \n", nalu_length - nuh.length, nalu_length - nuh.length );
1131 fprintf( stderr, " trailing_zero_bytes: %"PRIx64" \n", trailing_zero_bytes );
1132 fprintf( stderr, " Next start code position: %"PRIx64"\n", next_sc_head_pos );
1134 #endif
1135 /* Check if the end of sequence. Used for POC calculation. */
1136 info->eos |= info->prev_nalu_type == HEVC_NALU_TYPE_EOS
1137 || info->prev_nalu_type == HEVC_NALU_TYPE_EOB;
1138 /* Process the current NALU by its type. */
1139 if( nalu_type == HEVC_NALU_TYPE_FD )
1141 /* We don't support streams with both filler and HRD yet. Otherwise, just skip filler. */
1142 if( info->sps.vui.hrd.present )
1143 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, LSMASH_ERR_PATCH_WELCOME );
1145 else if( nalu_type <= HEVC_NALU_TYPE_RASL_R
1146 || (nalu_type >= HEVC_NALU_TYPE_BLA_W_LP && nalu_type <= HEVC_NALU_TYPE_CRA)
1147 || (nalu_type >= HEVC_NALU_TYPE_VPS && nalu_type <= HEVC_NALU_TYPE_SUFFIX_SEI) )
1149 int err;
1150 /* Increase the buffer if needed. */
1151 uint64_t possible_au_length = au->incomplete_length + NALU_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
1152 if( sb->bank->buffer_size < possible_au_length
1153 && (err = hevc_supplement_buffer( sb, au, 2 * possible_au_length )) < 0 )
1155 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to increase the buffer size.\n" );
1156 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, err );
1158 /* Get the EBSP of the current NALU here. */
1159 uint8_t *nalu = lsmash_bs_get_buffer_data( bs ) + start_code_length;
1160 if( nalu_type <= HEVC_NALU_TYPE_RSV_VCL31 )
1162 /* VCL NALU (slice) */
1163 hevc_slice_info_t prev_slice = *slice;
1164 if( (err = hevc_parse_slice_segment_header( info, &nuh, sb->rbsp,
1165 nalu + nuh.length,
1166 nalu_length - nuh.length )) < 0 )
1167 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, err );
1168 if( probe && info->hvcC_pending )
1170 /* Copy and append a Codec Specific info. */
1171 if( (err = hevc_store_codec_specific( hevc_imp, &info->hvcC_param )) < 0 )
1172 return err;
1174 if( (err = hevc_move_pending_hvcC_param( info )) < 0 )
1175 return err;
1176 if( prev_slice.present )
1178 /* Check whether the AU that contains the previous VCL NALU completed or not. */
1179 if( hevc_find_au_delimit_by_slice_info( info, slice, &prev_slice ) )
1181 /* The current NALU is the first VCL NALU of the primary coded picture of a new AU.
1182 * Therefore, the previous slice belongs to the AU you want at this time. */
1183 hevc_update_picture_info( info, picture, &prev_slice, &info->sps, &info->sei );
1184 complete_au = hevc_complete_au( au, probe );
1186 else
1187 hevc_update_picture_info_for_slice( info, picture, &prev_slice );
1189 hevc_append_nalu_to_au( au, nalu, nalu_length, probe );
1190 slice->present = 1;
1192 else
1194 if( hevc_find_au_delimit_by_nalu_type( nalu_type, info->prev_nalu_type ) )
1196 /* The last slice belongs to the AU you want at this time. */
1197 hevc_update_picture_info( info, picture, slice, &info->sps, &info->sei );
1198 complete_au = hevc_complete_au( au, probe );
1200 switch( nalu_type )
1202 case HEVC_NALU_TYPE_PREFIX_SEI :
1203 case HEVC_NALU_TYPE_SUFFIX_SEI :
1205 if( (err = hevc_parse_sei( info->bits, &info->vps, &info->sps, &info->sei, &nuh,
1206 sb->rbsp, nalu + nuh.length, nalu_length - nuh.length )) < 0 )
1207 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, err );
1208 hevc_append_nalu_to_au( au, nalu, nalu_length, probe );
1209 break;
1211 case HEVC_NALU_TYPE_VPS :
1212 if( (err = hevc_try_to_append_dcr_nalu( info, HEVC_DCR_NALU_TYPE_VPS, nalu, nalu_length )) < 0 )
1213 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, err );
1214 break;
1215 case HEVC_NALU_TYPE_SPS :
1216 if( (err = hevc_try_to_append_dcr_nalu( info, HEVC_DCR_NALU_TYPE_SPS, nalu, nalu_length )) < 0 )
1217 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, err );
1218 break;
1219 case HEVC_NALU_TYPE_PPS :
1220 if( (err = hevc_try_to_append_dcr_nalu( info, HEVC_DCR_NALU_TYPE_PPS, nalu, nalu_length )) < 0 )
1221 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, err );
1222 break;
1223 case HEVC_NALU_TYPE_AUD : /* We drop access unit delimiters. */
1224 break;
1225 default :
1226 hevc_append_nalu_to_au( au, nalu, nalu_length, probe );
1227 break;
1229 if( info->hvcC_pending )
1230 importer->status = IMPORTER_CHANGE;
1233 /* Move to the first byte of the next start code. */
1234 info->prev_nalu_type = nalu_type;
1235 if( lsmash_bs_read_seek( bs, next_sc_head_pos, SEEK_SET ) != next_sc_head_pos )
1237 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to seek the next start code.\n" );
1238 return hevc_get_au_internal_failed( hevc_imp, au, complete_au, LSMASH_ERR_NAMELESS );
1240 if( !lsmash_bs_is_end( bs, NALU_SHORT_START_CODE_LENGTH ) )
1241 hevc_imp->sc_head_pos = next_sc_head_pos;
1242 /* If there is no more data in the stream, and flushed chunk of NALUs, flush it as complete AU here. */
1243 else if( au->incomplete_length && au->length == 0 )
1245 hevc_update_picture_info( info, picture, slice, &info->sps, &info->sei );
1246 hevc_complete_au( au, probe );
1247 return hevc_get_au_internal_succeeded( hevc_imp, au );
1249 if( complete_au )
1250 return hevc_get_au_internal_succeeded( hevc_imp, au );
1254 static inline void hevc_importer_check_eof( importer_t *importer, hevc_access_unit_t *au )
1256 /* HEVC byte stream NALU consists of at least 5 bytes (start-code + NALU-header). */
1257 if( lsmash_bs_is_end( importer->bs, NALU_SHORT_START_CODE_LENGTH + 1 ) && au->incomplete_length == 0 )
1258 importer->status = IMPORTER_EOF;
1259 else if( importer->status != IMPORTER_CHANGE )
1260 importer->status = IMPORTER_OK;
1263 static int hevc_importer_get_accessunit( importer_t *importer, uint32_t track_number, lsmash_sample_t **p_sample )
1265 if( !importer->info )
1266 return LSMASH_ERR_NAMELESS;
1267 if( track_number != 1 )
1268 return LSMASH_ERR_FUNCTION_PARAM;
1269 hevc_importer_t *hevc_imp = (hevc_importer_t *)importer->info;
1270 hevc_info_t *info = &hevc_imp->info;
1271 importer_status current_status = importer->status;
1272 if( current_status == IMPORTER_ERROR )
1273 return LSMASH_ERR_NAMELESS;
1274 if( current_status == IMPORTER_EOF )
1275 return IMPORTER_EOF;
1276 int err = hevc_get_access_unit_internal( importer, 0 );
1277 if( err < 0 )
1279 importer->status = IMPORTER_ERROR;
1280 return err;
1282 hevc_importer_check_eof( importer, &info->au );
1283 if( importer->status == IMPORTER_CHANGE && !info->hvcC_pending )
1284 current_status = IMPORTER_CHANGE;
1285 if( current_status == IMPORTER_CHANGE )
1287 /* Update the active summary. */
1288 lsmash_codec_specific_t *cs = (lsmash_codec_specific_t *)lsmash_list_get_entry_data( hevc_imp->hvcC_list, ++ hevc_imp->hvcC_number );
1289 if( !cs )
1290 return LSMASH_ERR_NAMELESS;
1291 lsmash_hevc_specific_parameters_t *hvcC_param = (lsmash_hevc_specific_parameters_t *)cs->data.structured;
1292 lsmash_video_summary_t *summary = hevc_create_summary( hvcC_param, &info->sps, hevc_imp->max_au_length );
1293 if( !summary )
1294 return LSMASH_ERR_NAMELESS;
1295 lsmash_list_remove_entry( importer->summaries, track_number );
1296 if( lsmash_list_add_entry( importer->summaries, summary ) < 0 )
1298 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
1299 return LSMASH_ERR_MEMORY_ALLOC;
1301 importer->status = IMPORTER_OK;
1303 lsmash_sample_t *sample = lsmash_create_sample( hevc_imp->max_au_length );
1304 if( !sample )
1305 return LSMASH_ERR_MEMORY_ALLOC;
1306 *p_sample = sample;
1307 hevc_access_unit_t *au = &info->au;
1308 hevc_picture_info_t *picture = &au->picture;
1309 sample->dts = hevc_imp->ts_list.timestamp[ au->number - 1 ].dts;
1310 sample->cts = hevc_imp->ts_list.timestamp[ au->number - 1 ].cts;
1311 /* Set property of disposability. */
1312 if( picture->sublayer_nonref && au->TemporalId == hevc_imp->max_TemporalId )
1313 /* Sub-layer non-reference pictures are not referenced by subsequent pictures of
1314 * the same sub-layer in decoding order. */
1315 sample->prop.disposable = ISOM_SAMPLE_IS_DISPOSABLE;
1316 else
1317 sample->prop.disposable = ISOM_SAMPLE_IS_NOT_DISPOSABLE;
1318 /* Set property of leading. */
1319 if( picture->radl || picture->rasl )
1320 sample->prop.leading = picture->radl ? ISOM_SAMPLE_IS_DECODABLE_LEADING : ISOM_SAMPLE_IS_UNDECODABLE_LEADING;
1321 else
1323 if( au->number < hevc_imp->num_undecodable )
1324 sample->prop.leading = ISOM_SAMPLE_IS_UNDECODABLE_LEADING;
1325 else
1327 if( picture->independent || sample->cts >= hevc_imp->last_intra_cts )
1328 sample->prop.leading = ISOM_SAMPLE_IS_NOT_LEADING;
1329 else
1330 sample->prop.leading = ISOM_SAMPLE_IS_UNDECODABLE_LEADING;
1333 if( picture->independent )
1334 hevc_imp->last_intra_cts = sample->cts;
1335 /* Set property of independence. */
1336 sample->prop.independent = picture->independent ? ISOM_SAMPLE_IS_INDEPENDENT : ISOM_SAMPLE_IS_NOT_INDEPENDENT;
1337 sample->prop.redundant = ISOM_SAMPLE_HAS_NO_REDUNDANCY;
1338 sample->prop.post_roll.identifier = picture->poc;
1339 if( picture->random_accessible )
1341 if( picture->irap )
1343 sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
1344 if( picture->closed_rap )
1345 sample->prop.ra_flags |= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_CLOSED_RAP;
1346 else
1347 sample->prop.ra_flags |= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP;
1349 else if( picture->recovery_poc_cnt )
1351 sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_POST_ROLL_START;
1352 sample->prop.post_roll.complete = picture->poc + picture->recovery_poc_cnt;
1354 else
1355 sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP;
1357 sample->length = au->length;
1358 memcpy( sample->data, au->data, au->length );
1359 return current_status;
1362 static lsmash_video_summary_t *hevc_setup_first_summary
1364 importer_t *importer
1367 hevc_importer_t *hevc_imp = (hevc_importer_t *)importer->info;
1368 lsmash_codec_specific_t *cs = (lsmash_codec_specific_t *)lsmash_list_get_entry_data( hevc_imp->hvcC_list, ++ hevc_imp->hvcC_number );
1369 if( !cs || !cs->data.structured )
1371 lsmash_destroy_codec_specific_data( cs );
1372 return NULL;
1374 lsmash_video_summary_t *summary = hevc_create_summary( (lsmash_hevc_specific_parameters_t *)cs->data.structured,
1375 &hevc_imp->info.sps, hevc_imp->max_au_length );
1376 if( !summary )
1378 lsmash_destroy_codec_specific_data( cs );
1379 return NULL;
1381 if( lsmash_list_add_entry( importer->summaries, summary ) < 0 )
1383 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
1384 return NULL;
1386 summary->sample_per_field = hevc_imp->field_pic_present;
1387 return summary;
1390 static int hevc_analyze_whole_stream
1392 importer_t *importer
1395 /* Parse all NALU in the stream for preparation of calculating timestamps. */
1396 uint32_t npt_alloc = (1 << 12) * sizeof(nal_pic_timing_t);
1397 nal_pic_timing_t *npt = (nal_pic_timing_t *)lsmash_malloc( npt_alloc );
1398 if( !npt )
1399 return LSMASH_ERR_MEMORY_ALLOC;
1400 uint32_t picture_stats[HEVC_PICTURE_TYPE_NONE + 1] = { 0 };
1401 uint32_t num_access_units = 0;
1402 lsmash_class_t *logger = &(lsmash_class_t){ "HEVC" };
1403 lsmash_log( &logger, LSMASH_LOG_INFO, "Analyzing stream as HEVC\r" );
1404 hevc_importer_t *hevc_imp = (hevc_importer_t *)importer->info;
1405 hevc_info_t *info = &hevc_imp->info;
1406 importer->status = IMPORTER_OK;
1407 int err = LSMASH_ERR_MEMORY_ALLOC;
1408 while( importer->status != IMPORTER_EOF )
1410 #if 0
1411 lsmash_log( &logger, LSMASH_LOG_INFO, "Analyzing stream as HEVC: %"PRIu32"\n", num_access_units + 1 );
1412 #endif
1413 hevc_picture_info_t *picture = &info->au.picture;
1414 hevc_picture_info_t prev_picture = *picture;
1415 if( (err = hevc_get_access_unit_internal( importer, 1 )) < 0
1416 || (err = hevc_calculate_poc( info, &info->au.picture, &prev_picture )) < 0 )
1417 goto fail;
1418 hevc_importer_check_eof( importer, &info->au );
1419 if( npt_alloc <= num_access_units * sizeof(nal_pic_timing_t) )
1421 uint32_t alloc = 2 * num_access_units * sizeof(nal_pic_timing_t);
1422 nal_pic_timing_t *temp = (nal_pic_timing_t *)lsmash_realloc( npt, alloc );
1423 if( !temp )
1424 goto fail;
1425 npt = temp;
1426 npt_alloc = alloc;
1428 hevc_imp->field_pic_present |= picture->field_coded;
1429 npt[num_access_units].poc = picture->poc;
1430 npt[num_access_units].delta = picture->delta;
1431 npt[num_access_units].poc_delta = 1;
1432 npt[num_access_units].reset = 0;
1433 ++num_access_units;
1434 hevc_imp->max_au_length = LSMASH_MAX( hevc_imp->max_au_length, info->au.length );
1435 hevc_imp->max_TemporalId = LSMASH_MAX( hevc_imp->max_TemporalId, info->au.TemporalId );
1436 if( picture->idr )
1437 ++picture_stats[HEVC_PICTURE_TYPE_IDR];
1438 else if( picture->irap )
1439 ++picture_stats[ picture->broken_link ? HEVC_PICTURE_TYPE_BLA : HEVC_PICTURE_TYPE_CRA ];
1440 else if( picture->type >= HEVC_PICTURE_TYPE_NONE )
1441 ++picture_stats[HEVC_PICTURE_TYPE_NONE];
1442 else
1443 ++picture_stats[ picture->type ];
1445 lsmash_log_refresh_line( &logger );
1446 lsmash_log( &logger, LSMASH_LOG_INFO,
1447 "IDR: %"PRIu32", CRA: %"PRIu32", BLA: %"PRIu32", I: %"PRIu32", P: %"PRIu32", B: %"PRIu32", Unknown: %"PRIu32"\n",
1448 picture_stats[HEVC_PICTURE_TYPE_IDR], picture_stats[HEVC_PICTURE_TYPE_CRA],
1449 picture_stats[HEVC_PICTURE_TYPE_BLA], picture_stats[HEVC_PICTURE_TYPE_I],
1450 picture_stats[HEVC_PICTURE_TYPE_I_P], picture_stats[HEVC_PICTURE_TYPE_I_P_B],
1451 picture_stats[HEVC_PICTURE_TYPE_NONE]);
1452 /* Copy and append the last Codec Specific info. */
1453 if( (err = hevc_store_codec_specific( hevc_imp, &info->hvcC_param )) < 0 )
1454 goto fail;
1455 /* Set up the first summary. */
1456 lsmash_video_summary_t *summary = hevc_setup_first_summary( importer );
1457 if( !summary )
1459 err = LSMASH_ERR_NAMELESS;
1460 goto fail;
1462 /* */
1463 lsmash_media_ts_t *timestamp = lsmash_malloc( num_access_units * sizeof(lsmash_media_ts_t) );
1464 if( !timestamp )
1465 goto fail;
1466 /* Count leading samples that are undecodable. */
1467 for( uint32_t i = 0; i < num_access_units; i++ )
1469 if( npt[i].poc == 0 )
1470 break;
1471 ++ hevc_imp->num_undecodable;
1473 /* Deduplicate POCs. */
1474 uint32_t max_composition_delay = 0;
1475 nalu_deduplicate_poc( npt, &max_composition_delay, num_access_units, 15 );
1476 /* Generate timestamps. */
1477 nalu_generate_timestamps_from_poc( importer, timestamp, npt,
1478 &hevc_imp->composition_reordering_present,
1479 &hevc_imp->last_delta,
1480 max_composition_delay, num_access_units );
1481 summary->timescale *= 2; /* We assume that picture timing is in field level.
1482 * For HEVC, it seems time_scale is set in frame level basically.
1483 * So multiply by 2 for reducing timebase and timescale. */
1484 nalu_reduce_timescale( timestamp, npt, &hevc_imp->last_delta, &summary->timescale, num_access_units );
1485 lsmash_free( npt );
1486 hevc_imp->ts_list.sample_count = num_access_units;
1487 hevc_imp->ts_list.timestamp = timestamp;
1488 return 0;
1489 fail:
1490 lsmash_log_refresh_line( &logger );
1491 lsmash_free( npt );
1492 return err;
1495 static int hevc_importer_probe( importer_t *importer )
1497 /* Find the first start code. */
1498 hevc_importer_t *hevc_imp = create_hevc_importer( importer );
1499 if( !hevc_imp )
1500 return LSMASH_ERR_MEMORY_ALLOC;
1501 lsmash_bs_t *bs = importer->bs;
1502 uint64_t first_sc_head_pos = nalu_find_first_start_code( bs );
1503 int err;
1504 if( first_sc_head_pos == NALU_NO_START_CODE_FOUND )
1506 err = LSMASH_ERR_INVALID_DATA;
1507 goto fail;
1509 else if( first_sc_head_pos == NALU_IO_ERROR )
1511 err = LSMASH_ERR_IO;
1512 goto fail;
1514 /* OK. It seems the stream has a long start code of HEVC. */
1515 importer->info = hevc_imp;
1516 hevc_info_t *info = &hevc_imp->info;
1517 lsmash_bs_read_seek( bs, first_sc_head_pos, SEEK_SET );
1518 hevc_imp->sc_head_pos = first_sc_head_pos;
1519 if( (err = hevc_analyze_whole_stream( importer )) < 0 )
1520 goto fail;
1521 /* Go back to the start code of the first NALU. */
1522 importer->status = IMPORTER_OK;
1523 lsmash_bs_read_seek( bs, first_sc_head_pos, SEEK_SET );
1524 hevc_imp->sc_head_pos = first_sc_head_pos;
1525 info->prev_nalu_type = HEVC_NALU_TYPE_UNKNOWN;
1526 uint8_t *temp_au = info->au.data;
1527 uint8_t *temp_incomplete_au = info->au.incomplete_data;
1528 memset( &info->au, 0, sizeof(hevc_access_unit_t) );
1529 info->au.data = temp_au;
1530 info->au.incomplete_data = temp_incomplete_au;
1531 memset( &info->slice, 0, sizeof(hevc_slice_info_t) );
1532 memset( &info->vps, 0, sizeof(hevc_vps_t) );
1533 memset( &info->sps, 0, sizeof(hevc_sps_t) );
1534 memset( &info->pps, 0, SIZEOF_PPS_EXCLUDING_HEAP );
1535 for( int i = 0; i < HEVC_DCR_NALU_TYPE_NUM; i++ )
1536 lsmash_list_remove_entries( info->hvcC_param.parameter_arrays->ps_array[i].list );
1537 lsmash_destroy_hevc_parameter_arrays( &info->hvcC_param_next );
1538 return 0;
1539 fail:
1540 remove_hevc_importer( hevc_imp );
1541 importer->info = NULL;
1542 lsmash_list_remove_entries( importer->summaries );
1543 return err;
1546 static uint32_t hevc_importer_get_last_delta( importer_t *importer, uint32_t track_number )
1548 debug_if( !importer || !importer->info )
1549 return 0;
1550 hevc_importer_t *hevc_imp = (hevc_importer_t *)importer->info;
1551 if( !hevc_imp || track_number != 1 || importer->status != IMPORTER_EOF )
1552 return 0;
1553 return hevc_imp->ts_list.sample_count
1554 ? hevc_imp->last_delta
1555 : UINT32_MAX; /* arbitrary */
1558 const importer_functions hevc_importer =
1560 { "HEVC", offsetof( importer_t, log_level ) },
1562 hevc_importer_probe,
1563 hevc_importer_get_accessunit,
1564 hevc_importer_get_last_delta,
1565 hevc_importer_cleanup