1 /*****************************************************************************
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 */
29 #define LSMASH_IMPORTER_INTERNAL
32 /***************************************************************************
34 ITU-T Recommendation H.264 (04/13)
36 ***************************************************************************/
37 #include "codecs/h264.h"
38 #include "codecs/nalu.h"
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
;
49 uint64_t last_intra_cts
;
51 uint8_t composition_reordering_present
;
52 uint8_t field_pic_present
;
63 static void remove_h264_importer( h264_importer_t
*h264_imp
)
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
) );
84 if( h264_setup_parser( &h264_imp
->info
, 0 ) < 0 )
86 remove_h264_importer( h264_imp
);
89 lsmash_init_entry_list( h264_imp
->avcC_list
);
93 static inline int h264_complete_au( h264_access_unit_t
*au
, int probe
)
95 if( !au
->picture
.has_primary
|| au
->incomplete_length
== 0 )
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;
105 static void h264_append_nalu_to_au( h264_access_unit_t
*au
, uint8_t *src_nalu
, uint32_t nalu_length
, int 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
)
126 static int h264_get_au_internal_failed( h264_importer_t
*h264_imp
, h264_access_unit_t
*au
, int complete_au
, int ret
)
133 static lsmash_video_summary_t
*h264_create_summary
135 lsmash_h264_specific_parameters_t
*param
,
137 uint32_t max_au_length
140 lsmash_video_summary_t
*summary
= (lsmash_video_summary_t
*)lsmash_create_summary( LSMASH_SUMMARY_TYPE_VIDEO
);
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
);
149 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
152 cs
->data
.unstructured
= lsmash_create_h264_specific_info( param
, &cs
->size
);
153 if( !cs
->data
.unstructured
154 || lsmash_add_entry( &summary
->opaque
->list
, cs
) < 0 )
156 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
157 lsmash_destroy_codec_specific_data( cs
);
160 summary
->sample_type
= ISOM_CODEC_TYPE_AVC1_VIDEO
;
161 summary
->max_au_length
= max_au_length
;
162 summary
->timescale
= sps
->vui
.time_scale
;
163 summary
->timebase
= sps
->vui
.num_units_in_tick
;
164 summary
->vfr
= !sps
->vui
.fixed_frame_rate_flag
;
165 summary
->sample_per_field
= 0;
166 summary
->width
= sps
->cropped_width
;
167 summary
->height
= sps
->cropped_height
;
168 summary
->par_h
= sps
->vui
.sar_width
;
169 summary
->par_v
= sps
->vui
.sar_height
;
170 summary
->color
.primaries_index
= sps
->vui
.colour_primaries
;
171 summary
->color
.transfer_index
= sps
->vui
.transfer_characteristics
;
172 summary
->color
.matrix_index
= sps
->vui
.matrix_coefficients
;
173 summary
->color
.full_range
= sps
->vui
.video_full_range_flag
;
177 static int h264_store_codec_specific
179 h264_importer_t
*h264_imp
,
180 lsmash_h264_specific_parameters_t
*avcC_param
183 lsmash_codec_specific_t
*src_cs
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264
,
184 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
186 return LSMASH_ERR_NAMELESS
;
187 lsmash_h264_specific_parameters_t
*src_param
= (lsmash_h264_specific_parameters_t
*)src_cs
->data
.structured
;
188 *src_param
= *avcC_param
;
189 lsmash_codec_specific_t
*dst_cs
= lsmash_convert_codec_specific_format( src_cs
, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
190 src_param
->parameter_sets
= NULL
; /* Avoid freeing parameter sets within avcC_param. */
191 lsmash_destroy_codec_specific_data( src_cs
);
194 lsmash_destroy_codec_specific_data( dst_cs
);
195 return LSMASH_ERR_NAMELESS
;
197 if( lsmash_add_entry( h264_imp
->avcC_list
, dst_cs
) < 0 )
199 lsmash_destroy_codec_specific_data( dst_cs
);
200 return LSMASH_ERR_MEMORY_ALLOC
;
205 static inline void h264_new_access_unit
207 h264_access_unit_t
*au
211 au
->picture
.type
= H264_PICTURE_TYPE_NONE
;
212 au
->picture
.random_accessible
= 0;
213 au
->picture
.recovery_frame_cnt
= 0;
214 au
->picture
.has_mmco5
= 0;
215 au
->picture
.has_redundancy
= 0;
216 au
->picture
.broken_link_flag
= 0;
219 /* If probe equals 0, don't get the actual data (EBPS) of an access unit and only parse NALU.
220 * Currently, you can get AU of AVC video elemental stream only, not AVC parameter set elemental stream defined in 14496-15. */
221 static int h264_get_access_unit_internal
223 importer_t
*importer
,
227 h264_importer_t
*h264_imp
= (h264_importer_t
*)importer
->info
;
228 h264_info_t
*info
= &h264_imp
->info
;
229 h264_slice_info_t
*slice
= &info
->slice
;
230 h264_access_unit_t
*au
= &info
->au
;
231 h264_picture_info_t
*picture
= &au
->picture
;
232 h264_stream_buffer_t
*sb
= &info
->buffer
;
233 lsmash_bs_t
*bs
= importer
->bs
;
235 h264_new_access_unit( au
);
238 h264_nalu_header_t nuh
;
239 uint64_t start_code_length
;
240 uint64_t trailing_zero_bytes
;
241 uint64_t nalu_length
= h264_find_next_start_code( bs
, &nuh
, &start_code_length
, &trailing_zero_bytes
);
242 if( nalu_length
== NALU_NO_START_CODE_FOUND
)
244 /* For the last NALU.
245 * This NALU already has been appended into the latest access unit and parsed. */
246 h264_update_picture_info( info
, picture
, slice
, &info
->sei
);
247 complete_au
= h264_complete_au( au
, probe
);
249 return h264_get_au_internal_succeeded( h264_imp
, au
);
251 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
, LSMASH_ERR_INVALID_DATA
);
253 uint8_t nalu_type
= nuh
.nal_unit_type
;
254 uint64_t next_sc_head_pos
= h264_imp
->sc_head_pos
257 + trailing_zero_bytes
;
261 fprintf( stderr
, "NALU type: %"PRIu8
" \n", nalu_type
);
262 fprintf( stderr
, " NALU header position: %"PRIx64
" \n", h264_imp
->sc_head_pos
+ start_code_length
);
263 fprintf( stderr
, " EBSP position: %"PRIx64
" \n", h264_imp
->sc_head_pos
+ start_code_length
+ nuh
.length
);
264 fprintf( stderr
, " EBSP length: %"PRIx64
" (%"PRIu64
") \n", nalu_length
- nuh
.length
, nalu_length
- nuh
.length
);
265 fprintf( stderr
, " trailing_zero_bytes: %"PRIx64
" \n", trailing_zero_bytes
);
266 fprintf( stderr
, " Next start code position: %"PRIx64
"\n", next_sc_head_pos
);
269 if( nalu_type
== H264_NALU_TYPE_FD
)
271 /* We don't support streams with both filler and HRD yet.
272 * Otherwise, just skip filler because 'avc1' and 'avc2' samples are forbidden to use filler. */
273 if( info
->sps
.vui
.hrd
.present
)
274 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
, LSMASH_ERR_PATCH_WELCOME
);
276 else if( (nalu_type
>= H264_NALU_TYPE_SLICE_N_IDR
&& nalu_type
<= H264_NALU_TYPE_SPS_EXT
)
277 || nalu_type
== H264_NALU_TYPE_SLICE_AUX
)
280 /* Increase the buffer if needed. */
281 uint64_t possible_au_length
= au
->incomplete_length
+ NALU_DEFAULT_NALU_LENGTH_SIZE
+ nalu_length
;
282 if( sb
->bank
->buffer_size
< possible_au_length
283 && (err
= h264_supplement_buffer( sb
, au
, 2 * possible_au_length
)) < 0 )
285 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to increase the buffer size.\n" );
286 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
, err
);
288 /* Get the EBSP of the current NALU here.
289 * AVC elemental stream defined in 14496-15 can recognizes from 0 to 13, and 19 of nal_unit_type.
290 * We don't support SVC and MVC elemental stream defined in 14496-15 yet. */
291 uint8_t *nalu
= lsmash_bs_get_buffer_data( bs
) + start_code_length
;
292 if( nalu_type
>= H264_NALU_TYPE_SLICE_N_IDR
&& nalu_type
<= H264_NALU_TYPE_SLICE_IDR
)
294 /* VCL NALU (slice) */
295 h264_slice_info_t prev_slice
= *slice
;
296 if( (err
= h264_parse_slice( info
, &nuh
, sb
->rbsp
, nalu
+ nuh
.length
, nalu_length
- nuh
.length
)) < 0 )
297 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
, err
);
298 if( probe
&& info
->avcC_pending
)
300 /* Copy and append a Codec Specific info. */
301 if( (err
= h264_store_codec_specific( h264_imp
, &info
->avcC_param
)) < 0 )
304 if( (err
= h264_move_pending_avcC_param( info
)) < 0 )
306 if( prev_slice
.present
)
308 /* Check whether the AU that contains the previous VCL NALU completed or not. */
309 if( h264_find_au_delimit_by_slice_info( slice
, &prev_slice
) )
311 /* The current NALU is the first VCL NALU of the primary coded picture of an new AU.
312 * Therefore, the previous slice belongs to the AU you want at this time. */
313 h264_update_picture_info( info
, picture
, &prev_slice
, &info
->sei
);
314 complete_au
= h264_complete_au( au
, probe
);
317 h264_update_picture_info_for_slice( info
, picture
, &prev_slice
);
319 h264_append_nalu_to_au( au
, nalu
, nalu_length
, probe
);
324 if( h264_find_au_delimit_by_nalu_type( nalu_type
, info
->prev_nalu_type
) )
326 /* The last slice belongs to the AU you want at this time. */
327 h264_update_picture_info( info
, picture
, slice
, &info
->sei
);
328 complete_au
= h264_complete_au( au
, probe
);
332 case H264_NALU_TYPE_SEI
:
334 if( (err
= h264_parse_sei( info
->bits
, &info
->sps
, &info
->sei
, sb
->rbsp
,
336 nalu_length
- nuh
.length
)) < 0 )
337 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
, err
);
338 h264_append_nalu_to_au( au
, nalu
, nalu_length
, probe
);
341 case H264_NALU_TYPE_SPS
:
342 if( (err
= h264_try_to_append_parameter_set( info
, H264_PARAMETER_SET_TYPE_SPS
, nalu
, nalu_length
)) < 0 )
343 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
, err
);
345 case H264_NALU_TYPE_PPS
:
346 if( (err
= h264_try_to_append_parameter_set( info
, H264_PARAMETER_SET_TYPE_PPS
, nalu
, nalu_length
)) < 0 )
347 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
, err
);
349 case H264_NALU_TYPE_AUD
: /* We drop access unit delimiters. */
351 case H264_NALU_TYPE_SPS_EXT
:
352 if( (err
= h264_try_to_append_parameter_set( info
, H264_PARAMETER_SET_TYPE_SPSEXT
, nalu
, nalu_length
)) < 0 )
353 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
, err
);
356 h264_append_nalu_to_au( au
, nalu
, nalu_length
, probe
);
359 if( info
->avcC_pending
)
360 importer
->status
= IMPORTER_CHANGE
;
363 /* Move to the first byte of the next start code. */
364 info
->prev_nalu_type
= nalu_type
;
365 if( lsmash_bs_read_seek( bs
, next_sc_head_pos
, SEEK_SET
) != next_sc_head_pos
)
367 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to seek the next start code.\n" );
368 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
, LSMASH_ERR_NAMELESS
);
370 /* Check if no more data to read from the stream. */
371 if( !lsmash_bs_is_end( bs
, NALU_SHORT_START_CODE_LENGTH
) )
372 h264_imp
->sc_head_pos
= next_sc_head_pos
;
373 /* If there is no more data in the stream, and flushed chunk of NALUs, flush it as complete AU here. */
374 else if( au
->incomplete_length
&& au
->length
== 0 )
376 h264_update_picture_info( info
, picture
, slice
, &info
->sei
);
377 h264_complete_au( au
, probe
);
378 return h264_get_au_internal_succeeded( h264_imp
, au
);
381 return h264_get_au_internal_succeeded( h264_imp
, au
);
385 static inline void h264_importer_check_eof( importer_t
*importer
, h264_access_unit_t
*au
)
387 /* AVC byte stream NALU consists of at least 4 bytes (start-code + NALU-header). */
388 if( lsmash_bs_is_end( importer
->bs
, NALU_SHORT_START_CODE_LENGTH
) && au
->incomplete_length
== 0 )
389 importer
->status
= IMPORTER_EOF
;
390 else if( importer
->status
!= IMPORTER_CHANGE
)
391 importer
->status
= IMPORTER_OK
;
394 static int h264_importer_get_accessunit
396 importer_t
*importer
,
397 uint32_t track_number
,
398 lsmash_sample_t
**p_sample
401 if( !importer
->info
)
402 return LSMASH_ERR_NAMELESS
;
403 if( track_number
!= 1 )
404 return LSMASH_ERR_FUNCTION_PARAM
;
405 h264_importer_t
*h264_imp
= (h264_importer_t
*)importer
->info
;
406 h264_info_t
*info
= &h264_imp
->info
;
407 importer_status current_status
= importer
->status
;
408 if( current_status
== IMPORTER_ERROR
)
409 return LSMASH_ERR_NAMELESS
;
410 if( current_status
== IMPORTER_EOF
)
412 int err
= h264_get_access_unit_internal( importer
, 0 );
415 importer
->status
= IMPORTER_ERROR
;
418 h264_importer_check_eof( importer
, &info
->au
);
419 if( importer
->status
== IMPORTER_CHANGE
&& !info
->avcC_pending
)
420 current_status
= IMPORTER_CHANGE
;
421 if( current_status
== IMPORTER_CHANGE
)
423 /* Update the active summary. */
424 lsmash_codec_specific_t
*cs
= (lsmash_codec_specific_t
*)lsmash_get_entry_data( h264_imp
->avcC_list
, ++ h264_imp
->avcC_number
);
426 return LSMASH_ERR_NAMELESS
;
427 lsmash_h264_specific_parameters_t
*avcC_param
= (lsmash_h264_specific_parameters_t
*)cs
->data
.structured
;
428 lsmash_video_summary_t
*summary
= h264_create_summary( avcC_param
, &info
->sps
, h264_imp
->max_au_length
);
430 return LSMASH_ERR_NAMELESS
;
431 lsmash_remove_entry( importer
->summaries
, track_number
, lsmash_cleanup_summary
);
432 if( lsmash_add_entry( importer
->summaries
, summary
) < 0 )
434 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
435 return LSMASH_ERR_MEMORY_ALLOC
;
437 importer
->status
= IMPORTER_OK
;
439 lsmash_sample_t
*sample
= lsmash_create_sample( h264_imp
->max_au_length
);
441 return LSMASH_ERR_MEMORY_ALLOC
;
443 h264_access_unit_t
*au
= &info
->au
;
444 h264_picture_info_t
*picture
= &au
->picture
;
445 sample
->dts
= h264_imp
->ts_list
.timestamp
[ au
->number
- 1 ].dts
;
446 sample
->cts
= h264_imp
->ts_list
.timestamp
[ au
->number
- 1 ].cts
;
447 if( au
->number
< h264_imp
->num_undecodable
)
448 sample
->prop
.leading
= ISOM_SAMPLE_IS_UNDECODABLE_LEADING
;
450 sample
->prop
.leading
= picture
->independent
|| sample
->cts
>= h264_imp
->last_intra_cts
451 ? ISOM_SAMPLE_IS_NOT_LEADING
: ISOM_SAMPLE_IS_UNDECODABLE_LEADING
;
452 if( picture
->independent
)
453 h264_imp
->last_intra_cts
= sample
->cts
;
454 if( h264_imp
->composition_reordering_present
&& !picture
->disposable
&& !picture
->idr
)
455 sample
->prop
.allow_earlier
= QT_SAMPLE_EARLIER_PTS_ALLOWED
;
456 sample
->prop
.independent
= picture
->independent
? ISOM_SAMPLE_IS_INDEPENDENT
: ISOM_SAMPLE_IS_NOT_INDEPENDENT
;
457 sample
->prop
.disposable
= picture
->disposable
? ISOM_SAMPLE_IS_DISPOSABLE
: ISOM_SAMPLE_IS_NOT_DISPOSABLE
;
458 sample
->prop
.redundant
= picture
->has_redundancy
? ISOM_SAMPLE_HAS_REDUNDANCY
: ISOM_SAMPLE_HAS_NO_REDUNDANCY
;
459 sample
->prop
.post_roll
.identifier
= picture
->frame_num
;
460 if( picture
->random_accessible
)
463 sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
464 else if( picture
->recovery_frame_cnt
)
466 sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_POST_ROLL_START
;
467 sample
->prop
.post_roll
.complete
= (picture
->frame_num
+ picture
->recovery_frame_cnt
) % info
->sps
.MaxFrameNum
;
471 sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP
;
472 if( !picture
->broken_link_flag
)
473 sample
->prop
.ra_flags
|= QT_SAMPLE_RANDOM_ACCESS_FLAG_PARTIAL_SYNC
;
476 sample
->length
= au
->length
;
477 memcpy( sample
->data
, au
->data
, au
->length
);
478 return current_status
;
481 static void nalu_deduplicate_poc
483 nal_pic_timing_t
*npt
,
484 uint32_t *max_composition_delay
,
485 uint32_t num_access_units
,
486 uint32_t max_num_reorder_pics
489 /* Deduplicate POCs. */
490 int64_t poc_offset
= 0;
492 int64_t invalid_poc_min
= 0;
493 uint32_t last_poc_reset
= UINT32_MAX
;
494 uint32_t invalid_poc_start
= 0;
495 int invalid_poc_present
= 0;
496 for( uint32_t i
= 0; ; i
++ )
498 if( i
< num_access_units
&& npt
[i
].poc
!= 0 && !npt
[i
].reset
)
500 /* poc_offset is not added to each POC here.
501 * It is done when we encounter the next coded video sequence. */
504 /* Pictures with negative POC shall precede IDR-picture in composition order.
505 * The minimum POC is added to poc_offset when we encounter the next coded video sequence. */
506 if( last_poc_reset
== UINT32_MAX
|| i
> last_poc_reset
+ max_num_reorder_pics
)
508 if( !invalid_poc_present
)
510 invalid_poc_present
= 1;
511 invalid_poc_start
= i
;
513 if( invalid_poc_min
> npt
[i
].poc
)
514 invalid_poc_min
= npt
[i
].poc
;
516 else if( poc_min
> npt
[i
].poc
)
518 poc_min
= npt
[i
].poc
;
519 *max_composition_delay
= LSMASH_MAX( *max_composition_delay
, i
- last_poc_reset
);
524 /* Encountered a new coded video sequence or no more POCs.
525 * Add poc_offset to each POC of the previous coded video sequence. */
526 poc_offset
-= poc_min
;
528 for( uint32_t j
= last_poc_reset
; j
< i
+ !!npt
[i
].reset
; j
++ )
529 if( npt
[j
].poc
>= 0 || (j
<= last_poc_reset
+ max_num_reorder_pics
) )
531 npt
[j
].poc
+= poc_offset
;
532 if( poc_max
< npt
[j
].poc
)
533 poc_max
= npt
[j
].poc
;
535 poc_offset
= poc_max
+ 1;
536 if( invalid_poc_present
)
538 /* Pictures with invalid negative POC is probably supposed to be composited
539 * both before the next coded video sequence and after the current one. */
540 poc_offset
-= invalid_poc_min
;
541 for( uint32_t j
= invalid_poc_start
; j
< i
+ !!npt
[i
].reset
; j
++ )
544 npt
[j
].poc
+= poc_offset
;
545 if( poc_max
< npt
[j
].poc
)
546 poc_max
= npt
[j
].poc
;
548 invalid_poc_present
= 0;
549 invalid_poc_start
= 0;
551 poc_offset
= poc_max
+ 1;
553 if( i
< num_access_units
)
561 break; /* no more POCs */
565 static void nalu_generate_timestamps_from_poc
567 importer_t
*importer
,
568 lsmash_media_ts_t
*timestamp
,
569 nal_pic_timing_t
*npt
,
570 uint8_t *composition_reordering_present
,
571 uint32_t *last_delta
,
572 uint32_t max_composition_delay
,
573 uint32_t num_access_units
576 /* Check if composition delay derived from reordering is present. */
577 if( max_composition_delay
== 0 )
579 for( uint32_t i
= 1; i
< num_access_units
; i
++ )
580 if( npt
[i
].poc
< npt
[i
- 1].poc
)
582 *composition_reordering_present
= 1;
587 *composition_reordering_present
= 1;
588 /* Generate timestamps. */
589 if( *composition_reordering_present
)
591 /* Generate timestamps.
592 * Here, DTSs and CTSs are temporary values for sort. */
593 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
595 timestamp
[i
].cts
= (uint64_t)npt
[i
].poc
;
596 timestamp
[i
].dts
= (uint64_t)i
;
598 qsort( timestamp
, num_access_units
, sizeof(lsmash_media_ts_t
), (int(*)( const void *, const void * ))lsmash_compare_cts
);
599 /* Check POC gap in output order. */
600 lsmash_class_t
*logger
= &(lsmash_class_t
){ .name
= importer
->class->name
};
601 for( uint32_t i
= 1; i
< num_access_units
; i
++ )
602 if( timestamp
[i
].cts
> timestamp
[i
- 1].cts
+ npt
[i
- 1].poc_delta
)
603 lsmash_log( &logger
, LSMASH_LOG_WARNING
,
604 "POC gap is detected at picture %"PRIu64
". Maybe some pictures are lost.\n", timestamp
[i
].dts
);
605 /* Get the maximum composition delay derived from reordering. */
606 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
607 if( i
< timestamp
[i
].dts
)
609 uint32_t composition_delay
= timestamp
[i
].dts
- i
;
610 max_composition_delay
= LSMASH_MAX( max_composition_delay
, composition_delay
);
612 uint64_t *ts_buffer
= (uint64_t *)lsmash_malloc( (num_access_units
+ max_composition_delay
) * sizeof(uint64_t) );
615 /* It seems that there is no enough memory to generate more appropriate timestamps.
616 * Anyway, generate CTSs and DTSs. */
617 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
618 timestamp
[i
].cts
= i
+ max_composition_delay
;
619 qsort( timestamp
, num_access_units
, sizeof(lsmash_media_ts_t
), (int(*)( const void *, const void * ))lsmash_compare_dts
);
623 uint64_t *reorder_cts
= ts_buffer
;
624 uint64_t *prev_reorder_cts
= ts_buffer
+ num_access_units
;
625 *last_delta
= npt
[num_access_units
- 1].delta
;
627 timestamp
[0].cts
= 0;
628 for( uint32_t i
= 1; i
< num_access_units
; i
++ )
629 timestamp
[i
].cts
= timestamp
[i
- 1].cts
+ npt
[i
- 1].delta
;
630 int64_t composition_delay_time
= timestamp
[max_composition_delay
].cts
;
631 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
633 timestamp
[i
].cts
+= composition_delay_time
;
634 reorder_cts
[i
] = timestamp
[i
].cts
;
637 qsort( timestamp
, num_access_units
, sizeof(lsmash_media_ts_t
), (int(*)( const void *, const void * ))lsmash_compare_dts
);
638 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
640 timestamp
[i
].dts
= i
<= max_composition_delay
641 ? reorder_cts
[i
] - composition_delay_time
642 : prev_reorder_cts
[(i
- max_composition_delay
) % max_composition_delay
];
643 prev_reorder_cts
[i
% max_composition_delay
] = reorder_cts
[i
];
645 lsmash_free( ts_buffer
);
647 fprintf( stderr
, "max_composition_delay=%"PRIu32
", composition_delay_time=%"PRIu64
"\n",
648 max_composition_delay
, composition_delay_time
);
653 timestamp
[0].dts
= 0;
654 timestamp
[0].cts
= 0;
655 for( uint32_t i
= 1; i
< num_access_units
; i
++ )
657 timestamp
[i
].dts
= timestamp
[i
- 1].dts
+ npt
[i
- 1].delta
;
658 timestamp
[i
].cts
= timestamp
[i
- 1].cts
+ npt
[i
- 1].delta
;
660 *last_delta
= npt
[num_access_units
- 1].delta
;
664 static void nalu_reduce_timescale
666 lsmash_media_ts_t
*timestamp
,
667 nal_pic_timing_t
*npt
,
668 uint32_t *last_delta
,
670 uint32_t num_access_units
673 uint64_t gcd_delta
= *timescale
;
674 for( uint32_t i
= 0; i
< num_access_units
&& gcd_delta
> 1; i
++ )
675 gcd_delta
= lsmash_get_gcd( gcd_delta
, npt
[i
].delta
);
678 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
680 timestamp
[i
].dts
/= gcd_delta
;
681 timestamp
[i
].cts
/= gcd_delta
;
683 *last_delta
/= gcd_delta
;
684 *timescale
/= gcd_delta
;
687 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
688 fprintf( stderr
, "Timestamp[%"PRIu32
"]: POC=%"PRId64
", DTS=%"PRIu64
", CTS=%"PRIu64
"\n",
689 i
, npt
[i
].poc
, timestamp
[i
].dts
, timestamp
[i
].cts
);
693 static lsmash_video_summary_t
*h264_setup_first_summary
698 h264_importer_t
*h264_imp
= (h264_importer_t
*)importer
->info
;
699 lsmash_codec_specific_t
*cs
= (lsmash_codec_specific_t
*)lsmash_get_entry_data( h264_imp
->avcC_list
, ++ h264_imp
->avcC_number
);
700 if( !cs
|| !cs
->data
.structured
)
702 lsmash_destroy_codec_specific_data( cs
);
705 lsmash_video_summary_t
*summary
= h264_create_summary( (lsmash_h264_specific_parameters_t
*)cs
->data
.structured
,
706 &h264_imp
->info
.sps
, h264_imp
->max_au_length
);
709 lsmash_destroy_codec_specific_data( cs
);
712 if( lsmash_add_entry( importer
->summaries
, summary
) < 0 )
714 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
717 summary
->sample_per_field
= h264_imp
->field_pic_present
;
721 static int h264_analyze_whole_stream
726 /* Parse all NALU in the stream for preparation of calculating timestamps. */
727 uint32_t npt_alloc
= (1 << 12) * sizeof(nal_pic_timing_t
);
728 nal_pic_timing_t
*npt
= lsmash_malloc( npt_alloc
);
730 return LSMASH_ERR_MEMORY_ALLOC
;
731 uint32_t picture_stats
[H264_PICTURE_TYPE_NONE
+ 1] = { 0 };
732 uint32_t num_access_units
= 0;
733 lsmash_class_t
*logger
= &(lsmash_class_t
){ "H.264" };
734 lsmash_log( &logger
, LSMASH_LOG_INFO
, "Analyzing stream as H.264\r" );
735 h264_importer_t
*h264_imp
= (h264_importer_t
*)importer
->info
;
736 h264_info_t
*info
= &h264_imp
->info
;
737 importer
->status
= IMPORTER_OK
;
738 int err
= LSMASH_ERR_MEMORY_ALLOC
;
739 while( importer
->status
!= IMPORTER_EOF
)
742 lsmash_log( &logger
, LSMASH_LOG_INFO
, "Analyzing stream as H.264: %"PRIu32
"\n", num_access_units
+ 1 );
744 h264_picture_info_t
*picture
= &info
->au
.picture
;
745 h264_picture_info_t prev_picture
= *picture
;
746 if( (err
= h264_get_access_unit_internal( importer
, 1 )) < 0
747 || (err
= h264_calculate_poc( info
, picture
, &prev_picture
)) < 0 )
749 h264_importer_check_eof( importer
, &info
->au
);
750 if( npt_alloc
<= num_access_units
* sizeof(nal_pic_timing_t
) )
752 uint32_t alloc
= 2 * num_access_units
* sizeof(nal_pic_timing_t
);
753 nal_pic_timing_t
*temp
= (nal_pic_timing_t
*)lsmash_realloc( npt
, alloc
);
759 h264_imp
->field_pic_present
|= picture
->field_pic_flag
;
760 npt
[num_access_units
].poc
= picture
->PicOrderCnt
;
761 npt
[num_access_units
].delta
= picture
->delta
;
762 npt
[num_access_units
].poc_delta
= picture
->field_pic_flag
? 1 : 2;
763 npt
[num_access_units
].reset
= picture
->has_mmco5
;
765 h264_imp
->max_au_length
= LSMASH_MAX( info
->au
.length
, h264_imp
->max_au_length
);
767 ++picture_stats
[H264_PICTURE_TYPE_IDR
];
768 else if( picture
->type
>= H264_PICTURE_TYPE_NONE
)
769 ++picture_stats
[H264_PICTURE_TYPE_NONE
];
771 ++picture_stats
[ picture
->type
];
773 lsmash_log_refresh_line( &logger
);
774 lsmash_log( &logger
, LSMASH_LOG_INFO
,
775 "IDR: %"PRIu32
", I: %"PRIu32
", P: %"PRIu32
", B: %"PRIu32
", "
776 "SI: %"PRIu32
", SP: %"PRIu32
", Unknown: %"PRIu32
"\n",
777 picture_stats
[H264_PICTURE_TYPE_IDR
],
778 picture_stats
[H264_PICTURE_TYPE_I
],
779 picture_stats
[H264_PICTURE_TYPE_I_P
],
780 picture_stats
[H264_PICTURE_TYPE_I_P_B
],
781 picture_stats
[H264_PICTURE_TYPE_SI
]
782 + picture_stats
[H264_PICTURE_TYPE_I_SI
],
783 picture_stats
[H264_PICTURE_TYPE_SI_SP
]
784 + picture_stats
[H264_PICTURE_TYPE_I_SI_P_SP
]
785 + picture_stats
[H264_PICTURE_TYPE_I_SI_P_SP_B
],
786 picture_stats
[H264_PICTURE_TYPE_NONE
] );
787 /* Copy and append the last Codec Specific info. */
788 if( (err
= h264_store_codec_specific( h264_imp
, &info
->avcC_param
)) < 0 )
790 /* Set up the first summary. */
791 lsmash_video_summary_t
*summary
= h264_setup_first_summary( importer
);
794 err
= LSMASH_ERR_NAMELESS
;
797 /* Allocate timestamps. */
798 lsmash_media_ts_t
*timestamp
= lsmash_malloc( num_access_units
* sizeof(lsmash_media_ts_t
) );
801 /* Count leading samples that are undecodable. */
802 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
804 if( npt
[i
].poc
== 0 )
806 ++ h264_imp
->num_undecodable
;
808 /* Deduplicate POCs. */
809 uint32_t max_composition_delay
= 0;
810 nalu_deduplicate_poc( npt
, &max_composition_delay
, num_access_units
, 32 );
811 /* Generate timestamps. */
812 nalu_generate_timestamps_from_poc( importer
, timestamp
, npt
,
813 &h264_imp
->composition_reordering_present
,
814 &h264_imp
->last_delta
,
815 max_composition_delay
, num_access_units
);
816 nalu_reduce_timescale( timestamp
, npt
, &h264_imp
->last_delta
, &summary
->timescale
, num_access_units
);
818 h264_imp
->ts_list
.sample_count
= num_access_units
;
819 h264_imp
->ts_list
.timestamp
= timestamp
;
822 lsmash_log_refresh_line( &logger
);
827 static int h264_importer_probe( importer_t
*importer
)
829 /* Find the first start code. */
830 h264_importer_t
*h264_imp
= create_h264_importer( importer
);
832 return LSMASH_ERR_MEMORY_ALLOC
;
833 lsmash_bs_t
*bs
= importer
->bs
;
834 uint64_t first_sc_head_pos
= nalu_find_first_start_code( bs
);
836 if( first_sc_head_pos
== NALU_NO_START_CODE_FOUND
)
838 err
= LSMASH_ERR_INVALID_DATA
;
841 /* OK. It seems the stream has a long start code of H.264. */
842 importer
->info
= h264_imp
;
843 h264_info_t
*info
= &h264_imp
->info
;
844 lsmash_bs_read_seek( bs
, first_sc_head_pos
, SEEK_SET
);
845 h264_imp
->sc_head_pos
= first_sc_head_pos
;
846 if( (err
= h264_analyze_whole_stream( importer
)) < 0 )
848 /* Go back to the start code of the first NALU. */
849 importer
->status
= IMPORTER_OK
;
850 lsmash_bs_read_seek( bs
, first_sc_head_pos
, SEEK_SET
);
851 h264_imp
->sc_head_pos
= first_sc_head_pos
;
852 info
->prev_nalu_type
= H264_NALU_TYPE_UNSPECIFIED0
;
853 uint8_t *temp_au
= info
->au
.data
;
854 uint8_t *temp_incomplete_au
= info
->au
.incomplete_data
;
855 memset( &info
->au
, 0, sizeof(h264_access_unit_t
) );
856 info
->au
.data
= temp_au
;
857 info
->au
.incomplete_data
= temp_incomplete_au
;
858 memset( &info
->slice
, 0, sizeof(h264_slice_info_t
) );
859 memset( &info
->sps
, 0, sizeof(h264_sps_t
) );
860 memset( &info
->pps
, 0, sizeof(h264_pps_t
) );
861 lsmash_remove_entries( info
->avcC_param
.parameter_sets
->sps_list
, isom_remove_dcr_ps
);
862 lsmash_remove_entries( info
->avcC_param
.parameter_sets
->pps_list
, isom_remove_dcr_ps
);
863 lsmash_remove_entries( info
->avcC_param
.parameter_sets
->spsext_list
, isom_remove_dcr_ps
);
864 lsmash_destroy_h264_parameter_sets( &info
->avcC_param_next
);
867 remove_h264_importer( h264_imp
);
868 importer
->info
= NULL
;
869 lsmash_remove_entries( importer
->summaries
, lsmash_cleanup_summary
);
873 static uint32_t h264_importer_get_last_delta( importer_t
*importer
, uint32_t track_number
)
875 debug_if( !importer
|| !importer
->info
)
877 h264_importer_t
*h264_imp
= (h264_importer_t
*)importer
->info
;
878 if( !h264_imp
|| track_number
!= 1 || importer
->status
!= IMPORTER_EOF
)
880 return h264_imp
->ts_list
.sample_count
881 ? h264_imp
->last_delta
882 : UINT32_MAX
; /* arbitrary */
885 const importer_functions h264_importer
=
887 { "H.264", offsetof( importer_t
, log_level
) },
890 h264_importer_get_accessunit
,
891 h264_importer_get_last_delta
,
892 h264_importer_cleanup
895 /***************************************************************************
897 ITU-T Recommendation H.265 (04/13)
898 ISO/IEC 14496-15:2014
899 ***************************************************************************/
900 #include "codecs/hevc.h"
905 lsmash_entry_list_t hvcC_list
[1]; /* stored as lsmash_codec_specific_t */
906 lsmash_media_ts_list_t ts_list
;
907 uint32_t max_au_length
;
908 uint32_t num_undecodable
;
909 uint32_t hvcC_number
;
911 uint64_t last_intra_cts
;
912 uint64_t sc_head_pos
;
913 uint8_t composition_reordering_present
;
914 uint8_t field_pic_present
;
915 uint8_t max_TemporalId
;
918 static void remove_hevc_importer( hevc_importer_t
*hevc_imp
)
922 lsmash_remove_entries( hevc_imp
->hvcC_list
, lsmash_destroy_codec_specific_data
);
923 hevc_cleanup_parser( &hevc_imp
->info
);
924 lsmash_free( hevc_imp
->ts_list
.timestamp
);
925 lsmash_free( hevc_imp
);
928 static void hevc_importer_cleanup( importer_t
*importer
)
930 debug_if( importer
&& importer
->info
)
931 remove_hevc_importer( importer
->info
);
934 static hevc_importer_t
*create_hevc_importer( importer_t
*importer
)
936 hevc_importer_t
*hevc_imp
= lsmash_malloc_zero( sizeof(hevc_importer_t
) );
939 if( hevc_setup_parser( &hevc_imp
->info
, 0 ) < 0 )
941 remove_hevc_importer( hevc_imp
);
944 lsmash_init_entry_list( hevc_imp
->hvcC_list
);
945 hevc_imp
->info
.eos
= 1;
949 static inline int hevc_complete_au( hevc_access_unit_t
*au
, int probe
)
951 if( !au
->picture
.has_primary
|| au
->incomplete_length
== 0 )
954 memcpy( au
->data
, au
->incomplete_data
, au
->incomplete_length
);
955 au
->TemporalId
= au
->picture
.TemporalId
;
956 au
->length
= au
->incomplete_length
;
957 au
->incomplete_length
= 0;
958 au
->picture
.has_primary
= 0;
962 static void hevc_append_nalu_to_au( hevc_access_unit_t
*au
, uint8_t *src_nalu
, uint32_t nalu_length
, int probe
)
966 uint8_t *dst_nalu
= au
->incomplete_data
+ au
->incomplete_length
+ NALU_DEFAULT_NALU_LENGTH_SIZE
;
967 for( int i
= NALU_DEFAULT_NALU_LENGTH_SIZE
; i
; i
-- )
968 *(dst_nalu
- i
) = (nalu_length
>> ((i
- 1) * 8)) & 0xff;
969 memcpy( dst_nalu
, src_nalu
, nalu_length
);
971 /* Note: picture->incomplete_au_length shall be 0 immediately after AU has completed.
972 * Therefore, possible_au_length in hevc_get_access_unit_internal() can't be used here
973 * to avoid increasing AU length monotonously through the entire stream. */
974 au
->incomplete_length
+= NALU_DEFAULT_NALU_LENGTH_SIZE
+ nalu_length
;
977 static int hevc_get_au_internal_succeeded( hevc_importer_t
*hevc_imp
, hevc_access_unit_t
*au
)
983 static int hevc_get_au_internal_failed( hevc_importer_t
*hevc_imp
, hevc_access_unit_t
*au
, int complete_au
, int ret
)
990 static lsmash_video_summary_t
*hevc_create_summary
992 lsmash_hevc_specific_parameters_t
*param
,
994 uint32_t max_au_length
997 lsmash_video_summary_t
*summary
= (lsmash_video_summary_t
*)lsmash_create_summary( LSMASH_SUMMARY_TYPE_VIDEO
);
1000 /* Update summary here.
1001 * max_au_length is set at the last of hevc_importer_probe function. */
1002 lsmash_codec_specific_t
*specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_HEVC
,
1003 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
);
1006 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
1009 specific
->data
.unstructured
= lsmash_create_hevc_specific_info( param
, &specific
->size
);
1010 if( !specific
->data
.unstructured
1011 || lsmash_add_entry( &summary
->opaque
->list
, specific
) < 0 )
1013 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
1014 lsmash_destroy_codec_specific_data( specific
);
1017 summary
->sample_type
= ISOM_CODEC_TYPE_HVC1_VIDEO
;
1018 summary
->max_au_length
= max_au_length
;
1019 summary
->timescale
= sps
->vui
.time_scale
;
1020 summary
->timebase
= sps
->vui
.num_units_in_tick
;
1021 summary
->vfr
= (param
->constantFrameRate
== 0);
1022 summary
->sample_per_field
= 0;
1023 summary
->width
= sps
->cropped_width
;
1024 summary
->height
= sps
->cropped_height
;
1025 summary
->par_h
= sps
->vui
.sar_width
;
1026 summary
->par_v
= sps
->vui
.sar_height
;
1027 summary
->color
.primaries_index
= sps
->vui
.colour_primaries
!= 2 ? sps
->vui
.colour_primaries
: 0;
1028 summary
->color
.transfer_index
= sps
->vui
.transfer_characteristics
!= 2 ? sps
->vui
.transfer_characteristics
: 0;
1029 summary
->color
.matrix_index
= sps
->vui
.matrix_coeffs
!= 2 ? sps
->vui
.matrix_coeffs
: 0;
1030 summary
->color
.full_range
= sps
->vui
.video_full_range_flag
;
1031 lsmash_convert_crop_into_clap( sps
->vui
.def_disp_win_offset
, summary
->width
, summary
->height
, &summary
->clap
);
1035 static int hevc_store_codec_specific
1037 hevc_importer_t
*hevc_imp
,
1038 lsmash_hevc_specific_parameters_t
*hvcC_param
1041 lsmash_codec_specific_t
*src_cs
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_HEVC
,
1042 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
1044 return LSMASH_ERR_NAMELESS
;
1045 lsmash_hevc_specific_parameters_t
*src_param
= (lsmash_hevc_specific_parameters_t
*)src_cs
->data
.structured
;
1046 *src_param
= *hvcC_param
;
1047 lsmash_codec_specific_t
*dst_cs
= lsmash_convert_codec_specific_format( src_cs
, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
1048 src_param
->parameter_arrays
= NULL
; /* Avoid freeing parameter arrays within hvcC_param. */
1049 lsmash_destroy_codec_specific_data( src_cs
);
1052 lsmash_destroy_codec_specific_data( dst_cs
);
1053 return LSMASH_ERR_NAMELESS
;
1055 if( lsmash_add_entry( hevc_imp
->hvcC_list
, dst_cs
) < 0 )
1057 lsmash_destroy_codec_specific_data( dst_cs
);
1058 return LSMASH_ERR_MEMORY_ALLOC
;
1063 static inline void hevc_new_access_unit( hevc_access_unit_t
*au
)
1066 au
->picture
.type
= HEVC_PICTURE_TYPE_NONE
;
1067 au
->picture
.random_accessible
= 0;
1068 au
->picture
.recovery_poc_cnt
= 0;
1071 /* If probe equals 0, don't get the actual data (EBPS) of an access unit and only parse NALU. */
1072 static int hevc_get_access_unit_internal
1074 importer_t
*importer
,
1078 hevc_importer_t
*hevc_imp
= (hevc_importer_t
*)importer
->info
;
1079 hevc_info_t
*info
= &hevc_imp
->info
;
1080 hevc_slice_info_t
*slice
= &info
->slice
;
1081 hevc_access_unit_t
*au
= &info
->au
;
1082 hevc_picture_info_t
*picture
= &au
->picture
;
1083 hevc_stream_buffer_t
*sb
= &info
->buffer
;
1084 lsmash_bs_t
*bs
= importer
->bs
;
1085 int complete_au
= 0;
1086 hevc_new_access_unit( au
);
1089 hevc_nalu_header_t nuh
;
1090 uint64_t start_code_length
;
1091 uint64_t trailing_zero_bytes
;
1092 uint64_t nalu_length
= hevc_find_next_start_code( bs
, &nuh
, &start_code_length
, &trailing_zero_bytes
);
1093 if( nalu_length
== NALU_NO_START_CODE_FOUND
)
1095 /* For the last NALU.
1096 * This NALU already has been appended into the latest access unit and parsed. */
1097 hevc_update_picture_info( info
, picture
, slice
, &info
->sps
, &info
->sei
);
1098 complete_au
= hevc_complete_au( au
, probe
);
1100 return hevc_get_au_internal_succeeded( hevc_imp
, au
);
1102 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
, LSMASH_ERR_INVALID_DATA
);
1104 uint8_t nalu_type
= nuh
.nal_unit_type
;
1105 uint64_t next_sc_head_pos
= hevc_imp
->sc_head_pos
1108 + trailing_zero_bytes
;
1112 fprintf( stderr
, "NALU type: %"PRIu8
" \n", nalu_type
);
1113 fprintf( stderr
, " NALU header position: %"PRIx64
" \n", hevc_imp
->sc_head_pos
+ start_code_length
);
1114 fprintf( stderr
, " EBSP position: %"PRIx64
" \n", hevc_imp
->sc_head_pos
+ start_code_length
+ nuh
.length
);
1115 fprintf( stderr
, " EBSP length: %"PRIx64
" (%"PRIu64
") \n", nalu_length
- nuh
.length
, nalu_length
- nuh
.length
);
1116 fprintf( stderr
, " trailing_zero_bytes: %"PRIx64
" \n", trailing_zero_bytes
);
1117 fprintf( stderr
, " Next start code position: %"PRIx64
"\n", next_sc_head_pos
);
1120 /* Check if the end of sequence. Used for POC calculation. */
1121 info
->eos
|= info
->prev_nalu_type
== HEVC_NALU_TYPE_EOS
1122 || info
->prev_nalu_type
== HEVC_NALU_TYPE_EOB
;
1123 /* Process the current NALU by its type. */
1124 if( nalu_type
== HEVC_NALU_TYPE_FD
)
1126 /* We don't support streams with both filler and HRD yet. Otherwise, just skip filler. */
1127 if( info
->sps
.vui
.hrd
.present
)
1128 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
, LSMASH_ERR_PATCH_WELCOME
);
1130 else if( nalu_type
<= HEVC_NALU_TYPE_RASL_R
1131 || (nalu_type
>= HEVC_NALU_TYPE_BLA_W_LP
&& nalu_type
<= HEVC_NALU_TYPE_CRA
)
1132 || (nalu_type
>= HEVC_NALU_TYPE_VPS
&& nalu_type
<= HEVC_NALU_TYPE_SUFFIX_SEI
) )
1135 /* Increase the buffer if needed. */
1136 uint64_t possible_au_length
= au
->incomplete_length
+ NALU_DEFAULT_NALU_LENGTH_SIZE
+ nalu_length
;
1137 if( sb
->bank
->buffer_size
< possible_au_length
1138 && (err
= hevc_supplement_buffer( sb
, au
, 2 * possible_au_length
)) < 0 )
1140 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to increase the buffer size.\n" );
1141 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
, err
);
1143 /* Get the EBSP of the current NALU here. */
1144 uint8_t *nalu
= lsmash_bs_get_buffer_data( bs
) + start_code_length
;
1145 if( nalu_type
<= HEVC_NALU_TYPE_RSV_VCL31
)
1147 /* VCL NALU (slice) */
1148 hevc_slice_info_t prev_slice
= *slice
;
1149 if( (err
= hevc_parse_slice_segment_header( info
, &nuh
, sb
->rbsp
,
1151 nalu_length
- nuh
.length
)) < 0 )
1152 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
, err
);
1153 if( probe
&& info
->hvcC_pending
)
1155 /* Copy and append a Codec Specific info. */
1156 if( (err
= hevc_store_codec_specific( hevc_imp
, &info
->hvcC_param
)) < 0 )
1159 if( (err
= hevc_move_pending_hvcC_param( info
)) < 0 )
1161 if( prev_slice
.present
)
1163 /* Check whether the AU that contains the previous VCL NALU completed or not. */
1164 if( hevc_find_au_delimit_by_slice_info( info
, slice
, &prev_slice
) )
1166 /* The current NALU is the first VCL NALU of the primary coded picture of a new AU.
1167 * Therefore, the previous slice belongs to the AU you want at this time. */
1168 hevc_update_picture_info( info
, picture
, &prev_slice
, &info
->sps
, &info
->sei
);
1169 complete_au
= hevc_complete_au( au
, probe
);
1172 hevc_update_picture_info_for_slice( info
, picture
, &prev_slice
);
1174 hevc_append_nalu_to_au( au
, nalu
, nalu_length
, probe
);
1179 if( hevc_find_au_delimit_by_nalu_type( nalu_type
, info
->prev_nalu_type
) )
1181 /* The last slice belongs to the AU you want at this time. */
1182 hevc_update_picture_info( info
, picture
, slice
, &info
->sps
, &info
->sei
);
1183 complete_au
= hevc_complete_au( au
, probe
);
1187 case HEVC_NALU_TYPE_PREFIX_SEI
:
1188 case HEVC_NALU_TYPE_SUFFIX_SEI
:
1190 if( (err
= hevc_parse_sei( info
->bits
, &info
->vps
, &info
->sps
, &info
->sei
, &nuh
,
1191 sb
->rbsp
, nalu
+ nuh
.length
, nalu_length
- nuh
.length
)) < 0 )
1192 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
, err
);
1193 hevc_append_nalu_to_au( au
, nalu
, nalu_length
, probe
);
1196 case HEVC_NALU_TYPE_VPS
:
1197 if( (err
= hevc_try_to_append_dcr_nalu( info
, HEVC_DCR_NALU_TYPE_VPS
, nalu
, nalu_length
)) < 0 )
1198 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
, err
);
1200 case HEVC_NALU_TYPE_SPS
:
1201 if( (err
= hevc_try_to_append_dcr_nalu( info
, HEVC_DCR_NALU_TYPE_SPS
, nalu
, nalu_length
)) < 0 )
1202 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
, err
);
1204 case HEVC_NALU_TYPE_PPS
:
1205 if( (err
= hevc_try_to_append_dcr_nalu( info
, HEVC_DCR_NALU_TYPE_PPS
, nalu
, nalu_length
)) < 0 )
1206 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
, err
);
1208 case HEVC_NALU_TYPE_AUD
: /* We drop access unit delimiters. */
1211 hevc_append_nalu_to_au( au
, nalu
, nalu_length
, probe
);
1214 if( info
->hvcC_pending
)
1215 importer
->status
= IMPORTER_CHANGE
;
1218 /* Move to the first byte of the next start code. */
1219 info
->prev_nalu_type
= nalu_type
;
1220 if( lsmash_bs_read_seek( bs
, next_sc_head_pos
, SEEK_SET
) != next_sc_head_pos
)
1222 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to seek the next start code.\n" );
1223 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
, LSMASH_ERR_NAMELESS
);
1225 if( !lsmash_bs_is_end( bs
, NALU_SHORT_START_CODE_LENGTH
) )
1226 hevc_imp
->sc_head_pos
= next_sc_head_pos
;
1227 /* If there is no more data in the stream, and flushed chunk of NALUs, flush it as complete AU here. */
1228 else if( au
->incomplete_length
&& au
->length
== 0 )
1230 hevc_update_picture_info( info
, picture
, slice
, &info
->sps
, &info
->sei
);
1231 hevc_complete_au( au
, probe
);
1232 return hevc_get_au_internal_succeeded( hevc_imp
, au
);
1235 return hevc_get_au_internal_succeeded( hevc_imp
, au
);
1239 static inline void hevc_importer_check_eof( importer_t
*importer
, hevc_access_unit_t
*au
)
1241 /* HEVC byte stream NALU consists of at least 5 bytes (start-code + NALU-header). */
1242 if( lsmash_bs_is_end( importer
->bs
, NALU_SHORT_START_CODE_LENGTH
+ 1 ) && au
->incomplete_length
== 0 )
1243 importer
->status
= IMPORTER_EOF
;
1244 else if( importer
->status
!= IMPORTER_CHANGE
)
1245 importer
->status
= IMPORTER_OK
;
1248 static int hevc_importer_get_accessunit( importer_t
*importer
, uint32_t track_number
, lsmash_sample_t
**p_sample
)
1250 if( !importer
->info
)
1251 return LSMASH_ERR_NAMELESS
;
1252 if( track_number
!= 1 )
1253 return LSMASH_ERR_FUNCTION_PARAM
;
1254 hevc_importer_t
*hevc_imp
= (hevc_importer_t
*)importer
->info
;
1255 hevc_info_t
*info
= &hevc_imp
->info
;
1256 importer_status current_status
= importer
->status
;
1257 if( current_status
== IMPORTER_ERROR
)
1258 return LSMASH_ERR_NAMELESS
;
1259 if( current_status
== IMPORTER_EOF
)
1260 return IMPORTER_EOF
;
1261 int err
= hevc_get_access_unit_internal( importer
, 0 );
1264 importer
->status
= IMPORTER_ERROR
;
1267 hevc_importer_check_eof( importer
, &info
->au
);
1268 if( importer
->status
== IMPORTER_CHANGE
&& !info
->hvcC_pending
)
1269 current_status
= IMPORTER_CHANGE
;
1270 if( current_status
== IMPORTER_CHANGE
)
1272 /* Update the active summary. */
1273 lsmash_codec_specific_t
*cs
= (lsmash_codec_specific_t
*)lsmash_get_entry_data( hevc_imp
->hvcC_list
, ++ hevc_imp
->hvcC_number
);
1275 return LSMASH_ERR_NAMELESS
;
1276 lsmash_hevc_specific_parameters_t
*hvcC_param
= (lsmash_hevc_specific_parameters_t
*)cs
->data
.structured
;
1277 lsmash_video_summary_t
*summary
= hevc_create_summary( hvcC_param
, &info
->sps
, hevc_imp
->max_au_length
);
1279 return LSMASH_ERR_NAMELESS
;
1280 lsmash_remove_entry( importer
->summaries
, track_number
, lsmash_cleanup_summary
);
1281 if( lsmash_add_entry( importer
->summaries
, summary
) < 0 )
1283 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
1284 return LSMASH_ERR_MEMORY_ALLOC
;
1286 importer
->status
= IMPORTER_OK
;
1288 lsmash_sample_t
*sample
= lsmash_create_sample( hevc_imp
->max_au_length
);
1290 return LSMASH_ERR_MEMORY_ALLOC
;
1292 hevc_access_unit_t
*au
= &info
->au
;
1293 hevc_picture_info_t
*picture
= &au
->picture
;
1294 sample
->dts
= hevc_imp
->ts_list
.timestamp
[ au
->number
- 1 ].dts
;
1295 sample
->cts
= hevc_imp
->ts_list
.timestamp
[ au
->number
- 1 ].cts
;
1296 /* Set property of disposability. */
1297 if( picture
->sublayer_nonref
&& au
->TemporalId
== hevc_imp
->max_TemporalId
)
1298 /* Sub-layer non-reference pictures are not referenced by subsequent pictures of
1299 * the same sub-layer in decoding order. */
1300 sample
->prop
.disposable
= ISOM_SAMPLE_IS_DISPOSABLE
;
1302 sample
->prop
.disposable
= ISOM_SAMPLE_IS_NOT_DISPOSABLE
;
1303 /* Set property of leading. */
1304 if( picture
->radl
|| picture
->rasl
)
1305 sample
->prop
.leading
= picture
->radl
? ISOM_SAMPLE_IS_DECODABLE_LEADING
: ISOM_SAMPLE_IS_UNDECODABLE_LEADING
;
1308 if( au
->number
< hevc_imp
->num_undecodable
)
1309 sample
->prop
.leading
= ISOM_SAMPLE_IS_UNDECODABLE_LEADING
;
1312 if( picture
->independent
|| sample
->cts
>= hevc_imp
->last_intra_cts
)
1313 sample
->prop
.leading
= ISOM_SAMPLE_IS_NOT_LEADING
;
1315 sample
->prop
.leading
= ISOM_SAMPLE_IS_UNDECODABLE_LEADING
;
1318 if( picture
->independent
)
1319 hevc_imp
->last_intra_cts
= sample
->cts
;
1320 /* Set property of independence. */
1321 sample
->prop
.independent
= picture
->independent
? ISOM_SAMPLE_IS_INDEPENDENT
: ISOM_SAMPLE_IS_NOT_INDEPENDENT
;
1322 sample
->prop
.redundant
= ISOM_SAMPLE_HAS_NO_REDUNDANCY
;
1323 sample
->prop
.post_roll
.identifier
= picture
->poc
;
1324 if( picture
->random_accessible
)
1328 sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
1329 if( picture
->closed_rap
)
1330 sample
->prop
.ra_flags
|= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_CLOSED_RAP
;
1332 sample
->prop
.ra_flags
|= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP
;
1334 else if( picture
->recovery_poc_cnt
)
1336 sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_POST_ROLL_START
;
1337 sample
->prop
.post_roll
.complete
= picture
->poc
+ picture
->recovery_poc_cnt
;
1340 sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP
;
1342 sample
->length
= au
->length
;
1343 memcpy( sample
->data
, au
->data
, au
->length
);
1344 return current_status
;
1347 static lsmash_video_summary_t
*hevc_setup_first_summary
1349 importer_t
*importer
1352 hevc_importer_t
*hevc_imp
= (hevc_importer_t
*)importer
->info
;
1353 lsmash_codec_specific_t
*cs
= (lsmash_codec_specific_t
*)lsmash_get_entry_data( hevc_imp
->hvcC_list
, ++ hevc_imp
->hvcC_number
);
1354 if( !cs
|| !cs
->data
.structured
)
1356 lsmash_destroy_codec_specific_data( cs
);
1359 lsmash_video_summary_t
*summary
= hevc_create_summary( (lsmash_hevc_specific_parameters_t
*)cs
->data
.structured
,
1360 &hevc_imp
->info
.sps
, hevc_imp
->max_au_length
);
1363 lsmash_destroy_codec_specific_data( cs
);
1366 if( lsmash_add_entry( importer
->summaries
, summary
) < 0 )
1368 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
1371 summary
->sample_per_field
= hevc_imp
->field_pic_present
;
1375 static int hevc_analyze_whole_stream
1377 importer_t
*importer
1380 /* Parse all NALU in the stream for preparation of calculating timestamps. */
1381 uint32_t npt_alloc
= (1 << 12) * sizeof(nal_pic_timing_t
);
1382 nal_pic_timing_t
*npt
= (nal_pic_timing_t
*)lsmash_malloc( npt_alloc
);
1384 return LSMASH_ERR_MEMORY_ALLOC
;
1385 uint32_t picture_stats
[HEVC_PICTURE_TYPE_NONE
+ 1] = { 0 };
1386 uint32_t num_access_units
= 0;
1387 lsmash_class_t
*logger
= &(lsmash_class_t
){ "HEVC" };
1388 lsmash_log( &logger
, LSMASH_LOG_INFO
, "Analyzing stream as HEVC\r" );
1389 hevc_importer_t
*hevc_imp
= (hevc_importer_t
*)importer
->info
;
1390 hevc_info_t
*info
= &hevc_imp
->info
;
1391 importer
->status
= IMPORTER_OK
;
1392 int err
= LSMASH_ERR_MEMORY_ALLOC
;
1393 while( importer
->status
!= IMPORTER_EOF
)
1396 lsmash_log( &logger
, LSMASH_LOG_INFO
, "Analyzing stream as HEVC: %"PRIu32
"\n", num_access_units
+ 1 );
1398 hevc_picture_info_t
*picture
= &info
->au
.picture
;
1399 hevc_picture_info_t prev_picture
= *picture
;
1400 if( (err
= hevc_get_access_unit_internal( importer
, 1 )) < 0
1401 || (err
= hevc_calculate_poc( info
, &info
->au
.picture
, &prev_picture
)) < 0 )
1403 hevc_importer_check_eof( importer
, &info
->au
);
1404 if( npt_alloc
<= num_access_units
* sizeof(nal_pic_timing_t
) )
1406 uint32_t alloc
= 2 * num_access_units
* sizeof(nal_pic_timing_t
);
1407 nal_pic_timing_t
*temp
= (nal_pic_timing_t
*)lsmash_realloc( npt
, alloc
);
1413 hevc_imp
->field_pic_present
|= picture
->field_coded
;
1414 npt
[num_access_units
].poc
= picture
->poc
;
1415 npt
[num_access_units
].delta
= picture
->delta
;
1416 npt
[num_access_units
].poc_delta
= 1;
1417 npt
[num_access_units
].reset
= 0;
1419 hevc_imp
->max_au_length
= LSMASH_MAX( hevc_imp
->max_au_length
, info
->au
.length
);
1420 hevc_imp
->max_TemporalId
= LSMASH_MAX( hevc_imp
->max_TemporalId
, info
->au
.TemporalId
);
1422 ++picture_stats
[HEVC_PICTURE_TYPE_IDR
];
1423 else if( picture
->irap
)
1424 ++picture_stats
[ picture
->broken_link
? HEVC_PICTURE_TYPE_BLA
: HEVC_PICTURE_TYPE_CRA
];
1425 else if( picture
->type
>= HEVC_PICTURE_TYPE_NONE
)
1426 ++picture_stats
[HEVC_PICTURE_TYPE_NONE
];
1428 ++picture_stats
[ picture
->type
];
1430 lsmash_log_refresh_line( &logger
);
1431 lsmash_log( &logger
, LSMASH_LOG_INFO
,
1432 "IDR: %"PRIu32
", CRA: %"PRIu32
", BLA: %"PRIu32
", I: %"PRIu32
", P: %"PRIu32
", B: %"PRIu32
", Unknown: %"PRIu32
"\n",
1433 picture_stats
[HEVC_PICTURE_TYPE_IDR
], picture_stats
[HEVC_PICTURE_TYPE_CRA
],
1434 picture_stats
[HEVC_PICTURE_TYPE_BLA
], picture_stats
[HEVC_PICTURE_TYPE_I
],
1435 picture_stats
[HEVC_PICTURE_TYPE_I_P
], picture_stats
[HEVC_PICTURE_TYPE_I_P_B
],
1436 picture_stats
[HEVC_PICTURE_TYPE_NONE
]);
1437 /* Copy and append the last Codec Specific info. */
1438 if( (err
= hevc_store_codec_specific( hevc_imp
, &info
->hvcC_param
)) < 0 )
1440 /* Set up the first summary. */
1441 lsmash_video_summary_t
*summary
= hevc_setup_first_summary( importer
);
1444 err
= LSMASH_ERR_NAMELESS
;
1448 lsmash_media_ts_t
*timestamp
= lsmash_malloc( num_access_units
* sizeof(lsmash_media_ts_t
) );
1451 /* Count leading samples that are undecodable. */
1452 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
1454 if( npt
[i
].poc
== 0 )
1456 ++ hevc_imp
->num_undecodable
;
1458 /* Deduplicate POCs. */
1459 uint32_t max_composition_delay
= 0;
1460 nalu_deduplicate_poc( npt
, &max_composition_delay
, num_access_units
, 15 );
1461 /* Generate timestamps. */
1462 nalu_generate_timestamps_from_poc( importer
, timestamp
, npt
,
1463 &hevc_imp
->composition_reordering_present
,
1464 &hevc_imp
->last_delta
,
1465 max_composition_delay
, num_access_units
);
1466 summary
->timescale
*= 2; /* We assume that picture timing is in field level.
1467 * For HEVC, it seems time_scale is set in frame level basically.
1468 * So multiply by 2 for reducing timebase and timescale. */
1469 nalu_reduce_timescale( timestamp
, npt
, &hevc_imp
->last_delta
, &summary
->timescale
, num_access_units
);
1471 hevc_imp
->ts_list
.sample_count
= num_access_units
;
1472 hevc_imp
->ts_list
.timestamp
= timestamp
;
1475 lsmash_log_refresh_line( &logger
);
1480 static int hevc_importer_probe( importer_t
*importer
)
1482 /* Find the first start code. */
1483 hevc_importer_t
*hevc_imp
= create_hevc_importer( importer
);
1485 return LSMASH_ERR_MEMORY_ALLOC
;
1486 lsmash_bs_t
*bs
= importer
->bs
;
1487 uint64_t first_sc_head_pos
= nalu_find_first_start_code( bs
);
1489 if( first_sc_head_pos
== NALU_NO_START_CODE_FOUND
)
1491 err
= LSMASH_ERR_INVALID_DATA
;
1494 /* OK. It seems the stream has a long start code of HEVC. */
1495 importer
->info
= hevc_imp
;
1496 hevc_info_t
*info
= &hevc_imp
->info
;
1497 lsmash_bs_read_seek( bs
, first_sc_head_pos
, SEEK_SET
);
1498 hevc_imp
->sc_head_pos
= first_sc_head_pos
;
1499 if( (err
= hevc_analyze_whole_stream( importer
)) < 0 )
1501 /* Go back to the start code of the first NALU. */
1502 importer
->status
= IMPORTER_OK
;
1503 lsmash_bs_read_seek( bs
, first_sc_head_pos
, SEEK_SET
);
1504 hevc_imp
->sc_head_pos
= first_sc_head_pos
;
1505 info
->prev_nalu_type
= HEVC_NALU_TYPE_UNKNOWN
;
1506 uint8_t *temp_au
= info
->au
.data
;
1507 uint8_t *temp_incomplete_au
= info
->au
.incomplete_data
;
1508 memset( &info
->au
, 0, sizeof(hevc_access_unit_t
) );
1509 info
->au
.data
= temp_au
;
1510 info
->au
.incomplete_data
= temp_incomplete_au
;
1511 memset( &info
->slice
, 0, sizeof(hevc_slice_info_t
) );
1512 memset( &info
->vps
, 0, sizeof(hevc_vps_t
) );
1513 memset( &info
->sps
, 0, sizeof(hevc_sps_t
) );
1514 memset( &info
->pps
, 0, SIZEOF_PPS_EXCLUDING_HEAP
);
1515 for( int i
= 0; i
< HEVC_DCR_NALU_TYPE_NUM
; i
++ )
1516 lsmash_remove_entries( info
->hvcC_param
.parameter_arrays
->ps_array
[i
].list
, isom_remove_dcr_ps
);
1517 lsmash_destroy_hevc_parameter_arrays( &info
->hvcC_param_next
);
1520 remove_hevc_importer( hevc_imp
);
1521 importer
->info
= NULL
;
1522 lsmash_remove_entries( importer
->summaries
, lsmash_cleanup_summary
);
1526 static uint32_t hevc_importer_get_last_delta( importer_t
*importer
, uint32_t track_number
)
1528 debug_if( !importer
|| !importer
->info
)
1530 hevc_importer_t
*hevc_imp
= (hevc_importer_t
*)importer
->info
;
1531 if( !hevc_imp
|| track_number
!= 1 || importer
->status
!= IMPORTER_EOF
)
1533 return hevc_imp
->ts_list
.sample_count
1534 ? hevc_imp
->last_delta
1535 : UINT32_MAX
; /* arbitrary */
1538 const importer_functions hevc_importer
=
1540 { "HEVC", offsetof( importer_t
, log_level
) },
1542 hevc_importer_probe
,
1543 hevc_importer_get_accessunit
,
1544 hevc_importer_get_last_delta
,
1545 hevc_importer_cleanup