]> 4ch.mooo.com Git - 16.git/blob - src/lib/doslib/ext/faad/mp4.c
wwww
[16.git] / src / lib / doslib / ext / faad / mp4.c
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
31 #include "common.h"
32 #include "structs.h"
33
34 #include <stdlib.h>
35
36 #include "bits.h"
37 #include "mp4.h"
38 #include "syntax.h"
39
40 /* defines if an object type can be decoded by this library or not */
41 static uint8_t ObjectTypesTable[32] = {
42     0, /*  0 NULL */
43 #ifdef MAIN_DEC
44     1, /*  1 AAC Main */
45 #else
46     0, /*  1 AAC Main */
47 #endif
48     1, /*  2 AAC LC */
49 #ifdef SSR_DEC
50     1, /*  3 AAC SSR */
51 #else
52     0, /*  3 AAC SSR */
53 #endif
54 #ifdef LTP_DEC
55     1, /*  4 AAC LTP */
56 #else
57     0, /*  4 AAC LTP */
58 #endif
59 #ifdef SBR_DEC
60     1, /*  5 SBR */
61 #else
62     0, /*  5 SBR */
63 #endif
64     0, /*  6 AAC Scalable */
65     0, /*  7 TwinVQ */
66     0, /*  8 CELP */
67     0, /*  9 HVXC */
68     0, /* 10 Reserved */
69     0, /* 11 Reserved */
70     0, /* 12 TTSI */
71     0, /* 13 Main synthetic */
72     0, /* 14 Wavetable synthesis */
73     0, /* 15 General MIDI */
74     0, /* 16 Algorithmic Synthesis and Audio FX */
75
76     /* MPEG-4 Version 2 */
77 #ifdef ERROR_RESILIENCE
78     1, /* 17 ER AAC LC */
79     0, /* 18 (Reserved) */
80 #ifdef LTP_DEC
81     1, /* 19 ER AAC LTP */
82 #else
83     0, /* 19 ER AAC LTP */
84 #endif
85     0, /* 20 ER AAC scalable */
86     0, /* 21 ER TwinVQ */
87     0, /* 22 ER BSAC */
88 #ifdef LD_DEC
89     1, /* 23 ER AAC LD */
90 #else
91     0, /* 23 ER AAC LD */
92 #endif
93     0, /* 24 ER CELP */
94     0, /* 25 ER HVXC */
95     0, /* 26 ER HILN */
96     0, /* 27 ER Parametric */
97 #else /* No ER defined */
98     0, /* 17 ER AAC LC */
99     0, /* 18 (Reserved) */
100     0, /* 19 ER AAC LTP */
101     0, /* 20 ER AAC scalable */
102     0, /* 21 ER TwinVQ */
103     0, /* 22 ER BSAC */
104     0, /* 23 ER AAC LD */
105     0, /* 24 ER CELP */
106     0, /* 25 ER HVXC */
107     0, /* 26 ER HILN */
108     0, /* 27 ER Parametric */
109 #endif
110     0, /* 28 (Reserved) */
111     0, /* 29 (Reserved) */
112     0, /* 30 (Reserved) */
113     0  /* 31 (Reserved) */
114 };
115
116 /* Table 1.6.1 */
117 char NEAACDECAPI NeAACDecAudioSpecificConfig(unsigned char *pBuffer,
118                                              unsigned long buffer_size,
119                                              mp4AudioSpecificConfig *mp4ASC)
120 {
121     return AudioSpecificConfig2(pBuffer, buffer_size, mp4ASC, NULL, 0);
122 }
123
124 int8_t AudioSpecificConfigFromBitfile(bitfile *ld,
125                             mp4AudioSpecificConfig *mp4ASC,
126                             program_config *pce, uint32_t buffer_size, uint8_t short_form)
127 {
128     int8_t result = 0;
129     uint32_t startpos = faad_get_processed_bits(ld);
130 #ifdef SBR_DEC
131     int8_t bits_to_decode = 0;
132 #endif
133
134     if (mp4ASC == NULL)
135         return -8;
136
137     memset(mp4ASC, 0, sizeof(mp4AudioSpecificConfig));
138
139     mp4ASC->objectTypeIndex = (uint8_t)faad_getbits(ld, 5
140         DEBUGVAR(1,1,"parse_audio_decoder_specific_info(): ObjectTypeIndex"));
141
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);
146
147     mp4ASC->channelsConfiguration = (uint8_t)faad_getbits(ld, 4
148         DEBUGVAR(1,3,"parse_audio_decoder_specific_info(): ChannelsConfiguration"));
149
150     mp4ASC->samplingFrequency = get_sample_rate(mp4ASC->samplingFrequencyIndex);
151
152     if (ObjectTypesTable[mp4ASC->objectTypeIndex] != 1)
153     {
154         return -1;
155     }
156
157     if (mp4ASC->samplingFrequency == 0)
158     {
159         return -2;
160     }
161
162     if (mp4ASC->channelsConfiguration > 7)
163     {
164         return -3;
165     }
166
167 #if (defined(PS_DEC) || defined(DRM_PS))
168     /* check if we have a mono file */
169     if (mp4ASC->channelsConfiguration == 1)
170     {
171         /* upMatrix to 2 channels for implicit signalling of PS */
172         mp4ASC->channelsConfiguration = 2;
173     }
174 #endif
175
176 #ifdef SBR_DEC
177     mp4ASC->sbr_present_flag = -1;
178     if (mp4ASC->objectTypeIndex == 5)
179     {
180         uint8_t tmp;
181
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)
190         {
191             mp4ASC->samplingFrequency = (uint32_t)faad_getbits(ld, 24
192                 DEBUGVAR(1,6,"parse_audio_decoder_specific_info(): extensionSamplingFrequencyIndex"));
193         } else {
194             mp4ASC->samplingFrequency = get_sample_rate(mp4ASC->samplingFrequencyIndex);
195         }
196         mp4ASC->objectTypeIndex = (uint8_t)faad_getbits(ld, 5
197             DEBUGVAR(1,7,"parse_audio_decoder_specific_info(): ObjectTypeIndex"));
198     }
199 #endif
200
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)
205     {
206         result = GASpecificConfig(ld, mp4ASC, pce);
207
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"));
213
214         if (mp4ASC->epConfig != 0)
215             result = -5;
216 #endif
217
218     } else {
219         result = -4;
220     }
221
222 #ifdef SSR_DEC
223     /* shorter frames not allowed for SSR */
224     if ((mp4ASC->objectTypeIndex == 4) && mp4ASC->frameLengthFlag)
225         return -6;
226 #endif
227
228
229 #ifdef SBR_DEC
230     if(short_form)
231         bits_to_decode = 0;
232     else
233                 bits_to_decode = (int8_t)(buffer_size*8 - (startpos-faad_get_processed_bits(ld)));
234
235     if ((mp4ASC->objectTypeIndex != 5) && (bits_to_decode >= 16))
236     {
237         int16_t syncExtensionType = (int16_t)faad_getbits(ld, 11
238             DEBUGVAR(1,9,"parse_audio_decoder_specific_info(): syncExtensionType"));
239
240         if (syncExtensionType == 0x2b7)
241         {
242             uint8_t tmp_OTi = (uint8_t)faad_getbits(ld, 5
243                 DEBUGVAR(1,10,"parse_audio_decoder_specific_info(): extensionAudioObjectType"));
244
245             if (tmp_OTi == 5)
246             {
247                 mp4ASC->sbr_present_flag = (uint8_t)faad_get1bit(ld
248                     DEBUGVAR(1,11,"parse_audio_decoder_specific_info(): sbr_present_flag"));
249
250                 if (mp4ASC->sbr_present_flag)
251                 {
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                     mp4ASC->samplingFrequencyIndex = tmp;
264
265                     if (mp4ASC->samplingFrequencyIndex == 15)
266                     {
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     {
281         if (mp4ASC->samplingFrequency <= 24000)
282         {
283             mp4ASC->samplingFrequency *= 2;
284             mp4ASC->forceUpSampling = 1;
285         } else /* > 24000*/ {
286             mp4ASC->downSampledSBR = 1;
287         }
288     }
289 #endif
290
291     faad_endbits(ld);
292
293     return result;
294 }
295
296 int8_t AudioSpecificConfig2(uint8_t *pBuffer,
297                             uint32_t buffer_size,
298                             mp4AudioSpecificConfig *mp4ASC,
299                             program_config *pce,
300                             uint8_t short_form)
301 {
302     uint8_t ret = 0;
303     bitfile ld;
304     faad_initbits(&ld, pBuffer, buffer_size);
305     faad_byte_align(&ld);
306     ret = AudioSpecificConfigFromBitfile(&ld, mp4ASC, pce, buffer_size, short_form);
307     faad_endbits(&ld);
308     return ret;
309 }