summaryrefslogtreecommitdiff
path: root/audio_codec/libfaad/mp4.c (plain)
blob: 3b18f0fcc82710323d8c67f763cc5a50607eaca6
1/*
2** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
4**
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.
9**
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.
14**
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.
18**
19** Any non-GPL usage of this software or parts of this software is strictly
20** forbidden.
21**
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"
24**
25** Commercial non-GPL licensing of this software is possible.
26** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27**
28** $Id: mp4.c,v 1.40 2009/02/06 03:39:58 menno Exp $
29**/
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <fcntl.h>
34#include "common.h"
35#include "structs.h"
36
37
38#include "bits.h"
39#include "mp4.h"
40#include "syntax.h"
41
42/* defines if an object type can be decoded by this library or not */
43static uint8_t ObjectTypesTable[32] = {
44 0, /* 0 NULL */
45#ifdef MAIN_DEC
46 1, /* 1 AAC Main */
47#else
48 0, /* 1 AAC Main */
49#endif
50 1, /* 2 AAC LC */
51#ifdef SSR_DEC
52 1, /* 3 AAC SSR */
53#else
54 0, /* 3 AAC SSR */
55#endif
56#ifdef LTP_DEC
57 1, /* 4 AAC LTP */
58#else
59 0, /* 4 AAC LTP */
60#endif
61#ifdef SBR_DEC
62 1, /* 5 SBR */
63#else
64 0, /* 5 SBR */
65#endif
66 0, /* 6 AAC Scalable */
67 0, /* 7 TwinVQ */
68 0, /* 8 CELP */
69 0, /* 9 HVXC */
70 0, /* 10 Reserved */
71 0, /* 11 Reserved */
72 0, /* 12 TTSI */
73 0, /* 13 Main synthetic */
74 0, /* 14 Wavetable synthesis */
75 0, /* 15 General MIDI */
76 0, /* 16 Algorithmic Synthesis and Audio FX */
77
78 /* MPEG-4 Version 2 */
79#ifdef ERROR_RESILIENCE
80 1, /* 17 ER AAC LC */
81 0, /* 18 (Reserved) */
82#ifdef LTP_DEC
83 1, /* 19 ER AAC LTP */
84#else
85 0, /* 19 ER AAC LTP */
86#endif
87 0, /* 20 ER AAC scalable */
88 0, /* 21 ER TwinVQ */
89 0, /* 22 ER BSAC */
90#ifdef LD_DEC
91 1, /* 23 ER AAC LD */
92#else
93 0, /* 23 ER AAC LD */
94#endif
95 0, /* 24 ER CELP */
96 0, /* 25 ER HVXC */
97 0, /* 26 ER HILN */
98 0, /* 27 ER Parametric */
99#else /* No ER defined */
100 0, /* 17 ER AAC LC */
101 0, /* 18 (Reserved) */
102 0, /* 19 ER AAC LTP */
103 0, /* 20 ER AAC scalable */
104 0, /* 21 ER TwinVQ */
105 0, /* 22 ER BSAC */
106 0, /* 23 ER AAC LD */
107 0, /* 24 ER CELP */
108 0, /* 25 ER HVXC */
109 0, /* 26 ER HILN */
110 0, /* 27 ER Parametric */
111#endif
112 0, /* 28 (Reserved) */
113 1, /* 29 PS*/
114 0, /* 30 (Reserved) */
115 0 /* 31 (Reserved) */
116};
117
118/* Table 1.6.1 */
119char NEAACDECAPI NeAACDecAudioSpecificConfig(unsigned char *pBuffer,
120 unsigned long buffer_size,
121 mp4AudioSpecificConfig *mp4ASC)
122{
123 return AudioSpecificConfig2(pBuffer, buffer_size, mp4ASC, NULL, 0);
124}
125
126int AudioSpecificConfigFromBitfile(bitfile *ld,
127 mp4AudioSpecificConfig *mp4ASC,
128 program_config *pce, uint32_t buffer_size, uint8_t short_form)
129{
130 int8_t result = 0;
131 uint32_t startpos = faad_get_processed_bits(ld);
132#ifdef SBR_DEC
133 int8_t bits_to_decode = 0;
134#endif
135
136 if (mp4ASC == NULL) {
137 return -8;
138 }
139
140 memset(mp4ASC, 0, sizeof(mp4AudioSpecificConfig));
141
142 mp4ASC->objectTypeIndex = (uint8_t)faad_getbits(ld, 5
143 DEBUGVAR(1, 1, "parse_audio_decoder_specific_info(): ObjectTypeIndex"));
144 if (mp4ASC->objectTypeIndex == /*AOT_ESCAPE*/31) {
145 mp4ASC->objectTypeIndex = 32 + faad_getbits(ld, 6);
146 }
147 //audio_codec_print("object type %x \n", mp4ASC->objectTypeIndex);
148 mp4ASC->samplingFrequencyIndex = (uint8_t)faad_getbits(ld, 4
149 DEBUGVAR(1, 2, "parse_audio_decoder_specific_info(): SamplingFrequencyIndex"));
150 if (mp4ASC->samplingFrequencyIndex == 0x0f) {
151 faad_getbits(ld, 24);
152 }
153
154 mp4ASC->channelsConfiguration = (uint8_t)faad_getbits(ld, 4
155 DEBUGVAR(1, 3, "parse_audio_decoder_specific_info(): ChannelsConfiguration"));
156
157 mp4ASC->samplingFrequency = get_sample_rate(mp4ASC->samplingFrequencyIndex);
158
159 if (ObjectTypesTable[mp4ASC->objectTypeIndex] != 1) {
160 return -1;
161 }
162
163 if (mp4ASC->samplingFrequency == 0) {
164 return -2;
165 }
166
167 if (mp4ASC->channelsConfiguration > 7) {
168 return -3;
169 }
170
171#if (defined(PS_DEC) || defined(DRM_PS))
172 /* check if we have a mono file */
173 if (mp4ASC->channelsConfiguration == 1) {
174 /* upMatrix to 2 channels for implicit signalling of PS */
175 mp4ASC->channelsConfiguration = 2;
176 }
177#endif
178
179#ifdef SBR_DEC
180 mp4ASC->sbr_present_flag = -1;
181 if ((mp4ASC->objectTypeIndex == 5) || (mp4ASC->objectTypeIndex == 29 && !(faad_showbits(ld, 3) & 0x03 && !(faad_showbits(ld, 9) & 0x3F)))) {
182 uint8_t tmp;
183
184 mp4ASC->sbr_present_flag = 1;
185 tmp = (uint8_t)faad_getbits(ld, 4
186 DEBUGVAR(1, 5, "parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex"));
187 /* check for downsampled SBR */
188 if (tmp == mp4ASC->samplingFrequencyIndex) {
189 mp4ASC->downSampledSBR = 1;
190 }
191 mp4ASC->samplingFrequencyIndex = tmp;
192 if (mp4ASC->samplingFrequencyIndex == 15) {
193 mp4ASC->samplingFrequency = (uint32_t)faad_getbits(ld, 24
194 DEBUGVAR(1, 6, "parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex"));
195 } else {
196 mp4ASC->samplingFrequency = get_sample_rate(mp4ASC->samplingFrequencyIndex);
197 }
198 mp4ASC->objectTypeIndex = (uint8_t)faad_getbits(ld, 5
199 DEBUGVAR(1, 7, "parse_audio_decoder_specific_info(): ObjectTypeIndex"));
200 }
201#endif
202
203 /* get GASpecificConfig */
204 if (mp4ASC->objectTypeIndex == 1 || mp4ASC->objectTypeIndex == 2 ||
205 mp4ASC->objectTypeIndex == 3 || mp4ASC->objectTypeIndex == 4 ||
206 mp4ASC->objectTypeIndex == 6 || mp4ASC->objectTypeIndex == 7) {
207 result = GASpecificConfig(ld, mp4ASC, pce);
208
209#ifdef ERROR_RESILIENCE
210 } else if (mp4ASC->objectTypeIndex >= ER_OBJECT_START) { /* ER */
211 result = GASpecificConfig(ld, mp4ASC, pce);
212 mp4ASC->epConfig = (uint8_t)faad_getbits(ld, 2
213 DEBUGVAR(1, 143, "parse_audio_decoder_specific_info(): epConfig"));
214
215 if (mp4ASC->epConfig != 0) {
216 result = -5;
217 }
218#endif
219
220 } else {
221 result = -4;
222 }
223
224#ifdef SSR_DEC
225 /* shorter frames not allowed for SSR */
226 if ((mp4ASC->objectTypeIndex == 4) && mp4ASC->frameLengthFlag) {
227 return -6;
228 }
229#endif
230
231
232#ifdef SBR_DEC
233 if (short_form) {
234 bits_to_decode = 0;
235 } else {
236 bits_to_decode = (int8_t)(buffer_size * 8 - (startpos - faad_get_processed_bits(ld)));
237 }
238
239 if ((mp4ASC->objectTypeIndex != 5) && (bits_to_decode >= 16)) {
240 int16_t syncExtensionType = (int16_t)faad_getbits(ld, 11
241 DEBUGVAR(1, 9, "parse_audio_decoder_specific_info(): syncExtensionType"));
242
243 if (syncExtensionType == 0x2b7) {
244 uint8_t tmp_OTi = (uint8_t)faad_getbits(ld, 5
245 DEBUGVAR(1, 10, "parse_audio_decoder_specific_info(): extensionAudioObjectType"));
246
247 if (tmp_OTi == 5) {
248 mp4ASC->sbr_present_flag = (uint8_t)faad_get1bit(ld
249 DEBUGVAR(1, 11, "parse_audio_decoder_specific_info(): sbr_present_flag"));
250
251 if (mp4ASC->sbr_present_flag) {
252 uint8_t tmp;
253
254 /* Don't set OT to SBR until checked that it is actually there */
255 mp4ASC->objectTypeIndex = tmp_OTi;
256
257 tmp = (uint8_t)faad_getbits(ld, 4
258 DEBUGVAR(1, 12, "parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex"));
259
260 /* check for downsampled SBR */
261 if (tmp == mp4ASC->samplingFrequencyIndex) {
262 mp4ASC->downSampledSBR = 1;
263 }
264 mp4ASC->samplingFrequencyIndex = tmp;
265
266 if (mp4ASC->samplingFrequencyIndex == 15) {
267 mp4ASC->samplingFrequency = (uint32_t)faad_getbits(ld, 24
268 DEBUGVAR(1, 13, "parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex"));
269 } else {
270 mp4ASC->samplingFrequency = get_sample_rate(mp4ASC->samplingFrequencyIndex);
271 }
272 }
273 }
274 }
275 }
276
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) {
280 if (mp4ASC->samplingFrequency <= 24000) {
281 mp4ASC->samplingFrequency *= 2;
282 mp4ASC->forceUpSampling = 1;
283 } else { /* > 24000*/
284 mp4ASC->downSampledSBR = 1;
285 }
286 }
287#endif
288
289 faad_endbits(ld);
290
291 return result;
292}
293
294int AudioSpecificConfig2(uint8_t *pBuffer,
295 uint32_t buffer_size,
296 mp4AudioSpecificConfig *mp4ASC,
297 program_config *pce,
298 uint8_t short_form)
299{
300 uint8_t ret = 0;
301 bitfile ld;
302 faad_initbits(&ld, pBuffer, buffer_size);
303 faad_byte_align(&ld);
304 ret = AudioSpecificConfigFromBitfile(&ld, mp4ASC, pce, buffer_size, short_form);
305 faad_endbits(&ld);
306 return ret;
307}
308