Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3dec.c @ bec37145

History | View | Annotate | Download (55.3 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
 * Copyright (c) 2007 Justin Ruggles
7
 *
8
 * Portions of this code are derived from liba52
9
 * http://liba52.sourceforge.net
10
 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
11
 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
12
 *
13
 * This file is part of FFmpeg.
14
 *
15
 * FFmpeg is free software; you can redistribute it and/or
16
 * modify it under the terms of the GNU General Public
17
 * License as published by the Free Software Foundation; either
18
 * version 2 of the License, or (at your option) any later version.
19
 *
20
 * FFmpeg is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23
 * General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU General Public
26
 * License along with FFmpeg; if not, write to the Free Software
27
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28
 */
29

    
30
#include <stdio.h>
31
#include <stddef.h>
32
#include <math.h>
33
#include <string.h>
34

    
35
#include "avcodec.h"
36
#include "ac3_parser.h"
37
#include "bitstream.h"
38
#include "dsputil.h"
39
#include "random.h"
40

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

    
43
/* table for exponent to scale_factor mapping
44
 * scale_factor[i] = 2 ^ -(i + 15)
45
 */
46
static float scale_factors[25];
47

    
48
/** table for grouping exponents */
49
static uint8_t exp_ungroup_tbl[128][3];
50

    
51
static int16_t l3_quantizers_1[32];
52
static int16_t l3_quantizers_2[32];
53
static int16_t l3_quantizers_3[32];
54

    
55
static int16_t l5_quantizers_1[128];
56
static int16_t l5_quantizers_2[128];
57
static int16_t l5_quantizers_3[128];
58

    
59
static int16_t l7_quantizers[7];
60

    
61
static int16_t l11_quantizers_1[128];
62
static int16_t l11_quantizers_2[128];
63

    
64
static int16_t l15_quantizers[15];
65

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

    
68
/* Adjustmens in dB gain */
69
#define LEVEL_MINUS_3DB         0.7071067811865476
70
#define LEVEL_MINUS_4POINT5DB   0.5946035575013605
71
#define LEVEL_MINUS_6DB         0.5000000000000000
72
#define LEVEL_PLUS_3DB          1.4142135623730951
73
#define LEVEL_PLUS_6DB          2.0000000000000000
74
#define LEVEL_ZERO              0.0000000000000000
75

    
76
static const float clevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB,
77
    LEVEL_MINUS_6DB, LEVEL_MINUS_4POINT5DB };
78

    
79
static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB };
80

    
81
#define BLOCK_SIZE    256
82

    
83
/* Output and input configurations. */
84
#define AC3_OUTPUT_UNMODIFIED   0x01
85
#define AC3_OUTPUT_MONO         0x02
86
#define AC3_OUTPUT_STEREO       0x04
87
#define AC3_OUTPUT_DOLBY        0x08
88
#define AC3_OUTPUT_LFEON        0x10
89

    
90
typedef struct {
91
    uint8_t  acmod;
92
    uint8_t  cmixlev;
93
    uint8_t  surmixlev;
94
    uint8_t  dsurmod;
95

    
96
    uint8_t  blksw;
97
    uint8_t  dithflag;
98
    uint8_t  cplinu;
99
    uint8_t  chincpl;
100
    uint8_t  phsflginu;
101
    uint8_t  cplbegf;
102
    uint8_t  cplendf;
103
    uint8_t  cplcoe;
104
    uint32_t cplbndstrc;
105
    uint8_t  rematstr;
106
    uint8_t  rematflg;
107
    uint8_t  cplexpstr;
108
    uint8_t  lfeexpstr;
109
    uint8_t  chexpstr[5];
110
    uint8_t  sdcycod;
111
    uint8_t  fdcycod;
112
    uint8_t  sgaincod;
113
    uint8_t  dbpbcod;
114
    uint8_t  floorcod;
115
    uint8_t  csnroffst;
116
    uint8_t  cplfsnroffst;
117
    uint8_t  cplfgaincod;
118
    uint8_t  fsnroffst[5];
119
    uint8_t  fgaincod[5];
120
    uint8_t  lfefsnroffst;
121
    uint8_t  lfefgaincod;
122
    uint8_t  cplfleak;
123
    uint8_t  cplsleak;
124
    uint8_t  cpldeltbae;
125
    uint8_t  deltbae[5];
126
    uint8_t  cpldeltnseg;
127
    uint8_t  cpldeltoffst[8];
128
    uint8_t  cpldeltlen[8];
129
    uint8_t  cpldeltba[8];
130
    uint8_t  deltnseg[5];
131
    uint8_t  deltoffst[5][8];
132
    uint8_t  deltlen[5][8];
133
    uint8_t  deltba[5][8];
134

    
135
    /* Derived Attributes. */
136
    int      sampling_rate;
137
    int      bit_rate;
138
    int      frame_size;
139

    
140
    int      nchans;            //number of total channels
141
    int      nfchans;           //number of full-bandwidth channels
142
    int      lfeon;             //lfe channel in use
143

    
144
    float    dynrng;            //dynamic range gain
145
    float    dynrng2;           //dynamic range gain for 1+1 mode
146
    float    chcoeffs[6];       //normalized channel coefficients
147
    float    cplco[5][18];      //coupling coordinates
148
    int      ncplbnd;           //number of coupling bands
149
    int      ncplsubnd;         //number of coupling sub bands
150
    int      cplstrtmant;       //coupling start mantissa
151
    int      cplendmant;        //coupling end mantissa
152
    int      endmant[5];        //channel end mantissas
153
    AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
154

    
155
    uint8_t  dcplexps[256];     //decoded coupling exponents
156
    uint8_t  dexps[5][256];     //decoded fbw channel exponents
157
    uint8_t  dlfeexps[256];     //decoded lfe channel exponents
158
    uint8_t  cplbap[256];       //coupling bit allocation pointers
159
    uint8_t  bap[5][256];       //fbw channel bit allocation pointers
160
    uint8_t  lfebap[256];       //lfe channel bit allocation pointers
161

    
162
    int      blkoutput;         //output configuration for block
163

    
164
    DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][BLOCK_SIZE]);  //transform coefficients
165

    
166
    /* For IMDCT. */
167
    MDCTContext imdct_512;  //for 512 sample imdct transform
168
    MDCTContext imdct_256;  //for 256 sample imdct transform
169
    DSPContext  dsp;        //for optimization
170

    
171
    DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS][BLOCK_SIZE]);    //output after imdct transform and windowing
172
    DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS][BLOCK_SIZE]);     //delay - added to the next block
173
    DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]);               //temporary storage for imdct transform
174
    DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]);          //temporary storage for output before windowing
175
    DECLARE_ALIGNED_16(float, window[BLOCK_SIZE]);                  //window coefficients
176

    
177
    /* Miscellaneous. */
178
    GetBitContext gb;
179
    AVRandomState dith_state;   //for dither generation
180
} AC3DecodeContext;
181

    
182
/*********** BEGIN INIT HELPER FUNCTIONS ***********/
183
/**
184
 * Generate a Kaiser-Bessel Derived Window.
185
 */
186
static void ac3_window_init(float *window)
187
{
188
   int i, j;
189
   double sum = 0.0, bessel, tmp;
190
   double local_window[256];
191
   double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
192

    
193
   for (i = 0; i < 256; i++) {
194
       tmp = i * (256 - i) * alpha2;
195
       bessel = 1.0;
196
       for (j = 100; j > 0; j--) /* defaul to 100 iterations */
197
           bessel = bessel * tmp / (j * j) + 1;
198
       sum += bessel;
199
       local_window[i] = sum;
200
   }
201

    
202
   sum++;
203
   for (i = 0; i < 256; i++)
204
       window[i] = sqrt(local_window[i] / sum);
205
}
206

    
207
/*
208
 * Generate quantizer tables.
209
 */
210
static void generate_quantizers_table(int16_t quantizers[], int level, int length)
211
{
212
    int i;
213

    
214
    for (i = 0; i < length; i++)
215
        quantizers[i] = ((2 * i - level + 1) << 15) / level;
216
}
217

    
218
static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
219
{
220
    int i, j;
221
    int16_t v;
222

    
223
    for (i = 0; i < length1; i++) {
224
        v = ((2 * i - level + 1) << 15) / level;
225
        for (j = 0; j < length2; j++)
226
            quantizers[i * length2 + j] = v;
227
    }
228

    
229
    for (i = length1 * length2; i < size; i++)
230
        quantizers[i] = 0;
231
}
232

    
233
static void generate_quantizers_table_2(int16_t quantizers[], int level, int length1, int length2, int size)
234
{
235
    int i, j;
236
    int16_t v;
237

    
238
    for (i = 0; i < length1; i++) {
239
        v = ((2 * (i % level) - level + 1) << 15) / level;
240
        for (j = 0; j < length2; j++)
241
            quantizers[i * length2 + j] = v;
242
    }
243

    
244
    for (i = length1 * length2; i < size; i++)
245
        quantizers[i] = 0;
246

    
247
}
248

    
249
static void generate_quantizers_table_3(int16_t quantizers[], int level, int length1, int length2, int size)
250
{
251
    int i, j;
252

    
253
    for (i = 0; i < length1; i++)
254
        for (j = 0; j < length2; j++)
255
            quantizers[i * length2 + j] = ((2 * (j % level) - level + 1) << 15) / level;
256

    
257
    for (i = length1 * length2; i < size; i++)
258
        quantizers[i] = 0;
259
}
260

    
261
/*
262
 * Initialize tables at runtime.
263
 */
264
static void ac3_tables_init(void)
265
{
266
    int i;
267

    
268
    /* Quantizer ungrouping tables. */
269
    // for level-3 quantizers
270
    generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
271
    generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
272
    generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
273

    
274
    //for level-5 quantizers
275
    generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
276
    generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
277
    generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
278

    
279
    //for level-7 quantizers
280
    generate_quantizers_table(l7_quantizers, 7, 7);
281

    
282
    //for level-4 quantizers
283
    generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
284
    generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
285

    
286
    //for level-15 quantizers
287
    generate_quantizers_table(l15_quantizers, 15, 15);
288
    /* End Quantizer ungrouping tables. */
289

    
290
    //generate scale factors
291
    for (i = 0; i < 25; i++)
292
        scale_factors[i] = pow(2.0, -(i + 15));
293

    
294
    /* generate exponent tables
295
       reference: Section 7.1.3 Exponent Decoding */
296
    for(i=0; i<128; i++) {
297
        exp_ungroup_tbl[i][0] =  i / 25;
298
        exp_ungroup_tbl[i][1] = (i % 25) / 5;
299
        exp_ungroup_tbl[i][2] = (i % 25) % 5;
300
    }
301
}
302

    
303

    
304
static int ac3_decode_init(AVCodecContext *avctx)
305
{
306
    AC3DecodeContext *ctx = avctx->priv_data;
307

    
308
    ac3_common_init();
309
    ac3_tables_init();
310
    ff_mdct_init(&ctx->imdct_256, 8, 1);
311
    ff_mdct_init(&ctx->imdct_512, 9, 1);
312
    ac3_window_init(ctx->window);
313
    dsputil_init(&ctx->dsp, avctx);
314
    av_init_random(0, &ctx->dith_state);
315

    
316
    return 0;
317
}
318
/*********** END INIT FUNCTIONS ***********/
319

    
320
/**
321
 * Parses the 'sync info' and 'bit stream info' from the AC-3 bitstream.
322
 * GetBitContext within AC3DecodeContext must point to
323
 * start of the synchronized ac3 bitstream.
324
 */
325
static int ac3_parse_header(AC3DecodeContext *ctx)
326
{
327
    AC3HeaderInfo hdr;
328
    GetBitContext *gb = &ctx->gb;
329
    int err, i;
330

    
331
    err = ff_ac3_parse_header(gb->buffer, &hdr);
332
    if(err)
333
        return err;
334

    
335
    /* get decoding parameters from header info */
336
    ctx->bit_alloc_params.fscod       = hdr.fscod;
337
    ctx->acmod                        = hdr.acmod;
338
    ctx->cmixlev                      = hdr.cmixlev;
339
    ctx->surmixlev                    = hdr.surmixlev;
340
    ctx->dsurmod                      = hdr.dsurmod;
341
    ctx->lfeon                        = hdr.lfeon;
342
    ctx->bit_alloc_params.halfratecod = hdr.halfratecod;
343
    ctx->sampling_rate                = hdr.sample_rate;
344
    ctx->bit_rate                     = hdr.bit_rate;
345
    ctx->nchans                       = hdr.channels;
346
    ctx->nfchans                      = ctx->nchans - ctx->lfeon;
347
    ctx->frame_size                   = hdr.frame_size;
348
    ctx->blkoutput                    = nfchans_tbl[ctx->acmod];
349
    if(ctx->lfeon)
350
        ctx->blkoutput |= AC3_OUTPUT_LFEON;
351

    
352
    /* skip over portion of header which has already been read */
353
    skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
354
    skip_bits(gb, 16); // skip crc1
355
    skip_bits(gb, 8);  // skip fscod and frmsizecod
356
    skip_bits(gb, 11); // skip bsid, bsmod, and acmod
357
    if(ctx->acmod == AC3_ACMOD_STEREO) {
358
        skip_bits(gb, 2); // skip dsurmod
359
    } else {
360
        if((ctx->acmod & 1) && ctx->acmod != AC3_ACMOD_MONO)
361
            skip_bits(gb, 2); // skip cmixlev
362
        if(ctx->acmod & 4)
363
            skip_bits(gb, 2); // skip surmixlev
364
    }
365
    skip_bits1(gb); // skip lfeon
366

    
367
    /* read the rest of the bsi. read twice for dual mono mode. */
368
    i = !(ctx->acmod);
369
    do {
370
        skip_bits(gb, 5); //skip dialog normalization
371
        if (get_bits1(gb))
372
            skip_bits(gb, 8); //skip compression
373
        if (get_bits1(gb))
374
            skip_bits(gb, 8); //skip language code
375
        if (get_bits1(gb))
376
            skip_bits(gb, 7); //skip audio production information
377
    } while (i--);
378

    
379
    skip_bits(gb, 2); //skip copyright bit and original bitstream bit
380

    
381
    /* FIXME: read & use the xbsi1 downmix levels */
382
    if (get_bits1(gb))
383
        skip_bits(gb, 14); //skip timecode1
384
    if (get_bits1(gb))
385
        skip_bits(gb, 14); //skip timecode2
386

    
387
    if (get_bits1(gb)) {
388
        i = get_bits(gb, 6); //additional bsi length
389
        do {
390
            skip_bits(gb, 8);
391
        } while(i--);
392
    }
393

    
394
    return 0;
395
}
396

    
397
/**
398
 * Decodes the grouped exponents.
399
 * This function decodes the coded exponents according to exponent strategy
400
 * and stores them in the decoded exponents buffer.
401
 *
402
 * @param[in]  gb      GetBitContext which points to start of coded exponents
403
 * @param[in]  expstr  Exponent coding strategy
404
 * @param[in]  ngrps   Number of grouped exponents
405
 * @param[in]  absexp  Absolute exponent or DC exponent
406
 * @param[out] dexps   Decoded exponents are stored in dexps
407
 */
408
static void decode_exponents(GetBitContext *gb, int expstr, int ngrps,
409
                             uint8_t absexp, uint8_t *dexps)
410
{
411
    int i, j, grp, grpsize;
412
    int dexp[256];
413
    int expacc, prevexp;
414

    
415
    /* unpack groups */
416
    grpsize = expstr + (expstr == EXP_D45);
417
    for(grp=0,i=0; grp<ngrps; grp++) {
418
        expacc = get_bits(gb, 7);
419
        dexp[i++] = exp_ungroup_tbl[expacc][0];
420
        dexp[i++] = exp_ungroup_tbl[expacc][1];
421
        dexp[i++] = exp_ungroup_tbl[expacc][2];
422
    }
423

    
424
    /* convert to absolute exps and expand groups */
425
    prevexp = absexp;
426
    for(i=0; i<ngrps*3; i++) {
427
        prevexp = av_clip(prevexp + dexp[i]-2, 0, 24);
428
        for(j=0; j<grpsize; j++) {
429
            dexps[(i*grpsize)+j] = prevexp;
430
        }
431
    }
432
}
433

    
434
/* Performs bit allocation.
435
 * This function performs bit allocation for the requested chanenl.
436
 */
437
static void do_bit_allocation(AC3DecodeContext *ctx, int chnl)
438
{
439
    int fgain, snroffset;
440

    
441
    if (chnl == 5) {
442
        fgain = ff_fgaintab[ctx->cplfgaincod];
443
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->cplfsnroffst) << 2;
444
        ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->cplbap,
445
                                      ctx->dcplexps, ctx->cplstrtmant,
446
                                      ctx->cplendmant, snroffset, fgain, 0,
447
                                      ctx->cpldeltbae, ctx->cpldeltnseg,
448
                                      ctx->cpldeltoffst, ctx->cpldeltlen,
449
                                      ctx->cpldeltba);
450
    }
451
    else if (chnl == 6) {
452
        fgain = ff_fgaintab[ctx->lfefgaincod];
453
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->lfefsnroffst) << 2;
454
        ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->lfebap,
455
                                      ctx->dlfeexps, 0, 7, snroffset, fgain, 1,
456
                                      DBA_NONE, 0, NULL, NULL, NULL);
457
    }
458
    else {
459
        fgain = ff_fgaintab[ctx->fgaincod[chnl]];
460
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->fsnroffst[chnl]) << 2;
461
        ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->bap[chnl],
462
                                      ctx->dexps[chnl], 0, ctx->endmant[chnl],
463
                                      snroffset, fgain, 0, ctx->deltbae[chnl],
464
                                      ctx->deltnseg[chnl], ctx->deltoffst[chnl],
465
                                      ctx->deltlen[chnl], ctx->deltba[chnl]);
466
    }
467
}
468

    
469
typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
470
    int16_t l3_quantizers[3];
471
    int16_t l5_quantizers[3];
472
    int16_t l11_quantizers[2];
473
    int l3ptr;
474
    int l5ptr;
475
    int l11ptr;
476
} mant_groups;
477

    
478
/* Get the transform coefficients for coupling channel and uncouple channels.
479
 * The coupling transform coefficients starts at the the cplstrtmant, which is
480
 * equal to endmant[ch] for fbw channels. Hence we can uncouple channels before
481
 * getting transform coefficients for the channel.
482
 */
483
static int get_transform_coeffs_cpling(AC3DecodeContext *ctx, mant_groups *m)
484
{
485
    GetBitContext *gb = &ctx->gb;
486
    int ch, start, end, cplbndstrc, bnd, gcode, tbap;
487
    float cplcos[5], cplcoeff;
488
    uint8_t *exps = ctx->dcplexps;
489
    uint8_t *bap = ctx->cplbap;
490

    
491
    cplbndstrc = ctx->cplbndstrc;
492
    start = ctx->cplstrtmant;
493
    bnd = 0;
494

    
495
    while (start < ctx->cplendmant) {
496
        end = start + 12;
497
        while (cplbndstrc & 1) {
498
            end += 12;
499
            cplbndstrc >>= 1;
500
        }
501
        cplbndstrc >>= 1;
502
        for (ch = 0; ch < ctx->nfchans; ch++)
503
            cplcos[ch] = ctx->chcoeffs[ch] * ctx->cplco[ch][bnd];
504
        bnd++;
505

    
506
        while (start < end) {
507
            tbap = bap[start];
508
            switch(tbap) {
509
                case 0:
510
                    for (ch = 0; ch < ctx->nfchans; ch++)
511
                        if (((ctx->chincpl) >> ch) & 1) {
512
                            if ((ctx->dithflag >> ch) & 1) {
513
                                cplcoeff = (av_random(&ctx->dith_state) & 0xFFFF) * scale_factors[exps[start]];
514
                                ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch] * LEVEL_MINUS_3DB;
515
                            } else
516
                                ctx->transform_coeffs[ch + 1][start] = 0;
517
                        }
518
                    start++;
519
                    continue;
520
                case 1:
521
                    if (m->l3ptr > 2) {
522
                        gcode = get_bits(gb, 5);
523
                        m->l3_quantizers[0] = l3_quantizers_1[gcode];
524
                        m->l3_quantizers[1] = l3_quantizers_2[gcode];
525
                        m->l3_quantizers[2] = l3_quantizers_3[gcode];
526
                        m->l3ptr = 0;
527
                    }
528
                    cplcoeff = m->l3_quantizers[m->l3ptr++] * scale_factors[exps[start]];
529
                    break;
530

    
531
                case 2:
532
                    if (m->l5ptr > 2) {
533
                        gcode = get_bits(gb, 7);
534
                        m->l5_quantizers[0] = l5_quantizers_1[gcode];
535
                        m->l5_quantizers[1] = l5_quantizers_2[gcode];
536
                        m->l5_quantizers[2] = l5_quantizers_3[gcode];
537
                        m->l5ptr = 0;
538
                    }
539
                    cplcoeff = m->l5_quantizers[m->l5ptr++] * scale_factors[exps[start]];
540
                    break;
541

    
542
                case 3:
543
                    cplcoeff = l7_quantizers[get_bits(gb, 3)] * scale_factors[exps[start]];
544
                    break;
545

    
546
                case 4:
547
                    if (m->l11ptr > 1) {
548
                        gcode = get_bits(gb, 7);
549
                        m->l11_quantizers[0] = l11_quantizers_1[gcode];
550
                        m->l11_quantizers[1] = l11_quantizers_2[gcode];
551
                        m->l11ptr = 0;
552
                    }
553
                    cplcoeff = m->l11_quantizers[m->l11ptr++] * scale_factors[exps[start]];
554
                    break;
555

    
556
                case 5:
557
                    cplcoeff = l15_quantizers[get_bits(gb, 4)] * scale_factors[exps[start]];
558
                    break;
559

    
560
                default:
561
                    cplcoeff = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * scale_factors[exps[start]];
562
            }
563
            for (ch = 0; ch < ctx->nfchans; ch++)
564
                if ((ctx->chincpl >> ch) & 1)
565
                    ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
566
            start++;
567
        }
568
    }
569

    
570
    return 0;
571
}
572

    
573
/* Get the transform coefficients for particular channel */
574
static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
575
{
576
    GetBitContext *gb = &ctx->gb;
577
    int i, gcode, tbap, dithflag, end;
578
    uint8_t *exps;
579
    uint8_t *bap;
580
    float *coeffs;
581
    float factors[25];
582

    
583
    for (i = 0; i < 25; i++)
584
        factors[i] = scale_factors[i] * ctx->chcoeffs[ch_index];
585

    
586
    if (ch_index != -1) { /* fbw channels */
587
        dithflag = (ctx->dithflag >> ch_index) & 1;
588
        exps = ctx->dexps[ch_index];
589
        bap = ctx->bap[ch_index];
590
        coeffs = ctx->transform_coeffs[ch_index + 1];
591
        end = ctx->endmant[ch_index];
592
    } else if (ch_index == -1) {
593
        dithflag = 0;
594
        exps = ctx->dlfeexps;
595
        bap = ctx->lfebap;
596
        coeffs = ctx->transform_coeffs[0];
597
        end = 7;
598
    }
599

    
600

    
601
    for (i = 0; i < end; i++) {
602
        tbap = bap[i];
603
        switch (tbap) {
604
            case 0:
605
                if (!dithflag) {
606
                    coeffs[i] = 0;
607
                    continue;
608
                }
609
                else {
610
                    coeffs[i] = (av_random(&ctx->dith_state) & 0xFFFF) * factors[exps[i]];
611
                    coeffs[i] *= LEVEL_MINUS_3DB;
612
                    continue;
613
                }
614

    
615
            case 1:
616
                if (m->l3ptr > 2) {
617
                    gcode = get_bits(gb, 5);
618
                    m->l3_quantizers[0] = l3_quantizers_1[gcode];
619
                    m->l3_quantizers[1] = l3_quantizers_2[gcode];
620
                    m->l3_quantizers[2] = l3_quantizers_3[gcode];
621
                    m->l3ptr = 0;
622
                }
623
                coeffs[i] = m->l3_quantizers[m->l3ptr++] * factors[exps[i]];
624
                continue;
625

    
626
            case 2:
627
                if (m->l5ptr > 2) {
628
                    gcode = get_bits(gb, 7);
629
                    m->l5_quantizers[0] = l5_quantizers_1[gcode];
630
                    m->l5_quantizers[1] = l5_quantizers_2[gcode];
631
                    m->l5_quantizers[2] = l5_quantizers_3[gcode];
632
                    m->l5ptr = 0;
633
                }
634
                coeffs[i] = m->l5_quantizers[m->l5ptr++] * factors[exps[i]];
635
                continue;
636

    
637
            case 3:
638
                coeffs[i] = l7_quantizers[get_bits(gb, 3)] * factors[exps[i]];
639
                continue;
640

    
641
            case 4:
642
                if (m->l11ptr > 1) {
643
                    gcode = get_bits(gb, 7);
644
                    m->l11_quantizers[0] = l11_quantizers_1[gcode];
645
                    m->l11_quantizers[1] = l11_quantizers_2[gcode];
646
                    m->l11ptr = 0;
647
                }
648
                coeffs[i] = m->l11_quantizers[m->l11ptr++] * factors[exps[i]];
649
                continue;
650

    
651
            case 5:
652
                coeffs[i] = l15_quantizers[get_bits(gb, 4)] * factors[exps[i]];
653
                continue;
654

    
655
            default:
656
                coeffs[i] = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * factors[exps[i]];
657
                continue;
658
        }
659
    }
660

    
661
    return 0;
662
}
663

    
664
/* Get the transform coefficients.
665
 * This function extracts the tranform coefficients form the ac3 bitstream.
666
 * This function is called after bit allocation is performed.
667
 */
668
static int get_transform_coeffs(AC3DecodeContext * ctx)
669
{
670
    int i, end;
671
    int got_cplchan = 0;
672
    mant_groups m;
673

    
674
    m.l3ptr = m.l5ptr = m.l11ptr = 3;
675

    
676
    for (i = 0; i < ctx->nfchans; i++) {
677
        /* transform coefficients for individual channel */
678
        if (get_transform_coeffs_ch(ctx, i, &m))
679
            return -1;
680
        /* tranform coefficients for coupling channels */
681
        if ((ctx->chincpl >> i) & 1)  {
682
            if (!got_cplchan) {
683
                if (get_transform_coeffs_cpling(ctx, &m)) {
684
                    av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
685
                    return -1;
686
                }
687
                got_cplchan = 1;
688
            }
689
            end = ctx->cplendmant;
690
        } else
691
            end = ctx->endmant[i];
692
        do
693
            ctx->transform_coeffs[i + 1][end] = 0;
694
        while(++end < 256);
695
    }
696
    if (ctx->lfeon) {
697
        if (get_transform_coeffs_ch(ctx, -1, &m))
698
                return -1;
699
        for (i = 7; i < 256; i++) {
700
            ctx->transform_coeffs[0][i] = 0;
701
        }
702
    }
703

    
704
    return 0;
705
}
706

    
707
/* Rematrixing routines. */
708
static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
709
{
710
    float tmp0, tmp1;
711

    
712
    while (start < end) {
713
        tmp0 = ctx->transform_coeffs[1][start];
714
        tmp1 = ctx->transform_coeffs[2][start];
715
        ctx->transform_coeffs[1][start] = tmp0 + tmp1;
716
        ctx->transform_coeffs[2][start] = tmp0 - tmp1;
717
        start++;
718
    }
719
}
720

    
721
static void do_rematrixing(AC3DecodeContext *ctx)
722
{
723
    int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
724
    int end, bndend;
725

    
726
    end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
727

    
728
    if (ctx->rematflg & 1)
729
        do_rematrixing1(ctx, bnd1, bnd2);
730

    
731
    if (ctx->rematflg & 2)
732
        do_rematrixing1(ctx, bnd2, bnd3);
733

    
734
    bndend = bnd4;
735
    if (bndend > end) {
736
        bndend = end;
737
        if (ctx->rematflg & 4)
738
            do_rematrixing1(ctx, bnd3, bndend);
739
    } else {
740
        if (ctx->rematflg & 4)
741
            do_rematrixing1(ctx, bnd3, bnd4);
742
        if (ctx->rematflg & 8)
743
            do_rematrixing1(ctx, bnd4, end);
744
    }
745
}
746

    
747
/* This function sets the normalized channel coefficients.
748
 * Transform coefficients are multipllied by the channel
749
 * coefficients to get normalized transform coefficients.
750
 */
751
static void get_downmix_coeffs(AC3DecodeContext *ctx)
752
{
753
    int from = ctx->acmod;
754
    int to = ctx->blkoutput;
755
    float clev = clevs[ctx->cmixlev];
756
    float slev = slevs[ctx->surmixlev];
757
    float nf = 1.0; //normalization factor for downmix coeffs
758
    int i;
759

    
760
    if (!ctx->acmod) {
761
        ctx->chcoeffs[0] = 2 * ctx->dynrng;
762
        ctx->chcoeffs[1] = 2 * ctx->dynrng2;
763
    } else {
764
        for (i = 0; i < ctx->nfchans; i++)
765
            ctx->chcoeffs[i] = 2 * ctx->dynrng;
766
    }
767

    
768
    if (to == AC3_OUTPUT_UNMODIFIED)
769
        return;
770

    
771
    switch (from) {
772
        case AC3_ACMOD_DUALMONO:
773
            switch (to) {
774
                case AC3_OUTPUT_MONO:
775
                case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */
776
                    nf = 0.5;
777
                    ctx->chcoeffs[0] *= nf;
778
                    ctx->chcoeffs[1] *= nf;
779
                    break;
780
            }
781
            break;
782
        case AC3_ACMOD_MONO:
783
            switch (to) {
784
                case AC3_OUTPUT_STEREO:
785
                    nf = LEVEL_MINUS_3DB;
786
                    ctx->chcoeffs[0] *= nf;
787
                    break;
788
            }
789
            break;
790
        case AC3_ACMOD_STEREO:
791
            switch (to) {
792
                case AC3_OUTPUT_MONO:
793
                    nf = LEVEL_MINUS_3DB;
794
                    ctx->chcoeffs[0] *= nf;
795
                    ctx->chcoeffs[1] *= nf;
796
                    break;
797
            }
798
            break;
799
        case AC3_ACMOD_3F:
800
            switch (to) {
801
                case AC3_OUTPUT_MONO:
802
                    nf = LEVEL_MINUS_3DB / (1.0 + clev);
803
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
804
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
805
                    ctx->chcoeffs[1] *= ((nf * clev * LEVEL_MINUS_3DB) / 2.0);
806
                    break;
807
                case AC3_OUTPUT_STEREO:
808
                    nf = 1.0 / (1.0 + clev);
809
                    ctx->chcoeffs[0] *= nf;
810
                    ctx->chcoeffs[2] *= nf;
811
                    ctx->chcoeffs[1] *= (nf * clev);
812
                    break;
813
            }
814
            break;
815
        case AC3_ACMOD_2F1R:
816
            switch (to) {
817
                case AC3_OUTPUT_MONO:
818
                    nf = 2.0 * LEVEL_MINUS_3DB / (2.0 + slev);
819
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
820
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
821
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
822
                    break;
823
                case AC3_OUTPUT_STEREO:
824
                    nf = 1.0 / (1.0 + (slev * LEVEL_MINUS_3DB));
825
                    ctx->chcoeffs[0] *= nf;
826
                    ctx->chcoeffs[1] *= nf;
827
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
828
                    break;
829
                case AC3_OUTPUT_DOLBY:
830
                    nf = 1.0 / (1.0 + LEVEL_MINUS_3DB);
831
                    ctx->chcoeffs[0] *= nf;
832
                    ctx->chcoeffs[1] *= nf;
833
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
834
                    break;
835
            }
836
            break;
837
        case AC3_ACMOD_3F1R:
838
            switch (to) {
839
                case AC3_OUTPUT_MONO:
840
                    nf = LEVEL_MINUS_3DB / (1.0 + clev + (slev / 2.0));
841
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
842
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
843
                    ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
844
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
845
                    break;
846
                case AC3_OUTPUT_STEREO:
847
                    nf = 1.0 / (1.0 + clev + (slev * LEVEL_MINUS_3DB));
848
                    ctx->chcoeffs[0] *= nf;
849
                    ctx->chcoeffs[2] *= nf;
850
                    ctx->chcoeffs[1] *= (nf * clev);
851
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
852
                    break;
853
                case AC3_OUTPUT_DOLBY:
854
                    nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
855
                    ctx->chcoeffs[0] *= nf;
856
                    ctx->chcoeffs[1] *= nf;
857
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
858
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
859
                    break;
860
            }
861
            break;
862
        case AC3_ACMOD_2F2R:
863
            switch (to) {
864
                case AC3_OUTPUT_MONO:
865
                    nf = LEVEL_MINUS_3DB / (1.0 + slev);
866
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
867
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
868
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
869
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
870
                    break;
871
                case AC3_OUTPUT_STEREO:
872
                    nf = 1.0 / (1.0 + slev);
873
                    ctx->chcoeffs[0] *= nf;
874
                    ctx->chcoeffs[1] *= nf;
875
                    ctx->chcoeffs[2] *= (nf * slev);
876
                    ctx->chcoeffs[3] *= (nf * slev);
877
                    break;
878
                case AC3_OUTPUT_DOLBY:
879
                    nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
880
                    ctx->chcoeffs[0] *= nf;
881
                    ctx->chcoeffs[1] *= nf;
882
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
883
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
884
                    break;
885
            }
886
            break;
887
        case AC3_ACMOD_3F2R:
888
            switch (to) {
889
                case AC3_OUTPUT_MONO:
890
                    nf = LEVEL_MINUS_3DB / (1.0 + clev + slev);
891
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
892
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
893
                    ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
894
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
895
                    ctx->chcoeffs[4] *= (nf * slev * LEVEL_MINUS_3DB);
896
                    break;
897
                case AC3_OUTPUT_STEREO:
898
                    nf = 1.0 / (1.0 + clev + slev);
899
                    ctx->chcoeffs[0] *= nf;
900
                    ctx->chcoeffs[2] *= nf;
901
                    ctx->chcoeffs[1] *= (nf * clev);
902
                    ctx->chcoeffs[3] *= (nf * slev);
903
                    ctx->chcoeffs[4] *= (nf * slev);
904
                    break;
905
                case AC3_OUTPUT_DOLBY:
906
                    nf = 1.0 / (1.0 + (3.0 * LEVEL_MINUS_3DB));
907
                    ctx->chcoeffs[0] *= nf;
908
                    ctx->chcoeffs[1] *= nf;
909
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
910
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
911
                    ctx->chcoeffs[4] *= (nf * LEVEL_MINUS_3DB);
912
                    break;
913
            }
914
            break;
915
    }
916
}
917

    
918
/*********** BEGIN DOWNMIX FUNCTIONS ***********/
919
static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx)
920
{
921
    int i;
922
    float (*output)[BLOCK_SIZE] = ctx->output;
923

    
924
    for (i = 0; i < 256; i++)
925
        output[1][i] += output[2][i];
926
    memset(output[2], 0, sizeof(output[2]));
927
}
928

    
929
static inline void mix_dualmono_to_stereo(AC3DecodeContext *ctx)
930
{
931
    int i;
932
    float tmp;
933
    float (*output)[BLOCK_SIZE] = ctx->output;
934

    
935
    for (i = 0; i < 256; i++) {
936
        tmp = output[1][i] + output[2][i];
937
        output[1][i] = output[2][i] = tmp;
938
    }
939
}
940

    
941
static inline void upmix_mono_to_stereo(AC3DecodeContext *ctx)
942
{
943
    int i;
944
    float (*output)[BLOCK_SIZE] = ctx->output;
945

    
946
    for (i = 0; i < 256; i++)
947
        output[2][i] = output[1][i];
948
}
949

    
950
static inline void mix_stereo_to_mono(AC3DecodeContext *ctx)
951
{
952
    int i;
953
    float (*output)[BLOCK_SIZE] = ctx->output;
954

    
955
    for (i = 0; i < 256; i++)
956
        output[1][i] += output[2][i];
957
    memset(output[2], 0, sizeof(output[2]));
958
}
959

    
960
static inline void mix_3f_to_mono(AC3DecodeContext *ctx)
961
{
962
    int i;
963
    float (*output)[BLOCK_SIZE] = ctx->output;
964

    
965
    for (i = 0; i < 256; i++)
966
        output[1][i] += (output[2][i] + output[3][i]);
967
    memset(output[2], 0, sizeof(output[2]));
968
    memset(output[3], 0, sizeof(output[3]));
969
}
970

    
971
static inline void mix_3f_to_stereo(AC3DecodeContext *ctx)
972
{
973
    int i;
974
    float (*output)[BLOCK_SIZE] = ctx->output;
975

    
976
    for (i = 0; i < 256; i++) {
977
        output[1][i] += output[2][i];
978
        output[2][i] += output[3][i];
979
    }
980
    memset(output[3], 0, sizeof(output[3]));
981
}
982

    
983
static inline void mix_2f_1r_to_mono(AC3DecodeContext *ctx)
984
{
985
    int i;
986
    float (*output)[BLOCK_SIZE] = ctx->output;
987

    
988
    for (i = 0; i < 256; i++)
989
        output[1][i] += (output[2][i] + output[3][i]);
990
    memset(output[2], 0, sizeof(output[2]));
991
    memset(output[3], 0, sizeof(output[3]));
992

    
993
}
994

    
995
static inline void mix_2f_1r_to_stereo(AC3DecodeContext *ctx)
996
{
997
    int i;
998
    float (*output)[BLOCK_SIZE] = ctx->output;
999

    
1000
    for (i = 0; i < 256; i++) {
1001
        output[1][i] += output[2][i];
1002
        output[2][i] += output[3][i];
1003
    }
1004
    memset(output[3], 0, sizeof(output[3]));
1005
}
1006

    
1007
static inline void mix_2f_1r_to_dolby(AC3DecodeContext *ctx)
1008
{
1009
    int i;
1010
    float (*output)[BLOCK_SIZE] = ctx->output;
1011

    
1012
    for (i = 0; i < 256; i++) {
1013
        output[1][i] -= output[3][i];
1014
        output[2][i] += output[3][i];
1015
    }
1016
    memset(output[3], 0, sizeof(output[3]));
1017
}
1018

    
1019
static inline void mix_3f_1r_to_mono(AC3DecodeContext *ctx)
1020
{
1021
    int i;
1022
    float (*output)[BLOCK_SIZE] = ctx->output;
1023

    
1024
    for (i = 0; i < 256; i++)
1025
        output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1026
    memset(output[2], 0, sizeof(output[2]));
1027
    memset(output[3], 0, sizeof(output[3]));
1028
    memset(output[4], 0, sizeof(output[4]));
1029
}
1030

    
1031
static inline void mix_3f_1r_to_stereo(AC3DecodeContext *ctx)
1032
{
1033
    int i;
1034
    float (*output)[BLOCK_SIZE] = ctx->output;
1035

    
1036
    for (i = 0; i < 256; i++) {
1037
        output[1][i] += (output[2][i] + output[4][i]);
1038
        output[2][i] += (output[3][i] + output[4][i]);
1039
    }
1040
    memset(output[3], 0, sizeof(output[3]));
1041
    memset(output[4], 0, sizeof(output[4]));
1042
}
1043

    
1044
static inline void mix_3f_1r_to_dolby(AC3DecodeContext *ctx)
1045
{
1046
    int i;
1047
    float (*output)[BLOCK_SIZE] = ctx->output;
1048

    
1049
    for (i = 0; i < 256; i++) {
1050
        output[1][i] += (output[2][i] - output[4][i]);
1051
        output[2][i] += (output[3][i] + output[4][i]);
1052
    }
1053
    memset(output[3], 0, sizeof(output[3]));
1054
    memset(output[4], 0, sizeof(output[4]));
1055
}
1056

    
1057
static inline void mix_2f_2r_to_mono(AC3DecodeContext *ctx)
1058
{
1059
    int i;
1060
    float (*output)[BLOCK_SIZE] = ctx->output;
1061

    
1062
    for (i = 0; i < 256; i++)
1063
        output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1064
    memset(output[2], 0, sizeof(output[2]));
1065
    memset(output[3], 0, sizeof(output[3]));
1066
    memset(output[4], 0, sizeof(output[4]));
1067
}
1068

    
1069
static inline void mix_2f_2r_to_stereo(AC3DecodeContext *ctx)
1070
{
1071
    int i;
1072
    float (*output)[BLOCK_SIZE] = ctx->output;
1073

    
1074
    for (i = 0; i < 256; i++) {
1075
        output[1][i] += output[3][i];
1076
        output[2][i] += output[4][i];
1077
    }
1078
    memset(output[3], 0, sizeof(output[3]));
1079
    memset(output[4], 0, sizeof(output[4]));
1080
}
1081

    
1082
static inline void mix_2f_2r_to_dolby(AC3DecodeContext *ctx)
1083
{
1084
    int i;
1085
    float (*output)[BLOCK_SIZE] = ctx->output;
1086

    
1087
    for (i = 0; i < 256; i++) {
1088
        output[1][i] -= output[3][i];
1089
        output[2][i] += output[4][i];
1090
    }
1091
    memset(output[3], 0, sizeof(output[3]));
1092
    memset(output[4], 0, sizeof(output[4]));
1093
}
1094

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

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

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

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

    
1122
static inline void mix_3f_2r_to_dolby(AC3DecodeContext *ctx)
1123
{
1124
    int i;
1125
    float (*output)[BLOCK_SIZE] = ctx->output;
1126

    
1127
    for (i = 0; i < 256; i++) {
1128
        output[1][i] += (output[2][i] - output[4][i] - output[5][i]);
1129
        output[2][i] += (output[3][i] + output[4][i] + output[5][i]);
1130
    }
1131
    memset(output[3], 0, sizeof(output[3]));
1132
    memset(output[4], 0, sizeof(output[4]));
1133
    memset(output[5], 0, sizeof(output[5]));
1134
}
1135
/*********** END DOWNMIX FUNCTIONS ***********/
1136

    
1137
/* Downmix the output.
1138
 * This function downmixes the output when the number of input
1139
 * channels is not equal to the number of output channels requested.
1140
 */
1141
static void do_downmix(AC3DecodeContext *ctx)
1142
{
1143
    int from = ctx->acmod;
1144
    int to = ctx->blkoutput;
1145

    
1146
    if (to == AC3_OUTPUT_UNMODIFIED)
1147
        return;
1148

    
1149
    switch (from) {
1150
        case AC3_ACMOD_DUALMONO:
1151
            switch (to) {
1152
                case AC3_OUTPUT_MONO:
1153
                    mix_dualmono_to_mono(ctx);
1154
                    break;
1155
                case AC3_OUTPUT_STEREO: /* We assume that sum of both mono channels is requested */
1156
                    mix_dualmono_to_stereo(ctx);
1157
                    break;
1158
            }
1159
            break;
1160
        case AC3_ACMOD_MONO:
1161
            switch (to) {
1162
                case AC3_OUTPUT_STEREO:
1163
                    upmix_mono_to_stereo(ctx);
1164
                    break;
1165
            }
1166
            break;
1167
        case AC3_ACMOD_STEREO:
1168
            switch (to) {
1169
                case AC3_OUTPUT_MONO:
1170
                    mix_stereo_to_mono(ctx);
1171
                    break;
1172
            }
1173
            break;
1174
        case AC3_ACMOD_3F:
1175
            switch (to) {
1176
                case AC3_OUTPUT_MONO:
1177
                    mix_3f_to_mono(ctx);
1178
                    break;
1179
                case AC3_OUTPUT_STEREO:
1180
                    mix_3f_to_stereo(ctx);
1181
                    break;
1182
            }
1183
            break;
1184
        case AC3_ACMOD_2F1R:
1185
            switch (to) {
1186
                case AC3_OUTPUT_MONO:
1187
                    mix_2f_1r_to_mono(ctx);
1188
                    break;
1189
                case AC3_OUTPUT_STEREO:
1190
                    mix_2f_1r_to_stereo(ctx);
1191
                    break;
1192
                case AC3_OUTPUT_DOLBY:
1193
                    mix_2f_1r_to_dolby(ctx);
1194
                    break;
1195
            }
1196
            break;
1197
        case AC3_ACMOD_3F1R:
1198
            switch (to) {
1199
                case AC3_OUTPUT_MONO:
1200
                    mix_3f_1r_to_mono(ctx);
1201
                    break;
1202
                case AC3_OUTPUT_STEREO:
1203
                    mix_3f_1r_to_stereo(ctx);
1204
                    break;
1205
                case AC3_OUTPUT_DOLBY:
1206
                    mix_3f_1r_to_dolby(ctx);
1207
                    break;
1208
            }
1209
            break;
1210
        case AC3_ACMOD_2F2R:
1211
            switch (to) {
1212
                case AC3_OUTPUT_MONO:
1213
                    mix_2f_2r_to_mono(ctx);
1214
                    break;
1215
                case AC3_OUTPUT_STEREO:
1216
                    mix_2f_2r_to_stereo(ctx);
1217
                    break;
1218
                case AC3_OUTPUT_DOLBY:
1219
                    mix_2f_2r_to_dolby(ctx);
1220
                    break;
1221
            }
1222
            break;
1223
        case AC3_ACMOD_3F2R:
1224
            switch (to) {
1225
                case AC3_OUTPUT_MONO:
1226
                    mix_3f_2r_to_mono(ctx);
1227
                    break;
1228
                case AC3_OUTPUT_STEREO:
1229
                    mix_3f_2r_to_stereo(ctx);
1230
                    break;
1231
                case AC3_OUTPUT_DOLBY:
1232
                    mix_3f_2r_to_dolby(ctx);
1233
                    break;
1234
            }
1235
            break;
1236
    }
1237
}
1238

    
1239
/* This function performs the imdct on 256 sample transform
1240
 * coefficients.
1241
 */
1242
static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
1243
{
1244
    int i, k;
1245
    float x[128];
1246
    FFTComplex z[2][64];
1247
    float *o_ptr = ctx->tmp_output;
1248

    
1249
    for(i=0; i<2; i++) {
1250
        /* de-interleave coefficients */
1251
        for(k=0; k<128; k++) {
1252
            x[k] = ctx->transform_coeffs[chindex][2*k+i];
1253
        }
1254

    
1255
        /* run standard IMDCT */
1256
        ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, o_ptr, x, ctx->tmp_imdct);
1257

    
1258
        /* reverse the post-rotation & reordering from standard IMDCT */
1259
        for(k=0; k<32; k++) {
1260
            z[i][32+k].re = -o_ptr[128+2*k];
1261
            z[i][32+k].im = -o_ptr[2*k];
1262
            z[i][31-k].re =  o_ptr[2*k+1];
1263
            z[i][31-k].im =  o_ptr[128+2*k+1];
1264
        }
1265
    }
1266

    
1267
    /* apply AC-3 post-rotation & reordering */
1268
    for(k=0; k<64; k++) {
1269
        o_ptr[    2*k  ] = -z[0][   k].im;
1270
        o_ptr[    2*k+1] =  z[0][63-k].re;
1271
        o_ptr[128+2*k  ] = -z[0][   k].re;
1272
        o_ptr[128+2*k+1] =  z[0][63-k].im;
1273
        o_ptr[256+2*k  ] = -z[1][   k].re;
1274
        o_ptr[256+2*k+1] =  z[1][63-k].im;
1275
        o_ptr[384+2*k  ] =  z[1][   k].im;
1276
        o_ptr[384+2*k+1] = -z[1][63-k].re;
1277
    }
1278
}
1279

    
1280
/* IMDCT Transform. */
1281
static inline void do_imdct(AC3DecodeContext *ctx)
1282
{
1283
    int ch;
1284

    
1285
    if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
1286
        ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1287
                                      ctx->transform_coeffs[0], ctx->tmp_imdct);
1288
    }
1289
    for (ch=1; ch<=ctx->nfchans; ch++) {
1290
        if ((ctx->blksw >> (ch-1)) & 1)
1291
            do_imdct_256(ctx, ch);
1292
        else
1293
            ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1294
                                          ctx->transform_coeffs[ch],
1295
                                          ctx->tmp_imdct);
1296

    
1297
        ctx->dsp.vector_fmul_add_add(ctx->output[ch], ctx->tmp_output,
1298
                                     ctx->window, ctx->delay[ch], 384, 256, 1);
1299
        ctx->dsp.vector_fmul_reverse(ctx->delay[ch], ctx->tmp_output+256,
1300
                                     ctx->window, 256);
1301
    }
1302
}
1303

    
1304
/* Parse the audio block from ac3 bitstream.
1305
 * This function extract the audio block from the ac3 bitstream
1306
 * and produces the output for the block. This function must
1307
 * be called for each of the six audio block in the ac3 bitstream.
1308
 */
1309
static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
1310
{
1311
    int nfchans = ctx->nfchans;
1312
    int acmod = ctx->acmod;
1313
    int i, bnd, rbnd, seg, grpsize;
1314
    GetBitContext *gb = &ctx->gb;
1315
    int bit_alloc_flags = 0;
1316
    uint8_t *dexps;
1317
    int mstrcplco, cplcoexp, cplcomant;
1318
    int dynrng, chbwcod, ngrps, cplabsexp, skipl;
1319

    
1320
    ctx->blksw = 0;
1321
    for (i = 0; i < nfchans; i++) /*block switch flag */
1322
        ctx->blksw |= get_bits1(gb) << i;
1323

    
1324
    ctx->dithflag = 0;
1325
    for (i = 0; i < nfchans; i++) /* dithering flag */
1326
        ctx->dithflag |= get_bits1(gb) << i;
1327

    
1328
    if (get_bits1(gb)) { /* dynamic range */
1329
        dynrng = get_sbits(gb, 8);
1330
        ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1331
    } else if(blk == 0) {
1332
        ctx->dynrng = 1.0;
1333
    }
1334

    
1335
    if(acmod == AC3_ACMOD_DUALMONO) { /* dynamic range 1+1 mode */
1336
        if(get_bits1(gb)) {
1337
            dynrng = get_sbits(gb, 8);
1338
            ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1339
        } else if(blk == 0) {
1340
            ctx->dynrng2 = 1.0;
1341
        }
1342
    }
1343

    
1344
    get_downmix_coeffs(ctx);
1345

    
1346
    if (get_bits1(gb)) { /* coupling strategy */
1347
        ctx->cplinu = get_bits1(gb);
1348
        ctx->cplbndstrc = 0;
1349
        ctx->chincpl = 0;
1350
        if (ctx->cplinu) { /* coupling in use */
1351
            for (i = 0; i < nfchans; i++)
1352
                ctx->chincpl |= get_bits1(gb) << i;
1353

    
1354
            if (acmod == 0x02)
1355
                ctx->phsflginu = get_bits1(gb); //phase flag in use
1356

    
1357
            ctx->cplbegf = get_bits(gb, 4);
1358
            ctx->cplendf = get_bits(gb, 4);
1359

    
1360
            if (3 + ctx->cplendf - ctx->cplbegf < 0) {
1361
                av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", ctx->cplendf, ctx->cplbegf);
1362
                return -1;
1363
            }
1364

    
1365
            ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf;
1366
            ctx->cplstrtmant = ctx->cplbegf * 12 + 37;
1367
            ctx->cplendmant = ctx->cplendf * 12 + 73;
1368
            for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
1369
                if (get_bits1(gb)) {
1370
                    ctx->cplbndstrc |= 1 << i;
1371
                    ctx->ncplbnd--;
1372
                }
1373
        }
1374
    }
1375

    
1376
    if (ctx->cplinu) {
1377
        ctx->cplcoe = 0;
1378

    
1379
        for (i = 0; i < nfchans; i++)
1380
            if ((ctx->chincpl) >> i & 1)
1381
                if (get_bits1(gb)) { /* coupling co-ordinates */
1382
                    ctx->cplcoe |= 1 << i;
1383
                    mstrcplco = 3 * get_bits(gb, 2);
1384
                    for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
1385
                        cplcoexp = get_bits(gb, 4);
1386
                        cplcomant = get_bits(gb, 4);
1387
                        if (cplcoexp == 15)
1388
                            cplcomant <<= 14;
1389
                        else
1390
                            cplcomant = (cplcomant | 0x10) << 13;
1391
                        ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
1392
                    }
1393
                }
1394

    
1395
        if (acmod == 0x02 && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
1396
            for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
1397
                if (get_bits1(gb))
1398
                    ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
1399
    }
1400

    
1401
    if (acmod == 0x02) {/* rematrixing */
1402
        ctx->rematstr = get_bits1(gb);
1403
        if (ctx->rematstr) {
1404
            ctx->rematflg = 0;
1405

    
1406
            if (!(ctx->cplinu) || ctx->cplbegf > 2)
1407
                for (rbnd = 0; rbnd < 4; rbnd++)
1408
                    ctx->rematflg |= get_bits1(gb) << rbnd;
1409
            if (ctx->cplbegf > 0 && ctx->cplbegf <= 2 && ctx->cplinu)
1410
                for (rbnd = 0; rbnd < 3; rbnd++)
1411
                    ctx->rematflg |= get_bits1(gb) << rbnd;
1412
            if (ctx->cplbegf == 0 && ctx->cplinu)
1413
                for (rbnd = 0; rbnd < 2; rbnd++)
1414
                    ctx->rematflg |= get_bits1(gb) << rbnd;
1415
        }
1416
    }
1417

    
1418
    ctx->cplexpstr = EXP_REUSE;
1419
    ctx->lfeexpstr = EXP_REUSE;
1420
    if (ctx->cplinu) /* coupling exponent strategy */
1421
        ctx->cplexpstr = get_bits(gb, 2);
1422
    for (i = 0; i < nfchans; i++)  /* channel exponent strategy */
1423
        ctx->chexpstr[i] = get_bits(gb, 2);
1424
    if (ctx->lfeon)  /* lfe exponent strategy */
1425
        ctx->lfeexpstr = get_bits1(gb);
1426

    
1427
    for (i = 0; i < nfchans; i++) /* channel bandwidth code */
1428
        if (ctx->chexpstr[i] != EXP_REUSE) {
1429
            if ((ctx->chincpl >> i) & 1)
1430
                ctx->endmant[i] = ctx->cplstrtmant;
1431
            else {
1432
                chbwcod = get_bits(gb, 6);
1433
                if (chbwcod > 60) {
1434
                    av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
1435
                    return -1;
1436
                }
1437
                ctx->endmant[i] = chbwcod * 3 + 73;
1438
            }
1439
        }
1440

    
1441
    if (ctx->cplexpstr != EXP_REUSE) {/* coupling exponents */
1442
        bit_alloc_flags = 64;
1443
        cplabsexp = get_bits(gb, 4) << 1;
1444
        ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
1445
        decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant);
1446
    }
1447

    
1448
    for (i = 0; i < nfchans; i++) /* fbw channel exponents */
1449
        if (ctx->chexpstr[i] != EXP_REUSE) {
1450
            bit_alloc_flags |= 1 << i;
1451
            grpsize = 3 << (ctx->chexpstr[i] - 1);
1452
            ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
1453
            dexps = ctx->dexps[i];
1454
            dexps[0] = get_bits(gb, 4);
1455
            decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1);
1456
            skip_bits(gb, 2); /* skip gainrng */
1457
        }
1458

    
1459
    if (ctx->lfeexpstr != EXP_REUSE) { /* lfe exponents */
1460
        bit_alloc_flags |= 32;
1461
        ctx->dlfeexps[0] = get_bits(gb, 4);
1462
        decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1);
1463
    }
1464

    
1465
    if (get_bits1(gb)) { /* bit allocation information */
1466
        bit_alloc_flags = 127;
1467
        ctx->sdcycod = get_bits(gb, 2);
1468
        ctx->fdcycod = get_bits(gb, 2);
1469
        ctx->sgaincod = get_bits(gb, 2);
1470
        ctx->dbpbcod = get_bits(gb, 2);
1471
        ctx->floorcod = get_bits(gb, 3);
1472
    }
1473

    
1474
    if (get_bits1(gb)) { /* snroffset */
1475
        bit_alloc_flags = 127;
1476
        ctx->csnroffst = get_bits(gb, 6);
1477
        if (ctx->cplinu) { /* coupling fine snr offset and fast gain code */
1478
            ctx->cplfsnroffst = get_bits(gb, 4);
1479
            ctx->cplfgaincod = get_bits(gb, 3);
1480
        }
1481
        for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
1482
            ctx->fsnroffst[i] = get_bits(gb, 4);
1483
            ctx->fgaincod[i] = get_bits(gb, 3);
1484
        }
1485
        if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
1486
            ctx->lfefsnroffst = get_bits(gb, 4);
1487
            ctx->lfefgaincod = get_bits(gb, 3);
1488
        }
1489
    }
1490

    
1491
    if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
1492
        bit_alloc_flags |= 64;
1493
        ctx->cplfleak = get_bits(gb, 3);
1494
        ctx->cplsleak = get_bits(gb, 3);
1495
    }
1496

    
1497
    if (get_bits1(gb)) { /* delta bit allocation information */
1498
        bit_alloc_flags = 127;
1499

    
1500
        if (ctx->cplinu) {
1501
            ctx->cpldeltbae = get_bits(gb, 2);
1502
            if (ctx->cpldeltbae == DBA_RESERVED) {
1503
                av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
1504
                return -1;
1505
            }
1506
        }
1507

    
1508
        for (i = 0; i < nfchans; i++) {
1509
            ctx->deltbae[i] = get_bits(gb, 2);
1510
            if (ctx->deltbae[i] == DBA_RESERVED) {
1511
                av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
1512
                return -1;
1513
            }
1514
        }
1515

    
1516
        if (ctx->cplinu)
1517
            if (ctx->cpldeltbae == DBA_NEW) { /*coupling delta offset, len and bit allocation */
1518
                ctx->cpldeltnseg = get_bits(gb, 3);
1519
                for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
1520
                    ctx->cpldeltoffst[seg] = get_bits(gb, 5);
1521
                    ctx->cpldeltlen[seg] = get_bits(gb, 4);
1522
                    ctx->cpldeltba[seg] = get_bits(gb, 3);
1523
                }
1524
            }
1525

    
1526
        for (i = 0; i < nfchans; i++)
1527
            if (ctx->deltbae[i] == DBA_NEW) {/*channel delta offset, len and bit allocation */
1528
                ctx->deltnseg[i] = get_bits(gb, 3);
1529
                for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
1530
                    ctx->deltoffst[i][seg] = get_bits(gb, 5);
1531
                    ctx->deltlen[i][seg] = get_bits(gb, 4);
1532
                    ctx->deltba[i][seg] = get_bits(gb, 3);
1533
                }
1534
            }
1535
    } else if(blk == 0) {
1536
        if(ctx->cplinu)
1537
            ctx->cpldeltbae = DBA_NONE;
1538
        for(i=0; i<nfchans; i++) {
1539
            ctx->deltbae[i] = DBA_NONE;
1540
        }
1541
    }
1542

    
1543
    if (bit_alloc_flags) {
1544
        /* set bit allocation parameters */
1545
        ctx->bit_alloc_params.sdecay = ff_sdecaytab[ctx->sdcycod];
1546
        ctx->bit_alloc_params.fdecay = ff_fdecaytab[ctx->fdcycod];
1547
        ctx->bit_alloc_params.sgain = ff_sgaintab[ctx->sgaincod];
1548
        ctx->bit_alloc_params.dbknee = ff_dbkneetab[ctx->dbpbcod];
1549
        ctx->bit_alloc_params.floor = ff_floortab[ctx->floorcod];
1550
        ctx->bit_alloc_params.cplfleak = ctx->cplfleak;
1551
        ctx->bit_alloc_params.cplsleak = ctx->cplsleak;
1552

    
1553
        if (ctx->chincpl && (bit_alloc_flags & 64))
1554
            do_bit_allocation(ctx, 5);
1555
        for (i = 0; i < nfchans; i++)
1556
            if ((bit_alloc_flags >> i) & 1)
1557
                do_bit_allocation(ctx, i);
1558
        if (ctx->lfeon && (bit_alloc_flags & 32))
1559
            do_bit_allocation(ctx, 6);
1560
    }
1561

    
1562
    if (get_bits1(gb)) { /* unused dummy data */
1563
        skipl = get_bits(gb, 9);
1564
        while(skipl--)
1565
            skip_bits(gb, 8);
1566
    }
1567
    /* unpack the transform coefficients
1568
     * * this also uncouples channels if coupling is in use.
1569
     */
1570
    if (get_transform_coeffs(ctx)) {
1571
        av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1572
        return -1;
1573
    }
1574

    
1575
    /* recover coefficients if rematrixing is in use */
1576
    if (ctx->rematflg)
1577
        do_rematrixing(ctx);
1578

    
1579
    do_downmix(ctx);
1580

    
1581
    do_imdct(ctx);
1582

    
1583
    return 0;
1584
}
1585

    
1586
static inline int16_t convert(int32_t i)
1587
{
1588
    if (i > 0x43c07fff)
1589
        return 32767;
1590
    else if (i <= 0x43bf8000)
1591
        return -32768;
1592
    else
1593
        return (i - 0x43c00000);
1594
}
1595

    
1596
/* Decode ac3 frame.
1597
 *
1598
 * @param avctx Pointer to AVCodecContext
1599
 * @param data Pointer to pcm smaples
1600
 * @param data_size Set to number of pcm samples produced by decoding
1601
 * @param buf Data to be decoded
1602
 * @param buf_size Size of the buffer
1603
 */
1604
static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1605
{
1606
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1607
    int16_t *out_samples = (int16_t *)data;
1608
    int i, j, k, start;
1609
    int32_t *int_ptr[6];
1610

    
1611
    for (i = 0; i < 6; i++)
1612
        int_ptr[i] = (int32_t *)(&ctx->output[i]);
1613

    
1614
    //Initialize the GetBitContext with the start of valid AC3 Frame.
1615
    init_get_bits(&ctx->gb, buf, buf_size * 8);
1616

    
1617
    //Parse the syncinfo.
1618
    if (ac3_parse_header(ctx)) {
1619
        av_log(avctx, AV_LOG_ERROR, "\n");
1620
        *data_size = 0;
1621
        return buf_size;
1622
    }
1623

    
1624
    avctx->sample_rate = ctx->sampling_rate;
1625
    avctx->bit_rate = ctx->bit_rate;
1626

    
1627
    if (avctx->channels == 0) {
1628
        ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1629
        if (ctx->lfeon)
1630
            ctx->blkoutput |= AC3_OUTPUT_LFEON;
1631
        avctx->channels = ctx->nfchans + ctx->lfeon;
1632
    }
1633
    else if (avctx->channels == 1)
1634
        ctx->blkoutput |= AC3_OUTPUT_MONO;
1635
    else if (avctx->channels == 2) {
1636
        if (ctx->dsurmod == 0x02)
1637
            ctx->blkoutput |= AC3_OUTPUT_DOLBY;
1638
        else
1639
            ctx->blkoutput |= AC3_OUTPUT_STEREO;
1640
    }
1641
    else {
1642
        if (avctx->channels < (ctx->nfchans + ctx->lfeon))
1643
            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);
1644
        ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1645
        if (ctx->lfeon)
1646
            ctx->blkoutput |= AC3_OUTPUT_LFEON;
1647
        avctx->channels = ctx->nfchans + ctx->lfeon;
1648
    }
1649

    
1650
    //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);
1651

    
1652
    //Parse the Audio Blocks.
1653
    for (i = 0; i < NB_BLOCKS; i++) {
1654
        if (ac3_parse_audio_block(ctx, i)) {
1655
            av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1656
            *data_size = 0;
1657
            return ctx->frame_size;
1658
        }
1659
        start = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1;
1660
        for (k = 0; k < BLOCK_SIZE; k++)
1661
            for (j = start; j <= avctx->channels; j++)
1662
                *(out_samples++) = convert(int_ptr[j][k]);
1663
    }
1664
    *data_size = NB_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);
1665
    return ctx->frame_size;
1666
}
1667

    
1668
/* Uninitialize ac3 decoder.
1669
 */
1670
static int ac3_decode_end(AVCodecContext *avctx)
1671
{
1672
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1673
    ff_mdct_end(&ctx->imdct_512);
1674
    ff_mdct_end(&ctx->imdct_256);
1675

    
1676
    return 0;
1677
}
1678

    
1679
AVCodec ac3_decoder = {
1680
    .name = "ac3",
1681
    .type = CODEC_TYPE_AUDIO,
1682
    .id = CODEC_ID_AC3,
1683
    .priv_data_size = sizeof (AC3DecodeContext),
1684
    .init = ac3_decode_init,
1685
    .close = ac3_decode_end,
1686
    .decode = ac3_decode_frame,
1687
};
1688