Coverage Report

Created: 2026-10-17 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/libfaad/decoder.c
Line
Count
Source
1
/*
2
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3
** Copyright (C) 2003-2005 M. Bakker, Nero 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.
14
**
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.
18
**
19
** Any non-GPL usage of this software or parts of this software is strictly
20
** forbidden.
21
**
22
** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23
** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24
**
25
** Commercial non-GPL licensing of this software is possible.
26
** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27
**
28
** $Id: decoder.c,v 1.119 2015/01/24 14:21:05 knik Exp $
29
**/
30
31
#include "common.h"
32
#include "structs.h"
33
34
#include <stdlib.h>
35
#include <stdio.h>
36
#include <string.h>
37
38
#include "mp4.h"
39
#include "syntax.h"
40
#include "error.h"
41
#include "output.h"
42
#include "filtbank.h"
43
#include "drc.h"
44
#ifdef SBR_DEC
45
#include "sbr_dec.h"
46
#include "sbr_syntax.h"
47
#endif
48
#ifdef SSR_DEC
49
#include "ssr.h"
50
#endif
51
52
#ifdef ANALYSIS
53
uint16_t dbg_count;
54
#endif
55
56
#if !defined(PACKAGE_VERSION)
57
#error "PACKAGE_VERSION must be defined"
58
#endif
59
60
/* static function declarations */
61
static void* aac_frame_decode(NeAACDecStruct *hDecoder,
62
                              NeAACDecFrameInfo *hInfo,
63
                              unsigned char *buffer,
64
                              unsigned long buffer_size,
65
                              void **sample_buffer2,
66
                              unsigned long sample_buffer_size);
67
static void create_channel_config(NeAACDecStruct *hDecoder,
68
                                  NeAACDecFrameInfo *hInfo);
69
70
71
int NeAACDecGetVersion(char **faad_id_string,
72
                                   char **faad_copyright_string)
73
372
{
74
372
    static char *libfaadName = PACKAGE_VERSION;
75
372
    static char *libCopyright =
76
372
        " Copyright 2002-2004: Ahead Software AG\n"
77
372
  " http://www.audiocoding.com\n"
78
372
  " bug tracking: https://sourceforge.net/p/faac/bugs/\n";
79
80
372
    if (faad_id_string)
81
372
        *faad_id_string = libfaadName;
82
83
372
    if (faad_copyright_string)
84
372
        *faad_copyright_string = libCopyright;
85
86
372
    return 0;
87
372
}
88
89
char* NeAACDecGetErrorMessage(unsigned char errcode)
90
372
{
91
372
    if (errcode >= NUM_ERROR_MESSAGES)
92
266
        return NULL;
93
106
    return err_msg[errcode];
94
372
}
95
96
unsigned long NeAACDecGetCapabilities(void)
97
372
{
98
372
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
372
    cap += LC_DEC_CAP;
102
103
372
#ifdef MAIN_DEC
104
372
    cap += MAIN_DEC_CAP;
105
372
#endif
106
372
#ifdef LTP_DEC
107
372
    cap += LTP_DEC_CAP;
108
372
#endif
109
372
#ifdef LD_DEC
110
372
    cap += LD_DEC_CAP;
111
372
#endif
112
372
#ifdef ERROR_RESILIENCE
113
372
    cap += ERROR_RESILIENCE_CAP;
114
372
#endif
115
#ifdef FIXED_POINT
116
    cap += FIXED_POINT_CAP;
117
#endif
118
119
372
    return cap;
120
372
}
121
122
const unsigned char mes[] = { 0x67,0x20,0x61,0x20,0x20,0x20,0x6f,0x20,0x72,0x20,0x65,0x20,0x6e,0x20,0x20,0x20,0x74,0x20,0x68,0x20,0x67,0x20,0x69,0x20,0x72,0x20,0x79,0x20,0x70,0x20,0x6f,0x20,0x63 };
123
NeAACDecHandle NeAACDecOpen(void)
124
0
{
125
0
    uint8_t i;
126
0
    NeAACDecStruct *hDecoder = NULL;
127
128
0
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
0
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
0
    hDecoder->cmes = mes;
134
0
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
0
    hDecoder->config.defObjectType = MAIN;
136
0
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
0
    hDecoder->config.downMatrix = 0;
138
0
    hDecoder->adts_header_present = 0;
139
0
    hDecoder->adif_header_present = 0;
140
0
    hDecoder->latm_header_present = 0;
141
0
#ifdef ERROR_RESILIENCE
142
0
    hDecoder->aacSectionDataResilienceFlag = 0;
143
0
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
0
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
0
#endif
146
0
    hDecoder->frameLength = 1024;
147
148
0
    hDecoder->frame = 0;
149
0
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
0
    hDecoder->__r1 = 0x2bb431ea;
153
0
    hDecoder->__r2 = 0x206155b7;
154
155
0
    for (i = 0; i < MAX_CHANNELS; i++)
156
0
    {
157
0
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
0
        hDecoder->window_shape_prev[i] = 0;
159
0
        hDecoder->time_out[i] = NULL;
160
0
        hDecoder->fb_intermed[i] = NULL;
161
#ifdef SSR_DEC
162
        hDecoder->ssr_overlap[i] = NULL;
163
        hDecoder->prev_fmd[i] = NULL;
164
#endif
165
0
#ifdef MAIN_DEC
166
0
        hDecoder->pred_stat[i] = NULL;
167
0
#endif
168
0
#ifdef LTP_DEC
169
0
        hDecoder->ltp_lag[i] = 0;
170
0
        hDecoder->lt_pred_stat[i] = NULL;
171
0
#endif
172
0
    }
173
174
0
#ifdef SBR_DEC
175
0
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
0
    {
177
0
        hDecoder->sbr[i] = NULL;
178
0
    }
179
0
#endif
180
181
0
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
0
#if (defined(PS_DEC) || defined(DRM_PS))
183
0
    hDecoder->ps_present_flag = -1;
184
0
#endif
185
186
0
    return hDecoder;
187
0
}
188
189
NeAACDecConfigurationPtr NeAACDecGetCurrentConfiguration(NeAACDecHandle hpDecoder)
190
0
{
191
0
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
192
0
    if (hDecoder)
193
0
    {
194
0
        NeAACDecConfigurationPtr config = &(hDecoder->config);
195
196
0
        return config;
197
0
    }
198
199
0
    return NULL;
200
0
}
201
202
unsigned char NeAACDecSetConfiguration(NeAACDecHandle hpDecoder,
203
                                                   NeAACDecConfigurationPtr config)
204
0
{
205
0
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
206
0
    if (hDecoder && config)
207
0
    {
208
        /* check if we can decode this object type */
209
0
        if (can_decode_ot(config->defObjectType) < 0)
210
0
            return 0;
211
0
        hDecoder->config.defObjectType = config->defObjectType;
212
213
        /* samplerate: anything but 0 should be possible */
214
0
        if (config->defSampleRate == 0)
215
0
            return 0;
216
0
        hDecoder->config.defSampleRate = config->defSampleRate;
217
218
        /* check output format */
219
#ifdef FIXED_POINT
220
        if ((config->outputFormat < 1) || (config->outputFormat > 4))
221
            return 0;
222
#else
223
0
        if ((config->outputFormat < 1) || (config->outputFormat > 5))
224
0
            return 0;
225
0
#endif
226
0
        hDecoder->config.outputFormat = config->outputFormat;
227
228
0
        if (config->downMatrix > 1)
229
0
            return 0;
230
0
        hDecoder->config.downMatrix = config->downMatrix;
231
232
        /* OK */
233
0
        return 1;
234
0
    }
235
236
0
    return 0;
237
0
}
238
239
240
#if 0
241
static int latmCheck(latm_header *latm, bitfile *ld)
242
{
243
    uint32_t good=0, bad=0, bits, m;
244
245
    while (ld->bytes_left)
246
    {
247
        bits = faad_latm_frame(latm, ld);
248
        if(bits==0xFFFFFFFF)
249
            bad++;
250
        else
251
        {
252
            good++;
253
            while(bits>0)
254
            {
255
                m = min(bits, 8);
256
                faad_getbits(ld, m);
257
                bits -= m;
258
            }
259
        }
260
    }
261
262
    return (good>0);
263
}
264
#endif
265
266
long NeAACDecInit(NeAACDecHandle hpDecoder,
267
                              unsigned char *buffer,
268
                              unsigned long buffer_size,
269
                              unsigned long *samplerate,
270
                              unsigned char *channels)
271
0
{
272
0
    uint32_t bits = 0;
273
0
    bitfile ld;
274
0
    adif_header adif;
275
0
    adts_header adts;
276
0
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
277
278
279
0
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
280
0
        return -1;
281
282
0
    adts.old_format = hDecoder->config.useOldADTSFormat;
283
0
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
284
0
    hDecoder->object_type = hDecoder->config.defObjectType;
285
0
    *samplerate = get_sample_rate(hDecoder->sf_index);
286
0
    *channels = 1;
287
288
0
    if (buffer != NULL)
289
0
    {
290
#if 0
291
        int is_latm;
292
        latm_header *l = &hDecoder->latm_config;
293
#endif
294
295
0
        faad_initbits(&ld, buffer, buffer_size);
296
297
#if 0
298
        memset(l, 0, sizeof(latm_header));
299
        is_latm = latmCheck(l, &ld);
300
        l->inited = 0;
301
        l->frameLength = 0;
302
        faad_rewindbits(&ld);
303
        if(is_latm && l->ASCbits>0)
304
        {
305
            int32_t x;
306
            hDecoder->latm_header_present = 1;
307
            x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits+7)/8, samplerate, channels);
308
            if(x!=0)
309
                hDecoder->latm_header_present = 0;
310
            return x;
311
        } else
312
#endif
313
        /* Check if an ADIF header is present */
314
0
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
315
0
            (buffer[2] == 'I') && (buffer[3] == 'F'))
316
0
        {
317
0
            hDecoder->adif_header_present = 1;
318
319
0
            get_adif_header(&adif, &ld);
320
0
            faad_byte_align(&ld);
321
322
0
            hDecoder->sf_index = adif.pce[0].sf_index;
323
0
            hDecoder->object_type = adif.pce[0].object_type + 1;
324
325
0
            *samplerate = get_sample_rate(hDecoder->sf_index);
326
0
            *channels = adif.pce[0].channels;
327
328
0
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
329
0
            hDecoder->pce_set = 1;
330
331
0
            bits = bit2byte(faad_get_processed_bits(&ld));
332
333
        /* Check if an ADTS header is present */
334
0
        } else if (adts_frame(&adts, &ld) == 0) {
335
0
            hDecoder->adts_header_present = 1;
336
337
0
            hDecoder->sf_index = adts.sf_index;
338
0
            hDecoder->object_type = adts.profile + 1;
339
340
0
            *samplerate = get_sample_rate(hDecoder->sf_index);
341
0
            *channels = (adts.channel_configuration > 6) ?
342
0
                2 : adts.channel_configuration;
343
0
        }
344
345
0
        if (ld.error)
346
0
        {
347
0
            faad_endbits(&ld);
348
0
            return -1;
349
0
        }
350
0
        faad_endbits(&ld);
351
0
    }
352
353
0
    if (!*samplerate)
354
0
        return -1;
355
356
0
    hDecoder->channelConfiguration = *channels;
357
358
0
#ifdef SBR_DEC
359
    /* implicit signalling */
360
0
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
361
0
    {
362
0
        *samplerate *= 2;
363
0
        hDecoder->forceUpSampling = 1;
364
0
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
365
0
        hDecoder->downSampledSBR = 1;
366
0
    }
367
0
#endif
368
369
0
    if (can_decode_ot(hDecoder->object_type) < 0)
370
0
        return -1;
371
372
    /* must be done before frameLength is divided by 2 for LD */
373
#ifdef SSR_DEC
374
    if (hDecoder->object_type == SSR)
375
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
376
    else
377
#endif
378
0
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
379
380
0
#ifdef LD_DEC
381
0
    if (hDecoder->object_type == LD)
382
0
        hDecoder->frameLength >>= 1;
383
0
#endif
384
385
0
    return bits;
386
0
}
387
388
/* Init the library using a DecoderSpecificInfo */
389
char NeAACDecInit2(NeAACDecHandle hpDecoder,
390
                               unsigned char *pBuffer,
391
                               unsigned long SizeOfDecoderSpecificInfo,
392
                               unsigned long *samplerate,
393
                               unsigned char *channels)
394
0
{
395
0
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
396
0
    int8_t rc;
397
0
    mp4AudioSpecificConfig mp4ASC;
398
399
0
    if((hDecoder == NULL)
400
0
        || (pBuffer == NULL)
401
0
        || (SizeOfDecoderSpecificInfo < 2)
402
0
        || (samplerate == NULL)
403
0
        || (channels == NULL))
404
0
    {
405
0
        return -1;
406
0
    }
407
408
0
    hDecoder->adif_header_present = 0;
409
0
    hDecoder->adts_header_present = 0;
410
411
    /* decode the audio specific config */
412
0
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
413
0
        &(hDecoder->pce), hDecoder->latm_header_present);
414
415
    /* copy the relevant info to the decoder handle */
416
0
    *samplerate = mp4ASC.samplingFrequency;
417
0
    if (mp4ASC.channelsConfiguration)
418
0
    {
419
0
        *channels = mp4ASC.channelsConfiguration;
420
0
    } else {
421
0
        *channels = hDecoder->pce.channels;
422
0
        hDecoder->pce_set = 1;
423
0
    }
424
0
#if (defined(PS_DEC) || defined(DRM_PS))
425
0
    hDecoder->ps_present_flag = mp4ASC.ps_present_flag;
426
0
#endif
427
0
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
428
0
    hDecoder->object_type = mp4ASC.objectTypeIndex;
429
0
#ifdef ERROR_RESILIENCE
430
0
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
431
0
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
432
0
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
433
0
#endif
434
0
#ifdef SBR_DEC
435
0
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
436
0
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
437
0
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
438
0
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
439
0
    else
440
0
        hDecoder->forceUpSampling = 0;
441
442
    /* AAC core decoder samplerate is 2 times as low */
443
0
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
444
0
    {
445
0
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
446
0
    }
447
0
#endif
448
449
0
    if (rc != 0)
450
0
    {
451
0
        return rc;
452
0
    }
453
0
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
454
0
    if (mp4ASC.frameLengthFlag)
455
0
#ifdef ALLOW_SMALL_FRAMELENGTH
456
0
        hDecoder->frameLength = 960;
457
#else
458
        return -1;
459
#endif
460
461
    /* must be done before frameLength is divided by 2 for LD */
462
#ifdef SSR_DEC
463
    if (hDecoder->object_type == SSR)
464
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
465
    else
466
#endif
467
0
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
468
469
0
#ifdef LD_DEC
470
0
    if (hDecoder->object_type == LD)
471
0
        hDecoder->frameLength >>= 1;
472
0
#endif
473
474
0
    return 0;
475
0
}
476
477
#ifdef DRM
478
char NeAACDecInitDRM(NeAACDecHandle *hpDecoder,
479
                                 unsigned long samplerate,
480
                                 unsigned char channels)
481
{
482
    NeAACDecStruct** hDecoder = (NeAACDecStruct**)hpDecoder;
483
    if (hDecoder == NULL)
484
        return 1; /* error */
485
486
    NeAACDecClose(*hDecoder);
487
488
    *hDecoder = NeAACDecOpen();
489
490
    /* Special object type defined for DRM */
491
    (*hDecoder)->config.defObjectType = DRM_ER_LC;
492
493
    (*hDecoder)->config.defSampleRate = samplerate;
494
#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
495
    (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
496
    (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
497
    (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
498
#endif
499
    (*hDecoder)->frameLength = 960;
500
    (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
501
    (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
502
503
    if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
504
        (*hDecoder)->channelConfiguration = 2;
505
    else
506
        (*hDecoder)->channelConfiguration = 1;
507
508
#ifdef SBR_DEC
509
    if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
510
        (*hDecoder)->sbr_present_flag = 0;
511
    else
512
        (*hDecoder)->sbr_present_flag = 1;
513
#endif
514
515
    (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
516
517
    return 0;
518
}
519
#endif
520
521
void NeAACDecClose(NeAACDecHandle hpDecoder)
522
0
{
523
0
    uint8_t i;
524
0
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
525
526
0
    if (hDecoder == NULL)
527
0
        return;
528
529
#ifdef PROFILE
530
    printf("AAC decoder total:  %I64d cycles\n", hDecoder->cycles);
531
    printf("requant:            %I64d cycles\n", hDecoder->requant_cycles);
532
    printf("spectral_data:      %I64d cycles\n", hDecoder->spectral_cycles);
533
    printf("scalefactors:       %I64d cycles\n", hDecoder->scalefac_cycles);
534
    printf("output:             %I64d cycles\n", hDecoder->output_cycles);
535
#endif
536
537
0
    for (i = 0; i < MAX_CHANNELS; i++)
538
0
    {
539
0
        if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
540
0
        if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]);
541
#ifdef SSR_DEC
542
        if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]);
543
        if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]);
544
#endif
545
0
#ifdef MAIN_DEC
546
0
        if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
547
0
#endif
548
0
#ifdef LTP_DEC
549
0
        if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
550
0
#endif
551
0
    }
552
553
#ifdef SSR_DEC
554
    if (hDecoder->object_type == SSR)
555
        ssr_filter_bank_end(hDecoder->fb);
556
    else
557
#endif
558
0
        filter_bank_end(hDecoder->fb);
559
560
0
    drc_end(hDecoder->drc);
561
562
0
    if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
563
564
0
#ifdef SBR_DEC
565
0
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
566
0
    {
567
0
        if (hDecoder->sbr[i])
568
0
            sbrDecodeEnd(hDecoder->sbr[i]);
569
0
    }
570
0
#endif
571
572
0
    if (hDecoder) faad_free(hDecoder);
573
0
}
574
575
void NeAACDecPostSeekReset(NeAACDecHandle hpDecoder, long frame)
576
0
{
577
0
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
578
0
    if (hDecoder)
579
0
    {
580
0
        hDecoder->postSeekResetFlag = 1;
581
582
0
        if (frame != -1)
583
0
            hDecoder->frame = frame;
584
0
    }
585
0
}
586
587
static void create_channel_config(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo)
588
0
{
589
0
    hInfo->num_front_channels = 0;
590
0
    hInfo->num_side_channels = 0;
591
0
    hInfo->num_back_channels = 0;
592
0
    hInfo->num_lfe_channels = 0;
593
0
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
594
595
0
    if (hDecoder->downMatrix)
596
0
    {
597
0
        hInfo->num_front_channels = 2;
598
0
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
599
0
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
600
0
        return;
601
0
    }
602
603
    /* check if there is a PCE */
604
    /* TODO: why CPE flag is ignored? */
605
0
    if (hDecoder->pce_set)
606
0
    {
607
0
        uint8_t i, chpos = 0;
608
0
        uint8_t chdir, back_center = 0;
609
610
0
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
611
0
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
612
0
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
613
0
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
614
0
        chdir = hInfo->num_front_channels;
615
0
        if (chdir & 1)
616
0
        {
617
0
#if (defined(PS_DEC) || defined(DRM_PS))
618
0
            if (hInfo->num_front_channels == 1 &&
619
0
                hInfo->num_side_channels == 0 &&
620
0
                hInfo->num_back_channels == 0 &&
621
0
                hInfo->num_lfe_channels == 0 &&
622
0
                hDecoder->ps_used_global)
623
0
            {
624
                /* When PS is enabled output is always stereo */
625
0
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
626
0
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
627
0
            } else
628
0
#endif
629
0
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
630
0
            chdir--;
631
0
        }
632
0
        for (i = 0; i < chdir; i++)
633
0
        {
634
0
            hInfo->channel_position[chpos++] =
635
0
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
636
0
        }
637
638
0
        for (i = 0; i < hInfo->num_side_channels; i++)
639
0
        {
640
0
            hInfo->channel_position[chpos++] =
641
0
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
642
0
        }
643
644
0
        chdir = hInfo->num_back_channels;
645
0
        if (chdir & 1)
646
0
        {
647
0
            back_center = 1;
648
0
            chdir--;
649
0
        }
650
0
        for (i = 0; i < chdir; i++)
651
0
        {
652
0
            hInfo->channel_position[chpos++] =
653
0
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
654
0
        }
655
0
        if (back_center)
656
0
        {
657
0
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
658
0
        }
659
660
0
        for (i = 0; i < hInfo->num_lfe_channels; i++)
661
0
        {
662
0
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
663
0
        }
664
665
0
    } else {
666
0
        switch (hDecoder->channelConfiguration)
667
0
        {
668
0
        case 1:
669
0
#if (defined(PS_DEC) || defined(DRM_PS))
670
0
            if (hDecoder->ps_used_global)
671
0
            {
672
                /* When PS is enabled output is always stereo */
673
0
                hInfo->num_front_channels = 2;
674
0
                hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
675
0
                hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
676
0
            } else {
677
0
                hInfo->num_front_channels = 1;
678
0
                hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
679
0
            }
680
#else
681
            hInfo->num_front_channels = 1;
682
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
683
#endif
684
0
            break;
685
0
        case 2:
686
0
            hInfo->num_front_channels = 2;
687
0
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
688
0
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
689
0
            break;
690
0
        case 3:
691
0
            hInfo->num_front_channels = 3;
692
0
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
693
0
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
694
0
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
695
0
            break;
696
0
        case 4:
697
0
            hInfo->num_front_channels = 3;
698
0
            hInfo->num_back_channels = 1;
699
0
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
700
0
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
701
0
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
702
0
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
703
0
            break;
704
0
        case 5:
705
0
            hInfo->num_front_channels = 3;
706
0
            hInfo->num_back_channels = 2;
707
0
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
708
0
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
709
0
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
710
0
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
711
0
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
712
0
            break;
713
0
        case 6:
714
0
            hInfo->num_front_channels = 3;
715
0
            hInfo->num_back_channels = 2;
716
0
            hInfo->num_lfe_channels = 1;
717
0
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
718
0
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
719
0
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
720
0
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
721
0
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
722
0
            hInfo->channel_position[5] = LFE_CHANNEL;
723
0
            break;
724
0
        case 7:
725
0
            hInfo->num_front_channels = 3;
726
0
            hInfo->num_side_channels = 2;
727
0
            hInfo->num_back_channels = 2;
728
0
            hInfo->num_lfe_channels = 1;
729
0
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
730
0
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
731
0
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
732
0
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
733
0
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
734
0
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
735
0
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
736
0
            hInfo->channel_position[7] = LFE_CHANNEL;
737
0
            break;
738
0
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
739
0
            {
740
0
                uint8_t i;
741
0
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
742
0
                if (ch & 1) /* there's either a center front or a center back channel */
743
0
                {
744
0
                    uint8_t ch1 = (ch-1)/2;
745
0
                    if (hDecoder->first_syn_ele == ID_SCE)
746
0
                    {
747
0
                        hInfo->num_front_channels = ch1 + 1;
748
0
                        hInfo->num_back_channels = ch1;
749
0
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
750
0
                        for (i = 1; i <= ch1; i+=2)
751
0
                        {
752
0
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
753
0
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
754
0
                        }
755
0
                        for (i = ch1+1; i < ch; i+=2)
756
0
                        {
757
0
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
758
0
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
759
0
                        }
760
0
                    } else {
761
0
                        hInfo->num_front_channels = ch1;
762
0
                        hInfo->num_back_channels = ch1 + 1;
763
0
                        for (i = 0; i < ch1; i+=2)
764
0
                        {
765
0
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
766
0
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
767
0
                        }
768
0
                        for (i = ch1; i < ch-1; i+=2)
769
0
                        {
770
0
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
771
0
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
772
0
                        }
773
0
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
774
0
                    }
775
0
                } else {
776
0
                    uint8_t ch1 = (ch)/2;
777
0
                    hInfo->num_front_channels = ch1;
778
0
                    hInfo->num_back_channels = ch1;
779
0
                    if (ch1 & 1)
780
0
                    {
781
0
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
782
0
                        for (i = 1; i <= ch1; i+=2)
783
0
                        {
784
0
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
785
0
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
786
0
                        }
787
0
                        for (i = ch1+1; i < ch-1; i+=2)
788
0
                        {
789
0
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
790
0
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
791
0
                        }
792
0
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
793
0
                    } else {
794
0
                        for (i = 0; i < ch1; i+=2)
795
0
                        {
796
0
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
797
0
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
798
0
                        }
799
0
                        for (i = ch1; i < ch; i+=2)
800
0
                        {
801
0
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
802
0
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
803
0
                        }
804
0
                    }
805
0
                }
806
0
                hInfo->num_lfe_channels = hDecoder->has_lfe;
807
0
                for (i = ch; i < hDecoder->fr_channels; i++)
808
0
                {
809
0
                    hInfo->channel_position[i] = LFE_CHANNEL;
810
0
                }
811
0
            }
812
0
            break;
813
0
        }
814
0
    }
815
0
}
816
817
void* NeAACDecDecode(NeAACDecHandle hpDecoder,
818
                                 NeAACDecFrameInfo *hInfo,
819
                                 unsigned char *buffer,
820
                                 unsigned long buffer_size)
821
0
{
822
0
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
823
0
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
824
0
}
825
826
void* NeAACDecDecode2(NeAACDecHandle hpDecoder,
827
                                  NeAACDecFrameInfo *hInfo,
828
                                  unsigned char *buffer,
829
                                  unsigned long buffer_size,
830
                                  void **sample_buffer,
831
                                  unsigned long sample_buffer_size)
832
0
{
833
0
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
834
0
    if ((sample_buffer_size == 0) || (sample_buffer == NULL) || (*sample_buffer == NULL))
835
0
    {
836
0
        hInfo->error = 27;
837
0
        return NULL;
838
0
    }
839
840
0
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
841
0
        sample_buffer, sample_buffer_size);
842
0
}
843
844
static void* aac_frame_decode(NeAACDecStruct *hDecoder,
845
                              NeAACDecFrameInfo *hInfo,
846
                              unsigned char *buffer,
847
                              unsigned long buffer_size,
848
                              void **sample_buffer2,
849
                              unsigned long sample_buffer_size)
850
0
{
851
0
    uint16_t i;
852
0
    uint8_t channels = 0;
853
0
    uint8_t output_channels = 0;
854
0
    bitfile ld;
855
0
    uint32_t bitsconsumed;
856
0
    uint16_t frame_len;
857
0
    void *sample_buffer;
858
#if 0
859
    uint32_t startbit=0, endbit=0, payload_bits=0;
860
#endif
861
0
    uint32_t required_buffer_size=0;
862
863
#ifdef PROFILE
864
    int64_t count = faad_get_ts();
865
#endif
866
867
    /* safety checks */
868
0
    if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
869
0
    {
870
0
        return NULL;
871
0
    }
872
873
#if 0
874
    printf("%d\n", buffer_size*8);
875
#endif
876
877
0
    frame_len = hDecoder->frameLength;
878
879
880
0
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
881
0
    memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
882
883
#ifdef USE_TIME_LIMIT
884
    if ((TIME_LIMIT * get_sample_rate(hDecoder->sf_index)) > hDecoder->TL_count)
885
    {
886
        hDecoder->TL_count += 1024;
887
    } else {
888
        hInfo->error = (NUM_ERROR_MESSAGES-1);
889
        goto error;
890
    }
891
#endif
892
893
894
    /* check for some common metadata tag types in the bitstream
895
     * No need to return an error
896
     */
897
    /* ID3 */
898
0
    if (buffer_size >= 128)
899
0
    {
900
0
        if (memcmp(buffer, "TAG", 3) == 0)
901
0
        {
902
            /* found it */
903
0
            hInfo->bytesconsumed = 128; /* 128 bytes fixed size */
904
            /* no error, but no output either */
905
0
            return NULL;
906
0
        }
907
0
    }
908
909
910
    /* initialize the bitstream */
911
0
    faad_initbits(&ld, buffer, buffer_size);
912
0
    if (ld.error != 0)
913
0
        return NULL;
914
915
#if 0
916
    {
917
        int i;
918
        for (i = 0; i < ((buffer_size+3)>>2); i++)
919
        {
920
            uint8_t *buf;
921
            uint32_t temp = 0;
922
            buf = faad_getbitbuffer(&ld, 32);
923
            //temp = getdword((void*)buf);
924
            temp = *((uint32_t*)buf);
925
            printf("0x%.8X\n", temp);
926
            free(buf);
927
        }
928
        faad_endbits(&ld);
929
        faad_initbits(&ld, buffer, buffer_size);
930
    }
931
#endif
932
933
#if 0
934
    if(hDecoder->latm_header_present)
935
    {
936
        payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
937
        startbit = faad_get_processed_bits(&ld);
938
        if(payload_bits == -1U)
939
        {
940
            hInfo->error = 1;
941
            goto error;
942
        }
943
    }
944
#endif
945
946
#ifdef DRM
947
    if (hDecoder->object_type == DRM_ER_LC)
948
    {
949
        /* We do not support stereo right now */
950
        if (0) //(hDecoder->channelConfiguration == 2)
951
        {
952
            hInfo->error = 28; // Throw CRC error
953
            goto error;
954
        }
955
956
        faad_getbits(&ld, 8
957
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
958
    }
959
#endif
960
961
0
    if (hDecoder->adts_header_present)
962
0
    {
963
0
        adts_header adts;
964
965
0
        adts.old_format = hDecoder->config.useOldADTSFormat;
966
0
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
967
0
            goto error;
968
969
        /* MPEG2 does byte_alignment() here,
970
         * but ADTS header is always multiple of 8 bits in MPEG2
971
         * so not needed to actually do it.
972
         */
973
0
    }
974
975
#ifdef ANALYSIS
976
    dbg_count = 0;
977
#endif
978
979
    /* decode the complete bitstream */
980
#ifdef DRM
981
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
982
    {
983
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
984
    } else {
985
#endif
986
0
        raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
987
#ifdef DRM
988
    }
989
#endif
990
991
#if 0
992
    if(hDecoder->latm_header_present)
993
    {
994
        endbit = faad_get_processed_bits(&ld);
995
        if(endbit-startbit > payload_bits)
996
            fprintf(stderr, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
997
                endbit-startbit, payload_bits);
998
        if(hDecoder->latm_config.otherDataLenBits > 0)
999
            faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
1000
        faad_byte_align(&ld);
1001
    }
1002
#endif
1003
1004
0
    channels = hDecoder->fr_channels;
1005
1006
0
    if (hInfo->error > 0)
1007
0
        goto error;
1008
1009
    /* safety check */
1010
0
    if (channels == 0 || channels > MAX_CHANNELS)
1011
0
    {
1012
        /* invalid number of channels */
1013
0
        hInfo->error = 12;
1014
0
        goto error;
1015
0
    }
1016
1017
    /* no more bit reading after this */
1018
0
    bitsconsumed = faad_get_processed_bits(&ld);
1019
0
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1020
0
    if (ld.error)
1021
0
    {
1022
0
        hInfo->error = 14;
1023
0
        goto error;
1024
0
    }
1025
0
    faad_endbits(&ld);
1026
1027
1028
0
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1029
#if 0
1030
        && !hDecoder->latm_header_present
1031
#endif
1032
0
        )
1033
0
    {
1034
0
        if (hDecoder->channelConfiguration == 0)
1035
0
            hDecoder->channelConfiguration = channels;
1036
1037
0
        if (channels == 8) /* 7.1 */
1038
0
            hDecoder->channelConfiguration = 7;
1039
0
        if (channels == 7) /* not a standard channelConfiguration */
1040
0
            hDecoder->channelConfiguration = 0;
1041
0
    }
1042
1043
0
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1044
0
    {
1045
0
        hDecoder->downMatrix = 1;
1046
0
        output_channels = 2;
1047
0
    } else {
1048
0
        output_channels = channels;
1049
0
    }
1050
1051
0
#if (defined(PS_DEC) || defined(DRM_PS))
1052
0
    hDecoder->upMatrix = 0;
1053
0
#endif
1054
1055
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1056
0
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1057
0
    {
1058
        /* In some codepath program_config_element result is ignored. */
1059
0
        if (hDecoder->pce.channels > MAX_CHANNELS)
1060
0
        {
1061
0
            hInfo->error = 22;
1062
0
            return NULL;
1063
0
        }
1064
0
    }
1065
0
    create_channel_config(hDecoder, hInfo);
1066
1067
    /* number of samples in this frame */
1068
0
    hInfo->samples = frame_len*output_channels;
1069
    /* number of channels in this frame */
1070
0
    hInfo->channels = output_channels;
1071
    /* samplerate */
1072
0
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1073
    /* object type */
1074
0
    hInfo->object_type = hDecoder->object_type;
1075
    /* sbr */
1076
0
    hInfo->sbr = NO_SBR;
1077
    /* header type */
1078
0
    hInfo->header_type = RAW;
1079
0
    if (hDecoder->adif_header_present)
1080
0
        hInfo->header_type = ADIF;
1081
0
    if (hDecoder->adts_header_present)
1082
0
        hInfo->header_type = ADTS;
1083
#if 0
1084
    if (hDecoder->latm_header_present)
1085
        hInfo->header_type = LATM;
1086
#endif
1087
0
#if (defined(PS_DEC) || defined(DRM_PS))
1088
0
    hInfo->ps = hDecoder->ps_used_global;
1089
0
#endif
1090
1091
    /* check if frame has channel elements */
1092
0
    if (channels == 0)
1093
0
    {
1094
0
        hDecoder->frame++;
1095
0
        return NULL;
1096
0
    }
1097
1098
0
    {
1099
0
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1100
0
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1101
0
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1102
0
        };
1103
0
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1104
0
#ifdef SBR_DEC
1105
0
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1106
0
        {
1107
0
            stride = 2 * stride;
1108
0
        }
1109
0
#endif
1110
0
        required_buffer_size = frame_len*output_channels*stride;
1111
0
    }
1112
1113
    /* check if we want to use internal sample_buffer */
1114
0
    if (sample_buffer_size == 0)
1115
0
    {
1116
        /* allocate the buffer for the final samples */
1117
0
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1118
0
        {
1119
0
            if (hDecoder->sample_buffer)
1120
0
                faad_free(hDecoder->sample_buffer);
1121
0
            hDecoder->sample_buffer = NULL;
1122
0
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1123
0
            hDecoder->sample_buffer_size = required_buffer_size;
1124
0
        }
1125
0
    } else if (sample_buffer_size < required_buffer_size) {
1126
        /* provided sample buffer is not big enough */
1127
0
        hInfo->error = 27;
1128
0
        return NULL;
1129
0
    }
1130
1131
0
    if (sample_buffer_size == 0)
1132
0
    {
1133
0
        sample_buffer = hDecoder->sample_buffer;
1134
0
    } else {
1135
0
        sample_buffer = *sample_buffer2;
1136
0
    }
1137
1138
0
#ifdef SBR_DEC
1139
0
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1140
0
    {
1141
0
        uint8_t ele;
1142
1143
        /* this data is different when SBR is used or when the data is upsampled */
1144
0
        if (!hDecoder->downSampledSBR)
1145
0
        {
1146
0
            frame_len *= 2;
1147
0
            hInfo->samples *= 2;
1148
0
            hInfo->samplerate *= 2;
1149
0
        }
1150
1151
        /* check if every element was provided with SBR data */
1152
0
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1153
0
        {
1154
0
            if (hDecoder->sbr[ele] == NULL)
1155
0
            {
1156
0
                hInfo->error = 25;
1157
0
                goto error;
1158
0
            }
1159
0
        }
1160
1161
        /* sbr */
1162
0
        if (hDecoder->sbr_present_flag == 1)
1163
0
        {
1164
0
            hInfo->object_type = HE_AAC;
1165
0
            hInfo->sbr = SBR_UPSAMPLED;
1166
0
        } else {
1167
0
            hInfo->sbr = NO_SBR_UPSAMPLED;
1168
0
        }
1169
0
        if (hDecoder->downSampledSBR)
1170
0
        {
1171
0
            hInfo->sbr = SBR_DOWNSAMPLED;
1172
0
        }
1173
0
    }
1174
0
#endif
1175
1176
1177
0
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1178
0
        output_channels, frame_len, hDecoder->config.outputFormat);
1179
1180
1181
#ifdef DRM
1182
    //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1183
#endif
1184
1185
1186
0
    hDecoder->postSeekResetFlag = 0;
1187
1188
0
    hDecoder->frame++;
1189
0
#ifdef LD_DEC
1190
0
    if (hDecoder->object_type != LD)
1191
0
    {
1192
0
#endif
1193
0
        if (hDecoder->frame <= 1)
1194
0
            hInfo->samples = 0;
1195
0
#ifdef LD_DEC
1196
0
    } else {
1197
        /* LD encoders will give lower delay */
1198
0
        if (hDecoder->frame <= 0)
1199
0
            hInfo->samples = 0;
1200
0
    }
1201
0
#endif
1202
1203
    /* cleanup */
1204
#ifdef ANALYSIS
1205
    fflush(stdout);
1206
#endif
1207
1208
#ifdef PROFILE
1209
    count = faad_get_ts() - count;
1210
    hDecoder->cycles += count;
1211
#endif
1212
1213
0
    return sample_buffer;
1214
1215
0
error:
1216
1217
    /* reset filterbank state */
1218
0
    for (i = 0; i < MAX_CHANNELS; i++)
1219
0
    {
1220
0
        if (hDecoder->fb_intermed[i] != NULL)
1221
0
        {
1222
0
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1223
0
        }
1224
0
    }
1225
0
#ifdef SBR_DEC
1226
0
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1227
0
    {
1228
0
        if (hDecoder->sbr[i] != NULL)
1229
0
        {
1230
0
            sbrReset(hDecoder->sbr[i]);
1231
0
        }
1232
0
    }
1233
0
#endif
1234
1235
1236
0
    faad_endbits(&ld);
1237
1238
    /* cleanup */
1239
#ifdef ANALYSIS
1240
    fflush(stdout);
1241
#endif
1242
1243
    return NULL;
1244
0
}