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"
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
)
66 return err_msg
[errcode
];
69 uint32_t NEAACDECAPI
NeAACDecGetCapabilities(void)
73 /* can't do without it */
85 #ifdef ERROR_RESILIENCE
86 cap
+= ERROR_RESILIENCE_CAP
;
89 cap
+= FIXED_POINT_CAP
;
95 NeAACDecHandle NEAACDECAPI
NeAACDecOpen(void)
98 NeAACDecHandle hDecoder
= NULL
;
100 #if defined(CPU_COLDFIRE)
101 coldfire_set_macsr(EMAC_FRACTIONAL
| EMAC_SATURATE
);
104 if ((hDecoder
= (NeAACDecHandle
)faad_malloc(sizeof(NeAACDecStruct
))) == 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;
120 hDecoder
->frameLength
= 1024;
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
;
131 hDecoder
->ssr_overlap
[i
] = NULL
;
132 hDecoder
->prev_fmd
[i
] = NULL
;
135 hDecoder
->pred_stat
[i
] = NULL
;
138 hDecoder
->ltp_lag
[i
] = 0;
139 hDecoder
->lt_pred_stat
[i
] = NULL
;
144 for (i
= 0; i
< MAX_SYNTAX_ELEMENTS
; i
++)
146 hDecoder
->sbr
[i
] = NULL
;
150 hDecoder
->drc
= drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
155 NeAACDecConfigurationPtr NEAACDECAPI
NeAACDecGetCurrentConfiguration(NeAACDecHandle hDecoder
)
159 NeAACDecConfigurationPtr config
= &(hDecoder
->config
);
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)
175 hDecoder
->config
.defObjectType
= config
->defObjectType
;
177 /* samplerate: anything but 0 should be possible */
178 if (config
->defSampleRate
== 0)
180 hDecoder
->config
.defSampleRate
= config
->defSampleRate
;
182 /* check output format */
184 if ((config
->outputFormat
< 1) || (config
->outputFormat
> 4))
187 if ((config
->outputFormat
< 1) || (config
->outputFormat
> 5))
190 hDecoder
->config
.outputFormat
= config
->outputFormat
;
192 if (config
->downMatrix
> 1)
194 hDecoder
->config
.downMatrix
= config
->downMatrix
;
203 int32_t NEAACDECAPI
NeAACDecInit(NeAACDecHandle hDecoder
, uint8_t *buffer
,
204 uint32_t buffer_size
,
205 uint32_t *samplerate
, uint8_t *channels
)
212 if ((hDecoder
== NULL
) || (samplerate
== NULL
) || (channels
== NULL
))
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
);
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
;
266 hDecoder
->channelConfiguration
= *channels
;
268 #if (defined(PS_DEC) || defined(DRM_PS))
269 /* check if we have a mono file */
272 /* upMatrix to 2 channels for implicit signalling of PS */
278 /* implicit signalling */
279 if (*samplerate
<= 24000 && !(hDecoder
->config
.dontUpSampleImplicitSBR
))
282 hDecoder
->forceUpSampling
= 1;
283 } else if (*samplerate
> 24000 && !(hDecoder
->config
.dontUpSampleImplicitSBR
)) {
284 hDecoder
->downSampledSBR
= 1;
288 /* must be done before frameLength is divided by 2 for LD */
290 if (hDecoder
->object_type
== SSR
)
291 hDecoder
->fb
= ssr_filter_bank_init(hDecoder
->frameLength
/SSR_BANDS
);
297 if (hDecoder
->object_type
== LD
)
298 hDecoder
->frameLength
>>= 1;
301 if (can_decode_ot(hDecoder
->object_type
) < 0)
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
)
313 mp4AudioSpecificConfig mp4ASC
;
315 if((hDecoder
== NULL
)
317 || (SizeOfDecoderSpecificInfo
< 2)
318 || (samplerate
== NULL
)
319 || (channels
== NULL
))
324 hDecoder
->adif_header_present
= 0;
325 hDecoder
->adts_header_present
= 0;
327 /* decode the audio specific config */
328 rc
= AudioSpecificConfig2(pBuffer
, SizeOfDecoderSpecificInfo
, &mp4ASC
,
331 /* copy the relevant info to the decoder handle */
332 *samplerate
= mp4ASC
.samplingFrequency
;
333 if (mp4ASC
.channelsConfiguration
)
335 *channels
= mp4ASC
.channelsConfiguration
;
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 */
344 /* upMatrix to 2 channels for implicit signalling of PS */
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
;
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
;
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);
374 hDecoder
->channelConfiguration
= mp4ASC
.channelsConfiguration
;
375 if (mp4ASC
.frameLengthFlag
)
376 #ifdef ALLOW_SMALL_FRAMELENGTH
377 hDecoder
->frameLength
= 960;
382 /* must be done before frameLength is divided by 2 for LD */
384 if (hDecoder
->object_type
== SSR
)
385 hDecoder
->fb
= ssr_filter_bank_init(hDecoder
->frameLength
/SSR_BANDS
);
390 if (hDecoder
->object_type
== LD
)
391 hDecoder
->frameLength
>>= 1;
398 int8_t NEAACDECAPI
NeAACDecInitDRM(NeAACDecHandle
*hDecoder
, uint32_t samplerate
,
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 */
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;
424 (*hDecoder
)->channelConfiguration
= 1;
427 if ((channels
== DRMCH_MONO
) || (channels
== DRMCH_STEREO
))
428 (*hDecoder
)->sbr_present_flag
= 0;
430 (*hDecoder
)->sbr_present_flag
= 1;
438 void NEAACDECAPI
NeAACDecClose(NeAACDecHandle hDecoder
)
442 if (hDecoder
== NULL
)
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
);
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
]);
458 if (hDecoder
->ssr_overlap
[i
]) faad_free(hDecoder
->ssr_overlap
[i
]);
459 if (hDecoder
->prev_fmd
[i
]) faad_free(hDecoder
->prev_fmd
[i
]);
462 if (hDecoder
->pred_stat
[i
]) faad_free(hDecoder
->pred_stat
[i
]);
465 if (hDecoder
->lt_pred_stat
[i
]) faad_free(hDecoder
->lt_pred_stat
[i
]);
470 if (hDecoder
->object_type
== SSR
)
471 ssr_filter_bank_end(hDecoder
->fb
);
476 drc_end(hDecoder
->drc
);
478 if (hDecoder
->sample_buffer
) faad_free(hDecoder
->sample_buffer
);
481 for (i
= 0; i
< MAX_SYNTAX_ELEMENTS
; i
++)
483 if (hDecoder
->sbr
[i
])
484 sbrDecodeEnd(hDecoder
->sbr
[i
]);
488 if (hDecoder
) faad_free(hDecoder
);
491 void NEAACDECAPI
NeAACDecPostSeekReset(NeAACDecHandle hDecoder
, int32_t frame
)
495 hDecoder
->postSeekResetFlag
= 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
;
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
;
532 hInfo
->channel_position
[chpos
++] = FRONT_CHANNEL_CENTER
;
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
;
553 for (i
= 0; i
< chdir
; i
+= 2)
555 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_LEFT
;
556 hInfo
->channel_position
[chpos
++] = BACK_CHANNEL_RIGHT
;
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
;
569 switch (hDecoder
->channelConfiguration
)
572 hInfo
->num_front_channels
= 1;
573 hInfo
->channel_position
[0] = FRONT_CHANNEL_CENTER
;
576 hInfo
->num_front_channels
= 2;
577 hInfo
->channel_position
[0] = FRONT_CHANNEL_LEFT
;
578 hInfo
->channel_position
[1] = FRONT_CHANNEL_RIGHT
;
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
;
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
;
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
;
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
;
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
;
628 default: /* channelConfiguration == 0 || channelConfiguration > 7 */
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
;
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
;
666 uint8_t ch1
= (ch
)/2;
667 hInfo
->num_front_channels
= ch1
;
668 hInfo
->num_back_channels
= ch1
;
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
;
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
;
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))
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;
736 uint32_t bitsconsumed
;
741 int64_t count
= faad_get_ts();
745 if ((hDecoder
== NULL
) || (hInfo
== NULL
) || (buffer
== NULL
))
751 printf("%d\n", buffer_size
*8);
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
);
766 for (i
= 0; i
< ((buffer_size
+3)>>2); i
++)
770 buf
= faad_getbitbuffer(&ld
, 32);
771 //temp = getdword((void*)buf);
772 temp
= *((uint32_t*)buf
);
773 printf("0x%.8X\n", temp
);
777 faad_initbits(&ld
, buffer
, buffer_size
);
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
792 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
796 if (hDecoder
->adts_header_present
)
800 adts
.old_format
= hDecoder
->config
.useOldADTSFormat
;
801 if ((hInfo
->error
= adts_frame(&adts
, &ld
)) > 0)
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.
814 /* decode the complete bitstream */
816 if ((hDecoder
->object_type
== 6) || (hDecoder
->object_type
== DRM_ER_LC
))
818 aac_scalable_main_element(hDecoder
, hInfo
, &ld
, &hDecoder
->pce
, hDecoder
->drc
);
821 raw_data_block(hDecoder
, hInfo
, &ld
, &hDecoder
->pce
, hDecoder
->drc
);
826 channels
= hDecoder
->fr_channels
;
828 if (hInfo
->error
> 0)
832 if (channels
== 0 || channels
> MAX_CHANNELS
)
834 /* invalid number of channels */
839 /* no more bit reading after this */
840 bitsconsumed
= faad_get_processed_bits(&ld
);
841 hInfo
->bytesconsumed
= bit2byte(bitsconsumed
);
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;
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;
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
;
888 hInfo
->samplerate
= get_sample_rate(hDecoder
->sf_index
);
890 hInfo
->object_type
= hDecoder
->object_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
;
903 /* check if frame has channel elements */
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];
920 if (((hDecoder
->sbr_present_flag
== 1)&&(!hDecoder
->downSampledSBR
)) || (hDecoder
->forceUpSampling
== 1))
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 */
937 hDecoder
->alloced_channels
= output_channels
;
940 if (sample_buffer_size
== 0)
942 sample_buffer
= hDecoder
->sample_buffer
;
944 sample_buffer
= *sample_buffer2
;
948 if ((hDecoder
->sbr_present_flag
== 1) || (hDecoder
->forceUpSampling
== 1))
952 /* this data is different when SBR is used or when the data is upsampled */
953 if (!hDecoder
->downSampledSBR
)
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
)
971 if (hDecoder
->sbr_present_flag
== 1)
973 hInfo
->object_type
= HE_AAC
;
974 hInfo
->sbr
= SBR_UPSAMPLED
;
976 hInfo
->sbr
= NO_SBR_UPSAMPLED
;
978 if (hDecoder
->downSampledSBR
)
980 hInfo
->sbr
= SBR_DOWNSAMPLED
;
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;
994 if (hDecoder
->object_type
!= LD
)
997 if (hDecoder
->frame
<= 1)
1001 /* LD encoders will give lower delay */
1002 if (hDecoder
->frame
<= 0)
1013 count
= faad_get_ts() - count
;
1014 hDecoder
->cycles
+= count
;
1017 return sample_buffer
;