Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3dec.c @ 7bfd22f2

History | View | Annotate | Download (38.5 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 BLOCK_SIZE    256
80

    
81
#define AC3_OUTPUT_LFEON  8
82

    
83
typedef struct {
84
    int acmod;
85
    int cmixlev;
86
    int surmixlev;
87
    int dsurmod;
88

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

    
120
    /* Derived Attributes. */
121
    int      sampling_rate;
122
    int      bit_rate;
123
    int      frame_size;
124

    
125
    int      nchans;            //number of total channels
126
    int      nfchans;           //number of full-bandwidth channels
127
    int      lfeon;             //lfe channel in use
128
    int      output_mode;       ///< output channel configuration
129
    int      out_channels;      ///< number of output channels
130

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

    
141
    int8_t   dcplexps[256];     //decoded coupling exponents
142
    int8_t   dexps[5][256];     //decoded fbw channel exponents
143
    int8_t   dlfeexps[256];     //decoded lfe channel exponents
144
    uint8_t  cplbap[256];       //coupling bit allocation pointers
145
    uint8_t  bap[5][256];       //fbw channel bit allocation pointers
146
    uint8_t  lfebap[256];       //lfe channel bit allocation pointers
147

    
148
    DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][BLOCK_SIZE]);  //transform coefficients
149

    
150
    /* For IMDCT. */
151
    MDCTContext imdct_512;  //for 512 sample imdct transform
152
    MDCTContext imdct_256;  //for 256 sample imdct transform
153
    DSPContext  dsp;        //for optimization
154

    
155
    DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS][BLOCK_SIZE]);    //output after imdct transform and windowing
156
    DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS][BLOCK_SIZE]);     //delay - added to the next block
157
    DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]);               //temporary storage for imdct transform
158
    DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]);          //temporary storage for output before windowing
159
    DECLARE_ALIGNED_16(float, window[BLOCK_SIZE]);                  //window coefficients
160

    
161
    /* Miscellaneous. */
162
    GetBitContext gb;
163
    AVRandomState dith_state;   //for dither generation
164
} AC3DecodeContext;
165

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

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

    
186
   sum++;
187
   for (i = 0; i < 256; i++)
188
       window[i] = sqrt(local_window[i] / sum);
189
}
190

    
191
/*
192
 * Generate quantizer tables.
193
 */
194
static void generate_quantizers_table(int16_t quantizers[], int level, int length)
195
{
196
    int i;
197

    
198
    for (i = 0; i < length; i++)
199
        quantizers[i] = ((2 * i - level + 1) << 15) / level;
200
}
201

    
202
static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
203
{
204
    int i, j;
205
    int16_t v;
206

    
207
    for (i = 0; i < length1; i++) {
208
        v = ((2 * i - level + 1) << 15) / level;
209
        for (j = 0; j < length2; j++)
210
            quantizers[i * length2 + j] = v;
211
    }
212

    
213
    for (i = length1 * length2; i < size; i++)
214
        quantizers[i] = 0;
215
}
216

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

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

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

    
231
}
232

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

    
237
    for (i = 0; i < length1; i++)
238
        for (j = 0; j < length2; j++)
239
            quantizers[i * length2 + j] = ((2 * (j % level) - level + 1) << 15) / level;
240

    
241
    for (i = length1 * length2; i < size; i++)
242
        quantizers[i] = 0;
243
}
244

    
245
/*
246
 * Initialize tables at runtime.
247
 */
248
static void ac3_tables_init(void)
249
{
250
    int i;
251

    
252
    /* Quantizer ungrouping tables. */
253
    // for level-3 quantizers
254
    generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
255
    generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
256
    generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
257

    
258
    //for level-5 quantizers
259
    generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
260
    generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
261
    generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
262

    
263
    //for level-7 quantizers
264
    generate_quantizers_table(l7_quantizers, 7, 7);
265

    
266
    //for level-4 quantizers
267
    generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
268
    generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
269

    
270
    //for level-15 quantizers
271
    generate_quantizers_table(l15_quantizers, 15, 15);
272
    /* End Quantizer ungrouping tables. */
273

    
274
    //generate scale factors
275
    for (i = 0; i < 25; i++)
276
        scale_factors[i] = pow(2.0, -(i + 15));
277

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

    
287

    
288
static int ac3_decode_init(AVCodecContext *avctx)
289
{
290
    AC3DecodeContext *ctx = avctx->priv_data;
291

    
292
    ac3_common_init();
293
    ac3_tables_init();
294
    ff_mdct_init(&ctx->imdct_256, 8, 1);
295
    ff_mdct_init(&ctx->imdct_512, 9, 1);
296
    ac3_window_init(ctx->window);
297
    dsputil_init(&ctx->dsp, avctx);
298
    av_init_random(0, &ctx->dith_state);
299

    
300
    return 0;
301
}
302
/*********** END INIT FUNCTIONS ***********/
303

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

    
315
    err = ff_ac3_parse_header(gb->buffer, &hdr);
316
    if(err)
317
        return err;
318

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

    
333
    /* set default output to all source channels */
334
    ctx->out_channels = ctx->nchans;
335
    ctx->output_mode = ctx->acmod;
336
    if(ctx->lfeon)
337
        ctx->output_mode |= AC3_OUTPUT_LFEON;
338

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

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

    
366
    skip_bits(gb, 2); //skip copyright bit and original bitstream bit
367

    
368
    /* FIXME: read & use the xbsi1 downmix levels */
369
    if (get_bits1(gb))
370
        skip_bits(gb, 14); //skip timecode1
371
    if (get_bits1(gb))
372
        skip_bits(gb, 14); //skip timecode2
373

    
374
    if (get_bits1(gb)) {
375
        i = get_bits(gb, 6); //additional bsi length
376
        do {
377
            skip_bits(gb, 8);
378
        } while(i--);
379
    }
380

    
381
    return 0;
382
}
383

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

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

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

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

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

    
443
    cplbndstrc = ctx->cplbndstrc;
444
    start = ctx->cplstrtmant;
445
    bnd = 0;
446

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

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

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

    
494
                case 3:
495
                    cplcoeff = l7_quantizers[get_bits(gb, 3)] * scale_factors[exps[start]];
496
                    break;
497

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

    
508
                case 5:
509
                    cplcoeff = l15_quantizers[get_bits(gb, 4)] * scale_factors[exps[start]];
510
                    break;
511

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

    
522
    return 0;
523
}
524

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

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

    
548

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

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

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

    
585
            case 3:
586
                coeffs[i] = l7_quantizers[get_bits(gb, 3)] * scale_factors[exps[i]];
587
                continue;
588

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

    
599
            case 5:
600
                coeffs[i] = l15_quantizers[get_bits(gb, 4)] * scale_factors[exps[i]];
601
                continue;
602

    
603
            default:
604
                coeffs[i] = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * scale_factors[exps[i]];
605
                continue;
606
        }
607
    }
608

    
609
    return 0;
610
}
611

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

    
622
    m.l3ptr = m.l5ptr = m.l11ptr = 3;
623

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

    
652
    return 0;
653
}
654

    
655
/* Rematrixing routines. */
656
static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
657
{
658
    float tmp0, tmp1;
659

    
660
    while (start < end) {
661
        tmp0 = ctx->transform_coeffs[1][start];
662
        tmp1 = ctx->transform_coeffs[2][start];
663
        ctx->transform_coeffs[1][start] = tmp0 + tmp1;
664
        ctx->transform_coeffs[2][start] = tmp0 - tmp1;
665
        start++;
666
    }
667
}
668

    
669
static void do_rematrixing(AC3DecodeContext *ctx)
670
{
671
    int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
672
    int end, bndend;
673

    
674
    end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
675

    
676
    if (ctx->rematflg[0])
677
        do_rematrixing1(ctx, bnd1, bnd2);
678

    
679
    if (ctx->rematflg[1])
680
        do_rematrixing1(ctx, bnd2, bnd3);
681

    
682
    bndend = bnd4;
683
    if (bndend > end) {
684
        bndend = end;
685
        if (ctx->rematflg[2])
686
            do_rematrixing1(ctx, bnd3, bndend);
687
    } else {
688
        if (ctx->rematflg[2])
689
            do_rematrixing1(ctx, bnd3, bnd4);
690
        if (ctx->rematflg[3])
691
            do_rematrixing1(ctx, bnd4, end);
692
    }
693
}
694

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

    
705
    for(i=0; i<2; i++) {
706
        /* de-interleave coefficients */
707
        for(k=0; k<128; k++) {
708
            x[k] = ctx->transform_coeffs[chindex][2*k+i];
709
        }
710

    
711
        /* run standard IMDCT */
712
        ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, o_ptr, x, ctx->tmp_imdct);
713

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

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

    
736
/* IMDCT Transform. */
737
static inline void do_imdct(AC3DecodeContext *ctx)
738
{
739
    int ch;
740

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

    
757
        ctx->dsp.vector_fmul_add_add(ctx->output[ch], ctx->tmp_output,
758
                                     ctx->window, ctx->delay[ch], 384, 256, 1);
759
        ctx->dsp.vector_fmul_reverse(ctx->delay[ch], ctx->tmp_output+256,
760
                                     ctx->window, 256);
761
    }
762
}
763

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

    
780
    for (i = 0; i < nfchans; i++) /*block switch flag */
781
        ctx->blksw[i] = get_bits1(gb);
782

    
783
    for (i = 0; i < nfchans; i++) /* dithering flag */
784
        ctx->dithflag[i] = get_bits1(gb);
785

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

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

    
802
    if (get_bits1(gb)) { /* coupling strategy */
803
        ctx->cplinu = get_bits1(gb);
804
        ctx->cplbndstrc = 0;
805
        if (ctx->cplinu) { /* coupling in use */
806
            for (i = 0; i < nfchans; i++)
807
                ctx->chincpl[i] = get_bits1(gb);
808

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

    
812
            ctx->cplbegf = get_bits(gb, 4);
813
            ctx->cplendf = get_bits(gb, 4);
814

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

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

    
834
    if (ctx->cplinu) {
835
        ctx->cplcoe = 0;
836

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1059
    do_imdct(ctx);
1060

    
1061
    return 0;
1062
}
1063

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

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

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

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

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

    
1102
    avctx->sample_rate = ctx->sampling_rate;
1103
    avctx->bit_rate = ctx->bit_rate;
1104

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

    
1115
    //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);
1116

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

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

    
1141
    return 0;
1142
}
1143

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