print: Avoid Variable Length Arrays.
[L-SMASH.git] / importer / a52_imp.c
bloba7ec2825a580de7237eda8be58423e346c35cf24
1 /*****************************************************************************
2 * a52_imp.c
3 *****************************************************************************
4 * Copyright (C) 2011-2014 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
21 /* This file is available under an ISC license. */
23 #include "common/internal.h" /* must be placed first */
25 #include <string.h>
27 #define LSMASH_IMPORTER_INTERNAL
28 #include "importer.h"
30 /***************************************************************************
31 AC-3 importer
32 ETSI TS 102 366 V1.2.1 (2008-08)
33 ***************************************************************************/
34 #include "codecs/a52.h"
36 #define AC3_SAMPLE_DURATION 1536 /* 256 (samples per audio block) * 6 (audio blocks) */
38 typedef struct
40 ac3_info_t info;
41 uint64_t next_frame_pos;
42 uint8_t *next_dac3;
43 uint8_t buffer[AC3_MAX_SYNCFRAME_LENGTH];
44 uint32_t au_number;
45 } ac3_importer_t;
47 static void remove_ac3_importer( ac3_importer_t *ac3_imp )
49 if( !ac3_imp )
50 return;
51 lsmash_bits_cleanup( ac3_imp->info.bits );
52 lsmash_free( ac3_imp );
55 static ac3_importer_t *create_ac3_importer( importer_t *importer )
57 ac3_importer_t *ac3_imp = (ac3_importer_t *)lsmash_malloc_zero( sizeof(ac3_importer_t) );
58 if( !ac3_imp )
59 return NULL;
60 ac3_imp->info.bits = lsmash_bits_create( importer->bs );
61 if( !ac3_imp->info.bits )
63 lsmash_free( ac3_imp );
64 return NULL;
66 return ac3_imp;
69 static void ac3_importer_cleanup( importer_t *importer )
71 debug_if( importer && importer->info )
72 remove_ac3_importer( importer->info );
75 static const uint32_t ac3_frame_size_table[19][3] =
77 /* 48, 44.1, 32 */
78 { 128, 138, 192 },
79 { 160, 174, 240 },
80 { 192, 208, 288 },
81 { 224, 242, 336 },
82 { 256, 278, 384 },
83 { 320, 348, 480 },
84 { 384, 416, 576 },
85 { 448, 486, 672 },
86 { 512, 556, 768 },
87 { 640, 696, 960 },
88 { 768, 834, 1152 },
89 { 896, 974, 1344 },
90 { 1024, 1114, 1536 },
91 { 1280, 1392, 1920 },
92 { 1536, 1670, 2304 },
93 { 1792, 1950, 2688 },
94 { 2048, 2228, 3072 },
95 { 2304, 2506, 3456 },
96 { 2560, 2786, 3840 }
99 static lsmash_audio_summary_t *ac3_create_summary( ac3_info_t *info )
101 lsmash_audio_summary_t *summary = (lsmash_audio_summary_t *)lsmash_create_summary( LSMASH_SUMMARY_TYPE_AUDIO );
102 if( !summary )
103 return NULL;
104 lsmash_ac3_specific_parameters_t *param = &info->dac3_param;
105 lsmash_codec_specific_t *cs = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_AC_3,
106 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED );
107 cs->data.unstructured = lsmash_create_ac3_specific_info( &info->dac3_param, &cs->size );
108 if( !cs->data.unstructured
109 || lsmash_add_entry( &summary->opaque->list, cs ) < 0 )
111 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
112 lsmash_destroy_codec_specific_data( cs );
113 return NULL;
115 summary->sample_type = ISOM_CODEC_TYPE_AC_3_AUDIO;
116 summary->max_au_length = AC3_MAX_SYNCFRAME_LENGTH;
117 summary->aot = MP4A_AUDIO_OBJECT_TYPE_NULL; /* no effect */
118 summary->frequency = ac3_get_sample_rate( param );
119 summary->channels = ac3_get_channel_count( param );
120 summary->sample_size = 16; /* no effect */
121 summary->samples_in_frame = AC3_SAMPLE_DURATION;
122 summary->sbr_mode = MP4A_AAC_SBR_NOT_SPECIFIED; /* no effect */
123 return summary;
126 static int ac3_compare_specific_param( lsmash_ac3_specific_parameters_t *a, lsmash_ac3_specific_parameters_t *b )
128 return (a->fscod != b->fscod)
129 || (a->bsid != b->bsid)
130 || (a->bsmod != b->bsmod)
131 || (a->acmod != b->acmod)
132 || (a->lfeon != b->lfeon)
133 || ((a->frmsizecod >> 1) != (b->frmsizecod >> 1));
136 static int ac3_buffer_frame( uint8_t *buffer, lsmash_bs_t *bs )
138 uint64_t remain_size = lsmash_bs_get_remaining_buffer_size( bs );
139 if( remain_size < AC3_MAX_SYNCFRAME_LENGTH )
141 int err = lsmash_bs_read( bs, bs->buffer.max_size );
142 if( err < 0 )
143 return err;
144 remain_size = lsmash_bs_get_remaining_buffer_size( bs );
146 uint64_t copy_size = LSMASH_MIN( remain_size, AC3_MAX_SYNCFRAME_LENGTH );
147 memcpy( buffer, lsmash_bs_get_buffer_data( bs ), copy_size );
148 return 0;
151 static int ac3_importer_get_accessunit( importer_t *importer, uint32_t track_number, lsmash_sample_t **p_sample )
153 if( !importer->info )
154 return LSMASH_ERR_NAMELESS;
155 if( track_number != 1 )
156 return LSMASH_ERR_FUNCTION_PARAM;
157 lsmash_audio_summary_t *summary = (lsmash_audio_summary_t *)lsmash_get_entry_data( importer->summaries, track_number );
158 if( !summary )
159 return LSMASH_ERR_NAMELESS;
160 ac3_importer_t *ac3_imp = (ac3_importer_t *)importer->info;
161 ac3_info_t *info = &ac3_imp->info;
162 importer_status current_status = importer->status;
163 if( current_status == IMPORTER_ERROR )
164 return LSMASH_ERR_NAMELESS;
165 if( current_status == IMPORTER_EOF )
166 return IMPORTER_EOF;
167 lsmash_ac3_specific_parameters_t *param = &info->dac3_param;
168 uint32_t frame_size = ac3_frame_size_table[ param->frmsizecod >> 1 ][ param->fscod ];
169 if( param->fscod == 0x1 && param->frmsizecod & 0x1 )
170 frame_size += 2;
171 if( current_status == IMPORTER_CHANGE )
173 lsmash_codec_specific_t *cs = isom_get_codec_specific( summary->opaque, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_AC_3 );
174 if( cs )
176 cs->destruct( cs->data.unstructured );
177 cs->data.unstructured = ac3_imp->next_dac3;
179 summary->frequency = ac3_get_sample_rate( param );
180 summary->channels = ac3_get_channel_count( param );
181 //summary->layout_tag = ac3_channel_layout_table[ param->acmod ][ param->lfeon ];
183 lsmash_sample_t *sample = lsmash_create_sample( frame_size );
184 if( !sample )
185 return LSMASH_ERR_MEMORY_ALLOC;
186 *p_sample = sample;
187 memcpy( sample->data, ac3_imp->buffer, frame_size );
188 sample->length = frame_size;
189 sample->dts = ac3_imp->au_number++ * summary->samples_in_frame;
190 sample->cts = sample->dts;
191 sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
192 sample->prop.pre_roll.distance = 1; /* MDCT */
193 lsmash_bs_t *bs = info->bits->bs;
194 ac3_imp->next_frame_pos += frame_size;
195 lsmash_bs_read_seek( bs, ac3_imp->next_frame_pos, SEEK_SET );
196 uint8_t syncword[2] =
198 lsmash_bs_show_byte( bs, 0 ),
199 lsmash_bs_show_byte( bs, 1 )
201 if( bs->eob || (bs->eof && 0 == lsmash_bs_get_remaining_buffer_size( bs )) )
202 importer->status = IMPORTER_EOF;
203 else
205 /* Parse the next syncframe header. */
206 if( syncword[0] != 0x0b
207 || syncword[1] != 0x77
208 || ac3_buffer_frame( ac3_imp->buffer, bs ) < 0 )
210 importer->status = IMPORTER_ERROR;
211 return current_status;
213 lsmash_ac3_specific_parameters_t current_param = info->dac3_param;
214 ac3_parse_syncframe_header( info );
215 if( ac3_compare_specific_param( &current_param, &info->dac3_param ) )
217 uint32_t dummy;
218 uint8_t *dac3 = lsmash_create_ac3_specific_info( &info->dac3_param, &dummy );
219 if( !dac3 )
221 importer->status = IMPORTER_ERROR;
222 return current_status;
224 ac3_imp->next_dac3 = dac3;
225 importer->status = IMPORTER_CHANGE;
227 else
228 importer->status = IMPORTER_OK;
230 return current_status;
233 static int ac3_importer_probe( importer_t *importer )
235 ac3_importer_t *ac3_imp = create_ac3_importer( importer );
236 if( !ac3_imp )
237 return LSMASH_ERR_MEMORY_ALLOC;
238 lsmash_bits_t *bits = ac3_imp->info.bits;
239 lsmash_bs_t *bs = bits->bs;
240 bs->buffer.max_size = AC3_MAX_SYNCFRAME_LENGTH;
241 /* Check the syncword and parse the syncframe header */
242 int err;
243 if( lsmash_bs_show_byte( bs, 0 ) != 0x0b
244 || lsmash_bs_show_byte( bs, 1 ) != 0x77 )
246 err = LSMASH_ERR_INVALID_DATA;
247 goto fail;
249 if( (err = ac3_buffer_frame( ac3_imp->buffer, bs )) < 0
250 || (err = ac3_parse_syncframe_header( &ac3_imp->info )) < 0 )
251 goto fail;
252 lsmash_audio_summary_t *summary = ac3_create_summary( &ac3_imp->info );
253 if( !summary )
255 err = LSMASH_ERR_NAMELESS;
256 goto fail;
258 if( lsmash_add_entry( importer->summaries, summary ) < 0 )
260 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
261 err = LSMASH_ERR_MEMORY_ALLOC;
262 goto fail;
264 ac3_imp->au_number = 0;
265 importer->info = ac3_imp;
266 importer->status = IMPORTER_OK;
267 return 0;
268 fail:
269 remove_ac3_importer( ac3_imp );
270 return err;
273 static uint32_t ac3_importer_get_last_delta( importer_t *importer, uint32_t track_number )
275 debug_if( !importer || !importer->info )
276 return 0;
277 ac3_importer_t *ac3_imp = (ac3_importer_t *)importer->info;
278 if( !ac3_imp || track_number != 1 || importer->status != IMPORTER_EOF )
279 return 0;
280 return AC3_SAMPLE_DURATION;
283 const importer_functions ac3_importer =
285 { "AC-3" },
287 ac3_importer_probe,
288 ac3_importer_get_accessunit,
289 ac3_importer_get_last_delta,
290 ac3_importer_cleanup
293 /***************************************************************************
294 Enhanced AC-3 importer
295 ETSI TS 102 366 V1.2.1 (2008-08)
296 ***************************************************************************/
297 #define EAC3_MIN_SAMPLE_DURATION 256
299 typedef struct
301 eac3_info_t info;
302 uint64_t next_frame_pos;
303 uint32_t next_dec3_length;
304 uint8_t *next_dec3;
305 uint8_t current_fscod2;
306 uint8_t buffer[EAC3_MAX_SYNCFRAME_LENGTH];
307 lsmash_multiple_buffers_t *au_buffers;
308 uint8_t *au;
309 uint8_t *incomplete_au;
310 uint32_t au_length;
311 uint32_t incomplete_au_length;
312 uint32_t au_number;
313 uint32_t syncframe_count_in_au;
314 } eac3_importer_t;
316 static void remove_eac3_importer( eac3_importer_t *eac3_imp )
318 if( !eac3_imp )
319 return;
320 lsmash_destroy_multiple_buffers( eac3_imp->au_buffers );
321 lsmash_bits_cleanup( eac3_imp->info.bits );
322 lsmash_free( eac3_imp );
325 static eac3_importer_t *create_eac3_importer( importer_t *importer )
327 eac3_importer_t *eac3_imp = (eac3_importer_t *)lsmash_malloc_zero( sizeof(eac3_importer_t) );
328 if( !eac3_imp )
329 return NULL;
330 eac3_info_t *info = &eac3_imp->info;
331 info->bits = lsmash_bits_create( importer->bs );
332 if( !info->bits )
334 lsmash_free( eac3_imp );
335 return NULL;
337 eac3_imp->au_buffers = lsmash_create_multiple_buffers( 2, EAC3_MAX_SYNCFRAME_LENGTH );
338 if( !eac3_imp->au_buffers )
340 lsmash_bits_cleanup( info->bits );
341 lsmash_free( eac3_imp );
342 return NULL;
344 eac3_imp->au = lsmash_withdraw_buffer( eac3_imp->au_buffers, 1 );
345 eac3_imp->incomplete_au = lsmash_withdraw_buffer( eac3_imp->au_buffers, 2 );
346 return eac3_imp;
349 static void eac3_importer_cleanup( importer_t *importer )
351 debug_if( importer && importer->info )
352 remove_eac3_importer( importer->info );
355 static int eac3_importer_get_next_accessunit_internal( importer_t *importer )
357 int au_completed = 0;
358 eac3_importer_t *eac3_imp = (eac3_importer_t *)importer->info;
359 eac3_info_t *info = &eac3_imp->info;
360 lsmash_bs_t *bs = info->bits->bs;
361 while( !au_completed )
363 /* Read data from the stream if needed. */
364 eac3_imp->next_frame_pos += info->frame_size;
365 lsmash_bs_read_seek( bs, eac3_imp->next_frame_pos, SEEK_SET );
366 uint64_t remain_size = lsmash_bs_get_remaining_buffer_size( bs );
367 if( remain_size < EAC3_MAX_SYNCFRAME_LENGTH )
369 int err = lsmash_bs_read( bs, bs->buffer.max_size );
370 if( err < 0 )
372 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to read data from the stream.\n" );
373 return err;
375 remain_size = lsmash_bs_get_remaining_buffer_size( bs );
377 uint64_t copy_size = LSMASH_MIN( remain_size, EAC3_MAX_SYNCFRAME_LENGTH );
378 memcpy( eac3_imp->buffer, lsmash_bs_get_buffer_data( bs ), copy_size );
379 /* Check the remainder length of the buffer.
380 * If there is enough length, then parse the syncframe in it.
381 * The length 5 is the required byte length to get frame size. */
382 if( bs->eob || (bs->eof && remain_size < 5) )
384 /* Reached the end of stream.
385 * According to ETSI TS 102 366 V1.2.1 (2008-08),
386 * one access unit consists of 6 audio blocks and begins with independent substream 0.
387 * The specification doesn't mention the case where a enhanced AC-3 stream ends at non-mod6 audio blocks.
388 * At the end of the stream, therefore, we might make an access unit which has less than 6 audio blocks anyway. */
389 importer->status = IMPORTER_EOF;
390 au_completed = !!eac3_imp->incomplete_au_length;
391 if( !au_completed )
393 /* No more access units in the stream. */
394 if( lsmash_bs_get_remaining_buffer_size( bs ) )
396 lsmash_log( importer, LSMASH_LOG_WARNING, "the stream is truncated at the end.\n" );
397 return LSMASH_ERR_INVALID_DATA;
399 return 0;
401 if( !info->dec3_param_initialized )
402 eac3_update_specific_param( info );
404 else
406 /* Check the syncword. */
407 if( lsmash_bs_show_byte( bs, 0 ) != 0x0b
408 || lsmash_bs_show_byte( bs, 1 ) != 0x77 )
410 lsmash_log( importer, LSMASH_LOG_ERROR, "a syncword is not found.\n" );
411 return LSMASH_ERR_INVALID_DATA;
413 /* Parse syncframe. */
414 info->frame_size = 0;
415 int err = eac3_parse_syncframe( info );
416 if( err < 0 )
418 lsmash_log( importer, LSMASH_LOG_ERROR, "failed to parse syncframe.\n" );
419 return err;
421 if( remain_size < info->frame_size )
423 lsmash_log( importer, LSMASH_LOG_ERROR, "a frame is truncated.\n" );
424 return LSMASH_ERR_INVALID_DATA;
426 int independent = info->strmtyp != 0x1;
427 if( independent && info->substreamid == 0x0 )
429 if( info->number_of_audio_blocks == 6 )
431 /* Encountered the first syncframe of the next access unit. */
432 info->number_of_audio_blocks = 0;
433 au_completed = 1;
435 else if( info->number_of_audio_blocks > 6 )
437 lsmash_log( importer, LSMASH_LOG_ERROR, "greater than 6 consecutive independent substreams.\n" );
438 return LSMASH_ERR_INVALID_DATA;
440 info->number_of_audio_blocks += eac3_audio_block_table[ info->numblkscod ];
441 info->number_of_independent_substreams = 0;
442 eac3_imp->current_fscod2 = info->fscod2;
444 else if( info->syncframe_count == 0 )
446 /* The first syncframe in an AU must be independent and assigned substream ID 0. */
447 lsmash_log( importer, LSMASH_LOG_ERROR, "the first syncframe is NOT an independent substream.\n" );
448 return LSMASH_ERR_INVALID_DATA;
450 if( independent )
451 info->independent_info[info->number_of_independent_substreams ++].num_dep_sub = 0;
452 else
453 ++ info->independent_info[info->number_of_independent_substreams - 1].num_dep_sub;
455 if( au_completed )
457 memcpy( eac3_imp->au, eac3_imp->incomplete_au, eac3_imp->incomplete_au_length );
458 eac3_imp->au_length = eac3_imp->incomplete_au_length;
459 eac3_imp->incomplete_au_length = 0;
460 eac3_imp->syncframe_count_in_au = info->syncframe_count;
461 info->syncframe_count = 0;
462 if( importer->status == IMPORTER_EOF )
463 break;
465 /* Increase buffer size to store AU if short. */
466 if( eac3_imp->incomplete_au_length + info->frame_size > eac3_imp->au_buffers->buffer_size )
468 lsmash_multiple_buffers_t *temp = lsmash_resize_multiple_buffers( eac3_imp->au_buffers,
469 eac3_imp->au_buffers->buffer_size + EAC3_MAX_SYNCFRAME_LENGTH );
470 if( !temp )
471 return LSMASH_ERR_MEMORY_ALLOC;
472 eac3_imp->au_buffers = temp;
473 eac3_imp->au = lsmash_withdraw_buffer( eac3_imp->au_buffers, 1 );
474 eac3_imp->incomplete_au = lsmash_withdraw_buffer( eac3_imp->au_buffers, 2 );
476 /* Append syncframe data. */
477 memcpy( eac3_imp->incomplete_au + eac3_imp->incomplete_au_length, eac3_imp->buffer, info->frame_size );
478 eac3_imp->incomplete_au_length += info->frame_size;
479 ++ info->syncframe_count;
481 return bs->error ? LSMASH_ERR_NAMELESS : 0;
484 static int eac3_importer_get_accessunit( importer_t *importer, uint32_t track_number, lsmash_sample_t **p_sample )
486 if( !importer->info )
487 return LSMASH_ERR_NAMELESS;
488 if( track_number != 1 )
489 return LSMASH_ERR_FUNCTION_PARAM;
490 lsmash_audio_summary_t *summary = (lsmash_audio_summary_t *)lsmash_get_entry_data( importer->summaries, track_number );
491 if( !summary )
492 return LSMASH_ERR_NAMELESS;
493 eac3_importer_t *eac3_imp = (eac3_importer_t *)importer->info;
494 eac3_info_t *info = &eac3_imp->info;
495 importer_status current_status = importer->status;
496 if( current_status == IMPORTER_ERROR )
497 return LSMASH_ERR_NAMELESS;
498 if( current_status == IMPORTER_EOF && eac3_imp->au_length == 0 )
499 return IMPORTER_EOF;
500 if( current_status == IMPORTER_CHANGE )
502 lsmash_codec_specific_t *cs = isom_get_codec_specific( summary->opaque, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3 );
503 if( cs )
505 cs->destruct( cs->data.unstructured );
506 cs->data.unstructured = eac3_imp->next_dec3;
507 cs->size = eac3_imp->next_dec3_length;
509 summary->max_au_length = eac3_imp->syncframe_count_in_au * EAC3_MAX_SYNCFRAME_LENGTH;
510 eac3_update_sample_rate( &summary->frequency, &info->dec3_param, &eac3_imp->current_fscod2 );
511 eac3_update_channel_count( &summary->channels, &info->dec3_param );
513 lsmash_sample_t *sample = lsmash_create_sample( eac3_imp->au_length );
514 if( !sample )
515 return LSMASH_ERR_MEMORY_ALLOC;
516 *p_sample = sample;
517 memcpy( sample->data, eac3_imp->au, eac3_imp->au_length );
518 sample->length = eac3_imp->au_length;
519 sample->dts = eac3_imp->au_number++ * summary->samples_in_frame;
520 sample->cts = sample->dts;
521 sample->prop.ra_flags = ISOM_SAMPLE_RANDOM_ACCESS_FLAG_SYNC;
522 sample->prop.pre_roll.distance = 1; /* MDCT */
523 if( importer->status == IMPORTER_EOF )
525 eac3_imp->au_length = 0;
526 return 0;
528 uint32_t old_syncframe_count_in_au = eac3_imp->syncframe_count_in_au;
529 if( eac3_importer_get_next_accessunit_internal( importer ) < 0 )
531 importer->status = IMPORTER_ERROR;
532 return current_status;
534 if( eac3_imp->syncframe_count_in_au )
536 /* Check sample description change. */
537 uint32_t new_length;
538 uint8_t *dec3 = lsmash_create_eac3_specific_info( &info->dec3_param, &new_length );
539 if( !dec3 )
541 importer->status = IMPORTER_ERROR;
542 return current_status;
544 lsmash_codec_specific_t *cs = isom_get_codec_specific( summary->opaque, LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3 );
545 if( (eac3_imp->syncframe_count_in_au > old_syncframe_count_in_au)
546 || (cs && (new_length != cs->size || memcmp( dec3, cs->data.unstructured, cs->size ))) )
548 importer->status = IMPORTER_CHANGE;
549 eac3_imp->next_dec3 = dec3;
550 eac3_imp->next_dec3_length = new_length;
552 else
554 if( importer->status != IMPORTER_EOF )
555 importer->status = IMPORTER_OK;
556 lsmash_free( dec3 );
559 return current_status;
562 static lsmash_audio_summary_t *eac3_create_summary( eac3_importer_t *eac3_imp )
564 lsmash_audio_summary_t *summary = (lsmash_audio_summary_t *)lsmash_create_summary( LSMASH_SUMMARY_TYPE_AUDIO );
565 if( !summary )
566 return NULL;
567 eac3_info_t *info = &eac3_imp->info;
568 lsmash_codec_specific_t *cs = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_EC_3,
569 LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED );
570 cs->data.unstructured = lsmash_create_eac3_specific_info( &info->dec3_param, &cs->size );
571 if( !cs->data.unstructured
572 || lsmash_add_entry( &summary->opaque->list, cs ) < 0 )
574 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
575 lsmash_destroy_codec_specific_data( cs );
576 return NULL;
578 summary->sample_type = ISOM_CODEC_TYPE_EC_3_AUDIO;
579 summary->max_au_length = eac3_imp->syncframe_count_in_au * EAC3_MAX_SYNCFRAME_LENGTH;
580 summary->aot = MP4A_AUDIO_OBJECT_TYPE_NULL; /* no effect */
581 summary->sample_size = 16; /* no effect */
582 summary->samples_in_frame = EAC3_MIN_SAMPLE_DURATION * 6; /* 256 (samples per audio block) * 6 (audio blocks) */
583 summary->sbr_mode = MP4A_AAC_SBR_NOT_SPECIFIED; /* no effect */
584 eac3_update_sample_rate( &summary->frequency, &info->dec3_param, &eac3_imp->current_fscod2 );
585 eac3_update_channel_count( &summary->channels, &info->dec3_param );
586 return summary;
589 static int eac3_importer_probe( importer_t *importer )
591 eac3_importer_t *eac3_imp = create_eac3_importer( importer );
592 if( !eac3_imp )
593 return LSMASH_ERR_MEMORY_ALLOC;
594 lsmash_bits_t *bits = eac3_imp->info.bits;
595 lsmash_bs_t *bs = bits->bs;
596 bs->buffer.max_size = EAC3_MAX_SYNCFRAME_LENGTH;
597 importer->info = eac3_imp;
598 int err = eac3_importer_get_next_accessunit_internal( importer );
599 if( err < 0 )
600 goto fail;
601 lsmash_audio_summary_t *summary = eac3_create_summary( eac3_imp );
602 if( !summary )
604 err = LSMASH_ERR_NAMELESS;
605 goto fail;
607 if( importer->status != IMPORTER_EOF )
608 importer->status = IMPORTER_OK;
609 eac3_imp->au_number = 0;
610 if( lsmash_add_entry( importer->summaries, summary ) < 0 )
612 lsmash_cleanup_summary( (lsmash_summary_t *)summary );
613 err = LSMASH_ERR_MEMORY_ALLOC;
614 goto fail;
616 return 0;
617 fail:
618 remove_eac3_importer( eac3_imp );
619 importer->info = NULL;
620 return err;
623 static uint32_t eac3_importer_get_last_delta( importer_t *importer, uint32_t track_number )
625 debug_if( !importer || !importer->info )
626 return 0;
627 eac3_importer_t *eac3_imp = (eac3_importer_t *)importer->info;
628 if( !eac3_imp || track_number != 1 || importer->status != IMPORTER_EOF || eac3_imp->au_length )
629 return 0;
630 return EAC3_MIN_SAMPLE_DURATION * eac3_imp->info.number_of_audio_blocks;
633 const importer_functions eac3_importer =
635 { "Enhanced AC-3", offsetof( importer_t, log_level ) },
637 eac3_importer_probe,
638 eac3_importer_get_accessunit,
639 eac3_importer_get_last_delta,
640 eac3_importer_cleanup