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