Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3dec.c @ b6acc57f

History | View | Annotate | Download (38.4 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
/* table for exponent to scale_factor mapping
42
 * scale_factor[i] = 2 ^ -(i + 15)
43
 */
44
static float scale_factors[25];
45

    
46
/** table for grouping exponents */
47
static uint8_t exp_ungroup_tbl[128][3];
48

    
49
static int16_t l3_quantizers_1[32];
50
static int16_t l3_quantizers_2[32];
51
static int16_t l3_quantizers_3[32];
52

    
53
static int16_t l5_quantizers_1[128];
54
static int16_t l5_quantizers_2[128];
55
static int16_t l5_quantizers_3[128];
56

    
57
static int16_t l7_quantizers[7];
58

    
59
static int16_t l11_quantizers_1[128];
60
static int16_t l11_quantizers_2[128];
61

    
62
static int16_t l15_quantizers[15];
63

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

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

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

    
77
static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB };
78

    
79
#define AC3_OUTPUT_LFEON  8
80

    
81
typedef struct {
82
    int acmod;
83
    int cmixlev;
84
    int surmixlev;
85
    int dsurmod;
86

    
87
    int blksw[AC3_MAX_CHANNELS];
88
    int dithflag[AC3_MAX_CHANNELS];
89
    int cplinu;
90
    int chincpl[AC3_MAX_CHANNELS];
91
    int phsflginu;
92
    int cplcoe;
93
    uint32_t cplbndstrc;
94
    int rematstr;
95
    int rematflg[AC3_MAX_CHANNELS];
96
    int cplexpstr;
97
    int lfeexpstr;
98
    int chexpstr[5];
99
    int cplsnroffst;
100
    int cplfgain;
101
    int snroffst[5];
102
    int fgain[5];
103
    int lfesnroffst;
104
    int lfefgain;
105
    int cpldeltbae;
106
    int deltbae[5];
107
    int cpldeltnseg;
108
    uint8_t  cpldeltoffst[8];
109
    uint8_t  cpldeltlen[8];
110
    uint8_t  cpldeltba[8];
111
    int deltnseg[5];
112
    uint8_t  deltoffst[5][8];
113
    uint8_t  deltlen[5][8];
114
    uint8_t  deltba[5][8];
115

    
116
    /* Derived Attributes. */
117
    int      sampling_rate;
118
    int      bit_rate;
119
    int      frame_size;
120

    
121
    int      nchans;            //number of total channels
122
    int      nfchans;           //number of full-bandwidth channels
123
    int      lfeon;             //lfe channel in use
124
    int      output_mode;       ///< output channel configuration
125
    int      out_channels;      ///< number of output channels
126

    
127
    float    dynrng;            //dynamic range gain
128
    float    dynrng2;           //dynamic range gain for 1+1 mode
129
    float    cplco[5][18];      //coupling coordinates
130
    int      ncplbnd;           //number of coupling bands
131
    int      ncplsubnd;         //number of coupling sub bands
132
    int      cplstrtmant;       //coupling start mantissa
133
    int      cplendmant;        //coupling end mantissa
134
    int      endmant[5];        //channel end mantissas
135
    AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
136

    
137
    int8_t   dcplexps[256];     //decoded coupling exponents
138
    int8_t   dexps[5][256];     //decoded fbw channel exponents
139
    int8_t   dlfeexps[256];     //decoded lfe channel exponents
140
    uint8_t  cplbap[256];       //coupling bit allocation pointers
141
    uint8_t  bap[5][256];       //fbw channel bit allocation pointers
142
    uint8_t  lfebap[256];       //lfe channel bit allocation pointers
143

    
144
    DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][256]);  //transform coefficients
145

    
146
    /* For IMDCT. */
147
    MDCTContext imdct_512;  //for 512 sample imdct transform
148
    MDCTContext imdct_256;  //for 256 sample imdct transform
149
    DSPContext  dsp;        //for optimization
150

    
151
    DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS][256]);   //output after imdct transform and windowing
152
    DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS][256]);    //delay - added to the next block
153
    DECLARE_ALIGNED_16(float, tmp_imdct[256]);                  //temporary storage for imdct transform
154
    DECLARE_ALIGNED_16(float, tmp_output[512]);                 //temporary storage for output before windowing
155
    DECLARE_ALIGNED_16(float, window[256]);                     //window coefficients
156

    
157
    /* Miscellaneous. */
158
    GetBitContext gb;
159
    AVRandomState dith_state;   //for dither generation
160
} AC3DecodeContext;
161

    
162
/*********** BEGIN INIT HELPER FUNCTIONS ***********/
163
/**
164
 * Generate a Kaiser-Bessel Derived Window.
165
 */
166
static void ac3_window_init(float *window)
167
{
168
   int i, j;
169
   double sum = 0.0, bessel, tmp;
170
   double local_window[256];
171
   double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
172

    
173
   for (i = 0; i < 256; i++) {
174
       tmp = i * (256 - i) * alpha2;
175
       bessel = 1.0;
176
       for (j = 100; j > 0; j--) /* defaul to 100 iterations */
177
           bessel = bessel * tmp / (j * j) + 1;
178
       sum += bessel;
179
       local_window[i] = sum;
180
   }
181

    
182
   sum++;
183
   for (i = 0; i < 256; i++)
184
       window[i] = sqrt(local_window[i] / sum);
185
}
186

    
187
/*
188
 * Generate quantizer tables.
189
 */
190
static void generate_quantizers_table(int16_t quantizers[], int level, int length)
191
{
192
    int i;
193

    
194
    for (i = 0; i < length; i++)
195
        quantizers[i] = ((2 * i - level + 1) << 15) / level;
196
}
197

    
198
static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
199
{
200
    int i, j;
201
    int16_t v;
202

    
203
    for (i = 0; i < length1; i++) {
204
        v = ((2 * i - level + 1) << 15) / level;
205
        for (j = 0; j < length2; j++)
206
            quantizers[i * length2 + j] = v;
207
    }
208

    
209
    for (i = length1 * length2; i < size; i++)
210
        quantizers[i] = 0;
211
}
212

    
213
static void generate_quantizers_table_2(int16_t quantizers[], int level, int length1, int length2, int size)
214
{
215
    int i, j;
216
    int16_t v;
217

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

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

    
227
}
228

    
229
static void generate_quantizers_table_3(int16_t quantizers[], int level, int length1, int length2, int size)
230
{
231
    int i, j;
232

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

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

    
241
/*
242
 * Initialize tables at runtime.
243
 */
244
static void ac3_tables_init(void)
245
{
246
    int i;
247

    
248
    /* Quantizer ungrouping tables. */
249
    // for level-3 quantizers
250
    generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
251
    generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
252
    generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
253

    
254
    //for level-5 quantizers
255
    generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
256
    generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
257
    generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
258

    
259
    //for level-7 quantizers
260
    generate_quantizers_table(l7_quantizers, 7, 7);
261

    
262
    //for level-4 quantizers
263
    generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
264
    generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
265

    
266
    //for level-15 quantizers
267
    generate_quantizers_table(l15_quantizers, 15, 15);
268
    /* End Quantizer ungrouping tables. */
269

    
270
    //generate scale factors
271
    for (i = 0; i < 25; i++)
272
        scale_factors[i] = pow(2.0, -(i + 15));
273

    
274
    /* generate exponent tables
275
       reference: Section 7.1.3 Exponent Decoding */
276
    for(i=0; i<128; i++) {
277
        exp_ungroup_tbl[i][0] =  i / 25;
278
        exp_ungroup_tbl[i][1] = (i % 25) / 5;
279
        exp_ungroup_tbl[i][2] = (i % 25) % 5;
280
    }
281
}
282

    
283

    
284
static int ac3_decode_init(AVCodecContext *avctx)
285
{
286
    AC3DecodeContext *ctx = avctx->priv_data;
287

    
288
    ac3_common_init();
289
    ac3_tables_init();
290
    ff_mdct_init(&ctx->imdct_256, 8, 1);
291
    ff_mdct_init(&ctx->imdct_512, 9, 1);
292
    ac3_window_init(ctx->window);
293
    dsputil_init(&ctx->dsp, avctx);
294
    av_init_random(0, &ctx->dith_state);
295

    
296
    return 0;
297
}
298
/*********** END INIT FUNCTIONS ***********/
299

    
300
/**
301
 * Parses the 'sync info' and 'bit stream info' from the AC-3 bitstream.
302
 * GetBitContext within AC3DecodeContext must point to
303
 * start of the synchronized ac3 bitstream.
304
 */
305
static int ac3_parse_header(AC3DecodeContext *ctx)
306
{
307
    AC3HeaderInfo hdr;
308
    GetBitContext *gb = &ctx->gb;
309
    int err, i;
310

    
311
    err = ff_ac3_parse_header(gb->buffer, &hdr);
312
    if(err)
313
        return err;
314

    
315
    /* get decoding parameters from header info */
316
    ctx->bit_alloc_params.fscod       = hdr.fscod;
317
    ctx->acmod                        = hdr.acmod;
318
    ctx->cmixlev                      = hdr.cmixlev;
319
    ctx->surmixlev                    = hdr.surmixlev;
320
    ctx->dsurmod                      = hdr.dsurmod;
321
    ctx->lfeon                        = hdr.lfeon;
322
    ctx->bit_alloc_params.halfratecod = hdr.halfratecod;
323
    ctx->sampling_rate                = hdr.sample_rate;
324
    ctx->bit_rate                     = hdr.bit_rate;
325
    ctx->nchans                       = hdr.channels;
326
    ctx->nfchans                      = ctx->nchans - ctx->lfeon;
327
    ctx->frame_size                   = hdr.frame_size;
328

    
329
    /* set default output to all source channels */
330
    ctx->out_channels = ctx->nchans;
331
    ctx->output_mode = ctx->acmod;
332
    if(ctx->lfeon)
333
        ctx->output_mode |= AC3_OUTPUT_LFEON;
334

    
335
    /* skip over portion of header which has already been read */
336
    skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
337
    skip_bits(gb, 16); // skip crc1
338
    skip_bits(gb, 8);  // skip fscod and frmsizecod
339
    skip_bits(gb, 11); // skip bsid, bsmod, and acmod
340
    if(ctx->acmod == AC3_ACMOD_STEREO) {
341
        skip_bits(gb, 2); // skip dsurmod
342
    } else {
343
        if((ctx->acmod & 1) && ctx->acmod != AC3_ACMOD_MONO)
344
            skip_bits(gb, 2); // skip cmixlev
345
        if(ctx->acmod & 4)
346
            skip_bits(gb, 2); // skip surmixlev
347
    }
348
    skip_bits1(gb); // skip lfeon
349

    
350
    /* read the rest of the bsi. read twice for dual mono mode. */
351
    i = !(ctx->acmod);
352
    do {
353
        skip_bits(gb, 5); //skip dialog normalization
354
        if (get_bits1(gb))
355
            skip_bits(gb, 8); //skip compression
356
        if (get_bits1(gb))
357
            skip_bits(gb, 8); //skip language code
358
        if (get_bits1(gb))
359
            skip_bits(gb, 7); //skip audio production information
360
    } while (i--);
361

    
362
    skip_bits(gb, 2); //skip copyright bit and original bitstream bit
363

    
364
    /* FIXME: read & use the xbsi1 downmix levels */
365
    if (get_bits1(gb))
366
        skip_bits(gb, 14); //skip timecode1
367
    if (get_bits1(gb))
368
        skip_bits(gb, 14); //skip timecode2
369

    
370
    if (get_bits1(gb)) {
371
        i = get_bits(gb, 6); //additional bsi length
372
        do {
373
            skip_bits(gb, 8);
374
        } while(i--);
375
    }
376

    
377
    return 0;
378
}
379

    
380
/**
381
 * Decodes the grouped exponents.
382
 * This function decodes the coded exponents according to exponent strategy
383
 * and stores them in the decoded exponents buffer.
384
 *
385
 * @param[in]  gb      GetBitContext which points to start of coded exponents
386
 * @param[in]  expstr  Exponent coding strategy
387
 * @param[in]  ngrps   Number of grouped exponents
388
 * @param[in]  absexp  Absolute exponent or DC exponent
389
 * @param[out] dexps   Decoded exponents are stored in dexps
390
 */
391
static void decode_exponents(GetBitContext *gb, int expstr, int ngrps,
392
                             uint8_t absexp, int8_t *dexps)
393
{
394
    int i, j, grp, grpsize;
395
    int dexp[256];
396
    int expacc, prevexp;
397

    
398
    /* unpack groups */
399
    grpsize = expstr + (expstr == EXP_D45);
400
    for(grp=0,i=0; grp<ngrps; grp++) {
401
        expacc = get_bits(gb, 7);
402
        dexp[i++] = exp_ungroup_tbl[expacc][0];
403
        dexp[i++] = exp_ungroup_tbl[expacc][1];
404
        dexp[i++] = exp_ungroup_tbl[expacc][2];
405
    }
406

    
407
    /* convert to absolute exps and expand groups */
408
    prevexp = absexp;
409
    for(i=0; i<ngrps*3; i++) {
410
        prevexp = av_clip(prevexp + dexp[i]-2, 0, 24);
411
        for(j=0; j<grpsize; j++) {
412
            dexps[(i*grpsize)+j] = prevexp;
413
        }
414
    }
415
}
416

    
417
typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
418
    int16_t l3_quantizers[3];
419
    int16_t l5_quantizers[3];
420
    int16_t l11_quantizers[2];
421
    int l3ptr;
422
    int l5ptr;
423
    int l11ptr;
424
} mant_groups;
425

    
426
/* Get the transform coefficients for coupling channel and uncouple channels.
427
 * The coupling transform coefficients starts at the the cplstrtmant, which is
428
 * equal to endmant[ch] for fbw channels. Hence we can uncouple channels before
429
 * getting transform coefficients for the channel.
430
 */
431
static int get_transform_coeffs_cpling(AC3DecodeContext *ctx, mant_groups *m)
432
{
433
    GetBitContext *gb = &ctx->gb;
434
    int ch, start, end, cplbndstrc, bnd, gcode, tbap;
435
    float cplcos[5], cplcoeff;
436
    uint8_t *exps = ctx->dcplexps;
437
    uint8_t *bap = ctx->cplbap;
438

    
439
    cplbndstrc = ctx->cplbndstrc;
440
    start = ctx->cplstrtmant;
441
    bnd = 0;
442

    
443
    while (start < ctx->cplendmant) {
444
        end = start + 12;
445
        while (cplbndstrc & 1) {
446
            end += 12;
447
            cplbndstrc >>= 1;
448
        }
449
        cplbndstrc >>= 1;
450
        for (ch = 0; ch < ctx->nfchans; ch++)
451
            cplcos[ch] = ctx->cplco[ch][bnd];
452
        bnd++;
453

    
454
        while (start < end) {
455
            tbap = bap[start];
456
            switch(tbap) {
457
                case 0:
458
                    for (ch = 0; ch < ctx->nfchans; ch++)
459
                        if (ctx->chincpl[ch]) {
460
                            if (ctx->dithflag[ch]) {
461
                                cplcoeff = (av_random(&ctx->dith_state) & 0xFFFF) * scale_factors[exps[start]];
462
                                ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch] * LEVEL_MINUS_3DB;
463
                            } else
464
                                ctx->transform_coeffs[ch + 1][start] = 0;
465
                        }
466
                    start++;
467
                    continue;
468
                case 1:
469
                    if (m->l3ptr > 2) {
470
                        gcode = get_bits(gb, 5);
471
                        m->l3_quantizers[0] = l3_quantizers_1[gcode];
472
                        m->l3_quantizers[1] = l3_quantizers_2[gcode];
473
                        m->l3_quantizers[2] = l3_quantizers_3[gcode];
474
                        m->l3ptr = 0;
475
                    }
476
                    cplcoeff = m->l3_quantizers[m->l3ptr++] * scale_factors[exps[start]];
477
                    break;
478

    
479
                case 2:
480
                    if (m->l5ptr > 2) {
481
                        gcode = get_bits(gb, 7);
482
                        m->l5_quantizers[0] = l5_quantizers_1[gcode];
483
                        m->l5_quantizers[1] = l5_quantizers_2[gcode];
484
                        m->l5_quantizers[2] = l5_quantizers_3[gcode];
485
                        m->l5ptr = 0;
486
                    }
487
                    cplcoeff = m->l5_quantizers[m->l5ptr++] * scale_factors[exps[start]];
488
                    break;
489

    
490
                case 3:
491
                    cplcoeff = l7_quantizers[get_bits(gb, 3)] * scale_factors[exps[start]];
492
                    break;
493

    
494
                case 4:
495
                    if (m->l11ptr > 1) {
496
                        gcode = get_bits(gb, 7);
497
                        m->l11_quantizers[0] = l11_quantizers_1[gcode];
498
                        m->l11_quantizers[1] = l11_quantizers_2[gcode];
499
                        m->l11ptr = 0;
500
                    }
501
                    cplcoeff = m->l11_quantizers[m->l11ptr++] * scale_factors[exps[start]];
502
                    break;
503

    
504
                case 5:
505
                    cplcoeff = l15_quantizers[get_bits(gb, 4)] * scale_factors[exps[start]];
506
                    break;
507

    
508
                default:
509
                    cplcoeff = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * scale_factors[exps[start]];
510
            }
511
            for (ch = 0; ch < ctx->nfchans; ch++)
512
                if (ctx->chincpl[ch])
513
                    ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
514
            start++;
515
        }
516
    }
517

    
518
    return 0;
519
}
520

    
521
/* Get the transform coefficients for particular channel */
522
static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
523
{
524
    GetBitContext *gb = &ctx->gb;
525
    int i, gcode, tbap, dithflag, end;
526
    uint8_t *exps;
527
    uint8_t *bap;
528
    float *coeffs;
529

    
530
    if (ch_index != -1) { /* fbw channels */
531
        dithflag = ctx->dithflag[ch_index];
532
        exps = ctx->dexps[ch_index];
533
        bap = ctx->bap[ch_index];
534
        coeffs = ctx->transform_coeffs[ch_index + 1];
535
        end = ctx->endmant[ch_index];
536
    } else if (ch_index == -1) {
537
        dithflag = 0;
538
        exps = ctx->dlfeexps;
539
        bap = ctx->lfebap;
540
        coeffs = ctx->transform_coeffs[0];
541
        end = 7;
542
    }
543

    
544

    
545
    for (i = 0; i < end; i++) {
546
        tbap = bap[i];
547
        switch (tbap) {
548
            case 0:
549
                if (!dithflag) {
550
                    coeffs[i] = 0;
551
                    continue;
552
                }
553
                else {
554
                    coeffs[i] = (av_random(&ctx->dith_state) & 0xFFFF) * scale_factors[exps[i]];
555
                    coeffs[i] *= LEVEL_MINUS_3DB;
556
                    continue;
557
                }
558

    
559
            case 1:
560
                if (m->l3ptr > 2) {
561
                    gcode = get_bits(gb, 5);
562
                    m->l3_quantizers[0] = l3_quantizers_1[gcode];
563
                    m->l3_quantizers[1] = l3_quantizers_2[gcode];
564
                    m->l3_quantizers[2] = l3_quantizers_3[gcode];
565
                    m->l3ptr = 0;
566
                }
567
                coeffs[i] = m->l3_quantizers[m->l3ptr++] * scale_factors[exps[i]];
568
                continue;
569

    
570
            case 2:
571
                if (m->l5ptr > 2) {
572
                    gcode = get_bits(gb, 7);
573
                    m->l5_quantizers[0] = l5_quantizers_1[gcode];
574
                    m->l5_quantizers[1] = l5_quantizers_2[gcode];
575
                    m->l5_quantizers[2] = l5_quantizers_3[gcode];
576
                    m->l5ptr = 0;
577
                }
578
                coeffs[i] = m->l5_quantizers[m->l5ptr++] * scale_factors[exps[i]];
579
                continue;
580

    
581
            case 3:
582
                coeffs[i] = l7_quantizers[get_bits(gb, 3)] * scale_factors[exps[i]];
583
                continue;
584

    
585
            case 4:
586
                if (m->l11ptr > 1) {
587
                    gcode = get_bits(gb, 7);
588
                    m->l11_quantizers[0] = l11_quantizers_1[gcode];
589
                    m->l11_quantizers[1] = l11_quantizers_2[gcode];
590
                    m->l11ptr = 0;
591
                }
592
                coeffs[i] = m->l11_quantizers[m->l11ptr++] * scale_factors[exps[i]];
593
                continue;
594

    
595
            case 5:
596
                coeffs[i] = l15_quantizers[get_bits(gb, 4)] * scale_factors[exps[i]];
597
                continue;
598

    
599
            default:
600
                coeffs[i] = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * scale_factors[exps[i]];
601
                continue;
602
        }
603
    }
604

    
605
    return 0;
606
}
607

    
608
/* Get the transform coefficients.
609
 * This function extracts the tranform coefficients form the ac3 bitstream.
610
 * This function is called after bit allocation is performed.
611
 */
612
static int get_transform_coeffs(AC3DecodeContext * ctx)
613
{
614
    int i, end;
615
    int got_cplchan = 0;
616
    mant_groups m;
617

    
618
    m.l3ptr = m.l5ptr = m.l11ptr = 3;
619

    
620
    for (i = 0; i < ctx->nfchans; i++) {
621
        /* transform coefficients for individual channel */
622
        if (get_transform_coeffs_ch(ctx, i, &m))
623
            return -1;
624
        /* tranform coefficients for coupling channels */
625
        if (ctx->chincpl[i])  {
626
            if (!got_cplchan) {
627
                if (get_transform_coeffs_cpling(ctx, &m)) {
628
                    av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
629
                    return -1;
630
                }
631
                got_cplchan = 1;
632
            }
633
            end = ctx->cplendmant;
634
        } else
635
            end = ctx->endmant[i];
636
        do
637
            ctx->transform_coeffs[i + 1][end] = 0;
638
        while(++end < 256);
639
    }
640
    if (ctx->lfeon) {
641
        if (get_transform_coeffs_ch(ctx, -1, &m))
642
                return -1;
643
        for (i = 7; i < 256; i++) {
644
            ctx->transform_coeffs[0][i] = 0;
645
        }
646
    }
647

    
648
    return 0;
649
}
650

    
651
/* Rematrixing routines. */
652
static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
653
{
654
    float tmp0, tmp1;
655

    
656
    while (start < end) {
657
        tmp0 = ctx->transform_coeffs[1][start];
658
        tmp1 = ctx->transform_coeffs[2][start];
659
        ctx->transform_coeffs[1][start] = tmp0 + tmp1;
660
        ctx->transform_coeffs[2][start] = tmp0 - tmp1;
661
        start++;
662
    }
663
}
664

    
665
static void do_rematrixing(AC3DecodeContext *ctx)
666
{
667
    int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
668
    int end, bndend;
669

    
670
    end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
671

    
672
    if (ctx->rematflg[0])
673
        do_rematrixing1(ctx, bnd1, bnd2);
674

    
675
    if (ctx->rematflg[1])
676
        do_rematrixing1(ctx, bnd2, bnd3);
677

    
678
    bndend = bnd4;
679
    if (bndend > end) {
680
        bndend = end;
681
        if (ctx->rematflg[2])
682
            do_rematrixing1(ctx, bnd3, bndend);
683
    } else {
684
        if (ctx->rematflg[2])
685
            do_rematrixing1(ctx, bnd3, bnd4);
686
        if (ctx->rematflg[3])
687
            do_rematrixing1(ctx, bnd4, end);
688
    }
689
}
690

    
691
/* This function performs the imdct on 256 sample transform
692
 * coefficients.
693
 */
694
static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
695
{
696
    int i, k;
697
    float x[128];
698
    FFTComplex z[2][64];
699
    float *o_ptr = ctx->tmp_output;
700

    
701
    for(i=0; i<2; i++) {
702
        /* de-interleave coefficients */
703
        for(k=0; k<128; k++) {
704
            x[k] = ctx->transform_coeffs[chindex][2*k+i];
705
        }
706

    
707
        /* run standard IMDCT */
708
        ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, o_ptr, x, ctx->tmp_imdct);
709

    
710
        /* reverse the post-rotation & reordering from standard IMDCT */
711
        for(k=0; k<32; k++) {
712
            z[i][32+k].re = -o_ptr[128+2*k];
713
            z[i][32+k].im = -o_ptr[2*k];
714
            z[i][31-k].re =  o_ptr[2*k+1];
715
            z[i][31-k].im =  o_ptr[128+2*k+1];
716
        }
717
    }
718

    
719
    /* apply AC-3 post-rotation & reordering */
720
    for(k=0; k<64; k++) {
721
        o_ptr[    2*k  ] = -z[0][   k].im;
722
        o_ptr[    2*k+1] =  z[0][63-k].re;
723
        o_ptr[128+2*k  ] = -z[0][   k].re;
724
        o_ptr[128+2*k+1] =  z[0][63-k].im;
725
        o_ptr[256+2*k  ] = -z[1][   k].re;
726
        o_ptr[256+2*k+1] =  z[1][63-k].im;
727
        o_ptr[384+2*k  ] =  z[1][   k].im;
728
        o_ptr[384+2*k+1] = -z[1][63-k].re;
729
    }
730
}
731

    
732
/* IMDCT Transform. */
733
static inline void do_imdct(AC3DecodeContext *ctx)
734
{
735
    int ch;
736

    
737
    if (ctx->output_mode & AC3_OUTPUT_LFEON) {
738
        ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
739
                                      ctx->transform_coeffs[0], ctx->tmp_imdct);
740
        ctx->dsp.vector_fmul_add_add(ctx->output[0], ctx->tmp_output,
741
                                     ctx->window, ctx->delay[0], 384, 256, 1);
742
        ctx->dsp.vector_fmul_reverse(ctx->delay[0], ctx->tmp_output+256,
743
                                     ctx->window, 256);
744
    }
745
    for (ch=1; ch<=ctx->nfchans; ch++) {
746
        if (ctx->blksw[ch-1])
747
            do_imdct_256(ctx, ch);
748
        else
749
            ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
750
                                          ctx->transform_coeffs[ch],
751
                                          ctx->tmp_imdct);
752

    
753
        ctx->dsp.vector_fmul_add_add(ctx->output[ch], ctx->tmp_output,
754
                                     ctx->window, ctx->delay[ch], 384, 256, 1);
755
        ctx->dsp.vector_fmul_reverse(ctx->delay[ch], ctx->tmp_output+256,
756
                                     ctx->window, 256);
757
    }
758
}
759

    
760
/* Parse the audio block from ac3 bitstream.
761
 * This function extract the audio block from the ac3 bitstream
762
 * and produces the output for the block. This function must
763
 * be called for each of the six audio block in the ac3 bitstream.
764
 */
765
static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
766
{
767
    int nfchans = ctx->nfchans;
768
    int acmod = ctx->acmod;
769
    int i, bnd, rbnd, seg, grpsize, ch;
770
    GetBitContext *gb = &ctx->gb;
771
    int bit_alloc_flags = 0;
772
    int8_t *dexps;
773
    int mstrcplco, cplcoexp, cplcomant;
774
    int dynrng, chbwcod, ngrps, cplabsexp, skipl;
775

    
776
    for (i = 0; i < nfchans; i++) /*block switch flag */
777
        ctx->blksw[i] = get_bits1(gb);
778

    
779
    for (i = 0; i < nfchans; i++) /* dithering flag */
780
        ctx->dithflag[i] = get_bits1(gb);
781

    
782
    if (get_bits1(gb)) { /* dynamic range */
783
        dynrng = get_sbits(gb, 8);
784
        ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
785
    } else if(blk == 0) {
786
        ctx->dynrng = 1.0;
787
    }
788

    
789
    if(acmod == AC3_ACMOD_DUALMONO) { /* dynamic range 1+1 mode */
790
        if(get_bits1(gb)) {
791
            dynrng = get_sbits(gb, 8);
792
            ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
793
        } else if(blk == 0) {
794
            ctx->dynrng2 = 1.0;
795
        }
796
    }
797

    
798
    if (get_bits1(gb)) { /* coupling strategy */
799
        ctx->cplinu = get_bits1(gb);
800
        ctx->cplbndstrc = 0;
801
        if (ctx->cplinu) { /* coupling in use */
802
            int cplbegf, cplendf;
803

    
804
            for (i = 0; i < nfchans; i++)
805
                ctx->chincpl[i] = get_bits1(gb);
806

    
807
            if (acmod == AC3_ACMOD_STEREO)
808
                ctx->phsflginu = get_bits1(gb); //phase flag in use
809

    
810
            cplbegf = get_bits(gb, 4);
811
            cplendf = get_bits(gb, 4);
812

    
813
            if (3 + cplendf - cplbegf < 0) {
814
                av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", cplendf, cplbegf);
815
                return -1;
816
            }
817

    
818
            ctx->ncplbnd = ctx->ncplsubnd = 3 + cplendf - cplbegf;
819
            ctx->cplstrtmant = cplbegf * 12 + 37;
820
            ctx->cplendmant = cplendf * 12 + 73;
821
            for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
822
                if (get_bits1(gb)) {
823
                    ctx->cplbndstrc |= 1 << i;
824
                    ctx->ncplbnd--;
825
                }
826
        } else {
827
            for (i = 0; i < nfchans; i++)
828
                ctx->chincpl[i] = 0;
829
        }
830
    }
831

    
832
    if (ctx->cplinu) {
833
        ctx->cplcoe = 0;
834

    
835
        for (i = 0; i < nfchans; i++)
836
            if (ctx->chincpl[i])
837
                if (get_bits1(gb)) { /* coupling co-ordinates */
838
                    ctx->cplcoe |= 1 << i;
839
                    mstrcplco = 3 * get_bits(gb, 2);
840
                    for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
841
                        cplcoexp = get_bits(gb, 4);
842
                        cplcomant = get_bits(gb, 4);
843
                        if (cplcoexp == 15)
844
                            cplcomant <<= 14;
845
                        else
846
                            cplcomant = (cplcomant | 0x10) << 13;
847
                        ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
848
                    }
849
                }
850

    
851
        if (acmod == AC3_ACMOD_STEREO && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
852
            for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
853
                if (get_bits1(gb))
854
                    ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
855
    }
856

    
857
    if (acmod == AC3_ACMOD_STEREO) {/* rematrixing */
858
        ctx->rematstr = get_bits1(gb);
859
        if (ctx->rematstr) {
860
            if (!(ctx->cplinu) || ctx->cplstrtmant > 61)
861
                for (rbnd = 0; rbnd < 4; rbnd++)
862
                    ctx->rematflg[rbnd] = get_bits1(gb);
863
            if (ctx->cplstrtmant > 37 && ctx->cplstrtmant <= 61 && ctx->cplinu)
864
                for (rbnd = 0; rbnd < 3; rbnd++)
865
                    ctx->rematflg[rbnd] = get_bits1(gb);
866
            if (ctx->cplstrtmant == 37 && ctx->cplinu)
867
                for (rbnd = 0; rbnd < 2; rbnd++)
868
                    ctx->rematflg[rbnd] = get_bits1(gb);
869
        }
870
    }
871

    
872
    ctx->cplexpstr = EXP_REUSE;
873
    ctx->lfeexpstr = EXP_REUSE;
874
    if (ctx->cplinu) /* coupling exponent strategy */
875
        ctx->cplexpstr = get_bits(gb, 2);
876
    for (i = 0; i < nfchans; i++)  /* channel exponent strategy */
877
        ctx->chexpstr[i] = get_bits(gb, 2);
878
    if (ctx->lfeon)  /* lfe exponent strategy */
879
        ctx->lfeexpstr = get_bits1(gb);
880

    
881
    for (i = 0; i < nfchans; i++) /* channel bandwidth code */
882
        if (ctx->chexpstr[i] != EXP_REUSE) {
883
            if (ctx->chincpl[i])
884
                ctx->endmant[i] = ctx->cplstrtmant;
885
            else {
886
                chbwcod = get_bits(gb, 6);
887
                if (chbwcod > 60) {
888
                    av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
889
                    return -1;
890
                }
891
                ctx->endmant[i] = chbwcod * 3 + 73;
892
            }
893
        }
894

    
895
    if (ctx->cplexpstr != EXP_REUSE) {/* coupling exponents */
896
        bit_alloc_flags = 64;
897
        cplabsexp = get_bits(gb, 4) << 1;
898
        ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
899
        decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant);
900
    }
901

    
902
    for (i = 0; i < nfchans; i++) /* fbw channel exponents */
903
        if (ctx->chexpstr[i] != EXP_REUSE) {
904
            bit_alloc_flags |= 1 << i;
905
            grpsize = 3 << (ctx->chexpstr[i] - 1);
906
            ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
907
            dexps = ctx->dexps[i];
908
            dexps[0] = get_bits(gb, 4);
909
            decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1);
910
            skip_bits(gb, 2); /* skip gainrng */
911
        }
912

    
913
    if (ctx->lfeexpstr != EXP_REUSE) { /* lfe exponents */
914
        bit_alloc_flags |= 32;
915
        ctx->dlfeexps[0] = get_bits(gb, 4);
916
        decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1);
917
    }
918

    
919
    if (get_bits1(gb)) { /* bit allocation information */
920
        bit_alloc_flags = 127;
921
        ctx->bit_alloc_params.sdecay = ff_sdecaytab[get_bits(gb, 2)];
922
        ctx->bit_alloc_params.fdecay = ff_fdecaytab[get_bits(gb, 2)];
923
        ctx->bit_alloc_params.sgain  = ff_sgaintab[get_bits(gb, 2)];
924
        ctx->bit_alloc_params.dbknee = ff_dbkneetab[get_bits(gb, 2)];
925
        ctx->bit_alloc_params.floor  = ff_floortab[get_bits(gb, 3)];
926
    }
927

    
928
    if (get_bits1(gb)) { /* snroffset */
929
        int csnr;
930
        bit_alloc_flags = 127;
931
        csnr = (get_bits(gb, 6) - 15) << 4;
932
        if (ctx->cplinu) { /* coupling fine snr offset and fast gain code */
933
            ctx->cplsnroffst = (csnr + get_bits(gb, 4)) << 2;
934
            ctx->cplfgain = ff_fgaintab[get_bits(gb, 3)];
935
        }
936
        for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
937
            ctx->snroffst[i] = (csnr + get_bits(gb, 4)) << 2;
938
            ctx->fgain[i] = ff_fgaintab[get_bits(gb, 3)];
939
        }
940
        if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
941
            ctx->lfesnroffst = (csnr + get_bits(gb, 4)) << 2;
942
            ctx->lfefgain = ff_fgaintab[get_bits(gb, 3)];
943
        }
944
    }
945

    
946
    if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
947
        bit_alloc_flags |= 64;
948
        ctx->bit_alloc_params.cplfleak = get_bits(gb, 3);
949
        ctx->bit_alloc_params.cplsleak = get_bits(gb, 3);
950
    }
951

    
952
    if (get_bits1(gb)) { /* delta bit allocation information */
953
        bit_alloc_flags = 127;
954

    
955
        if (ctx->cplinu) {
956
            ctx->cpldeltbae = get_bits(gb, 2);
957
            if (ctx->cpldeltbae == DBA_RESERVED) {
958
                av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
959
                return -1;
960
            }
961
        }
962

    
963
        for (i = 0; i < nfchans; i++) {
964
            ctx->deltbae[i] = get_bits(gb, 2);
965
            if (ctx->deltbae[i] == DBA_RESERVED) {
966
                av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
967
                return -1;
968
            }
969
        }
970

    
971
        if (ctx->cplinu)
972
            if (ctx->cpldeltbae == DBA_NEW) { /*coupling delta offset, len and bit allocation */
973
                ctx->cpldeltnseg = get_bits(gb, 3);
974
                for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
975
                    ctx->cpldeltoffst[seg] = get_bits(gb, 5);
976
                    ctx->cpldeltlen[seg] = get_bits(gb, 4);
977
                    ctx->cpldeltba[seg] = get_bits(gb, 3);
978
                }
979
            }
980

    
981
        for (i = 0; i < nfchans; i++)
982
            if (ctx->deltbae[i] == DBA_NEW) {/*channel delta offset, len and bit allocation */
983
                ctx->deltnseg[i] = get_bits(gb, 3);
984
                for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
985
                    ctx->deltoffst[i][seg] = get_bits(gb, 5);
986
                    ctx->deltlen[i][seg] = get_bits(gb, 4);
987
                    ctx->deltba[i][seg] = get_bits(gb, 3);
988
                }
989
            }
990
    } else if(blk == 0) {
991
        if(ctx->cplinu)
992
            ctx->cpldeltbae = DBA_NONE;
993
        for(i=0; i<nfchans; i++) {
994
            ctx->deltbae[i] = DBA_NONE;
995
        }
996
    }
997

    
998
    if (bit_alloc_flags) {
999
        if (ctx->cplinu && (bit_alloc_flags & 64))
1000
            ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->cplbap,
1001
                                          ctx->dcplexps, ctx->cplstrtmant,
1002
                                          ctx->cplendmant, ctx->cplsnroffst,
1003
                                          ctx->cplfgain, 0,
1004
                                          ctx->cpldeltbae, ctx->cpldeltnseg,
1005
                                          ctx->cpldeltoffst, ctx->cpldeltlen,
1006
                                          ctx->cpldeltba);
1007
        for (i = 0; i < nfchans; i++)
1008
            if ((bit_alloc_flags >> i) & 1)
1009
                ac3_parametric_bit_allocation(&ctx->bit_alloc_params,
1010
                                              ctx->bap[i], ctx->dexps[i], 0,
1011
                                              ctx->endmant[i], ctx->snroffst[i],
1012
                                              ctx->fgain[i], 0, ctx->deltbae[i],
1013
                                              ctx->deltnseg[i], ctx->deltoffst[i],
1014
                                              ctx->deltlen[i], ctx->deltba[i]);
1015
        if (ctx->lfeon && (bit_alloc_flags & 32))
1016
            ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->lfebap,
1017
                                          ctx->dlfeexps, 0, 7, ctx->lfesnroffst,
1018
                                          ctx->lfefgain, 1,
1019
                                          DBA_NONE, 0, NULL, NULL, NULL);
1020
    }
1021

    
1022
    if (get_bits1(gb)) { /* unused dummy data */
1023
        skipl = get_bits(gb, 9);
1024
        while(skipl--)
1025
            skip_bits(gb, 8);
1026
    }
1027
    /* unpack the transform coefficients
1028
     * * this also uncouples channels if coupling is in use.
1029
     */
1030
    if (get_transform_coeffs(ctx)) {
1031
        av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1032
        return -1;
1033
    }
1034

    
1035
    /* recover coefficients if rematrixing is in use */
1036
    if(ctx->acmod == AC3_ACMOD_STEREO)
1037
        do_rematrixing(ctx);
1038

    
1039
    /* apply scaling to coefficients (headroom, dynrng) */
1040
    if(ctx->lfeon) {
1041
        for(i=0; i<7; i++) {
1042
            ctx->transform_coeffs[0][i] *= 2.0f * ctx->dynrng;
1043
        }
1044
    }
1045
    for(ch=1; ch<=ctx->nfchans; ch++) {
1046
        float gain = 2.0f;
1047
        if(ctx->acmod == AC3_ACMOD_DUALMONO && ch == 2) {
1048
            gain *= ctx->dynrng2;
1049
        } else {
1050
            gain *= ctx->dynrng;
1051
        }
1052
        for(i=0; i<ctx->endmant[ch-1]; i++) {
1053
            ctx->transform_coeffs[ch][i] *= gain;
1054
        }
1055
    }
1056

    
1057
    do_imdct(ctx);
1058

    
1059
    return 0;
1060
}
1061

    
1062
static inline int16_t convert(int32_t i)
1063
{
1064
    if (i > 0x43c07fff)
1065
        return 32767;
1066
    else if (i <= 0x43bf8000)
1067
        return -32768;
1068
    else
1069
        return (i - 0x43c00000);
1070
}
1071

    
1072
/* Decode ac3 frame.
1073
 *
1074
 * @param avctx Pointer to AVCodecContext
1075
 * @param data Pointer to pcm smaples
1076
 * @param data_size Set to number of pcm samples produced by decoding
1077
 * @param buf Data to be decoded
1078
 * @param buf_size Size of the buffer
1079
 */
1080
static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1081
{
1082
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1083
    int16_t *out_samples = (int16_t *)data;
1084
    int i, j, k, start;
1085
    int32_t *int_ptr[6];
1086

    
1087
    for (i = 0; i < 6; i++)
1088
        int_ptr[i] = (int32_t *)(&ctx->output[i]);
1089

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

    
1093
    //Parse the syncinfo.
1094
    if (ac3_parse_header(ctx)) {
1095
        av_log(avctx, AV_LOG_ERROR, "\n");
1096
        *data_size = 0;
1097
        return buf_size;
1098
    }
1099

    
1100
    avctx->sample_rate = ctx->sampling_rate;
1101
    avctx->bit_rate = ctx->bit_rate;
1102

    
1103
    /* channel config */
1104
    if (avctx->channels == 0) {
1105
        avctx->channels = ctx->out_channels;
1106
    }
1107
    if(avctx->channels != ctx->out_channels) {
1108
        av_log(avctx, AV_LOG_ERROR, "Cannot mix AC3 to %d channels.\n",
1109
               avctx->channels);
1110
        return -1;
1111
    }
1112

    
1113
    //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);
1114

    
1115
    //Parse the Audio Blocks.
1116
    for (i = 0; i < NB_BLOCKS; i++) {
1117
        if (ac3_parse_audio_block(ctx, i)) {
1118
            av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1119
            *data_size = 0;
1120
            return ctx->frame_size;
1121
        }
1122
        start = (ctx->output_mode & AC3_OUTPUT_LFEON) ? 0 : 1;
1123
        for (k = 0; k < 256; k++)
1124
            for (j = start; j <= ctx->nfchans; j++)
1125
                *(out_samples++) = convert(int_ptr[j][k]);
1126
    }
1127
    *data_size = NB_BLOCKS * 256 * avctx->channels * sizeof (int16_t);
1128
    return ctx->frame_size;
1129
}
1130

    
1131
/* Uninitialize ac3 decoder.
1132
 */
1133
static int ac3_decode_end(AVCodecContext *avctx)
1134
{
1135
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1136
    ff_mdct_end(&ctx->imdct_512);
1137
    ff_mdct_end(&ctx->imdct_256);
1138

    
1139
    return 0;
1140
}
1141

    
1142
AVCodec ac3_decoder = {
1143
    .name = "ac3",
1144
    .type = CODEC_TYPE_AUDIO,
1145
    .id = CODEC_ID_AC3,
1146
    .priv_data_size = sizeof (AC3DecodeContext),
1147
    .init = ac3_decode_init,
1148
    .close = ac3_decode_end,
1149
    .decode = ac3_decode_frame,
1150
};
1151