2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 ** Any non-GPL usage of this software or parts of this software is strictly
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** $Id: mp4.c,v 1.32 2004/09/04 14:56:28 menno Exp $
37 /* defines if an object type can be decoded by this library or not */
38 static uint8_t ObjectTypesTable
[32] = {
62 1, /* 6 AAC Scalable */
64 0, /* 6 AAC Scalable */
72 0, /* 13 Main synthetic */
73 0, /* 14 Wavetable synthesis */
74 0, /* 15 General MIDI */
75 0, /* 16 Algorithmic Synthesis and Audio FX */
77 /* MPEG-4 Version 2 */
78 #ifdef ERROR_RESILIENCE
80 0, /* 18 (Reserved) */
82 1, /* 19 ER AAC LTP */
84 0, /* 19 ER AAC LTP */
87 1, /* 20 ER AAC scalable */
89 0, /* 20 ER AAC scalable */
101 0, /* 27 ER Parametric */
102 #else /* No ER defined */
103 0, /* 17 ER AAC LC */
104 0, /* 18 (Reserved) */
105 0, /* 19 ER AAC LTP */
106 0, /* 20 ER AAC scalable */
107 0, /* 21 ER TwinVQ */
109 0, /* 23 ER AAC LD */
113 0, /* 27 ER Parametric */
115 0, /* 28 (Reserved) */
116 0, /* 29 (Reserved) */
117 0, /* 30 (Reserved) */
118 0 /* 31 (Reserved) */
122 int8_t NEAACDECAPI
NeAACDecAudioSpecificConfig(uint8_t *pBuffer
,
123 uint32_t buffer_size
,
124 mp4AudioSpecificConfig
*mp4ASC
)
126 return AudioSpecificConfig2(pBuffer
, buffer_size
, mp4ASC
, NULL
, 0);
129 int8_t AudioSpecificConfigFromBitfile(bitfile
*ld
,
130 mp4AudioSpecificConfig
*mp4ASC
,
131 program_config
*pce
, uint32_t buffer_size
, uint8_t short_form
)
134 uint32_t startpos
= faad_get_processed_bits(ld
);
136 int8_t bits_to_decode
= 0;
142 memset(mp4ASC
, 0, sizeof(mp4AudioSpecificConfig
));
144 mp4ASC
->objectTypeIndex
= (uint8_t)faad_getbits(ld
, 5
145 DEBUGVAR(1,1,"parse_audio_decoder_specific_info(): ObjectTypeIndex"));
147 mp4ASC
->samplingFrequencyIndex
= (uint8_t)faad_getbits(ld
, 4
148 DEBUGVAR(1,2,"parse_audio_decoder_specific_info(): SamplingFrequencyIndex"));
149 if(mp4ASC
->samplingFrequencyIndex
==0x0f)
150 faad_getbits(ld
, 24);
152 mp4ASC
->channelsConfiguration
= (uint8_t)faad_getbits(ld
, 4
153 DEBUGVAR(1,3,"parse_audio_decoder_specific_info(): ChannelsConfiguration"));
155 mp4ASC
->samplingFrequency
= get_sample_rate(mp4ASC
->samplingFrequencyIndex
);
157 if (ObjectTypesTable
[mp4ASC
->objectTypeIndex
] != 1)
162 if (mp4ASC
->samplingFrequency
== 0)
167 if (mp4ASC
->channelsConfiguration
> 7)
172 #if (defined(PS_DEC) || defined(DRM_PS))
173 /* check if we have a mono file */
174 if (mp4ASC
->channelsConfiguration
== 1)
176 /* upMatrix to 2 channels for implicit signalling of PS */
177 mp4ASC
->channelsConfiguration
= 2;
182 mp4ASC
->sbr_present_flag
= -1;
183 if (mp4ASC
->objectTypeIndex
== 5)
187 mp4ASC
->sbr_present_flag
= 1;
188 tmp
= (uint8_t)faad_getbits(ld
, 4
189 DEBUGVAR(1,5,"parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex"));
190 /* check for downsampled SBR */
191 if (tmp
== mp4ASC
->samplingFrequencyIndex
)
192 mp4ASC
->downSampledSBR
= 1;
193 mp4ASC
->samplingFrequencyIndex
= tmp
;
194 if (mp4ASC
->samplingFrequencyIndex
== 15)
196 mp4ASC
->samplingFrequency
= (uint32_t)faad_getbits(ld
, 24
197 DEBUGVAR(1,6,"parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex"));
199 mp4ASC
->samplingFrequency
= get_sample_rate(mp4ASC
->samplingFrequencyIndex
);
201 mp4ASC
->objectTypeIndex
= (uint8_t)faad_getbits(ld
, 5
202 DEBUGVAR(1,7,"parse_audio_decoder_specific_info(): ObjectTypeIndex"));
206 /* get GASpecificConfig */
207 if (mp4ASC
->objectTypeIndex
== 1 || mp4ASC
->objectTypeIndex
== 2 ||
208 mp4ASC
->objectTypeIndex
== 3 || mp4ASC
->objectTypeIndex
== 4 ||
209 mp4ASC
->objectTypeIndex
== 6 || mp4ASC
->objectTypeIndex
== 7)
211 result
= GASpecificConfig(ld
, mp4ASC
, pce
);
213 #ifdef ERROR_RESILIENCE
214 } else if (mp4ASC
->objectTypeIndex
>= ER_OBJECT_START
) { /* ER */
215 result
= GASpecificConfig(ld
, mp4ASC
, pce
);
216 mp4ASC
->epConfig
= (uint8_t)faad_getbits(ld
, 2
217 DEBUGVAR(1,143,"parse_audio_decoder_specific_info(): epConfig"));
219 if (mp4ASC
->epConfig
!= 0)
228 /* shorter frames not allowed for SSR */
229 if ((mp4ASC
->objectTypeIndex
== 4) && mp4ASC
->frameLengthFlag
)
238 bits_to_decode
= (int8_t)(buffer_size
*8 - (startpos
-faad_get_processed_bits(ld
)));
239 if ((mp4ASC
->objectTypeIndex
!= 5) && (bits_to_decode
>= 16))
241 int16_t syncExtensionType
= (int16_t)faad_getbits(ld
, 11
242 DEBUGVAR(1,9,"parse_audio_decoder_specific_info(): syncExtensionType"));
244 if (syncExtensionType
== 0x2b7)
246 mp4ASC
->objectTypeIndex
= (uint8_t)faad_getbits(ld
, 5
247 DEBUGVAR(1,10,"parse_audio_decoder_specific_info(): extensionAudioObjectType"));
249 if (mp4ASC
->objectTypeIndex
== 5)
251 mp4ASC
->sbr_present_flag
= (uint8_t)faad_get1bit(ld
252 DEBUGVAR(1,11,"parse_audio_decoder_specific_info(): sbr_present_flag"));
254 if (mp4ASC
->sbr_present_flag
)
257 tmp
= (uint8_t)faad_getbits(ld
, 4
258 DEBUGVAR(1,12,"parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex"));
260 /* check for downsampled SBR */
261 if (tmp
== mp4ASC
->samplingFrequencyIndex
)
262 mp4ASC
->downSampledSBR
= 1;
263 mp4ASC
->samplingFrequencyIndex
= tmp
;
265 if (mp4ASC
->samplingFrequencyIndex
== 15)
267 mp4ASC
->samplingFrequency
= (uint32_t)faad_getbits(ld
, 24
268 DEBUGVAR(1,13,"parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex"));
270 mp4ASC
->samplingFrequency
= get_sample_rate(mp4ASC
->samplingFrequencyIndex
);
277 /* no SBR signalled, this could mean either implicit signalling or no SBR in this file */
278 /* MPEG specification states: assume SBR on files with samplerate <= 24000 Hz */
279 if (mp4ASC
->sbr_present_flag
== -1)
281 if (mp4ASC
->samplingFrequency
<= 24000)
283 mp4ASC
->samplingFrequency
*= 2;
284 mp4ASC
->forceUpSampling
= 1;
285 } else /* > 24000*/ {
286 mp4ASC
->downSampledSBR
= 1;
294 int8_t AudioSpecificConfig2(uint8_t *pBuffer
,
295 uint32_t buffer_size
,
296 mp4AudioSpecificConfig
*mp4ASC
,
302 faad_initbits(&ld
, pBuffer
, buffer_size
);
303 faad_byte_align(&ld
);
304 ret
= AudioSpecificConfigFromBitfile(&ld
, mp4ASC
, pce
, buffer_size
, short_form
);