hevc: Fix an AU delimit finder by TileId and CtbAddrRsToTs.
[L-SMASH.git] / codecs / mp4a.c
blob9315bbebdbf88f4b05e9ff46aed42c9a13d48e41
1 /*****************************************************************************
2 * mp4a.c:
3 *****************************************************************************
4 * Copyright (C) 2010-2015 L-SMASH project
6 * Authors: Takashi Hirata <silverfilain@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 #define MP4A_INTERNAL
26 #include "core/box.h"
28 #include "mp4a.h"
29 #include "description.h"
31 #include <stdlib.h>
32 #include <string.h>
33 #include <inttypes.h>
35 /***************************************************************************
36 implementation of part of ISO/IEC 14496-3 (ISO/IEC 14496-1 relevant)
37 ***************************************************************************/
39 /* ISO/IEC 14496-3 samplingFrequencyIndex */
40 /* ISO/IEC 14496-3 Sampling frequency mapping */
41 const uint32_t mp4a_sampling_frequency_table[13][5] = {
42 /* threshold, exact, idx_for_ga, idx_for_sbr, idx */
43 { 92017, 96000, 0x0, 0xF, 0x0 }, /* SBR is not allowed */
44 { 75132, 88200, 0x1, 0xF, 0x1 }, /* SBR is not allowed */
45 { 55426, 64000, 0x2, 0xF, 0x2 }, /* SBR is not allowed */
46 { 46009, 48000, 0x3, 0x0, 0x3 },
47 { 37566, 44100, 0x4, 0x1, 0x4 },
48 { 27713, 32000, 0x5, 0x2, 0x5 },
49 { 23004, 24000, 0x6, 0x3, 0x6 },
50 { 18783, 22050, 0x7, 0x4, 0x7 },
51 { 13856, 16000, 0x8, 0x5, 0x8 },
52 { 11502, 12000, 0x9, 0x6, 0x9 },
53 { 9391, 11025, 0xA, 0x7, 0xA },
54 { 8000, 8000, 0xB, 0x8, 0xB },
55 { 0, 7350, 0xB, 0xF, 0xC } /* samplingFrequencyIndex for GASpecificConfig is 0xB (same as 8000Hz). */
58 /* ISO/IEC 14496-3 Interface to ISO/IEC 14496-1 (MPEG-4 Systems), Syntax of AudioSpecificConfig(). */
59 /* This structure is represent of regularized AudioSpecificConfig. */
60 /* for actual definition, see Syntax of GetAudioObjectType() for audioObjectType and extensionAudioObjectType. */
61 typedef struct
63 lsmash_mp4a_aac_sbr_mode sbr_mode; /* L-SMASH's original, including sbrPresent flag. */
64 lsmash_mp4a_AudioObjectType audioObjectType;
65 unsigned samplingFrequencyIndex : 4;
66 unsigned samplingFrequency : 24;
67 unsigned channelConfiguration : 4;
68 lsmash_mp4a_AudioObjectType extensionAudioObjectType;
69 unsigned extensionSamplingFrequencyIndex : 4;
70 unsigned extensionSamplingFrequency : 24;
71 unsigned extensionChannelConfiguration : 4;
72 /* if( audioObjectType in
73 #[ 1, 2, 3, 4, 6, 7, *17, *19, *20, *21, *22, *23 ] // GASpecificConfig, AAC relatives and TwinVQ, BSAC
74 [ 8 ] // CelpSpecificConfig, not supported
75 [ 9 ] // HvxcSpecificConfig, not supported
76 [ 12 ] // TTSSpecificConfig, not supported
77 [ 13, 14, 15, 16 ] // StructuredAudioSpecificConfig, notsupported
78 [ 24 ] // ErrorResilientCelpSpecificConfig, notsupported
79 [ 25 ] // ErrorResilientHvxcSpecificConfig, notsupported
80 [ 26, 27 ] // ParametricSpecificConfig, notsupported
81 [ 28 ] // SSCSpecificConfig, notsupported
82 #[ 32, 33, 34 ] // MPEG_1_2_SpecificConfig
83 [ 35 ] // DSTSpecificConfig, notsupported
84 ){ */
85 void *deepAudioSpecificConfig; // L-SMASH's original name, reperesents such as GASpecificConfig. */
86 /* } */
88 // error resilient stuff, not supported
89 if( audioObjectType in [17, 19, 20, 21, 22, 23, 24, 25, 26, 27] ){
90 uint8_t epConfig // 2bit
91 if( epConfig == 2 || epConfig == 3 ){
92 ErrorProtectionSpecificConfig();
94 if( epConfig == 3 ){
95 uint8_t directMapping; // 1bit, currently always 1.
96 if( !directMapping ){
97 // tbd
102 } mp4a_AudioSpecificConfig_t;
104 /* ISO/IEC 14496-3 Decoder configuration (GASpecificConfig), Syntax of GASpecificConfig() */
105 /* ISO/IEC 14496-3 GASpecificConfig(), Sampling frequency mapping */
106 typedef struct
108 unsigned frameLengthFlag : 1; /* FIXME: AAC_SSR: shall be 0, Others: depends, but noramally 0. */
109 unsigned dependsOnCoreCoder : 1; /* FIXME: used if scalable AAC. */
110 unsigned coreCoderDelay : 14;
111 unsigned extensionFlag : 1; /* 1bit, 1 if ErrorResilience */
112 /* if( !channelConfiguration ){ */
113 void* program_config_element; /* currently not supported. */
114 /* } */
116 // we do not support AAC_scalable
117 if( (audioObjectType == MP4A_AUDIO_OBJECT_TYPE_AAC_scalable) || (audioObjectType == MP4A_AUDIO_OBJECT_TYPE_ER_AAC_scalable) ){
118 uint8_t layerNr; // 3bits
122 // we do not support special AACs
123 if( extensionFlag ){
124 if( audioObjectType == MP4A_AUDIO_OBJECT_TYPE_ER_BSAC ){
125 uint8_t numOfSubFrame; // 5bits
126 uint8_t layer_length; // 11bits
128 if( audioObjectType == MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LC || audioObjectType == MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LTP
129 || audioObjectType == MP4A_AUDIO_OBJECT_TYPE_ER_AAC_scalable || audioObjectType == MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LD
131 uint8_t aacSectionDataResilienceFlag; // 1bit
132 uint8_t aacScalefactorDataResilienceFlag; // 1bit
133 uint8_t aacSpectralDataResilienceFlag; // 1bit
135 uint8_t extensionFlag3; // 1bit
136 if( extensionFlag3 ){
137 // tbd in version 3
141 } mp4a_GASpecificConfig_t;
143 /* ISO/IEC 14496-3 MPEG_1_2_SpecificConfig */
144 typedef struct
146 uint8_t extension; /* shall be 0. */
147 } mp4a_MPEG_1_2_SpecificConfig_t;
149 /* ISO/IEC 14496-3 ALSSpecificConfig */
150 typedef struct
152 uint32_t size;
153 uint8_t *data;
154 uint32_t als_id;
155 uint32_t samp_freq;
156 uint32_t samples;
157 uint16_t channels;
158 unsigned file_type : 3;
159 unsigned resolution : 3;
160 unsigned floating : 1;
161 unsigned msb_first : 1;
162 uint16_t frame_length;
163 uint8_t random_access;
164 unsigned ra_flag : 2;
165 unsigned adapt_order : 1;
166 unsigned coef_table : 2;
167 unsigned long_term_prediction : 1;
168 unsigned max_order : 10;
169 unsigned block_switching : 2;
170 unsigned bgmc_mode : 1;
171 unsigned sb_part : 1;
172 unsigned joint_stereo : 1;
173 unsigned mc_coding : 1;
174 unsigned chan_config : 1;
175 unsigned chan_sort : 1;
176 unsigned crc_enabled : 1;
177 unsigned RLSLMS : 1;
178 unsigned reserved : 5;
179 unsigned aux_data_enabled : 1;
180 } mp4a_ALSSpecificConfig_t;
182 static inline void mp4a_remove_GASpecificConfig( mp4a_GASpecificConfig_t* gasc )
184 debug_if( !gasc )
185 return;
186 lsmash_free( gasc->program_config_element );
187 lsmash_free( gasc );
190 static inline void mp4a_remove_MPEG_1_2_SpecificConfig( mp4a_MPEG_1_2_SpecificConfig_t* mpeg_1_2_sc )
192 lsmash_free( mpeg_1_2_sc );
195 void mp4a_remove_AudioSpecificConfig( mp4a_AudioSpecificConfig_t* asc )
197 if( !asc )
198 return;
199 switch( asc->audioObjectType ){
200 case MP4A_AUDIO_OBJECT_TYPE_AAC_MAIN:
201 case MP4A_AUDIO_OBJECT_TYPE_AAC_LC:
202 case MP4A_AUDIO_OBJECT_TYPE_AAC_SSR:
203 case MP4A_AUDIO_OBJECT_TYPE_AAC_LTP:
204 case MP4A_AUDIO_OBJECT_TYPE_SBR:
205 case MP4A_AUDIO_OBJECT_TYPE_AAC_scalable:
206 case MP4A_AUDIO_OBJECT_TYPE_TwinVQ:
207 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LC:
208 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LTP:
209 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_scalable:
210 case MP4A_AUDIO_OBJECT_TYPE_ER_Twin_VQ:
211 case MP4A_AUDIO_OBJECT_TYPE_ER_BSAC:
212 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LD:
213 mp4a_remove_GASpecificConfig( (mp4a_GASpecificConfig_t*)asc->deepAudioSpecificConfig );
214 break;
215 case MP4A_AUDIO_OBJECT_TYPE_Layer_1:
216 case MP4A_AUDIO_OBJECT_TYPE_Layer_2:
217 case MP4A_AUDIO_OBJECT_TYPE_Layer_3:
218 mp4a_remove_MPEG_1_2_SpecificConfig( (mp4a_MPEG_1_2_SpecificConfig_t*)asc->deepAudioSpecificConfig );
219 break;
220 default:
221 lsmash_free( asc->deepAudioSpecificConfig );
222 break;
224 lsmash_free( asc );
227 /* ADIF/PCE(program config element) style GASpecificConfig is not not supported. */
228 /* channelConfig/samplingFrequencyIndex will be used when we support ADIF/PCE style GASpecificConfig. */
229 static mp4a_GASpecificConfig_t* mp4a_create_GASpecificConfig( uint8_t samplingFrequencyIndex, uint8_t channelConfig, lsmash_mp4a_AudioObjectType aot )
231 debug_if( aot != MP4A_AUDIO_OBJECT_TYPE_AAC_MAIN && aot != MP4A_AUDIO_OBJECT_TYPE_AAC_LC
232 && aot != MP4A_AUDIO_OBJECT_TYPE_AAC_SSR && aot != MP4A_AUDIO_OBJECT_TYPE_AAC_LTP
233 && aot != MP4A_AUDIO_OBJECT_TYPE_TwinVQ )
234 return NULL;
235 if( samplingFrequencyIndex > 0xB || channelConfig == 0 || channelConfig == 7 )
236 return NULL;
237 mp4a_GASpecificConfig_t *gasc = (mp4a_GASpecificConfig_t *)lsmash_malloc_zero( sizeof(mp4a_GASpecificConfig_t) );
238 if( !gasc )
239 return NULL;
240 gasc->frameLengthFlag = 0; /* FIXME: AAC_SSR: shall be 0, Others: depends, but noramally 0. */
241 gasc->dependsOnCoreCoder = 0; /* FIXME: used if scalable AAC. */
242 switch( aot ){
243 case MP4A_AUDIO_OBJECT_TYPE_AAC_MAIN:
244 case MP4A_AUDIO_OBJECT_TYPE_AAC_LC:
245 case MP4A_AUDIO_OBJECT_TYPE_AAC_SSR:
246 case MP4A_AUDIO_OBJECT_TYPE_AAC_LTP:
247 case MP4A_AUDIO_OBJECT_TYPE_AAC_scalable:
248 case MP4A_AUDIO_OBJECT_TYPE_TwinVQ:
249 gasc->extensionFlag = 0;
250 break;
251 #if 0 /* intentional dead code */
252 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LC:
253 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LTP:
254 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_scalable:
255 case MP4A_AUDIO_OBJECT_TYPE_ER_Twin_VQ:
256 case MP4A_AUDIO_OBJECT_TYPE_ER_BSAC:
257 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LD:
258 gasc->extensionFlag = 1;
259 break;
260 #endif
261 default:
262 gasc->extensionFlag = 0;
263 break;
265 return gasc;
268 static mp4a_MPEG_1_2_SpecificConfig_t* mp4a_create_MPEG_1_2_SpecificConfig()
270 mp4a_MPEG_1_2_SpecificConfig_t *mpeg_1_2_sc = (mp4a_MPEG_1_2_SpecificConfig_t *)lsmash_malloc_zero( sizeof(mp4a_MPEG_1_2_SpecificConfig_t) );
271 if( !mpeg_1_2_sc )
272 return NULL;
273 mpeg_1_2_sc->extension = 0; /* shall be 0. */
274 return mpeg_1_2_sc;
277 static mp4a_ALSSpecificConfig_t *mp4a_create_ALSSpecificConfig( uint8_t *exdata, uint32_t exdata_length )
279 mp4a_ALSSpecificConfig_t *alssc = (mp4a_ALSSpecificConfig_t *)lsmash_malloc_zero( sizeof(mp4a_ALSSpecificConfig_t) );
280 if( !alssc )
281 return NULL;
282 alssc->data = lsmash_memdup( exdata, exdata_length );
283 if( !alssc->data )
285 lsmash_free( alssc );
286 return NULL;
288 alssc->size = exdata_length;
289 return alssc;
292 /* Currently, only normal AAC, MPEG_1_2 are supported.
293 For AAC, other than normal AAC, such as AAC_scalable, ER_AAC_xxx, are not supported.
294 ADIF/PCE(program config element) style AudioSpecificConfig is not supported.
295 aot shall not be MP4A_AUDIO_OBJECT_TYPE_SBR even if you wish to signal SBR explicitly, use sbr_mode instead.
296 Frequency/channels shall be base AAC's one, even if SBR/PS.
297 If other than AAC with SBR, sbr_mode shall be MP4A_AAC_SBR_NOT_SPECIFIED. */
298 mp4a_AudioSpecificConfig_t *mp4a_create_AudioSpecificConfig(
299 lsmash_mp4a_AudioObjectType aot,
300 uint32_t frequency,
301 uint32_t channels,
302 lsmash_mp4a_aac_sbr_mode sbr_mode,
303 uint8_t *exdata,
304 uint32_t exdata_length
307 if( aot != MP4A_AUDIO_OBJECT_TYPE_AAC_MAIN && aot != MP4A_AUDIO_OBJECT_TYPE_AAC_LC
308 && aot != MP4A_AUDIO_OBJECT_TYPE_AAC_SSR && aot != MP4A_AUDIO_OBJECT_TYPE_AAC_LTP
309 && aot != MP4A_AUDIO_OBJECT_TYPE_TwinVQ && aot != MP4A_AUDIO_OBJECT_TYPE_Layer_1
310 && aot != MP4A_AUDIO_OBJECT_TYPE_Layer_2 && aot != MP4A_AUDIO_OBJECT_TYPE_Layer_3
311 && aot != MP4A_AUDIO_OBJECT_TYPE_ALS )
312 return NULL;
313 if( frequency == 0 )
314 return NULL;
316 uint8_t channelConfig;
317 switch( channels ){
318 case 1:
319 case 2:
320 case 3:
321 case 4:
322 case 5:
323 case 6:
324 channelConfig = channels;
325 break;
326 case 8:
327 channelConfig = 7;
328 break;
329 default:
330 return NULL;
333 mp4a_AudioSpecificConfig_t *asc = (mp4a_AudioSpecificConfig_t *)lsmash_malloc_zero( sizeof(mp4a_AudioSpecificConfig_t) );
334 if( !asc )
335 return NULL;
337 asc->sbr_mode = sbr_mode;
338 asc->audioObjectType = aot;
339 asc->channelConfiguration = channelConfig;
341 uint8_t samplingFrequencyIndex = 0xF;
342 uint8_t i = 0x0;
343 if( sbr_mode != MP4A_AAC_SBR_NOT_SPECIFIED
344 || aot == MP4A_AUDIO_OBJECT_TYPE_AAC_MAIN
345 || aot == MP4A_AUDIO_OBJECT_TYPE_AAC_LC
346 || aot == MP4A_AUDIO_OBJECT_TYPE_AAC_SSR
347 || aot == MP4A_AUDIO_OBJECT_TYPE_AAC_LTP
348 || aot == MP4A_AUDIO_OBJECT_TYPE_SBR )
350 while( frequency < mp4a_sampling_frequency_table[i][0] )
351 i++;
352 asc->samplingFrequencyIndex = frequency == mp4a_sampling_frequency_table[i][1] ? i : 0xF;
353 asc->samplingFrequency = frequency;
354 samplingFrequencyIndex = mp4a_sampling_frequency_table[i][2];
355 /* SBR settings */
356 if( sbr_mode != MP4A_AAC_SBR_NOT_SPECIFIED )
358 /* SBR limitation */
359 /* see ISO/IEC 14496-3 Levels within the profiles / Levels for the High Efficiency AAC Profile */
360 if( i < 0x3 )
362 lsmash_free( asc );
363 return NULL;
365 asc->extensionAudioObjectType = MP4A_AUDIO_OBJECT_TYPE_SBR;
367 else
368 asc->extensionAudioObjectType = MP4A_AUDIO_OBJECT_TYPE_NULL;
370 if( sbr_mode == MP4A_AAC_SBR_BACKWARD_COMPATIBLE || sbr_mode == MP4A_AAC_SBR_HIERARCHICAL )
372 asc->extensionSamplingFrequency = frequency * 2;
373 asc->extensionSamplingFrequencyIndex = i == 0xC ? 0xF : mp4a_sampling_frequency_table[i][3];
375 else
377 asc->extensionSamplingFrequencyIndex = asc->samplingFrequencyIndex;
378 asc->extensionSamplingFrequency = asc->samplingFrequency;
381 else
383 while( i < 0xD && frequency != mp4a_sampling_frequency_table[i][1] )
384 i++;
385 asc->samplingFrequencyIndex = i != 0xD ? i : 0xF;
386 asc->samplingFrequency = frequency;
387 asc->extensionAudioObjectType = MP4A_AUDIO_OBJECT_TYPE_NULL;
388 asc->extensionSamplingFrequencyIndex = asc->samplingFrequencyIndex;
389 asc->extensionSamplingFrequency = asc->samplingFrequency;
392 switch( aot )
394 case MP4A_AUDIO_OBJECT_TYPE_AAC_MAIN:
395 case MP4A_AUDIO_OBJECT_TYPE_AAC_LC:
396 case MP4A_AUDIO_OBJECT_TYPE_AAC_SSR:
397 case MP4A_AUDIO_OBJECT_TYPE_AAC_LTP:
398 case MP4A_AUDIO_OBJECT_TYPE_SBR:
399 #if 0 /* FIXME: here, stop currently unsupported codecs. */
400 case MP4A_AUDIO_OBJECT_TYPE_AAC_scalable:
401 case MP4A_AUDIO_OBJECT_TYPE_TwinVQ: /* NOTE: I think we already have a support for TwinVQ, but how to test this? */
402 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LC:
403 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LTP:
404 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_scalable:
405 case MP4A_AUDIO_OBJECT_TYPE_ER_Twin_VQ:
406 case MP4A_AUDIO_OBJECT_TYPE_ER_BSAC:
407 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LD:
408 #endif
409 asc->deepAudioSpecificConfig = mp4a_create_GASpecificConfig( samplingFrequencyIndex, channelConfig, aot );
410 break;
411 case MP4A_AUDIO_OBJECT_TYPE_Layer_1:
412 case MP4A_AUDIO_OBJECT_TYPE_Layer_2:
413 case MP4A_AUDIO_OBJECT_TYPE_Layer_3:
414 asc->deepAudioSpecificConfig = mp4a_create_MPEG_1_2_SpecificConfig();
415 break;
416 case MP4A_AUDIO_OBJECT_TYPE_ALS:
417 asc->deepAudioSpecificConfig = mp4a_create_ALSSpecificConfig( exdata, exdata_length );
418 break;
419 default:
420 break; /* this case is trapped below. */
422 if( !asc->deepAudioSpecificConfig )
424 lsmash_free( asc );
425 return NULL;
427 return asc;
430 /* ADIF/PCE(program config element) style GASpecificConfig is not supported. */
431 static void mp4a_put_GASpecificConfig( lsmash_bits_t* bits, mp4a_GASpecificConfig_t* gasc )
433 debug_if( !bits || !gasc )
434 return;
435 lsmash_bits_put( bits, 1, gasc->frameLengthFlag );
436 lsmash_bits_put( bits, 1, gasc->dependsOnCoreCoder );
437 lsmash_bits_put( bits, 1, gasc->extensionFlag );
440 static void mp4a_put_MPEG_1_2_SpecificConfig( lsmash_bits_t* bits, mp4a_MPEG_1_2_SpecificConfig_t* mpeg_1_2_sc )
442 debug_if( !bits || !mpeg_1_2_sc )
443 return;
444 lsmash_bits_put( bits, 1, mpeg_1_2_sc->extension ); /* shall be 0 */
447 static void mp4a_put_ALSSpecificConfig( lsmash_bits_t *bits, mp4a_ALSSpecificConfig_t *alssc )
449 debug_if( !bits || !alssc )
450 return;
451 lsmash_bits_import_data( bits, alssc->data, alssc->size );
454 static inline void mp4a_put_AudioObjectType( lsmash_bits_t* bits, lsmash_mp4a_AudioObjectType aot )
456 if( aot > MP4A_AUDIO_OBJECT_TYPE_ESCAPE )
458 lsmash_bits_put( bits, 5, MP4A_AUDIO_OBJECT_TYPE_ESCAPE );
459 lsmash_bits_put( bits, 6, aot - MP4A_AUDIO_OBJECT_TYPE_ESCAPE - 1 );
461 else
462 lsmash_bits_put( bits, 5, aot );
465 static inline void mp4a_put_SamplingFrequencyIndex( lsmash_bits_t* bits, uint8_t samplingFrequencyIndex, uint32_t samplingFrequency )
467 lsmash_bits_put( bits, 4, samplingFrequencyIndex );
468 if( samplingFrequencyIndex == 0xF )
469 lsmash_bits_put( bits, 24, samplingFrequency );
472 /* Currently, only normal AAC, MPEG_1_2 are supported.
473 For AAC, other than normal AAC, such as AAC_scalable, ER_AAC_xxx, are not supported.
474 ADIF/PCE(program config element) style AudioSpecificConfig is not supported either. */
475 void mp4a_put_AudioSpecificConfig( lsmash_bs_t* bs, mp4a_AudioSpecificConfig_t* asc )
477 debug_if( !bs || !asc )
478 return;
479 lsmash_bits_t bits;
480 lsmash_bits_init( &bits, bs );
482 if( asc->sbr_mode == MP4A_AAC_SBR_HIERARCHICAL )
483 mp4a_put_AudioObjectType( &bits, asc->extensionAudioObjectType ); /* puts MP4A_AUDIO_OBJECT_TYPE_SBR */
484 else
485 mp4a_put_AudioObjectType( &bits, asc->audioObjectType );
486 mp4a_put_SamplingFrequencyIndex( &bits, asc->samplingFrequencyIndex, asc->samplingFrequency );
487 lsmash_bits_put( &bits, 4, asc->channelConfiguration );
488 if( asc->sbr_mode == MP4A_AAC_SBR_HIERARCHICAL )
490 mp4a_put_SamplingFrequencyIndex( &bits, asc->extensionSamplingFrequencyIndex, asc->extensionSamplingFrequency );
491 mp4a_put_AudioObjectType( &bits, asc->audioObjectType );
493 switch( asc->audioObjectType ){
494 case MP4A_AUDIO_OBJECT_TYPE_AAC_MAIN:
495 case MP4A_AUDIO_OBJECT_TYPE_AAC_LC:
496 case MP4A_AUDIO_OBJECT_TYPE_AAC_SSR:
497 case MP4A_AUDIO_OBJECT_TYPE_AAC_LTP:
498 case MP4A_AUDIO_OBJECT_TYPE_SBR:
499 #if 0 /* FIXME: here, stop currently unsupported codecs */
500 case MP4A_AUDIO_OBJECT_TYPE_AAC_scalable:
501 case MP4A_AUDIO_OBJECT_TYPE_TwinVQ: /* NOTE: I think we already have a support for TwinVQ, but how to test this? */
502 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LC:
503 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LTP:
504 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_scalable:
505 case MP4A_AUDIO_OBJECT_TYPE_ER_Twin_VQ:
506 case MP4A_AUDIO_OBJECT_TYPE_ER_BSAC:
507 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LD:
508 #endif
509 mp4a_put_GASpecificConfig( &bits, (mp4a_GASpecificConfig_t*)asc->deepAudioSpecificConfig );
510 break;
511 case MP4A_AUDIO_OBJECT_TYPE_Layer_1:
512 case MP4A_AUDIO_OBJECT_TYPE_Layer_2:
513 case MP4A_AUDIO_OBJECT_TYPE_Layer_3:
514 mp4a_put_MPEG_1_2_SpecificConfig( &bits, (mp4a_MPEG_1_2_SpecificConfig_t*)asc->deepAudioSpecificConfig );
515 break;
516 case MP4A_AUDIO_OBJECT_TYPE_ALS:
517 lsmash_bits_put( &bits, 5, 0 ); /* fillBits for byte alignment */
518 mp4a_put_ALSSpecificConfig( &bits, (mp4a_ALSSpecificConfig_t *)asc->deepAudioSpecificConfig );
519 break;
520 default:
521 break; /* FIXME: do we have to return error? */
524 /* FIXME: Error Resiliant stuff omitted here. */
526 if( asc->sbr_mode == MP4A_AAC_SBR_BACKWARD_COMPATIBLE || asc->sbr_mode == MP4A_AAC_SBR_NONE )
528 lsmash_bits_put( &bits, 11, 0x2b7 );
529 mp4a_put_AudioObjectType( &bits, asc->extensionAudioObjectType ); /* puts MP4A_AUDIO_OBJECT_TYPE_SBR */
530 if( asc->extensionAudioObjectType == MP4A_AUDIO_OBJECT_TYPE_SBR ) /* this is always true, due to current spec */
532 /* sbrPresentFlag */
533 if( asc->sbr_mode == MP4A_AAC_SBR_NONE )
534 lsmash_bits_put( &bits, 1, 0x0 );
535 else
537 lsmash_bits_put( &bits, 1, 0x1 );
538 mp4a_put_SamplingFrequencyIndex( &bits, asc->extensionSamplingFrequencyIndex, asc->extensionSamplingFrequency );
542 lsmash_bits_put_align( &bits );
545 static int mp4a_get_GASpecificConfig( lsmash_bits_t *bits, mp4a_AudioSpecificConfig_t *asc )
547 mp4a_GASpecificConfig_t *gasc = (mp4a_GASpecificConfig_t *)lsmash_malloc_zero( sizeof(mp4a_GASpecificConfig_t) );
548 if( !gasc )
549 return LSMASH_ERR_MEMORY_ALLOC;
550 asc->deepAudioSpecificConfig = gasc;
551 gasc->frameLengthFlag = lsmash_bits_get( bits, 1 );
552 gasc->dependsOnCoreCoder = lsmash_bits_get( bits, 1 );
553 if( gasc->dependsOnCoreCoder )
554 gasc->coreCoderDelay = lsmash_bits_get( bits, 14 );
555 gasc->extensionFlag = lsmash_bits_get( bits, 1 );
556 return 0;
559 static int mp4a_get_MPEG_1_2_SpecificConfig( lsmash_bits_t *bits, mp4a_AudioSpecificConfig_t *asc )
561 mp4a_MPEG_1_2_SpecificConfig_t *mpeg_1_2_sc = (mp4a_MPEG_1_2_SpecificConfig_t *)lsmash_malloc_zero( sizeof(mp4a_MPEG_1_2_SpecificConfig_t) );
562 if( !mpeg_1_2_sc )
563 return LSMASH_ERR_MEMORY_ALLOC;
564 asc->deepAudioSpecificConfig = mpeg_1_2_sc;
565 mpeg_1_2_sc->extension = lsmash_bits_get( bits, 1 );
566 return 0;
569 static int mp4a_get_ALSSpecificConfig( lsmash_bits_t *bits, mp4a_AudioSpecificConfig_t *asc )
571 mp4a_ALSSpecificConfig_t *alssc = (mp4a_ALSSpecificConfig_t *)lsmash_malloc_zero( sizeof(mp4a_ALSSpecificConfig_t) );
572 if( !alssc )
573 return LSMASH_ERR_MEMORY_ALLOC;
574 asc->deepAudioSpecificConfig = alssc;
575 alssc->als_id = lsmash_bits_get( bits, 32 );
576 alssc->samp_freq = lsmash_bits_get( bits, 32 );
577 alssc->samples = lsmash_bits_get( bits, 32 );
578 alssc->channels = lsmash_bits_get( bits, 16 );
579 alssc->file_type = lsmash_bits_get( bits, 3 );
580 alssc->resolution = lsmash_bits_get( bits, 3 );
581 alssc->floating = lsmash_bits_get( bits, 1 );
582 alssc->msb_first = lsmash_bits_get( bits, 1 );
583 alssc->frame_length = lsmash_bits_get( bits, 16 );
584 alssc->random_access = lsmash_bits_get( bits, 8 );
585 alssc->ra_flag = lsmash_bits_get( bits, 2 );
586 alssc->adapt_order = lsmash_bits_get( bits, 1 );
587 alssc->coef_table = lsmash_bits_get( bits, 2 );
588 alssc->long_term_prediction = lsmash_bits_get( bits, 1 );
589 alssc->max_order = lsmash_bits_get( bits, 10 );
590 alssc->block_switching = lsmash_bits_get( bits, 2 );
591 alssc->bgmc_mode = lsmash_bits_get( bits, 1 );
592 alssc->sb_part = lsmash_bits_get( bits, 1 );
593 alssc->joint_stereo = lsmash_bits_get( bits, 1 );
594 alssc->mc_coding = lsmash_bits_get( bits, 1 );
595 alssc->chan_config = lsmash_bits_get( bits, 1 );
596 alssc->chan_sort = lsmash_bits_get( bits, 1 );
597 alssc->crc_enabled = lsmash_bits_get( bits, 1 );
598 alssc->RLSLMS = lsmash_bits_get( bits, 1 );
599 alssc->reserved = lsmash_bits_get( bits, 5 );
600 alssc->aux_data_enabled = lsmash_bits_get( bits, 1 );
601 return 0;
604 static mp4a_AudioSpecificConfig_t *mp4a_get_AudioSpecificConfig( uint8_t *dsi_payload, uint32_t dsi_payload_length )
606 lsmash_bits_t *bits = lsmash_bits_adhoc_create();
607 if( !bits )
608 return NULL;
609 if( lsmash_bits_import_data( bits, dsi_payload, dsi_payload_length ) < 0 )
611 lsmash_bits_adhoc_cleanup( bits );
612 return NULL;
614 mp4a_AudioSpecificConfig_t *asc = (mp4a_AudioSpecificConfig_t *)lsmash_malloc_zero( sizeof(mp4a_AudioSpecificConfig_t) );
615 if( !asc )
616 goto fail;
617 asc->audioObjectType = lsmash_bits_get( bits, 5 );
618 if( asc->audioObjectType == 31 )
619 asc->extensionAudioObjectType = asc->audioObjectType += 1 + lsmash_bits_get( bits, 6 );
620 asc->samplingFrequencyIndex = lsmash_bits_get( bits, 4 );
621 if( asc->samplingFrequencyIndex == 0xf )
622 asc->samplingFrequency = lsmash_bits_get( bits, 24 );
623 asc->channelConfiguration = lsmash_bits_get( bits, 4 );
624 int ret = 0;
625 switch( asc->audioObjectType )
627 case MP4A_AUDIO_OBJECT_TYPE_AAC_MAIN :
628 case MP4A_AUDIO_OBJECT_TYPE_AAC_LC :
629 case MP4A_AUDIO_OBJECT_TYPE_AAC_SSR :
630 case MP4A_AUDIO_OBJECT_TYPE_AAC_LTP :
631 case MP4A_AUDIO_OBJECT_TYPE_AAC_scalable :
632 case MP4A_AUDIO_OBJECT_TYPE_TwinVQ :
633 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LC :
634 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LTP :
635 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_scalable :
636 case MP4A_AUDIO_OBJECT_TYPE_ER_Twin_VQ :
637 case MP4A_AUDIO_OBJECT_TYPE_ER_BSAC :
638 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LD :
639 ret = mp4a_get_GASpecificConfig( bits, asc );
640 break;
641 case MP4A_AUDIO_OBJECT_TYPE_Layer_1 :
642 case MP4A_AUDIO_OBJECT_TYPE_Layer_2 :
643 case MP4A_AUDIO_OBJECT_TYPE_Layer_3 :
644 ret = mp4a_get_MPEG_1_2_SpecificConfig( bits, asc );
645 break;
646 case MP4A_AUDIO_OBJECT_TYPE_ALS :
647 lsmash_bits_get( bits, 5 );
648 ret = mp4a_get_ALSSpecificConfig( bits, asc );
649 break;
650 default :
651 break;
653 if( ret < 0 )
654 goto fail;
655 lsmash_bits_adhoc_cleanup( bits );
656 return asc;
657 fail:
658 lsmash_bits_adhoc_cleanup( bits );
659 lsmash_free( asc );
660 return NULL;
663 int mp4a_setup_summary_from_AudioSpecificConfig( lsmash_audio_summary_t *summary, uint8_t *dsi_payload, uint32_t dsi_payload_length )
665 mp4a_AudioSpecificConfig_t *asc = mp4a_get_AudioSpecificConfig( dsi_payload, dsi_payload_length );
666 if( !asc )
667 return LSMASH_ERR_NAMELESS;
668 summary->summary_type = LSMASH_SUMMARY_TYPE_AUDIO;
669 summary->sample_type = ISOM_CODEC_TYPE_MP4A_AUDIO;
670 summary->aot = asc->audioObjectType;
671 switch( asc->audioObjectType )
673 case MP4A_AUDIO_OBJECT_TYPE_AAC_MAIN :
674 case MP4A_AUDIO_OBJECT_TYPE_AAC_LC :
675 case MP4A_AUDIO_OBJECT_TYPE_AAC_SSR :
676 case MP4A_AUDIO_OBJECT_TYPE_AAC_LTP :
677 case MP4A_AUDIO_OBJECT_TYPE_AAC_scalable :
678 case MP4A_AUDIO_OBJECT_TYPE_TwinVQ :
679 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LC :
680 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LTP :
681 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_scalable :
682 case MP4A_AUDIO_OBJECT_TYPE_ER_Twin_VQ :
683 case MP4A_AUDIO_OBJECT_TYPE_ER_BSAC :
684 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LD :
685 case MP4A_AUDIO_OBJECT_TYPE_Layer_1 :
686 case MP4A_AUDIO_OBJECT_TYPE_Layer_2 :
687 case MP4A_AUDIO_OBJECT_TYPE_Layer_3 :
688 if( asc->samplingFrequencyIndex == 0xf )
689 summary->frequency = asc->samplingFrequency;
690 else
692 uint8_t i = 0x0;
693 while( i != 0xc )
695 if( mp4a_sampling_frequency_table[i][2] == asc->samplingFrequencyIndex )
697 summary->frequency = mp4a_sampling_frequency_table[i][1];
698 break;
700 ++i;
702 if( i == 0xc )
704 mp4a_remove_AudioSpecificConfig( asc );
705 return LSMASH_ERR_INVALID_DATA;
708 if( asc->channelConfiguration < 8 )
709 summary->channels = asc->channelConfiguration != 7 ? asc->channelConfiguration : 8;
710 else
711 summary->channels = 0; /* reserved */
712 summary->sample_size = 16;
713 switch( asc->audioObjectType )
715 case MP4A_AUDIO_OBJECT_TYPE_AAC_SSR :
716 summary->samples_in_frame = 1024;
717 break;
718 case MP4A_AUDIO_OBJECT_TYPE_Layer_1 :
719 summary->samples_in_frame = 384;
720 break;
721 case MP4A_AUDIO_OBJECT_TYPE_Layer_2 :
722 case MP4A_AUDIO_OBJECT_TYPE_Layer_3 :
723 summary->samples_in_frame = 1152;
724 break;
725 default :
726 summary->samples_in_frame = !((mp4a_GASpecificConfig_t *)asc->deepAudioSpecificConfig)->frameLengthFlag ? 1024 : 960;
727 break;
729 break;
730 case MP4A_AUDIO_OBJECT_TYPE_ALS :
732 mp4a_ALSSpecificConfig_t *alssc = (mp4a_ALSSpecificConfig_t *)asc->deepAudioSpecificConfig;
733 summary->frequency = alssc->samp_freq;
734 summary->channels = alssc->channels + 1;
735 summary->sample_size = (alssc->resolution + 1) * 8;
736 summary->samples_in_frame = alssc->frame_length + 1;
737 break;
739 default :
740 break;
742 mp4a_remove_AudioSpecificConfig( asc );
743 return 0;
746 /* This function is very ad-hoc. */
747 uint8_t *mp4a_export_AudioSpecificConfig( lsmash_mp4a_AudioObjectType aot,
748 uint32_t frequency,
749 uint32_t channels,
750 lsmash_mp4a_aac_sbr_mode sbr_mode,
751 uint8_t *exdata,
752 uint32_t exdata_length,
753 uint32_t *data_length )
755 lsmash_bs_t *bs = lsmash_bs_create();
756 if( !bs )
757 return NULL;
758 mp4a_AudioSpecificConfig_t *asc = mp4a_create_AudioSpecificConfig( aot, frequency, channels, sbr_mode, exdata, exdata_length );
759 if( !asc )
761 lsmash_bs_cleanup( bs );
762 return NULL;
764 mp4a_put_AudioSpecificConfig( bs, asc );
765 uint8_t *data = lsmash_bs_export_data( bs, data_length );
766 mp4a_remove_AudioSpecificConfig( asc );
767 lsmash_bs_cleanup( bs );
768 if( !data )
769 return NULL;
770 return data;
773 static void mp4a_print_GASpecificConfig( FILE *fp, mp4a_AudioSpecificConfig_t *asc, int indent )
775 mp4a_GASpecificConfig_t *gasc = (mp4a_GASpecificConfig_t *)asc->deepAudioSpecificConfig;
776 lsmash_ifprintf( fp, indent++, "[GASpecificConfig]\n" );
777 lsmash_ifprintf( fp, indent, "frameLengthFlag = %"PRIu8"\n", gasc->frameLengthFlag );
778 lsmash_ifprintf( fp, indent, "dependsOnCoreCoder = %"PRIu8"\n", gasc->dependsOnCoreCoder );
779 if( gasc->dependsOnCoreCoder )
780 lsmash_ifprintf( fp, indent, "coreCoderDelay = %"PRIu16"\n", gasc->coreCoderDelay );
781 lsmash_ifprintf( fp, indent, "extensionFlag = %"PRIu8"\n", gasc->extensionFlag );
782 if( !asc->channelConfiguration )
783 lsmash_ifprintf( fp, indent, "program_config_element()\n" );
786 static void mp4a_print_MPEG_1_2_SpecificConfig( FILE *fp, mp4a_AudioSpecificConfig_t *asc, int indent )
788 mp4a_MPEG_1_2_SpecificConfig_t *mpeg_1_2_sc = (mp4a_MPEG_1_2_SpecificConfig_t *)asc->deepAudioSpecificConfig;
789 lsmash_ifprintf( fp, indent++, "[MPEG_1_2_SpecificConfig]\n" );
790 lsmash_ifprintf( fp, indent, "extension = %"PRIu8"\n", mpeg_1_2_sc->extension );
793 static void mp4a_print_ALSSpecificConfig( FILE *fp, mp4a_AudioSpecificConfig_t *asc, int indent )
795 mp4a_ALSSpecificConfig_t *alssc = (mp4a_ALSSpecificConfig_t *)asc->deepAudioSpecificConfig;
796 const char *file_type [4] = { "raw", "wave", "aiff", "bwf" };
797 const char *floating [2] = { "integer", "IEEE 32-bit floating-point" };
798 const char *endian [2] = { "little", "big" };
799 const char *ra_flag [4] = { "not stored", "stored at the beginning of frame_data()", "stored at the end of ALSSpecificConfig", "?" };
800 lsmash_ifprintf( fp, indent++, "[ALSSpecificConfig]\n" );
801 lsmash_ifprintf( fp, indent, "als_id = 0x%"PRIx32"\n", alssc->als_id );
802 lsmash_ifprintf( fp, indent, "samp_freq = %"PRIu32" Hz\n", alssc->samp_freq );
803 lsmash_ifprintf( fp, indent, "samples = %"PRIu32"\n", alssc->samples );
804 lsmash_ifprintf( fp, indent, "channels = %"PRIu16"\n", alssc->channels );
805 if( alssc->file_type <= 3 )
806 lsmash_ifprintf( fp, indent, "file_type = %"PRIu8" (%s file)\n", alssc->file_type, file_type[ alssc->file_type ] );
807 else
808 lsmash_ifprintf( fp, indent, "file_type = %"PRIu8"\n", alssc->file_type );
809 if( alssc->resolution <= 3 )
810 lsmash_ifprintf( fp, indent, "resolution = %"PRIu8" (%d-bit)\n", alssc->resolution, 8 * (1 + alssc->resolution) );
811 else
812 lsmash_ifprintf( fp, indent, "resolution = %"PRIu8"\n", alssc->resolution );
813 lsmash_ifprintf( fp, indent, "floating = %"PRIu8" (%s)\n", alssc->floating, floating[ alssc->floating ] );
814 if( alssc->resolution )
815 lsmash_ifprintf( fp, indent, "msb_first = %"PRIu8" (%s-endian)\n", alssc->msb_first, endian[ alssc->msb_first ] );
816 else
817 lsmash_ifprintf( fp, indent, "msb_first = %"PRIu8" (%ssigned data)\n", alssc->msb_first, ((const char *[2]){ "un", "" })[ alssc->msb_first ] );
818 lsmash_ifprintf( fp, indent, "frame_length = %"PRIu16"\n", alssc->frame_length );
819 lsmash_ifprintf( fp, indent, "random_access = %"PRIu8"\n", alssc->random_access );
820 lsmash_ifprintf( fp, indent, "ra_flag = %"PRIu8" (ra_unit_size is %s)\n", alssc->ra_flag, ra_flag[ alssc->ra_flag ] );
821 lsmash_ifprintf( fp, indent, "adapt_order = %"PRIu8"\n", alssc->adapt_order );
822 lsmash_ifprintf( fp, indent, "coef_table = %"PRIu8"\n", alssc->coef_table );
823 lsmash_ifprintf( fp, indent, "long_term_prediction = %"PRIu8"\n", alssc->long_term_prediction );
824 lsmash_ifprintf( fp, indent, "max_order = %"PRIu8"\n", alssc->max_order );
825 lsmash_ifprintf( fp, indent, "block_switching = %"PRIu8"\n", alssc->block_switching );
826 lsmash_ifprintf( fp, indent, "bgmc_mode = %"PRIu8"\n", alssc->bgmc_mode );
827 lsmash_ifprintf( fp, indent, "sb_part = %"PRIu8"\n", alssc->sb_part );
828 lsmash_ifprintf( fp, indent, "joint_stereo = %"PRIu8"\n", alssc->joint_stereo );
829 lsmash_ifprintf( fp, indent, "mc_coding = %"PRIu8"\n", alssc->mc_coding );
830 lsmash_ifprintf( fp, indent, "chan_config = %"PRIu8"\n", alssc->chan_config );
831 lsmash_ifprintf( fp, indent, "chan_sort = %"PRIu8"\n", alssc->chan_sort );
832 lsmash_ifprintf( fp, indent, "crc_enabled = %"PRIu8"\n", alssc->crc_enabled );
833 lsmash_ifprintf( fp, indent, "RLSLMS = %"PRIu8"\n", alssc->RLSLMS );
834 lsmash_ifprintf( fp, indent, "reserved = %"PRIu8"\n", alssc->reserved );
835 lsmash_ifprintf( fp, indent, "aux_data_enabled = %"PRIu8"\n", alssc->aux_data_enabled );
838 void mp4a_print_AudioSpecificConfig( FILE *fp, uint8_t *dsi_payload, uint32_t dsi_payload_length, int indent )
840 assert( fp && dsi_payload && dsi_payload_length );
841 mp4a_AudioSpecificConfig_t *asc = mp4a_get_AudioSpecificConfig( dsi_payload, dsi_payload_length );
842 if( !asc )
843 return;
844 const char *audio_object_type[] =
846 "NULL",
847 "AAC MAIN",
848 "AAC LC (Low Complexity)",
849 "AAC SSR (Scalable Sample Rate)",
850 "AAC LTP (Long Term Prediction)",
851 "SBR (Spectral Band Replication)",
852 "AAC scalable",
853 "TwinVQ",
854 "CELP (Code Excited Linear Prediction)",
855 "HVXC (Harmonic Vector Excitation Coding)",
856 "reserved",
857 "reserved",
858 "TTSI (Text-To-Speech Interface)",
859 "Main synthetic",
860 "Wavetable synthesis",
861 "General MIDI",
862 "Algorithmic Synthesis and Audio FX",
863 "ER AAC LC",
864 "reserved",
865 "ER AAC LTP",
866 "ER AAC scalable",
867 "ER Twin VQ",
868 "ER BSAC (Bit-Sliced Arithmetic Coding)",
869 "ER AAC LD",
870 "ER CELP",
871 "ER HVXC",
872 "ER HILN (Harmonic and Individual Lines plus Noise)",
873 "ER Parametric",
874 "SSC (SinuSoidal Coding)",
875 "PS (Parametric Stereo)",
876 "MPEG Surround",
877 "escape",
878 "Layer-1",
879 "Layer-2",
880 "Layer-3",
881 "DST (Direct Stream Transfer)",
882 "ALS (Audio Lossless Coding)",
883 "SLS (Scalable Lossless Coding)",
884 "SLS non-core",
885 "ER AAC ELD",
886 "SMR Simple",
887 "SMR Main",
888 "USAC (Unified Speech and Audio Coding)",
889 "SAOC",
890 "LD MPEG Surround",
891 "SAOC-DE"
893 lsmash_ifprintf( fp, indent++, "[AudioSpecificConfig]\n" );
894 if( asc->audioObjectType < sizeof(audio_object_type) / sizeof(audio_object_type[0]) )
895 lsmash_ifprintf( fp, indent, "audioObjectType = %d (%s)\n", asc->audioObjectType, audio_object_type[ asc->audioObjectType ] );
896 else
897 lsmash_ifprintf( fp, indent, "audioObjectType = %d\n", asc->audioObjectType );
898 lsmash_ifprintf( fp, indent, "samplingFrequencyIndex = %"PRIu8"\n", asc->samplingFrequencyIndex );
899 if( asc->samplingFrequencyIndex == 0xf )
900 lsmash_ifprintf( fp, indent, "samplingFrequency = %"PRIu32"\n", asc->samplingFrequency );
901 lsmash_ifprintf( fp, indent, "channelConfiguration = %"PRIu8"\n", asc->channelConfiguration );
902 if( asc->extensionAudioObjectType == 5 )
904 lsmash_ifprintf( fp, indent, "extensionSamplingFrequencyIndex = %"PRIu8"\n", asc->extensionSamplingFrequencyIndex );
905 if( asc->extensionSamplingFrequencyIndex == 0xf )
906 lsmash_ifprintf( fp, indent, "extensionSamplingFrequency = %"PRIu32"\n", asc->extensionSamplingFrequency );
907 if( asc->audioObjectType == 22 )
908 lsmash_ifprintf( fp, indent, "extensionChannelConfiguration = %"PRIu8"\n", asc->extensionChannelConfiguration );
910 if( asc->deepAudioSpecificConfig )
911 switch( asc->audioObjectType )
913 case MP4A_AUDIO_OBJECT_TYPE_AAC_MAIN :
914 case MP4A_AUDIO_OBJECT_TYPE_AAC_LC :
915 case MP4A_AUDIO_OBJECT_TYPE_AAC_SSR :
916 case MP4A_AUDIO_OBJECT_TYPE_AAC_LTP :
917 case MP4A_AUDIO_OBJECT_TYPE_AAC_scalable :
918 case MP4A_AUDIO_OBJECT_TYPE_TwinVQ :
919 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LC :
920 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LTP :
921 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_scalable :
922 case MP4A_AUDIO_OBJECT_TYPE_ER_Twin_VQ :
923 case MP4A_AUDIO_OBJECT_TYPE_ER_BSAC :
924 case MP4A_AUDIO_OBJECT_TYPE_ER_AAC_LD :
925 mp4a_print_GASpecificConfig( fp, asc, indent );
926 break;
927 case MP4A_AUDIO_OBJECT_TYPE_Layer_1 :
928 case MP4A_AUDIO_OBJECT_TYPE_Layer_2 :
929 case MP4A_AUDIO_OBJECT_TYPE_Layer_3 :
930 mp4a_print_MPEG_1_2_SpecificConfig( fp, asc, indent );
931 break;
932 case MP4A_AUDIO_OBJECT_TYPE_ALS :
933 mp4a_print_ALSSpecificConfig( fp, asc, indent );
934 break;
935 default :
936 break;
938 mp4a_remove_AudioSpecificConfig( asc );
941 /***************************************************************************
942 audioProfileLevelIndication
943 ***************************************************************************/
944 /* NOTE: This function is not strictly preferable, but accurate.
945 The spec of audioProfileLevelIndication is too much complicated. */
946 mp4a_audioProfileLevelIndication mp4a_get_audioProfileLevelIndication( lsmash_audio_summary_t *summary )
948 if( !summary || summary->summary_type != LSMASH_SUMMARY_TYPE_AUDIO )
949 return MP4A_AUDIO_PLI_NONE_REQUIRED; /* means error. */
950 if( lsmash_mp4sys_get_object_type_indication( (lsmash_summary_t *)summary ) != MP4SYS_OBJECT_TYPE_Audio_ISO_14496_3 )
951 return MP4A_AUDIO_PLI_NOT_SPECIFIED; /* This is of audio stream, but not described in ISO/IEC 14496-3. */
952 if( summary->channels == 0 || summary->frequency == 0 )
953 return MP4A_AUDIO_PLI_NONE_REQUIRED; /* means error. */
954 mp4a_audioProfileLevelIndication pli = MP4A_AUDIO_PLI_NOT_SPECIFIED;
955 switch( summary->aot )
957 case MP4A_AUDIO_OBJECT_TYPE_AAC_LC:
958 if( summary->sbr_mode == MP4A_AAC_SBR_HIERARCHICAL )
960 /* NOTE: This is not strictly preferable, but accurate; just possibly over-estimated.
961 We do not expect to use MP4A_AAC_SBR_HIERARCHICAL mode without SBR, nor downsampled mode with SBR. */
962 if( summary->channels <= 2 && summary->frequency <= 24000 )
963 pli = MP4A_AUDIO_PLI_HE_AAC_L2;
964 else if( summary->channels <= 5 && summary->frequency <= 48000 )
965 pli = MP4A_AUDIO_PLI_HE_AAC_L5;
966 else
967 pli = MP4A_AUDIO_PLI_NOT_SPECIFIED;
968 break;
970 /* pretending plain AAC-LC, if actually HE-AAC. */
971 static const uint32_t mp4sys_aac_pli_table[5][3] = {
972 /* channels, frequency, audioProfileLevelIndication */
973 { 6, 96000, MP4A_AUDIO_PLI_AAC_L5 }, /* FIXME: 6ch is not strictly correct, but works in many case. */
974 { 6, 48000, MP4A_AUDIO_PLI_AAC_L4 }, /* FIXME: 6ch is not strictly correct, but works in many case. */
975 { 2, 48000, MP4A_AUDIO_PLI_AAC_L2 },
976 { 2, 24000, MP4A_AUDIO_PLI_AAC_L1 },
977 { 0, 0, MP4A_AUDIO_PLI_NOT_SPECIFIED }
979 for( int i = 0; summary->channels <= mp4sys_aac_pli_table[i][0] && summary->frequency <= mp4sys_aac_pli_table[i][1] ; i++ )
980 pli = mp4sys_aac_pli_table[i][2];
981 break;
982 case MP4A_AUDIO_OBJECT_TYPE_ALS:
983 /* FIXME: this is not stricly. Summary shall carry max_order, block_switching, bgmc_mode and RLSLMS. */
984 if( summary->channels <= 2 && summary->frequency <= 48000 && summary->sample_size <= 16 && summary->samples_in_frame <= 4096 )
985 pli = MP4A_AUDIO_PLI_ALS_Simple_L1;
986 else
987 pli = MP4A_AUDIO_PLI_NOT_SPECIFIED;
988 break;
989 case MP4A_AUDIO_OBJECT_TYPE_Layer_1:
990 case MP4A_AUDIO_OBJECT_TYPE_Layer_2:
991 case MP4A_AUDIO_OBJECT_TYPE_Layer_3:
992 pli = MP4A_AUDIO_PLI_NOT_SPECIFIED; /* 14496-3, Audio profiles and levels, does not allow any pli. */
993 break;
994 default:
995 pli = MP4A_AUDIO_PLI_NOT_SPECIFIED; /* something we don't know/support, or what the spec never covers. */
996 break;
998 return pli;
1001 static int mp4sys_is_same_profile( mp4a_audioProfileLevelIndication a, mp4a_audioProfileLevelIndication b )
1003 switch( a )
1005 case MP4A_AUDIO_PLI_Main_L1:
1006 case MP4A_AUDIO_PLI_Main_L2:
1007 case MP4A_AUDIO_PLI_Main_L3:
1008 case MP4A_AUDIO_PLI_Main_L4:
1009 if( MP4A_AUDIO_PLI_Main_L1 <= b && b <= MP4A_AUDIO_PLI_Main_L4 )
1010 return 1;
1011 return 0;
1012 break;
1013 case MP4A_AUDIO_PLI_Scalable_L1:
1014 case MP4A_AUDIO_PLI_Scalable_L2:
1015 case MP4A_AUDIO_PLI_Scalable_L3:
1016 case MP4A_AUDIO_PLI_Scalable_L4:
1017 if( MP4A_AUDIO_PLI_Scalable_L1 <= b && b <= MP4A_AUDIO_PLI_Scalable_L4 )
1018 return 1;
1019 return 0;
1020 break;
1021 case MP4A_AUDIO_PLI_Speech_L1:
1022 case MP4A_AUDIO_PLI_Speech_L2:
1023 if( MP4A_AUDIO_PLI_Speech_L1 <= b && b <= MP4A_AUDIO_PLI_Speech_L2 )
1024 return 1;
1025 return 0;
1026 break;
1027 case MP4A_AUDIO_PLI_Synthetic_L1:
1028 case MP4A_AUDIO_PLI_Synthetic_L2:
1029 case MP4A_AUDIO_PLI_Synthetic_L3:
1030 if( MP4A_AUDIO_PLI_Synthetic_L1 <= b && b <= MP4A_AUDIO_PLI_Synthetic_L3 )
1031 return 1;
1032 return 0;
1033 break;
1034 case MP4A_AUDIO_PLI_HighQuality_L1:
1035 case MP4A_AUDIO_PLI_HighQuality_L2:
1036 case MP4A_AUDIO_PLI_HighQuality_L3:
1037 case MP4A_AUDIO_PLI_HighQuality_L4:
1038 case MP4A_AUDIO_PLI_HighQuality_L5:
1039 case MP4A_AUDIO_PLI_HighQuality_L6:
1040 case MP4A_AUDIO_PLI_HighQuality_L7:
1041 case MP4A_AUDIO_PLI_HighQuality_L8:
1042 if( MP4A_AUDIO_PLI_HighQuality_L1 <= b && b <= MP4A_AUDIO_PLI_HighQuality_L8 )
1043 return 1;
1044 return 0;
1045 break;
1046 case MP4A_AUDIO_PLI_LowDelay_L1:
1047 case MP4A_AUDIO_PLI_LowDelay_L2:
1048 case MP4A_AUDIO_PLI_LowDelay_L3:
1049 case MP4A_AUDIO_PLI_LowDelay_L4:
1050 case MP4A_AUDIO_PLI_LowDelay_L5:
1051 case MP4A_AUDIO_PLI_LowDelay_L6:
1052 case MP4A_AUDIO_PLI_LowDelay_L7:
1053 case MP4A_AUDIO_PLI_LowDelay_L8:
1054 if( MP4A_AUDIO_PLI_LowDelay_L1 <= b && b <= MP4A_AUDIO_PLI_LowDelay_L8 )
1055 return 1;
1056 return 0;
1057 break;
1058 case MP4A_AUDIO_PLI_Natural_L1:
1059 case MP4A_AUDIO_PLI_Natural_L2:
1060 case MP4A_AUDIO_PLI_Natural_L3:
1061 case MP4A_AUDIO_PLI_Natural_L4:
1062 if( MP4A_AUDIO_PLI_Natural_L1 <= b && b <= MP4A_AUDIO_PLI_Natural_L4 )
1063 return 1;
1064 return 0;
1065 break;
1066 case MP4A_AUDIO_PLI_MobileInternetworking_L1:
1067 case MP4A_AUDIO_PLI_MobileInternetworking_L2:
1068 case MP4A_AUDIO_PLI_MobileInternetworking_L3:
1069 case MP4A_AUDIO_PLI_MobileInternetworking_L4:
1070 case MP4A_AUDIO_PLI_MobileInternetworking_L5:
1071 case MP4A_AUDIO_PLI_MobileInternetworking_L6:
1072 if( MP4A_AUDIO_PLI_MobileInternetworking_L1 <= b && b <= MP4A_AUDIO_PLI_MobileInternetworking_L6 )
1073 return 1;
1074 return 0;
1075 break;
1076 case MP4A_AUDIO_PLI_AAC_L1:
1077 case MP4A_AUDIO_PLI_AAC_L2:
1078 case MP4A_AUDIO_PLI_AAC_L4:
1079 case MP4A_AUDIO_PLI_AAC_L5:
1080 if( MP4A_AUDIO_PLI_AAC_L1 <= b && b <= MP4A_AUDIO_PLI_AAC_L5 )
1081 return 1;
1082 return 0;
1083 break;
1084 case MP4A_AUDIO_PLI_HE_AAC_L2:
1085 case MP4A_AUDIO_PLI_HE_AAC_L3:
1086 case MP4A_AUDIO_PLI_HE_AAC_L4:
1087 case MP4A_AUDIO_PLI_HE_AAC_L5:
1088 if( MP4A_AUDIO_PLI_HE_AAC_L2 <= b && b <= MP4A_AUDIO_PLI_HE_AAC_L5 )
1089 return 1;
1090 return 0;
1091 break;
1092 default:
1093 break;
1095 return 0;
1098 /* NOTE: This function is not strictly preferable, but accurate.
1099 The spec of audioProfileLevelIndication is too much complicated. */
1100 mp4a_audioProfileLevelIndication mp4a_max_audioProfileLevelIndication( mp4a_audioProfileLevelIndication a, mp4a_audioProfileLevelIndication b )
1102 /* NONE_REQUIRED is minimal priotity, and NOT_SPECIFIED is max priority. */
1103 if( a == MP4A_AUDIO_PLI_NOT_SPECIFIED || b == MP4A_AUDIO_PLI_NONE_REQUIRED )
1104 return a;
1105 if( a == MP4A_AUDIO_PLI_NONE_REQUIRED || b == MP4A_AUDIO_PLI_NOT_SPECIFIED )
1106 return b;
1107 mp4a_audioProfileLevelIndication c, d;
1108 if( a < b )
1110 c = a;
1111 d = b;
1113 else
1115 c = b;
1116 d = a;
1118 /* AAC-LC and SBR specific; If mixtured there, use correspond HE_AAC profile. */
1119 if( MP4A_AUDIO_PLI_AAC_L1 <= c && c <= MP4A_AUDIO_PLI_AAC_L5
1120 && MP4A_AUDIO_PLI_HE_AAC_L2 <= d && d <= MP4A_AUDIO_PLI_HE_AAC_L5 )
1122 if( c <= MP4A_AUDIO_PLI_AAC_L2 )
1123 return d;
1124 c += 4; /* upgrade to HE-AAC */
1125 return c > d ? c : d;
1127 /* General */
1128 if( mp4sys_is_same_profile( c, d ) )
1129 return d;
1130 return MP4A_AUDIO_PLI_NOT_SPECIFIED;