Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3dec.c @ e9a38248

History | View | Annotate | Download (54.7 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
    int acmod;
92
    int cmixlev;
93
    int surmixlev;
94
    int dsurmod;
95

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

    
128
    /* Derived Attributes. */
129
    int      sampling_rate;
130
    int      bit_rate;
131
    int      frame_size;
132

    
133
    int      nchans;            //number of total channels
134
    int      nfchans;           //number of full-bandwidth channels
135
    int      lfeon;             //lfe channel in use
136

    
137
    float    dynrng;            //dynamic range gain
138
    float    dynrng2;           //dynamic range gain for 1+1 mode
139
    float    chcoeffs[6];       //normalized channel coefficients
140
    float    cplco[5][18];      //coupling coordinates
141
    int      ncplbnd;           //number of coupling bands
142
    int      ncplsubnd;         //number of coupling sub bands
143
    int      cplstrtmant;       //coupling start mantissa
144
    int      cplendmant;        //coupling end mantissa
145
    int      endmant[5];        //channel end mantissas
146
    AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
147

    
148
    int8_t   dcplexps[256];     //decoded coupling exponents
149
    int8_t   dexps[5][256];     //decoded fbw channel exponents
150
    int8_t   dlfeexps[256];     //decoded lfe channel exponents
151
    uint8_t  cplbap[256];       //coupling bit allocation pointers
152
    uint8_t  bap[5][256];       //fbw channel bit allocation pointers
153
    uint8_t  lfebap[256];       //lfe channel bit allocation pointers
154

    
155
    int      blkoutput;         //output configuration for block
156

    
157
    DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][BLOCK_SIZE]);  //transform coefficients
158

    
159
    /* For IMDCT. */
160
    MDCTContext imdct_512;  //for 512 sample imdct transform
161
    MDCTContext imdct_256;  //for 256 sample imdct transform
162
    DSPContext  dsp;        //for optimization
163

    
164
    DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS][BLOCK_SIZE]);    //output after imdct transform and windowing
165
    DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS][BLOCK_SIZE]);     //delay - added to the next block
166
    DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]);               //temporary storage for imdct transform
167
    DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]);          //temporary storage for output before windowing
168
    DECLARE_ALIGNED_16(float, window[BLOCK_SIZE]);                  //window coefficients
169

    
170
    /* Miscellaneous. */
171
    GetBitContext gb;
172
    AVRandomState dith_state;   //for dither generation
173
} AC3DecodeContext;
174

    
175
/*********** BEGIN INIT HELPER FUNCTIONS ***********/
176
/**
177
 * Generate a Kaiser-Bessel Derived Window.
178
 */
179
static void ac3_window_init(float *window)
180
{
181
   int i, j;
182
   double sum = 0.0, bessel, tmp;
183
   double local_window[256];
184
   double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
185

    
186
   for (i = 0; i < 256; i++) {
187
       tmp = i * (256 - i) * alpha2;
188
       bessel = 1.0;
189
       for (j = 100; j > 0; j--) /* defaul to 100 iterations */
190
           bessel = bessel * tmp / (j * j) + 1;
191
       sum += bessel;
192
       local_window[i] = sum;
193
   }
194

    
195
   sum++;
196
   for (i = 0; i < 256; i++)
197
       window[i] = sqrt(local_window[i] / sum);
198
}
199

    
200
/*
201
 * Generate quantizer tables.
202
 */
203
static void generate_quantizers_table(int16_t quantizers[], int level, int length)
204
{
205
    int i;
206

    
207
    for (i = 0; i < length; i++)
208
        quantizers[i] = ((2 * i - level + 1) << 15) / level;
209
}
210

    
211
static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
212
{
213
    int i, j;
214
    int16_t v;
215

    
216
    for (i = 0; i < length1; i++) {
217
        v = ((2 * i - level + 1) << 15) / level;
218
        for (j = 0; j < length2; j++)
219
            quantizers[i * length2 + j] = v;
220
    }
221

    
222
    for (i = length1 * length2; i < size; i++)
223
        quantizers[i] = 0;
224
}
225

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

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

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

    
240
}
241

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

    
246
    for (i = 0; i < length1; i++)
247
        for (j = 0; j < length2; j++)
248
            quantizers[i * length2 + j] = ((2 * (j % level) - level + 1) << 15) / level;
249

    
250
    for (i = length1 * length2; i < size; i++)
251
        quantizers[i] = 0;
252
}
253

    
254
/*
255
 * Initialize tables at runtime.
256
 */
257
static void ac3_tables_init(void)
258
{
259
    int i;
260

    
261
    /* Quantizer ungrouping tables. */
262
    // for level-3 quantizers
263
    generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
264
    generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
265
    generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
266

    
267
    //for level-5 quantizers
268
    generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
269
    generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
270
    generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
271

    
272
    //for level-7 quantizers
273
    generate_quantizers_table(l7_quantizers, 7, 7);
274

    
275
    //for level-4 quantizers
276
    generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
277
    generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
278

    
279
    //for level-15 quantizers
280
    generate_quantizers_table(l15_quantizers, 15, 15);
281
    /* End Quantizer ungrouping tables. */
282

    
283
    //generate scale factors
284
    for (i = 0; i < 25; i++)
285
        scale_factors[i] = pow(2.0, -(i + 15));
286

    
287
    /* generate exponent tables
288
       reference: Section 7.1.3 Exponent Decoding */
289
    for(i=0; i<128; i++) {
290
        exp_ungroup_tbl[i][0] =  i / 25;
291
        exp_ungroup_tbl[i][1] = (i % 25) / 5;
292
        exp_ungroup_tbl[i][2] = (i % 25) % 5;
293
    }
294
}
295

    
296

    
297
static int ac3_decode_init(AVCodecContext *avctx)
298
{
299
    AC3DecodeContext *ctx = avctx->priv_data;
300

    
301
    ac3_common_init();
302
    ac3_tables_init();
303
    ff_mdct_init(&ctx->imdct_256, 8, 1);
304
    ff_mdct_init(&ctx->imdct_512, 9, 1);
305
    ac3_window_init(ctx->window);
306
    dsputil_init(&ctx->dsp, avctx);
307
    av_init_random(0, &ctx->dith_state);
308

    
309
    return 0;
310
}
311
/*********** END INIT FUNCTIONS ***********/
312

    
313
/**
314
 * Parses the 'sync info' and 'bit stream info' from the AC-3 bitstream.
315
 * GetBitContext within AC3DecodeContext must point to
316
 * start of the synchronized ac3 bitstream.
317
 */
318
static int ac3_parse_header(AC3DecodeContext *ctx)
319
{
320
    AC3HeaderInfo hdr;
321
    GetBitContext *gb = &ctx->gb;
322
    int err, i;
323

    
324
    err = ff_ac3_parse_header(gb->buffer, &hdr);
325
    if(err)
326
        return err;
327

    
328
    /* get decoding parameters from header info */
329
    ctx->bit_alloc_params.fscod       = hdr.fscod;
330
    ctx->acmod                        = hdr.acmod;
331
    ctx->cmixlev                      = hdr.cmixlev;
332
    ctx->surmixlev                    = hdr.surmixlev;
333
    ctx->dsurmod                      = hdr.dsurmod;
334
    ctx->lfeon                        = hdr.lfeon;
335
    ctx->bit_alloc_params.halfratecod = hdr.halfratecod;
336
    ctx->sampling_rate                = hdr.sample_rate;
337
    ctx->bit_rate                     = hdr.bit_rate;
338
    ctx->nchans                       = hdr.channels;
339
    ctx->nfchans                      = ctx->nchans - ctx->lfeon;
340
    ctx->frame_size                   = hdr.frame_size;
341
    ctx->blkoutput                    = nfchans_tbl[ctx->acmod];
342
    if(ctx->lfeon)
343
        ctx->blkoutput |= AC3_OUTPUT_LFEON;
344

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

    
360
    /* read the rest of the bsi. read twice for dual mono mode. */
361
    i = !(ctx->acmod);
362
    do {
363
        skip_bits(gb, 5); //skip dialog normalization
364
        if (get_bits1(gb))
365
            skip_bits(gb, 8); //skip compression
366
        if (get_bits1(gb))
367
            skip_bits(gb, 8); //skip language code
368
        if (get_bits1(gb))
369
            skip_bits(gb, 7); //skip audio production information
370
    } while (i--);
371

    
372
    skip_bits(gb, 2); //skip copyright bit and original bitstream bit
373

    
374
    /* FIXME: read & use the xbsi1 downmix levels */
375
    if (get_bits1(gb))
376
        skip_bits(gb, 14); //skip timecode1
377
    if (get_bits1(gb))
378
        skip_bits(gb, 14); //skip timecode2
379

    
380
    if (get_bits1(gb)) {
381
        i = get_bits(gb, 6); //additional bsi length
382
        do {
383
            skip_bits(gb, 8);
384
        } while(i--);
385
    }
386

    
387
    return 0;
388
}
389

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

    
408
    /* unpack groups */
409
    grpsize = expstr + (expstr == EXP_D45);
410
    for(grp=0,i=0; grp<ngrps; grp++) {
411
        expacc = get_bits(gb, 7);
412
        dexp[i++] = exp_ungroup_tbl[expacc][0];
413
        dexp[i++] = exp_ungroup_tbl[expacc][1];
414
        dexp[i++] = exp_ungroup_tbl[expacc][2];
415
    }
416

    
417
    /* convert to absolute exps and expand groups */
418
    prevexp = absexp;
419
    for(i=0; i<ngrps*3; i++) {
420
        prevexp = av_clip(prevexp + dexp[i]-2, 0, 24);
421
        for(j=0; j<grpsize; j++) {
422
            dexps[(i*grpsize)+j] = prevexp;
423
        }
424
    }
425
}
426

    
427
/* Performs bit allocation.
428
 * This function performs bit allocation for the requested chanenl.
429
 */
430
static void do_bit_allocation(AC3DecodeContext *ctx, int chnl)
431
{
432
    int fgain, snroffset;
433

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

    
462
typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
463
    int16_t l3_quantizers[3];
464
    int16_t l5_quantizers[3];
465
    int16_t l11_quantizers[2];
466
    int l3ptr;
467
    int l5ptr;
468
    int l11ptr;
469
} mant_groups;
470

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

    
484
    cplbndstrc = ctx->cplbndstrc;
485
    start = ctx->cplstrtmant;
486
    bnd = 0;
487

    
488
    while (start < ctx->cplendmant) {
489
        end = start + 12;
490
        while (cplbndstrc & 1) {
491
            end += 12;
492
            cplbndstrc >>= 1;
493
        }
494
        cplbndstrc >>= 1;
495
        for (ch = 0; ch < ctx->nfchans; ch++)
496
            cplcos[ch] = ctx->chcoeffs[ch] * ctx->cplco[ch][bnd];
497
        bnd++;
498

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

    
524
                case 2:
525
                    if (m->l5ptr > 2) {
526
                        gcode = get_bits(gb, 7);
527
                        m->l5_quantizers[0] = l5_quantizers_1[gcode];
528
                        m->l5_quantizers[1] = l5_quantizers_2[gcode];
529
                        m->l5_quantizers[2] = l5_quantizers_3[gcode];
530
                        m->l5ptr = 0;
531
                    }
532
                    cplcoeff = m->l5_quantizers[m->l5ptr++] * scale_factors[exps[start]];
533
                    break;
534

    
535
                case 3:
536
                    cplcoeff = l7_quantizers[get_bits(gb, 3)] * scale_factors[exps[start]];
537
                    break;
538

    
539
                case 4:
540
                    if (m->l11ptr > 1) {
541
                        gcode = get_bits(gb, 7);
542
                        m->l11_quantizers[0] = l11_quantizers_1[gcode];
543
                        m->l11_quantizers[1] = l11_quantizers_2[gcode];
544
                        m->l11ptr = 0;
545
                    }
546
                    cplcoeff = m->l11_quantizers[m->l11ptr++] * scale_factors[exps[start]];
547
                    break;
548

    
549
                case 5:
550
                    cplcoeff = l15_quantizers[get_bits(gb, 4)] * scale_factors[exps[start]];
551
                    break;
552

    
553
                default:
554
                    cplcoeff = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * scale_factors[exps[start]];
555
            }
556
            for (ch = 0; ch < ctx->nfchans; ch++)
557
                if (ctx->chincpl[ch])
558
                    ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
559
            start++;
560
        }
561
    }
562

    
563
    return 0;
564
}
565

    
566
/* Get the transform coefficients for particular channel */
567
static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
568
{
569
    GetBitContext *gb = &ctx->gb;
570
    int i, gcode, tbap, dithflag, end;
571
    uint8_t *exps;
572
    uint8_t *bap;
573
    float *coeffs;
574
    float factors[25];
575

    
576
    for (i = 0; i < 25; i++)
577
        factors[i] = scale_factors[i] * ctx->chcoeffs[ch_index];
578

    
579
    if (ch_index != -1) { /* fbw channels */
580
        dithflag = ctx->dithflag[ch_index];
581
        exps = ctx->dexps[ch_index];
582
        bap = ctx->bap[ch_index];
583
        coeffs = ctx->transform_coeffs[ch_index + 1];
584
        end = ctx->endmant[ch_index];
585
    } else if (ch_index == -1) {
586
        dithflag = 0;
587
        exps = ctx->dlfeexps;
588
        bap = ctx->lfebap;
589
        coeffs = ctx->transform_coeffs[0];
590
        end = 7;
591
    }
592

    
593

    
594
    for (i = 0; i < end; i++) {
595
        tbap = bap[i];
596
        switch (tbap) {
597
            case 0:
598
                if (!dithflag) {
599
                    coeffs[i] = 0;
600
                    continue;
601
                }
602
                else {
603
                    coeffs[i] = (av_random(&ctx->dith_state) & 0xFFFF) * factors[exps[i]];
604
                    coeffs[i] *= LEVEL_MINUS_3DB;
605
                    continue;
606
                }
607

    
608
            case 1:
609
                if (m->l3ptr > 2) {
610
                    gcode = get_bits(gb, 5);
611
                    m->l3_quantizers[0] = l3_quantizers_1[gcode];
612
                    m->l3_quantizers[1] = l3_quantizers_2[gcode];
613
                    m->l3_quantizers[2] = l3_quantizers_3[gcode];
614
                    m->l3ptr = 0;
615
                }
616
                coeffs[i] = m->l3_quantizers[m->l3ptr++] * factors[exps[i]];
617
                continue;
618

    
619
            case 2:
620
                if (m->l5ptr > 2) {
621
                    gcode = get_bits(gb, 7);
622
                    m->l5_quantizers[0] = l5_quantizers_1[gcode];
623
                    m->l5_quantizers[1] = l5_quantizers_2[gcode];
624
                    m->l5_quantizers[2] = l5_quantizers_3[gcode];
625
                    m->l5ptr = 0;
626
                }
627
                coeffs[i] = m->l5_quantizers[m->l5ptr++] * factors[exps[i]];
628
                continue;
629

    
630
            case 3:
631
                coeffs[i] = l7_quantizers[get_bits(gb, 3)] * factors[exps[i]];
632
                continue;
633

    
634
            case 4:
635
                if (m->l11ptr > 1) {
636
                    gcode = get_bits(gb, 7);
637
                    m->l11_quantizers[0] = l11_quantizers_1[gcode];
638
                    m->l11_quantizers[1] = l11_quantizers_2[gcode];
639
                    m->l11ptr = 0;
640
                }
641
                coeffs[i] = m->l11_quantizers[m->l11ptr++] * factors[exps[i]];
642
                continue;
643

    
644
            case 5:
645
                coeffs[i] = l15_quantizers[get_bits(gb, 4)] * factors[exps[i]];
646
                continue;
647

    
648
            default:
649
                coeffs[i] = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * factors[exps[i]];
650
                continue;
651
        }
652
    }
653

    
654
    return 0;
655
}
656

    
657
/* Get the transform coefficients.
658
 * This function extracts the tranform coefficients form the ac3 bitstream.
659
 * This function is called after bit allocation is performed.
660
 */
661
static int get_transform_coeffs(AC3DecodeContext * ctx)
662
{
663
    int i, end;
664
    int got_cplchan = 0;
665
    mant_groups m;
666

    
667
    m.l3ptr = m.l5ptr = m.l11ptr = 3;
668

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

    
697
    return 0;
698
}
699

    
700
/* Rematrixing routines. */
701
static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
702
{
703
    float tmp0, tmp1;
704

    
705
    while (start < end) {
706
        tmp0 = ctx->transform_coeffs[1][start];
707
        tmp1 = ctx->transform_coeffs[2][start];
708
        ctx->transform_coeffs[1][start] = tmp0 + tmp1;
709
        ctx->transform_coeffs[2][start] = tmp0 - tmp1;
710
        start++;
711
    }
712
}
713

    
714
static void do_rematrixing(AC3DecodeContext *ctx)
715
{
716
    int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
717
    int end, bndend;
718

    
719
    end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
720

    
721
    if (ctx->rematflg[0])
722
        do_rematrixing1(ctx, bnd1, bnd2);
723

    
724
    if (ctx->rematflg[1])
725
        do_rematrixing1(ctx, bnd2, bnd3);
726

    
727
    bndend = bnd4;
728
    if (bndend > end) {
729
        bndend = end;
730
        if (ctx->rematflg[2])
731
            do_rematrixing1(ctx, bnd3, bndend);
732
    } else {
733
        if (ctx->rematflg[2])
734
            do_rematrixing1(ctx, bnd3, bnd4);
735
        if (ctx->rematflg[3])
736
            do_rematrixing1(ctx, bnd4, end);
737
    }
738
}
739

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

    
753
    if (!ctx->acmod) {
754
        ctx->chcoeffs[0] = 2 * ctx->dynrng;
755
        ctx->chcoeffs[1] = 2 * ctx->dynrng2;
756
    } else {
757
        for (i = 0; i < ctx->nfchans; i++)
758
            ctx->chcoeffs[i] = 2 * ctx->dynrng;
759
    }
760

    
761
    if (to == AC3_OUTPUT_UNMODIFIED)
762
        return;
763

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

    
911
/*********** BEGIN DOWNMIX FUNCTIONS ***********/
912
static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx)
913
{
914
    int i;
915
    float (*output)[BLOCK_SIZE] = ctx->output;
916

    
917
    for (i = 0; i < 256; i++)
918
        output[1][i] += output[2][i];
919
    memset(output[2], 0, sizeof(output[2]));
920
}
921

    
922
static inline void mix_dualmono_to_stereo(AC3DecodeContext *ctx)
923
{
924
    int i;
925
    float tmp;
926
    float (*output)[BLOCK_SIZE] = ctx->output;
927

    
928
    for (i = 0; i < 256; i++) {
929
        tmp = output[1][i] + output[2][i];
930
        output[1][i] = output[2][i] = tmp;
931
    }
932
}
933

    
934
static inline void upmix_mono_to_stereo(AC3DecodeContext *ctx)
935
{
936
    int i;
937
    float (*output)[BLOCK_SIZE] = ctx->output;
938

    
939
    for (i = 0; i < 256; i++)
940
        output[2][i] = output[1][i];
941
}
942

    
943
static inline void mix_stereo_to_mono(AC3DecodeContext *ctx)
944
{
945
    int i;
946
    float (*output)[BLOCK_SIZE] = ctx->output;
947

    
948
    for (i = 0; i < 256; i++)
949
        output[1][i] += output[2][i];
950
    memset(output[2], 0, sizeof(output[2]));
951
}
952

    
953
static inline void mix_3f_to_mono(AC3DecodeContext *ctx)
954
{
955
    int i;
956
    float (*output)[BLOCK_SIZE] = ctx->output;
957

    
958
    for (i = 0; i < 256; i++)
959
        output[1][i] += (output[2][i] + output[3][i]);
960
    memset(output[2], 0, sizeof(output[2]));
961
    memset(output[3], 0, sizeof(output[3]));
962
}
963

    
964
static inline void mix_3f_to_stereo(AC3DecodeContext *ctx)
965
{
966
    int i;
967
    float (*output)[BLOCK_SIZE] = ctx->output;
968

    
969
    for (i = 0; i < 256; i++) {
970
        output[1][i] += output[2][i];
971
        output[2][i] += output[3][i];
972
    }
973
    memset(output[3], 0, sizeof(output[3]));
974
}
975

    
976
static inline void mix_2f_1r_to_mono(AC3DecodeContext *ctx)
977
{
978
    int i;
979
    float (*output)[BLOCK_SIZE] = ctx->output;
980

    
981
    for (i = 0; i < 256; i++)
982
        output[1][i] += (output[2][i] + output[3][i]);
983
    memset(output[2], 0, sizeof(output[2]));
984
    memset(output[3], 0, sizeof(output[3]));
985

    
986
}
987

    
988
static inline void mix_2f_1r_to_stereo(AC3DecodeContext *ctx)
989
{
990
    int i;
991
    float (*output)[BLOCK_SIZE] = ctx->output;
992

    
993
    for (i = 0; i < 256; i++) {
994
        output[1][i] += output[2][i];
995
        output[2][i] += output[3][i];
996
    }
997
    memset(output[3], 0, sizeof(output[3]));
998
}
999

    
1000
static inline void mix_2f_1r_to_dolby(AC3DecodeContext *ctx)
1001
{
1002
    int i;
1003
    float (*output)[BLOCK_SIZE] = ctx->output;
1004

    
1005
    for (i = 0; i < 256; i++) {
1006
        output[1][i] -= output[3][i];
1007
        output[2][i] += output[3][i];
1008
    }
1009
    memset(output[3], 0, sizeof(output[3]));
1010
}
1011

    
1012
static inline void mix_3f_1r_to_mono(AC3DecodeContext *ctx)
1013
{
1014
    int i;
1015
    float (*output)[BLOCK_SIZE] = ctx->output;
1016

    
1017
    for (i = 0; i < 256; i++)
1018
        output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1019
    memset(output[2], 0, sizeof(output[2]));
1020
    memset(output[3], 0, sizeof(output[3]));
1021
    memset(output[4], 0, sizeof(output[4]));
1022
}
1023

    
1024
static inline void mix_3f_1r_to_stereo(AC3DecodeContext *ctx)
1025
{
1026
    int i;
1027
    float (*output)[BLOCK_SIZE] = ctx->output;
1028

    
1029
    for (i = 0; i < 256; i++) {
1030
        output[1][i] += (output[2][i] + output[4][i]);
1031
        output[2][i] += (output[3][i] + output[4][i]);
1032
    }
1033
    memset(output[3], 0, sizeof(output[3]));
1034
    memset(output[4], 0, sizeof(output[4]));
1035
}
1036

    
1037
static inline void mix_3f_1r_to_dolby(AC3DecodeContext *ctx)
1038
{
1039
    int i;
1040
    float (*output)[BLOCK_SIZE] = ctx->output;
1041

    
1042
    for (i = 0; i < 256; i++) {
1043
        output[1][i] += (output[2][i] - output[4][i]);
1044
        output[2][i] += (output[3][i] + output[4][i]);
1045
    }
1046
    memset(output[3], 0, sizeof(output[3]));
1047
    memset(output[4], 0, sizeof(output[4]));
1048
}
1049

    
1050
static inline void mix_2f_2r_to_mono(AC3DecodeContext *ctx)
1051
{
1052
    int i;
1053
    float (*output)[BLOCK_SIZE] = ctx->output;
1054

    
1055
    for (i = 0; i < 256; i++)
1056
        output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1057
    memset(output[2], 0, sizeof(output[2]));
1058
    memset(output[3], 0, sizeof(output[3]));
1059
    memset(output[4], 0, sizeof(output[4]));
1060
}
1061

    
1062
static inline void mix_2f_2r_to_stereo(AC3DecodeContext *ctx)
1063
{
1064
    int i;
1065
    float (*output)[BLOCK_SIZE] = ctx->output;
1066

    
1067
    for (i = 0; i < 256; i++) {
1068
        output[1][i] += output[3][i];
1069
        output[2][i] += output[4][i];
1070
    }
1071
    memset(output[3], 0, sizeof(output[3]));
1072
    memset(output[4], 0, sizeof(output[4]));
1073
}
1074

    
1075
static inline void mix_2f_2r_to_dolby(AC3DecodeContext *ctx)
1076
{
1077
    int i;
1078
    float (*output)[BLOCK_SIZE] = ctx->output;
1079

    
1080
    for (i = 0; i < 256; i++) {
1081
        output[1][i] -= output[3][i];
1082
        output[2][i] += output[4][i];
1083
    }
1084
    memset(output[3], 0, sizeof(output[3]));
1085
    memset(output[4], 0, sizeof(output[4]));
1086
}
1087

    
1088
static inline void mix_3f_2r_to_mono(AC3DecodeContext *ctx)
1089
{
1090
    int i;
1091
    float (*output)[BLOCK_SIZE] = ctx->output;
1092

    
1093
    for (i = 0; i < 256; i++)
1094
        output[1][i] += (output[2][i] + output[3][i] + output[4][i] + output[5][i]);
1095
    memset(output[2], 0, sizeof(output[2]));
1096
    memset(output[3], 0, sizeof(output[3]));
1097
    memset(output[4], 0, sizeof(output[4]));
1098
    memset(output[5], 0, sizeof(output[5]));
1099
}
1100

    
1101
static inline void mix_3f_2r_to_stereo(AC3DecodeContext *ctx)
1102
{
1103
    int i;
1104
    float (*output)[BLOCK_SIZE] = ctx->output;
1105

    
1106
    for (i = 0; i < 256; i++) {
1107
        output[1][i] += (output[2][i] + output[4][i]);
1108
        output[2][i] += (output[3][i] + output[5][i]);
1109
    }
1110
    memset(output[3], 0, sizeof(output[3]));
1111
    memset(output[4], 0, sizeof(output[4]));
1112
    memset(output[5], 0, sizeof(output[5]));
1113
}
1114

    
1115
static inline void mix_3f_2r_to_dolby(AC3DecodeContext *ctx)
1116
{
1117
    int i;
1118
    float (*output)[BLOCK_SIZE] = ctx->output;
1119

    
1120
    for (i = 0; i < 256; i++) {
1121
        output[1][i] += (output[2][i] - output[4][i] - output[5][i]);
1122
        output[2][i] += (output[3][i] + output[4][i] + output[5][i]);
1123
    }
1124
    memset(output[3], 0, sizeof(output[3]));
1125
    memset(output[4], 0, sizeof(output[4]));
1126
    memset(output[5], 0, sizeof(output[5]));
1127
}
1128
/*********** END DOWNMIX FUNCTIONS ***********/
1129

    
1130
/* Downmix the output.
1131
 * This function downmixes the output when the number of input
1132
 * channels is not equal to the number of output channels requested.
1133
 */
1134
static void do_downmix(AC3DecodeContext *ctx)
1135
{
1136
    int from = ctx->acmod;
1137
    int to = ctx->blkoutput;
1138

    
1139
    if (to == AC3_OUTPUT_UNMODIFIED)
1140
        return;
1141

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

    
1232
/* This function performs the imdct on 256 sample transform
1233
 * coefficients.
1234
 */
1235
static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
1236
{
1237
    int i, k;
1238
    float x[128];
1239
    FFTComplex z[2][64];
1240
    float *o_ptr = ctx->tmp_output;
1241

    
1242
    for(i=0; i<2; i++) {
1243
        /* de-interleave coefficients */
1244
        for(k=0; k<128; k++) {
1245
            x[k] = ctx->transform_coeffs[chindex][2*k+i];
1246
        }
1247

    
1248
        /* run standard IMDCT */
1249
        ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, o_ptr, x, ctx->tmp_imdct);
1250

    
1251
        /* reverse the post-rotation & reordering from standard IMDCT */
1252
        for(k=0; k<32; k++) {
1253
            z[i][32+k].re = -o_ptr[128+2*k];
1254
            z[i][32+k].im = -o_ptr[2*k];
1255
            z[i][31-k].re =  o_ptr[2*k+1];
1256
            z[i][31-k].im =  o_ptr[128+2*k+1];
1257
        }
1258
    }
1259

    
1260
    /* apply AC-3 post-rotation & reordering */
1261
    for(k=0; k<64; k++) {
1262
        o_ptr[    2*k  ] = -z[0][   k].im;
1263
        o_ptr[    2*k+1] =  z[0][63-k].re;
1264
        o_ptr[128+2*k  ] = -z[0][   k].re;
1265
        o_ptr[128+2*k+1] =  z[0][63-k].im;
1266
        o_ptr[256+2*k  ] = -z[1][   k].re;
1267
        o_ptr[256+2*k+1] =  z[1][63-k].im;
1268
        o_ptr[384+2*k  ] =  z[1][   k].im;
1269
        o_ptr[384+2*k+1] = -z[1][63-k].re;
1270
    }
1271
}
1272

    
1273
/* IMDCT Transform. */
1274
static inline void do_imdct(AC3DecodeContext *ctx)
1275
{
1276
    int ch;
1277

    
1278
    if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
1279
        ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1280
                                      ctx->transform_coeffs[0], ctx->tmp_imdct);
1281
    }
1282
    for (ch=1; ch<=ctx->nfchans; ch++) {
1283
        if (ctx->blksw[ch-1])
1284
            do_imdct_256(ctx, ch);
1285
        else
1286
            ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1287
                                          ctx->transform_coeffs[ch],
1288
                                          ctx->tmp_imdct);
1289

    
1290
        ctx->dsp.vector_fmul_add_add(ctx->output[ch], ctx->tmp_output,
1291
                                     ctx->window, ctx->delay[ch], 384, 256, 1);
1292
        ctx->dsp.vector_fmul_reverse(ctx->delay[ch], ctx->tmp_output+256,
1293
                                     ctx->window, 256);
1294
    }
1295
}
1296

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

    
1313
    for (i = 0; i < nfchans; i++) /*block switch flag */
1314
        ctx->blksw[i] = get_bits1(gb);
1315

    
1316
    for (i = 0; i < nfchans; i++) /* dithering flag */
1317
        ctx->dithflag[i] = get_bits1(gb);
1318

    
1319
    if (get_bits1(gb)) { /* dynamic range */
1320
        dynrng = get_sbits(gb, 8);
1321
        ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1322
    } else if(blk == 0) {
1323
        ctx->dynrng = 1.0;
1324
    }
1325

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

    
1335
    get_downmix_coeffs(ctx);
1336

    
1337
    if (get_bits1(gb)) { /* coupling strategy */
1338
        ctx->cplinu = get_bits1(gb);
1339
        ctx->cplbndstrc = 0;
1340
        if (ctx->cplinu) { /* coupling in use */
1341
            for (i = 0; i < nfchans; i++)
1342
                ctx->chincpl[i] = get_bits1(gb);
1343

    
1344
            if (acmod == AC3_ACMOD_STEREO)
1345
                ctx->phsflginu = get_bits1(gb); //phase flag in use
1346

    
1347
            ctx->cplbegf = get_bits(gb, 4);
1348
            ctx->cplendf = get_bits(gb, 4);
1349

    
1350
            if (3 + ctx->cplendf - ctx->cplbegf < 0) {
1351
                av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", ctx->cplendf, ctx->cplbegf);
1352
                return -1;
1353
            }
1354

    
1355
            ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf;
1356
            ctx->cplstrtmant = ctx->cplbegf * 12 + 37;
1357
            ctx->cplendmant = ctx->cplendf * 12 + 73;
1358
            for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
1359
                if (get_bits1(gb)) {
1360
                    ctx->cplbndstrc |= 1 << i;
1361
                    ctx->ncplbnd--;
1362
                }
1363
        } else {
1364
            for (i = 0; i < nfchans; i++)
1365
                ctx->chincpl[i] = 0;
1366
        }
1367
    }
1368

    
1369
    if (ctx->cplinu) {
1370
        ctx->cplcoe = 0;
1371

    
1372
        for (i = 0; i < nfchans; i++)
1373
            if (ctx->chincpl[i])
1374
                if (get_bits1(gb)) { /* coupling co-ordinates */
1375
                    ctx->cplcoe |= 1 << i;
1376
                    mstrcplco = 3 * get_bits(gb, 2);
1377
                    for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
1378
                        cplcoexp = get_bits(gb, 4);
1379
                        cplcomant = get_bits(gb, 4);
1380
                        if (cplcoexp == 15)
1381
                            cplcomant <<= 14;
1382
                        else
1383
                            cplcomant = (cplcomant | 0x10) << 13;
1384
                        ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
1385
                    }
1386
                }
1387

    
1388
        if (acmod == AC3_ACMOD_STEREO && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
1389
            for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
1390
                if (get_bits1(gb))
1391
                    ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
1392
    }
1393

    
1394
    if (acmod == AC3_ACMOD_STEREO) {/* rematrixing */
1395
        ctx->rematstr = get_bits1(gb);
1396
        if (ctx->rematstr) {
1397
            if (!(ctx->cplinu) || ctx->cplbegf > 2)
1398
                for (rbnd = 0; rbnd < 4; rbnd++)
1399
                    ctx->rematflg[rbnd] = get_bits1(gb);
1400
            if (ctx->cplbegf > 0 && ctx->cplbegf <= 2 && ctx->cplinu)
1401
                for (rbnd = 0; rbnd < 3; rbnd++)
1402
                    ctx->rematflg[rbnd] = get_bits1(gb);
1403
            if (ctx->cplbegf == 0 && ctx->cplinu)
1404
                for (rbnd = 0; rbnd < 2; rbnd++)
1405
                    ctx->rematflg[rbnd] = get_bits1(gb);
1406
        }
1407
    }
1408

    
1409
    ctx->cplexpstr = EXP_REUSE;
1410
    ctx->lfeexpstr = EXP_REUSE;
1411
    if (ctx->cplinu) /* coupling exponent strategy */
1412
        ctx->cplexpstr = get_bits(gb, 2);
1413
    for (i = 0; i < nfchans; i++)  /* channel exponent strategy */
1414
        ctx->chexpstr[i] = get_bits(gb, 2);
1415
    if (ctx->lfeon)  /* lfe exponent strategy */
1416
        ctx->lfeexpstr = get_bits1(gb);
1417

    
1418
    for (i = 0; i < nfchans; i++) /* channel bandwidth code */
1419
        if (ctx->chexpstr[i] != EXP_REUSE) {
1420
            if (ctx->chincpl[i])
1421
                ctx->endmant[i] = ctx->cplstrtmant;
1422
            else {
1423
                chbwcod = get_bits(gb, 6);
1424
                if (chbwcod > 60) {
1425
                    av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
1426
                    return -1;
1427
                }
1428
                ctx->endmant[i] = chbwcod * 3 + 73;
1429
            }
1430
        }
1431

    
1432
    if (ctx->cplexpstr != EXP_REUSE) {/* coupling exponents */
1433
        bit_alloc_flags = 64;
1434
        cplabsexp = get_bits(gb, 4) << 1;
1435
        ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
1436
        decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant);
1437
    }
1438

    
1439
    for (i = 0; i < nfchans; i++) /* fbw channel exponents */
1440
        if (ctx->chexpstr[i] != EXP_REUSE) {
1441
            bit_alloc_flags |= 1 << i;
1442
            grpsize = 3 << (ctx->chexpstr[i] - 1);
1443
            ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
1444
            dexps = ctx->dexps[i];
1445
            dexps[0] = get_bits(gb, 4);
1446
            decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1);
1447
            skip_bits(gb, 2); /* skip gainrng */
1448
        }
1449

    
1450
    if (ctx->lfeexpstr != EXP_REUSE) { /* lfe exponents */
1451
        bit_alloc_flags |= 32;
1452
        ctx->dlfeexps[0] = get_bits(gb, 4);
1453
        decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1);
1454
    }
1455

    
1456
    if (get_bits1(gb)) { /* bit allocation information */
1457
        bit_alloc_flags = 127;
1458
        ctx->bit_alloc_params.sdecay = ff_sdecaytab[get_bits(gb, 2)];
1459
        ctx->bit_alloc_params.fdecay = ff_fdecaytab[get_bits(gb, 2)];
1460
        ctx->bit_alloc_params.sgain  = ff_sgaintab[get_bits(gb, 2)];
1461
        ctx->bit_alloc_params.dbknee = ff_dbkneetab[get_bits(gb, 2)];
1462
        ctx->bit_alloc_params.floor  = ff_floortab[get_bits(gb, 3)];
1463
    }
1464

    
1465
    if (get_bits1(gb)) { /* snroffset */
1466
        bit_alloc_flags = 127;
1467
        ctx->csnroffst = get_bits(gb, 6);
1468
        if (ctx->cplinu) { /* coupling fine snr offset and fast gain code */
1469
            ctx->cplfsnroffst = get_bits(gb, 4);
1470
            ctx->cplfgaincod = get_bits(gb, 3);
1471
        }
1472
        for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
1473
            ctx->fsnroffst[i] = get_bits(gb, 4);
1474
            ctx->fgaincod[i] = get_bits(gb, 3);
1475
        }
1476
        if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
1477
            ctx->lfefsnroffst = get_bits(gb, 4);
1478
            ctx->lfefgaincod = get_bits(gb, 3);
1479
        }
1480
    }
1481

    
1482
    if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
1483
        bit_alloc_flags |= 64;
1484
        ctx->bit_alloc_params.cplfleak = get_bits(gb, 3);
1485
        ctx->bit_alloc_params.cplsleak = get_bits(gb, 3);
1486
    }
1487

    
1488
    if (get_bits1(gb)) { /* delta bit allocation information */
1489
        bit_alloc_flags = 127;
1490

    
1491
        if (ctx->cplinu) {
1492
            ctx->cpldeltbae = get_bits(gb, 2);
1493
            if (ctx->cpldeltbae == DBA_RESERVED) {
1494
                av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
1495
                return -1;
1496
            }
1497
        }
1498

    
1499
        for (i = 0; i < nfchans; i++) {
1500
            ctx->deltbae[i] = get_bits(gb, 2);
1501
            if (ctx->deltbae[i] == DBA_RESERVED) {
1502
                av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
1503
                return -1;
1504
            }
1505
        }
1506

    
1507
        if (ctx->cplinu)
1508
            if (ctx->cpldeltbae == DBA_NEW) { /*coupling delta offset, len and bit allocation */
1509
                ctx->cpldeltnseg = get_bits(gb, 3);
1510
                for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
1511
                    ctx->cpldeltoffst[seg] = get_bits(gb, 5);
1512
                    ctx->cpldeltlen[seg] = get_bits(gb, 4);
1513
                    ctx->cpldeltba[seg] = get_bits(gb, 3);
1514
                }
1515
            }
1516

    
1517
        for (i = 0; i < nfchans; i++)
1518
            if (ctx->deltbae[i] == DBA_NEW) {/*channel delta offset, len and bit allocation */
1519
                ctx->deltnseg[i] = get_bits(gb, 3);
1520
                for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
1521
                    ctx->deltoffst[i][seg] = get_bits(gb, 5);
1522
                    ctx->deltlen[i][seg] = get_bits(gb, 4);
1523
                    ctx->deltba[i][seg] = get_bits(gb, 3);
1524
                }
1525
            }
1526
    } else if(blk == 0) {
1527
        if(ctx->cplinu)
1528
            ctx->cpldeltbae = DBA_NONE;
1529
        for(i=0; i<nfchans; i++) {
1530
            ctx->deltbae[i] = DBA_NONE;
1531
        }
1532
    }
1533

    
1534
    if (bit_alloc_flags) {
1535
        if (ctx->cplinu && (bit_alloc_flags & 64))
1536
            do_bit_allocation(ctx, 5);
1537
        for (i = 0; i < nfchans; i++)
1538
            if ((bit_alloc_flags >> i) & 1)
1539
                do_bit_allocation(ctx, i);
1540
        if (ctx->lfeon && (bit_alloc_flags & 32))
1541
            do_bit_allocation(ctx, 6);
1542
    }
1543

    
1544
    if (get_bits1(gb)) { /* unused dummy data */
1545
        skipl = get_bits(gb, 9);
1546
        while(skipl--)
1547
            skip_bits(gb, 8);
1548
    }
1549
    /* unpack the transform coefficients
1550
     * * this also uncouples channels if coupling is in use.
1551
     */
1552
    if (get_transform_coeffs(ctx)) {
1553
        av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1554
        return -1;
1555
    }
1556

    
1557
    /* recover coefficients if rematrixing is in use */
1558
    if(ctx->acmod == AC3_ACMOD_STEREO)
1559
        do_rematrixing(ctx);
1560

    
1561
    do_downmix(ctx);
1562

    
1563
    do_imdct(ctx);
1564

    
1565
    return 0;
1566
}
1567

    
1568
static inline int16_t convert(int32_t i)
1569
{
1570
    if (i > 0x43c07fff)
1571
        return 32767;
1572
    else if (i <= 0x43bf8000)
1573
        return -32768;
1574
    else
1575
        return (i - 0x43c00000);
1576
}
1577

    
1578
/* Decode ac3 frame.
1579
 *
1580
 * @param avctx Pointer to AVCodecContext
1581
 * @param data Pointer to pcm smaples
1582
 * @param data_size Set to number of pcm samples produced by decoding
1583
 * @param buf Data to be decoded
1584
 * @param buf_size Size of the buffer
1585
 */
1586
static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1587
{
1588
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1589
    int16_t *out_samples = (int16_t *)data;
1590
    int i, j, k, start;
1591
    int32_t *int_ptr[6];
1592

    
1593
    for (i = 0; i < 6; i++)
1594
        int_ptr[i] = (int32_t *)(&ctx->output[i]);
1595

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

    
1599
    //Parse the syncinfo.
1600
    if (ac3_parse_header(ctx)) {
1601
        av_log(avctx, AV_LOG_ERROR, "\n");
1602
        *data_size = 0;
1603
        return buf_size;
1604
    }
1605

    
1606
    avctx->sample_rate = ctx->sampling_rate;
1607
    avctx->bit_rate = ctx->bit_rate;
1608

    
1609
    if (avctx->channels == 0) {
1610
        ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1611
        if (ctx->lfeon)
1612
            ctx->blkoutput |= AC3_OUTPUT_LFEON;
1613
        avctx->channels = ctx->nfchans + ctx->lfeon;
1614
    }
1615
    else if (avctx->channels == 1)
1616
        ctx->blkoutput |= AC3_OUTPUT_MONO;
1617
    else if (avctx->channels == 2) {
1618
        if (ctx->dsurmod == 0x02)
1619
            ctx->blkoutput |= AC3_OUTPUT_DOLBY;
1620
        else
1621
            ctx->blkoutput |= AC3_OUTPUT_STEREO;
1622
    }
1623
    else {
1624
        if (avctx->channels < (ctx->nfchans + ctx->lfeon))
1625
            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);
1626
        ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1627
        if (ctx->lfeon)
1628
            ctx->blkoutput |= AC3_OUTPUT_LFEON;
1629
        avctx->channels = ctx->nfchans + ctx->lfeon;
1630
    }
1631

    
1632
    //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);
1633

    
1634
    //Parse the Audio Blocks.
1635
    for (i = 0; i < NB_BLOCKS; i++) {
1636
        if (ac3_parse_audio_block(ctx, i)) {
1637
            av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1638
            *data_size = 0;
1639
            return ctx->frame_size;
1640
        }
1641
        start = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1;
1642
        for (k = 0; k < BLOCK_SIZE; k++)
1643
            for (j = start; j <= avctx->channels; j++)
1644
                *(out_samples++) = convert(int_ptr[j][k]);
1645
    }
1646
    *data_size = NB_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);
1647
    return ctx->frame_size;
1648
}
1649

    
1650
/* Uninitialize ac3 decoder.
1651
 */
1652
static int ac3_decode_end(AVCodecContext *avctx)
1653
{
1654
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1655
    ff_mdct_end(&ctx->imdct_512);
1656
    ff_mdct_end(&ctx->imdct_256);
1657

    
1658
    return 0;
1659
}
1660

    
1661
AVCodec ac3_decoder = {
1662
    .name = "ac3",
1663
    .type = CODEC_TYPE_AUDIO,
1664
    .id = CODEC_ID_AC3,
1665
    .priv_data_size = sizeof (AC3DecodeContext),
1666
    .init = ac3_decode_init,
1667
    .close = ac3_decode_end,
1668
    .decode = ac3_decode_frame,
1669
};
1670