summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libfaad/decoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libfaad/decoder.c')
-rw-r--r--lib/rbcodec/codecs/libfaad/decoder.c1029
1 files changed, 1029 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libfaad/decoder.c b/lib/rbcodec/codecs/libfaad/decoder.c
new file mode 100644
index 0000000000..d68d093b0b
--- /dev/null
+++ b/lib/rbcodec/codecs/libfaad/decoder.c
@@ -0,0 +1,1029 @@
1/*
2** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3** Copyright (C) 2003-2004 M. Bakker, Ahead Software 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** Commercial non-GPL licensing of this software is possible.
23** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
24**
25** $Id$
26**/
27
28#include "common.h"
29#include "structs.h"
30
31#include <stdlib.h>
32#include <string.h>
33
34#include "decoder.h"
35#include "mp4.h"
36#include "syntax.h"
37#include "error.h"
38/* rockbox: not used
39#include "output.h"
40*/
41#include "filtbank.h"
42#include "drc.h"
43#ifdef SBR_DEC
44#include "sbr_dec.h"
45#include "sbr_syntax.h"
46#endif
47#ifdef SSR_DEC
48#include "ssr.h"
49#endif
50
51/* Globals */
52#ifdef ANALYSIS
53uint16_t dbg_count;
54#endif
55
56/* static variables */
57static NeAACDecStruct s_AACDec;
58static real_t s_fb_intermed [MAX_CHANNELS][1*FRAME_LEN] IBSS_ATTR_FAAD_LARGE_IRAM MEM_ALIGN_ATTR;
59static real_t s_time_buf_1024[MAX_CHANNELS][1*FRAME_LEN] IBSS_ATTR_FAAD_LARGE_IRAM MEM_ALIGN_ATTR;
60#ifdef SBR_DEC
61#ifdef FAAD_STATIC_ALLOC
62static real_t s_time_buf_2048[MAX_CHANNELS][2*FRAME_LEN] MEM_ALIGN_ATTR;
63#endif
64#endif
65#ifdef SSR_DEC
66static real_t s_ssr_overlap [MAX_CHANNELS][2*FRAME_LEN] MEM_ALIGN_ATTR;
67static real_t s_prev_fmd [MAX_CHANNELS][2*FRAME_LEN] MEM_ALIGN_ATTR;
68#endif
69#ifdef MAIN_DEC
70static pred_state s_pred_stat[MAX_CHANNELS][1*FRAME_LEN] MEM_ALIGN_ATTR;
71#endif
72#ifdef LTP_DEC
73static int16_t s_lt_pred_stat[MAX_CHANNELS][4*FRAME_LEN] MEM_ALIGN_ATTR;
74#endif
75
76
77/* static function declarations */
78static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
79 uint8_t *buffer, uint32_t buffer_size);
80/* not used by rockbox
81static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo);
82*/
83
84char* NEAACDECAPI NeAACDecGetErrorMessage(uint8_t errcode)
85{
86 if (errcode >= NUM_ERROR_MESSAGES)
87 return NULL;
88 return err_msg[errcode];
89}
90
91/* rockbox: not used */
92#if 0
93uint32_t NEAACDECAPI NeAACDecGetCapabilities(void)
94{
95 uint32_t cap = 0;
96
97 /* can't do without it */
98 cap += LC_DEC_CAP;
99
100#ifdef MAIN_DEC
101 cap += MAIN_DEC_CAP;
102#endif
103#ifdef LTP_DEC
104 cap += LTP_DEC_CAP;
105#endif
106#ifdef LD_DEC
107 cap += LD_DEC_CAP;
108#endif
109#ifdef ERROR_RESILIENCE
110 cap += ERROR_RESILIENCE_CAP;
111#endif
112#ifdef FIXED_POINT
113 cap += FIXED_POINT_CAP;
114#endif
115
116 return cap;
117}
118#endif
119
120NeAACDecHandle NEAACDECAPI NeAACDecOpen(void)
121{
122 uint8_t i;
123 NeAACDecHandle hDecoder = NULL;
124
125 #if defined(CPU_COLDFIRE)
126 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
127 #endif
128
129 hDecoder = &s_AACDec;
130
131 memset(hDecoder , 0, sizeof(NeAACDecStruct));
132 memset(s_fb_intermed, 0, sizeof(s_fb_intermed));
133#ifdef SSR_DEC
134 memset(s_ssr_overlap, 0, sizeof(s_ssr_overlap));
135 memset(s_prev_fmd , 0, sizeof(s_prev_fmd));
136#endif
137#ifdef LTP_DEC
138 memset(s_lt_pred_stat, 0, sizeof(s_s_lt_pred_statpred_stat));
139#endif
140
141 hDecoder->config.outputFormat = FAAD_FMT_16BIT;
142 hDecoder->config.defObjectType = MAIN;
143 hDecoder->config.defSampleRate = 44100;
144 hDecoder->config.downMatrix = 0;
145 hDecoder->adts_header_present = 0;
146 hDecoder->adif_header_present = 0;
147#ifdef ERROR_RESILIENCE
148 hDecoder->aacSectionDataResilienceFlag = 0;
149 hDecoder->aacScalefactorDataResilienceFlag = 0;
150 hDecoder->aacSpectralDataResilienceFlag = 0;
151#endif
152 hDecoder->frameLength = FRAME_LEN;
153
154 hDecoder->frame = 0;
155
156 for (i = 0; i < MAX_CHANNELS; i++)
157 {
158 hDecoder->window_shape_prev[i] = 0;
159 hDecoder->time_out[i] = NULL;
160 hDecoder->fb_intermed[i] = s_fb_intermed[i];
161#ifdef SSR_DEC
162 hDecoder->ssr_overlap[i] = s_ssr_overlap[i];
163 hDecoder->prev_fmd[i] = s_prev_fmd[i];
164 for (int k = 0; k < 2048; k++)
165 hDecoder->prev_fmd[i][k] = REAL_CONST(-1);
166#endif
167#ifdef MAIN_DEC
168 hDecoder->pred_stat[i] = s_pred_stat[i];
169 reset_all_predictors(hDecoder->pred_stat[channel], FRAME_LEN);
170#endif
171#ifdef LTP_DEC
172 hDecoder->ltp_lag[i] = 0;
173 hDecoder->lt_pred_stat[i] = s_lt_pred_stat[i];
174#endif
175 }
176
177#ifdef SBR_DEC
178 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
179 {
180 hDecoder->sbr[i] = NULL;
181 }
182#endif
183
184 hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
185
186 return hDecoder;
187}
188
189NeAACDecConfigurationPtr NEAACDECAPI NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder)
190{
191 if (hDecoder)
192 {
193 NeAACDecConfigurationPtr config = &(hDecoder->config);
194
195 return config;
196 }
197
198 return NULL;
199}
200
201uint8_t NEAACDECAPI NeAACDecSetConfiguration(NeAACDecHandle hDecoder,
202 NeAACDecConfigurationPtr config)
203{
204 if (hDecoder && config)
205 {
206 /* check if we can decode this object type */
207 if (can_decode_ot(config->defObjectType) < 0)
208 return 0;
209 hDecoder->config.defObjectType = config->defObjectType;
210
211 /* samplerate: anything but 0 should be possible */
212 if (config->defSampleRate == 0)
213 return 0;
214 hDecoder->config.defSampleRate = config->defSampleRate;
215
216 /* check output format */
217#ifdef FIXED_POINT
218 if ((config->outputFormat < 1) || (config->outputFormat > 4))
219 return 0;
220#else
221 if ((config->outputFormat < 1) || (config->outputFormat > 5))
222 return 0;
223#endif
224 hDecoder->config.outputFormat = config->outputFormat;
225
226 if (config->downMatrix > 1)
227 return 0;
228 hDecoder->config.downMatrix = config->downMatrix;
229
230 /* OK */
231 return 1;
232 }
233
234 return 0;
235}
236
237int32_t NEAACDECAPI NeAACDecInit(NeAACDecHandle hDecoder, uint8_t *buffer,
238 uint32_t buffer_size,
239 uint32_t *samplerate, uint8_t *channels)
240{
241 uint32_t i;
242 uint32_t bits = 0;
243 bitfile ld;
244 adif_header adif;
245 adts_header adts;
246
247 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL))
248 return -1;
249
250 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
251 hDecoder->object_type = hDecoder->config.defObjectType;
252 *samplerate = get_sample_rate(hDecoder->sf_index);
253 *channels = 1;
254
255 if (buffer != NULL)
256 {
257 faad_initbits(&ld, buffer, buffer_size);
258
259 /* Check if an ADIF header is present */
260 if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
261 (buffer[2] == 'I') && (buffer[3] == 'F'))
262 {
263 hDecoder->adif_header_present = 1;
264
265 get_adif_header(&adif, &ld);
266 faad_byte_align(&ld);
267
268 hDecoder->sf_index = adif.pce[0].sf_index;
269 hDecoder->object_type = adif.pce[0].object_type + 1;
270
271 *samplerate = get_sample_rate(hDecoder->sf_index);
272 *channels = adif.pce[0].channels;
273
274 memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
275 hDecoder->pce_set = 1;
276
277 bits = bit2byte(faad_get_processed_bits(&ld));
278
279 /* Check if an ADTS header is present */
280 } else if (faad_showbits(&ld, 12) == 0xfff) {
281 hDecoder->adts_header_present = 1;
282
283 adts.old_format = hDecoder->config.useOldADTSFormat;
284 adts_frame(&adts, &ld);
285
286 hDecoder->sf_index = adts.sf_index;
287 hDecoder->object_type = adts.profile + 1;
288
289 *samplerate = get_sample_rate(hDecoder->sf_index);
290 *channels = (adts.channel_configuration > 6) ?
291 2 : adts.channel_configuration;
292 }
293
294 if (ld.error)
295 {
296 faad_endbits(&ld);
297 return -1;
298 }
299 faad_endbits(&ld);
300 }
301 hDecoder->channelConfiguration = *channels;
302
303#if (defined(PS_DEC) || defined(DRM_PS))
304 /* check if we have a mono file */
305 if (*channels == 1)
306 {
307 /* upMatrix to 2 channels for implicit signalling of PS */
308 *channels = 2;
309 }
310#endif
311
312 /* A maximum of MAX_CHANNELS channels is supported. */
313 if (*channels > MAX_CHANNELS)
314 {
315 return -1;
316 }
317
318#ifdef SBR_DEC
319 /* implicit signalling */
320 if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR))
321 {
322 *samplerate *= 2;
323 hDecoder->forceUpSampling = 1;
324 } else if (*samplerate > 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) {
325 hDecoder->downSampledSBR = 1;
326 }
327#endif
328
329 /* must be done before frameLength is divided by 2 for LD */
330#ifdef SSR_DEC
331 if (hDecoder->object_type == SSR)
332 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
333 else
334#endif
335
336
337#ifdef LD_DEC
338 if (hDecoder->object_type == LD)
339 hDecoder->frameLength >>= 1;
340#endif
341
342 for (i=0; i<MAX_CHANNELS; ++i)
343 {
344#ifdef SBR_DEC
345 hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
346 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
347 {
348#ifdef FAAD_STATIC_ALLOC
349 hDecoder->time_out[i] = s_time_buf_2048[i];
350#else
351 hDecoder->time_out[i] = (real_t*)faad_malloc(2*FRAME_LEN*sizeof(real_t));
352 if (hDecoder->time_out[i] == NULL)
353 {
354 /* could not allocate memory */
355 return -1;
356 }
357#endif
358 memset(hDecoder->time_out[i], 0, 2*FRAME_LEN);
359 hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
360 }
361 else
362#endif
363 {
364 hDecoder->time_out[i] = s_time_buf_1024[i];
365 memset(hDecoder->time_out[i], 0, 1*FRAME_LEN);
366 }
367 }
368
369 if (can_decode_ot(hDecoder->object_type) < 0)
370 return -1;
371
372 return bits;
373}
374
375/* Init the library using a DecoderSpecificInfo */
376int8_t NEAACDECAPI NeAACDecInit2(NeAACDecHandle hDecoder, uint8_t *pBuffer,
377 uint32_t SizeOfDecoderSpecificInfo,
378 uint32_t *samplerate, uint8_t *channels)
379{
380 int8_t rc;
381 uint32_t i;
382 mp4AudioSpecificConfig mp4ASC;
383
384 if((hDecoder == NULL)
385 || (pBuffer == NULL)
386 || (SizeOfDecoderSpecificInfo < 2)
387 || (samplerate == NULL)
388 || (channels == NULL))
389 {
390 return -1;
391 }
392
393 hDecoder->adif_header_present = 0;
394 hDecoder->adts_header_present = 0;
395
396 /* decode the audio specific config */
397 rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
398 &(hDecoder->pce));
399
400 /* copy the relevant info to the decoder handle */
401 *samplerate = mp4ASC.samplingFrequency;
402 if (mp4ASC.channelsConfiguration)
403 {
404 *channels = mp4ASC.channelsConfiguration;
405 } else {
406 *channels = hDecoder->pce.channels;
407 hDecoder->pce_set = 1;
408 }
409#if (defined(PS_DEC) || defined(DRM_PS))
410 /* check if we have a mono file */
411 if (*channels == 1)
412 {
413 /* upMatrix to 2 channels for implicit signalling of PS */
414 *channels = 2;
415 }
416#endif
417
418 /* A maximum of MAX_CHANNELS channels is supported. */
419 if (*channels > MAX_CHANNELS)
420 {
421 return -1;
422 }
423
424 hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
425 hDecoder->object_type = mp4ASC.objectTypeIndex;
426#ifdef ERROR_RESILIENCE
427 hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
428 hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
429 hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
430#endif
431#ifdef SBR_DEC
432 hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
433 hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
434 if (hDecoder->config.dontUpSampleImplicitSBR == 0)
435 hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
436 else
437 hDecoder->forceUpSampling = 0;
438
439 /* AAC core decoder samplerate is 2 times as low */
440 if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
441 {
442 hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
443 }
444#endif
445
446 if (rc != 0)
447 {
448 return rc;
449 }
450 hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
451 if (mp4ASC.frameLengthFlag)
452#ifdef ALLOW_SMALL_FRAMELENGTH
453 hDecoder->frameLength = 960;
454#else
455 return -1;
456#endif
457
458 /* must be done before frameLength is divided by 2 for LD */
459#ifdef SSR_DEC
460 if (hDecoder->object_type == SSR)
461 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
462 else
463#endif
464
465#ifdef LD_DEC
466 if (hDecoder->object_type == LD)
467 hDecoder->frameLength >>= 1;
468#endif
469
470 for (i=0; i<MAX_CHANNELS; ++i)
471 {
472#ifdef SBR_DEC
473 hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
474 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
475 {
476#ifdef FAAD_STATIC_ALLOC
477 hDecoder->time_out[i] = s_time_buf_2048[i];
478#else
479 hDecoder->time_out[i] = (real_t*)faad_malloc(2*FRAME_LEN*sizeof(real_t));
480 if (hDecoder->time_out[i] == NULL)
481 {
482 /* could not allocate memory */
483 return -1;
484 }
485#endif
486 memset(hDecoder->time_out[i], 0, 2*FRAME_LEN);
487 hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
488 }
489 else
490#endif
491 {
492 hDecoder->time_out[i] = s_time_buf_1024[i];
493 memset(hDecoder->time_out[i], 0, 1*FRAME_LEN);
494 }
495 }
496
497 return 0;
498}
499
500#ifdef DRM
501int8_t NEAACDECAPI NeAACDecInitDRM(NeAACDecHandle *hDecoder, uint32_t samplerate,
502 uint8_t channels)
503{
504 if (hDecoder == NULL)
505 return 1; /* error */
506
507 *hDecoder = NeAACDecOpen();
508
509 /* Special object type defined for DRM */
510 (*hDecoder)->config.defObjectType = DRM_ER_LC;
511
512 (*hDecoder)->config.defSampleRate = samplerate;
513#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
514 (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
515 (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
516 (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
517#endif
518 (*hDecoder)->frameLength = 960;
519 (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
520 (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
521
522 if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
523 (*hDecoder)->channelConfiguration = 2;
524 else
525 (*hDecoder)->channelConfiguration = 1;
526
527#ifdef SBR_DEC
528 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
529 (*hDecoder)->sbr_present_flag = 0;
530 else
531 (*hDecoder)->sbr_present_flag = 1;
532#endif
533
534
535 return 0;
536}
537#endif
538
539void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hDecoder, int32_t frame)
540{
541 if (hDecoder)
542 {
543 hDecoder->postSeekResetFlag = 1;
544
545 if (frame != -1)
546 hDecoder->frame = frame;
547 }
548}
549
550/* not used by rockbox */
551#if 0
552static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo)
553{
554 hInfo->num_front_channels = 0;
555 hInfo->num_side_channels = 0;
556 hInfo->num_back_channels = 0;
557 hInfo->num_lfe_channels = 0;
558 memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
559
560 if (hDecoder->downMatrix)
561 {
562 hInfo->num_front_channels = 2;
563 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
564 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
565 return;
566 }
567
568 /* check if there is a PCE */
569 if (hDecoder->pce_set)
570 {
571 uint8_t i, chpos = 0;
572 uint8_t chdir, back_center = 0;
573
574 hInfo->num_front_channels = hDecoder->pce.num_front_channels;
575 hInfo->num_side_channels = hDecoder->pce.num_side_channels;
576 hInfo->num_back_channels = hDecoder->pce.num_back_channels;
577 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
578
579 chdir = hInfo->num_front_channels;
580 if (chdir & 1)
581 {
582 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
583 chdir--;
584 }
585 for (i = 0; i < chdir; i += 2)
586 {
587 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
588 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
589 }
590
591 for (i = 0; i < hInfo->num_side_channels; i += 2)
592 {
593 hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT;
594 hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT;
595 }
596
597 chdir = hInfo->num_back_channels;
598 if (chdir & 1)
599 {
600 back_center = 1;
601 chdir--;
602 }
603 for (i = 0; i < chdir; i += 2)
604 {
605 hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT;
606 hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT;
607 }
608 if (back_center)
609 {
610 hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
611 }
612
613 for (i = 0; i < hInfo->num_lfe_channels; i++)
614 {
615 hInfo->channel_position[chpos++] = LFE_CHANNEL;
616 }
617
618 } else {
619 switch (hDecoder->channelConfiguration)
620 {
621 case 1:
622 hInfo->num_front_channels = 1;
623 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
624 break;
625 case 2:
626 hInfo->num_front_channels = 2;
627 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
628 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
629 break;
630 case 3:
631 hInfo->num_front_channels = 3;
632 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
633 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
634 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
635 break;
636 case 4:
637 hInfo->num_front_channels = 3;
638 hInfo->num_back_channels = 1;
639 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
640 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
641 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
642 hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
643 break;
644 case 5:
645 hInfo->num_front_channels = 3;
646 hInfo->num_back_channels = 2;
647 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
648 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
649 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
650 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
651 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
652 break;
653 case 6:
654 hInfo->num_front_channels = 3;
655 hInfo->num_back_channels = 2;
656 hInfo->num_lfe_channels = 1;
657 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
658 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
659 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
660 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
661 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
662 hInfo->channel_position[5] = LFE_CHANNEL;
663 break;
664 case 7:
665 hInfo->num_front_channels = 3;
666 hInfo->num_side_channels = 2;
667 hInfo->num_back_channels = 2;
668 hInfo->num_lfe_channels = 1;
669 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
670 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
671 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
672 hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
673 hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
674 hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
675 hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
676 hInfo->channel_position[7] = LFE_CHANNEL;
677 break;
678 default: /* channelConfiguration == 0 || channelConfiguration > 7 */
679 {
680 uint8_t i;
681 uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
682 if (ch & 1) /* there's either a center front or a center back channel */
683 {
684 uint8_t ch1 = (ch-1)/2;
685 if (hDecoder->first_syn_ele == ID_SCE)
686 {
687 hInfo->num_front_channels = ch1 + 1;
688 hInfo->num_back_channels = ch1;
689 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
690 for (i = 1; i <= ch1; i+=2)
691 {
692 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
693 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
694 }
695 for (i = ch1+1; i < ch; i+=2)
696 {
697 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
698 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
699 }
700 } else {
701 hInfo->num_front_channels = ch1;
702 hInfo->num_back_channels = ch1 + 1;
703 for (i = 0; i < ch1; i+=2)
704 {
705 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
706 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
707 }
708 for (i = ch1; i < ch-1; i+=2)
709 {
710 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
711 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
712 }
713 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
714 }
715 } else {
716 uint8_t ch1 = (ch)/2;
717 hInfo->num_front_channels = ch1;
718 hInfo->num_back_channels = ch1;
719 if (ch1 & 1)
720 {
721 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722 for (i = 1; i <= ch1; i+=2)
723 {
724 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
725 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
726 }
727 for (i = ch1+1; i < ch-1; i+=2)
728 {
729 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
730 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
731 }
732 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
733 } else {
734 for (i = 0; i < ch1; i+=2)
735 {
736 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
737 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
738 }
739 for (i = ch1; i < ch; i+=2)
740 {
741 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
742 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
743 }
744 }
745 }
746 hInfo->num_lfe_channels = hDecoder->has_lfe;
747 for (i = ch; i < hDecoder->fr_channels; i++)
748 {
749 hInfo->channel_position[i] = LFE_CHANNEL;
750 }
751 }
752 break;
753 }
754 }
755}
756#endif
757
758void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hDecoder,
759 NeAACDecFrameInfo *hInfo,
760 uint8_t *buffer, uint32_t buffer_size)
761{
762 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size);
763}
764
765static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
766 uint8_t *buffer, uint32_t buffer_size)
767{
768 uint8_t channels = 0;
769 uint8_t output_channels = 0;
770 bitfile ld;
771 uint32_t bitsconsumed;
772 uint16_t frame_len;
773
774#ifdef PROFILE
775 int64_t count = faad_get_ts();
776#endif
777
778 /* safety checks */
779 if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
780 {
781 return NULL;
782 }
783
784#if 0
785 printf("%d\n", buffer_size*8);
786#endif
787
788 frame_len = hDecoder->frameLength;
789
790
791 memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
792 memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
793
794 /* initialize the bitstream */
795 faad_initbits(&ld, buffer, buffer_size);
796
797#if 0
798 {
799 int i;
800 for (i = 0; i < ((buffer_size+3)>>2); i++)
801 {
802 uint8_t *buf;
803 uint32_t temp = 0;
804 buf = faad_getbitbuffer(&ld, 32);
805 //temp = getdword((void*)buf);
806 temp = *((uint32_t*)buf);
807 printf("0x%.8X\n", temp);
808 free(buf);
809 }
810 faad_endbits(&ld);
811 faad_initbits(&ld, buffer, buffer_size);
812 }
813#endif
814
815#ifdef DRM
816 if (hDecoder->object_type == DRM_ER_LC)
817 {
818 /* We do not support stereo right now */
819 if (0) //(hDecoder->channelConfiguration == 2)
820 {
821 hInfo->error = 8; // Throw CRC error
822 goto error;
823 }
824
825 faad_getbits(&ld, 8
826 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
827 }
828#endif
829
830 if (hDecoder->adts_header_present)
831 {
832 adts_header adts;
833
834 adts.old_format = hDecoder->config.useOldADTSFormat;
835 if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
836 goto error;
837
838 /* MPEG2 does byte_alignment() here,
839 * but ADTS header is always multiple of 8 bits in MPEG2
840 * so not needed to actually do it.
841 */
842 }
843
844#ifdef ANALYSIS
845 dbg_count = 0;
846#endif
847
848 /* decode the complete bitstream */
849#ifdef SCALABLE_DEC
850 if ((hDecoder->object_type == 6) || (hDecoder->object_type == DRM_ER_LC))
851 {
852 aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
853 } else {
854#endif
855 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
856#ifdef SCALABLE_DEC
857 }
858#endif
859
860 channels = hDecoder->fr_channels;
861
862 if (hInfo->error > 0)
863 goto error;
864
865 /* safety check */
866 if (channels == 0 || channels > MAX_CHANNELS)
867 {
868 /* invalid number of channels */
869 hInfo->error = 12;
870 goto error;
871 }
872
873 /* no more bit reading after this */
874 bitsconsumed = faad_get_processed_bits(&ld);
875 hInfo->bytesconsumed = bit2byte(bitsconsumed);
876 if (ld.error)
877 {
878 hInfo->error = 14;
879 goto error;
880 }
881 faad_endbits(&ld);
882
883
884 if (!hDecoder->adts_header_present && !hDecoder->adif_header_present)
885 {
886 if (hDecoder->channelConfiguration == 0)
887 hDecoder->channelConfiguration = channels;
888
889 if (channels == 8) /* 7.1 */
890 hDecoder->channelConfiguration = 7;
891 if (channels == 7) /* not a standard channelConfiguration */
892 hDecoder->channelConfiguration = 0;
893 }
894
895 if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
896 {
897 hDecoder->downMatrix = 1;
898 output_channels = 2;
899 } else {
900 output_channels = channels;
901 }
902
903#if (defined(PS_DEC) || defined(DRM_PS))
904 hDecoder->upMatrix = 0;
905 /* check if we have a mono file */
906 if (output_channels == 1)
907 {
908 /* upMatrix to 2 channels for implicit signalling of PS */
909 hDecoder->upMatrix = 1;
910 output_channels = 2;
911 }
912#endif
913
914 /* Make a channel configuration based on either a PCE or a channelConfiguration */
915 /* not used by rockbox
916 create_channel_config(hDecoder, hInfo);
917 */
918
919 /* number of samples in this frame */
920 hInfo->samples = frame_len*output_channels;
921 /* number of channels in this frame */
922 hInfo->channels = output_channels;
923 /* samplerate */
924 hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
925 /* object type */
926 hInfo->object_type = hDecoder->object_type;
927 /* sbr */
928 hInfo->sbr = NO_SBR;
929 /* header type */
930 hInfo->header_type = RAW;
931 if (hDecoder->adif_header_present)
932 hInfo->header_type = ADIF;
933 if (hDecoder->adts_header_present)
934 hInfo->header_type = ADTS;
935#if (defined(PS_DEC) || defined(DRM_PS))
936 hInfo->ps = hDecoder->ps_used_global;
937#endif
938
939 /* check if frame has channel elements */
940 if (channels == 0)
941 {
942 hDecoder->frame++;
943 return NULL;
944 }
945
946 /* allocate the buffer for the final samples */
947 if (hDecoder->alloced_channels != output_channels)
948 {
949 hDecoder->alloced_channels = output_channels;
950 }
951
952#ifdef SBR_DEC
953 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
954 {
955 uint8_t ele;
956
957 /* this data is different when SBR is used or when the data is upsampled */
958 if (!hDecoder->downSampledSBR)
959 {
960 frame_len *= 2;
961 hInfo->samples *= 2;
962 hInfo->samplerate *= 2;
963 }
964
965 /* check if every element was provided with SBR data */
966 for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
967 {
968 if (hDecoder->sbr[ele] == NULL)
969 {
970 hInfo->error = 25;
971 goto error;
972 }
973 }
974
975 /* sbr */
976 if (hDecoder->sbr_present_flag == 1)
977 {
978 hInfo->object_type = HE_AAC;
979 hInfo->sbr = SBR_UPSAMPLED;
980 } else {
981 hInfo->sbr = NO_SBR_UPSAMPLED;
982 }
983 if (hDecoder->downSampledSBR)
984 {
985 hInfo->sbr = SBR_DOWNSAMPLED;
986 }
987 }
988#endif
989
990 hDecoder->postSeekResetFlag = 0;
991
992 hDecoder->frame++;
993#ifdef LD_DEC
994 if (hDecoder->object_type != LD)
995 {
996#endif
997 if (hDecoder->frame <= 1)
998 hInfo->samples = 0;
999#ifdef LD_DEC
1000 } else {
1001 /* LD encoders will give lower delay */
1002 if (hDecoder->frame <= 0)
1003 hInfo->samples = 0;
1004 }
1005#endif
1006
1007 /* cleanup */
1008#ifdef ANALYSIS
1009 fflush(stdout);
1010#endif
1011
1012#ifdef PROFILE
1013 count = faad_get_ts() - count;
1014 hDecoder->cycles += count;
1015#endif
1016
1017 return hDecoder; /* return void* != NULL */
1018
1019error:
1020
1021 faad_endbits(&ld);
1022
1023 /* cleanup */
1024#ifdef ANALYSIS
1025 fflush(stdout);
1026#endif
1027
1028 return NULL;
1029}