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.
25
** $Id: decoder.c,v 1.94 2004/02/04 20:07:24 menno Exp $
43
#include "sbr_syntax.h"
53
int8_t* FAADAPI faacDecGetErrorMessage(uint8_t errcode)
55
if (errcode >= NUM_ERROR_MESSAGES)
57
return err_msg[errcode];
60
uint32_t FAADAPI faacDecGetCapabilities(void)
64
/* can't do without it */
76
#ifdef ERROR_RESILIENCE
77
cap += ERROR_RESILIENCE_CAP;
80
cap += FIXED_POINT_CAP;
86
faacDecHandle FAADAPI faacDecOpen(void)
89
faacDecHandle hDecoder = NULL;
91
if ((hDecoder = (faacDecHandle)faad_malloc(sizeof(faacDecStruct))) == NULL)
94
memset(hDecoder, 0, sizeof(faacDecStruct));
96
hDecoder->config.outputFormat = FAAD_FMT_16BIT;
97
hDecoder->config.defObjectType = MAIN;
98
hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
99
hDecoder->config.downMatrix = 0;
100
hDecoder->adts_header_present = 0;
101
hDecoder->adif_header_present = 0;
102
#ifdef ERROR_RESILIENCE
103
hDecoder->aacSectionDataResilienceFlag = 0;
104
hDecoder->aacScalefactorDataResilienceFlag = 0;
105
hDecoder->aacSpectralDataResilienceFlag = 0;
107
hDecoder->frameLength = 1024;
110
hDecoder->sample_buffer = NULL;
112
for (i = 0; i < MAX_CHANNELS; i++)
114
hDecoder->window_shape_prev[i] = 0;
115
hDecoder->time_out[i] = NULL;
116
hDecoder->fb_intermed[i] = NULL;
118
hDecoder->ssr_overlap[i] = NULL;
119
hDecoder->prev_fmd[i] = NULL;
122
hDecoder->pred_stat[i] = NULL;
125
hDecoder->ltp_lag[i] = 0;
126
hDecoder->lt_pred_stat[i] = NULL;
131
for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
133
hDecoder->sbr[i] = NULL;
137
hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
142
hDecoder->apply_sf_func = apply_scalefactors_sse;
144
hDecoder->apply_sf_func = apply_scalefactors;
151
faacDecConfigurationPtr FAADAPI faacDecGetCurrentConfiguration(faacDecHandle hDecoder)
155
faacDecConfigurationPtr config = &(hDecoder->config);
163
uint8_t FAADAPI faacDecSetConfiguration(faacDecHandle hDecoder,
164
faacDecConfigurationPtr config)
166
if (hDecoder && config)
168
/* check if we can decode this object type */
169
if (can_decode_ot(config->defObjectType) < 0)
171
hDecoder->config.defObjectType = config->defObjectType;
173
/* samplerate: anything but 0 should be possible */
174
if (config->defSampleRate == 0)
176
hDecoder->config.defSampleRate = config->defSampleRate;
178
/* check output format */
179
if ((config->outputFormat < 1) || (config->outputFormat > 9))
181
hDecoder->config.outputFormat = config->outputFormat;
183
if (config->downMatrix > 1)
184
hDecoder->config.downMatrix = config->downMatrix;
193
int32_t FAADAPI faacDecInit(faacDecHandle hDecoder, uint8_t *buffer,
194
uint32_t buffer_size,
195
uint32_t *samplerate, uint8_t *channels)
202
if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL))
205
hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
206
hDecoder->object_type = hDecoder->config.defObjectType;
207
*samplerate = get_sample_rate(hDecoder->sf_index);
212
faad_initbits(&ld, buffer, buffer_size);
214
/* Check if an ADIF header is present */
215
if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
216
(buffer[2] == 'I') && (buffer[3] == 'F'))
218
hDecoder->adif_header_present = 1;
220
get_adif_header(&adif, &ld);
221
faad_byte_align(&ld);
223
hDecoder->sf_index = adif.pce[0].sf_index;
224
hDecoder->object_type = adif.pce[0].object_type + 1;
226
*samplerate = get_sample_rate(hDecoder->sf_index);
227
*channels = adif.pce[0].channels;
229
memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
230
hDecoder->pce_set = 1;
232
bits = bit2byte(faad_get_processed_bits(&ld));
234
/* Check if an ADTS header is present */
235
} else if (faad_showbits(&ld, 12) == 0xfff) {
236
hDecoder->adts_header_present = 1;
238
adts.old_format = hDecoder->config.useOldADTSFormat;
239
adts_frame(&adts, &ld);
241
hDecoder->sf_index = adts.sf_index;
242
hDecoder->object_type = adts.profile + 1;
244
*samplerate = get_sample_rate(hDecoder->sf_index);
245
*channels = (adts.channel_configuration > 6) ?
246
2 : adts.channel_configuration;
256
hDecoder->channelConfiguration = *channels;
259
/* implicit signalling */
260
if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR))
263
hDecoder->forceUpSampling = 1;
267
/* must be done before frameLength is divided by 2 for LD */
269
if (hDecoder->object_type == SSR)
270
hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
273
hDecoder->fb = filter_bank_init(hDecoder->frameLength);
276
if (hDecoder->object_type == LD)
277
hDecoder->frameLength >>= 1;
280
if (can_decode_ot(hDecoder->object_type) < 0)
286
/* Init the library using a DecoderSpecificInfo */
287
int8_t FAADAPI faacDecInit2(faacDecHandle hDecoder, uint8_t *pBuffer,
288
uint32_t SizeOfDecoderSpecificInfo,
289
uint32_t *samplerate, uint8_t *channels)
292
mp4AudioSpecificConfig mp4ASC;
294
if((hDecoder == NULL)
296
|| (SizeOfDecoderSpecificInfo < 2)
297
|| (samplerate == NULL)
298
|| (channels == NULL))
303
hDecoder->adif_header_present = 0;
304
hDecoder->adts_header_present = 0;
306
/* decode the audio specific config */
307
rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
310
/* copy the relevant info to the decoder handle */
311
*samplerate = mp4ASC.samplingFrequency;
312
if (mp4ASC.channelsConfiguration)
314
*channels = mp4ASC.channelsConfiguration;
316
*channels = hDecoder->pce.channels;
317
hDecoder->pce_set = 1;
319
hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
320
hDecoder->object_type = mp4ASC.objectTypeIndex;
321
#ifdef ERROR_RESILIENCE
322
hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
323
hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
324
hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
327
hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
328
if (hDecoder->config.dontUpSampleImplicitSBR == 0)
329
hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
331
hDecoder->forceUpSampling = 0;
333
/* AAC core decoder samplerate is 2 times as low */
334
if (hDecoder->sbr_present_flag == 1 || hDecoder->forceUpSampling == 1)
336
hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
344
hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
345
if (mp4ASC.frameLengthFlag)
346
#ifdef ALLOW_SMALL_FRAMELENGTH
347
hDecoder->frameLength = 960;
352
/* must be done before frameLength is divided by 2 for LD */
354
if (hDecoder->object_type == SSR)
355
hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
358
hDecoder->fb = filter_bank_init(hDecoder->frameLength);
361
if (hDecoder->object_type == LD)
362
hDecoder->frameLength >>= 1;
369
int8_t FAADAPI faacDecInitDRM(faacDecHandle hDecoder, uint32_t samplerate,
374
if (hDecoder == NULL)
375
return 1; /* error */
377
/* Special object type defined for DRM */
378
hDecoder->config.defObjectType = DRM_ER_LC;
380
hDecoder->config.defSampleRate = samplerate;
381
#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
382
hDecoder->aacSectionDataResilienceFlag = 1; /* VCB11 */
383
hDecoder->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
384
hDecoder->aacSpectralDataResilienceFlag = 1; /* HCR */
386
hDecoder->frameLength = 960;
387
hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
388
hDecoder->object_type = hDecoder->config.defObjectType;
390
if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
391
hDecoder->channelConfiguration = 2;
393
hDecoder->channelConfiguration = 1;
396
if (channels == DRMCH_SBR_LC_STEREO)
397
hDecoder->lcstereo_flag = 1;
399
hDecoder->lcstereo_flag = 0;
401
if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
402
hDecoder->sbr_present_flag = 0;
404
hDecoder->sbr_present_flag = 1;
406
/* Reset sbr for new initialization */
407
sbrDecodeEnd(hDecoder->sbr[0]);
408
hDecoder->sbr[0] = NULL;
411
if (hDecoder->fb) filter_bank_end(hDecoder->fb);
414
/* Take care of buffers */
415
if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
416
hDecoder->sample_buffer = NULL;
417
hDecoder->alloced_channels = 0;
419
for (i = 0; i < MAX_CHANNELS; i++)
421
hDecoder->window_shape_prev[i] = 0;
423
if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
424
hDecoder->time_out[i] = NULL;
425
if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]);
426
hDecoder->fb_intermed[i] = NULL;
428
if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]);
429
hDecoder->ssr_overlap[i] = NULL;
430
if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]);
431
hDecoder->prev_fmd[i] = NULL;
434
if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
435
hDecoder->pred_stat[i] = NULL;
438
hDecoder->ltp_lag[i] = 0;
439
if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
440
hDecoder->lt_pred_stat[i] = NULL;
444
for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
447
if (hDecoder->sbr[i])
448
sbrDecodeEnd(hDecoder->sbr[i]);
450
hDecoder->sbr_alloced[i] = 0;
452
hDecoder->element_alloced[i] = 0;
453
hDecoder->element_output_channels[i] = 0;
456
hDecoder->fb = filter_bank_init(hDecoder->frameLength);
462
void FAADAPI faacDecClose(faacDecHandle hDecoder)
466
if (hDecoder == NULL)
470
printf("AAC decoder total: %I64d cycles\n", hDecoder->cycles);
471
printf("requant: %I64d cycles\n", hDecoder->requant_cycles);
472
printf("spectral_data: %I64d cycles\n", hDecoder->spectral_cycles);
473
printf("scalefactors: %I64d cycles\n", hDecoder->scalefac_cycles);
474
printf("output: %I64d cycles\n", hDecoder->output_cycles);
477
for (i = 0; i < MAX_CHANNELS; i++)
479
if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
480
if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]);
482
if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]);
483
if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]);
486
if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
489
if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
494
if (hDecoder->object_type == SSR)
495
ssr_filter_bank_end(hDecoder->fb);
498
filter_bank_end(hDecoder->fb);
500
drc_end(hDecoder->drc);
502
if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
505
for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
507
if (hDecoder->sbr[i])
508
sbrDecodeEnd(hDecoder->sbr[i]);
512
if (hDecoder) faad_free(hDecoder);
515
void FAADAPI faacDecPostSeekReset(faacDecHandle hDecoder, int32_t frame)
519
hDecoder->postSeekResetFlag = 1;
522
hDecoder->frame = frame;
526
static void create_channel_config(faacDecHandle hDecoder, faacDecFrameInfo *hInfo)
528
hInfo->num_front_channels = 0;
529
hInfo->num_side_channels = 0;
530
hInfo->num_back_channels = 0;
531
hInfo->num_lfe_channels = 0;
532
memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
534
if (hDecoder->downMatrix)
536
hInfo->num_front_channels = 2;
537
hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
538
hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
542
/* check if there is a PCE */
543
if (hDecoder->pce_set)
545
uint8_t i, chpos = 0;
546
uint8_t chdir, back_center = 0;
548
hInfo->num_front_channels = hDecoder->pce.num_front_channels;
549
hInfo->num_side_channels = hDecoder->pce.num_side_channels;
550
hInfo->num_back_channels = hDecoder->pce.num_back_channels;
551
hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
553
chdir = hInfo->num_front_channels;
556
hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
559
for (i = 0; i < chdir; i += 2)
561
hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
562
hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
565
for (i = 0; i < hInfo->num_side_channels; i += 2)
567
hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT;
568
hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT;
571
chdir = hInfo->num_back_channels;
577
for (i = 0; i < chdir; i += 2)
579
hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT;
580
hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT;
584
hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
587
for (i = 0; i < hInfo->num_lfe_channels; i++)
589
hInfo->channel_position[chpos++] = LFE_CHANNEL;
593
switch (hDecoder->channelConfiguration)
596
hInfo->num_front_channels = 1;
597
hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
600
hInfo->num_front_channels = 2;
601
hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
602
hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
605
hInfo->num_front_channels = 3;
606
hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
607
hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
608
hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
611
hInfo->num_front_channels = 3;
612
hInfo->num_back_channels = 1;
613
hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
614
hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
615
hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
616
hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
619
hInfo->num_front_channels = 3;
620
hInfo->num_back_channels = 2;
621
hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
622
hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
623
hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
624
hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
625
hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
628
hInfo->num_front_channels = 3;
629
hInfo->num_back_channels = 2;
630
hInfo->num_lfe_channels = 1;
631
hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
632
hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
633
hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
634
hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
635
hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
636
hInfo->channel_position[5] = LFE_CHANNEL;
639
hInfo->num_front_channels = 3;
640
hInfo->num_side_channels = 2;
641
hInfo->num_back_channels = 2;
642
hInfo->num_lfe_channels = 1;
643
hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
644
hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
645
hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
646
hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
647
hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
648
hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
649
hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
650
hInfo->channel_position[7] = LFE_CHANNEL;
652
default: /* channelConfiguration == 0 || channelConfiguration > 7 */
655
uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
656
if (ch & 1) /* there's either a center front or a center back channel */
658
uint8_t ch1 = (ch-1)/2;
659
if (hDecoder->first_syn_ele == ID_SCE)
661
hInfo->num_front_channels = ch1 + 1;
662
hInfo->num_back_channels = ch1;
663
hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
664
for (i = 1; i <= ch1; i+=2)
666
hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
667
hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
669
for (i = ch1+1; i < ch; i+=2)
671
hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
672
hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
675
hInfo->num_front_channels = ch1;
676
hInfo->num_back_channels = ch1 + 1;
677
for (i = 0; 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; i < ch-1; i+=2)
684
hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
685
hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
687
hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
690
uint8_t ch1 = (ch)/2;
691
hInfo->num_front_channels = ch1;
692
hInfo->num_back_channels = ch1;
695
hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
696
for (i = 1; i <= ch1; i+=2)
698
hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
699
hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
701
for (i = ch1+1; i < ch-1; i+=2)
703
hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
704
hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
706
hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
708
for (i = 0; i < ch1; i+=2)
710
hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
711
hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
713
for (i = ch1; i < ch; i+=2)
715
hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
716
hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
720
hInfo->num_lfe_channels = hDecoder->has_lfe;
721
for (i = ch; i < hDecoder->fr_channels; i++)
723
hInfo->channel_position[i] = LFE_CHANNEL;
731
void* FAADAPI faacDecDecode(faacDecHandle hDecoder,
732
faacDecFrameInfo *hInfo,
733
uint8_t *buffer, uint32_t buffer_size)
735
uint8_t channels = 0;
736
uint8_t output_channels = 0;
738
uint32_t bitsconsumed;
743
int64_t count = faad_get_ts();
747
if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
752
frame_len = hDecoder->frameLength;
755
memset(hInfo, 0, sizeof(faacDecFrameInfo));
756
memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
758
/* initialize the bitstream */
759
faad_initbits(&ld, buffer, buffer_size);
762
if (hDecoder->object_type == DRM_ER_LC)
764
/* We do not support stereo right now */
765
if (hDecoder->channelConfiguration == 2)
767
hInfo->error = 8; // Throw CRC error
772
DEBUGVAR(1,1,"faacDecDecode(): skip CRC"));
776
if (hDecoder->adts_header_present)
780
adts.old_format = hDecoder->config.useOldADTSFormat;
781
if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
784
/* MPEG2 does byte_alignment() here,
785
* but ADTS header is always multiple of 8 bits in MPEG2
786
* so not needed to actually do it.
794
/* decode the complete bitstream */
796
if ((hDecoder->object_type == 6) || (hDecoder->object_type == DRM_ER_LC))
798
aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
801
raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
806
channels = hDecoder->fr_channels;
808
if (hInfo->error > 0)
812
if (channels == 0 || channels > MAX_CHANNELS)
814
/* invalid number of channels */
819
/* no more bit reading after this */
820
bitsconsumed = faad_get_processed_bits(&ld);
821
hInfo->bytesconsumed = bit2byte(bitsconsumed);
830
if (!hDecoder->adts_header_present && !hDecoder->adif_header_present)
832
if (channels != hDecoder->channelConfiguration)
833
hDecoder->channelConfiguration = channels;
835
if (channels == 8) /* 7.1 */
836
hDecoder->channelConfiguration = 7;
837
if (channels == 7) /* not a standard channelConfiguration */
838
hDecoder->channelConfiguration = 0;
841
if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
843
hDecoder->downMatrix = 1;
846
output_channels = channels;
849
/* Make a channel configuration based on either a PCE or a channelConfiguration */
850
create_channel_config(hDecoder, hInfo);
852
/* number of samples in this frame */
853
hInfo->samples = frame_len*output_channels;
854
/* number of channels in this frame */
855
hInfo->channels = output_channels;
857
hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
859
hInfo->object_type = hDecoder->object_type;
863
hInfo->header_type = RAW;
864
if (hDecoder->adif_header_present)
865
hInfo->header_type = ADIF;
866
if (hDecoder->adts_header_present)
867
hInfo->header_type = ADTS;
869
/* check if frame has channel elements */
876
/* allocate the buffer for the final samples */
877
if ((hDecoder->sample_buffer == NULL) ||
878
(hDecoder->alloced_channels != output_channels))
880
static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
881
sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
882
sizeof(int16_t), sizeof(int16_t), 0, 0, 0
884
uint8_t stride = str[hDecoder->config.outputFormat-1];
886
if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
889
if (hDecoder->sample_buffer)
890
faad_free(hDecoder->sample_buffer);
891
hDecoder->sample_buffer = NULL;
892
hDecoder->sample_buffer = faad_malloc(frame_len*output_channels*stride);
893
hDecoder->alloced_channels = output_channels;
896
sample_buffer = hDecoder->sample_buffer;
899
if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
903
/* this data is different when SBR is used or when the data is upsampled */
906
hInfo->samplerate *= 2;
908
/* check if every element was provided with SBR data */
909
for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
911
if (hDecoder->sbr[ele] == NULL)
919
if (hDecoder->sbr_present_flag == 1)
921
hInfo->object_type = HE_AAC;
922
hInfo->sbr = SBR_UPSAMPLED;
924
hInfo->sbr = NO_SBR_UPSAMPLED;
929
sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
930
output_channels, frame_len, hDecoder->config.outputFormat);
933
hDecoder->postSeekResetFlag = 0;
937
if (hDecoder->object_type != LD)
940
if (hDecoder->frame <= 1)
944
/* LD encoders will give lower delay */
945
if (hDecoder->frame <= 0)
956
count = faad_get_ts() - count;
957
hDecoder->cycles += count;
960
return sample_buffer;