Clean up libfaad's fixed point implementation.
[kugel-rb.git] / apps / codecs / libfaad / decoder.c
blobeb810de59e38a70a4660f76a118ff6db76394b98
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 #ifdef ANALYSIS
52 uint16_t dbg_count;
53 #endif
55 /* static function declarations */
56 static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
57 uint8_t *buffer, uint32_t buffer_size,
58 void **sample_buffer, int32_t sample_buffer_size);
59 static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo);
62 char* NEAACDECAPI NeAACDecGetErrorMessage(uint8_t errcode)
64 if (errcode >= NUM_ERROR_MESSAGES)
65 return NULL;
66 return err_msg[errcode];
69 uint32_t NEAACDECAPI NeAACDecGetCapabilities(void)
71 uint32_t cap = 0;
73 /* can't do without it */
74 cap += LC_DEC_CAP;
76 #ifdef MAIN_DEC
77 cap += MAIN_DEC_CAP;
78 #endif
79 #ifdef LTP_DEC
80 cap += LTP_DEC_CAP;
81 #endif
82 #ifdef LD_DEC
83 cap += LD_DEC_CAP;
84 #endif
85 #ifdef ERROR_RESILIENCE
86 cap += ERROR_RESILIENCE_CAP;
87 #endif
88 #ifdef FIXED_POINT
89 cap += FIXED_POINT_CAP;
90 #endif
92 return cap;
95 NeAACDecHandle NEAACDECAPI NeAACDecOpen(void)
97 uint8_t i;
98 NeAACDecHandle hDecoder = NULL;
100 #if defined(CPU_COLDFIRE)
101 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
102 #endif
104 if ((hDecoder = (NeAACDecHandle)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
105 return NULL;
107 memset(hDecoder, 0, sizeof(NeAACDecStruct));
109 hDecoder->config.outputFormat = FAAD_FMT_16BIT;
110 hDecoder->config.defObjectType = MAIN;
111 hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
112 hDecoder->config.downMatrix = 0;
113 hDecoder->adts_header_present = 0;
114 hDecoder->adif_header_present = 0;
115 #ifdef ERROR_RESILIENCE
116 hDecoder->aacSectionDataResilienceFlag = 0;
117 hDecoder->aacScalefactorDataResilienceFlag = 0;
118 hDecoder->aacSpectralDataResilienceFlag = 0;
119 #endif
120 hDecoder->frameLength = 1024;
122 hDecoder->frame = 0;
123 hDecoder->sample_buffer = NULL;
125 for (i = 0; i < MAX_CHANNELS; i++)
127 hDecoder->window_shape_prev[i] = 0;
128 hDecoder->time_out[i] = NULL;
129 hDecoder->fb_intermed[i] = NULL;
130 #ifdef SSR_DEC
131 hDecoder->ssr_overlap[i] = NULL;
132 hDecoder->prev_fmd[i] = NULL;
133 #endif
134 #ifdef MAIN_DEC
135 hDecoder->pred_stat[i] = NULL;
136 #endif
137 #ifdef LTP_DEC
138 hDecoder->ltp_lag[i] = 0;
139 hDecoder->lt_pred_stat[i] = NULL;
140 #endif
143 #ifdef SBR_DEC
144 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
146 hDecoder->sbr[i] = NULL;
148 #endif
150 hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
152 return hDecoder;
155 NeAACDecConfigurationPtr NEAACDECAPI NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder)
157 if (hDecoder)
159 NeAACDecConfigurationPtr config = &(hDecoder->config);
161 return config;
164 return NULL;
167 uint8_t NEAACDECAPI NeAACDecSetConfiguration(NeAACDecHandle hDecoder,
168 NeAACDecConfigurationPtr config)
170 if (hDecoder && config)
172 /* check if we can decode this object type */
173 if (can_decode_ot(config->defObjectType) < 0)
174 return 0;
175 hDecoder->config.defObjectType = config->defObjectType;
177 /* samplerate: anything but 0 should be possible */
178 if (config->defSampleRate == 0)
179 return 0;
180 hDecoder->config.defSampleRate = config->defSampleRate;
182 /* check output format */
183 #ifdef FIXED_POINT
184 if ((config->outputFormat < 1) || (config->outputFormat > 4))
185 return 0;
186 #else
187 if ((config->outputFormat < 1) || (config->outputFormat > 5))
188 return 0;
189 #endif
190 hDecoder->config.outputFormat = config->outputFormat;
192 if (config->downMatrix > 1)
193 return 0;
194 hDecoder->config.downMatrix = config->downMatrix;
196 /* OK */
197 return 1;
200 return 0;
203 int32_t NEAACDECAPI NeAACDecInit(NeAACDecHandle hDecoder, uint8_t *buffer,
204 uint32_t buffer_size,
205 uint32_t *samplerate, uint8_t *channels)
207 uint32_t bits = 0;
208 bitfile ld;
209 adif_header adif;
210 adts_header adts;
212 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL))
213 return -1;
215 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
216 hDecoder->object_type = hDecoder->config.defObjectType;
217 *samplerate = get_sample_rate(hDecoder->sf_index);
218 *channels = 1;
220 if (buffer != NULL)
222 faad_initbits(&ld, buffer, buffer_size);
224 /* Check if an ADIF header is present */
225 if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
226 (buffer[2] == 'I') && (buffer[3] == 'F'))
228 hDecoder->adif_header_present = 1;
230 get_adif_header(&adif, &ld);
231 faad_byte_align(&ld);
233 hDecoder->sf_index = adif.pce[0].sf_index;
234 hDecoder->object_type = adif.pce[0].object_type + 1;
236 *samplerate = get_sample_rate(hDecoder->sf_index);
237 *channels = adif.pce[0].channels;
239 memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
240 hDecoder->pce_set = 1;
242 bits = bit2byte(faad_get_processed_bits(&ld));
244 /* Check if an ADTS header is present */
245 } else if (faad_showbits(&ld, 12) == 0xfff) {
246 hDecoder->adts_header_present = 1;
248 adts.old_format = hDecoder->config.useOldADTSFormat;
249 adts_frame(&adts, &ld);
251 hDecoder->sf_index = adts.sf_index;
252 hDecoder->object_type = adts.profile + 1;
254 *samplerate = get_sample_rate(hDecoder->sf_index);
255 *channels = (adts.channel_configuration > 6) ?
256 2 : adts.channel_configuration;
259 if (ld.error)
261 faad_endbits(&ld);
262 return -1;
264 faad_endbits(&ld);
266 hDecoder->channelConfiguration = *channels;
268 #if (defined(PS_DEC) || defined(DRM_PS))
269 /* check if we have a mono file */
270 if (*channels == 1)
272 /* upMatrix to 2 channels for implicit signalling of PS */
273 *channels = 2;
275 #endif
277 #ifdef SBR_DEC
278 /* implicit signalling */
279 if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR))
281 *samplerate *= 2;
282 hDecoder->forceUpSampling = 1;
283 } else if (*samplerate > 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) {
284 hDecoder->downSampledSBR = 1;
286 #endif
288 /* must be done before frameLength is divided by 2 for LD */
289 #ifdef SSR_DEC
290 if (hDecoder->object_type == SSR)
291 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
292 else
293 #endif
296 #ifdef LD_DEC
297 if (hDecoder->object_type == LD)
298 hDecoder->frameLength >>= 1;
299 #endif
301 if (can_decode_ot(hDecoder->object_type) < 0)
302 return -1;
304 return bits;
307 /* Init the library using a DecoderSpecificInfo */
308 int8_t NEAACDECAPI NeAACDecInit2(NeAACDecHandle hDecoder, uint8_t *pBuffer,
309 uint32_t SizeOfDecoderSpecificInfo,
310 uint32_t *samplerate, uint8_t *channels)
312 int8_t rc;
313 mp4AudioSpecificConfig mp4ASC;
315 if((hDecoder == NULL)
316 || (pBuffer == NULL)
317 || (SizeOfDecoderSpecificInfo < 2)
318 || (samplerate == NULL)
319 || (channels == NULL))
321 return -1;
324 hDecoder->adif_header_present = 0;
325 hDecoder->adts_header_present = 0;
327 /* decode the audio specific config */
328 rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
329 &(hDecoder->pce));
331 /* copy the relevant info to the decoder handle */
332 *samplerate = mp4ASC.samplingFrequency;
333 if (mp4ASC.channelsConfiguration)
335 *channels = mp4ASC.channelsConfiguration;
336 } else {
337 *channels = hDecoder->pce.channels;
338 hDecoder->pce_set = 1;
340 #if (defined(PS_DEC) || defined(DRM_PS))
341 /* check if we have a mono file */
342 if (*channels == 1)
344 /* upMatrix to 2 channels for implicit signalling of PS */
345 *channels = 2;
347 #endif
348 hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
349 hDecoder->object_type = mp4ASC.objectTypeIndex;
350 #ifdef ERROR_RESILIENCE
351 hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
352 hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
353 hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
354 #endif
355 #ifdef SBR_DEC
356 hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
357 hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
358 if (hDecoder->config.dontUpSampleImplicitSBR == 0)
359 hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
360 else
361 hDecoder->forceUpSampling = 0;
363 /* AAC core decoder samplerate is 2 times as low */
364 if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
366 hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
368 #endif
370 if (rc != 0)
372 return rc;
374 hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
375 if (mp4ASC.frameLengthFlag)
376 #ifdef ALLOW_SMALL_FRAMELENGTH
377 hDecoder->frameLength = 960;
378 #else
379 return -1;
380 #endif
382 /* must be done before frameLength is divided by 2 for LD */
383 #ifdef SSR_DEC
384 if (hDecoder->object_type == SSR)
385 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
386 else
387 #endif
389 #ifdef LD_DEC
390 if (hDecoder->object_type == LD)
391 hDecoder->frameLength >>= 1;
392 #endif
394 return 0;
397 #ifdef DRM
398 int8_t NEAACDECAPI NeAACDecInitDRM(NeAACDecHandle *hDecoder, uint32_t samplerate,
399 uint8_t channels)
401 if (hDecoder == NULL)
402 return 1; /* error */
404 NeAACDecClose(*hDecoder);
406 *hDecoder = NeAACDecOpen();
408 /* Special object type defined for DRM */
409 (*hDecoder)->config.defObjectType = DRM_ER_LC;
411 (*hDecoder)->config.defSampleRate = samplerate;
412 #ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
413 (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
414 (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
415 (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
416 #endif
417 (*hDecoder)->frameLength = 960;
418 (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
419 (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
421 if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
422 (*hDecoder)->channelConfiguration = 2;
423 else
424 (*hDecoder)->channelConfiguration = 1;
426 #ifdef SBR_DEC
427 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
428 (*hDecoder)->sbr_present_flag = 0;
429 else
430 (*hDecoder)->sbr_present_flag = 1;
431 #endif
434 return 0;
436 #endif
438 void NEAACDECAPI NeAACDecClose(NeAACDecHandle hDecoder)
440 uint8_t i;
442 if (hDecoder == NULL)
443 return;
445 #ifdef PROFILE
446 printf("AAC decoder total: %I64d cycles\n", hDecoder->cycles);
447 printf("requant: %I64d cycles\n", hDecoder->requant_cycles);
448 printf("spectral_data: %I64d cycles\n", hDecoder->spectral_cycles);
449 printf("scalefactors: %I64d cycles\n", hDecoder->scalefac_cycles);
450 printf("output: %I64d cycles\n", hDecoder->output_cycles);
451 #endif
453 for (i = 0; i < MAX_CHANNELS; i++)
455 if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
456 if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]);
457 #ifdef SSR_DEC
458 if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]);
459 if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]);
460 #endif
461 #ifdef MAIN_DEC
462 if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
463 #endif
464 #ifdef LTP_DEC
465 if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
466 #endif
469 #ifdef SSR_DEC
470 if (hDecoder->object_type == SSR)
471 ssr_filter_bank_end(hDecoder->fb);
472 else
473 #endif
476 drc_end(hDecoder->drc);
478 if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
480 #ifdef SBR_DEC
481 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
483 if (hDecoder->sbr[i])
484 sbrDecodeEnd(hDecoder->sbr[i]);
486 #endif
488 if (hDecoder) faad_free(hDecoder);
491 void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hDecoder, int32_t frame)
493 if (hDecoder)
495 hDecoder->postSeekResetFlag = 1;
497 if (frame != -1)
498 hDecoder->frame = frame;
502 static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo)
504 hInfo->num_front_channels = 0;
505 hInfo->num_side_channels = 0;
506 hInfo->num_back_channels = 0;
507 hInfo->num_lfe_channels = 0;
508 memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
510 if (hDecoder->downMatrix)
512 hInfo->num_front_channels = 2;
513 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
514 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
515 return;
518 /* check if there is a PCE */
519 if (hDecoder->pce_set)
521 uint8_t i, chpos = 0;
522 uint8_t chdir, back_center = 0;
524 hInfo->num_front_channels = hDecoder->pce.num_front_channels;
525 hInfo->num_side_channels = hDecoder->pce.num_side_channels;
526 hInfo->num_back_channels = hDecoder->pce.num_back_channels;
527 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
529 chdir = hInfo->num_front_channels;
530 if (chdir & 1)
532 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
533 chdir--;
535 for (i = 0; i < chdir; i += 2)
537 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
538 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
541 for (i = 0; i < hInfo->num_side_channels; i += 2)
543 hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT;
544 hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT;
547 chdir = hInfo->num_back_channels;
548 if (chdir & 1)
550 back_center = 1;
551 chdir--;
553 for (i = 0; i < chdir; i += 2)
555 hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT;
556 hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT;
558 if (back_center)
560 hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
563 for (i = 0; i < hInfo->num_lfe_channels; i++)
565 hInfo->channel_position[chpos++] = LFE_CHANNEL;
568 } else {
569 switch (hDecoder->channelConfiguration)
571 case 1:
572 hInfo->num_front_channels = 1;
573 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
574 break;
575 case 2:
576 hInfo->num_front_channels = 2;
577 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
578 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
579 break;
580 case 3:
581 hInfo->num_front_channels = 3;
582 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
583 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
584 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
585 break;
586 case 4:
587 hInfo->num_front_channels = 3;
588 hInfo->num_back_channels = 1;
589 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
590 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
591 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
592 hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
593 break;
594 case 5:
595 hInfo->num_front_channels = 3;
596 hInfo->num_back_channels = 2;
597 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
598 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
599 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
600 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
601 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
602 break;
603 case 6:
604 hInfo->num_front_channels = 3;
605 hInfo->num_back_channels = 2;
606 hInfo->num_lfe_channels = 1;
607 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
608 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
609 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
610 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
611 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
612 hInfo->channel_position[5] = LFE_CHANNEL;
613 break;
614 case 7:
615 hInfo->num_front_channels = 3;
616 hInfo->num_side_channels = 2;
617 hInfo->num_back_channels = 2;
618 hInfo->num_lfe_channels = 1;
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 hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
623 hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
624 hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
625 hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
626 hInfo->channel_position[7] = LFE_CHANNEL;
627 break;
628 default: /* channelConfiguration == 0 || channelConfiguration > 7 */
630 uint8_t i;
631 uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
632 if (ch & 1) /* there's either a center front or a center back channel */
634 uint8_t ch1 = (ch-1)/2;
635 if (hDecoder->first_syn_ele == ID_SCE)
637 hInfo->num_front_channels = ch1 + 1;
638 hInfo->num_back_channels = ch1;
639 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
640 for (i = 1; i <= ch1; i+=2)
642 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
643 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
645 for (i = ch1+1; i < ch; i+=2)
647 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
648 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
650 } else {
651 hInfo->num_front_channels = ch1;
652 hInfo->num_back_channels = ch1 + 1;
653 for (i = 0; i < ch1; i+=2)
655 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
656 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
658 for (i = ch1; i < ch-1; i+=2)
660 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
661 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
663 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
665 } else {
666 uint8_t ch1 = (ch)/2;
667 hInfo->num_front_channels = ch1;
668 hInfo->num_back_channels = ch1;
669 if (ch1 & 1)
671 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
672 for (i = 1; i <= ch1; i+=2)
674 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
675 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
677 for (i = ch1+1; i < ch-1; i+=2)
679 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
680 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
682 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
683 } else {
684 for (i = 0; i < ch1; i+=2)
686 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
687 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
689 for (i = ch1; i < ch; i+=2)
691 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
692 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
696 hInfo->num_lfe_channels = hDecoder->has_lfe;
697 for (i = ch; i < hDecoder->fr_channels; i++)
699 hInfo->channel_position[i] = LFE_CHANNEL;
702 break;
707 void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hDecoder,
708 NeAACDecFrameInfo *hInfo,
709 uint8_t *buffer, uint32_t buffer_size)
711 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
714 void* NEAACDECAPI NeAACDecDecode2(NeAACDecHandle hDecoder,
715 NeAACDecFrameInfo *hInfo,
716 uint8_t *buffer, uint32_t buffer_size,
717 void **sample_buffer, uint32_t sample_buffer_size)
719 if ((sample_buffer == NULL) || (sample_buffer_size == 0))
721 hInfo->error = 27;
722 return NULL;
725 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
726 sample_buffer, sample_buffer_size);
729 static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
730 uint8_t *buffer, uint32_t buffer_size,
731 void **sample_buffer2, int32_t sample_buffer_size)
733 uint8_t channels = 0;
734 uint8_t output_channels = 0;
735 bitfile ld;
736 uint32_t bitsconsumed;
737 uint16_t frame_len;
738 void *sample_buffer;
740 #ifdef PROFILE
741 int64_t count = faad_get_ts();
742 #endif
744 /* safety checks */
745 if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
747 return NULL;
750 #if 0
751 printf("%d\n", buffer_size*8);
752 #endif
754 frame_len = hDecoder->frameLength;
757 memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
758 memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
760 /* initialize the bitstream */
761 faad_initbits(&ld, buffer, buffer_size);
763 #if 0
765 int i;
766 for (i = 0; i < ((buffer_size+3)>>2); i++)
768 uint8_t *buf;
769 uint32_t temp = 0;
770 buf = faad_getbitbuffer(&ld, 32);
771 //temp = getdword((void*)buf);
772 temp = *((uint32_t*)buf);
773 printf("0x%.8X\n", temp);
774 free(buf);
776 faad_endbits(&ld);
777 faad_initbits(&ld, buffer, buffer_size);
779 #endif
781 #ifdef DRM
782 if (hDecoder->object_type == DRM_ER_LC)
784 /* We do not support stereo right now */
785 if (0) //(hDecoder->channelConfiguration == 2)
787 hInfo->error = 8; // Throw CRC error
788 goto error;
791 faad_getbits(&ld, 8
792 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
794 #endif
796 if (hDecoder->adts_header_present)
798 adts_header adts;
800 adts.old_format = hDecoder->config.useOldADTSFormat;
801 if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
802 goto error;
804 /* MPEG2 does byte_alignment() here,
805 * but ADTS header is always multiple of 8 bits in MPEG2
806 * so not needed to actually do it.
810 #ifdef ANALYSIS
811 dbg_count = 0;
812 #endif
814 /* decode the complete bitstream */
815 #ifdef SCALABLE_DEC
816 if ((hDecoder->object_type == 6) || (hDecoder->object_type == DRM_ER_LC))
818 aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
819 } else {
820 #endif
821 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
822 #ifdef SCALABLE_DEC
824 #endif
826 channels = hDecoder->fr_channels;
828 if (hInfo->error > 0)
829 goto error;
831 /* safety check */
832 if (channels == 0 || channels > MAX_CHANNELS)
834 /* invalid number of channels */
835 hInfo->error = 12;
836 goto error;
839 /* no more bit reading after this */
840 bitsconsumed = faad_get_processed_bits(&ld);
841 hInfo->bytesconsumed = bit2byte(bitsconsumed);
842 if (ld.error)
844 hInfo->error = 14;
845 goto error;
847 faad_endbits(&ld);
850 if (!hDecoder->adts_header_present && !hDecoder->adif_header_present)
852 if (hDecoder->channelConfiguration == 0)
853 hDecoder->channelConfiguration = channels;
855 if (channels == 8) /* 7.1 */
856 hDecoder->channelConfiguration = 7;
857 if (channels == 7) /* not a standard channelConfiguration */
858 hDecoder->channelConfiguration = 0;
861 if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
863 hDecoder->downMatrix = 1;
864 output_channels = 2;
865 } else {
866 output_channels = channels;
869 #if (defined(PS_DEC) || defined(DRM_PS))
870 hDecoder->upMatrix = 0;
871 /* check if we have a mono file */
872 if (output_channels == 1)
874 /* upMatrix to 2 channels for implicit signalling of PS */
875 hDecoder->upMatrix = 1;
876 output_channels = 2;
878 #endif
880 /* Make a channel configuration based on either a PCE or a channelConfiguration */
881 create_channel_config(hDecoder, hInfo);
883 /* number of samples in this frame */
884 hInfo->samples = frame_len*output_channels;
885 /* number of channels in this frame */
886 hInfo->channels = output_channels;
887 /* samplerate */
888 hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
889 /* object type */
890 hInfo->object_type = hDecoder->object_type;
891 /* sbr */
892 hInfo->sbr = NO_SBR;
893 /* header type */
894 hInfo->header_type = RAW;
895 if (hDecoder->adif_header_present)
896 hInfo->header_type = ADIF;
897 if (hDecoder->adts_header_present)
898 hInfo->header_type = ADTS;
899 #if (defined(PS_DEC) || defined(DRM_PS))
900 hInfo->ps = hDecoder->ps_used_global;
901 #endif
903 /* check if frame has channel elements */
904 if (channels == 0)
906 hDecoder->frame++;
907 return NULL;
910 /* allocate the buffer for the final samples */
911 if ((hDecoder->sample_buffer == NULL) ||
912 (hDecoder->alloced_channels != output_channels))
914 static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
915 sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
916 sizeof(int16_t), sizeof(int16_t), 0, 0, 0
918 uint8_t stride = str[hDecoder->config.outputFormat-1];
919 #ifdef SBR_DEC
920 if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
922 stride = 2 * stride;
924 #endif
925 /* check if we want to use internal sample_buffer */
926 if (sample_buffer_size == 0)
928 if (hDecoder->sample_buffer)
929 faad_free(hDecoder->sample_buffer);
930 hDecoder->sample_buffer = NULL;
931 hDecoder->sample_buffer = faad_malloc(frame_len*output_channels*stride);
932 } else if (sample_buffer_size < frame_len*output_channels*stride) {
933 /* provided sample buffer is not big enough */
934 hInfo->error = 27;
935 return NULL;
937 hDecoder->alloced_channels = output_channels;
940 if (sample_buffer_size == 0)
942 sample_buffer = hDecoder->sample_buffer;
943 } else {
944 sample_buffer = *sample_buffer2;
947 #ifdef SBR_DEC
948 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
950 uint8_t ele;
952 /* this data is different when SBR is used or when the data is upsampled */
953 if (!hDecoder->downSampledSBR)
955 frame_len *= 2;
956 hInfo->samples *= 2;
957 hInfo->samplerate *= 2;
960 /* check if every element was provided with SBR data */
961 for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
963 if (hDecoder->sbr[ele] == NULL)
965 hInfo->error = 25;
966 goto error;
970 /* sbr */
971 if (hDecoder->sbr_present_flag == 1)
973 hInfo->object_type = HE_AAC;
974 hInfo->sbr = SBR_UPSAMPLED;
975 } else {
976 hInfo->sbr = NO_SBR_UPSAMPLED;
978 if (hDecoder->downSampledSBR)
980 hInfo->sbr = SBR_DOWNSAMPLED;
983 #endif
985 /* we don't need sample conversion in rockbox.
986 sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
987 output_channels, frame_len, hDecoder->config.outputFormat);
990 hDecoder->postSeekResetFlag = 0;
992 hDecoder->frame++;
993 #ifdef LD_DEC
994 if (hDecoder->object_type != LD)
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;
1005 #endif
1007 /* cleanup */
1008 #ifdef ANALYSIS
1009 fflush(stdout);
1010 #endif
1012 #ifdef PROFILE
1013 count = faad_get_ts() - count;
1014 hDecoder->cycles += count;
1015 #endif
1017 return sample_buffer;
1019 error:
1021 faad_endbits(&ld);
1023 /* cleanup */
1024 #ifdef ANALYSIS
1025 fflush(stdout);
1026 #endif
1028 return NULL;