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
0
{
74
0
    static char *libfaadName = PACKAGE_VERSION;
75
0
    static char *libCopyright =
76
0
        " Copyright 2002-2004: Ahead Software AG\n"
77
0
  " http://www.audiocoding.com\n"
78
0
  " bug tracking: https://sourceforge.net/p/faac/bugs/\n";
79
80
0
    if (faad_id_string)
81
0
        *faad_id_string = libfaadName;
82
83
0
    if (faad_copyright_string)
84
0
        *faad_copyright_string = libCopyright;
85
86
0
    return 0;
87
0
}
88
89
char* NeAACDecGetErrorMessage(unsigned char errcode)
90
0
{
91
0
    if (errcode >= NUM_ERROR_MESSAGES)
92
0
        return NULL;
93
0
    return err_msg[errcode];
94
0
}
95
96
unsigned long NeAACDecGetCapabilities(void)
97
0
{
98
0
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
0
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
    cap += MAIN_DEC_CAP;
105
#endif
106
#ifdef LTP_DEC
107
    cap += LTP_DEC_CAP;
108
#endif
109
#ifdef LD_DEC
110
    cap += LD_DEC_CAP;
111
#endif
112
0
#ifdef ERROR_RESILIENCE
113
0
    cap += ERROR_RESILIENCE_CAP;
114
0
#endif
115
#ifdef FIXED_POINT
116
    cap += FIXED_POINT_CAP;
117
#endif
118
119
0
    return cap;
120
0
}
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
9.25k
{
125
9.25k
    uint8_t i;
126
9.25k
    NeAACDecStruct *hDecoder = NULL;
127
128
9.25k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
9.25k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
9.25k
    hDecoder->cmes = mes;
134
9.25k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
9.25k
    hDecoder->config.defObjectType = MAIN;
136
9.25k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
9.25k
    hDecoder->config.downMatrix = 0;
138
9.25k
    hDecoder->adts_header_present = 0;
139
9.25k
    hDecoder->adif_header_present = 0;
140
9.25k
    hDecoder->latm_header_present = 0;
141
9.25k
#ifdef ERROR_RESILIENCE
142
9.25k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
9.25k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
9.25k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
9.25k
#endif
146
9.25k
    hDecoder->frameLength = 1024;
147
148
9.25k
    hDecoder->frame = 0;
149
9.25k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
9.25k
    hDecoder->__r1 = 0x2bb431ea;
153
9.25k
    hDecoder->__r2 = 0x206155b7;
154
155
601k
    for (i = 0; i < MAX_CHANNELS; i++)
156
592k
    {
157
592k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
592k
        hDecoder->window_shape_prev[i] = 0;
159
592k
        hDecoder->time_out[i] = NULL;
160
592k
        hDecoder->fb_intermed[i] = NULL;
161
#ifdef SSR_DEC
162
        hDecoder->ssr_overlap[i] = NULL;
163
        hDecoder->prev_fmd[i] = NULL;
164
#endif
165
#ifdef MAIN_DEC
166
        hDecoder->pred_stat[i] = NULL;
167
#endif
168
#ifdef LTP_DEC
169
        hDecoder->ltp_lag[i] = 0;
170
        hDecoder->lt_pred_stat[i] = NULL;
171
#endif
172
592k
    }
173
174
9.25k
#ifdef SBR_DEC
175
453k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
444k
    {
177
444k
        hDecoder->sbr[i] = NULL;
178
444k
    }
179
9.25k
#endif
180
181
9.25k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
9.25k
#if (defined(PS_DEC) || defined(DRM_PS))
183
9.25k
    hDecoder->ps_present_flag = -1;
184
9.25k
#endif
185
186
9.25k
    return hDecoder;
187
9.25k
}
188
189
NeAACDecConfigurationPtr NeAACDecGetCurrentConfiguration(NeAACDecHandle hpDecoder)
190
8.46k
{
191
8.46k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
192
8.46k
    if (hDecoder)
193
8.46k
    {
194
8.46k
        NeAACDecConfigurationPtr config = &(hDecoder->config);
195
196
8.46k
        return config;
197
8.46k
    }
198
199
0
    return NULL;
200
8.46k
}
201
202
unsigned char NeAACDecSetConfiguration(NeAACDecHandle hpDecoder,
203
                                                   NeAACDecConfigurationPtr config)
204
8.54k
{
205
8.54k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
206
8.54k
    if (hDecoder && config)
207
8.54k
    {
208
        /* check if we can decode this object type */
209
8.54k
        if (can_decode_ot(config->defObjectType) < 0)
210
3
            return 0;
211
8.54k
        hDecoder->config.defObjectType = config->defObjectType;
212
213
        /* samplerate: anything but 0 should be possible */
214
8.54k
        if (config->defSampleRate == 0)
215
1
            return 0;
216
8.54k
        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
8.54k
        if ((config->outputFormat < 1) || (config->outputFormat > 5))
224
67
            return 0;
225
8.47k
#endif
226
8.47k
        hDecoder->config.outputFormat = config->outputFormat;
227
228
8.47k
        if (config->downMatrix > 1)
229
9
            return 0;
230
8.46k
        hDecoder->config.downMatrix = config->downMatrix;
231
232
        /* OK */
233
8.46k
        return 1;
234
8.47k
    }
235
236
0
    return 0;
237
8.54k
}
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
5.77k
{
272
5.77k
    uint32_t bits = 0;
273
5.77k
    bitfile ld;
274
5.77k
    adif_header adif;
275
5.77k
    adts_header adts;
276
5.77k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
277
278
279
5.77k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
280
24
        return -1;
281
282
5.75k
    adts.old_format = hDecoder->config.useOldADTSFormat;
283
5.75k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
284
5.75k
    hDecoder->object_type = hDecoder->config.defObjectType;
285
5.75k
    *samplerate = get_sample_rate(hDecoder->sf_index);
286
5.75k
    *channels = 1;
287
288
5.75k
    if (buffer != NULL)
289
5.75k
    {
290
#if 0
291
        int is_latm;
292
        latm_header *l = &hDecoder->latm_config;
293
#endif
294
295
5.75k
        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
5.75k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
315
124
            (buffer[2] == 'I') && (buffer[3] == 'F'))
316
109
        {
317
109
            hDecoder->adif_header_present = 1;
318
319
109
            get_adif_header(&adif, &ld);
320
109
            faad_byte_align(&ld);
321
322
109
            hDecoder->sf_index = adif.pce[0].sf_index;
323
109
            hDecoder->object_type = adif.pce[0].object_type + 1;
324
325
109
            *samplerate = get_sample_rate(hDecoder->sf_index);
326
109
            *channels = adif.pce[0].channels;
327
328
109
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
329
109
            hDecoder->pce_set = 1;
330
331
109
            bits = bit2byte(faad_get_processed_bits(&ld));
332
333
        /* Check if an ADTS header is present */
334
5.64k
        } else if (adts_frame(&adts, &ld) == 0) {
335
99
            hDecoder->adts_header_present = 1;
336
337
99
            hDecoder->sf_index = adts.sf_index;
338
99
            hDecoder->object_type = adts.profile + 1;
339
340
99
            *samplerate = get_sample_rate(hDecoder->sf_index);
341
99
            *channels = (adts.channel_configuration > 6) ?
342
92
                2 : adts.channel_configuration;
343
99
        }
344
345
5.75k
        if (ld.error)
346
0
        {
347
0
            faad_endbits(&ld);
348
0
            return -1;
349
0
        }
350
5.75k
        faad_endbits(&ld);
351
5.75k
    }
352
353
5.75k
    if (!*samplerate)
354
32
        return -1;
355
356
5.71k
    hDecoder->channelConfiguration = *channels;
357
358
5.71k
#ifdef SBR_DEC
359
    /* implicit signalling */
360
5.71k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
361
2.70k
    {
362
2.70k
        *samplerate *= 2;
363
2.70k
        hDecoder->forceUpSampling = 1;
364
3.01k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
365
3.01k
        hDecoder->downSampledSBR = 1;
366
3.01k
    }
367
5.71k
#endif
368
369
5.71k
    if (can_decode_ot(hDecoder->object_type) < 0)
370
102
        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
5.61k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
379
380
#ifdef LD_DEC
381
    if (hDecoder->object_type == LD)
382
        hDecoder->frameLength >>= 1;
383
#endif
384
385
5.61k
    return bits;
386
5.71k
}
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
2.69k
{
395
2.69k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
396
2.69k
    int8_t rc;
397
2.69k
    mp4AudioSpecificConfig mp4ASC;
398
399
2.69k
    if((hDecoder == NULL)
400
2.69k
        || (pBuffer == NULL)
401
2.69k
        || (SizeOfDecoderSpecificInfo < 2)
402
2.68k
        || (samplerate == NULL)
403
2.68k
        || (channels == NULL))
404
8
    {
405
8
        return -1;
406
8
    }
407
408
2.68k
    hDecoder->adif_header_present = 0;
409
2.68k
    hDecoder->adts_header_present = 0;
410
411
    /* decode the audio specific config */
412
2.68k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
413
2.68k
        &(hDecoder->pce), hDecoder->latm_header_present);
414
415
    /* copy the relevant info to the decoder handle */
416
2.68k
    *samplerate = mp4ASC.samplingFrequency;
417
2.68k
    if (mp4ASC.channelsConfiguration)
418
2.22k
    {
419
2.22k
        *channels = mp4ASC.channelsConfiguration;
420
2.22k
    } else {
421
461
        *channels = hDecoder->pce.channels;
422
461
        hDecoder->pce_set = 1;
423
461
    }
424
2.68k
#if (defined(PS_DEC) || defined(DRM_PS))
425
2.68k
    hDecoder->ps_present_flag = mp4ASC.ps_present_flag;
426
2.68k
#endif
427
2.68k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
428
2.68k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
429
2.68k
#ifdef ERROR_RESILIENCE
430
2.68k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
431
2.68k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
432
2.68k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
433
2.68k
#endif
434
2.68k
#ifdef SBR_DEC
435
2.68k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
436
2.68k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
437
2.68k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
438
2.68k
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
439
0
    else
440
0
        hDecoder->forceUpSampling = 0;
441
442
    /* AAC core decoder samplerate is 2 times as low */
443
2.68k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
444
1.58k
    {
445
1.58k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
446
1.58k
    }
447
2.68k
#endif
448
449
2.68k
    if (rc != 0)
450
41
    {
451
41
        return rc;
452
41
    }
453
2.64k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
454
2.64k
    if (mp4ASC.frameLengthFlag)
455
1.73k
#ifdef ALLOW_SMALL_FRAMELENGTH
456
1.73k
        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
2.64k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
468
469
#ifdef LD_DEC
470
    if (hDecoder->object_type == LD)
471
        hDecoder->frameLength >>= 1;
472
#endif
473
474
2.64k
    return 0;
475
2.68k
}
476
477
#ifdef DRM
478
char NeAACDecInitDRM(NeAACDecHandle *hpDecoder,
479
                                 unsigned long samplerate,
480
                                 unsigned char channels)
481
711
{
482
711
    NeAACDecStruct** hDecoder = (NeAACDecStruct**)hpDecoder;
483
711
    if (hDecoder == NULL)
484
0
        return 1; /* error */
485
486
711
    NeAACDecClose(*hDecoder);
487
488
711
    *hDecoder = NeAACDecOpen();
489
490
    /* Special object type defined for DRM */
491
711
    (*hDecoder)->config.defObjectType = DRM_ER_LC;
492
493
711
    (*hDecoder)->config.defSampleRate = samplerate;
494
711
#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
495
711
    (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
496
711
    (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
497
711
    (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
498
711
#endif
499
711
    (*hDecoder)->frameLength = 960;
500
711
    (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
501
711
    (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
502
503
711
    if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
504
136
        (*hDecoder)->channelConfiguration = 2;
505
575
    else
506
575
        (*hDecoder)->channelConfiguration = 1;
507
508
711
#ifdef SBR_DEC
509
711
    if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
510
133
        (*hDecoder)->sbr_present_flag = 0;
511
578
    else
512
578
        (*hDecoder)->sbr_present_flag = 1;
513
711
#endif
514
515
711
    (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
516
517
711
    return 0;
518
711
}
519
#endif
520
521
void NeAACDecClose(NeAACDecHandle hpDecoder)
522
9.25k
{
523
9.25k
    uint8_t i;
524
9.25k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
525
526
9.25k
    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
601k
    for (i = 0; i < MAX_CHANNELS; i++)
538
592k
    {
539
592k
        if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
540
592k
        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
#ifdef MAIN_DEC
546
        if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
547
#endif
548
#ifdef LTP_DEC
549
        if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
550
#endif
551
592k
    }
552
553
#ifdef SSR_DEC
554
    if (hDecoder->object_type == SSR)
555
        ssr_filter_bank_end(hDecoder->fb);
556
    else
557
#endif
558
9.25k
        filter_bank_end(hDecoder->fb);
559
560
9.25k
    drc_end(hDecoder->drc);
561
562
9.25k
    if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
563
564
9.25k
#ifdef SBR_DEC
565
453k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
566
444k
    {
567
444k
        if (hDecoder->sbr[i])
568
108k
            sbrDecodeEnd(hDecoder->sbr[i]);
569
444k
    }
570
9.25k
#endif
571
572
9.25k
    if (hDecoder) faad_free(hDecoder);
573
9.25k
}
574
575
void NeAACDecPostSeekReset(NeAACDecHandle hpDecoder, long frame)
576
5.99k
{
577
5.99k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
578
5.99k
    if (hDecoder)
579
5.99k
    {
580
5.99k
        hDecoder->postSeekResetFlag = 1;
581
582
5.99k
        if (frame != -1)
583
5.99k
            hDecoder->frame = frame;
584
5.99k
    }
585
5.99k
}
586
587
static void create_channel_config(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo)
588
2.87k
{
589
2.87k
    hInfo->num_front_channels = 0;
590
2.87k
    hInfo->num_side_channels = 0;
591
2.87k
    hInfo->num_back_channels = 0;
592
2.87k
    hInfo->num_lfe_channels = 0;
593
2.87k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
594
595
2.87k
    if (hDecoder->downMatrix)
596
598
    {
597
598
        hInfo->num_front_channels = 2;
598
598
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
599
598
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
600
598
        return;
601
598
    }
602
603
    /* check if there is a PCE */
604
    /* TODO: why CPE flag is ignored? */
605
2.27k
    if (hDecoder->pce_set)
606
195
    {
607
195
        uint8_t i, chpos = 0;
608
195
        uint8_t chdir, back_center = 0;
609
610
195
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
611
195
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
612
195
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
613
195
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
614
195
        chdir = hInfo->num_front_channels;
615
195
        if (chdir & 1)
616
94
        {
617
94
#if (defined(PS_DEC) || defined(DRM_PS))
618
94
            if (hInfo->num_front_channels == 1 &&
619
42
                hInfo->num_side_channels == 0 &&
620
25
                hInfo->num_back_channels == 0 &&
621
22
                hInfo->num_lfe_channels == 0 &&
622
19
                hDecoder->ps_used_global)
623
5
            {
624
                /* When PS is enabled output is always stereo */
625
5
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
626
5
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
627
5
            } else
628
89
#endif
629
89
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
630
94
            chdir--;
631
94
        }
632
969
        for (i = 0; i < chdir; i++)
633
774
        {
634
774
            hInfo->channel_position[chpos++] =
635
774
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
636
774
        }
637
638
852
        for (i = 0; i < hInfo->num_side_channels; i++)
639
657
        {
640
657
            hInfo->channel_position[chpos++] =
641
657
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
642
657
        }
643
644
195
        chdir = hInfo->num_back_channels;
645
195
        if (chdir & 1)
646
15
        {
647
15
            back_center = 1;
648
15
            chdir--;
649
15
        }
650
467
        for (i = 0; i < chdir; i++)
651
272
        {
652
272
            hInfo->channel_position[chpos++] =
653
272
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
654
272
        }
655
195
        if (back_center)
656
15
        {
657
15
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
658
15
        }
659
660
261
        for (i = 0; i < hInfo->num_lfe_channels; i++)
661
66
        {
662
66
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
663
66
        }
664
665
2.08k
    } else {
666
2.08k
        switch (hDecoder->channelConfiguration)
667
2.08k
        {
668
880
        case 1:
669
880
#if (defined(PS_DEC) || defined(DRM_PS))
670
880
            if (hDecoder->ps_used_global)
671
237
            {
672
                /* When PS is enabled output is always stereo */
673
237
                hInfo->num_front_channels = 2;
674
237
                hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
675
237
                hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
676
643
            } else {
677
643
                hInfo->num_front_channels = 1;
678
643
                hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
679
643
            }
680
#else
681
            hInfo->num_front_channels = 1;
682
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
683
#endif
684
880
            break;
685
101
        case 2:
686
101
            hInfo->num_front_channels = 2;
687
101
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
688
101
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
689
101
            break;
690
158
        case 3:
691
158
            hInfo->num_front_channels = 3;
692
158
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
693
158
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
694
158
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
695
158
            break;
696
174
        case 4:
697
174
            hInfo->num_front_channels = 3;
698
174
            hInfo->num_back_channels = 1;
699
174
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
700
174
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
701
174
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
702
174
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
703
174
            break;
704
215
        case 5:
705
215
            hInfo->num_front_channels = 3;
706
215
            hInfo->num_back_channels = 2;
707
215
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
708
215
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
709
215
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
710
215
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
711
215
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
712
215
            break;
713
28
        case 6:
714
28
            hInfo->num_front_channels = 3;
715
28
            hInfo->num_back_channels = 2;
716
28
            hInfo->num_lfe_channels = 1;
717
28
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
718
28
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
719
28
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
720
28
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
721
28
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
722
28
            hInfo->channel_position[5] = LFE_CHANNEL;
723
28
            break;
724
494
        case 7:
725
494
            hInfo->num_front_channels = 3;
726
494
            hInfo->num_side_channels = 2;
727
494
            hInfo->num_back_channels = 2;
728
494
            hInfo->num_lfe_channels = 1;
729
494
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
730
494
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
731
494
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
732
494
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
733
494
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
734
494
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
735
494
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
736
494
            hInfo->channel_position[7] = LFE_CHANNEL;
737
494
            break;
738
33
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
739
33
            {
740
33
                uint8_t i;
741
33
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
742
33
                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
33
                } else {
776
33
                    uint8_t ch1 = (ch)/2;
777
33
                    hInfo->num_front_channels = ch1;
778
33
                    hInfo->num_back_channels = ch1;
779
33
                    if (ch1 & 1)
780
20
                    {
781
20
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
782
125
                        for (i = 1; i <= ch1; i+=2)
783
105
                        {
784
105
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
785
105
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
786
105
                        }
787
105
                        for (i = ch1+1; i < ch-1; i+=2)
788
85
                        {
789
85
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
790
85
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
791
85
                        }
792
20
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
793
20
                    } else {
794
92
                        for (i = 0; i < ch1; i+=2)
795
79
                        {
796
79
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
797
79
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
798
79
                        }
799
92
                        for (i = ch1; i < ch; i+=2)
800
79
                        {
801
79
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
802
79
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
803
79
                        }
804
13
                    }
805
33
                }
806
33
                hInfo->num_lfe_channels = hDecoder->has_lfe;
807
33
                for (i = ch; i < hDecoder->fr_channels; i++)
808
0
                {
809
0
                    hInfo->channel_position[i] = LFE_CHANNEL;
810
0
                }
811
33
            }
812
33
            break;
813
2.08k
        }
814
2.08k
    }
815
2.27k
}
816
817
void* NeAACDecDecode(NeAACDecHandle hpDecoder,
818
                                 NeAACDecFrameInfo *hInfo,
819
                                 unsigned char *buffer,
820
                                 unsigned long buffer_size)
821
9.92k
{
822
9.92k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
823
9.92k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
824
9.92k
}
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
6.55k
{
833
6.55k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
834
6.55k
    if ((sample_buffer_size == 0) || (sample_buffer == NULL) || (*sample_buffer == NULL))
835
58
    {
836
58
        hInfo->error = 27;
837
58
        return NULL;
838
58
    }
839
840
6.50k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
841
6.50k
        sample_buffer, sample_buffer_size);
842
6.55k
}
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
16.4k
{
851
16.4k
    uint16_t i;
852
16.4k
    uint8_t channels = 0;
853
16.4k
    uint8_t output_channels = 0;
854
16.4k
    bitfile ld;
855
16.4k
    uint32_t bitsconsumed;
856
16.4k
    uint16_t frame_len;
857
16.4k
    void *sample_buffer;
858
#if 0
859
    uint32_t startbit=0, endbit=0, payload_bits=0;
860
#endif
861
16.4k
    uint32_t required_buffer_size=0;
862
863
#ifdef PROFILE
864
    int64_t count = faad_get_ts();
865
#endif
866
867
    /* safety checks */
868
16.4k
    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
16.4k
    frame_len = hDecoder->frameLength;
878
879
880
16.4k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
881
16.4k
    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
16.4k
    if (buffer_size >= 128)
899
1.42k
    {
900
1.42k
        if (memcmp(buffer, "TAG", 3) == 0)
901
3
        {
902
            /* found it */
903
3
            hInfo->bytesconsumed = 128; /* 128 bytes fixed size */
904
            /* no error, but no output either */
905
3
            return NULL;
906
3
        }
907
1.42k
    }
908
909
910
    /* initialize the bitstream */
911
16.4k
    faad_initbits(&ld, buffer, buffer_size);
912
16.4k
    if (ld.error != 0)
913
6.83k
        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
9.58k
#ifdef DRM
947
9.58k
    if (hDecoder->object_type == DRM_ER_LC)
948
866
    {
949
        /* We do not support stereo right now */
950
866
        if (0) //(hDecoder->channelConfiguration == 2)
951
0
        {
952
0
            hInfo->error = 28; // Throw CRC error
953
0
            goto error;
954
0
        }
955
956
866
        faad_getbits(&ld, 8
957
866
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
958
866
    }
959
9.58k
#endif
960
961
9.58k
    if (hDecoder->adts_header_present)
962
87
    {
963
87
        adts_header adts;
964
965
87
        adts.old_format = hDecoder->config.useOldADTSFormat;
966
87
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
967
3
            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
87
    }
974
975
#ifdef ANALYSIS
976
    dbg_count = 0;
977
#endif
978
979
    /* decode the complete bitstream */
980
9.58k
#ifdef DRM
981
9.58k
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
982
866
    {
983
866
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
984
8.72k
    } else {
985
8.72k
#endif
986
8.72k
        raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
987
8.72k
#ifdef DRM
988
8.72k
    }
989
9.58k
#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
9.58k
    channels = hDecoder->fr_channels;
1005
1006
9.58k
    if (hInfo->error > 0)
1007
6.68k
        goto error;
1008
1009
    /* safety check */
1010
2.90k
    if (channels == 0 || channels > MAX_CHANNELS)
1011
23
    {
1012
        /* invalid number of channels */
1013
23
        hInfo->error = 12;
1014
23
        goto error;
1015
23
    }
1016
1017
    /* no more bit reading after this */
1018
2.87k
    bitsconsumed = faad_get_processed_bits(&ld);
1019
2.87k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1020
2.87k
    if (ld.error)
1021
0
    {
1022
0
        hInfo->error = 14;
1023
0
        goto error;
1024
0
    }
1025
2.87k
    faad_endbits(&ld);
1026
1027
1028
2.87k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1029
#if 0
1030
        && !hDecoder->latm_header_present
1031
#endif
1032
2.87k
        )
1033
2.84k
    {
1034
2.84k
        if (hDecoder->channelConfiguration == 0)
1035
156
            hDecoder->channelConfiguration = channels;
1036
1037
2.84k
        if (channels == 8) /* 7.1 */
1038
127
            hDecoder->channelConfiguration = 7;
1039
2.84k
        if (channels == 7) /* not a standard channelConfiguration */
1040
0
            hDecoder->channelConfiguration = 0;
1041
2.84k
    }
1042
1043
2.87k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1044
598
    {
1045
598
        hDecoder->downMatrix = 1;
1046
598
        output_channels = 2;
1047
2.28k
    } else {
1048
2.28k
        output_channels = channels;
1049
2.28k
    }
1050
1051
2.87k
#if (defined(PS_DEC) || defined(DRM_PS))
1052
2.87k
    hDecoder->upMatrix = 0;
1053
2.87k
#endif
1054
1055
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1056
2.87k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1057
198
    {
1058
        /* In some codepath program_config_element result is ignored. */
1059
198
        if (hDecoder->pce.channels > MAX_CHANNELS)
1060
3
        {
1061
3
            hInfo->error = 22;
1062
3
            return NULL;
1063
3
        }
1064
198
    }
1065
2.87k
    create_channel_config(hDecoder, hInfo);
1066
1067
    /* number of samples in this frame */
1068
2.87k
    hInfo->samples = frame_len*output_channels;
1069
    /* number of channels in this frame */
1070
2.87k
    hInfo->channels = output_channels;
1071
    /* samplerate */
1072
2.87k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1073
    /* object type */
1074
2.87k
    hInfo->object_type = hDecoder->object_type;
1075
    /* sbr */
1076
2.87k
    hInfo->sbr = NO_SBR;
1077
    /* header type */
1078
2.87k
    hInfo->header_type = RAW;
1079
2.87k
    if (hDecoder->adif_header_present)
1080
0
        hInfo->header_type = ADIF;
1081
2.87k
    if (hDecoder->adts_header_present)
1082
34
        hInfo->header_type = ADTS;
1083
#if 0
1084
    if (hDecoder->latm_header_present)
1085
        hInfo->header_type = LATM;
1086
#endif
1087
2.87k
#if (defined(PS_DEC) || defined(DRM_PS))
1088
2.87k
    hInfo->ps = hDecoder->ps_used_global;
1089
2.87k
#endif
1090
1091
    /* check if frame has channel elements */
1092
2.87k
    if (channels == 0)
1093
0
    {
1094
0
        hDecoder->frame++;
1095
0
        return NULL;
1096
0
    }
1097
1098
2.87k
    {
1099
2.87k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1100
2.87k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1101
2.87k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1102
2.87k
        };
1103
2.87k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1104
2.87k
#ifdef SBR_DEC
1105
2.87k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1106
1.55k
        {
1107
1.55k
            stride = 2 * stride;
1108
1.55k
        }
1109
2.87k
#endif
1110
2.87k
        required_buffer_size = frame_len*output_channels*stride;
1111
2.87k
    }
1112
1113
    /* check if we want to use internal sample_buffer */
1114
2.87k
    if (sample_buffer_size == 0)
1115
1.39k
    {
1116
        /* allocate the buffer for the final samples */
1117
1.39k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1118
1.27k
        {
1119
1.27k
            if (hDecoder->sample_buffer)
1120
33
                faad_free(hDecoder->sample_buffer);
1121
1.27k
            hDecoder->sample_buffer = NULL;
1122
1.27k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1123
1.27k
            hDecoder->sample_buffer_size = required_buffer_size;
1124
1.27k
        }
1125
1.48k
    } else if (sample_buffer_size < required_buffer_size) {
1126
        /* provided sample buffer is not big enough */
1127
609
        hInfo->error = 27;
1128
609
        return NULL;
1129
609
    }
1130
1131
2.26k
    if (sample_buffer_size == 0)
1132
1.39k
    {
1133
1.39k
        sample_buffer = hDecoder->sample_buffer;
1134
1.39k
    } else {
1135
875
        sample_buffer = *sample_buffer2;
1136
875
    }
1137
1138
2.26k
#ifdef SBR_DEC
1139
2.26k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1140
1.47k
    {
1141
1.47k
        uint8_t ele;
1142
1143
        /* this data is different when SBR is used or when the data is upsampled */
1144
1.47k
        if (!hDecoder->downSampledSBR)
1145
1.11k
        {
1146
1.11k
            frame_len *= 2;
1147
1.11k
            hInfo->samples *= 2;
1148
1.11k
            hInfo->samplerate *= 2;
1149
1.11k
        }
1150
1151
        /* check if every element was provided with SBR data */
1152
7.92k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1153
6.45k
        {
1154
6.45k
            if (hDecoder->sbr[ele] == NULL)
1155
2
            {
1156
2
                hInfo->error = 25;
1157
2
                goto error;
1158
2
            }
1159
6.45k
        }
1160
1161
        /* sbr */
1162
1.47k
        if (hDecoder->sbr_present_flag == 1)
1163
1.03k
        {
1164
1.03k
            hInfo->object_type = HE_AAC;
1165
1.03k
            hInfo->sbr = SBR_UPSAMPLED;
1166
1.03k
        } else {
1167
435
            hInfo->sbr = NO_SBR_UPSAMPLED;
1168
435
        }
1169
1.47k
        if (hDecoder->downSampledSBR)
1170
360
        {
1171
360
            hInfo->sbr = SBR_DOWNSAMPLED;
1172
360
        }
1173
1.47k
    }
1174
2.26k
#endif
1175
1176
1177
2.26k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1178
2.26k
        output_channels, frame_len, hDecoder->config.outputFormat);
1179
1180
1181
2.26k
#ifdef DRM
1182
    //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1183
2.26k
#endif
1184
1185
1186
2.26k
    hDecoder->postSeekResetFlag = 0;
1187
1188
2.26k
    hDecoder->frame++;
1189
#ifdef LD_DEC
1190
    if (hDecoder->object_type != LD)
1191
    {
1192
#endif
1193
2.26k
        if (hDecoder->frame <= 1)
1194
849
            hInfo->samples = 0;
1195
#ifdef LD_DEC
1196
    } else {
1197
        /* LD encoders will give lower delay */
1198
        if (hDecoder->frame <= 0)
1199
            hInfo->samples = 0;
1200
    }
1201
#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
2.26k
    return sample_buffer;
1214
1215
6.71k
error:
1216
1217
    /* reset filterbank state */
1218
436k
    for (i = 0; i < MAX_CHANNELS; i++)
1219
429k
    {
1220
429k
        if (hDecoder->fb_intermed[i] != NULL)
1221
161k
        {
1222
161k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1223
161k
        }
1224
429k
    }
1225
6.71k
#ifdef SBR_DEC
1226
328k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1227
322k
    {
1228
322k
        if (hDecoder->sbr[i] != NULL)
1229
121k
        {
1230
121k
            sbrReset(hDecoder->sbr[i]);
1231
121k
        }
1232
322k
    }
1233
6.71k
#endif
1234
1235
1236
6.71k
    faad_endbits(&ld);
1237
1238
    /* cleanup */
1239
#ifdef ANALYSIS
1240
    fflush(stdout);
1241
#endif
1242
1243
    return NULL;
1244
2.26k
}