Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3dec.c @ 262d8702

History | View | Annotate | Download (60.9 KB)

1
/* AC3 Audio Decoder.
2
 *
3
 * Copyright (c) 2006 Kartikey Mahendra BHATT (bhattkm at gmail dot com).
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
 */
19

    
20
#include <stdio.h>
21
#include <stddef.h>
22
#include <math.h>
23
#include <inttypes.h>
24
#include <string.h>
25

    
26
#define ALT_BITSTREAM_READER
27

    
28
#include "ac3tab.h"
29
#include "ac3.h"
30
#include "ac3_decoder.h"
31
#include "avcodec.h"
32
#include "bitstream.h"
33
#include "dsputil.h"
34
#include "avutil.h"
35
#include "common.h"
36
#include "math.h"
37
#include "crc.h"
38

    
39
#define N 512   /* constant for IMDCT Block size */
40

    
41
#define MAX_CHANNELS    6
42
#define BLOCK_SIZE    256
43
#define AUDIO_BLOCKS    6
44

    
45
/* Exponent strategies. */
46
#define AC3_EXPSTR_D15      0x01
47
#define AC3_EXPSTR_D25      0x02
48
#define AC3_EXPSTR_D45      0x03
49
#define AC3_EXPSTR_REUSE    0x00
50

    
51
/* Bit allocation strategies. */
52
#define AC3_DBASTR_NEW      0x01
53
#define AC3_DBASTR_NONE     0x02
54
#define AC3_DBASTR_RESERVED 0x03
55
#define AC3_DBASTR_REUSE    0x00
56

    
57
/* Output and input configurations. */
58
#define AC3_OUTPUT_UNMODIFIED   0x01
59
#define AC3_OUTPUT_MONO         0x02
60
#define AC3_OUTPUT_STEREO       0x04
61
#define AC3_OUTPUT_DOLBY        0x08
62
#define AC3_OUTPUT_LFEON        0x10
63

    
64
#define AC3_INPUT_DUALMONO      0x00
65
#define AC3_INPUT_MONO          0x01
66
#define AC3_INPUT_STEREO        0x02
67
#define AC3_INPUT_3F            0x03
68
#define AC3_INPUT_2F_1R         0x04
69
#define AC3_INPUT_3F_1R         0x05
70
#define AC3_INPUT_2F_2R         0x06
71
#define AC3_INPUT_3F_2R         0x07
72

    
73
/* Mersenne Twister */
74
#define NMT 624
75
#define MMT 397
76
#define MATRIX_A    0x9908b0df
77
#define UPPER_MASK  0x80000000
78
#define LOWER_MASK  0x7fffffff
79

    
80

    
81
typedef struct {
82
    uint32_t mt[NMT];
83
    int      mti;
84
} dither_state;
85
/* Mersenne Twister */
86

    
87
typedef struct {
88
    uint32_t flags;
89
    uint16_t crc1;
90
    uint8_t  fscod;
91

    
92
    uint8_t  acmod;
93
    uint8_t  cmixlev;
94
    uint8_t  surmixlev;
95
    uint8_t  dsurmod;
96

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

    
136
    /* Derived Attributes. */
137
    int      sampling_rate;
138
    int      bit_rate;
139
    int      frame_size;
140

    
141
    int      nfchans;
142
    int      lfeon;
143

    
144
    float    chcoeffs[6];
145
    float    cplco[5][18];
146
    int      ncplbnd;
147
    int      ncplsubnd;
148
    int      cplstrtmant;
149
    int      cplendmant;
150
    int      endmant[5];
151
    uint8_t  dcplexps[256];
152
    uint8_t  dexps[5][256];
153
    uint8_t  dlfeexps[256];
154
    uint8_t  cplbap[256];
155
    uint8_t  bap[5][256];
156
    uint8_t  lfebap[256];
157
    int      blkoutput;
158

    
159
    DECLARE_ALIGNED_16(float, transform_coeffs[MAX_CHANNELS][BLOCK_SIZE]);
160

    
161
    /* For IMDCT. */
162
    MDCTContext imdct_512;  //N/8 point IFFT context
163
    MDCTContext imdct_256;  //N/4 point IFFT context
164
    DECLARE_ALIGNED_16(float, output[MAX_CHANNELS][BLOCK_SIZE]);
165
    DECLARE_ALIGNED_16(float, delay[MAX_CHANNELS][BLOCK_SIZE]);
166
    DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]);
167
    DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]);
168

    
169
    /* Miscellaneous. */
170
    GetBitContext gb;
171
    dither_state  dith_state;
172
} AC3DecodeContext;
173

    
174

    
175
/* BEGIN Mersenne Twister Code. */
176
static void dither_seed(dither_state *state, uint32_t seed)
177
{
178
    if (seed == 0)
179
        seed = 0x1f2e3d4c;
180

    
181
    state->mt[0] = seed;
182
    for (state->mti = 1; state->mti < NMT; state->mti++)
183
        state->mt[state->mti] = ((69069 * state->mt[state->mti - 1]) + 1);
184
}
185

    
186
static uint32_t dither_uint32(dither_state *state)
187
{
188
    uint32_t y;
189
    static const uint32_t mag01[2] = { 0x00, MATRIX_A };
190
    int kk;
191

    
192
    if (state->mti >= NMT) {
193
        for (kk = 0; kk < NMT - MMT; kk++) {
194
            y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
195
            state->mt[kk] = state->mt[kk + MMT] ^ (y >> 1) ^ mag01[y & 0x01];
196
        }
197
        for (;kk < NMT - 1; kk++) {
198
            y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
199
            state->mt[kk] = state->mt[kk + (MMT - NMT)] ^ (y >> 1) ^ mag01[y & 0x01];
200
        }
201
        y = (state->mt[NMT - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK);
202
        state->mt[NMT - 1] = state->mt[MMT - 1] ^ (y >> 1) ^ mag01[y & 0x01];
203

    
204
        state->mti = 0;
205
    }
206

    
207
    y = state->mt[state->mti++];
208
    y ^= (y >> 11);
209
    y ^= ((y << 7) & 0x9d2c5680);
210
    y ^= ((y << 15) & 0xefc60000);
211
    y ^= (y >> 18);
212

    
213
    return y;
214
}
215

    
216
static inline int16_t dither_int16(dither_state *state)
217
{
218
    return ((dither_uint32(state) << 16) >> 16);
219
}
220

    
221
/* END Mersenne Twister */
222

    
223
/**
224
 * Generate a Kaiser Window.
225
 */
226
static void
227
k_window_init(int alpha, float *window, int n, int iter)
228
{
229
    int j, k;
230
    float a, x;
231
    a = alpha * M_PI / n;
232
    a = a*a;
233
    for(k=0; k<n; k++) {
234
        x = k * (n - k) * a;
235
        window[k] = 1.0;
236
        for(j=iter; j>0; j--) {
237
            window[k] = (window[k] * x / (j*j)) + 1.0;
238
        }
239
    }
240
}
241

    
242
/**
243
 * Generate a Kaiser-Bessel Derived Window.
244
 * @param alpha  determines window shape
245
 * @param window array to fill with window values
246
 * @param n      length of the window
247
 * @param iter   number of iterations to use in BesselI0
248
 */
249
static void
250
kbd_window_init(int alpha, float *window, int n, int iter)
251
{
252
    int k, n2;
253
    float *kwindow;
254

    
255
    n2 = n >> 1;
256
    kwindow = &window[n2];
257
    k_window_init(alpha, kwindow, n2, iter);
258
    window[0] = kwindow[0];
259
    for(k=1; k<n2; k++) {
260
        window[k] = window[k-1] + kwindow[k];
261
    }
262
    for(k=0; k<n2; k++) {
263
        window[k] = sqrt(window[k] / (window[n2-1]+1));
264
        window[n-1-k] = window[k];
265
    }
266
}
267

    
268
static void generate_quantizers_table(int16_t quantizers[], int level, int length)
269
{
270
    int i;
271

    
272
    for (i = 0; i < length; i++)
273
        quantizers[i] = ((2 * i - level + 1) << 15) / level;
274
}
275

    
276
static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
277
{
278
    int i, j;
279
    int16_t v;
280

    
281
    for (i = 0; i < length1; i++) {
282
        v = ((2 * i - level + 1) << 15) / level;
283
        for (j = 0; j < length2; j++)
284
            quantizers[i * length2 + j] = v;
285
    }
286

    
287
    for (i = length1 * length2; i < size; i++)
288
        quantizers[i] = 0;
289
}
290

    
291
static void generate_quantizers_table_2(int16_t quantizers[], int level, int length1, int length2, int size)
292
{
293
    int i, j;
294
    int16_t v;
295

    
296
    for (i = 0; i < length1; i++) {
297
        v = ((2 * (i % level) - level + 1) << 15) / level;
298
        for (j = 0; j < length2; j++)
299
            quantizers[i * length2 + j] = v;
300
    }
301

    
302
    for (i = length1 * length2; i < size; i++)
303
        quantizers[i] = 0;
304

    
305
}
306

    
307
static void generate_quantizers_table_3(int16_t quantizers[], int level, int length1, int length2, int size)
308
{
309
    int i, j;
310

    
311
    for (i = 0; i < length1; i++)
312
        for (j = 0; j < length2; j++)
313
            quantizers[i * length2 + j] = ((2 * (j % level) - level + 1) << 15) / level;
314

    
315
    for (i = length1 * length2; i < size; i++)
316
        quantizers[i] = 0;
317
}
318

    
319
static void ac3_tables_init(void)
320
{
321
    int i, j, k, l, v;
322
    /* compute bndtab and masktab from bandsz */
323
    k = 0;
324
    l = 0;
325
    for(i=0;i<50;i++) {
326
        bndtab[i] = l;
327
        v = bndsz[i];
328
        for(j=0;j<v;j++) masktab[k++]=i;
329
        l += v;
330
    }
331
    masktab[253] = masktab[254] = masktab[255] = 0;
332
    bndtab[50] = 0;
333

    
334
    /* Exponent Decoding Tables */
335
    for (i = 0; i < 5; i++) {
336
        v = i - 2;
337
        for (j = 0; j < 25; j++)
338
            exp_1[i * 25 + j] = v;
339
    }
340

    
341
    for (i = 0; i < 25; i++) {
342
        v = (i % 5) - 2;
343
        for (j = 0; j < 5; j++)
344
            exp_2[i * 5 + j] = v;
345
    }
346

    
347
    for (i = 0; i < 25; i++) {
348
        v = -2;
349
        for (j = 0; j < 5; j++)
350
            exp_3[i * 5 + j] = v++;
351
    }
352

    
353
    for (i = 125; i < 128; i++)
354
        exp_1[i] = exp_2[i] = exp_3[i] = 25;
355
    /* End Exponent Decoding Tables */
356

    
357
    /* Quantizer ungrouping tables. */
358
    // for level-3 quantizers
359
    generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
360
    generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
361
    generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
362

    
363
    //for level-5 quantizers
364
    generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
365
    generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
366
    generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
367

    
368
    //for level-7 quantizers
369
    generate_quantizers_table(l7_quantizers, 7, 7);
370

    
371
    //for level-4 quantizers
372
    generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
373
    generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
374

    
375
    //for level-15 quantizers
376
    generate_quantizers_table(l15_quantizers, 15, 15);
377

    
378
    /* Kaiser-Bessel derived window. */
379
    kbd_window_init(5, window, 256, 100);
380
}
381

    
382

    
383
static int ac3_decode_init(AVCodecContext *avctx)
384
{
385
    AC3DecodeContext *ctx = avctx->priv_data;
386

    
387
    ac3_tables_init();
388
    ff_mdct_init(&ctx->imdct_256, 8, 1);
389
    ff_mdct_init(&ctx->imdct_512, 9, 1);
390
    dither_seed(&ctx->dith_state, 0);
391

    
392
    return 0;
393
}
394

    
395
static int ac3_synchronize(uint8_t *buf, int buf_size)
396
{
397
    int i;
398

    
399
    for (i = 0; i < buf_size - 1; i++)
400
        if (buf[i] == 0x0b && buf[i + 1] == 0x77)
401
            return i;
402

    
403
    return -1;
404
}
405

    
406
//Returns -1 when 'fscod' is not valid;
407
static int ac3_parse_sync_info(AC3DecodeContext *ctx)
408
{
409
    GetBitContext *gb = &ctx->gb;
410
    int frmsizecod, bsid;
411

    
412
    skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
413
    ctx->crc1 = get_bits(gb, 16);
414
    ctx->fscod = get_bits(gb, 2);
415
    if (ctx->fscod == 0x03)
416
        return 0;
417
    frmsizecod = get_bits(gb, 6);
418
    if (frmsizecod >= 38)
419
        return 0;
420
    ctx->sampling_rate = ac3_freqs[ctx->fscod];
421
    ctx->bit_rate = ac3_bitratetab[frmsizecod >> 1];
422

    
423
    /* we include it here in order to determine validity of ac3 frame */
424
    bsid = get_bits(gb, 5);
425
    if (bsid > 0x08)
426
        return 0;
427
    skip_bits(gb, 3); //skip the bsmod, bsi->bsmod = get_bits(gb, 3);
428

    
429
    switch (ctx->fscod) {
430
        case 0x00:
431
            ctx->frame_size = 4 * ctx->bit_rate;
432
            return ctx->frame_size;
433
        case 0x01:
434
            ctx->frame_size = 2 * (320 * ctx->bit_rate / 147 + (frmsizecod & 1));
435
            return ctx->frame_size;
436
        case 0x02:
437
            ctx->frame_size =  6 * ctx->bit_rate;
438
            return ctx->frame_size;
439
    }
440

    
441
    /* never reached */
442
    return 0;
443
}
444

    
445
static void ac3_parse_bsi(AC3DecodeContext *ctx)
446
{
447
    GetBitContext *gb = &ctx->gb;
448
    int i;
449

    
450
    ctx->cmixlev = 0;
451
    ctx->surmixlev = 0;
452
    ctx->dsurmod = 0;
453
    ctx->nfchans = 0;
454
    ctx->cpldeltbae = AC3_DBASTR_NONE;
455
    ctx->cpldeltnseg = 0;
456
    for (i = 0; i < 5; i++) {
457
        ctx->deltbae[i] = AC3_DBASTR_NONE;
458
        ctx->deltnseg[i] = 0;
459
    }
460

    
461
    ctx->acmod = get_bits(gb, 3);
462
    ctx->nfchans = nfchans_tbl[ctx->acmod];
463

    
464
    if (ctx->acmod & 0x01 && ctx->acmod != 0x01)
465
        ctx->cmixlev = get_bits(gb, 2);
466
    if (ctx->acmod & 0x04)
467
        ctx->surmixlev = get_bits(gb, 2);
468
    if (ctx->acmod == 0x02)
469
        ctx->dsurmod = get_bits(gb, 2);
470

    
471
    ctx->lfeon = get_bits1(gb);
472

    
473
    i = !(ctx->acmod);
474
    do {
475
        skip_bits(gb, 5); //skip dialog normalization
476
        if (get_bits1(gb))
477
            skip_bits(gb, 8); //skip compression
478
        if (get_bits1(gb))
479
            skip_bits(gb, 8); //skip language code
480
        if (get_bits1(gb))
481
            skip_bits(gb, 7); //skip audio production information
482
    } while (i--);
483

    
484
    skip_bits(gb, 2); //skip copyright bit and original bitstream bit
485

    
486
    if (get_bits1(gb))
487
        skip_bits(gb, 14); //skip timecode1
488
    if (get_bits1(gb))
489
        skip_bits(gb, 14); //skip timecode2
490

    
491
    if (get_bits1(gb)) {
492
        i = get_bits(gb, 6); //additional bsi length
493
        do {
494
            skip_bits(gb, 8);
495
        } while(i--);
496
    }
497
}
498

    
499
/* Decodes the grouped exponents and stores them
500
 * in decoded exponents (dexps).
501
 * The code is derived from liba52.
502
 * Uses liba52 tables.
503
 */
504
static int decode_exponents(GetBitContext *gb, int expstr, int ngrps, uint8_t absexp, uint8_t *dexps)
505
{
506
    int exps;
507

    
508
    while (ngrps--) {
509
        exps = get_bits(gb, 7);
510

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

    
526
        absexp += exp_2[exps];
527
        if (absexp > 24) {
528
            av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
529
            return -ngrps;
530
        }
531
        switch (expstr) {
532
            case AC3_EXPSTR_D45:
533
                *(dexps++) = absexp;
534
                *(dexps++) = absexp;
535
            case AC3_EXPSTR_D25:
536
                *(dexps++) = absexp;
537
            case AC3_EXPSTR_D15:
538
                *(dexps++) = absexp;
539
        }
540

    
541
        absexp += exp_3[exps];
542
        if (absexp > 24) {
543
            av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
544
            return -ngrps;
545
        }
546
        switch (expstr) {
547
            case AC3_EXPSTR_D45:
548
                *(dexps++) = absexp;
549
                *(dexps++) = absexp;
550
            case AC3_EXPSTR_D25:
551
                *(dexps++) = absexp;
552
            case AC3_EXPSTR_D15:
553
                *(dexps++) = absexp;
554
        }
555
    }
556

    
557
    return 0;
558
}
559

    
560
static inline int logadd(int a, int b)
561
{
562
    int c = a - b;
563
    int address;
564

    
565
    address = FFMIN((ABS(c) >> 1), 255);
566

    
567
    if (c >= 0)
568
        return (a + latab[address]);
569
    else
570
        return (b + latab[address]);
571
}
572

    
573
static inline int calc_lowcomp(int a, int b0, int b1, int bin)
574
{
575
    if (bin < 7) {
576
        if ((b0 + 256) == b1)
577
            a = 384;
578
        else if (b0 > b1)
579
            a = FFMAX(0, (a - 64));
580
    }
581
    else if (bin < 20) {
582
        if ((b0 + 256) == b1)
583
            a = 320;
584
        else if (b0 > b1)
585
            a = FFMAX(0, (a - 64));
586
    }
587
    else
588
        a = FFMAX(0, (a - 128));
589

    
590
    return a;
591
}
592

    
593
/* do the bit allocation for chnl.
594
 * chnl = 0 to 4 - fbw channel
595
 * chnl = 5 coupling channel
596
 * chnl = 6 lfe channel
597
 */
598
static void do_bit_allocation(AC3DecodeContext *ctx, int chnl)
599
{
600
    int16_t psd[256], bndpsd[50], excite[50], mask[50], delta;
601
    int sdecay, fdecay, sgain, dbknee, floor;
602
    int lowcomp = 0, fgain = 0, snroffset = 0, fastleak = 0, slowleak = 0, do_delta = 0;
603
    int start = 0, end = 0, bin = 0, i = 0, j = 0, k = 0, lastbin = 0, bndstrt = 0;
604
    int bndend = 0, begin = 0, deltnseg = 0, band = 0, seg = 0, address = 0;
605
    int fscod = ctx->fscod;
606
    uint8_t *deltoffst = 0, *deltlen = 0, *deltba = 0;
607
    uint8_t *exps = 0, *bap = 0;
608

    
609
    /* initialization */
610
    sdecay = sdecaytab[ctx->sdcycod];
611
    fdecay = fdecaytab[ctx->fdcycod];
612
    sgain = sgaintab[ctx->sgaincod];
613
    dbknee = dbkneetab[ctx->dbpbcod];
614
    floor = floortab[ctx->floorcod];
615

    
616
    if (chnl == 5) {
617
        start = ctx->cplstrtmant;
618
        end = ctx->cplendmant;
619
        fgain = fgaintab[ctx->cplfgaincod];
620
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->cplfsnroffst) << 2;
621
        fastleak = (ctx->cplfleak << 8) + 768;
622
        slowleak = (ctx->cplsleak << 8) + 768;
623
        exps = ctx->dcplexps;
624
        bap = ctx->cplbap;
625
        if (ctx->cpldeltbae == AC3_DBASTR_NEW || ctx->deltbae == AC3_DBASTR_REUSE) {
626
            do_delta = 1;
627
            deltnseg = ctx->cpldeltnseg;
628
            deltoffst = ctx->cpldeltoffst;
629
            deltlen = ctx->cpldeltlen;
630
            deltba = ctx->cpldeltba;
631
        }
632
    }
633
    else if (chnl == 6) {
634
        start = 0;
635
        end = 7;
636
        lowcomp = 0;
637
        fastleak = 0;
638
        slowleak = 0;
639
        fgain = fgaintab[ctx->lfefgaincod];
640
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->lfefsnroffst) << 2;
641
        exps = ctx->dlfeexps;
642
        bap = ctx->lfebap;
643
    }
644
    else {
645
        start = 0;
646
        end = ctx->endmant[chnl];
647
        lowcomp = 0;
648
        fastleak = 0;
649
        slowleak = 0;
650
        fgain = fgaintab[ctx->fgaincod[chnl]];
651
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->fsnroffst[chnl]) << 2;
652
        exps = ctx->dexps[chnl];
653
        bap = ctx->bap[chnl];
654
        if (ctx->deltbae[chnl] == AC3_DBASTR_NEW || ctx->deltbae[chnl] == AC3_DBASTR_REUSE) {
655
            do_delta = 1;
656
            deltnseg = ctx->deltnseg[chnl];
657
            deltoffst = ctx->deltoffst[chnl];
658
            deltlen = ctx->deltlen[chnl];
659
            deltba = ctx->deltba[chnl];
660
        }
661
    }
662

    
663
    for (bin = start; bin < end; bin++) /* exponent mapping into psd */
664
        psd[bin] = (3072 - (exps[bin] << 7));
665

    
666
    /* psd integration */
667
    j = start;
668
    k = masktab[start];
669
    do {
670
        lastbin = FFMIN((bndtab[k] + bndsz[k]), end);
671
        bndpsd[k] = psd[j];
672
        j++;
673
        for (i = j; i < lastbin; i++) {
674
            bndpsd[k] = logadd(bndpsd[k], psd[j]);
675
            j++;
676
        }
677
        k++;
678
    } while (end > lastbin);
679

    
680
    /* compute the excite function */
681
    bndstrt = masktab[start];
682
    bndend = masktab[end - 1] + 1;
683
    if (bndstrt == 0) {
684
        lowcomp = calc_lowcomp(lowcomp, bndpsd[0], bndpsd[1], 0);
685
        excite[0] = bndpsd[0] - fgain - lowcomp;
686
        lowcomp = calc_lowcomp(lowcomp, bndpsd[1], bndpsd[2], 1);
687
        excite[1] = bndpsd[1] - fgain - lowcomp;
688
        begin = 7;
689
        for (bin = 2; bin < 7; bin++) {
690
            if ((bndend != 7) || (bin != 6))
691
                lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
692
            fastleak = bndpsd[bin] - fgain;
693
            slowleak = bndpsd[bin] - sgain;
694
            excite[bin] = fastleak - lowcomp;
695
            if ((bndend != 7) || (bin != 6))
696
                if (bndpsd[bin] <= bndpsd[bin + 1]) {
697
                    begin = bin + 1;
698
                    break;
699
                }
700
        }
701
        for (bin = begin; bin < FFMIN(bndend, 22); bin++) {
702
            if ((bndend != 7) || (bin != 6))
703
                lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
704
            fastleak -= fdecay;
705
            fastleak = FFMAX(fastleak, (bndpsd[bin] - fgain));
706
            slowleak -= sdecay;
707
            slowleak = FFMAX(slowleak, (bndpsd[bin] - sgain));
708
            excite[bin] = FFMAX((fastleak - lowcomp), slowleak);
709
        }
710
        begin = 22;
711
    }
712
    else {
713
        begin = bndstrt;
714
    }
715
    for (bin = begin; bin < bndend; bin++) {
716
        fastleak -= fdecay;
717
        fastleak = FFMAX(fastleak, (bndpsd[bin] - fgain));
718
        slowleak -= sdecay;
719
        slowleak = FFMAX(slowleak, (bndpsd[bin] - sgain));
720
        excite[bin] = FFMAX(fastleak, slowleak);
721
    }
722

    
723
    /* compute the masking curve */
724
    for (bin = bndstrt; bin < bndend; bin++) {
725
        if (bndpsd[bin] < dbknee)
726
            excite[bin] += ((dbknee - bndpsd[bin]) >> 2);
727
        mask[bin] = FFMAX(excite[bin], hth[bin][fscod]);
728
    }
729

    
730
    /* apply the delta bit allocation */
731
    if (do_delta) {
732
        band = 0;
733
        for (seg = 0; seg < deltnseg + 1; seg++) {
734
            band += deltoffst[seg];
735
            if (deltba[seg] >= 4)
736
                delta = (deltba[seg] - 3) << 7;
737
            else
738
                delta = (deltba[seg] - 4) << 7;
739
            for (k = 0; k < deltlen[seg]; k++) {
740
                mask[band] += delta;
741
                band++;
742
            }
743
        }
744
    }
745

    
746
    /*compute the bit allocation */
747
    i = start;
748
    j = masktab[start];
749
    do {
750
        lastbin = FFMIN((bndtab[j] + bndsz[j]), end);
751
        mask[j] -= snroffset;
752
        mask[j] -= floor;
753
        if (mask[j] < 0)
754
            mask[j] = 0;
755
        mask[j] &= 0x1fe0;
756
        mask[j] += floor;
757
        for (k = i; k < lastbin; k++) {
758
            address = (psd[i] - mask[j]) >> 5;
759
            address = FFMIN(63, (FFMAX(0, address)));
760
            bap[i] = baptab[address];
761
            i++;
762
        }
763
        j++;
764
    } while (end > lastbin);
765
}
766

    
767
/* Check if snroffsets are zero. */
768
static int is_snr_offsets_zero(AC3DecodeContext *ctx)
769
{
770
    int i;
771

    
772
    if ((ctx->csnroffst) || (ctx->cplinu && ctx->cplfsnroffst) ||
773
            (ctx->lfeon && ctx->lfefsnroffst))
774
        return 0;
775

    
776
    for (i = 0; i < ctx->nfchans; i++)
777
        if (ctx->fsnroffst[i])
778
            return 0;
779

    
780
    return 1;
781
}
782

    
783
typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
784
    int16_t l3_quantizers[3];
785
    int16_t l5_quantizers[3];
786
    int16_t l11_quantizers[2];
787
    int l3ptr;
788
    int l5ptr;
789
    int l11ptr;
790
} mant_groups;
791

    
792
#define TRANSFORM_COEFF(tc, m, e, f) (tc) = (m) * (f)[(e)]
793

    
794
/* Get the transform coefficients for coupling channel and uncouple channels.
795
 * The coupling transform coefficients starts at the the cplstrtmant, which is
796
 * equal to endmant[ch] for fbw channels. Hence we can uncouple channels before
797
 * getting transform coefficients for the channel.
798
 */
799
static int get_transform_coeffs_cpling(AC3DecodeContext *ctx, mant_groups *m)
800
{
801
    GetBitContext *gb = &ctx->gb;
802
    int ch, start, end, cplbndstrc, bnd, gcode, tbap;
803
    float cplcos[5], cplcoeff;
804
    uint8_t *exps = ctx->dcplexps;
805
    uint8_t *bap = ctx->cplbap;
806

    
807
    cplbndstrc = ctx->cplbndstrc;
808
    start = ctx->cplstrtmant;
809
    bnd = 0;
810

    
811
    while (start < ctx->cplendmant) {
812
        end = start + 12;
813
        while (cplbndstrc & 1) {
814
            end += 12;
815
            cplbndstrc >>= 1;
816
        }
817
        cplbndstrc >>= 1;
818
        for (ch = 0; ch < ctx->nfchans; ch++)
819
            cplcos[ch] = ctx->chcoeffs[ch] * ctx->cplco[ch][bnd];
820
        bnd++;
821

    
822
        while (start < end) {
823
            tbap = bap[start];
824
            switch(tbap) {
825
                case 0:
826
                    for (ch = 0; ch < ctx->nfchans; ch++)
827
                        if (((ctx->chincpl) >> ch) & 1) {
828
                            if ((ctx->dithflag >> ch) & 1) {
829
                                TRANSFORM_COEFF(cplcoeff, dither_int16(&ctx->dith_state), exps[start], scale_factors);
830
                                ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch] * LEVEL_MINUS_3DB;
831
                            } else
832
                                ctx->transform_coeffs[ch + 1][start] = 0;
833
                        }
834
                    start++;
835
                    continue;
836
                case 1:
837
                    if (m->l3ptr > 2) {
838
                        gcode = get_bits(gb, 5);
839
                        m->l3_quantizers[0] = l3_quantizers_1[gcode];
840
                        m->l3_quantizers[1] = l3_quantizers_2[gcode];
841
                        m->l3_quantizers[2] = l3_quantizers_3[gcode];
842
                        m->l3ptr = 0;
843
                    }
844
                    TRANSFORM_COEFF(cplcoeff, m->l3_quantizers[m->l3ptr++], exps[start], scale_factors);
845
                    break;
846

    
847
                case 2:
848
                    if (m->l5ptr > 2) {
849
                        gcode = get_bits(gb, 7);
850
                        m->l5_quantizers[0] = l5_quantizers_1[gcode];
851
                        m->l5_quantizers[1] = l5_quantizers_2[gcode];
852
                        m->l5_quantizers[2] = l5_quantizers_3[gcode];
853
                        m->l5ptr = 0;
854
                    }
855
                    TRANSFORM_COEFF(cplcoeff, m->l5_quantizers[m->l5ptr++], exps[start], scale_factors);
856
                    break;
857

    
858
                case 3:
859
                    TRANSFORM_COEFF(cplcoeff, l7_quantizers[get_bits(gb, 3)], exps[start], scale_factors);
860
                    break;
861

    
862
                case 4:
863
                    if (m->l11ptr > 1) {
864
                        gcode = get_bits(gb, 7);
865
                        m->l11_quantizers[0] = l11_quantizers_1[gcode];
866
                        m->l11_quantizers[1] = l11_quantizers_2[gcode];
867
                        m->l11ptr = 0;
868
                    }
869
                    TRANSFORM_COEFF(cplcoeff, m->l11_quantizers[m->l11ptr++], exps[start], scale_factors);
870
                    break;
871

    
872
                case 5:
873
                    TRANSFORM_COEFF(cplcoeff, l15_quantizers[get_bits(gb, 4)], exps[start], scale_factors);
874
                    break;
875

    
876
                default:
877
                    TRANSFORM_COEFF(cplcoeff, get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]),
878
                            exps[start], scale_factors);
879
            }
880
            for (ch = 0; ch < ctx->nfchans; ch++)
881
                if ((ctx->chincpl >> ch) & 1)
882
                    ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
883
            start++;
884
        }
885
    }
886

    
887
    return 0;
888
}
889

    
890
/* Get the transform coefficients for particular channel */
891
static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
892
{
893
    GetBitContext *gb = &ctx->gb;
894
    int i, gcode, tbap, dithflag, end;
895
    uint8_t *exps;
896
    uint8_t *bap;
897
    float *coeffs;
898
    float factors[25];
899

    
900
    for (i = 0; i < 25; i++)
901
        factors[i] = scale_factors[i] * ctx->chcoeffs[ch_index];
902

    
903
    if (ch_index != -1) { /* fbw channels */
904
        dithflag = (ctx->dithflag >> ch_index) & 1;
905
        exps = ctx->dexps[ch_index];
906
        bap = ctx->bap[ch_index];
907
        coeffs = ctx->transform_coeffs[ch_index + 1];
908
        end = ctx->endmant[ch_index];
909
    } else if (ch_index == -1) {
910
        dithflag = 0;
911
        exps = ctx->dlfeexps;
912
        bap = ctx->lfebap;
913
        coeffs = ctx->transform_coeffs[0];
914
        end = 7;
915
    }
916

    
917

    
918
    for (i = 0; i < end; i++) {
919
        tbap = bap[i];
920
        switch (tbap) {
921
            case 0:
922
                if (!dithflag) {
923
                    coeffs[i] = 0;
924
                    continue;
925
                }
926
                else {
927
                    TRANSFORM_COEFF(coeffs[i], dither_int16(&ctx->dith_state), exps[i], factors);
928
                    coeffs[i] *= LEVEL_MINUS_3DB;
929
                    continue;
930
                }
931

    
932
            case 1:
933
                if (m->l3ptr > 2) {
934
                    gcode = get_bits(gb, 5);
935
                    m->l3_quantizers[0] = l3_quantizers_1[gcode];
936
                    m->l3_quantizers[1] = l3_quantizers_2[gcode];
937
                    m->l3_quantizers[2] = l3_quantizers_3[gcode];
938
                    m->l3ptr = 0;
939
                }
940
                TRANSFORM_COEFF(coeffs[i], m->l3_quantizers[m->l3ptr++], exps[i], factors);
941
                continue;
942

    
943
            case 2:
944
                if (m->l5ptr > 2) {
945
                    gcode = get_bits(gb, 7);
946
                    m->l5_quantizers[0] = l5_quantizers_1[gcode];
947
                    m->l5_quantizers[1] = l5_quantizers_2[gcode];
948
                    m->l5_quantizers[2] = l5_quantizers_3[gcode];
949
                    m->l5ptr = 0;
950
                }
951
                TRANSFORM_COEFF(coeffs[i], m->l5_quantizers[m->l5ptr++], exps[i], factors);
952
                continue;
953

    
954
            case 3:
955
                TRANSFORM_COEFF(coeffs[i], l7_quantizers[get_bits(gb, 3)], exps[i], factors);
956
                continue;
957

    
958
            case 4:
959
                if (m->l11ptr > 1) {
960
                    gcode = get_bits(gb, 7);
961
                    m->l11_quantizers[0] = l11_quantizers_1[gcode];
962
                    m->l11_quantizers[1] = l11_quantizers_2[gcode];
963
                    m->l11ptr = 0;
964
                }
965
                TRANSFORM_COEFF(coeffs[i], m->l11_quantizers[m->l11ptr++], exps[i], factors);
966
                continue;
967

    
968
            case 5:
969
                TRANSFORM_COEFF(coeffs[i], l15_quantizers[get_bits(gb, 4)], exps[i], factors);
970
                continue;
971

    
972
            default:
973
                TRANSFORM_COEFF(coeffs[i], get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]), exps[i], factors);
974
                continue;
975
        }
976
    }
977

    
978
    return 0;
979
}
980

    
981
static int get_transform_coeffs(AC3DecodeContext * ctx)
982
{
983
    int i, end;
984
    int got_cplchan = 0;
985
    mant_groups m;
986

    
987
    m.l3ptr = m.l5ptr = m.l11ptr = 3;
988

    
989
    for (i = 0; i < ctx->nfchans; i++) {
990
        /* transform coefficients for individual channel */
991
        if (get_transform_coeffs_ch(ctx, i, &m))
992
            return -1;
993
        /* tranform coefficients for coupling channels */
994
        if ((ctx->chincpl >> i) & 1)  {
995
            if (!got_cplchan) {
996
                if (get_transform_coeffs_cpling(ctx, &m)) {
997
                    av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
998
                    return -1;
999
                }
1000
                got_cplchan = 1;
1001
            }
1002
            end = ctx->cplendmant;
1003
        } else
1004
            end = ctx->endmant[i];
1005
        do
1006
            ctx->transform_coeffs[i + 1][end] = 0;
1007
        while(++end < 256);
1008
    }
1009
    if (ctx->lfeon) {
1010
        if (get_transform_coeffs_ch(ctx, -1, &m))
1011
                return -1;
1012
        for (i = 7; i < 256; i++) {
1013
            ctx->transform_coeffs[0][i] = 0;
1014
        }
1015
    }
1016

    
1017
    return 0;
1018
}
1019

    
1020
static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
1021
{
1022
    float tmp0, tmp1;
1023

    
1024
    while (start < end) {
1025
        tmp0 = ctx->transform_coeffs[1][start];
1026
        tmp1 = ctx->transform_coeffs[2][start];
1027
        ctx->transform_coeffs[1][start] = tmp0 + tmp1;
1028
        ctx->transform_coeffs[2][start] = tmp0 - tmp1;
1029
        start++;
1030
    }
1031
}
1032

    
1033
static void do_rematrixing(AC3DecodeContext *ctx)
1034
{
1035
    int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
1036
    int end, bndend;
1037

    
1038
    end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
1039

    
1040
    if (ctx->rematflg & 1)
1041
        do_rematrixing1(ctx, bnd1, bnd2);
1042

    
1043
    if (ctx->rematflg & 2)
1044
        do_rematrixing1(ctx, bnd2, bnd3);
1045

    
1046
    bndend = bnd4;
1047
    if (bndend > end) {
1048
        bndend = end;
1049
        if (ctx->rematflg & 4)
1050
            do_rematrixing1(ctx, bnd3, bndend);
1051
    } else {
1052
        if (ctx->rematflg & 4)
1053
            do_rematrixing1(ctx, bnd3, bnd4);
1054
        if (ctx->rematflg & 8)
1055
            do_rematrixing1(ctx, bnd4, end);
1056
    }
1057
}
1058

    
1059
static void get_downmix_coeffs(AC3DecodeContext *ctx)
1060
{
1061
    int from = ctx->acmod;
1062
    int to = ctx->blkoutput;
1063
    float clev = clevs[ctx->cmixlev];
1064
    float slev = slevs[ctx->surmixlev];
1065
    float nf = 1.0; //normalization factor for downmix coeffs
1066

    
1067
    if (to == AC3_OUTPUT_UNMODIFIED)
1068
        return;
1069

    
1070
    switch (from) {
1071
        case AC3_INPUT_DUALMONO:
1072
            switch (to) {
1073
                case AC3_OUTPUT_MONO:
1074
                case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */
1075
                    nf = 0.5;
1076
                    ctx->chcoeffs[0] *= nf;
1077
                    ctx->chcoeffs[1] *= nf;
1078
                    break;
1079
            }
1080
            break;
1081
        case AC3_INPUT_MONO:
1082
            switch (to) {
1083
                case AC3_OUTPUT_STEREO:
1084
                    nf = LEVEL_MINUS_3DB;
1085
                    ctx->chcoeffs[0] *= nf;
1086
                    break;
1087
            }
1088
            break;
1089
        case AC3_INPUT_STEREO:
1090
            switch (to) {
1091
                case AC3_OUTPUT_MONO:
1092
                    nf = LEVEL_MINUS_3DB;
1093
                    ctx->chcoeffs[0] *= nf;
1094
                    ctx->chcoeffs[1] *= nf;
1095
                    break;
1096
            }
1097
            break;
1098
        case AC3_INPUT_3F:
1099
            switch (to) {
1100
                case AC3_OUTPUT_MONO:
1101
                    nf = LEVEL_MINUS_3DB / (1.0 + clev);
1102
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1103
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1104
                    ctx->chcoeffs[1] *= ((nf * clev * LEVEL_MINUS_3DB) / 2.0);
1105
                    break;
1106
                case AC3_OUTPUT_STEREO:
1107
                    nf = 1.0 / (1.0 + clev);
1108
                    ctx->chcoeffs[0] *= nf;
1109
                    ctx->chcoeffs[2] *= nf;
1110
                    ctx->chcoeffs[1] *= (nf * clev);
1111
                    break;
1112
            }
1113
            break;
1114
        case AC3_INPUT_2F_1R:
1115
            switch (to) {
1116
                case AC3_OUTPUT_MONO:
1117
                    nf = 2.0 * LEVEL_MINUS_3DB / (2.0 + slev);
1118
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1119
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1120
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
1121
                    break;
1122
                case AC3_OUTPUT_STEREO:
1123
                    nf = 1.0 / (1.0 + (slev * LEVEL_MINUS_3DB));
1124
                    ctx->chcoeffs[0] *= nf;
1125
                    ctx->chcoeffs[1] *= nf;
1126
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
1127
                    break;
1128
                case AC3_OUTPUT_DOLBY:
1129
                    nf = 1.0 / (1.0 + LEVEL_MINUS_3DB);
1130
                    ctx->chcoeffs[0] *= nf;
1131
                    ctx->chcoeffs[1] *= nf;
1132
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1133
                    break;
1134
            }
1135
            break;
1136
        case AC3_INPUT_3F_1R:
1137
            switch (to) {
1138
                case AC3_OUTPUT_MONO:
1139
                    nf = LEVEL_MINUS_3DB / (1.0 + clev + (slev / 2.0));
1140
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1141
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1142
                    ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
1143
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1144
                    break;
1145
                case AC3_OUTPUT_STEREO:
1146
                    nf = 1.0 / (1.0 + clev + (slev * LEVEL_MINUS_3DB));
1147
                    ctx->chcoeffs[0] *= nf;
1148
                    ctx->chcoeffs[2] *= nf;
1149
                    ctx->chcoeffs[1] *= (nf * clev);
1150
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1151
                    break;
1152
                case AC3_OUTPUT_DOLBY:
1153
                    nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
1154
                    ctx->chcoeffs[0] *= nf;
1155
                    ctx->chcoeffs[1] *= nf;
1156
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1157
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
1158
                    break;
1159
            }
1160
            break;
1161
        case AC3_INPUT_2F_2R:
1162
            switch (to) {
1163
                case AC3_OUTPUT_MONO:
1164
                    nf = LEVEL_MINUS_3DB / (1.0 + slev);
1165
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1166
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1167
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
1168
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1169
                    break;
1170
                case AC3_OUTPUT_STEREO:
1171
                    nf = 1.0 / (1.0 + slev);
1172
                    ctx->chcoeffs[0] *= nf;
1173
                    ctx->chcoeffs[1] *= nf;
1174
                    ctx->chcoeffs[2] *= (nf * slev);
1175
                    ctx->chcoeffs[3] *= (nf * slev);
1176
                    break;
1177
                case AC3_OUTPUT_DOLBY:
1178
                    nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
1179
                    ctx->chcoeffs[0] *= nf;
1180
                    ctx->chcoeffs[1] *= nf;
1181
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1182
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
1183
                    break;
1184
            }
1185
            break;
1186
        case AC3_INPUT_3F_2R:
1187
            switch (to) {
1188
                case AC3_OUTPUT_MONO:
1189
                    nf = LEVEL_MINUS_3DB / (1.0 + clev + slev);
1190
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1191
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1192
                    ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
1193
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1194
                    ctx->chcoeffs[4] *= (nf * slev * LEVEL_MINUS_3DB);
1195
                    break;
1196
                case AC3_OUTPUT_STEREO:
1197
                    nf = 1.0 / (1.0 + clev + slev);
1198
                    ctx->chcoeffs[0] *= nf;
1199
                    ctx->chcoeffs[2] *= nf;
1200
                    ctx->chcoeffs[1] *= (nf * clev);
1201
                    ctx->chcoeffs[3] *= (nf * slev);
1202
                    ctx->chcoeffs[4] *= (nf * slev);
1203
                    break;
1204
                case AC3_OUTPUT_DOLBY:
1205
                    nf = 1.0 / (1.0 + (3.0 * LEVEL_MINUS_3DB));
1206
                    ctx->chcoeffs[0] *= nf;
1207
                    ctx->chcoeffs[1] *= nf;
1208
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1209
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
1210
                    ctx->chcoeffs[4] *= (nf * LEVEL_MINUS_3DB);
1211
                    break;
1212
            }
1213
            break;
1214
    }
1215
}
1216

    
1217
static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx)
1218
{
1219
    int i;
1220
    float (*output)[BLOCK_SIZE] = ctx->output;
1221

    
1222
    for (i = 0; i < 256; i++)
1223
        output[1][i] += output[2][i];
1224
    memset(output[2], 0, sizeof(output[2]));
1225
}
1226

    
1227
static inline void mix_dualmono_to_stereo(AC3DecodeContext *ctx)
1228
{
1229
    int i;
1230
    float tmp;
1231
    float (*output)[BLOCK_SIZE] = ctx->output;
1232

    
1233
    for (i = 0; i < 256; i++) {
1234
        tmp = output[1][i] + output[2][i];
1235
        output[1][i] = output[2][i] = tmp;
1236
    }
1237
}
1238

    
1239
static inline void upmix_mono_to_stereo(AC3DecodeContext *ctx)
1240
{
1241
    int i;
1242
    float (*output)[BLOCK_SIZE] = ctx->output;
1243

    
1244
    for (i = 0; i < 256; i++)
1245
        output[2][i] = output[1][i];
1246
}
1247

    
1248
static inline void mix_stereo_to_mono(AC3DecodeContext *ctx)
1249
{
1250
    int i;
1251
    float (*output)[BLOCK_SIZE] = ctx->output;
1252

    
1253
    for (i = 0; i < 256; i++)
1254
        output[1][i] += output[2][i];
1255
    memset(output[2], 0, sizeof(output[2]));
1256
}
1257

    
1258
static inline void mix_3f_to_mono(AC3DecodeContext *ctx)
1259
{
1260
    int i;
1261
    float (*output)[BLOCK_SIZE] = ctx->output;
1262

    
1263
    for (i = 0; i < 256; i++)
1264
        output[1][i] += (output[2][i] + output[3][i]);
1265
    memset(output[2], 0, sizeof(output[2]));
1266
    memset(output[3], 0, sizeof(output[3]));
1267
}
1268

    
1269
static inline void mix_3f_to_stereo(AC3DecodeContext *ctx)
1270
{
1271
    int i;
1272
    float (*output)[BLOCK_SIZE] = ctx->output;
1273

    
1274
    for (i = 0; i < 256; i++) {
1275
        output[1][i] += output[2][i];
1276
        output[2][i] += output[3][i];
1277
    }
1278
    memset(output[3], 0, sizeof(output[3]));
1279
}
1280

    
1281
static inline void mix_2f_1r_to_mono(AC3DecodeContext *ctx)
1282
{
1283
    int i;
1284
    float (*output)[BLOCK_SIZE] = ctx->output;
1285

    
1286
    for (i = 0; i < 256; i++)
1287
        output[1][i] += (output[2][i] + output[3][i]);
1288
    memset(output[2], 0, sizeof(output[2]));
1289
    memset(output[3], 0, sizeof(output[3]));
1290

    
1291
}
1292

    
1293
static inline void mix_2f_1r_to_stereo(AC3DecodeContext *ctx)
1294
{
1295
    int i;
1296
    float (*output)[BLOCK_SIZE] = ctx->output;
1297

    
1298
    for (i = 0; i < 256; i++) {
1299
        output[1][i] += output[2][i];
1300
        output[2][i] += output[3][i];
1301
    }
1302
    memset(output[3], 0, sizeof(output[3]));
1303
}
1304

    
1305
static inline void mix_2f_1r_to_dolby(AC3DecodeContext *ctx)
1306
{
1307
    int i;
1308
    float (*output)[BLOCK_SIZE] = ctx->output;
1309

    
1310
    for (i = 0; i < 256; i++) {
1311
        output[1][i] -= output[3][i];
1312
        output[2][i] += output[3][i];
1313
    }
1314
    memset(output[3], 0, sizeof(output[3]));
1315
}
1316

    
1317
static inline void mix_3f_1r_to_mono(AC3DecodeContext *ctx)
1318
{
1319
    int i;
1320
    float (*output)[BLOCK_SIZE] = ctx->output;
1321

    
1322
    for (i = 0; i < 256; i++)
1323
        output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1324
    memset(output[2], 0, sizeof(output[2]));
1325
    memset(output[3], 0, sizeof(output[3]));
1326
    memset(output[4], 0, sizeof(output[4]));
1327
}
1328

    
1329
static inline void mix_3f_1r_to_stereo(AC3DecodeContext *ctx)
1330
{
1331
    int i;
1332
    float (*output)[BLOCK_SIZE] = ctx->output;
1333

    
1334
    for (i = 0; i < 256; i++) {
1335
        output[1][i] += (output[2][i] + output[4][i]);
1336
        output[2][i] += (output[3][i] + output[4][i]);
1337
    }
1338
    memset(output[3], 0, sizeof(output[3]));
1339
    memset(output[4], 0, sizeof(output[4]));
1340
}
1341

    
1342
static inline void mix_3f_1r_to_dolby(AC3DecodeContext *ctx)
1343
{
1344
    int i;
1345
    float (*output)[BLOCK_SIZE] = ctx->output;
1346

    
1347
    for (i = 0; i < 256; i++) {
1348
        output[1][i] += (output[2][i] - output[4][i]);
1349
        output[2][i] += (output[3][i] + output[4][i]);
1350
    }
1351
    memset(output[3], 0, sizeof(output[3]));
1352
    memset(output[4], 0, sizeof(output[4]));
1353
}
1354

    
1355
static inline void mix_2f_2r_to_mono(AC3DecodeContext *ctx)
1356
{
1357
    int i;
1358
    float (*output)[BLOCK_SIZE] = ctx->output;
1359

    
1360
    for (i = 0; i < 256; i++)
1361
        output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1362
    memset(output[2], 0, sizeof(output[2]));
1363
    memset(output[3], 0, sizeof(output[3]));
1364
    memset(output[4], 0, sizeof(output[4]));
1365
}
1366

    
1367
static inline void mix_2f_2r_to_stereo(AC3DecodeContext *ctx)
1368
{
1369
    int i;
1370
    float (*output)[BLOCK_SIZE] = ctx->output;
1371

    
1372
    for (i = 0; i < 256; i++) {
1373
        output[1][i] += output[3][i];
1374
        output[2][i] += output[4][i];
1375
    }
1376
    memset(output[3], 0, sizeof(output[3]));
1377
    memset(output[4], 0, sizeof(output[4]));
1378
}
1379

    
1380
static inline void mix_2f_2r_to_dolby(AC3DecodeContext *ctx)
1381
{
1382
    int i;
1383
    float (*output)[BLOCK_SIZE] = ctx->output;
1384

    
1385
    for (i = 0; i < 256; i++) {
1386
        output[1][i] -= output[3][i];
1387
        output[2][i] += output[4][i];
1388
    }
1389
    memset(output[3], 0, sizeof(output[3]));
1390
    memset(output[4], 0, sizeof(output[4]));
1391
}
1392

    
1393
static inline void mix_3f_2r_to_mono(AC3DecodeContext *ctx)
1394
{
1395
    int i;
1396
    float (*output)[BLOCK_SIZE] = ctx->output;
1397

    
1398
    for (i = 0; i < 256; i++)
1399
        output[1][i] += (output[2][i] + output[3][i] + output[4][i] + output[5][i]);
1400
    memset(output[2], 0, sizeof(output[2]));
1401
    memset(output[3], 0, sizeof(output[3]));
1402
    memset(output[4], 0, sizeof(output[4]));
1403
    memset(output[5], 0, sizeof(output[5]));
1404
}
1405

    
1406
static inline void mix_3f_2r_to_stereo(AC3DecodeContext *ctx)
1407
{
1408
    int i;
1409
    float (*output)[BLOCK_SIZE] = ctx->output;
1410

    
1411
    for (i = 0; i < 256; i++) {
1412
        output[1][i] += (output[2][i] + output[4][i]);
1413
        output[2][i] += (output[3][i] + output[5][i]);
1414
    }
1415
    memset(output[3], 0, sizeof(output[3]));
1416
    memset(output[4], 0, sizeof(output[4]));
1417
    memset(output[5], 0, sizeof(output[5]));
1418
}
1419

    
1420
static inline void mix_3f_2r_to_dolby(AC3DecodeContext *ctx)
1421
{
1422
    int i;
1423
    float (*output)[BLOCK_SIZE] = ctx->output;
1424

    
1425
    for (i = 0; i < 256; i++) {
1426
        output[1][i] += (output[2][i] - output[4][i] - output[5][i]);
1427
        output[2][i] += (output[3][i] + output[4][i] + output[5][i]);
1428
    }
1429
    memset(output[3], 0, sizeof(output[3]));
1430
    memset(output[4], 0, sizeof(output[4]));
1431
    memset(output[5], 0, sizeof(output[5]));
1432
}
1433

    
1434
static void do_downmix(AC3DecodeContext *ctx)
1435
{
1436
    int from = ctx->acmod;
1437
    int to = ctx->blkoutput;
1438

    
1439
    switch (from) {
1440
        case AC3_INPUT_DUALMONO:
1441
            switch (to) {
1442
                case AC3_OUTPUT_MONO:
1443
                    mix_dualmono_to_mono(ctx);
1444
                    break;
1445
                case AC3_OUTPUT_STEREO: /* We assume that sum of both mono channels is requested */
1446
                    mix_dualmono_to_stereo(ctx);
1447
                    break;
1448
            }
1449
            break;
1450
        case AC3_INPUT_MONO:
1451
            switch (to) {
1452
                case AC3_OUTPUT_STEREO:
1453
                    upmix_mono_to_stereo(ctx);
1454
                    break;
1455
            }
1456
            break;
1457
        case AC3_INPUT_STEREO:
1458
            switch (to) {
1459
                case AC3_OUTPUT_MONO:
1460
                    mix_stereo_to_mono(ctx);
1461
                    break;
1462
            }
1463
            break;
1464
        case AC3_INPUT_3F:
1465
            switch (to) {
1466
                case AC3_OUTPUT_MONO:
1467
                    mix_3f_to_mono(ctx);
1468
                    break;
1469
                case AC3_OUTPUT_STEREO:
1470
                    mix_3f_to_stereo(ctx);
1471
                    break;
1472
            }
1473
            break;
1474
        case AC3_INPUT_2F_1R:
1475
            switch (to) {
1476
                case AC3_OUTPUT_MONO:
1477
                    mix_2f_1r_to_mono(ctx);
1478
                    break;
1479
                case AC3_OUTPUT_STEREO:
1480
                    mix_2f_1r_to_stereo(ctx);
1481
                    break;
1482
                case AC3_OUTPUT_DOLBY:
1483
                    mix_2f_1r_to_dolby(ctx);
1484
                    break;
1485
            }
1486
            break;
1487
        case AC3_INPUT_3F_1R:
1488
            switch (to) {
1489
                case AC3_OUTPUT_MONO:
1490
                    mix_3f_1r_to_mono(ctx);
1491
                    break;
1492
                case AC3_OUTPUT_STEREO:
1493
                    mix_3f_1r_to_stereo(ctx);
1494
                    break;
1495
                case AC3_OUTPUT_DOLBY:
1496
                    mix_3f_1r_to_dolby(ctx);
1497
                    break;
1498
            }
1499
            break;
1500
        case AC3_INPUT_2F_2R:
1501
            switch (to) {
1502
                case AC3_OUTPUT_MONO:
1503
                    mix_2f_2r_to_mono(ctx);
1504
                    break;
1505
                case AC3_OUTPUT_STEREO:
1506
                    mix_2f_2r_to_stereo(ctx);
1507
                    break;
1508
                case AC3_OUTPUT_DOLBY:
1509
                    mix_2f_2r_to_dolby(ctx);
1510
                    break;
1511
            }
1512
            break;
1513
        case AC3_INPUT_3F_2R:
1514
            switch (to) {
1515
                case AC3_OUTPUT_MONO:
1516
                    mix_3f_2r_to_mono(ctx);
1517
                    break;
1518
                case AC3_OUTPUT_STEREO:
1519
                    mix_3f_2r_to_stereo(ctx);
1520
                    break;
1521
                case AC3_OUTPUT_DOLBY:
1522
                    mix_3f_2r_to_dolby(ctx);
1523
                    break;
1524
            }
1525
            break;
1526
    }
1527
}
1528

    
1529
static void dump_floats(const char *name, int prec, const float *tab, int n)
1530
{
1531
    int i;
1532

    
1533
    av_log(NULL, AV_LOG_INFO, "%s[%d]:\n", name, n);
1534
    for(i=0;i<n;i++) {
1535
        if ((i & 7) == 0)
1536
            av_log(NULL, AV_LOG_INFO, "%4d: ", i);
1537
        av_log(NULL, AV_LOG_INFO, " %8.*f", prec, tab[i]);
1538
        if ((i & 7) == 7)
1539
            av_log(NULL, AV_LOG_INFO, "\n");
1540
    }
1541
    if ((i & 7) != 0)
1542
        av_log(NULL, AV_LOG_INFO, "\n");
1543
}
1544

    
1545
#define CMUL(pre, pim, are, aim, bre, bim) \
1546
{\
1547
    float _are = (are);\
1548
    float _aim = (aim);\
1549
    float _bre = (bre);\
1550
    float _bim = (bim);\
1551
    (pre) = _are * _bre - _aim * _bim;\
1552
    (pim) = _are * _bim + _aim * _bre;\
1553
}
1554

    
1555
static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
1556
{
1557
    int k;
1558
    float x1[128], x2[128];
1559

    
1560
    for (k = 0; k < N / 4; k++) {
1561
        x1[k] = ctx->transform_coeffs[chindex][2 * k];
1562
        x2[k] = ctx->transform_coeffs[chindex][2 * k + 1];
1563
    }
1564

    
1565
    ff_imdct_calc(&ctx->imdct_256, ctx->tmp_output, x1, ctx->tmp_imdct);
1566
    ff_imdct_calc(&ctx->imdct_256, ctx->tmp_output + 256, x2, ctx->tmp_imdct);
1567

    
1568
    for (k = 0; k < N / 2; k++) {
1569
        ctx->output[chindex][k] = ctx->tmp_output[k] * window[k] + ctx->delay[chindex][k];
1570
        //dump_floats("samples", 10, ctx->output[chindex], 256);
1571
        ctx->delay[chindex][k] = ctx->tmp_output[N / 2 + k] * window[255 - k];
1572
    }
1573
}
1574

    
1575
static void do_imdct_512(AC3DecodeContext *ctx, int chindex)
1576
{
1577
    int k;
1578

    
1579
    ff_imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1580
            ctx->transform_coeffs[chindex], ctx->tmp_imdct);
1581
    //ff_imdct_calc_ac3_512(&ctx->imdct_512, ctx->tmp_output, ctx->transform_coeffs[chindex],
1582
    //        ctx->tmp_imdct, window);
1583

    
1584
    for (k = 0; k < N / 2; k++) {
1585
        ctx->output[chindex][k] = ctx->tmp_output[k] * window[k] + ctx->delay[chindex][k];
1586
        //dump_floats("samples", 10, ctx->output[chindex], 256);
1587
        ctx->delay[chindex][k] = ctx->tmp_output[N / 2 + k] * window[255 - k];
1588
    }
1589
}
1590

    
1591
static inline void do_imdct(AC3DecodeContext *ctx)
1592
{
1593
    int i;
1594

    
1595
    if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
1596
        do_imdct_512(ctx, 0);
1597
    }
1598
    for (i = 0; i < ctx->nfchans; i++) {
1599
        if ((ctx->blksw >> i) & 1)
1600
            do_imdct_256(ctx, i + 1);
1601
        else
1602
            do_imdct_512(ctx, i + 1);
1603
    }
1604
}
1605

    
1606
static int ac3_parse_audio_block(AC3DecodeContext * ctx)
1607
{
1608
    int nfchans = ctx->nfchans;
1609
    int acmod = ctx->acmod;
1610
    int i, bnd, rbnd, seg, grpsize;
1611
    GetBitContext *gb = &ctx->gb;
1612
    int bit_alloc_flags = 0;
1613
    float drange;
1614
    uint8_t *dexps;
1615
    int mstrcplco, cplcoexp, cplcomant;
1616
    int dynrng, chbwcod, ngrps, cplabsexp, skipl;
1617

    
1618
    for (i = 0; i < 5; i++)
1619
        ctx->chcoeffs[i] = 2.0;
1620

    
1621
    ctx->blksw = 0;
1622
    for (i = 0; i < nfchans; i++) /*block switch flag */
1623
        ctx->blksw |= get_bits1(gb) << i;
1624

    
1625
    ctx->dithflag = 0;
1626
    for (i = 0; i < nfchans; i++) /* dithering flag */
1627
        ctx->dithflag |= get_bits1(gb) << i;
1628

    
1629
    if (get_bits1(gb)) { /* dynamic range */
1630
        dynrng = get_sbits(gb, 8);
1631
        drange = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1632
        for (i = 0; i < nfchans; i++)
1633
            ctx->chcoeffs[i] *= drange;
1634
    }
1635

    
1636
    if (acmod == 0x00 && get_bits1(gb)) { /* dynamic range 1+1 mode */
1637
        dynrng = get_sbits(gb, 8);
1638
        drange = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1639
        ctx->chcoeffs[1] *= drange;
1640
    }
1641

    
1642
    get_downmix_coeffs(ctx);
1643

    
1644
    if (get_bits1(gb)) { /* coupling strategy */
1645
        ctx->cplinu = get_bits1(gb);
1646
        ctx->cplbndstrc = 0;
1647
        ctx->chincpl = 0;
1648
        if (ctx->cplinu) { /* coupling in use */
1649
            for (i = 0; i < nfchans; i++)
1650
                ctx->chincpl |= get_bits1(gb) << i;
1651

    
1652
            if (acmod == 0x02)
1653
                ctx->phsflginu = get_bits1(gb); //phase flag in use
1654

    
1655
            ctx->cplbegf = get_bits(gb, 4);
1656
            ctx->cplendf = get_bits(gb, 4);
1657

    
1658
            if (3 + ctx->cplendf - ctx->cplbegf < 0) {
1659
                av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", ctx->cplendf, ctx->cplbegf);
1660
                return -1;
1661
            }
1662

    
1663
            ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf;
1664
            ctx->cplstrtmant = ctx->cplbegf * 12 + 37;
1665
            ctx->cplendmant = ctx->cplendf * 12 + 73;
1666
            for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
1667
                if (get_bits1(gb)) {
1668
                    ctx->cplbndstrc |= 1 << i;
1669
                    ctx->ncplbnd--;
1670
                }
1671
        }
1672
    }
1673

    
1674
    if (ctx->cplinu) {
1675
        ctx->cplcoe = 0;
1676

    
1677
        for (i = 0; i < nfchans; i++)
1678
            if ((ctx->chincpl) >> i & 1)
1679
                if (get_bits1(gb)) { /* coupling co-ordinates */
1680
                    ctx->cplcoe |= 1 << i;
1681
                    mstrcplco = 3 * get_bits(gb, 2);
1682
                    for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
1683
                        cplcoexp = get_bits(gb, 4);
1684
                        cplcomant = get_bits(gb, 4);
1685
                        if (cplcoexp == 15)
1686
                            cplcomant <<= 14;
1687
                        else
1688
                            cplcomant = (cplcomant | 0x10) << 13;
1689
                        ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
1690
                    }
1691
                }
1692

    
1693
        if (acmod == 0x02 && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
1694
            for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
1695
                if (get_bits1(gb))
1696
                    ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
1697
    }
1698

    
1699
    if (acmod == 0x02) {/* rematrixing */
1700
        ctx->rematstr = get_bits1(gb);
1701
        if (ctx->rematstr) {
1702
            ctx->rematflg = 0;
1703

    
1704
            if (!(ctx->cplinu) || ctx->cplbegf > 2)
1705
                for (rbnd = 0; rbnd < 4; rbnd++)
1706
                    ctx->rematflg |= get_bits1(gb) << rbnd;
1707
            if (ctx->cplbegf > 0 && ctx->cplbegf <= 2 && ctx->cplinu)
1708
                for (rbnd = 0; rbnd < 3; rbnd++)
1709
                    ctx->rematflg |= get_bits1(gb) << rbnd;
1710
            if (ctx->cplbegf == 0 && ctx->cplinu)
1711
                for (rbnd = 0; rbnd < 2; rbnd++)
1712
                    ctx->rematflg |= get_bits1(gb) << rbnd;
1713
        }
1714
    }
1715

    
1716
    ctx->cplexpstr = AC3_EXPSTR_REUSE;
1717
    ctx->lfeexpstr = AC3_EXPSTR_REUSE;
1718
    if (ctx->cplinu) /* coupling exponent strategy */
1719
        ctx->cplexpstr = get_bits(gb, 2);
1720
    for (i = 0; i < nfchans; i++)  /* channel exponent strategy */
1721
        ctx->chexpstr[i] = get_bits(gb, 2);
1722
    if (ctx->lfeon)  /* lfe exponent strategy */
1723
        ctx->lfeexpstr = get_bits1(gb);
1724

    
1725
    for (i = 0; i < nfchans; i++) /* channel bandwidth code */
1726
        if (ctx->chexpstr[i] != AC3_EXPSTR_REUSE) {
1727
            if ((ctx->chincpl >> i) & 1)
1728
                ctx->endmant[i] = ctx->cplstrtmant;
1729
            else {
1730
                chbwcod = get_bits(gb, 6);
1731
                if (chbwcod > 60) {
1732
                    av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
1733
                    return -1;
1734
                }
1735
                ctx->endmant[i] = chbwcod * 3 + 73;
1736
            }
1737
        }
1738

    
1739
    if (ctx->cplexpstr != AC3_EXPSTR_REUSE) {/* coupling exponents */
1740
        bit_alloc_flags = 64;
1741
        cplabsexp = get_bits(gb, 4) << 1;
1742
        ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
1743
        if (decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant)) {
1744
            av_log(NULL, AV_LOG_ERROR, "error decoding coupling exponents\n");
1745
            return -1;
1746
        }
1747
    }
1748

    
1749
    for (i = 0; i < nfchans; i++) /* fbw channel exponents */
1750
        if (ctx->chexpstr[i] != AC3_EXPSTR_REUSE) {
1751
            bit_alloc_flags |= 1 << i;
1752
            grpsize = 3 << (ctx->chexpstr[i] - 1);
1753
            ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
1754
            dexps = ctx->dexps[i];
1755
            dexps[0] = get_bits(gb, 4);
1756
            if (decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1)) {
1757
                av_log(NULL, AV_LOG_ERROR, "error decoding channel %d exponents\n", i);
1758
                return -1;
1759
            }
1760
            skip_bits(gb, 2); /* skip gainrng */
1761
        }
1762

    
1763
    if (ctx->lfeexpstr != AC3_EXPSTR_REUSE) { /* lfe exponents */
1764
        bit_alloc_flags |= 32;
1765
        ctx->dlfeexps[0] = get_bits(gb, 4);
1766
        if (decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1)) {
1767
            av_log(NULL, AV_LOG_ERROR, "error decoding lfe exponents\n");
1768
            return -1;
1769
        }
1770
    }
1771

    
1772
    if (get_bits1(gb)) { /* bit allocation information */
1773
        bit_alloc_flags = 127;
1774
        ctx->sdcycod = get_bits(gb, 2);
1775
        ctx->fdcycod = get_bits(gb, 2);
1776
        ctx->sgaincod = get_bits(gb, 2);
1777
        ctx->dbpbcod = get_bits(gb, 2);
1778
        ctx->floorcod = get_bits(gb, 3);
1779
    }
1780

    
1781
    if (get_bits1(gb)) { /* snroffset */
1782
        bit_alloc_flags = 127;
1783
        ctx->csnroffst = get_bits(gb, 6);
1784
        if (ctx->cplinu) { /* couling fine snr offset and fast gain code */
1785
            ctx->cplfsnroffst = get_bits(gb, 4);
1786
            ctx->cplfgaincod = get_bits(gb, 3);
1787
        }
1788
        for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
1789
            ctx->fsnroffst[i] = get_bits(gb, 4);
1790
            ctx->fgaincod[i] = get_bits(gb, 3);
1791
        }
1792
        if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
1793
            ctx->lfefsnroffst = get_bits(gb, 4);
1794
            ctx->lfefgaincod = get_bits(gb, 3);
1795
        }
1796
    }
1797

    
1798
    if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
1799
        bit_alloc_flags |= 64;
1800
        ctx->cplfleak = get_bits(gb, 3);
1801
        ctx->cplsleak = get_bits(gb, 3);
1802
    }
1803

    
1804
    if (get_bits1(gb)) { /* delta bit allocation information */
1805
        bit_alloc_flags = 127;
1806

    
1807
        if (ctx->cplinu) {
1808
            ctx->cpldeltbae = get_bits(gb, 2);
1809
            if (ctx->cpldeltbae == AC3_DBASTR_RESERVED) {
1810
                av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
1811
                return -1;
1812
            }
1813
        }
1814

    
1815
        for (i = 0; i < nfchans; i++) {
1816
            ctx->deltbae[i] = get_bits(gb, 2);
1817
            if (ctx->deltbae[i] == AC3_DBASTR_RESERVED) {
1818
                av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
1819
                return -1;
1820
            }
1821
        }
1822

    
1823
        if (ctx->cplinu)
1824
            if (ctx->cpldeltbae == AC3_DBASTR_NEW) { /*coupling delta offset, len and bit allocation */
1825
                ctx->cpldeltnseg = get_bits(gb, 3);
1826
                for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
1827
                    ctx->cpldeltoffst[seg] = get_bits(gb, 5);
1828
                    ctx->cpldeltlen[seg] = get_bits(gb, 4);
1829
                    ctx->cpldeltba[seg] = get_bits(gb, 3);
1830
                }
1831
            }
1832

    
1833
        for (i = 0; i < nfchans; i++)
1834
            if (ctx->deltbae[i] == AC3_DBASTR_NEW) {/*channel delta offset, len and bit allocation */
1835
                ctx->deltnseg[i] = get_bits(gb, 3);
1836
                for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
1837
                    ctx->deltoffst[i][seg] = get_bits(gb, 5);
1838
                    ctx->deltlen[i][seg] = get_bits(gb, 4);
1839
                    ctx->deltba[i][seg] = get_bits(gb, 3);
1840
                }
1841
            }
1842
    }
1843

    
1844
    if (bit_alloc_flags) {
1845
        if (is_snr_offsets_zero(ctx)) {
1846
            memset(ctx->cplbap, 0, sizeof (ctx->cplbap));
1847
            memset(ctx->lfebap, 0, sizeof (ctx->lfebap));
1848
            for (i = 0; i < nfchans; i++)
1849
                memset(ctx->bap[i], 0, sizeof(ctx->bap[i]));
1850
        } else {
1851
            if (ctx->chincpl && (bit_alloc_flags & 64))
1852
                do_bit_allocation(ctx, 5);
1853
            for (i = 0; i < nfchans; i++)
1854
                if ((bit_alloc_flags >> i) & 1)
1855
                    do_bit_allocation(ctx, i);
1856
            if (ctx->lfeon && (bit_alloc_flags & 32))
1857
                do_bit_allocation(ctx, 6);
1858
        }
1859
    }
1860

    
1861
    if (get_bits1(gb)) { /* unused dummy data */
1862
        skipl = get_bits(gb, 9);
1863
        while(skipl--)
1864
            skip_bits(gb, 8);
1865
    }
1866
    /* unpack the transform coefficients
1867
     * * this also uncouples channels if coupling is in use.
1868
     */
1869
    if (get_transform_coeffs(ctx)) {
1870
        av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1871
        return -1;
1872
    }
1873
    /*for (i = 0; i < nfchans; i++)
1874
        dump_floats("channel transform coefficients", 10, ctx->transform_coeffs[i + 1], BLOCK_SIZE);*/
1875

    
1876
    /* recover coefficients if rematrixing is in use */
1877
    if (ctx->rematflg)
1878
        do_rematrixing(ctx);
1879

    
1880
    do_imdct(ctx);
1881
    /*for(i = 0; i < nfchans; i++)
1882
        dump_floats("channel output", 10, ctx->output[i + 1], BLOCK_SIZE);*/
1883

    
1884
    do_downmix(ctx);
1885

    
1886
    return 0;
1887
}
1888

    
1889
static inline int16_t convert(float f)
1890
{
1891
    if (f >= 1.0)
1892
        return 32767;
1893
    else if (f <= -1.0)
1894
        return -32768;
1895
    else
1896
        return (lrintf(f * 32767.0));
1897
}
1898

    
1899
static int frame_count = 0;
1900

    
1901
static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1902
{
1903
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1904
    int frame_start;
1905
    int16_t *out_samples = (int16_t *)data;
1906
    int i, j, k, value;
1907

    
1908
    av_log(NULL, AV_LOG_INFO, "decoding frame %d buf_size = %d\n", frame_count++, buf_size);
1909

    
1910
    //Synchronize the frame.
1911
    frame_start = ac3_synchronize(buf, buf_size);
1912
    if (frame_start == -1) {
1913
        av_log(avctx, AV_LOG_ERROR, "frame is not synchronized\n");
1914
        *data_size = 0;
1915
        return buf_size;
1916
    }
1917

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

    
1921
    //Parse the syncinfo.
1922
    //If 'fscod' or 'bsid' is not valid the decoder shall mute as per the standard.
1923
    if (!ac3_parse_sync_info(ctx)) {
1924
        av_log(avctx, AV_LOG_ERROR, "\n");
1925
        *data_size = 0;
1926
        return buf_size;
1927
    }
1928

    
1929
    //Parse the BSI.
1930
    //If 'bsid' is not valid decoder shall not decode the audio as per the standard.
1931
    ac3_parse_bsi(ctx);
1932

    
1933
    avctx->sample_rate = ctx->sampling_rate;
1934
    avctx->bit_rate = ctx->bit_rate;
1935

    
1936
    if (avctx->channels == 0) {
1937
        ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1938
        if (ctx->lfeon)
1939
            ctx->blkoutput |= AC3_OUTPUT_LFEON;
1940
        avctx->channels = ctx->nfchans + ctx->lfeon;
1941
    }
1942
    else if (avctx->channels == 1)
1943
        ctx->blkoutput |= AC3_OUTPUT_MONO;
1944
    else if (avctx->channels == 2) {
1945
        if (ctx->dsurmod == 0x02)
1946
            ctx->blkoutput |= AC3_OUTPUT_DOLBY;
1947
        else
1948
            ctx->blkoutput |= AC3_OUTPUT_STEREO;
1949
    }
1950
    else {
1951
        if (avctx->channels < (ctx->nfchans + ctx->lfeon))
1952
            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);
1953
        ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1954
        if (ctx->lfeon)
1955
            ctx->blkoutput |= AC3_OUTPUT_LFEON;
1956
        avctx->channels = ctx->nfchans + ctx->lfeon;
1957
    }
1958

    
1959
    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);
1960

    
1961
    //Parse the Audio Blocks.
1962
    for (i = 0; i < AUDIO_BLOCKS; i++) {
1963
        if (ac3_parse_audio_block(ctx)) {
1964
            av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1965
            *data_size = 0;
1966
            return ctx->frame_size;
1967
        }
1968
        for (k = 0; k < BLOCK_SIZE; k++) {
1969
            j = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1;
1970
            for (; j <= avctx->channels; j++) {
1971
                value = convert(ctx->output[j][k]);
1972
                *(out_samples++) = value;
1973
            }
1974
        }
1975
    }
1976
    *data_size = AUDIO_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);
1977
    return ctx->frame_size;
1978
}
1979

    
1980
static int ac3_decode_end(AVCodecContext *ctx)
1981
{
1982
    return 0;
1983
}
1984

    
1985
AVCodec lgpl_ac3_decoder = {
1986
    "ac3",
1987
    CODEC_TYPE_AUDIO,
1988
    CODEC_ID_AC3,
1989
    sizeof (AC3DecodeContext),
1990
    ac3_decode_init,
1991
    NULL,
1992
    ac3_decode_end,
1993
    ac3_decode_frame,
1994
};
1995