Submit FS#12081. Check validity of channel configuration, comment unneeded code that...
[kugel-rb.git] / apps / codecs / libfaad / decoder.c
blob05d788597eccbe7e253e9dee725f51ff9b6c67b1
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.
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.
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** $Id$
26 **/
28 #include "common.h"
29 #include "structs.h"
31 #include <stdlib.h>
32 #include <string.h>
34 #include "decoder.h"
35 #include "mp4.h"
36 #include "syntax.h"
37 #include "error.h"
38 /* rockbox: not used
39 #include "output.h"
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
51 /* Globals */
52 #ifdef ANALYSIS
53 uint16_t dbg_count;
54 #endif
56 /* static variables */
57 static NeAACDecStruct s_AACDec;
58 static real_t s_fb_intermed [MAX_CHANNELS][1*FRAME_LEN] IBSS_ATTR_FAAD_LARGE_IRAM MEM_ALIGN_ATTR;
59 static 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
62 static real_t s_time_buf_2048[MAX_CHANNELS][2*FRAME_LEN] MEM_ALIGN_ATTR;
63 #endif
64 #endif
65 #ifdef SSR_DEC
66 static real_t s_ssr_overlap [MAX_CHANNELS][2*FRAME_LEN] MEM_ALIGN_ATTR;
67 static real_t s_prev_fmd [MAX_CHANNELS][2*FRAME_LEN] MEM_ALIGN_ATTR;
68 #endif
69 #ifdef MAIN_DEC
70 static pred_state s_pred_stat[MAX_CHANNELS][1*FRAME_LEN] MEM_ALIGN_ATTR;
71 #endif
72 #ifdef LTP_DEC
73 static int16_t s_lt_pred_stat[MAX_CHANNELS][4*FRAME_LEN] MEM_ALIGN_ATTR;
74 #endif
77 /* static function declarations */
78 static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
79 uint8_t *buffer, uint32_t buffer_size);
80 /* not used by rockbox
81 static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo);
84 char* NEAACDECAPI NeAACDecGetErrorMessage(uint8_t errcode)
86 if (errcode >= NUM_ERROR_MESSAGES)
87 return NULL;
88 return err_msg[errcode];
91 uint32_t NEAACDECAPI NeAACDecGetCapabilities(void)
93 uint32_t cap = 0;
95 /* can't do without it */
96 cap += LC_DEC_CAP;
98 #ifdef MAIN_DEC
99 cap += MAIN_DEC_CAP;
100 #endif
101 #ifdef LTP_DEC
102 cap += LTP_DEC_CAP;
103 #endif
104 #ifdef LD_DEC
105 cap += LD_DEC_CAP;
106 #endif
107 #ifdef ERROR_RESILIENCE
108 cap += ERROR_RESILIENCE_CAP;
109 #endif
110 #ifdef FIXED_POINT
111 cap += FIXED_POINT_CAP;
112 #endif
114 return cap;
117 NeAACDecHandle NEAACDECAPI NeAACDecOpen(void)
119 uint8_t i;
120 NeAACDecHandle hDecoder = NULL;
122 #if defined(CPU_COLDFIRE)
123 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
124 #endif
126 hDecoder = &s_AACDec;
128 memset(hDecoder , 0, sizeof(NeAACDecStruct));
129 memset(s_fb_intermed, 0, sizeof(s_fb_intermed));
130 #ifdef SSR_DEC
131 memset(s_ssr_overlap, 0, sizeof(s_ssr_overlap));
132 memset(s_prev_fmd , 0, sizeof(s_prev_fmd));
133 #endif
134 #ifdef LTP_DEC
135 memset(s_lt_pred_stat, 0, sizeof(s_s_lt_pred_statpred_stat));
136 #endif
138 hDecoder->config.outputFormat = FAAD_FMT_16BIT;
139 hDecoder->config.defObjectType = MAIN;
140 hDecoder->config.defSampleRate = 44100;
141 hDecoder->config.downMatrix = 0;
142 hDecoder->adts_header_present = 0;
143 hDecoder->adif_header_present = 0;
144 #ifdef ERROR_RESILIENCE
145 hDecoder->aacSectionDataResilienceFlag = 0;
146 hDecoder->aacScalefactorDataResilienceFlag = 0;
147 hDecoder->aacSpectralDataResilienceFlag = 0;
148 #endif
149 hDecoder->frameLength = FRAME_LEN;
151 hDecoder->frame = 0;
153 for (i = 0; i < MAX_CHANNELS; i++)
155 hDecoder->window_shape_prev[i] = 0;
156 hDecoder->time_out[i] = NULL;
157 hDecoder->fb_intermed[i] = s_fb_intermed[i];
158 #ifdef SSR_DEC
159 hDecoder->ssr_overlap[i] = s_ssr_overlap[i];
160 hDecoder->prev_fmd[i] = s_prev_fmd[i];
161 for (int k = 0; k < 2048; k++)
162 hDecoder->prev_fmd[i][k] = REAL_CONST(-1);
163 #endif
164 #ifdef MAIN_DEC
165 hDecoder->pred_stat[i] = s_pred_stat[i];
166 reset_all_predictors(hDecoder->pred_stat[channel], FRAME_LEN);
167 #endif
168 #ifdef LTP_DEC
169 hDecoder->ltp_lag[i] = 0;
170 hDecoder->lt_pred_stat[i] = s_lt_pred_stat[i];
171 #endif
174 #ifdef SBR_DEC
175 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
177 hDecoder->sbr[i] = NULL;
179 #endif
181 hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
183 return hDecoder;
186 NeAACDecConfigurationPtr NEAACDECAPI NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder)
188 if (hDecoder)
190 NeAACDecConfigurationPtr config = &(hDecoder->config);
192 return config;
195 return NULL;
198 uint8_t NEAACDECAPI NeAACDecSetConfiguration(NeAACDecHandle hDecoder,
199 NeAACDecConfigurationPtr config)
201 if (hDecoder && config)
203 /* check if we can decode this object type */
204 if (can_decode_ot(config->defObjectType) < 0)
205 return 0;
206 hDecoder->config.defObjectType = config->defObjectType;
208 /* samplerate: anything but 0 should be possible */
209 if (config->defSampleRate == 0)
210 return 0;
211 hDecoder->config.defSampleRate = config->defSampleRate;
213 /* check output format */
214 #ifdef FIXED_POINT
215 if ((config->outputFormat < 1) || (config->outputFormat > 4))
216 return 0;
217 #else
218 if ((config->outputFormat < 1) || (config->outputFormat > 5))
219 return 0;
220 #endif
221 hDecoder->config.outputFormat = config->outputFormat;
223 if (config->downMatrix > 1)
224 return 0;
225 hDecoder->config.downMatrix = config->downMatrix;
227 /* OK */
228 return 1;
231 return 0;
234 int32_t NEAACDECAPI NeAACDecInit(NeAACDecHandle hDecoder, uint8_t *buffer,
235 uint32_t buffer_size,
236 uint32_t *samplerate, uint8_t *channels)
238 uint32_t i;
239 uint32_t bits = 0;
240 bitfile ld;
241 adif_header adif;
242 adts_header adts;
244 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL))
245 return -1;
247 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
248 hDecoder->object_type = hDecoder->config.defObjectType;
249 *samplerate = get_sample_rate(hDecoder->sf_index);
250 *channels = 1;
252 if (buffer != NULL)
254 faad_initbits(&ld, buffer, buffer_size);
256 /* Check if an ADIF header is present */
257 if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
258 (buffer[2] == 'I') && (buffer[3] == 'F'))
260 hDecoder->adif_header_present = 1;
262 get_adif_header(&adif, &ld);
263 faad_byte_align(&ld);
265 hDecoder->sf_index = adif.pce[0].sf_index;
266 hDecoder->object_type = adif.pce[0].object_type + 1;
268 *samplerate = get_sample_rate(hDecoder->sf_index);
269 *channels = adif.pce[0].channels;
271 memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
272 hDecoder->pce_set = 1;
274 bits = bit2byte(faad_get_processed_bits(&ld));
276 /* Check if an ADTS header is present */
277 } else if (faad_showbits(&ld, 12) == 0xfff) {
278 hDecoder->adts_header_present = 1;
280 adts.old_format = hDecoder->config.useOldADTSFormat;
281 adts_frame(&adts, &ld);
283 hDecoder->sf_index = adts.sf_index;
284 hDecoder->object_type = adts.profile + 1;
286 *samplerate = get_sample_rate(hDecoder->sf_index);
287 *channels = (adts.channel_configuration > 6) ?
288 2 : adts.channel_configuration;
291 if (ld.error)
293 faad_endbits(&ld);
294 return -1;
296 faad_endbits(&ld);
298 hDecoder->channelConfiguration = *channels;
300 #if (defined(PS_DEC) || defined(DRM_PS))
301 /* check if we have a mono file */
302 if (*channels == 1)
304 /* upMatrix to 2 channels for implicit signalling of PS */
305 *channels = 2;
307 #endif
309 /* A maximum of MAX_CHANNELS channels is supported. */
310 if (*channels > MAX_CHANNELS)
312 return -1;
315 #ifdef SBR_DEC
316 /* implicit signalling */
317 if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR))
319 *samplerate *= 2;
320 hDecoder->forceUpSampling = 1;
321 } else if (*samplerate > 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) {
322 hDecoder->downSampledSBR = 1;
324 #endif
326 /* must be done before frameLength is divided by 2 for LD */
327 #ifdef SSR_DEC
328 if (hDecoder->object_type == SSR)
329 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
330 else
331 #endif
334 #ifdef LD_DEC
335 if (hDecoder->object_type == LD)
336 hDecoder->frameLength >>= 1;
337 #endif
339 for (i=0; i<MAX_CHANNELS; ++i)
341 #ifdef SBR_DEC
342 hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
343 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
345 #ifdef FAAD_STATIC_ALLOC
346 hDecoder->time_out[i] = s_time_buf_2048[i];
347 #else
348 hDecoder->time_out[i] = (real_t*)faad_malloc(2*FRAME_LEN*sizeof(real_t));
349 #endif
350 memset(hDecoder->time_out[i], 0, 2*FRAME_LEN);
351 hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
353 else
354 #endif
356 hDecoder->time_out[i] = s_time_buf_1024[i];
357 memset(hDecoder->time_out[i], 0, 1*FRAME_LEN);
361 if (can_decode_ot(hDecoder->object_type) < 0)
362 return -1;
364 return bits;
367 /* Init the library using a DecoderSpecificInfo */
368 int8_t NEAACDECAPI NeAACDecInit2(NeAACDecHandle hDecoder, uint8_t *pBuffer,
369 uint32_t SizeOfDecoderSpecificInfo,
370 uint32_t *samplerate, uint8_t *channels)
372 int8_t rc;
373 uint32_t i;
374 mp4AudioSpecificConfig mp4ASC;
376 if((hDecoder == NULL)
377 || (pBuffer == NULL)
378 || (SizeOfDecoderSpecificInfo < 2)
379 || (samplerate == NULL)
380 || (channels == NULL))
382 return -1;
385 hDecoder->adif_header_present = 0;
386 hDecoder->adts_header_present = 0;
388 /* decode the audio specific config */
389 rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
390 &(hDecoder->pce));
392 /* copy the relevant info to the decoder handle */
393 *samplerate = mp4ASC.samplingFrequency;
394 if (mp4ASC.channelsConfiguration)
396 *channels = mp4ASC.channelsConfiguration;
397 } else {
398 *channels = hDecoder->pce.channels;
399 hDecoder->pce_set = 1;
401 #if (defined(PS_DEC) || defined(DRM_PS))
402 /* check if we have a mono file */
403 if (*channels == 1)
405 /* upMatrix to 2 channels for implicit signalling of PS */
406 *channels = 2;
408 #endif
410 /* A maximum of MAX_CHANNELS channels is supported. */
411 if (*channels > MAX_CHANNELS)
413 return -1;
416 hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
417 hDecoder->object_type = mp4ASC.objectTypeIndex;
418 #ifdef ERROR_RESILIENCE
419 hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
420 hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
421 hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
422 #endif
423 #ifdef SBR_DEC
424 hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
425 hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
426 if (hDecoder->config.dontUpSampleImplicitSBR == 0)
427 hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
428 else
429 hDecoder->forceUpSampling = 0;
431 /* AAC core decoder samplerate is 2 times as low */
432 if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
434 hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
436 #endif
438 if (rc != 0)
440 return rc;
442 hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
443 if (mp4ASC.frameLengthFlag)
444 #ifdef ALLOW_SMALL_FRAMELENGTH
445 hDecoder->frameLength = 960;
446 #else
447 return -1;
448 #endif
450 /* must be done before frameLength is divided by 2 for LD */
451 #ifdef SSR_DEC
452 if (hDecoder->object_type == SSR)
453 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
454 else
455 #endif
457 #ifdef LD_DEC
458 if (hDecoder->object_type == LD)
459 hDecoder->frameLength >>= 1;
460 #endif
462 for (i=0; i<MAX_CHANNELS; ++i)
464 #ifdef SBR_DEC
465 hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
466 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
468 #ifdef FAAD_STATIC_ALLOC
469 hDecoder->time_out[i] = s_time_buf_2048[i];
470 #else
471 hDecoder->time_out[i] = (real_t*)faad_malloc(2*FRAME_LEN*sizeof(real_t));
472 #endif
473 memset(hDecoder->time_out[i], 0, 2*FRAME_LEN);
474 hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
476 else
477 #endif
479 hDecoder->time_out[i] = s_time_buf_1024[i];
480 memset(hDecoder->time_out[i], 0, 1*FRAME_LEN);
484 return 0;
487 #ifdef DRM
488 int8_t NEAACDECAPI NeAACDecInitDRM(NeAACDecHandle *hDecoder, uint32_t samplerate,
489 uint8_t channels)
491 if (hDecoder == NULL)
492 return 1; /* error */
494 *hDecoder = NeAACDecOpen();
496 /* Special object type defined for DRM */
497 (*hDecoder)->config.defObjectType = DRM_ER_LC;
499 (*hDecoder)->config.defSampleRate = samplerate;
500 #ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
501 (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
502 (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
503 (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
504 #endif
505 (*hDecoder)->frameLength = 960;
506 (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
507 (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
509 if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
510 (*hDecoder)->channelConfiguration = 2;
511 else
512 (*hDecoder)->channelConfiguration = 1;
514 #ifdef SBR_DEC
515 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
516 (*hDecoder)->sbr_present_flag = 0;
517 else
518 (*hDecoder)->sbr_present_flag = 1;
519 #endif
522 return 0;
524 #endif
526 void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hDecoder, int32_t frame)
528 if (hDecoder)
530 hDecoder->postSeekResetFlag = 1;
532 if (frame != -1)
533 hDecoder->frame = frame;
537 /* not used by rockbox */
538 #if 0
539 static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo)
541 hInfo->num_front_channels = 0;
542 hInfo->num_side_channels = 0;
543 hInfo->num_back_channels = 0;
544 hInfo->num_lfe_channels = 0;
545 memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
547 if (hDecoder->downMatrix)
549 hInfo->num_front_channels = 2;
550 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
551 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
552 return;
555 /* check if there is a PCE */
556 if (hDecoder->pce_set)
558 uint8_t i, chpos = 0;
559 uint8_t chdir, back_center = 0;
561 hInfo->num_front_channels = hDecoder->pce.num_front_channels;
562 hInfo->num_side_channels = hDecoder->pce.num_side_channels;
563 hInfo->num_back_channels = hDecoder->pce.num_back_channels;
564 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
566 chdir = hInfo->num_front_channels;
567 if (chdir & 1)
569 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
570 chdir--;
572 for (i = 0; i < chdir; i += 2)
574 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
575 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
578 for (i = 0; i < hInfo->num_side_channels; i += 2)
580 hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT;
581 hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT;
584 chdir = hInfo->num_back_channels;
585 if (chdir & 1)
587 back_center = 1;
588 chdir--;
590 for (i = 0; i < chdir; i += 2)
592 hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT;
593 hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT;
595 if (back_center)
597 hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
600 for (i = 0; i < hInfo->num_lfe_channels; i++)
602 hInfo->channel_position[chpos++] = LFE_CHANNEL;
605 } else {
606 switch (hDecoder->channelConfiguration)
608 case 1:
609 hInfo->num_front_channels = 1;
610 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
611 break;
612 case 2:
613 hInfo->num_front_channels = 2;
614 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
615 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
616 break;
617 case 3:
618 hInfo->num_front_channels = 3;
619 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
620 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
621 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
622 break;
623 case 4:
624 hInfo->num_front_channels = 3;
625 hInfo->num_back_channels = 1;
626 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
627 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
628 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
629 hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
630 break;
631 case 5:
632 hInfo->num_front_channels = 3;
633 hInfo->num_back_channels = 2;
634 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
635 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
636 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
637 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
638 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
639 break;
640 case 6:
641 hInfo->num_front_channels = 3;
642 hInfo->num_back_channels = 2;
643 hInfo->num_lfe_channels = 1;
644 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
645 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
646 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
647 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
648 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
649 hInfo->channel_position[5] = LFE_CHANNEL;
650 break;
651 case 7:
652 hInfo->num_front_channels = 3;
653 hInfo->num_side_channels = 2;
654 hInfo->num_back_channels = 2;
655 hInfo->num_lfe_channels = 1;
656 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
657 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
658 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
659 hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
660 hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
661 hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
662 hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
663 hInfo->channel_position[7] = LFE_CHANNEL;
664 break;
665 default: /* channelConfiguration == 0 || channelConfiguration > 7 */
667 uint8_t i;
668 uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
669 if (ch & 1) /* there's either a center front or a center back channel */
671 uint8_t ch1 = (ch-1)/2;
672 if (hDecoder->first_syn_ele == ID_SCE)
674 hInfo->num_front_channels = ch1 + 1;
675 hInfo->num_back_channels = ch1;
676 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
677 for (i = 1; i <= ch1; i+=2)
679 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
680 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
682 for (i = ch1+1; i < ch; i+=2)
684 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
685 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
687 } else {
688 hInfo->num_front_channels = ch1;
689 hInfo->num_back_channels = ch1 + 1;
690 for (i = 0; i < ch1; i+=2)
692 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
693 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
695 for (i = ch1; i < ch-1; i+=2)
697 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
698 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
700 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
702 } else {
703 uint8_t ch1 = (ch)/2;
704 hInfo->num_front_channels = ch1;
705 hInfo->num_back_channels = ch1;
706 if (ch1 & 1)
708 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
709 for (i = 1; i <= ch1; i+=2)
711 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
712 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
714 for (i = ch1+1; i < ch-1; i+=2)
716 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
717 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
719 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
720 } else {
721 for (i = 0; i < ch1; i+=2)
723 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
724 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
726 for (i = ch1; i < ch; i+=2)
728 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
729 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
733 hInfo->num_lfe_channels = hDecoder->has_lfe;
734 for (i = ch; i < hDecoder->fr_channels; i++)
736 hInfo->channel_position[i] = LFE_CHANNEL;
739 break;
743 #endif
745 void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hDecoder,
746 NeAACDecFrameInfo *hInfo,
747 uint8_t *buffer, uint32_t buffer_size)
749 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size);
752 static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
753 uint8_t *buffer, uint32_t buffer_size)
755 uint8_t channels = 0;
756 uint8_t output_channels = 0;
757 bitfile ld;
758 uint32_t bitsconsumed;
759 uint16_t frame_len;
761 #ifdef PROFILE
762 int64_t count = faad_get_ts();
763 #endif
765 /* safety checks */
766 if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
768 return NULL;
771 #if 0
772 printf("%d\n", buffer_size*8);
773 #endif
775 frame_len = hDecoder->frameLength;
778 memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
779 memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
781 /* initialize the bitstream */
782 faad_initbits(&ld, buffer, buffer_size);
784 #if 0
786 int i;
787 for (i = 0; i < ((buffer_size+3)>>2); i++)
789 uint8_t *buf;
790 uint32_t temp = 0;
791 buf = faad_getbitbuffer(&ld, 32);
792 //temp = getdword((void*)buf);
793 temp = *((uint32_t*)buf);
794 printf("0x%.8X\n", temp);
795 free(buf);
797 faad_endbits(&ld);
798 faad_initbits(&ld, buffer, buffer_size);
800 #endif
802 #ifdef DRM
803 if (hDecoder->object_type == DRM_ER_LC)
805 /* We do not support stereo right now */
806 if (0) //(hDecoder->channelConfiguration == 2)
808 hInfo->error = 8; // Throw CRC error
809 goto error;
812 faad_getbits(&ld, 8
813 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
815 #endif
817 if (hDecoder->adts_header_present)
819 adts_header adts;
821 adts.old_format = hDecoder->config.useOldADTSFormat;
822 if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
823 goto error;
825 /* MPEG2 does byte_alignment() here,
826 * but ADTS header is always multiple of 8 bits in MPEG2
827 * so not needed to actually do it.
831 #ifdef ANALYSIS
832 dbg_count = 0;
833 #endif
835 /* decode the complete bitstream */
836 #ifdef SCALABLE_DEC
837 if ((hDecoder->object_type == 6) || (hDecoder->object_type == DRM_ER_LC))
839 aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
840 } else {
841 #endif
842 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
843 #ifdef SCALABLE_DEC
845 #endif
847 channels = hDecoder->fr_channels;
849 if (hInfo->error > 0)
850 goto error;
852 /* safety check */
853 if (channels == 0 || channels > MAX_CHANNELS)
855 /* invalid number of channels */
856 hInfo->error = 12;
857 goto error;
860 /* no more bit reading after this */
861 bitsconsumed = faad_get_processed_bits(&ld);
862 hInfo->bytesconsumed = bit2byte(bitsconsumed);
863 if (ld.error)
865 hInfo->error = 14;
866 goto error;
868 faad_endbits(&ld);
871 if (!hDecoder->adts_header_present && !hDecoder->adif_header_present)
873 if (hDecoder->channelConfiguration == 0)
874 hDecoder->channelConfiguration = channels;
876 if (channels == 8) /* 7.1 */
877 hDecoder->channelConfiguration = 7;
878 if (channels == 7) /* not a standard channelConfiguration */
879 hDecoder->channelConfiguration = 0;
882 if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
884 hDecoder->downMatrix = 1;
885 output_channels = 2;
886 } else {
887 output_channels = channels;
890 #if (defined(PS_DEC) || defined(DRM_PS))
891 hDecoder->upMatrix = 0;
892 /* check if we have a mono file */
893 if (output_channels == 1)
895 /* upMatrix to 2 channels for implicit signalling of PS */
896 hDecoder->upMatrix = 1;
897 output_channels = 2;
899 #endif
901 /* Make a channel configuration based on either a PCE or a channelConfiguration */
902 /* not used by rockbox
903 create_channel_config(hDecoder, hInfo);
906 /* number of samples in this frame */
907 hInfo->samples = frame_len*output_channels;
908 /* number of channels in this frame */
909 hInfo->channels = output_channels;
910 /* samplerate */
911 hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
912 /* object type */
913 hInfo->object_type = hDecoder->object_type;
914 /* sbr */
915 hInfo->sbr = NO_SBR;
916 /* header type */
917 hInfo->header_type = RAW;
918 if (hDecoder->adif_header_present)
919 hInfo->header_type = ADIF;
920 if (hDecoder->adts_header_present)
921 hInfo->header_type = ADTS;
922 #if (defined(PS_DEC) || defined(DRM_PS))
923 hInfo->ps = hDecoder->ps_used_global;
924 #endif
926 /* check if frame has channel elements */
927 if (channels == 0)
929 hDecoder->frame++;
930 return NULL;
933 /* allocate the buffer for the final samples */
934 if (hDecoder->alloced_channels != output_channels)
936 hDecoder->alloced_channels = output_channels;
939 #ifdef SBR_DEC
940 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
942 uint8_t ele;
944 /* this data is different when SBR is used or when the data is upsampled */
945 if (!hDecoder->downSampledSBR)
947 frame_len *= 2;
948 hInfo->samples *= 2;
949 hInfo->samplerate *= 2;
952 /* check if every element was provided with SBR data */
953 for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
955 if (hDecoder->sbr[ele] == NULL)
957 hInfo->error = 25;
958 goto error;
962 /* sbr */
963 if (hDecoder->sbr_present_flag == 1)
965 hInfo->object_type = HE_AAC;
966 hInfo->sbr = SBR_UPSAMPLED;
967 } else {
968 hInfo->sbr = NO_SBR_UPSAMPLED;
970 if (hDecoder->downSampledSBR)
972 hInfo->sbr = SBR_DOWNSAMPLED;
975 #endif
977 hDecoder->postSeekResetFlag = 0;
979 hDecoder->frame++;
980 #ifdef LD_DEC
981 if (hDecoder->object_type != LD)
983 #endif
984 if (hDecoder->frame <= 1)
985 hInfo->samples = 0;
986 #ifdef LD_DEC
987 } else {
988 /* LD encoders will give lower delay */
989 if (hDecoder->frame <= 0)
990 hInfo->samples = 0;
992 #endif
994 /* cleanup */
995 #ifdef ANALYSIS
996 fflush(stdout);
997 #endif
999 #ifdef PROFILE
1000 count = faad_get_ts() - count;
1001 hDecoder->cycles += count;
1002 #endif
1004 return hDecoder; /* return void* != NULL */
1006 error:
1008 faad_endbits(&ld);
1010 /* cleanup */
1011 #ifdef ANALYSIS
1012 fflush(stdout);
1013 #endif
1015 return NULL;