2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
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.
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
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
45 #include "sbr_syntax.h"
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
;
61 #ifdef FAAD_STATIC_ALLOC
62 static real_t s_time_buf_2048
[MAX_CHANNELS
][2*FRAME_LEN
] MEM_ALIGN_ATTR
;
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
;
70 static pred_state s_pred_stat
[MAX_CHANNELS
][1*FRAME_LEN
] MEM_ALIGN_ATTR
;
73 static int16_t s_lt_pred_stat
[MAX_CHANNELS
][4*FRAME_LEN
] MEM_ALIGN_ATTR
;
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
)
88 return err_msg
[errcode
];
91 uint32_t NEAACDECAPI
NeAACDecGetCapabilities(void)
95 /* can't do without it */
107 #ifdef ERROR_RESILIENCE
108 cap
+= ERROR_RESILIENCE_CAP
;
111 cap
+= FIXED_POINT_CAP
;
117 NeAACDecHandle NEAACDECAPI
NeAACDecOpen(void)
120 NeAACDecHandle hDecoder
= NULL
;
122 #if defined(CPU_COLDFIRE)
123 coldfire_set_macsr(EMAC_FRACTIONAL
| EMAC_SATURATE
);
126 hDecoder
= &s_AACDec
;
128 memset(hDecoder
, 0, sizeof(NeAACDecStruct
));
129 memset(s_fb_intermed
, 0, sizeof(s_fb_intermed
));
131 memset(s_ssr_overlap
, 0, sizeof(s_ssr_overlap
));
132 memset(s_prev_fmd
, 0, sizeof(s_prev_fmd
));
135 memset(s_lt_pred_stat
, 0, sizeof(s_s_lt_pred_statpred_stat
));
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;
149 hDecoder
->frameLength
= FRAME_LEN
;
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
];
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);
165 hDecoder
->pred_stat
[i
] = s_pred_stat
[i
];
166 reset_all_predictors(hDecoder
->pred_stat
[channel
], FRAME_LEN
);
169 hDecoder
->ltp_lag
[i
] = 0;
170 hDecoder
->lt_pred_stat
[i
] = s_lt_pred_stat
[i
];
175 for (i
= 0; i
< MAX_SYNTAX_ELEMENTS
; i
++)
177 hDecoder
->sbr
[i
] = NULL
;
181 hDecoder
->drc
= drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
186 NeAACDecConfigurationPtr NEAACDECAPI
NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder
)
190 NeAACDecConfigurationPtr config
= &(hDecoder
->config
);
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)
206 hDecoder
->config
.defObjectType
= config
->defObjectType
;
208 /* samplerate: anything but 0 should be possible */
209 if (config
->defSampleRate
== 0)
211 hDecoder
->config
.defSampleRate
= config
->defSampleRate
;
213 /* check output format */
215 if ((config
->outputFormat
< 1) || (config
->outputFormat
> 4))
218 if ((config
->outputFormat
< 1) || (config
->outputFormat
> 5))
221 hDecoder
->config
.outputFormat
= config
->outputFormat
;
223 if (config
->downMatrix
> 1)
225 hDecoder
->config
.downMatrix
= config
->downMatrix
;
234 int32_t NEAACDECAPI
NeAACDecInit(NeAACDecHandle hDecoder
, uint8_t *buffer
,
235 uint32_t buffer_size
,
236 uint32_t *samplerate
, uint8_t *channels
)
244 if ((hDecoder
== NULL
) || (samplerate
== NULL
) || (channels
== NULL
))
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
);
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
;
298 hDecoder
->channelConfiguration
= *channels
;
300 #if (defined(PS_DEC) || defined(DRM_PS))
301 /* check if we have a mono file */
304 /* upMatrix to 2 channels for implicit signalling of PS */
309 /* A maximum of MAX_CHANNELS channels is supported. */
310 if (*channels
> MAX_CHANNELS
)
316 /* implicit signalling */
317 if (*samplerate
<= 24000 && !(hDecoder
->config
.dontUpSampleImplicitSBR
))
320 hDecoder
->forceUpSampling
= 1;
321 } else if (*samplerate
> 24000 && !(hDecoder
->config
.dontUpSampleImplicitSBR
)) {
322 hDecoder
->downSampledSBR
= 1;
326 /* must be done before frameLength is divided by 2 for LD */
328 if (hDecoder
->object_type
== SSR
)
329 hDecoder
->fb
= ssr_filter_bank_init(hDecoder
->frameLength
/SSR_BANDS
);
335 if (hDecoder
->object_type
== LD
)
336 hDecoder
->frameLength
>>= 1;
339 for (i
=0; i
<MAX_CHANNELS
; ++i
)
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
];
348 hDecoder
->time_out
[i
] = (real_t
*)faad_malloc(2*FRAME_LEN
*sizeof(real_t
));
350 memset(hDecoder
->time_out
[i
], 0, 2*FRAME_LEN
);
351 hDecoder
->sbr_alloced
[hDecoder
->fr_ch_ele
] = 1;
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)
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
)
374 mp4AudioSpecificConfig mp4ASC
;
376 if((hDecoder
== NULL
)
378 || (SizeOfDecoderSpecificInfo
< 2)
379 || (samplerate
== NULL
)
380 || (channels
== NULL
))
385 hDecoder
->adif_header_present
= 0;
386 hDecoder
->adts_header_present
= 0;
388 /* decode the audio specific config */
389 rc
= AudioSpecificConfig2(pBuffer
, SizeOfDecoderSpecificInfo
, &mp4ASC
,
392 /* copy the relevant info to the decoder handle */
393 *samplerate
= mp4ASC
.samplingFrequency
;
394 if (mp4ASC
.channelsConfiguration
)
396 *channels
= mp4ASC
.channelsConfiguration
;
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 */
405 /* upMatrix to 2 channels for implicit signalling of PS */
410 /* A maximum of MAX_CHANNELS channels is supported. */
411 if (*channels
> MAX_CHANNELS
)
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
;
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
;
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);
442 hDecoder
->channelConfiguration
= mp4ASC
.channelsConfiguration
;
443 if (mp4ASC
.frameLengthFlag
)
444 #ifdef ALLOW_SMALL_FRAMELENGTH
445 hDecoder
->frameLength
= 960;
450 /* must be done before frameLength is divided by 2 for LD */
452 if (hDecoder
->object_type
== SSR
)
453 hDecoder
->fb
= ssr_filter_bank_init(hDecoder
->frameLength
/SSR_BANDS
);
458 if (hDecoder
->object_type
== LD
)
459 hDecoder
->frameLength
>>= 1;
462 for (i
=0; i
<MAX_CHANNELS
; ++i
)
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
];
471 hDecoder
->time_out
[i
] = (real_t
*)faad_malloc(2*FRAME_LEN
*sizeof(real_t
));
473 memset(hDecoder
->time_out
[i
], 0, 2*FRAME_LEN
);
474 hDecoder
->sbr_alloced
[hDecoder
->fr_ch_ele
] = 1;
479 hDecoder
->time_out
[i
] = s_time_buf_1024
[i
];
480 memset(hDecoder
->time_out
[i
], 0, 1*FRAME_LEN
);
488 int8_t NEAACDECAPI
NeAACDecInitDRM(NeAACDecHandle
*hDecoder
, uint32_t samplerate
,
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 */
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;
512 (*hDecoder
)->channelConfiguration
= 1;
515 if ((channels
== DRMCH_MONO
) || (channels
== DRMCH_STEREO
))
516 (*hDecoder
)->sbr_present_flag
= 0;
518 (*hDecoder
)->sbr_present_flag
= 1;
526 void NEAACDECAPI
NeAACDecPostSeekReset(NeAACDecHandle hDecoder
, int32_t frame
)
530 hDecoder
->postSeekResetFlag
= 1;
533 hDecoder
->frame
= frame
;
537 /* not used by rockbox */
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
;
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
;
569 hInfo
->channel_position
[chpos
++] = FRONT_CHANNEL_CENTER
;
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
;
590 for (i
= 0; i
< chdir
; i
+= 2)
592 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_LEFT
;
593 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_RIGHT
;
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
;
606 switch (hDecoder
->channelConfiguration
)
609 hInfo
->num_front_channels
= 1;
610 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
613 hInfo
->num_front_channels
= 2;
614 hInfo
->channel_position
[0] = FRONT_CHANNEL_LEFT
;
615 hInfo
->channel_position
[1] = FRONT_CHANNEL_RIGHT
;
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
;
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
;
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
;
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
;
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
;
665 default: /* channelConfiguration == 0 || channelConfiguration > 7 */
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
;
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
;
703 uint8_t ch1
= (ch
)/2;
704 hInfo
->num_front_channels
= ch1
;
705 hInfo
->num_back_channels
= ch1
;
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
;
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
;
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;
758 uint32_t bitsconsumed
;
762 int64_t count
= faad_get_ts();
766 if ((hDecoder
== NULL
) || (hInfo
== NULL
) || (buffer
== NULL
))
772 printf("%d\n", buffer_size
*8);
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
);
787 for (i
= 0; i
< ((buffer_size
+3)>>2); i
++)
791 buf
= faad_getbitbuffer(&ld
, 32);
792 //temp = getdword((void*)buf);
793 temp
= *((uint32_t*)buf
);
794 printf("0x%.8X\n", temp
);
798 faad_initbits(&ld
, buffer
, buffer_size
);
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
813 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
817 if (hDecoder
->adts_header_present
)
821 adts
.old_format
= hDecoder
->config
.useOldADTSFormat
;
822 if ((hInfo
->error
= adts_frame(&adts
, &ld
)) > 0)
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.
835 /* decode the complete bitstream */
837 if ((hDecoder
->object_type
== 6) || (hDecoder
->object_type
== DRM_ER_LC
))
839 aac_scalable_main_element(hDecoder
, hInfo
, &ld
, &hDecoder
->pce
, hDecoder
->drc
);
842 raw_data_block(hDecoder
, hInfo
, &ld
, &hDecoder
->pce
, hDecoder
->drc
);
847 channels
= hDecoder
->fr_channels
;
849 if (hInfo
->error
> 0)
853 if (channels
== 0 || channels
> MAX_CHANNELS
)
855 /* invalid number of channels */
860 /* no more bit reading after this */
861 bitsconsumed
= faad_get_processed_bits(&ld
);
862 hInfo
->bytesconsumed
= bit2byte(bitsconsumed
);
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;
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;
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
;
911 hInfo
->samplerate
= get_sample_rate(hDecoder
->sf_index
);
913 hInfo
->object_type
= hDecoder
->object_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
;
926 /* check if frame has channel elements */
933 /* allocate the buffer for the final samples */
934 if (hDecoder
->alloced_channels
!= output_channels
)
936 hDecoder
->alloced_channels
= output_channels
;
940 if ((hDecoder
->sbr_present_flag
== 1) || (hDecoder
->forceUpSampling
== 1))
944 /* this data is different when SBR is used or when the data is upsampled */
945 if (!hDecoder
->downSampledSBR
)
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
)
963 if (hDecoder
->sbr_present_flag
== 1)
965 hInfo
->object_type
= HE_AAC
;
966 hInfo
->sbr
= SBR_UPSAMPLED
;
968 hInfo
->sbr
= NO_SBR_UPSAMPLED
;
970 if (hDecoder
->downSampledSBR
)
972 hInfo
->sbr
= SBR_DOWNSAMPLED
;
977 hDecoder
->postSeekResetFlag
= 0;
981 if (hDecoder
->object_type
!= LD
)
984 if (hDecoder
->frame
<= 1)
988 /* LD encoders will give lower delay */
989 if (hDecoder
->frame
<= 0)
1000 count
= faad_get_ts() - count
;
1001 hDecoder
->cycles
+= count
;
1004 return hDecoder
; /* return void* != NULL */