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