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.
43 #include "sbr_syntax.h"
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
)
64 return err_msg
[errcode
];
67 uint32_t NEAACDECAPI
NeAACDecGetCapabilities(void)
71 /* can't do without it */
83 #ifdef ERROR_RESILIENCE
84 cap
+= ERROR_RESILIENCE_CAP
;
87 cap
+= FIXED_POINT_CAP
;
93 NeAACDecHandle NEAACDECAPI
NeAACDecOpen(void)
96 NeAACDecHandle hDecoder
= NULL
;
98 #if defined(CPU_COLDFIRE)
99 coldfire_set_macsr(EMAC_FRACTIONAL
| EMAC_SATURATE
);
102 if ((hDecoder
= (NeAACDecHandle
)faad_malloc(sizeof(NeAACDecStruct
))) == 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;
118 hDecoder
->frameLength
= 1024;
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
;
129 hDecoder
->ssr_overlap
[i
] = NULL
;
130 hDecoder
->prev_fmd
[i
] = NULL
;
133 hDecoder
->pred_stat
[i
] = NULL
;
136 hDecoder
->ltp_lag
[i
] = 0;
137 hDecoder
->lt_pred_stat
[i
] = NULL
;
142 for (i
= 0; i
< MAX_SYNTAX_ELEMENTS
; i
++)
144 hDecoder
->sbr
[i
] = NULL
;
148 hDecoder
->drc
= drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
153 NeAACDecConfigurationPtr NEAACDECAPI
NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder
)
157 NeAACDecConfigurationPtr config
= &(hDecoder
->config
);
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)
173 hDecoder
->config
.defObjectType
= config
->defObjectType
;
175 /* samplerate: anything but 0 should be possible */
176 if (config
->defSampleRate
== 0)
178 hDecoder
->config
.defSampleRate
= config
->defSampleRate
;
180 /* check output format */
182 if ((config
->outputFormat
< 1) || (config
->outputFormat
> 4))
185 if ((config
->outputFormat
< 1) || (config
->outputFormat
> 5))
188 hDecoder
->config
.outputFormat
= config
->outputFormat
;
190 if (config
->downMatrix
> 1)
192 hDecoder
->config
.downMatrix
= config
->downMatrix
;
201 int32_t NEAACDECAPI
NeAACDecInit(NeAACDecHandle hDecoder
, uint8_t *buffer
,
202 uint32_t buffer_size
,
203 uint32_t *samplerate
, uint8_t *channels
)
210 if ((hDecoder
== NULL
) || (samplerate
== NULL
) || (channels
== NULL
))
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
);
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
;
264 hDecoder
->channelConfiguration
= *channels
;
266 #if (defined(PS_DEC) || defined(DRM_PS))
267 /* check if we have a mono file */
270 /* upMatrix to 2 channels for implicit signalling of PS */
276 /* implicit signalling */
277 if (*samplerate
<= 24000 && !(hDecoder
->config
.dontUpSampleImplicitSBR
))
280 hDecoder
->forceUpSampling
= 1;
281 } else if (*samplerate
> 24000 && !(hDecoder
->config
.dontUpSampleImplicitSBR
)) {
282 hDecoder
->downSampledSBR
= 1;
286 /* must be done before frameLength is divided by 2 for LD */
288 if (hDecoder
->object_type
== SSR
)
289 hDecoder
->fb
= ssr_filter_bank_init(hDecoder
->frameLength
/SSR_BANDS
);
295 if (hDecoder
->object_type
== LD
)
296 hDecoder
->frameLength
>>= 1;
299 if (can_decode_ot(hDecoder
->object_type
) < 0)
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
)
311 mp4AudioSpecificConfig mp4ASC
;
313 if((hDecoder
== NULL
)
315 || (SizeOfDecoderSpecificInfo
< 2)
316 || (samplerate
== NULL
)
317 || (channels
== NULL
))
322 hDecoder
->adif_header_present
= 0;
323 hDecoder
->adts_header_present
= 0;
325 /* decode the audio specific config */
326 rc
= AudioSpecificConfig2(pBuffer
, SizeOfDecoderSpecificInfo
, &mp4ASC
,
329 /* copy the relevant info to the decoder handle */
330 *samplerate
= mp4ASC
.samplingFrequency
;
331 if (mp4ASC
.channelsConfiguration
)
333 *channels
= mp4ASC
.channelsConfiguration
;
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 */
342 /* upMatrix to 2 channels for implicit signalling of PS */
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
;
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
;
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);
372 hDecoder
->channelConfiguration
= mp4ASC
.channelsConfiguration
;
373 if (mp4ASC
.frameLengthFlag
)
374 #ifdef ALLOW_SMALL_FRAMELENGTH
375 hDecoder
->frameLength
= 960;
380 /* must be done before frameLength is divided by 2 for LD */
382 if (hDecoder
->object_type
== SSR
)
383 hDecoder
->fb
= ssr_filter_bank_init(hDecoder
->frameLength
/SSR_BANDS
);
388 if (hDecoder
->object_type
== LD
)
389 hDecoder
->frameLength
>>= 1;
396 int8_t NEAACDECAPI
NeAACDecInitDRM(NeAACDecHandle
*hDecoder
, uint32_t samplerate
,
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 */
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;
422 (*hDecoder
)->channelConfiguration
= 1;
425 if ((channels
== DRMCH_MONO
) || (channels
== DRMCH_STEREO
))
426 (*hDecoder
)->sbr_present_flag
= 0;
428 (*hDecoder
)->sbr_present_flag
= 1;
436 void NEAACDECAPI
NeAACDecClose(NeAACDecHandle hDecoder
)
440 if (hDecoder
== NULL
)
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
);
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
]);
456 if (hDecoder
->ssr_overlap
[i
]) faad_free(hDecoder
->ssr_overlap
[i
]);
457 if (hDecoder
->prev_fmd
[i
]) faad_free(hDecoder
->prev_fmd
[i
]);
460 if (hDecoder
->pred_stat
[i
]) faad_free(hDecoder
->pred_stat
[i
]);
463 if (hDecoder
->lt_pred_stat
[i
]) faad_free(hDecoder
->lt_pred_stat
[i
]);
468 if (hDecoder
->object_type
== SSR
)
469 ssr_filter_bank_end(hDecoder
->fb
);
474 drc_end(hDecoder
->drc
);
476 if (hDecoder
->sample_buffer
) faad_free(hDecoder
->sample_buffer
);
479 for (i
= 0; i
< MAX_SYNTAX_ELEMENTS
; i
++)
481 if (hDecoder
->sbr
[i
])
482 sbrDecodeEnd(hDecoder
->sbr
[i
]);
486 if (hDecoder
) faad_free(hDecoder
);
489 void NEAACDECAPI
NeAACDecPostSeekReset(NeAACDecHandle hDecoder
, int32_t frame
)
493 hDecoder
->postSeekResetFlag
= 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
;
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
;
530 hInfo
->channel_position
[chpos
++] = FRONT_CHANNEL_CENTER
;
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
;
551 for (i
= 0; i
< chdir
; i
+= 2)
553 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_LEFT
;
554 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_RIGHT
;
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
;
567 switch (hDecoder
->channelConfiguration
)
570 hInfo
->num_front_channels
= 1;
571 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
574 hInfo
->num_front_channels
= 2;
575 hInfo
->channel_position
[0] = FRONT_CHANNEL_LEFT
;
576 hInfo
->channel_position
[1] = FRONT_CHANNEL_RIGHT
;
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
;
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
;
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
;
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
;
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
;
626 default: /* channelConfiguration == 0 || channelConfiguration > 7 */
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
;
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
;
664 uint8_t ch1
= (ch
)/2;
665 hInfo
->num_front_channels
= ch1
;
666 hInfo
->num_back_channels
= ch1
;
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
;
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
;
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))
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;
734 uint32_t bitsconsumed
;
739 int64_t count
= faad_get_ts();
743 if ((hDecoder
== NULL
) || (hInfo
== NULL
) || (buffer
== NULL
))
749 printf("%d\n", buffer_size
*8);
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
);
764 for (i
= 0; i
< ((buffer_size
+3)>>2); i
++)
768 buf
= faad_getbitbuffer(&ld
, 32);
769 //temp = getdword((void*)buf);
770 temp
= *((uint32_t*)buf
);
771 printf("0x%.8X\n", temp
);
775 faad_initbits(&ld
, buffer
, buffer_size
);
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
790 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
794 if (hDecoder
->adts_header_present
)
798 adts
.old_format
= hDecoder
->config
.useOldADTSFormat
;
799 if ((hInfo
->error
= adts_frame(&adts
, &ld
)) > 0)
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.
812 /* decode the complete bitstream */
814 if ((hDecoder
->object_type
== 6) || (hDecoder
->object_type
== DRM_ER_LC
))
816 aac_scalable_main_element(hDecoder
, hInfo
, &ld
, &hDecoder
->pce
, hDecoder
->drc
);
819 raw_data_block(hDecoder
, hInfo
, &ld
, &hDecoder
->pce
, hDecoder
->drc
);
824 channels
= hDecoder
->fr_channels
;
826 if (hInfo
->error
> 0)
830 if (channels
== 0 || channels
> MAX_CHANNELS
)
832 /* invalid number of channels */
837 /* no more bit reading after this */
838 bitsconsumed
= faad_get_processed_bits(&ld
);
839 hInfo
->bytesconsumed
= bit2byte(bitsconsumed
);
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;
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;
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
;
886 hInfo
->samplerate
= get_sample_rate(hDecoder
->sf_index
);
888 hInfo
->object_type
= hDecoder
->object_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
;
901 /* check if frame has channel elements */
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];
918 if (((hDecoder
->sbr_present_flag
== 1)&&(!hDecoder
->downSampledSBR
)) || (hDecoder
->forceUpSampling
== 1))
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 */
935 hDecoder
->alloced_channels
= output_channels
;
938 if (sample_buffer_size
== 0)
940 sample_buffer
= hDecoder
->sample_buffer
;
942 sample_buffer
= *sample_buffer2
;
946 if ((hDecoder
->sbr_present_flag
== 1) || (hDecoder
->forceUpSampling
== 1))
950 /* this data is different when SBR is used or when the data is upsampled */
951 if (!hDecoder
->downSampledSBR
)
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
)
969 if (hDecoder
->sbr_present_flag
== 1)
971 hInfo
->object_type
= HE_AAC
;
972 hInfo
->sbr
= SBR_UPSAMPLED
;
974 hInfo
->sbr
= NO_SBR_UPSAMPLED
;
976 if (hDecoder
->downSampledSBR
)
978 hInfo
->sbr
= SBR_DOWNSAMPLED
;
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;
992 if (hDecoder
->object_type
!= LD
)
995 if (hDecoder
->frame
<= 1)
999 /* LD encoders will give lower delay */
1000 if (hDecoder
->frame
<= 0)
1011 count
= faad_get_ts() - count
;
1012 hDecoder
->cycles
+= count
;
1015 return sample_buffer
;