2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2005 M. Bakker, Nero 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 ** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23 ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
25 ** Commercial non-GPL licensing of this software is possible.
26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
28 ** $Id: mp4.c,v 1.40 2009/02/06 03:39:58 menno Exp $
40 /* defines if an object type can be decoded by this library or not */
41 static uint8_t ObjectTypesTable[32] = {
64 0, /* 6 AAC Scalable */
71 0, /* 13 Main synthetic */
72 0, /* 14 Wavetable synthesis */
73 0, /* 15 General MIDI */
74 0, /* 16 Algorithmic Synthesis and Audio FX */
76 /* MPEG-4 Version 2 */
77 #ifdef ERROR_RESILIENCE
79 0, /* 18 (Reserved) */
81 1, /* 19 ER AAC LTP */
83 0, /* 19 ER AAC LTP */
85 0, /* 20 ER AAC scalable */
96 0, /* 27 ER Parametric */
97 #else /* No ER defined */
99 0, /* 18 (Reserved) */
100 0, /* 19 ER AAC LTP */
101 0, /* 20 ER AAC scalable */
102 0, /* 21 ER TwinVQ */
104 0, /* 23 ER AAC LD */
108 0, /* 27 ER Parametric */
110 0, /* 28 (Reserved) */
111 0, /* 29 (Reserved) */
112 0, /* 30 (Reserved) */
113 0 /* 31 (Reserved) */
117 char NEAACDECAPI NeAACDecAudioSpecificConfig(unsigned char *pBuffer,
118 unsigned long buffer_size,
119 mp4AudioSpecificConfig *mp4ASC)
121 return AudioSpecificConfig2(pBuffer, buffer_size, mp4ASC, NULL, 0);
124 int8_t AudioSpecificConfigFromBitfile(bitfile *ld,
125 mp4AudioSpecificConfig *mp4ASC,
126 program_config *pce, uint32_t buffer_size, uint8_t short_form)
129 uint32_t startpos = faad_get_processed_bits(ld);
131 int8_t bits_to_decode = 0;
137 memset(mp4ASC, 0, sizeof(mp4AudioSpecificConfig));
139 mp4ASC->objectTypeIndex = (uint8_t)faad_getbits(ld, 5
140 DEBUGVAR(1,1,"parse_audio_decoder_specific_info(): ObjectTypeIndex"));
142 mp4ASC->samplingFrequencyIndex = (uint8_t)faad_getbits(ld, 4
143 DEBUGVAR(1,2,"parse_audio_decoder_specific_info(): SamplingFrequencyIndex"));
144 if(mp4ASC->samplingFrequencyIndex==0x0f)
145 faad_getbits(ld, 24);
147 mp4ASC->channelsConfiguration = (uint8_t)faad_getbits(ld, 4
148 DEBUGVAR(1,3,"parse_audio_decoder_specific_info(): ChannelsConfiguration"));
150 mp4ASC->samplingFrequency = get_sample_rate(mp4ASC->samplingFrequencyIndex);
152 if (ObjectTypesTable[mp4ASC->objectTypeIndex] != 1)
157 if (mp4ASC->samplingFrequency == 0)
162 if (mp4ASC->channelsConfiguration > 7)
167 #if (defined(PS_DEC) || defined(DRM_PS))
168 /* check if we have a mono file */
169 if (mp4ASC->channelsConfiguration == 1)
171 /* upMatrix to 2 channels for implicit signalling of PS */
172 mp4ASC->channelsConfiguration = 2;
177 mp4ASC->sbr_present_flag = -1;
178 if (mp4ASC->objectTypeIndex == 5)
182 mp4ASC->sbr_present_flag = 1;
183 tmp = (uint8_t)faad_getbits(ld, 4
184 DEBUGVAR(1,5,"parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex"));
185 /* check for downsampled SBR */
186 if (tmp == mp4ASC->samplingFrequencyIndex)
187 mp4ASC->downSampledSBR = 1;
188 mp4ASC->samplingFrequencyIndex = tmp;
189 if (mp4ASC->samplingFrequencyIndex == 15)
191 mp4ASC->samplingFrequency = (uint32_t)faad_getbits(ld, 24
192 DEBUGVAR(1,6,"parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex"));
194 mp4ASC->samplingFrequency = get_sample_rate(mp4ASC->samplingFrequencyIndex);
196 mp4ASC->objectTypeIndex = (uint8_t)faad_getbits(ld, 5
197 DEBUGVAR(1,7,"parse_audio_decoder_specific_info(): ObjectTypeIndex"));
201 /* get GASpecificConfig */
202 if (mp4ASC->objectTypeIndex == 1 || mp4ASC->objectTypeIndex == 2 ||
203 mp4ASC->objectTypeIndex == 3 || mp4ASC->objectTypeIndex == 4 ||
204 mp4ASC->objectTypeIndex == 6 || mp4ASC->objectTypeIndex == 7)
206 result = GASpecificConfig(ld, mp4ASC, pce);
208 #ifdef ERROR_RESILIENCE
209 } else if (mp4ASC->objectTypeIndex >= ER_OBJECT_START) { /* ER */
210 result = GASpecificConfig(ld, mp4ASC, pce);
211 mp4ASC->epConfig = (uint8_t)faad_getbits(ld, 2
212 DEBUGVAR(1,143,"parse_audio_decoder_specific_info(): epConfig"));
214 if (mp4ASC->epConfig != 0)
223 /* shorter frames not allowed for SSR */
224 if ((mp4ASC->objectTypeIndex == 4) && mp4ASC->frameLengthFlag)
233 bits_to_decode = (int8_t)(buffer_size*8 - (startpos-faad_get_processed_bits(ld)));
235 if ((mp4ASC->objectTypeIndex != 5) && (bits_to_decode >= 16))
237 int16_t syncExtensionType = (int16_t)faad_getbits(ld, 11
238 DEBUGVAR(1,9,"parse_audio_decoder_specific_info(): syncExtensionType"));
240 if (syncExtensionType == 0x2b7)
242 uint8_t tmp_OTi = (uint8_t)faad_getbits(ld, 5
243 DEBUGVAR(1,10,"parse_audio_decoder_specific_info(): extensionAudioObjectType"));
247 mp4ASC->sbr_present_flag = (uint8_t)faad_get1bit(ld
248 DEBUGVAR(1,11,"parse_audio_decoder_specific_info(): sbr_present_flag"));
250 if (mp4ASC->sbr_present_flag)
254 /* Don't set OT to SBR until checked that it is actually there */
255 mp4ASC->objectTypeIndex = tmp_OTi;
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;
296 int8_t AudioSpecificConfig2(uint8_t *pBuffer,
297 uint32_t buffer_size,
298 mp4AudioSpecificConfig *mp4ASC,
304 faad_initbits(&ld, pBuffer, buffer_size);
305 faad_byte_align(&ld);
306 ret = AudioSpecificConfigFromBitfile(&ld, mp4ASC, pce, buffer_size, short_form);