1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2010-2014 L-SMASH project
6 * Authors: Takashi Hirata <silverfilain@gmail.com>
7 * Contributors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
9 * Permission to use, copy, modify, and/or distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 *****************************************************************************/
22 /* This file is available under an ISC license. */
24 #include "common/internal.h" /* must be placed first */
31 #define LSMASH_IMPORTER_INTERNAL
36 #include "codecs/mp4a.h"
37 #include "codecs/description.h"
39 /***************************************************************************
41 ***************************************************************************/
44 typedef void ( *importer_cleanup
) ( struct importer_tag
* );
45 typedef int ( *importer_get_accessunit
) ( struct importer_tag
*, uint32_t, lsmash_sample_t
* );
46 typedef int ( *importer_probe
) ( struct importer_tag
* );
47 typedef uint32_t ( *importer_get_last_duration
)( struct importer_tag
*, uint32_t );
54 importer_get_accessunit get_accessunit
;
55 importer_get_last_duration get_last_delta
;
56 importer_cleanup cleanup
;
59 typedef struct importer_tag
61 const lsmash_class_t
*class;
62 lsmash_log_level log_level
;
63 lsmash_stream_buffers_t sb
;
64 FILE *stream
; /* will be deprecated */
66 void *info
; /* importer internal status information. */
67 importer_functions funcs
;
68 lsmash_entry_list_t
*summaries
;
79 static const lsmash_class_t lsmash_importer_class
=
82 offsetof( importer_t
, log_level
)
85 /***************************************************************************
87 ***************************************************************************/
88 #define MP4SYS_ADTS_FIXED_HEADER_LENGTH 4 /* this is partly a lie. actually 28 bits. */
89 #define MP4SYS_ADTS_BASIC_HEADER_LENGTH 7
90 #define MP4SYS_ADTS_MAX_FRAME_LENGTH ( ( 1 << 13 ) - 1 )
91 #define MP4SYS_ADTS_MAX_RAW_DATA_BLOCKS 4
95 uint16_t syncword
; /* 12; */
97 uint8_t layer
; /* 2; */
98 uint8_t protection_absent
; /* 1; */
99 uint8_t profile_ObjectType
; /* 2; */
100 uint8_t sampling_frequency_index
; /* 4; */
101 // uint8_t private_bit; /* 1; we don't care. */
102 uint8_t channel_configuration
; /* 3; */
103 // uint8_t original_copy; /* 1; we don't care. */
104 // uint8_t home; /* 1; we don't care. */
106 } mp4sys_adts_fixed_header_t
;
110 // uint8_t copyright_identification_bit; /* 1; we don't care. */
111 // uint8_t copyright_identification_start; /* 1; we don't care. */
112 uint16_t frame_length
; /* 13; */
113 // uint16_t adts_buffer_fullness; /* 11; we don't care. */
114 uint8_t number_of_raw_data_blocks_in_frame
; /* 2; */
115 // uint16_t adts_error_check; /* we don't support */
116 // uint16_t raw_data_block_position[MP4SYS_ADTS_MAX_RAW_DATA_BLOCKS-1]; /* we don't use this directly, and... */
117 uint16_t raw_data_block_size
[MP4SYS_ADTS_MAX_RAW_DATA_BLOCKS
]; /* use this instead of above. */
118 // uint16_t adts_header_error_check; /* we don't support, actually crc_check within this */
119 // uint16_t adts_raw_data_block_error_check[MP4SYS_ADTS_MAX_RAW_DATA_BLOCKS]; /* we don't support */
120 } mp4sys_adts_variable_header_t
;
122 static void mp4sys_adts_parse_fixed_header( uint8_t* buf
, mp4sys_adts_fixed_header_t
* header
)
124 /* FIXME: should we rewrite these code using bitstream reader? */
125 header
->syncword
= (buf
[0] << 4) | (buf
[1] >> 4);
126 header
->ID
= (buf
[1] >> 3) & 0x1;
127 header
->layer
= (buf
[1] >> 1) & 0x3;
128 header
->protection_absent
= buf
[1] & 0x1;
129 header
->profile_ObjectType
= buf
[2] >> 6;
130 header
->sampling_frequency_index
= (buf
[2] >> 2) & 0xF;
131 // header->private_bit = (buf[2] >> 1) & 0x1; /* we don't care currently. */
132 header
->channel_configuration
= ((buf
[2] << 2) | (buf
[3] >> 6)) & 0x07;
133 // header->original_copy = (buf[3] >> 5) & 0x1; /* we don't care currently. */
134 // header->home = (buf[3] >> 4) & 0x1; /* we don't care currently. */
137 static int mp4sys_adts_check_fixed_header( mp4sys_adts_fixed_header_t
* header
)
139 if( header
->syncword
!= 0xFFF ) return -1;
140 // if( header->ID != 0x0 ) return -1; /* we don't care. */
141 if( header
->layer
!= 0x0 ) return -1; /* must be 0b00 for any type of AAC */
142 // if( header->protection_absent != 0x1 ) return -1; /* we don't care. */
143 if( header
->profile_ObjectType
!= 0x1 ) return -1; /* FIXME: 0b00=Main, 0b01=LC, 0b10=SSR, 0b11=LTP. */
144 if( header
->sampling_frequency_index
> 0xB ) return -1; /* must not be > 0xB. */
145 if( header
->channel_configuration
== 0x0 ) return -1; /* FIXME: we do not support 0b000 currently. */
146 if( header
->profile_ObjectType
== 0x3 && header
->ID
!= 0x0 ) return -1; /* LTP is valid only if ID==0. */
150 static int mp4sys_adts_parse_variable_header( FILE* stream
, uint8_t* buf
, unsigned int protection_absent
, mp4sys_adts_variable_header_t
* header
)
152 /* FIXME: should we rewrite these code using bitstream reader? */
153 // header->copyright_identification_bit = (buf[3] >> 3) & 0x1; /* we don't care. */
154 // header->copyright_identification_start = (buf[3] >> 2) & 0x1; /* we don't care. */
155 header
->frame_length
= ((buf
[3] << 11) | (buf
[4] << 3) | (buf
[5] >> 5)) & 0x1FFF ;
156 // header->adts_buffer_fullness = ((buf[5] << 6) | (buf[6] >> 2)) 0x7FF ; /* we don't care. */
157 header
->number_of_raw_data_blocks_in_frame
= buf
[6] & 0x3;
159 if( header
->frame_length
<= MP4SYS_ADTS_BASIC_HEADER_LENGTH
+ 2 * (protection_absent
== 0) )
160 return -1; /* easy error check */
162 /* protection_absent and number_of_raw_data_blocks_in_frame relatives */
165 unsigned int number_of_blocks
= header
->number_of_raw_data_blocks_in_frame
;
166 if( number_of_blocks
== 0 )
168 header
->raw_data_block_size
[0] = header
->frame_length
- MP4SYS_ADTS_BASIC_HEADER_LENGTH
;
169 /* skip adts_error_check() and subtract that from block_size */
170 if( protection_absent
== 0 )
172 header
->raw_data_block_size
[0] -= 2;
173 if( fread( buf2
, 1, 2, stream
) != 2 )
179 /* now we have multiple raw_data_block()s, so evaluate adts_header_error_check() */
181 uint16_t raw_data_block_position
[MP4SYS_ADTS_MAX_RAW_DATA_BLOCKS
];
182 uint16_t first_offset
= MP4SYS_ADTS_BASIC_HEADER_LENGTH
;
183 if( protection_absent
== 0 )
185 /* process adts_header_error_check() */
186 for( int i
= 0 ; i
< number_of_blocks
; i
++ ) /* 1-based in the spec, but we use 0-based */
188 if( fread( buf2
, 1, 2, stream
) != 2 )
190 raw_data_block_position
[i
] = LSMASH_GET_BE16( buf2
);
192 /* skip crc_check in adts_header_error_check().
193 Or might be sizeof( adts_error_check() ) if we share with the case number_of_raw_data_blocks_in_frame == 0 */
194 if( fread( buf2
, 1, 2, stream
) != 2 )
196 first_offset
+= ( 2 * number_of_blocks
) + 2; /* according to above */
201 * NOTE: We never support the case where number_of_raw_data_blocks_in_frame != 0 && protection_absent != 0,
202 * because we have to parse the raw AAC bitstream itself to find boundaries of raw_data_block()s in this case.
203 * Which is to say, that braindamaged spec requires us (mp4 muxer) to decode AAC once to split frames.
204 * L-SMASH is NOT AAC DECODER, so that we've just given up for this case.
205 * This is ISO/IEC 13818-7's sin which defines ADTS format originally.
210 /* convert raw_data_block_position --> raw_data_block_size */
212 /* do conversion for first */
213 header
->raw_data_block_size
[0] = raw_data_block_position
[0] - first_offset
;
214 /* set dummy offset to tail for loop, do coversion for rest. */
215 raw_data_block_position
[number_of_blocks
] = header
->frame_length
;
216 for( int i
= 1 ; i
<= number_of_blocks
; i
++ )
217 header
->raw_data_block_size
[i
] = raw_data_block_position
[i
] - raw_data_block_position
[i
-1];
219 /* adjustment for adts_raw_data_block_error_check() */
220 if( protection_absent
== 0 && number_of_blocks
!= 0 )
221 for( int i
= 0 ; i
<= number_of_blocks
; i
++ )
222 header
->raw_data_block_size
[i
] -= 2;
227 static int mp4sys_adts_parse_headers( FILE* stream
, uint8_t* buf
, mp4sys_adts_fixed_header_t
* header
, mp4sys_adts_variable_header_t
* variable_header
)
229 mp4sys_adts_parse_fixed_header( buf
, header
);
230 if( mp4sys_adts_check_fixed_header( header
) )
232 /* get payload length & skip extra(crc) header */
233 return mp4sys_adts_parse_variable_header( stream
, buf
, header
->protection_absent
, variable_header
);
236 static lsmash_audio_summary_t
*mp4sys_adts_create_summary( mp4sys_adts_fixed_header_t
*header
)
238 lsmash_audio_summary_t
*summary
= (lsmash_audio_summary_t
*)lsmash_create_summary( LSMASH_SUMMARY_TYPE_AUDIO
);
241 summary
->sample_type
= ISOM_CODEC_TYPE_MP4A_AUDIO
;
242 summary
->max_au_length
= MP4SYS_ADTS_MAX_FRAME_LENGTH
;
243 summary
->frequency
= mp4a_sampling_frequency_table
[header
->sampling_frequency_index
][1];
244 summary
->channels
= header
->channel_configuration
+ ( header
->channel_configuration
== 0x07 ); /* 0x07 means 7.1ch */
245 summary
->sample_size
= 16;
246 summary
->samples_in_frame
= 1024;
247 summary
->aot
= header
->profile_ObjectType
+ MP4A_AUDIO_OBJECT_TYPE_AAC_MAIN
;
248 summary
->sbr_mode
= MP4A_AAC_SBR_NOT_SPECIFIED
;
249 #if 0 /* FIXME: This is very unstable. Many players crash with this. */
250 if( header
->ID
!= 0 )
253 * NOTE: This ADTS seems of ISO/IEC 13818-7 (MPEG-2 AAC).
254 * It has special object_type_indications, depending on it's profile (Legacy Interface).
255 * If ADIF header is not available, it should not have decoder specific information, so AudioObjectType neither.
256 * see ISO/IEC 14496-1, DecoderSpecificInfo and 14496-3 Subpart 9: MPEG-1/2 Audio in MPEG-4.
258 summary
->object_type_indication
= header
->profile_ObjectType
+ MP4SYS_OBJECT_TYPE_Audio_ISO_13818_7_Main_Profile
;
259 summary
->aot
= MP4A_AUDIO_OBJECT_TYPE_NULL
;
261 summary
->asc_length
= 0;
262 // summary->sbr_mode = MP4A_AAC_SBR_NONE; /* MPEG-2 AAC should not be HE-AAC, but we forgive them. */
266 uint32_t data_length
;
267 uint8_t *data
= mp4a_export_AudioSpecificConfig( header
->profile_ObjectType
+ MP4A_AUDIO_OBJECT_TYPE_AAC_MAIN
,
268 summary
->frequency
, summary
->channels
, summary
->sbr_mode
,
269 NULL
, 0, &data_length
);
272 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
275 lsmash_codec_specific_t
*specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG
,
276 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
279 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
283 lsmash_mp4sys_decoder_parameters_t
*param
= (lsmash_mp4sys_decoder_parameters_t
*)specific
->data
.structured
;
284 param
->objectTypeIndication
= MP4SYS_OBJECT_TYPE_Audio_ISO_14496_3
;
285 param
->streamType
= MP4SYS_STREAM_TYPE_AudioStream
;
286 if( lsmash_set_mp4sys_decoder_specific_info( param
, data
, data_length
) )
288 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
289 lsmash_destroy_codec_specific_data( specific
);
294 if( lsmash_add_entry( &summary
->opaque
->list
, specific
) )
296 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
297 lsmash_destroy_codec_specific_data( specific
);
305 importer_status status
;
306 unsigned int raw_data_block_idx
;
307 mp4sys_adts_fixed_header_t header
;
308 mp4sys_adts_variable_header_t variable_header
;
309 uint32_t samples_in_frame
;
311 } mp4sys_adts_info_t
;
313 static int mp4sys_adts_get_accessunit( importer_t
*importer
, uint32_t track_number
, lsmash_sample_t
*buffered_sample
)
315 debug_if( !importer
|| !importer
->info
|| !buffered_sample
->data
|| !buffered_sample
->length
)
317 if( !importer
->info
|| track_number
!= 1 )
319 mp4sys_adts_info_t
* info
= (mp4sys_adts_info_t
*)importer
->info
;
320 importer_status current_status
= info
->status
;
321 uint16_t raw_data_block_size
= info
->variable_header
.raw_data_block_size
[info
->raw_data_block_idx
];
322 if( current_status
== IMPORTER_ERROR
|| buffered_sample
->length
< raw_data_block_size
)
324 if( current_status
== IMPORTER_EOF
)
326 buffered_sample
->length
= 0;
329 if( current_status
== IMPORTER_CHANGE
)
331 lsmash_audio_summary_t
* summary
= mp4sys_adts_create_summary( &info
->header
);
334 lsmash_entry_t
* entry
= lsmash_get_entry( importer
->summaries
, track_number
);
335 if( !entry
|| !entry
->data
)
337 lsmash_cleanup_summary( entry
->data
);
338 entry
->data
= summary
;
339 info
->samples_in_frame
= summary
->samples_in_frame
;
342 /* read a raw_data_block(), typically == payload of a ADTS frame */
343 if( fread( buffered_sample
->data
, 1, raw_data_block_size
, importer
->stream
) != raw_data_block_size
)
345 info
->status
= IMPORTER_ERROR
;
348 buffered_sample
->length
= raw_data_block_size
;
349 buffered_sample
->dts
= info
->au_number
++ * info
->samples_in_frame
;
350 buffered_sample
->cts
= buffered_sample
->dts
;
351 buffered_sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
352 buffered_sample
->prop
.pre_roll
.distance
= 1; /* MDCT */
354 /* now we succeeded to read current frame, so "return" takes 0 always below. */
356 /* skip adts_raw_data_block_error_check() */
357 if( info
->header
.protection_absent
== 0
358 && info
->variable_header
.number_of_raw_data_blocks_in_frame
!= 0
359 && fread( buffered_sample
->data
, 1, 2, importer
->stream
) != 2 )
361 info
->status
= IMPORTER_ERROR
;
364 /* current adts_frame() has any more raw_data_block()? */
365 if( info
->raw_data_block_idx
< info
->variable_header
.number_of_raw_data_blocks_in_frame
)
367 info
->raw_data_block_idx
++;
368 info
->status
= IMPORTER_OK
;
371 info
->raw_data_block_idx
= 0;
373 /* preparation for next frame */
375 uint8_t buf
[MP4SYS_ADTS_MAX_FRAME_LENGTH
];
376 size_t ret
= fread( buf
, 1, MP4SYS_ADTS_BASIC_HEADER_LENGTH
, importer
->stream
);
379 info
->status
= IMPORTER_EOF
;
382 if( ret
!= MP4SYS_ADTS_BASIC_HEADER_LENGTH
)
384 info
->status
= IMPORTER_ERROR
;
388 * NOTE: About the spec of ADTS headers.
389 * By the spec definition, ADTS's fixed header cannot change in the middle of stream.
390 * But spec of MP4 allows that a stream(track) changes its properties in the middle of it.
393 * NOTE: About detailed check for ADTS headers.
394 * We do not ommit detailed check for fixed header by simply testing bits' identification,
395 * because there're some flags which does not matter to audio_summary (so AudioSpecificConfig neither)
396 * so that we can take them as no change and never make new ObjectDescriptor.
397 * I know that can be done with/by bitmask also and that should be fast, but L-SMASH project prefers
398 * even foolishly straightforward way.
401 * NOTE: About our reading algorithm for ADTS.
402 * It's rather simple if we retrieve payload of ADTS (i.e. raw AAC frame) at the same time to
404 * But then we have to cache and memcpy every frame so that it requires more clocks and memory.
405 * To avoid them, I adopted this separate retrieving method.
407 mp4sys_adts_fixed_header_t header
= {0};
408 mp4sys_adts_variable_header_t variable_header
= {0};
409 if( mp4sys_adts_parse_headers( importer
->stream
, buf
, &header
, &variable_header
) )
411 info
->status
= IMPORTER_ERROR
;
414 info
->variable_header
= variable_header
;
417 * NOTE: About our support for change(s) of properties within an ADTS stream.
418 * We have to modify these conditions depending on the features we support.
419 * For example, if we support copyright_identification_* in any way within any feature
420 * defined by/in any specs, such as ISO/IEC 14496-1 (MPEG-4 Systems), like...
421 * "8.3 Intellectual Property Management and Protection (IPMP)", or something similar,
422 * we have to check copyright_identification_* and treat them in audio_summary.
423 * "Change(s)" may result in MP4SYS_IMPORTER_ERROR or MP4SYS_IMPORTER_CHANGE
424 * depending on the features we support, and what the spec allows.
425 * Sometimes the "change(s)" can be allowed, while sometimes they're forbidden.
427 /* currently UNsupported "change(s)". */
428 if( info
->header
.profile_ObjectType
!= header
.profile_ObjectType
/* currently unsupported. */
429 || info
->header
.ID
!= header
.ID
/* In strict, this means change of object_type_indication. */
430 || info
->header
.sampling_frequency_index
!= header
.sampling_frequency_index
) /* This may change timebase. */
432 info
->status
= IMPORTER_ERROR
;
435 /* currently supported "change(s)". */
436 if( info
->header
.channel_configuration
!= header
.channel_configuration
)
439 * FIXME: About conditions of VALID "change(s)".
440 * we have to check whether any "change(s)" affect to audioProfileLevelIndication
441 * in InitialObjectDescriptor (MP4_IOD) or not.
442 * If another type or upper level is required by the change(s), that is forbidden.
443 * Because ObjectDescriptor does not have audioProfileLevelIndication,
444 * so that it seems impossible to change audioProfileLevelIndication in the middle of the stream.
445 * Note also any other properties, such as AudioObjectType, object_type_indication.
448 * NOTE: updating summary must be done on next call,
449 * because user may retrieve summary right after this function call of this time,
450 * and that should be of current, before change, one.
452 info
->header
= header
;
453 info
->status
= IMPORTER_CHANGE
;
456 /* no change which matters to mp4 muxing was found */
457 info
->status
= IMPORTER_OK
;
461 static void mp4sys_adts_cleanup( importer_t
*importer
)
463 debug_if( importer
&& importer
->info
)
464 lsmash_free( importer
->info
);
467 /* returns 0 if it seems adts. */
468 static int mp4sys_adts_probe( importer_t
*importer
)
470 uint8_t buf
[MP4SYS_ADTS_MAX_FRAME_LENGTH
];
471 if( fread( buf
, 1, MP4SYS_ADTS_BASIC_HEADER_LENGTH
, importer
->stream
) != MP4SYS_ADTS_BASIC_HEADER_LENGTH
)
474 mp4sys_adts_fixed_header_t header
= {0};
475 mp4sys_adts_variable_header_t variable_header
= {0};
476 if( mp4sys_adts_parse_headers( importer
->stream
, buf
, &header
, &variable_header
) )
479 /* now the stream seems valid ADTS */
481 lsmash_audio_summary_t
* summary
= mp4sys_adts_create_summary( &header
);
485 /* importer status */
486 mp4sys_adts_info_t
* info
= lsmash_malloc_zero( sizeof(mp4sys_adts_info_t
) );
489 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
492 info
->status
= IMPORTER_OK
;
493 info
->raw_data_block_idx
= 0;
494 info
->header
= header
;
495 info
->variable_header
= variable_header
;
496 info
->samples_in_frame
= summary
->samples_in_frame
;
498 if( lsmash_add_entry( importer
->summaries
, summary
) )
501 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
504 importer
->info
= info
;
509 static uint32_t mp4sys_adts_get_last_delta( importer_t
*importer
, uint32_t track_number
)
511 debug_if( !importer
|| !importer
->info
)
513 mp4sys_adts_info_t
*info
= (mp4sys_adts_info_t
*)importer
->info
;
514 if( !info
|| track_number
!= 1 || info
->status
!= IMPORTER_EOF
)
516 return info
->samples_in_frame
;
519 static const importer_functions mp4sys_adts_importer
=
524 mp4sys_adts_get_accessunit
,
525 mp4sys_adts_get_last_delta
,
529 /***************************************************************************
530 mp3 (Legacy Interface) importer
531 ***************************************************************************/
533 #define USE_MP4SYS_LEGACY_INTERFACE 1
535 static void mp4sys_mp3_cleanup( importer_t
*importer
)
537 debug_if( importer
&& importer
->info
)
538 lsmash_free( importer
->info
);
543 uint16_t syncword
; /* <12> */
544 uint8_t ID
; /* <1> */
545 uint8_t layer
; /* <2> */
546 uint8_t protection_bit
; /* <1> */
547 uint8_t bitrate_index
; /* <4> */
548 uint8_t sampling_frequency
; /* <2> */
549 uint8_t padding_bit
; /* <1> */
550 // uint8_t private_bit; /* <1> don't care. */
551 uint8_t mode
; /* <2> */
552 // uint8_t mode_extension; /* <2> don't care. */
553 // uint8_t copyright; /* <1> don't care. */
554 // uint8_t original_copy; /* <1> don't care. */
555 uint8_t emphasis
; /* <2> for error check only. */
557 } mp4sys_mp3_header_t
;
559 static int mp4sys_mp3_parse_header( uint8_t* buf
, mp4sys_mp3_header_t
* header
)
561 /* FIXME: should we rewrite these code using bitstream reader? */
562 uint32_t data
= LSMASH_GET_BE32( buf
);
563 header
->syncword
= (data
>> 20) & 0xFFF; /* NOTE: don't consider what is called MPEG2.5, which last bit is 0. */
564 header
->ID
= (data
>> 19) & 0x1;
565 header
->layer
= (data
>> 17) & 0x3;
566 header
->protection_bit
= (data
>> 16) & 0x1;
567 header
->bitrate_index
= (data
>> 12) & 0xF;
568 header
->sampling_frequency
= (data
>> 10) & 0x3;
569 header
->padding_bit
= (data
>> 9) & 0x1;
570 // header->private_bit = (data >> 8) & 0x1; /* don't care. */
571 header
->mode
= (data
>> 6) & 0x3;
572 // header->mode_extension = (data >> 4) & 0x3;
573 // header->copyright = (data >> 3) & 0x1; /* don't care. */
574 // header->original_copy = (data >> 2) & 0x1; /* don't care. */
575 header
->emphasis
= data
& 0x3; /* for error check only. */
577 if( header
->syncword
!= 0xFFF ) return -1;
578 if( header
->layer
== 0x0 ) return -1;
579 if( header
->bitrate_index
== 0x0 || header
->bitrate_index
== 0xF ) return -1; /* FIXME: "free" bitrate is unsupported currently. */
580 if( header
->sampling_frequency
== 0x3) return -1;
581 if( header
->emphasis
== 0x2) return -1;
585 #define MP4SYS_MP3_MAX_FRAME_LENGTH (1152*(16/8)*2)
586 #define MP4SYS_MP3_HEADER_LENGTH 4
587 #define MP4SYS_MODE_IS_2CH( mode ) ((mode)!=3)
588 #define MP4SYS_LAYER_III 0x1
589 #define MP4SYS_LAYER_II 0x2
590 #define MP4SYS_LAYER_I 0x3
592 static const uint32_t mp4sys_mp3_frequency_tbl
[2][3] = {
593 { 22050, 24000, 16000 }, /* MPEG-2 BC audio */
594 { 44100, 48000, 32000 } /* MPEG-1 audio */
597 static int mp4sys_mp3_samples_in_frame( mp4sys_mp3_header_t
*header
)
599 if( header
->layer
== MP4SYS_LAYER_I
)
601 else if( header
->ID
== 1 || header
->layer
== MP4SYS_LAYER_II
)
607 static lsmash_audio_summary_t
*mp4sys_mp3_create_summary( mp4sys_mp3_header_t
*header
, int legacy_mode
)
609 lsmash_audio_summary_t
*summary
= (lsmash_audio_summary_t
*)lsmash_create_summary( LSMASH_SUMMARY_TYPE_AUDIO
);
612 summary
->sample_type
= ISOM_CODEC_TYPE_MP4A_AUDIO
;
613 summary
->max_au_length
= MP4SYS_MP3_MAX_FRAME_LENGTH
;
614 summary
->frequency
= mp4sys_mp3_frequency_tbl
[header
->ID
][header
->sampling_frequency
];
615 summary
->channels
= MP4SYS_MODE_IS_2CH( header
->mode
) + 1;
616 summary
->sample_size
= 16;
617 summary
->samples_in_frame
= mp4sys_mp3_samples_in_frame( header
);
618 summary
->aot
= MP4A_AUDIO_OBJECT_TYPE_Layer_1
+ (MP4SYS_LAYER_I
- header
->layer
); /* no effect with Legacy Interface. */
619 summary
->sbr_mode
= MP4A_AAC_SBR_NOT_SPECIFIED
; /* no effect */
620 #if !USE_MP4SYS_LEGACY_INTERFACE /* FIXME: This is very unstable. Many players crash with this. */
623 summary
->object_type_indication
= MP4SYS_OBJECT_TYPE_Audio_ISO_14496_3
;
624 if( lsmash_setup_AudioSpecificConfig( summary
) )
626 lsmash_cleanup_summary( summary
);
630 uint32_t data_length
;
631 uint8_t *data
= mp4a_export_AudioSpecificConfig( MP4A_AUDIO_OBJECT_TYPE_Layer_1
+ (MP4SYS_LAYER_I
- header
->layer
),
632 summary
->frequency
, summary
->channels
, summary
->sbr_mode
,
633 NULL
, 0, &data_length
);
636 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
640 lsmash_codec_specific_t
*specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG
,
641 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
644 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
645 #if !USE_MP4SYS_LEGACY_INTERFACE
650 lsmash_mp4sys_decoder_parameters_t
*param
= (lsmash_mp4sys_decoder_parameters_t
*)specific
->data
.structured
;
651 param
->objectTypeIndication
= header
->ID
? MP4SYS_OBJECT_TYPE_Audio_ISO_11172_3
: MP4SYS_OBJECT_TYPE_Audio_ISO_13818_3
;
652 param
->streamType
= MP4SYS_STREAM_TYPE_AudioStream
;
653 #if !USE_MP4SYS_LEGACY_INTERFACE
654 if( lsmash_set_mp4sys_decoder_specific_info( param
, data
, data_length
) )
656 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
657 lsmash_destroy_codec_specific_data( specific
);
663 if( lsmash_add_entry( &summary
->opaque
->list
, specific
) )
665 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
666 lsmash_destroy_codec_specific_data( specific
);
674 importer_status status
;
675 mp4sys_mp3_header_t header
;
676 uint8_t raw_header
[MP4SYS_MP3_HEADER_LENGTH
];
677 uint32_t samples_in_frame
;
679 uint16_t main_data_size
[32]; /* size of main_data of the last 32 frames, FIFO */
680 uint16_t prev_preroll_count
; /* number of dependent frames of *previous* frame */
683 uint64_t valid_samples
;
686 static int parse_xing_info_header( mp4sys_mp3_info_t
*info
, mp4sys_mp3_header_t
*header
, uint8_t *frame
)
688 unsigned int sip
= header
->protection_bit
? 4 : 6;
689 unsigned int side_info_size
;
690 if( header
->ID
== 1 )
691 side_info_size
= MP4SYS_MODE_IS_2CH( header
->mode
) ? 32 : 17;
693 side_info_size
= MP4SYS_MODE_IS_2CH( header
->mode
) ? 17 : 9;
695 uint8_t *mdp
= frame
+ sip
+ side_info_size
;
696 if( memcmp( mdp
, "Info", 4 ) && memcmp( mdp
, "Xing", 4 ) )
698 uint32_t flags
= LSMASH_GET_BE32( &mdp
[4] );
700 uint32_t frame_count
= 0;
703 frame_count
= LSMASH_GET_BE32( &mdp
[8] );
704 info
->valid_samples
= (uint64_t)frame_count
* mp4sys_mp3_samples_in_frame( header
);
707 if( flags
& 2 ) off
+= 4; /* file size */
708 if( flags
& 4 ) off
+= 100; /* TOC */
709 if( flags
& 8 ) off
+= 4; /* VBR quality */
711 if( mdp
[off
] == 'L' )
712 { /* LAME header present */
713 unsigned v
= LSMASH_GET_BE24( &mdp
[off
+ 21] );
714 info
->enc_delay
= v
>> 12;
715 info
->padding
= v
& 0xfff;
717 info
->valid_samples
-= info
->enc_delay
+ info
->padding
;
722 static int parse_vbri_header( mp4sys_mp3_info_t
*info
, mp4sys_mp3_header_t
*header
, uint8_t *frame
)
724 return memcmp( frame
+ 36, "VBRI", 4 ) == 0;
727 static int mp4sys_mp3_get_accessunit( importer_t
*importer
, uint32_t track_number
, lsmash_sample_t
*buffered_sample
)
729 debug_if( !importer
|| !importer
->info
|| !buffered_sample
->data
|| !buffered_sample
->length
)
731 if( !importer
->info
|| track_number
!= 1 )
733 mp4sys_mp3_info_t
*info
= (mp4sys_mp3_info_t
*)importer
->info
;
734 mp4sys_mp3_header_t
*header
= (mp4sys_mp3_header_t
*)&info
->header
;
735 importer_status current_status
= info
->status
;
737 const uint32_t bitrate_tbl
[2][3][16] = {
738 { /* MPEG-2 BC audio */
739 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 }, /* Layer III */
740 { 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0 }, /* Layer II */
741 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0 } /* Layer I */
744 { 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0 }, /* Layer III */
745 { 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0 }, /* Layer II */
746 { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0 } /* Layer I */
749 uint32_t bitrate
= bitrate_tbl
[header
->ID
][header
->layer
-1][header
->bitrate_index
];
750 uint32_t frequency
= mp4sys_mp3_frequency_tbl
[header
->ID
][header
->sampling_frequency
];
751 debug_if( bitrate
== 0 || frequency
== 0 )
754 if( header
->layer
== MP4SYS_LAYER_I
)
756 /* mp1's 'slot' is 4 bytes unit. see 11172-3, Audio Sequence General. */
757 frame_size
= ( 12 * 1000 * bitrate
/ frequency
+ header
->padding_bit
) * 4;
761 /* mp2/3's 'slot' is 1 bytes unit. */
762 uint32_t div
= frequency
;
763 if( header
->layer
== MP4SYS_LAYER_III
&& header
->ID
== 0 )
765 frame_size
= 144 * 1000 * bitrate
/ div
+ header
->padding_bit
;
768 if( current_status
== IMPORTER_ERROR
|| frame_size
<= 4 || buffered_sample
->length
< frame_size
)
770 if( current_status
== IMPORTER_EOF
)
772 buffered_sample
->length
= 0;
775 if( current_status
== IMPORTER_CHANGE
)
777 lsmash_audio_summary_t
* summary
= mp4sys_mp3_create_summary( header
, 1 ); /* FIXME: use legacy mode. */
780 lsmash_entry_t
* entry
= lsmash_get_entry( importer
->summaries
, track_number
);
781 if( !entry
|| !entry
->data
)
783 lsmash_cleanup_summary( entry
->data
);
784 entry
->data
= summary
;
785 info
->samples_in_frame
= summary
->samples_in_frame
;
787 /* read a frame's data. */
788 memcpy( buffered_sample
->data
, info
->raw_header
, MP4SYS_MP3_HEADER_LENGTH
);
789 frame_size
-= MP4SYS_MP3_HEADER_LENGTH
;
790 if( fread( ((uint8_t*)buffered_sample
->data
)+MP4SYS_MP3_HEADER_LENGTH
, 1, frame_size
, importer
->stream
) != frame_size
)
792 info
->status
= IMPORTER_ERROR
;
795 buffered_sample
->length
= MP4SYS_MP3_HEADER_LENGTH
+ frame_size
;
796 buffered_sample
->dts
= info
->au_number
++ * info
->samples_in_frame
;
797 buffered_sample
->cts
= buffered_sample
->dts
;
798 buffered_sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
799 buffered_sample
->prop
.pre_roll
.distance
= header
->layer
== MP4SYS_LAYER_III
? 1 : 0; /* Layer III uses MDCT */
801 int vbr_header_present
= 0;
802 if( info
->au_number
== 1
803 && (parse_xing_info_header( info
, header
, buffered_sample
->data
)
804 || parse_vbri_header( info
, header
, buffered_sample
->data
)) )
806 vbr_header_present
= 1;
810 /* handle additional inter-frame dependency due to bit reservoir */
811 if( !vbr_header_present
&& header
->layer
== MP4SYS_LAYER_III
)
813 /* position of side_info */
814 unsigned int sip
= header
->protection_bit
? 4 : 6;
815 unsigned int main_data_begin
= buffered_sample
->data
[sip
];
816 if( header
->ID
== 1 )
818 main_data_begin
<<= 1;
819 main_data_begin
|= (buffered_sample
->data
[sip
+ 1] >> 7);
821 if( main_data_begin
> 0 )
823 /* main_data_begin is a backpointer to the start of
824 * bit reservoir data for this frame.
825 * it contains total amount of bytes required from
827 * we just add up main_data size from history until it reaches
828 * the required amount.
830 unsigned int reservoir_data
= 0;
832 for( i
= 0; i
< 32 && reservoir_data
< main_data_begin
; ++i
)
834 reservoir_data
+= info
->main_data_size
[i
];
835 if( info
->main_data_size
[i
] == 0 )
838 buffered_sample
->prop
.pre_roll
.distance
+= info
->prev_preroll_count
;
839 info
->prev_preroll_count
= i
;
841 uint16_t side_info_size
;
842 if( header
->ID
== 1 )
843 side_info_size
= MP4SYS_MODE_IS_2CH( header
->mode
) ? 32 : 17;
845 side_info_size
= MP4SYS_MODE_IS_2CH( header
->mode
) ? 17 : 9;
847 /* pop back main_data_size[] and push main_data size of this frame
849 memmove( info
->main_data_size
+ 1, info
->main_data_size
, sizeof(info
->main_data_size
) - sizeof( info
->main_data_size
[0] ) );
850 info
->main_data_size
[0] = frame_size
- sip
- side_info_size
;
852 /* now we succeeded to read current frame, so "return" takes 0 always below. */
853 /* preparation for next frame */
855 uint8_t buf
[MP4SYS_MP3_HEADER_LENGTH
];
856 size_t ret
= fread( buf
, 1, MP4SYS_MP3_HEADER_LENGTH
, importer
->stream
);
859 info
->status
= IMPORTER_EOF
;
862 if( ret
>= 2 && (!memcmp( buf
, "TA", 2 ) || !memcmp( buf
, "AP", 2 )) )
864 /* ID3v1 or APE tag */
865 info
->status
= IMPORTER_EOF
;
868 if( ret
== 1 && *buf
== 0x00 )
870 /* NOTE: ugly hack for mp1 stream created with SCMPX. */
871 info
->status
= IMPORTER_EOF
;
874 if( ret
!= MP4SYS_MP3_HEADER_LENGTH
)
876 info
->status
= IMPORTER_ERROR
;
880 mp4sys_mp3_header_t new_header
= {0};
881 if( mp4sys_mp3_parse_header( buf
, &new_header
) )
883 info
->status
= IMPORTER_ERROR
;
886 memcpy( info
->raw_header
, buf
, MP4SYS_MP3_HEADER_LENGTH
);
888 /* currently UNsupported "change(s)". */
889 if( header
->layer
!= new_header
.layer
/* This means change of object_type_indication with Legacy Interface. */
890 || header
->sampling_frequency
!= new_header
.sampling_frequency
) /* This may change timescale. */
892 info
->status
= IMPORTER_ERROR
;
896 /* currently supported "change(s)". */
897 if( MP4SYS_MODE_IS_2CH( header
->mode
) != MP4SYS_MODE_IS_2CH( new_header
.mode
) )
898 info
->status
= IMPORTER_CHANGE
;
900 info
->status
= IMPORTER_OK
; /* no change which matters to mp4 muxing was found */
901 info
->header
= new_header
;
903 if( vbr_header_present
)
904 return mp4sys_mp3_get_accessunit( importer
, track_number
, buffered_sample
);
908 static int mp4sys_mp3_probe( importer_t
*importer
)
911 if( (c
= getc( importer
->stream
)) == 'I'
912 && (c
= getc( importer
->stream
)) == 'D'
913 && (c
= getc( importer
->stream
)) == '3' )
915 lsmash_fseek( importer
->stream
, 3, SEEK_CUR
);
917 for( int i
= 0 ; i
< 4; i
++ )
920 size
|= getc( importer
->stream
);
922 lsmash_fseek( importer
->stream
, size
, SEEK_CUR
);
925 ungetc( c
, importer
->stream
);
927 uint8_t buf
[MP4SYS_MP3_HEADER_LENGTH
];
928 if( fread( buf
, 1, MP4SYS_MP3_HEADER_LENGTH
, importer
->stream
) != MP4SYS_MP3_HEADER_LENGTH
)
931 mp4sys_mp3_header_t header
= {0};
932 if( mp4sys_mp3_parse_header( buf
, &header
) )
935 /* now the stream seems valid mp3 */
937 lsmash_audio_summary_t
* summary
= mp4sys_mp3_create_summary( &header
, 1 ); /* FIXME: use legacy mode. */
941 /* importer status */
942 mp4sys_mp3_info_t
* info
= lsmash_malloc_zero( sizeof(mp4sys_mp3_info_t
) );
945 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
948 info
->status
= IMPORTER_OK
;
949 info
->header
= header
;
950 info
->samples_in_frame
= summary
->samples_in_frame
;
951 memcpy( info
->raw_header
, buf
, MP4SYS_MP3_HEADER_LENGTH
);
953 if( lsmash_add_entry( importer
->summaries
, summary
) )
956 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
959 importer
->info
= info
;
964 static uint32_t mp4sys_mp3_get_last_delta( importer_t
*importer
, uint32_t track_number
)
966 debug_if( !importer
|| !importer
->info
)
968 mp4sys_mp3_info_t
*info
= (mp4sys_mp3_info_t
*)importer
->info
;
969 if( !info
|| track_number
!= 1 || info
->status
!= IMPORTER_EOF
)
971 return info
->samples_in_frame
;
974 static const importer_functions mp4sys_mp3_importer
=
976 { "MPEG-1/2BC_Audio_Legacy" },
979 mp4sys_mp3_get_accessunit
,
980 mp4sys_mp3_get_last_delta
,
984 /***************************************************************************
985 AMR-NB/WB storage format importer
986 3GPP TS 26.101 V11.0.0 (2012-9)
987 3GPP TS 26.201 V11.0.0 (2012-9)
988 3GPP TS 26.244 V12.3.0 (2014-03)
989 http://www.ietf.org/rfc/rfc3267.txt (Obsoleted)
990 http://www.ietf.org/rfc/rfc4867.txt
991 ***************************************************************************/
994 importer_status status
;
996 int wb
; /* 0: AMR-NB, 1: AMR-WB */
997 uint32_t samples_in_frame
;
1001 static void remove_amr_importer
1003 amr_importer_t
*amr_imp
1006 lsmash_bs_cleanup( amr_imp
->bs
);
1007 lsmash_free( amr_imp
);
1010 static amr_importer_t
*create_amr_importer
1012 importer_t
*importer
1015 amr_importer_t
*amr_imp
= (amr_importer_t
*)lsmash_malloc_zero( sizeof(amr_importer_t
) );
1018 lsmash_bs_t
*bs
= lsmash_bs_create();
1021 lsmash_free( amr_imp
);
1025 bs
->stream
= importer
->stream
;
1026 bs
->read
= lsmash_fread_wrapper
;
1027 bs
->seek
= lsmash_fseek_wrapper
;
1028 bs
->unseekable
= importer
->is_stdin
;
1029 bs
->buffer
.max_size
= BS_MAX_DEFAULT_READ_SIZE
;
1033 static void amr_cleanup
1035 importer_t
*importer
1038 debug_if( importer
&& importer
->info
)
1039 remove_amr_importer( importer
->info
);
1042 static int amr_get_accessunit
1044 importer_t
*importer
,
1045 uint32_t track_number
,
1046 lsmash_sample_t
*buffered_sample
1049 debug_if( !importer
|| !importer
->info
|| !buffered_sample
->data
|| !buffered_sample
->length
)
1051 if( track_number
!= 1 )
1053 amr_importer_t
*amr_imp
= (amr_importer_t
*)importer
->info
;
1054 lsmash_bs_t
*bs
= amr_imp
->bs
;
1055 if( amr_imp
->status
== IMPORTER_EOF
|| lsmash_bs_is_end( bs
, 0 ) )
1058 amr_imp
->status
= IMPORTER_EOF
;
1059 buffered_sample
->length
= 0;
1062 /* Each speech frame consists of one speech frame header and one speech data.
1063 * At the end of each speech data, octet alignment if needed.
1064 * Speech frame header
1069 * FT: Frame type index
1070 * Q : Frame quality indicator
1071 * P : Must be set to 0
1072 * FT= 9, 10 and 11 for AMR-NB shall not be used in the file format.
1073 * FT=12, 13 and 14 for AMR-NB are not defined yet in the file format.
1074 * FT=10, 11, 12 and 13 for AMR-WB are not defined yet in the file format.
1075 * FT determines the size of the speech frame starting with it.
1077 uint8_t FT
= (lsmash_bs_show_byte( bs
, 0 ) >> 3) & 0x0F;
1078 const int frame_size
[2][16] =
1080 { 13, 14, 16, 18, 20, 21, 27, 32, 6, -1, -1, -1, 0, 0, 0, 1 },
1081 { 18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 0, 0, 0, 0, 1, 1 }
1083 int read_size
= frame_size
[ amr_imp
->wb
][FT
];
1084 if( read_size
<= 0 )
1086 lsmash_log( importer
, LSMASH_LOG_ERROR
, "an %s speech frame is detected.\n", read_size
< 0 ? "invalid" : "unknown" );
1087 amr_imp
->status
= IMPORTER_ERROR
;
1090 if( buffered_sample
->length
< read_size
)
1092 if( lsmash_bs_get_bytes_ex( bs
, read_size
, buffered_sample
->data
) != read_size
)
1094 lsmash_log( importer
, LSMASH_LOG_WARNING
, "the stream is truncated at the end.\n" );
1095 amr_imp
->status
= IMPORTER_EOF
;
1098 buffered_sample
->length
= read_size
;
1099 buffered_sample
->dts
= amr_imp
->au_number
++ * amr_imp
->samples_in_frame
;
1100 buffered_sample
->cts
= buffered_sample
->dts
;
1101 buffered_sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
1105 static int amr_check_magic_number
1110 #define AMR_STORAGE_MAGIC_LENGTH 6
1111 #define AMR_AMRWB_EX_MAGIC_LENGTH 3
1112 /* Check the magic number for single-channel AMR-NB/AMR-WB files.
1113 * For AMR-NB, "#!AMR\n" (or 0x2321414d520a in hexadecimal).
1114 * For AMR-WB, "#!AMR-WB\n" (or 0x2321414d522d57420a in hexadecimal).
1115 * Note that AMR-NB and AMR-WB data is stored in the 3GPP/3GPP2 file format according to
1116 * the AMR-NB and AMR-WB storage format for single channel header without the AMR magic numbers. */
1117 uint8_t buf
[AMR_STORAGE_MAGIC_LENGTH
];
1118 if( lsmash_bs_get_bytes_ex( bs
, AMR_STORAGE_MAGIC_LENGTH
, buf
) != AMR_STORAGE_MAGIC_LENGTH
1119 || memcmp( buf
, "#!AMR", AMR_STORAGE_MAGIC_LENGTH
- 1 ) )
1121 if( buf
[AMR_STORAGE_MAGIC_LENGTH
- 1] == '\n' )
1122 /* single-channel AMR-NB file */
1124 if( buf
[AMR_STORAGE_MAGIC_LENGTH
- 1] != '-'
1125 || lsmash_bs_get_bytes_ex( bs
, AMR_AMRWB_EX_MAGIC_LENGTH
, buf
) != AMR_AMRWB_EX_MAGIC_LENGTH
1126 || memcmp( buf
, "WB\n", AMR_AMRWB_EX_MAGIC_LENGTH
) )
1128 /* single-channel AMR-WB file */
1130 #undef AMR_STORAGE_MAGIC_LENGTH
1131 #undef AMR_AMRWB_EX_MAGIC_LENGTH
1134 static int amr_create_damr
1136 lsmash_audio_summary_t
*summary
,
1140 #define AMR_DAMR_LENGTH 17
1141 lsmash_bs_t
*bs
= lsmash_bs_create();
1144 lsmash_bs_put_be32( bs
, AMR_DAMR_LENGTH
);
1145 lsmash_bs_put_be32( bs
, ISOM_BOX_TYPE_DAMR
.fourcc
);
1146 /* NOTE: These are specific to each codec vendor, but we're surely not a vendor.
1147 * Using dummy data. */
1148 lsmash_bs_put_be32( bs
, 0x20202020 ); /* vendor */
1149 lsmash_bs_put_byte( bs
, 0 ); /* decoder_version */
1150 /* NOTE: Using safe value for these settings, maybe sub-optimal. */
1151 lsmash_bs_put_be16( bs
, wb
? 0xC3FF : 0x81FF ); /* mode_set, represents for all possibly existing and supported frame-types. */
1152 lsmash_bs_put_byte( bs
, 1 ); /* mode_change_period */
1153 lsmash_bs_put_byte( bs
, 1 ); /* frames_per_sample */
1154 lsmash_codec_specific_t
*cs
= lsmash_malloc_zero( sizeof(lsmash_codec_specific_t
) );
1157 lsmash_bs_cleanup( bs
);
1160 cs
->type
= LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNKNOWN
;
1161 cs
->format
= LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
;
1162 cs
->destruct
= (lsmash_codec_specific_destructor_t
)lsmash_free
;
1163 cs
->data
.unstructured
= lsmash_bs_export_data( bs
, &cs
->size
);
1164 cs
->size
= AMR_DAMR_LENGTH
;
1165 lsmash_bs_cleanup( bs
);
1166 if( !cs
->data
.unstructured
1167 || lsmash_add_entry( &summary
->opaque
->list
, cs
) < 0 )
1169 lsmash_destroy_codec_specific_data( cs
);
1173 #undef AMR_DAMR_LENGTH
1176 static lsmash_audio_summary_t
*amr_create_summary
1178 importer_t
*importer
,
1182 /* Establish an audio summary for AMR-NB or AMR-WB stream. */
1183 lsmash_audio_summary_t
*summary
= (lsmash_audio_summary_t
*)lsmash_create_summary( LSMASH_SUMMARY_TYPE_AUDIO
);
1186 summary
->sample_type
= wb
? ISOM_CODEC_TYPE_SAWB_AUDIO
: ISOM_CODEC_TYPE_SAMR_AUDIO
;
1187 summary
->max_au_length
= wb
? 61 : 32;
1188 summary
->aot
= MP4A_AUDIO_OBJECT_TYPE_NULL
; /* no effect */
1189 summary
->frequency
= (8000 << wb
);
1190 summary
->channels
= 1; /* always single channel */
1191 summary
->sample_size
= 16;
1192 summary
->samples_in_frame
= (160 << wb
);
1193 summary
->sbr_mode
= MP4A_AAC_SBR_NOT_SPECIFIED
; /* no effect */
1194 if( amr_create_damr( summary
, wb
) < 0
1195 || lsmash_add_entry( importer
->summaries
, summary
) < 0 )
1197 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
1203 static int amr_probe
1205 importer_t
*importer
1209 amr_importer_t
*amr_imp
= create_amr_importer( importer
);
1212 int wb
= amr_check_magic_number( amr_imp
->bs
);
1215 lsmash_audio_summary_t
*summary
= amr_create_summary( importer
, wb
);
1218 amr_imp
->status
= IMPORTER_OK
;
1220 amr_imp
->samples_in_frame
= summary
->samples_in_frame
;
1221 amr_imp
->au_number
= 0;
1222 importer
->info
= amr_imp
;
1225 remove_amr_importer( amr_imp
);
1229 static uint32_t amr_get_last_delta
1231 importer_t
*importer
,
1232 uint32_t track_number
1235 debug_if( !importer
|| !importer
->info
)
1237 amr_importer_t
*amr_imp
= (amr_importer_t
*)importer
->info
;
1238 if( !amr_imp
|| track_number
!= 1 )
1240 return amr_imp
->samples_in_frame
;
1243 static const importer_functions amr_importer
=
1245 { "AMR", offsetof( importer_t
, log_level
) },
1253 /***************************************************************************
1255 ETSI TS 102 366 V1.2.1 (2008-08)
1256 ***************************************************************************/
1257 #include "codecs/a52.h"
1259 #define AC3_SAMPLE_DURATION 1536 /* 256 (samples per audio block) * 6 (audio blocks) */
1263 importer_status status
;
1265 uint64_t next_frame_pos
;
1267 uint8_t buffer
[AC3_MAX_SYNCFRAME_LENGTH
];
1271 static void remove_ac3_importer( ac3_importer_t
*ac3_imp
)
1275 lsmash_bits_adhoc_cleanup( ac3_imp
->info
.bits
);
1276 lsmash_free( ac3_imp
);
1279 static ac3_importer_t
*create_ac3_importer( void )
1281 ac3_importer_t
*ac3_imp
= (ac3_importer_t
*)lsmash_malloc_zero( sizeof(ac3_importer_t
) );
1284 ac3_imp
->info
.bits
= lsmash_bits_adhoc_create();
1285 if( !ac3_imp
->info
.bits
)
1287 lsmash_free( ac3_imp
);
1293 static void ac3_importer_cleanup( importer_t
*importer
)
1295 debug_if( importer
&& importer
->info
)
1296 remove_ac3_importer( importer
->info
);
1299 static const uint32_t ac3_frame_size_table
[19][3] =
1314 { 1024, 1114, 1536 },
1315 { 1280, 1392, 1920 },
1316 { 1536, 1670, 2304 },
1317 { 1792, 1950, 2688 },
1318 { 2048, 2228, 3072 },
1319 { 2304, 2506, 3456 },
1320 { 2560, 2786, 3840 }
1323 static lsmash_audio_summary_t
*ac3_create_summary( ac3_info_t
*info
)
1325 lsmash_audio_summary_t
*summary
= (lsmash_audio_summary_t
*)lsmash_create_summary( LSMASH_SUMMARY_TYPE_AUDIO
);
1328 lsmash_ac3_specific_parameters_t
*param
= &info
->dac3_param
;
1329 lsmash_codec_specific_t
*cs
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_AC_3
,
1330 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
);
1331 cs
->data
.unstructured
= lsmash_create_ac3_specific_info( &info
->dac3_param
, &cs
->size
);
1332 if( !cs
->data
.unstructured
1333 || lsmash_add_entry( &summary
->opaque
->list
, cs
) )
1335 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
1336 lsmash_destroy_codec_specific_data( cs
);
1339 summary
->sample_type
= ISOM_CODEC_TYPE_AC_3_AUDIO
;
1340 summary
->max_au_length
= AC3_MAX_SYNCFRAME_LENGTH
;
1341 summary
->aot
= MP4A_AUDIO_OBJECT_TYPE_NULL
; /* no effect */
1342 summary
->frequency
= ac3_get_sample_rate( param
);
1343 summary
->channels
= ac3_get_channel_count( param
);
1344 summary
->sample_size
= 16; /* no effect */
1345 summary
->samples_in_frame
= AC3_SAMPLE_DURATION
;
1346 summary
->sbr_mode
= MP4A_AAC_SBR_NOT_SPECIFIED
; /* no effect */
1350 static int ac3_compare_specific_param( lsmash_ac3_specific_parameters_t
*a
, lsmash_ac3_specific_parameters_t
*b
)
1352 return (a
->fscod
!= b
->fscod
)
1353 || (a
->bsid
!= b
->bsid
)
1354 || (a
->bsmod
!= b
->bsmod
)
1355 || (a
->acmod
!= b
->acmod
)
1356 || (a
->lfeon
!= b
->lfeon
)
1357 || ((a
->frmsizecod
>> 1) != (b
->frmsizecod
>> 1));
1360 static int ac3_buffer_frame( uint8_t *buffer
, lsmash_bs_t
*bs
)
1362 uint64_t remain_size
= lsmash_bs_get_remaining_buffer_size( bs
);
1363 if( remain_size
< AC3_MAX_SYNCFRAME_LENGTH
)
1365 if( lsmash_bs_read( bs
, bs
->buffer
.max_size
) < 0 )
1367 remain_size
= lsmash_bs_get_remaining_buffer_size( bs
);
1369 uint64_t copy_size
= LSMASH_MIN( remain_size
, AC3_MAX_SYNCFRAME_LENGTH
);
1370 memcpy( buffer
, lsmash_bs_get_buffer_data( bs
), copy_size
);
1374 static int ac3_importer_get_accessunit( importer_t
*importer
, uint32_t track_number
, lsmash_sample_t
*buffered_sample
)
1376 debug_if( !importer
|| !importer
->info
|| !buffered_sample
->data
|| !buffered_sample
->length
)
1378 if( !importer
->info
|| track_number
!= 1 )
1380 lsmash_audio_summary_t
*summary
= (lsmash_audio_summary_t
*)lsmash_get_entry_data( importer
->summaries
, track_number
);
1383 ac3_importer_t
*ac3_imp
= (ac3_importer_t
*)importer
->info
;
1384 ac3_info_t
*info
= &ac3_imp
->info
;
1385 importer_status current_status
= ac3_imp
->status
;
1386 if( current_status
== IMPORTER_ERROR
)
1388 if( current_status
== IMPORTER_EOF
)
1390 buffered_sample
->length
= 0;
1393 lsmash_ac3_specific_parameters_t
*param
= &info
->dac3_param
;
1394 uint32_t frame_size
= ac3_frame_size_table
[ param
->frmsizecod
>> 1 ][ param
->fscod
];
1395 if( param
->fscod
== 0x1 && param
->frmsizecod
& 0x1 )
1397 if( buffered_sample
->length
< frame_size
)
1399 if( current_status
== IMPORTER_CHANGE
)
1401 lsmash_codec_specific_t
*cs
= isom_get_codec_specific( summary
->opaque
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_AC_3
);
1404 cs
->destruct( cs
->data
.unstructured
);
1405 cs
->data
.unstructured
= ac3_imp
->next_dac3
;
1407 summary
->frequency
= ac3_get_sample_rate( param
);
1408 summary
->channels
= ac3_get_channel_count( param
);
1409 //summary->layout_tag = ac3_channel_layout_table[ param->acmod ][ param->lfeon ];
1411 memcpy( buffered_sample
->data
, ac3_imp
->buffer
, frame_size
);
1412 buffered_sample
->length
= frame_size
;
1413 buffered_sample
->dts
= ac3_imp
->au_number
++ * summary
->samples_in_frame
;
1414 buffered_sample
->cts
= buffered_sample
->dts
;
1415 buffered_sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
1416 buffered_sample
->prop
.pre_roll
.distance
= 1; /* MDCT */
1417 lsmash_bs_t
*bs
= info
->bits
->bs
;
1418 ac3_imp
->next_frame_pos
+= frame_size
;
1419 lsmash_bs_read_seek( bs
, ac3_imp
->next_frame_pos
, SEEK_SET
);
1420 uint8_t syncword
[2] =
1422 lsmash_bs_show_byte( bs
, 0 ),
1423 lsmash_bs_show_byte( bs
, 1 )
1425 if( bs
->eob
|| (bs
->eof
&& 0 == lsmash_bs_get_remaining_buffer_size( bs
)) )
1426 ac3_imp
->status
= IMPORTER_EOF
;
1429 /* Parse the next syncframe header. */
1430 if( syncword
[0] != 0x0b
1431 || syncword
[1] != 0x77
1432 || ac3_buffer_frame( ac3_imp
->buffer
, bs
) < 0 )
1434 ac3_imp
->status
= IMPORTER_ERROR
;
1435 return current_status
;
1437 lsmash_ac3_specific_parameters_t current_param
= info
->dac3_param
;
1438 ac3_parse_syncframe_header( info
);
1439 if( ac3_compare_specific_param( ¤t_param
, &info
->dac3_param
) )
1442 uint8_t *dac3
= lsmash_create_ac3_specific_info( &info
->dac3_param
, &dummy
);
1445 ac3_imp
->status
= IMPORTER_ERROR
;
1446 return current_status
;
1448 ac3_imp
->status
= IMPORTER_CHANGE
;
1449 ac3_imp
->next_dac3
= dac3
;
1452 ac3_imp
->status
= IMPORTER_OK
;
1454 return current_status
;
1457 static int ac3_importer_probe( importer_t
*importer
)
1459 ac3_importer_t
*ac3_imp
= create_ac3_importer();
1462 lsmash_bits_t
*bits
= ac3_imp
->info
.bits
;
1463 lsmash_bs_t
*bs
= bits
->bs
;
1464 bs
->stream
= importer
->stream
;
1465 bs
->read
= lsmash_fread_wrapper
;
1466 bs
->seek
= lsmash_fseek_wrapper
;
1467 bs
->unseekable
= importer
->is_stdin
;
1468 bs
->buffer
.max_size
= AC3_MAX_SYNCFRAME_LENGTH
;
1469 /* Check the syncword and parse the syncframe header */
1470 if( lsmash_bs_show_byte( bs
, 0 ) != 0x0b
1471 || lsmash_bs_show_byte( bs
, 1 ) != 0x77
1472 || ac3_buffer_frame( ac3_imp
->buffer
, bs
) < 0
1473 || ac3_parse_syncframe_header( &ac3_imp
->info
) < 0 )
1475 lsmash_audio_summary_t
*summary
= ac3_create_summary( &ac3_imp
->info
);
1478 if( lsmash_add_entry( importer
->summaries
, summary
) < 0 )
1480 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
1483 ac3_imp
->status
= IMPORTER_OK
;
1484 ac3_imp
->au_number
= 0;
1485 importer
->info
= ac3_imp
;
1488 remove_ac3_importer( ac3_imp
);
1492 static uint32_t ac3_importer_get_last_delta( importer_t
*importer
, uint32_t track_number
)
1494 debug_if( !importer
|| !importer
->info
)
1496 ac3_importer_t
*ac3_imp
= (ac3_importer_t
*)importer
->info
;
1497 if( !ac3_imp
|| track_number
!= 1 || ac3_imp
->status
!= IMPORTER_EOF
)
1499 return AC3_SAMPLE_DURATION
;
1502 static const importer_functions ac3_importer
=
1507 ac3_importer_get_accessunit
,
1508 ac3_importer_get_last_delta
,
1509 ac3_importer_cleanup
1512 /***************************************************************************
1513 Enhanced AC-3 importer
1514 ETSI TS 102 366 V1.2.1 (2008-08)
1515 ***************************************************************************/
1516 #define EAC3_MIN_SAMPLE_DURATION 256
1520 importer_status status
;
1522 uint64_t next_frame_pos
;
1523 uint32_t next_dec3_length
;
1525 uint8_t current_fscod2
;
1526 uint8_t buffer
[EAC3_MAX_SYNCFRAME_LENGTH
];
1527 lsmash_multiple_buffers_t
*au_buffers
;
1529 uint8_t *incomplete_au
;
1531 uint32_t incomplete_au_length
;
1533 uint32_t syncframe_count_in_au
;
1536 static void remove_eac3_importer( eac3_importer_t
*eac3_imp
)
1540 lsmash_destroy_multiple_buffers( eac3_imp
->au_buffers
);
1541 lsmash_bits_adhoc_cleanup( eac3_imp
->info
.bits
);
1542 lsmash_free( eac3_imp
);
1545 static eac3_importer_t
*create_eac3_importer( void )
1547 eac3_importer_t
*eac3_imp
= (eac3_importer_t
*)lsmash_malloc_zero( sizeof(eac3_importer_t
) );
1550 eac3_info_t
*info
= &eac3_imp
->info
;
1551 info
->bits
= lsmash_bits_adhoc_create();
1554 lsmash_free( eac3_imp
);
1557 eac3_imp
->au_buffers
= lsmash_create_multiple_buffers( 2, EAC3_MAX_SYNCFRAME_LENGTH
);
1558 if( !eac3_imp
->au_buffers
)
1560 lsmash_bits_adhoc_cleanup( info
->bits
);
1561 lsmash_free( eac3_imp
);
1564 eac3_imp
->au
= lsmash_withdraw_buffer( eac3_imp
->au_buffers
, 1 );
1565 eac3_imp
->incomplete_au
= lsmash_withdraw_buffer( eac3_imp
->au_buffers
, 2 );
1569 static void eac3_importer_cleanup( importer_t
*importer
)
1571 debug_if( importer
&& importer
->info
)
1572 remove_eac3_importer( importer
->info
);
1575 static int eac3_importer_get_next_accessunit_internal( importer_t
*importer
)
1577 int au_completed
= 0;
1578 eac3_importer_t
*eac3_imp
= (eac3_importer_t
*)importer
->info
;
1579 eac3_info_t
*info
= &eac3_imp
->info
;
1580 lsmash_bs_t
*bs
= info
->bits
->bs
;
1581 while( !au_completed
)
1583 /* Read data from the stream if needed. */
1584 eac3_imp
->next_frame_pos
+= info
->frame_size
;
1585 lsmash_bs_read_seek( bs
, eac3_imp
->next_frame_pos
, SEEK_SET
);
1586 uint64_t remain_size
= lsmash_bs_get_remaining_buffer_size( bs
);
1587 if( remain_size
< EAC3_MAX_SYNCFRAME_LENGTH
)
1589 if( lsmash_bs_read( bs
, bs
->buffer
.max_size
) < 0 )
1591 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to read data from the stream.\n" );
1594 remain_size
= lsmash_bs_get_remaining_buffer_size( bs
);
1596 uint64_t copy_size
= LSMASH_MIN( remain_size
, EAC3_MAX_SYNCFRAME_LENGTH
);
1597 memcpy( eac3_imp
->buffer
, lsmash_bs_get_buffer_data( bs
), copy_size
);
1598 /* Check the remainder length of the buffer.
1599 * If there is enough length, then parse the syncframe in it.
1600 * The length 5 is the required byte length to get frame size. */
1601 if( bs
->eob
|| (bs
->eof
&& remain_size
< 5) )
1603 /* Reached the end of stream.
1604 * According to ETSI TS 102 366 V1.2.1 (2008-08),
1605 * one access unit consists of 6 audio blocks and begins with independent substream 0.
1606 * The specification doesn't mention the case where a enhanced AC-3 stream ends at non-mod6 audio blocks.
1607 * At the end of the stream, therefore, we might make an access unit which has less than 6 audio blocks anyway. */
1608 eac3_imp
->status
= IMPORTER_EOF
;
1609 au_completed
= !!eac3_imp
->incomplete_au_length
;
1612 /* No more access units in the stream. */
1613 if( lsmash_bs_get_remaining_buffer_size( bs
) )
1615 lsmash_log( importer
, LSMASH_LOG_WARNING
, "the stream is truncated at the end.\n" );
1620 if( !info
->dec3_param_initialized
)
1621 eac3_update_specific_param( info
);
1625 /* Check the syncword. */
1626 if( lsmash_bs_show_byte( bs
, 0 ) != 0x0b
1627 || lsmash_bs_show_byte( bs
, 1 ) != 0x77 )
1629 lsmash_log( importer
, LSMASH_LOG_ERROR
, "a syncword is not found.\n" );
1632 /* Parse syncframe. */
1633 info
->frame_size
= 0;
1634 if( eac3_parse_syncframe( info
) < 0 )
1636 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to parse syncframe.\n" );
1639 if( remain_size
< info
->frame_size
)
1641 lsmash_log( importer
, LSMASH_LOG_ERROR
, "a frame is truncated.\n" );
1644 int independent
= info
->strmtyp
!= 0x1;
1645 if( independent
&& info
->substreamid
== 0x0 )
1647 if( info
->number_of_audio_blocks
== 6 )
1649 /* Encountered the first syncframe of the next access unit. */
1650 info
->number_of_audio_blocks
= 0;
1653 else if( info
->number_of_audio_blocks
> 6 )
1655 lsmash_log( importer
, LSMASH_LOG_ERROR
, "greater than 6 consecutive independent substreams.\n" );
1658 info
->number_of_audio_blocks
+= eac3_audio_block_table
[ info
->numblkscod
];
1659 info
->number_of_independent_substreams
= 0;
1660 eac3_imp
->current_fscod2
= info
->fscod2
;
1662 else if( info
->syncframe_count
== 0 )
1664 /* The first syncframe in an AU must be independent and assigned substream ID 0. */
1665 lsmash_log( importer
, LSMASH_LOG_ERROR
, "the first syncframe is NOT an independent substream.\n" );
1669 info
->independent_info
[info
->number_of_independent_substreams
++].num_dep_sub
= 0;
1671 ++ info
->independent_info
[info
->number_of_independent_substreams
- 1].num_dep_sub
;
1675 memcpy( eac3_imp
->au
, eac3_imp
->incomplete_au
, eac3_imp
->incomplete_au_length
);
1676 eac3_imp
->au_length
= eac3_imp
->incomplete_au_length
;
1677 eac3_imp
->incomplete_au_length
= 0;
1678 eac3_imp
->syncframe_count_in_au
= info
->syncframe_count
;
1679 info
->syncframe_count
= 0;
1680 if( eac3_imp
->status
== IMPORTER_EOF
)
1683 /* Increase buffer size to store AU if short. */
1684 if( eac3_imp
->incomplete_au_length
+ info
->frame_size
> eac3_imp
->au_buffers
->buffer_size
)
1686 lsmash_multiple_buffers_t
*temp
= lsmash_resize_multiple_buffers( eac3_imp
->au_buffers
,
1687 eac3_imp
->au_buffers
->buffer_size
+ EAC3_MAX_SYNCFRAME_LENGTH
);
1690 eac3_imp
->au_buffers
= temp
;
1691 eac3_imp
->au
= lsmash_withdraw_buffer( eac3_imp
->au_buffers
, 1 );
1692 eac3_imp
->incomplete_au
= lsmash_withdraw_buffer( eac3_imp
->au_buffers
, 2 );
1694 /* Append syncframe data. */
1695 memcpy( eac3_imp
->incomplete_au
+ eac3_imp
->incomplete_au_length
, eac3_imp
->buffer
, info
->frame_size
);
1696 eac3_imp
->incomplete_au_length
+= info
->frame_size
;
1697 ++ info
->syncframe_count
;
1699 return info
->bits
->bs
->error
? -1 : 0;
1702 static int eac3_importer_get_accessunit( importer_t
*importer
, uint32_t track_number
, lsmash_sample_t
*buffered_sample
)
1704 debug_if( !importer
|| !importer
->info
|| !buffered_sample
->data
|| !buffered_sample
->length
)
1706 if( !importer
->info
|| track_number
!= 1 )
1708 lsmash_audio_summary_t
*summary
= (lsmash_audio_summary_t
*)lsmash_get_entry_data( importer
->summaries
, track_number
);
1711 eac3_importer_t
*eac3_imp
= (eac3_importer_t
*)importer
->info
;
1712 eac3_info_t
*info
= &eac3_imp
->info
;
1713 importer_status current_status
= eac3_imp
->status
;
1714 if( current_status
== IMPORTER_ERROR
|| buffered_sample
->length
< eac3_imp
->au_length
)
1716 if( current_status
== IMPORTER_EOF
&& eac3_imp
->au_length
== 0 )
1718 buffered_sample
->length
= 0;
1721 if( current_status
== IMPORTER_CHANGE
)
1723 lsmash_codec_specific_t
*cs
= isom_get_codec_specific( summary
->opaque
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3
);
1726 cs
->destruct( cs
->data
.unstructured
);
1727 cs
->data
.unstructured
= eac3_imp
->next_dec3
;
1728 cs
->size
= eac3_imp
->next_dec3_length
;
1730 summary
->max_au_length
= eac3_imp
->syncframe_count_in_au
* EAC3_MAX_SYNCFRAME_LENGTH
;
1731 eac3_update_sample_rate( &summary
->frequency
, &info
->dec3_param
, &eac3_imp
->current_fscod2
);
1732 eac3_update_channel_count( &summary
->channels
, &info
->dec3_param
);
1734 memcpy( buffered_sample
->data
, eac3_imp
->au
, eac3_imp
->au_length
);
1735 buffered_sample
->length
= eac3_imp
->au_length
;
1736 buffered_sample
->dts
= eac3_imp
->au_number
++ * summary
->samples_in_frame
;
1737 buffered_sample
->cts
= buffered_sample
->dts
;
1738 buffered_sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
1739 buffered_sample
->prop
.pre_roll
.distance
= 1; /* MDCT */
1740 if( eac3_imp
->status
== IMPORTER_EOF
)
1742 eac3_imp
->au_length
= 0;
1745 uint32_t old_syncframe_count_in_au
= eac3_imp
->syncframe_count_in_au
;
1746 if( eac3_importer_get_next_accessunit_internal( importer
) < 0 )
1748 eac3_imp
->status
= IMPORTER_ERROR
;
1749 return current_status
;
1751 if( eac3_imp
->syncframe_count_in_au
)
1753 /* Check sample description change. */
1754 uint32_t new_length
;
1755 uint8_t *dec3
= lsmash_create_eac3_specific_info( &info
->dec3_param
, &new_length
);
1758 eac3_imp
->status
= IMPORTER_ERROR
;
1759 return current_status
;
1761 lsmash_codec_specific_t
*cs
= isom_get_codec_specific( summary
->opaque
, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3
);
1762 if( (eac3_imp
->syncframe_count_in_au
> old_syncframe_count_in_au
)
1763 || (cs
&& (new_length
!= cs
->size
|| memcmp( dec3
, cs
->data
.unstructured
, cs
->size
))) )
1765 eac3_imp
->status
= IMPORTER_CHANGE
;
1766 eac3_imp
->next_dec3
= dec3
;
1767 eac3_imp
->next_dec3_length
= new_length
;
1771 if( eac3_imp
->status
!= IMPORTER_EOF
)
1772 eac3_imp
->status
= IMPORTER_OK
;
1773 lsmash_free( dec3
);
1776 return current_status
;
1779 static lsmash_audio_summary_t
*eac3_create_summary( eac3_importer_t
*eac3_imp
)
1781 lsmash_audio_summary_t
*summary
= (lsmash_audio_summary_t
*)lsmash_create_summary( LSMASH_SUMMARY_TYPE_AUDIO
);
1784 eac3_info_t
*info
= &eac3_imp
->info
;
1785 lsmash_codec_specific_t
*cs
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3
,
1786 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
);
1787 cs
->data
.unstructured
= lsmash_create_eac3_specific_info( &info
->dec3_param
, &cs
->size
);
1788 if( !cs
->data
.unstructured
1789 || lsmash_add_entry( &summary
->opaque
->list
, cs
) < 0 )
1791 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
1792 lsmash_destroy_codec_specific_data( cs
);
1795 summary
->sample_type
= ISOM_CODEC_TYPE_EC_3_AUDIO
;
1796 summary
->max_au_length
= eac3_imp
->syncframe_count_in_au
* EAC3_MAX_SYNCFRAME_LENGTH
;
1797 summary
->aot
= MP4A_AUDIO_OBJECT_TYPE_NULL
; /* no effect */
1798 summary
->sample_size
= 16; /* no effect */
1799 summary
->samples_in_frame
= EAC3_MIN_SAMPLE_DURATION
* 6; /* 256 (samples per audio block) * 6 (audio blocks) */
1800 summary
->sbr_mode
= MP4A_AAC_SBR_NOT_SPECIFIED
; /* no effect */
1801 eac3_update_sample_rate( &summary
->frequency
, &info
->dec3_param
, &eac3_imp
->current_fscod2
);
1802 eac3_update_channel_count( &summary
->channels
, &info
->dec3_param
);
1806 static int eac3_importer_probe( importer_t
*importer
)
1808 eac3_importer_t
*eac3_imp
= create_eac3_importer();
1811 lsmash_bits_t
*bits
= eac3_imp
->info
.bits
;
1812 lsmash_bs_t
*bs
= bits
->bs
;
1813 bs
->stream
= importer
->stream
;
1814 bs
->read
= lsmash_fread_wrapper
;
1815 bs
->seek
= lsmash_fseek_wrapper
;
1816 bs
->unseekable
= importer
->is_stdin
;
1817 bs
->buffer
.max_size
= EAC3_MAX_SYNCFRAME_LENGTH
;
1818 importer
->info
= eac3_imp
;
1819 if( eac3_importer_get_next_accessunit_internal( importer
) < 0 )
1821 lsmash_audio_summary_t
*summary
= eac3_create_summary( eac3_imp
);
1824 if( eac3_imp
->status
!= IMPORTER_EOF
)
1825 eac3_imp
->status
= IMPORTER_OK
;
1826 eac3_imp
->au_number
= 0;
1827 if( lsmash_add_entry( importer
->summaries
, summary
) < 0 )
1829 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
1834 remove_eac3_importer( eac3_imp
);
1835 importer
->info
= NULL
;
1839 static uint32_t eac3_importer_get_last_delta( importer_t
*importer
, uint32_t track_number
)
1841 debug_if( !importer
|| !importer
->info
)
1843 eac3_importer_t
*eac3_imp
= (eac3_importer_t
*)importer
->info
;
1844 if( !eac3_imp
|| track_number
!= 1 || eac3_imp
->status
!= IMPORTER_EOF
|| eac3_imp
->au_length
)
1846 return EAC3_MIN_SAMPLE_DURATION
* eac3_imp
->info
.number_of_audio_blocks
;
1849 static const importer_functions eac3_importer
=
1851 { "Enhanced AC-3", offsetof( importer_t
, log_level
) },
1853 eac3_importer_probe
,
1854 eac3_importer_get_accessunit
,
1855 eac3_importer_get_last_delta
,
1856 eac3_importer_cleanup
1859 /***************************************************************************
1861 ISO/IEC 14496-3 2009 Fourth edition
1862 ***************************************************************************/
1863 #define ALSSC_TWELVE_LENGTH 22
1871 uint16_t frame_length
;
1873 uint8_t random_access
;
1875 uint32_t access_unit_size
;
1876 uint32_t number_of_ra_units
;
1877 uint32_t *ra_unit_size
;
1880 } als_specific_config_t
;
1884 importer_status status
;
1886 als_specific_config_t alssc
;
1887 uint32_t samples_in_frame
;
1889 } mp4a_als_importer_t
;
1891 static void remove_mp4a_als_importer( mp4a_als_importer_t
*als_imp
)
1893 lsmash_bs_cleanup( als_imp
->bs
);
1894 lsmash_free( als_imp
->alssc
.ra_unit_size
);
1895 lsmash_free( als_imp
->alssc
.sc_data
);
1896 lsmash_free( als_imp
);
1899 static void mp4a_als_importer_cleanup( importer_t
*importer
)
1901 debug_if( importer
&& importer
->info
)
1902 remove_mp4a_als_importer( importer
->info
);
1905 static mp4a_als_importer_t
*create_mp4a_als_importer( importer_t
*importer
)
1907 mp4a_als_importer_t
*als_imp
= lsmash_malloc_zero( sizeof(mp4a_als_importer_t
) );
1910 als_imp
->bs
= lsmash_bs_create();
1913 lsmash_free( als_imp
);
1919 static void als_copy_from_buffer( als_specific_config_t
*alssc
, lsmash_bs_t
*bs
, uint64_t size
)
1921 if( alssc
->alloc
< size
)
1923 size_t alloc
= alssc
->alloc
? (alssc
->alloc
<< 1) : (1 << 10);
1924 uint8_t *temp
= lsmash_realloc( alssc
->sc_data
, alloc
);
1927 alssc
->sc_data
= temp
;
1928 alssc
->alloc
= alloc
;
1930 memcpy( alssc
->sc_data
+ alssc
->size
, lsmash_bs_get_buffer_data( bs
), size
);
1931 alssc
->size
+= size
;
1932 lsmash_bs_read_seek( bs
, size
, SEEK_CUR
);
1935 static int als_parse_specific_config( mp4a_als_importer_t
*als_imp
)
1937 lsmash_bs_t
*bs
= als_imp
->bs
;
1938 /* Check ALS identifier( = 0x414C5300). */
1939 if( 0x414C5300 != lsmash_bs_show_be32( bs
, 0 ) )
1941 als_specific_config_t
*alssc
= &als_imp
->alssc
;
1942 alssc
->samp_freq
= lsmash_bs_show_be32( bs
, 4 );
1943 alssc
->samples
= lsmash_bs_show_be32( bs
, 8 );
1944 if( alssc
->samples
== 0xffffffff )
1945 return -1; /* We don't support this case. */
1946 alssc
->channels
= lsmash_bs_show_be16( bs
, 12 );
1947 alssc
->resolution
= (lsmash_bs_show_byte( bs
, 14 ) & 0x1c) >> 2;
1948 if( alssc
->resolution
> 3 )
1949 return -1; /* reserved */
1950 alssc
->frame_length
= lsmash_bs_show_be16( bs
, 15 );
1951 alssc
->random_access
= lsmash_bs_show_byte( bs
, 17 );
1952 alssc
->ra_flag
= (lsmash_bs_show_byte( bs
, 18 ) & 0xc0) >> 6;
1953 if( alssc
->ra_flag
== 0 )
1954 return -1; /* We don't support this case. */
1956 if( alssc
->samples
== 0xffffffff && alssc
->ra_flag
== 2 )
1959 uint8_t temp8
= lsmash_bs_show_byte( bs
, 20 );
1960 int chan_sort
= !!(temp8
& 0x1);
1961 if( alssc
->channels
== 0 )
1964 return -1; /* If channels = 0 (mono), joint_stereo = 0. */
1965 else if( temp8
& 0x4 )
1966 return -1; /* If channels = 0 (mono), mc_coding = 0. */
1967 else if( chan_sort
)
1968 return -1; /* If channels = 0 (mono), chan_sort = 0. */
1970 int chan_config
= !!(temp8
& 0x2);
1971 temp8
= lsmash_bs_show_byte( bs
, 21 );
1972 int crc_enabled
= !!(temp8
& 0x80);
1973 int aux_data_enabled
= !!(temp8
& 0x1);
1974 als_copy_from_buffer( alssc
, bs
, ALSSC_TWELVE_LENGTH
);
1977 /* chan_config_info */
1978 lsmash_bs_read( bs
, 2 );
1979 als_copy_from_buffer( alssc
, bs
, 2 );
1983 uint32_t ChBits
= lsmash_ceil_log2( alssc
->channels
+ 1 );
1984 uint32_t chan_pos_length
= (alssc
->channels
+ 1) * ChBits
;
1985 chan_pos_length
= ((uint64_t)chan_pos_length
+ 7) / 8; /* byte_align */
1986 lsmash_bs_read( bs
, chan_pos_length
);
1987 als_copy_from_buffer( alssc
, bs
, chan_pos_length
);
1989 /* orig_header, orig_trailer and crc. */
1991 uint32_t header_size
= lsmash_bs_show_be32( bs
, 0 );
1992 uint32_t trailer_size
= lsmash_bs_show_be32( bs
, 4 );
1993 als_copy_from_buffer( alssc
, bs
, 8 );
1994 if( header_size
!= 0xffffffff )
1996 lsmash_bs_read( bs
, header_size
);
1997 als_copy_from_buffer( alssc
, bs
, header_size
);
1999 if( trailer_size
!= 0xffffffff )
2001 lsmash_bs_read( bs
, trailer_size
);
2002 als_copy_from_buffer( alssc
, bs
, trailer_size
);
2006 lsmash_bs_read( bs
, 4 );
2007 als_copy_from_buffer( alssc
, bs
, 4 );
2010 /* Random access units */
2012 uint32_t number_of_frames
= ((alssc
->samples
+ alssc
->frame_length
) / (alssc
->frame_length
+ 1));
2013 if( alssc
->random_access
!= 0 )
2014 alssc
->number_of_ra_units
= ((uint64_t)number_of_frames
+ alssc
->random_access
- 1) / alssc
->random_access
;
2016 alssc
->number_of_ra_units
= 0;
2017 if( alssc
->ra_flag
== 2 && alssc
->random_access
!= 0 )
2019 /* We don't copy all ra_unit_size into alssc->sc_data. */
2020 int64_t read_size
= (int64_t)alssc
->number_of_ra_units
* 4;
2021 int64_t end_offset
= lsmash_bs_get_stream_pos( bs
) + read_size
;
2022 alssc
->ra_unit_size
= lsmash_malloc( read_size
);
2023 if( !alssc
->ra_unit_size
)
2025 uint32_t max_ra_unit_size
= 0;
2026 for( uint32_t i
= 0; i
< alssc
->number_of_ra_units
; i
++ )
2028 alssc
->ra_unit_size
[i
] = lsmash_bs_get_be32( bs
);
2029 max_ra_unit_size
= LSMASH_MAX( max_ra_unit_size
, alssc
->ra_unit_size
[i
] );
2031 lsmash_bs_read_seek( bs
, end_offset
, SEEK_SET
);
2034 alssc
->ra_unit_size
= NULL
;
2036 /* auxiliary data */
2037 if( aux_data_enabled
)
2039 uint32_t aux_size
= lsmash_bs_show_be32( bs
, 0 );
2040 als_copy_from_buffer( alssc
, bs
, 4 );
2041 if( aux_size
&& aux_size
!= 0xffffffff )
2043 lsmash_bs_read( bs
, aux_size
);
2044 als_copy_from_buffer( alssc
, bs
, aux_size
);
2047 /* Set 0 to ra_flag. We will remove ra_unit_size in each access unit. */
2048 alssc
->sc_data
[18] &= 0x3f;
2052 static int mp4a_als_importer_get_accessunit( importer_t
*importer
, uint32_t track_number
, lsmash_sample_t
*buffered_sample
)
2054 debug_if( !importer
|| !importer
->info
|| !buffered_sample
->data
|| !buffered_sample
->length
)
2056 if( !importer
->info
|| track_number
!= 1 )
2058 lsmash_audio_summary_t
*summary
= (lsmash_audio_summary_t
*)lsmash_get_entry_data( importer
->summaries
, track_number
);
2061 mp4a_als_importer_t
*als_imp
= (mp4a_als_importer_t
*)importer
->info
;
2062 importer_status current_status
= als_imp
->status
;
2063 if( current_status
== IMPORTER_EOF
)
2065 buffered_sample
->length
= 0;
2068 lsmash_bs_t
*bs
= als_imp
->bs
;
2069 als_specific_config_t
*alssc
= &als_imp
->alssc
;
2070 if( alssc
->number_of_ra_units
== 0 )
2072 memcpy( buffered_sample
->data
, lsmash_bs_get_buffer_data( bs
), alssc
->access_unit_size
);
2073 buffered_sample
->length
= alssc
->access_unit_size
;
2074 buffered_sample
->cts
= 0;
2075 buffered_sample
->dts
= 0;
2076 buffered_sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
2077 als_imp
->status
= IMPORTER_EOF
;
2081 if( alssc
->ra_flag
== 2 )
2082 au_length
= alssc
->ra_unit_size
[ als_imp
->au_number
];
2083 else /* if( alssc->ra_flag == 1 ) */
2084 /* We don't export ra_unit_size into a sample. */
2085 au_length
= lsmash_bs_get_be32( bs
);
2086 if( buffered_sample
->length
< au_length
)
2088 lsmash_log( importer
, LSMASH_LOG_WARNING
, "the buffer has not enough size.\n" );
2091 if( lsmash_bs_get_bytes_ex( bs
, au_length
, buffered_sample
->data
) != au_length
)
2093 lsmash_log( importer
, LSMASH_LOG_WARNING
, "failed to read an access unit.\n" );
2094 als_imp
->status
= IMPORTER_ERROR
;
2097 buffered_sample
->length
= au_length
;
2098 buffered_sample
->dts
= als_imp
->au_number
++ * als_imp
->samples_in_frame
;
2099 buffered_sample
->cts
= buffered_sample
->dts
;
2100 buffered_sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
2101 if( als_imp
->au_number
== alssc
->number_of_ra_units
)
2102 als_imp
->status
= IMPORTER_EOF
;
2108 static lsmash_audio_summary_t
*als_create_summary( lsmash_bs_t
*bs
, als_specific_config_t
*alssc
)
2110 lsmash_audio_summary_t
*summary
= (lsmash_audio_summary_t
*)lsmash_create_summary( LSMASH_SUMMARY_TYPE_AUDIO
);
2113 summary
->sample_type
= ISOM_CODEC_TYPE_MP4A_AUDIO
;
2114 summary
->aot
= MP4A_AUDIO_OBJECT_TYPE_ALS
;
2115 summary
->frequency
= alssc
->samp_freq
;
2116 summary
->channels
= alssc
->channels
+ 1;
2117 summary
->sample_size
= (alssc
->resolution
+ 1) * 8;
2118 summary
->sbr_mode
= MP4A_AAC_SBR_NOT_SPECIFIED
; /* no effect */
2119 if( alssc
->random_access
!= 0 )
2121 summary
->samples_in_frame
= (alssc
->frame_length
+ 1) * alssc
->random_access
;
2122 summary
->max_au_length
= summary
->channels
* (summary
->sample_size
/ 8) * summary
->samples_in_frame
;
2126 /* Read the remainder of overall stream as an access unit. */
2127 alssc
->access_unit_size
= lsmash_bs_get_remaining_buffer_size( bs
);
2130 if( lsmash_bs_read( bs
, bs
->buffer
.max_size
) < 0 )
2132 alssc
->access_unit_size
= lsmash_bs_get_remaining_buffer_size( bs
);
2134 summary
->max_au_length
= alssc
->access_unit_size
;
2135 summary
->samples_in_frame
= 0; /* hack for mp4sys_als_importer_get_last_delta() */
2137 uint32_t data_length
;
2138 uint8_t *data
= mp4a_export_AudioSpecificConfig( MP4A_AUDIO_OBJECT_TYPE_ALS
,
2139 summary
->frequency
, summary
->channels
, summary
->sbr_mode
,
2140 alssc
->sc_data
, alssc
->size
, &data_length
);
2143 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
2146 lsmash_codec_specific_t
*specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG
,
2147 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
2150 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
2151 lsmash_free( data
);
2154 lsmash_mp4sys_decoder_parameters_t
*param
= (lsmash_mp4sys_decoder_parameters_t
*)specific
->data
.structured
;
2155 param
->objectTypeIndication
= MP4SYS_OBJECT_TYPE_Audio_ISO_14496_3
;
2156 param
->streamType
= MP4SYS_STREAM_TYPE_AudioStream
;
2157 if( lsmash_set_mp4sys_decoder_specific_info( param
, data
, data_length
) < 0 )
2159 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
2160 lsmash_destroy_codec_specific_data( specific
);
2161 lsmash_free( data
);
2164 lsmash_free( data
);
2165 if( lsmash_add_entry( &summary
->opaque
->list
, specific
) < 0 )
2167 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
2168 lsmash_destroy_codec_specific_data( specific
);
2174 static int mp4a_als_importer_probe( importer_t
*importer
)
2176 mp4a_als_importer_t
*als_imp
= create_mp4a_als_importer( importer
);
2179 lsmash_bs_t
*bs
= als_imp
->bs
;
2180 bs
->stream
= importer
->stream
;
2181 bs
->read
= lsmash_fread_wrapper
;
2182 bs
->seek
= lsmash_fseek_wrapper
;
2183 bs
->unseekable
= importer
->is_stdin
;
2184 bs
->buffer
.max_size
= BS_MAX_DEFAULT_READ_SIZE
;
2185 /* Parse ALS specific configuration. */
2186 if( als_parse_specific_config( als_imp
) < 0 )
2188 lsmash_audio_summary_t
*summary
= als_create_summary( bs
, &als_imp
->alssc
);
2191 /* importer status */
2192 als_imp
->status
= IMPORTER_OK
;
2193 als_imp
->samples_in_frame
= summary
->samples_in_frame
;
2194 if( lsmash_add_entry( importer
->summaries
, summary
) < 0 )
2196 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
2199 importer
->info
= als_imp
;
2202 remove_mp4a_als_importer( als_imp
);
2206 static uint32_t mp4a_als_importer_get_last_delta( importer_t
*importer
, uint32_t track_number
)
2208 debug_if( !importer
|| !importer
->info
)
2210 mp4a_als_importer_t
*als_imp
= (mp4a_als_importer_t
*)importer
->info
;
2211 if( !als_imp
|| track_number
!= 1 || als_imp
->status
!= IMPORTER_EOF
)
2213 als_specific_config_t
*alssc
= &als_imp
->alssc
;
2214 /* If alssc->number_of_ra_units == 0, then the last sample duration is just alssc->samples
2215 * since als_create_summary sets 0 to summary->samples_in_frame i.e. als_imp->samples_in_frame. */
2216 return alssc
->samples
- (alssc
->number_of_ra_units
- 1) * als_imp
->samples_in_frame
;
2219 static const importer_functions mp4a_als_importer
=
2221 { "MPEG-4 ALS", offsetof( importer_t
, log_level
) },
2223 mp4a_als_importer_probe
,
2224 mp4a_als_importer_get_accessunit
,
2225 mp4a_als_importer_get_last_delta
,
2226 mp4a_als_importer_cleanup
2229 /***************************************************************************
2231 ETSI TS 102 114 V1.2.1 (2002-12)
2232 ETSI TS 102 114 V1.3.1 (2011-08)
2233 ETSI TS 102 114 V1.4.1 (2012-09)
2234 ***************************************************************************/
2235 #include "codecs/dts.h"
2239 importer_status status
;
2241 uint64_t next_frame_pos
;
2242 uint8_t buffer
[DTS_MAX_EXSS_SIZE
];
2243 lsmash_multiple_buffers_t
*au_buffers
;
2246 uint8_t *incomplete_au
;
2247 uint32_t incomplete_au_length
;
2251 static void remove_dts_importer( dts_importer_t
*dts_imp
)
2255 lsmash_destroy_multiple_buffers( dts_imp
->au_buffers
);
2256 lsmash_bits_adhoc_cleanup( dts_imp
->info
.bits
);
2257 lsmash_free( dts_imp
);
2260 static dts_importer_t
*create_dts_importer( void )
2262 dts_importer_t
*dts_imp
= (dts_importer_t
*)lsmash_malloc_zero( sizeof(dts_importer_t
) );
2265 dts_info_t
*dts_info
= &dts_imp
->info
;
2266 dts_info
->bits
= lsmash_bits_adhoc_create();
2267 if( !dts_info
->bits
)
2269 lsmash_free( dts_imp
);
2272 dts_imp
->au_buffers
= lsmash_create_multiple_buffers( 2, DTS_MAX_EXSS_SIZE
);
2273 if( !dts_imp
->au_buffers
)
2275 lsmash_bits_adhoc_cleanup( dts_info
->bits
);
2276 lsmash_free( dts_imp
);
2279 dts_imp
->au
= lsmash_withdraw_buffer( dts_imp
->au_buffers
, 1 );
2280 dts_imp
->incomplete_au
= lsmash_withdraw_buffer( dts_imp
->au_buffers
, 2 );
2281 dts_setup_parser( dts_info
);
2285 static void dts_importer_cleanup( importer_t
*importer
)
2287 debug_if( importer
&& importer
->info
)
2288 remove_dts_importer( importer
->info
);
2291 static int dts_importer_get_next_accessunit_internal( importer_t
*importer
)
2293 int au_completed
= 0;
2294 dts_importer_t
*dts_imp
= (dts_importer_t
*)importer
->info
;
2295 dts_info_t
*info
= &dts_imp
->info
;
2296 lsmash_bs_t
*bs
= info
->bits
->bs
;
2297 while( !au_completed
)
2299 /* Read data from the stream if needed. */
2300 dts_imp
->next_frame_pos
+= info
->frame_size
;
2301 lsmash_bs_read_seek( bs
, dts_imp
->next_frame_pos
, SEEK_SET
);
2302 uint64_t remain_size
= lsmash_bs_get_remaining_buffer_size( bs
);
2303 if( remain_size
< DTS_MAX_EXSS_SIZE
)
2305 if( lsmash_bs_read( bs
, bs
->buffer
.max_size
) < 0 )
2307 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to read data from the stream.\n" );
2310 remain_size
= lsmash_bs_get_remaining_buffer_size( bs
);
2312 memcpy( dts_imp
->buffer
, lsmash_bs_get_buffer_data( bs
), LSMASH_MIN( remain_size
, DTS_MAX_EXSS_SIZE
) );
2313 /* Check the remainder length of the buffer.
2314 * If there is enough length, then parse the frame in it.
2315 * The length 10 is the required byte length to get frame size. */
2316 if( bs
->eob
|| (bs
->eof
&& remain_size
< 10) )
2318 /* Reached the end of stream. */
2319 dts_imp
->status
= IMPORTER_EOF
;
2320 au_completed
= !!dts_imp
->incomplete_au_length
;
2323 /* No more access units in the stream. */
2324 if( lsmash_bs_get_remaining_buffer_size( bs
) )
2326 lsmash_log( importer
, LSMASH_LOG_WARNING
, "the stream is truncated at the end.\n" );
2331 if( !info
->ddts_param_initialized
)
2332 dts_update_specific_param( info
);
2336 /* Parse substream frame. */
2337 dts_substream_type prev_substream_type
= info
->substream_type
;
2338 info
->substream_type
= dts_get_substream_type( info
);
2339 int (*dts_parse_frame
)( dts_info_t
* ) = NULL
;
2340 switch( info
->substream_type
)
2342 /* Decide substream frame parser and check if this frame and the previous frame belong to the same AU. */
2343 case DTS_SUBSTREAM_TYPE_CORE
:
2344 if( prev_substream_type
!= DTS_SUBSTREAM_TYPE_NONE
)
2346 dts_parse_frame
= dts_parse_core_substream
;
2348 case DTS_SUBSTREAM_TYPE_EXTENSION
:
2350 uint8_t prev_exss_index
= info
->exss_index
;
2351 if( dts_get_exss_index( info
, &info
->exss_index
) < 0 )
2353 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to get the index of an extension substream.\n" );
2356 if( prev_substream_type
== DTS_SUBSTREAM_TYPE_EXTENSION
2357 && info
->exss_index
<= prev_exss_index
)
2359 dts_parse_frame
= dts_parse_extension_substream
;
2363 lsmash_log( importer
, LSMASH_LOG_ERROR
, "unknown substream type is detected.\n" );
2366 if( !info
->ddts_param_initialized
&& au_completed
)
2367 dts_update_specific_param( info
);
2368 info
->frame_size
= 0;
2369 if( dts_parse_frame( info
) < 0 )
2371 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to parse a frame.\n" );
2377 memcpy( dts_imp
->au
, dts_imp
->incomplete_au
, dts_imp
->incomplete_au_length
);
2378 dts_imp
->au_length
= dts_imp
->incomplete_au_length
;
2379 dts_imp
->incomplete_au_length
= 0;
2380 info
->exss_count
= (info
->substream_type
== DTS_SUBSTREAM_TYPE_EXTENSION
);
2381 if( dts_imp
->status
== IMPORTER_EOF
)
2384 /* Increase buffer size to store AU if short. */
2385 if( dts_imp
->incomplete_au_length
+ info
->frame_size
> dts_imp
->au_buffers
->buffer_size
)
2387 lsmash_multiple_buffers_t
*temp
= lsmash_resize_multiple_buffers( dts_imp
->au_buffers
,
2388 dts_imp
->au_buffers
->buffer_size
+ DTS_MAX_EXSS_SIZE
);
2391 dts_imp
->au_buffers
= temp
;
2392 dts_imp
->au
= lsmash_withdraw_buffer( dts_imp
->au_buffers
, 1 );
2393 dts_imp
->incomplete_au
= lsmash_withdraw_buffer( dts_imp
->au_buffers
, 2 );
2395 /* Append frame data. */
2396 memcpy( dts_imp
->incomplete_au
+ dts_imp
->incomplete_au_length
, dts_imp
->buffer
, info
->frame_size
);
2397 dts_imp
->incomplete_au_length
+= info
->frame_size
;
2399 return info
->bits
->bs
->error
? -1 : 0;
2402 static int dts_importer_get_accessunit( importer_t
*importer
, uint32_t track_number
, lsmash_sample_t
*buffered_sample
)
2404 debug_if( !importer
|| !importer
->info
|| !buffered_sample
->data
|| !buffered_sample
->length
)
2406 if( !importer
->info
|| track_number
!= 1 )
2408 lsmash_audio_summary_t
*summary
= (lsmash_audio_summary_t
*)lsmash_get_entry_data( importer
->summaries
, track_number
);
2411 dts_importer_t
*dts_imp
= (dts_importer_t
*)importer
->info
;
2412 dts_info_t
*info
= &dts_imp
->info
;
2413 importer_status current_status
= dts_imp
->status
;
2414 if( current_status
== IMPORTER_ERROR
|| buffered_sample
->length
< dts_imp
->au_length
)
2416 if( current_status
== IMPORTER_EOF
&& dts_imp
->au_length
== 0 )
2418 buffered_sample
->length
= 0;
2421 if( current_status
== IMPORTER_CHANGE
)
2422 summary
->max_au_length
= 0;
2423 memcpy( buffered_sample
->data
, dts_imp
->au
, dts_imp
->au_length
);
2424 buffered_sample
->length
= dts_imp
->au_length
;
2425 buffered_sample
->dts
= dts_imp
->au_number
++ * summary
->samples_in_frame
;
2426 buffered_sample
->cts
= buffered_sample
->dts
;
2427 buffered_sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
2428 buffered_sample
->prop
.pre_roll
.distance
= !!(info
->flags
& DTS_EXT_SUBSTREAM_LBR_FLAG
); /* MDCT */
2429 if( dts_imp
->status
== IMPORTER_EOF
)
2431 dts_imp
->au_length
= 0;
2434 if( dts_importer_get_next_accessunit_internal( importer
) )
2435 dts_imp
->status
= IMPORTER_ERROR
;
2436 return current_status
;
2439 static lsmash_audio_summary_t
*dts_create_summary( dts_info_t
*info
)
2441 lsmash_audio_summary_t
*summary
= (lsmash_audio_summary_t
*)lsmash_create_summary( LSMASH_SUMMARY_TYPE_AUDIO
);
2444 lsmash_dts_specific_parameters_t
*param
= &info
->ddts_param
;
2445 lsmash_codec_specific_t
*specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_DTS
,
2446 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
);
2447 specific
->data
.unstructured
= lsmash_create_dts_specific_info( param
, &specific
->size
);
2448 if( !specific
->data
.unstructured
2449 || lsmash_add_entry( &summary
->opaque
->list
, specific
) )
2451 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
2452 lsmash_destroy_codec_specific_data( specific
);
2455 /* The CODEC identifiers probably should not be the combination of 'mp4a' and
2456 * the objectTypeIndications for DTS audio since there is no public specification
2457 * which defines the encapsulation of the stream as the MPEG-4 Audio context yet.
2458 * In the world, there are muxers which is using such doubtful implementation.
2459 * The objectTypeIndications are registered at MP4RA, but this does not always
2460 * mean we can mux by using those objectTypeIndications.
2461 * If available, there shall be the specification which defines the existence of
2462 * DecoderSpecificInfo and its semantics, and what access unit consists of. */
2463 summary
->sample_type
= lsmash_dts_get_codingname( param
);
2464 summary
->aot
= MP4A_AUDIO_OBJECT_TYPE_NULL
; /* make no sense */
2465 summary
->sbr_mode
= MP4A_AAC_SBR_NOT_SPECIFIED
; /* make no sense */
2466 switch( param
->DTSSamplingFrequency
)
2468 case 12000 : /* Invalid? (No reference in the spec) */
2473 case 384000 : /* Invalid? (No reference in the spec) */
2474 summary
->frequency
= 48000;
2480 case 352800 : /* Invalid? (No reference in the spec) */
2481 summary
->frequency
= 44100;
2483 case 8000 : /* Invalid? (No reference in the spec) */
2488 summary
->frequency
= 32000;
2491 summary
->frequency
= 0;
2494 summary
->samples_in_frame
= (summary
->frequency
* info
->frame_duration
) / param
->DTSSamplingFrequency
;
2495 summary
->max_au_length
= DTS_MAX_CORE_SIZE
+ DTS_MAX_NUM_EXSS
* DTS_MAX_EXSS_SIZE
;
2496 summary
->sample_size
= param
->pcmSampleDepth
;
2497 summary
->channels
= dts_get_max_channel_count( info
);
2501 static int dts_importer_probe( importer_t
*importer
)
2503 dts_importer_t
*dts_imp
= create_dts_importer();
2506 lsmash_bits_t
*bits
= dts_imp
->info
.bits
;
2507 lsmash_bs_t
*bs
= bits
->bs
;
2508 bs
->stream
= importer
->stream
;
2509 bs
->read
= lsmash_fread_wrapper
;
2510 bs
->seek
= lsmash_fseek_wrapper
;
2511 bs
->unseekable
= importer
->is_stdin
;
2512 bs
->buffer
.max_size
= DTS_MAX_EXSS_SIZE
;
2513 importer
->info
= dts_imp
;
2514 if( dts_importer_get_next_accessunit_internal( importer
) < 0 )
2516 lsmash_audio_summary_t
*summary
= dts_create_summary( &dts_imp
->info
);
2519 if( dts_imp
->status
!= IMPORTER_EOF
)
2520 dts_imp
->status
= IMPORTER_OK
;
2521 dts_imp
->au_number
= 0;
2522 if( lsmash_add_entry( importer
->summaries
, summary
) < 0 )
2524 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
2529 remove_dts_importer( dts_imp
);
2530 importer
->info
= NULL
;
2534 static uint32_t dts_importer_get_last_delta( importer_t
* importer
, uint32_t track_number
)
2536 debug_if( !importer
|| !importer
->info
)
2538 dts_importer_t
*dts_imp
= (dts_importer_t
*)importer
->info
;
2539 if( !dts_imp
|| track_number
!= 1 || dts_imp
->status
!= IMPORTER_EOF
|| dts_imp
->au_length
)
2541 lsmash_audio_summary_t
*summary
= (lsmash_audio_summary_t
*)lsmash_get_entry_data( importer
->summaries
, track_number
);
2544 return (summary
->frequency
* dts_imp
->info
.frame_duration
) / dts_imp
->info
.ddts_param
.DTSSamplingFrequency
;
2547 static const importer_functions dts_importer
=
2549 { "DTS Coherent Acoustics", offsetof( importer_t
, log_level
) },
2552 dts_importer_get_accessunit
,
2553 dts_importer_get_last_delta
,
2554 dts_importer_cleanup
2557 /***************************************************************************
2559 ITU-T Recommendation H.264 (04/13)
2560 ISO/IEC 14496-15:2010
2561 ***************************************************************************/
2562 #include "codecs/h264.h"
2563 #include "codecs/nalu.h"
2567 importer_status status
;
2569 lsmash_entry_list_t avcC_list
[1]; /* stored as lsmash_codec_specific_t */
2570 lsmash_media_ts_list_t ts_list
;
2572 uint32_t max_au_length
;
2573 uint32_t num_undecodable
;
2574 uint32_t avcC_number
;
2575 uint32_t last_delta
;
2576 uint64_t last_intra_cts
;
2577 uint64_t sc_head_pos
;
2578 uint8_t composition_reordering_present
;
2579 uint8_t field_pic_present
;
2590 static void remove_h264_importer( h264_importer_t
*h264_imp
)
2594 lsmash_remove_entries( h264_imp
->avcC_list
, lsmash_destroy_codec_specific_data
);
2595 h264_cleanup_parser( &h264_imp
->info
);
2596 lsmash_bs_cleanup( h264_imp
->bs
);
2597 lsmash_free( h264_imp
->ts_list
.timestamp
);
2598 lsmash_free( h264_imp
);
2601 static void h264_importer_cleanup( importer_t
*importer
)
2603 debug_if( importer
&& importer
->info
)
2604 remove_h264_importer( importer
->info
);
2607 static h264_importer_t
*create_h264_importer( importer_t
*importer
)
2609 h264_importer_t
*h264_imp
= lsmash_malloc_zero( sizeof(h264_importer_t
) );
2612 if( h264_setup_parser( &h264_imp
->info
, 0 ) < 0 )
2614 remove_h264_importer( h264_imp
);
2617 lsmash_bs_t
*bs
= lsmash_bs_create();
2620 remove_h264_importer( h264_imp
);
2623 bs
->stream
= importer
->stream
;
2624 bs
->read
= lsmash_fread_wrapper
;
2625 bs
->seek
= lsmash_fseek_wrapper
;
2626 bs
->unseekable
= importer
->is_stdin
;
2627 bs
->buffer
.max_size
= BS_MAX_DEFAULT_READ_SIZE
;
2628 lsmash_init_entry_list( h264_imp
->avcC_list
);
2633 static inline int h264_complete_au( h264_access_unit_t
*au
, int probe
)
2635 if( !au
->picture
.has_primary
|| au
->incomplete_length
== 0 )
2638 memcpy( au
->data
, au
->incomplete_data
, au
->incomplete_length
);
2639 au
->length
= au
->incomplete_length
;
2640 au
->incomplete_length
= 0;
2641 au
->picture
.has_primary
= 0;
2645 static void h264_append_nalu_to_au( h264_access_unit_t
*au
, uint8_t *src_nalu
, uint32_t nalu_length
, int probe
)
2649 uint8_t *dst_nalu
= au
->incomplete_data
+ au
->incomplete_length
+ NALU_DEFAULT_NALU_LENGTH_SIZE
;
2650 for( int i
= NALU_DEFAULT_NALU_LENGTH_SIZE
; i
; i
-- )
2651 *(dst_nalu
- i
) = (nalu_length
>> ((i
- 1) * 8)) & 0xff;
2652 memcpy( dst_nalu
, src_nalu
, nalu_length
);
2654 /* Note: au->incomplete_length shall be 0 immediately after AU has completed.
2655 * Therefore, possible_au_length in h264_get_access_unit_internal() can't be used here
2656 * to avoid increasing AU length monotonously through the entire stream. */
2657 au
->incomplete_length
+= NALU_DEFAULT_NALU_LENGTH_SIZE
+ nalu_length
;
2660 static inline void h264_get_au_internal_end( h264_importer_t
*h264_imp
, h264_access_unit_t
*au
)
2662 if( lsmash_bs_is_end( h264_imp
->bs
, 0 ) && (au
->incomplete_length
== 0) )
2663 h264_imp
->status
= IMPORTER_EOF
;
2664 else if( h264_imp
->status
!= IMPORTER_CHANGE
)
2665 h264_imp
->status
= IMPORTER_OK
;
2668 static int h264_get_au_internal_succeeded( h264_importer_t
*h264_imp
, h264_access_unit_t
*au
)
2670 h264_get_au_internal_end( h264_imp
, au
);
2675 static int h264_get_au_internal_failed( h264_importer_t
*h264_imp
, h264_access_unit_t
*au
, int complete_au
)
2677 h264_get_au_internal_end( h264_imp
, au
);
2683 static lsmash_video_summary_t
*h264_create_summary
2685 lsmash_h264_specific_parameters_t
*param
,
2687 uint32_t max_au_length
2690 lsmash_video_summary_t
*summary
= (lsmash_video_summary_t
*)lsmash_create_summary( LSMASH_SUMMARY_TYPE_VIDEO
);
2693 /* Update summary here.
2694 * max_au_length is set at the last of mp4sys_h264_probe function. */
2695 lsmash_codec_specific_t
*cs
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264
,
2696 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
);
2697 cs
->data
.unstructured
= lsmash_create_h264_specific_info( param
, &cs
->size
);
2698 if( !cs
->data
.unstructured
2699 || lsmash_add_entry( &summary
->opaque
->list
, cs
) < 0 )
2701 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
2702 lsmash_destroy_codec_specific_data( cs
);
2705 summary
->sample_type
= ISOM_CODEC_TYPE_AVC1_VIDEO
;
2706 summary
->max_au_length
= max_au_length
;
2707 summary
->timescale
= sps
->vui
.time_scale
;
2708 summary
->timebase
= sps
->vui
.num_units_in_tick
;
2709 summary
->vfr
= !sps
->vui
.fixed_frame_rate_flag
;
2710 summary
->sample_per_field
= 0;
2711 summary
->width
= sps
->cropped_width
;
2712 summary
->height
= sps
->cropped_height
;
2713 summary
->par_h
= sps
->vui
.sar_width
;
2714 summary
->par_v
= sps
->vui
.sar_height
;
2715 summary
->color
.primaries_index
= sps
->vui
.colour_primaries
;
2716 summary
->color
.transfer_index
= sps
->vui
.transfer_characteristics
;
2717 summary
->color
.matrix_index
= sps
->vui
.matrix_coefficients
;
2718 summary
->color
.full_range
= sps
->vui
.video_full_range_flag
;
2722 static int h264_store_codec_specific
2724 h264_importer_t
*h264_imp
,
2725 lsmash_h264_specific_parameters_t
*avcC_param
2728 lsmash_codec_specific_t
*src_cs
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264
,
2729 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
2732 lsmash_h264_specific_parameters_t
*src_param
= (lsmash_h264_specific_parameters_t
*)src_cs
->data
.structured
;
2733 *src_param
= *avcC_param
;
2734 lsmash_codec_specific_t
*dst_cs
= lsmash_convert_codec_specific_format( src_cs
, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
2735 src_param
->parameter_sets
= NULL
; /* Avoid freeing parameter sets within avcC_param. */
2736 lsmash_destroy_codec_specific_data( src_cs
);
2737 if( !dst_cs
|| lsmash_add_entry( h264_imp
->avcC_list
, dst_cs
) < 0 )
2739 lsmash_destroy_codec_specific_data( dst_cs
);
2745 static inline void h264_new_access_unit
2747 h264_access_unit_t
*au
2751 au
->picture
.type
= H264_PICTURE_TYPE_NONE
;
2752 au
->picture
.random_accessible
= 0;
2753 au
->picture
.recovery_frame_cnt
= 0;
2754 au
->picture
.has_mmco5
= 0;
2755 au
->picture
.has_redundancy
= 0;
2756 au
->picture
.broken_link_flag
= 0;
2759 /* If probe equals 0, don't get the actual data (EBPS) of an access unit and only parse NALU.
2760 * Currently, you can get AU of AVC video elemental stream only, not AVC parameter set elemental stream defined in 14496-15. */
2761 static int h264_get_access_unit_internal
2763 importer_t
*importer
,
2767 h264_importer_t
*h264_imp
= (h264_importer_t
*)importer
->info
;
2768 h264_info_t
*info
= &h264_imp
->info
;
2769 h264_slice_info_t
*slice
= &info
->slice
;
2770 h264_access_unit_t
*au
= &info
->au
;
2771 h264_picture_info_t
*picture
= &au
->picture
;
2772 h264_stream_buffer_t
*sb
= &info
->buffer
;
2773 lsmash_bs_t
*bs
= h264_imp
->bs
;
2774 int complete_au
= 0;
2775 h264_new_access_unit( au
);
2778 h264_nalu_header_t nuh
;
2779 uint64_t start_code_length
;
2780 uint64_t trailing_zero_bytes
;
2781 uint64_t nalu_length
= h264_find_next_start_code( bs
, &nuh
, &start_code_length
, &trailing_zero_bytes
);
2782 if( start_code_length
<= NALU_SHORT_START_CODE_LENGTH
&& lsmash_bs_is_end( bs
, nalu_length
) )
2784 /* For the last NALU.
2785 * This NALU already has been appended into the latest access unit and parsed. */
2786 h264_update_picture_info( info
, picture
, slice
, &info
->sei
);
2787 complete_au
= h264_complete_au( au
, probe
);
2789 return h264_get_au_internal_succeeded( h264_imp
, au
);
2791 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
);
2793 uint8_t nalu_type
= nuh
.nal_unit_type
;
2794 uint64_t next_sc_head_pos
= h264_imp
->sc_head_pos
2797 + trailing_zero_bytes
;
2801 fprintf( stderr
, "NALU type: %"PRIu8
" \n", nalu_type
);
2802 fprintf( stderr
, " NALU header position: %"PRIx64
" \n", h264_imp
->sc_head_pos
+ start_code_length
);
2803 fprintf( stderr
, " EBSP position: %"PRIx64
" \n", h264_imp
->sc_head_pos
+ start_code_length
+ nuh
.length
);
2804 fprintf( stderr
, " EBSP length: %"PRIx64
" (%"PRIu64
") \n", nalu_length
- nuh
.length
, nalu_length
- nuh
.length
);
2805 fprintf( stderr
, " trailing_zero_bytes: %"PRIx64
" \n", trailing_zero_bytes
);
2806 fprintf( stderr
, " Next start code position: %"PRIx64
"\n", next_sc_head_pos
);
2809 if( nalu_type
== H264_NALU_TYPE_FD
)
2811 /* We don't support streams with both filler and HRD yet.
2812 * Otherwise, just skip filler because elemental streams defined in 14496-15 are forbidden to use filler. */
2813 if( info
->sps
.vui
.hrd
.present
)
2814 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
);
2816 else if( (nalu_type
>= H264_NALU_TYPE_SLICE_N_IDR
&& nalu_type
<= H264_NALU_TYPE_SPS_EXT
)
2817 || nalu_type
== H264_NALU_TYPE_SLICE_AUX
)
2819 /* Increase the buffer if needed. */
2820 uint64_t possible_au_length
= au
->incomplete_length
+ NALU_DEFAULT_NALU_LENGTH_SIZE
+ nalu_length
;
2821 if( sb
->bank
->buffer_size
< possible_au_length
2822 && h264_supplement_buffer( sb
, au
, 2 * possible_au_length
) < 0 )
2824 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to increase the buffer size.\n" );
2825 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
);
2827 /* Get the EBSP of the current NALU here.
2828 * AVC elemental stream defined in 14496-15 can recognizes from 0 to 13, and 19 of nal_unit_type.
2829 * We don't support SVC and MVC elemental stream defined in 14496-15 yet. */
2830 uint8_t *nalu
= lsmash_bs_get_buffer_data( bs
) + start_code_length
;
2831 if( nalu_type
>= H264_NALU_TYPE_SLICE_N_IDR
&& nalu_type
<= H264_NALU_TYPE_SLICE_IDR
)
2833 /* VCL NALU (slice) */
2834 h264_slice_info_t prev_slice
= *slice
;
2835 if( h264_parse_slice( info
, &nuh
, sb
->rbsp
, nalu
+ nuh
.length
, nalu_length
- nuh
.length
) )
2836 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
);
2837 if( probe
&& info
->avcC_pending
)
2839 /* Copy and append a Codec Specific info. */
2840 if( h264_store_codec_specific( h264_imp
, &info
->avcC_param
) < 0 )
2843 if( h264_move_pending_avcC_param( info
) < 0 )
2845 if( prev_slice
.present
)
2847 /* Check whether the AU that contains the previous VCL NALU completed or not. */
2848 if( h264_find_au_delimit_by_slice_info( slice
, &prev_slice
) )
2850 /* The current NALU is the first VCL NALU of the primary coded picture of an new AU.
2851 * Therefore, the previous slice belongs to the AU you want at this time. */
2852 h264_update_picture_info( info
, picture
, &prev_slice
, &info
->sei
);
2853 complete_au
= h264_complete_au( au
, probe
);
2856 h264_update_picture_info_for_slice( info
, picture
, &prev_slice
);
2858 h264_append_nalu_to_au( au
, nalu
, nalu_length
, probe
);
2863 if( h264_find_au_delimit_by_nalu_type( nalu_type
, info
->prev_nalu_type
) )
2865 /* The last slice belongs to the AU you want at this time. */
2866 h264_update_picture_info( info
, picture
, slice
, &info
->sei
);
2867 complete_au
= h264_complete_au( au
, probe
);
2871 case H264_NALU_TYPE_SEI
:
2873 if( h264_parse_sei( info
->bits
, &info
->sps
, &info
->sei
, sb
->rbsp
, nalu
+ nuh
.length
, nalu_length
- nuh
.length
) < 0 )
2874 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
);
2875 h264_append_nalu_to_au( au
, nalu
, nalu_length
, probe
);
2878 case H264_NALU_TYPE_SPS
:
2879 if( h264_try_to_append_parameter_set( info
, H264_PARAMETER_SET_TYPE_SPS
, nalu
, nalu_length
) < 0 )
2880 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
);
2882 case H264_NALU_TYPE_PPS
:
2883 if( h264_try_to_append_parameter_set( info
, H264_PARAMETER_SET_TYPE_PPS
, nalu
, nalu_length
) < 0 )
2884 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
);
2886 case H264_NALU_TYPE_AUD
: /* We drop access unit delimiters. */
2888 case H264_NALU_TYPE_SPS_EXT
:
2889 if( h264_try_to_append_parameter_set( info
, H264_PARAMETER_SET_TYPE_SPSEXT
, nalu
, nalu_length
) < 0 )
2890 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
);
2893 h264_append_nalu_to_au( au
, nalu
, nalu_length
, probe
);
2896 if( info
->avcC_pending
)
2897 h264_imp
->status
= IMPORTER_CHANGE
;
2900 /* Move to the first byte of the next start code. */
2901 info
->prev_nalu_type
= nalu_type
;
2902 if( lsmash_bs_read_seek( bs
, next_sc_head_pos
, SEEK_SET
) != next_sc_head_pos
)
2904 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to seek the next start code.\n" );
2905 return h264_get_au_internal_failed( h264_imp
, au
, complete_au
);
2907 /* Check if no more data to read from the stream. */
2908 if( !lsmash_bs_is_end( bs
, NALU_SHORT_START_CODE_LENGTH
) )
2909 h264_imp
->sc_head_pos
= next_sc_head_pos
;
2910 /* If there is no more data in the stream, and flushed chunk of NALUs, flush it as complete AU here. */
2911 else if( au
->incomplete_length
&& au
->length
== 0 )
2913 h264_update_picture_info( info
, picture
, slice
, &info
->sei
);
2914 h264_complete_au( au
, probe
);
2915 return h264_get_au_internal_succeeded( h264_imp
, au
);
2918 return h264_get_au_internal_succeeded( h264_imp
, au
);
2922 static int h264_importer_get_accessunit
2924 importer_t
*importer
,
2925 uint32_t track_number
,
2926 lsmash_sample_t
*buffered_sample
2929 debug_if( !importer
|| !importer
->info
|| !buffered_sample
->data
|| !buffered_sample
->length
)
2931 if( !importer
->info
|| track_number
!= 1 )
2933 h264_importer_t
*h264_imp
= (h264_importer_t
*)importer
->info
;
2934 h264_info_t
*info
= &h264_imp
->info
;
2935 importer_status current_status
= h264_imp
->status
;
2936 if( current_status
== IMPORTER_ERROR
|| buffered_sample
->length
< h264_imp
->max_au_length
)
2938 if( current_status
== IMPORTER_EOF
)
2940 buffered_sample
->length
= 0;
2943 if( h264_get_access_unit_internal( importer
, 0 ) < 0 )
2945 h264_imp
->status
= IMPORTER_ERROR
;
2948 if( h264_imp
->status
== IMPORTER_CHANGE
&& !info
->avcC_pending
)
2949 current_status
= IMPORTER_CHANGE
;
2950 if( current_status
== IMPORTER_CHANGE
)
2952 /* Update the active summary. */
2953 lsmash_codec_specific_t
*cs
= (lsmash_codec_specific_t
*)lsmash_get_entry_data( h264_imp
->avcC_list
, ++ h264_imp
->avcC_number
);
2956 lsmash_h264_specific_parameters_t
*avcC_param
= (lsmash_h264_specific_parameters_t
*)cs
->data
.structured
;
2957 lsmash_video_summary_t
*summary
= h264_create_summary( avcC_param
, &info
->sps
, h264_imp
->max_au_length
);
2960 lsmash_remove_entry( importer
->summaries
, track_number
, lsmash_cleanup_summary
);
2961 if( lsmash_add_entry( importer
->summaries
, summary
) < 0 )
2963 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
2966 h264_imp
->status
= IMPORTER_OK
;
2968 h264_access_unit_t
*au
= &info
->au
;
2969 h264_picture_info_t
*picture
= &au
->picture
;
2970 buffered_sample
->dts
= h264_imp
->ts_list
.timestamp
[ au
->number
- 1 ].dts
;
2971 buffered_sample
->cts
= h264_imp
->ts_list
.timestamp
[ au
->number
- 1 ].cts
;
2972 if( au
->number
< h264_imp
->num_undecodable
)
2973 buffered_sample
->prop
.leading
= ISOM_SAMPLE_IS_UNDECODABLE_LEADING
;
2975 buffered_sample
->prop
.leading
= picture
->independent
|| buffered_sample
->cts
>= h264_imp
->last_intra_cts
2976 ? ISOM_SAMPLE_IS_NOT_LEADING
: ISOM_SAMPLE_IS_UNDECODABLE_LEADING
;
2977 if( picture
->independent
)
2978 h264_imp
->last_intra_cts
= buffered_sample
->cts
;
2979 if( h264_imp
->composition_reordering_present
&& !picture
->disposable
&& !picture
->idr
)
2980 buffered_sample
->prop
.allow_earlier
= QT_SAMPLE_EARLIER_PTS_ALLOWED
;
2981 buffered_sample
->prop
.independent
= picture
->independent
? ISOM_SAMPLE_IS_INDEPENDENT
: ISOM_SAMPLE_IS_NOT_INDEPENDENT
;
2982 buffered_sample
->prop
.disposable
= picture
->disposable
? ISOM_SAMPLE_IS_DISPOSABLE
: ISOM_SAMPLE_IS_NOT_DISPOSABLE
;
2983 buffered_sample
->prop
.redundant
= picture
->has_redundancy
? ISOM_SAMPLE_HAS_REDUNDANCY
: ISOM_SAMPLE_HAS_NO_REDUNDANCY
;
2984 buffered_sample
->prop
.post_roll
.identifier
= picture
->frame_num
;
2985 if( picture
->random_accessible
)
2988 buffered_sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
2989 else if( picture
->recovery_frame_cnt
)
2991 buffered_sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_POST_ROLL_START
;
2992 buffered_sample
->prop
.post_roll
.complete
= (picture
->frame_num
+ picture
->recovery_frame_cnt
) % info
->sps
.MaxFrameNum
;
2996 buffered_sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP
;
2997 if( !picture
->broken_link_flag
)
2998 buffered_sample
->prop
.ra_flags
|= QT_SAMPLE_RANDOM_ACCESS_FLAG_PARTIAL_SYNC
;
3001 buffered_sample
->length
= au
->length
;
3002 memcpy( buffered_sample
->data
, au
->data
, au
->length
);
3003 return current_status
;
3006 static void nalu_deduplicate_poc
3008 nal_pic_timing_t
*npt
,
3009 uint32_t *max_composition_delay
,
3010 uint32_t num_access_units
,
3011 uint32_t max_num_reorder_pics
3014 /* Deduplicate POCs. */
3015 int64_t poc_offset
= 0;
3016 int64_t poc_min
= 0;
3017 int64_t invalid_poc_min
= 0;
3018 uint32_t last_poc_reset
= UINT32_MAX
;
3019 uint32_t invalid_poc_start
= 0;
3020 int invalid_poc_present
= 0;
3021 for( uint32_t i
= 0; ; i
++ )
3023 if( i
< num_access_units
&& npt
[i
].poc
!= 0 && !npt
[i
].reset
)
3025 /* poc_offset is not added to each POC here.
3026 * It is done when we encounter the next coded video sequence. */
3027 if( npt
[i
].poc
< 0 )
3029 /* Pictures with negative POC shall precede IDR-picture in composition order.
3030 * The minimum POC is added to poc_offset when we encounter the next coded video sequence. */
3031 if( last_poc_reset
== UINT32_MAX
|| i
> last_poc_reset
+ max_num_reorder_pics
)
3033 if( !invalid_poc_present
)
3035 invalid_poc_present
= 1;
3036 invalid_poc_start
= i
;
3038 if( invalid_poc_min
> npt
[i
].poc
)
3039 invalid_poc_min
= npt
[i
].poc
;
3041 else if( poc_min
> npt
[i
].poc
)
3043 poc_min
= npt
[i
].poc
;
3044 *max_composition_delay
= LSMASH_MAX( *max_composition_delay
, i
- last_poc_reset
);
3049 /* Encountered a new coded video sequence or no more POCs.
3050 * Add poc_offset to each POC of the previous coded video sequence. */
3051 poc_offset
-= poc_min
;
3052 int64_t poc_max
= 0;
3053 for( uint32_t j
= last_poc_reset
; j
< i
+ !!npt
[i
].reset
; j
++ )
3054 if( npt
[j
].poc
>= 0 || (j
<= last_poc_reset
+ max_num_reorder_pics
) )
3056 npt
[j
].poc
+= poc_offset
;
3057 if( poc_max
< npt
[j
].poc
)
3058 poc_max
= npt
[j
].poc
;
3060 poc_offset
= poc_max
+ 1;
3061 if( invalid_poc_present
)
3063 /* Pictures with invalid negative POC is probably supposed to be composited
3064 * both before the next coded video sequence and after the current one. */
3065 poc_offset
-= invalid_poc_min
;
3066 for( uint32_t j
= invalid_poc_start
; j
< i
+ !!npt
[i
].reset
; j
++ )
3067 if( npt
[j
].poc
< 0 )
3069 npt
[j
].poc
+= poc_offset
;
3070 if( poc_max
< npt
[j
].poc
)
3071 poc_max
= npt
[j
].poc
;
3073 invalid_poc_present
= 0;
3074 invalid_poc_start
= 0;
3075 invalid_poc_min
= 0;
3076 poc_offset
= poc_max
+ 1;
3078 if( i
< num_access_units
)
3086 break; /* no more POCs */
3090 static void nalu_generate_timestamps_from_poc
3092 importer_t
*importer
,
3093 lsmash_media_ts_t
*timestamp
,
3094 nal_pic_timing_t
*npt
,
3095 uint8_t *composition_reordering_present
,
3096 uint32_t *last_delta
,
3097 uint32_t max_composition_delay
,
3098 uint32_t num_access_units
3101 /* Check if composition delay derived from reordering is present. */
3102 if( max_composition_delay
== 0 )
3104 for( uint32_t i
= 1; i
< num_access_units
; i
++ )
3105 if( npt
[i
].poc
< npt
[i
- 1].poc
)
3107 *composition_reordering_present
= 1;
3112 *composition_reordering_present
= 1;
3113 /* Generate timestamps. */
3114 if( *composition_reordering_present
)
3116 /* Generate timestamps.
3117 * Here, DTSs and CTSs are temporary values for sort. */
3118 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
3120 timestamp
[i
].cts
= (uint64_t)npt
[i
].poc
;
3121 timestamp
[i
].dts
= (uint64_t)i
;
3123 qsort( timestamp
, num_access_units
, sizeof(lsmash_media_ts_t
), (int(*)( const void *, const void * ))lsmash_compare_cts
);
3124 /* Check POC gap in output order. */
3125 lsmash_class_t
*logger
= &(lsmash_class_t
){ .name
= importer
->class->name
};
3126 for( uint32_t i
= 1; i
< num_access_units
; i
++ )
3127 if( timestamp
[i
].cts
> timestamp
[i
- 1].cts
+ npt
[i
- 1].poc_delta
)
3128 lsmash_log( &logger
, LSMASH_LOG_WARNING
,
3129 "POC gap is detected at picture %"PRIu64
". Maybe some pictures are lost.\n", timestamp
[i
].dts
);
3130 /* Get the maximum composition delay derived from reordering. */
3131 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
3132 if( i
< timestamp
[i
].dts
)
3134 uint32_t composition_delay
= timestamp
[i
].dts
- i
;
3135 max_composition_delay
= LSMASH_MAX( max_composition_delay
, composition_delay
);
3137 uint64_t *ts_buffer
= (uint64_t *)lsmash_malloc( (num_access_units
+ max_composition_delay
) * sizeof(uint64_t) );
3140 /* It seems that there is no enough memory to generate more appropriate timestamps.
3141 * Anyway, generate CTSs and DTSs. */
3142 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
3143 timestamp
[i
].cts
= i
+ max_composition_delay
;
3144 qsort( timestamp
, num_access_units
, sizeof(lsmash_media_ts_t
), (int(*)( const void *, const void * ))lsmash_compare_dts
);
3148 uint64_t *reorder_cts
= ts_buffer
;
3149 uint64_t *prev_reorder_cts
= ts_buffer
+ num_access_units
;
3150 *last_delta
= npt
[num_access_units
- 1].delta
;
3151 /* Generate CTSs. */
3152 timestamp
[0].cts
= 0;
3153 for( uint32_t i
= 1; i
< num_access_units
; i
++ )
3154 timestamp
[i
].cts
= timestamp
[i
- 1].cts
+ npt
[i
- 1].delta
;
3155 int64_t composition_delay_time
= timestamp
[max_composition_delay
].cts
;
3156 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
3158 timestamp
[i
].cts
+= composition_delay_time
;
3159 reorder_cts
[i
] = timestamp
[i
].cts
;
3161 /* Generate DTSs. */
3162 qsort( timestamp
, num_access_units
, sizeof(lsmash_media_ts_t
), (int(*)( const void *, const void * ))lsmash_compare_dts
);
3163 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
3165 timestamp
[i
].dts
= i
<= max_composition_delay
3166 ? reorder_cts
[i
] - composition_delay_time
3167 : prev_reorder_cts
[(i
- max_composition_delay
) % max_composition_delay
];
3168 prev_reorder_cts
[i
% max_composition_delay
] = reorder_cts
[i
];
3170 lsmash_free( ts_buffer
);
3172 fprintf( stderr
, "max_composition_delay=%"PRIu32
", composition_delay_time=%"PRIu64
"\n",
3173 max_composition_delay
, composition_delay_time
);
3178 timestamp
[0].dts
= 0;
3179 timestamp
[0].cts
= 0;
3180 for( uint32_t i
= 1; i
< num_access_units
; i
++ )
3182 timestamp
[i
].dts
= timestamp
[i
- 1].dts
+ npt
[i
- 1].delta
;
3183 timestamp
[i
].cts
= timestamp
[i
- 1].cts
+ npt
[i
- 1].delta
;
3185 *last_delta
= npt
[num_access_units
- 1].delta
;
3189 static void nalu_reduce_timescale
3191 lsmash_media_ts_t
*timestamp
,
3192 nal_pic_timing_t
*npt
,
3193 uint32_t *last_delta
,
3194 uint32_t *timescale
,
3195 uint32_t num_access_units
3198 uint64_t gcd_delta
= *timescale
;
3199 for( uint32_t i
= 0; i
< num_access_units
&& gcd_delta
> 1; i
++ )
3200 gcd_delta
= lsmash_get_gcd( gcd_delta
, npt
[i
].delta
);
3203 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
3205 timestamp
[i
].dts
/= gcd_delta
;
3206 timestamp
[i
].cts
/= gcd_delta
;
3208 *last_delta
/= gcd_delta
;
3209 *timescale
/= gcd_delta
;
3212 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
3213 fprintf( stderr
, "Timestamp[%"PRIu32
"]: POC=%"PRId64
", DTS=%"PRIu64
", CTS=%"PRIu64
"\n",
3214 i
, npt
[i
].poc
, timestamp
[i
].dts
, timestamp
[i
].cts
);
3218 static lsmash_video_summary_t
*h264_setup_first_summary
3220 importer_t
*importer
3223 h264_importer_t
*h264_imp
= (h264_importer_t
*)importer
->info
;
3224 lsmash_codec_specific_t
*cs
= (lsmash_codec_specific_t
*)lsmash_get_entry_data( h264_imp
->avcC_list
, ++ h264_imp
->avcC_number
);
3225 if( !cs
|| !cs
->data
.structured
)
3227 lsmash_destroy_codec_specific_data( cs
);
3230 lsmash_video_summary_t
*summary
= h264_create_summary( (lsmash_h264_specific_parameters_t
*)cs
->data
.structured
,
3231 &h264_imp
->info
.sps
, h264_imp
->max_au_length
);
3234 lsmash_destroy_codec_specific_data( cs
);
3237 if( lsmash_add_entry( importer
->summaries
, summary
) < 0 )
3239 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
3242 summary
->sample_per_field
= h264_imp
->field_pic_present
;
3246 static int h264_analyze_whole_stream
3248 importer_t
*importer
3251 /* Parse all NALU in the stream for preparation of calculating timestamps. */
3252 uint32_t npt_alloc
= (1 << 12) * sizeof(nal_pic_timing_t
);
3253 nal_pic_timing_t
*npt
= lsmash_malloc( npt_alloc
);
3256 uint32_t picture_stats
[H264_PICTURE_TYPE_NONE
+ 1] = { 0 };
3257 uint32_t num_access_units
= 0;
3258 lsmash_class_t
*logger
= &(lsmash_class_t
){ "H.264" };
3259 lsmash_log( &logger
, LSMASH_LOG_INFO
, "Analyzing stream as H.264\r" );
3260 h264_importer_t
*h264_imp
= (h264_importer_t
*)importer
->info
;
3261 h264_info_t
*info
= &h264_imp
->info
;
3262 h264_imp
->status
= IMPORTER_OK
;
3263 while( h264_imp
->status
!= IMPORTER_EOF
)
3266 lsmash_log( &logger
, LSMASH_LOG_INFO
, "Analyzing stream as H.264: %"PRIu32
"\n", num_access_units
+ 1 );
3268 h264_picture_info_t
*picture
= &info
->au
.picture
;
3269 h264_picture_info_t prev_picture
= *picture
;
3270 if( h264_get_access_unit_internal( importer
, 1 ) < 0
3271 || h264_calculate_poc( info
, picture
, &prev_picture
) < 0 )
3273 if( npt_alloc
<= num_access_units
* sizeof(nal_pic_timing_t
) )
3275 uint32_t alloc
= 2 * num_access_units
* sizeof(nal_pic_timing_t
);
3276 nal_pic_timing_t
*temp
= (nal_pic_timing_t
*)lsmash_realloc( npt
, alloc
);
3282 h264_imp
->field_pic_present
|= picture
->field_pic_flag
;
3283 npt
[num_access_units
].poc
= picture
->PicOrderCnt
;
3284 npt
[num_access_units
].delta
= picture
->delta
;
3285 npt
[num_access_units
].poc_delta
= picture
->field_pic_flag
? 1 : 2;
3286 npt
[num_access_units
].reset
= picture
->has_mmco5
;
3288 h264_imp
->max_au_length
= LSMASH_MAX( info
->au
.length
, h264_imp
->max_au_length
);
3290 ++picture_stats
[H264_PICTURE_TYPE_IDR
];
3291 else if( picture
->type
>= H264_PICTURE_TYPE_NONE
)
3292 ++picture_stats
[H264_PICTURE_TYPE_NONE
];
3294 ++picture_stats
[ picture
->type
];
3296 lsmash_log_refresh_line( &logger
);
3297 lsmash_log( &logger
, LSMASH_LOG_INFO
,
3298 "IDR: %"PRIu32
", I: %"PRIu32
", P: %"PRIu32
", B: %"PRIu32
", "
3299 "SI: %"PRIu32
", SP: %"PRIu32
", Unknown: %"PRIu32
"\n",
3300 picture_stats
[H264_PICTURE_TYPE_IDR
],
3301 picture_stats
[H264_PICTURE_TYPE_I
],
3302 picture_stats
[H264_PICTURE_TYPE_I_P
],
3303 picture_stats
[H264_PICTURE_TYPE_I_P_B
],
3304 picture_stats
[H264_PICTURE_TYPE_SI
]
3305 + picture_stats
[H264_PICTURE_TYPE_I_SI
],
3306 picture_stats
[H264_PICTURE_TYPE_SI_SP
]
3307 + picture_stats
[H264_PICTURE_TYPE_I_SI_P_SP
]
3308 + picture_stats
[H264_PICTURE_TYPE_I_SI_P_SP_B
],
3309 picture_stats
[H264_PICTURE_TYPE_NONE
] );
3310 /* Copy and append the last Codec Specific info. */
3311 if( h264_store_codec_specific( h264_imp
, &info
->avcC_param
) < 0 )
3313 /* Set up the first summary. */
3314 lsmash_video_summary_t
*summary
= h264_setup_first_summary( importer
);
3317 /* Allocate timestamps. */
3318 lsmash_media_ts_t
*timestamp
= lsmash_malloc( num_access_units
* sizeof(lsmash_media_ts_t
) );
3321 /* Count leading samples that are undecodable. */
3322 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
3324 if( npt
[i
].poc
== 0 )
3326 ++ h264_imp
->num_undecodable
;
3328 /* Deduplicate POCs. */
3329 uint32_t max_composition_delay
= 0;
3330 nalu_deduplicate_poc( npt
, &max_composition_delay
, num_access_units
, 32 );
3331 /* Generate timestamps. */
3332 nalu_generate_timestamps_from_poc( importer
, timestamp
, npt
,
3333 &h264_imp
->composition_reordering_present
,
3334 &h264_imp
->last_delta
,
3335 max_composition_delay
, num_access_units
);
3336 nalu_reduce_timescale( timestamp
, npt
, &h264_imp
->last_delta
, &summary
->timescale
, num_access_units
);
3338 h264_imp
->ts_list
.sample_count
= num_access_units
;
3339 h264_imp
->ts_list
.timestamp
= timestamp
;
3342 lsmash_log_refresh_line( &logger
);
3347 static int h264_importer_probe( importer_t
*importer
)
3349 /* Find the first start code. */
3350 h264_importer_t
*h264_imp
= create_h264_importer( importer
);
3353 lsmash_bs_t
*bs
= h264_imp
->bs
;
3354 uint64_t first_sc_head_pos
= nalu_find_first_start_code( bs
);
3355 if( first_sc_head_pos
== NALU_NO_START_CODE_FOUND
)
3357 /* OK. It seems the stream has a long start code of H.264. */
3358 importer
->info
= h264_imp
;
3359 h264_info_t
*info
= &h264_imp
->info
;
3360 lsmash_bs_read_seek( bs
, first_sc_head_pos
, SEEK_SET
);
3361 h264_imp
->sc_head_pos
= first_sc_head_pos
;
3362 if( h264_analyze_whole_stream( importer
) < 0 )
3364 /* Go back to the start code of the first NALU. */
3365 h264_imp
->status
= IMPORTER_OK
;
3366 lsmash_bs_read_seek( bs
, first_sc_head_pos
, SEEK_SET
);
3367 h264_imp
->sc_head_pos
= first_sc_head_pos
;
3368 info
->prev_nalu_type
= H264_NALU_TYPE_UNSPECIFIED0
;
3369 uint8_t *temp_au
= info
->au
.data
;
3370 uint8_t *temp_incomplete_au
= info
->au
.incomplete_data
;
3371 memset( &info
->au
, 0, sizeof(h264_access_unit_t
) );
3372 info
->au
.data
= temp_au
;
3373 info
->au
.incomplete_data
= temp_incomplete_au
;
3374 memset( &info
->slice
, 0, sizeof(h264_slice_info_t
) );
3375 memset( &info
->sps
, 0, sizeof(h264_sps_t
) );
3376 memset( &info
->pps
, 0, sizeof(h264_pps_t
) );
3377 lsmash_remove_entries( info
->avcC_param
.parameter_sets
->sps_list
, isom_remove_dcr_ps
);
3378 lsmash_remove_entries( info
->avcC_param
.parameter_sets
->pps_list
, isom_remove_dcr_ps
);
3379 lsmash_remove_entries( info
->avcC_param
.parameter_sets
->spsext_list
, isom_remove_dcr_ps
);
3380 lsmash_destroy_h264_parameter_sets( &info
->avcC_param_next
);
3383 remove_h264_importer( h264_imp
);
3384 importer
->info
= NULL
;
3385 lsmash_remove_entries( importer
->summaries
, lsmash_cleanup_summary
);
3389 static uint32_t h264_importer_get_last_delta( importer_t
*importer
, uint32_t track_number
)
3391 debug_if( !importer
|| !importer
->info
)
3393 h264_importer_t
*h264_imp
= (h264_importer_t
*)importer
->info
;
3394 if( !h264_imp
|| track_number
!= 1 || h264_imp
->status
!= IMPORTER_EOF
)
3396 return h264_imp
->ts_list
.sample_count
3397 ? h264_imp
->last_delta
3398 : UINT32_MAX
; /* arbitrary */
3401 static const importer_functions h264_importer
=
3403 { "H.264", offsetof( importer_t
, log_level
) },
3405 h264_importer_probe
,
3406 h264_importer_get_accessunit
,
3407 h264_importer_get_last_delta
,
3408 h264_importer_cleanup
3411 /***************************************************************************
3413 ITU-T Recommendation H.265 (04/13)
3414 ISO/IEC 14496-15:2014
3415 ***************************************************************************/
3416 #include "codecs/hevc.h"
3420 importer_status status
;
3422 lsmash_entry_list_t hvcC_list
[1]; /* stored as lsmash_codec_specific_t */
3423 lsmash_media_ts_list_t ts_list
;
3425 uint32_t max_au_length
;
3426 uint32_t num_undecodable
;
3427 uint32_t hvcC_number
;
3428 uint32_t last_delta
;
3429 uint64_t last_intra_cts
;
3430 uint64_t sc_head_pos
;
3431 uint8_t composition_reordering_present
;
3432 uint8_t field_pic_present
;
3433 uint8_t max_TemporalId
;
3436 static void remove_hevc_importer( hevc_importer_t
*hevc_imp
)
3440 lsmash_remove_entries( hevc_imp
->hvcC_list
, lsmash_destroy_codec_specific_data
);
3441 hevc_cleanup_parser( &hevc_imp
->info
);
3442 lsmash_bs_cleanup( hevc_imp
->bs
);
3443 lsmash_free( hevc_imp
->ts_list
.timestamp
);
3444 lsmash_free( hevc_imp
);
3447 static void hevc_importer_cleanup( importer_t
*importer
)
3449 debug_if( importer
&& importer
->info
)
3450 remove_hevc_importer( importer
->info
);
3453 static hevc_importer_t
*create_hevc_importer( importer_t
*importer
)
3455 hevc_importer_t
*hevc_imp
= lsmash_malloc_zero( sizeof(hevc_importer_t
) );
3458 if( hevc_setup_parser( &hevc_imp
->info
, 0 ) < 0 )
3460 remove_hevc_importer( hevc_imp
);
3463 lsmash_bs_t
*bs
= lsmash_bs_create();
3466 remove_hevc_importer( hevc_imp
);
3469 bs
->stream
= importer
->stream
;
3470 bs
->read
= lsmash_fread_wrapper
;
3471 bs
->seek
= lsmash_fseek_wrapper
;
3472 bs
->unseekable
= importer
->is_stdin
;
3473 bs
->buffer
.max_size
= BS_MAX_DEFAULT_READ_SIZE
;
3474 lsmash_init_entry_list( hevc_imp
->hvcC_list
);
3476 hevc_imp
->info
.eos
= 1;
3480 static inline int hevc_complete_au( hevc_access_unit_t
*au
, int probe
)
3482 if( !au
->picture
.has_primary
|| au
->incomplete_length
== 0 )
3485 memcpy( au
->data
, au
->incomplete_data
, au
->incomplete_length
);
3486 au
->TemporalId
= au
->picture
.TemporalId
;
3487 au
->length
= au
->incomplete_length
;
3488 au
->incomplete_length
= 0;
3489 au
->picture
.has_primary
= 0;
3493 static void hevc_append_nalu_to_au( hevc_access_unit_t
*au
, uint8_t *src_nalu
, uint32_t nalu_length
, int probe
)
3497 uint8_t *dst_nalu
= au
->incomplete_data
+ au
->incomplete_length
+ NALU_DEFAULT_NALU_LENGTH_SIZE
;
3498 for( int i
= NALU_DEFAULT_NALU_LENGTH_SIZE
; i
; i
-- )
3499 *(dst_nalu
- i
) = (nalu_length
>> ((i
- 1) * 8)) & 0xff;
3500 memcpy( dst_nalu
, src_nalu
, nalu_length
);
3502 /* Note: picture->incomplete_au_length shall be 0 immediately after AU has completed.
3503 * Therefore, possible_au_length in hevc_get_access_unit_internal() can't be used here
3504 * to avoid increasing AU length monotonously through the entire stream. */
3505 au
->incomplete_length
+= NALU_DEFAULT_NALU_LENGTH_SIZE
+ nalu_length
;
3508 static inline void hevc_get_au_internal_end( hevc_importer_t
*hevc_imp
, hevc_access_unit_t
*au
)
3510 if( lsmash_bs_is_end( hevc_imp
->bs
, 0 ) && (au
->incomplete_length
== 0) )
3511 hevc_imp
->status
= IMPORTER_EOF
;
3512 else if( hevc_imp
->status
!= IMPORTER_CHANGE
)
3513 hevc_imp
->status
= IMPORTER_OK
;
3516 static int hevc_get_au_internal_succeeded( hevc_importer_t
*hevc_imp
, hevc_access_unit_t
*au
)
3518 hevc_get_au_internal_end( hevc_imp
, au
);
3523 static int hevc_get_au_internal_failed( hevc_importer_t
*hevc_imp
, hevc_access_unit_t
*au
, int complete_au
)
3525 hevc_get_au_internal_end( hevc_imp
, au
);
3531 static lsmash_video_summary_t
*hevc_create_summary
3533 lsmash_hevc_specific_parameters_t
*param
,
3535 uint32_t max_au_length
3538 lsmash_video_summary_t
*summary
= (lsmash_video_summary_t
*)lsmash_create_summary( LSMASH_SUMMARY_TYPE_VIDEO
);
3541 /* Update summary here.
3542 * max_au_length is set at the last of hevc_importer_probe function. */
3543 lsmash_codec_specific_t
*specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_HEVC
,
3544 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
);
3545 specific
->data
.unstructured
= lsmash_create_hevc_specific_info( param
, &specific
->size
);
3546 if( !specific
->data
.unstructured
3547 || lsmash_add_entry( &summary
->opaque
->list
, specific
) < 0 )
3549 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
3550 lsmash_destroy_codec_specific_data( specific
);
3553 summary
->sample_type
= ISOM_CODEC_TYPE_HVC1_VIDEO
;
3554 summary
->max_au_length
= max_au_length
;
3555 summary
->timescale
= sps
->vui
.time_scale
;
3556 summary
->timebase
= sps
->vui
.num_units_in_tick
;
3557 summary
->vfr
= (param
->constantFrameRate
== 0);
3558 summary
->sample_per_field
= 0;
3559 summary
->width
= sps
->cropped_width
;
3560 summary
->height
= sps
->cropped_height
;
3561 summary
->par_h
= sps
->vui
.sar_width
;
3562 summary
->par_v
= sps
->vui
.sar_height
;
3563 summary
->color
.primaries_index
= sps
->vui
.colour_primaries
!= 2 ? sps
->vui
.colour_primaries
: 0;
3564 summary
->color
.transfer_index
= sps
->vui
.transfer_characteristics
!= 2 ? sps
->vui
.transfer_characteristics
: 0;
3565 summary
->color
.matrix_index
= sps
->vui
.matrix_coeffs
!= 2 ? sps
->vui
.matrix_coeffs
: 0;
3566 summary
->color
.full_range
= sps
->vui
.video_full_range_flag
;
3567 lsmash_convert_crop_into_clap( sps
->vui
.def_disp_win_offset
, summary
->width
, summary
->height
, &summary
->clap
);
3571 static int hevc_store_codec_specific
3573 hevc_importer_t
*hevc_imp
,
3574 lsmash_hevc_specific_parameters_t
*hvcC_param
3577 lsmash_codec_specific_t
*src_cs
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_HEVC
,
3578 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
3581 lsmash_hevc_specific_parameters_t
*src_param
= (lsmash_hevc_specific_parameters_t
*)src_cs
->data
.structured
;
3582 *src_param
= *hvcC_param
;
3583 lsmash_codec_specific_t
*dst_cs
= lsmash_convert_codec_specific_format( src_cs
, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
);
3584 src_param
->parameter_arrays
= NULL
; /* Avoid freeing parameter arrays within hvcC_param. */
3585 lsmash_destroy_codec_specific_data( src_cs
);
3586 if( !dst_cs
|| lsmash_add_entry( hevc_imp
->hvcC_list
, dst_cs
) < 0 )
3588 lsmash_destroy_codec_specific_data( dst_cs
);
3594 static inline void hevc_new_access_unit( hevc_access_unit_t
*au
)
3597 au
->picture
.type
= HEVC_PICTURE_TYPE_NONE
;
3598 au
->picture
.random_accessible
= 0;
3599 au
->picture
.recovery_poc_cnt
= 0;
3602 /* If probe equals 0, don't get the actual data (EBPS) of an access unit and only parse NALU. */
3603 static int hevc_get_access_unit_internal
3605 importer_t
*importer
,
3609 hevc_importer_t
*hevc_imp
= (hevc_importer_t
*)importer
->info
;
3610 hevc_info_t
*info
= &hevc_imp
->info
;
3611 hevc_slice_info_t
*slice
= &info
->slice
;
3612 hevc_access_unit_t
*au
= &info
->au
;
3613 hevc_picture_info_t
*picture
= &au
->picture
;
3614 hevc_stream_buffer_t
*sb
= &info
->buffer
;
3615 lsmash_bs_t
*bs
= hevc_imp
->bs
;
3616 int complete_au
= 0;
3617 hevc_new_access_unit( au
);
3620 hevc_nalu_header_t nuh
;
3621 uint64_t start_code_length
;
3622 uint64_t trailing_zero_bytes
;
3623 uint64_t nalu_length
= hevc_find_next_start_code( bs
, &nuh
, &start_code_length
, &trailing_zero_bytes
);
3624 if( start_code_length
<= NALU_SHORT_START_CODE_LENGTH
&& lsmash_bs_is_end( bs
, nalu_length
) )
3626 /* For the last NALU.
3627 * This NALU already has been appended into the latest access unit and parsed. */
3628 hevc_update_picture_info( info
, picture
, slice
, &info
->sps
, &info
->sei
);
3629 complete_au
= hevc_complete_au( au
, probe
);
3631 return hevc_get_au_internal_succeeded( hevc_imp
, au
);
3633 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
);
3635 uint8_t nalu_type
= nuh
.nal_unit_type
;
3636 uint64_t next_sc_head_pos
= hevc_imp
->sc_head_pos
3639 + trailing_zero_bytes
;
3643 fprintf( stderr
, "NALU type: %"PRIu8
" \n", nalu_type
);
3644 fprintf( stderr
, " NALU header position: %"PRIx64
" \n", hevc_imp
->sc_head_pos
+ start_code_length
);
3645 fprintf( stderr
, " EBSP position: %"PRIx64
" \n", hevc_imp
->sc_head_pos
+ start_code_length
+ nuh
.length
);
3646 fprintf( stderr
, " EBSP length: %"PRIx64
" (%"PRIu64
") \n", nalu_length
- nuh
.length
, nalu_length
- nuh
.length
);
3647 fprintf( stderr
, " trailing_zero_bytes: %"PRIx64
" \n", trailing_zero_bytes
);
3648 fprintf( stderr
, " Next start code position: %"PRIx64
"\n", next_sc_head_pos
);
3651 /* Check if the end of sequence. Used for POC calculation. */
3652 info
->eos
|= info
->prev_nalu_type
== HEVC_NALU_TYPE_EOS
3653 || info
->prev_nalu_type
== HEVC_NALU_TYPE_EOB
;
3654 /* Process the current NALU by its type. */
3655 if( nalu_type
== HEVC_NALU_TYPE_FD
)
3657 /* We don't support streams with both filler and HRD yet.
3658 * Otherwise, just skip filler because elemental streams defined in 14496-15 are forbidden to use filler. */
3659 if( info
->sps
.vui
.hrd
.present
)
3660 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
);
3662 else if( nalu_type
<= HEVC_NALU_TYPE_RASL_R
3663 || (nalu_type
>= HEVC_NALU_TYPE_BLA_W_LP
&& nalu_type
<= HEVC_NALU_TYPE_CRA
)
3664 || (nalu_type
>= HEVC_NALU_TYPE_VPS
&& nalu_type
<= HEVC_NALU_TYPE_SUFFIX_SEI
) )
3666 /* Increase the buffer if needed. */
3667 uint64_t possible_au_length
= au
->incomplete_length
+ NALU_DEFAULT_NALU_LENGTH_SIZE
+ nalu_length
;
3668 if( sb
->bank
->buffer_size
< possible_au_length
3669 && hevc_supplement_buffer( sb
, au
, 2 * possible_au_length
) < 0 )
3671 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to increase the buffer size.\n" );
3672 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
);
3674 /* Get the EBSP of the current NALU here. */
3675 uint8_t *nalu
= lsmash_bs_get_buffer_data( bs
) + start_code_length
;
3676 if( nalu_type
<= HEVC_NALU_TYPE_RSV_VCL31
)
3678 /* VCL NALU (slice) */
3679 hevc_slice_info_t prev_slice
= *slice
;
3680 if( hevc_parse_slice_segment_header( info
, &nuh
, sb
->rbsp
,
3681 nalu
+ nuh
.length
, nalu_length
- nuh
.length
) )
3682 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
);
3683 if( probe
&& info
->hvcC_pending
)
3685 /* Copy and append a Codec Specific info. */
3686 if( hevc_store_codec_specific( hevc_imp
, &info
->hvcC_param
) < 0 )
3689 if( hevc_move_pending_hvcC_param( info
) < 0 )
3691 if( prev_slice
.present
)
3693 /* Check whether the AU that contains the previous VCL NALU completed or not. */
3694 if( hevc_find_au_delimit_by_slice_info( info
, slice
, &prev_slice
) )
3696 /* The current NALU is the first VCL NALU of the primary coded picture of a new AU.
3697 * Therefore, the previous slice belongs to the AU you want at this time. */
3698 hevc_update_picture_info( info
, picture
, &prev_slice
, &info
->sps
, &info
->sei
);
3699 complete_au
= hevc_complete_au( au
, probe
);
3702 hevc_update_picture_info_for_slice( info
, picture
, &prev_slice
);
3704 hevc_append_nalu_to_au( au
, nalu
, nalu_length
, probe
);
3709 if( hevc_find_au_delimit_by_nalu_type( nalu_type
, info
->prev_nalu_type
) )
3711 /* The last slice belongs to the AU you want at this time. */
3712 hevc_update_picture_info( info
, picture
, slice
, &info
->sps
, &info
->sei
);
3713 complete_au
= hevc_complete_au( au
, probe
);
3717 case HEVC_NALU_TYPE_PREFIX_SEI
:
3718 case HEVC_NALU_TYPE_SUFFIX_SEI
:
3720 if( hevc_parse_sei( info
->bits
, &info
->vps
, &info
->sps
, &info
->sei
, &nuh
,
3721 sb
->rbsp
, nalu
+ nuh
.length
, nalu_length
- nuh
.length
) < 0 )
3722 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
);
3723 hevc_append_nalu_to_au( au
, nalu
, nalu_length
, probe
);
3726 case HEVC_NALU_TYPE_VPS
:
3727 if( hevc_try_to_append_dcr_nalu( info
, HEVC_DCR_NALU_TYPE_VPS
, nalu
, nalu_length
) < 0 )
3728 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
);
3730 case HEVC_NALU_TYPE_SPS
:
3731 if( hevc_try_to_append_dcr_nalu( info
, HEVC_DCR_NALU_TYPE_SPS
, nalu
, nalu_length
) < 0 )
3732 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
);
3734 case HEVC_NALU_TYPE_PPS
:
3735 if( hevc_try_to_append_dcr_nalu( info
, HEVC_DCR_NALU_TYPE_PPS
, nalu
, nalu_length
) < 0 )
3736 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
);
3738 case HEVC_NALU_TYPE_AUD
: /* We drop access unit delimiters. */
3741 hevc_append_nalu_to_au( au
, nalu
, nalu_length
, probe
);
3744 if( info
->hvcC_pending
)
3745 hevc_imp
->status
= IMPORTER_CHANGE
;
3748 /* Move to the first byte of the next start code. */
3749 info
->prev_nalu_type
= nalu_type
;
3750 if( lsmash_bs_read_seek( bs
, next_sc_head_pos
, SEEK_SET
) != next_sc_head_pos
)
3752 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to seek the next start code.\n" );
3753 return hevc_get_au_internal_failed( hevc_imp
, au
, complete_au
);
3755 if( !lsmash_bs_is_end( bs
, NALU_SHORT_START_CODE_LENGTH
) )
3756 hevc_imp
->sc_head_pos
= next_sc_head_pos
;
3757 /* If there is no more data in the stream, and flushed chunk of NALUs, flush it as complete AU here. */
3758 else if( au
->incomplete_length
&& au
->length
== 0 )
3760 hevc_update_picture_info( info
, picture
, slice
, &info
->sps
, &info
->sei
);
3761 hevc_complete_au( au
, probe
);
3762 return hevc_get_au_internal_succeeded( hevc_imp
, au
);
3765 return hevc_get_au_internal_succeeded( hevc_imp
, au
);
3769 static int hevc_importer_get_accessunit( importer_t
*importer
, uint32_t track_number
, lsmash_sample_t
*buffered_sample
)
3771 debug_if( !importer
|| !importer
->info
|| !buffered_sample
->data
|| !buffered_sample
->length
)
3773 if( !importer
->info
|| track_number
!= 1 )
3775 hevc_importer_t
*hevc_imp
= (hevc_importer_t
*)importer
->info
;
3776 hevc_info_t
*info
= &hevc_imp
->info
;
3777 importer_status current_status
= hevc_imp
->status
;
3778 if( current_status
== IMPORTER_ERROR
|| buffered_sample
->length
< hevc_imp
->max_au_length
)
3780 if( current_status
== IMPORTER_EOF
)
3782 buffered_sample
->length
= 0;
3785 if( hevc_get_access_unit_internal( importer
, 0 ) < 0 )
3787 hevc_imp
->status
= IMPORTER_ERROR
;
3790 if( hevc_imp
->status
== IMPORTER_CHANGE
&& !info
->hvcC_pending
)
3791 current_status
= IMPORTER_CHANGE
;
3792 if( current_status
== IMPORTER_CHANGE
)
3794 /* Update the active summary. */
3795 lsmash_codec_specific_t
*cs
= (lsmash_codec_specific_t
*)lsmash_get_entry_data( hevc_imp
->hvcC_list
, ++ hevc_imp
->hvcC_number
);
3798 lsmash_hevc_specific_parameters_t
*hvcC_param
= (lsmash_hevc_specific_parameters_t
*)cs
->data
.structured
;
3799 lsmash_video_summary_t
*summary
= hevc_create_summary( hvcC_param
, &info
->sps
, hevc_imp
->max_au_length
);
3802 lsmash_remove_entry( importer
->summaries
, track_number
, lsmash_cleanup_summary
);
3803 if( lsmash_add_entry( importer
->summaries
, summary
) )
3805 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
3808 hevc_imp
->status
= IMPORTER_OK
;
3810 //hevc_sps_t *sps = &info->sps;
3811 hevc_access_unit_t
*au
= &info
->au
;
3812 hevc_picture_info_t
*picture
= &au
->picture
;
3813 buffered_sample
->dts
= hevc_imp
->ts_list
.timestamp
[ au
->number
- 1 ].dts
;
3814 buffered_sample
->cts
= hevc_imp
->ts_list
.timestamp
[ au
->number
- 1 ].cts
;
3815 /* Set property of disposability. */
3816 if( picture
->sublayer_nonref
&& au
->TemporalId
== hevc_imp
->max_TemporalId
)
3817 /* Sub-layer non-reference pictures are not referenced by subsequent pictures of
3818 * the same sub-layer in decoding order. */
3819 buffered_sample
->prop
.disposable
= ISOM_SAMPLE_IS_DISPOSABLE
;
3821 buffered_sample
->prop
.disposable
= ISOM_SAMPLE_IS_NOT_DISPOSABLE
;
3822 /* Set property of leading. */
3823 if( picture
->radl
|| picture
->rasl
)
3824 buffered_sample
->prop
.leading
= picture
->radl
? ISOM_SAMPLE_IS_DECODABLE_LEADING
: ISOM_SAMPLE_IS_UNDECODABLE_LEADING
;
3827 if( au
->number
< hevc_imp
->num_undecodable
)
3828 buffered_sample
->prop
.leading
= ISOM_SAMPLE_IS_UNDECODABLE_LEADING
;
3831 if( picture
->independent
|| buffered_sample
->cts
>= hevc_imp
->last_intra_cts
)
3832 buffered_sample
->prop
.leading
= ISOM_SAMPLE_IS_NOT_LEADING
;
3834 buffered_sample
->prop
.leading
= ISOM_SAMPLE_IS_UNDECODABLE_LEADING
;
3837 if( picture
->independent
)
3838 hevc_imp
->last_intra_cts
= buffered_sample
->cts
;
3839 /* Set property of independence. */
3840 buffered_sample
->prop
.independent
= picture
->independent
? ISOM_SAMPLE_IS_INDEPENDENT
: ISOM_SAMPLE_IS_NOT_INDEPENDENT
;
3841 buffered_sample
->prop
.redundant
= ISOM_SAMPLE_HAS_NO_REDUNDANCY
;
3842 buffered_sample
->prop
.post_roll
.identifier
= picture
->poc
;
3843 if( picture
->random_accessible
)
3847 buffered_sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
3848 if( picture
->closed_rap
)
3849 buffered_sample
->prop
.ra_flags
|= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_CLOSED_RAP
;
3851 buffered_sample
->prop
.ra_flags
|= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP
;
3853 else if( picture
->recovery_poc_cnt
)
3855 buffered_sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_POST_ROLL_START
;
3856 buffered_sample
->prop
.post_roll
.complete
= picture
->poc
+ picture
->recovery_poc_cnt
;
3859 buffered_sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP
;
3861 buffered_sample
->length
= au
->length
;
3862 memcpy( buffered_sample
->data
, au
->data
, au
->length
);
3863 return current_status
;
3866 static lsmash_video_summary_t
*hevc_setup_first_summary
3868 importer_t
*importer
3871 hevc_importer_t
*hevc_imp
= (hevc_importer_t
*)importer
->info
;
3872 lsmash_codec_specific_t
*cs
= (lsmash_codec_specific_t
*)lsmash_get_entry_data( hevc_imp
->hvcC_list
, ++ hevc_imp
->hvcC_number
);
3873 if( !cs
|| !cs
->data
.structured
)
3875 lsmash_destroy_codec_specific_data( cs
);
3878 lsmash_video_summary_t
*summary
= hevc_create_summary( (lsmash_hevc_specific_parameters_t
*)cs
->data
.structured
,
3879 &hevc_imp
->info
.sps
, hevc_imp
->max_au_length
);
3882 lsmash_destroy_codec_specific_data( cs
);
3885 if( lsmash_add_entry( importer
->summaries
, summary
) < 0 )
3887 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
3890 summary
->sample_per_field
= hevc_imp
->field_pic_present
;
3894 static int hevc_analyze_whole_stream
3896 importer_t
*importer
3899 /* Parse all NALU in the stream for preparation of calculating timestamps. */
3900 uint32_t npt_alloc
= (1 << 12) * sizeof(nal_pic_timing_t
);
3901 nal_pic_timing_t
*npt
= (nal_pic_timing_t
*)lsmash_malloc( npt_alloc
);
3904 uint32_t picture_stats
[HEVC_PICTURE_TYPE_NONE
+ 1] = { 0 };
3905 uint32_t num_access_units
= 0;
3906 lsmash_class_t
*logger
= &(lsmash_class_t
){ "HEVC" };
3907 lsmash_log( &logger
, LSMASH_LOG_INFO
, "Analyzing stream as HEVC\r" );
3908 hevc_importer_t
*hevc_imp
= (hevc_importer_t
*)importer
->info
;
3909 hevc_info_t
*info
= &hevc_imp
->info
;
3910 hevc_imp
->status
= IMPORTER_OK
;
3911 while( hevc_imp
->status
!= IMPORTER_EOF
)
3914 lsmash_log( &logger
, LSMASH_LOG_INFO
, "Analyzing stream as HEVC: %"PRIu32
"\n", num_access_units
+ 1 );
3916 hevc_picture_info_t
*picture
= &info
->au
.picture
;
3917 hevc_picture_info_t prev_picture
= *picture
;
3918 if( hevc_get_access_unit_internal( importer
, 1 ) < 0
3919 || hevc_calculate_poc( info
, &info
->au
.picture
, &prev_picture
) < 0 )
3921 if( npt_alloc
<= num_access_units
* sizeof(nal_pic_timing_t
) )
3923 uint32_t alloc
= 2 * num_access_units
* sizeof(nal_pic_timing_t
);
3924 nal_pic_timing_t
*temp
= (nal_pic_timing_t
*)lsmash_realloc( npt
, alloc
);
3930 hevc_imp
->field_pic_present
|= picture
->field_coded
;
3931 npt
[num_access_units
].poc
= picture
->poc
;
3932 npt
[num_access_units
].delta
= picture
->delta
;
3933 npt
[num_access_units
].poc_delta
= 1;
3934 npt
[num_access_units
].reset
= 0;
3936 hevc_imp
->max_au_length
= LSMASH_MAX( hevc_imp
->max_au_length
, info
->au
.length
);
3937 hevc_imp
->max_TemporalId
= LSMASH_MAX( hevc_imp
->max_TemporalId
, info
->au
.TemporalId
);
3939 ++picture_stats
[HEVC_PICTURE_TYPE_IDR
];
3940 else if( picture
->irap
)
3941 ++picture_stats
[ picture
->broken_link
? HEVC_PICTURE_TYPE_BLA
: HEVC_PICTURE_TYPE_CRA
];
3942 else if( picture
->type
>= HEVC_PICTURE_TYPE_NONE
)
3943 ++picture_stats
[HEVC_PICTURE_TYPE_NONE
];
3945 ++picture_stats
[ picture
->type
];
3947 lsmash_log_refresh_line( &logger
);
3948 lsmash_log( &logger
, LSMASH_LOG_INFO
,
3949 "IDR: %"PRIu32
", CRA: %"PRIu32
", BLA: %"PRIu32
", I: %"PRIu32
", P: %"PRIu32
", B: %"PRIu32
", Unknown: %"PRIu32
"\n",
3950 picture_stats
[HEVC_PICTURE_TYPE_IDR
], picture_stats
[HEVC_PICTURE_TYPE_CRA
],
3951 picture_stats
[HEVC_PICTURE_TYPE_BLA
], picture_stats
[HEVC_PICTURE_TYPE_I
],
3952 picture_stats
[HEVC_PICTURE_TYPE_I_P
], picture_stats
[HEVC_PICTURE_TYPE_I_P_B
],
3953 picture_stats
[HEVC_PICTURE_TYPE_NONE
]);
3954 /* Copy and append the last Codec Specific info. */
3955 if( hevc_store_codec_specific( hevc_imp
, &info
->hvcC_param
) < 0 )
3957 /* Set up the first summary. */
3958 lsmash_video_summary_t
*summary
= hevc_setup_first_summary( importer
);
3962 lsmash_media_ts_t
*timestamp
= lsmash_malloc( num_access_units
* sizeof(lsmash_media_ts_t
) );
3965 /* Count leading samples that are undecodable. */
3966 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
3968 if( npt
[i
].poc
== 0 )
3970 ++ hevc_imp
->num_undecodable
;
3972 /* Deduplicate POCs. */
3973 uint32_t max_composition_delay
= 0;
3974 nalu_deduplicate_poc( npt
, &max_composition_delay
, num_access_units
, 15 );
3975 /* Generate timestamps. */
3976 nalu_generate_timestamps_from_poc( importer
, timestamp
, npt
,
3977 &hevc_imp
->composition_reordering_present
,
3978 &hevc_imp
->last_delta
,
3979 max_composition_delay
, num_access_units
);
3980 summary
->timescale
*= 2; /* We assume that picture timing is in field level.
3981 * For HEVC, it seems time_scale is set in frame level basically.
3982 * So multiply by 2 for reducing timebase and timescale. */
3983 nalu_reduce_timescale( timestamp
, npt
, &hevc_imp
->last_delta
, &summary
->timescale
, num_access_units
);
3985 hevc_imp
->ts_list
.sample_count
= num_access_units
;
3986 hevc_imp
->ts_list
.timestamp
= timestamp
;
3989 lsmash_log_refresh_line( &logger
);
3994 static int hevc_importer_probe( importer_t
*importer
)
3996 /* Find the first start code. */
3997 hevc_importer_t
*hevc_imp
= create_hevc_importer( importer
);
4000 lsmash_bs_t
*bs
= hevc_imp
->bs
;
4001 uint64_t first_sc_head_pos
= nalu_find_first_start_code( bs
);
4002 if( first_sc_head_pos
== NALU_NO_START_CODE_FOUND
)
4004 /* OK. It seems the stream has a long start code of HEVC. */
4005 importer
->info
= hevc_imp
;
4006 hevc_info_t
*info
= &hevc_imp
->info
;
4007 lsmash_bs_read_seek( bs
, first_sc_head_pos
, SEEK_SET
);
4008 hevc_imp
->sc_head_pos
= first_sc_head_pos
;
4009 if( hevc_analyze_whole_stream( importer
) < 0 )
4011 /* Go back to the start code of the first NALU. */
4012 hevc_imp
->status
= IMPORTER_OK
;
4013 lsmash_bs_read_seek( bs
, first_sc_head_pos
, SEEK_SET
);
4014 hevc_imp
->sc_head_pos
= first_sc_head_pos
;
4015 info
->prev_nalu_type
= HEVC_NALU_TYPE_UNKNOWN
;
4016 uint8_t *temp_au
= info
->au
.data
;
4017 uint8_t *temp_incomplete_au
= info
->au
.incomplete_data
;
4018 memset( &info
->au
, 0, sizeof(hevc_access_unit_t
) );
4019 info
->au
.data
= temp_au
;
4020 info
->au
.incomplete_data
= temp_incomplete_au
;
4021 memset( &info
->slice
, 0, sizeof(hevc_slice_info_t
) );
4022 memset( &info
->vps
, 0, sizeof(hevc_vps_t
) );
4023 memset( &info
->sps
, 0, sizeof(hevc_sps_t
) );
4024 memset( &info
->pps
, 0, SIZEOF_PPS_EXCLUDING_HEAP
);
4025 for( int i
= 0; i
< HEVC_DCR_NALU_TYPE_NUM
; i
++ )
4026 lsmash_remove_entries( info
->hvcC_param
.parameter_arrays
->ps_array
[i
].list
, isom_remove_dcr_ps
);
4027 lsmash_destroy_hevc_parameter_arrays( &info
->hvcC_param_next
);
4030 remove_hevc_importer( hevc_imp
);
4031 importer
->info
= NULL
;
4032 lsmash_remove_entries( importer
->summaries
, lsmash_cleanup_summary
);
4036 static uint32_t hevc_importer_get_last_delta( importer_t
*importer
, uint32_t track_number
)
4038 debug_if( !importer
|| !importer
->info
)
4040 hevc_importer_t
*hevc_imp
= (hevc_importer_t
*)importer
->info
;
4041 if( !hevc_imp
|| track_number
!= 1 || hevc_imp
->status
!= IMPORTER_EOF
)
4043 return hevc_imp
->ts_list
.sample_count
4044 ? hevc_imp
->last_delta
4045 : UINT32_MAX
; /* arbitrary */
4048 static const importer_functions hevc_importer
=
4050 { "HEVC", offsetof( importer_t
, log_level
) },
4052 hevc_importer_probe
,
4053 hevc_importer_get_accessunit
,
4054 hevc_importer_get_last_delta
,
4055 hevc_importer_cleanup
4058 /***************************************************************************
4059 SMPTE VC-1 importer (only for Advanced Profile)
4062 ***************************************************************************/
4063 #include "codecs/vc1.h"
4067 importer_status status
;
4069 vc1_sequence_header_t first_sequence
;
4070 lsmash_media_ts_list_t ts_list
;
4072 uint8_t composition_reordering_present
;
4073 uint32_t max_au_length
;
4074 uint32_t num_undecodable
;
4075 uint64_t last_ref_intra_cts
;
4078 static void remove_vc1_importer( vc1_importer_t
*vc1_imp
)
4082 vc1_cleanup_parser( &vc1_imp
->info
);
4083 lsmash_bs_cleanup( vc1_imp
->bs
);
4084 lsmash_free( vc1_imp
->ts_list
.timestamp
);
4085 lsmash_free( vc1_imp
);
4088 static void vc1_importer_cleanup( importer_t
*importer
)
4090 debug_if( importer
&& importer
->info
)
4091 remove_vc1_importer( importer
->info
);
4094 static vc1_importer_t
*create_vc1_importer( importer_t
*importer
)
4096 vc1_importer_t
*vc1_imp
= lsmash_malloc_zero( sizeof(vc1_importer_t
) );
4099 if( vc1_setup_parser( &vc1_imp
->info
, 0 ) < 0 )
4101 remove_vc1_importer( vc1_imp
);
4104 lsmash_bs_t
*bs
= lsmash_bs_create();
4107 remove_vc1_importer( vc1_imp
);
4110 bs
->stream
= importer
->stream
;
4111 bs
->read
= lsmash_fread_wrapper
;
4112 bs
->seek
= lsmash_fseek_wrapper
;
4113 bs
->unseekable
= importer
->is_stdin
;
4114 bs
->buffer
.max_size
= BS_MAX_DEFAULT_READ_SIZE
;
4119 static inline int vc1_complete_au( vc1_access_unit_t
*access_unit
, vc1_picture_info_t
*picture
, int probe
)
4121 if( !picture
->present
)
4124 memcpy( access_unit
->data
, access_unit
->incomplete_data
, access_unit
->incomplete_data_length
);
4125 access_unit
->data_length
= access_unit
->incomplete_data_length
;
4126 access_unit
->incomplete_data_length
= 0;
4127 vc1_update_au_property( access_unit
, picture
);
4131 static inline void vc1_append_ebdu_to_au( vc1_access_unit_t
*access_unit
, uint8_t *ebdu
, uint32_t ebdu_length
, int probe
)
4134 memcpy( access_unit
->incomplete_data
+ access_unit
->incomplete_data_length
, ebdu
, ebdu_length
);
4135 /* Note: access_unit->incomplete_data_length shall be 0 immediately after AU has completed.
4136 * Therefore, possible_au_length in vc1_get_access_unit_internal() can't be used here
4137 * to avoid increasing AU length monotonously through the entire stream. */
4138 access_unit
->incomplete_data_length
+= ebdu_length
;
4141 static inline void vc1_get_au_internal_end( vc1_importer_t
*vc1_imp
, vc1_access_unit_t
*access_unit
)
4143 vc1_imp
->status
= lsmash_bs_is_end( vc1_imp
->bs
, 0 ) && (access_unit
->incomplete_data_length
== 0)
4148 static int vc1_get_au_internal_succeeded( vc1_importer_t
*vc1_imp
)
4150 vc1_access_unit_t
*access_unit
= &vc1_imp
->info
.access_unit
;
4151 vc1_get_au_internal_end( vc1_imp
, access_unit
);
4152 access_unit
->number
+= 1;
4156 static int vc1_get_au_internal_failed( vc1_importer_t
*vc1_imp
, int complete_au
)
4158 vc1_access_unit_t
*access_unit
= &vc1_imp
->info
.access_unit
;
4159 vc1_get_au_internal_end( vc1_imp
, access_unit
);
4161 access_unit
->number
+= 1;
4165 static int vc1_importer_get_access_unit_internal( importer_t
*importer
, int probe
)
4167 vc1_importer_t
*vc1_imp
= (vc1_importer_t
*)importer
->info
;
4168 vc1_info_t
*info
= &vc1_imp
->info
;
4169 vc1_stream_buffer_t
*sb
= &info
->buffer
;
4170 vc1_access_unit_t
*access_unit
= &info
->access_unit
;
4171 lsmash_bs_t
*bs
= vc1_imp
->bs
;
4172 int complete_au
= 0;
4173 access_unit
->data_length
= 0;
4177 uint64_t trailing_zero_bytes
;
4178 uint64_t ebdu_length
= vc1_find_next_start_code_prefix( bs
, &bdu_type
, &trailing_zero_bytes
);
4179 if( ebdu_length
<= VC1_START_CODE_LENGTH
&& lsmash_bs_is_end( bs
, ebdu_length
) )
4181 /* For the last EBDU.
4182 * This EBDU already has been appended into the latest access unit and parsed. */
4183 vc1_complete_au( access_unit
, &info
->picture
, probe
);
4184 return vc1_get_au_internal_succeeded( vc1_imp
);
4186 else if( bdu_type
== 0xFF )
4188 lsmash_log( importer
, LSMASH_LOG_ERROR
, "a forbidden BDU type is detected.\n" );
4189 return vc1_get_au_internal_failed( vc1_imp
, complete_au
);
4191 uint64_t next_ebdu_head_pos
= info
->ebdu_head_pos
4193 + trailing_zero_bytes
;
4197 fprintf( stderr
, "BDU type: %"PRIu8
" \n", bdu_type
);
4198 fprintf( stderr
, " EBDU position: %"PRIx64
" \n", info
->ebdu_head_pos
);
4199 fprintf( stderr
, " EBDU length: %"PRIx64
" (%"PRIu64
")\n", ebdu_length
, ebdu_length
);
4200 fprintf( stderr
, " trailing_zero_bytes: %"PRIx64
" \n", trailing_zero_bytes
);
4201 fprintf( stderr
, " Next EBDU position: %"PRIx64
" \n", next_ebdu_head_pos
);
4204 if( bdu_type
>= 0x0A && bdu_type
<= 0x0F )
4206 /* Complete the current access unit if encountered delimiter of current access unit. */
4207 if( vc1_find_au_delimit_by_bdu_type( bdu_type
, info
->prev_bdu_type
) )
4208 /* The last video coded EBDU belongs to the access unit you want at this time. */
4209 complete_au
= vc1_complete_au( access_unit
, &info
->picture
, probe
);
4210 /* Increase the buffer if needed. */
4211 uint64_t possible_au_length
= access_unit
->incomplete_data_length
+ ebdu_length
;
4212 if( sb
->bank
->buffer_size
< possible_au_length
4213 && vc1_supplement_buffer( sb
, access_unit
, 2 * possible_au_length
) < 0 )
4215 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to increase the buffer size.\n" );
4216 return vc1_get_au_internal_failed( vc1_imp
, complete_au
);
4218 /* Process EBDU by its BDU type and append it to access unit. */
4219 uint8_t *ebdu
= lsmash_bs_get_buffer_data( bs
);
4222 /* FRM_SC: Frame start code
4223 * FLD_SC: Field start code
4224 * SLC_SC: Slice start code
4225 * SEQ_SC: Sequence header start code
4226 * EP_SC: Entry-point start code
4227 * PIC_L: Picture layer
4228 * SLC_L: Slice layer
4229 * SEQ_L: Sequence layer
4230 * EP_L: Entry-point layer */
4231 case 0x0D : /* Frame
4232 * For the Progressive or Frame Interlace mode, shall signal the beginning of a new video frame.
4233 * For the Field Interlace mode, shall signal the beginning of a sequence of two independently coded video fields.
4234 * [FRM_SC][PIC_L][[FLD_SC][PIC_L] (optional)][[SLC_SC][SLC_L] (optional)] ... */
4235 if( vc1_parse_advanced_picture( info
->bits
, &info
->sequence
, &info
->picture
, sb
->rbdu
, ebdu
, ebdu_length
) < 0 )
4237 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to parse a frame.\n" );
4238 return vc1_get_au_internal_failed( vc1_imp
, complete_au
);
4240 case 0x0C : /* Field
4241 * Shall only be used for Field Interlaced frames
4242 * and shall only be used to signal the beginning of the second field of the frame.
4243 * [FRM_SC][PIC_L][FLD_SC][PIC_L][[SLC_SC][SLC_L] (optional)] ...
4244 * Field start code is followed by INTERLACE_FIELD_PICTURE_FIELD2() which doesn't have info of its field picture type.*/
4246 case 0x0B : /* Slice
4247 * Shall not be used for start code of the first slice of a frame.
4248 * Shall not be used for start code of the first slice of an interlace field coded picture.
4249 * [FRM_SC][PIC_L][[FLD_SC][PIC_L] (optional)][SLC_SC][SLC_L][[SLC_SC][SLC_L] (optional)] ...
4250 * Slice layer may repeat frame header. We just ignore it. */
4251 info
->dvc1_param
.slice_present
= 1;
4253 case 0x0E : /* Entry-point header
4254 * Entry-point indicates the direct followed frame is a start of group of frames.
4255 * Entry-point doesn't indicates the frame is a random access point when multiple sequence headers are present,
4256 * since it is necessary to decode sequence header which subsequent frames belong to for decoding them.
4257 * Entry point shall be followed by
4258 * 1. I-picture - progressive or frame interlace
4259 * 2. I/I-picture, I/P-picture, or P/I-picture - field interlace
4260 * [[SEQ_SC][SEQ_L] (optional)][EP_SC][EP_L][FRM_SC][PIC_L] ... */
4261 if( vc1_parse_entry_point_header( info
, ebdu
, ebdu_length
, probe
) < 0 )
4263 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to parse an entry point.\n" );
4264 return vc1_get_au_internal_failed( vc1_imp
, complete_au
);
4266 /* Signal random access type of the frame that follows this entry-point header. */
4267 info
->picture
.closed_gop
= info
->entry_point
.closed_entry_point
;
4268 info
->picture
.random_accessible
= info
->dvc1_param
.multiple_sequence
? info
->picture
.start_of_sequence
: 1;
4270 case 0x0F : /* Sequence header
4271 * [SEQ_SC][SEQ_L][EP_SC][EP_L][FRM_SC][PIC_L] ... */
4272 if( vc1_parse_sequence_header( info
, ebdu
, ebdu_length
, probe
) < 0 )
4274 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to parse a sequence header.\n" );
4275 return vc1_get_au_internal_failed( vc1_imp
, complete_au
);
4277 /* The frame that is the first frame after this sequence header shall be a random accessible point. */
4278 info
->picture
.start_of_sequence
= 1;
4279 if( probe
&& !vc1_imp
->first_sequence
.present
)
4280 vc1_imp
->first_sequence
= info
->sequence
;
4282 default : /* End-of-sequence (0x0A) */
4285 /* Append the current EBDU into the end of an incomplete access unit. */
4286 vc1_append_ebdu_to_au( access_unit
, ebdu
, ebdu_length
, probe
);
4288 else /* We don't support other BDU types such as user data yet. */
4289 return vc1_get_au_internal_failed( vc1_imp
, complete_au
);
4290 /* Move to the first byte of the next EBDU. */
4291 info
->prev_bdu_type
= bdu_type
;
4292 if( lsmash_bs_read_seek( bs
, next_ebdu_head_pos
, SEEK_SET
) != next_ebdu_head_pos
)
4294 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to seek the next start code suffix.\n" );
4295 return vc1_get_au_internal_failed( vc1_imp
, complete_au
);
4297 /* Check if no more data to read from the stream. */
4298 if( !lsmash_bs_is_end( bs
, VC1_START_CODE_PREFIX_LENGTH
) )
4299 info
->ebdu_head_pos
= next_ebdu_head_pos
;
4300 /* If there is no more data in the stream, and flushed chunk of EBDUs, flush it as complete AU here. */
4301 else if( access_unit
->incomplete_data_length
&& access_unit
->data_length
== 0 )
4303 vc1_complete_au( access_unit
, &info
->picture
, probe
);
4304 return vc1_get_au_internal_succeeded( vc1_imp
);
4307 return vc1_get_au_internal_succeeded( vc1_imp
);
4311 static int vc1_importer_get_accessunit( importer_t
*importer
, uint32_t track_number
, lsmash_sample_t
*buffered_sample
)
4313 debug_if( !importer
|| !importer
->info
|| !buffered_sample
->data
|| !buffered_sample
->length
)
4315 if( !importer
->info
|| track_number
!= 1 )
4317 vc1_importer_t
*vc1_imp
= (vc1_importer_t
*)importer
->info
;
4318 vc1_info_t
*info
= &vc1_imp
->info
;
4319 importer_status current_status
= vc1_imp
->status
;
4320 if( current_status
== IMPORTER_ERROR
|| buffered_sample
->length
< vc1_imp
->max_au_length
)
4322 if( current_status
== IMPORTER_EOF
)
4324 buffered_sample
->length
= 0;
4327 if( vc1_importer_get_access_unit_internal( importer
, 0 ) < 0 )
4329 vc1_imp
->status
= IMPORTER_ERROR
;
4332 vc1_access_unit_t
*access_unit
= &info
->access_unit
;
4333 buffered_sample
->dts
= vc1_imp
->ts_list
.timestamp
[ access_unit
->number
- 1 ].dts
;
4334 buffered_sample
->cts
= vc1_imp
->ts_list
.timestamp
[ access_unit
->number
- 1 ].cts
;
4335 buffered_sample
->prop
.leading
= access_unit
->independent
4336 || access_unit
->non_bipredictive
4337 || buffered_sample
->cts
>= vc1_imp
->last_ref_intra_cts
4338 ? ISOM_SAMPLE_IS_NOT_LEADING
: ISOM_SAMPLE_IS_UNDECODABLE_LEADING
;
4339 if( access_unit
->independent
&& !access_unit
->disposable
)
4340 vc1_imp
->last_ref_intra_cts
= buffered_sample
->cts
;
4341 if( vc1_imp
->composition_reordering_present
&& !access_unit
->disposable
&& !access_unit
->closed_gop
)
4342 buffered_sample
->prop
.allow_earlier
= QT_SAMPLE_EARLIER_PTS_ALLOWED
;
4343 buffered_sample
->prop
.independent
= access_unit
->independent
? ISOM_SAMPLE_IS_INDEPENDENT
: ISOM_SAMPLE_IS_NOT_INDEPENDENT
;
4344 buffered_sample
->prop
.disposable
= access_unit
->disposable
? ISOM_SAMPLE_IS_DISPOSABLE
: ISOM_SAMPLE_IS_NOT_DISPOSABLE
;
4345 buffered_sample
->prop
.redundant
= ISOM_SAMPLE_HAS_NO_REDUNDANCY
;
4346 if( access_unit
->random_accessible
)
4347 /* All random access point is a sync sample even if it's an open RAP. */
4348 buffered_sample
->prop
.ra_flags
= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC
;
4349 buffered_sample
->length
= access_unit
->data_length
;
4350 memcpy( buffered_sample
->data
, access_unit
->data
, access_unit
->data_length
);
4351 return current_status
;
4354 static lsmash_video_summary_t
*vc1_create_summary( vc1_info_t
*info
, vc1_sequence_header_t
*sequence
, uint32_t max_au_length
)
4356 if( !info
->sequence
.present
|| !info
->entry_point
.present
)
4358 lsmash_video_summary_t
*summary
= (lsmash_video_summary_t
*)lsmash_create_summary( LSMASH_SUMMARY_TYPE_VIDEO
);
4361 lsmash_codec_specific_t
*specific
= lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_VC_1
,
4362 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED
);
4363 specific
->data
.unstructured
= lsmash_create_vc1_specific_info( &info
->dvc1_param
, &specific
->size
);
4364 if( !specific
->data
.unstructured
4365 || lsmash_add_entry( &summary
->opaque
->list
, specific
) < 0 )
4367 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
4368 lsmash_destroy_codec_specific_data( specific
);
4371 summary
->sample_type
= ISOM_CODEC_TYPE_VC_1_VIDEO
;
4372 summary
->max_au_length
= max_au_length
;
4373 summary
->timescale
= sequence
->framerate_numerator
;
4374 summary
->timebase
= sequence
->framerate_denominator
;
4375 summary
->vfr
= !sequence
->framerate_flag
;
4376 summary
->sample_per_field
= 0;
4377 summary
->width
= sequence
->disp_horiz_size
;
4378 summary
->height
= sequence
->disp_vert_size
;
4379 summary
->par_h
= sequence
->aspect_width
;
4380 summary
->par_v
= sequence
->aspect_height
;
4381 summary
->color
.primaries_index
= sequence
->color_prim
;
4382 summary
->color
.transfer_index
= sequence
->transfer_char
;
4383 summary
->color
.matrix_index
= sequence
->matrix_coef
;
4387 static int vc1_analyze_whole_stream
4389 importer_t
*importer
4392 /* Parse all EBDU in the stream for preparation of calculating timestamps. */
4393 uint32_t cts_alloc
= (1 << 12) * sizeof(uint64_t);
4394 uint64_t *cts
= lsmash_malloc( cts_alloc
);
4396 return -1; /* Failed to allocate CTS list */
4397 uint32_t num_access_units
= 0;
4398 uint32_t num_consecutive_b
= 0;
4399 lsmash_class_t
*logger
= &(lsmash_class_t
){ "VC-1" };
4400 lsmash_log( &logger
, LSMASH_LOG_INFO
, "Analyzing stream as VC-1\r" );
4401 vc1_importer_t
*vc1_imp
= (vc1_importer_t
*)importer
->info
;
4402 vc1_info_t
*info
= &vc1_imp
->info
;
4403 vc1_imp
->status
= IMPORTER_OK
;
4404 while( vc1_imp
->status
!= IMPORTER_EOF
)
4407 lsmash_log( &logger
, LSMASH_LOG_INFO
, "Analyzing stream as VC-1: %"PRIu32
"\n", num_access_units
+ 1 );
4409 if( vc1_importer_get_access_unit_internal( importer
, 1 ) < 0 )
4411 /* In the case where B-pictures exist
4413 * I[0]P[1]P[2]B[3]B[4]P[5]...
4417 * I[0]P[1]B[3]B[4]P[2]P[5]...
4420 * We assumes B or BI-pictures always be present in the stream here. */
4421 if( !info
->access_unit
.disposable
)
4423 /* Apply CTS of the last B-picture plus 1 to the last non-B-picture. */
4424 if( num_access_units
> num_consecutive_b
)
4425 cts
[ num_access_units
- num_consecutive_b
- 1 ] = num_access_units
;
4426 num_consecutive_b
= 0;
4428 else /* B or BI-picture */
4430 /* B and BI-pictures shall be output or displayed in the same order as they are encoded. */
4431 cts
[ num_access_units
] = num_access_units
;
4432 ++num_consecutive_b
;
4433 info
->dvc1_param
.bframe_present
= 1;
4435 if( cts_alloc
<= num_access_units
* sizeof(uint64_t) )
4437 uint32_t alloc
= 2 * num_access_units
* sizeof(uint64_t);
4438 uint64_t *temp
= lsmash_realloc( cts
, alloc
);
4440 goto fail
; /* Failed to re-allocate CTS list */
4444 vc1_imp
->max_au_length
= LSMASH_MAX( info
->access_unit
.data_length
, vc1_imp
->max_au_length
);
4447 if( num_access_units
> num_consecutive_b
)
4448 cts
[ num_access_units
- num_consecutive_b
- 1 ] = num_access_units
;
4451 /* Construct timestamps. */
4452 lsmash_media_ts_t
*timestamp
= lsmash_malloc( num_access_units
* sizeof(lsmash_media_ts_t
) );
4454 goto fail
; /* Failed to allocate timestamp list */
4455 for( uint32_t i
= 1; i
< num_access_units
; i
++ )
4456 if( cts
[i
] < cts
[i
- 1] )
4458 vc1_imp
->composition_reordering_present
= 1;
4461 if( vc1_imp
->composition_reordering_present
)
4462 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
4464 timestamp
[i
].cts
= cts
[i
];
4465 timestamp
[i
].dts
= i
;
4468 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
4469 timestamp
[i
].cts
= timestamp
[i
].dts
= i
;
4471 lsmash_log_refresh_line( &logger
);
4473 for( uint32_t i
= 0; i
< num_access_units
; i
++ )
4474 fprintf( stderr
, "Timestamp[%"PRIu32
"]: DTS=%"PRIu64
", CTS=%"PRIu64
"\n", i
, timestamp
[i
].dts
, timestamp
[i
].cts
);
4476 vc1_imp
->ts_list
.sample_count
= num_access_units
;
4477 vc1_imp
->ts_list
.timestamp
= timestamp
;
4480 lsmash_log_refresh_line( &logger
);
4485 static int vc1_importer_probe( importer_t
*importer
)
4487 /* Find the first start code. */
4488 vc1_importer_t
*vc1_imp
= create_vc1_importer( importer
);
4491 lsmash_bs_t
*bs
= vc1_imp
->bs
;
4492 uint64_t first_ebdu_head_pos
= 0;
4495 /* The first EBDU in decoding order of the stream shall have start code (0x000001). */
4496 if( 0x000001 == lsmash_bs_show_be24( bs
, first_ebdu_head_pos
) )
4498 /* Invalid if encountered any value of non-zero before the first start code. */
4499 if( lsmash_bs_show_byte( bs
, first_ebdu_head_pos
) )
4501 ++first_ebdu_head_pos
;
4503 /* OK. It seems the stream has a sequence header of VC-1. */
4504 importer
->info
= vc1_imp
;
4505 vc1_info_t
*info
= &vc1_imp
->info
;
4506 lsmash_bs_read_seek( bs
, first_ebdu_head_pos
, SEEK_SET
);
4507 info
->ebdu_head_pos
= first_ebdu_head_pos
;
4508 if( vc1_analyze_whole_stream( importer
) < 0 )
4510 lsmash_video_summary_t
*summary
= vc1_create_summary( info
, &vc1_imp
->first_sequence
, vc1_imp
->max_au_length
);
4513 if( lsmash_add_entry( importer
->summaries
, summary
) < 0 )
4515 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
4518 /* Go back to layer of the first EBDU. */
4519 vc1_imp
->status
= IMPORTER_OK
;
4520 lsmash_bs_read_seek( bs
, first_ebdu_head_pos
, SEEK_SET
);
4521 info
->prev_bdu_type
= 0xFF; /* 0xFF is a forbidden value. */
4522 info
->ebdu_head_pos
= first_ebdu_head_pos
;
4523 uint8_t *temp_access_unit
= info
->access_unit
.data
;
4524 uint8_t *temp_incomplete_access_unit
= info
->access_unit
.incomplete_data
;
4525 memset( &info
->access_unit
, 0, sizeof(vc1_access_unit_t
) );
4526 info
->access_unit
.data
= temp_access_unit
;
4527 info
->access_unit
.incomplete_data
= temp_incomplete_access_unit
;
4528 memset( &info
->picture
, 0, sizeof(vc1_picture_info_t
) );
4531 remove_vc1_importer( vc1_imp
);
4532 importer
->info
= NULL
;
4533 lsmash_remove_entries( importer
->summaries
, lsmash_cleanup_summary
);
4537 static uint32_t vc1_importer_get_last_delta( importer_t
*importer
, uint32_t track_number
)
4539 debug_if( !importer
|| !importer
->info
)
4541 vc1_importer_t
*vc1_imp
= (vc1_importer_t
*)importer
->info
;
4542 if( !vc1_imp
|| track_number
!= 1 || vc1_imp
->status
!= IMPORTER_EOF
)
4544 return vc1_imp
->ts_list
.sample_count
4546 : UINT32_MAX
; /* arbitrary */
4549 static const importer_functions vc1_importer
=
4551 { "VC-1", offsetof( importer_t
, log_level
) },
4554 vc1_importer_get_accessunit
,
4555 vc1_importer_get_last_delta
,
4556 vc1_importer_cleanup
4559 /***************************************************************************
4560 importer public interfaces
4561 ***************************************************************************/
4563 /******** importer listing table ********/
4564 static const importer_functions
*importer_func_table
[] =
4566 &mp4sys_adts_importer
,
4567 &mp4sys_mp3_importer
,
4579 /******** importer public functions ********/
4581 void lsmash_importer_close( importer_t
*importer
)
4585 if( !importer
->is_stdin
&& importer
->stream
)
4586 fclose( importer
->stream
);
4587 if( importer
->funcs
.cleanup
)
4588 importer
->funcs
.cleanup( importer
);
4589 lsmash_remove_list( importer
->summaries
, lsmash_cleanup_summary
);
4590 lsmash_free( importer
);
4593 importer_t
*lsmash_importer_open( const char *identifier
, const char *format
)
4595 if( identifier
== NULL
)
4597 int auto_detect
= ( format
== NULL
|| !strcmp( format
, "auto" ) );
4598 importer_t
*importer
= (importer_t
*)lsmash_malloc_zero( sizeof(importer_t
) );
4601 importer
->class = &lsmash_importer_class
;
4602 if( !strcmp( identifier
, "-" ) )
4604 /* special treatment for stdin */
4607 lsmash_log( importer
, LSMASH_LOG_ERROR
, "auto importer detection on stdin is not supported.\n" );
4610 importer
->stream
= stdin
;
4611 importer
->is_stdin
= 1;
4613 else if( (importer
->stream
= lsmash_fopen( identifier
, "rb" )) == NULL
)
4615 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to open %s.\n", identifier
);
4618 importer
->summaries
= lsmash_create_entry_list();
4619 if( !importer
->summaries
)
4621 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to set up the importer.\n" );
4625 importer
->log_level
= LSMASH_LOG_QUIET
; /* Any error log is confusing for the probe step. */
4626 const importer_functions
*funcs
;
4629 /* just rely on detector. */
4630 for( int i
= 0; (funcs
= importer_func_table
[i
]) != NULL
; i
++ )
4632 importer
->class = &funcs
->class;
4633 if( !funcs
->detectable
)
4635 if( !funcs
->probe( importer
) || lsmash_fseek( importer
->stream
, 0, SEEK_SET
) )
4641 /* needs name matching. */
4642 for( int i
= 0; (funcs
= importer_func_table
[i
]) != NULL
; i
++ )
4644 importer
->class = &funcs
->class;
4645 if( strcmp( importer
->class->name
, format
) )
4647 if( funcs
->probe( importer
) )
4652 importer
->log_level
= LSMASH_LOG_INFO
;
4655 importer
->class = &lsmash_importer_class
;
4656 lsmash_log( importer
, LSMASH_LOG_ERROR
, "failed to find the matched importer.\n" );
4659 importer
->funcs
= *funcs
;
4662 lsmash_importer_close( importer
);
4666 /* 0 if success, positive if changed, negative if failed */
4667 int lsmash_importer_get_access_unit( importer_t
*importer
, uint32_t track_number
, lsmash_sample_t
*buffered_sample
)
4669 if( !importer
|| !importer
->funcs
.get_accessunit
|| !buffered_sample
->data
|| buffered_sample
->length
== 0 )
4671 return importer
->funcs
.get_accessunit( importer
, track_number
, buffered_sample
);
4674 /* Return 0 if failed, otherwise succeeded. */
4675 uint32_t lsmash_importer_get_last_delta( importer_t
*importer
, uint32_t track_number
)
4677 if( !importer
|| !importer
->funcs
.get_last_delta
)
4679 return importer
->funcs
.get_last_delta( importer
, track_number
);
4682 uint32_t lsmash_importer_get_track_count( importer_t
*importer
)
4684 if( !importer
|| !importer
->summaries
)
4686 return importer
->summaries
->entry_count
;
4689 lsmash_summary_t
*lsmash_duplicate_summary( importer_t
*importer
, uint32_t track_number
)
4693 lsmash_summary_t
*src_summary
= lsmash_get_entry_data( importer
->summaries
, track_number
);
4696 lsmash_summary_t
*summary
= lsmash_create_summary( src_summary
->summary_type
);
4699 lsmash_codec_specific_list_t
*opaque
= summary
->opaque
;
4700 switch( src_summary
->summary_type
)
4702 case LSMASH_SUMMARY_TYPE_VIDEO
:
4703 *(lsmash_video_summary_t
*)summary
= *(lsmash_video_summary_t
*)src_summary
;
4705 case LSMASH_SUMMARY_TYPE_AUDIO
:
4706 *(lsmash_audio_summary_t
*)summary
= *(lsmash_audio_summary_t
*)src_summary
;
4709 lsmash_cleanup_summary( summary
);
4712 summary
->opaque
= opaque
;
4713 for( lsmash_entry_t
*entry
= src_summary
->opaque
->list
.head
; entry
; entry
= entry
->next
)
4715 lsmash_codec_specific_t
*src_specific
= (lsmash_codec_specific_t
*)entry
->data
;
4718 lsmash_codec_specific_t
*dup
= isom_duplicate_codec_specific_data( src_specific
);
4719 if( lsmash_add_entry( &summary
->opaque
->list
, dup
) )
4721 lsmash_cleanup_summary( (lsmash_summary_t
*)summary
);
4722 lsmash_destroy_codec_specific_data( dup
);