Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3dec.c @ 0de73a46

History | View | Annotate | Download (59.2 KB)

1
/*
2
 * AC-3 Audio Decoder
3
 * This code is developed as part of Google Summer of Code 2006 Program.
4
 *
5
 * Copyright (c) 2006 Kartikey Mahendra BHATT (bhattkm at gmail dot com).
6
 *
7
 * For exponent decoding the code is inspired by the code in liba52 by
8
 * Michel Lespinasse and Aaron Holtzman.
9
 * http://liba52.sourceforge.net
10
 *
11
 * This file is part of FFmpeg.
12
 *
13
 * FFmpeg is free software; you can redistribute it and/or
14
 * modify it under the terms of the GNU General Public
15
 * License as published by the Free Software Foundation; either
16
 * version 2 of the License, or (at your option) any later version.
17
 *
18
 * FFmpeg is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21
 * General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public
24
 * License along with FFmpeg; if not, write to the Free Software
25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26
 */
27

    
28
#include <stdio.h>
29
#include <stddef.h>
30
#include <math.h>
31
#include <string.h>
32

    
33
#define ALT_BITSTREAM_READER
34

    
35
#include "avcodec.h"
36
#include "ac3.h"
37
#include "ac3tab.h"
38
#include "bitstream.h"
39
#include "dsputil.h"
40
#include "random.h"
41

    
42
static uint8_t bndtab[51];
43
static uint8_t masktab[253];
44

    
45
static const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 };
46

    
47
/* table for exponent to scale_factor mapping
48
 * scale_factor[i] = 2 ^ -(i + 15)
49
 */
50
static float scale_factors[25];
51

    
52
static int16_t psdtab[25];
53

    
54
static int8_t exp_1[128];
55
static int8_t exp_2[128];
56
static int8_t exp_3[128];
57

    
58
static int16_t l3_quantizers_1[32];
59
static int16_t l3_quantizers_2[32];
60
static int16_t l3_quantizers_3[32];
61

    
62
static int16_t l5_quantizers_1[128];
63
static int16_t l5_quantizers_2[128];
64
static int16_t l5_quantizers_3[128];
65

    
66
static int16_t l7_quantizers[7];
67

    
68
static int16_t l11_quantizers_1[128];
69
static int16_t l11_quantizers_2[128];
70

    
71
static int16_t l15_quantizers[15];
72

    
73
static const uint8_t qntztab[16] = { 0, 5, 7, 3, 7, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16 };
74

    
75
/* Adjustmens in dB gain */
76
#define LEVEL_MINUS_3DB         0.7071067811865476
77
#define LEVEL_MINUS_4POINT5DB   0.5946035575013605
78
#define LEVEL_MINUS_6DB         0.5000000000000000
79
#define LEVEL_PLUS_3DB          1.4142135623730951
80
#define LEVEL_PLUS_6DB          2.0000000000000000
81
#define LEVEL_ZERO              0.0000000000000000
82

    
83
static const float clevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB,
84
    LEVEL_MINUS_6DB, LEVEL_MINUS_4POINT5DB };
85

    
86
static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB };
87

    
88
#define BLOCK_SIZE    256
89

    
90
/* Output and input configurations. */
91
#define AC3_OUTPUT_UNMODIFIED   0x01
92
#define AC3_OUTPUT_MONO         0x02
93
#define AC3_OUTPUT_STEREO       0x04
94
#define AC3_OUTPUT_DOLBY        0x08
95
#define AC3_OUTPUT_LFEON        0x10
96

    
97
typedef struct {
98
    uint16_t crc1;
99
    uint8_t  fscod;
100

    
101
    uint8_t  acmod;
102
    uint8_t  cmixlev;
103
    uint8_t  surmixlev;
104
    uint8_t  dsurmod;
105

    
106
    uint8_t  blksw;
107
    uint8_t  dithflag;
108
    uint8_t  cplinu;
109
    uint8_t  chincpl;
110
    uint8_t  phsflginu;
111
    uint8_t  cplbegf;
112
    uint8_t  cplendf;
113
    uint8_t  cplcoe;
114
    uint32_t cplbndstrc;
115
    uint8_t  rematstr;
116
    uint8_t  rematflg;
117
    uint8_t  cplexpstr;
118
    uint8_t  lfeexpstr;
119
    uint8_t  chexpstr[5];
120
    uint8_t  sdcycod;
121
    uint8_t  fdcycod;
122
    uint8_t  sgaincod;
123
    uint8_t  dbpbcod;
124
    uint8_t  floorcod;
125
    uint8_t  csnroffst;
126
    uint8_t  cplfsnroffst;
127
    uint8_t  cplfgaincod;
128
    uint8_t  fsnroffst[5];
129
    uint8_t  fgaincod[5];
130
    uint8_t  lfefsnroffst;
131
    uint8_t  lfefgaincod;
132
    uint8_t  cplfleak;
133
    uint8_t  cplsleak;
134
    uint8_t  cpldeltbae;
135
    uint8_t  deltbae[5];
136
    uint8_t  cpldeltnseg;
137
    uint8_t  cpldeltoffst[8];
138
    uint8_t  cpldeltlen[8];
139
    uint8_t  cpldeltba[8];
140
    uint8_t  deltnseg[5];
141
    uint8_t  deltoffst[5][8];
142
    uint8_t  deltlen[5][8];
143
    uint8_t  deltba[5][8];
144

    
145
    /* Derived Attributes. */
146
    int      sampling_rate;
147
    int      bit_rate;
148
    int      frame_size;
149

    
150
    int      nfchans;           //number of channels
151
    int      lfeon;             //lfe channel in use
152

    
153
    float    dynrng;            //dynamic range gain
154
    float    dynrng2;           //dynamic range gain for 1+1 mode
155
    float    chcoeffs[6];       //normalized channel coefficients
156
    float    cplco[5][18];      //coupling coordinates
157
    int      ncplbnd;           //number of coupling bands
158
    int      ncplsubnd;         //number of coupling sub bands
159
    int      cplstrtmant;       //coupling start mantissa
160
    int      cplendmant;        //coupling end mantissa
161
    int      endmant[5];        //channel end mantissas
162
    AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
163

    
164
    uint8_t  dcplexps[256];     //decoded coupling exponents
165
    uint8_t  dexps[5][256];     //decoded fbw channel exponents
166
    uint8_t  dlfeexps[256];     //decoded lfe channel exponents
167
    uint8_t  cplbap[256];       //coupling bit allocation pointers
168
    uint8_t  bap[5][256];       //fbw channel bit allocation pointers
169
    uint8_t  lfebap[256];       //lfe channel bit allocation pointers
170

    
171
    int      blkoutput;         //output configuration for block
172

    
173
    DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][BLOCK_SIZE]);  //transform coefficients
174

    
175
    /* For IMDCT. */
176
    MDCTContext imdct_512;  //for 512 sample imdct transform
177
    MDCTContext imdct_256;  //for 256 sample imdct transform
178
    DSPContext  dsp;        //for optimization
179

    
180
    DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS][BLOCK_SIZE]);    //output after imdct transform and windowing
181
    DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS][BLOCK_SIZE]);     //delay - added to the next block
182
    DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]);               //temporary storage for imdct transform
183
    DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]);          //temporary storage for output before windowing
184
    DECLARE_ALIGNED_16(float, window[BLOCK_SIZE]);                  //window coefficients
185

    
186
    /* Miscellaneous. */
187
    GetBitContext gb;
188
    AVRandomState dith_state;   //for dither generation
189
} AC3DecodeContext;
190

    
191
/*********** BEGIN INIT HELPER FUNCTIONS ***********/
192
/**
193
 * Generate a Kaiser-Bessel Derived Window.
194
 */
195
static void ac3_window_init(float *window)
196
{
197
   int i, j;
198
   double sum = 0.0, bessel, tmp;
199
   double local_window[256];
200
   double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
201

    
202
   for (i = 0; i < 256; i++) {
203
       tmp = i * (256 - i) * alpha2;
204
       bessel = 1.0;
205
       for (j = 100; j > 0; j--) /* defaul to 100 iterations */
206
           bessel = bessel * tmp / (j * j) + 1;
207
       sum += bessel;
208
       local_window[i] = sum;
209
   }
210

    
211
   sum++;
212
   for (i = 0; i < 256; i++)
213
       window[i] = sqrt(local_window[i] / sum);
214
}
215

    
216
/*
217
 * Generate quantizer tables.
218
 */
219
static void generate_quantizers_table(int16_t quantizers[], int level, int length)
220
{
221
    int i;
222

    
223
    for (i = 0; i < length; i++)
224
        quantizers[i] = ((2 * i - level + 1) << 15) / level;
225
}
226

    
227
static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
228
{
229
    int i, j;
230
    int16_t v;
231

    
232
    for (i = 0; i < length1; i++) {
233
        v = ((2 * i - level + 1) << 15) / level;
234
        for (j = 0; j < length2; j++)
235
            quantizers[i * length2 + j] = v;
236
    }
237

    
238
    for (i = length1 * length2; i < size; i++)
239
        quantizers[i] = 0;
240
}
241

    
242
static void generate_quantizers_table_2(int16_t quantizers[], int level, int length1, int length2, int size)
243
{
244
    int i, j;
245
    int16_t v;
246

    
247
    for (i = 0; i < length1; i++) {
248
        v = ((2 * (i % level) - level + 1) << 15) / level;
249
        for (j = 0; j < length2; j++)
250
            quantizers[i * length2 + j] = v;
251
    }
252

    
253
    for (i = length1 * length2; i < size; i++)
254
        quantizers[i] = 0;
255

    
256
}
257

    
258
static void generate_quantizers_table_3(int16_t quantizers[], int level, int length1, int length2, int size)
259
{
260
    int i, j;
261

    
262
    for (i = 0; i < length1; i++)
263
        for (j = 0; j < length2; j++)
264
            quantizers[i * length2 + j] = ((2 * (j % level) - level + 1) << 15) / level;
265

    
266
    for (i = length1 * length2; i < size; i++)
267
        quantizers[i] = 0;
268
}
269

    
270
/*
271
 * Initialize tables at runtime.
272
 */
273
static void ac3_tables_init(void)
274
{
275
    int i, j, k, l, v;
276
    /* compute bndtab and masktab from bandsz */
277
    k = 0;
278
    l = 0;
279
    for(i=0;i<50;i++) {
280
        bndtab[i] = l;
281
        v = ff_ac3_bndsz[i];
282
        for(j=0;j<v;j++) masktab[k++]=i;
283
        l += v;
284
    }
285
    masktab[253] = masktab[254] = masktab[255] = 0;
286
    bndtab[50] = 0;
287

    
288
    /* PSD Table For Mapping Exponents To PSD. */
289
    for (i = 0; i < 25; i++)
290
        psdtab[i] = 3072 - (i << 7);
291

    
292
    /* Exponent Decoding Tables */
293
    for (i = 0; i < 5; i++) {
294
        v = i - 2;
295
        for (j = 0; j < 25; j++)
296
            exp_1[i * 25 + j] = v;
297
    }
298

    
299
    for (i = 0; i < 25; i++) {
300
        v = (i % 5) - 2;
301
        for (j = 0; j < 5; j++)
302
            exp_2[i * 5 + j] = v;
303
    }
304

    
305
    for (i = 0; i < 25; i++) {
306
        v = -2;
307
        for (j = 0; j < 5; j++)
308
            exp_3[i * 5 + j] = v++;
309
    }
310

    
311
    for (i = 125; i < 128; i++)
312
        exp_1[i] = exp_2[i] = exp_3[i] = 25;
313
    /* End Exponent Decoding Tables */
314

    
315
    /* Quantizer ungrouping tables. */
316
    // for level-3 quantizers
317
    generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
318
    generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
319
    generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
320

    
321
    //for level-5 quantizers
322
    generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
323
    generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
324
    generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
325

    
326
    //for level-7 quantizers
327
    generate_quantizers_table(l7_quantizers, 7, 7);
328

    
329
    //for level-4 quantizers
330
    generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
331
    generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
332

    
333
    //for level-15 quantizers
334
    generate_quantizers_table(l15_quantizers, 15, 15);
335
    /* End Quantizer ungrouping tables. */
336

    
337
    //generate scale factors
338
    for (i = 0; i < 25; i++)
339
        scale_factors[i] = pow(2.0, -(i + 15));
340
}
341

    
342

    
343
static int ac3_decode_init(AVCodecContext *avctx)
344
{
345
    AC3DecodeContext *ctx = avctx->priv_data;
346

    
347
    ac3_common_init();
348
    ac3_tables_init();
349
    ff_mdct_init(&ctx->imdct_256, 8, 1);
350
    ff_mdct_init(&ctx->imdct_512, 9, 1);
351
    ac3_window_init(ctx->window);
352
    dsputil_init(&ctx->dsp, avctx);
353
    av_init_random(0, &ctx->dith_state);
354

    
355
    return 0;
356
}
357
/*********** END INIT FUNCTIONS ***********/
358

    
359
/* Synchronize to ac3 bitstream.
360
 * This function searches for the syncword '0xb77'.
361
 *
362
 * @param buf Pointer to "probable" ac3 bitstream buffer
363
 * @param buf_size Size of buffer
364
 * @return Returns the position where syncword is found, -1 if no syncword is found
365
 */
366
static int ac3_synchronize(uint8_t *buf, int buf_size)
367
{
368
    int i;
369

    
370
    for (i = 0; i < buf_size - 1; i++)
371
        if (buf[i] == 0x0b && buf[i + 1] == 0x77)
372
            return i;
373

    
374
    return -1;
375
}
376

    
377
/* Parse the 'sync_info' from the ac3 bitstream.
378
 * This function extracts the sync_info from ac3 bitstream.
379
 * GetBitContext within AC3DecodeContext must point to
380
 * start of the synchronized ac3 bitstream.
381
 *
382
 * @param ctx  AC3DecodeContext
383
 * @return Returns framesize, returns 0 if fscod, frmsizecod or bsid is not valid
384
 */
385
static int ac3_parse_sync_info(AC3DecodeContext *ctx)
386
{
387
    GetBitContext *gb = &ctx->gb;
388
    int frmsizecod, bsid;
389

    
390
    skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
391
    ctx->crc1 = get_bits(gb, 16);
392
    ctx->fscod = get_bits(gb, 2);
393
    if (ctx->fscod == 0x03)
394
        return 0;
395
    frmsizecod = get_bits(gb, 6);
396
    if (frmsizecod >= 38)
397
        return 0;
398
    ctx->sampling_rate = ff_ac3_freqs[ctx->fscod];
399
    ctx->bit_rate = ff_ac3_bitratetab[frmsizecod >> 1];
400

    
401
    /* we include it here in order to determine validity of ac3 frame */
402
    bsid = get_bits(gb, 5);
403
    if (bsid > 0x08)
404
        return 0;
405
    skip_bits(gb, 3); //skip the bsmod, bsi->bsmod = get_bits(gb, 3);
406

    
407
    switch (ctx->fscod) {
408
        case 0x00:
409
            ctx->frame_size = 4 * ctx->bit_rate;
410
            return ctx->frame_size;
411
        case 0x01:
412
            ctx->frame_size = 2 * (320 * ctx->bit_rate / 147 + (frmsizecod & 1));
413
            return ctx->frame_size;
414
        case 0x02:
415
            ctx->frame_size =  6 * ctx->bit_rate;
416
            return ctx->frame_size;
417
    }
418

    
419
    /* never reached */
420
    return 0;
421
}
422

    
423
/* Parse bsi from ac3 bitstream.
424
 * This function extracts the bitstream information (bsi) from ac3 bitstream.
425
 *
426
 * @param ctx AC3DecodeContext after processed by ac3_parse_sync_info
427
 */
428
static void ac3_parse_bsi(AC3DecodeContext *ctx)
429
{
430
    GetBitContext *gb = &ctx->gb;
431
    int i;
432

    
433
    ctx->cmixlev = 0;
434
    ctx->surmixlev = 0;
435
    ctx->dsurmod = 0;
436
    ctx->nfchans = 0;
437
    ctx->cpldeltbae = DBA_NONE;
438
    ctx->cpldeltnseg = 0;
439
    for (i = 0; i < 5; i++) {
440
        ctx->deltbae[i] = DBA_NONE;
441
        ctx->deltnseg[i] = 0;
442
    }
443
    ctx->dynrng = 1.0;
444
    ctx->dynrng2 = 1.0;
445

    
446
    ctx->acmod = get_bits(gb, 3);
447
    ctx->nfchans = nfchans_tbl[ctx->acmod];
448

    
449
    if (ctx->acmod & 0x01 && ctx->acmod != 0x01)
450
        ctx->cmixlev = get_bits(gb, 2);
451
    if (ctx->acmod & 0x04)
452
        ctx->surmixlev = get_bits(gb, 2);
453
    if (ctx->acmod == 0x02)
454
        ctx->dsurmod = get_bits(gb, 2);
455

    
456
    ctx->lfeon = get_bits1(gb);
457

    
458
    i = !(ctx->acmod);
459
    do {
460
        skip_bits(gb, 5); //skip dialog normalization
461
        if (get_bits1(gb))
462
            skip_bits(gb, 8); //skip compression
463
        if (get_bits1(gb))
464
            skip_bits(gb, 8); //skip language code
465
        if (get_bits1(gb))
466
            skip_bits(gb, 7); //skip audio production information
467
    } while (i--);
468

    
469
    skip_bits(gb, 2); //skip copyright bit and original bitstream bit
470

    
471
    if (get_bits1(gb))
472
        skip_bits(gb, 14); //skip timecode1
473
    if (get_bits1(gb))
474
        skip_bits(gb, 14); //skip timecode2
475

    
476
    if (get_bits1(gb)) {
477
        i = get_bits(gb, 6); //additional bsi length
478
        do {
479
            skip_bits(gb, 8);
480
        } while(i--);
481
    }
482
}
483

    
484
/* Decodes the grouped exponents.
485
 * This function decodes the coded exponents according to exponent strategy
486
 * and stores them in the decoded exponents buffer.
487
 *
488
 * @param gb GetBitContext which points to start of coded exponents
489
 * @param expstr Exponent coding strategy
490
 * @param ngrps Number of grouped exponetns
491
 * @param absexp Absolute exponent
492
 * @param dexps Decoded exponents are stored in dexps
493
 * @return Returns 0 if exponents are decoded successfully, -1 if error occurs
494
 */
495
static int decode_exponents(GetBitContext *gb, int expstr, int ngrps, uint8_t absexp, uint8_t *dexps)
496
{
497
    int exps;
498

    
499
    while (ngrps--) {
500
        exps = get_bits(gb, 7);
501

    
502
        absexp += exp_1[exps];
503
        if (absexp > 24) {
504
            av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
505
            return -ngrps;
506
        }
507
        switch (expstr) {
508
            case EXP_D45:
509
                *(dexps++) = absexp;
510
                *(dexps++) = absexp;
511
            case EXP_D25:
512
                *(dexps++) = absexp;
513
            case EXP_D15:
514
                *(dexps++) = absexp;
515
        }
516

    
517
        absexp += exp_2[exps];
518
        if (absexp > 24) {
519
            av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
520
            return -ngrps;
521
        }
522
        switch (expstr) {
523
            case EXP_D45:
524
                *(dexps++) = absexp;
525
                *(dexps++) = absexp;
526
            case EXP_D25:
527
                *(dexps++) = absexp;
528
            case EXP_D15:
529
                *(dexps++) = absexp;
530
        }
531

    
532
        absexp += exp_3[exps];
533
        if (absexp > 24) {
534
            av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
535
            return -ngrps;
536
        }
537
        switch (expstr) {
538
            case EXP_D45:
539
                *(dexps++) = absexp;
540
                *(dexps++) = absexp;
541
            case EXP_D25:
542
                *(dexps++) = absexp;
543
            case EXP_D15:
544
                *(dexps++) = absexp;
545
        }
546
    }
547

    
548
    return 0;
549
}
550

    
551
/* Performs bit allocation.
552
 * This function performs bit allocation for the requested chanenl.
553
 */
554
static void do_bit_allocation(AC3DecodeContext *ctx, int chnl)
555
{
556
    int fgain, snroffset;
557

    
558
    if (chnl == 5) {
559
        fgain = ff_fgaintab[ctx->cplfgaincod];
560
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->cplfsnroffst) << 2;
561
        ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->cplbap,
562
                                      ctx->dcplexps, ctx->cplstrtmant,
563
                                      ctx->cplendmant, snroffset, fgain, 0,
564
                                      ctx->cpldeltbae, ctx->cpldeltnseg,
565
                                      ctx->cpldeltoffst, ctx->cpldeltlen,
566
                                      ctx->cpldeltba);
567
    }
568
    else if (chnl == 6) {
569
        fgain = ff_fgaintab[ctx->lfefgaincod];
570
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->lfefsnroffst) << 2;
571
        ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->lfebap,
572
                                      ctx->dlfeexps, 0, 7, snroffset, fgain, 1,
573
                                      DBA_NONE, 0, NULL, NULL, NULL);
574
    }
575
    else {
576
        fgain = ff_fgaintab[ctx->fgaincod[chnl]];
577
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->fsnroffst[chnl]) << 2;
578
        ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->bap[chnl],
579
                                      ctx->dexps[chnl], 0, ctx->endmant[chnl],
580
                                      snroffset, fgain, 0, ctx->deltbae[chnl],
581
                                      ctx->deltnseg[chnl], ctx->deltoffst[chnl],
582
                                      ctx->deltlen[chnl], ctx->deltba[chnl]);
583
    }
584
}
585

    
586
/* Check if snroffsets are zero. */
587
static int is_snr_offsets_zero(AC3DecodeContext *ctx)
588
{
589
    int i;
590

    
591
    if ((ctx->csnroffst) || (ctx->cplinu && ctx->cplfsnroffst) ||
592
            (ctx->lfeon && ctx->lfefsnroffst))
593
        return 0;
594

    
595
    for (i = 0; i < ctx->nfchans; i++)
596
        if (ctx->fsnroffst[i])
597
            return 0;
598

    
599
    return 1;
600
}
601

    
602
typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
603
    int16_t l3_quantizers[3];
604
    int16_t l5_quantizers[3];
605
    int16_t l11_quantizers[2];
606
    int l3ptr;
607
    int l5ptr;
608
    int l11ptr;
609
} mant_groups;
610

    
611
#define TRANSFORM_COEFF(tc, m, e, f) (tc) = (m) * (f)[(e)]
612

    
613
/* Get the transform coefficients for coupling channel and uncouple channels.
614
 * The coupling transform coefficients starts at the the cplstrtmant, which is
615
 * equal to endmant[ch] for fbw channels. Hence we can uncouple channels before
616
 * getting transform coefficients for the channel.
617
 */
618
static int get_transform_coeffs_cpling(AC3DecodeContext *ctx, mant_groups *m)
619
{
620
    GetBitContext *gb = &ctx->gb;
621
    int ch, start, end, cplbndstrc, bnd, gcode, tbap;
622
    float cplcos[5], cplcoeff;
623
    uint8_t *exps = ctx->dcplexps;
624
    uint8_t *bap = ctx->cplbap;
625

    
626
    cplbndstrc = ctx->cplbndstrc;
627
    start = ctx->cplstrtmant;
628
    bnd = 0;
629

    
630
    while (start < ctx->cplendmant) {
631
        end = start + 12;
632
        while (cplbndstrc & 1) {
633
            end += 12;
634
            cplbndstrc >>= 1;
635
        }
636
        cplbndstrc >>= 1;
637
        for (ch = 0; ch < ctx->nfchans; ch++)
638
            cplcos[ch] = ctx->chcoeffs[ch] * ctx->cplco[ch][bnd];
639
        bnd++;
640

    
641
        while (start < end) {
642
            tbap = bap[start];
643
            switch(tbap) {
644
                case 0:
645
                    for (ch = 0; ch < ctx->nfchans; ch++)
646
                        if (((ctx->chincpl) >> ch) & 1) {
647
                            if ((ctx->dithflag >> ch) & 1) {
648
                                TRANSFORM_COEFF(cplcoeff, av_random(&ctx->dith_state) & 0xFFFF, exps[start], scale_factors);
649
                                ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch] * LEVEL_MINUS_3DB;
650
                            } else
651
                                ctx->transform_coeffs[ch + 1][start] = 0;
652
                        }
653
                    start++;
654
                    continue;
655
                case 1:
656
                    if (m->l3ptr > 2) {
657
                        gcode = get_bits(gb, 5);
658
                        m->l3_quantizers[0] = l3_quantizers_1[gcode];
659
                        m->l3_quantizers[1] = l3_quantizers_2[gcode];
660
                        m->l3_quantizers[2] = l3_quantizers_3[gcode];
661
                        m->l3ptr = 0;
662
                    }
663
                    TRANSFORM_COEFF(cplcoeff, m->l3_quantizers[m->l3ptr++], exps[start], scale_factors);
664
                    break;
665

    
666
                case 2:
667
                    if (m->l5ptr > 2) {
668
                        gcode = get_bits(gb, 7);
669
                        m->l5_quantizers[0] = l5_quantizers_1[gcode];
670
                        m->l5_quantizers[1] = l5_quantizers_2[gcode];
671
                        m->l5_quantizers[2] = l5_quantizers_3[gcode];
672
                        m->l5ptr = 0;
673
                    }
674
                    TRANSFORM_COEFF(cplcoeff, m->l5_quantizers[m->l5ptr++], exps[start], scale_factors);
675
                    break;
676

    
677
                case 3:
678
                    TRANSFORM_COEFF(cplcoeff, l7_quantizers[get_bits(gb, 3)], exps[start], scale_factors);
679
                    break;
680

    
681
                case 4:
682
                    if (m->l11ptr > 1) {
683
                        gcode = get_bits(gb, 7);
684
                        m->l11_quantizers[0] = l11_quantizers_1[gcode];
685
                        m->l11_quantizers[1] = l11_quantizers_2[gcode];
686
                        m->l11ptr = 0;
687
                    }
688
                    TRANSFORM_COEFF(cplcoeff, m->l11_quantizers[m->l11ptr++], exps[start], scale_factors);
689
                    break;
690

    
691
                case 5:
692
                    TRANSFORM_COEFF(cplcoeff, l15_quantizers[get_bits(gb, 4)], exps[start], scale_factors);
693
                    break;
694

    
695
                default:
696
                    TRANSFORM_COEFF(cplcoeff, get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]),
697
                            exps[start], scale_factors);
698
            }
699
            for (ch = 0; ch < ctx->nfchans; ch++)
700
                if ((ctx->chincpl >> ch) & 1)
701
                    ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
702
            start++;
703
        }
704
    }
705

    
706
    return 0;
707
}
708

    
709
/* Get the transform coefficients for particular channel */
710
static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
711
{
712
    GetBitContext *gb = &ctx->gb;
713
    int i, gcode, tbap, dithflag, end;
714
    uint8_t *exps;
715
    uint8_t *bap;
716
    float *coeffs;
717
    float factors[25];
718

    
719
    for (i = 0; i < 25; i++)
720
        factors[i] = scale_factors[i] * ctx->chcoeffs[ch_index];
721

    
722
    if (ch_index != -1) { /* fbw channels */
723
        dithflag = (ctx->dithflag >> ch_index) & 1;
724
        exps = ctx->dexps[ch_index];
725
        bap = ctx->bap[ch_index];
726
        coeffs = ctx->transform_coeffs[ch_index + 1];
727
        end = ctx->endmant[ch_index];
728
    } else if (ch_index == -1) {
729
        dithflag = 0;
730
        exps = ctx->dlfeexps;
731
        bap = ctx->lfebap;
732
        coeffs = ctx->transform_coeffs[0];
733
        end = 7;
734
    }
735

    
736

    
737
    for (i = 0; i < end; i++) {
738
        tbap = bap[i];
739
        switch (tbap) {
740
            case 0:
741
                if (!dithflag) {
742
                    coeffs[i] = 0;
743
                    continue;
744
                }
745
                else {
746
                    TRANSFORM_COEFF(coeffs[i], av_random(&ctx->dith_state) & 0xFFFF, exps[i], factors);
747
                    coeffs[i] *= LEVEL_MINUS_3DB;
748
                    continue;
749
                }
750

    
751
            case 1:
752
                if (m->l3ptr > 2) {
753
                    gcode = get_bits(gb, 5);
754
                    m->l3_quantizers[0] = l3_quantizers_1[gcode];
755
                    m->l3_quantizers[1] = l3_quantizers_2[gcode];
756
                    m->l3_quantizers[2] = l3_quantizers_3[gcode];
757
                    m->l3ptr = 0;
758
                }
759
                TRANSFORM_COEFF(coeffs[i], m->l3_quantizers[m->l3ptr++], exps[i], factors);
760
                continue;
761

    
762
            case 2:
763
                if (m->l5ptr > 2) {
764
                    gcode = get_bits(gb, 7);
765
                    m->l5_quantizers[0] = l5_quantizers_1[gcode];
766
                    m->l5_quantizers[1] = l5_quantizers_2[gcode];
767
                    m->l5_quantizers[2] = l5_quantizers_3[gcode];
768
                    m->l5ptr = 0;
769
                }
770
                TRANSFORM_COEFF(coeffs[i], m->l5_quantizers[m->l5ptr++], exps[i], factors);
771
                continue;
772

    
773
            case 3:
774
                TRANSFORM_COEFF(coeffs[i], l7_quantizers[get_bits(gb, 3)], exps[i], factors);
775
                continue;
776

    
777
            case 4:
778
                if (m->l11ptr > 1) {
779
                    gcode = get_bits(gb, 7);
780
                    m->l11_quantizers[0] = l11_quantizers_1[gcode];
781
                    m->l11_quantizers[1] = l11_quantizers_2[gcode];
782
                    m->l11ptr = 0;
783
                }
784
                TRANSFORM_COEFF(coeffs[i], m->l11_quantizers[m->l11ptr++], exps[i], factors);
785
                continue;
786

    
787
            case 5:
788
                TRANSFORM_COEFF(coeffs[i], l15_quantizers[get_bits(gb, 4)], exps[i], factors);
789
                continue;
790

    
791
            default:
792
                TRANSFORM_COEFF(coeffs[i], get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]), exps[i], factors);
793
                continue;
794
        }
795
    }
796

    
797
    return 0;
798
}
799

    
800
/* Get the transform coefficients.
801
 * This function extracts the tranform coefficients form the ac3 bitstream.
802
 * This function is called after bit allocation is performed.
803
 */
804
static int get_transform_coeffs(AC3DecodeContext * ctx)
805
{
806
    int i, end;
807
    int got_cplchan = 0;
808
    mant_groups m;
809

    
810
    m.l3ptr = m.l5ptr = m.l11ptr = 3;
811

    
812
    for (i = 0; i < ctx->nfchans; i++) {
813
        /* transform coefficients for individual channel */
814
        if (get_transform_coeffs_ch(ctx, i, &m))
815
            return -1;
816
        /* tranform coefficients for coupling channels */
817
        if ((ctx->chincpl >> i) & 1)  {
818
            if (!got_cplchan) {
819
                if (get_transform_coeffs_cpling(ctx, &m)) {
820
                    av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
821
                    return -1;
822
                }
823
                got_cplchan = 1;
824
            }
825
            end = ctx->cplendmant;
826
        } else
827
            end = ctx->endmant[i];
828
        do
829
            ctx->transform_coeffs[i + 1][end] = 0;
830
        while(++end < 256);
831
    }
832
    if (ctx->lfeon) {
833
        if (get_transform_coeffs_ch(ctx, -1, &m))
834
                return -1;
835
        for (i = 7; i < 256; i++) {
836
            ctx->transform_coeffs[0][i] = 0;
837
        }
838
    }
839

    
840
    return 0;
841
}
842

    
843
/* Rematrixing routines. */
844
static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
845
{
846
    float tmp0, tmp1;
847

    
848
    while (start < end) {
849
        tmp0 = ctx->transform_coeffs[1][start];
850
        tmp1 = ctx->transform_coeffs[2][start];
851
        ctx->transform_coeffs[1][start] = tmp0 + tmp1;
852
        ctx->transform_coeffs[2][start] = tmp0 - tmp1;
853
        start++;
854
    }
855
}
856

    
857
static void do_rematrixing(AC3DecodeContext *ctx)
858
{
859
    int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
860
    int end, bndend;
861

    
862
    end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
863

    
864
    if (ctx->rematflg & 1)
865
        do_rematrixing1(ctx, bnd1, bnd2);
866

    
867
    if (ctx->rematflg & 2)
868
        do_rematrixing1(ctx, bnd2, bnd3);
869

    
870
    bndend = bnd4;
871
    if (bndend > end) {
872
        bndend = end;
873
        if (ctx->rematflg & 4)
874
            do_rematrixing1(ctx, bnd3, bndend);
875
    } else {
876
        if (ctx->rematflg & 4)
877
            do_rematrixing1(ctx, bnd3, bnd4);
878
        if (ctx->rematflg & 8)
879
            do_rematrixing1(ctx, bnd4, end);
880
    }
881
}
882

    
883
/* This function sets the normalized channel coefficients.
884
 * Transform coefficients are multipllied by the channel
885
 * coefficients to get normalized transform coefficients.
886
 */
887
static void get_downmix_coeffs(AC3DecodeContext *ctx)
888
{
889
    int from = ctx->acmod;
890
    int to = ctx->blkoutput;
891
    float clev = clevs[ctx->cmixlev];
892
    float slev = slevs[ctx->surmixlev];
893
    float nf = 1.0; //normalization factor for downmix coeffs
894
    int i;
895

    
896
    if (!ctx->acmod) {
897
        ctx->chcoeffs[0] = 2 * ctx->dynrng;
898
        ctx->chcoeffs[1] = 2 * ctx->dynrng2;
899
    } else {
900
        for (i = 0; i < ctx->nfchans; i++)
901
            ctx->chcoeffs[i] = 2 * ctx->dynrng;
902
    }
903

    
904
    if (to == AC3_OUTPUT_UNMODIFIED)
905
        return;
906

    
907
    switch (from) {
908
        case AC3_ACMOD_DUALMONO:
909
            switch (to) {
910
                case AC3_OUTPUT_MONO:
911
                case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */
912
                    nf = 0.5;
913
                    ctx->chcoeffs[0] *= nf;
914
                    ctx->chcoeffs[1] *= nf;
915
                    break;
916
            }
917
            break;
918
        case AC3_ACMOD_MONO:
919
            switch (to) {
920
                case AC3_OUTPUT_STEREO:
921
                    nf = LEVEL_MINUS_3DB;
922
                    ctx->chcoeffs[0] *= nf;
923
                    break;
924
            }
925
            break;
926
        case AC3_ACMOD_STEREO:
927
            switch (to) {
928
                case AC3_OUTPUT_MONO:
929
                    nf = LEVEL_MINUS_3DB;
930
                    ctx->chcoeffs[0] *= nf;
931
                    ctx->chcoeffs[1] *= nf;
932
                    break;
933
            }
934
            break;
935
        case AC3_ACMOD_3F:
936
            switch (to) {
937
                case AC3_OUTPUT_MONO:
938
                    nf = LEVEL_MINUS_3DB / (1.0 + clev);
939
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
940
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
941
                    ctx->chcoeffs[1] *= ((nf * clev * LEVEL_MINUS_3DB) / 2.0);
942
                    break;
943
                case AC3_OUTPUT_STEREO:
944
                    nf = 1.0 / (1.0 + clev);
945
                    ctx->chcoeffs[0] *= nf;
946
                    ctx->chcoeffs[2] *= nf;
947
                    ctx->chcoeffs[1] *= (nf * clev);
948
                    break;
949
            }
950
            break;
951
        case AC3_ACMOD_2F1R:
952
            switch (to) {
953
                case AC3_OUTPUT_MONO:
954
                    nf = 2.0 * LEVEL_MINUS_3DB / (2.0 + slev);
955
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
956
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
957
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
958
                    break;
959
                case AC3_OUTPUT_STEREO:
960
                    nf = 1.0 / (1.0 + (slev * LEVEL_MINUS_3DB));
961
                    ctx->chcoeffs[0] *= nf;
962
                    ctx->chcoeffs[1] *= nf;
963
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
964
                    break;
965
                case AC3_OUTPUT_DOLBY:
966
                    nf = 1.0 / (1.0 + LEVEL_MINUS_3DB);
967
                    ctx->chcoeffs[0] *= nf;
968
                    ctx->chcoeffs[1] *= nf;
969
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
970
                    break;
971
            }
972
            break;
973
        case AC3_ACMOD_3F1R:
974
            switch (to) {
975
                case AC3_OUTPUT_MONO:
976
                    nf = LEVEL_MINUS_3DB / (1.0 + clev + (slev / 2.0));
977
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
978
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
979
                    ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
980
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
981
                    break;
982
                case AC3_OUTPUT_STEREO:
983
                    nf = 1.0 / (1.0 + clev + (slev * LEVEL_MINUS_3DB));
984
                    ctx->chcoeffs[0] *= nf;
985
                    ctx->chcoeffs[2] *= nf;
986
                    ctx->chcoeffs[1] *= (nf * clev);
987
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
988
                    break;
989
                case AC3_OUTPUT_DOLBY:
990
                    nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
991
                    ctx->chcoeffs[0] *= nf;
992
                    ctx->chcoeffs[1] *= nf;
993
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
994
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
995
                    break;
996
            }
997
            break;
998
        case AC3_ACMOD_2F2R:
999
            switch (to) {
1000
                case AC3_OUTPUT_MONO:
1001
                    nf = LEVEL_MINUS_3DB / (1.0 + slev);
1002
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1003
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1004
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
1005
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1006
                    break;
1007
                case AC3_OUTPUT_STEREO:
1008
                    nf = 1.0 / (1.0 + slev);
1009
                    ctx->chcoeffs[0] *= nf;
1010
                    ctx->chcoeffs[1] *= nf;
1011
                    ctx->chcoeffs[2] *= (nf * slev);
1012
                    ctx->chcoeffs[3] *= (nf * slev);
1013
                    break;
1014
                case AC3_OUTPUT_DOLBY:
1015
                    nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
1016
                    ctx->chcoeffs[0] *= nf;
1017
                    ctx->chcoeffs[1] *= nf;
1018
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1019
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
1020
                    break;
1021
            }
1022
            break;
1023
        case AC3_ACMOD_3F2R:
1024
            switch (to) {
1025
                case AC3_OUTPUT_MONO:
1026
                    nf = LEVEL_MINUS_3DB / (1.0 + clev + slev);
1027
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1028
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1029
                    ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
1030
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1031
                    ctx->chcoeffs[4] *= (nf * slev * LEVEL_MINUS_3DB);
1032
                    break;
1033
                case AC3_OUTPUT_STEREO:
1034
                    nf = 1.0 / (1.0 + clev + slev);
1035
                    ctx->chcoeffs[0] *= nf;
1036
                    ctx->chcoeffs[2] *= nf;
1037
                    ctx->chcoeffs[1] *= (nf * clev);
1038
                    ctx->chcoeffs[3] *= (nf * slev);
1039
                    ctx->chcoeffs[4] *= (nf * slev);
1040
                    break;
1041
                case AC3_OUTPUT_DOLBY:
1042
                    nf = 1.0 / (1.0 + (3.0 * LEVEL_MINUS_3DB));
1043
                    ctx->chcoeffs[0] *= nf;
1044
                    ctx->chcoeffs[1] *= nf;
1045
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1046
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
1047
                    ctx->chcoeffs[4] *= (nf * LEVEL_MINUS_3DB);
1048
                    break;
1049
            }
1050
            break;
1051
    }
1052
}
1053

    
1054
/*********** BEGIN DOWNMIX FUNCTIONS ***********/
1055
static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx)
1056
{
1057
    int i;
1058
    float (*output)[BLOCK_SIZE] = ctx->output;
1059

    
1060
    for (i = 0; i < 256; i++)
1061
        output[1][i] += output[2][i];
1062
    memset(output[2], 0, sizeof(output[2]));
1063
}
1064

    
1065
static inline void mix_dualmono_to_stereo(AC3DecodeContext *ctx)
1066
{
1067
    int i;
1068
    float tmp;
1069
    float (*output)[BLOCK_SIZE] = ctx->output;
1070

    
1071
    for (i = 0; i < 256; i++) {
1072
        tmp = output[1][i] + output[2][i];
1073
        output[1][i] = output[2][i] = tmp;
1074
    }
1075
}
1076

    
1077
static inline void upmix_mono_to_stereo(AC3DecodeContext *ctx)
1078
{
1079
    int i;
1080
    float (*output)[BLOCK_SIZE] = ctx->output;
1081

    
1082
    for (i = 0; i < 256; i++)
1083
        output[2][i] = output[1][i];
1084
}
1085

    
1086
static inline void mix_stereo_to_mono(AC3DecodeContext *ctx)
1087
{
1088
    int i;
1089
    float (*output)[BLOCK_SIZE] = ctx->output;
1090

    
1091
    for (i = 0; i < 256; i++)
1092
        output[1][i] += output[2][i];
1093
    memset(output[2], 0, sizeof(output[2]));
1094
}
1095

    
1096
static inline void mix_3f_to_mono(AC3DecodeContext *ctx)
1097
{
1098
    int i;
1099
    float (*output)[BLOCK_SIZE] = ctx->output;
1100

    
1101
    for (i = 0; i < 256; i++)
1102
        output[1][i] += (output[2][i] + output[3][i]);
1103
    memset(output[2], 0, sizeof(output[2]));
1104
    memset(output[3], 0, sizeof(output[3]));
1105
}
1106

    
1107
static inline void mix_3f_to_stereo(AC3DecodeContext *ctx)
1108
{
1109
    int i;
1110
    float (*output)[BLOCK_SIZE] = ctx->output;
1111

    
1112
    for (i = 0; i < 256; i++) {
1113
        output[1][i] += output[2][i];
1114
        output[2][i] += output[3][i];
1115
    }
1116
    memset(output[3], 0, sizeof(output[3]));
1117
}
1118

    
1119
static inline void mix_2f_1r_to_mono(AC3DecodeContext *ctx)
1120
{
1121
    int i;
1122
    float (*output)[BLOCK_SIZE] = ctx->output;
1123

    
1124
    for (i = 0; i < 256; i++)
1125
        output[1][i] += (output[2][i] + output[3][i]);
1126
    memset(output[2], 0, sizeof(output[2]));
1127
    memset(output[3], 0, sizeof(output[3]));
1128

    
1129
}
1130

    
1131
static inline void mix_2f_1r_to_stereo(AC3DecodeContext *ctx)
1132
{
1133
    int i;
1134
    float (*output)[BLOCK_SIZE] = ctx->output;
1135

    
1136
    for (i = 0; i < 256; i++) {
1137
        output[1][i] += output[2][i];
1138
        output[2][i] += output[3][i];
1139
    }
1140
    memset(output[3], 0, sizeof(output[3]));
1141
}
1142

    
1143
static inline void mix_2f_1r_to_dolby(AC3DecodeContext *ctx)
1144
{
1145
    int i;
1146
    float (*output)[BLOCK_SIZE] = ctx->output;
1147

    
1148
    for (i = 0; i < 256; i++) {
1149
        output[1][i] -= output[3][i];
1150
        output[2][i] += output[3][i];
1151
    }
1152
    memset(output[3], 0, sizeof(output[3]));
1153
}
1154

    
1155
static inline void mix_3f_1r_to_mono(AC3DecodeContext *ctx)
1156
{
1157
    int i;
1158
    float (*output)[BLOCK_SIZE] = ctx->output;
1159

    
1160
    for (i = 0; i < 256; i++)
1161
        output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1162
    memset(output[2], 0, sizeof(output[2]));
1163
    memset(output[3], 0, sizeof(output[3]));
1164
    memset(output[4], 0, sizeof(output[4]));
1165
}
1166

    
1167
static inline void mix_3f_1r_to_stereo(AC3DecodeContext *ctx)
1168
{
1169
    int i;
1170
    float (*output)[BLOCK_SIZE] = ctx->output;
1171

    
1172
    for (i = 0; i < 256; i++) {
1173
        output[1][i] += (output[2][i] + output[4][i]);
1174
        output[2][i] += (output[3][i] + output[4][i]);
1175
    }
1176
    memset(output[3], 0, sizeof(output[3]));
1177
    memset(output[4], 0, sizeof(output[4]));
1178
}
1179

    
1180
static inline void mix_3f_1r_to_dolby(AC3DecodeContext *ctx)
1181
{
1182
    int i;
1183
    float (*output)[BLOCK_SIZE] = ctx->output;
1184

    
1185
    for (i = 0; i < 256; i++) {
1186
        output[1][i] += (output[2][i] - output[4][i]);
1187
        output[2][i] += (output[3][i] + output[4][i]);
1188
    }
1189
    memset(output[3], 0, sizeof(output[3]));
1190
    memset(output[4], 0, sizeof(output[4]));
1191
}
1192

    
1193
static inline void mix_2f_2r_to_mono(AC3DecodeContext *ctx)
1194
{
1195
    int i;
1196
    float (*output)[BLOCK_SIZE] = ctx->output;
1197

    
1198
    for (i = 0; i < 256; i++)
1199
        output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1200
    memset(output[2], 0, sizeof(output[2]));
1201
    memset(output[3], 0, sizeof(output[3]));
1202
    memset(output[4], 0, sizeof(output[4]));
1203
}
1204

    
1205
static inline void mix_2f_2r_to_stereo(AC3DecodeContext *ctx)
1206
{
1207
    int i;
1208
    float (*output)[BLOCK_SIZE] = ctx->output;
1209

    
1210
    for (i = 0; i < 256; i++) {
1211
        output[1][i] += output[3][i];
1212
        output[2][i] += output[4][i];
1213
    }
1214
    memset(output[3], 0, sizeof(output[3]));
1215
    memset(output[4], 0, sizeof(output[4]));
1216
}
1217

    
1218
static inline void mix_2f_2r_to_dolby(AC3DecodeContext *ctx)
1219
{
1220
    int i;
1221
    float (*output)[BLOCK_SIZE] = ctx->output;
1222

    
1223
    for (i = 0; i < 256; i++) {
1224
        output[1][i] -= output[3][i];
1225
        output[2][i] += output[4][i];
1226
    }
1227
    memset(output[3], 0, sizeof(output[3]));
1228
    memset(output[4], 0, sizeof(output[4]));
1229
}
1230

    
1231
static inline void mix_3f_2r_to_mono(AC3DecodeContext *ctx)
1232
{
1233
    int i;
1234
    float (*output)[BLOCK_SIZE] = ctx->output;
1235

    
1236
    for (i = 0; i < 256; i++)
1237
        output[1][i] += (output[2][i] + output[3][i] + output[4][i] + output[5][i]);
1238
    memset(output[2], 0, sizeof(output[2]));
1239
    memset(output[3], 0, sizeof(output[3]));
1240
    memset(output[4], 0, sizeof(output[4]));
1241
    memset(output[5], 0, sizeof(output[5]));
1242
}
1243

    
1244
static inline void mix_3f_2r_to_stereo(AC3DecodeContext *ctx)
1245
{
1246
    int i;
1247
    float (*output)[BLOCK_SIZE] = ctx->output;
1248

    
1249
    for (i = 0; i < 256; i++) {
1250
        output[1][i] += (output[2][i] + output[4][i]);
1251
        output[2][i] += (output[3][i] + output[5][i]);
1252
    }
1253
    memset(output[3], 0, sizeof(output[3]));
1254
    memset(output[4], 0, sizeof(output[4]));
1255
    memset(output[5], 0, sizeof(output[5]));
1256
}
1257

    
1258
static inline void mix_3f_2r_to_dolby(AC3DecodeContext *ctx)
1259
{
1260
    int i;
1261
    float (*output)[BLOCK_SIZE] = ctx->output;
1262

    
1263
    for (i = 0; i < 256; i++) {
1264
        output[1][i] += (output[2][i] - output[4][i] - output[5][i]);
1265
        output[2][i] += (output[3][i] + output[4][i] + output[5][i]);
1266
    }
1267
    memset(output[3], 0, sizeof(output[3]));
1268
    memset(output[4], 0, sizeof(output[4]));
1269
    memset(output[5], 0, sizeof(output[5]));
1270
}
1271
/*********** END DOWNMIX FUNCTIONS ***********/
1272

    
1273
/* Downmix the output.
1274
 * This function downmixes the output when the number of input
1275
 * channels is not equal to the number of output channels requested.
1276
 */
1277
static void do_downmix(AC3DecodeContext *ctx)
1278
{
1279
    int from = ctx->acmod;
1280
    int to = ctx->blkoutput;
1281

    
1282
    if (to == AC3_OUTPUT_UNMODIFIED)
1283
        return;
1284

    
1285
    switch (from) {
1286
        case AC3_ACMOD_DUALMONO:
1287
            switch (to) {
1288
                case AC3_OUTPUT_MONO:
1289
                    mix_dualmono_to_mono(ctx);
1290
                    break;
1291
                case AC3_OUTPUT_STEREO: /* We assume that sum of both mono channels is requested */
1292
                    mix_dualmono_to_stereo(ctx);
1293
                    break;
1294
            }
1295
            break;
1296
        case AC3_ACMOD_MONO:
1297
            switch (to) {
1298
                case AC3_OUTPUT_STEREO:
1299
                    upmix_mono_to_stereo(ctx);
1300
                    break;
1301
            }
1302
            break;
1303
        case AC3_ACMOD_STEREO:
1304
            switch (to) {
1305
                case AC3_OUTPUT_MONO:
1306
                    mix_stereo_to_mono(ctx);
1307
                    break;
1308
            }
1309
            break;
1310
        case AC3_ACMOD_3F:
1311
            switch (to) {
1312
                case AC3_OUTPUT_MONO:
1313
                    mix_3f_to_mono(ctx);
1314
                    break;
1315
                case AC3_OUTPUT_STEREO:
1316
                    mix_3f_to_stereo(ctx);
1317
                    break;
1318
            }
1319
            break;
1320
        case AC3_ACMOD_2F1R:
1321
            switch (to) {
1322
                case AC3_OUTPUT_MONO:
1323
                    mix_2f_1r_to_mono(ctx);
1324
                    break;
1325
                case AC3_OUTPUT_STEREO:
1326
                    mix_2f_1r_to_stereo(ctx);
1327
                    break;
1328
                case AC3_OUTPUT_DOLBY:
1329
                    mix_2f_1r_to_dolby(ctx);
1330
                    break;
1331
            }
1332
            break;
1333
        case AC3_ACMOD_3F1R:
1334
            switch (to) {
1335
                case AC3_OUTPUT_MONO:
1336
                    mix_3f_1r_to_mono(ctx);
1337
                    break;
1338
                case AC3_OUTPUT_STEREO:
1339
                    mix_3f_1r_to_stereo(ctx);
1340
                    break;
1341
                case AC3_OUTPUT_DOLBY:
1342
                    mix_3f_1r_to_dolby(ctx);
1343
                    break;
1344
            }
1345
            break;
1346
        case AC3_ACMOD_2F2R:
1347
            switch (to) {
1348
                case AC3_OUTPUT_MONO:
1349
                    mix_2f_2r_to_mono(ctx);
1350
                    break;
1351
                case AC3_OUTPUT_STEREO:
1352
                    mix_2f_2r_to_stereo(ctx);
1353
                    break;
1354
                case AC3_OUTPUT_DOLBY:
1355
                    mix_2f_2r_to_dolby(ctx);
1356
                    break;
1357
            }
1358
            break;
1359
        case AC3_ACMOD_3F2R:
1360
            switch (to) {
1361
                case AC3_OUTPUT_MONO:
1362
                    mix_3f_2r_to_mono(ctx);
1363
                    break;
1364
                case AC3_OUTPUT_STEREO:
1365
                    mix_3f_2r_to_stereo(ctx);
1366
                    break;
1367
                case AC3_OUTPUT_DOLBY:
1368
                    mix_3f_2r_to_dolby(ctx);
1369
                    break;
1370
            }
1371
            break;
1372
    }
1373
}
1374

    
1375
/* This function performs the imdct on 256 sample transform
1376
 * coefficients.
1377
 */
1378
static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
1379
{
1380
    int i, k;
1381
    float x[128];
1382
    FFTComplex z[2][64];
1383
    float *o_ptr = ctx->tmp_output;
1384

    
1385
    for(i=0; i<2; i++) {
1386
        /* de-interleave coefficients */
1387
        for(k=0; k<128; k++) {
1388
            x[k] = ctx->transform_coeffs[chindex][2*k+i];
1389
        }
1390

    
1391
        /* run standard IMDCT */
1392
        ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, o_ptr, x, ctx->tmp_imdct);
1393

    
1394
        /* reverse the post-rotation & reordering from standard IMDCT */
1395
        for(k=0; k<32; k++) {
1396
            z[i][32+k].re = -o_ptr[128+2*k];
1397
            z[i][32+k].im = -o_ptr[2*k];
1398
            z[i][31-k].re =  o_ptr[2*k+1];
1399
            z[i][31-k].im =  o_ptr[128+2*k+1];
1400
        }
1401
    }
1402

    
1403
    /* apply AC-3 post-rotation & reordering */
1404
    for(k=0; k<64; k++) {
1405
        o_ptr[    2*k  ] = -z[0][   k].im;
1406
        o_ptr[    2*k+1] =  z[0][63-k].re;
1407
        o_ptr[128+2*k  ] = -z[0][   k].re;
1408
        o_ptr[128+2*k+1] =  z[0][63-k].im;
1409
        o_ptr[256+2*k  ] = -z[1][   k].re;
1410
        o_ptr[256+2*k+1] =  z[1][63-k].im;
1411
        o_ptr[384+2*k  ] =  z[1][   k].im;
1412
        o_ptr[384+2*k+1] = -z[1][63-k].re;
1413
    }
1414
}
1415

    
1416
/* IMDCT Transform. */
1417
static inline void do_imdct(AC3DecodeContext *ctx)
1418
{
1419
    int ch;
1420

    
1421
    if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
1422
        ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1423
                                      ctx->transform_coeffs[0], ctx->tmp_imdct);
1424
    }
1425
    for (ch=1; ch<=ctx->nfchans; ch++) {
1426
        if ((ctx->blksw >> (ch-1)) & 1)
1427
            do_imdct_256(ctx, ch);
1428
        else
1429
            ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1430
                                          ctx->transform_coeffs[ch],
1431
                                          ctx->tmp_imdct);
1432

    
1433
        ctx->dsp.vector_fmul_add_add(ctx->output[ch], ctx->tmp_output,
1434
                                     ctx->window, ctx->delay[ch], 384, 256, 1);
1435
        ctx->dsp.vector_fmul_reverse(ctx->delay[ch], ctx->tmp_output+256,
1436
                                     ctx->window, 256);
1437
    }
1438
}
1439

    
1440
/* Parse the audio block from ac3 bitstream.
1441
 * This function extract the audio block from the ac3 bitstream
1442
 * and produces the output for the block. This function must
1443
 * be called for each of the six audio block in the ac3 bitstream.
1444
 */
1445
static int ac3_parse_audio_block(AC3DecodeContext * ctx)
1446
{
1447
    int nfchans = ctx->nfchans;
1448
    int acmod = ctx->acmod;
1449
    int i, bnd, rbnd, seg, grpsize;
1450
    GetBitContext *gb = &ctx->gb;
1451
    int bit_alloc_flags = 0;
1452
    uint8_t *dexps;
1453
    int mstrcplco, cplcoexp, cplcomant;
1454
    int dynrng, chbwcod, ngrps, cplabsexp, skipl;
1455

    
1456
    ctx->blksw = 0;
1457
    for (i = 0; i < nfchans; i++) /*block switch flag */
1458
        ctx->blksw |= get_bits1(gb) << i;
1459

    
1460
    ctx->dithflag = 0;
1461
    for (i = 0; i < nfchans; i++) /* dithering flag */
1462
        ctx->dithflag |= get_bits1(gb) << i;
1463

    
1464
    if (get_bits1(gb)) { /* dynamic range */
1465
        dynrng = get_sbits(gb, 8);
1466
        ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1467
    }
1468

    
1469
    if (acmod == 0x00 && get_bits1(gb)) { /* dynamic range 1+1 mode */
1470
        dynrng = get_sbits(gb, 8);
1471
        ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1472
    }
1473

    
1474
    get_downmix_coeffs(ctx);
1475

    
1476
    if (get_bits1(gb)) { /* coupling strategy */
1477
        ctx->cplinu = get_bits1(gb);
1478
        ctx->cplbndstrc = 0;
1479
        ctx->chincpl = 0;
1480
        if (ctx->cplinu) { /* coupling in use */
1481
            for (i = 0; i < nfchans; i++)
1482
                ctx->chincpl |= get_bits1(gb) << i;
1483

    
1484
            if (acmod == 0x02)
1485
                ctx->phsflginu = get_bits1(gb); //phase flag in use
1486

    
1487
            ctx->cplbegf = get_bits(gb, 4);
1488
            ctx->cplendf = get_bits(gb, 4);
1489

    
1490
            if (3 + ctx->cplendf - ctx->cplbegf < 0) {
1491
                av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", ctx->cplendf, ctx->cplbegf);
1492
                return -1;
1493
            }
1494

    
1495
            ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf;
1496
            ctx->cplstrtmant = ctx->cplbegf * 12 + 37;
1497
            ctx->cplendmant = ctx->cplendf * 12 + 73;
1498
            for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
1499
                if (get_bits1(gb)) {
1500
                    ctx->cplbndstrc |= 1 << i;
1501
                    ctx->ncplbnd--;
1502
                }
1503
        }
1504
    }
1505

    
1506
    if (ctx->cplinu) {
1507
        ctx->cplcoe = 0;
1508

    
1509
        for (i = 0; i < nfchans; i++)
1510
            if ((ctx->chincpl) >> i & 1)
1511
                if (get_bits1(gb)) { /* coupling co-ordinates */
1512
                    ctx->cplcoe |= 1 << i;
1513
                    mstrcplco = 3 * get_bits(gb, 2);
1514
                    for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
1515
                        cplcoexp = get_bits(gb, 4);
1516
                        cplcomant = get_bits(gb, 4);
1517
                        if (cplcoexp == 15)
1518
                            cplcomant <<= 14;
1519
                        else
1520
                            cplcomant = (cplcomant | 0x10) << 13;
1521
                        ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
1522
                    }
1523
                }
1524

    
1525
        if (acmod == 0x02 && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
1526
            for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
1527
                if (get_bits1(gb))
1528
                    ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
1529
    }
1530

    
1531
    if (acmod == 0x02) {/* rematrixing */
1532
        ctx->rematstr = get_bits1(gb);
1533
        if (ctx->rematstr) {
1534
            ctx->rematflg = 0;
1535

    
1536
            if (!(ctx->cplinu) || ctx->cplbegf > 2)
1537
                for (rbnd = 0; rbnd < 4; rbnd++)
1538
                    ctx->rematflg |= get_bits1(gb) << rbnd;
1539
            if (ctx->cplbegf > 0 && ctx->cplbegf <= 2 && ctx->cplinu)
1540
                for (rbnd = 0; rbnd < 3; rbnd++)
1541
                    ctx->rematflg |= get_bits1(gb) << rbnd;
1542
            if (ctx->cplbegf == 0 && ctx->cplinu)
1543
                for (rbnd = 0; rbnd < 2; rbnd++)
1544
                    ctx->rematflg |= get_bits1(gb) << rbnd;
1545
        }
1546
    }
1547

    
1548
    ctx->cplexpstr = EXP_REUSE;
1549
    ctx->lfeexpstr = EXP_REUSE;
1550
    if (ctx->cplinu) /* coupling exponent strategy */
1551
        ctx->cplexpstr = get_bits(gb, 2);
1552
    for (i = 0; i < nfchans; i++)  /* channel exponent strategy */
1553
        ctx->chexpstr[i] = get_bits(gb, 2);
1554
    if (ctx->lfeon)  /* lfe exponent strategy */
1555
        ctx->lfeexpstr = get_bits1(gb);
1556

    
1557
    for (i = 0; i < nfchans; i++) /* channel bandwidth code */
1558
        if (ctx->chexpstr[i] != EXP_REUSE) {
1559
            if ((ctx->chincpl >> i) & 1)
1560
                ctx->endmant[i] = ctx->cplstrtmant;
1561
            else {
1562
                chbwcod = get_bits(gb, 6);
1563
                if (chbwcod > 60) {
1564
                    av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
1565
                    return -1;
1566
                }
1567
                ctx->endmant[i] = chbwcod * 3 + 73;
1568
            }
1569
        }
1570

    
1571
    if (ctx->cplexpstr != EXP_REUSE) {/* coupling exponents */
1572
        bit_alloc_flags = 64;
1573
        cplabsexp = get_bits(gb, 4) << 1;
1574
        ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
1575
        if (decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant)) {
1576
            av_log(NULL, AV_LOG_ERROR, "error decoding coupling exponents\n");
1577
            return -1;
1578
        }
1579
    }
1580

    
1581
    for (i = 0; i < nfchans; i++) /* fbw channel exponents */
1582
        if (ctx->chexpstr[i] != EXP_REUSE) {
1583
            bit_alloc_flags |= 1 << i;
1584
            grpsize = 3 << (ctx->chexpstr[i] - 1);
1585
            ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
1586
            dexps = ctx->dexps[i];
1587
            dexps[0] = get_bits(gb, 4);
1588
            if (decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1)) {
1589
                av_log(NULL, AV_LOG_ERROR, "error decoding channel %d exponents\n", i);
1590
                return -1;
1591
            }
1592
            skip_bits(gb, 2); /* skip gainrng */
1593
        }
1594

    
1595
    if (ctx->lfeexpstr != EXP_REUSE) { /* lfe exponents */
1596
        bit_alloc_flags |= 32;
1597
        ctx->dlfeexps[0] = get_bits(gb, 4);
1598
        if (decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1)) {
1599
            av_log(NULL, AV_LOG_ERROR, "error decoding lfe exponents\n");
1600
            return -1;
1601
        }
1602
    }
1603

    
1604
    if (get_bits1(gb)) { /* bit allocation information */
1605
        bit_alloc_flags = 127;
1606
        ctx->sdcycod = get_bits(gb, 2);
1607
        ctx->fdcycod = get_bits(gb, 2);
1608
        ctx->sgaincod = get_bits(gb, 2);
1609
        ctx->dbpbcod = get_bits(gb, 2);
1610
        ctx->floorcod = get_bits(gb, 3);
1611
    }
1612

    
1613
    if (get_bits1(gb)) { /* snroffset */
1614
        bit_alloc_flags = 127;
1615
        ctx->csnroffst = get_bits(gb, 6);
1616
        if (ctx->cplinu) { /* coupling fine snr offset and fast gain code */
1617
            ctx->cplfsnroffst = get_bits(gb, 4);
1618
            ctx->cplfgaincod = get_bits(gb, 3);
1619
        }
1620
        for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
1621
            ctx->fsnroffst[i] = get_bits(gb, 4);
1622
            ctx->fgaincod[i] = get_bits(gb, 3);
1623
        }
1624
        if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
1625
            ctx->lfefsnroffst = get_bits(gb, 4);
1626
            ctx->lfefgaincod = get_bits(gb, 3);
1627
        }
1628
    }
1629

    
1630
    if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
1631
        bit_alloc_flags |= 64;
1632
        ctx->cplfleak = get_bits(gb, 3);
1633
        ctx->cplsleak = get_bits(gb, 3);
1634
    }
1635

    
1636
    if (get_bits1(gb)) { /* delta bit allocation information */
1637
        bit_alloc_flags = 127;
1638

    
1639
        if (ctx->cplinu) {
1640
            ctx->cpldeltbae = get_bits(gb, 2);
1641
            if (ctx->cpldeltbae == DBA_RESERVED) {
1642
                av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
1643
                return -1;
1644
            }
1645
        }
1646

    
1647
        for (i = 0; i < nfchans; i++) {
1648
            ctx->deltbae[i] = get_bits(gb, 2);
1649
            if (ctx->deltbae[i] == DBA_RESERVED) {
1650
                av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
1651
                return -1;
1652
            }
1653
        }
1654

    
1655
        if (ctx->cplinu)
1656
            if (ctx->cpldeltbae == DBA_NEW) { /*coupling delta offset, len and bit allocation */
1657
                ctx->cpldeltnseg = get_bits(gb, 3);
1658
                for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
1659
                    ctx->cpldeltoffst[seg] = get_bits(gb, 5);
1660
                    ctx->cpldeltlen[seg] = get_bits(gb, 4);
1661
                    ctx->cpldeltba[seg] = get_bits(gb, 3);
1662
                }
1663
            }
1664

    
1665
        for (i = 0; i < nfchans; i++)
1666
            if (ctx->deltbae[i] == DBA_NEW) {/*channel delta offset, len and bit allocation */
1667
                ctx->deltnseg[i] = get_bits(gb, 3);
1668
                for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
1669
                    ctx->deltoffst[i][seg] = get_bits(gb, 5);
1670
                    ctx->deltlen[i][seg] = get_bits(gb, 4);
1671
                    ctx->deltba[i][seg] = get_bits(gb, 3);
1672
                }
1673
            }
1674
    }
1675

    
1676
    if (bit_alloc_flags) {
1677
        if (is_snr_offsets_zero(ctx)) {
1678
            memset(ctx->cplbap, 0, sizeof (ctx->cplbap));
1679
            memset(ctx->lfebap, 0, sizeof (ctx->lfebap));
1680
            for (i = 0; i < nfchans; i++)
1681
                memset(ctx->bap[i], 0, sizeof(ctx->bap[i]));
1682
        } else {
1683
            /* set bit allocation parameters */
1684
            ctx->bit_alloc_params.fscod = ctx->fscod;
1685
            ctx->bit_alloc_params.halfratecod = 0;
1686
            ctx->bit_alloc_params.sdecay = ff_sdecaytab[ctx->sdcycod];
1687
            ctx->bit_alloc_params.fdecay = ff_fdecaytab[ctx->fdcycod];
1688
            ctx->bit_alloc_params.sgain = ff_sgaintab[ctx->sgaincod];
1689
            ctx->bit_alloc_params.dbknee = ff_dbkneetab[ctx->dbpbcod];
1690
            ctx->bit_alloc_params.floor = ff_floortab[ctx->floorcod];
1691
            ctx->bit_alloc_params.cplfleak = ctx->cplfleak;
1692
            ctx->bit_alloc_params.cplsleak = ctx->cplsleak;
1693

    
1694
            if (ctx->chincpl && (bit_alloc_flags & 64))
1695
                do_bit_allocation(ctx, 5);
1696
            for (i = 0; i < nfchans; i++)
1697
                if ((bit_alloc_flags >> i) & 1)
1698
                    do_bit_allocation(ctx, i);
1699
            if (ctx->lfeon && (bit_alloc_flags & 32))
1700
                do_bit_allocation(ctx, 6);
1701
        }
1702
    }
1703

    
1704
    if (get_bits1(gb)) { /* unused dummy data */
1705
        skipl = get_bits(gb, 9);
1706
        while(skipl--)
1707
            skip_bits(gb, 8);
1708
    }
1709
    /* unpack the transform coefficients
1710
     * * this also uncouples channels if coupling is in use.
1711
     */
1712
    if (get_transform_coeffs(ctx)) {
1713
        av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1714
        return -1;
1715
    }
1716

    
1717
    /* recover coefficients if rematrixing is in use */
1718
    if (ctx->rematflg)
1719
        do_rematrixing(ctx);
1720

    
1721
    do_downmix(ctx);
1722

    
1723
    do_imdct(ctx);
1724

    
1725
    return 0;
1726
}
1727

    
1728
static inline int16_t convert(int32_t i)
1729
{
1730
    if (i > 0x43c07fff)
1731
        return 32767;
1732
    else if (i <= 0x43bf8000)
1733
        return -32768;
1734
    else
1735
        return (i - 0x43c00000);
1736
}
1737

    
1738
/* Decode ac3 frame.
1739
 *
1740
 * @param avctx Pointer to AVCodecContext
1741
 * @param data Pointer to pcm smaples
1742
 * @param data_size Set to number of pcm samples produced by decoding
1743
 * @param buf Data to be decoded
1744
 * @param buf_size Size of the buffer
1745
 */
1746
static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1747
{
1748
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1749
    int frame_start;
1750
    int16_t *out_samples = (int16_t *)data;
1751
    int i, j, k, start;
1752
    int32_t *int_ptr[6];
1753

    
1754
    for (i = 0; i < 6; i++)
1755
        int_ptr[i] = (int32_t *)(&ctx->output[i]);
1756

    
1757
    //Synchronize the frame.
1758
    frame_start = ac3_synchronize(buf, buf_size);
1759
    if (frame_start == -1) {
1760
        av_log(avctx, AV_LOG_ERROR, "frame is not synchronized\n");
1761
        *data_size = 0;
1762
        return buf_size;
1763
    }
1764

    
1765
    //Initialize the GetBitContext with the start of valid AC3 Frame.
1766
    init_get_bits(&(ctx->gb), buf + frame_start, (buf_size - frame_start) * 8);
1767

    
1768
    //Parse the syncinfo.
1769
    //If 'fscod' or 'bsid' is not valid the decoder shall mute as per the standard.
1770
    if (!ac3_parse_sync_info(ctx)) {
1771
        av_log(avctx, AV_LOG_ERROR, "\n");
1772
        *data_size = 0;
1773
        return buf_size;
1774
    }
1775

    
1776
    //Parse the BSI.
1777
    //If 'bsid' is not valid decoder shall not decode the audio as per the standard.
1778
    ac3_parse_bsi(ctx);
1779

    
1780
    avctx->sample_rate = ctx->sampling_rate;
1781
    avctx->bit_rate = ctx->bit_rate;
1782

    
1783
    if (avctx->channels == 0) {
1784
        ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1785
        if (ctx->lfeon)
1786
            ctx->blkoutput |= AC3_OUTPUT_LFEON;
1787
        avctx->channels = ctx->nfchans + ctx->lfeon;
1788
    }
1789
    else if (avctx->channels == 1)
1790
        ctx->blkoutput |= AC3_OUTPUT_MONO;
1791
    else if (avctx->channels == 2) {
1792
        if (ctx->dsurmod == 0x02)
1793
            ctx->blkoutput |= AC3_OUTPUT_DOLBY;
1794
        else
1795
            ctx->blkoutput |= AC3_OUTPUT_STEREO;
1796
    }
1797
    else {
1798
        if (avctx->channels < (ctx->nfchans + ctx->lfeon))
1799
            av_log(avctx, AV_LOG_INFO, "ac3_decoder: AC3 Source Channels Are Less Then Specified %d: Output to %d Channels\n",avctx->channels, ctx->nfchans + ctx->lfeon);
1800
        ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1801
        if (ctx->lfeon)
1802
            ctx->blkoutput |= AC3_OUTPUT_LFEON;
1803
        avctx->channels = ctx->nfchans + ctx->lfeon;
1804
    }
1805

    
1806
    //av_log(avctx, AV_LOG_INFO, "channels = %d \t bit rate = %d \t sampling rate = %d \n", avctx->channels, avctx->bit_rate * 1000, avctx->sample_rate);
1807

    
1808
    //Parse the Audio Blocks.
1809
    for (i = 0; i < NB_BLOCKS; i++) {
1810
        if (ac3_parse_audio_block(ctx)) {
1811
            av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1812
            *data_size = 0;
1813
            return ctx->frame_size;
1814
        }
1815
        start = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1;
1816
        for (k = 0; k < BLOCK_SIZE; k++)
1817
            for (j = start; j <= avctx->channels; j++)
1818
                *(out_samples++) = convert(int_ptr[j][k]);
1819
    }
1820
    *data_size = NB_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);
1821
    return ctx->frame_size;
1822
}
1823

    
1824
/* Uninitialize ac3 decoder.
1825
 */
1826
static int ac3_decode_end(AVCodecContext *avctx)
1827
{
1828
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1829
    ff_mdct_end(&ctx->imdct_512);
1830
    ff_mdct_end(&ctx->imdct_256);
1831

    
1832
    return 0;
1833
}
1834

    
1835
AVCodec ac3_decoder = {
1836
    .name = "ac3",
1837
    .type = CODEC_TYPE_AUDIO,
1838
    .id = CODEC_ID_AC3,
1839
    .priv_data_size = sizeof (AC3DecodeContext),
1840
    .init = ac3_decode_init,
1841
    .close = ac3_decode_end,
1842
    .decode = ac3_decode_frame,
1843
};
1844