Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3dec.c @ 9ef60390

History | View | Annotate | Download (55.6 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.h"
37
#include "ac3tab.h"
38
#include "bitstream.h"
39
#include "dsputil.h"
40
#include "random.h"
41

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

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

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

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

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

    
60
static int16_t l7_quantizers[7];
61

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

    
65
static int16_t l15_quantizers[15];
66

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

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

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

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

    
82
#define BLOCK_SIZE    256
83

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

    
91
typedef struct {
92
    uint16_t crc1;
93
    uint8_t  fscod;
94

    
95
    uint8_t  acmod;
96
    uint8_t  cmixlev;
97
    uint8_t  surmixlev;
98
    uint8_t  dsurmod;
99

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

    
139
    /* Derived Attributes. */
140
    int      sampling_rate;
141
    int      bit_rate;
142
    int      frame_size;
143

    
144
    int      nfchans;           //number of channels
145
    int      lfeon;             //lfe channel in use
146

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

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

    
165
    int      blkoutput;         //output configuration for block
166

    
167
    DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][BLOCK_SIZE]);  //transform coefficients
168

    
169
    /* For IMDCT. */
170
    MDCTContext imdct_512;  //for 512 sample imdct transform
171
    MDCTContext imdct_256;  //for 256 sample imdct transform
172
    DSPContext  dsp;        //for optimization
173

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

    
180
    /* Miscellaneous. */
181
    GetBitContext gb;
182
    AVRandomState dith_state;   //for dither generation
183
} AC3DecodeContext;
184

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

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

    
205
   sum++;
206
   for (i = 0; i < 256; i++)
207
       window[i] = sqrt(local_window[i] / sum);
208
}
209

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

    
217
    for (i = 0; i < length; i++)
218
        quantizers[i] = ((2 * i - level + 1) << 15) / level;
219
}
220

    
221
static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
222
{
223
    int i, j;
224
    int16_t v;
225

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

    
232
    for (i = length1 * length2; i < size; i++)
233
        quantizers[i] = 0;
234
}
235

    
236
static void generate_quantizers_table_2(int16_t quantizers[], int level, int length1, int length2, int size)
237
{
238
    int i, j;
239
    int16_t v;
240

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

    
247
    for (i = length1 * length2; i < size; i++)
248
        quantizers[i] = 0;
249

    
250
}
251

    
252
static void generate_quantizers_table_3(int16_t quantizers[], int level, int length1, int length2, int size)
253
{
254
    int i, j;
255

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

    
260
    for (i = length1 * length2; i < size; i++)
261
        quantizers[i] = 0;
262
}
263

    
264
/*
265
 * Initialize tables at runtime.
266
 */
267
static void ac3_tables_init(void)
268
{
269
    int i;
270

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

    
277
    //for level-5 quantizers
278
    generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
279
    generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
280
    generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
281

    
282
    //for level-7 quantizers
283
    generate_quantizers_table(l7_quantizers, 7, 7);
284

    
285
    //for level-4 quantizers
286
    generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
287
    generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
288

    
289
    //for level-15 quantizers
290
    generate_quantizers_table(l15_quantizers, 15, 15);
291
    /* End Quantizer ungrouping tables. */
292

    
293
    //generate scale factors
294
    for (i = 0; i < 25; i++)
295
        scale_factors[i] = pow(2.0, -(i + 15));
296

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

    
306

    
307
static int ac3_decode_init(AVCodecContext *avctx)
308
{
309
    AC3DecodeContext *ctx = avctx->priv_data;
310

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

    
319
    return 0;
320
}
321
/*********** END INIT FUNCTIONS ***********/
322

    
323
/* Parse the 'sync_info' from the ac3 bitstream.
324
 * This function extracts the sync_info from ac3 bitstream.
325
 * GetBitContext within AC3DecodeContext must point to
326
 * start of the synchronized ac3 bitstream.
327
 *
328
 * @param ctx  AC3DecodeContext
329
 * @return Returns framesize, returns 0 if fscod, frmsizecod or bsid is not valid
330
 */
331
static int ac3_parse_sync_info(AC3DecodeContext *ctx)
332
{
333
    GetBitContext *gb = &ctx->gb;
334
    int frmsizecod, bsid;
335

    
336
    skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
337
    ctx->crc1 = get_bits(gb, 16);
338
    ctx->fscod = get_bits(gb, 2);
339
    if (ctx->fscod == 0x03)
340
        return 0;
341
    frmsizecod = get_bits(gb, 6);
342
    if (frmsizecod >= 38)
343
        return 0;
344
    ctx->sampling_rate = ff_ac3_freqs[ctx->fscod];
345
    ctx->bit_rate = ff_ac3_bitratetab[frmsizecod >> 1];
346

    
347
    /* we include it here in order to determine validity of ac3 frame */
348
    bsid = get_bits(gb, 5);
349
    if (bsid > 0x08)
350
        return 0;
351
    skip_bits(gb, 3); //skip the bsmod, bsi->bsmod = get_bits(gb, 3);
352

    
353
    switch (ctx->fscod) {
354
        case 0x00:
355
            ctx->frame_size = 4 * ctx->bit_rate;
356
            return ctx->frame_size;
357
        case 0x01:
358
            ctx->frame_size = 2 * (320 * ctx->bit_rate / 147 + (frmsizecod & 1));
359
            return ctx->frame_size;
360
        case 0x02:
361
            ctx->frame_size =  6 * ctx->bit_rate;
362
            return ctx->frame_size;
363
    }
364

    
365
    /* never reached */
366
    return 0;
367
}
368

    
369
/* Parse bsi from ac3 bitstream.
370
 * This function extracts the bitstream information (bsi) from ac3 bitstream.
371
 *
372
 * @param ctx AC3DecodeContext after processed by ac3_parse_sync_info
373
 */
374
static void ac3_parse_bsi(AC3DecodeContext *ctx)
375
{
376
    GetBitContext *gb = &ctx->gb;
377
    int i;
378

    
379
    ctx->cmixlev = 0;
380
    ctx->surmixlev = 0;
381
    ctx->dsurmod = 0;
382
    ctx->nfchans = 0;
383
    ctx->cpldeltbae = DBA_NONE;
384
    ctx->cpldeltnseg = 0;
385
    for (i = 0; i < 5; i++) {
386
        ctx->deltbae[i] = DBA_NONE;
387
        ctx->deltnseg[i] = 0;
388
    }
389
    ctx->dynrng = 1.0;
390
    ctx->dynrng2 = 1.0;
391

    
392
    ctx->acmod = get_bits(gb, 3);
393
    ctx->nfchans = nfchans_tbl[ctx->acmod];
394

    
395
    if (ctx->acmod & 0x01 && ctx->acmod != 0x01)
396
        ctx->cmixlev = get_bits(gb, 2);
397
    if (ctx->acmod & 0x04)
398
        ctx->surmixlev = get_bits(gb, 2);
399
    if (ctx->acmod == 0x02)
400
        ctx->dsurmod = get_bits(gb, 2);
401

    
402
    ctx->lfeon = get_bits1(gb);
403

    
404
    i = !(ctx->acmod);
405
    do {
406
        skip_bits(gb, 5); //skip dialog normalization
407
        if (get_bits1(gb))
408
            skip_bits(gb, 8); //skip compression
409
        if (get_bits1(gb))
410
            skip_bits(gb, 8); //skip language code
411
        if (get_bits1(gb))
412
            skip_bits(gb, 7); //skip audio production information
413
    } while (i--);
414

    
415
    skip_bits(gb, 2); //skip copyright bit and original bitstream bit
416

    
417
    if (get_bits1(gb))
418
        skip_bits(gb, 14); //skip timecode1
419
    if (get_bits1(gb))
420
        skip_bits(gb, 14); //skip timecode2
421

    
422
    if (get_bits1(gb)) {
423
        i = get_bits(gb, 6); //additional bsi length
424
        do {
425
            skip_bits(gb, 8);
426
        } while(i--);
427
    }
428
}
429

    
430
/**
431
 * Decodes the grouped exponents.
432
 * This function decodes the coded exponents according to exponent strategy
433
 * and stores them in the decoded exponents buffer.
434
 *
435
 * @param[in]  gb      GetBitContext which points to start of coded exponents
436
 * @param[in]  expstr  Exponent coding strategy
437
 * @param[in]  ngrps   Number of grouped exponents
438
 * @param[in]  absexp  Absolute exponent or DC exponent
439
 * @param[out] dexps   Decoded exponents are stored in dexps
440
 */
441
static void decode_exponents(GetBitContext *gb, int expstr, int ngrps,
442
                             uint8_t absexp, uint8_t *dexps)
443
{
444
    int i, j, grp, grpsize;
445
    int dexp[256];
446
    int expacc, prevexp;
447

    
448
    /* unpack groups */
449
    grpsize = expstr + (expstr == EXP_D45);
450
    for(grp=0,i=0; grp<ngrps; grp++) {
451
        expacc = get_bits(gb, 7);
452
        dexp[i++] = exp_ungroup_tbl[expacc][0];
453
        dexp[i++] = exp_ungroup_tbl[expacc][1];
454
        dexp[i++] = exp_ungroup_tbl[expacc][2];
455
    }
456

    
457
    /* convert to absolute exps and expand groups */
458
    prevexp = absexp;
459
    for(i=0; i<ngrps*3; i++) {
460
        prevexp = av_clip(prevexp + dexp[i]-2, 0, 24);
461
        for(j=0; j<grpsize; j++) {
462
            dexps[(i*grpsize)+j] = prevexp;
463
        }
464
    }
465
}
466

    
467
/* Performs bit allocation.
468
 * This function performs bit allocation for the requested chanenl.
469
 */
470
static void do_bit_allocation(AC3DecodeContext *ctx, int chnl)
471
{
472
    int fgain, snroffset;
473

    
474
    if (chnl == 5) {
475
        fgain = ff_fgaintab[ctx->cplfgaincod];
476
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->cplfsnroffst) << 2;
477
        ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->cplbap,
478
                                      ctx->dcplexps, ctx->cplstrtmant,
479
                                      ctx->cplendmant, snroffset, fgain, 0,
480
                                      ctx->cpldeltbae, ctx->cpldeltnseg,
481
                                      ctx->cpldeltoffst, ctx->cpldeltlen,
482
                                      ctx->cpldeltba);
483
    }
484
    else if (chnl == 6) {
485
        fgain = ff_fgaintab[ctx->lfefgaincod];
486
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->lfefsnroffst) << 2;
487
        ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->lfebap,
488
                                      ctx->dlfeexps, 0, 7, snroffset, fgain, 1,
489
                                      DBA_NONE, 0, NULL, NULL, NULL);
490
    }
491
    else {
492
        fgain = ff_fgaintab[ctx->fgaincod[chnl]];
493
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->fsnroffst[chnl]) << 2;
494
        ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->bap[chnl],
495
                                      ctx->dexps[chnl], 0, ctx->endmant[chnl],
496
                                      snroffset, fgain, 0, ctx->deltbae[chnl],
497
                                      ctx->deltnseg[chnl], ctx->deltoffst[chnl],
498
                                      ctx->deltlen[chnl], ctx->deltba[chnl]);
499
    }
500
}
501

    
502
typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
503
    int16_t l3_quantizers[3];
504
    int16_t l5_quantizers[3];
505
    int16_t l11_quantizers[2];
506
    int l3ptr;
507
    int l5ptr;
508
    int l11ptr;
509
} mant_groups;
510

    
511
/* Get the transform coefficients for coupling channel and uncouple channels.
512
 * The coupling transform coefficients starts at the the cplstrtmant, which is
513
 * equal to endmant[ch] for fbw channels. Hence we can uncouple channels before
514
 * getting transform coefficients for the channel.
515
 */
516
static int get_transform_coeffs_cpling(AC3DecodeContext *ctx, mant_groups *m)
517
{
518
    GetBitContext *gb = &ctx->gb;
519
    int ch, start, end, cplbndstrc, bnd, gcode, tbap;
520
    float cplcos[5], cplcoeff;
521
    uint8_t *exps = ctx->dcplexps;
522
    uint8_t *bap = ctx->cplbap;
523

    
524
    cplbndstrc = ctx->cplbndstrc;
525
    start = ctx->cplstrtmant;
526
    bnd = 0;
527

    
528
    while (start < ctx->cplendmant) {
529
        end = start + 12;
530
        while (cplbndstrc & 1) {
531
            end += 12;
532
            cplbndstrc >>= 1;
533
        }
534
        cplbndstrc >>= 1;
535
        for (ch = 0; ch < ctx->nfchans; ch++)
536
            cplcos[ch] = ctx->chcoeffs[ch] * ctx->cplco[ch][bnd];
537
        bnd++;
538

    
539
        while (start < end) {
540
            tbap = bap[start];
541
            switch(tbap) {
542
                case 0:
543
                    for (ch = 0; ch < ctx->nfchans; ch++)
544
                        if (((ctx->chincpl) >> ch) & 1) {
545
                            if ((ctx->dithflag >> ch) & 1) {
546
                                cplcoeff = (av_random(&ctx->dith_state) & 0xFFFF) * scale_factors[exps[start]];
547
                                ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch] * LEVEL_MINUS_3DB;
548
                            } else
549
                                ctx->transform_coeffs[ch + 1][start] = 0;
550
                        }
551
                    start++;
552
                    continue;
553
                case 1:
554
                    if (m->l3ptr > 2) {
555
                        gcode = get_bits(gb, 5);
556
                        m->l3_quantizers[0] = l3_quantizers_1[gcode];
557
                        m->l3_quantizers[1] = l3_quantizers_2[gcode];
558
                        m->l3_quantizers[2] = l3_quantizers_3[gcode];
559
                        m->l3ptr = 0;
560
                    }
561
                    cplcoeff = m->l3_quantizers[m->l3ptr++] * scale_factors[exps[start]];
562
                    break;
563

    
564
                case 2:
565
                    if (m->l5ptr > 2) {
566
                        gcode = get_bits(gb, 7);
567
                        m->l5_quantizers[0] = l5_quantizers_1[gcode];
568
                        m->l5_quantizers[1] = l5_quantizers_2[gcode];
569
                        m->l5_quantizers[2] = l5_quantizers_3[gcode];
570
                        m->l5ptr = 0;
571
                    }
572
                    cplcoeff = m->l5_quantizers[m->l5ptr++] * scale_factors[exps[start]];
573
                    break;
574

    
575
                case 3:
576
                    cplcoeff = l7_quantizers[get_bits(gb, 3)] * scale_factors[exps[start]];
577
                    break;
578

    
579
                case 4:
580
                    if (m->l11ptr > 1) {
581
                        gcode = get_bits(gb, 7);
582
                        m->l11_quantizers[0] = l11_quantizers_1[gcode];
583
                        m->l11_quantizers[1] = l11_quantizers_2[gcode];
584
                        m->l11ptr = 0;
585
                    }
586
                    cplcoeff = m->l11_quantizers[m->l11ptr++] * scale_factors[exps[start]];
587
                    break;
588

    
589
                case 5:
590
                    cplcoeff = l15_quantizers[get_bits(gb, 4)] * scale_factors[exps[start]];
591
                    break;
592

    
593
                default:
594
                    cplcoeff = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * scale_factors[exps[start]];
595
            }
596
            for (ch = 0; ch < ctx->nfchans; ch++)
597
                if ((ctx->chincpl >> ch) & 1)
598
                    ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
599
            start++;
600
        }
601
    }
602

    
603
    return 0;
604
}
605

    
606
/* Get the transform coefficients for particular channel */
607
static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
608
{
609
    GetBitContext *gb = &ctx->gb;
610
    int i, gcode, tbap, dithflag, end;
611
    uint8_t *exps;
612
    uint8_t *bap;
613
    float *coeffs;
614
    float factors[25];
615

    
616
    for (i = 0; i < 25; i++)
617
        factors[i] = scale_factors[i] * ctx->chcoeffs[ch_index];
618

    
619
    if (ch_index != -1) { /* fbw channels */
620
        dithflag = (ctx->dithflag >> ch_index) & 1;
621
        exps = ctx->dexps[ch_index];
622
        bap = ctx->bap[ch_index];
623
        coeffs = ctx->transform_coeffs[ch_index + 1];
624
        end = ctx->endmant[ch_index];
625
    } else if (ch_index == -1) {
626
        dithflag = 0;
627
        exps = ctx->dlfeexps;
628
        bap = ctx->lfebap;
629
        coeffs = ctx->transform_coeffs[0];
630
        end = 7;
631
    }
632

    
633

    
634
    for (i = 0; i < end; i++) {
635
        tbap = bap[i];
636
        switch (tbap) {
637
            case 0:
638
                if (!dithflag) {
639
                    coeffs[i] = 0;
640
                    continue;
641
                }
642
                else {
643
                    coeffs[i] = (av_random(&ctx->dith_state) & 0xFFFF) * factors[exps[i]];
644
                    coeffs[i] *= LEVEL_MINUS_3DB;
645
                    continue;
646
                }
647

    
648
            case 1:
649
                if (m->l3ptr > 2) {
650
                    gcode = get_bits(gb, 5);
651
                    m->l3_quantizers[0] = l3_quantizers_1[gcode];
652
                    m->l3_quantizers[1] = l3_quantizers_2[gcode];
653
                    m->l3_quantizers[2] = l3_quantizers_3[gcode];
654
                    m->l3ptr = 0;
655
                }
656
                coeffs[i] = m->l3_quantizers[m->l3ptr++] * factors[exps[i]];
657
                continue;
658

    
659
            case 2:
660
                if (m->l5ptr > 2) {
661
                    gcode = get_bits(gb, 7);
662
                    m->l5_quantizers[0] = l5_quantizers_1[gcode];
663
                    m->l5_quantizers[1] = l5_quantizers_2[gcode];
664
                    m->l5_quantizers[2] = l5_quantizers_3[gcode];
665
                    m->l5ptr = 0;
666
                }
667
                coeffs[i] = m->l5_quantizers[m->l5ptr++] * factors[exps[i]];
668
                continue;
669

    
670
            case 3:
671
                coeffs[i] = l7_quantizers[get_bits(gb, 3)] * factors[exps[i]];
672
                continue;
673

    
674
            case 4:
675
                if (m->l11ptr > 1) {
676
                    gcode = get_bits(gb, 7);
677
                    m->l11_quantizers[0] = l11_quantizers_1[gcode];
678
                    m->l11_quantizers[1] = l11_quantizers_2[gcode];
679
                    m->l11ptr = 0;
680
                }
681
                coeffs[i] = m->l11_quantizers[m->l11ptr++] * factors[exps[i]];
682
                continue;
683

    
684
            case 5:
685
                coeffs[i] = l15_quantizers[get_bits(gb, 4)] * factors[exps[i]];
686
                continue;
687

    
688
            default:
689
                coeffs[i] = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * factors[exps[i]];
690
                continue;
691
        }
692
    }
693

    
694
    return 0;
695
}
696

    
697
/* Get the transform coefficients.
698
 * This function extracts the tranform coefficients form the ac3 bitstream.
699
 * This function is called after bit allocation is performed.
700
 */
701
static int get_transform_coeffs(AC3DecodeContext * ctx)
702
{
703
    int i, end;
704
    int got_cplchan = 0;
705
    mant_groups m;
706

    
707
    m.l3ptr = m.l5ptr = m.l11ptr = 3;
708

    
709
    for (i = 0; i < ctx->nfchans; i++) {
710
        /* transform coefficients for individual channel */
711
        if (get_transform_coeffs_ch(ctx, i, &m))
712
            return -1;
713
        /* tranform coefficients for coupling channels */
714
        if ((ctx->chincpl >> i) & 1)  {
715
            if (!got_cplchan) {
716
                if (get_transform_coeffs_cpling(ctx, &m)) {
717
                    av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
718
                    return -1;
719
                }
720
                got_cplchan = 1;
721
            }
722
            end = ctx->cplendmant;
723
        } else
724
            end = ctx->endmant[i];
725
        do
726
            ctx->transform_coeffs[i + 1][end] = 0;
727
        while(++end < 256);
728
    }
729
    if (ctx->lfeon) {
730
        if (get_transform_coeffs_ch(ctx, -1, &m))
731
                return -1;
732
        for (i = 7; i < 256; i++) {
733
            ctx->transform_coeffs[0][i] = 0;
734
        }
735
    }
736

    
737
    return 0;
738
}
739

    
740
/* Rematrixing routines. */
741
static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
742
{
743
    float tmp0, tmp1;
744

    
745
    while (start < end) {
746
        tmp0 = ctx->transform_coeffs[1][start];
747
        tmp1 = ctx->transform_coeffs[2][start];
748
        ctx->transform_coeffs[1][start] = tmp0 + tmp1;
749
        ctx->transform_coeffs[2][start] = tmp0 - tmp1;
750
        start++;
751
    }
752
}
753

    
754
static void do_rematrixing(AC3DecodeContext *ctx)
755
{
756
    int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
757
    int end, bndend;
758

    
759
    end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
760

    
761
    if (ctx->rematflg & 1)
762
        do_rematrixing1(ctx, bnd1, bnd2);
763

    
764
    if (ctx->rematflg & 2)
765
        do_rematrixing1(ctx, bnd2, bnd3);
766

    
767
    bndend = bnd4;
768
    if (bndend > end) {
769
        bndend = end;
770
        if (ctx->rematflg & 4)
771
            do_rematrixing1(ctx, bnd3, bndend);
772
    } else {
773
        if (ctx->rematflg & 4)
774
            do_rematrixing1(ctx, bnd3, bnd4);
775
        if (ctx->rematflg & 8)
776
            do_rematrixing1(ctx, bnd4, end);
777
    }
778
}
779

    
780
/* This function sets the normalized channel coefficients.
781
 * Transform coefficients are multipllied by the channel
782
 * coefficients to get normalized transform coefficients.
783
 */
784
static void get_downmix_coeffs(AC3DecodeContext *ctx)
785
{
786
    int from = ctx->acmod;
787
    int to = ctx->blkoutput;
788
    float clev = clevs[ctx->cmixlev];
789
    float slev = slevs[ctx->surmixlev];
790
    float nf = 1.0; //normalization factor for downmix coeffs
791
    int i;
792

    
793
    if (!ctx->acmod) {
794
        ctx->chcoeffs[0] = 2 * ctx->dynrng;
795
        ctx->chcoeffs[1] = 2 * ctx->dynrng2;
796
    } else {
797
        for (i = 0; i < ctx->nfchans; i++)
798
            ctx->chcoeffs[i] = 2 * ctx->dynrng;
799
    }
800

    
801
    if (to == AC3_OUTPUT_UNMODIFIED)
802
        return;
803

    
804
    switch (from) {
805
        case AC3_ACMOD_DUALMONO:
806
            switch (to) {
807
                case AC3_OUTPUT_MONO:
808
                case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */
809
                    nf = 0.5;
810
                    ctx->chcoeffs[0] *= nf;
811
                    ctx->chcoeffs[1] *= nf;
812
                    break;
813
            }
814
            break;
815
        case AC3_ACMOD_MONO:
816
            switch (to) {
817
                case AC3_OUTPUT_STEREO:
818
                    nf = LEVEL_MINUS_3DB;
819
                    ctx->chcoeffs[0] *= nf;
820
                    break;
821
            }
822
            break;
823
        case AC3_ACMOD_STEREO:
824
            switch (to) {
825
                case AC3_OUTPUT_MONO:
826
                    nf = LEVEL_MINUS_3DB;
827
                    ctx->chcoeffs[0] *= nf;
828
                    ctx->chcoeffs[1] *= nf;
829
                    break;
830
            }
831
            break;
832
        case AC3_ACMOD_3F:
833
            switch (to) {
834
                case AC3_OUTPUT_MONO:
835
                    nf = LEVEL_MINUS_3DB / (1.0 + clev);
836
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
837
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
838
                    ctx->chcoeffs[1] *= ((nf * clev * LEVEL_MINUS_3DB) / 2.0);
839
                    break;
840
                case AC3_OUTPUT_STEREO:
841
                    nf = 1.0 / (1.0 + clev);
842
                    ctx->chcoeffs[0] *= nf;
843
                    ctx->chcoeffs[2] *= nf;
844
                    ctx->chcoeffs[1] *= (nf * clev);
845
                    break;
846
            }
847
            break;
848
        case AC3_ACMOD_2F1R:
849
            switch (to) {
850
                case AC3_OUTPUT_MONO:
851
                    nf = 2.0 * LEVEL_MINUS_3DB / (2.0 + slev);
852
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
853
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
854
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
855
                    break;
856
                case AC3_OUTPUT_STEREO:
857
                    nf = 1.0 / (1.0 + (slev * LEVEL_MINUS_3DB));
858
                    ctx->chcoeffs[0] *= nf;
859
                    ctx->chcoeffs[1] *= nf;
860
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
861
                    break;
862
                case AC3_OUTPUT_DOLBY:
863
                    nf = 1.0 / (1.0 + LEVEL_MINUS_3DB);
864
                    ctx->chcoeffs[0] *= nf;
865
                    ctx->chcoeffs[1] *= nf;
866
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
867
                    break;
868
            }
869
            break;
870
        case AC3_ACMOD_3F1R:
871
            switch (to) {
872
                case AC3_OUTPUT_MONO:
873
                    nf = LEVEL_MINUS_3DB / (1.0 + clev + (slev / 2.0));
874
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
875
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
876
                    ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
877
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
878
                    break;
879
                case AC3_OUTPUT_STEREO:
880
                    nf = 1.0 / (1.0 + clev + (slev * LEVEL_MINUS_3DB));
881
                    ctx->chcoeffs[0] *= nf;
882
                    ctx->chcoeffs[2] *= nf;
883
                    ctx->chcoeffs[1] *= (nf * clev);
884
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
885
                    break;
886
                case AC3_OUTPUT_DOLBY:
887
                    nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
888
                    ctx->chcoeffs[0] *= nf;
889
                    ctx->chcoeffs[1] *= nf;
890
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
891
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
892
                    break;
893
            }
894
            break;
895
        case AC3_ACMOD_2F2R:
896
            switch (to) {
897
                case AC3_OUTPUT_MONO:
898
                    nf = LEVEL_MINUS_3DB / (1.0 + slev);
899
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
900
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
901
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
902
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
903
                    break;
904
                case AC3_OUTPUT_STEREO:
905
                    nf = 1.0 / (1.0 + slev);
906
                    ctx->chcoeffs[0] *= nf;
907
                    ctx->chcoeffs[1] *= nf;
908
                    ctx->chcoeffs[2] *= (nf * slev);
909
                    ctx->chcoeffs[3] *= (nf * slev);
910
                    break;
911
                case AC3_OUTPUT_DOLBY:
912
                    nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
913
                    ctx->chcoeffs[0] *= nf;
914
                    ctx->chcoeffs[1] *= nf;
915
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
916
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
917
                    break;
918
            }
919
            break;
920
        case AC3_ACMOD_3F2R:
921
            switch (to) {
922
                case AC3_OUTPUT_MONO:
923
                    nf = LEVEL_MINUS_3DB / (1.0 + clev + slev);
924
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
925
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
926
                    ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
927
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
928
                    ctx->chcoeffs[4] *= (nf * slev * LEVEL_MINUS_3DB);
929
                    break;
930
                case AC3_OUTPUT_STEREO:
931
                    nf = 1.0 / (1.0 + clev + slev);
932
                    ctx->chcoeffs[0] *= nf;
933
                    ctx->chcoeffs[2] *= nf;
934
                    ctx->chcoeffs[1] *= (nf * clev);
935
                    ctx->chcoeffs[3] *= (nf * slev);
936
                    ctx->chcoeffs[4] *= (nf * slev);
937
                    break;
938
                case AC3_OUTPUT_DOLBY:
939
                    nf = 1.0 / (1.0 + (3.0 * LEVEL_MINUS_3DB));
940
                    ctx->chcoeffs[0] *= nf;
941
                    ctx->chcoeffs[1] *= nf;
942
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
943
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
944
                    ctx->chcoeffs[4] *= (nf * LEVEL_MINUS_3DB);
945
                    break;
946
            }
947
            break;
948
    }
949
}
950

    
951
/*********** BEGIN DOWNMIX FUNCTIONS ***********/
952
static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx)
953
{
954
    int i;
955
    float (*output)[BLOCK_SIZE] = ctx->output;
956

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

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

    
968
    for (i = 0; i < 256; i++) {
969
        tmp = output[1][i] + output[2][i];
970
        output[1][i] = output[2][i] = tmp;
971
    }
972
}
973

    
974
static inline void upmix_mono_to_stereo(AC3DecodeContext *ctx)
975
{
976
    int i;
977
    float (*output)[BLOCK_SIZE] = ctx->output;
978

    
979
    for (i = 0; i < 256; i++)
980
        output[2][i] = output[1][i];
981
}
982

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

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

    
993
static inline void mix_3f_to_mono(AC3DecodeContext *ctx)
994
{
995
    int i;
996
    float (*output)[BLOCK_SIZE] = ctx->output;
997

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

    
1004
static inline void mix_3f_to_stereo(AC3DecodeContext *ctx)
1005
{
1006
    int i;
1007
    float (*output)[BLOCK_SIZE] = ctx->output;
1008

    
1009
    for (i = 0; i < 256; i++) {
1010
        output[1][i] += output[2][i];
1011
        output[2][i] += output[3][i];
1012
    }
1013
    memset(output[3], 0, sizeof(output[3]));
1014
}
1015

    
1016
static inline void mix_2f_1r_to_mono(AC3DecodeContext *ctx)
1017
{
1018
    int i;
1019
    float (*output)[BLOCK_SIZE] = ctx->output;
1020

    
1021
    for (i = 0; i < 256; i++)
1022
        output[1][i] += (output[2][i] + output[3][i]);
1023
    memset(output[2], 0, sizeof(output[2]));
1024
    memset(output[3], 0, sizeof(output[3]));
1025

    
1026
}
1027

    
1028
static inline void mix_2f_1r_to_stereo(AC3DecodeContext *ctx)
1029
{
1030
    int i;
1031
    float (*output)[BLOCK_SIZE] = ctx->output;
1032

    
1033
    for (i = 0; i < 256; i++) {
1034
        output[1][i] += output[2][i];
1035
        output[2][i] += output[3][i];
1036
    }
1037
    memset(output[3], 0, sizeof(output[3]));
1038
}
1039

    
1040
static inline void mix_2f_1r_to_dolby(AC3DecodeContext *ctx)
1041
{
1042
    int i;
1043
    float (*output)[BLOCK_SIZE] = ctx->output;
1044

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

    
1052
static inline void mix_3f_1r_to_mono(AC3DecodeContext *ctx)
1053
{
1054
    int i;
1055
    float (*output)[BLOCK_SIZE] = ctx->output;
1056

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

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

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

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

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

    
1090
static inline void mix_2f_2r_to_mono(AC3DecodeContext *ctx)
1091
{
1092
    int i;
1093
    float (*output)[BLOCK_SIZE] = ctx->output;
1094

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

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

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

    
1115
static inline void mix_2f_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[3][i];
1122
        output[2][i] += output[4][i];
1123
    }
1124
    memset(output[3], 0, sizeof(output[3]));
1125
    memset(output[4], 0, sizeof(output[4]));
1126
}
1127

    
1128
static inline void mix_3f_2r_to_mono(AC3DecodeContext *ctx)
1129
{
1130
    int i;
1131
    float (*output)[BLOCK_SIZE] = ctx->output;
1132

    
1133
    for (i = 0; i < 256; i++)
1134
        output[1][i] += (output[2][i] + output[3][i] + output[4][i] + output[5][i]);
1135
    memset(output[2], 0, sizeof(output[2]));
1136
    memset(output[3], 0, sizeof(output[3]));
1137
    memset(output[4], 0, sizeof(output[4]));
1138
    memset(output[5], 0, sizeof(output[5]));
1139
}
1140

    
1141
static inline void mix_3f_2r_to_stereo(AC3DecodeContext *ctx)
1142
{
1143
    int i;
1144
    float (*output)[BLOCK_SIZE] = ctx->output;
1145

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

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

    
1160
    for (i = 0; i < 256; i++) {
1161
        output[1][i] += (output[2][i] - output[4][i] - output[5][i]);
1162
        output[2][i] += (output[3][i] + output[4][i] + output[5][i]);
1163
    }
1164
    memset(output[3], 0, sizeof(output[3]));
1165
    memset(output[4], 0, sizeof(output[4]));
1166
    memset(output[5], 0, sizeof(output[5]));
1167
}
1168
/*********** END DOWNMIX FUNCTIONS ***********/
1169

    
1170
/* Downmix the output.
1171
 * This function downmixes the output when the number of input
1172
 * channels is not equal to the number of output channels requested.
1173
 */
1174
static void do_downmix(AC3DecodeContext *ctx)
1175
{
1176
    int from = ctx->acmod;
1177
    int to = ctx->blkoutput;
1178

    
1179
    if (to == AC3_OUTPUT_UNMODIFIED)
1180
        return;
1181

    
1182
    switch (from) {
1183
        case AC3_ACMOD_DUALMONO:
1184
            switch (to) {
1185
                case AC3_OUTPUT_MONO:
1186
                    mix_dualmono_to_mono(ctx);
1187
                    break;
1188
                case AC3_OUTPUT_STEREO: /* We assume that sum of both mono channels is requested */
1189
                    mix_dualmono_to_stereo(ctx);
1190
                    break;
1191
            }
1192
            break;
1193
        case AC3_ACMOD_MONO:
1194
            switch (to) {
1195
                case AC3_OUTPUT_STEREO:
1196
                    upmix_mono_to_stereo(ctx);
1197
                    break;
1198
            }
1199
            break;
1200
        case AC3_ACMOD_STEREO:
1201
            switch (to) {
1202
                case AC3_OUTPUT_MONO:
1203
                    mix_stereo_to_mono(ctx);
1204
                    break;
1205
            }
1206
            break;
1207
        case AC3_ACMOD_3F:
1208
            switch (to) {
1209
                case AC3_OUTPUT_MONO:
1210
                    mix_3f_to_mono(ctx);
1211
                    break;
1212
                case AC3_OUTPUT_STEREO:
1213
                    mix_3f_to_stereo(ctx);
1214
                    break;
1215
            }
1216
            break;
1217
        case AC3_ACMOD_2F1R:
1218
            switch (to) {
1219
                case AC3_OUTPUT_MONO:
1220
                    mix_2f_1r_to_mono(ctx);
1221
                    break;
1222
                case AC3_OUTPUT_STEREO:
1223
                    mix_2f_1r_to_stereo(ctx);
1224
                    break;
1225
                case AC3_OUTPUT_DOLBY:
1226
                    mix_2f_1r_to_dolby(ctx);
1227
                    break;
1228
            }
1229
            break;
1230
        case AC3_ACMOD_3F1R:
1231
            switch (to) {
1232
                case AC3_OUTPUT_MONO:
1233
                    mix_3f_1r_to_mono(ctx);
1234
                    break;
1235
                case AC3_OUTPUT_STEREO:
1236
                    mix_3f_1r_to_stereo(ctx);
1237
                    break;
1238
                case AC3_OUTPUT_DOLBY:
1239
                    mix_3f_1r_to_dolby(ctx);
1240
                    break;
1241
            }
1242
            break;
1243
        case AC3_ACMOD_2F2R:
1244
            switch (to) {
1245
                case AC3_OUTPUT_MONO:
1246
                    mix_2f_2r_to_mono(ctx);
1247
                    break;
1248
                case AC3_OUTPUT_STEREO:
1249
                    mix_2f_2r_to_stereo(ctx);
1250
                    break;
1251
                case AC3_OUTPUT_DOLBY:
1252
                    mix_2f_2r_to_dolby(ctx);
1253
                    break;
1254
            }
1255
            break;
1256
        case AC3_ACMOD_3F2R:
1257
            switch (to) {
1258
                case AC3_OUTPUT_MONO:
1259
                    mix_3f_2r_to_mono(ctx);
1260
                    break;
1261
                case AC3_OUTPUT_STEREO:
1262
                    mix_3f_2r_to_stereo(ctx);
1263
                    break;
1264
                case AC3_OUTPUT_DOLBY:
1265
                    mix_3f_2r_to_dolby(ctx);
1266
                    break;
1267
            }
1268
            break;
1269
    }
1270
}
1271

    
1272
/* This function performs the imdct on 256 sample transform
1273
 * coefficients.
1274
 */
1275
static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
1276
{
1277
    int i, k;
1278
    float x[128];
1279
    FFTComplex z[2][64];
1280
    float *o_ptr = ctx->tmp_output;
1281

    
1282
    for(i=0; i<2; i++) {
1283
        /* de-interleave coefficients */
1284
        for(k=0; k<128; k++) {
1285
            x[k] = ctx->transform_coeffs[chindex][2*k+i];
1286
        }
1287

    
1288
        /* run standard IMDCT */
1289
        ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, o_ptr, x, ctx->tmp_imdct);
1290

    
1291
        /* reverse the post-rotation & reordering from standard IMDCT */
1292
        for(k=0; k<32; k++) {
1293
            z[i][32+k].re = -o_ptr[128+2*k];
1294
            z[i][32+k].im = -o_ptr[2*k];
1295
            z[i][31-k].re =  o_ptr[2*k+1];
1296
            z[i][31-k].im =  o_ptr[128+2*k+1];
1297
        }
1298
    }
1299

    
1300
    /* apply AC-3 post-rotation & reordering */
1301
    for(k=0; k<64; k++) {
1302
        o_ptr[    2*k  ] = -z[0][   k].im;
1303
        o_ptr[    2*k+1] =  z[0][63-k].re;
1304
        o_ptr[128+2*k  ] = -z[0][   k].re;
1305
        o_ptr[128+2*k+1] =  z[0][63-k].im;
1306
        o_ptr[256+2*k  ] = -z[1][   k].re;
1307
        o_ptr[256+2*k+1] =  z[1][63-k].im;
1308
        o_ptr[384+2*k  ] =  z[1][   k].im;
1309
        o_ptr[384+2*k+1] = -z[1][63-k].re;
1310
    }
1311
}
1312

    
1313
/* IMDCT Transform. */
1314
static inline void do_imdct(AC3DecodeContext *ctx)
1315
{
1316
    int ch;
1317

    
1318
    if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
1319
        ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1320
                                      ctx->transform_coeffs[0], ctx->tmp_imdct);
1321
    }
1322
    for (ch=1; ch<=ctx->nfchans; ch++) {
1323
        if ((ctx->blksw >> (ch-1)) & 1)
1324
            do_imdct_256(ctx, ch);
1325
        else
1326
            ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1327
                                          ctx->transform_coeffs[ch],
1328
                                          ctx->tmp_imdct);
1329

    
1330
        ctx->dsp.vector_fmul_add_add(ctx->output[ch], ctx->tmp_output,
1331
                                     ctx->window, ctx->delay[ch], 384, 256, 1);
1332
        ctx->dsp.vector_fmul_reverse(ctx->delay[ch], ctx->tmp_output+256,
1333
                                     ctx->window, 256);
1334
    }
1335
}
1336

    
1337
/* Parse the audio block from ac3 bitstream.
1338
 * This function extract the audio block from the ac3 bitstream
1339
 * and produces the output for the block. This function must
1340
 * be called for each of the six audio block in the ac3 bitstream.
1341
 */
1342
static int ac3_parse_audio_block(AC3DecodeContext * ctx)
1343
{
1344
    int nfchans = ctx->nfchans;
1345
    int acmod = ctx->acmod;
1346
    int i, bnd, rbnd, seg, grpsize;
1347
    GetBitContext *gb = &ctx->gb;
1348
    int bit_alloc_flags = 0;
1349
    uint8_t *dexps;
1350
    int mstrcplco, cplcoexp, cplcomant;
1351
    int dynrng, chbwcod, ngrps, cplabsexp, skipl;
1352

    
1353
    ctx->blksw = 0;
1354
    for (i = 0; i < nfchans; i++) /*block switch flag */
1355
        ctx->blksw |= get_bits1(gb) << i;
1356

    
1357
    ctx->dithflag = 0;
1358
    for (i = 0; i < nfchans; i++) /* dithering flag */
1359
        ctx->dithflag |= get_bits1(gb) << i;
1360

    
1361
    if (get_bits1(gb)) { /* dynamic range */
1362
        dynrng = get_sbits(gb, 8);
1363
        ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1364
    }
1365

    
1366
    if (acmod == 0x00 && get_bits1(gb)) { /* dynamic range 1+1 mode */
1367
        dynrng = get_sbits(gb, 8);
1368
        ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1369
    }
1370

    
1371
    get_downmix_coeffs(ctx);
1372

    
1373
    if (get_bits1(gb)) { /* coupling strategy */
1374
        ctx->cplinu = get_bits1(gb);
1375
        ctx->cplbndstrc = 0;
1376
        ctx->chincpl = 0;
1377
        if (ctx->cplinu) { /* coupling in use */
1378
            for (i = 0; i < nfchans; i++)
1379
                ctx->chincpl |= get_bits1(gb) << i;
1380

    
1381
            if (acmod == 0x02)
1382
                ctx->phsflginu = get_bits1(gb); //phase flag in use
1383

    
1384
            ctx->cplbegf = get_bits(gb, 4);
1385
            ctx->cplendf = get_bits(gb, 4);
1386

    
1387
            if (3 + ctx->cplendf - ctx->cplbegf < 0) {
1388
                av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", ctx->cplendf, ctx->cplbegf);
1389
                return -1;
1390
            }
1391

    
1392
            ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf;
1393
            ctx->cplstrtmant = ctx->cplbegf * 12 + 37;
1394
            ctx->cplendmant = ctx->cplendf * 12 + 73;
1395
            for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
1396
                if (get_bits1(gb)) {
1397
                    ctx->cplbndstrc |= 1 << i;
1398
                    ctx->ncplbnd--;
1399
                }
1400
        }
1401
    }
1402

    
1403
    if (ctx->cplinu) {
1404
        ctx->cplcoe = 0;
1405

    
1406
        for (i = 0; i < nfchans; i++)
1407
            if ((ctx->chincpl) >> i & 1)
1408
                if (get_bits1(gb)) { /* coupling co-ordinates */
1409
                    ctx->cplcoe |= 1 << i;
1410
                    mstrcplco = 3 * get_bits(gb, 2);
1411
                    for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
1412
                        cplcoexp = get_bits(gb, 4);
1413
                        cplcomant = get_bits(gb, 4);
1414
                        if (cplcoexp == 15)
1415
                            cplcomant <<= 14;
1416
                        else
1417
                            cplcomant = (cplcomant | 0x10) << 13;
1418
                        ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
1419
                    }
1420
                }
1421

    
1422
        if (acmod == 0x02 && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
1423
            for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
1424
                if (get_bits1(gb))
1425
                    ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
1426
    }
1427

    
1428
    if (acmod == 0x02) {/* rematrixing */
1429
        ctx->rematstr = get_bits1(gb);
1430
        if (ctx->rematstr) {
1431
            ctx->rematflg = 0;
1432

    
1433
            if (!(ctx->cplinu) || ctx->cplbegf > 2)
1434
                for (rbnd = 0; rbnd < 4; rbnd++)
1435
                    ctx->rematflg |= get_bits1(gb) << rbnd;
1436
            if (ctx->cplbegf > 0 && ctx->cplbegf <= 2 && ctx->cplinu)
1437
                for (rbnd = 0; rbnd < 3; rbnd++)
1438
                    ctx->rematflg |= get_bits1(gb) << rbnd;
1439
            if (ctx->cplbegf == 0 && ctx->cplinu)
1440
                for (rbnd = 0; rbnd < 2; rbnd++)
1441
                    ctx->rematflg |= get_bits1(gb) << rbnd;
1442
        }
1443
    }
1444

    
1445
    ctx->cplexpstr = EXP_REUSE;
1446
    ctx->lfeexpstr = EXP_REUSE;
1447
    if (ctx->cplinu) /* coupling exponent strategy */
1448
        ctx->cplexpstr = get_bits(gb, 2);
1449
    for (i = 0; i < nfchans; i++)  /* channel exponent strategy */
1450
        ctx->chexpstr[i] = get_bits(gb, 2);
1451
    if (ctx->lfeon)  /* lfe exponent strategy */
1452
        ctx->lfeexpstr = get_bits1(gb);
1453

    
1454
    for (i = 0; i < nfchans; i++) /* channel bandwidth code */
1455
        if (ctx->chexpstr[i] != EXP_REUSE) {
1456
            if ((ctx->chincpl >> i) & 1)
1457
                ctx->endmant[i] = ctx->cplstrtmant;
1458
            else {
1459
                chbwcod = get_bits(gb, 6);
1460
                if (chbwcod > 60) {
1461
                    av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
1462
                    return -1;
1463
                }
1464
                ctx->endmant[i] = chbwcod * 3 + 73;
1465
            }
1466
        }
1467

    
1468
    if (ctx->cplexpstr != EXP_REUSE) {/* coupling exponents */
1469
        bit_alloc_flags = 64;
1470
        cplabsexp = get_bits(gb, 4) << 1;
1471
        ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
1472
        decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant);
1473
    }
1474

    
1475
    for (i = 0; i < nfchans; i++) /* fbw channel exponents */
1476
        if (ctx->chexpstr[i] != EXP_REUSE) {
1477
            bit_alloc_flags |= 1 << i;
1478
            grpsize = 3 << (ctx->chexpstr[i] - 1);
1479
            ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
1480
            dexps = ctx->dexps[i];
1481
            dexps[0] = get_bits(gb, 4);
1482
            decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1);
1483
            skip_bits(gb, 2); /* skip gainrng */
1484
        }
1485

    
1486
    if (ctx->lfeexpstr != EXP_REUSE) { /* lfe exponents */
1487
        bit_alloc_flags |= 32;
1488
        ctx->dlfeexps[0] = get_bits(gb, 4);
1489
        decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1);
1490
    }
1491

    
1492
    if (get_bits1(gb)) { /* bit allocation information */
1493
        bit_alloc_flags = 127;
1494
        ctx->sdcycod = get_bits(gb, 2);
1495
        ctx->fdcycod = get_bits(gb, 2);
1496
        ctx->sgaincod = get_bits(gb, 2);
1497
        ctx->dbpbcod = get_bits(gb, 2);
1498
        ctx->floorcod = get_bits(gb, 3);
1499
    }
1500

    
1501
    if (get_bits1(gb)) { /* snroffset */
1502
        bit_alloc_flags = 127;
1503
        ctx->csnroffst = get_bits(gb, 6);
1504
        if (ctx->cplinu) { /* coupling fine snr offset and fast gain code */
1505
            ctx->cplfsnroffst = get_bits(gb, 4);
1506
            ctx->cplfgaincod = get_bits(gb, 3);
1507
        }
1508
        for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
1509
            ctx->fsnroffst[i] = get_bits(gb, 4);
1510
            ctx->fgaincod[i] = get_bits(gb, 3);
1511
        }
1512
        if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
1513
            ctx->lfefsnroffst = get_bits(gb, 4);
1514
            ctx->lfefgaincod = get_bits(gb, 3);
1515
        }
1516
    }
1517

    
1518
    if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
1519
        bit_alloc_flags |= 64;
1520
        ctx->cplfleak = get_bits(gb, 3);
1521
        ctx->cplsleak = get_bits(gb, 3);
1522
    }
1523

    
1524
    if (get_bits1(gb)) { /* delta bit allocation information */
1525
        bit_alloc_flags = 127;
1526

    
1527
        if (ctx->cplinu) {
1528
            ctx->cpldeltbae = get_bits(gb, 2);
1529
            if (ctx->cpldeltbae == DBA_RESERVED) {
1530
                av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
1531
                return -1;
1532
            }
1533
        }
1534

    
1535
        for (i = 0; i < nfchans; i++) {
1536
            ctx->deltbae[i] = get_bits(gb, 2);
1537
            if (ctx->deltbae[i] == DBA_RESERVED) {
1538
                av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
1539
                return -1;
1540
            }
1541
        }
1542

    
1543
        if (ctx->cplinu)
1544
            if (ctx->cpldeltbae == DBA_NEW) { /*coupling delta offset, len and bit allocation */
1545
                ctx->cpldeltnseg = get_bits(gb, 3);
1546
                for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
1547
                    ctx->cpldeltoffst[seg] = get_bits(gb, 5);
1548
                    ctx->cpldeltlen[seg] = get_bits(gb, 4);
1549
                    ctx->cpldeltba[seg] = get_bits(gb, 3);
1550
                }
1551
            }
1552

    
1553
        for (i = 0; i < nfchans; i++)
1554
            if (ctx->deltbae[i] == DBA_NEW) {/*channel delta offset, len and bit allocation */
1555
                ctx->deltnseg[i] = get_bits(gb, 3);
1556
                for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
1557
                    ctx->deltoffst[i][seg] = get_bits(gb, 5);
1558
                    ctx->deltlen[i][seg] = get_bits(gb, 4);
1559
                    ctx->deltba[i][seg] = get_bits(gb, 3);
1560
                }
1561
            }
1562
    }
1563

    
1564
    if (bit_alloc_flags) {
1565
        /* set bit allocation parameters */
1566
        ctx->bit_alloc_params.fscod = ctx->fscod;
1567
        ctx->bit_alloc_params.halfratecod = 0;
1568
        ctx->bit_alloc_params.sdecay = ff_sdecaytab[ctx->sdcycod];
1569
        ctx->bit_alloc_params.fdecay = ff_fdecaytab[ctx->fdcycod];
1570
        ctx->bit_alloc_params.sgain = ff_sgaintab[ctx->sgaincod];
1571
        ctx->bit_alloc_params.dbknee = ff_dbkneetab[ctx->dbpbcod];
1572
        ctx->bit_alloc_params.floor = ff_floortab[ctx->floorcod];
1573
        ctx->bit_alloc_params.cplfleak = ctx->cplfleak;
1574
        ctx->bit_alloc_params.cplsleak = ctx->cplsleak;
1575

    
1576
        if (ctx->chincpl && (bit_alloc_flags & 64))
1577
            do_bit_allocation(ctx, 5);
1578
        for (i = 0; i < nfchans; i++)
1579
            if ((bit_alloc_flags >> i) & 1)
1580
                do_bit_allocation(ctx, i);
1581
        if (ctx->lfeon && (bit_alloc_flags & 32))
1582
            do_bit_allocation(ctx, 6);
1583
    }
1584

    
1585
    if (get_bits1(gb)) { /* unused dummy data */
1586
        skipl = get_bits(gb, 9);
1587
        while(skipl--)
1588
            skip_bits(gb, 8);
1589
    }
1590
    /* unpack the transform coefficients
1591
     * * this also uncouples channels if coupling is in use.
1592
     */
1593
    if (get_transform_coeffs(ctx)) {
1594
        av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1595
        return -1;
1596
    }
1597

    
1598
    /* recover coefficients if rematrixing is in use */
1599
    if (ctx->rematflg)
1600
        do_rematrixing(ctx);
1601

    
1602
    do_downmix(ctx);
1603

    
1604
    do_imdct(ctx);
1605

    
1606
    return 0;
1607
}
1608

    
1609
static inline int16_t convert(int32_t i)
1610
{
1611
    if (i > 0x43c07fff)
1612
        return 32767;
1613
    else if (i <= 0x43bf8000)
1614
        return -32768;
1615
    else
1616
        return (i - 0x43c00000);
1617
}
1618

    
1619
/* Decode ac3 frame.
1620
 *
1621
 * @param avctx Pointer to AVCodecContext
1622
 * @param data Pointer to pcm smaples
1623
 * @param data_size Set to number of pcm samples produced by decoding
1624
 * @param buf Data to be decoded
1625
 * @param buf_size Size of the buffer
1626
 */
1627
static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1628
{
1629
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1630
    int16_t *out_samples = (int16_t *)data;
1631
    int i, j, k, start;
1632
    int32_t *int_ptr[6];
1633

    
1634
    for (i = 0; i < 6; i++)
1635
        int_ptr[i] = (int32_t *)(&ctx->output[i]);
1636

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

    
1640
    //Parse the syncinfo.
1641
    //If 'fscod' or 'bsid' is not valid the decoder shall mute as per the standard.
1642
    if (!ac3_parse_sync_info(ctx)) {
1643
        av_log(avctx, AV_LOG_ERROR, "\n");
1644
        *data_size = 0;
1645
        return buf_size;
1646
    }
1647

    
1648
    //Parse the BSI.
1649
    //If 'bsid' is not valid decoder shall not decode the audio as per the standard.
1650
    ac3_parse_bsi(ctx);
1651

    
1652
    avctx->sample_rate = ctx->sampling_rate;
1653
    avctx->bit_rate = ctx->bit_rate;
1654

    
1655
    if (avctx->channels == 0) {
1656
        ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1657
        if (ctx->lfeon)
1658
            ctx->blkoutput |= AC3_OUTPUT_LFEON;
1659
        avctx->channels = ctx->nfchans + ctx->lfeon;
1660
    }
1661
    else if (avctx->channels == 1)
1662
        ctx->blkoutput |= AC3_OUTPUT_MONO;
1663
    else if (avctx->channels == 2) {
1664
        if (ctx->dsurmod == 0x02)
1665
            ctx->blkoutput |= AC3_OUTPUT_DOLBY;
1666
        else
1667
            ctx->blkoutput |= AC3_OUTPUT_STEREO;
1668
    }
1669
    else {
1670
        if (avctx->channels < (ctx->nfchans + ctx->lfeon))
1671
            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);
1672
        ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1673
        if (ctx->lfeon)
1674
            ctx->blkoutput |= AC3_OUTPUT_LFEON;
1675
        avctx->channels = ctx->nfchans + ctx->lfeon;
1676
    }
1677

    
1678
    //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);
1679

    
1680
    //Parse the Audio Blocks.
1681
    for (i = 0; i < NB_BLOCKS; i++) {
1682
        if (ac3_parse_audio_block(ctx)) {
1683
            av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1684
            *data_size = 0;
1685
            return ctx->frame_size;
1686
        }
1687
        start = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1;
1688
        for (k = 0; k < BLOCK_SIZE; k++)
1689
            for (j = start; j <= avctx->channels; j++)
1690
                *(out_samples++) = convert(int_ptr[j][k]);
1691
    }
1692
    *data_size = NB_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);
1693
    return ctx->frame_size;
1694
}
1695

    
1696
/* Uninitialize ac3 decoder.
1697
 */
1698
static int ac3_decode_end(AVCodecContext *avctx)
1699
{
1700
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1701
    ff_mdct_end(&ctx->imdct_512);
1702
    ff_mdct_end(&ctx->imdct_256);
1703

    
1704
    return 0;
1705
}
1706

    
1707
AVCodec ac3_decoder = {
1708
    .name = "ac3",
1709
    .type = CODEC_TYPE_AUDIO,
1710
    .id = CODEC_ID_AC3,
1711
    .priv_data_size = sizeof (AC3DecodeContext),
1712
    .init = ac3_decode_init,
1713
    .close = ac3_decode_end,
1714
    .decode = ac3_decode_frame,
1715
};
1716