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