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