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
0
#ifdef MAIN_DEC
104
0
    cap += MAIN_DEC_CAP;
105
0
#endif
106
0
#ifdef LTP_DEC
107
0
    cap += LTP_DEC_CAP;
108
0
#endif
109
0
#ifdef LD_DEC
110
0
    cap += LD_DEC_CAP;
111
0
#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.45k
{
125
9.45k
    uint8_t i;
126
9.45k
    NeAACDecStruct *hDecoder = NULL;
127
128
9.45k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
9.45k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
9.45k
    hDecoder->cmes = mes;
134
9.45k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
9.45k
    hDecoder->config.defObjectType = MAIN;
136
9.45k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
9.45k
    hDecoder->config.downMatrix = 0;
138
9.45k
    hDecoder->adts_header_present = 0;
139
9.45k
    hDecoder->adif_header_present = 0;
140
9.45k
    hDecoder->latm_header_present = 0;
141
9.45k
#ifdef ERROR_RESILIENCE
142
9.45k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
9.45k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
9.45k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
9.45k
#endif
146
9.45k
    hDecoder->frameLength = 1024;
147
148
9.45k
    hDecoder->frame = 0;
149
9.45k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
9.45k
    hDecoder->__r1 = 0x2bb431ea;
153
9.45k
    hDecoder->__r2 = 0x206155b7;
154
155
614k
    for (i = 0; i < MAX_CHANNELS; i++)
156
605k
    {
157
605k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
605k
        hDecoder->window_shape_prev[i] = 0;
159
605k
        hDecoder->time_out[i] = NULL;
160
605k
        hDecoder->fb_intermed[i] = NULL;
161
#ifdef SSR_DEC
162
        hDecoder->ssr_overlap[i] = NULL;
163
        hDecoder->prev_fmd[i] = NULL;
164
#endif
165
605k
#ifdef MAIN_DEC
166
605k
        hDecoder->pred_stat[i] = NULL;
167
605k
#endif
168
605k
#ifdef LTP_DEC
169
605k
        hDecoder->ltp_lag[i] = 0;
170
605k
        hDecoder->lt_pred_stat[i] = NULL;
171
605k
#endif
172
605k
    }
173
174
9.45k
#ifdef SBR_DEC
175
463k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
453k
    {
177
453k
        hDecoder->sbr[i] = NULL;
178
453k
    }
179
9.45k
#endif
180
181
9.45k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
9.45k
#if (defined(PS_DEC) || defined(DRM_PS))
183
9.45k
    hDecoder->ps_present_flag = -1;
184
9.45k
#endif
185
186
9.45k
    return hDecoder;
187
9.45k
}
188
189
NeAACDecConfigurationPtr NeAACDecGetCurrentConfiguration(NeAACDecHandle hpDecoder)
190
9.37k
{
191
9.37k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
192
9.37k
    if (hDecoder)
193
9.37k
    {
194
9.37k
        NeAACDecConfigurationPtr config = &(hDecoder->config);
195
196
9.37k
        return config;
197
9.37k
    }
198
199
0
    return NULL;
200
9.37k
}
201
202
unsigned char NeAACDecSetConfiguration(NeAACDecHandle hpDecoder,
203
                                                   NeAACDecConfigurationPtr config)
204
9.45k
{
205
9.45k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
206
9.45k
    if (hDecoder && config)
207
9.45k
    {
208
        /* check if we can decode this object type */
209
9.45k
        if (can_decode_ot(config->defObjectType) < 0)
210
4
            return 0;
211
9.45k
        hDecoder->config.defObjectType = config->defObjectType;
212
213
        /* samplerate: anything but 0 should be possible */
214
9.45k
        if (config->defSampleRate == 0)
215
1
            return 0;
216
9.44k
        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
9.44k
        if ((config->outputFormat < 1) || (config->outputFormat > 5))
224
71
            return 0;
225
9.37k
#endif
226
9.37k
        hDecoder->config.outputFormat = config->outputFormat;
227
228
9.37k
        if (config->downMatrix > 1)
229
8
            return 0;
230
9.37k
        hDecoder->config.downMatrix = config->downMatrix;
231
232
        /* OK */
233
9.37k
        return 1;
234
9.37k
    }
235
236
0
    return 0;
237
9.45k
}
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
6.22k
{
272
6.22k
    uint32_t bits = 0;
273
6.22k
    bitfile ld;
274
6.22k
    adif_header adif;
275
6.22k
    adts_header adts;
276
6.22k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
277
278
279
6.22k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
280
18
        return -1;
281
282
6.20k
    adts.old_format = hDecoder->config.useOldADTSFormat;
283
6.20k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
284
6.20k
    hDecoder->object_type = hDecoder->config.defObjectType;
285
6.20k
    *samplerate = get_sample_rate(hDecoder->sf_index);
286
6.20k
    *channels = 1;
287
288
6.20k
    if (buffer != NULL)
289
6.20k
    {
290
#if 0
291
        int is_latm;
292
        latm_header *l = &hDecoder->latm_config;
293
#endif
294
295
6.20k
        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
6.20k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
315
132
            (buffer[2] == 'I') && (buffer[3] == 'F'))
316
119
        {
317
119
            hDecoder->adif_header_present = 1;
318
319
119
            get_adif_header(&adif, &ld);
320
119
            faad_byte_align(&ld);
321
322
119
            hDecoder->sf_index = adif.pce[0].sf_index;
323
119
            hDecoder->object_type = adif.pce[0].object_type + 1;
324
325
119
            *samplerate = get_sample_rate(hDecoder->sf_index);
326
119
            *channels = adif.pce[0].channels;
327
328
119
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
329
119
            hDecoder->pce_set = 1;
330
331
119
            bits = bit2byte(faad_get_processed_bits(&ld));
332
333
        /* Check if an ADTS header is present */
334
6.08k
        } else if (adts_frame(&adts, &ld) == 0) {
335
327
            hDecoder->adts_header_present = 1;
336
337
327
            hDecoder->sf_index = adts.sf_index;
338
327
            hDecoder->object_type = adts.profile + 1;
339
340
327
            *samplerate = get_sample_rate(hDecoder->sf_index);
341
327
            *channels = (adts.channel_configuration > 6) ?
342
320
                2 : adts.channel_configuration;
343
327
        }
344
345
6.20k
        if (ld.error)
346
0
        {
347
0
            faad_endbits(&ld);
348
0
            return -1;
349
0
        }
350
6.20k
        faad_endbits(&ld);
351
6.20k
    }
352
353
6.20k
    if (!*samplerate)
354
43
        return -1;
355
356
6.16k
    hDecoder->channelConfiguration = *channels;
357
358
6.16k
#ifdef SBR_DEC
359
    /* implicit signalling */
360
6.16k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
361
2.89k
    {
362
2.89k
        *samplerate *= 2;
363
2.89k
        hDecoder->forceUpSampling = 1;
364
3.27k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
365
3.27k
        hDecoder->downSampledSBR = 1;
366
3.27k
    }
367
6.16k
#endif
368
369
6.16k
    if (can_decode_ot(hDecoder->object_type) < 0)
370
19
        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
6.14k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
379
380
6.14k
#ifdef LD_DEC
381
6.14k
    if (hDecoder->object_type == LD)
382
244
        hDecoder->frameLength >>= 1;
383
6.14k
#endif
384
385
6.14k
    return bits;
386
6.16k
}
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
3.14k
{
395
3.14k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
396
3.14k
    int8_t rc;
397
3.14k
    mp4AudioSpecificConfig mp4ASC;
398
399
3.14k
    if((hDecoder == NULL)
400
3.14k
        || (pBuffer == NULL)
401
3.14k
        || (SizeOfDecoderSpecificInfo < 2)
402
3.14k
        || (samplerate == NULL)
403
3.14k
        || (channels == NULL))
404
2
    {
405
2
        return -1;
406
2
    }
407
408
3.14k
    hDecoder->adif_header_present = 0;
409
3.14k
    hDecoder->adts_header_present = 0;
410
411
    /* decode the audio specific config */
412
3.14k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
413
3.14k
        &(hDecoder->pce), hDecoder->latm_header_present);
414
415
    /* copy the relevant info to the decoder handle */
416
3.14k
    *samplerate = mp4ASC.samplingFrequency;
417
3.14k
    if (mp4ASC.channelsConfiguration)
418
2.82k
    {
419
2.82k
        *channels = mp4ASC.channelsConfiguration;
420
2.82k
    } else {
421
316
        *channels = hDecoder->pce.channels;
422
316
        hDecoder->pce_set = 1;
423
316
    }
424
3.14k
#if (defined(PS_DEC) || defined(DRM_PS))
425
3.14k
    hDecoder->ps_present_flag = mp4ASC.ps_present_flag;
426
3.14k
#endif
427
3.14k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
428
3.14k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
429
3.14k
#ifdef ERROR_RESILIENCE
430
3.14k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
431
3.14k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
432
3.14k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
433
3.14k
#endif
434
3.14k
#ifdef SBR_DEC
435
3.14k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
436
3.14k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
437
3.14k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
438
3.14k
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
439
0
    else
440
0
        hDecoder->forceUpSampling = 0;
441
442
    /* AAC core decoder samplerate is 2 times as low */
443
3.14k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
444
1.91k
    {
445
1.91k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
446
1.91k
    }
447
3.14k
#endif
448
449
3.14k
    if (rc != 0)
450
45
    {
451
45
        return rc;
452
45
    }
453
3.09k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
454
3.09k
    if (mp4ASC.frameLengthFlag)
455
1.78k
#ifdef ALLOW_SMALL_FRAMELENGTH
456
1.78k
        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
3.09k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
468
469
3.09k
#ifdef LD_DEC
470
3.09k
    if (hDecoder->object_type == LD)
471
442
        hDecoder->frameLength >>= 1;
472
3.09k
#endif
473
474
3.09k
    return 0;
475
3.14k
}
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
9.45k
{
523
9.45k
    uint8_t i;
524
9.45k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
525
526
9.45k
    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
614k
    for (i = 0; i < MAX_CHANNELS; i++)
538
605k
    {
539
605k
        if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
540
605k
        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
605k
#ifdef MAIN_DEC
546
605k
        if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
547
605k
#endif
548
605k
#ifdef LTP_DEC
549
605k
        if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
550
605k
#endif
551
605k
    }
552
553
#ifdef SSR_DEC
554
    if (hDecoder->object_type == SSR)
555
        ssr_filter_bank_end(hDecoder->fb);
556
    else
557
#endif
558
9.45k
        filter_bank_end(hDecoder->fb);
559
560
9.45k
    drc_end(hDecoder->drc);
561
562
9.45k
    if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
563
564
9.45k
#ifdef SBR_DEC
565
463k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
566
453k
    {
567
453k
        if (hDecoder->sbr[i])
568
165k
            sbrDecodeEnd(hDecoder->sbr[i]);
569
453k
    }
570
9.45k
#endif
571
572
9.45k
    if (hDecoder) faad_free(hDecoder);
573
9.45k
}
574
575
void NeAACDecPostSeekReset(NeAACDecHandle hpDecoder, long frame)
576
7.59k
{
577
7.59k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
578
7.59k
    if (hDecoder)
579
7.59k
    {
580
7.59k
        hDecoder->postSeekResetFlag = 1;
581
582
7.59k
        if (frame != -1)
583
7.59k
            hDecoder->frame = frame;
584
7.59k
    }
585
7.59k
}
586
587
static void create_channel_config(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo)
588
3.13k
{
589
3.13k
    hInfo->num_front_channels = 0;
590
3.13k
    hInfo->num_side_channels = 0;
591
3.13k
    hInfo->num_back_channels = 0;
592
3.13k
    hInfo->num_lfe_channels = 0;
593
3.13k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
594
595
3.13k
    if (hDecoder->downMatrix)
596
428
    {
597
428
        hInfo->num_front_channels = 2;
598
428
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
599
428
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
600
428
        return;
601
428
    }
602
603
    /* check if there is a PCE */
604
    /* TODO: why CPE flag is ignored? */
605
2.71k
    if (hDecoder->pce_set)
606
90
    {
607
90
        uint8_t i, chpos = 0;
608
90
        uint8_t chdir, back_center = 0;
609
610
90
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
611
90
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
612
90
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
613
90
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
614
90
        chdir = hInfo->num_front_channels;
615
90
        if (chdir & 1)
616
46
        {
617
46
#if (defined(PS_DEC) || defined(DRM_PS))
618
46
            if (hInfo->num_front_channels == 1 &&
619
28
                hInfo->num_side_channels == 0 &&
620
12
                hInfo->num_back_channels == 0 &&
621
9
                hInfo->num_lfe_channels == 0 &&
622
6
                hDecoder->ps_used_global)
623
3
            {
624
                /* When PS is enabled output is always stereo */
625
3
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
626
3
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
627
3
            } else
628
43
#endif
629
43
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
630
46
            chdir--;
631
46
        }
632
548
        for (i = 0; i < chdir; i++)
633
458
        {
634
458
            hInfo->channel_position[chpos++] =
635
458
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
636
458
        }
637
638
541
        for (i = 0; i < hInfo->num_side_channels; i++)
639
451
        {
640
451
            hInfo->channel_position[chpos++] =
641
451
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
642
451
        }
643
644
90
        chdir = hInfo->num_back_channels;
645
90
        if (chdir & 1)
646
10
        {
647
10
            back_center = 1;
648
10
            chdir--;
649
10
        }
650
282
        for (i = 0; i < chdir; i++)
651
192
        {
652
192
            hInfo->channel_position[chpos++] =
653
192
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
654
192
        }
655
90
        if (back_center)
656
10
        {
657
10
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
658
10
        }
659
660
157
        for (i = 0; i < hInfo->num_lfe_channels; i++)
661
67
        {
662
67
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
663
67
        }
664
665
2.62k
    } else {
666
2.62k
        switch (hDecoder->channelConfiguration)
667
2.62k
        {
668
703
        case 1:
669
703
#if (defined(PS_DEC) || defined(DRM_PS))
670
703
            if (hDecoder->ps_used_global)
671
38
            {
672
                /* When PS is enabled output is always stereo */
673
38
                hInfo->num_front_channels = 2;
674
38
                hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
675
38
                hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
676
665
            } else {
677
665
                hInfo->num_front_channels = 1;
678
665
                hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
679
665
            }
680
#else
681
            hInfo->num_front_channels = 1;
682
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
683
#endif
684
703
            break;
685
223
        case 2:
686
223
            hInfo->num_front_channels = 2;
687
223
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
688
223
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
689
223
            break;
690
267
        case 3:
691
267
            hInfo->num_front_channels = 3;
692
267
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
693
267
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
694
267
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
695
267
            break;
696
312
        case 4:
697
312
            hInfo->num_front_channels = 3;
698
312
            hInfo->num_back_channels = 1;
699
312
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
700
312
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
701
312
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
702
312
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
703
312
            break;
704
181
        case 5:
705
181
            hInfo->num_front_channels = 3;
706
181
            hInfo->num_back_channels = 2;
707
181
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
708
181
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
709
181
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
710
181
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
711
181
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
712
181
            break;
713
238
        case 6:
714
238
            hInfo->num_front_channels = 3;
715
238
            hInfo->num_back_channels = 2;
716
238
            hInfo->num_lfe_channels = 1;
717
238
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
718
238
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
719
238
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
720
238
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
721
238
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
722
238
            hInfo->channel_position[5] = LFE_CHANNEL;
723
238
            break;
724
591
        case 7:
725
591
            hInfo->num_front_channels = 3;
726
591
            hInfo->num_side_channels = 2;
727
591
            hInfo->num_back_channels = 2;
728
591
            hInfo->num_lfe_channels = 1;
729
591
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
730
591
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
731
591
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
732
591
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
733
591
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
734
591
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
735
591
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
736
591
            hInfo->channel_position[7] = LFE_CHANNEL;
737
591
            break;
738
106
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
739
106
            {
740
106
                uint8_t i;
741
106
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
742
106
                if (ch & 1) /* there's either a center front or a center back channel */
743
47
                {
744
47
                    uint8_t ch1 = (ch-1)/2;
745
47
                    if (hDecoder->first_syn_ele == ID_SCE)
746
33
                    {
747
33
                        hInfo->num_front_channels = ch1 + 1;
748
33
                        hInfo->num_back_channels = ch1;
749
33
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
750
133
                        for (i = 1; i <= ch1; i+=2)
751
100
                        {
752
100
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
753
100
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
754
100
                        }
755
133
                        for (i = ch1+1; i < ch; i+=2)
756
100
                        {
757
100
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
758
100
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
759
100
                        }
760
33
                    } else {
761
14
                        hInfo->num_front_channels = ch1;
762
14
                        hInfo->num_back_channels = ch1 + 1;
763
100
                        for (i = 0; i < ch1; i+=2)
764
86
                        {
765
86
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
766
86
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
767
86
                        }
768
100
                        for (i = ch1; i < ch-1; i+=2)
769
86
                        {
770
86
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
771
86
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
772
86
                        }
773
14
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
774
14
                    }
775
59
                } else {
776
59
                    uint8_t ch1 = (ch)/2;
777
59
                    hInfo->num_front_channels = ch1;
778
59
                    hInfo->num_back_channels = ch1;
779
59
                    if (ch1 & 1)
780
19
                    {
781
19
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
782
101
                        for (i = 1; i <= ch1; i+=2)
783
82
                        {
784
82
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
785
82
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
786
82
                        }
787
82
                        for (i = ch1+1; i < ch-1; i+=2)
788
63
                        {
789
63
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
790
63
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
791
63
                        }
792
19
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
793
40
                    } else {
794
338
                        for (i = 0; i < ch1; i+=2)
795
298
                        {
796
298
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
797
298
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
798
298
                        }
799
338
                        for (i = ch1; i < ch; i+=2)
800
298
                        {
801
298
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
802
298
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
803
298
                        }
804
40
                    }
805
59
                }
806
106
                hInfo->num_lfe_channels = hDecoder->has_lfe;
807
240
                for (i = ch; i < hDecoder->fr_channels; i++)
808
134
                {
809
134
                    hInfo->channel_position[i] = LFE_CHANNEL;
810
134
                }
811
106
            }
812
106
            break;
813
2.62k
        }
814
2.62k
    }
815
2.71k
}
816
817
void* NeAACDecDecode(NeAACDecHandle hpDecoder,
818
                                 NeAACDecFrameInfo *hInfo,
819
                                 unsigned char *buffer,
820
                                 unsigned long buffer_size)
821
14.9k
{
822
14.9k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
823
14.9k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
824
14.9k
}
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
3.43k
{
833
3.43k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
834
3.43k
    if ((sample_buffer_size == 0) || (sample_buffer == NULL) || (*sample_buffer == NULL))
835
25
    {
836
25
        hInfo->error = 27;
837
25
        return NULL;
838
25
    }
839
840
3.40k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
841
3.40k
        sample_buffer, sample_buffer_size);
842
3.43k
}
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
18.3k
{
851
18.3k
    uint16_t i;
852
18.3k
    uint8_t channels = 0;
853
18.3k
    uint8_t output_channels = 0;
854
18.3k
    bitfile ld;
855
18.3k
    uint32_t bitsconsumed;
856
18.3k
    uint16_t frame_len;
857
18.3k
    void *sample_buffer;
858
#if 0
859
    uint32_t startbit=0, endbit=0, payload_bits=0;
860
#endif
861
18.3k
    uint32_t required_buffer_size=0;
862
863
#ifdef PROFILE
864
    int64_t count = faad_get_ts();
865
#endif
866
867
    /* safety checks */
868
18.3k
    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
18.3k
    frame_len = hDecoder->frameLength;
878
879
880
18.3k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
881
18.3k
    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
18.3k
    if (buffer_size >= 128)
899
1.73k
    {
900
1.73k
        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.73k
    }
908
909
910
    /* initialize the bitstream */
911
18.3k
    faad_initbits(&ld, buffer, buffer_size);
912
18.3k
    if (ld.error != 0)
913
7.51k
        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
10.8k
    if (hDecoder->adts_header_present)
962
515
    {
963
515
        adts_header adts;
964
965
515
        adts.old_format = hDecoder->config.useOldADTSFormat;
966
515
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
967
61
            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
515
    }
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
10.7k
        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
10.7k
    channels = hDecoder->fr_channels;
1005
1006
10.7k
    if (hInfo->error > 0)
1007
7.54k
        goto error;
1008
1009
    /* safety check */
1010
3.20k
    if (channels == 0 || channels > MAX_CHANNELS)
1011
59
    {
1012
        /* invalid number of channels */
1013
59
        hInfo->error = 12;
1014
59
        goto error;
1015
59
    }
1016
1017
    /* no more bit reading after this */
1018
3.14k
    bitsconsumed = faad_get_processed_bits(&ld);
1019
3.14k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1020
3.14k
    if (ld.error)
1021
0
    {
1022
0
        hInfo->error = 14;
1023
0
        goto error;
1024
0
    }
1025
3.14k
    faad_endbits(&ld);
1026
1027
1028
3.14k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1029
#if 0
1030
        && !hDecoder->latm_header_present
1031
#endif
1032
3.14k
        )
1033
3.02k
    {
1034
3.02k
        if (hDecoder->channelConfiguration == 0)
1035
74
            hDecoder->channelConfiguration = channels;
1036
1037
3.02k
        if (channels == 8) /* 7.1 */
1038
510
            hDecoder->channelConfiguration = 7;
1039
3.02k
        if (channels == 7) /* not a standard channelConfiguration */
1040
22
            hDecoder->channelConfiguration = 0;
1041
3.02k
    }
1042
1043
3.14k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1044
418
    {
1045
418
        hDecoder->downMatrix = 1;
1046
418
        output_channels = 2;
1047
2.72k
    } else {
1048
2.72k
        output_channels = channels;
1049
2.72k
    }
1050
1051
3.14k
#if (defined(PS_DEC) || defined(DRM_PS))
1052
3.14k
    hDecoder->upMatrix = 0;
1053
3.14k
#endif
1054
1055
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1056
3.14k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1057
93
    {
1058
        /* In some codepath program_config_element result is ignored. */
1059
93
        if (hDecoder->pce.channels > MAX_CHANNELS)
1060
3
        {
1061
3
            hInfo->error = 22;
1062
3
            return NULL;
1063
3
        }
1064
93
    }
1065
3.13k
    create_channel_config(hDecoder, hInfo);
1066
1067
    /* number of samples in this frame */
1068
3.13k
    hInfo->samples = frame_len*output_channels;
1069
    /* number of channels in this frame */
1070
3.13k
    hInfo->channels = output_channels;
1071
    /* samplerate */
1072
3.13k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1073
    /* object type */
1074
3.13k
    hInfo->object_type = hDecoder->object_type;
1075
    /* sbr */
1076
3.13k
    hInfo->sbr = NO_SBR;
1077
    /* header type */
1078
3.13k
    hInfo->header_type = RAW;
1079
3.13k
    if (hDecoder->adif_header_present)
1080
0
        hInfo->header_type = ADIF;
1081
3.13k
    if (hDecoder->adts_header_present)
1082
115
        hInfo->header_type = ADTS;
1083
#if 0
1084
    if (hDecoder->latm_header_present)
1085
        hInfo->header_type = LATM;
1086
#endif
1087
3.13k
#if (defined(PS_DEC) || defined(DRM_PS))
1088
3.13k
    hInfo->ps = hDecoder->ps_used_global;
1089
3.13k
#endif
1090
1091
    /* check if frame has channel elements */
1092
3.13k
    if (channels == 0)
1093
0
    {
1094
0
        hDecoder->frame++;
1095
0
        return NULL;
1096
0
    }
1097
1098
3.13k
    {
1099
3.13k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1100
3.13k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1101
3.13k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1102
3.13k
        };
1103
3.13k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1104
3.13k
#ifdef SBR_DEC
1105
3.13k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1106
1.80k
        {
1107
1.80k
            stride = 2 * stride;
1108
1.80k
        }
1109
3.13k
#endif
1110
3.13k
        required_buffer_size = frame_len*output_channels*stride;
1111
3.13k
    }
1112
1113
    /* check if we want to use internal sample_buffer */
1114
3.13k
    if (sample_buffer_size == 0)
1115
2.36k
    {
1116
        /* allocate the buffer for the final samples */
1117
2.36k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1118
2.05k
        {
1119
2.05k
            if (hDecoder->sample_buffer)
1120
32
                faad_free(hDecoder->sample_buffer);
1121
2.05k
            hDecoder->sample_buffer = NULL;
1122
2.05k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1123
2.05k
            hDecoder->sample_buffer_size = required_buffer_size;
1124
2.05k
        }
1125
2.36k
    } else if (sample_buffer_size < required_buffer_size) {
1126
        /* provided sample buffer is not big enough */
1127
421
        hInfo->error = 27;
1128
421
        return NULL;
1129
421
    }
1130
1131
2.71k
    if (sample_buffer_size == 0)
1132
2.36k
    {
1133
2.36k
        sample_buffer = hDecoder->sample_buffer;
1134
2.36k
    } else {
1135
357
        sample_buffer = *sample_buffer2;
1136
357
    }
1137
1138
2.71k
#ifdef SBR_DEC
1139
2.71k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1140
1.71k
    {
1141
1.71k
        uint8_t ele;
1142
1143
        /* this data is different when SBR is used or when the data is upsampled */
1144
1.71k
        if (!hDecoder->downSampledSBR)
1145
1.48k
        {
1146
1.48k
            frame_len *= 2;
1147
1.48k
            hInfo->samples *= 2;
1148
1.48k
            hInfo->samplerate *= 2;
1149
1.48k
        }
1150
1151
        /* check if every element was provided with SBR data */
1152
10.0k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1153
8.33k
        {
1154
8.33k
            if (hDecoder->sbr[ele] == NULL)
1155
3
            {
1156
3
                hInfo->error = 25;
1157
3
                goto error;
1158
3
            }
1159
8.33k
        }
1160
1161
        /* sbr */
1162
1.71k
        if (hDecoder->sbr_present_flag == 1)
1163
787
        {
1164
787
            hInfo->object_type = HE_AAC;
1165
787
            hInfo->sbr = SBR_UPSAMPLED;
1166
929
        } else {
1167
929
            hInfo->sbr = NO_SBR_UPSAMPLED;
1168
929
        }
1169
1.71k
        if (hDecoder->downSampledSBR)
1170
230
        {
1171
230
            hInfo->sbr = SBR_DOWNSAMPLED;
1172
230
        }
1173
1.71k
    }
1174
2.71k
#endif
1175
1176
1177
2.71k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1178
2.71k
        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
2.71k
    hDecoder->postSeekResetFlag = 0;
1187
1188
2.71k
    hDecoder->frame++;
1189
2.71k
#ifdef LD_DEC
1190
2.71k
    if (hDecoder->object_type != LD)
1191
2.23k
    {
1192
2.23k
#endif
1193
2.23k
        if (hDecoder->frame <= 1)
1194
817
            hInfo->samples = 0;
1195
2.23k
#ifdef LD_DEC
1196
2.23k
    } else {
1197
        /* LD encoders will give lower delay */
1198
484
        if (hDecoder->frame <= 0)
1199
0
            hInfo->samples = 0;
1200
484
    }
1201
2.71k
#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.71k
    return sample_buffer;
1214
1215
7.67k
error:
1216
1217
    /* reset filterbank state */
1218
498k
    for (i = 0; i < MAX_CHANNELS; i++)
1219
491k
    {
1220
491k
        if (hDecoder->fb_intermed[i] != NULL)
1221
277k
        {
1222
277k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1223
277k
        }
1224
491k
    }
1225
7.67k
#ifdef SBR_DEC
1226
375k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1227
368k
    {
1228
368k
        if (hDecoder->sbr[i] != NULL)
1229
188k
        {
1230
188k
            sbrReset(hDecoder->sbr[i]);
1231
188k
        }
1232
368k
    }
1233
7.67k
#endif
1234
1235
1236
7.67k
    faad_endbits(&ld);
1237
1238
    /* cleanup */
1239
#ifdef ANALYSIS
1240
    fflush(stdout);
1241
#endif
1242
1243
    return NULL;
1244
2.71k
}