~ubuntu-branches/debian/wheezy/vlc/wheezy

« back to all changes in this revision

Viewing changes to extras/faad2/libfaad/decoder.c

Tags: upstream-0.7.2.final
ImportĀ upstreamĀ versionĀ 0.7.2.final

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
 
3
** Copyright (C) 2003-2004 M. Bakker, Ahead Software 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
** Commercial non-GPL licensing of this software is possible.
 
23
** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 
24
**
 
25
** $Id: decoder.c,v 1.94 2004/02/04 20:07:24 menno Exp $
 
26
**/
 
27
 
 
28
#include "common.h"
 
29
#include "structs.h"
 
30
 
 
31
#include <stdlib.h>
 
32
#include <string.h>
 
33
 
 
34
#include "decoder.h"
 
35
#include "mp4.h"
 
36
#include "syntax.h"
 
37
#include "error.h"
 
38
#include "output.h"
 
39
#include "filtbank.h"
 
40
#include "drc.h"
 
41
#ifdef SBR_DEC
 
42
#include "sbr_dec.h"
 
43
#include "sbr_syntax.h"
 
44
#endif
 
45
#ifdef SSR_DEC
 
46
#include "ssr.h"
 
47
#endif
 
48
 
 
49
#ifdef ANALYSIS
 
50
uint16_t dbg_count;
 
51
#endif
 
52
 
 
53
int8_t* FAADAPI faacDecGetErrorMessage(uint8_t errcode)
 
54
{
 
55
    if (errcode >= NUM_ERROR_MESSAGES)
 
56
        return NULL;
 
57
    return err_msg[errcode];
 
58
}
 
59
 
 
60
uint32_t FAADAPI faacDecGetCapabilities(void)
 
61
{
 
62
    uint32_t cap = 0;
 
63
 
 
64
    /* can't do without it */
 
65
    cap += LC_DEC_CAP;
 
66
 
 
67
#ifdef MAIN_DEC
 
68
    cap += MAIN_DEC_CAP;
 
69
#endif
 
70
#ifdef LTP_DEC
 
71
    cap += LTP_DEC_CAP;
 
72
#endif
 
73
#ifdef LD_DEC
 
74
    cap += LD_DEC_CAP;
 
75
#endif
 
76
#ifdef ERROR_RESILIENCE
 
77
    cap += ERROR_RESILIENCE_CAP;
 
78
#endif
 
79
#ifdef FIXED_POINT
 
80
    cap += FIXED_POINT_CAP;
 
81
#endif
 
82
 
 
83
    return cap;
 
84
}
 
85
 
 
86
faacDecHandle FAADAPI faacDecOpen(void)
 
87
{
 
88
    uint8_t i;
 
89
    faacDecHandle hDecoder = NULL;
 
90
 
 
91
    if ((hDecoder = (faacDecHandle)faad_malloc(sizeof(faacDecStruct))) == NULL)
 
92
        return NULL;
 
93
 
 
94
    memset(hDecoder, 0, sizeof(faacDecStruct));
 
95
 
 
96
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
 
97
    hDecoder->config.defObjectType = MAIN;
 
98
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
 
99
    hDecoder->config.downMatrix = 0;
 
100
    hDecoder->adts_header_present = 0;
 
101
    hDecoder->adif_header_present = 0;
 
102
#ifdef ERROR_RESILIENCE
 
103
    hDecoder->aacSectionDataResilienceFlag = 0;
 
104
    hDecoder->aacScalefactorDataResilienceFlag = 0;
 
105
    hDecoder->aacSpectralDataResilienceFlag = 0;
 
106
#endif
 
107
    hDecoder->frameLength = 1024;
 
108
 
 
109
    hDecoder->frame = 0;
 
110
    hDecoder->sample_buffer = NULL;
 
111
 
 
112
    for (i = 0; i < MAX_CHANNELS; i++)
 
113
    {
 
114
        hDecoder->window_shape_prev[i] = 0;
 
115
        hDecoder->time_out[i] = NULL;
 
116
        hDecoder->fb_intermed[i] = NULL;
 
117
#ifdef SSR_DEC
 
118
        hDecoder->ssr_overlap[i] = NULL;
 
119
        hDecoder->prev_fmd[i] = NULL;
 
120
#endif
 
121
#ifdef MAIN_DEC
 
122
        hDecoder->pred_stat[i] = NULL;
 
123
#endif
 
124
#ifdef LTP_DEC
 
125
        hDecoder->ltp_lag[i] = 0;
 
126
        hDecoder->lt_pred_stat[i] = NULL;
 
127
#endif
 
128
    }
 
129
 
 
130
#ifdef SBR_DEC
 
131
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
 
132
    {
 
133
        hDecoder->sbr[i] = NULL;
 
134
    }
 
135
#endif
 
136
 
 
137
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
 
138
 
 
139
#ifdef USE_SSE
 
140
    if (cpu_has_sse())
 
141
    {
 
142
        hDecoder->apply_sf_func = apply_scalefactors_sse;
 
143
    } else {
 
144
        hDecoder->apply_sf_func = apply_scalefactors;
 
145
    }
 
146
#endif
 
147
 
 
148
    return hDecoder;
 
149
}
 
150
 
 
151
faacDecConfigurationPtr FAADAPI faacDecGetCurrentConfiguration(faacDecHandle hDecoder)
 
152
{
 
153
    if (hDecoder)
 
154
    {
 
155
        faacDecConfigurationPtr config = &(hDecoder->config);
 
156
 
 
157
        return config;
 
158
    }
 
159
 
 
160
    return NULL;
 
161
}
 
162
 
 
163
uint8_t FAADAPI faacDecSetConfiguration(faacDecHandle hDecoder,
 
164
                                    faacDecConfigurationPtr config)
 
165
{
 
166
    if (hDecoder && config)
 
167
    {
 
168
        /* check if we can decode this object type */
 
169
        if (can_decode_ot(config->defObjectType) < 0)
 
170
            return 0;
 
171
        hDecoder->config.defObjectType = config->defObjectType;
 
172
 
 
173
        /* samplerate: anything but 0 should be possible */
 
174
        if (config->defSampleRate == 0)
 
175
            return 0;
 
176
        hDecoder->config.defSampleRate = config->defSampleRate;
 
177
 
 
178
        /* check output format */
 
179
        if ((config->outputFormat < 1) || (config->outputFormat > 9))
 
180
            return 0;
 
181
        hDecoder->config.outputFormat = config->outputFormat;
 
182
 
 
183
        if (config->downMatrix > 1)
 
184
            hDecoder->config.downMatrix = config->downMatrix;
 
185
 
 
186
        /* OK */
 
187
        return 1;
 
188
    }
 
189
 
 
190
    return 0;
 
191
}
 
192
 
 
193
int32_t FAADAPI faacDecInit(faacDecHandle hDecoder, uint8_t *buffer,
 
194
                            uint32_t buffer_size,
 
195
                            uint32_t *samplerate, uint8_t *channels)
 
196
{
 
197
    uint32_t bits = 0;
 
198
    bitfile ld;
 
199
    adif_header adif;
 
200
    adts_header adts;
 
201
 
 
202
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL))
 
203
        return -1;
 
204
 
 
205
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
 
206
    hDecoder->object_type = hDecoder->config.defObjectType;
 
207
    *samplerate = get_sample_rate(hDecoder->sf_index);
 
208
    *channels = 1;
 
209
 
 
210
    if (buffer != NULL)
 
211
    {
 
212
        faad_initbits(&ld, buffer, buffer_size);
 
213
 
 
214
        /* Check if an ADIF header is present */
 
215
        if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
 
216
            (buffer[2] == 'I') && (buffer[3] == 'F'))
 
217
        {
 
218
            hDecoder->adif_header_present = 1;
 
219
 
 
220
            get_adif_header(&adif, &ld);
 
221
            faad_byte_align(&ld);
 
222
 
 
223
            hDecoder->sf_index = adif.pce[0].sf_index;
 
224
            hDecoder->object_type = adif.pce[0].object_type + 1;
 
225
 
 
226
            *samplerate = get_sample_rate(hDecoder->sf_index);
 
227
            *channels = adif.pce[0].channels;
 
228
 
 
229
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
 
230
            hDecoder->pce_set = 1;
 
231
 
 
232
            bits = bit2byte(faad_get_processed_bits(&ld));
 
233
 
 
234
        /* Check if an ADTS header is present */
 
235
        } else if (faad_showbits(&ld, 12) == 0xfff) {
 
236
            hDecoder->adts_header_present = 1;
 
237
 
 
238
            adts.old_format = hDecoder->config.useOldADTSFormat;
 
239
            adts_frame(&adts, &ld);
 
240
 
 
241
            hDecoder->sf_index = adts.sf_index;
 
242
            hDecoder->object_type = adts.profile + 1;
 
243
 
 
244
            *samplerate = get_sample_rate(hDecoder->sf_index);
 
245
            *channels = (adts.channel_configuration > 6) ?
 
246
                2 : adts.channel_configuration;
 
247
        }
 
248
 
 
249
        if (ld.error)
 
250
        {
 
251
            faad_endbits(&ld);
 
252
            return -1;
 
253
        }
 
254
        faad_endbits(&ld);
 
255
    }
 
256
    hDecoder->channelConfiguration = *channels;
 
257
 
 
258
#ifdef SBR_DEC
 
259
    /* implicit signalling */
 
260
    if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR))
 
261
    {
 
262
        *samplerate *= 2;
 
263
        hDecoder->forceUpSampling = 1;
 
264
    }
 
265
#endif
 
266
 
 
267
    /* must be done before frameLength is divided by 2 for LD */
 
268
#ifdef SSR_DEC
 
269
    if (hDecoder->object_type == SSR)
 
270
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
 
271
    else
 
272
#endif
 
273
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
 
274
 
 
275
#ifdef LD_DEC
 
276
    if (hDecoder->object_type == LD)
 
277
        hDecoder->frameLength >>= 1;
 
278
#endif
 
279
 
 
280
    if (can_decode_ot(hDecoder->object_type) < 0)
 
281
        return -1;
 
282
 
 
283
    return bits;
 
284
}
 
285
 
 
286
/* Init the library using a DecoderSpecificInfo */
 
287
int8_t FAADAPI faacDecInit2(faacDecHandle hDecoder, uint8_t *pBuffer,
 
288
                            uint32_t SizeOfDecoderSpecificInfo,
 
289
                            uint32_t *samplerate, uint8_t *channels)
 
290
{
 
291
    int8_t rc;
 
292
    mp4AudioSpecificConfig mp4ASC;
 
293
 
 
294
    if((hDecoder == NULL)
 
295
        || (pBuffer == NULL)
 
296
        || (SizeOfDecoderSpecificInfo < 2)
 
297
        || (samplerate == NULL)
 
298
        || (channels == NULL))
 
299
    {
 
300
        return -1;
 
301
    }
 
302
 
 
303
    hDecoder->adif_header_present = 0;
 
304
    hDecoder->adts_header_present = 0;
 
305
 
 
306
    /* decode the audio specific config */
 
307
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
 
308
        &(hDecoder->pce));
 
309
 
 
310
    /* copy the relevant info to the decoder handle */
 
311
    *samplerate = mp4ASC.samplingFrequency;
 
312
    if (mp4ASC.channelsConfiguration)
 
313
    {
 
314
        *channels = mp4ASC.channelsConfiguration;
 
315
    } else {
 
316
        *channels = hDecoder->pce.channels;
 
317
        hDecoder->pce_set = 1;
 
318
    }
 
319
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
 
320
    hDecoder->object_type = mp4ASC.objectTypeIndex;
 
321
#ifdef ERROR_RESILIENCE
 
322
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
 
323
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
 
324
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
 
325
#endif
 
326
#ifdef SBR_DEC
 
327
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
 
328
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
 
329
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
 
330
    else
 
331
        hDecoder->forceUpSampling = 0;
 
332
 
 
333
    /* AAC core decoder samplerate is 2 times as low */
 
334
    if (hDecoder->sbr_present_flag == 1 || hDecoder->forceUpSampling == 1)
 
335
    {
 
336
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
 
337
    }
 
338
#endif
 
339
 
 
340
    if (rc != 0)
 
341
    {
 
342
        return rc;
 
343
    }
 
344
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
 
345
    if (mp4ASC.frameLengthFlag)
 
346
#ifdef ALLOW_SMALL_FRAMELENGTH
 
347
        hDecoder->frameLength = 960;
 
348
#else
 
349
        return -1;
 
350
#endif
 
351
 
 
352
    /* must be done before frameLength is divided by 2 for LD */
 
353
#ifdef SSR_DEC
 
354
    if (hDecoder->object_type == SSR)
 
355
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
 
356
    else
 
357
#endif
 
358
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
 
359
 
 
360
#ifdef LD_DEC
 
361
    if (hDecoder->object_type == LD)
 
362
        hDecoder->frameLength >>= 1;
 
363
#endif
 
364
 
 
365
    return 0;
 
366
}
 
367
 
 
368
#ifdef DRM
 
369
int8_t FAADAPI faacDecInitDRM(faacDecHandle hDecoder, uint32_t samplerate,
 
370
                              uint8_t channels)
 
371
{
 
372
    uint8_t i;
 
373
 
 
374
    if (hDecoder == NULL)
 
375
        return 1; /* error */
 
376
 
 
377
    /* Special object type defined for DRM */
 
378
    hDecoder->config.defObjectType = DRM_ER_LC;
 
379
 
 
380
    hDecoder->config.defSampleRate = samplerate;
 
381
#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
 
382
    hDecoder->aacSectionDataResilienceFlag = 1; /* VCB11 */
 
383
    hDecoder->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
 
384
    hDecoder->aacSpectralDataResilienceFlag = 1; /* HCR */
 
385
#endif
 
386
    hDecoder->frameLength = 960;
 
387
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
 
388
    hDecoder->object_type = hDecoder->config.defObjectType;
 
389
 
 
390
    if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
 
391
        hDecoder->channelConfiguration = 2;
 
392
    else
 
393
        hDecoder->channelConfiguration = 1;
 
394
 
 
395
#ifdef SBR_DEC
 
396
    if (channels == DRMCH_SBR_LC_STEREO)
 
397
        hDecoder->lcstereo_flag = 1;
 
398
    else
 
399
        hDecoder->lcstereo_flag = 0;
 
400
 
 
401
    if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
 
402
        hDecoder->sbr_present_flag = 0;
 
403
    else
 
404
        hDecoder->sbr_present_flag = 1;
 
405
 
 
406
    /* Reset sbr for new initialization */
 
407
    sbrDecodeEnd(hDecoder->sbr[0]);
 
408
    hDecoder->sbr[0] = NULL;
 
409
#endif
 
410
 
 
411
    if (hDecoder->fb) filter_bank_end(hDecoder->fb);
 
412
    hDecoder->fb = NULL;
 
413
 
 
414
    /* Take care of buffers */
 
415
    if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
 
416
    hDecoder->sample_buffer = NULL;
 
417
    hDecoder->alloced_channels = 0;
 
418
 
 
419
    for (i = 0; i < MAX_CHANNELS; i++)
 
420
    {
 
421
        hDecoder->window_shape_prev[i] = 0;
 
422
 
 
423
        if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
 
424
        hDecoder->time_out[i] = NULL;
 
425
        if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]);
 
426
        hDecoder->fb_intermed[i] = NULL;
 
427
#ifdef SSR_DEC
 
428
        if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]);
 
429
        hDecoder->ssr_overlap[i] = NULL;
 
430
        if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]);
 
431
        hDecoder->prev_fmd[i] = NULL;
 
432
#endif
 
433
#ifdef MAIN_DEC
 
434
        if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
 
435
        hDecoder->pred_stat[i] = NULL;
 
436
#endif
 
437
#ifdef LTP_DEC
 
438
        hDecoder->ltp_lag[i] = 0;
 
439
        if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
 
440
        hDecoder->lt_pred_stat[i] = NULL;
 
441
#endif
 
442
    }
 
443
 
 
444
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
 
445
    {
 
446
#ifdef SBR_DEC
 
447
        if (hDecoder->sbr[i])
 
448
            sbrDecodeEnd(hDecoder->sbr[i]);
 
449
 
 
450
        hDecoder->sbr_alloced[i] = 0;
 
451
#endif
 
452
        hDecoder->element_alloced[i] = 0;
 
453
        hDecoder->element_output_channels[i] = 0;
 
454
    }
 
455
 
 
456
    hDecoder->fb = filter_bank_init(hDecoder->frameLength);
 
457
 
 
458
    return 0;
 
459
}
 
460
#endif
 
461
 
 
462
void FAADAPI faacDecClose(faacDecHandle hDecoder)
 
463
{
 
464
    uint8_t i;
 
465
 
 
466
    if (hDecoder == NULL)
 
467
        return;
 
468
 
 
469
#ifdef PROFILE
 
470
    printf("AAC decoder total:  %I64d cycles\n", hDecoder->cycles);
 
471
    printf("requant:            %I64d cycles\n", hDecoder->requant_cycles);
 
472
    printf("spectral_data:      %I64d cycles\n", hDecoder->spectral_cycles);
 
473
    printf("scalefactors:       %I64d cycles\n", hDecoder->scalefac_cycles);
 
474
    printf("output:             %I64d cycles\n", hDecoder->output_cycles);
 
475
#endif
 
476
 
 
477
    for (i = 0; i < MAX_CHANNELS; i++)
 
478
    {
 
479
        if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
 
480
        if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]);
 
481
#ifdef SSR_DEC
 
482
        if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]);
 
483
        if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]);
 
484
#endif
 
485
#ifdef MAIN_DEC
 
486
        if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
 
487
#endif
 
488
#ifdef LTP_DEC
 
489
        if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
 
490
#endif
 
491
    }
 
492
 
 
493
#ifdef SSR_DEC
 
494
    if (hDecoder->object_type == SSR)
 
495
        ssr_filter_bank_end(hDecoder->fb);
 
496
    else
 
497
#endif
 
498
        filter_bank_end(hDecoder->fb);
 
499
 
 
500
    drc_end(hDecoder->drc);
 
501
 
 
502
    if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
 
503
 
 
504
#ifdef SBR_DEC
 
505
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
 
506
    {
 
507
        if (hDecoder->sbr[i])
 
508
            sbrDecodeEnd(hDecoder->sbr[i]);
 
509
    }
 
510
#endif
 
511
 
 
512
    if (hDecoder) faad_free(hDecoder);
 
513
}
 
514
 
 
515
void FAADAPI faacDecPostSeekReset(faacDecHandle hDecoder, int32_t frame)
 
516
{
 
517
    if (hDecoder)
 
518
    {
 
519
        hDecoder->postSeekResetFlag = 1;
 
520
 
 
521
        if (frame != -1)
 
522
            hDecoder->frame = frame;
 
523
    }
 
524
}
 
525
 
 
526
static void create_channel_config(faacDecHandle hDecoder, faacDecFrameInfo *hInfo)
 
527
{
 
528
    hInfo->num_front_channels = 0;
 
529
    hInfo->num_side_channels = 0;
 
530
    hInfo->num_back_channels = 0;
 
531
    hInfo->num_lfe_channels = 0;
 
532
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
 
533
 
 
534
    if (hDecoder->downMatrix)
 
535
    {
 
536
        hInfo->num_front_channels = 2;
 
537
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
 
538
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
 
539
        return;
 
540
    }
 
541
 
 
542
    /* check if there is a PCE */
 
543
    if (hDecoder->pce_set)
 
544
    {
 
545
        uint8_t i, chpos = 0;
 
546
        uint8_t chdir, back_center = 0;
 
547
 
 
548
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
 
549
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
 
550
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
 
551
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
 
552
 
 
553
        chdir = hInfo->num_front_channels;
 
554
        if (chdir & 1)
 
555
        {
 
556
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
 
557
            chdir--;
 
558
        }
 
559
        for (i = 0; i < chdir; i += 2)
 
560
        {
 
561
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
 
562
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
 
563
        }
 
564
 
 
565
        for (i = 0; i < hInfo->num_side_channels; i += 2)
 
566
        {
 
567
            hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT;
 
568
            hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT;
 
569
        }
 
570
 
 
571
        chdir = hInfo->num_back_channels;
 
572
        if (chdir & 1)
 
573
        {
 
574
            back_center = 1;
 
575
            chdir--;
 
576
        }
 
577
        for (i = 0; i < chdir; i += 2)
 
578
        {
 
579
            hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT;
 
580
            hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT;
 
581
        }
 
582
        if (back_center)
 
583
        {
 
584
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
 
585
        }
 
586
 
 
587
        for (i = 0; i < hInfo->num_lfe_channels; i++)
 
588
        {
 
589
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
 
590
        }
 
591
 
 
592
    } else {
 
593
        switch (hDecoder->channelConfiguration)
 
594
        {
 
595
        case 1:
 
596
            hInfo->num_front_channels = 1;
 
597
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
 
598
            break;
 
599
        case 2:
 
600
            hInfo->num_front_channels = 2;
 
601
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
 
602
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
 
603
            break;
 
604
        case 3:
 
605
            hInfo->num_front_channels = 3;
 
606
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
 
607
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
 
608
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
 
609
            break;
 
610
        case 4:
 
611
            hInfo->num_front_channels = 3;
 
612
            hInfo->num_back_channels = 1;
 
613
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
 
614
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
 
615
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
 
616
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
 
617
            break;
 
618
        case 5:
 
619
            hInfo->num_front_channels = 3;
 
620
            hInfo->num_back_channels = 2;
 
621
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
 
622
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
 
623
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
 
624
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
 
625
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
 
626
            break;
 
627
        case 6:
 
628
            hInfo->num_front_channels = 3;
 
629
            hInfo->num_back_channels = 2;
 
630
            hInfo->num_lfe_channels = 1;
 
631
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
 
632
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
 
633
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
 
634
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
 
635
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
 
636
            hInfo->channel_position[5] = LFE_CHANNEL;
 
637
            break;
 
638
        case 7:
 
639
            hInfo->num_front_channels = 3;
 
640
            hInfo->num_side_channels = 2;
 
641
            hInfo->num_back_channels = 2;
 
642
            hInfo->num_lfe_channels = 1;
 
643
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
 
644
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
 
645
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
 
646
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
 
647
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
 
648
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
 
649
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
 
650
            hInfo->channel_position[7] = LFE_CHANNEL;
 
651
            break;
 
652
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
 
653
            {
 
654
                uint8_t i;
 
655
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
 
656
                if (ch & 1) /* there's either a center front or a center back channel */
 
657
                {
 
658
                    uint8_t ch1 = (ch-1)/2;
 
659
                    if (hDecoder->first_syn_ele == ID_SCE)
 
660
                    {
 
661
                        hInfo->num_front_channels = ch1 + 1;
 
662
                        hInfo->num_back_channels = ch1;
 
663
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
 
664
                        for (i = 1; i <= ch1; i+=2)
 
665
                        {
 
666
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
 
667
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
 
668
                        }
 
669
                        for (i = ch1+1; i < ch; i+=2)
 
670
                        {
 
671
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
 
672
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
 
673
                        }
 
674
                    } else {
 
675
                        hInfo->num_front_channels = ch1;
 
676
                        hInfo->num_back_channels = ch1 + 1;
 
677
                        for (i = 0; i < ch1; i+=2)
 
678
                        {
 
679
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
 
680
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
 
681
                        }
 
682
                        for (i = ch1; i < ch-1; i+=2)
 
683
                        {
 
684
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
 
685
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
 
686
                        }
 
687
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
 
688
                    }
 
689
                } else {
 
690
                    uint8_t ch1 = (ch)/2;
 
691
                    hInfo->num_front_channels = ch1;
 
692
                    hInfo->num_back_channels = ch1;
 
693
                    if (ch1 & 1)
 
694
                    {
 
695
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
 
696
                        for (i = 1; i <= ch1; i+=2)
 
697
                        {
 
698
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
 
699
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
 
700
                        }
 
701
                        for (i = ch1+1; i < ch-1; i+=2)
 
702
                        {
 
703
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
 
704
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
 
705
                        }
 
706
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
 
707
                    } else {
 
708
                        for (i = 0; i < ch1; i+=2)
 
709
                        {
 
710
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
 
711
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
 
712
                        }
 
713
                        for (i = ch1; i < ch; i+=2)
 
714
                        {
 
715
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
 
716
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
 
717
                        }
 
718
                    }
 
719
                }
 
720
                hInfo->num_lfe_channels = hDecoder->has_lfe;
 
721
                for (i = ch; i < hDecoder->fr_channels; i++)
 
722
                {
 
723
                    hInfo->channel_position[i] = LFE_CHANNEL;
 
724
                }
 
725
            }
 
726
            break;
 
727
        }
 
728
    }
 
729
}
 
730
 
 
731
void* FAADAPI faacDecDecode(faacDecHandle hDecoder,
 
732
                            faacDecFrameInfo *hInfo,
 
733
                            uint8_t *buffer, uint32_t buffer_size)
 
734
{
 
735
    uint8_t channels = 0;
 
736
    uint8_t output_channels = 0;
 
737
    bitfile ld;
 
738
    uint32_t bitsconsumed;
 
739
    uint16_t frame_len;
 
740
    void *sample_buffer;
 
741
 
 
742
#ifdef PROFILE
 
743
    int64_t count = faad_get_ts();
 
744
#endif
 
745
 
 
746
    /* safety checks */
 
747
    if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
 
748
    {
 
749
        return NULL;
 
750
    }
 
751
 
 
752
    frame_len = hDecoder->frameLength;
 
753
 
 
754
 
 
755
    memset(hInfo, 0, sizeof(faacDecFrameInfo));
 
756
    memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
 
757
 
 
758
    /* initialize the bitstream */
 
759
    faad_initbits(&ld, buffer, buffer_size);
 
760
 
 
761
#ifdef DRM
 
762
    if (hDecoder->object_type == DRM_ER_LC)
 
763
    {
 
764
        /* We do not support stereo right now */
 
765
        if (hDecoder->channelConfiguration == 2)
 
766
        {
 
767
            hInfo->error = 8; // Throw CRC error
 
768
            goto error;
 
769
        }
 
770
 
 
771
        faad_getbits(&ld, 8
 
772
            DEBUGVAR(1,1,"faacDecDecode(): skip CRC"));
 
773
    }
 
774
#endif
 
775
 
 
776
    if (hDecoder->adts_header_present)
 
777
    {
 
778
        adts_header adts;
 
779
 
 
780
        adts.old_format = hDecoder->config.useOldADTSFormat;
 
781
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
 
782
            goto error;
 
783
 
 
784
        /* MPEG2 does byte_alignment() here,
 
785
         * but ADTS header is always multiple of 8 bits in MPEG2
 
786
         * so not needed to actually do it.
 
787
         */
 
788
    }
 
789
 
 
790
#ifdef ANALYSIS
 
791
    dbg_count = 0;
 
792
#endif
 
793
 
 
794
    /* decode the complete bitstream */
 
795
#ifdef SCALABLE_DEC
 
796
    if ((hDecoder->object_type == 6) || (hDecoder->object_type == DRM_ER_LC))
 
797
    {
 
798
        aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
 
799
    } else {
 
800
#endif
 
801
        raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
 
802
#ifdef SCALABLE_DEC
 
803
    }
 
804
#endif
 
805
 
 
806
    channels = hDecoder->fr_channels;
 
807
 
 
808
    if (hInfo->error > 0)
 
809
        goto error;
 
810
 
 
811
    /* safety check */
 
812
    if (channels == 0 || channels > MAX_CHANNELS)
 
813
    {
 
814
        /* invalid number of channels */
 
815
        hInfo->error = 12;
 
816
        goto error;
 
817
    }
 
818
 
 
819
    /* no more bit reading after this */
 
820
    bitsconsumed = faad_get_processed_bits(&ld);
 
821
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
 
822
    if (ld.error)
 
823
    {
 
824
        hInfo->error = 14;
 
825
        goto error;
 
826
    }
 
827
    faad_endbits(&ld);
 
828
 
 
829
 
 
830
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present)
 
831
    {
 
832
        if (channels != hDecoder->channelConfiguration)
 
833
            hDecoder->channelConfiguration = channels;
 
834
 
 
835
        if (channels == 8) /* 7.1 */
 
836
            hDecoder->channelConfiguration = 7;
 
837
        if (channels == 7) /* not a standard channelConfiguration */
 
838
            hDecoder->channelConfiguration = 0;
 
839
    }
 
840
 
 
841
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
 
842
    {
 
843
        hDecoder->downMatrix = 1;
 
844
        output_channels = 2;
 
845
    } else {
 
846
        output_channels = channels;
 
847
    }
 
848
 
 
849
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
 
850
    create_channel_config(hDecoder, hInfo);
 
851
 
 
852
    /* number of samples in this frame */
 
853
    hInfo->samples = frame_len*output_channels;
 
854
    /* number of channels in this frame */
 
855
    hInfo->channels = output_channels;
 
856
    /* samplerate */
 
857
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
 
858
    /* object type */
 
859
    hInfo->object_type = hDecoder->object_type;
 
860
    /* sbr */
 
861
    hInfo->sbr = NO_SBR;
 
862
    /* header type */
 
863
    hInfo->header_type = RAW;
 
864
    if (hDecoder->adif_header_present)
 
865
        hInfo->header_type = ADIF;
 
866
    if (hDecoder->adts_header_present)
 
867
        hInfo->header_type = ADTS;
 
868
 
 
869
    /* check if frame has channel elements */
 
870
    if (channels == 0)
 
871
    {
 
872
        hDecoder->frame++;
 
873
        return NULL;
 
874
    }
 
875
 
 
876
    /* allocate the buffer for the final samples */
 
877
    if ((hDecoder->sample_buffer == NULL) ||
 
878
        (hDecoder->alloced_channels != output_channels))
 
879
    {
 
880
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
 
881
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
 
882
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
 
883
        };
 
884
        uint8_t stride = str[hDecoder->config.outputFormat-1];
 
885
#ifdef SBR_DEC
 
886
        if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
 
887
            stride = 2 * stride;
 
888
#endif
 
889
        if (hDecoder->sample_buffer)
 
890
            faad_free(hDecoder->sample_buffer);
 
891
        hDecoder->sample_buffer = NULL;
 
892
        hDecoder->sample_buffer = faad_malloc(frame_len*output_channels*stride);
 
893
        hDecoder->alloced_channels = output_channels;
 
894
    }
 
895
 
 
896
    sample_buffer = hDecoder->sample_buffer;
 
897
 
 
898
#ifdef SBR_DEC
 
899
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
 
900
    {
 
901
        uint8_t ele;
 
902
 
 
903
        /* this data is different when SBR is used or when the data is upsampled */
 
904
        frame_len *= 2;
 
905
        hInfo->samples *= 2;
 
906
        hInfo->samplerate *= 2;
 
907
 
 
908
        /* check if every element was provided with SBR data */
 
909
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
 
910
        {
 
911
            if (hDecoder->sbr[ele] == NULL)
 
912
            {
 
913
                hInfo->error = 25;
 
914
                goto error;
 
915
            }
 
916
        }
 
917
 
 
918
        /* sbr */
 
919
        if (hDecoder->sbr_present_flag == 1)
 
920
        {
 
921
            hInfo->object_type = HE_AAC;
 
922
            hInfo->sbr = SBR_UPSAMPLED;
 
923
        } else {
 
924
            hInfo->sbr = NO_SBR_UPSAMPLED;
 
925
        }
 
926
    }
 
927
#endif
 
928
 
 
929
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
 
930
        output_channels, frame_len, hDecoder->config.outputFormat);
 
931
 
 
932
 
 
933
    hDecoder->postSeekResetFlag = 0;
 
934
 
 
935
    hDecoder->frame++;
 
936
#ifdef LD_DEC
 
937
    if (hDecoder->object_type != LD)
 
938
    {
 
939
#endif
 
940
        if (hDecoder->frame <= 1)
 
941
            hInfo->samples = 0;
 
942
#ifdef LD_DEC
 
943
    } else {
 
944
        /* LD encoders will give lower delay */
 
945
        if (hDecoder->frame <= 0)
 
946
            hInfo->samples = 0;
 
947
    }
 
948
#endif
 
949
 
 
950
    /* cleanup */
 
951
#ifdef ANALYSIS
 
952
    fflush(stdout);
 
953
#endif
 
954
 
 
955
#ifdef PROFILE
 
956
    count = faad_get_ts() - count;
 
957
    hDecoder->cycles += count;
 
958
#endif
 
959
 
 
960
    return sample_buffer;
 
961
 
 
962
error:
 
963
 
 
964
    faad_endbits(&ld);
 
965
 
 
966
    /* cleanup */
 
967
#ifdef ANALYSIS
 
968
    fflush(stdout);
 
969
#endif
 
970
 
 
971
    return NULL;
 
972
}