/proc/self/cwd/libfaad/decoder.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: decoder.c,v 1.119 2015/01/24 14:21:05 knik Exp $ |
29 | | **/ |
30 | | |
31 | | #include "common.h" |
32 | | #include "structs.h" |
33 | | |
34 | | #include <stdlib.h> |
35 | | #include <stdio.h> |
36 | | #include <string.h> |
37 | | |
38 | | #include "mp4.h" |
39 | | #include "syntax.h" |
40 | | #include "error.h" |
41 | | #include "output.h" |
42 | | #include "filtbank.h" |
43 | | #include "drc.h" |
44 | | #ifdef SBR_DEC |
45 | | #include "sbr_dec.h" |
46 | | #include "sbr_syntax.h" |
47 | | #endif |
48 | | #ifdef SSR_DEC |
49 | | #include "ssr.h" |
50 | | #endif |
51 | | |
52 | | #ifdef ANALYSIS |
53 | | uint16_t dbg_count; |
54 | | #endif |
55 | | |
56 | | #if !defined(PACKAGE_VERSION) |
57 | | #error "PACKAGE_VERSION must be defined" |
58 | | #endif |
59 | | |
60 | | /* static function declarations */ |
61 | | static void* aac_frame_decode(NeAACDecStruct *hDecoder, |
62 | | NeAACDecFrameInfo *hInfo, |
63 | | unsigned char *buffer, |
64 | | unsigned long buffer_size, |
65 | | void **sample_buffer2, |
66 | | unsigned long sample_buffer_size); |
67 | | static void create_channel_config(NeAACDecStruct *hDecoder, |
68 | | NeAACDecFrameInfo *hInfo); |
69 | | |
70 | | |
71 | | int NeAACDecGetVersion(char **faad_id_string, |
72 | | char **faad_copyright_string) |
73 | 0 | { |
74 | 0 | static char *libfaadName = PACKAGE_VERSION; |
75 | 0 | static char *libCopyright = |
76 | 0 | " Copyright 2002-2004: Ahead Software AG\n" |
77 | 0 | " http://www.audiocoding.com\n" |
78 | 0 | " bug tracking: https://sourceforge.net/p/faac/bugs/\n"; |
79 | |
|
80 | 0 | if (faad_id_string) |
81 | 0 | *faad_id_string = libfaadName; |
82 | |
|
83 | 0 | if (faad_copyright_string) |
84 | 0 | *faad_copyright_string = libCopyright; |
85 | |
|
86 | 0 | return 0; |
87 | 0 | } |
88 | | |
89 | | char* NeAACDecGetErrorMessage(unsigned char errcode) |
90 | 0 | { |
91 | 0 | if (errcode >= NUM_ERROR_MESSAGES) |
92 | 0 | return NULL; |
93 | 0 | return err_msg[errcode]; |
94 | 0 | } |
95 | | |
96 | | unsigned long NeAACDecGetCapabilities(void) |
97 | 0 | { |
98 | 0 | uint32_t cap = 0; |
99 | | |
100 | | /* can't do without it */ |
101 | 0 | cap += LC_DEC_CAP; |
102 | |
|
103 | | #ifdef MAIN_DEC |
104 | | cap += MAIN_DEC_CAP; |
105 | | #endif |
106 | 0 | #ifdef LTP_DEC |
107 | 0 | cap += LTP_DEC_CAP; |
108 | 0 | #endif |
109 | 0 | #ifdef LD_DEC |
110 | 0 | cap += LD_DEC_CAP; |
111 | 0 | #endif |
112 | 0 | #ifdef ERROR_RESILIENCE |
113 | 0 | cap += ERROR_RESILIENCE_CAP; |
114 | 0 | #endif |
115 | 0 | #ifdef FIXED_POINT |
116 | 0 | cap += FIXED_POINT_CAP; |
117 | 0 | #endif |
118 | |
|
119 | 0 | return cap; |
120 | 0 | } |
121 | | |
122 | | const unsigned char mes[] = { 0x67,0x20,0x61,0x20,0x20,0x20,0x6f,0x20,0x72,0x20,0x65,0x20,0x6e,0x20,0x20,0x20,0x74,0x20,0x68,0x20,0x67,0x20,0x69,0x20,0x72,0x20,0x79,0x20,0x70,0x20,0x6f,0x20,0x63 }; |
123 | | NeAACDecHandle NeAACDecOpen(void) |
124 | 6.96k | { |
125 | 6.96k | uint8_t i; |
126 | 6.96k | NeAACDecStruct *hDecoder = NULL; |
127 | | |
128 | 6.96k | if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL) |
129 | 0 | return NULL; |
130 | | |
131 | 6.96k | memset(hDecoder, 0, sizeof(NeAACDecStruct)); |
132 | | |
133 | 6.96k | hDecoder->cmes = mes; |
134 | 6.96k | hDecoder->config.outputFormat = FAAD_FMT_16BIT; |
135 | 6.96k | hDecoder->config.defObjectType = MAIN; |
136 | 6.96k | hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */ |
137 | 6.96k | hDecoder->config.downMatrix = 0; |
138 | 6.96k | hDecoder->adts_header_present = 0; |
139 | 6.96k | hDecoder->adif_header_present = 0; |
140 | 6.96k | hDecoder->latm_header_present = 0; |
141 | 6.96k | #ifdef ERROR_RESILIENCE |
142 | 6.96k | hDecoder->aacSectionDataResilienceFlag = 0; |
143 | 6.96k | hDecoder->aacScalefactorDataResilienceFlag = 0; |
144 | 6.96k | hDecoder->aacSpectralDataResilienceFlag = 0; |
145 | 6.96k | #endif |
146 | 6.96k | hDecoder->frameLength = 1024; |
147 | | |
148 | 6.96k | hDecoder->frame = 0; |
149 | 6.96k | hDecoder->sample_buffer = NULL; |
150 | | |
151 | | // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all. |
152 | 6.96k | hDecoder->__r1 = 0x2bb431ea; |
153 | 6.96k | hDecoder->__r2 = 0x206155b7; |
154 | | |
155 | 452k | for (i = 0; i < MAX_CHANNELS; i++) |
156 | 445k | { |
157 | 445k | hDecoder->element_id[i] = INVALID_ELEMENT_ID; |
158 | 445k | hDecoder->window_shape_prev[i] = 0; |
159 | 445k | hDecoder->time_out[i] = NULL; |
160 | 445k | hDecoder->fb_intermed[i] = NULL; |
161 | | #ifdef SSR_DEC |
162 | | hDecoder->ssr_overlap[i] = NULL; |
163 | | hDecoder->prev_fmd[i] = NULL; |
164 | | #endif |
165 | | #ifdef MAIN_DEC |
166 | | hDecoder->pred_stat[i] = NULL; |
167 | | #endif |
168 | 445k | #ifdef LTP_DEC |
169 | 445k | hDecoder->ltp_lag[i] = 0; |
170 | 445k | hDecoder->lt_pred_stat[i] = NULL; |
171 | 445k | #endif |
172 | 445k | } |
173 | | |
174 | 6.96k | #ifdef SBR_DEC |
175 | 341k | for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++) |
176 | 334k | { |
177 | 334k | hDecoder->sbr[i] = NULL; |
178 | 334k | } |
179 | 6.96k | #endif |
180 | | |
181 | 6.96k | hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0)); |
182 | | |
183 | 6.96k | return hDecoder; |
184 | 6.96k | } |
185 | | |
186 | | NeAACDecConfigurationPtr NeAACDecGetCurrentConfiguration(NeAACDecHandle hpDecoder) |
187 | 6.87k | { |
188 | 6.87k | NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder; |
189 | 6.87k | if (hDecoder) |
190 | 6.87k | { |
191 | 6.87k | NeAACDecConfigurationPtr config = &(hDecoder->config); |
192 | | |
193 | 6.87k | return config; |
194 | 6.87k | } |
195 | | |
196 | 0 | return NULL; |
197 | 6.87k | } |
198 | | |
199 | | unsigned char NeAACDecSetConfiguration(NeAACDecHandle hpDecoder, |
200 | | NeAACDecConfigurationPtr config) |
201 | 6.96k | { |
202 | 6.96k | NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder; |
203 | 6.96k | if (hDecoder && config) |
204 | 6.96k | { |
205 | | /* check if we can decode this object type */ |
206 | 6.96k | if (can_decode_ot(config->defObjectType) < 0) |
207 | 7 | return 0; |
208 | 6.96k | hDecoder->config.defObjectType = config->defObjectType; |
209 | | |
210 | | /* samplerate: anything but 0 should be possible */ |
211 | 6.96k | if (config->defSampleRate == 0) |
212 | 1 | return 0; |
213 | 6.96k | hDecoder->config.defSampleRate = config->defSampleRate; |
214 | | |
215 | | /* check output format */ |
216 | 6.96k | #ifdef FIXED_POINT |
217 | 6.96k | if ((config->outputFormat < 1) || (config->outputFormat > 4)) |
218 | 76 | return 0; |
219 | | #else |
220 | | if ((config->outputFormat < 1) || (config->outputFormat > 5)) |
221 | | return 0; |
222 | | #endif |
223 | 6.88k | hDecoder->config.outputFormat = config->outputFormat; |
224 | | |
225 | 6.88k | if (config->downMatrix > 1) |
226 | 10 | return 0; |
227 | 6.87k | hDecoder->config.downMatrix = config->downMatrix; |
228 | | |
229 | | /* OK */ |
230 | 6.87k | return 1; |
231 | 6.88k | } |
232 | | |
233 | 0 | return 0; |
234 | 6.96k | } |
235 | | |
236 | | |
237 | | #if 0 |
238 | | static int latmCheck(latm_header *latm, bitfile *ld) |
239 | | { |
240 | | uint32_t good=0, bad=0, bits, m; |
241 | | |
242 | | while (ld->bytes_left) |
243 | | { |
244 | | bits = faad_latm_frame(latm, ld); |
245 | | if(bits==0xFFFFFFFF) |
246 | | bad++; |
247 | | else |
248 | | { |
249 | | good++; |
250 | | while(bits>0) |
251 | | { |
252 | | m = min(bits, 8); |
253 | | faad_getbits(ld, m); |
254 | | bits -= m; |
255 | | } |
256 | | } |
257 | | } |
258 | | |
259 | | return (good>0); |
260 | | } |
261 | | #endif |
262 | | |
263 | | long NeAACDecInit(NeAACDecHandle hpDecoder, |
264 | | unsigned char *buffer, |
265 | | unsigned long buffer_size, |
266 | | unsigned long *samplerate, |
267 | | unsigned char *channels) |
268 | 4.69k | { |
269 | 4.69k | uint32_t bits = 0; |
270 | 4.69k | bitfile ld; |
271 | 4.69k | adif_header adif; |
272 | 4.69k | adts_header adts; |
273 | 4.69k | NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder; |
274 | | |
275 | | |
276 | 4.69k | if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0)) |
277 | 5 | return -1; |
278 | | |
279 | 4.68k | adts.old_format = hDecoder->config.useOldADTSFormat; |
280 | 4.68k | hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate); |
281 | 4.68k | hDecoder->object_type = hDecoder->config.defObjectType; |
282 | 4.68k | *samplerate = get_sample_rate(hDecoder->sf_index); |
283 | 4.68k | *channels = 1; |
284 | | |
285 | 4.68k | if (buffer != NULL) |
286 | 4.68k | { |
287 | | #if 0 |
288 | | int is_latm; |
289 | | latm_header *l = &hDecoder->latm_config; |
290 | | #endif |
291 | | |
292 | 4.68k | faad_initbits(&ld, buffer, buffer_size); |
293 | | |
294 | | #if 0 |
295 | | memset(l, 0, sizeof(latm_header)); |
296 | | is_latm = latmCheck(l, &ld); |
297 | | l->inited = 0; |
298 | | l->frameLength = 0; |
299 | | faad_rewindbits(&ld); |
300 | | if(is_latm && l->ASCbits>0) |
301 | | { |
302 | | int32_t x; |
303 | | hDecoder->latm_header_present = 1; |
304 | | x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits+7)/8, samplerate, channels); |
305 | | if(x!=0) |
306 | | hDecoder->latm_header_present = 0; |
307 | | return x; |
308 | | } else |
309 | | #endif |
310 | | /* Check if an ADIF header is present */ |
311 | 4.68k | if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') && |
312 | 4.68k | (buffer[2] == 'I') && (buffer[3] == 'F')) |
313 | 109 | { |
314 | 109 | hDecoder->adif_header_present = 1; |
315 | | |
316 | 109 | get_adif_header(&adif, &ld); |
317 | 109 | faad_byte_align(&ld); |
318 | | |
319 | 109 | hDecoder->sf_index = adif.pce[0].sf_index; |
320 | 109 | hDecoder->object_type = adif.pce[0].object_type + 1; |
321 | | |
322 | 109 | *samplerate = get_sample_rate(hDecoder->sf_index); |
323 | 109 | *channels = adif.pce[0].channels; |
324 | | |
325 | 109 | memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config)); |
326 | 109 | hDecoder->pce_set = 1; |
327 | | |
328 | 109 | bits = bit2byte(faad_get_processed_bits(&ld)); |
329 | | |
330 | | /* Check if an ADTS header is present */ |
331 | 4.57k | } else if (adts_frame(&adts, &ld) == 0) { |
332 | 126 | hDecoder->adts_header_present = 1; |
333 | | |
334 | 126 | hDecoder->sf_index = adts.sf_index; |
335 | 126 | hDecoder->object_type = adts.profile + 1; |
336 | | |
337 | 126 | *samplerate = get_sample_rate(hDecoder->sf_index); |
338 | 126 | *channels = (adts.channel_configuration > 6) ? |
339 | 92 | 2 : adts.channel_configuration; |
340 | 126 | } |
341 | | |
342 | 4.68k | if (ld.error) |
343 | 0 | { |
344 | 0 | faad_endbits(&ld); |
345 | 0 | return -1; |
346 | 0 | } |
347 | 4.68k | faad_endbits(&ld); |
348 | 4.68k | } |
349 | | |
350 | 4.68k | if (!*samplerate) |
351 | 44 | return -1; |
352 | | |
353 | 4.64k | #if (defined(PS_DEC) || defined(DRM_PS)) |
354 | | /* check if we have a mono file */ |
355 | 4.64k | if (*channels == 1) |
356 | 4.45k | { |
357 | | /* upMatrix to 2 channels for implicit signalling of PS */ |
358 | 4.45k | *channels = 2; |
359 | 4.45k | } |
360 | 4.64k | #endif |
361 | | |
362 | 4.64k | hDecoder->channelConfiguration = *channels; |
363 | | |
364 | 4.64k | #ifdef SBR_DEC |
365 | | /* implicit signalling */ |
366 | 4.64k | if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) |
367 | 2.15k | { |
368 | 2.15k | *samplerate *= 2; |
369 | 2.15k | hDecoder->forceUpSampling = 1; |
370 | 2.48k | } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) { |
371 | 2.48k | hDecoder->downSampledSBR = 1; |
372 | 2.48k | } |
373 | 4.64k | #endif |
374 | | |
375 | | /* must be done before frameLength is divided by 2 for LD */ |
376 | | #ifdef SSR_DEC |
377 | | if (hDecoder->object_type == SSR) |
378 | | hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS); |
379 | | else |
380 | | #endif |
381 | 4.64k | hDecoder->fb = filter_bank_init(hDecoder->frameLength); |
382 | | |
383 | 4.64k | #ifdef LD_DEC |
384 | 4.64k | if (hDecoder->object_type == LD) |
385 | 224 | hDecoder->frameLength >>= 1; |
386 | 4.64k | #endif |
387 | | |
388 | 4.64k | if (can_decode_ot(hDecoder->object_type) < 0) |
389 | 74 | return -1; |
390 | | |
391 | 4.57k | return bits; |
392 | 4.64k | } |
393 | | |
394 | | /* Init the library using a DecoderSpecificInfo */ |
395 | | char NeAACDecInit2(NeAACDecHandle hpDecoder, |
396 | | unsigned char *pBuffer, |
397 | | unsigned long SizeOfDecoderSpecificInfo, |
398 | | unsigned long *samplerate, |
399 | | unsigned char *channels) |
400 | 2.18k | { |
401 | 2.18k | NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder; |
402 | 2.18k | int8_t rc; |
403 | 2.18k | mp4AudioSpecificConfig mp4ASC; |
404 | | |
405 | 2.18k | if((hDecoder == NULL) |
406 | 2.18k | || (pBuffer == NULL) |
407 | 2.18k | || (SizeOfDecoderSpecificInfo < 2) |
408 | 2.18k | || (samplerate == NULL) |
409 | 2.18k | || (channels == NULL)) |
410 | 2 | { |
411 | 2 | return -1; |
412 | 2 | } |
413 | | |
414 | 2.17k | hDecoder->adif_header_present = 0; |
415 | 2.17k | hDecoder->adts_header_present = 0; |
416 | | |
417 | | /* decode the audio specific config */ |
418 | 2.17k | rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC, |
419 | 2.17k | &(hDecoder->pce), hDecoder->latm_header_present); |
420 | | |
421 | | /* copy the relevant info to the decoder handle */ |
422 | 2.17k | *samplerate = mp4ASC.samplingFrequency; |
423 | 2.17k | if (mp4ASC.channelsConfiguration) |
424 | 1.85k | { |
425 | 1.85k | *channels = mp4ASC.channelsConfiguration; |
426 | 1.85k | } else { |
427 | 326 | *channels = hDecoder->pce.channels; |
428 | 326 | hDecoder->pce_set = 1; |
429 | 326 | } |
430 | 2.17k | #if (defined(PS_DEC) || defined(DRM_PS)) |
431 | | /* check if we have a mono file */ |
432 | 2.17k | if (*channels == 1) |
433 | 8 | { |
434 | | /* upMatrix to 2 channels for implicit signalling of PS */ |
435 | 8 | *channels = 2; |
436 | 8 | } |
437 | 2.17k | #endif |
438 | 2.17k | hDecoder->sf_index = mp4ASC.samplingFrequencyIndex; |
439 | 2.17k | hDecoder->object_type = mp4ASC.objectTypeIndex; |
440 | 2.17k | #ifdef ERROR_RESILIENCE |
441 | 2.17k | hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag; |
442 | 2.17k | hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag; |
443 | 2.17k | hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag; |
444 | 2.17k | #endif |
445 | 2.17k | #ifdef SBR_DEC |
446 | 2.17k | hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag; |
447 | 2.17k | hDecoder->downSampledSBR = mp4ASC.downSampledSBR; |
448 | 2.17k | if (hDecoder->config.dontUpSampleImplicitSBR == 0) |
449 | 2.17k | hDecoder->forceUpSampling = mp4ASC.forceUpSampling; |
450 | 0 | else |
451 | 0 | hDecoder->forceUpSampling = 0; |
452 | | |
453 | | /* AAC core decoder samplerate is 2 times as low */ |
454 | 2.17k | if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1) |
455 | 1.44k | { |
456 | 1.44k | hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2); |
457 | 1.44k | } |
458 | 2.17k | #endif |
459 | | |
460 | 2.17k | if (rc != 0) |
461 | 16 | { |
462 | 16 | return rc; |
463 | 16 | } |
464 | 2.16k | hDecoder->channelConfiguration = mp4ASC.channelsConfiguration; |
465 | 2.16k | if (mp4ASC.frameLengthFlag) |
466 | 1.29k | #ifdef ALLOW_SMALL_FRAMELENGTH |
467 | 1.29k | hDecoder->frameLength = 960; |
468 | | #else |
469 | | return -1; |
470 | | #endif |
471 | | |
472 | | /* must be done before frameLength is divided by 2 for LD */ |
473 | | #ifdef SSR_DEC |
474 | | if (hDecoder->object_type == SSR) |
475 | | hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS); |
476 | | else |
477 | | #endif |
478 | 2.16k | hDecoder->fb = filter_bank_init(hDecoder->frameLength); |
479 | | |
480 | 2.16k | #ifdef LD_DEC |
481 | 2.16k | if (hDecoder->object_type == LD) |
482 | 276 | hDecoder->frameLength >>= 1; |
483 | 2.16k | #endif |
484 | | |
485 | 2.16k | return 0; |
486 | 2.17k | } |
487 | | |
488 | | #ifdef DRM |
489 | | char NeAACDecInitDRM(NeAACDecHandle *hpDecoder, |
490 | | unsigned long samplerate, |
491 | | unsigned char channels) |
492 | | { |
493 | | NeAACDecStruct** hDecoder = (NeAACDecStruct**)hpDecoder; |
494 | | if (hDecoder == NULL) |
495 | | return 1; /* error */ |
496 | | |
497 | | NeAACDecClose(*hDecoder); |
498 | | |
499 | | *hDecoder = NeAACDecOpen(); |
500 | | |
501 | | /* Special object type defined for DRM */ |
502 | | (*hDecoder)->config.defObjectType = DRM_ER_LC; |
503 | | |
504 | | (*hDecoder)->config.defSampleRate = samplerate; |
505 | | #ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM |
506 | | (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */ |
507 | | (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */ |
508 | | (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */ |
509 | | #endif |
510 | | (*hDecoder)->frameLength = 960; |
511 | | (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate); |
512 | | (*hDecoder)->object_type = (*hDecoder)->config.defObjectType; |
513 | | |
514 | | if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO)) |
515 | | (*hDecoder)->channelConfiguration = 2; |
516 | | else |
517 | | (*hDecoder)->channelConfiguration = 1; |
518 | | |
519 | | #ifdef SBR_DEC |
520 | | if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO)) |
521 | | (*hDecoder)->sbr_present_flag = 0; |
522 | | else |
523 | | (*hDecoder)->sbr_present_flag = 1; |
524 | | #endif |
525 | | |
526 | | (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength); |
527 | | |
528 | | return 0; |
529 | | } |
530 | | #endif |
531 | | |
532 | | void NeAACDecClose(NeAACDecHandle hpDecoder) |
533 | 6.96k | { |
534 | 6.96k | uint8_t i; |
535 | 6.96k | NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder; |
536 | | |
537 | 6.96k | if (hDecoder == NULL) |
538 | 0 | return; |
539 | | |
540 | | #ifdef PROFILE |
541 | | printf("AAC decoder total: %I64d cycles\n", hDecoder->cycles); |
542 | | printf("requant: %I64d cycles\n", hDecoder->requant_cycles); |
543 | | printf("spectral_data: %I64d cycles\n", hDecoder->spectral_cycles); |
544 | | printf("scalefactors: %I64d cycles\n", hDecoder->scalefac_cycles); |
545 | | printf("output: %I64d cycles\n", hDecoder->output_cycles); |
546 | | #endif |
547 | | |
548 | 452k | for (i = 0; i < MAX_CHANNELS; i++) |
549 | 445k | { |
550 | 445k | if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]); |
551 | 445k | if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]); |
552 | | #ifdef SSR_DEC |
553 | | if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]); |
554 | | if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]); |
555 | | #endif |
556 | | #ifdef MAIN_DEC |
557 | | if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]); |
558 | | #endif |
559 | 445k | #ifdef LTP_DEC |
560 | 445k | if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]); |
561 | 445k | #endif |
562 | 445k | } |
563 | | |
564 | | #ifdef SSR_DEC |
565 | | if (hDecoder->object_type == SSR) |
566 | | ssr_filter_bank_end(hDecoder->fb); |
567 | | else |
568 | | #endif |
569 | 6.96k | filter_bank_end(hDecoder->fb); |
570 | | |
571 | 6.96k | drc_end(hDecoder->drc); |
572 | | |
573 | 6.96k | if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer); |
574 | | |
575 | 6.96k | #ifdef SBR_DEC |
576 | 341k | for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++) |
577 | 334k | { |
578 | 334k | if (hDecoder->sbr[i]) |
579 | 119k | sbrDecodeEnd(hDecoder->sbr[i]); |
580 | 334k | } |
581 | 6.96k | #endif |
582 | | |
583 | 6.96k | if (hDecoder) faad_free(hDecoder); |
584 | 6.96k | } |
585 | | |
586 | | void NeAACDecPostSeekReset(NeAACDecHandle hpDecoder, long frame) |
587 | 4.31k | { |
588 | 4.31k | NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder; |
589 | 4.31k | if (hDecoder) |
590 | 4.31k | { |
591 | 4.31k | hDecoder->postSeekResetFlag = 1; |
592 | | |
593 | 4.31k | if (frame != -1) |
594 | 4.31k | hDecoder->frame = frame; |
595 | 4.31k | } |
596 | 4.31k | } |
597 | | |
598 | | static void create_channel_config(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo) |
599 | 2.71k | { |
600 | 2.71k | hInfo->num_front_channels = 0; |
601 | 2.71k | hInfo->num_side_channels = 0; |
602 | 2.71k | hInfo->num_back_channels = 0; |
603 | 2.71k | hInfo->num_lfe_channels = 0; |
604 | 2.71k | memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t)); |
605 | | |
606 | 2.71k | if (hDecoder->downMatrix) |
607 | 104 | { |
608 | 104 | hInfo->num_front_channels = 2; |
609 | 104 | hInfo->channel_position[0] = FRONT_CHANNEL_LEFT; |
610 | 104 | hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT; |
611 | 104 | return; |
612 | 104 | } |
613 | | |
614 | | /* check if there is a PCE */ |
615 | | /* TODO: why CPE flag is ignored? */ |
616 | 2.61k | if (hDecoder->pce_set) |
617 | 64 | { |
618 | 64 | uint8_t i, chpos = 0; |
619 | 64 | uint8_t chdir, back_center = 0; |
620 | | |
621 | 64 | hInfo->num_front_channels = hDecoder->pce.num_front_channels; |
622 | 64 | hInfo->num_side_channels = hDecoder->pce.num_side_channels; |
623 | 64 | hInfo->num_back_channels = hDecoder->pce.num_back_channels; |
624 | 64 | hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels; |
625 | 64 | chdir = hInfo->num_front_channels; |
626 | 64 | if (chdir & 1) |
627 | 26 | { |
628 | 26 | #if (defined(PS_DEC) || defined(DRM_PS)) |
629 | 26 | if (hInfo->num_front_channels == 1 && |
630 | 26 | hInfo->num_side_channels == 0 && |
631 | 26 | hInfo->num_back_channels == 0 && |
632 | 26 | hInfo->num_lfe_channels == 0) |
633 | 3 | { |
634 | | /* When PS is enabled output is always stereo */ |
635 | 3 | hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT; |
636 | 3 | hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT; |
637 | 3 | } else |
638 | 23 | #endif |
639 | 23 | hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER; |
640 | 26 | chdir--; |
641 | 26 | } |
642 | 398 | for (i = 0; i < chdir; i++) |
643 | 334 | { |
644 | 334 | hInfo->channel_position[chpos++] = |
645 | 334 | (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT; |
646 | 334 | } |
647 | | |
648 | 423 | for (i = 0; i < hInfo->num_side_channels; i++) |
649 | 359 | { |
650 | 359 | hInfo->channel_position[chpos++] = |
651 | 359 | (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT; |
652 | 359 | } |
653 | | |
654 | 64 | chdir = hInfo->num_back_channels; |
655 | 64 | if (chdir & 1) |
656 | 17 | { |
657 | 17 | back_center = 1; |
658 | 17 | chdir--; |
659 | 17 | } |
660 | 296 | for (i = 0; i < chdir; i++) |
661 | 232 | { |
662 | 232 | hInfo->channel_position[chpos++] = |
663 | 232 | (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT; |
664 | 232 | } |
665 | 64 | if (back_center) |
666 | 17 | { |
667 | 17 | hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER; |
668 | 17 | } |
669 | | |
670 | 126 | for (i = 0; i < hInfo->num_lfe_channels; i++) |
671 | 62 | { |
672 | 62 | hInfo->channel_position[chpos++] = LFE_CHANNEL; |
673 | 62 | } |
674 | | |
675 | 2.54k | } else { |
676 | 2.54k | switch (hDecoder->channelConfiguration) |
677 | 2.54k | { |
678 | 1 | case 1: |
679 | 1 | #if (defined(PS_DEC) || defined(DRM_PS)) |
680 | | /* When PS is enabled output is always stereo */ |
681 | 1 | hInfo->num_front_channels = 2; |
682 | 1 | hInfo->channel_position[0] = FRONT_CHANNEL_LEFT; |
683 | 1 | hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT; |
684 | | #else |
685 | | hInfo->num_front_channels = 1; |
686 | | hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; |
687 | | #endif |
688 | 1 | break; |
689 | 1.31k | case 2: |
690 | 1.31k | hInfo->num_front_channels = 2; |
691 | 1.31k | hInfo->channel_position[0] = FRONT_CHANNEL_LEFT; |
692 | 1.31k | hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT; |
693 | 1.31k | break; |
694 | 168 | case 3: |
695 | 168 | hInfo->num_front_channels = 3; |
696 | 168 | hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; |
697 | 168 | hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; |
698 | 168 | hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; |
699 | 168 | break; |
700 | 295 | case 4: |
701 | 295 | hInfo->num_front_channels = 3; |
702 | 295 | hInfo->num_back_channels = 1; |
703 | 295 | hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; |
704 | 295 | hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; |
705 | 295 | hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; |
706 | 295 | hInfo->channel_position[3] = BACK_CHANNEL_CENTER; |
707 | 295 | break; |
708 | 180 | case 5: |
709 | 180 | hInfo->num_front_channels = 3; |
710 | 180 | hInfo->num_back_channels = 2; |
711 | 180 | hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; |
712 | 180 | hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; |
713 | 180 | hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; |
714 | 180 | hInfo->channel_position[3] = BACK_CHANNEL_LEFT; |
715 | 180 | hInfo->channel_position[4] = BACK_CHANNEL_RIGHT; |
716 | 180 | break; |
717 | 141 | case 6: |
718 | 141 | hInfo->num_front_channels = 3; |
719 | 141 | hInfo->num_back_channels = 2; |
720 | 141 | hInfo->num_lfe_channels = 1; |
721 | 141 | hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; |
722 | 141 | hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; |
723 | 141 | hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; |
724 | 141 | hInfo->channel_position[3] = BACK_CHANNEL_LEFT; |
725 | 141 | hInfo->channel_position[4] = BACK_CHANNEL_RIGHT; |
726 | 141 | hInfo->channel_position[5] = LFE_CHANNEL; |
727 | 141 | break; |
728 | 394 | case 7: |
729 | 394 | hInfo->num_front_channels = 3; |
730 | 394 | hInfo->num_side_channels = 2; |
731 | 394 | hInfo->num_back_channels = 2; |
732 | 394 | hInfo->num_lfe_channels = 1; |
733 | 394 | hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; |
734 | 394 | hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; |
735 | 394 | hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; |
736 | 394 | hInfo->channel_position[3] = SIDE_CHANNEL_LEFT; |
737 | 394 | hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT; |
738 | 394 | hInfo->channel_position[5] = BACK_CHANNEL_LEFT; |
739 | 394 | hInfo->channel_position[6] = BACK_CHANNEL_RIGHT; |
740 | 394 | hInfo->channel_position[7] = LFE_CHANNEL; |
741 | 394 | break; |
742 | 59 | default: /* channelConfiguration == 0 || channelConfiguration > 7 */ |
743 | 59 | { |
744 | 59 | uint8_t i; |
745 | 59 | uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe; |
746 | 59 | if (ch & 1) /* there's either a center front or a center back channel */ |
747 | 32 | { |
748 | 32 | uint8_t ch1 = (ch-1)/2; |
749 | 32 | if (hDecoder->first_syn_ele == ID_SCE) |
750 | 19 | { |
751 | 19 | hInfo->num_front_channels = ch1 + 1; |
752 | 19 | hInfo->num_back_channels = ch1; |
753 | 19 | hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; |
754 | 77 | for (i = 1; i <= ch1; i+=2) |
755 | 58 | { |
756 | 58 | hInfo->channel_position[i] = FRONT_CHANNEL_LEFT; |
757 | 58 | hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT; |
758 | 58 | } |
759 | 77 | for (i = ch1+1; i < ch; i+=2) |
760 | 58 | { |
761 | 58 | hInfo->channel_position[i] = BACK_CHANNEL_LEFT; |
762 | 58 | hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT; |
763 | 58 | } |
764 | 19 | } else { |
765 | 13 | hInfo->num_front_channels = ch1; |
766 | 13 | hInfo->num_back_channels = ch1 + 1; |
767 | 71 | for (i = 0; i < ch1; i+=2) |
768 | 58 | { |
769 | 58 | hInfo->channel_position[i] = FRONT_CHANNEL_LEFT; |
770 | 58 | hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT; |
771 | 58 | } |
772 | 71 | for (i = ch1; i < ch-1; i+=2) |
773 | 58 | { |
774 | 58 | hInfo->channel_position[i] = BACK_CHANNEL_LEFT; |
775 | 58 | hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT; |
776 | 58 | } |
777 | 13 | hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER; |
778 | 13 | } |
779 | 32 | } else { |
780 | 27 | uint8_t ch1 = (ch)/2; |
781 | 27 | hInfo->num_front_channels = ch1; |
782 | 27 | hInfo->num_back_channels = ch1; |
783 | 27 | if (ch1 & 1) |
784 | 13 | { |
785 | 13 | hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; |
786 | 66 | for (i = 1; i <= ch1; i+=2) |
787 | 53 | { |
788 | 53 | hInfo->channel_position[i] = FRONT_CHANNEL_LEFT; |
789 | 53 | hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT; |
790 | 53 | } |
791 | 53 | for (i = ch1+1; i < ch-1; i+=2) |
792 | 40 | { |
793 | 40 | hInfo->channel_position[i] = BACK_CHANNEL_LEFT; |
794 | 40 | hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT; |
795 | 40 | } |
796 | 13 | hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER; |
797 | 14 | } else { |
798 | 50 | for (i = 0; i < ch1; i+=2) |
799 | 36 | { |
800 | 36 | hInfo->channel_position[i] = FRONT_CHANNEL_LEFT; |
801 | 36 | hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT; |
802 | 36 | } |
803 | 50 | for (i = ch1; i < ch; i+=2) |
804 | 36 | { |
805 | 36 | hInfo->channel_position[i] = BACK_CHANNEL_LEFT; |
806 | 36 | hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT; |
807 | 36 | } |
808 | 14 | } |
809 | 27 | } |
810 | 59 | hInfo->num_lfe_channels = hDecoder->has_lfe; |
811 | 110 | for (i = ch; i < hDecoder->fr_channels; i++) |
812 | 51 | { |
813 | 51 | hInfo->channel_position[i] = LFE_CHANNEL; |
814 | 51 | } |
815 | 59 | } |
816 | 59 | break; |
817 | 2.54k | } |
818 | 2.54k | } |
819 | 2.61k | } |
820 | | |
821 | | void* NeAACDecDecode(NeAACDecHandle hpDecoder, |
822 | | NeAACDecFrameInfo *hInfo, |
823 | | unsigned char *buffer, |
824 | | unsigned long buffer_size) |
825 | 11.2k | { |
826 | 11.2k | NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder; |
827 | 11.2k | return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0); |
828 | 11.2k | } |
829 | | |
830 | | void* NeAACDecDecode2(NeAACDecHandle hpDecoder, |
831 | | NeAACDecFrameInfo *hInfo, |
832 | | unsigned char *buffer, |
833 | | unsigned long buffer_size, |
834 | | void **sample_buffer, |
835 | | unsigned long sample_buffer_size) |
836 | 2.19k | { |
837 | 2.19k | NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder; |
838 | 2.19k | if ((sample_buffer_size == 0) || (sample_buffer == NULL) || (*sample_buffer == NULL)) |
839 | 13 | { |
840 | 13 | hInfo->error = 27; |
841 | 13 | return NULL; |
842 | 13 | } |
843 | | |
844 | 2.18k | return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, |
845 | 2.18k | sample_buffer, sample_buffer_size); |
846 | 2.19k | } |
847 | | |
848 | | static void* aac_frame_decode(NeAACDecStruct *hDecoder, |
849 | | NeAACDecFrameInfo *hInfo, |
850 | | unsigned char *buffer, |
851 | | unsigned long buffer_size, |
852 | | void **sample_buffer2, |
853 | | unsigned long sample_buffer_size) |
854 | 13.4k | { |
855 | 13.4k | uint16_t i; |
856 | 13.4k | uint8_t channels = 0; |
857 | 13.4k | uint8_t output_channels = 0; |
858 | 13.4k | bitfile ld; |
859 | 13.4k | uint32_t bitsconsumed; |
860 | 13.4k | uint16_t frame_len; |
861 | 13.4k | void *sample_buffer; |
862 | | #if 0 |
863 | | uint32_t startbit=0, endbit=0, payload_bits=0; |
864 | | #endif |
865 | 13.4k | uint32_t required_buffer_size=0; |
866 | | |
867 | | #ifdef PROFILE |
868 | | int64_t count = faad_get_ts(); |
869 | | #endif |
870 | | |
871 | | /* safety checks */ |
872 | 13.4k | if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL)) |
873 | 0 | { |
874 | 0 | return NULL; |
875 | 0 | } |
876 | | |
877 | | #if 0 |
878 | | printf("%d\n", buffer_size*8); |
879 | | #endif |
880 | | |
881 | 13.4k | frame_len = hDecoder->frameLength; |
882 | | |
883 | | |
884 | 13.4k | memset(hInfo, 0, sizeof(NeAACDecFrameInfo)); |
885 | 13.4k | memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0])); |
886 | | |
887 | | #ifdef USE_TIME_LIMIT |
888 | | if ((TIME_LIMIT * get_sample_rate(hDecoder->sf_index)) > hDecoder->TL_count) |
889 | | { |
890 | | hDecoder->TL_count += 1024; |
891 | | } else { |
892 | | hInfo->error = (NUM_ERROR_MESSAGES-1); |
893 | | goto error; |
894 | | } |
895 | | #endif |
896 | | |
897 | | |
898 | | /* check for some common metadata tag types in the bitstream |
899 | | * No need to return an error |
900 | | */ |
901 | | /* ID3 */ |
902 | 13.4k | if (buffer_size >= 128) |
903 | 1.49k | { |
904 | 1.49k | if (memcmp(buffer, "TAG", 3) == 0) |
905 | 3 | { |
906 | | /* found it */ |
907 | 3 | hInfo->bytesconsumed = 128; /* 128 bytes fixed size */ |
908 | | /* no error, but no output either */ |
909 | 3 | return NULL; |
910 | 3 | } |
911 | 1.49k | } |
912 | | |
913 | | |
914 | | /* initialize the bitstream */ |
915 | 13.4k | faad_initbits(&ld, buffer, buffer_size); |
916 | 13.4k | if (ld.error != 0) |
917 | 5.05k | return NULL; |
918 | | |
919 | | #if 0 |
920 | | { |
921 | | int i; |
922 | | for (i = 0; i < ((buffer_size+3)>>2); i++) |
923 | | { |
924 | | uint8_t *buf; |
925 | | uint32_t temp = 0; |
926 | | buf = faad_getbitbuffer(&ld, 32); |
927 | | //temp = getdword((void*)buf); |
928 | | temp = *((uint32_t*)buf); |
929 | | printf("0x%.8X\n", temp); |
930 | | free(buf); |
931 | | } |
932 | | faad_endbits(&ld); |
933 | | faad_initbits(&ld, buffer, buffer_size); |
934 | | } |
935 | | #endif |
936 | | |
937 | | #if 0 |
938 | | if(hDecoder->latm_header_present) |
939 | | { |
940 | | payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld); |
941 | | startbit = faad_get_processed_bits(&ld); |
942 | | if(payload_bits == -1U) |
943 | | { |
944 | | hInfo->error = 1; |
945 | | goto error; |
946 | | } |
947 | | } |
948 | | #endif |
949 | | |
950 | | #ifdef DRM |
951 | | if (hDecoder->object_type == DRM_ER_LC) |
952 | | { |
953 | | /* We do not support stereo right now */ |
954 | | if (0) //(hDecoder->channelConfiguration == 2) |
955 | | { |
956 | | hInfo->error = 28; // Throw CRC error |
957 | | goto error; |
958 | | } |
959 | | |
960 | | faad_getbits(&ld, 8 |
961 | | DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC")); |
962 | | } |
963 | | #endif |
964 | | |
965 | 8.35k | if (hDecoder->adts_header_present) |
966 | 178 | { |
967 | 178 | adts_header adts; |
968 | | |
969 | 178 | adts.old_format = hDecoder->config.useOldADTSFormat; |
970 | 178 | if ((hInfo->error = adts_frame(&adts, &ld)) > 0) |
971 | 38 | goto error; |
972 | | |
973 | | /* MPEG2 does byte_alignment() here, |
974 | | * but ADTS header is always multiple of 8 bits in MPEG2 |
975 | | * so not needed to actually do it. |
976 | | */ |
977 | 178 | } |
978 | | |
979 | | #ifdef ANALYSIS |
980 | | dbg_count = 0; |
981 | | #endif |
982 | | |
983 | | /* decode the complete bitstream */ |
984 | | #ifdef DRM |
985 | | if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC) |
986 | | { |
987 | | DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc); |
988 | | } else { |
989 | | #endif |
990 | 8.31k | raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc); |
991 | | #ifdef DRM |
992 | | } |
993 | | #endif |
994 | | |
995 | | #if 0 |
996 | | if(hDecoder->latm_header_present) |
997 | | { |
998 | | endbit = faad_get_processed_bits(&ld); |
999 | | if(endbit-startbit > payload_bits) |
1000 | | fprintf(stderr, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n", |
1001 | | endbit-startbit, payload_bits); |
1002 | | if(hDecoder->latm_config.otherDataLenBits > 0) |
1003 | | faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits); |
1004 | | faad_byte_align(&ld); |
1005 | | } |
1006 | | #endif |
1007 | | |
1008 | 8.31k | channels = hDecoder->fr_channels; |
1009 | | |
1010 | 8.31k | if (hInfo->error > 0) |
1011 | 5.56k | goto error; |
1012 | | |
1013 | | /* safety check */ |
1014 | 2.75k | if (channels == 0 || channels > MAX_CHANNELS) |
1015 | 36 | { |
1016 | | /* invalid number of channels */ |
1017 | 36 | hInfo->error = 12; |
1018 | 36 | goto error; |
1019 | 36 | } |
1020 | | |
1021 | | /* no more bit reading after this */ |
1022 | 2.71k | bitsconsumed = faad_get_processed_bits(&ld); |
1023 | 2.71k | hInfo->bytesconsumed = bit2byte(bitsconsumed); |
1024 | 2.71k | if (ld.error) |
1025 | 0 | { |
1026 | 0 | hInfo->error = 14; |
1027 | 0 | goto error; |
1028 | 0 | } |
1029 | 2.71k | faad_endbits(&ld); |
1030 | | |
1031 | | |
1032 | 2.71k | if (!hDecoder->adts_header_present && !hDecoder->adif_header_present |
1033 | | #if 0 |
1034 | | && !hDecoder->latm_header_present |
1035 | | #endif |
1036 | 2.71k | ) |
1037 | 2.66k | { |
1038 | 2.66k | if (hDecoder->channelConfiguration == 0) |
1039 | 57 | hDecoder->channelConfiguration = channels; |
1040 | | |
1041 | 2.66k | if (channels == 8) /* 7.1 */ |
1042 | 382 | hDecoder->channelConfiguration = 7; |
1043 | 2.66k | if (channels == 7) /* not a standard channelConfiguration */ |
1044 | 24 | hDecoder->channelConfiguration = 0; |
1045 | 2.66k | } |
1046 | | |
1047 | 2.71k | if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix) |
1048 | 101 | { |
1049 | 101 | hDecoder->downMatrix = 1; |
1050 | 101 | output_channels = 2; |
1051 | 2.61k | } else { |
1052 | 2.61k | output_channels = channels; |
1053 | 2.61k | } |
1054 | | |
1055 | 2.71k | #if (defined(PS_DEC) || defined(DRM_PS)) |
1056 | 2.71k | hDecoder->upMatrix = 0; |
1057 | | /* check if we have a mono file */ |
1058 | 2.71k | if (output_channels == 1) |
1059 | 68 | { |
1060 | | /* upMatrix to 2 channels for implicit signalling of PS */ |
1061 | 68 | hDecoder->upMatrix = 1; |
1062 | 68 | output_channels = 2; |
1063 | 68 | } |
1064 | 2.71k | #endif |
1065 | | |
1066 | | /* Make a channel configuration based on either a PCE or a channelConfiguration */ |
1067 | 2.71k | if (!hDecoder->downMatrix && hDecoder->pce_set) |
1068 | 67 | { |
1069 | | /* In some codepath program_config_element result is ignored. */ |
1070 | 67 | if (hDecoder->pce.channels > MAX_CHANNELS) |
1071 | 3 | { |
1072 | 3 | hInfo->error = 22; |
1073 | 3 | return NULL; |
1074 | 3 | } |
1075 | 67 | } |
1076 | 2.71k | create_channel_config(hDecoder, hInfo); |
1077 | | |
1078 | | /* number of samples in this frame */ |
1079 | 2.71k | hInfo->samples = frame_len*output_channels; |
1080 | | /* number of channels in this frame */ |
1081 | 2.71k | hInfo->channels = output_channels; |
1082 | | /* samplerate */ |
1083 | 2.71k | hInfo->samplerate = get_sample_rate(hDecoder->sf_index); |
1084 | | /* object type */ |
1085 | 2.71k | hInfo->object_type = hDecoder->object_type; |
1086 | | /* sbr */ |
1087 | 2.71k | hInfo->sbr = NO_SBR; |
1088 | | /* header type */ |
1089 | 2.71k | hInfo->header_type = RAW; |
1090 | 2.71k | if (hDecoder->adif_header_present) |
1091 | 0 | hInfo->header_type = ADIF; |
1092 | 2.71k | if (hDecoder->adts_header_present) |
1093 | 56 | hInfo->header_type = ADTS; |
1094 | | #if 0 |
1095 | | if (hDecoder->latm_header_present) |
1096 | | hInfo->header_type = LATM; |
1097 | | #endif |
1098 | 2.71k | #if (defined(PS_DEC) || defined(DRM_PS)) |
1099 | 2.71k | hInfo->ps = hDecoder->ps_used_global; |
1100 | 2.71k | #endif |
1101 | | |
1102 | | /* check if frame has channel elements */ |
1103 | 2.71k | if (channels == 0) |
1104 | 0 | { |
1105 | 0 | hDecoder->frame++; |
1106 | 0 | return NULL; |
1107 | 0 | } |
1108 | | |
1109 | 2.71k | { |
1110 | 2.71k | static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t), |
1111 | 2.71k | sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t), |
1112 | 2.71k | sizeof(int16_t), sizeof(int16_t), 0, 0, 0 |
1113 | 2.71k | }; |
1114 | 2.71k | uint8_t stride = str[hDecoder->config.outputFormat-1]; |
1115 | 2.71k | #ifdef SBR_DEC |
1116 | 2.71k | if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1)) |
1117 | 1.50k | { |
1118 | 1.50k | stride = 2 * stride; |
1119 | 1.50k | } |
1120 | 2.71k | #endif |
1121 | 2.71k | required_buffer_size = frame_len*output_channels*stride; |
1122 | 2.71k | } |
1123 | | |
1124 | | /* check if we want to use internal sample_buffer */ |
1125 | 2.71k | if (sample_buffer_size == 0) |
1126 | 2.17k | { |
1127 | | /* allocate the buffer for the final samples */ |
1128 | 2.17k | if (hDecoder->sample_buffer_size != required_buffer_size) |
1129 | 1.82k | { |
1130 | 1.82k | if (hDecoder->sample_buffer) |
1131 | 17 | faad_free(hDecoder->sample_buffer); |
1132 | 1.82k | hDecoder->sample_buffer = NULL; |
1133 | 1.82k | hDecoder->sample_buffer = faad_malloc(required_buffer_size); |
1134 | 1.82k | hDecoder->sample_buffer_size = required_buffer_size; |
1135 | 1.82k | } |
1136 | 2.17k | } else if (sample_buffer_size < required_buffer_size) { |
1137 | | /* provided sample buffer is not big enough */ |
1138 | 249 | hInfo->error = 27; |
1139 | 249 | return NULL; |
1140 | 249 | } |
1141 | | |
1142 | 2.46k | if (sample_buffer_size == 0) |
1143 | 2.17k | { |
1144 | 2.17k | sample_buffer = hDecoder->sample_buffer; |
1145 | 2.17k | } else { |
1146 | 292 | sample_buffer = *sample_buffer2; |
1147 | 292 | } |
1148 | | |
1149 | 2.46k | #ifdef SBR_DEC |
1150 | 2.46k | if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) |
1151 | 1.65k | { |
1152 | 1.65k | uint8_t ele; |
1153 | | |
1154 | | /* this data is different when SBR is used or when the data is upsampled */ |
1155 | 1.65k | if (!hDecoder->downSampledSBR) |
1156 | 1.32k | { |
1157 | 1.32k | frame_len *= 2; |
1158 | 1.32k | hInfo->samples *= 2; |
1159 | 1.32k | hInfo->samplerate *= 2; |
1160 | 1.32k | } |
1161 | | |
1162 | | /* check if every element was provided with SBR data */ |
1163 | 7.63k | for (ele = 0; ele < hDecoder->fr_ch_ele; ele++) |
1164 | 5.97k | { |
1165 | 5.97k | if (hDecoder->sbr[ele] == NULL) |
1166 | 3 | { |
1167 | 3 | hInfo->error = 25; |
1168 | 3 | goto error; |
1169 | 3 | } |
1170 | 5.97k | } |
1171 | | |
1172 | | /* sbr */ |
1173 | 1.65k | if (hDecoder->sbr_present_flag == 1) |
1174 | 1.19k | { |
1175 | 1.19k | hInfo->object_type = HE_AAC; |
1176 | 1.19k | hInfo->sbr = SBR_UPSAMPLED; |
1177 | 1.19k | } else { |
1178 | 456 | hInfo->sbr = NO_SBR_UPSAMPLED; |
1179 | 456 | } |
1180 | 1.65k | if (hDecoder->downSampledSBR) |
1181 | 333 | { |
1182 | 333 | hInfo->sbr = SBR_DOWNSAMPLED; |
1183 | 333 | } |
1184 | 1.65k | } |
1185 | 2.46k | #endif |
1186 | | |
1187 | | |
1188 | 2.46k | sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer, |
1189 | 2.46k | output_channels, frame_len, hDecoder->config.outputFormat); |
1190 | | |
1191 | | |
1192 | | #ifdef DRM |
1193 | | //conceal_output(hDecoder, frame_len, output_channels, sample_buffer); |
1194 | | #endif |
1195 | | |
1196 | | |
1197 | 2.46k | hDecoder->postSeekResetFlag = 0; |
1198 | | |
1199 | 2.46k | hDecoder->frame++; |
1200 | 2.46k | #ifdef LD_DEC |
1201 | 2.46k | if (hDecoder->object_type != LD) |
1202 | 2.17k | { |
1203 | 2.17k | #endif |
1204 | 2.17k | if (hDecoder->frame <= 1) |
1205 | 929 | hInfo->samples = 0; |
1206 | 2.17k | #ifdef LD_DEC |
1207 | 2.17k | } else { |
1208 | | /* LD encoders will give lower delay */ |
1209 | 290 | if (hDecoder->frame <= 0) |
1210 | 0 | hInfo->samples = 0; |
1211 | 290 | } |
1212 | 2.46k | #endif |
1213 | | |
1214 | | /* cleanup */ |
1215 | | #ifdef ANALYSIS |
1216 | | fflush(stdout); |
1217 | | #endif |
1218 | | |
1219 | | #ifdef PROFILE |
1220 | | count = faad_get_ts() - count; |
1221 | | hDecoder->cycles += count; |
1222 | | #endif |
1223 | | |
1224 | 2.46k | return sample_buffer; |
1225 | | |
1226 | 5.64k | error: |
1227 | | |
1228 | | /* reset filterbank state */ |
1229 | 366k | for (i = 0; i < MAX_CHANNELS; i++) |
1230 | 360k | { |
1231 | 360k | if (hDecoder->fb_intermed[i] != NULL) |
1232 | 188k | { |
1233 | 188k | memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t)); |
1234 | 188k | } |
1235 | 360k | } |
1236 | 5.64k | #ifdef SBR_DEC |
1237 | 276k | for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++) |
1238 | 270k | { |
1239 | 270k | if (hDecoder->sbr[i] != NULL) |
1240 | 134k | { |
1241 | 134k | sbrReset(hDecoder->sbr[i]); |
1242 | 134k | } |
1243 | 270k | } |
1244 | 5.64k | #endif |
1245 | | |
1246 | | |
1247 | 5.64k | faad_endbits(&ld); |
1248 | | |
1249 | | /* cleanup */ |
1250 | | #ifdef ANALYSIS |
1251 | | fflush(stdout); |
1252 | | #endif |
1253 | | |
1254 | 5.64k | return NULL; |
1255 | 2.46k | } |