Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3dec.c @ 0ce66801

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

    
28
#include <stdio.h>
29
#include <stddef.h>
30
#include <math.h>
31
#include <string.h>
32

    
33
#define ALT_BITSTREAM_READER
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
static int8_t exp_1[128];
50
static int8_t exp_2[128];
51
static int8_t exp_3[128];
52

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

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

    
61
static int16_t l7_quantizers[7];
62

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

    
66
static int16_t l15_quantizers[15];
67

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

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

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

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

    
83
#define BLOCK_SIZE    256
84

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

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

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

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

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

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

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

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

    
166
    int      blkoutput;         //output configuration for block
167

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
251
}
252

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

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

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

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

    
272
    /* Exponent Decoding Tables */
273
    for (i = 0; i < 5; i++) {
274
        v = i - 2;
275
        for (j = 0; j < 25; j++)
276
            exp_1[i * 25 + j] = v;
277
    }
278

    
279
    for (i = 0; i < 25; i++) {
280
        v = (i % 5) - 2;
281
        for (j = 0; j < 5; j++)
282
            exp_2[i * 5 + j] = v;
283
    }
284

    
285
    for (i = 0; i < 25; i++) {
286
        v = -2;
287
        for (j = 0; j < 5; j++)
288
            exp_3[i * 5 + j] = v++;
289
    }
290

    
291
    for (i = 125; i < 128; i++)
292
        exp_1[i] = exp_2[i] = exp_3[i] = 25;
293
    /* End Exponent Decoding Tables */
294

    
295
    /* Quantizer ungrouping tables. */
296
    // for level-3 quantizers
297
    generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
298
    generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
299
    generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
300

    
301
    //for level-5 quantizers
302
    generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
303
    generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
304
    generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
305

    
306
    //for level-7 quantizers
307
    generate_quantizers_table(l7_quantizers, 7, 7);
308

    
309
    //for level-4 quantizers
310
    generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
311
    generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
312

    
313
    //for level-15 quantizers
314
    generate_quantizers_table(l15_quantizers, 15, 15);
315
    /* End Quantizer ungrouping tables. */
316

    
317
    //generate scale factors
318
    for (i = 0; i < 25; i++)
319
        scale_factors[i] = pow(2.0, -(i + 15));
320
}
321

    
322

    
323
static int ac3_decode_init(AVCodecContext *avctx)
324
{
325
    AC3DecodeContext *ctx = avctx->priv_data;
326

    
327
    ac3_common_init();
328
    ac3_tables_init();
329
    ff_mdct_init(&ctx->imdct_256, 8, 1);
330
    ff_mdct_init(&ctx->imdct_512, 9, 1);
331
    ac3_window_init(ctx->window);
332
    dsputil_init(&ctx->dsp, avctx);
333
    av_init_random(0, &ctx->dith_state);
334

    
335
    return 0;
336
}
337
/*********** END INIT FUNCTIONS ***********/
338

    
339
/* Synchronize to ac3 bitstream.
340
 * This function searches for the syncword '0xb77'.
341
 *
342
 * @param buf Pointer to "probable" ac3 bitstream buffer
343
 * @param buf_size Size of buffer
344
 * @return Returns the position where syncword is found, -1 if no syncword is found
345
 */
346
static int ac3_synchronize(uint8_t *buf, int buf_size)
347
{
348
    int i;
349

    
350
    for (i = 0; i < buf_size - 1; i++)
351
        if (buf[i] == 0x0b && buf[i + 1] == 0x77)
352
            return i;
353

    
354
    return -1;
355
}
356

    
357
/* Parse the 'sync_info' from the ac3 bitstream.
358
 * This function extracts the sync_info from ac3 bitstream.
359
 * GetBitContext within AC3DecodeContext must point to
360
 * start of the synchronized ac3 bitstream.
361
 *
362
 * @param ctx  AC3DecodeContext
363
 * @return Returns framesize, returns 0 if fscod, frmsizecod or bsid is not valid
364
 */
365
static int ac3_parse_sync_info(AC3DecodeContext *ctx)
366
{
367
    GetBitContext *gb = &ctx->gb;
368
    int frmsizecod, bsid;
369

    
370
    skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
371
    ctx->crc1 = get_bits(gb, 16);
372
    ctx->fscod = get_bits(gb, 2);
373
    if (ctx->fscod == 0x03)
374
        return 0;
375
    frmsizecod = get_bits(gb, 6);
376
    if (frmsizecod >= 38)
377
        return 0;
378
    ctx->sampling_rate = ff_ac3_freqs[ctx->fscod];
379
    ctx->bit_rate = ff_ac3_bitratetab[frmsizecod >> 1];
380

    
381
    /* we include it here in order to determine validity of ac3 frame */
382
    bsid = get_bits(gb, 5);
383
    if (bsid > 0x08)
384
        return 0;
385
    skip_bits(gb, 3); //skip the bsmod, bsi->bsmod = get_bits(gb, 3);
386

    
387
    switch (ctx->fscod) {
388
        case 0x00:
389
            ctx->frame_size = 4 * ctx->bit_rate;
390
            return ctx->frame_size;
391
        case 0x01:
392
            ctx->frame_size = 2 * (320 * ctx->bit_rate / 147 + (frmsizecod & 1));
393
            return ctx->frame_size;
394
        case 0x02:
395
            ctx->frame_size =  6 * ctx->bit_rate;
396
            return ctx->frame_size;
397
    }
398

    
399
    /* never reached */
400
    return 0;
401
}
402

    
403
/* Parse bsi from ac3 bitstream.
404
 * This function extracts the bitstream information (bsi) from ac3 bitstream.
405
 *
406
 * @param ctx AC3DecodeContext after processed by ac3_parse_sync_info
407
 */
408
static void ac3_parse_bsi(AC3DecodeContext *ctx)
409
{
410
    GetBitContext *gb = &ctx->gb;
411
    int i;
412

    
413
    ctx->cmixlev = 0;
414
    ctx->surmixlev = 0;
415
    ctx->dsurmod = 0;
416
    ctx->nfchans = 0;
417
    ctx->cpldeltbae = DBA_NONE;
418
    ctx->cpldeltnseg = 0;
419
    for (i = 0; i < 5; i++) {
420
        ctx->deltbae[i] = DBA_NONE;
421
        ctx->deltnseg[i] = 0;
422
    }
423
    ctx->dynrng = 1.0;
424
    ctx->dynrng2 = 1.0;
425

    
426
    ctx->acmod = get_bits(gb, 3);
427
    ctx->nfchans = nfchans_tbl[ctx->acmod];
428

    
429
    if (ctx->acmod & 0x01 && ctx->acmod != 0x01)
430
        ctx->cmixlev = get_bits(gb, 2);
431
    if (ctx->acmod & 0x04)
432
        ctx->surmixlev = get_bits(gb, 2);
433
    if (ctx->acmod == 0x02)
434
        ctx->dsurmod = get_bits(gb, 2);
435

    
436
    ctx->lfeon = get_bits1(gb);
437

    
438
    i = !(ctx->acmod);
439
    do {
440
        skip_bits(gb, 5); //skip dialog normalization
441
        if (get_bits1(gb))
442
            skip_bits(gb, 8); //skip compression
443
        if (get_bits1(gb))
444
            skip_bits(gb, 8); //skip language code
445
        if (get_bits1(gb))
446
            skip_bits(gb, 7); //skip audio production information
447
    } while (i--);
448

    
449
    skip_bits(gb, 2); //skip copyright bit and original bitstream bit
450

    
451
    if (get_bits1(gb))
452
        skip_bits(gb, 14); //skip timecode1
453
    if (get_bits1(gb))
454
        skip_bits(gb, 14); //skip timecode2
455

    
456
    if (get_bits1(gb)) {
457
        i = get_bits(gb, 6); //additional bsi length
458
        do {
459
            skip_bits(gb, 8);
460
        } while(i--);
461
    }
462
}
463

    
464
/* Decodes the grouped exponents.
465
 * This function decodes the coded exponents according to exponent strategy
466
 * and stores them in the decoded exponents buffer.
467
 *
468
 * @param gb GetBitContext which points to start of coded exponents
469
 * @param expstr Exponent coding strategy
470
 * @param ngrps Number of grouped exponetns
471
 * @param absexp Absolute exponent
472
 * @param dexps Decoded exponents are stored in dexps
473
 * @return Returns 0 if exponents are decoded successfully, -1 if error occurs
474
 */
475
static int decode_exponents(GetBitContext *gb, int expstr, int ngrps, uint8_t absexp, uint8_t *dexps)
476
{
477
    int exps;
478

    
479
    while (ngrps--) {
480
        exps = get_bits(gb, 7);
481

    
482
        absexp += exp_1[exps];
483
        if (absexp > 24) {
484
            av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
485
            return -ngrps;
486
        }
487
        switch (expstr) {
488
            case EXP_D45:
489
                *(dexps++) = absexp;
490
                *(dexps++) = absexp;
491
            case EXP_D25:
492
                *(dexps++) = absexp;
493
            case EXP_D15:
494
                *(dexps++) = absexp;
495
        }
496

    
497
        absexp += exp_2[exps];
498
        if (absexp > 24) {
499
            av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
500
            return -ngrps;
501
        }
502
        switch (expstr) {
503
            case EXP_D45:
504
                *(dexps++) = absexp;
505
                *(dexps++) = absexp;
506
            case EXP_D25:
507
                *(dexps++) = absexp;
508
            case EXP_D15:
509
                *(dexps++) = absexp;
510
        }
511

    
512
        absexp += exp_3[exps];
513
        if (absexp > 24) {
514
            av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
515
            return -ngrps;
516
        }
517
        switch (expstr) {
518
            case EXP_D45:
519
                *(dexps++) = absexp;
520
                *(dexps++) = absexp;
521
            case EXP_D25:
522
                *(dexps++) = absexp;
523
            case EXP_D15:
524
                *(dexps++) = absexp;
525
        }
526
    }
527

    
528
    return 0;
529
}
530

    
531
/* Performs bit allocation.
532
 * This function performs bit allocation for the requested chanenl.
533
 */
534
static void do_bit_allocation(AC3DecodeContext *ctx, int chnl)
535
{
536
    int fgain, snroffset;
537

    
538
    if (chnl == 5) {
539
        fgain = ff_fgaintab[ctx->cplfgaincod];
540
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->cplfsnroffst) << 2;
541
        ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->cplbap,
542
                                      ctx->dcplexps, ctx->cplstrtmant,
543
                                      ctx->cplendmant, snroffset, fgain, 0,
544
                                      ctx->cpldeltbae, ctx->cpldeltnseg,
545
                                      ctx->cpldeltoffst, ctx->cpldeltlen,
546
                                      ctx->cpldeltba);
547
    }
548
    else if (chnl == 6) {
549
        fgain = ff_fgaintab[ctx->lfefgaincod];
550
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->lfefsnroffst) << 2;
551
        ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->lfebap,
552
                                      ctx->dlfeexps, 0, 7, snroffset, fgain, 1,
553
                                      DBA_NONE, 0, NULL, NULL, NULL);
554
    }
555
    else {
556
        fgain = ff_fgaintab[ctx->fgaincod[chnl]];
557
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->fsnroffst[chnl]) << 2;
558
        ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->bap[chnl],
559
                                      ctx->dexps[chnl], 0, ctx->endmant[chnl],
560
                                      snroffset, fgain, 0, ctx->deltbae[chnl],
561
                                      ctx->deltnseg[chnl], ctx->deltoffst[chnl],
562
                                      ctx->deltlen[chnl], ctx->deltba[chnl]);
563
    }
564
}
565

    
566
typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
567
    int16_t l3_quantizers[3];
568
    int16_t l5_quantizers[3];
569
    int16_t l11_quantizers[2];
570
    int l3ptr;
571
    int l5ptr;
572
    int l11ptr;
573
} mant_groups;
574

    
575
#define TRANSFORM_COEFF(tc, m, e, f) (tc) = (m) * (f)[(e)]
576

    
577
/* Get the transform coefficients for coupling channel and uncouple channels.
578
 * The coupling transform coefficients starts at the the cplstrtmant, which is
579
 * equal to endmant[ch] for fbw channels. Hence we can uncouple channels before
580
 * getting transform coefficients for the channel.
581
 */
582
static int get_transform_coeffs_cpling(AC3DecodeContext *ctx, mant_groups *m)
583
{
584
    GetBitContext *gb = &ctx->gb;
585
    int ch, start, end, cplbndstrc, bnd, gcode, tbap;
586
    float cplcos[5], cplcoeff;
587
    uint8_t *exps = ctx->dcplexps;
588
    uint8_t *bap = ctx->cplbap;
589

    
590
    cplbndstrc = ctx->cplbndstrc;
591
    start = ctx->cplstrtmant;
592
    bnd = 0;
593

    
594
    while (start < ctx->cplendmant) {
595
        end = start + 12;
596
        while (cplbndstrc & 1) {
597
            end += 12;
598
            cplbndstrc >>= 1;
599
        }
600
        cplbndstrc >>= 1;
601
        for (ch = 0; ch < ctx->nfchans; ch++)
602
            cplcos[ch] = ctx->chcoeffs[ch] * ctx->cplco[ch][bnd];
603
        bnd++;
604

    
605
        while (start < end) {
606
            tbap = bap[start];
607
            switch(tbap) {
608
                case 0:
609
                    for (ch = 0; ch < ctx->nfchans; ch++)
610
                        if (((ctx->chincpl) >> ch) & 1) {
611
                            if ((ctx->dithflag >> ch) & 1) {
612
                                TRANSFORM_COEFF(cplcoeff, av_random(&ctx->dith_state) & 0xFFFF, exps[start], scale_factors);
613
                                ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch] * LEVEL_MINUS_3DB;
614
                            } else
615
                                ctx->transform_coeffs[ch + 1][start] = 0;
616
                        }
617
                    start++;
618
                    continue;
619
                case 1:
620
                    if (m->l3ptr > 2) {
621
                        gcode = get_bits(gb, 5);
622
                        m->l3_quantizers[0] = l3_quantizers_1[gcode];
623
                        m->l3_quantizers[1] = l3_quantizers_2[gcode];
624
                        m->l3_quantizers[2] = l3_quantizers_3[gcode];
625
                        m->l3ptr = 0;
626
                    }
627
                    TRANSFORM_COEFF(cplcoeff, m->l3_quantizers[m->l3ptr++], exps[start], scale_factors);
628
                    break;
629

    
630
                case 2:
631
                    if (m->l5ptr > 2) {
632
                        gcode = get_bits(gb, 7);
633
                        m->l5_quantizers[0] = l5_quantizers_1[gcode];
634
                        m->l5_quantizers[1] = l5_quantizers_2[gcode];
635
                        m->l5_quantizers[2] = l5_quantizers_3[gcode];
636
                        m->l5ptr = 0;
637
                    }
638
                    TRANSFORM_COEFF(cplcoeff, m->l5_quantizers[m->l5ptr++], exps[start], scale_factors);
639
                    break;
640

    
641
                case 3:
642
                    TRANSFORM_COEFF(cplcoeff, l7_quantizers[get_bits(gb, 3)], exps[start], scale_factors);
643
                    break;
644

    
645
                case 4:
646
                    if (m->l11ptr > 1) {
647
                        gcode = get_bits(gb, 7);
648
                        m->l11_quantizers[0] = l11_quantizers_1[gcode];
649
                        m->l11_quantizers[1] = l11_quantizers_2[gcode];
650
                        m->l11ptr = 0;
651
                    }
652
                    TRANSFORM_COEFF(cplcoeff, m->l11_quantizers[m->l11ptr++], exps[start], scale_factors);
653
                    break;
654

    
655
                case 5:
656
                    TRANSFORM_COEFF(cplcoeff, l15_quantizers[get_bits(gb, 4)], exps[start], scale_factors);
657
                    break;
658

    
659
                default:
660
                    TRANSFORM_COEFF(cplcoeff, get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]),
661
                            exps[start], scale_factors);
662
            }
663
            for (ch = 0; ch < ctx->nfchans; ch++)
664
                if ((ctx->chincpl >> ch) & 1)
665
                    ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
666
            start++;
667
        }
668
    }
669

    
670
    return 0;
671
}
672

    
673
/* Get the transform coefficients for particular channel */
674
static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
675
{
676
    GetBitContext *gb = &ctx->gb;
677
    int i, gcode, tbap, dithflag, end;
678
    uint8_t *exps;
679
    uint8_t *bap;
680
    float *coeffs;
681
    float factors[25];
682

    
683
    for (i = 0; i < 25; i++)
684
        factors[i] = scale_factors[i] * ctx->chcoeffs[ch_index];
685

    
686
    if (ch_index != -1) { /* fbw channels */
687
        dithflag = (ctx->dithflag >> ch_index) & 1;
688
        exps = ctx->dexps[ch_index];
689
        bap = ctx->bap[ch_index];
690
        coeffs = ctx->transform_coeffs[ch_index + 1];
691
        end = ctx->endmant[ch_index];
692
    } else if (ch_index == -1) {
693
        dithflag = 0;
694
        exps = ctx->dlfeexps;
695
        bap = ctx->lfebap;
696
        coeffs = ctx->transform_coeffs[0];
697
        end = 7;
698
    }
699

    
700

    
701
    for (i = 0; i < end; i++) {
702
        tbap = bap[i];
703
        switch (tbap) {
704
            case 0:
705
                if (!dithflag) {
706
                    coeffs[i] = 0;
707
                    continue;
708
                }
709
                else {
710
                    TRANSFORM_COEFF(coeffs[i], av_random(&ctx->dith_state) & 0xFFFF, exps[i], factors);
711
                    coeffs[i] *= LEVEL_MINUS_3DB;
712
                    continue;
713
                }
714

    
715
            case 1:
716
                if (m->l3ptr > 2) {
717
                    gcode = get_bits(gb, 5);
718
                    m->l3_quantizers[0] = l3_quantizers_1[gcode];
719
                    m->l3_quantizers[1] = l3_quantizers_2[gcode];
720
                    m->l3_quantizers[2] = l3_quantizers_3[gcode];
721
                    m->l3ptr = 0;
722
                }
723
                TRANSFORM_COEFF(coeffs[i], m->l3_quantizers[m->l3ptr++], exps[i], factors);
724
                continue;
725

    
726
            case 2:
727
                if (m->l5ptr > 2) {
728
                    gcode = get_bits(gb, 7);
729
                    m->l5_quantizers[0] = l5_quantizers_1[gcode];
730
                    m->l5_quantizers[1] = l5_quantizers_2[gcode];
731
                    m->l5_quantizers[2] = l5_quantizers_3[gcode];
732
                    m->l5ptr = 0;
733
                }
734
                TRANSFORM_COEFF(coeffs[i], m->l5_quantizers[m->l5ptr++], exps[i], factors);
735
                continue;
736

    
737
            case 3:
738
                TRANSFORM_COEFF(coeffs[i], l7_quantizers[get_bits(gb, 3)], exps[i], factors);
739
                continue;
740

    
741
            case 4:
742
                if (m->l11ptr > 1) {
743
                    gcode = get_bits(gb, 7);
744
                    m->l11_quantizers[0] = l11_quantizers_1[gcode];
745
                    m->l11_quantizers[1] = l11_quantizers_2[gcode];
746
                    m->l11ptr = 0;
747
                }
748
                TRANSFORM_COEFF(coeffs[i], m->l11_quantizers[m->l11ptr++], exps[i], factors);
749
                continue;
750

    
751
            case 5:
752
                TRANSFORM_COEFF(coeffs[i], l15_quantizers[get_bits(gb, 4)], exps[i], factors);
753
                continue;
754

    
755
            default:
756
                TRANSFORM_COEFF(coeffs[i], get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]), exps[i], factors);
757
                continue;
758
        }
759
    }
760

    
761
    return 0;
762
}
763

    
764
/* Get the transform coefficients.
765
 * This function extracts the tranform coefficients form the ac3 bitstream.
766
 * This function is called after bit allocation is performed.
767
 */
768
static int get_transform_coeffs(AC3DecodeContext * ctx)
769
{
770
    int i, end;
771
    int got_cplchan = 0;
772
    mant_groups m;
773

    
774
    m.l3ptr = m.l5ptr = m.l11ptr = 3;
775

    
776
    for (i = 0; i < ctx->nfchans; i++) {
777
        /* transform coefficients for individual channel */
778
        if (get_transform_coeffs_ch(ctx, i, &m))
779
            return -1;
780
        /* tranform coefficients for coupling channels */
781
        if ((ctx->chincpl >> i) & 1)  {
782
            if (!got_cplchan) {
783
                if (get_transform_coeffs_cpling(ctx, &m)) {
784
                    av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
785
                    return -1;
786
                }
787
                got_cplchan = 1;
788
            }
789
            end = ctx->cplendmant;
790
        } else
791
            end = ctx->endmant[i];
792
        do
793
            ctx->transform_coeffs[i + 1][end] = 0;
794
        while(++end < 256);
795
    }
796
    if (ctx->lfeon) {
797
        if (get_transform_coeffs_ch(ctx, -1, &m))
798
                return -1;
799
        for (i = 7; i < 256; i++) {
800
            ctx->transform_coeffs[0][i] = 0;
801
        }
802
    }
803

    
804
    return 0;
805
}
806

    
807
/* Rematrixing routines. */
808
static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
809
{
810
    float tmp0, tmp1;
811

    
812
    while (start < end) {
813
        tmp0 = ctx->transform_coeffs[1][start];
814
        tmp1 = ctx->transform_coeffs[2][start];
815
        ctx->transform_coeffs[1][start] = tmp0 + tmp1;
816
        ctx->transform_coeffs[2][start] = tmp0 - tmp1;
817
        start++;
818
    }
819
}
820

    
821
static void do_rematrixing(AC3DecodeContext *ctx)
822
{
823
    int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
824
    int end, bndend;
825

    
826
    end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
827

    
828
    if (ctx->rematflg & 1)
829
        do_rematrixing1(ctx, bnd1, bnd2);
830

    
831
    if (ctx->rematflg & 2)
832
        do_rematrixing1(ctx, bnd2, bnd3);
833

    
834
    bndend = bnd4;
835
    if (bndend > end) {
836
        bndend = end;
837
        if (ctx->rematflg & 4)
838
            do_rematrixing1(ctx, bnd3, bndend);
839
    } else {
840
        if (ctx->rematflg & 4)
841
            do_rematrixing1(ctx, bnd3, bnd4);
842
        if (ctx->rematflg & 8)
843
            do_rematrixing1(ctx, bnd4, end);
844
    }
845
}
846

    
847
/* This function sets the normalized channel coefficients.
848
 * Transform coefficients are multipllied by the channel
849
 * coefficients to get normalized transform coefficients.
850
 */
851
static void get_downmix_coeffs(AC3DecodeContext *ctx)
852
{
853
    int from = ctx->acmod;
854
    int to = ctx->blkoutput;
855
    float clev = clevs[ctx->cmixlev];
856
    float slev = slevs[ctx->surmixlev];
857
    float nf = 1.0; //normalization factor for downmix coeffs
858
    int i;
859

    
860
    if (!ctx->acmod) {
861
        ctx->chcoeffs[0] = 2 * ctx->dynrng;
862
        ctx->chcoeffs[1] = 2 * ctx->dynrng2;
863
    } else {
864
        for (i = 0; i < ctx->nfchans; i++)
865
            ctx->chcoeffs[i] = 2 * ctx->dynrng;
866
    }
867

    
868
    if (to == AC3_OUTPUT_UNMODIFIED)
869
        return;
870

    
871
    switch (from) {
872
        case AC3_ACMOD_DUALMONO:
873
            switch (to) {
874
                case AC3_OUTPUT_MONO:
875
                case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */
876
                    nf = 0.5;
877
                    ctx->chcoeffs[0] *= nf;
878
                    ctx->chcoeffs[1] *= nf;
879
                    break;
880
            }
881
            break;
882
        case AC3_ACMOD_MONO:
883
            switch (to) {
884
                case AC3_OUTPUT_STEREO:
885
                    nf = LEVEL_MINUS_3DB;
886
                    ctx->chcoeffs[0] *= nf;
887
                    break;
888
            }
889
            break;
890
        case AC3_ACMOD_STEREO:
891
            switch (to) {
892
                case AC3_OUTPUT_MONO:
893
                    nf = LEVEL_MINUS_3DB;
894
                    ctx->chcoeffs[0] *= nf;
895
                    ctx->chcoeffs[1] *= nf;
896
                    break;
897
            }
898
            break;
899
        case AC3_ACMOD_3F:
900
            switch (to) {
901
                case AC3_OUTPUT_MONO:
902
                    nf = LEVEL_MINUS_3DB / (1.0 + clev);
903
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
904
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
905
                    ctx->chcoeffs[1] *= ((nf * clev * LEVEL_MINUS_3DB) / 2.0);
906
                    break;
907
                case AC3_OUTPUT_STEREO:
908
                    nf = 1.0 / (1.0 + clev);
909
                    ctx->chcoeffs[0] *= nf;
910
                    ctx->chcoeffs[2] *= nf;
911
                    ctx->chcoeffs[1] *= (nf * clev);
912
                    break;
913
            }
914
            break;
915
        case AC3_ACMOD_2F1R:
916
            switch (to) {
917
                case AC3_OUTPUT_MONO:
918
                    nf = 2.0 * LEVEL_MINUS_3DB / (2.0 + slev);
919
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
920
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
921
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
922
                    break;
923
                case AC3_OUTPUT_STEREO:
924
                    nf = 1.0 / (1.0 + (slev * LEVEL_MINUS_3DB));
925
                    ctx->chcoeffs[0] *= nf;
926
                    ctx->chcoeffs[1] *= nf;
927
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
928
                    break;
929
                case AC3_OUTPUT_DOLBY:
930
                    nf = 1.0 / (1.0 + LEVEL_MINUS_3DB);
931
                    ctx->chcoeffs[0] *= nf;
932
                    ctx->chcoeffs[1] *= nf;
933
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
934
                    break;
935
            }
936
            break;
937
        case AC3_ACMOD_3F1R:
938
            switch (to) {
939
                case AC3_OUTPUT_MONO:
940
                    nf = LEVEL_MINUS_3DB / (1.0 + clev + (slev / 2.0));
941
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
942
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
943
                    ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
944
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
945
                    break;
946
                case AC3_OUTPUT_STEREO:
947
                    nf = 1.0 / (1.0 + clev + (slev * LEVEL_MINUS_3DB));
948
                    ctx->chcoeffs[0] *= nf;
949
                    ctx->chcoeffs[2] *= nf;
950
                    ctx->chcoeffs[1] *= (nf * clev);
951
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
952
                    break;
953
                case AC3_OUTPUT_DOLBY:
954
                    nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
955
                    ctx->chcoeffs[0] *= nf;
956
                    ctx->chcoeffs[1] *= nf;
957
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
958
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
959
                    break;
960
            }
961
            break;
962
        case AC3_ACMOD_2F2R:
963
            switch (to) {
964
                case AC3_OUTPUT_MONO:
965
                    nf = LEVEL_MINUS_3DB / (1.0 + slev);
966
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
967
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
968
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
969
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
970
                    break;
971
                case AC3_OUTPUT_STEREO:
972
                    nf = 1.0 / (1.0 + slev);
973
                    ctx->chcoeffs[0] *= nf;
974
                    ctx->chcoeffs[1] *= nf;
975
                    ctx->chcoeffs[2] *= (nf * slev);
976
                    ctx->chcoeffs[3] *= (nf * slev);
977
                    break;
978
                case AC3_OUTPUT_DOLBY:
979
                    nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
980
                    ctx->chcoeffs[0] *= nf;
981
                    ctx->chcoeffs[1] *= nf;
982
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
983
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
984
                    break;
985
            }
986
            break;
987
        case AC3_ACMOD_3F2R:
988
            switch (to) {
989
                case AC3_OUTPUT_MONO:
990
                    nf = LEVEL_MINUS_3DB / (1.0 + clev + slev);
991
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
992
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
993
                    ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
994
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
995
                    ctx->chcoeffs[4] *= (nf * slev * LEVEL_MINUS_3DB);
996
                    break;
997
                case AC3_OUTPUT_STEREO:
998
                    nf = 1.0 / (1.0 + clev + slev);
999
                    ctx->chcoeffs[0] *= nf;
1000
                    ctx->chcoeffs[2] *= nf;
1001
                    ctx->chcoeffs[1] *= (nf * clev);
1002
                    ctx->chcoeffs[3] *= (nf * slev);
1003
                    ctx->chcoeffs[4] *= (nf * slev);
1004
                    break;
1005
                case AC3_OUTPUT_DOLBY:
1006
                    nf = 1.0 / (1.0 + (3.0 * LEVEL_MINUS_3DB));
1007
                    ctx->chcoeffs[0] *= nf;
1008
                    ctx->chcoeffs[1] *= nf;
1009
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1010
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
1011
                    ctx->chcoeffs[4] *= (nf * LEVEL_MINUS_3DB);
1012
                    break;
1013
            }
1014
            break;
1015
    }
1016
}
1017

    
1018
/*********** BEGIN DOWNMIX FUNCTIONS ***********/
1019
static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx)
1020
{
1021
    int i;
1022
    float (*output)[BLOCK_SIZE] = ctx->output;
1023

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

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

    
1035
    for (i = 0; i < 256; i++) {
1036
        tmp = output[1][i] + output[2][i];
1037
        output[1][i] = output[2][i] = tmp;
1038
    }
1039
}
1040

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

    
1046
    for (i = 0; i < 256; i++)
1047
        output[2][i] = output[1][i];
1048
}
1049

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

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

    
1060
static inline void mix_3f_to_mono(AC3DecodeContext *ctx)
1061
{
1062
    int i;
1063
    float (*output)[BLOCK_SIZE] = ctx->output;
1064

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

    
1071
static inline void mix_3f_to_stereo(AC3DecodeContext *ctx)
1072
{
1073
    int i;
1074
    float (*output)[BLOCK_SIZE] = ctx->output;
1075

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

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

    
1088
    for (i = 0; i < 256; i++)
1089
        output[1][i] += (output[2][i] + output[3][i]);
1090
    memset(output[2], 0, sizeof(output[2]));
1091
    memset(output[3], 0, sizeof(output[3]));
1092

    
1093
}
1094

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

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

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

    
1112
    for (i = 0; i < 256; i++) {
1113
        output[1][i] -= output[3][i];
1114
        output[2][i] += output[3][i];
1115
    }
1116
    memset(output[3], 0, sizeof(output[3]));
1117
}
1118

    
1119
static inline void mix_3f_1r_to_mono(AC3DecodeContext *ctx)
1120
{
1121
    int i;
1122
    float (*output)[BLOCK_SIZE] = ctx->output;
1123

    
1124
    for (i = 0; i < 256; i++)
1125
        output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1126
    memset(output[2], 0, sizeof(output[2]));
1127
    memset(output[3], 0, sizeof(output[3]));
1128
    memset(output[4], 0, sizeof(output[4]));
1129
}
1130

    
1131
static inline void mix_3f_1r_to_stereo(AC3DecodeContext *ctx)
1132
{
1133
    int i;
1134
    float (*output)[BLOCK_SIZE] = ctx->output;
1135

    
1136
    for (i = 0; i < 256; i++) {
1137
        output[1][i] += (output[2][i] + output[4][i]);
1138
        output[2][i] += (output[3][i] + output[4][i]);
1139
    }
1140
    memset(output[3], 0, sizeof(output[3]));
1141
    memset(output[4], 0, sizeof(output[4]));
1142
}
1143

    
1144
static inline void mix_3f_1r_to_dolby(AC3DecodeContext *ctx)
1145
{
1146
    int i;
1147
    float (*output)[BLOCK_SIZE] = ctx->output;
1148

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

    
1157
static inline void mix_2f_2r_to_mono(AC3DecodeContext *ctx)
1158
{
1159
    int i;
1160
    float (*output)[BLOCK_SIZE] = ctx->output;
1161

    
1162
    for (i = 0; i < 256; i++)
1163
        output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1164
    memset(output[2], 0, sizeof(output[2]));
1165
    memset(output[3], 0, sizeof(output[3]));
1166
    memset(output[4], 0, sizeof(output[4]));
1167
}
1168

    
1169
static inline void mix_2f_2r_to_stereo(AC3DecodeContext *ctx)
1170
{
1171
    int i;
1172
    float (*output)[BLOCK_SIZE] = ctx->output;
1173

    
1174
    for (i = 0; i < 256; i++) {
1175
        output[1][i] += output[3][i];
1176
        output[2][i] += output[4][i];
1177
    }
1178
    memset(output[3], 0, sizeof(output[3]));
1179
    memset(output[4], 0, sizeof(output[4]));
1180
}
1181

    
1182
static inline void mix_2f_2r_to_dolby(AC3DecodeContext *ctx)
1183
{
1184
    int i;
1185
    float (*output)[BLOCK_SIZE] = ctx->output;
1186

    
1187
    for (i = 0; i < 256; i++) {
1188
        output[1][i] -= output[3][i];
1189
        output[2][i] += output[4][i];
1190
    }
1191
    memset(output[3], 0, sizeof(output[3]));
1192
    memset(output[4], 0, sizeof(output[4]));
1193
}
1194

    
1195
static inline void mix_3f_2r_to_mono(AC3DecodeContext *ctx)
1196
{
1197
    int i;
1198
    float (*output)[BLOCK_SIZE] = ctx->output;
1199

    
1200
    for (i = 0; i < 256; i++)
1201
        output[1][i] += (output[2][i] + output[3][i] + output[4][i] + output[5][i]);
1202
    memset(output[2], 0, sizeof(output[2]));
1203
    memset(output[3], 0, sizeof(output[3]));
1204
    memset(output[4], 0, sizeof(output[4]));
1205
    memset(output[5], 0, sizeof(output[5]));
1206
}
1207

    
1208
static inline void mix_3f_2r_to_stereo(AC3DecodeContext *ctx)
1209
{
1210
    int i;
1211
    float (*output)[BLOCK_SIZE] = ctx->output;
1212

    
1213
    for (i = 0; i < 256; i++) {
1214
        output[1][i] += (output[2][i] + output[4][i]);
1215
        output[2][i] += (output[3][i] + output[5][i]);
1216
    }
1217
    memset(output[3], 0, sizeof(output[3]));
1218
    memset(output[4], 0, sizeof(output[4]));
1219
    memset(output[5], 0, sizeof(output[5]));
1220
}
1221

    
1222
static inline void mix_3f_2r_to_dolby(AC3DecodeContext *ctx)
1223
{
1224
    int i;
1225
    float (*output)[BLOCK_SIZE] = ctx->output;
1226

    
1227
    for (i = 0; i < 256; i++) {
1228
        output[1][i] += (output[2][i] - output[4][i] - output[5][i]);
1229
        output[2][i] += (output[3][i] + output[4][i] + output[5][i]);
1230
    }
1231
    memset(output[3], 0, sizeof(output[3]));
1232
    memset(output[4], 0, sizeof(output[4]));
1233
    memset(output[5], 0, sizeof(output[5]));
1234
}
1235
/*********** END DOWNMIX FUNCTIONS ***********/
1236

    
1237
/* Downmix the output.
1238
 * This function downmixes the output when the number of input
1239
 * channels is not equal to the number of output channels requested.
1240
 */
1241
static void do_downmix(AC3DecodeContext *ctx)
1242
{
1243
    int from = ctx->acmod;
1244
    int to = ctx->blkoutput;
1245

    
1246
    if (to == AC3_OUTPUT_UNMODIFIED)
1247
        return;
1248

    
1249
    switch (from) {
1250
        case AC3_ACMOD_DUALMONO:
1251
            switch (to) {
1252
                case AC3_OUTPUT_MONO:
1253
                    mix_dualmono_to_mono(ctx);
1254
                    break;
1255
                case AC3_OUTPUT_STEREO: /* We assume that sum of both mono channels is requested */
1256
                    mix_dualmono_to_stereo(ctx);
1257
                    break;
1258
            }
1259
            break;
1260
        case AC3_ACMOD_MONO:
1261
            switch (to) {
1262
                case AC3_OUTPUT_STEREO:
1263
                    upmix_mono_to_stereo(ctx);
1264
                    break;
1265
            }
1266
            break;
1267
        case AC3_ACMOD_STEREO:
1268
            switch (to) {
1269
                case AC3_OUTPUT_MONO:
1270
                    mix_stereo_to_mono(ctx);
1271
                    break;
1272
            }
1273
            break;
1274
        case AC3_ACMOD_3F:
1275
            switch (to) {
1276
                case AC3_OUTPUT_MONO:
1277
                    mix_3f_to_mono(ctx);
1278
                    break;
1279
                case AC3_OUTPUT_STEREO:
1280
                    mix_3f_to_stereo(ctx);
1281
                    break;
1282
            }
1283
            break;
1284
        case AC3_ACMOD_2F1R:
1285
            switch (to) {
1286
                case AC3_OUTPUT_MONO:
1287
                    mix_2f_1r_to_mono(ctx);
1288
                    break;
1289
                case AC3_OUTPUT_STEREO:
1290
                    mix_2f_1r_to_stereo(ctx);
1291
                    break;
1292
                case AC3_OUTPUT_DOLBY:
1293
                    mix_2f_1r_to_dolby(ctx);
1294
                    break;
1295
            }
1296
            break;
1297
        case AC3_ACMOD_3F1R:
1298
            switch (to) {
1299
                case AC3_OUTPUT_MONO:
1300
                    mix_3f_1r_to_mono(ctx);
1301
                    break;
1302
                case AC3_OUTPUT_STEREO:
1303
                    mix_3f_1r_to_stereo(ctx);
1304
                    break;
1305
                case AC3_OUTPUT_DOLBY:
1306
                    mix_3f_1r_to_dolby(ctx);
1307
                    break;
1308
            }
1309
            break;
1310
        case AC3_ACMOD_2F2R:
1311
            switch (to) {
1312
                case AC3_OUTPUT_MONO:
1313
                    mix_2f_2r_to_mono(ctx);
1314
                    break;
1315
                case AC3_OUTPUT_STEREO:
1316
                    mix_2f_2r_to_stereo(ctx);
1317
                    break;
1318
                case AC3_OUTPUT_DOLBY:
1319
                    mix_2f_2r_to_dolby(ctx);
1320
                    break;
1321
            }
1322
            break;
1323
        case AC3_ACMOD_3F2R:
1324
            switch (to) {
1325
                case AC3_OUTPUT_MONO:
1326
                    mix_3f_2r_to_mono(ctx);
1327
                    break;
1328
                case AC3_OUTPUT_STEREO:
1329
                    mix_3f_2r_to_stereo(ctx);
1330
                    break;
1331
                case AC3_OUTPUT_DOLBY:
1332
                    mix_3f_2r_to_dolby(ctx);
1333
                    break;
1334
            }
1335
            break;
1336
    }
1337
}
1338

    
1339
/* This function performs the imdct on 256 sample transform
1340
 * coefficients.
1341
 */
1342
static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
1343
{
1344
    int i, k;
1345
    float x[128];
1346
    FFTComplex z[2][64];
1347
    float *o_ptr = ctx->tmp_output;
1348

    
1349
    for(i=0; i<2; i++) {
1350
        /* de-interleave coefficients */
1351
        for(k=0; k<128; k++) {
1352
            x[k] = ctx->transform_coeffs[chindex][2*k+i];
1353
        }
1354

    
1355
        /* run standard IMDCT */
1356
        ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, o_ptr, x, ctx->tmp_imdct);
1357

    
1358
        /* reverse the post-rotation & reordering from standard IMDCT */
1359
        for(k=0; k<32; k++) {
1360
            z[i][32+k].re = -o_ptr[128+2*k];
1361
            z[i][32+k].im = -o_ptr[2*k];
1362
            z[i][31-k].re =  o_ptr[2*k+1];
1363
            z[i][31-k].im =  o_ptr[128+2*k+1];
1364
        }
1365
    }
1366

    
1367
    /* apply AC-3 post-rotation & reordering */
1368
    for(k=0; k<64; k++) {
1369
        o_ptr[    2*k  ] = -z[0][   k].im;
1370
        o_ptr[    2*k+1] =  z[0][63-k].re;
1371
        o_ptr[128+2*k  ] = -z[0][   k].re;
1372
        o_ptr[128+2*k+1] =  z[0][63-k].im;
1373
        o_ptr[256+2*k  ] = -z[1][   k].re;
1374
        o_ptr[256+2*k+1] =  z[1][63-k].im;
1375
        o_ptr[384+2*k  ] =  z[1][   k].im;
1376
        o_ptr[384+2*k+1] = -z[1][63-k].re;
1377
    }
1378
}
1379

    
1380
/* IMDCT Transform. */
1381
static inline void do_imdct(AC3DecodeContext *ctx)
1382
{
1383
    int ch;
1384

    
1385
    if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
1386
        ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1387
                                      ctx->transform_coeffs[0], ctx->tmp_imdct);
1388
    }
1389
    for (ch=1; ch<=ctx->nfchans; ch++) {
1390
        if ((ctx->blksw >> (ch-1)) & 1)
1391
            do_imdct_256(ctx, ch);
1392
        else
1393
            ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1394
                                          ctx->transform_coeffs[ch],
1395
                                          ctx->tmp_imdct);
1396

    
1397
        ctx->dsp.vector_fmul_add_add(ctx->output[ch], ctx->tmp_output,
1398
                                     ctx->window, ctx->delay[ch], 384, 256, 1);
1399
        ctx->dsp.vector_fmul_reverse(ctx->delay[ch], ctx->tmp_output+256,
1400
                                     ctx->window, 256);
1401
    }
1402
}
1403

    
1404
/* Parse the audio block from ac3 bitstream.
1405
 * This function extract the audio block from the ac3 bitstream
1406
 * and produces the output for the block. This function must
1407
 * be called for each of the six audio block in the ac3 bitstream.
1408
 */
1409
static int ac3_parse_audio_block(AC3DecodeContext * ctx)
1410
{
1411
    int nfchans = ctx->nfchans;
1412
    int acmod = ctx->acmod;
1413
    int i, bnd, rbnd, seg, grpsize;
1414
    GetBitContext *gb = &ctx->gb;
1415
    int bit_alloc_flags = 0;
1416
    uint8_t *dexps;
1417
    int mstrcplco, cplcoexp, cplcomant;
1418
    int dynrng, chbwcod, ngrps, cplabsexp, skipl;
1419

    
1420
    ctx->blksw = 0;
1421
    for (i = 0; i < nfchans; i++) /*block switch flag */
1422
        ctx->blksw |= get_bits1(gb) << i;
1423

    
1424
    ctx->dithflag = 0;
1425
    for (i = 0; i < nfchans; i++) /* dithering flag */
1426
        ctx->dithflag |= get_bits1(gb) << i;
1427

    
1428
    if (get_bits1(gb)) { /* dynamic range */
1429
        dynrng = get_sbits(gb, 8);
1430
        ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1431
    }
1432

    
1433
    if (acmod == 0x00 && get_bits1(gb)) { /* dynamic range 1+1 mode */
1434
        dynrng = get_sbits(gb, 8);
1435
        ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1436
    }
1437

    
1438
    get_downmix_coeffs(ctx);
1439

    
1440
    if (get_bits1(gb)) { /* coupling strategy */
1441
        ctx->cplinu = get_bits1(gb);
1442
        ctx->cplbndstrc = 0;
1443
        ctx->chincpl = 0;
1444
        if (ctx->cplinu) { /* coupling in use */
1445
            for (i = 0; i < nfchans; i++)
1446
                ctx->chincpl |= get_bits1(gb) << i;
1447

    
1448
            if (acmod == 0x02)
1449
                ctx->phsflginu = get_bits1(gb); //phase flag in use
1450

    
1451
            ctx->cplbegf = get_bits(gb, 4);
1452
            ctx->cplendf = get_bits(gb, 4);
1453

    
1454
            if (3 + ctx->cplendf - ctx->cplbegf < 0) {
1455
                av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", ctx->cplendf, ctx->cplbegf);
1456
                return -1;
1457
            }
1458

    
1459
            ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf;
1460
            ctx->cplstrtmant = ctx->cplbegf * 12 + 37;
1461
            ctx->cplendmant = ctx->cplendf * 12 + 73;
1462
            for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
1463
                if (get_bits1(gb)) {
1464
                    ctx->cplbndstrc |= 1 << i;
1465
                    ctx->ncplbnd--;
1466
                }
1467
        }
1468
    }
1469

    
1470
    if (ctx->cplinu) {
1471
        ctx->cplcoe = 0;
1472

    
1473
        for (i = 0; i < nfchans; i++)
1474
            if ((ctx->chincpl) >> i & 1)
1475
                if (get_bits1(gb)) { /* coupling co-ordinates */
1476
                    ctx->cplcoe |= 1 << i;
1477
                    mstrcplco = 3 * get_bits(gb, 2);
1478
                    for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
1479
                        cplcoexp = get_bits(gb, 4);
1480
                        cplcomant = get_bits(gb, 4);
1481
                        if (cplcoexp == 15)
1482
                            cplcomant <<= 14;
1483
                        else
1484
                            cplcomant = (cplcomant | 0x10) << 13;
1485
                        ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
1486
                    }
1487
                }
1488

    
1489
        if (acmod == 0x02 && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
1490
            for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
1491
                if (get_bits1(gb))
1492
                    ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
1493
    }
1494

    
1495
    if (acmod == 0x02) {/* rematrixing */
1496
        ctx->rematstr = get_bits1(gb);
1497
        if (ctx->rematstr) {
1498
            ctx->rematflg = 0;
1499

    
1500
            if (!(ctx->cplinu) || ctx->cplbegf > 2)
1501
                for (rbnd = 0; rbnd < 4; rbnd++)
1502
                    ctx->rematflg |= get_bits1(gb) << rbnd;
1503
            if (ctx->cplbegf > 0 && ctx->cplbegf <= 2 && ctx->cplinu)
1504
                for (rbnd = 0; rbnd < 3; rbnd++)
1505
                    ctx->rematflg |= get_bits1(gb) << rbnd;
1506
            if (ctx->cplbegf == 0 && ctx->cplinu)
1507
                for (rbnd = 0; rbnd < 2; rbnd++)
1508
                    ctx->rematflg |= get_bits1(gb) << rbnd;
1509
        }
1510
    }
1511

    
1512
    ctx->cplexpstr = EXP_REUSE;
1513
    ctx->lfeexpstr = EXP_REUSE;
1514
    if (ctx->cplinu) /* coupling exponent strategy */
1515
        ctx->cplexpstr = get_bits(gb, 2);
1516
    for (i = 0; i < nfchans; i++)  /* channel exponent strategy */
1517
        ctx->chexpstr[i] = get_bits(gb, 2);
1518
    if (ctx->lfeon)  /* lfe exponent strategy */
1519
        ctx->lfeexpstr = get_bits1(gb);
1520

    
1521
    for (i = 0; i < nfchans; i++) /* channel bandwidth code */
1522
        if (ctx->chexpstr[i] != EXP_REUSE) {
1523
            if ((ctx->chincpl >> i) & 1)
1524
                ctx->endmant[i] = ctx->cplstrtmant;
1525
            else {
1526
                chbwcod = get_bits(gb, 6);
1527
                if (chbwcod > 60) {
1528
                    av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
1529
                    return -1;
1530
                }
1531
                ctx->endmant[i] = chbwcod * 3 + 73;
1532
            }
1533
        }
1534

    
1535
    if (ctx->cplexpstr != EXP_REUSE) {/* coupling exponents */
1536
        bit_alloc_flags = 64;
1537
        cplabsexp = get_bits(gb, 4) << 1;
1538
        ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
1539
        if (decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant)) {
1540
            av_log(NULL, AV_LOG_ERROR, "error decoding coupling exponents\n");
1541
            return -1;
1542
        }
1543
    }
1544

    
1545
    for (i = 0; i < nfchans; i++) /* fbw channel exponents */
1546
        if (ctx->chexpstr[i] != EXP_REUSE) {
1547
            bit_alloc_flags |= 1 << i;
1548
            grpsize = 3 << (ctx->chexpstr[i] - 1);
1549
            ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
1550
            dexps = ctx->dexps[i];
1551
            dexps[0] = get_bits(gb, 4);
1552
            if (decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1)) {
1553
                av_log(NULL, AV_LOG_ERROR, "error decoding channel %d exponents\n", i);
1554
                return -1;
1555
            }
1556
            skip_bits(gb, 2); /* skip gainrng */
1557
        }
1558

    
1559
    if (ctx->lfeexpstr != EXP_REUSE) { /* lfe exponents */
1560
        bit_alloc_flags |= 32;
1561
        ctx->dlfeexps[0] = get_bits(gb, 4);
1562
        if (decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1)) {
1563
            av_log(NULL, AV_LOG_ERROR, "error decoding lfe exponents\n");
1564
            return -1;
1565
        }
1566
    }
1567

    
1568
    if (get_bits1(gb)) { /* bit allocation information */
1569
        bit_alloc_flags = 127;
1570
        ctx->sdcycod = get_bits(gb, 2);
1571
        ctx->fdcycod = get_bits(gb, 2);
1572
        ctx->sgaincod = get_bits(gb, 2);
1573
        ctx->dbpbcod = get_bits(gb, 2);
1574
        ctx->floorcod = get_bits(gb, 3);
1575
    }
1576

    
1577
    if (get_bits1(gb)) { /* snroffset */
1578
        bit_alloc_flags = 127;
1579
        ctx->csnroffst = get_bits(gb, 6);
1580
        if (ctx->cplinu) { /* coupling fine snr offset and fast gain code */
1581
            ctx->cplfsnroffst = get_bits(gb, 4);
1582
            ctx->cplfgaincod = get_bits(gb, 3);
1583
        }
1584
        for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
1585
            ctx->fsnroffst[i] = get_bits(gb, 4);
1586
            ctx->fgaincod[i] = get_bits(gb, 3);
1587
        }
1588
        if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
1589
            ctx->lfefsnroffst = get_bits(gb, 4);
1590
            ctx->lfefgaincod = get_bits(gb, 3);
1591
        }
1592
    }
1593

    
1594
    if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
1595
        bit_alloc_flags |= 64;
1596
        ctx->cplfleak = get_bits(gb, 3);
1597
        ctx->cplsleak = get_bits(gb, 3);
1598
    }
1599

    
1600
    if (get_bits1(gb)) { /* delta bit allocation information */
1601
        bit_alloc_flags = 127;
1602

    
1603
        if (ctx->cplinu) {
1604
            ctx->cpldeltbae = get_bits(gb, 2);
1605
            if (ctx->cpldeltbae == DBA_RESERVED) {
1606
                av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
1607
                return -1;
1608
            }
1609
        }
1610

    
1611
        for (i = 0; i < nfchans; i++) {
1612
            ctx->deltbae[i] = get_bits(gb, 2);
1613
            if (ctx->deltbae[i] == DBA_RESERVED) {
1614
                av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
1615
                return -1;
1616
            }
1617
        }
1618

    
1619
        if (ctx->cplinu)
1620
            if (ctx->cpldeltbae == DBA_NEW) { /*coupling delta offset, len and bit allocation */
1621
                ctx->cpldeltnseg = get_bits(gb, 3);
1622
                for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
1623
                    ctx->cpldeltoffst[seg] = get_bits(gb, 5);
1624
                    ctx->cpldeltlen[seg] = get_bits(gb, 4);
1625
                    ctx->cpldeltba[seg] = get_bits(gb, 3);
1626
                }
1627
            }
1628

    
1629
        for (i = 0; i < nfchans; i++)
1630
            if (ctx->deltbae[i] == DBA_NEW) {/*channel delta offset, len and bit allocation */
1631
                ctx->deltnseg[i] = get_bits(gb, 3);
1632
                for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
1633
                    ctx->deltoffst[i][seg] = get_bits(gb, 5);
1634
                    ctx->deltlen[i][seg] = get_bits(gb, 4);
1635
                    ctx->deltba[i][seg] = get_bits(gb, 3);
1636
                }
1637
            }
1638
    }
1639

    
1640
    if (bit_alloc_flags) {
1641
            /* set bit allocation parameters */
1642
            ctx->bit_alloc_params.fscod = ctx->fscod;
1643
            ctx->bit_alloc_params.halfratecod = 0;
1644
            ctx->bit_alloc_params.sdecay = ff_sdecaytab[ctx->sdcycod];
1645
            ctx->bit_alloc_params.fdecay = ff_fdecaytab[ctx->fdcycod];
1646
            ctx->bit_alloc_params.sgain = ff_sgaintab[ctx->sgaincod];
1647
            ctx->bit_alloc_params.dbknee = ff_dbkneetab[ctx->dbpbcod];
1648
            ctx->bit_alloc_params.floor = ff_floortab[ctx->floorcod];
1649
            ctx->bit_alloc_params.cplfleak = ctx->cplfleak;
1650
            ctx->bit_alloc_params.cplsleak = ctx->cplsleak;
1651

    
1652
            if (ctx->chincpl && (bit_alloc_flags & 64))
1653
                do_bit_allocation(ctx, 5);
1654
            for (i = 0; i < nfchans; i++)
1655
                if ((bit_alloc_flags >> i) & 1)
1656
                    do_bit_allocation(ctx, i);
1657
            if (ctx->lfeon && (bit_alloc_flags & 32))
1658
                do_bit_allocation(ctx, 6);
1659
    }
1660

    
1661
    if (get_bits1(gb)) { /* unused dummy data */
1662
        skipl = get_bits(gb, 9);
1663
        while(skipl--)
1664
            skip_bits(gb, 8);
1665
    }
1666
    /* unpack the transform coefficients
1667
     * * this also uncouples channels if coupling is in use.
1668
     */
1669
    if (get_transform_coeffs(ctx)) {
1670
        av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1671
        return -1;
1672
    }
1673

    
1674
    /* recover coefficients if rematrixing is in use */
1675
    if (ctx->rematflg)
1676
        do_rematrixing(ctx);
1677

    
1678
    do_downmix(ctx);
1679

    
1680
    do_imdct(ctx);
1681

    
1682
    return 0;
1683
}
1684

    
1685
static inline int16_t convert(int32_t i)
1686
{
1687
    if (i > 0x43c07fff)
1688
        return 32767;
1689
    else if (i <= 0x43bf8000)
1690
        return -32768;
1691
    else
1692
        return (i - 0x43c00000);
1693
}
1694

    
1695
/* Decode ac3 frame.
1696
 *
1697
 * @param avctx Pointer to AVCodecContext
1698
 * @param data Pointer to pcm smaples
1699
 * @param data_size Set to number of pcm samples produced by decoding
1700
 * @param buf Data to be decoded
1701
 * @param buf_size Size of the buffer
1702
 */
1703
static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1704
{
1705
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1706
    int frame_start;
1707
    int16_t *out_samples = (int16_t *)data;
1708
    int i, j, k, start;
1709
    int32_t *int_ptr[6];
1710

    
1711
    for (i = 0; i < 6; i++)
1712
        int_ptr[i] = (int32_t *)(&ctx->output[i]);
1713

    
1714
    //Synchronize the frame.
1715
    frame_start = ac3_synchronize(buf, buf_size);
1716
    if (frame_start == -1) {
1717
        av_log(avctx, AV_LOG_ERROR, "frame is not synchronized\n");
1718
        *data_size = 0;
1719
        return buf_size;
1720
    }
1721

    
1722
    //Initialize the GetBitContext with the start of valid AC3 Frame.
1723
    init_get_bits(&(ctx->gb), buf + frame_start, (buf_size - frame_start) * 8);
1724

    
1725
    //Parse the syncinfo.
1726
    //If 'fscod' or 'bsid' is not valid the decoder shall mute as per the standard.
1727
    if (!ac3_parse_sync_info(ctx)) {
1728
        av_log(avctx, AV_LOG_ERROR, "\n");
1729
        *data_size = 0;
1730
        return buf_size;
1731
    }
1732

    
1733
    //Parse the BSI.
1734
    //If 'bsid' is not valid decoder shall not decode the audio as per the standard.
1735
    ac3_parse_bsi(ctx);
1736

    
1737
    avctx->sample_rate = ctx->sampling_rate;
1738
    avctx->bit_rate = ctx->bit_rate;
1739

    
1740
    if (avctx->channels == 0) {
1741
        ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1742
        if (ctx->lfeon)
1743
            ctx->blkoutput |= AC3_OUTPUT_LFEON;
1744
        avctx->channels = ctx->nfchans + ctx->lfeon;
1745
    }
1746
    else if (avctx->channels == 1)
1747
        ctx->blkoutput |= AC3_OUTPUT_MONO;
1748
    else if (avctx->channels == 2) {
1749
        if (ctx->dsurmod == 0x02)
1750
            ctx->blkoutput |= AC3_OUTPUT_DOLBY;
1751
        else
1752
            ctx->blkoutput |= AC3_OUTPUT_STEREO;
1753
    }
1754
    else {
1755
        if (avctx->channels < (ctx->nfchans + ctx->lfeon))
1756
            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);
1757
        ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1758
        if (ctx->lfeon)
1759
            ctx->blkoutput |= AC3_OUTPUT_LFEON;
1760
        avctx->channels = ctx->nfchans + ctx->lfeon;
1761
    }
1762

    
1763
    //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);
1764

    
1765
    //Parse the Audio Blocks.
1766
    for (i = 0; i < NB_BLOCKS; i++) {
1767
        if (ac3_parse_audio_block(ctx)) {
1768
            av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1769
            *data_size = 0;
1770
            return ctx->frame_size;
1771
        }
1772
        start = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1;
1773
        for (k = 0; k < BLOCK_SIZE; k++)
1774
            for (j = start; j <= avctx->channels; j++)
1775
                *(out_samples++) = convert(int_ptr[j][k]);
1776
    }
1777
    *data_size = NB_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);
1778
    return ctx->frame_size;
1779
}
1780

    
1781
/* Uninitialize ac3 decoder.
1782
 */
1783
static int ac3_decode_end(AVCodecContext *avctx)
1784
{
1785
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1786
    ff_mdct_end(&ctx->imdct_512);
1787
    ff_mdct_end(&ctx->imdct_256);
1788

    
1789
    return 0;
1790
}
1791

    
1792
AVCodec ac3_decoder = {
1793
    .name = "ac3",
1794
    .type = CODEC_TYPE_AUDIO,
1795
    .id = CODEC_ID_AC3,
1796
    .priv_data_size = sizeof (AC3DecodeContext),
1797
    .init = ac3_decode_init,
1798
    .close = ac3_decode_end,
1799
    .decode = ac3_decode_frame,
1800
};
1801