Add gigabeat F/S volume limits to the manual, and a few minor formatting changes.
[kugel-rb.git] / apps / codecs / libfaad / decoder.c
blobdb51d2ab6d07f3ed671261f4384939f37d163bd8
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 #include "output.h"
39 #include "filtbank.h"
40 #include "drc.h"
41 #ifdef SBR_DEC
42 #include "sbr_dec.h"
43 #include "sbr_syntax.h"
44 #endif
45 #ifdef SSR_DEC
46 #include "ssr.h"
47 #endif
49 #ifdef ANALYSIS
50 uint16_t dbg_count;
51 #endif
53 /* static function declarations */
54 static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
55 uint8_t *buffer, uint32_t buffer_size,
56 void **sample_buffer, int32_t sample_buffer_size);
57 static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo);
60 char* NEAACDECAPI NeAACDecGetErrorMessage(uint8_t errcode)
62 if (errcode >= NUM_ERROR_MESSAGES)
63 return NULL;
64 return err_msg[errcode];
67 uint32_t NEAACDECAPI NeAACDecGetCapabilities(void)
69 uint32_t cap = 0;
71 /* can't do without it */
72 cap += LC_DEC_CAP;
74 #ifdef MAIN_DEC
75 cap += MAIN_DEC_CAP;
76 #endif
77 #ifdef LTP_DEC
78 cap += LTP_DEC_CAP;
79 #endif
80 #ifdef LD_DEC
81 cap += LD_DEC_CAP;
82 #endif
83 #ifdef ERROR_RESILIENCE
84 cap += ERROR_RESILIENCE_CAP;
85 #endif
86 #ifdef FIXED_POINT
87 cap += FIXED_POINT_CAP;
88 #endif
90 return cap;
93 NeAACDecHandle NEAACDECAPI NeAACDecOpen(void)
95 uint8_t i;
96 NeAACDecHandle hDecoder = NULL;
98 #if defined(CPU_COLDFIRE)
99 coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE);
100 #endif
102 if ((hDecoder = (NeAACDecHandle)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
103 return NULL;
105 memset(hDecoder, 0, sizeof(NeAACDecStruct));
107 hDecoder->config.outputFormat = FAAD_FMT_16BIT;
108 hDecoder->config.defObjectType = MAIN;
109 hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
110 hDecoder->config.downMatrix = 0;
111 hDecoder->adts_header_present = 0;
112 hDecoder->adif_header_present = 0;
113 #ifdef ERROR_RESILIENCE
114 hDecoder->aacSectionDataResilienceFlag = 0;
115 hDecoder->aacScalefactorDataResilienceFlag = 0;
116 hDecoder->aacSpectralDataResilienceFlag = 0;
117 #endif
118 hDecoder->frameLength = 1024;
120 hDecoder->frame = 0;
121 hDecoder->sample_buffer = NULL;
123 for (i = 0; i < MAX_CHANNELS; i++)
125 hDecoder->window_shape_prev[i] = 0;
126 hDecoder->time_out[i] = NULL;
127 hDecoder->fb_intermed[i] = NULL;
128 #ifdef SSR_DEC
129 hDecoder->ssr_overlap[i] = NULL;
130 hDecoder->prev_fmd[i] = NULL;
131 #endif
132 #ifdef MAIN_DEC
133 hDecoder->pred_stat[i] = NULL;
134 #endif
135 #ifdef LTP_DEC
136 hDecoder->ltp_lag[i] = 0;
137 hDecoder->lt_pred_stat[i] = NULL;
138 #endif
141 #ifdef SBR_DEC
142 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
144 hDecoder->sbr[i] = NULL;
146 #endif
148 hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
150 return hDecoder;
153 NeAACDecConfigurationPtr NEAACDECAPI NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder)
155 if (hDecoder)
157 NeAACDecConfigurationPtr config = &(hDecoder->config);
159 return config;
162 return NULL;
165 uint8_t NEAACDECAPI NeAACDecSetConfiguration(NeAACDecHandle hDecoder,
166 NeAACDecConfigurationPtr config)
168 if (hDecoder && config)
170 /* check if we can decode this object type */
171 if (can_decode_ot(config->defObjectType) < 0)
172 return 0;
173 hDecoder->config.defObjectType = config->defObjectType;
175 /* samplerate: anything but 0 should be possible */
176 if (config->defSampleRate == 0)
177 return 0;
178 hDecoder->config.defSampleRate = config->defSampleRate;
180 /* check output format */
181 #ifdef FIXED_POINT
182 if ((config->outputFormat < 1) || (config->outputFormat > 4))
183 return 0;
184 #else
185 if ((config->outputFormat < 1) || (config->outputFormat > 5))
186 return 0;
187 #endif
188 hDecoder->config.outputFormat = config->outputFormat;
190 if (config->downMatrix > 1)
191 return 0;
192 hDecoder->config.downMatrix = config->downMatrix;
194 /* OK */
195 return 1;
198 return 0;
201 int32_t NEAACDECAPI NeAACDecInit(NeAACDecHandle hDecoder, uint8_t *buffer,
202 uint32_t buffer_size,
203 uint32_t *samplerate, uint8_t *channels)
205 uint32_t bits = 0;
206 bitfile ld;
207 adif_header adif;
208 adts_header adts;
210 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL))
211 return -1;
213 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
214 hDecoder->object_type = hDecoder->config.defObjectType;
215 *samplerate = get_sample_rate(hDecoder->sf_index);
216 *channels = 1;
218 if (buffer != NULL)
220 faad_initbits(&ld, buffer, buffer_size);
222 /* Check if an ADIF header is present */
223 if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
224 (buffer[2] == 'I') && (buffer[3] == 'F'))
226 hDecoder->adif_header_present = 1;
228 get_adif_header(&adif, &ld);
229 faad_byte_align(&ld);
231 hDecoder->sf_index = adif.pce[0].sf_index;
232 hDecoder->object_type = adif.pce[0].object_type + 1;
234 *samplerate = get_sample_rate(hDecoder->sf_index);
235 *channels = adif.pce[0].channels;
237 memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
238 hDecoder->pce_set = 1;
240 bits = bit2byte(faad_get_processed_bits(&ld));
242 /* Check if an ADTS header is present */
243 } else if (faad_showbits(&ld, 12) == 0xfff) {
244 hDecoder->adts_header_present = 1;
246 adts.old_format = hDecoder->config.useOldADTSFormat;
247 adts_frame(&adts, &ld);
249 hDecoder->sf_index = adts.sf_index;
250 hDecoder->object_type = adts.profile + 1;
252 *samplerate = get_sample_rate(hDecoder->sf_index);
253 *channels = (adts.channel_configuration > 6) ?
254 2 : adts.channel_configuration;
257 if (ld.error)
259 faad_endbits(&ld);
260 return -1;
262 faad_endbits(&ld);
264 hDecoder->channelConfiguration = *channels;
266 #if (defined(PS_DEC) || defined(DRM_PS))
267 /* check if we have a mono file */
268 if (*channels == 1)
270 /* upMatrix to 2 channels for implicit signalling of PS */
271 *channels = 2;
273 #endif
275 #ifdef SBR_DEC
276 /* implicit signalling */
277 if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR))
279 *samplerate *= 2;
280 hDecoder->forceUpSampling = 1;
281 } else if (*samplerate > 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) {
282 hDecoder->downSampledSBR = 1;
284 #endif
286 /* must be done before frameLength is divided by 2 for LD */
287 #ifdef SSR_DEC
288 if (hDecoder->object_type == SSR)
289 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
290 else
291 #endif
294 #ifdef LD_DEC
295 if (hDecoder->object_type == LD)
296 hDecoder->frameLength >>= 1;
297 #endif
299 if (can_decode_ot(hDecoder->object_type) < 0)
300 return -1;
302 return bits;
305 /* Init the library using a DecoderSpecificInfo */
306 int8_t NEAACDECAPI NeAACDecInit2(NeAACDecHandle hDecoder, uint8_t *pBuffer,
307 uint32_t SizeOfDecoderSpecificInfo,
308 uint32_t *samplerate, uint8_t *channels)
310 int8_t rc;
311 mp4AudioSpecificConfig mp4ASC;
313 if((hDecoder == NULL)
314 || (pBuffer == NULL)
315 || (SizeOfDecoderSpecificInfo < 2)
316 || (samplerate == NULL)
317 || (channels == NULL))
319 return -1;
322 hDecoder->adif_header_present = 0;
323 hDecoder->adts_header_present = 0;
325 /* decode the audio specific config */
326 rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
327 &(hDecoder->pce));
329 /* copy the relevant info to the decoder handle */
330 *samplerate = mp4ASC.samplingFrequency;
331 if (mp4ASC.channelsConfiguration)
333 *channels = mp4ASC.channelsConfiguration;
334 } else {
335 *channels = hDecoder->pce.channels;
336 hDecoder->pce_set = 1;
338 #if (defined(PS_DEC) || defined(DRM_PS))
339 /* check if we have a mono file */
340 if (*channels == 1)
342 /* upMatrix to 2 channels for implicit signalling of PS */
343 *channels = 2;
345 #endif
346 hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
347 hDecoder->object_type = mp4ASC.objectTypeIndex;
348 #ifdef ERROR_RESILIENCE
349 hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
350 hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
351 hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
352 #endif
353 #ifdef SBR_DEC
354 hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
355 hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
356 if (hDecoder->config.dontUpSampleImplicitSBR == 0)
357 hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
358 else
359 hDecoder->forceUpSampling = 0;
361 /* AAC core decoder samplerate is 2 times as low */
362 if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
364 hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
366 #endif
368 if (rc != 0)
370 return rc;
372 hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
373 if (mp4ASC.frameLengthFlag)
374 #ifdef ALLOW_SMALL_FRAMELENGTH
375 hDecoder->frameLength = 960;
376 #else
377 return -1;
378 #endif
380 /* must be done before frameLength is divided by 2 for LD */
381 #ifdef SSR_DEC
382 if (hDecoder->object_type == SSR)
383 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
384 else
385 #endif
387 #ifdef LD_DEC
388 if (hDecoder->object_type == LD)
389 hDecoder->frameLength >>= 1;
390 #endif
392 return 0;
395 #ifdef DRM
396 int8_t NEAACDECAPI NeAACDecInitDRM(NeAACDecHandle *hDecoder, uint32_t samplerate,
397 uint8_t channels)
399 if (hDecoder == NULL)
400 return 1; /* error */
402 NeAACDecClose(*hDecoder);
404 *hDecoder = NeAACDecOpen();
406 /* Special object type defined for DRM */
407 (*hDecoder)->config.defObjectType = DRM_ER_LC;
409 (*hDecoder)->config.defSampleRate = samplerate;
410 #ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
411 (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
412 (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
413 (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
414 #endif
415 (*hDecoder)->frameLength = 960;
416 (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
417 (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
419 if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
420 (*hDecoder)->channelConfiguration = 2;
421 else
422 (*hDecoder)->channelConfiguration = 1;
424 #ifdef SBR_DEC
425 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
426 (*hDecoder)->sbr_present_flag = 0;
427 else
428 (*hDecoder)->sbr_present_flag = 1;
429 #endif
432 return 0;
434 #endif
436 void NEAACDECAPI NeAACDecClose(NeAACDecHandle hDecoder)
438 uint8_t i;
440 if (hDecoder == NULL)
441 return;
443 #ifdef PROFILE
444 printf("AAC decoder total: %I64d cycles\n", hDecoder->cycles);
445 printf("requant: %I64d cycles\n", hDecoder->requant_cycles);
446 printf("spectral_data: %I64d cycles\n", hDecoder->spectral_cycles);
447 printf("scalefactors: %I64d cycles\n", hDecoder->scalefac_cycles);
448 printf("output: %I64d cycles\n", hDecoder->output_cycles);
449 #endif
451 for (i = 0; i < MAX_CHANNELS; i++)
453 if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
454 if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]);
455 #ifdef SSR_DEC
456 if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]);
457 if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]);
458 #endif
459 #ifdef MAIN_DEC
460 if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
461 #endif
462 #ifdef LTP_DEC
463 if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
464 #endif
467 #ifdef SSR_DEC
468 if (hDecoder->object_type == SSR)
469 ssr_filter_bank_end(hDecoder->fb);
470 else
471 #endif
474 drc_end(hDecoder->drc);
476 if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
478 #ifdef SBR_DEC
479 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
481 if (hDecoder->sbr[i])
482 sbrDecodeEnd(hDecoder->sbr[i]);
484 #endif
486 if (hDecoder) faad_free(hDecoder);
489 void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hDecoder, int32_t frame)
491 if (hDecoder)
493 hDecoder->postSeekResetFlag = 1;
495 if (frame != -1)
496 hDecoder->frame = frame;
500 static void create_channel_config(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo)
502 hInfo->num_front_channels = 0;
503 hInfo->num_side_channels = 0;
504 hInfo->num_back_channels = 0;
505 hInfo->num_lfe_channels = 0;
506 memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
508 if (hDecoder->downMatrix)
510 hInfo->num_front_channels = 2;
511 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
512 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
513 return;
516 /* check if there is a PCE */
517 if (hDecoder->pce_set)
519 uint8_t i, chpos = 0;
520 uint8_t chdir, back_center = 0;
522 hInfo->num_front_channels = hDecoder->pce.num_front_channels;
523 hInfo->num_side_channels = hDecoder->pce.num_side_channels;
524 hInfo->num_back_channels = hDecoder->pce.num_back_channels;
525 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
527 chdir = hInfo->num_front_channels;
528 if (chdir & 1)
530 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
531 chdir--;
533 for (i = 0; i < chdir; i += 2)
535 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
536 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
539 for (i = 0; i < hInfo->num_side_channels; i += 2)
541 hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT;
542 hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT;
545 chdir = hInfo->num_back_channels;
546 if (chdir & 1)
548 back_center = 1;
549 chdir--;
551 for (i = 0; i < chdir; i += 2)
553 hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT;
554 hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT;
556 if (back_center)
558 hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
561 for (i = 0; i < hInfo->num_lfe_channels; i++)
563 hInfo->channel_position[chpos++] = LFE_CHANNEL;
566 } else {
567 switch (hDecoder->channelConfiguration)
569 case 1:
570 hInfo->num_front_channels = 1;
571 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
572 break;
573 case 2:
574 hInfo->num_front_channels = 2;
575 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
576 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
577 break;
578 case 3:
579 hInfo->num_front_channels = 3;
580 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
581 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
582 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
583 break;
584 case 4:
585 hInfo->num_front_channels = 3;
586 hInfo->num_back_channels = 1;
587 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
588 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
589 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
590 hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
591 break;
592 case 5:
593 hInfo->num_front_channels = 3;
594 hInfo->num_back_channels = 2;
595 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
596 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
597 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
598 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
599 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
600 break;
601 case 6:
602 hInfo->num_front_channels = 3;
603 hInfo->num_back_channels = 2;
604 hInfo->num_lfe_channels = 1;
605 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
606 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
607 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
608 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
609 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
610 hInfo->channel_position[5] = LFE_CHANNEL;
611 break;
612 case 7:
613 hInfo->num_front_channels = 3;
614 hInfo->num_side_channels = 2;
615 hInfo->num_back_channels = 2;
616 hInfo->num_lfe_channels = 1;
617 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
618 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
619 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
620 hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
621 hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
622 hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
623 hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
624 hInfo->channel_position[7] = LFE_CHANNEL;
625 break;
626 default: /* channelConfiguration == 0 || channelConfiguration > 7 */
628 uint8_t i;
629 uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
630 if (ch & 1) /* there's either a center front or a center back channel */
632 uint8_t ch1 = (ch-1)/2;
633 if (hDecoder->first_syn_ele == ID_SCE)
635 hInfo->num_front_channels = ch1 + 1;
636 hInfo->num_back_channels = ch1;
637 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
638 for (i = 1; i <= ch1; i+=2)
640 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
641 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
643 for (i = ch1+1; i < ch; i+=2)
645 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
646 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
648 } else {
649 hInfo->num_front_channels = ch1;
650 hInfo->num_back_channels = ch1 + 1;
651 for (i = 0; i < ch1; i+=2)
653 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
654 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
656 for (i = ch1; i < ch-1; i+=2)
658 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
659 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
661 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
663 } else {
664 uint8_t ch1 = (ch)/2;
665 hInfo->num_front_channels = ch1;
666 hInfo->num_back_channels = ch1;
667 if (ch1 & 1)
669 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
670 for (i = 1; i <= ch1; i+=2)
672 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
673 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
675 for (i = ch1+1; i < ch-1; i+=2)
677 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
678 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
680 hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
681 } else {
682 for (i = 0; i < ch1; i+=2)
684 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
685 hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
687 for (i = ch1; i < ch; i+=2)
689 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
690 hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
694 hInfo->num_lfe_channels = hDecoder->has_lfe;
695 for (i = ch; i < hDecoder->fr_channels; i++)
697 hInfo->channel_position[i] = LFE_CHANNEL;
700 break;
705 void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hDecoder,
706 NeAACDecFrameInfo *hInfo,
707 uint8_t *buffer, uint32_t buffer_size)
709 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
712 void* NEAACDECAPI NeAACDecDecode2(NeAACDecHandle hDecoder,
713 NeAACDecFrameInfo *hInfo,
714 uint8_t *buffer, uint32_t buffer_size,
715 void **sample_buffer, uint32_t sample_buffer_size)
717 if ((sample_buffer == NULL) || (sample_buffer_size == 0))
719 hInfo->error = 27;
720 return NULL;
723 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
724 sample_buffer, sample_buffer_size);
727 static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
728 uint8_t *buffer, uint32_t buffer_size,
729 void **sample_buffer2, int32_t sample_buffer_size)
731 uint8_t channels = 0;
732 uint8_t output_channels = 0;
733 bitfile ld;
734 uint32_t bitsconsumed;
735 uint16_t frame_len;
736 void *sample_buffer;
738 #ifdef PROFILE
739 int64_t count = faad_get_ts();
740 #endif
742 /* safety checks */
743 if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
745 return NULL;
748 #if 0
749 printf("%d\n", buffer_size*8);
750 #endif
752 frame_len = hDecoder->frameLength;
755 memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
756 memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
758 /* initialize the bitstream */
759 faad_initbits(&ld, buffer, buffer_size);
761 #if 0
763 int i;
764 for (i = 0; i < ((buffer_size+3)>>2); i++)
766 uint8_t *buf;
767 uint32_t temp = 0;
768 buf = faad_getbitbuffer(&ld, 32);
769 //temp = getdword((void*)buf);
770 temp = *((uint32_t*)buf);
771 printf("0x%.8X\n", temp);
772 free(buf);
774 faad_endbits(&ld);
775 faad_initbits(&ld, buffer, buffer_size);
777 #endif
779 #ifdef DRM
780 if (hDecoder->object_type == DRM_ER_LC)
782 /* We do not support stereo right now */
783 if (0) //(hDecoder->channelConfiguration == 2)
785 hInfo->error = 8; // Throw CRC error
786 goto error;
789 faad_getbits(&ld, 8
790 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
792 #endif
794 if (hDecoder->adts_header_present)
796 adts_header adts;
798 adts.old_format = hDecoder->config.useOldADTSFormat;
799 if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
800 goto error;
802 /* MPEG2 does byte_alignment() here,
803 * but ADTS header is always multiple of 8 bits in MPEG2
804 * so not needed to actually do it.
808 #ifdef ANALYSIS
809 dbg_count = 0;
810 #endif
812 /* decode the complete bitstream */
813 #ifdef SCALABLE_DEC
814 if ((hDecoder->object_type == 6) || (hDecoder->object_type == DRM_ER_LC))
816 aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
817 } else {
818 #endif
819 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
820 #ifdef SCALABLE_DEC
822 #endif
824 channels = hDecoder->fr_channels;
826 if (hInfo->error > 0)
827 goto error;
829 /* safety check */
830 if (channels == 0 || channels > MAX_CHANNELS)
832 /* invalid number of channels */
833 hInfo->error = 12;
834 goto error;
837 /* no more bit reading after this */
838 bitsconsumed = faad_get_processed_bits(&ld);
839 hInfo->bytesconsumed = bit2byte(bitsconsumed);
840 if (ld.error)
842 hInfo->error = 14;
843 goto error;
845 faad_endbits(&ld);
848 if (!hDecoder->adts_header_present && !hDecoder->adif_header_present)
850 if (hDecoder->channelConfiguration == 0)
851 hDecoder->channelConfiguration = channels;
853 if (channels == 8) /* 7.1 */
854 hDecoder->channelConfiguration = 7;
855 if (channels == 7) /* not a standard channelConfiguration */
856 hDecoder->channelConfiguration = 0;
859 if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
861 hDecoder->downMatrix = 1;
862 output_channels = 2;
863 } else {
864 output_channels = channels;
867 #if (defined(PS_DEC) || defined(DRM_PS))
868 hDecoder->upMatrix = 0;
869 /* check if we have a mono file */
870 if (output_channels == 1)
872 /* upMatrix to 2 channels for implicit signalling of PS */
873 hDecoder->upMatrix = 1;
874 output_channels = 2;
876 #endif
878 /* Make a channel configuration based on either a PCE or a channelConfiguration */
879 create_channel_config(hDecoder, hInfo);
881 /* number of samples in this frame */
882 hInfo->samples = frame_len*output_channels;
883 /* number of channels in this frame */
884 hInfo->channels = output_channels;
885 /* samplerate */
886 hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
887 /* object type */
888 hInfo->object_type = hDecoder->object_type;
889 /* sbr */
890 hInfo->sbr = NO_SBR;
891 /* header type */
892 hInfo->header_type = RAW;
893 if (hDecoder->adif_header_present)
894 hInfo->header_type = ADIF;
895 if (hDecoder->adts_header_present)
896 hInfo->header_type = ADTS;
897 #if (defined(PS_DEC) || defined(DRM_PS))
898 hInfo->ps = hDecoder->ps_used_global;
899 #endif
901 /* check if frame has channel elements */
902 if (channels == 0)
904 hDecoder->frame++;
905 return NULL;
908 /* allocate the buffer for the final samples */
909 if ((hDecoder->sample_buffer == NULL) ||
910 (hDecoder->alloced_channels != output_channels))
912 static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
913 sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
914 sizeof(int16_t), sizeof(int16_t), 0, 0, 0
916 uint8_t stride = str[hDecoder->config.outputFormat-1];
917 #ifdef SBR_DEC
918 if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
920 stride = 2 * stride;
922 #endif
923 /* check if we want to use internal sample_buffer */
924 if (sample_buffer_size == 0)
926 if (hDecoder->sample_buffer)
927 faad_free(hDecoder->sample_buffer);
928 hDecoder->sample_buffer = NULL;
929 hDecoder->sample_buffer = faad_malloc(frame_len*output_channels*stride);
930 } else if (sample_buffer_size < frame_len*output_channels*stride) {
931 /* provided sample buffer is not big enough */
932 hInfo->error = 27;
933 return NULL;
935 hDecoder->alloced_channels = output_channels;
938 if (sample_buffer_size == 0)
940 sample_buffer = hDecoder->sample_buffer;
941 } else {
942 sample_buffer = *sample_buffer2;
945 #ifdef SBR_DEC
946 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
948 uint8_t ele;
950 /* this data is different when SBR is used or when the data is upsampled */
951 if (!hDecoder->downSampledSBR)
953 frame_len *= 2;
954 hInfo->samples *= 2;
955 hInfo->samplerate *= 2;
958 /* check if every element was provided with SBR data */
959 for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
961 if (hDecoder->sbr[ele] == NULL)
963 hInfo->error = 25;
964 goto error;
968 /* sbr */
969 if (hDecoder->sbr_present_flag == 1)
971 hInfo->object_type = HE_AAC;
972 hInfo->sbr = SBR_UPSAMPLED;
973 } else {
974 hInfo->sbr = NO_SBR_UPSAMPLED;
976 if (hDecoder->downSampledSBR)
978 hInfo->sbr = SBR_DOWNSAMPLED;
981 #endif
983 /* we don't need sample conversion in rockbox.
984 sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
985 output_channels, frame_len, hDecoder->config.outputFormat);
988 hDecoder->postSeekResetFlag = 0;
990 hDecoder->frame++;
991 #ifdef LD_DEC
992 if (hDecoder->object_type != LD)
994 #endif
995 if (hDecoder->frame <= 1)
996 hInfo->samples = 0;
997 #ifdef LD_DEC
998 } else {
999 /* LD encoders will give lower delay */
1000 if (hDecoder->frame <= 0)
1001 hInfo->samples = 0;
1003 #endif
1005 /* cleanup */
1006 #ifdef ANALYSIS
1007 fflush(stdout);
1008 #endif
1010 #ifdef PROFILE
1011 count = faad_get_ts() - count;
1012 hDecoder->cycles += count;
1013 #endif
1015 return sample_buffer;
1017 error:
1019 faad_endbits(&ld);
1021 /* cleanup */
1022 #ifdef ANALYSIS
1023 fflush(stdout);
1024 #endif
1026 return NULL;