H.264 importer: Fix a memory leak introduced by the previous commit.
[L-SMASH.git] / cli / importer.c
bloba0eb98ff65ee3c2018b5e1394f8182ef1d273be8
1 /*****************************************************************************
2 * importer.c:
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 */
26 #include <stdlib.h>
27 #include <string.h>
28 #include <math.h>
29 #include <inttypes.h>
31 #define LSMASH_IMPORTER_INTERNAL
32 #include "importer.h"
34 #include "core/box.h"
36 #include "codecs/mp4a.h"
37 #include "codecs/description.h"
39 /***************************************************************************
40 importer framework
41 ***************************************************************************/
42 struct importer_tag;
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 );
49 typedef struct
51 lsmash_class_t class;
52 int detectable;
53 importer_probe probe;
54 importer_get_accessunit get_accessunit;
55 importer_get_last_duration get_last_delta;
56 importer_cleanup cleanup;
57 } importer_functions;
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 */
65 int is_stdin;
66 void *info; /* importer internal status information. */
67 importer_functions funcs;
68 lsmash_entry_list_t *summaries;
69 } importer_t;
71 typedef enum
73 IMPORTER_ERROR = -1,
74 IMPORTER_OK = 0,
75 IMPORTER_CHANGE = 1,
76 IMPORTER_EOF = 2,
77 } importer_status;
79 static const lsmash_class_t lsmash_importer_class =
81 "importer",
82 offsetof( importer_t, log_level )
85 /***************************************************************************
86 ADTS importer
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
93 typedef struct
95 uint16_t syncword; /* 12; */
96 uint8_t ID; /* 1; */
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;
108 typedef struct
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. */
147 return 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 */
164 uint8_t buf2[2];
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 )
174 return -1;
176 return 0;
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 )
189 return -1;
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 )
195 return -1;
196 first_offset += ( 2 * number_of_blocks ) + 2; /* according to above */
198 else
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.
207 return -1;
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;
224 return 0;
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 ) )
231 return -1;
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 );
239 if( !summary )
240 return NULL;
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;
260 summary->asc = 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. */
263 return summary;
265 #endif
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 );
270 if( !data )
272 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
273 return NULL;
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 );
277 if( !specific )
279 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
280 lsmash_free( data );
281 return NULL;
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 );
290 lsmash_free( data );
291 return NULL;
293 lsmash_free( data );
294 if( lsmash_add_entry( &summary->opaque->list, specific ) )
296 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
297 lsmash_destroy_codec_specific_data( specific );
298 return NULL;
300 return summary;
303 typedef struct
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;
310 uint32_t au_number;
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 )
316 return -1;
317 if( !importer->info || track_number != 1 )
318 return -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 )
323 return -1;
324 if( current_status == IMPORTER_EOF )
326 buffered_sample->length = 0;
327 return 0;
329 if( current_status == IMPORTER_CHANGE )
331 lsmash_audio_summary_t* summary = mp4sys_adts_create_summary( &info->header );
332 if( !summary )
333 return -1;
334 lsmash_entry_t* entry = lsmash_get_entry( importer->summaries, track_number );
335 if( !entry || !entry->data )
336 return -1;
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;
346 return -1;
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;
362 return 0;
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;
369 return 0;
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 );
377 if( ret == 0 )
379 info->status = IMPORTER_EOF;
380 return 0;
382 if( ret != MP4SYS_ADTS_BASIC_HEADER_LENGTH )
384 info->status = IMPORTER_ERROR;
385 return 0;
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
403 * retrieve headers.
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;
412 return 0;
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;
433 return 0;
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;
454 return 0;
456 /* no change which matters to mp4 muxing was found */
457 info->status = IMPORTER_OK;
458 return 0;
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 )
472 return -1;
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 ) )
477 return -1;
479 /* now the stream seems valid ADTS */
481 lsmash_audio_summary_t* summary = mp4sys_adts_create_summary( &header );
482 if( !summary )
483 return -1;
485 /* importer status */
486 mp4sys_adts_info_t* info = lsmash_malloc_zero( sizeof(mp4sys_adts_info_t) );
487 if( !info )
489 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
490 return -1;
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 ) )
500 lsmash_free( info );
501 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
502 return -1;
504 importer->info = info;
506 return 0;
509 static uint32_t mp4sys_adts_get_last_delta( importer_t *importer, uint32_t track_number )
511 debug_if( !importer || !importer->info )
512 return 0;
513 mp4sys_adts_info_t *info = (mp4sys_adts_info_t *)importer->info;
514 if( !info || track_number != 1 || info->status != IMPORTER_EOF )
515 return 0;
516 return info->samples_in_frame;
519 static const importer_functions mp4sys_adts_importer =
521 { "adts" },
523 mp4sys_adts_probe,
524 mp4sys_adts_get_accessunit,
525 mp4sys_adts_get_last_delta,
526 mp4sys_adts_cleanup
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 );
541 typedef struct
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;
582 return 0;
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 )
600 return 384;
601 else if( header->ID == 1 || header->layer == MP4SYS_LAYER_II )
602 return 1152;
603 else
604 return 576;
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 );
610 if( !summary )
611 return NULL;
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. */
621 if( !legacy_mode )
623 summary->object_type_indication = MP4SYS_OBJECT_TYPE_Audio_ISO_14496_3;
624 if( lsmash_setup_AudioSpecificConfig( summary ) )
626 lsmash_cleanup_summary( summary );
627 return NULL;
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 );
634 if( !data )
636 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
637 return NULL;
639 #endif
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 );
642 if( !specific )
644 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
645 #if !USE_MP4SYS_LEGACY_INTERFACE
646 lsmash_free( data );
647 #endif
648 return NULL;
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 );
658 lsmash_free( data );
659 return NULL;
661 lsmash_free( data );
662 #endif
663 if( lsmash_add_entry( &summary->opaque->list, specific ) )
665 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
666 lsmash_destroy_codec_specific_data( specific );
667 return NULL;
669 return summary;
672 typedef struct
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;
678 uint32_t au_number;
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 */
681 uint16_t enc_delay;
682 uint16_t padding;
683 uint64_t valid_samples;
684 } mp4sys_mp3_info_t;
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;
692 else
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 ) )
697 return 0;
698 uint32_t flags = LSMASH_GET_BE32( &mdp[4] );
699 uint32_t off = 8;
700 uint32_t frame_count = 0;
701 if( flags & 1 )
703 frame_count = LSMASH_GET_BE32( &mdp[8] );
704 info->valid_samples = (uint64_t)frame_count * mp4sys_mp3_samples_in_frame( header );
705 off += 4;
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;
716 if( frame_count )
717 info->valid_samples -= info->enc_delay + info->padding;
719 return 1;
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 )
730 return -1;
731 if( !importer->info || track_number != 1 )
732 return -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 */
743 { /* MPEG-1 audio */
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 )
752 return -1;
753 uint32_t frame_size;
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;
759 else
761 /* mp2/3's 'slot' is 1 bytes unit. */
762 uint32_t div = frequency;
763 if( header->layer == MP4SYS_LAYER_III && header->ID == 0 )
764 div <<= 1;
765 frame_size = 144 * 1000 * bitrate / div + header->padding_bit;
768 if( current_status == IMPORTER_ERROR || frame_size <= 4 || buffered_sample->length < frame_size )
769 return -1;
770 if( current_status == IMPORTER_EOF )
772 buffered_sample->length = 0;
773 return 0;
775 if( current_status == IMPORTER_CHANGE )
777 lsmash_audio_summary_t* summary = mp4sys_mp3_create_summary( header, 1 ); /* FIXME: use legacy mode. */
778 if( !summary )
779 return -1;
780 lsmash_entry_t* entry = lsmash_get_entry( importer->summaries, track_number );
781 if( !entry || !entry->data )
782 return -1;
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;
793 return -1;
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;
807 info->au_number--;
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
826 * preceding frames.
827 * we just add up main_data size from history until it reaches
828 * the required amount.
830 unsigned int reservoir_data = 0;
831 unsigned int i;
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 )
836 break;
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;
844 else
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
848 * to the front */
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 );
857 if( ret == 0 )
859 info->status = IMPORTER_EOF;
860 return 0;
862 if( ret >= 2 && (!memcmp( buf, "TA", 2 ) || !memcmp( buf, "AP", 2 )) )
864 /* ID3v1 or APE tag */
865 info->status = IMPORTER_EOF;
866 return 0;
868 if( ret == 1 && *buf == 0x00 )
870 /* NOTE: ugly hack for mp1 stream created with SCMPX. */
871 info->status = IMPORTER_EOF;
872 return 0;
874 if( ret != MP4SYS_MP3_HEADER_LENGTH )
876 info->status = IMPORTER_ERROR;
877 return 0;
880 mp4sys_mp3_header_t new_header = {0};
881 if( mp4sys_mp3_parse_header( buf, &new_header ) )
883 info->status = IMPORTER_ERROR;
884 return 0;
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;
893 return 0;
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;
899 else
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 );
905 return 0;
908 static int mp4sys_mp3_probe( importer_t *importer )
910 int c;
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 );
916 uint32_t size = 0;
917 for( int i = 0 ; i < 4; i++ )
919 size <<= 7;
920 size |= getc( importer->stream );
922 lsmash_fseek( importer->stream, size, SEEK_CUR );
924 else
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 )
929 return -1;
931 mp4sys_mp3_header_t header = {0};
932 if( mp4sys_mp3_parse_header( buf, &header ) )
933 return -1;
935 /* now the stream seems valid mp3 */
937 lsmash_audio_summary_t* summary = mp4sys_mp3_create_summary( &header, 1 ); /* FIXME: use legacy mode. */
938 if( !summary )
939 return -1;
941 /* importer status */
942 mp4sys_mp3_info_t* info = lsmash_malloc_zero( sizeof(mp4sys_mp3_info_t) );
943 if( !info )
945 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
946 return -1;
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 ) )
955 lsmash_free( info );
956 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
957 return -1;
959 importer->info = info;
961 return 0;
964 static uint32_t mp4sys_mp3_get_last_delta( importer_t *importer, uint32_t track_number )
966 debug_if( !importer || !importer->info )
967 return 0;
968 mp4sys_mp3_info_t *info = (mp4sys_mp3_info_t *)importer->info;
969 if( !info || track_number != 1 || info->status != IMPORTER_EOF )
970 return 0;
971 return info->samples_in_frame;
974 static const importer_functions mp4sys_mp3_importer =
976 { "MPEG-1/2BC_Audio_Legacy" },
978 mp4sys_mp3_probe,
979 mp4sys_mp3_get_accessunit,
980 mp4sys_mp3_get_last_delta,
981 mp4sys_mp3_cleanup
984 /***************************************************************************
985 AMR-NB/WB storage format importer
986 http://www.ietf.org/rfc/rfc3267.txt (Obsoleted)
987 http://www.ietf.org/rfc/rfc4867.txt
988 ***************************************************************************/
989 static void amr_cleanup( importer_t* importer )
991 debug_if( importer && importer->info )
992 lsmash_free( importer->info );
995 typedef struct
997 uint8_t wb;
998 uint32_t samples_in_frame;
999 uint32_t au_number;
1000 } amr_importer_info_t;
1002 static int amr_get_accessunit( importer_t* importer, uint32_t track_number, lsmash_sample_t *buffered_sample )
1004 debug_if( !importer || !importer->info || !buffered_sample->data || !buffered_sample->length )
1005 return -1;
1006 if( track_number != 1 )
1007 return -1;
1008 amr_importer_info_t *info = (amr_importer_info_t *)importer->info;
1010 uint8_t* buf = buffered_sample->data;
1011 if( fread( buf, 1, 1, importer->stream ) == 0 )
1013 /* EOF */
1014 buffered_sample->length = 0;
1015 return 0;
1017 uint8_t FT = (*buf >> 3) & 0x0F;
1019 /* AMR-NB has varieties of frame-size table like this. so I'm not sure yet. */
1020 const int frame_size[2][16] = {
1021 { 13, 14, 16, 18, 20, 21, 27, 32, 5, 5, 5, 5, 0, 0, 0, 1 },
1022 { 18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1 }
1024 int read_size = frame_size[info->wb][FT];
1025 if( read_size == 0 || buffered_sample->length < read_size-- )
1026 return -1;
1027 if( read_size == 0 )
1028 buffered_sample->length = 1;
1029 else
1031 if( fread( buf+1, 1, read_size, importer->stream ) != read_size )
1032 return -1;
1033 buffered_sample->length = read_size + 1;
1035 buffered_sample->dts = info->au_number++ * info->samples_in_frame;
1036 buffered_sample->cts = buffered_sample->dts;
1037 buffered_sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
1038 return 0;
1041 #define MP4SYS_DAMR_LENGTH 17
1043 int mp4sys_amr_create_damr( lsmash_audio_summary_t *summary )
1045 lsmash_bs_t* bs = lsmash_bs_create();
1046 if( !bs )
1047 return -1;
1048 lsmash_bs_put_be32( bs, MP4SYS_DAMR_LENGTH );
1049 lsmash_bs_put_be32( bs, ISOM_BOX_TYPE_DAMR.fourcc );
1050 /* NOTE: These are specific to each codec vendor, but we're surely not a vendor.
1051 Using dummy data. */
1052 lsmash_bs_put_be32( bs, 0x20202020 ); /* vendor */
1053 lsmash_bs_put_byte( bs, 0 ); /* decoder_version */
1054 /* NOTE: Using safe value for these settings, maybe sub-optimal. */
1055 lsmash_bs_put_be16( bs, 0x83FF ); /* mode_set, represents for possibly existing frame-type (0x83FF == all). */
1056 lsmash_bs_put_byte( bs, 1 ); /* mode_change_period */
1057 lsmash_bs_put_byte( bs, 1 ); /* frames_per_sample */
1058 lsmash_codec_specific_t *specific = lsmash_malloc_zero( sizeof(lsmash_codec_specific_t) );
1059 if( !specific )
1061 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
1062 lsmash_bs_cleanup( bs );
1063 return -1;
1065 specific->type = LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNKNOWN;
1066 specific->format = LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED;
1067 specific->destruct = (lsmash_codec_specific_destructor_t)lsmash_free;
1068 specific->data.unstructured = lsmash_bs_export_data( bs, &specific->size );
1069 specific->size = MP4SYS_DAMR_LENGTH;
1070 lsmash_bs_cleanup( bs );
1071 if( !specific->data.unstructured
1072 || lsmash_add_entry( &summary->opaque->list, specific ) )
1074 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
1075 lsmash_destroy_codec_specific_data( specific );
1076 return -1;
1078 return 0;
1081 #define MP4SYS_AMR_STORAGE_MAGIC_LENGTH 6
1082 #define MP4SYS_AMRWB_EX_MAGIC_LENGTH 3
1084 static int amr_probe( importer_t* importer )
1086 uint8_t buf[MP4SYS_AMR_STORAGE_MAGIC_LENGTH];
1087 uint8_t wb = 0;
1088 if( fread( buf, 1, MP4SYS_AMR_STORAGE_MAGIC_LENGTH, importer->stream ) != MP4SYS_AMR_STORAGE_MAGIC_LENGTH )
1089 return -1;
1090 if( memcmp( buf, "#!AMR", MP4SYS_AMR_STORAGE_MAGIC_LENGTH-1 ) )
1091 return -1;
1092 if( buf[MP4SYS_AMR_STORAGE_MAGIC_LENGTH-1] != '\n' )
1094 if( buf[MP4SYS_AMR_STORAGE_MAGIC_LENGTH-1] != '-' )
1095 return -1;
1096 if( fread( buf, 1, MP4SYS_AMRWB_EX_MAGIC_LENGTH, importer->stream ) != MP4SYS_AMRWB_EX_MAGIC_LENGTH )
1097 return -1;
1098 if( memcmp( buf, "WB\n", MP4SYS_AMRWB_EX_MAGIC_LENGTH ) )
1099 return -1;
1100 wb = 1;
1102 lsmash_audio_summary_t *summary = (lsmash_audio_summary_t *)lsmash_create_summary( LSMASH_SUMMARY_TYPE_AUDIO );
1103 if( !summary )
1104 return -1;
1105 summary->sample_type = wb ? ISOM_CODEC_TYPE_SAWB_AUDIO : ISOM_CODEC_TYPE_SAMR_AUDIO;
1106 summary->max_au_length = wb ? 61 : 32;
1107 summary->aot = MP4A_AUDIO_OBJECT_TYPE_NULL; /* no effect */
1108 summary->frequency = (8000 << wb);
1109 summary->channels = 1;
1110 summary->sample_size = 16;
1111 summary->samples_in_frame = (160 << wb);
1112 summary->sbr_mode = MP4A_AAC_SBR_NOT_SPECIFIED; /* no effect */
1113 amr_importer_info_t *info = lsmash_malloc( sizeof(amr_importer_info_t) );
1114 if( !info )
1116 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
1117 return -1;
1119 info->wb = wb;
1120 info->samples_in_frame = summary->samples_in_frame;
1121 info->au_number = 0;
1122 importer->info = info;
1123 if( mp4sys_amr_create_damr( summary ) || lsmash_add_entry( importer->summaries, summary ) )
1125 lsmash_free( importer->info );
1126 importer->info = NULL;
1127 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
1128 return -1;
1130 return 0;
1133 static uint32_t amr_get_last_delta( importer_t* importer, uint32_t track_number )
1135 debug_if( !importer || !importer->info )
1136 return 0;
1137 amr_importer_info_t *info = (amr_importer_info_t *)importer->info;
1138 if( !info || track_number != 1 )
1139 return 0;
1140 return info->samples_in_frame;
1143 static const importer_functions amr_importer =
1145 { "amr" },
1147 amr_probe,
1148 amr_get_accessunit,
1149 amr_get_last_delta,
1150 amr_cleanup
1153 /***************************************************************************
1154 AC-3 importer
1155 ETSI TS 102 366 V1.2.1 (2008-08)
1156 ***************************************************************************/
1157 #include "codecs/a52.h"
1159 #define AC3_SAMPLE_DURATION 1536 /* 256 (samples per audio block) * 6 (audio blocks) */
1161 typedef struct
1163 importer_status status;
1164 ac3_info_t info;
1165 uint64_t next_frame_pos;
1166 uint8_t *next_dac3;
1167 uint8_t buffer[AC3_MAX_SYNCFRAME_LENGTH];
1168 uint32_t au_number;
1169 } ac3_importer_t;
1171 static void remove_ac3_importer( ac3_importer_t *ac3_imp )
1173 if( !ac3_imp )
1174 return;
1175 lsmash_bits_adhoc_cleanup( ac3_imp->info.bits );
1176 lsmash_free( ac3_imp );
1179 static ac3_importer_t *create_ac3_importer( void )
1181 ac3_importer_t *ac3_imp = (ac3_importer_t *)lsmash_malloc_zero( sizeof(ac3_importer_t) );
1182 if( !ac3_imp )
1183 return NULL;
1184 ac3_imp->info.bits = lsmash_bits_adhoc_create();
1185 if( !ac3_imp->info.bits )
1187 lsmash_free( ac3_imp );
1188 return NULL;
1190 return ac3_imp;
1193 static void ac3_importer_cleanup( importer_t *importer )
1195 debug_if( importer && importer->info )
1196 remove_ac3_importer( importer->info );
1199 static const uint32_t ac3_frame_size_table[19][3] =
1201 /* 48, 44.1, 32 */
1202 { 128, 138, 192 },
1203 { 160, 174, 240 },
1204 { 192, 208, 288 },
1205 { 224, 242, 336 },
1206 { 256, 278, 384 },
1207 { 320, 348, 480 },
1208 { 384, 416, 576 },
1209 { 448, 486, 672 },
1210 { 512, 556, 768 },
1211 { 640, 696, 960 },
1212 { 768, 834, 1152 },
1213 { 896, 974, 1344 },
1214 { 1024, 1114, 1536 },
1215 { 1280, 1392, 1920 },
1216 { 1536, 1670, 2304 },
1217 { 1792, 1950, 2688 },
1218 { 2048, 2228, 3072 },
1219 { 2304, 2506, 3456 },
1220 { 2560, 2786, 3840 }
1223 static const uint32_t ac3_channel_count_table[8] = { 2, 1, 2, 3, 3, 4, 4, 5 };
1225 #if 0
1226 /* FIXME: though this table is for AC-3 in QTFF, we don't support it yet since the structure of CODEC specific info is unknown.
1227 * ChannelLayout is given by ac3_channel_layout_table[ acmod ][ lfeon ]. */
1228 static const lsmash_channel_layout_tag ac3_channel_layout_table[8][2] =
1230 /* LFE: off LFE: on */
1231 { QT_CHANNEL_LAYOUT_UNKNOWN, QT_CHANNEL_LAYOUT_UNKNOWN }, /* FIXME: dual mono */
1232 { QT_CHANNEL_LAYOUT_MONO, QT_CHANNEL_LAYOUT_AC3_1_0_1 },
1233 { QT_CHANNEL_LAYOUT_STEREO, QT_CHANNEL_LAYOUT_DVD_4 },
1234 { QT_CHANNEL_LAYOUT_AC3_3_0, QT_CHANNEL_LAYOUT_AC3_3_0_1 },
1235 { QT_CHANNEL_LAYOUT_DVD_2, QT_CHANNEL_LAYOUT_AC3_2_1_1 },
1236 { QT_CHANNEL_LAYOUT_AC3_3_1, QT_CHANNEL_LAYOUT_AC3_3_1_1 },
1237 { QT_CHANNEL_LAYOUT_DVD_3, QT_CHANNEL_LAYOUT_DVD_18 },
1238 { QT_CHANNEL_LAYOUT_MPEG_5_0_C, QT_CHANNEL_LAYOUT_MPEG_5_1_C }
1240 #endif
1242 static lsmash_audio_summary_t *ac3_create_summary( ac3_info_t *info )
1244 lsmash_audio_summary_t *summary = (lsmash_audio_summary_t *)lsmash_create_summary( LSMASH_SUMMARY_TYPE_AUDIO );
1245 if( !summary )
1246 return NULL;
1247 lsmash_ac3_specific_parameters_t *param = &info->dac3_param;
1248 lsmash_codec_specific_t *specific = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_AC_3,
1249 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED );
1250 specific->data.unstructured = lsmash_create_ac3_specific_info( &info->dac3_param, &specific->size );
1251 if( !specific->data.unstructured
1252 || lsmash_add_entry( &summary->opaque->list, specific ) )
1254 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
1255 lsmash_destroy_codec_specific_data( specific );
1256 return NULL;
1258 summary->sample_type = ISOM_CODEC_TYPE_AC_3_AUDIO;
1259 summary->max_au_length = AC3_MAX_SYNCFRAME_LENGTH;
1260 summary->aot = MP4A_AUDIO_OBJECT_TYPE_NULL; /* no effect */
1261 summary->frequency = ac3_sample_rate_table[ param->fscod ];
1262 summary->channels = ac3_channel_count_table[ param->acmod ] + param->lfeon;
1263 summary->sample_size = 16; /* no effect */
1264 summary->samples_in_frame = AC3_SAMPLE_DURATION;
1265 summary->sbr_mode = MP4A_AAC_SBR_NOT_SPECIFIED; /* no effect */
1266 return summary;
1269 static int ac3_compare_specific_param( lsmash_ac3_specific_parameters_t *a, lsmash_ac3_specific_parameters_t *b )
1271 return (a->fscod != b->fscod)
1272 || (a->bsid != b->bsid)
1273 || (a->bsmod != b->bsmod)
1274 || (a->acmod != b->acmod)
1275 || (a->lfeon != b->lfeon)
1276 || ((a->frmsizecod >> 1) != (b->frmsizecod >> 1));
1279 static int ac3_buffer_frame( uint8_t *buffer, lsmash_bs_t *bs )
1281 uint64_t remain_size = lsmash_bs_get_remaining_buffer_size( bs );
1282 if( remain_size < AC3_MAX_SYNCFRAME_LENGTH )
1284 if( lsmash_bs_read( bs, bs->buffer.max_size ) < 0 )
1285 return -1;
1286 remain_size = lsmash_bs_get_remaining_buffer_size( bs );
1288 uint64_t copy_size = LSMASH_MIN( remain_size, AC3_MAX_SYNCFRAME_LENGTH );
1289 memcpy( buffer, lsmash_bs_get_buffer_data( bs ), copy_size );
1290 return 0;
1293 static int ac3_importer_get_accessunit( importer_t *importer, uint32_t track_number, lsmash_sample_t *buffered_sample )
1295 debug_if( !importer || !importer->info || !buffered_sample->data || !buffered_sample->length )
1296 return -1;
1297 if( !importer->info || track_number != 1 )
1298 return -1;
1299 lsmash_audio_summary_t *summary = (lsmash_audio_summary_t *)lsmash_get_entry_data( importer->summaries, track_number );
1300 if( !summary )
1301 return -1;
1302 ac3_importer_t *ac3_imp = (ac3_importer_t *)importer->info;
1303 ac3_info_t *info = &ac3_imp->info;
1304 importer_status current_status = ac3_imp->status;
1305 if( current_status == IMPORTER_ERROR )
1306 return -1;
1307 if( current_status == IMPORTER_EOF )
1309 buffered_sample->length = 0;
1310 return 0;
1312 lsmash_ac3_specific_parameters_t *param = &info->dac3_param;
1313 uint32_t frame_size = ac3_frame_size_table[ param->frmsizecod >> 1 ][ param->fscod ];
1314 if( param->fscod == 0x1 && param->frmsizecod & 0x1 )
1315 frame_size += 2;
1316 if( buffered_sample->length < frame_size )
1317 return -1;
1318 if( current_status == IMPORTER_CHANGE )
1320 lsmash_codec_specific_t *specific = isom_get_codec_specific( summary->opaque, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_AC_3 );
1321 if( specific )
1323 specific->destruct( specific->data.unstructured );
1324 specific->data.unstructured = ac3_imp->next_dac3;
1326 summary->frequency = ac3_sample_rate_table[ param->fscod ];
1327 summary->channels = ac3_channel_count_table[ param->acmod ] + param->lfeon;
1328 //summary->layout_tag = ac3_channel_layout_table[ param->acmod ][ param->lfeon ];
1330 memcpy( buffered_sample->data, ac3_imp->buffer, frame_size );
1331 buffered_sample->length = frame_size;
1332 buffered_sample->dts = ac3_imp->au_number++ * summary->samples_in_frame;
1333 buffered_sample->cts = buffered_sample->dts;
1334 buffered_sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
1335 buffered_sample->prop.pre_roll.distance = 1; /* MDCT */
1336 lsmash_bs_t *bs = info->bits->bs;
1337 ac3_imp->next_frame_pos += frame_size;
1338 lsmash_bs_read_seek( bs, ac3_imp->next_frame_pos, SEEK_SET );
1339 uint8_t syncword[2] =
1341 lsmash_bs_show_byte( bs, 0 ),
1342 lsmash_bs_show_byte( bs, 1 )
1344 if( bs->eob || (bs->eof && 0 == lsmash_bs_get_remaining_buffer_size( bs )) )
1345 ac3_imp->status = IMPORTER_EOF;
1346 else
1348 /* Parse the next syncframe header. */
1349 if( syncword[0] != 0x0b
1350 || syncword[1] != 0x77
1351 || ac3_buffer_frame( ac3_imp->buffer, bs ) < 0 )
1353 ac3_imp->status = IMPORTER_ERROR;
1354 return current_status;
1356 lsmash_ac3_specific_parameters_t current_param = info->dac3_param;
1357 ac3_parse_syncframe_header( info );
1358 if( ac3_compare_specific_param( &current_param, &info->dac3_param ) )
1360 uint32_t dummy;
1361 uint8_t *dac3 = lsmash_create_ac3_specific_info( &info->dac3_param, &dummy );
1362 if( !dac3 )
1364 ac3_imp->status = IMPORTER_ERROR;
1365 return current_status;
1367 ac3_imp->status = IMPORTER_CHANGE;
1368 ac3_imp->next_dac3 = dac3;
1370 else
1371 ac3_imp->status = IMPORTER_OK;
1373 return current_status;
1376 static int ac3_importer_probe( importer_t *importer )
1378 ac3_importer_t *ac3_imp = create_ac3_importer();
1379 if( !ac3_imp )
1380 return -1;
1381 lsmash_bits_t *bits = ac3_imp->info.bits;
1382 lsmash_bs_t *bs = bits->bs;
1383 bs->stream = importer->stream;
1384 bs->read = lsmash_fread_wrapper;
1385 bs->seek = lsmash_fseek_wrapper;
1386 bs->unseekable = importer->is_stdin;
1387 bs->buffer.max_size = AC3_MAX_SYNCFRAME_LENGTH;
1388 /* Check the syncword and parse the syncframe header */
1389 if( lsmash_bs_show_byte( bs, 0 ) != 0x0b
1390 || lsmash_bs_show_byte( bs, 1 ) != 0x77
1391 || ac3_buffer_frame( ac3_imp->buffer, bs ) < 0
1392 || ac3_parse_syncframe_header( &ac3_imp->info ) < 0 )
1393 goto fail;
1394 lsmash_audio_summary_t *summary = ac3_create_summary( &ac3_imp->info );
1395 if( !summary )
1396 goto fail;
1397 if( lsmash_add_entry( importer->summaries, summary ) < 0 )
1399 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
1400 goto fail;
1402 ac3_imp->status = IMPORTER_OK;
1403 ac3_imp->au_number = 0;
1404 importer->info = ac3_imp;
1405 return 0;
1406 fail:
1407 remove_ac3_importer( ac3_imp );
1408 return -1;
1411 static uint32_t ac3_importer_get_last_delta( importer_t *importer, uint32_t track_number )
1413 debug_if( !importer || !importer->info )
1414 return 0;
1415 ac3_importer_t *ac3_imp = (ac3_importer_t *)importer->info;
1416 if( !ac3_imp || track_number != 1 || ac3_imp->status != IMPORTER_EOF )
1417 return 0;
1418 return AC3_SAMPLE_DURATION;
1421 static const importer_functions ac3_importer =
1423 { "AC-3" },
1425 ac3_importer_probe,
1426 ac3_importer_get_accessunit,
1427 ac3_importer_get_last_delta,
1428 ac3_importer_cleanup
1431 /***************************************************************************
1432 Enhanced AC-3 importer
1433 ETSI TS 102 366 V1.2.1 (2008-08)
1434 ***************************************************************************/
1435 #define EAC3_MIN_SAMPLE_DURATION 256
1437 typedef struct
1439 importer_status status;
1440 eac3_info_t info;
1441 uint64_t next_frame_pos;
1442 uint32_t next_dec3_length;
1443 uint8_t *next_dec3;
1444 uint8_t buffer[EAC3_MAX_SYNCFRAME_LENGTH];
1445 lsmash_multiple_buffers_t *au_buffers;
1446 uint8_t *au;
1447 uint8_t *incomplete_au;
1448 uint32_t au_length;
1449 uint32_t incomplete_au_length;
1450 uint32_t au_number;
1451 uint32_t syncframe_count_in_au;
1452 } eac3_importer_t;
1454 static void remove_eac3_importer( eac3_importer_t *eac3_imp )
1456 if( !eac3_imp )
1457 return;
1458 lsmash_destroy_multiple_buffers( eac3_imp->au_buffers );
1459 lsmash_bits_adhoc_cleanup( eac3_imp->info.bits );
1460 lsmash_free( eac3_imp );
1463 static eac3_importer_t *create_eac3_importer( void )
1465 eac3_importer_t *eac3_imp = (eac3_importer_t *)lsmash_malloc_zero( sizeof(eac3_importer_t) );
1466 if( !eac3_imp )
1467 return NULL;
1468 eac3_info_t *info = &eac3_imp->info;
1469 info->bits = lsmash_bits_adhoc_create();
1470 if( !info->bits )
1472 lsmash_free( eac3_imp );
1473 return NULL;
1475 eac3_imp->au_buffers = lsmash_create_multiple_buffers( 2, EAC3_MAX_SYNCFRAME_LENGTH );
1476 if( !eac3_imp->au_buffers )
1478 lsmash_bits_adhoc_cleanup( info->bits );
1479 lsmash_free( eac3_imp );
1480 return NULL;
1482 eac3_imp->au = lsmash_withdraw_buffer( eac3_imp->au_buffers, 1 );
1483 eac3_imp->incomplete_au = lsmash_withdraw_buffer( eac3_imp->au_buffers, 2 );
1484 return eac3_imp;
1487 static void eac3_importer_cleanup( importer_t *importer )
1489 debug_if( importer && importer->info )
1490 remove_eac3_importer( importer->info );
1493 static void eac3_update_sample_rate( lsmash_audio_summary_t *summary, lsmash_eac3_specific_parameters_t *dec3_param )
1495 /* Additional independent substreams 1 to 7 must be encoded at the same sample rate as independent substream 0. */
1496 summary->frequency = ac3_sample_rate_table[ dec3_param->independent_info[0].fscod ];
1497 if( summary->frequency == 0 )
1499 static const uint32_t eac3_reduced_sample_rate_table[4] = { 24000, 22050, 16000, 0 };
1500 summary->frequency = eac3_reduced_sample_rate_table[ dec3_param->independent_info[0].fscod2 ];
1504 #if 0
1505 /* FIXME: though this table is for EAC-3 in QTFF, we don't support it yet since the structure of CODEC specific info is unknown. */
1506 static void eac3_update_channel_layout( lsmash_audio_summary_t *summary, lsmash_eac3_substream_info_t *independent_info )
1508 if( independent_info->chan_loc == 0 )
1510 summary->layout_tag = ac3_channel_layout_table[ independent_info->acmod ][ independent_info->lfeon ];
1511 return;
1513 else if( independent_info->acmod != 0x7 )
1515 summary->layout_tag = QT_CHANNEL_LAYOUT_UNKNOWN;
1516 return;
1518 /* OK. All L, C, R, Ls and Rs exsist. */
1519 if( !independent_info->lfeon )
1521 if( independent_info->chan_loc == 0x80 )
1522 summary->layout_tag = QT_CHANNEL_LAYOUT_EAC_7_0_A;
1523 else if( independent_info->chan_loc == 0x40 )
1524 summary->layout_tag = QT_CHANNEL_LAYOUT_EAC_6_0_A;
1525 else
1526 summary->layout_tag = QT_CHANNEL_LAYOUT_UNKNOWN;
1527 return;
1529 /* Also LFE exsists. */
1530 static const struct
1532 uint16_t chan_loc;
1533 lsmash_channel_layout_tag tag;
1534 } eac3_channel_layout_table[]
1536 { 0x100, QT_CHANNEL_LAYOUT_EAC3_7_1_B },
1537 { 0x80, QT_CHANNEL_LAYOUT_EAC3_7_1_A },
1538 { 0x40, QT_CHANNEL_LAYOUT_EAC3_6_1_A },
1539 { 0x20, QT_CHANNEL_LAYOUT_EAC3_6_1_B },
1540 { 0x10, QT_CHANNEL_LAYOUT_EAC3_7_1_C },
1541 { 0x10, QT_CHANNEL_LAYOUT_EAC3_7_1_D },
1542 { 0x4, QT_CHANNEL_LAYOUT_EAC3_7_1_E },
1543 { 0x2, QT_CHANNEL_LAYOUT_EAC3_6_1_C },
1544 { 0x60, QT_CHANNEL_LAYOUT_EAC3_7_1_F },
1545 { 0x42, QT_CHANNEL_LAYOUT_EAC3_7_1_G },
1546 { 0x22, QT_CHANNEL_LAYOUT_EAC3_7_1_H },
1547 { 0 }
1549 for( int i = 0; eac3_channel_layout_table[i].chan_loc; i++ )
1550 if( independent_info->chan_loc == eac3_channel_layout_table[i].chan_loc )
1552 summary->layout_tag = eac3_channel_layout_table[i].tag;
1553 return;
1555 summary->layout_tag = QT_CHANNEL_LAYOUT_UNKNOWN;
1557 #endif
1559 static void eac3_update_channel_info( lsmash_audio_summary_t *summary, lsmash_eac3_specific_parameters_t *dec3_param )
1561 summary->channels = 0;
1562 for( int i = 0; i <= dec3_param->num_ind_sub; i++ )
1564 int channel_count = 0;
1565 lsmash_eac3_substream_info_t *independent_info = &dec3_param->independent_info[i];
1566 channel_count = ac3_channel_count_table[ independent_info->acmod ] /* L/C/R/Ls/Rs combination */
1567 + 2 * !!(independent_info->chan_loc & 0x100) /* Lc/Rc pair */
1568 + 2 * !!(independent_info->chan_loc & 0x80) /* Lrs/Rrs pair */
1569 + !!(independent_info->chan_loc & 0x40) /* Cs */
1570 + !!(independent_info->chan_loc & 0x20) /* Ts */
1571 + 2 * !!(independent_info->chan_loc & 0x10) /* Lsd/Rsd pair */
1572 + 2 * !!(independent_info->chan_loc & 0x8) /* Lw/Rw pair */
1573 + 2 * !!(independent_info->chan_loc & 0x4) /* Lvh/Rvh pair */
1574 + !!(independent_info->chan_loc & 0x2) /* Cvh */
1575 + !!(independent_info->chan_loc & 0x1) /* LFE2 */
1576 + independent_info->lfeon; /* LFE */
1577 if( channel_count > summary->channels )
1579 /* Pick the maximum number of channels. */
1580 summary->channels = channel_count;
1581 //eac3_update_channel_layout( summary, independent_info );
1586 static int eac3_importer_get_next_accessunit_internal( importer_t *importer )
1588 int au_completed = 0;
1589 eac3_importer_t *eac3_imp = (eac3_importer_t *)importer->info;
1590 eac3_info_t *info = &eac3_imp->info;
1591 lsmash_bs_t *bs = info->bits->bs;
1592 while( !au_completed )
1594 /* Read data from the stream if needed. */
1595 eac3_imp->next_frame_pos += info->frame_size;
1596 lsmash_bs_read_seek( bs, eac3_imp->next_frame_pos, SEEK_SET );
1597 uint64_t remain_size = lsmash_bs_get_remaining_buffer_size( bs );
1598 if( remain_size < EAC3_MAX_SYNCFRAME_LENGTH )
1600 if( lsmash_bs_read( bs, bs->buffer.max_size ) < 0 )
1602 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to read data from the stream.\n" );
1603 return -1;
1605 remain_size = lsmash_bs_get_remaining_buffer_size( bs );
1607 uint64_t copy_size = LSMASH_MIN( remain_size, EAC3_MAX_SYNCFRAME_LENGTH );
1608 memcpy( eac3_imp->buffer, lsmash_bs_get_buffer_data( bs ), copy_size );
1609 /* Check the remainder length of the buffer.
1610 * If there is enough length, then parse the syncframe in it.
1611 * The length 5 is the required byte length to get frame size. */
1612 if( bs->eob || (bs->eof && remain_size < 5) )
1614 /* Reached the end of stream.
1615 * According to ETSI TS 102 366 V1.2.1 (2008-08),
1616 * one access unit consists of 6 audio blocks and begins with independent substream 0.
1617 * The specification doesn't mention the case where a enhanced AC-3 stream ends at non-mod6 audio blocks.
1618 * At the end of the stream, therefore, we might make an access unit which has less than 6 audio blocks anyway. */
1619 eac3_imp->status = IMPORTER_EOF;
1620 au_completed = !!eac3_imp->incomplete_au_length;
1621 if( !au_completed )
1623 /* No more access units in the stream. */
1624 if( lsmash_bs_get_remaining_buffer_size( bs ) )
1626 lsmash_log( importer, LSMASH_LOG_WARNING, "the stream is truncated at the end.\n" );
1627 return -1;
1629 return 0;
1631 if( !info->dec3_param_initialized )
1632 eac3_update_specific_param( info );
1634 else
1636 /* Check the syncword. */
1637 if( lsmash_bs_show_byte( bs, 0 ) != 0x0b
1638 || lsmash_bs_show_byte( bs, 1 ) != 0x77 )
1640 lsmash_log( importer, LSMASH_LOG_ERROR, "a syncword is not found.\n" );
1641 return -1;
1643 /* Parse syncframe. */
1644 info->frame_size = 0;
1645 if( eac3_parse_syncframe( info ) < 0 )
1647 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to parse syncframe.\n" );
1648 return -1;
1650 if( remain_size < info->frame_size )
1652 lsmash_log( importer, LSMASH_LOG_ERROR, "a frame is truncated.\n" );
1653 return -1;
1655 int independent = info->strmtyp != 0x1;
1656 if( independent && info->substreamid == 0x0 )
1658 if( info->number_of_audio_blocks == 6 )
1660 /* Encountered the first syncframe of the next access unit. */
1661 info->number_of_audio_blocks = 0;
1662 au_completed = 1;
1664 else if( info->number_of_audio_blocks > 6 )
1666 lsmash_log( importer, LSMASH_LOG_ERROR, "greater than 6 consecutive independent substreams.\n" );
1667 return -1;
1669 info->number_of_audio_blocks += eac3_audio_block_table[ info->numblkscod ];
1670 info->number_of_independent_substreams = 0;
1672 else if( info->syncframe_count == 0 )
1674 /* The first syncframe in an AU must be independent and assigned substream ID 0. */
1675 lsmash_log( importer, LSMASH_LOG_ERROR, "the first syncframe is NOT an independent substream.\n" );
1676 return -1;
1678 if( independent )
1679 info->independent_info[info->number_of_independent_substreams ++].num_dep_sub = 0;
1680 else
1681 ++ info->independent_info[info->number_of_independent_substreams - 1].num_dep_sub;
1683 if( au_completed )
1685 memcpy( eac3_imp->au, eac3_imp->incomplete_au, eac3_imp->incomplete_au_length );
1686 eac3_imp->au_length = eac3_imp->incomplete_au_length;
1687 eac3_imp->incomplete_au_length = 0;
1688 eac3_imp->syncframe_count_in_au = info->syncframe_count;
1689 info->syncframe_count = 0;
1690 if( eac3_imp->status == IMPORTER_EOF )
1691 break;
1693 /* Increase buffer size to store AU if short. */
1694 if( eac3_imp->incomplete_au_length + info->frame_size > eac3_imp->au_buffers->buffer_size )
1696 lsmash_multiple_buffers_t *temp = lsmash_resize_multiple_buffers( eac3_imp->au_buffers,
1697 eac3_imp->au_buffers->buffer_size + EAC3_MAX_SYNCFRAME_LENGTH );
1698 if( !temp )
1699 return -1;
1700 eac3_imp->au_buffers = temp;
1701 eac3_imp->au = lsmash_withdraw_buffer( eac3_imp->au_buffers, 1 );
1702 eac3_imp->incomplete_au = lsmash_withdraw_buffer( eac3_imp->au_buffers, 2 );
1704 /* Append syncframe data. */
1705 memcpy( eac3_imp->incomplete_au + eac3_imp->incomplete_au_length, eac3_imp->buffer, info->frame_size );
1706 eac3_imp->incomplete_au_length += info->frame_size;
1707 ++ info->syncframe_count;
1709 return info->bits->bs->error ? -1 : 0;
1712 static int eac3_importer_get_accessunit( importer_t *importer, uint32_t track_number, lsmash_sample_t *buffered_sample )
1714 debug_if( !importer || !importer->info || !buffered_sample->data || !buffered_sample->length )
1715 return -1;
1716 if( !importer->info || track_number != 1 )
1717 return -1;
1718 lsmash_audio_summary_t *summary = (lsmash_audio_summary_t *)lsmash_get_entry_data( importer->summaries, track_number );
1719 if( !summary )
1720 return -1;
1721 eac3_importer_t *eac3_imp = (eac3_importer_t *)importer->info;
1722 eac3_info_t *info = &eac3_imp->info;
1723 importer_status current_status = eac3_imp->status;
1724 if( current_status == IMPORTER_ERROR || buffered_sample->length < eac3_imp->au_length )
1725 return -1;
1726 if( current_status == IMPORTER_EOF && eac3_imp->au_length == 0 )
1728 buffered_sample->length = 0;
1729 return 0;
1731 if( current_status == IMPORTER_CHANGE )
1733 lsmash_codec_specific_t *specific = isom_get_codec_specific( summary->opaque, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3 );
1734 if( specific )
1736 specific->destruct( specific->data.unstructured );
1737 specific->data.unstructured = eac3_imp->next_dec3;
1738 specific->size = eac3_imp->next_dec3_length;
1740 summary->max_au_length = eac3_imp->syncframe_count_in_au * EAC3_MAX_SYNCFRAME_LENGTH;
1741 eac3_update_sample_rate( summary, &info->dec3_param );
1742 eac3_update_channel_info( summary, &info->dec3_param );
1744 memcpy( buffered_sample->data, eac3_imp->au, eac3_imp->au_length );
1745 buffered_sample->length = eac3_imp->au_length;
1746 buffered_sample->dts = eac3_imp->au_number++ * summary->samples_in_frame;
1747 buffered_sample->cts = buffered_sample->dts;
1748 buffered_sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
1749 buffered_sample->prop.pre_roll.distance = 1; /* MDCT */
1750 if( eac3_imp->status == IMPORTER_EOF )
1752 eac3_imp->au_length = 0;
1753 return 0;
1755 uint32_t old_syncframe_count_in_au = eac3_imp->syncframe_count_in_au;
1756 if( eac3_importer_get_next_accessunit_internal( importer ) < 0 )
1758 eac3_imp->status = IMPORTER_ERROR;
1759 return current_status;
1761 if( eac3_imp->syncframe_count_in_au )
1763 /* Check sample description change. */
1764 uint32_t new_length;
1765 uint8_t *dec3 = lsmash_create_eac3_specific_info( &info->dec3_param, &new_length );
1766 if( !dec3 )
1768 eac3_imp->status = IMPORTER_ERROR;
1769 return current_status;
1771 lsmash_codec_specific_t *specific = isom_get_codec_specific( summary->opaque, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3 );
1772 if( (eac3_imp->syncframe_count_in_au > old_syncframe_count_in_au)
1773 || (specific && (new_length != specific->size || memcmp( dec3, specific->data.unstructured, specific->size ))) )
1775 eac3_imp->status = IMPORTER_CHANGE;
1776 eac3_imp->next_dec3 = dec3;
1777 eac3_imp->next_dec3_length = new_length;
1779 else
1781 if( eac3_imp->status != IMPORTER_EOF )
1782 eac3_imp->status = IMPORTER_OK;
1783 lsmash_free( dec3 );
1786 return current_status;
1789 static lsmash_audio_summary_t *eac3_create_summary( eac3_importer_t *eac3_imp )
1791 lsmash_audio_summary_t *summary = (lsmash_audio_summary_t *)lsmash_create_summary( LSMASH_SUMMARY_TYPE_AUDIO );
1792 if( !summary )
1793 return NULL;
1794 eac3_info_t *info = &eac3_imp->info;
1795 lsmash_codec_specific_t *specific = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3,
1796 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED );
1797 specific->data.unstructured = lsmash_create_eac3_specific_info( &info->dec3_param, &specific->size );
1798 if( !specific->data.unstructured
1799 || lsmash_add_entry( &summary->opaque->list, specific ) < 0 )
1801 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
1802 lsmash_destroy_codec_specific_data( specific );
1803 return NULL;
1805 summary->sample_type = ISOM_CODEC_TYPE_EC_3_AUDIO;
1806 summary->max_au_length = eac3_imp->syncframe_count_in_au * EAC3_MAX_SYNCFRAME_LENGTH;
1807 summary->aot = MP4A_AUDIO_OBJECT_TYPE_NULL; /* no effect */
1808 summary->sample_size = 16; /* no effect */
1809 summary->samples_in_frame = EAC3_MIN_SAMPLE_DURATION * 6; /* 256 (samples per audio block) * 6 (audio blocks) */
1810 summary->sbr_mode = MP4A_AAC_SBR_NOT_SPECIFIED; /* no effect */
1811 eac3_update_sample_rate( summary, &info->dec3_param );
1812 eac3_update_channel_info( summary, &info->dec3_param );
1813 return summary;
1816 static int eac3_importer_probe( importer_t *importer )
1818 eac3_importer_t *eac3_imp = create_eac3_importer();
1819 if( !eac3_imp )
1820 return -1;
1821 lsmash_bits_t *bits = eac3_imp->info.bits;
1822 lsmash_bs_t *bs = bits->bs;
1823 bs->stream = importer->stream;
1824 bs->read = lsmash_fread_wrapper;
1825 bs->seek = lsmash_fseek_wrapper;
1826 bs->unseekable = importer->is_stdin;
1827 bs->buffer.max_size = EAC3_MAX_SYNCFRAME_LENGTH;
1828 importer->info = eac3_imp;
1829 if( eac3_importer_get_next_accessunit_internal( importer ) < 0 )
1830 goto fail;
1831 lsmash_audio_summary_t *summary = eac3_create_summary( eac3_imp );
1832 if( !summary )
1833 goto fail;
1834 if( eac3_imp->status != IMPORTER_EOF )
1835 eac3_imp->status = IMPORTER_OK;
1836 eac3_imp->au_number = 0;
1837 if( lsmash_add_entry( importer->summaries, summary ) < 0 )
1839 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
1840 goto fail;
1842 return 0;
1843 fail:
1844 remove_eac3_importer( eac3_imp );
1845 importer->info = NULL;
1846 return -1;
1849 static uint32_t eac3_importer_get_last_delta( importer_t *importer, uint32_t track_number )
1851 debug_if( !importer || !importer->info )
1852 return 0;
1853 eac3_importer_t *eac3_imp = (eac3_importer_t *)importer->info;
1854 if( !eac3_imp || track_number != 1 || eac3_imp->status != IMPORTER_EOF || eac3_imp->au_length )
1855 return 0;
1856 return EAC3_MIN_SAMPLE_DURATION * eac3_imp->info.number_of_audio_blocks;
1859 static const importer_functions eac3_importer =
1861 { "Enhanced AC-3", offsetof( importer_t, log_level ) },
1863 eac3_importer_probe,
1864 eac3_importer_get_accessunit,
1865 eac3_importer_get_last_delta,
1866 eac3_importer_cleanup
1869 /***************************************************************************
1870 MPEG-4 ALS importer
1871 ISO/IEC 14496-3 2009 Fourth edition
1872 ***************************************************************************/
1873 #define ALSSC_TWELVE_LENGTH 22
1875 typedef struct
1877 uint32_t size;
1878 uint32_t samp_freq;
1879 uint32_t samples;
1880 uint32_t channels;
1881 uint16_t frame_length;
1882 uint8_t resolution;
1883 uint8_t random_access;
1884 uint8_t ra_flag;
1885 uint32_t access_unit_size;
1886 uint32_t number_of_ra_units;
1887 uint32_t *ra_unit_size;
1888 uint8_t *sc_data;
1889 size_t alloc;
1890 } als_specific_config_t;
1892 typedef struct
1894 importer_status status;
1895 lsmash_bs_t *bs;
1896 als_specific_config_t alssc;
1897 uint32_t samples_in_frame;
1898 uint32_t au_number;
1899 } mp4a_als_importer_t;
1901 static void remove_mp4a_als_importer( mp4a_als_importer_t *als_imp )
1903 lsmash_bs_cleanup( als_imp->bs );
1904 lsmash_free( als_imp->alssc.ra_unit_size );
1905 lsmash_free( als_imp->alssc.sc_data );
1906 lsmash_free( als_imp );
1909 static void mp4a_als_importer_cleanup( importer_t *importer )
1911 debug_if( importer && importer->info )
1912 remove_mp4a_als_importer( importer->info );
1915 static mp4a_als_importer_t *create_mp4a_als_importer( importer_t *importer )
1917 mp4a_als_importer_t *als_imp = lsmash_malloc_zero( sizeof(mp4a_als_importer_t) );
1918 if( !als_imp )
1919 return NULL;
1920 als_imp->bs = lsmash_bs_create();
1921 if( !als_imp->bs )
1923 lsmash_free( als_imp );
1924 return NULL;
1926 return als_imp;
1929 static void als_copy_from_buffer( als_specific_config_t *alssc, lsmash_bs_t *bs, uint64_t size )
1931 if( alssc->alloc < size )
1933 size_t alloc = alssc->alloc ? (alssc->alloc << 1) : (1 << 10);
1934 uint8_t *temp = lsmash_realloc( alssc->sc_data, alloc );
1935 if( !temp )
1936 return;
1937 alssc->sc_data = temp;
1938 alssc->alloc = alloc;
1940 memcpy( alssc->sc_data + alssc->size, lsmash_bs_get_buffer_data( bs ), size );
1941 alssc->size += size;
1942 lsmash_bs_read_seek( bs, size, SEEK_CUR );
1945 static int als_parse_specific_config( mp4a_als_importer_t *als_imp )
1947 lsmash_bs_t *bs = als_imp->bs;
1948 /* Check ALS identifier( = 0x414C5300). */
1949 if( 0x414C5300 != lsmash_bs_show_be32( bs, 0 ) )
1950 return -1;
1951 als_specific_config_t *alssc = &als_imp->alssc;
1952 alssc->samp_freq = lsmash_bs_show_be32( bs, 4 );
1953 alssc->samples = lsmash_bs_show_be32( bs, 8 );
1954 if( alssc->samples == 0xffffffff )
1955 return -1; /* We don't support this case. */
1956 alssc->channels = lsmash_bs_show_be16( bs, 12 );
1957 alssc->resolution = (lsmash_bs_show_byte( bs, 14 ) & 0x1c) >> 2;
1958 if( alssc->resolution > 3 )
1959 return -1; /* reserved */
1960 alssc->frame_length = lsmash_bs_show_be16( bs, 15 );
1961 alssc->random_access = lsmash_bs_show_byte( bs, 17 );
1962 alssc->ra_flag = (lsmash_bs_show_byte( bs, 18 ) & 0xc0) >> 6;
1963 if( alssc->ra_flag == 0 )
1964 return -1; /* We don't support this case. */
1965 #if 0
1966 if( alssc->samples == 0xffffffff && alssc->ra_flag == 2 )
1967 return -1;
1968 #endif
1969 uint8_t temp8 = lsmash_bs_show_byte( bs, 20 );
1970 int chan_sort = !!(temp8 & 0x1);
1971 if( alssc->channels == 0 )
1973 if( temp8 & 0x8 )
1974 return -1; /* If channels = 0 (mono), joint_stereo = 0. */
1975 else if( temp8 & 0x4 )
1976 return -1; /* If channels = 0 (mono), mc_coding = 0. */
1977 else if( chan_sort )
1978 return -1; /* If channels = 0 (mono), chan_sort = 0. */
1980 int chan_config = !!(temp8 & 0x2);
1981 temp8 = lsmash_bs_show_byte( bs, 21 );
1982 int crc_enabled = !!(temp8 & 0x80);
1983 int aux_data_enabled = !!(temp8 & 0x1);
1984 als_copy_from_buffer( alssc, bs, ALSSC_TWELVE_LENGTH );
1985 if( chan_config )
1987 /* chan_config_info */
1988 lsmash_bs_read( bs, 2 );
1989 als_copy_from_buffer( alssc, bs, 2 );
1991 if( chan_sort )
1993 uint32_t ChBits = lsmash_ceil_log2( alssc->channels + 1 );
1994 uint32_t chan_pos_length = (alssc->channels + 1) * ChBits;
1995 chan_pos_length = ((uint64_t)chan_pos_length + 7) / 8; /* byte_align */
1996 lsmash_bs_read( bs, chan_pos_length );
1997 als_copy_from_buffer( alssc, bs, chan_pos_length );
1999 /* orig_header, orig_trailer and crc. */
2001 uint32_t header_size = lsmash_bs_show_be32( bs, 0 );
2002 uint32_t trailer_size = lsmash_bs_show_be32( bs, 4 );
2003 als_copy_from_buffer( alssc, bs, 8 );
2004 if( header_size != 0xffffffff )
2006 lsmash_bs_read( bs, header_size );
2007 als_copy_from_buffer( alssc, bs, header_size );
2009 if( trailer_size != 0xffffffff )
2011 lsmash_bs_read( bs, trailer_size );
2012 als_copy_from_buffer( alssc, bs, trailer_size );
2014 if( crc_enabled )
2016 lsmash_bs_read( bs, 4 );
2017 als_copy_from_buffer( alssc, bs, 4 );
2020 /* Random access units */
2022 uint32_t number_of_frames = ((alssc->samples + alssc->frame_length) / (alssc->frame_length + 1));
2023 if( alssc->random_access != 0 )
2024 alssc->number_of_ra_units = ((uint64_t)number_of_frames + alssc->random_access - 1) / alssc->random_access;
2025 else
2026 alssc->number_of_ra_units = 0;
2027 if( alssc->ra_flag == 2 && alssc->random_access != 0 )
2029 /* We don't copy all ra_unit_size into alssc->sc_data. */
2030 int64_t read_size = (int64_t)alssc->number_of_ra_units * 4;
2031 int64_t end_offset = lsmash_bs_get_stream_pos( bs ) + read_size;
2032 alssc->ra_unit_size = lsmash_malloc( read_size );
2033 if( !alssc->ra_unit_size )
2034 return -1;
2035 uint32_t max_ra_unit_size = 0;
2036 for( uint32_t i = 0; i < alssc->number_of_ra_units; i++ )
2038 alssc->ra_unit_size[i] = lsmash_bs_get_be32( bs );
2039 max_ra_unit_size = LSMASH_MAX( max_ra_unit_size, alssc->ra_unit_size[i] );
2041 lsmash_bs_read_seek( bs, end_offset, SEEK_SET );
2043 else
2044 alssc->ra_unit_size = NULL;
2046 /* auxiliary data */
2047 if( aux_data_enabled )
2049 uint32_t aux_size = lsmash_bs_show_be32( bs, 0 );
2050 als_copy_from_buffer( alssc, bs, 4 );
2051 if( aux_size && aux_size != 0xffffffff )
2053 lsmash_bs_read( bs, aux_size );
2054 als_copy_from_buffer( alssc, bs, aux_size );
2057 /* Set 0 to ra_flag. We will remove ra_unit_size in each access unit. */
2058 alssc->sc_data[18] &= 0x3f;
2059 return 0;
2062 static int mp4a_als_importer_get_accessunit( importer_t *importer, uint32_t track_number, lsmash_sample_t *buffered_sample )
2064 debug_if( !importer || !importer->info || !buffered_sample->data || !buffered_sample->length )
2065 return -1;
2066 if( !importer->info || track_number != 1 )
2067 return -1;
2068 lsmash_audio_summary_t *summary = (lsmash_audio_summary_t *)lsmash_get_entry_data( importer->summaries, track_number );
2069 if( !summary )
2070 return -1;
2071 mp4a_als_importer_t *als_imp = (mp4a_als_importer_t *)importer->info;
2072 importer_status current_status = als_imp->status;
2073 if( current_status == IMPORTER_EOF )
2075 buffered_sample->length = 0;
2076 return 0;
2078 lsmash_bs_t *bs = als_imp->bs;
2079 als_specific_config_t *alssc = &als_imp->alssc;
2080 if( alssc->number_of_ra_units == 0 )
2082 memcpy( buffered_sample->data, lsmash_bs_get_buffer_data( bs ), alssc->access_unit_size );
2083 buffered_sample->length = alssc->access_unit_size;
2084 buffered_sample->cts = 0;
2085 buffered_sample->dts = 0;
2086 buffered_sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
2087 als_imp->status = IMPORTER_EOF;
2088 return 0;
2090 uint32_t au_length;
2091 if( alssc->ra_flag == 2 )
2092 au_length = alssc->ra_unit_size[ als_imp->au_number ];
2093 else /* if( alssc->ra_flag == 1 ) */
2094 /* We don't export ra_unit_size into a sample. */
2095 au_length = lsmash_bs_get_be32( bs );
2096 if( buffered_sample->length < au_length )
2098 lsmash_log( importer, LSMASH_LOG_WARNING, "the buffer has not enough size.\n" );
2099 return -1;
2101 if( lsmash_bs_get_bytes_ex( bs, au_length, buffered_sample->data ) != au_length )
2103 lsmash_log( importer, LSMASH_LOG_WARNING, "failed to read an access unit.\n" );
2104 als_imp->status = IMPORTER_ERROR;
2105 return -1;
2107 buffered_sample->length = au_length;
2108 buffered_sample->dts = als_imp->au_number ++ * als_imp->samples_in_frame;
2109 buffered_sample->cts = buffered_sample->dts;
2110 buffered_sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
2111 if( als_imp->au_number == alssc->number_of_ra_units )
2112 als_imp->status = IMPORTER_EOF;
2113 return 0;
2116 #undef CHECK_UPDATE
2118 static lsmash_audio_summary_t *als_create_summary( lsmash_bs_t *bs, als_specific_config_t *alssc )
2120 lsmash_audio_summary_t *summary = (lsmash_audio_summary_t *)lsmash_create_summary( LSMASH_SUMMARY_TYPE_AUDIO );
2121 if( !summary )
2122 return NULL;
2123 summary->sample_type = ISOM_CODEC_TYPE_MP4A_AUDIO;
2124 summary->aot = MP4A_AUDIO_OBJECT_TYPE_ALS;
2125 summary->frequency = alssc->samp_freq;
2126 summary->channels = alssc->channels + 1;
2127 summary->sample_size = (alssc->resolution + 1) * 8;
2128 summary->sbr_mode = MP4A_AAC_SBR_NOT_SPECIFIED; /* no effect */
2129 if( alssc->random_access != 0 )
2131 summary->samples_in_frame = (alssc->frame_length + 1) * alssc->random_access;
2132 summary->max_au_length = summary->channels * (summary->sample_size / 8) * summary->samples_in_frame;
2134 else
2136 /* Read the remainder of overall stream as an access unit. */
2137 alssc->access_unit_size = lsmash_bs_get_remaining_buffer_size( bs );
2138 while( !bs->eof )
2140 if( lsmash_bs_read( bs, bs->buffer.max_size ) < 0 )
2141 return NULL;
2142 alssc->access_unit_size = lsmash_bs_get_remaining_buffer_size( bs );
2144 summary->max_au_length = alssc->access_unit_size;
2145 summary->samples_in_frame = 0; /* hack for mp4sys_als_importer_get_last_delta() */
2147 uint32_t data_length;
2148 uint8_t *data = mp4a_export_AudioSpecificConfig( MP4A_AUDIO_OBJECT_TYPE_ALS,
2149 summary->frequency, summary->channels, summary->sbr_mode,
2150 alssc->sc_data, alssc->size, &data_length );
2151 if( !data )
2153 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
2154 return NULL;
2156 lsmash_codec_specific_t *specific = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG,
2157 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED );
2158 if( !specific )
2160 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
2161 lsmash_free( data );
2162 return NULL;
2164 lsmash_mp4sys_decoder_parameters_t *param = (lsmash_mp4sys_decoder_parameters_t *)specific->data.structured;
2165 param->objectTypeIndication = MP4SYS_OBJECT_TYPE_Audio_ISO_14496_3;
2166 param->streamType = MP4SYS_STREAM_TYPE_AudioStream;
2167 if( lsmash_set_mp4sys_decoder_specific_info( param, data, data_length ) < 0 )
2169 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
2170 lsmash_destroy_codec_specific_data( specific );
2171 lsmash_free( data );
2172 return NULL;
2174 lsmash_free( data );
2175 if( lsmash_add_entry( &summary->opaque->list, specific ) < 0 )
2177 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
2178 lsmash_destroy_codec_specific_data( specific );
2179 return NULL;
2181 return summary;
2184 static int mp4a_als_importer_probe( importer_t *importer )
2186 mp4a_als_importer_t *als_imp = create_mp4a_als_importer( importer );
2187 if( !als_imp )
2188 return -1;
2189 lsmash_bs_t *bs = als_imp->bs;
2190 bs->stream = importer->stream;
2191 bs->read = lsmash_fread_wrapper;
2192 bs->seek = lsmash_fseek_wrapper;
2193 bs->unseekable = importer->is_stdin;
2194 bs->buffer.max_size = BS_MAX_DEFAULT_READ_SIZE;
2195 /* Parse ALS specific configuration. */
2196 if( als_parse_specific_config( als_imp ) < 0 )
2197 goto fail;
2198 lsmash_audio_summary_t *summary = als_create_summary( bs, &als_imp->alssc );
2199 if( !summary )
2200 goto fail;
2201 /* importer status */
2202 als_imp->status = IMPORTER_OK;
2203 als_imp->samples_in_frame = summary->samples_in_frame;
2204 if( lsmash_add_entry( importer->summaries, summary ) < 0 )
2206 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
2207 goto fail;
2209 importer->info = als_imp;
2210 return 0;
2211 fail:
2212 remove_mp4a_als_importer( als_imp );
2213 return -1;
2216 static uint32_t mp4a_als_importer_get_last_delta( importer_t *importer, uint32_t track_number )
2218 debug_if( !importer || !importer->info )
2219 return 0;
2220 mp4a_als_importer_t *als_imp = (mp4a_als_importer_t *)importer->info;
2221 if( !als_imp || track_number != 1 || als_imp->status != IMPORTER_EOF )
2222 return 0;
2223 als_specific_config_t *alssc = &als_imp->alssc;
2224 /* If alssc->number_of_ra_units == 0, then the last sample duration is just alssc->samples
2225 * since als_create_summary sets 0 to summary->samples_in_frame i.e. als_imp->samples_in_frame. */
2226 return alssc->samples - (alssc->number_of_ra_units - 1) * als_imp->samples_in_frame;
2229 static const importer_functions mp4a_als_importer =
2231 { "MPEG-4 ALS", offsetof( importer_t, log_level ) },
2233 mp4a_als_importer_probe,
2234 mp4a_als_importer_get_accessunit,
2235 mp4a_als_importer_get_last_delta,
2236 mp4a_als_importer_cleanup
2239 /***************************************************************************
2240 DTS importer
2241 ETSI TS 102 114 V1.2.1 (2002-12)
2242 ETSI TS 102 114 V1.3.1 (2011-08)
2243 ETSI TS 102 114 V1.4.1 (2012-09)
2244 ***************************************************************************/
2245 #include "codecs/dts.h"
2247 typedef struct
2249 importer_status status;
2250 dts_info_t info;
2251 uint64_t next_frame_pos;
2252 uint8_t buffer[DTS_MAX_EXSS_SIZE];
2253 lsmash_multiple_buffers_t *au_buffers;
2254 uint8_t *au;
2255 uint32_t au_length;
2256 uint8_t *incomplete_au;
2257 uint32_t incomplete_au_length;
2258 uint32_t au_number;
2259 } dts_importer_t;
2261 static void remove_dts_importer( dts_importer_t *dts_imp )
2263 if( !dts_imp )
2264 return;
2265 lsmash_destroy_multiple_buffers( dts_imp->au_buffers );
2266 lsmash_bits_adhoc_cleanup( dts_imp->info.bits );
2267 lsmash_free( dts_imp );
2270 static dts_importer_t *create_dts_importer( void )
2272 dts_importer_t *dts_imp = (dts_importer_t *)lsmash_malloc_zero( sizeof(dts_importer_t) );
2273 if( !dts_imp )
2274 return NULL;
2275 dts_info_t *dts_info = &dts_imp->info;
2276 dts_info->bits = lsmash_bits_adhoc_create();
2277 if( !dts_info->bits )
2279 lsmash_free( dts_imp );
2280 return NULL;
2282 dts_imp->au_buffers = lsmash_create_multiple_buffers( 2, DTS_MAX_EXSS_SIZE );
2283 if( !dts_imp->au_buffers )
2285 lsmash_bits_adhoc_cleanup( dts_info->bits );
2286 lsmash_free( dts_imp );
2287 return NULL;
2289 dts_imp->au = lsmash_withdraw_buffer( dts_imp->au_buffers, 1 );
2290 dts_imp->incomplete_au = lsmash_withdraw_buffer( dts_imp->au_buffers, 2 );
2291 dts_setup_parser( dts_info );
2292 return dts_imp;
2295 static void dts_importer_cleanup( importer_t *importer )
2297 debug_if( importer && importer->info )
2298 remove_dts_importer( importer->info );
2301 static int dts_importer_get_next_accessunit_internal( importer_t *importer )
2303 int au_completed = 0;
2304 dts_importer_t *dts_imp = (dts_importer_t *)importer->info;
2305 dts_info_t *info = &dts_imp->info;
2306 lsmash_bs_t *bs = info->bits->bs;
2307 while( !au_completed )
2309 /* Read data from the stream if needed. */
2310 dts_imp->next_frame_pos += info->frame_size;
2311 lsmash_bs_read_seek( bs, dts_imp->next_frame_pos, SEEK_SET );
2312 uint64_t remain_size = lsmash_bs_get_remaining_buffer_size( bs );
2313 if( remain_size < DTS_MAX_EXSS_SIZE )
2315 if( lsmash_bs_read( bs, bs->buffer.max_size ) < 0 )
2317 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to read data from the stream.\n" );
2318 return -1;
2320 remain_size = lsmash_bs_get_remaining_buffer_size( bs );
2322 memcpy( dts_imp->buffer, lsmash_bs_get_buffer_data( bs ), LSMASH_MIN( remain_size, DTS_MAX_EXSS_SIZE ) );
2323 /* Check the remainder length of the buffer.
2324 * If there is enough length, then parse the frame in it.
2325 * The length 10 is the required byte length to get frame size. */
2326 if( bs->eob || (bs->eof && remain_size < 10) )
2328 /* Reached the end of stream. */
2329 dts_imp->status = IMPORTER_EOF;
2330 au_completed = !!dts_imp->incomplete_au_length;
2331 if( !au_completed )
2333 /* No more access units in the stream. */
2334 if( lsmash_bs_get_remaining_buffer_size( bs ) )
2336 lsmash_log( importer, LSMASH_LOG_WARNING, "the stream is truncated at the end.\n" );
2337 return -1;
2339 return 0;
2341 if( !info->ddts_param_initialized )
2342 dts_update_specific_param( info );
2344 else
2346 /* Parse substream frame. */
2347 dts_substream_type prev_substream_type = info->substream_type;
2348 info->substream_type = dts_get_substream_type( info );
2349 int (*dts_parse_frame)( dts_info_t * ) = NULL;
2350 switch( info->substream_type )
2352 /* Decide substream frame parser and check if this frame and the previous frame belong to the same AU. */
2353 case DTS_SUBSTREAM_TYPE_CORE :
2354 if( prev_substream_type != DTS_SUBSTREAM_TYPE_NONE )
2355 au_completed = 1;
2356 dts_parse_frame = dts_parse_core_substream;
2357 break;
2358 case DTS_SUBSTREAM_TYPE_EXTENSION :
2360 uint8_t prev_exss_index = info->exss_index;
2361 if( dts_get_exss_index( info, &info->exss_index ) < 0 )
2363 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to get the index of an extension substream.\n" );
2364 return -1;
2366 if( prev_substream_type == DTS_SUBSTREAM_TYPE_EXTENSION
2367 && info->exss_index <= prev_exss_index )
2368 au_completed = 1;
2369 dts_parse_frame = dts_parse_extension_substream;
2370 break;
2372 default :
2373 lsmash_log( importer, LSMASH_LOG_ERROR, "unknown substream type is detected.\n" );
2374 return -1;
2376 if( !info->ddts_param_initialized && au_completed )
2377 dts_update_specific_param( info );
2378 info->frame_size = 0;
2379 if( dts_parse_frame( info ) < 0 )
2381 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to parse a frame.\n" );
2382 return -1;
2385 if( au_completed )
2387 memcpy( dts_imp->au, dts_imp->incomplete_au, dts_imp->incomplete_au_length );
2388 dts_imp->au_length = dts_imp->incomplete_au_length;
2389 dts_imp->incomplete_au_length = 0;
2390 info->exss_count = (info->substream_type == DTS_SUBSTREAM_TYPE_EXTENSION);
2391 if( dts_imp->status == IMPORTER_EOF )
2392 break;
2394 /* Increase buffer size to store AU if short. */
2395 if( dts_imp->incomplete_au_length + info->frame_size > dts_imp->au_buffers->buffer_size )
2397 lsmash_multiple_buffers_t *temp = lsmash_resize_multiple_buffers( dts_imp->au_buffers,
2398 dts_imp->au_buffers->buffer_size + DTS_MAX_EXSS_SIZE );
2399 if( !temp )
2400 return -1;
2401 dts_imp->au_buffers = temp;
2402 dts_imp->au = lsmash_withdraw_buffer( dts_imp->au_buffers, 1 );
2403 dts_imp->incomplete_au = lsmash_withdraw_buffer( dts_imp->au_buffers, 2 );
2405 /* Append frame data. */
2406 memcpy( dts_imp->incomplete_au + dts_imp->incomplete_au_length, dts_imp->buffer, info->frame_size );
2407 dts_imp->incomplete_au_length += info->frame_size;
2409 return info->bits->bs->error ? -1 : 0;
2412 static int dts_importer_get_accessunit( importer_t *importer, uint32_t track_number, lsmash_sample_t *buffered_sample )
2414 debug_if( !importer || !importer->info || !buffered_sample->data || !buffered_sample->length )
2415 return -1;
2416 if( !importer->info || track_number != 1 )
2417 return -1;
2418 lsmash_audio_summary_t *summary = (lsmash_audio_summary_t *)lsmash_get_entry_data( importer->summaries, track_number );
2419 if( !summary )
2420 return -1;
2421 dts_importer_t *dts_imp = (dts_importer_t *)importer->info;
2422 dts_info_t *info = &dts_imp->info;
2423 importer_status current_status = dts_imp->status;
2424 if( current_status == IMPORTER_ERROR || buffered_sample->length < dts_imp->au_length )
2425 return -1;
2426 if( current_status == IMPORTER_EOF && dts_imp->au_length == 0 )
2428 buffered_sample->length = 0;
2429 return 0;
2431 if( current_status == IMPORTER_CHANGE )
2432 summary->max_au_length = 0;
2433 memcpy( buffered_sample->data, dts_imp->au, dts_imp->au_length );
2434 buffered_sample->length = dts_imp->au_length;
2435 buffered_sample->dts = dts_imp->au_number++ * summary->samples_in_frame;
2436 buffered_sample->cts = buffered_sample->dts;
2437 buffered_sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
2438 buffered_sample->prop.pre_roll.distance = !!(info->flags & DTS_EXT_SUBSTREAM_LBR_FLAG); /* MDCT */
2439 if( dts_imp->status == IMPORTER_EOF )
2441 dts_imp->au_length = 0;
2442 return 0;
2444 if( dts_importer_get_next_accessunit_internal( importer ) )
2445 dts_imp->status = IMPORTER_ERROR;
2446 return current_status;
2449 static lsmash_audio_summary_t *dts_create_summary( dts_info_t *info )
2451 lsmash_audio_summary_t *summary = (lsmash_audio_summary_t *)lsmash_create_summary( LSMASH_SUMMARY_TYPE_AUDIO );
2452 if( !summary )
2453 return NULL;
2454 lsmash_dts_specific_parameters_t *param = &info->ddts_param;
2455 lsmash_codec_specific_t *specific = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_DTS,
2456 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED );
2457 specific->data.unstructured = lsmash_create_dts_specific_info( param, &specific->size );
2458 if( !specific->data.unstructured
2459 || lsmash_add_entry( &summary->opaque->list, specific ) )
2461 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
2462 lsmash_destroy_codec_specific_data( specific );
2463 return NULL;
2465 /* The CODEC identifiers probably should not be the combination of 'mp4a' and
2466 * the objectTypeIndications for DTS audio since there is no public specification
2467 * which defines the encapsulation of the stream as the MPEG-4 Audio context yet.
2468 * In the world, there are muxers which is using such doubtful implementation.
2469 * The objectTypeIndications are registered at MP4RA, but this does not always
2470 * mean we can mux by using those objectTypeIndications.
2471 * If available, there shall be the specification which defines the existence of
2472 * DecoderSpecificInfo and its semantics, and what access unit consists of. */
2473 summary->sample_type = lsmash_dts_get_codingname( param );
2474 summary->aot = MP4A_AUDIO_OBJECT_TYPE_NULL; /* make no sense */
2475 summary->sbr_mode = MP4A_AAC_SBR_NOT_SPECIFIED; /* make no sense */
2476 switch( param->DTSSamplingFrequency )
2478 case 12000 : /* Invalid? (No reference in the spec) */
2479 case 24000 :
2480 case 48000 :
2481 case 96000 :
2482 case 192000 :
2483 case 384000 : /* Invalid? (No reference in the spec) */
2484 summary->frequency = 48000;
2485 break;
2486 case 22050 :
2487 case 44100 :
2488 case 88200 :
2489 case 176400 :
2490 case 352800 : /* Invalid? (No reference in the spec) */
2491 summary->frequency = 44100;
2492 break;
2493 case 8000 : /* Invalid? (No reference in the spec) */
2494 case 16000 :
2495 case 32000 :
2496 case 64000 :
2497 case 128000 :
2498 summary->frequency = 32000;
2499 break;
2500 default :
2501 summary->frequency = 0;
2502 break;
2504 summary->samples_in_frame = (summary->frequency * info->frame_duration) / param->DTSSamplingFrequency;
2505 summary->max_au_length = DTS_MAX_CORE_SIZE + DTS_MAX_NUM_EXSS * DTS_MAX_EXSS_SIZE;
2506 summary->sample_size = param->pcmSampleDepth;
2507 summary->channels = dts_get_max_channel_count( info );
2508 return summary;
2511 static int dts_importer_probe( importer_t *importer )
2513 dts_importer_t *dts_imp = create_dts_importer();
2514 if( !dts_imp )
2515 return -1;
2516 lsmash_bits_t *bits = dts_imp->info.bits;
2517 lsmash_bs_t *bs = bits->bs;
2518 bs->stream = importer->stream;
2519 bs->read = lsmash_fread_wrapper;
2520 bs->seek = lsmash_fseek_wrapper;
2521 bs->unseekable = importer->is_stdin;
2522 bs->buffer.max_size = DTS_MAX_EXSS_SIZE;
2523 importer->info = dts_imp;
2524 if( dts_importer_get_next_accessunit_internal( importer ) < 0 )
2525 goto fail;
2526 lsmash_audio_summary_t *summary = dts_create_summary( &dts_imp->info );
2527 if( !summary )
2528 goto fail;
2529 if( dts_imp->status != IMPORTER_EOF )
2530 dts_imp->status = IMPORTER_OK;
2531 dts_imp->au_number = 0;
2532 if( lsmash_add_entry( importer->summaries, summary ) < 0 )
2534 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
2535 goto fail;
2537 return 0;
2538 fail:
2539 remove_dts_importer( dts_imp );
2540 importer->info = NULL;
2541 return -1;
2544 static uint32_t dts_importer_get_last_delta( importer_t* importer, uint32_t track_number )
2546 debug_if( !importer || !importer->info )
2547 return 0;
2548 dts_importer_t *dts_imp = (dts_importer_t *)importer->info;
2549 if( !dts_imp || track_number != 1 || dts_imp->status != IMPORTER_EOF || dts_imp->au_length )
2550 return 0;
2551 lsmash_audio_summary_t *summary = (lsmash_audio_summary_t *)lsmash_get_entry_data( importer->summaries, track_number );
2552 if( !summary )
2553 return 0;
2554 return (summary->frequency * dts_imp->info.frame_duration) / dts_imp->info.ddts_param.DTSSamplingFrequency;
2557 static const importer_functions dts_importer =
2559 { "DTS Coherent Acoustics", offsetof( importer_t, log_level ) },
2561 dts_importer_probe,
2562 dts_importer_get_accessunit,
2563 dts_importer_get_last_delta,
2564 dts_importer_cleanup
2567 /***************************************************************************
2568 H.264 importer
2569 ITU-T Recommendation H.264 (04/13)
2570 ISO/IEC 14496-15:2010
2571 ***************************************************************************/
2572 #include "codecs/h264.h"
2573 #include "codecs/nalu.h"
2575 typedef struct
2577 importer_status status;
2578 h264_info_t info;
2579 lsmash_entry_list_t avcC_list[1]; /* stored as lsmash_codec_specific_t */
2580 lsmash_media_ts_list_t ts_list;
2581 lsmash_bs_t *bs;
2582 uint32_t max_au_length;
2583 uint32_t num_undecodable;
2584 uint32_t avcC_number;
2585 uint32_t last_delta;
2586 uint64_t last_intra_cts;
2587 uint64_t sc_head_pos;
2588 uint8_t composition_reordering_present;
2589 uint8_t field_pic_present;
2590 } h264_importer_t;
2592 typedef struct
2594 int64_t poc;
2595 uint32_t delta;
2596 uint16_t poc_delta;
2597 uint16_t reset;
2598 } nal_pic_timing_t;
2600 static void remove_h264_importer( h264_importer_t *h264_imp )
2602 if( !h264_imp )
2603 return;
2604 lsmash_remove_entries( h264_imp->avcC_list, lsmash_destroy_codec_specific_data );
2605 h264_cleanup_parser( &h264_imp->info );
2606 lsmash_bs_cleanup( h264_imp->bs );
2607 lsmash_free( h264_imp->ts_list.timestamp );
2608 lsmash_free( h264_imp );
2611 static void h264_importer_cleanup( importer_t *importer )
2613 debug_if( importer && importer->info )
2614 remove_h264_importer( importer->info );
2617 static h264_importer_t *create_h264_importer( importer_t *importer )
2619 h264_importer_t *h264_imp = lsmash_malloc_zero( sizeof(h264_importer_t) );
2620 if( !h264_imp )
2621 return NULL;
2622 if( h264_setup_parser( &h264_imp->info, 0 ) < 0 )
2624 remove_h264_importer( h264_imp );
2625 return NULL;
2627 lsmash_bs_t *bs = lsmash_bs_create();
2628 if( !bs )
2630 remove_h264_importer( h264_imp );
2631 return NULL;
2633 bs->stream = importer->stream;
2634 bs->read = lsmash_fread_wrapper;
2635 bs->seek = lsmash_fseek_wrapper;
2636 bs->unseekable = importer->is_stdin;
2637 bs->buffer.max_size = BS_MAX_DEFAULT_READ_SIZE;
2638 lsmash_init_entry_list( h264_imp->avcC_list );
2639 h264_imp->bs = bs;
2640 return h264_imp;
2643 static inline int h264_complete_au( h264_picture_info_t *picture, int probe )
2645 if( !picture->incomplete_au_has_primary || picture->incomplete_au_length == 0 )
2646 return 0;
2647 if( !probe )
2648 memcpy( picture->au, picture->incomplete_au, picture->incomplete_au_length );
2649 picture->au_length = picture->incomplete_au_length;
2650 picture->incomplete_au_length = 0;
2651 picture->incomplete_au_has_primary = 0;
2652 return 1;
2655 static void h264_append_nalu_to_au( h264_picture_info_t *picture, uint8_t *src_nalu, uint32_t nalu_length, int probe )
2657 if( !probe )
2659 uint8_t *dst_nalu = picture->incomplete_au + picture->incomplete_au_length + H264_DEFAULT_NALU_LENGTH_SIZE;
2660 for( int i = H264_DEFAULT_NALU_LENGTH_SIZE; i; i-- )
2661 *(dst_nalu - i) = (nalu_length >> ((i - 1) * 8)) & 0xff;
2662 memcpy( dst_nalu, src_nalu, nalu_length );
2664 /* Note: picture->incomplete_au_length shall be 0 immediately after AU has completed.
2665 * Therefore, possible_au_length in h264_get_access_unit_internal() can't be used here
2666 * to avoid increasing AU length monotonously through the entire stream. */
2667 picture->incomplete_au_length += H264_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
2670 static inline void h264_get_au_internal_end( h264_importer_t *h264_imp, h264_picture_info_t *picture )
2672 if( lsmash_bs_is_end( h264_imp->bs, 0 ) && (picture->incomplete_au_length == 0) )
2673 h264_imp->status = IMPORTER_EOF;
2674 else if( h264_imp->status != IMPORTER_CHANGE )
2675 h264_imp->status = IMPORTER_OK;
2678 static int h264_get_au_internal_succeeded( h264_importer_t *h264_imp, h264_picture_info_t *picture )
2680 h264_get_au_internal_end( h264_imp, picture );
2681 picture->au_number += 1;
2682 return 0;
2685 static int h264_get_au_internal_failed( h264_importer_t *h264_imp, h264_picture_info_t *picture, int complete_au )
2687 h264_get_au_internal_end( h264_imp, picture );
2688 if( complete_au )
2689 picture->au_number += 1;
2690 return -1;
2693 static lsmash_video_summary_t *h264_create_summary
2695 lsmash_h264_specific_parameters_t *param,
2696 h264_sps_t *sps,
2697 uint32_t max_au_length
2700 lsmash_video_summary_t *summary = (lsmash_video_summary_t *)lsmash_create_summary( LSMASH_SUMMARY_TYPE_VIDEO );
2701 if( !summary )
2702 return NULL;
2703 /* Update summary here.
2704 * max_au_length is set at the last of mp4sys_h264_probe function. */
2705 lsmash_codec_specific_t *cs = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264,
2706 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED );
2707 cs->data.unstructured = lsmash_create_h264_specific_info( param, &cs->size );
2708 if( !cs->data.unstructured
2709 || lsmash_add_entry( &summary->opaque->list, cs ) < 0 )
2711 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
2712 lsmash_destroy_codec_specific_data( cs );
2713 return NULL;
2715 summary->sample_type = ISOM_CODEC_TYPE_AVC1_VIDEO;
2716 summary->max_au_length = max_au_length;
2717 summary->timescale = sps->vui.time_scale;
2718 summary->timebase = sps->vui.num_units_in_tick;
2719 summary->vfr = !sps->vui.fixed_frame_rate_flag;
2720 summary->sample_per_field = 0;
2721 summary->width = sps->cropped_width;
2722 summary->height = sps->cropped_height;
2723 summary->par_h = sps->vui.sar_width;
2724 summary->par_v = sps->vui.sar_height;
2725 summary->color.primaries_index = sps->vui.colour_primaries;
2726 summary->color.transfer_index = sps->vui.transfer_characteristics;
2727 summary->color.matrix_index = sps->vui.matrix_coefficients;
2728 summary->color.full_range = sps->vui.video_full_range_flag;
2729 return summary;
2732 static int h264_store_codec_specific
2734 h264_importer_t *info,
2735 lsmash_h264_specific_parameters_t *avcC_param
2738 lsmash_codec_specific_t *src_cs = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264,
2739 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED );
2740 if( !src_cs )
2741 return -1;
2742 lsmash_h264_specific_parameters_t *src_param = (lsmash_h264_specific_parameters_t *)src_cs->data.structured;
2743 *src_param = *avcC_param;
2744 lsmash_codec_specific_t *dst_cs = lsmash_convert_codec_specific_format( src_cs, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED );
2745 src_param->parameter_sets = NULL; /* Avoid freeing parameter sets within avcC_param. */
2746 lsmash_destroy_codec_specific_data( src_cs );
2747 if( !dst_cs || lsmash_add_entry( info->avcC_list, dst_cs ) < 0 )
2749 lsmash_destroy_codec_specific_data( dst_cs );
2750 return -1;
2752 return 0;
2755 static inline void h264_new_picture( h264_picture_info_t *picture )
2757 picture->au_length = 0;
2758 picture->type = H264_PICTURE_TYPE_NONE;
2759 picture->random_accessible = 0;
2760 picture->recovery_frame_cnt = 0;
2761 picture->has_mmco5 = 0;
2762 picture->has_redundancy = 0;
2763 picture->broken_link_flag = 0;
2766 /* If probe equals 0, don't get the actual data (EBPS) of an access unit and only parse NALU.
2767 * Currently, you can get AU of AVC video elemental stream only, not AVC parameter set elemental stream defined in 14496-15. */
2768 static int h264_get_access_unit_internal
2770 importer_t *importer,
2771 int probe
2774 h264_importer_t *h264_imp = (h264_importer_t *)importer->info;
2775 h264_info_t *info = &h264_imp->info;
2776 h264_slice_info_t *slice = &info->slice;
2777 h264_picture_info_t *picture = &info->picture;
2778 h264_stream_buffer_t *sb = &info->buffer;
2779 lsmash_bs_t *bs = h264_imp->bs;
2780 int complete_au = 0;
2781 h264_new_picture( picture );
2782 while( 1 )
2784 h264_nalu_header_t nuh;
2785 uint64_t start_code_length;
2786 uint64_t trailing_zero_bytes;
2787 uint64_t nalu_length = h264_find_next_start_code( bs, &nuh, &start_code_length, &trailing_zero_bytes );
2788 if( start_code_length <= H264_SHORT_START_CODE_LENGTH && lsmash_bs_is_end( bs, nalu_length ) )
2790 /* For the last NALU.
2791 * This NALU already has been appended into the latest access unit and parsed. */
2792 h264_update_picture_info( info, picture, slice, &info->sei );
2793 complete_au = h264_complete_au( picture, probe );
2794 if( complete_au )
2795 return h264_get_au_internal_succeeded( h264_imp, picture );
2796 else
2797 return h264_get_au_internal_failed( h264_imp, picture, complete_au );
2799 uint8_t nalu_type = nuh.nal_unit_type;
2800 uint64_t next_sc_head_pos = h264_imp->sc_head_pos
2801 + start_code_length
2802 + nalu_length
2803 + trailing_zero_bytes;
2804 #if 0
2805 if( probe )
2807 fprintf( stderr, "NALU type: %"PRIu8" \n", nalu_type );
2808 fprintf( stderr, " NALU header position: %"PRIx64" \n", h264_imp->sc_head_pos + start_code_length );
2809 fprintf( stderr, " EBSP position: %"PRIx64" \n", h264_imp->sc_head_pos + start_code_length + nuh.length );
2810 fprintf( stderr, " EBSP length: %"PRIx64" (%"PRIu64") \n", nalu_length - nuh.length, nalu_length - nuh.length );
2811 fprintf( stderr, " trailing_zero_bytes: %"PRIx64" \n", trailing_zero_bytes );
2812 fprintf( stderr, " Next start code position: %"PRIx64"\n", next_sc_head_pos );
2814 #endif
2815 if( nalu_type == H264_NALU_TYPE_FD )
2817 /* We don't support streams with both filler and HRD yet.
2818 * Otherwise, just skip filler because elemental streams defined in 14496-15 are forbidden to use filler. */
2819 if( info->sps.vui.hrd.present )
2820 return h264_get_au_internal_failed( h264_imp, picture, complete_au );
2822 else if( (nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && nalu_type <= H264_NALU_TYPE_SPS_EXT)
2823 || nalu_type == H264_NALU_TYPE_SLICE_AUX )
2825 /* Increase the buffer if needed. */
2826 uint64_t possible_au_length = picture->incomplete_au_length + H264_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
2827 if( sb->bank->buffer_size < possible_au_length
2828 && h264_supplement_buffer( sb, picture, 2 * possible_au_length ) < 0 )
2830 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to increase the buffer size.\n" );
2831 return h264_get_au_internal_failed( h264_imp, picture, complete_au );
2833 /* Get the EBSP of the current NALU here.
2834 * AVC elemental stream defined in 14496-15 can recognizes from 0 to 13, and 19 of nal_unit_type.
2835 * We don't support SVC and MVC elemental stream defined in 14496-15 yet. */
2836 uint8_t *nalu = lsmash_bs_get_buffer_data( bs ) + start_code_length;
2837 if( nalu_type >= H264_NALU_TYPE_SLICE_N_IDR && nalu_type <= H264_NALU_TYPE_SLICE_IDR )
2839 /* VCL NALU (slice) */
2840 h264_slice_info_t prev_slice = *slice;
2841 if( h264_parse_slice( info, &nuh, sb->rbsp, nalu + nuh.length, nalu_length - nuh.length ) )
2842 return h264_get_au_internal_failed( h264_imp, picture, complete_au );
2843 if( probe && info->avcC_pending )
2845 /* Copy and append a Codec Specific info. */
2846 if( h264_store_codec_specific( h264_imp, &info->avcC_param ) < 0 )
2847 return -1;
2849 if( h264_move_pending_avcC_param( info ) < 0 )
2850 return -1;
2851 if( prev_slice.present )
2853 /* Check whether the AU that contains the previous VCL NALU completed or not. */
2854 if( h264_find_au_delimit_by_slice_info( slice, &prev_slice ) )
2856 /* The current NALU is the first VCL NALU of the primary coded picture of an new AU.
2857 * Therefore, the previous slice belongs to the AU you want at this time. */
2858 h264_update_picture_info( info, picture, &prev_slice, &info->sei );
2859 complete_au = h264_complete_au( picture, probe );
2861 else
2862 h264_update_picture_info_for_slice( info, picture, &prev_slice );
2864 h264_append_nalu_to_au( picture, nalu, nalu_length, probe );
2865 slice->present = 1;
2867 else
2869 if( h264_find_au_delimit_by_nalu_type( nalu_type, info->prev_nalu_type ) )
2871 /* The last slice belongs to the AU you want at this time. */
2872 h264_update_picture_info( info, picture, slice, &info->sei );
2873 complete_au = h264_complete_au( picture, probe );
2875 switch( nalu_type )
2877 case H264_NALU_TYPE_SEI :
2879 if( h264_parse_sei( info->bits, &info->sps, &info->sei, sb->rbsp, nalu + nuh.length, nalu_length - nuh.length ) < 0 )
2880 return h264_get_au_internal_failed( h264_imp, picture, complete_au );
2881 h264_append_nalu_to_au( picture, nalu, nalu_length, probe );
2882 break;
2884 case H264_NALU_TYPE_SPS :
2885 if( h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_SPS, nalu, nalu_length ) < 0 )
2886 return h264_get_au_internal_failed( h264_imp, picture, complete_au );
2887 break;
2888 case H264_NALU_TYPE_PPS :
2889 if( h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_PPS, nalu, nalu_length ) < 0 )
2890 return h264_get_au_internal_failed( h264_imp, picture, complete_au );
2891 break;
2892 case H264_NALU_TYPE_AUD : /* We drop access unit delimiters. */
2893 break;
2894 case H264_NALU_TYPE_SPS_EXT :
2895 if( h264_try_to_append_parameter_set( info, H264_PARAMETER_SET_TYPE_SPSEXT, nalu, nalu_length ) < 0 )
2896 return h264_get_au_internal_failed( h264_imp, picture, complete_au );
2897 break;
2898 default :
2899 h264_append_nalu_to_au( picture, nalu, nalu_length, probe );
2900 break;
2902 if( info->avcC_pending )
2903 h264_imp->status = IMPORTER_CHANGE;
2906 /* Move to the first byte of the next start code. */
2907 info->prev_nalu_type = nalu_type;
2908 if( lsmash_bs_read_seek( bs, next_sc_head_pos, SEEK_SET ) != next_sc_head_pos )
2910 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to seek the next start code.\n" );
2911 return h264_get_au_internal_failed( h264_imp, picture, complete_au );
2913 /* Check if no more data to read from the stream. */
2914 if( !lsmash_bs_is_end( bs, H264_SHORT_START_CODE_LENGTH ) )
2915 h264_imp->sc_head_pos = next_sc_head_pos;
2916 /* If there is no more data in the stream, and flushed chunk of NALUs, flush it as complete AU here. */
2917 else if( picture->incomplete_au_length && picture->au_length == 0 )
2919 h264_update_picture_info( info, picture, slice, &info->sei );
2920 h264_complete_au( picture, probe );
2921 return h264_get_au_internal_succeeded( h264_imp, picture );
2923 if( complete_au )
2924 return h264_get_au_internal_succeeded( h264_imp, picture );
2928 static int h264_importer_get_accessunit
2930 importer_t *importer,
2931 uint32_t track_number,
2932 lsmash_sample_t *buffered_sample
2935 debug_if( !importer || !importer->info || !buffered_sample->data || !buffered_sample->length )
2936 return -1;
2937 if( !importer->info || track_number != 1 )
2938 return -1;
2939 h264_importer_t *h264_imp = (h264_importer_t *)importer->info;
2940 h264_info_t *info = &h264_imp->info;
2941 importer_status current_status = h264_imp->status;
2942 if( current_status == IMPORTER_ERROR || buffered_sample->length < h264_imp->max_au_length )
2943 return -1;
2944 if( current_status == IMPORTER_EOF )
2946 buffered_sample->length = 0;
2947 return 0;
2949 if( h264_get_access_unit_internal( importer, 0 ) < 0 )
2951 h264_imp->status = IMPORTER_ERROR;
2952 return -1;
2954 if( h264_imp->status == IMPORTER_CHANGE && !info->avcC_pending )
2955 current_status = IMPORTER_CHANGE;
2956 if( current_status == IMPORTER_CHANGE )
2958 /* Update the active summary. */
2959 lsmash_codec_specific_t *cs = (lsmash_codec_specific_t *)lsmash_get_entry_data( h264_imp->avcC_list, ++ h264_imp->avcC_number );
2960 if( !cs )
2961 return -1;
2962 lsmash_h264_specific_parameters_t *avcC_param = (lsmash_h264_specific_parameters_t *)cs->data.structured;
2963 lsmash_video_summary_t *summary = h264_create_summary( avcC_param, &info->sps, h264_imp->max_au_length );
2964 if( !summary )
2965 return -1;
2966 lsmash_remove_entry( importer->summaries, track_number, lsmash_cleanup_summary );
2967 if( lsmash_add_entry( importer->summaries, summary ) < 0 )
2969 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
2970 return -1;
2972 h264_imp->status = IMPORTER_OK;
2974 h264_picture_info_t *picture = &info->picture;
2975 buffered_sample->dts = h264_imp->ts_list.timestamp[picture->au_number - 1].dts;
2976 buffered_sample->cts = h264_imp->ts_list.timestamp[picture->au_number - 1].cts;
2977 if( picture->au_number < h264_imp->num_undecodable )
2978 buffered_sample->prop.leading = ISOM_SAMPLE_IS_UNDECODABLE_LEADING;
2979 else
2980 buffered_sample->prop.leading = picture->independent || buffered_sample->cts >= h264_imp->last_intra_cts
2981 ? ISOM_SAMPLE_IS_NOT_LEADING : ISOM_SAMPLE_IS_UNDECODABLE_LEADING;
2982 if( picture->independent )
2983 h264_imp->last_intra_cts = buffered_sample->cts;
2984 if( h264_imp->composition_reordering_present && !picture->disposable && !picture->idr )
2985 buffered_sample->prop.allow_earlier = QT_SAMPLE_EARLIER_PTS_ALLOWED;
2986 buffered_sample->prop.independent = picture->independent ? ISOM_SAMPLE_IS_INDEPENDENT : ISOM_SAMPLE_IS_NOT_INDEPENDENT;
2987 buffered_sample->prop.disposable = picture->disposable ? ISOM_SAMPLE_IS_DISPOSABLE : ISOM_SAMPLE_IS_NOT_DISPOSABLE;
2988 buffered_sample->prop.redundant = picture->has_redundancy ? ISOM_SAMPLE_HAS_REDUNDANCY : ISOM_SAMPLE_HAS_NO_REDUNDANCY;
2989 buffered_sample->prop.post_roll.identifier = picture->frame_num;
2990 if( picture->random_accessible )
2992 if( picture->idr )
2993 buffered_sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
2994 else if( picture->recovery_frame_cnt )
2996 buffered_sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_POST_ROLL_START;
2997 buffered_sample->prop.post_roll.complete = (picture->frame_num + picture->recovery_frame_cnt) % info->sps.MaxFrameNum;
2999 else
3001 buffered_sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP;
3002 if( !picture->broken_link_flag )
3003 buffered_sample->prop.ra_flags |= QT_SAMPLE_RANDOM_ACCESS_FLAG_PARTIAL_SYNC;
3006 buffered_sample->length = picture->au_length;
3007 memcpy( buffered_sample->data, picture->au, picture->au_length );
3008 return current_status;
3011 static void nalu_deduplicate_poc
3013 nal_pic_timing_t *npt,
3014 uint32_t *max_composition_delay,
3015 uint32_t num_access_units,
3016 uint32_t max_num_reorder_pics
3019 /* Deduplicate POCs. */
3020 int64_t poc_offset = 0;
3021 int64_t poc_min = 0;
3022 int64_t invalid_poc_min = 0;
3023 uint32_t last_poc_reset = UINT32_MAX;
3024 uint32_t invalid_poc_start = 0;
3025 int invalid_poc_present = 0;
3026 for( uint32_t i = 0; ; i++ )
3028 if( i < num_access_units && npt[i].poc != 0 && !npt[i].reset )
3030 /* poc_offset is not added to each POC here.
3031 * It is done when we encounter the next coded video sequence. */
3032 if( npt[i].poc < 0 )
3034 /* Pictures with negative POC shall precede IDR-picture in composition order.
3035 * The minimum POC is added to poc_offset when we encounter the next coded video sequence. */
3036 if( last_poc_reset == UINT32_MAX || i > last_poc_reset + max_num_reorder_pics )
3038 if( !invalid_poc_present )
3040 invalid_poc_present = 1;
3041 invalid_poc_start = i;
3043 if( invalid_poc_min > npt[i].poc )
3044 invalid_poc_min = npt[i].poc;
3046 else if( poc_min > npt[i].poc )
3048 poc_min = npt[i].poc;
3049 *max_composition_delay = LSMASH_MAX( *max_composition_delay, i - last_poc_reset );
3052 continue;
3054 /* Encountered a new coded video sequence or no more POCs.
3055 * Add poc_offset to each POC of the previous coded video sequence. */
3056 poc_offset -= poc_min;
3057 int64_t poc_max = 0;
3058 for( uint32_t j = last_poc_reset; j < i + !!npt[i].reset; j++ )
3059 if( npt[j].poc >= 0 || (j <= last_poc_reset + max_num_reorder_pics) )
3061 npt[j].poc += poc_offset;
3062 if( poc_max < npt[j].poc )
3063 poc_max = npt[j].poc;
3065 poc_offset = poc_max + 1;
3066 if( invalid_poc_present )
3068 /* Pictures with invalid negative POC is probably supposed to be composited
3069 * both before the next coded video sequence and after the current one. */
3070 poc_offset -= invalid_poc_min;
3071 for( uint32_t j = invalid_poc_start; j < i + !!npt[i].reset; j++ )
3072 if( npt[j].poc < 0 )
3074 npt[j].poc += poc_offset;
3075 if( poc_max < npt[j].poc )
3076 poc_max = npt[j].poc;
3078 invalid_poc_present = 0;
3079 invalid_poc_start = 0;
3080 invalid_poc_min = 0;
3081 poc_offset = poc_max + 1;
3083 if( i < num_access_units )
3085 if( npt[i].reset )
3086 npt[i].poc = 0;
3087 poc_min = 0;
3088 last_poc_reset = i;
3090 else
3091 break; /* no more POCs */
3095 static void nalu_generate_timestamps_from_poc
3097 importer_t *importer,
3098 lsmash_media_ts_t *timestamp,
3099 nal_pic_timing_t *npt,
3100 uint8_t *composition_reordering_present,
3101 uint32_t *last_delta,
3102 uint32_t max_composition_delay,
3103 uint32_t num_access_units
3106 /* Check if composition delay derived from reordering is present. */
3107 if( max_composition_delay == 0 )
3109 for( uint32_t i = 1; i < num_access_units; i++ )
3110 if( npt[i].poc < npt[i - 1].poc )
3112 *composition_reordering_present = 1;
3113 break;
3116 else
3117 *composition_reordering_present = 1;
3118 /* Generate timestamps. */
3119 if( *composition_reordering_present )
3121 /* Generate timestamps.
3122 * Here, DTSs and CTSs are temporary values for sort. */
3123 for( uint32_t i = 0; i < num_access_units; i++ )
3125 timestamp[i].cts = (uint64_t)npt[i].poc;
3126 timestamp[i].dts = (uint64_t)i;
3128 qsort( timestamp, num_access_units, sizeof(lsmash_media_ts_t), (int(*)( const void *, const void * ))lsmash_compare_cts );
3129 /* Check POC gap in output order. */
3130 lsmash_class_t *logger = &(lsmash_class_t){ .name = importer->class->name };
3131 for( uint32_t i = 1; i < num_access_units; i++ )
3132 if( timestamp[i].cts > timestamp[i - 1].cts + npt[i - 1].poc_delta )
3133 lsmash_log( &logger, LSMASH_LOG_WARNING,
3134 "POC gap is detected at picture %"PRIu64". Maybe some pictures are lost.\n", timestamp[i].dts );
3135 /* Get the maximum composition delay derived from reordering. */
3136 for( uint32_t i = 0; i < num_access_units; i++ )
3137 if( i < timestamp[i].dts )
3139 uint32_t composition_delay = timestamp[i].dts - i;
3140 max_composition_delay = LSMASH_MAX( max_composition_delay, composition_delay );
3142 uint64_t *ts_buffer = (uint64_t *)lsmash_malloc( (num_access_units + max_composition_delay) * sizeof(uint64_t) );
3143 if( !ts_buffer )
3145 /* It seems that there is no enough memory to generate more appropriate timestamps.
3146 * Anyway, generate CTSs and DTSs. */
3147 for( uint32_t i = 0; i < num_access_units; i++ )
3148 timestamp[i].cts = i + max_composition_delay;
3149 qsort( timestamp, num_access_units, sizeof(lsmash_media_ts_t), (int(*)( const void *, const void * ))lsmash_compare_dts );
3150 *last_delta = 1;
3151 return;
3153 uint64_t *reorder_cts = ts_buffer;
3154 uint64_t *prev_reorder_cts = ts_buffer + num_access_units;
3155 *last_delta = npt[num_access_units - 1].delta;
3156 /* Generate CTSs. */
3157 timestamp[0].cts = 0;
3158 for( uint32_t i = 1; i < num_access_units; i++ )
3159 timestamp[i].cts = timestamp[i - 1].cts + npt[i - 1].delta;
3160 int64_t composition_delay_time = timestamp[max_composition_delay].cts;
3161 for( uint32_t i = 0; i < num_access_units; i++ )
3163 timestamp[i].cts += composition_delay_time;
3164 reorder_cts[i] = timestamp[i].cts;
3166 /* Generate DTSs. */
3167 qsort( timestamp, num_access_units, sizeof(lsmash_media_ts_t), (int(*)( const void *, const void * ))lsmash_compare_dts );
3168 for( uint32_t i = 0; i < num_access_units; i++ )
3170 timestamp[i].dts = i <= max_composition_delay
3171 ? reorder_cts[i] - composition_delay_time
3172 : prev_reorder_cts[(i - max_composition_delay) % max_composition_delay];
3173 prev_reorder_cts[i % max_composition_delay] = reorder_cts[i];
3175 lsmash_free( ts_buffer );
3176 #if 0
3177 fprintf( stderr, "max_composition_delay=%"PRIu32", composition_delay_time=%"PRIu64"\n",
3178 max_composition_delay, composition_delay_time );
3179 #endif
3181 else
3183 timestamp[0].dts = 0;
3184 timestamp[0].cts = 0;
3185 for( uint32_t i = 1; i < num_access_units; i++ )
3187 timestamp[i].dts = timestamp[i - 1].dts + npt[i - 1].delta;
3188 timestamp[i].cts = timestamp[i - 1].cts + npt[i - 1].delta;
3190 *last_delta = npt[num_access_units - 1].delta;
3194 static void nalu_reduce_timescale
3196 lsmash_media_ts_t *timestamp,
3197 nal_pic_timing_t *npt,
3198 uint32_t *last_delta,
3199 uint32_t *timescale,
3200 uint32_t num_access_units
3203 uint64_t gcd_delta = *timescale;
3204 for( uint32_t i = 0; i < num_access_units && gcd_delta > 1; i++ )
3205 gcd_delta = lsmash_get_gcd( gcd_delta, npt[i].delta );
3206 if( gcd_delta > 1 )
3208 for( uint32_t i = 0; i < num_access_units; i++ )
3210 timestamp[i].dts /= gcd_delta;
3211 timestamp[i].cts /= gcd_delta;
3213 *last_delta /= gcd_delta;
3214 *timescale /= gcd_delta;
3216 #if 0
3217 for( uint32_t i = 0; i < num_access_units; i++ )
3218 fprintf( stderr, "Timestamp[%"PRIu32"]: POC=%"PRId64", DTS=%"PRIu64", CTS=%"PRIu64"\n",
3219 i, npt[i].poc, timestamp[i].dts, timestamp[i].cts );
3220 #endif
3223 static lsmash_video_summary_t *h264_setup_first_summary
3225 importer_t *importer
3228 h264_importer_t *h264_imp = (h264_importer_t *)importer->info;
3229 lsmash_codec_specific_t *cs = (lsmash_codec_specific_t *)lsmash_get_entry_data( h264_imp->avcC_list, ++ h264_imp->avcC_number );
3230 if( !cs || !cs->data.structured )
3232 lsmash_destroy_codec_specific_data( cs );
3233 return NULL;
3235 lsmash_video_summary_t *summary = h264_create_summary( (lsmash_h264_specific_parameters_t *)cs->data.structured,
3236 &h264_imp->info.sps, h264_imp->max_au_length );
3237 if( !summary )
3239 lsmash_destroy_codec_specific_data( cs );
3240 return NULL;
3242 if( lsmash_add_entry( importer->summaries, summary ) < 0 )
3244 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
3245 return NULL;
3247 summary->sample_per_field = h264_imp->field_pic_present;
3248 return summary;
3251 static int h264_analyze_whole_stream
3253 importer_t *importer
3256 /* Parse all NALU in the stream for preparation of calculating timestamps. */
3257 uint32_t npt_alloc = (1 << 12) * sizeof(nal_pic_timing_t);
3258 nal_pic_timing_t *npt = lsmash_malloc( npt_alloc );
3259 if( !npt )
3260 goto fail;
3261 uint32_t picture_stats[H264_PICTURE_TYPE_NONE + 1] = { 0 };
3262 uint32_t num_access_units = 0;
3263 lsmash_class_t *logger = &(lsmash_class_t){ "H.264" };
3264 lsmash_log( &logger, LSMASH_LOG_INFO, "Analyzing stream as H.264\r" );
3265 h264_importer_t *h264_imp = (h264_importer_t *)importer->info;
3266 h264_info_t *info = &h264_imp->info;
3267 h264_imp->status = IMPORTER_OK;
3268 while( h264_imp->status != IMPORTER_EOF )
3270 #if 0
3271 lsmash_log( &logger, LSMASH_LOG_INFO, "Analyzing stream as H.264: %"PRIu32"\n", num_access_units + 1 );
3272 #endif
3273 h264_picture_info_t *picture = &info->picture;
3274 h264_picture_info_t prev_picture = *picture;
3275 if( h264_get_access_unit_internal( importer, 1 ) < 0
3276 || h264_calculate_poc( info, picture, &prev_picture ) < 0 )
3277 goto fail;
3278 if( npt_alloc <= num_access_units * sizeof(nal_pic_timing_t) )
3280 uint32_t alloc = 2 * num_access_units * sizeof(nal_pic_timing_t);
3281 nal_pic_timing_t *temp = (nal_pic_timing_t *)lsmash_realloc( npt, alloc );
3282 if( !temp )
3283 goto fail;
3284 npt = temp;
3285 npt_alloc = alloc;
3287 h264_imp->field_pic_present |= info->picture.field_pic_flag;
3288 npt[num_access_units].poc = picture->PicOrderCnt;
3289 npt[num_access_units].delta = picture->delta;
3290 npt[num_access_units].poc_delta = picture->field_pic_flag ? 1 : 2;
3291 npt[num_access_units].reset = picture->has_mmco5;
3292 ++num_access_units;
3293 h264_imp->max_au_length = LSMASH_MAX( info->picture.au_length, h264_imp->max_au_length );
3294 if( picture->idr )
3295 ++picture_stats[H264_PICTURE_TYPE_IDR];
3296 else if( picture->type >= H264_PICTURE_TYPE_NONE )
3297 ++picture_stats[H264_PICTURE_TYPE_NONE];
3298 else
3299 ++picture_stats[ picture->type ];
3301 lsmash_log_refresh_line( &logger );
3302 lsmash_log( &logger, LSMASH_LOG_INFO,
3303 "IDR: %"PRIu32", I: %"PRIu32", P: %"PRIu32", B: %"PRIu32", "
3304 "SI: %"PRIu32", SP: %"PRIu32", Unknown: %"PRIu32"\n",
3305 picture_stats[H264_PICTURE_TYPE_IDR ],
3306 picture_stats[H264_PICTURE_TYPE_I ],
3307 picture_stats[H264_PICTURE_TYPE_I_P ],
3308 picture_stats[H264_PICTURE_TYPE_I_P_B ],
3309 picture_stats[H264_PICTURE_TYPE_SI ]
3310 + picture_stats[H264_PICTURE_TYPE_I_SI ],
3311 picture_stats[H264_PICTURE_TYPE_SI_SP ]
3312 + picture_stats[H264_PICTURE_TYPE_I_SI_P_SP ]
3313 + picture_stats[H264_PICTURE_TYPE_I_SI_P_SP_B],
3314 picture_stats[H264_PICTURE_TYPE_NONE ] );
3315 /* Copy and append the last Codec Specific info. */
3316 if( h264_store_codec_specific( h264_imp, &info->avcC_param ) < 0 )
3317 goto fail;
3318 /* Set up the first summary. */
3319 lsmash_video_summary_t *summary = h264_setup_first_summary( importer );
3320 if( !summary )
3321 goto fail;
3322 /* Allocate timestamps. */
3323 lsmash_media_ts_t *timestamp = lsmash_malloc( num_access_units * sizeof(lsmash_media_ts_t) );
3324 if( !timestamp )
3325 goto fail;
3326 /* Count leading samples that are undecodable. */
3327 for( uint32_t i = 0; i < num_access_units; i++ )
3329 if( npt[i].poc == 0 )
3330 break;
3331 ++ h264_imp->num_undecodable;
3333 /* Deduplicate POCs. */
3334 uint32_t max_composition_delay = 0;
3335 nalu_deduplicate_poc( npt, &max_composition_delay, num_access_units, 32 );
3336 /* Generate timestamps. */
3337 nalu_generate_timestamps_from_poc( importer, timestamp, npt,
3338 &h264_imp->composition_reordering_present,
3339 &h264_imp->last_delta,
3340 max_composition_delay, num_access_units );
3341 nalu_reduce_timescale( timestamp, npt, &h264_imp->last_delta, &summary->timescale, num_access_units );
3342 lsmash_free( npt );
3343 h264_imp->ts_list.sample_count = num_access_units;
3344 h264_imp->ts_list.timestamp = timestamp;
3345 return 0;
3346 fail:
3347 lsmash_log_refresh_line( &logger );
3348 lsmash_free( npt );
3349 return -1;
3352 static int h264_importer_probe( importer_t *importer )
3354 /* Find the first start code. */
3355 h264_importer_t *h264_imp = create_h264_importer( importer );
3356 if( !h264_imp )
3357 return -1;
3358 lsmash_bs_t *bs = h264_imp->bs;
3359 uint64_t first_sc_head_pos = 0;
3360 while( 1 )
3362 /* The first NALU of an AU in decoding order shall have long start code (0x00000001). */
3363 if( 0x00000001 == lsmash_bs_show_be32( bs, first_sc_head_pos ) )
3364 break;
3365 /* Invalid if encountered any value of non-zero before the first start code. */
3366 if( lsmash_bs_show_byte( bs, first_sc_head_pos ) )
3367 goto fail;
3368 ++first_sc_head_pos;
3370 /* OK. It seems the stream has a long start code of H.264. */
3371 importer->info = h264_imp;
3372 h264_info_t *info = &h264_imp->info;
3373 lsmash_bs_read_seek( bs, first_sc_head_pos, SEEK_SET );
3374 h264_imp->sc_head_pos = first_sc_head_pos;
3375 if( h264_analyze_whole_stream( importer ) < 0 )
3376 goto fail;
3377 /* Go back to the start code of the first NALU. */
3378 h264_imp->status = IMPORTER_OK;
3379 lsmash_bs_read_seek( bs, first_sc_head_pos, SEEK_SET );
3380 info->prev_nalu_type = H264_NALU_TYPE_UNSPECIFIED0;
3381 h264_imp->sc_head_pos = first_sc_head_pos;
3382 uint8_t *temp_au = info->picture.au;
3383 uint8_t *temp_incomplete_au = info->picture.incomplete_au;
3384 memset( &info->picture, 0, sizeof(h264_picture_info_t) );
3385 info->picture.au = temp_au;
3386 info->picture.incomplete_au = temp_incomplete_au;
3387 memset( &info->slice, 0, sizeof(h264_slice_info_t) );
3388 memset( &info->sps, 0, sizeof(h264_sps_t) );
3389 memset( &info->pps, 0, sizeof(h264_pps_t) );
3390 lsmash_remove_entries( info->avcC_param.parameter_sets->sps_list, isom_remove_dcr_ps );
3391 lsmash_remove_entries( info->avcC_param.parameter_sets->pps_list, isom_remove_dcr_ps );
3392 lsmash_remove_entries( info->avcC_param.parameter_sets->spsext_list, isom_remove_dcr_ps );
3393 lsmash_destroy_h264_parameter_sets( &info->avcC_param_next );
3394 return 0;
3395 fail:
3396 remove_h264_importer( h264_imp );
3397 importer->info = NULL;
3398 lsmash_remove_entries( importer->summaries, lsmash_cleanup_summary );
3399 return -1;
3402 static uint32_t h264_importer_get_last_delta( importer_t *importer, uint32_t track_number )
3404 debug_if( !importer || !importer->info )
3405 return 0;
3406 h264_importer_t *h264_imp = (h264_importer_t *)importer->info;
3407 if( !h264_imp || track_number != 1 || h264_imp->status != IMPORTER_EOF )
3408 return 0;
3409 return h264_imp->ts_list.sample_count
3410 ? h264_imp->last_delta
3411 : UINT32_MAX; /* arbitrary */
3414 static const importer_functions h264_importer =
3416 { "H.264", offsetof( importer_t, log_level ) },
3418 h264_importer_probe,
3419 h264_importer_get_accessunit,
3420 h264_importer_get_last_delta,
3421 h264_importer_cleanup
3424 /***************************************************************************
3425 HEVC importer
3426 ITU-T Recommendation H.265 (04/13)
3427 ISO/IEC 14496-15:2014
3428 ***************************************************************************/
3429 #include "codecs/hevc.h"
3431 typedef struct
3433 importer_status status;
3434 hevc_info_t info;
3435 lsmash_entry_list_t hvcC_list[1]; /* stored as lsmash_codec_specific_t */
3436 lsmash_media_ts_list_t ts_list;
3437 uint32_t max_au_length;
3438 uint32_t num_undecodable;
3439 uint32_t hvcC_number;
3440 uint32_t last_delta;
3441 uint64_t last_intra_cts;
3442 uint8_t composition_reordering_present;
3443 uint8_t field_pic_present;
3444 uint8_t max_TemporalId;
3445 } hevc_importer_info_t;
3447 static void remove_hevc_importer_info( hevc_importer_info_t *info )
3449 if( !info )
3450 return;
3451 lsmash_remove_entries( info->hvcC_list, lsmash_destroy_codec_specific_data );
3452 hevc_cleanup_parser( &info->info );
3453 if( info->ts_list.timestamp )
3454 lsmash_free( info->ts_list.timestamp );
3455 lsmash_free( info );
3458 static void hevc_importer_cleanup( importer_t *importer )
3460 debug_if( importer && importer->info )
3461 remove_hevc_importer_info( importer->info );
3464 static hevc_importer_info_t *create_hevc_importer_info( importer_t *importer )
3466 hevc_importer_info_t *info = lsmash_malloc_zero( sizeof(hevc_importer_info_t) );
3467 if( !info )
3468 return NULL;
3469 if( hevc_setup_parser( &info->info, &importer->sb, 0, LSMASH_STREAM_BUFFERS_TYPE_FILE, importer->stream ) )
3471 remove_hevc_importer_info( info );
3472 return NULL;
3474 lsmash_init_entry_list( info->hvcC_list );
3475 info->info.eos = 1;
3476 return info;
3479 static inline int hevc_complete_au( hevc_access_unit_t *au, int probe )
3481 if( !au->picture.has_primary || au->incomplete_length == 0 )
3482 return 0;
3483 if( !probe )
3484 memcpy( au->data, au->incomplete_data, au->incomplete_length );
3485 au->TemporalId = au->picture.TemporalId;
3486 au->length = au->incomplete_length;
3487 au->incomplete_length = 0;
3488 au->picture.has_primary = 0;
3489 return 1;
3492 static void hevc_append_nalu_to_au( hevc_access_unit_t *au, uint8_t *src_nalu, uint32_t nalu_length, int probe )
3494 if( !probe )
3496 uint8_t *dst_nalu = au->incomplete_data + au->incomplete_length + HEVC_DEFAULT_NALU_LENGTH_SIZE;
3497 for( int i = HEVC_DEFAULT_NALU_LENGTH_SIZE; i; i-- )
3498 *(dst_nalu - i) = (nalu_length >> ((i - 1) * 8)) & 0xff;
3499 memcpy( dst_nalu, src_nalu, nalu_length );
3501 /* Note: picture->incomplete_au_length shall be 0 immediately after AU has completed.
3502 * Therefore, possible_au_length in hevc_get_access_unit_internal() can't be used here
3503 * to avoid increasing AU length monotonously through the entire stream. */
3504 au->incomplete_length += HEVC_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
3507 static inline void hevc_get_au_internal_end( hevc_importer_info_t *info, hevc_access_unit_t *au, hevc_nalu_header_t *nalu_header, int no_more_buf )
3509 if( info->info.buffer.sb->no_more_read && no_more_buf && (au->incomplete_length == 0) )
3510 info->status = IMPORTER_EOF;
3511 else if( info->status != IMPORTER_CHANGE )
3512 info->status = IMPORTER_OK;
3513 info->info.nalu_header = *nalu_header;
3516 static int hevc_get_au_internal_succeeded( hevc_importer_info_t *info, hevc_access_unit_t *au, hevc_nalu_header_t *nalu_header, int no_more_buf )
3518 hevc_get_au_internal_end( info, au, nalu_header, no_more_buf );
3519 au->number += 1;
3520 return 0;
3523 static int hevc_get_au_internal_failed( hevc_importer_info_t *info, hevc_access_unit_t *au, hevc_nalu_header_t *nalu_header, int no_more_buf, int complete_au )
3525 hevc_get_au_internal_end( info, au, nalu_header, no_more_buf );
3526 if( complete_au )
3527 au->number += 1;
3528 return -1;
3531 static lsmash_video_summary_t *hevc_create_summary
3533 lsmash_hevc_specific_parameters_t *param,
3534 hevc_sps_t *sps,
3535 uint32_t max_au_length
3538 lsmash_video_summary_t *summary = (lsmash_video_summary_t *)lsmash_create_summary( LSMASH_SUMMARY_TYPE_VIDEO );
3539 if( !summary )
3540 return NULL;
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 ) )
3549 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
3550 lsmash_destroy_codec_specific_data( specific );
3551 return NULL;
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 );
3568 return summary;
3571 static int hevc_store_codec_specific
3573 hevc_importer_info_t *info,
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 );
3579 if( !src_cs )
3580 return -1;
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( info->hvcC_list, dst_cs ) )
3588 lsmash_destroy_codec_specific_data( dst_cs );
3589 return -1;
3591 return 0;
3594 /* If probe equals 0, don't get the actual data (EBPS) of an access unit and only parse NALU. */
3595 static int hevc_get_access_unit_internal
3597 hevc_importer_info_t *importer_info,
3598 int probe
3601 hevc_info_t *info = &importer_info->info;
3602 hevc_slice_info_t *slice = &info->slice;
3603 hevc_access_unit_t *au = &info->au;
3604 hevc_picture_info_t *picture = &au->picture;
3605 hevc_stream_buffer_t *hb = &info->buffer;
3606 lsmash_stream_buffers_t *sb = hb->sb;
3607 hevc_nalu_header_t nalu_header = info->nalu_header;
3608 uint64_t consecutive_zero_byte_count = 0;
3609 uint64_t ebsp_length = 0;
3610 int no_more_buf = 0;
3611 int complete_au = 0;
3612 picture->type = HEVC_PICTURE_TYPE_NONE;
3613 picture->random_accessible = 0;
3614 picture->recovery_poc_cnt = 0;
3615 au->length = 0;
3616 while( 1 )
3618 lsmash_stream_buffers_update( sb, 2 );
3619 no_more_buf = (lsmash_stream_buffers_get_remainder( sb ) == 0);
3620 int no_more = lsmash_stream_buffers_is_eos( sb ) && no_more_buf;
3621 if( !nalu_check_next_short_start_code( sb->pos, sb->end ) && !no_more )
3623 if( lsmash_stream_buffers_get_byte( sb ) )
3624 consecutive_zero_byte_count = 0;
3625 else
3626 ++consecutive_zero_byte_count;
3627 ++ebsp_length;
3628 continue;
3630 if( no_more && ebsp_length == 0 )
3632 /* For the last NALU.
3633 * This NALU already has been appended into the latest access unit and parsed. */
3634 hevc_update_picture_info( info, picture, slice, &info->sps, &info->sei );
3635 hevc_complete_au( au, probe );
3636 return hevc_get_au_internal_succeeded( importer_info, au, &nalu_header, no_more_buf );
3638 uint64_t next_nalu_head_pos = info->ebsp_head_pos + ebsp_length + !no_more * HEVC_SHORT_START_CODE_LENGTH;
3639 /* Memorize position of short start code of the next NALU in buffer.
3640 * This is used when backward reading of stream doesn't occur. */
3641 uint8_t *next_short_start_code_pos = lsmash_stream_buffers_get_pos( sb );
3642 uint8_t nalu_type = nalu_header.nal_unit_type;
3643 int read_back = 0;
3644 #if 0
3645 if( probe )
3647 fprintf( stderr, "NALU type: %"PRIu8"\n", nalu_type );
3648 fprintf( stderr, " NALU header position: %"PRIx64"\n", info->ebsp_head_pos - nalu_header.length );
3649 fprintf( stderr, " EBSP position: %"PRIx64"\n", info->ebsp_head_pos );
3650 fprintf( stderr, " EBSP length: %"PRIx64" (%"PRIu64")\n", ebsp_length - consecutive_zero_byte_count,
3651 ebsp_length - consecutive_zero_byte_count );
3652 fprintf( stderr, " consecutive_zero_byte_count: %"PRIx64"\n", consecutive_zero_byte_count );
3653 fprintf( stderr, " Next NALU header position: %"PRIx64"\n", next_nalu_head_pos );
3655 #endif
3656 if( nalu_type == HEVC_NALU_TYPE_FD )
3658 /* We don't support streams with both filler and HRD yet.
3659 * Otherwise, just skip filler because elemental streams defined in 14496-15 are forbidden to use filler. */
3660 if( info->sps.vui.hrd.present )
3661 return hevc_get_au_internal_failed( importer_info, au, &nalu_header, no_more_buf, complete_au );
3663 else if( nalu_type <= HEVC_NALU_TYPE_RASL_R
3664 || (nalu_type >= HEVC_NALU_TYPE_BLA_W_LP && nalu_type <= HEVC_NALU_TYPE_CRA)
3665 || (nalu_type >= HEVC_NALU_TYPE_VPS && nalu_type <= HEVC_NALU_TYPE_SUFFIX_SEI) )
3667 /* Get the EBSP of the current NALU here. */
3668 ebsp_length -= consecutive_zero_byte_count; /* Any EBSP doesn't have zero bytes at the end. */
3669 uint64_t nalu_length = nalu_header.length + ebsp_length;
3670 uint64_t possible_au_length = au->incomplete_length + HEVC_DEFAULT_NALU_LENGTH_SIZE + nalu_length;
3671 if( lsmash_stream_buffers_get_buffer_size( sb ) < possible_au_length )
3673 if( hevc_supplement_buffer( hb, au, 2 * possible_au_length ) )
3674 return hevc_get_au_internal_failed( importer_info, au, &nalu_header, no_more_buf, complete_au );
3675 next_short_start_code_pos = lsmash_stream_buffers_get_pos( sb );
3677 /* Move to the first byte of the current NALU. */
3678 read_back = (lsmash_stream_buffers_get_offset( sb ) < (nalu_length + consecutive_zero_byte_count));
3679 if( read_back )
3681 lsmash_fseek( sb->stream, info->ebsp_head_pos - nalu_header.length, SEEK_SET );
3682 lsmash_stream_buffers_seek( sb, 0, SEEK_SET );
3683 lsmash_stream_buffers_read( sb, nalu_length );
3684 if( lsmash_stream_buffers_get_valid_size( sb ) != nalu_length )
3685 return hevc_get_au_internal_failed( importer_info, au, &nalu_header, no_more_buf, complete_au );
3686 #if 0
3687 if( probe )
3688 fprintf( stderr, " ----Read Back\n" );
3689 #endif
3691 else
3692 lsmash_stream_buffers_seek( sb, -(nalu_length + consecutive_zero_byte_count), SEEK_CUR );
3693 if( nalu_type <= HEVC_NALU_TYPE_RSV_VCL31 )
3695 /* VCL NALU (slice) */
3696 hevc_slice_info_t prev_slice = *slice;
3697 if( hevc_parse_slice_segment_header( info, &nalu_header, hb->rbsp,
3698 lsmash_stream_buffers_get_pos( sb ) + nalu_header.length, ebsp_length ) )
3699 return hevc_get_au_internal_failed( importer_info, au, &nalu_header, no_more_buf, complete_au );
3700 if( probe && info->hvcC_pending )
3702 /* Copy and append a Codec Specific info. */
3703 if( hevc_store_codec_specific( importer_info, &info->hvcC_param ) < 0 )
3704 return -1;
3706 if( hevc_move_pending_hvcC_param( info ) < 0 )
3707 return -1;
3708 if( prev_slice.present )
3710 /* Check whether the AU that contains the previous VCL NALU completed or not. */
3711 if( hevc_find_au_delimit_by_slice_info( info, slice, &prev_slice ) )
3713 /* The current NALU is the first VCL NALU of the primary coded picture of a new AU.
3714 * Therefore, the previous slice belongs to the AU you want at this time. */
3715 hevc_update_picture_info( info, picture, &prev_slice, &info->sps, &info->sei );
3716 complete_au = hevc_complete_au( au, probe );
3718 else
3719 hevc_update_picture_info_for_slice( info, picture, &prev_slice );
3721 hevc_append_nalu_to_au( au, lsmash_stream_buffers_get_pos( sb ), nalu_length, probe );
3722 slice->present = 1;
3724 else
3726 if( hevc_find_au_delimit_by_nalu_type( nalu_type, info->prev_nalu_type ) )
3728 /* The last slice belongs to the AU you want at this time. */
3729 hevc_update_picture_info( info, picture, slice, &info->sps, &info->sei );
3730 complete_au = hevc_complete_au( au, probe );
3732 else if( no_more )
3733 complete_au = hevc_complete_au( au, probe );
3734 switch( nalu_type )
3736 case HEVC_NALU_TYPE_PREFIX_SEI :
3737 case HEVC_NALU_TYPE_SUFFIX_SEI :
3739 uint8_t *sei_pos = lsmash_stream_buffers_get_pos( sb );
3740 if( hevc_parse_sei( info->bits, &info->vps, &info->sps, &info->sei, &nalu_header,
3741 hb->rbsp, sei_pos + nalu_header.length, ebsp_length ) )
3742 return hevc_get_au_internal_failed( importer_info, au, &nalu_header, no_more_buf, complete_au );
3743 hevc_append_nalu_to_au( au, sei_pos, nalu_length, probe );
3744 break;
3746 case HEVC_NALU_TYPE_VPS :
3747 if( hevc_try_to_append_dcr_nalu( info, HEVC_DCR_NALU_TYPE_VPS, sb->pos, nalu_header.length + ebsp_length ) < 0 )
3748 return hevc_get_au_internal_failed( importer_info, au, &nalu_header, no_more_buf, complete_au );
3749 break;
3750 case HEVC_NALU_TYPE_SPS :
3751 if( hevc_try_to_append_dcr_nalu( info, HEVC_DCR_NALU_TYPE_SPS, sb->pos, nalu_header.length + ebsp_length ) < 0 )
3752 return hevc_get_au_internal_failed( importer_info, au, &nalu_header, no_more_buf, complete_au );
3753 break;
3754 case HEVC_NALU_TYPE_PPS :
3755 if( hevc_try_to_append_dcr_nalu( info, HEVC_DCR_NALU_TYPE_PPS, sb->pos, nalu_header.length + ebsp_length ) < 0 )
3756 return hevc_get_au_internal_failed( importer_info, au, &nalu_header, no_more_buf, complete_au );
3757 break;
3758 case HEVC_NALU_TYPE_AUD : /* We drop access unit delimiters. */
3759 break;
3760 default :
3761 hevc_append_nalu_to_au( au, lsmash_stream_buffers_get_pos( sb ), nalu_length, probe );
3762 break;
3764 if( info->hvcC_pending )
3765 importer_info->status = IMPORTER_CHANGE;
3768 /* Move to the first byte of the next NALU. */
3769 if( read_back )
3771 lsmash_fseek( sb->stream, next_nalu_head_pos, SEEK_SET );
3772 lsmash_stream_buffers_seek( sb, 0, SEEK_SET );
3773 lsmash_stream_buffers_read( sb, 0 );
3775 else
3776 lsmash_stream_buffers_set_pos( sb, next_short_start_code_pos + HEVC_SHORT_START_CODE_LENGTH );
3777 info->prev_nalu_type = nalu_type;
3778 lsmash_stream_buffers_update( sb, 1 );
3779 no_more_buf = (lsmash_stream_buffers_get_remainder( sb ) == 0);
3780 ebsp_length = 0;
3781 no_more = lsmash_stream_buffers_is_eos( sb ) && no_more_buf;
3782 if( !no_more )
3784 /* Check the next NALU header. */
3785 if( hevc_check_nalu_header( &nalu_header, sb, !!consecutive_zero_byte_count ) )
3786 return hevc_get_au_internal_failed( importer_info, au, &nalu_header, no_more_buf, complete_au );
3787 info->ebsp_head_pos = next_nalu_head_pos + nalu_header.length;
3788 /* Check if the end of sequence. Used for POC calculation. */
3789 info->eos = nalu_header.nal_unit_type == HEVC_NALU_TYPE_EOS
3790 || nalu_header.nal_unit_type == HEVC_NALU_TYPE_EOB;
3792 /* If there is no more data in the stream, and flushed chunk of NALUs, flush it as complete AU here. */
3793 else if( au->incomplete_length && au->length == 0 )
3795 hevc_update_picture_info( info, picture, slice, &info->sps, &info->sei );
3796 hevc_complete_au( au, probe );
3797 return hevc_get_au_internal_succeeded( importer_info, au, &nalu_header, no_more_buf );
3799 if( complete_au )
3800 return hevc_get_au_internal_succeeded( importer_info, au, &nalu_header, no_more_buf );
3801 consecutive_zero_byte_count = 0;
3805 static int hevc_importer_get_accessunit( importer_t *importer, uint32_t track_number, lsmash_sample_t *buffered_sample )
3807 debug_if( !importer || !importer->info || !buffered_sample->data || !buffered_sample->length )
3808 return -1;
3809 if( !importer->info || track_number != 1 )
3810 return -1;
3811 hevc_importer_info_t *importer_info = (hevc_importer_info_t *)importer->info;
3812 hevc_info_t *info = &importer_info->info;
3813 importer_status current_status = importer_info->status;
3814 if( current_status == IMPORTER_ERROR || buffered_sample->length < importer_info->max_au_length )
3815 return -1;
3816 if( current_status == IMPORTER_EOF )
3818 buffered_sample->length = 0;
3819 return 0;
3821 if( hevc_get_access_unit_internal( importer_info, 0 ) )
3823 importer_info->status = IMPORTER_ERROR;
3824 return -1;
3826 if( importer_info->status == IMPORTER_CHANGE && !info->hvcC_pending )
3827 current_status = IMPORTER_CHANGE;
3828 if( current_status == IMPORTER_CHANGE )
3830 /* Update the active summary. */
3831 lsmash_codec_specific_t *cs = (lsmash_codec_specific_t *)lsmash_get_entry_data( importer_info->hvcC_list, ++ importer_info->hvcC_number );
3832 if( !cs )
3833 return -1;
3834 lsmash_hevc_specific_parameters_t *hvcC_param = (lsmash_hevc_specific_parameters_t *)cs->data.structured;
3835 lsmash_video_summary_t *summary = hevc_create_summary( hvcC_param, &info->sps, importer_info->max_au_length );
3836 if( !summary )
3837 return -1;
3838 lsmash_remove_entry( importer->summaries, track_number, lsmash_cleanup_summary );
3839 if( lsmash_add_entry( importer->summaries, summary ) )
3841 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
3842 return -1;
3844 importer_info->status = IMPORTER_OK;
3846 //hevc_sps_t *sps = &info->sps;
3847 hevc_access_unit_t *au = &info->au;
3848 hevc_picture_info_t *picture = &au->picture;
3849 buffered_sample->dts = importer_info->ts_list.timestamp[ au->number - 1 ].dts;
3850 buffered_sample->cts = importer_info->ts_list.timestamp[ au->number - 1 ].cts;
3851 /* Set property of disposability. */
3852 if( picture->sublayer_nonref && au->TemporalId == importer_info->max_TemporalId )
3853 /* Sub-layer non-reference pictures are not referenced by subsequent pictures of
3854 * the same sub-layer in decoding order. */
3855 buffered_sample->prop.disposable = ISOM_SAMPLE_IS_DISPOSABLE;
3856 else
3857 buffered_sample->prop.disposable = ISOM_SAMPLE_IS_NOT_DISPOSABLE;
3858 /* Set property of leading. */
3859 if( picture->radl || picture->rasl )
3860 buffered_sample->prop.leading = picture->radl ? ISOM_SAMPLE_IS_DECODABLE_LEADING : ISOM_SAMPLE_IS_UNDECODABLE_LEADING;
3861 else
3863 if( au->number < importer_info->num_undecodable )
3864 buffered_sample->prop.leading = ISOM_SAMPLE_IS_UNDECODABLE_LEADING;
3865 else
3867 if( picture->independent || buffered_sample->cts >= importer_info->last_intra_cts )
3868 buffered_sample->prop.leading = ISOM_SAMPLE_IS_NOT_LEADING;
3869 else
3870 buffered_sample->prop.leading = ISOM_SAMPLE_IS_UNDECODABLE_LEADING;
3873 if( picture->independent )
3874 importer_info->last_intra_cts = buffered_sample->cts;
3875 /* Set property of independence. */
3876 buffered_sample->prop.independent = picture->independent ? ISOM_SAMPLE_IS_INDEPENDENT : ISOM_SAMPLE_IS_NOT_INDEPENDENT;
3877 buffered_sample->prop.redundant = ISOM_SAMPLE_HAS_NO_REDUNDANCY;
3878 buffered_sample->prop.post_roll.identifier = picture->poc;
3879 if( picture->random_accessible )
3881 if( picture->irap )
3883 buffered_sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
3884 if( picture->closed_rap )
3885 buffered_sample->prop.ra_flags |= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_CLOSED_RAP;
3886 else
3887 buffered_sample->prop.ra_flags |= ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP;
3889 else if( picture->recovery_poc_cnt )
3891 buffered_sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_POST_ROLL_START;
3892 buffered_sample->prop.post_roll.complete = picture->poc + picture->recovery_poc_cnt;
3894 else
3895 buffered_sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_RAP;
3897 buffered_sample->length = au->length;
3898 memcpy( buffered_sample->data, au->data, au->length );
3899 return current_status;
3902 static int hevc_importer_probe( importer_t *importer )
3904 #define HEVC_LONG_START_CODE_LENGTH 4
3905 #define HEVC_CHECK_NEXT_LONG_START_CODE( x ) (!(x)[0] && !(x)[1] && !(x)[2] && ((x)[3] == 0x01))
3906 hevc_importer_info_t *importer_info = create_hevc_importer_info( importer );
3907 if( !importer_info )
3908 return -1;
3909 hevc_info_t *info = &importer_info->info;
3910 hevc_stream_buffer_t *hb = &info->buffer;
3911 lsmash_stream_buffers_t *sb = hb->sb; /* shall be equal to &importer->sb */
3912 /* Find the first start code. */
3913 lsmash_stream_buffers_seek( sb, 0, SEEK_SET );
3914 lsmash_stream_buffers_read( sb, 0 );
3915 while( 1 )
3917 /* The first NALU of an AU in decoding order shall have long start code (0x00000001). */
3918 if( HEVC_CHECK_NEXT_LONG_START_CODE( sb->pos ) )
3919 break;
3920 /* If the first trial of finding long start code failed, we assume this stream is not byte stream format of H.264. */
3921 if( lsmash_stream_buffers_get_remainder( sb ) == HEVC_LONG_START_CODE_LENGTH )
3922 goto fail;
3923 /* Invalid if encountered any value of non-zero before the first start code. */
3924 if( lsmash_stream_buffers_get_byte( sb ) )
3925 goto fail;
3927 /* OK. It seems the stream has a long start code of H.264. */
3928 importer->info = importer_info;
3929 lsmash_stream_buffers_seek( sb, HEVC_LONG_START_CODE_LENGTH, SEEK_CUR );
3930 uint64_t first_ebsp_head_pos = lsmash_stream_buffers_get_offset( sb );
3931 lsmash_stream_buffers_update( sb, 0 );
3932 hevc_nalu_header_t first_nalu_header;
3933 if( hevc_check_nalu_header( &first_nalu_header, sb, 1 ) )
3934 goto fail;
3935 if( lsmash_stream_buffers_get_remainder( sb ) == 0 )
3936 goto fail; /* It seems the stream ends at the first incomplete access unit. */
3937 first_ebsp_head_pos += first_nalu_header.length; /* EBSP doesn't include NALU header. */
3938 importer_info->status = IMPORTER_OK;
3939 info->nalu_header = first_nalu_header;
3940 info->ebsp_head_pos = first_ebsp_head_pos;
3941 info->prev_nalu_type = HEVC_NALU_TYPE_UNKNOWN;
3942 /* Parse all NALU in the stream for preparation of calculating timestamps. */
3943 uint32_t npt_alloc = (1 << 12) * sizeof(nal_pic_timing_t);
3944 nal_pic_timing_t *npt = (nal_pic_timing_t *)lsmash_malloc( npt_alloc );
3945 if( !npt )
3946 goto fail;
3947 uint32_t picture_stats[HEVC_PICTURE_TYPE_NONE + 1] = { 0 };
3948 uint32_t num_access_units = 0;
3949 lsmash_log( importer, LSMASH_LOG_INFO, "Analyzing stream as HEVC\r" );
3950 while( importer_info->status != IMPORTER_EOF )
3952 #if 0
3953 lsmash_log( importer, LSMASH_LOG_INFO, "Analyzing stream as HEVC: %"PRIu32"\n", num_access_units + 1 );
3954 #endif
3955 hevc_picture_info_t *picture = &info->au.picture;
3956 hevc_picture_info_t prev_picture = *picture;
3957 if( hevc_get_access_unit_internal( importer_info, 1 )
3958 || hevc_calculate_poc( info, &info->au.picture, &prev_picture ) )
3960 lsmash_free( npt );
3961 goto fail;
3963 if( npt_alloc <= num_access_units * sizeof(nal_pic_timing_t) )
3965 uint32_t alloc = 2 * num_access_units * sizeof(nal_pic_timing_t);
3966 nal_pic_timing_t *temp = (nal_pic_timing_t *)lsmash_realloc( npt, alloc );
3967 if( !temp )
3969 lsmash_free( npt );
3970 goto fail;
3972 npt = temp;
3973 npt_alloc = alloc;
3975 importer_info->field_pic_present |= picture->field_coded;
3976 npt[num_access_units].poc = picture->poc;
3977 npt[num_access_units].delta = picture->delta;
3978 npt[num_access_units].poc_delta = 1;
3979 npt[num_access_units].reset = 0;
3980 ++num_access_units;
3981 importer_info->max_au_length = LSMASH_MAX( importer_info->max_au_length, info->au.length );
3982 importer_info->max_TemporalId = LSMASH_MAX( importer_info->max_TemporalId, info->au.TemporalId );
3983 if( picture->idr )
3984 ++picture_stats[HEVC_PICTURE_TYPE_IDR];
3985 else if( picture->irap )
3986 ++picture_stats[ picture->broken_link ? HEVC_PICTURE_TYPE_BLA : HEVC_PICTURE_TYPE_CRA ];
3987 else if( picture->type >= HEVC_PICTURE_TYPE_NONE )
3988 ++picture_stats[HEVC_PICTURE_TYPE_NONE];
3989 else
3990 ++picture_stats[ picture->type ];
3992 lsmash_log_refresh_line( importer );
3993 lsmash_log( importer, LSMASH_LOG_INFO,
3994 "IDR: %"PRIu32", CRA: %"PRIu32", BLA: %"PRIu32", I: %"PRIu32", P: %"PRIu32", B: %"PRIu32", Unknown: %"PRIu32"\n",
3995 picture_stats[HEVC_PICTURE_TYPE_IDR], picture_stats[HEVC_PICTURE_TYPE_CRA],
3996 picture_stats[HEVC_PICTURE_TYPE_BLA], picture_stats[HEVC_PICTURE_TYPE_I],
3997 picture_stats[HEVC_PICTURE_TYPE_I_P], picture_stats[HEVC_PICTURE_TYPE_I_P_B],
3998 picture_stats[HEVC_PICTURE_TYPE_NONE]);
3999 /* Copy and append the last Codec Specific info. */
4000 if( hevc_store_codec_specific( importer_info, &info->hvcC_param ) < 0 )
4001 return -1;
4002 /* Set up the first summary. */
4003 lsmash_codec_specific_t *cs = (lsmash_codec_specific_t *)lsmash_get_entry_data( importer_info->hvcC_list, ++ importer_info->hvcC_number );
4004 if( !cs || !cs->data.structured )
4006 lsmash_free( npt );
4007 goto fail;
4009 lsmash_hevc_specific_parameters_t *hvcC_param = (lsmash_hevc_specific_parameters_t *)cs->data.structured;
4010 lsmash_video_summary_t *summary = hevc_create_summary( hvcC_param, &info->sps, importer_info->max_au_length );
4011 if( !summary || lsmash_add_entry( importer->summaries, summary ) )
4013 if( summary )
4014 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
4015 lsmash_free( npt );
4016 goto fail;
4018 summary->sample_per_field = importer_info->field_pic_present;
4019 /* */
4020 lsmash_media_ts_t *timestamp = lsmash_malloc( num_access_units * sizeof(lsmash_media_ts_t) );
4021 if( !timestamp )
4023 lsmash_free( npt );
4024 goto fail;
4026 /* Count leading samples that are undecodable. */
4027 for( uint32_t i = 0; i < num_access_units; i++ )
4029 if( npt[i].poc == 0 )
4030 break;
4031 ++ importer_info->num_undecodable;
4033 /* Deduplicate POCs. */
4034 uint32_t max_composition_delay = 0;
4035 nalu_deduplicate_poc( npt, &max_composition_delay, num_access_units, 15 );
4036 /* Generate timestamps. */
4037 nalu_generate_timestamps_from_poc( importer, timestamp, npt,
4038 &importer_info->composition_reordering_present,
4039 &importer_info->last_delta,
4040 max_composition_delay, num_access_units );
4041 summary->timescale *= 2; /* We assume that picture timing is in field level.
4042 * For HEVC, it seems time_scale is set in frame level basically.
4043 * So multiply by 2 for reducing timebase and timescale. */
4044 nalu_reduce_timescale( timestamp, npt, &importer_info->last_delta, &summary->timescale, num_access_units );
4045 lsmash_free( npt );
4046 importer_info->ts_list.sample_count = num_access_units;
4047 importer_info->ts_list.timestamp = timestamp;
4048 /* Go back to EBSP of the first NALU. */
4049 lsmash_fseek( sb->stream, first_ebsp_head_pos, SEEK_SET );
4050 importer_info->status = IMPORTER_OK;
4051 info->nalu_header = first_nalu_header;
4052 info->ebsp_head_pos = first_ebsp_head_pos;
4053 info->prev_nalu_type = HEVC_NALU_TYPE_UNKNOWN;
4054 sb->no_more_read = 0;
4055 lsmash_stream_buffers_seek( sb, 0, SEEK_SET );
4056 lsmash_stream_buffers_read( sb, 0 );
4057 uint8_t *temp_au = info->au.data;
4058 uint8_t *temp_incomplete_au = info->au.incomplete_data;
4059 memset( &info->au, 0, sizeof(hevc_access_unit_t) );
4060 info->au.data = temp_au;
4061 info->au.incomplete_data = temp_incomplete_au;
4062 memset( &info->slice, 0, sizeof(hevc_slice_info_t) );
4063 memset( &info->vps, 0, sizeof(hevc_vps_t) );
4064 memset( &info->sps, 0, sizeof(hevc_sps_t) );
4065 memset( &info->pps, 0, SIZEOF_PPS_EXCLUDING_HEAP );
4066 for( int i = 0; i < HEVC_DCR_NALU_TYPE_NUM; i++ )
4067 lsmash_remove_entries( info->hvcC_param.parameter_arrays->ps_array[i].list, isom_remove_dcr_ps );
4068 lsmash_destroy_hevc_parameter_arrays( &info->hvcC_param_next );
4069 return 0;
4070 fail:
4071 lsmash_log_refresh_line( importer );
4072 remove_hevc_importer_info( importer_info );
4073 importer->info = NULL;
4074 lsmash_remove_entries( importer->summaries, lsmash_cleanup_summary );
4075 return -1;
4076 #undef HEVC_LONG_START_CODE_LENGTH
4077 #undef HEVC_CHECK_NEXT_LONG_START_CODE
4080 static uint32_t hevc_importer_get_last_delta( importer_t *importer, uint32_t track_number )
4082 debug_if( !importer || !importer->info )
4083 return 0;
4084 hevc_importer_info_t *info = (hevc_importer_info_t *)importer->info;
4085 if( !info || track_number != 1 || info->status != IMPORTER_EOF )
4086 return 0;
4087 return info->ts_list.sample_count
4088 ? info->last_delta
4089 : UINT32_MAX; /* arbitrary */
4092 static const importer_functions hevc_importer =
4094 { "HEVC" },
4096 hevc_importer_probe,
4097 hevc_importer_get_accessunit,
4098 hevc_importer_get_last_delta,
4099 hevc_importer_cleanup
4102 /***************************************************************************
4103 SMPTE VC-1 importer (only for Advanced Profile)
4104 SMPTE 421M-2006
4105 SMPTE RP 2025-2007
4106 ***************************************************************************/
4107 #include "codecs/vc1.h"
4109 typedef struct
4111 importer_status status;
4112 vc1_info_t info;
4113 vc1_sequence_header_t first_sequence;
4114 lsmash_media_ts_list_t ts_list;
4115 lsmash_bs_t *bs;
4116 uint8_t composition_reordering_present;
4117 uint32_t max_au_length;
4118 uint32_t num_undecodable;
4119 uint64_t last_ref_intra_cts;
4120 } vc1_importer_t;
4122 static void remove_vc1_importer( vc1_importer_t *vc1_imp )
4124 if( !vc1_imp )
4125 return;
4126 vc1_cleanup_parser( &vc1_imp->info );
4127 lsmash_bs_cleanup( vc1_imp->bs );
4128 lsmash_free( vc1_imp->ts_list.timestamp );
4129 lsmash_free( vc1_imp );
4132 static void vc1_importer_cleanup( importer_t *importer )
4134 debug_if( importer && importer->info )
4135 remove_vc1_importer( importer->info );
4138 static vc1_importer_t *create_vc1_importer( importer_t *importer )
4140 vc1_importer_t *vc1_imp = lsmash_malloc_zero( sizeof(vc1_importer_t) );
4141 if( !vc1_imp )
4142 return NULL;
4143 if( vc1_setup_parser( &vc1_imp->info, 0 ) < 0 )
4145 remove_vc1_importer( vc1_imp );
4146 return NULL;
4148 lsmash_bs_t *bs = lsmash_bs_create();
4149 if( !bs )
4151 remove_vc1_importer( vc1_imp );
4152 return NULL;
4154 bs->stream = importer->stream;
4155 bs->read = lsmash_fread_wrapper;
4156 bs->seek = lsmash_fseek_wrapper;
4157 bs->unseekable = importer->is_stdin;
4158 bs->buffer.max_size = BS_MAX_DEFAULT_READ_SIZE;
4159 vc1_imp->bs = bs;
4160 return vc1_imp;
4163 static inline int vc1_complete_au( vc1_access_unit_t *access_unit, vc1_picture_info_t *picture, int probe )
4165 if( !picture->present )
4166 return 0;
4167 if( !probe )
4168 memcpy( access_unit->data, access_unit->incomplete_data, access_unit->incomplete_data_length );
4169 access_unit->data_length = access_unit->incomplete_data_length;
4170 access_unit->incomplete_data_length = 0;
4171 vc1_update_au_property( access_unit, picture );
4172 return 1;
4175 static inline void vc1_append_ebdu_to_au( vc1_access_unit_t *access_unit, uint8_t *ebdu, uint32_t ebdu_length, int probe )
4177 if( !probe )
4178 memcpy( access_unit->incomplete_data + access_unit->incomplete_data_length, ebdu, ebdu_length );
4179 /* Note: access_unit->incomplete_data_length shall be 0 immediately after AU has completed.
4180 * Therefore, possible_au_length in vc1_get_access_unit_internal() can't be used here
4181 * to avoid increasing AU length monotonously through the entire stream. */
4182 access_unit->incomplete_data_length += ebdu_length;
4185 static inline void vc1_get_au_internal_end( vc1_importer_t *vc1_imp, vc1_access_unit_t *access_unit )
4187 vc1_imp->status = lsmash_bs_is_end( vc1_imp->bs, 0 ) && (access_unit->incomplete_data_length == 0)
4188 ? IMPORTER_EOF
4189 : IMPORTER_OK;
4192 static int vc1_get_au_internal_succeeded( vc1_importer_t *vc1_imp )
4194 vc1_access_unit_t *access_unit = &vc1_imp->info.access_unit;
4195 vc1_get_au_internal_end( vc1_imp, access_unit );
4196 access_unit->number += 1;
4197 return 0;
4200 static int vc1_get_au_internal_failed( vc1_importer_t *vc1_imp, int complete_au )
4202 vc1_access_unit_t *access_unit = &vc1_imp->info.access_unit;
4203 vc1_get_au_internal_end( vc1_imp, access_unit );
4204 if( complete_au )
4205 access_unit->number += 1;
4206 return -1;
4209 static int vc1_importer_get_access_unit_internal( importer_t *importer, int probe )
4211 vc1_importer_t *vc1_imp = (vc1_importer_t *)importer->info;
4212 vc1_info_t *info = &vc1_imp->info;
4213 vc1_stream_buffer_t *sb = &info->buffer;
4214 vc1_access_unit_t *access_unit = &info->access_unit;
4215 lsmash_bs_t *bs = vc1_imp->bs;
4216 int complete_au = 0;
4217 access_unit->data_length = 0;
4218 while( 1 )
4220 uint8_t bdu_type;
4221 uint64_t trailing_zero_bytes;
4222 uint64_t ebdu_length = vc1_find_next_start_code_prefix( bs, &bdu_type, &trailing_zero_bytes );
4223 if( ebdu_length <= VC1_START_CODE_LENGTH && lsmash_bs_is_end( bs, ebdu_length ) )
4225 /* For the last EBDU.
4226 * This EBDU already has been appended into the latest access unit and parsed. */
4227 vc1_complete_au( access_unit, &info->picture, probe );
4228 return vc1_get_au_internal_succeeded( vc1_imp );
4230 else if( bdu_type == 0xFF )
4232 lsmash_log( importer, LSMASH_LOG_ERROR, "a forbidden BDU type is detected.\n" );
4233 return vc1_get_au_internal_failed( vc1_imp, complete_au );
4235 uint64_t next_ebdu_head_pos = info->ebdu_head_pos
4236 + ebdu_length
4237 + trailing_zero_bytes;
4238 #if 0
4239 if( probe )
4241 fprintf( stderr, "BDU type: %"PRIu8" \n", bdu_type );
4242 fprintf( stderr, " EBDU position: %"PRIx64" \n", info->ebdu_head_pos );
4243 fprintf( stderr, " EBDU length: %"PRIx64" (%"PRIu64")\n", ebdu_length, ebdu_length );
4244 fprintf( stderr, " trailing_zero_bytes: %"PRIx64" \n", trailing_zero_bytes );
4245 fprintf( stderr, " Next EBDU position: %"PRIx64" \n", next_ebdu_head_pos );
4247 #endif
4248 if( bdu_type >= 0x0A && bdu_type <= 0x0F )
4250 /* Complete the current access unit if encountered delimiter of current access unit. */
4251 if( vc1_find_au_delimit_by_bdu_type( bdu_type, info->prev_bdu_type ) )
4252 /* The last video coded EBDU belongs to the access unit you want at this time. */
4253 complete_au = vc1_complete_au( access_unit, &info->picture, probe );
4254 /* Increase the buffer if needed. */
4255 uint64_t possible_au_length = access_unit->incomplete_data_length + ebdu_length;
4256 if( sb->bank->buffer_size < possible_au_length
4257 && vc1_supplement_buffer( sb, access_unit, 2 * possible_au_length ) < 0 )
4259 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to increase the buffer size.\n" );
4260 return vc1_get_au_internal_failed( vc1_imp, complete_au );
4262 /* Process EBDU by its BDU type and append it to access unit. */
4263 uint8_t *ebdu = lsmash_bs_get_buffer_data( bs );
4264 switch( bdu_type )
4266 /* FRM_SC: Frame start code
4267 * FLD_SC: Field start code
4268 * SLC_SC: Slice start code
4269 * SEQ_SC: Sequence header start code
4270 * EP_SC: Entry-point start code
4271 * PIC_L: Picture layer
4272 * SLC_L: Slice layer
4273 * SEQ_L: Sequence layer
4274 * EP_L: Entry-point layer */
4275 case 0x0D : /* Frame
4276 * For the Progressive or Frame Interlace mode, shall signal the beginning of a new video frame.
4277 * For the Field Interlace mode, shall signal the beginning of a sequence of two independently coded video fields.
4278 * [FRM_SC][PIC_L][[FLD_SC][PIC_L] (optional)][[SLC_SC][SLC_L] (optional)] ... */
4279 if( vc1_parse_advanced_picture( info->bits, &info->sequence, &info->picture, sb->rbdu, ebdu, ebdu_length ) < 0 )
4281 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to parse a frame.\n" );
4282 return vc1_get_au_internal_failed( vc1_imp, complete_au );
4284 case 0x0C : /* Field
4285 * Shall only be used for Field Interlaced frames
4286 * and shall only be used to signal the beginning of the second field of the frame.
4287 * [FRM_SC][PIC_L][FLD_SC][PIC_L][[SLC_SC][SLC_L] (optional)] ...
4288 * Field start code is followed by INTERLACE_FIELD_PICTURE_FIELD2() which doesn't have info of its field picture type.*/
4289 break;
4290 case 0x0B : /* Slice
4291 * Shall not be used for start code of the first slice of a frame.
4292 * Shall not be used for start code of the first slice of an interlace field coded picture.
4293 * [FRM_SC][PIC_L][[FLD_SC][PIC_L] (optional)][SLC_SC][SLC_L][[SLC_SC][SLC_L] (optional)] ...
4294 * Slice layer may repeat frame header. We just ignore it. */
4295 info->dvc1_param.slice_present = 1;
4296 break;
4297 case 0x0E : /* Entry-point header
4298 * Entry-point indicates the direct followed frame is a start of group of frames.
4299 * Entry-point doesn't indicates the frame is a random access point when multiple sequence headers are present,
4300 * since it is necessary to decode sequence header which subsequent frames belong to for decoding them.
4301 * Entry point shall be followed by
4302 * 1. I-picture - progressive or frame interlace
4303 * 2. I/I-picture, I/P-picture, or P/I-picture - field interlace
4304 * [[SEQ_SC][SEQ_L] (optional)][EP_SC][EP_L][FRM_SC][PIC_L] ... */
4305 if( vc1_parse_entry_point_header( info, ebdu, ebdu_length, probe ) < 0 )
4307 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to parse an entry point.\n" );
4308 return vc1_get_au_internal_failed( vc1_imp, complete_au );
4310 /* Signal random access type of the frame that follows this entry-point header. */
4311 info->picture.closed_gop = info->entry_point.closed_entry_point;
4312 info->picture.random_accessible = info->dvc1_param.multiple_sequence ? info->picture.start_of_sequence : 1;
4313 break;
4314 case 0x0F : /* Sequence header
4315 * [SEQ_SC][SEQ_L][EP_SC][EP_L][FRM_SC][PIC_L] ... */
4316 if( vc1_parse_sequence_header( info, ebdu, ebdu_length, probe ) < 0 )
4318 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to parse a sequence header.\n" );
4319 return vc1_get_au_internal_failed( vc1_imp, complete_au );
4321 /* The frame that is the first frame after this sequence header shall be a random accessible point. */
4322 info->picture.start_of_sequence = 1;
4323 if( probe && !vc1_imp->first_sequence.present )
4324 vc1_imp->first_sequence = info->sequence;
4325 break;
4326 default : /* End-of-sequence (0x0A) */
4327 break;
4329 /* Append the current EBDU into the end of an incomplete access unit. */
4330 vc1_append_ebdu_to_au( access_unit, ebdu, ebdu_length, probe );
4332 else /* We don't support other BDU types such as user data yet. */
4333 return vc1_get_au_internal_failed( vc1_imp, complete_au );
4334 /* Move to the first byte of the next EBDU. */
4335 info->prev_bdu_type = bdu_type;
4336 if( lsmash_bs_read_seek( bs, next_ebdu_head_pos, SEEK_SET ) != next_ebdu_head_pos )
4338 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to seek the next start code suffix.\n" );
4339 return vc1_get_au_internal_failed( vc1_imp, complete_au );
4341 /* Check if no more data to read from the stream. */
4342 if( !lsmash_bs_is_end( bs, VC1_START_CODE_PREFIX_LENGTH ) )
4343 info->ebdu_head_pos = next_ebdu_head_pos;
4344 /* If there is no more data in the stream, and flushed chunk of EBDUs, flush it as complete AU here. */
4345 else if( access_unit->incomplete_data_length && access_unit->data_length == 0 )
4347 vc1_complete_au( access_unit, &info->picture, probe );
4348 return vc1_get_au_internal_succeeded( vc1_imp );
4350 if( complete_au )
4351 return vc1_get_au_internal_succeeded( vc1_imp );
4355 static int vc1_importer_get_accessunit( importer_t *importer, uint32_t track_number, lsmash_sample_t *buffered_sample )
4357 debug_if( !importer || !importer->info || !buffered_sample->data || !buffered_sample->length )
4358 return -1;
4359 if( !importer->info || track_number != 1 )
4360 return -1;
4361 vc1_importer_t *vc1_imp = (vc1_importer_t *)importer->info;
4362 vc1_info_t *info = &vc1_imp->info;
4363 importer_status current_status = vc1_imp->status;
4364 if( current_status == IMPORTER_ERROR || buffered_sample->length < vc1_imp->max_au_length )
4365 return -1;
4366 if( current_status == IMPORTER_EOF )
4368 buffered_sample->length = 0;
4369 return 0;
4371 if( vc1_importer_get_access_unit_internal( importer, 0 ) < 0 )
4373 vc1_imp->status = IMPORTER_ERROR;
4374 return -1;
4376 vc1_access_unit_t *access_unit = &info->access_unit;
4377 buffered_sample->dts = vc1_imp->ts_list.timestamp[ access_unit->number - 1 ].dts;
4378 buffered_sample->cts = vc1_imp->ts_list.timestamp[ access_unit->number - 1 ].cts;
4379 buffered_sample->prop.leading = access_unit->independent
4380 || access_unit->non_bipredictive
4381 || buffered_sample->cts >= vc1_imp->last_ref_intra_cts
4382 ? ISOM_SAMPLE_IS_NOT_LEADING : ISOM_SAMPLE_IS_UNDECODABLE_LEADING;
4383 if( access_unit->independent && !access_unit->disposable )
4384 vc1_imp->last_ref_intra_cts = buffered_sample->cts;
4385 if( vc1_imp->composition_reordering_present && !access_unit->disposable && !access_unit->closed_gop )
4386 buffered_sample->prop.allow_earlier = QT_SAMPLE_EARLIER_PTS_ALLOWED;
4387 buffered_sample->prop.independent = access_unit->independent ? ISOM_SAMPLE_IS_INDEPENDENT : ISOM_SAMPLE_IS_NOT_INDEPENDENT;
4388 buffered_sample->prop.disposable = access_unit->disposable ? ISOM_SAMPLE_IS_DISPOSABLE : ISOM_SAMPLE_IS_NOT_DISPOSABLE;
4389 buffered_sample->prop.redundant = ISOM_SAMPLE_HAS_NO_REDUNDANCY;
4390 if( access_unit->random_accessible )
4391 /* All random access point is a sync sample even if it's an open RAP. */
4392 buffered_sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
4393 buffered_sample->length = access_unit->data_length;
4394 memcpy( buffered_sample->data, access_unit->data, access_unit->data_length );
4395 return current_status;
4398 static lsmash_video_summary_t *vc1_create_summary( vc1_info_t *info, vc1_sequence_header_t *sequence, uint32_t max_au_length )
4400 if( !info->sequence.present || !info->entry_point.present )
4401 return NULL;
4402 lsmash_video_summary_t *summary = (lsmash_video_summary_t *)lsmash_create_summary( LSMASH_SUMMARY_TYPE_VIDEO );
4403 if( !summary )
4404 return NULL;
4405 lsmash_codec_specific_t *specific = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_VC_1,
4406 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED );
4407 specific->data.unstructured = lsmash_create_vc1_specific_info( &info->dvc1_param, &specific->size );
4408 if( !specific->data.unstructured
4409 || lsmash_add_entry( &summary->opaque->list, specific ) < 0 )
4411 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
4412 lsmash_destroy_codec_specific_data( specific );
4413 return NULL;
4415 summary->sample_type = ISOM_CODEC_TYPE_VC_1_VIDEO;
4416 summary->max_au_length = max_au_length;
4417 summary->timescale = sequence->framerate_numerator;
4418 summary->timebase = sequence->framerate_denominator;
4419 summary->vfr = !sequence->framerate_flag;
4420 summary->sample_per_field = 0;
4421 summary->width = sequence->disp_horiz_size;
4422 summary->height = sequence->disp_vert_size;
4423 summary->par_h = sequence->aspect_width;
4424 summary->par_v = sequence->aspect_height;
4425 summary->color.primaries_index = sequence->color_prim;
4426 summary->color.transfer_index = sequence->transfer_char;
4427 summary->color.matrix_index = sequence->matrix_coef;
4428 return summary;
4431 static int vc1_analyze_whole_stream
4433 importer_t *importer
4436 /* Parse all EBDU in the stream for preparation of calculating timestamps. */
4437 uint32_t cts_alloc = (1 << 12) * sizeof(uint64_t);
4438 uint64_t *cts = lsmash_malloc( cts_alloc );
4439 if( !cts )
4440 return -1; /* Failed to allocate CTS list */
4441 uint32_t num_access_units = 0;
4442 uint32_t num_consecutive_b = 0;
4443 lsmash_class_t *logger = &(lsmash_class_t){ "VC-1" };
4444 lsmash_log( &logger, LSMASH_LOG_INFO, "Analyzing stream as VC-1\r" );
4445 vc1_importer_t *vc1_imp = (vc1_importer_t *)importer->info;
4446 vc1_info_t *info = &vc1_imp->info;
4447 vc1_imp->status = IMPORTER_OK;
4448 while( vc1_imp->status != IMPORTER_EOF )
4450 #if 0
4451 lsmash_log( &logger, LSMASH_LOG_INFO, "Analyzing stream as VC-1: %"PRIu32"\n", num_access_units + 1 );
4452 #endif
4453 if( vc1_importer_get_access_unit_internal( importer, 1 ) < 0 )
4454 goto fail;
4455 /* In the case where B-pictures exist
4456 * Decode order
4457 * I[0]P[1]P[2]B[3]B[4]P[5]...
4458 * DTS
4459 * 0 1 2 3 4 5 ...
4460 * Composition order
4461 * I[0]P[1]B[3]B[4]P[2]P[5]...
4462 * CTS
4463 * 1 2 3 4 5 6 ...
4464 * We assumes B or BI-pictures always be present in the stream here. */
4465 if( !info->access_unit.disposable )
4467 /* Apply CTS of the last B-picture plus 1 to the last non-B-picture. */
4468 if( num_access_units > num_consecutive_b )
4469 cts[ num_access_units - num_consecutive_b - 1 ] = num_access_units;
4470 num_consecutive_b = 0;
4472 else /* B or BI-picture */
4474 /* B and BI-pictures shall be output or displayed in the same order as they are encoded. */
4475 cts[ num_access_units ] = num_access_units;
4476 ++num_consecutive_b;
4477 info->dvc1_param.bframe_present = 1;
4479 if( cts_alloc <= num_access_units * sizeof(uint64_t) )
4481 uint32_t alloc = 2 * num_access_units * sizeof(uint64_t);
4482 uint64_t *temp = lsmash_realloc( cts, alloc );
4483 if( !temp )
4484 goto fail; /* Failed to re-allocate CTS list */
4485 cts = temp;
4486 cts_alloc = alloc;
4488 vc1_imp->max_au_length = LSMASH_MAX( info->access_unit.data_length, vc1_imp->max_au_length );
4489 ++num_access_units;
4491 if( num_access_units > num_consecutive_b )
4492 cts[ num_access_units - num_consecutive_b - 1 ] = num_access_units;
4493 else
4494 goto fail;
4495 /* Construct timestamps. */
4496 lsmash_media_ts_t *timestamp = lsmash_malloc( num_access_units * sizeof(lsmash_media_ts_t) );
4497 if( !timestamp )
4498 goto fail; /* Failed to allocate timestamp list */
4499 for( uint32_t i = 1; i < num_access_units; i++ )
4500 if( cts[i] < cts[i - 1] )
4502 vc1_imp->composition_reordering_present = 1;
4503 break;
4505 if( vc1_imp->composition_reordering_present )
4506 for( uint32_t i = 0; i < num_access_units; i++ )
4508 timestamp[i].cts = cts[i];
4509 timestamp[i].dts = i;
4511 else
4512 for( uint32_t i = 0; i < num_access_units; i++ )
4513 timestamp[i].cts = timestamp[i].dts = i;
4514 lsmash_free( cts );
4515 lsmash_log_refresh_line( &logger );
4516 #if 0
4517 for( uint32_t i = 0; i < num_access_units; i++ )
4518 fprintf( stderr, "Timestamp[%"PRIu32"]: DTS=%"PRIu64", CTS=%"PRIu64"\n", i, timestamp[i].dts, timestamp[i].cts );
4519 #endif
4520 vc1_imp->ts_list.sample_count = num_access_units;
4521 vc1_imp->ts_list.timestamp = timestamp;
4522 return 0;
4523 fail:
4524 lsmash_log_refresh_line( &logger );
4525 lsmash_free( cts );
4526 return -1;
4529 static int vc1_importer_probe( importer_t *importer )
4531 /* Find the first start code. */
4532 vc1_importer_t *vc1_imp = create_vc1_importer( importer );
4533 if( !vc1_imp )
4534 return -1;
4535 lsmash_bs_t *bs = vc1_imp->bs;
4536 uint64_t first_ebdu_head_pos = 0;
4537 while( 1 )
4539 /* The first EBDU in decoding order of the stream shall have start code (0x000001). */
4540 if( 0x000001 == lsmash_bs_show_be24( bs, first_ebdu_head_pos ) )
4541 break;
4542 /* Invalid if encountered any value of non-zero before the first start code. */
4543 if( lsmash_bs_show_byte( bs, first_ebdu_head_pos ) )
4544 goto fail;
4545 ++first_ebdu_head_pos;
4547 /* OK. It seems the stream has a sequence header of VC-1. */
4548 importer->info = vc1_imp;
4549 vc1_info_t *info = &vc1_imp->info;
4550 lsmash_bs_read_seek( bs, first_ebdu_head_pos, SEEK_SET );
4551 info->ebdu_head_pos = first_ebdu_head_pos;
4552 if( vc1_analyze_whole_stream( importer ) < 0 )
4553 goto fail;
4554 lsmash_video_summary_t *summary = vc1_create_summary( info, &vc1_imp->first_sequence, vc1_imp->max_au_length );
4555 if( !summary )
4556 goto fail;
4557 if( lsmash_add_entry( importer->summaries, summary ) < 0 )
4559 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
4560 goto fail;
4562 /* Go back to layer of the first EBDU. */
4563 vc1_imp->status = IMPORTER_OK;
4564 lsmash_bs_read_seek( bs, first_ebdu_head_pos, SEEK_SET );
4565 info->prev_bdu_type = 0xFF; /* 0xFF is a forbidden value. */
4566 info->ebdu_head_pos = first_ebdu_head_pos;
4567 uint8_t *temp_access_unit = info->access_unit.data;
4568 uint8_t *temp_incomplete_access_unit = info->access_unit.incomplete_data;
4569 memset( &info->access_unit, 0, sizeof(vc1_access_unit_t) );
4570 info->access_unit.data = temp_access_unit;
4571 info->access_unit.incomplete_data = temp_incomplete_access_unit;
4572 memset( &info->picture, 0, sizeof(vc1_picture_info_t) );
4573 return 0;
4574 fail:
4575 remove_vc1_importer( vc1_imp );
4576 importer->info = NULL;
4577 lsmash_remove_entries( importer->summaries, lsmash_cleanup_summary );
4578 return -1;
4581 static uint32_t vc1_importer_get_last_delta( importer_t *importer, uint32_t track_number )
4583 debug_if( !importer || !importer->info )
4584 return 0;
4585 vc1_importer_t *vc1_imp = (vc1_importer_t *)importer->info;
4586 if( !vc1_imp || track_number != 1 || vc1_imp->status != IMPORTER_EOF )
4587 return 0;
4588 return vc1_imp->ts_list.sample_count
4590 : UINT32_MAX; /* arbitrary */
4593 static const importer_functions vc1_importer =
4595 { "VC-1", offsetof( importer_t, log_level ) },
4597 vc1_importer_probe,
4598 vc1_importer_get_accessunit,
4599 vc1_importer_get_last_delta,
4600 vc1_importer_cleanup
4603 /***************************************************************************
4604 importer public interfaces
4605 ***************************************************************************/
4607 /******** importer listing table ********/
4608 static const importer_functions *importer_func_table[] =
4610 &mp4sys_adts_importer,
4611 &mp4sys_mp3_importer,
4612 &amr_importer,
4613 &ac3_importer,
4614 &eac3_importer,
4615 &mp4a_als_importer,
4616 &dts_importer,
4617 &h264_importer,
4618 &hevc_importer,
4619 &vc1_importer,
4620 NULL,
4623 /******** importer public functions ********/
4625 void lsmash_importer_close( importer_t *importer )
4627 if( !importer )
4628 return;
4629 if( !importer->is_stdin && importer->stream )
4630 fclose( importer->stream );
4631 if( importer->funcs.cleanup )
4632 importer->funcs.cleanup( importer );
4633 lsmash_remove_list( importer->summaries, lsmash_cleanup_summary );
4634 lsmash_free( importer );
4637 importer_t *lsmash_importer_open( const char *identifier, const char *format )
4639 if( identifier == NULL )
4640 return NULL;
4641 int auto_detect = ( format == NULL || !strcmp( format, "auto" ) );
4642 importer_t *importer = (importer_t *)lsmash_malloc_zero( sizeof(importer_t) );
4643 if( !importer )
4644 return NULL;
4645 importer->class = &lsmash_importer_class;
4646 if( !strcmp( identifier, "-" ) )
4648 /* special treatment for stdin */
4649 if( auto_detect )
4651 lsmash_log( importer, LSMASH_LOG_ERROR, "auto importer detection on stdin is not supported.\n" );
4652 goto fail;
4654 importer->stream = stdin;
4655 importer->is_stdin = 1;
4657 else if( (importer->stream = lsmash_fopen( identifier, "rb" )) == NULL )
4659 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to open %s.\n", identifier );
4660 goto fail;
4662 importer->summaries = lsmash_create_entry_list();
4663 if( !importer->summaries )
4665 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to set up the importer.\n" );
4666 goto fail;
4668 /* find importer */
4669 importer->log_level = LSMASH_LOG_QUIET; /* Any error log is confusing for the probe step. */
4670 const importer_functions *funcs;
4671 if( auto_detect )
4673 /* just rely on detector. */
4674 for( int i = 0; (funcs = importer_func_table[i]) != NULL; i++ )
4676 importer->class = &funcs->class;
4677 if( !funcs->detectable )
4678 continue;
4679 if( !funcs->probe( importer ) || lsmash_fseek( importer->stream, 0, SEEK_SET ) )
4680 break;
4683 else
4685 /* needs name matching. */
4686 for( int i = 0; (funcs = importer_func_table[i]) != NULL; i++ )
4688 importer->class = &funcs->class;
4689 if( strcmp( importer->class->name, format ) )
4690 continue;
4691 if( funcs->probe( importer ) )
4692 funcs = NULL;
4693 break;
4696 importer->log_level = LSMASH_LOG_INFO;
4697 if( !funcs )
4699 importer->class = &lsmash_importer_class;
4700 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to find the matched importer.\n" );
4701 goto fail;
4703 importer->funcs = *funcs;
4704 return importer;
4705 fail:
4706 lsmash_importer_close( importer );
4707 return NULL;
4710 /* 0 if success, positive if changed, negative if failed */
4711 int lsmash_importer_get_access_unit( importer_t *importer, uint32_t track_number, lsmash_sample_t *buffered_sample )
4713 if( !importer || !importer->funcs.get_accessunit || !buffered_sample->data || buffered_sample->length == 0 )
4714 return -1;
4715 return importer->funcs.get_accessunit( importer, track_number, buffered_sample );
4718 /* Return 0 if failed, otherwise succeeded. */
4719 uint32_t lsmash_importer_get_last_delta( importer_t *importer, uint32_t track_number )
4721 if( !importer || !importer->funcs.get_last_delta )
4722 return 0;
4723 return importer->funcs.get_last_delta( importer, track_number );
4726 uint32_t lsmash_importer_get_track_count( importer_t *importer )
4728 if( !importer || !importer->summaries )
4729 return 0;
4730 return importer->summaries->entry_count;
4733 lsmash_summary_t *lsmash_duplicate_summary( importer_t *importer, uint32_t track_number )
4735 if( !importer )
4736 return NULL;
4737 lsmash_summary_t *src_summary = lsmash_get_entry_data( importer->summaries, track_number );
4738 if( !src_summary )
4739 return NULL;
4740 lsmash_summary_t *summary = lsmash_create_summary( src_summary->summary_type );
4741 if( !summary )
4742 return NULL;
4743 lsmash_codec_specific_list_t *opaque = summary->opaque;
4744 switch( src_summary->summary_type )
4746 case LSMASH_SUMMARY_TYPE_VIDEO :
4747 *(lsmash_video_summary_t *)summary = *(lsmash_video_summary_t *)src_summary;
4748 break;
4749 case LSMASH_SUMMARY_TYPE_AUDIO :
4750 *(lsmash_audio_summary_t *)summary = *(lsmash_audio_summary_t *)src_summary;
4751 break;
4752 default :
4753 lsmash_cleanup_summary( summary );
4754 return NULL;
4756 summary->opaque = opaque;
4757 for( lsmash_entry_t *entry = src_summary->opaque->list.head; entry; entry = entry->next )
4759 lsmash_codec_specific_t *src_specific = (lsmash_codec_specific_t *)entry->data;
4760 if( !src_specific )
4761 continue;
4762 lsmash_codec_specific_t *dup = isom_duplicate_codec_specific_data( src_specific );
4763 if( lsmash_add_entry( &summary->opaque->list, dup ) )
4765 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
4766 lsmash_destroy_codec_specific_data( dup );
4767 return NULL;
4770 return summary;