Coverage Report

Created: 2026-10-28 07:04

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