Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3dec.c @ 8fbb368d

History | View | Annotate | Download (38.5 KB)

1 0ec2cc35 Justin Ruggles
/*
2
 * AC-3 Audio Decoder
3 032732d4 Justin Ruggles
 * This code is developed as part of Google Summer of Code 2006 Program.
4
 *
5 0ec2cc35 Justin Ruggles
 * Copyright (c) 2006 Kartikey Mahendra BHATT (bhattkm at gmail dot com).
6 36266421 Justin Ruggles
 * Copyright (c) 2007 Justin Ruggles
7 032732d4 Justin Ruggles
 *
8 36266421 Justin Ruggles
 * Portions of this code are derived from liba52
9 032732d4 Justin Ruggles
 * http://liba52.sourceforge.net
10 36266421 Justin Ruggles
 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
11
 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
12 032732d4 Justin Ruggles
 *
13 0ec2cc35 Justin Ruggles
 * This file is part of FFmpeg.
14 2aa2c5c4 Justin Ruggles
 *
15 0ec2cc35 Justin Ruggles
 * FFmpeg is free software; you can redistribute it and/or
16 9d109601 Justin Ruggles
 * modify it under the terms of the GNU General Public
17 2aa2c5c4 Justin Ruggles
 * License as published by the Free Software Foundation; either
18
 * version 2 of the License, or (at your option) any later version.
19
 *
20 0ec2cc35 Justin Ruggles
 * FFmpeg is distributed in the hope that it will be useful,
21 2aa2c5c4 Justin Ruggles
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23 283614b5 Justin Ruggles
 * General Public License for more details.
24 2aa2c5c4 Justin Ruggles
 *
25 283614b5 Justin Ruggles
 * You should have received a copy of the GNU General Public
26 0ec2cc35 Justin Ruggles
 * License along with FFmpeg; if not, write to the Free Software
27 2aa2c5c4 Justin Ruggles
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28
 */
29
30
#include <stdio.h>
31
#include <stddef.h>
32
#include <math.h>
33
#include <string.h>
34
35 e6bca37c Justin Ruggles
#include "avcodec.h"
36 9fc1ab72 Justin Ruggles
#include "ac3_parser.h"
37 2aa2c5c4 Justin Ruggles
#include "bitstream.h"
38
#include "dsputil.h"
39 cb503702 Justin Ruggles
#include "random.h"
40 98a27a8a Justin Ruggles
41 967d397a Justin Ruggles
/* table for exponent to scale_factor mapping
42
 * scale_factor[i] = 2 ^ -(i + 15)
43
 */
44
static float scale_factors[25];
45
46 4415076f Justin Ruggles
/** table for grouping exponents */
47
static uint8_t exp_ungroup_tbl[128][3];
48 967d397a Justin Ruggles
49
static int16_t l3_quantizers_1[32];
50
static int16_t l3_quantizers_2[32];
51
static int16_t l3_quantizers_3[32];
52
53
static int16_t l5_quantizers_1[128];
54
static int16_t l5_quantizers_2[128];
55
static int16_t l5_quantizers_3[128];
56
57
static int16_t l7_quantizers[7];
58
59
static int16_t l11_quantizers_1[128];
60
static int16_t l11_quantizers_2[128];
61
62
static int16_t l15_quantizers[15];
63
64
static const uint8_t qntztab[16] = { 0, 5, 7, 3, 7, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16 };
65
66
/* Adjustmens in dB gain */
67
#define LEVEL_MINUS_3DB         0.7071067811865476
68
#define LEVEL_MINUS_4POINT5DB   0.5946035575013605
69
#define LEVEL_MINUS_6DB         0.5000000000000000
70
#define LEVEL_PLUS_3DB          1.4142135623730951
71
#define LEVEL_PLUS_6DB          2.0000000000000000
72
#define LEVEL_ZERO              0.0000000000000000
73
74
static const float clevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB,
75
    LEVEL_MINUS_6DB, LEVEL_MINUS_4POINT5DB };
76
77
static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB };
78
79 7bfd22f2 Justin Ruggles
#define AC3_OUTPUT_LFEON  8
80 1b293437 Justin Ruggles
81 98a27a8a Justin Ruggles
typedef struct {
82 e9a38248 Justin Ruggles
    int acmod;
83
    int cmixlev;
84
    int surmixlev;
85
    int dsurmod;
86
87
    int blksw[AC3_MAX_CHANNELS];
88
    int dithflag[AC3_MAX_CHANNELS];
89
    int cplinu;
90
    int chincpl[AC3_MAX_CHANNELS];
91
    int phsflginu;
92
    int cplbegf;
93
    int cplendf;
94
    int cplcoe;
95 98a27a8a Justin Ruggles
    uint32_t cplbndstrc;
96 e9a38248 Justin Ruggles
    int rematstr;
97
    int rematflg[AC3_MAX_CHANNELS];
98
    int cplexpstr;
99
    int lfeexpstr;
100
    int chexpstr[5];
101 4611b64e Justin Ruggles
    int cplsnroffst;
102
    int cplfgain;
103
    int snroffst[5];
104
    int fgain[5];
105
    int lfesnroffst;
106
    int lfefgain;
107 e9a38248 Justin Ruggles
    int cpldeltbae;
108
    int deltbae[5];
109
    int cpldeltnseg;
110 98a27a8a Justin Ruggles
    uint8_t  cpldeltoffst[8];
111
    uint8_t  cpldeltlen[8];
112
    uint8_t  cpldeltba[8];
113 878c40a1 Justin Ruggles
    int deltnseg[5];
114 98a27a8a Justin Ruggles
    uint8_t  deltoffst[5][8];
115
    uint8_t  deltlen[5][8];
116
    uint8_t  deltba[5][8];
117
118
    /* Derived Attributes. */
119
    int      sampling_rate;
120
    int      bit_rate;
121
    int      frame_size;
122
123 9fc1ab72 Justin Ruggles
    int      nchans;            //number of total channels
124
    int      nfchans;           //number of full-bandwidth channels
125 c7cfc48f Justin Ruggles
    int      lfeon;             //lfe channel in use
126 7bfd22f2 Justin Ruggles
    int      output_mode;       ///< output channel configuration
127
    int      out_channels;      ///< number of output channels
128 c7cfc48f Justin Ruggles
129
    float    dynrng;            //dynamic range gain
130
    float    dynrng2;           //dynamic range gain for 1+1 mode
131
    float    cplco[5][18];      //coupling coordinates
132
    int      ncplbnd;           //number of coupling bands
133
    int      ncplsubnd;         //number of coupling sub bands
134
    int      cplstrtmant;       //coupling start mantissa
135
    int      cplendmant;        //coupling end mantissa
136
    int      endmant[5];        //channel end mantissas
137 623b7943 Justin Ruggles
    AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
138 c7cfc48f Justin Ruggles
139 bc8edb7e Justin Ruggles
    int8_t   dcplexps[256];     //decoded coupling exponents
140
    int8_t   dexps[5][256];     //decoded fbw channel exponents
141
    int8_t   dlfeexps[256];     //decoded lfe channel exponents
142 c7cfc48f Justin Ruggles
    uint8_t  cplbap[256];       //coupling bit allocation pointers
143
    uint8_t  bap[5][256];       //fbw channel bit allocation pointers
144
    uint8_t  lfebap[256];       //lfe channel bit allocation pointers
145
146 8fbb368d Justin Ruggles
    DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][256]);  //transform coefficients
147 98a27a8a Justin Ruggles
148
    /* For IMDCT. */
149 c7cfc48f Justin Ruggles
    MDCTContext imdct_512;  //for 512 sample imdct transform
150
    MDCTContext imdct_256;  //for 256 sample imdct transform
151 1d0a6f52 Justin Ruggles
    DSPContext  dsp;        //for optimization
152 c7cfc48f Justin Ruggles
153 8fbb368d Justin Ruggles
    DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS][256]);   //output after imdct transform and windowing
154
    DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS][256]);    //delay - added to the next block
155
    DECLARE_ALIGNED_16(float, tmp_imdct[256]);                  //temporary storage for imdct transform
156
    DECLARE_ALIGNED_16(float, tmp_output[512]);                 //temporary storage for output before windowing
157
    DECLARE_ALIGNED_16(float, window[256]);                     //window coefficients
158 98a27a8a Justin Ruggles
159
    /* Miscellaneous. */
160
    GetBitContext gb;
161 cb503702 Justin Ruggles
    AVRandomState dith_state;   //for dither generation
162 98a27a8a Justin Ruggles
} AC3DecodeContext;
163
164 c7cfc48f Justin Ruggles
/*********** BEGIN INIT HELPER FUNCTIONS ***********/
165 2fbbd087 Justin Ruggles
/**
166
 * Generate a Kaiser-Bessel Derived Window.
167
 */
168 860fe8c9 Justin Ruggles
static void ac3_window_init(float *window)
169 2fbbd087 Justin Ruggles
{
170 6dc5d71f Justin Ruggles
   int i, j;
171
   double sum = 0.0, bessel, tmp;
172
   double local_window[256];
173
   double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
174
175
   for (i = 0; i < 256; i++) {
176
       tmp = i * (256 - i) * alpha2;
177
       bessel = 1.0;
178
       for (j = 100; j > 0; j--) /* defaul to 100 iterations */
179
           bessel = bessel * tmp / (j * j) + 1;
180
       sum += bessel;
181
       local_window[i] = sum;
182
   }
183
184
   sum++;
185
   for (i = 0; i < 256; i++)
186
       window[i] = sqrt(local_window[i] / sum);
187 2fbbd087 Justin Ruggles
}
188
189 c7cfc48f Justin Ruggles
/*
190
 * Generate quantizer tables.
191
 */
192 98a27a8a Justin Ruggles
static void generate_quantizers_table(int16_t quantizers[], int level, int length)
193
{
194
    int i;
195
196
    for (i = 0; i < length; i++)
197
        quantizers[i] = ((2 * i - level + 1) << 15) / level;
198
}
199
200
static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
201
{
202
    int i, j;
203
    int16_t v;
204
205
    for (i = 0; i < length1; i++) {
206
        v = ((2 * i - level + 1) << 15) / level;
207
        for (j = 0; j < length2; j++)
208
            quantizers[i * length2 + j] = v;
209
    }
210
211
    for (i = length1 * length2; i < size; i++)
212
        quantizers[i] = 0;
213
}
214
215
static void generate_quantizers_table_2(int16_t quantizers[], int level, int length1, int length2, int size)
216
{
217
    int i, j;
218
    int16_t v;
219
220
    for (i = 0; i < length1; i++) {
221
        v = ((2 * (i % level) - level + 1) << 15) / level;
222
        for (j = 0; j < length2; j++)
223
            quantizers[i * length2 + j] = v;
224
    }
225
226
    for (i = length1 * length2; i < size; i++)
227
        quantizers[i] = 0;
228
229
}
230
231
static void generate_quantizers_table_3(int16_t quantizers[], int level, int length1, int length2, int size)
232
{
233
    int i, j;
234
235
    for (i = 0; i < length1; i++)
236
        for (j = 0; j < length2; j++)
237
            quantizers[i * length2 + j] = ((2 * (j % level) - level + 1) << 15) / level;
238 1b293437 Justin Ruggles
239 98a27a8a Justin Ruggles
    for (i = length1 * length2; i < size; i++)
240
        quantizers[i] = 0;
241
}
242
243 c7cfc48f Justin Ruggles
/*
244
 * Initialize tables at runtime.
245
 */
246 98a27a8a Justin Ruggles
static void ac3_tables_init(void)
247 00585845 Justin Ruggles
{
248 4415076f Justin Ruggles
    int i;
249 98a27a8a Justin Ruggles
250
    /* Quantizer ungrouping tables. */
251
    // for level-3 quantizers
252
    generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
253
    generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
254
    generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
255
256
    //for level-5 quantizers
257
    generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
258
    generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
259
    generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
260
261
    //for level-7 quantizers
262
    generate_quantizers_table(l7_quantizers, 7, 7);
263
264
    //for level-4 quantizers
265
    generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
266
    generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
267
268
    //for level-15 quantizers
269
    generate_quantizers_table(l15_quantizers, 15, 15);
270 c7cfc48f Justin Ruggles
    /* End Quantizer ungrouping tables. */
271
272
    //generate scale factors
273
    for (i = 0; i < 25; i++)
274
        scale_factors[i] = pow(2.0, -(i + 15));
275 4415076f Justin Ruggles
276
    /* generate exponent tables
277
       reference: Section 7.1.3 Exponent Decoding */
278
    for(i=0; i<128; i++) {
279
        exp_ungroup_tbl[i][0] =  i / 25;
280
        exp_ungroup_tbl[i][1] = (i % 25) / 5;
281
        exp_ungroup_tbl[i][2] = (i % 25) % 5;
282
    }
283 00585845 Justin Ruggles
}
284
285 1b293437 Justin Ruggles
286
static int ac3_decode_init(AVCodecContext *avctx)
287
{
288
    AC3DecodeContext *ctx = avctx->priv_data;
289
290 cc2a8443 Justin Ruggles
    ac3_common_init();
291 98a27a8a Justin Ruggles
    ac3_tables_init();
292 45b0ed13 Justin Ruggles
    ff_mdct_init(&ctx->imdct_256, 8, 1);
293
    ff_mdct_init(&ctx->imdct_512, 9, 1);
294 6dc5d71f Justin Ruggles
    ac3_window_init(ctx->window);
295 1d0a6f52 Justin Ruggles
    dsputil_init(&ctx->dsp, avctx);
296 cb503702 Justin Ruggles
    av_init_random(0, &ctx->dith_state);
297 2aa2c5c4 Justin Ruggles
298 1b293437 Justin Ruggles
    return 0;
299 2aa2c5c4 Justin Ruggles
}
300 c7cfc48f Justin Ruggles
/*********** END INIT FUNCTIONS ***********/
301 2aa2c5c4 Justin Ruggles
302 9fc1ab72 Justin Ruggles
/**
303
 * Parses the 'sync info' and 'bit stream info' from the AC-3 bitstream.
304 c7cfc48f Justin Ruggles
 * GetBitContext within AC3DecodeContext must point to
305
 * start of the synchronized ac3 bitstream.
306
 */
307 9fc1ab72 Justin Ruggles
static int ac3_parse_header(AC3DecodeContext *ctx)
308 2aa2c5c4 Justin Ruggles
{
309 9fc1ab72 Justin Ruggles
    AC3HeaderInfo hdr;
310 1b293437 Justin Ruggles
    GetBitContext *gb = &ctx->gb;
311 9fc1ab72 Justin Ruggles
    int err, i;
312
313
    err = ff_ac3_parse_header(gb->buffer, &hdr);
314
    if(err)
315
        return err;
316
317
    /* get decoding parameters from header info */
318
    ctx->bit_alloc_params.fscod       = hdr.fscod;
319
    ctx->acmod                        = hdr.acmod;
320
    ctx->cmixlev                      = hdr.cmixlev;
321
    ctx->surmixlev                    = hdr.surmixlev;
322
    ctx->dsurmod                      = hdr.dsurmod;
323
    ctx->lfeon                        = hdr.lfeon;
324
    ctx->bit_alloc_params.halfratecod = hdr.halfratecod;
325
    ctx->sampling_rate                = hdr.sample_rate;
326
    ctx->bit_rate                     = hdr.bit_rate;
327
    ctx->nchans                       = hdr.channels;
328
    ctx->nfchans                      = ctx->nchans - ctx->lfeon;
329
    ctx->frame_size                   = hdr.frame_size;
330 7bfd22f2 Justin Ruggles
331
    /* set default output to all source channels */
332
    ctx->out_channels = ctx->nchans;
333
    ctx->output_mode = ctx->acmod;
334 9fc1ab72 Justin Ruggles
    if(ctx->lfeon)
335 7bfd22f2 Justin Ruggles
        ctx->output_mode |= AC3_OUTPUT_LFEON;
336 9fc1ab72 Justin Ruggles
337
    /* skip over portion of header which has already been read */
338 98a27a8a Justin Ruggles
    skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
339 9fc1ab72 Justin Ruggles
    skip_bits(gb, 16); // skip crc1
340
    skip_bits(gb, 8);  // skip fscod and frmsizecod
341
    skip_bits(gb, 11); // skip bsid, bsmod, and acmod
342
    if(ctx->acmod == AC3_ACMOD_STEREO) {
343
        skip_bits(gb, 2); // skip dsurmod
344
    } else {
345
        if((ctx->acmod & 1) && ctx->acmod != AC3_ACMOD_MONO)
346
            skip_bits(gb, 2); // skip cmixlev
347
        if(ctx->acmod & 4)
348
            skip_bits(gb, 2); // skip surmixlev
349 00585845 Justin Ruggles
    }
350 9fc1ab72 Justin Ruggles
    skip_bits1(gb); // skip lfeon
351 98a27a8a Justin Ruggles
352 9fc1ab72 Justin Ruggles
    /* read the rest of the bsi. read twice for dual mono mode. */
353 98a27a8a Justin Ruggles
    i = !(ctx->acmod);
354
    do {
355
        skip_bits(gb, 5); //skip dialog normalization
356
        if (get_bits1(gb))
357
            skip_bits(gb, 8); //skip compression
358
        if (get_bits1(gb))
359
            skip_bits(gb, 8); //skip language code
360
        if (get_bits1(gb))
361
            skip_bits(gb, 7); //skip audio production information
362
    } while (i--);
363
364
    skip_bits(gb, 2); //skip copyright bit and original bitstream bit
365
366 9fc1ab72 Justin Ruggles
    /* FIXME: read & use the xbsi1 downmix levels */
367 00585845 Justin Ruggles
    if (get_bits1(gb))
368 98a27a8a Justin Ruggles
        skip_bits(gb, 14); //skip timecode1
369 00585845 Justin Ruggles
    if (get_bits1(gb))
370 98a27a8a Justin Ruggles
        skip_bits(gb, 14); //skip timecode2
371
372 00585845 Justin Ruggles
    if (get_bits1(gb)) {
373 98a27a8a Justin Ruggles
        i = get_bits(gb, 6); //additional bsi length
374 486637af Justin Ruggles
        do {
375 00585845 Justin Ruggles
            skip_bits(gb, 8);
376 98a27a8a Justin Ruggles
        } while(i--);
377 1b293437 Justin Ruggles
    }
378 9fc1ab72 Justin Ruggles
379
    return 0;
380 2aa2c5c4 Justin Ruggles
}
381
382 3bb004fc Justin Ruggles
/**
383
 * Decodes the grouped exponents.
384 c7cfc48f Justin Ruggles
 * This function decodes the coded exponents according to exponent strategy
385
 * and stores them in the decoded exponents buffer.
386
 *
387 3bb004fc Justin Ruggles
 * @param[in]  gb      GetBitContext which points to start of coded exponents
388
 * @param[in]  expstr  Exponent coding strategy
389
 * @param[in]  ngrps   Number of grouped exponents
390
 * @param[in]  absexp  Absolute exponent or DC exponent
391
 * @param[out] dexps   Decoded exponents are stored in dexps
392 2aa2c5c4 Justin Ruggles
 */
393 4415076f Justin Ruggles
static void decode_exponents(GetBitContext *gb, int expstr, int ngrps,
394 bc8edb7e Justin Ruggles
                             uint8_t absexp, int8_t *dexps)
395 2aa2c5c4 Justin Ruggles
{
396 4415076f Justin Ruggles
    int i, j, grp, grpsize;
397
    int dexp[256];
398
    int expacc, prevexp;
399
400
    /* unpack groups */
401
    grpsize = expstr + (expstr == EXP_D45);
402
    for(grp=0,i=0; grp<ngrps; grp++) {
403
        expacc = get_bits(gb, 7);
404
        dexp[i++] = exp_ungroup_tbl[expacc][0];
405
        dexp[i++] = exp_ungroup_tbl[expacc][1];
406
        dexp[i++] = exp_ungroup_tbl[expacc][2];
407
    }
408 2aa2c5c4 Justin Ruggles
409 4415076f Justin Ruggles
    /* convert to absolute exps and expand groups */
410
    prevexp = absexp;
411
    for(i=0; i<ngrps*3; i++) {
412
        prevexp = av_clip(prevexp + dexp[i]-2, 0, 24);
413
        for(j=0; j<grpsize; j++) {
414
            dexps[(i*grpsize)+j] = prevexp;
415 1b293437 Justin Ruggles
        }
416 2aa2c5c4 Justin Ruggles
    }
417
}
418
419 486637af Justin Ruggles
typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
420 98a27a8a Justin Ruggles
    int16_t l3_quantizers[3];
421
    int16_t l5_quantizers[3];
422
    int16_t l11_quantizers[2];
423 486637af Justin Ruggles
    int l3ptr;
424
    int l5ptr;
425
    int l11ptr;
426
} mant_groups;
427
428
/* Get the transform coefficients for coupling channel and uncouple channels.
429
 * The coupling transform coefficients starts at the the cplstrtmant, which is
430
 * equal to endmant[ch] for fbw channels. Hence we can uncouple channels before
431
 * getting transform coefficients for the channel.
432
 */
433
static int get_transform_coeffs_cpling(AC3DecodeContext *ctx, mant_groups *m)
434 1b293437 Justin Ruggles
{
435 486637af Justin Ruggles
    GetBitContext *gb = &ctx->gb;
436 2fbbd087 Justin Ruggles
    int ch, start, end, cplbndstrc, bnd, gcode, tbap;
437 486637af Justin Ruggles
    float cplcos[5], cplcoeff;
438 98a27a8a Justin Ruggles
    uint8_t *exps = ctx->dcplexps;
439
    uint8_t *bap = ctx->cplbap;
440
441
    cplbndstrc = ctx->cplbndstrc;
442
    start = ctx->cplstrtmant;
443
    bnd = 0;
444
445
    while (start < ctx->cplendmant) {
446
        end = start + 12;
447
        while (cplbndstrc & 1) {
448
            end += 12;
449
            cplbndstrc >>= 1;
450
        }
451
        cplbndstrc >>= 1;
452
        for (ch = 0; ch < ctx->nfchans; ch++)
453 7bfd22f2 Justin Ruggles
            cplcos[ch] = ctx->cplco[ch][bnd];
454 98a27a8a Justin Ruggles
        bnd++;
455
456
        while (start < end) {
457
            tbap = bap[start];
458
            switch(tbap) {
459 486637af Justin Ruggles
                case 0:
460 2fbbd087 Justin Ruggles
                    for (ch = 0; ch < ctx->nfchans; ch++)
461 878c40a1 Justin Ruggles
                        if (ctx->chincpl[ch]) {
462
                            if (ctx->dithflag[ch]) {
463 de34875c Justin Ruggles
                                cplcoeff = (av_random(&ctx->dith_state) & 0xFFFF) * scale_factors[exps[start]];
464 45b0ed13 Justin Ruggles
                                ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch] * LEVEL_MINUS_3DB;
465 486637af Justin Ruggles
                            } else
466 98a27a8a Justin Ruggles
                                ctx->transform_coeffs[ch + 1][start] = 0;
467 486637af Justin Ruggles
                        }
468 98a27a8a Justin Ruggles
                    start++;
469 486637af Justin Ruggles
                    continue;
470
                case 1:
471
                    if (m->l3ptr > 2) {
472
                        gcode = get_bits(gb, 5);
473 98a27a8a Justin Ruggles
                        m->l3_quantizers[0] = l3_quantizers_1[gcode];
474
                        m->l3_quantizers[1] = l3_quantizers_2[gcode];
475
                        m->l3_quantizers[2] = l3_quantizers_3[gcode];
476 486637af Justin Ruggles
                        m->l3ptr = 0;
477
                    }
478 de34875c Justin Ruggles
                    cplcoeff = m->l3_quantizers[m->l3ptr++] * scale_factors[exps[start]];
479 486637af Justin Ruggles
                    break;
480
481
                case 2:
482
                    if (m->l5ptr > 2) {
483
                        gcode = get_bits(gb, 7);
484 98a27a8a Justin Ruggles
                        m->l5_quantizers[0] = l5_quantizers_1[gcode];
485
                        m->l5_quantizers[1] = l5_quantizers_2[gcode];
486
                        m->l5_quantizers[2] = l5_quantizers_3[gcode];
487 486637af Justin Ruggles
                        m->l5ptr = 0;
488
                    }
489 de34875c Justin Ruggles
                    cplcoeff = m->l5_quantizers[m->l5ptr++] * scale_factors[exps[start]];
490 486637af Justin Ruggles
                    break;
491
492
                case 3:
493 de34875c Justin Ruggles
                    cplcoeff = l7_quantizers[get_bits(gb, 3)] * scale_factors[exps[start]];
494 486637af Justin Ruggles
                    break;
495
496
                case 4:
497
                    if (m->l11ptr > 1) {
498
                        gcode = get_bits(gb, 7);
499 98a27a8a Justin Ruggles
                        m->l11_quantizers[0] = l11_quantizers_1[gcode];
500
                        m->l11_quantizers[1] = l11_quantizers_2[gcode];
501 486637af Justin Ruggles
                        m->l11ptr = 0;
502
                    }
503 de34875c Justin Ruggles
                    cplcoeff = m->l11_quantizers[m->l11ptr++] * scale_factors[exps[start]];
504 486637af Justin Ruggles
                    break;
505
506
                case 5:
507 de34875c Justin Ruggles
                    cplcoeff = l15_quantizers[get_bits(gb, 4)] * scale_factors[exps[start]];
508 486637af Justin Ruggles
                    break;
509
510
                default:
511 de34875c Justin Ruggles
                    cplcoeff = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * scale_factors[exps[start]];
512 486637af Justin Ruggles
            }
513 98a27a8a Justin Ruggles
            for (ch = 0; ch < ctx->nfchans; ch++)
514 878c40a1 Justin Ruggles
                if (ctx->chincpl[ch])
515 2fbbd087 Justin Ruggles
                    ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
516 98a27a8a Justin Ruggles
            start++;
517 486637af Justin Ruggles
        }
518
    }
519
520
    return 0;
521
}
522 1b293437 Justin Ruggles
523
/* Get the transform coefficients for particular channel */
524 2fbbd087 Justin Ruggles
static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
525 1b293437 Justin Ruggles
{
526 2fbbd087 Justin Ruggles
    GetBitContext *gb = &ctx->gb;
527
    int i, gcode, tbap, dithflag, end;
528
    uint8_t *exps;
529
    uint8_t *bap;
530
    float *coeffs;
531
532
    if (ch_index != -1) { /* fbw channels */
533 878c40a1 Justin Ruggles
        dithflag = ctx->dithflag[ch_index];
534 2fbbd087 Justin Ruggles
        exps = ctx->dexps[ch_index];
535
        bap = ctx->bap[ch_index];
536
        coeffs = ctx->transform_coeffs[ch_index + 1];
537
        end = ctx->endmant[ch_index];
538
    } else if (ch_index == -1) {
539
        dithflag = 0;
540
        exps = ctx->dlfeexps;
541
        bap = ctx->lfebap;
542
        coeffs = ctx->transform_coeffs[0];
543
        end = 7;
544
    }
545 1b293437 Justin Ruggles
546 2fbbd087 Justin Ruggles
547
    for (i = 0; i < end; i++) {
548 98a27a8a Justin Ruggles
        tbap = bap[i];
549
        switch (tbap) {
550 1b293437 Justin Ruggles
            case 0:
551 45b0ed13 Justin Ruggles
                if (!dithflag) {
552 00585845 Justin Ruggles
                    coeffs[i] = 0;
553
                    continue;
554
                }
555
                else {
556 7bfd22f2 Justin Ruggles
                    coeffs[i] = (av_random(&ctx->dith_state) & 0xFFFF) * scale_factors[exps[i]];
557 45b0ed13 Justin Ruggles
                    coeffs[i] *= LEVEL_MINUS_3DB;
558 00585845 Justin Ruggles
                    continue;
559
                }
560 1b293437 Justin Ruggles
561
            case 1:
562 486637af Justin Ruggles
                if (m->l3ptr > 2) {
563 00585845 Justin Ruggles
                    gcode = get_bits(gb, 5);
564 98a27a8a Justin Ruggles
                    m->l3_quantizers[0] = l3_quantizers_1[gcode];
565
                    m->l3_quantizers[1] = l3_quantizers_2[gcode];
566
                    m->l3_quantizers[2] = l3_quantizers_3[gcode];
567 486637af Justin Ruggles
                    m->l3ptr = 0;
568 1b293437 Justin Ruggles
                }
569 7bfd22f2 Justin Ruggles
                coeffs[i] = m->l3_quantizers[m->l3ptr++] * scale_factors[exps[i]];
570 00585845 Justin Ruggles
                continue;
571 1b293437 Justin Ruggles
572
            case 2:
573 486637af Justin Ruggles
                if (m->l5ptr > 2) {
574 00585845 Justin Ruggles
                    gcode = get_bits(gb, 7);
575 98a27a8a Justin Ruggles
                    m->l5_quantizers[0] = l5_quantizers_1[gcode];
576
                    m->l5_quantizers[1] = l5_quantizers_2[gcode];
577
                    m->l5_quantizers[2] = l5_quantizers_3[gcode];
578 486637af Justin Ruggles
                    m->l5ptr = 0;
579 1b293437 Justin Ruggles
                }
580 7bfd22f2 Justin Ruggles
                coeffs[i] = m->l5_quantizers[m->l5ptr++] * scale_factors[exps[i]];
581 00585845 Justin Ruggles
                continue;
582 1b293437 Justin Ruggles
583
            case 3:
584 7bfd22f2 Justin Ruggles
                coeffs[i] = l7_quantizers[get_bits(gb, 3)] * scale_factors[exps[i]];
585 00585845 Justin Ruggles
                continue;
586 1b293437 Justin Ruggles
587
            case 4:
588 486637af Justin Ruggles
                if (m->l11ptr > 1) {
589 00585845 Justin Ruggles
                    gcode = get_bits(gb, 7);
590 98a27a8a Justin Ruggles
                    m->l11_quantizers[0] = l11_quantizers_1[gcode];
591
                    m->l11_quantizers[1] = l11_quantizers_2[gcode];
592 486637af Justin Ruggles
                    m->l11ptr = 0;
593 1b293437 Justin Ruggles
                }
594 7bfd22f2 Justin Ruggles
                coeffs[i] = m->l11_quantizers[m->l11ptr++] * scale_factors[exps[i]];
595 00585845 Justin Ruggles
                continue;
596 1b293437 Justin Ruggles
597
            case 5:
598 7bfd22f2 Justin Ruggles
                coeffs[i] = l15_quantizers[get_bits(gb, 4)] * scale_factors[exps[i]];
599 00585845 Justin Ruggles
                continue;
600 1b293437 Justin Ruggles
601
            default:
602 7bfd22f2 Justin Ruggles
                coeffs[i] = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * scale_factors[exps[i]];
603 00585845 Justin Ruggles
                continue;
604 1b293437 Justin Ruggles
        }
605
    }
606
607
    return 0;
608
}
609
610 c7cfc48f Justin Ruggles
/* Get the transform coefficients.
611
 * This function extracts the tranform coefficients form the ac3 bitstream.
612
 * This function is called after bit allocation is performed.
613
 */
614 1b293437 Justin Ruggles
static int get_transform_coeffs(AC3DecodeContext * ctx)
615
{
616 486637af Justin Ruggles
    int i, end;
617 1b293437 Justin Ruggles
    int got_cplchan = 0;
618 486637af Justin Ruggles
    mant_groups m;
619
620
    m.l3ptr = m.l5ptr = m.l11ptr = 3;
621 1b293437 Justin Ruggles
622 98a27a8a Justin Ruggles
    for (i = 0; i < ctx->nfchans; i++) {
623 1b293437 Justin Ruggles
        /* transform coefficients for individual channel */
624 2fbbd087 Justin Ruggles
        if (get_transform_coeffs_ch(ctx, i, &m))
625 1b293437 Justin Ruggles
            return -1;
626
        /* tranform coefficients for coupling channels */
627 878c40a1 Justin Ruggles
        if (ctx->chincpl[i])  {
628 486637af Justin Ruggles
            if (!got_cplchan) {
629 98a27a8a Justin Ruggles
                if (get_transform_coeffs_cpling(ctx, &m)) {
630
                    av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
631 486637af Justin Ruggles
                    return -1;
632 98a27a8a Justin Ruggles
                }
633 486637af Justin Ruggles
                got_cplchan = 1;
634
            }
635 98a27a8a Justin Ruggles
            end = ctx->cplendmant;
636 486637af Justin Ruggles
        } else
637 98a27a8a Justin Ruggles
            end = ctx->endmant[i];
638 486637af Justin Ruggles
        do
639 98a27a8a Justin Ruggles
            ctx->transform_coeffs[i + 1][end] = 0;
640 486637af Justin Ruggles
        while(++end < 256);
641
    }
642 98a27a8a Justin Ruggles
    if (ctx->lfeon) {
643 2fbbd087 Justin Ruggles
        if (get_transform_coeffs_ch(ctx, -1, &m))
644 1b293437 Justin Ruggles
                return -1;
645 486637af Justin Ruggles
        for (i = 7; i < 256; i++) {
646 98a27a8a Justin Ruggles
            ctx->transform_coeffs[0][i] = 0;
647 1b293437 Justin Ruggles
        }
648
    }
649
650
    return 0;
651 2aa2c5c4 Justin Ruggles
}
652
653 c7cfc48f Justin Ruggles
/* Rematrixing routines. */
654 00585845 Justin Ruggles
static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
655 1b293437 Justin Ruggles
{
656
    float tmp0, tmp1;
657
658
    while (start < end) {
659 98a27a8a Justin Ruggles
        tmp0 = ctx->transform_coeffs[1][start];
660
        tmp1 = ctx->transform_coeffs[2][start];
661
        ctx->transform_coeffs[1][start] = tmp0 + tmp1;
662
        ctx->transform_coeffs[2][start] = tmp0 - tmp1;
663 1b293437 Justin Ruggles
        start++;
664
    }
665
}
666 2aa2c5c4 Justin Ruggles
667 1b293437 Justin Ruggles
static void do_rematrixing(AC3DecodeContext *ctx)
668
{
669 2fbbd087 Justin Ruggles
    int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
670
    int end, bndend;
671
672
    end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
673 1b293437 Justin Ruggles
674 878c40a1 Justin Ruggles
    if (ctx->rematflg[0])
675 00585845 Justin Ruggles
        do_rematrixing1(ctx, bnd1, bnd2);
676 2fbbd087 Justin Ruggles
677 878c40a1 Justin Ruggles
    if (ctx->rematflg[1])
678 00585845 Justin Ruggles
        do_rematrixing1(ctx, bnd2, bnd3);
679 2fbbd087 Justin Ruggles
680
    bndend = bnd4;
681
    if (bndend > end) {
682
        bndend = end;
683 878c40a1 Justin Ruggles
        if (ctx->rematflg[2])
684 00585845 Justin Ruggles
            do_rematrixing1(ctx, bnd3, bndend);
685 2fbbd087 Justin Ruggles
    } else {
686 878c40a1 Justin Ruggles
        if (ctx->rematflg[2])
687 00585845 Justin Ruggles
            do_rematrixing1(ctx, bnd3, bnd4);
688 878c40a1 Justin Ruggles
        if (ctx->rematflg[3])
689 2fbbd087 Justin Ruggles
            do_rematrixing1(ctx, bnd4, end);
690 1b293437 Justin Ruggles
    }
691
}
692 2aa2c5c4 Justin Ruggles
693 c7cfc48f Justin Ruggles
/* This function performs the imdct on 256 sample transform
694
 * coefficients.
695
 */
696 45b0ed13 Justin Ruggles
static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
697 486637af Justin Ruggles
{
698 0de73a46 Justin Ruggles
    int i, k;
699
    float x[128];
700
    FFTComplex z[2][64];
701
    float *o_ptr = ctx->tmp_output;
702
703
    for(i=0; i<2; i++) {
704
        /* de-interleave coefficients */
705
        for(k=0; k<128; k++) {
706
            x[k] = ctx->transform_coeffs[chindex][2*k+i];
707
        }
708 98a27a8a Justin Ruggles
709 0de73a46 Justin Ruggles
        /* run standard IMDCT */
710
        ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, o_ptr, x, ctx->tmp_imdct);
711
712
        /* reverse the post-rotation & reordering from standard IMDCT */
713
        for(k=0; k<32; k++) {
714
            z[i][32+k].re = -o_ptr[128+2*k];
715
            z[i][32+k].im = -o_ptr[2*k];
716
            z[i][31-k].re =  o_ptr[2*k+1];
717
            z[i][31-k].im =  o_ptr[128+2*k+1];
718
        }
719 1ea76064 Justin Ruggles
    }
720 486637af Justin Ruggles
721 0de73a46 Justin Ruggles
    /* apply AC-3 post-rotation & reordering */
722
    for(k=0; k<64; k++) {
723
        o_ptr[    2*k  ] = -z[0][   k].im;
724
        o_ptr[    2*k+1] =  z[0][63-k].re;
725
        o_ptr[128+2*k  ] = -z[0][   k].re;
726
        o_ptr[128+2*k+1] =  z[0][63-k].im;
727
        o_ptr[256+2*k  ] = -z[1][   k].re;
728
        o_ptr[256+2*k+1] =  z[1][63-k].im;
729
        o_ptr[384+2*k  ] =  z[1][   k].im;
730
        o_ptr[384+2*k+1] = -z[1][63-k].re;
731
    }
732 98a27a8a Justin Ruggles
}
733 486637af Justin Ruggles
734 c7cfc48f Justin Ruggles
/* IMDCT Transform. */
735 486637af Justin Ruggles
static inline void do_imdct(AC3DecodeContext *ctx)
736
{
737 0de73a46 Justin Ruggles
    int ch;
738 486637af Justin Ruggles
739 7bfd22f2 Justin Ruggles
    if (ctx->output_mode & AC3_OUTPUT_LFEON) {
740 0de73a46 Justin Ruggles
        ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
741
                                      ctx->transform_coeffs[0], ctx->tmp_imdct);
742 7bfd22f2 Justin Ruggles
        ctx->dsp.vector_fmul_add_add(ctx->output[0], ctx->tmp_output,
743
                                     ctx->window, ctx->delay[0], 384, 256, 1);
744
        ctx->dsp.vector_fmul_reverse(ctx->delay[0], ctx->tmp_output+256,
745
                                     ctx->window, 256);
746 486637af Justin Ruggles
    }
747 0de73a46 Justin Ruggles
    for (ch=1; ch<=ctx->nfchans; ch++) {
748 878c40a1 Justin Ruggles
        if (ctx->blksw[ch-1])
749 0de73a46 Justin Ruggles
            do_imdct_256(ctx, ch);
750 45b0ed13 Justin Ruggles
        else
751 0de73a46 Justin Ruggles
            ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
752
                                          ctx->transform_coeffs[ch],
753
                                          ctx->tmp_imdct);
754
755
        ctx->dsp.vector_fmul_add_add(ctx->output[ch], ctx->tmp_output,
756
                                     ctx->window, ctx->delay[ch], 384, 256, 1);
757
        ctx->dsp.vector_fmul_reverse(ctx->delay[ch], ctx->tmp_output+256,
758
                                     ctx->window, 256);
759 486637af Justin Ruggles
    }
760
}
761
762 c7cfc48f Justin Ruggles
/* Parse the audio block from ac3 bitstream.
763
 * This function extract the audio block from the ac3 bitstream
764
 * and produces the output for the block. This function must
765
 * be called for each of the six audio block in the ac3 bitstream.
766
 */
767 9fc1ab72 Justin Ruggles
static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
768 2aa2c5c4 Justin Ruggles
{
769 98a27a8a Justin Ruggles
    int nfchans = ctx->nfchans;
770
    int acmod = ctx->acmod;
771 7bfd22f2 Justin Ruggles
    int i, bnd, rbnd, seg, grpsize, ch;
772 1b293437 Justin Ruggles
    GetBitContext *gb = &ctx->gb;
773
    int bit_alloc_flags = 0;
774 bc8edb7e Justin Ruggles
    int8_t *dexps;
775 2fbbd087 Justin Ruggles
    int mstrcplco, cplcoexp, cplcomant;
776 98a27a8a Justin Ruggles
    int dynrng, chbwcod, ngrps, cplabsexp, skipl;
777 1b293437 Justin Ruggles
778
    for (i = 0; i < nfchans; i++) /*block switch flag */
779 878c40a1 Justin Ruggles
        ctx->blksw[i] = get_bits1(gb);
780 98a27a8a Justin Ruggles
781 1b293437 Justin Ruggles
    for (i = 0; i < nfchans; i++) /* dithering flag */
782 878c40a1 Justin Ruggles
        ctx->dithflag[i] = get_bits1(gb);
783 98a27a8a Justin Ruggles
784 00585845 Justin Ruggles
    if (get_bits1(gb)) { /* dynamic range */
785 45b0ed13 Justin Ruggles
        dynrng = get_sbits(gb, 8);
786 6dc5d71f Justin Ruggles
        ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
787 9fc1ab72 Justin Ruggles
    } else if(blk == 0) {
788
        ctx->dynrng = 1.0;
789 1b293437 Justin Ruggles
    }
790 98a27a8a Justin Ruggles
791 9fc1ab72 Justin Ruggles
    if(acmod == AC3_ACMOD_DUALMONO) { /* dynamic range 1+1 mode */
792
        if(get_bits1(gb)) {
793 bec37145 Justin Ruggles
            dynrng = get_sbits(gb, 8);
794
            ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
795 9fc1ab72 Justin Ruggles
        } else if(blk == 0) {
796
            ctx->dynrng2 = 1.0;
797
        }
798 98a27a8a Justin Ruggles
    }
799
800 00585845 Justin Ruggles
    if (get_bits1(gb)) { /* coupling strategy */
801 98a27a8a Justin Ruggles
        ctx->cplinu = get_bits1(gb);
802 2fbbd087 Justin Ruggles
        ctx->cplbndstrc = 0;
803 98a27a8a Justin Ruggles
        if (ctx->cplinu) { /* coupling in use */
804 1b293437 Justin Ruggles
            for (i = 0; i < nfchans; i++)
805 878c40a1 Justin Ruggles
                ctx->chincpl[i] = get_bits1(gb);
806 98a27a8a Justin Ruggles
807 e2cd6686 Justin Ruggles
            if (acmod == AC3_ACMOD_STEREO)
808 98a27a8a Justin Ruggles
                ctx->phsflginu = get_bits1(gb); //phase flag in use
809
810
            ctx->cplbegf = get_bits(gb, 4);
811
            ctx->cplendf = get_bits(gb, 4);
812
813
            if (3 + ctx->cplendf - ctx->cplbegf < 0) {
814
                av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", ctx->cplendf, ctx->cplbegf);
815 00585845 Justin Ruggles
                return -1;
816 98a27a8a Justin Ruggles
            }
817
818
            ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf;
819
            ctx->cplstrtmant = ctx->cplbegf * 12 + 37;
820
            ctx->cplendmant = ctx->cplendf * 12 + 73;
821
            for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
822 00585845 Justin Ruggles
                if (get_bits1(gb)) {
823 98a27a8a Justin Ruggles
                    ctx->cplbndstrc |= 1 << i;
824
                    ctx->ncplbnd--;
825 1b293437 Justin Ruggles
                }
826 878c40a1 Justin Ruggles
        } else {
827
            for (i = 0; i < nfchans; i++)
828
                ctx->chincpl[i] = 0;
829 1b293437 Justin Ruggles
        }
830
    }
831 98a27a8a Justin Ruggles
832
    if (ctx->cplinu) {
833
        ctx->cplcoe = 0;
834
835 1b293437 Justin Ruggles
        for (i = 0; i < nfchans; i++)
836 878c40a1 Justin Ruggles
            if (ctx->chincpl[i])
837 00585845 Justin Ruggles
                if (get_bits1(gb)) { /* coupling co-ordinates */
838 2fbbd087 Justin Ruggles
                    ctx->cplcoe |= 1 << i;
839 486637af Justin Ruggles
                    mstrcplco = 3 * get_bits(gb, 2);
840 98a27a8a Justin Ruggles
                    for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
841 486637af Justin Ruggles
                        cplcoexp = get_bits(gb, 4);
842
                        cplcomant = get_bits(gb, 4);
843
                        if (cplcoexp == 15)
844
                            cplcomant <<= 14;
845
                        else
846
                            cplcomant = (cplcomant | 0x10) << 13;
847 98a27a8a Justin Ruggles
                        ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
848 486637af Justin Ruggles
                    }
849
                }
850 98a27a8a Justin Ruggles
851 e2cd6686 Justin Ruggles
        if (acmod == AC3_ACMOD_STEREO && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
852 98a27a8a Justin Ruggles
            for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
853
                if (get_bits1(gb))
854
                    ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
855 2aa2c5c4 Justin Ruggles
    }
856 98a27a8a Justin Ruggles
857 e2cd6686 Justin Ruggles
    if (acmod == AC3_ACMOD_STEREO) {/* rematrixing */
858 98a27a8a Justin Ruggles
        ctx->rematstr = get_bits1(gb);
859
        if (ctx->rematstr) {
860
            if (!(ctx->cplinu) || ctx->cplbegf > 2)
861 1b293437 Justin Ruggles
                for (rbnd = 0; rbnd < 4; rbnd++)
862 878c40a1 Justin Ruggles
                    ctx->rematflg[rbnd] = get_bits1(gb);
863 98a27a8a Justin Ruggles
            if (ctx->cplbegf > 0 && ctx->cplbegf <= 2 && ctx->cplinu)
864 1b293437 Justin Ruggles
                for (rbnd = 0; rbnd < 3; rbnd++)
865 878c40a1 Justin Ruggles
                    ctx->rematflg[rbnd] = get_bits1(gb);
866 98a27a8a Justin Ruggles
            if (ctx->cplbegf == 0 && ctx->cplinu)
867 1b293437 Justin Ruggles
                for (rbnd = 0; rbnd < 2; rbnd++)
868 878c40a1 Justin Ruggles
                    ctx->rematflg[rbnd] = get_bits1(gb);
869 1b293437 Justin Ruggles
        }
870 98a27a8a Justin Ruggles
    }
871
872 cc2a8443 Justin Ruggles
    ctx->cplexpstr = EXP_REUSE;
873
    ctx->lfeexpstr = EXP_REUSE;
874 98a27a8a Justin Ruggles
    if (ctx->cplinu) /* coupling exponent strategy */
875
        ctx->cplexpstr = get_bits(gb, 2);
876
    for (i = 0; i < nfchans; i++)  /* channel exponent strategy */
877
        ctx->chexpstr[i] = get_bits(gb, 2);
878
    if (ctx->lfeon)  /* lfe exponent strategy */
879
        ctx->lfeexpstr = get_bits1(gb);
880
881 1b293437 Justin Ruggles
    for (i = 0; i < nfchans; i++) /* channel bandwidth code */
882 cc2a8443 Justin Ruggles
        if (ctx->chexpstr[i] != EXP_REUSE) {
883 878c40a1 Justin Ruggles
            if (ctx->chincpl[i])
884 98a27a8a Justin Ruggles
                ctx->endmant[i] = ctx->cplstrtmant;
885 00585845 Justin Ruggles
            else {
886 98a27a8a Justin Ruggles
                chbwcod = get_bits(gb, 6);
887
                if (chbwcod > 60) {
888
                    av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
889 00585845 Justin Ruggles
                    return -1;
890
                }
891 98a27a8a Justin Ruggles
                ctx->endmant[i] = chbwcod * 3 + 73;
892 1b293437 Justin Ruggles
            }
893 00585845 Justin Ruggles
        }
894 98a27a8a Justin Ruggles
895 cc2a8443 Justin Ruggles
    if (ctx->cplexpstr != EXP_REUSE) {/* coupling exponents */
896 2fbbd087 Justin Ruggles
        bit_alloc_flags = 64;
897 98a27a8a Justin Ruggles
        cplabsexp = get_bits(gb, 4) << 1;
898
        ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
899 4415076f Justin Ruggles
        decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant);
900 486637af Justin Ruggles
    }
901 98a27a8a Justin Ruggles
902 1b293437 Justin Ruggles
    for (i = 0; i < nfchans; i++) /* fbw channel exponents */
903 cc2a8443 Justin Ruggles
        if (ctx->chexpstr[i] != EXP_REUSE) {
904 1b293437 Justin Ruggles
            bit_alloc_flags |= 1 << i;
905 98a27a8a Justin Ruggles
            grpsize = 3 << (ctx->chexpstr[i] - 1);
906
            ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
907
            dexps = ctx->dexps[i];
908 00585845 Justin Ruggles
            dexps[0] = get_bits(gb, 4);
909 4415076f Justin Ruggles
            decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1);
910 98a27a8a Justin Ruggles
            skip_bits(gb, 2); /* skip gainrng */
911 1b293437 Justin Ruggles
        }
912 98a27a8a Justin Ruggles
913 cc2a8443 Justin Ruggles
    if (ctx->lfeexpstr != EXP_REUSE) { /* lfe exponents */
914 486637af Justin Ruggles
        bit_alloc_flags |= 32;
915 98a27a8a Justin Ruggles
        ctx->dlfeexps[0] = get_bits(gb, 4);
916 4415076f Justin Ruggles
        decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1);
917 486637af Justin Ruggles
    }
918 1b293437 Justin Ruggles
919 00585845 Justin Ruggles
    if (get_bits1(gb)) { /* bit allocation information */
920 2fbbd087 Justin Ruggles
        bit_alloc_flags = 127;
921 8f58a4c9 Justin Ruggles
        ctx->bit_alloc_params.sdecay = ff_sdecaytab[get_bits(gb, 2)];
922
        ctx->bit_alloc_params.fdecay = ff_fdecaytab[get_bits(gb, 2)];
923
        ctx->bit_alloc_params.sgain  = ff_sgaintab[get_bits(gb, 2)];
924
        ctx->bit_alloc_params.dbknee = ff_dbkneetab[get_bits(gb, 2)];
925
        ctx->bit_alloc_params.floor  = ff_floortab[get_bits(gb, 3)];
926 1b293437 Justin Ruggles
    }
927 98a27a8a Justin Ruggles
928 00585845 Justin Ruggles
    if (get_bits1(gb)) { /* snroffset */
929 4611b64e Justin Ruggles
        int csnr;
930 2fbbd087 Justin Ruggles
        bit_alloc_flags = 127;
931 4611b64e Justin Ruggles
        csnr = (get_bits(gb, 6) - 15) << 4;
932 de17eee7 Justin Ruggles
        if (ctx->cplinu) { /* coupling fine snr offset and fast gain code */
933 4611b64e Justin Ruggles
            ctx->cplsnroffst = (csnr + get_bits(gb, 4)) << 2;
934
            ctx->cplfgain = ff_fgaintab[get_bits(gb, 3)];
935 1b293437 Justin Ruggles
        }
936
        for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
937 4611b64e Justin Ruggles
            ctx->snroffst[i] = (csnr + get_bits(gb, 4)) << 2;
938
            ctx->fgain[i] = ff_fgaintab[get_bits(gb, 3)];
939 1b293437 Justin Ruggles
        }
940 98a27a8a Justin Ruggles
        if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
941 4611b64e Justin Ruggles
            ctx->lfesnroffst = (csnr + get_bits(gb, 4)) << 2;
942
            ctx->lfefgain = ff_fgaintab[get_bits(gb, 3)];
943 1b293437 Justin Ruggles
        }
944
    }
945 98a27a8a Justin Ruggles
946
    if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
947
        bit_alloc_flags |= 64;
948 8f58a4c9 Justin Ruggles
        ctx->bit_alloc_params.cplfleak = get_bits(gb, 3);
949
        ctx->bit_alloc_params.cplsleak = get_bits(gb, 3);
950 98a27a8a Justin Ruggles
    }
951
952 00585845 Justin Ruggles
    if (get_bits1(gb)) { /* delta bit allocation information */
953 2fbbd087 Justin Ruggles
        bit_alloc_flags = 127;
954 98a27a8a Justin Ruggles
955
        if (ctx->cplinu) {
956
            ctx->cpldeltbae = get_bits(gb, 2);
957 cc2a8443 Justin Ruggles
            if (ctx->cpldeltbae == DBA_RESERVED) {
958 1b293437 Justin Ruggles
                av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
959
                return -1;
960
            }
961
        }
962 98a27a8a Justin Ruggles
963 1b293437 Justin Ruggles
        for (i = 0; i < nfchans; i++) {
964 98a27a8a Justin Ruggles
            ctx->deltbae[i] = get_bits(gb, 2);
965 cc2a8443 Justin Ruggles
            if (ctx->deltbae[i] == DBA_RESERVED) {
966 1b293437 Justin Ruggles
                av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
967
                return -1;
968
            }
969
        }
970 98a27a8a Justin Ruggles
971
        if (ctx->cplinu)
972 cc2a8443 Justin Ruggles
            if (ctx->cpldeltbae == DBA_NEW) { /*coupling delta offset, len and bit allocation */
973 98a27a8a Justin Ruggles
                ctx->cpldeltnseg = get_bits(gb, 3);
974
                for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
975
                    ctx->cpldeltoffst[seg] = get_bits(gb, 5);
976
                    ctx->cpldeltlen[seg] = get_bits(gb, 4);
977
                    ctx->cpldeltba[seg] = get_bits(gb, 3);
978 1b293437 Justin Ruggles
                }
979
            }
980 98a27a8a Justin Ruggles
981 1b293437 Justin Ruggles
        for (i = 0; i < nfchans; i++)
982 cc2a8443 Justin Ruggles
            if (ctx->deltbae[i] == DBA_NEW) {/*channel delta offset, len and bit allocation */
983 98a27a8a Justin Ruggles
                ctx->deltnseg[i] = get_bits(gb, 3);
984
                for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
985
                    ctx->deltoffst[i][seg] = get_bits(gb, 5);
986
                    ctx->deltlen[i][seg] = get_bits(gb, 4);
987
                    ctx->deltba[i][seg] = get_bits(gb, 3);
988 1b293437 Justin Ruggles
                }
989
            }
990 9fc1ab72 Justin Ruggles
    } else if(blk == 0) {
991
        if(ctx->cplinu)
992
            ctx->cpldeltbae = DBA_NONE;
993
        for(i=0; i<nfchans; i++) {
994
            ctx->deltbae[i] = DBA_NONE;
995
        }
996 1b293437 Justin Ruggles
    }
997 00585845 Justin Ruggles
998 2fbbd087 Justin Ruggles
    if (bit_alloc_flags) {
999 878c40a1 Justin Ruggles
        if (ctx->cplinu && (bit_alloc_flags & 64))
1000 4611b64e Justin Ruggles
            ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->cplbap,
1001
                                          ctx->dcplexps, ctx->cplstrtmant,
1002
                                          ctx->cplendmant, ctx->cplsnroffst,
1003
                                          ctx->cplfgain, 0,
1004
                                          ctx->cpldeltbae, ctx->cpldeltnseg,
1005
                                          ctx->cpldeltoffst, ctx->cpldeltlen,
1006
                                          ctx->cpldeltba);
1007 d699d383 Justin Ruggles
        for (i = 0; i < nfchans; i++)
1008
            if ((bit_alloc_flags >> i) & 1)
1009 4611b64e Justin Ruggles
                ac3_parametric_bit_allocation(&ctx->bit_alloc_params,
1010
                                              ctx->bap[i], ctx->dexps[i], 0,
1011
                                              ctx->endmant[i], ctx->snroffst[i],
1012
                                              ctx->fgain[i], 0, ctx->deltbae[i],
1013
                                              ctx->deltnseg[i], ctx->deltoffst[i],
1014
                                              ctx->deltlen[i], ctx->deltba[i]);
1015 d699d383 Justin Ruggles
        if (ctx->lfeon && (bit_alloc_flags & 32))
1016 4611b64e Justin Ruggles
            ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->lfebap,
1017
                                          ctx->dlfeexps, 0, 7, ctx->lfesnroffst,
1018
                                          ctx->lfefgain, 1,
1019
                                          DBA_NONE, 0, NULL, NULL, NULL);
1020 2fbbd087 Justin Ruggles
    }
1021 98a27a8a Justin Ruggles
1022 00585845 Justin Ruggles
    if (get_bits1(gb)) { /* unused dummy data */
1023 98a27a8a Justin Ruggles
        skipl = get_bits(gb, 9);
1024
        while(skipl--)
1025 00585845 Justin Ruggles
            skip_bits(gb, 8);
1026 1b293437 Justin Ruggles
    }
1027
    /* unpack the transform coefficients
1028
     * * this also uncouples channels if coupling is in use.
1029
     */
1030
    if (get_transform_coeffs(ctx)) {
1031
        av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1032
        return -1;
1033
    }
1034 486637af Justin Ruggles
1035 1b293437 Justin Ruggles
    /* recover coefficients if rematrixing is in use */
1036 878c40a1 Justin Ruggles
    if(ctx->acmod == AC3_ACMOD_STEREO)
1037 1b293437 Justin Ruggles
        do_rematrixing(ctx);
1038
1039 7bfd22f2 Justin Ruggles
    /* apply scaling to coefficients (headroom, dynrng) */
1040
    if(ctx->lfeon) {
1041
        for(i=0; i<7; i++) {
1042
            ctx->transform_coeffs[0][i] *= 2.0f * ctx->dynrng;
1043
        }
1044
    }
1045
    for(ch=1; ch<=ctx->nfchans; ch++) {
1046
        float gain = 2.0f;
1047
        if(ctx->acmod == AC3_ACMOD_DUALMONO && ch == 2) {
1048
            gain *= ctx->dynrng2;
1049
        } else {
1050
            gain *= ctx->dynrng;
1051
        }
1052
        for(i=0; i<ctx->endmant[ch-1]; i++) {
1053
            ctx->transform_coeffs[ch][i] *= gain;
1054
        }
1055
    }
1056 d7bcc4ad Justin Ruggles
1057 486637af Justin Ruggles
    do_imdct(ctx);
1058
1059 1b293437 Justin Ruggles
    return 0;
1060
}
1061
1062 d7bcc4ad Justin Ruggles
static inline int16_t convert(int32_t i)
1063
{
1064
    if (i > 0x43c07fff)
1065
        return 32767;
1066
    else if (i <= 0x43bf8000)
1067
        return -32768;
1068
    else
1069
        return (i - 0x43c00000);
1070 486637af Justin Ruggles
}
1071
1072 c7cfc48f Justin Ruggles
/* Decode ac3 frame.
1073
 *
1074
 * @param avctx Pointer to AVCodecContext
1075
 * @param data Pointer to pcm smaples
1076
 * @param data_size Set to number of pcm samples produced by decoding
1077
 * @param buf Data to be decoded
1078
 * @param buf_size Size of the buffer
1079
 */
1080 00585845 Justin Ruggles
static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1081 1b293437 Justin Ruggles
{
1082 00585845 Justin Ruggles
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1083
    int16_t *out_samples = (int16_t *)data;
1084 d7bcc4ad Justin Ruggles
    int i, j, k, start;
1085
    int32_t *int_ptr[6];
1086 1b293437 Justin Ruggles
1087 d7bcc4ad Justin Ruggles
    for (i = 0; i < 6; i++)
1088
        int_ptr[i] = (int32_t *)(&ctx->output[i]);
1089
1090 1b293437 Justin Ruggles
    //Initialize the GetBitContext with the start of valid AC3 Frame.
1091 9ef60390 Justin Ruggles
    init_get_bits(&ctx->gb, buf, buf_size * 8);
1092 00585845 Justin Ruggles
1093 1b293437 Justin Ruggles
    //Parse the syncinfo.
1094 9fc1ab72 Justin Ruggles
    if (ac3_parse_header(ctx)) {
1095 00585845 Justin Ruggles
        av_log(avctx, AV_LOG_ERROR, "\n");
1096 1b293437 Justin Ruggles
        *data_size = 0;
1097 486637af Justin Ruggles
        return buf_size;
1098 1b293437 Justin Ruggles
    }
1099 2aa2c5c4 Justin Ruggles
1100 98a27a8a Justin Ruggles
    avctx->sample_rate = ctx->sampling_rate;
1101
    avctx->bit_rate = ctx->bit_rate;
1102 45b0ed13 Justin Ruggles
1103 7bfd22f2 Justin Ruggles
    /* channel config */
1104 1b293437 Justin Ruggles
    if (avctx->channels == 0) {
1105 7bfd22f2 Justin Ruggles
        avctx->channels = ctx->out_channels;
1106 45b0ed13 Justin Ruggles
    }
1107 7bfd22f2 Justin Ruggles
    if(avctx->channels != ctx->out_channels) {
1108
        av_log(avctx, AV_LOG_ERROR, "Cannot mix AC3 to %d channels.\n",
1109
               avctx->channels);
1110
        return -1;
1111 1b293437 Justin Ruggles
    }
1112
1113 d7bcc4ad Justin Ruggles
    //av_log(avctx, AV_LOG_INFO, "channels = %d \t bit rate = %d \t sampling rate = %d \n", avctx->channels, avctx->bit_rate * 1000, avctx->sample_rate);
1114 1b293437 Justin Ruggles
1115
    //Parse the Audio Blocks.
1116 cc2a8443 Justin Ruggles
    for (i = 0; i < NB_BLOCKS; i++) {
1117 9fc1ab72 Justin Ruggles
        if (ac3_parse_audio_block(ctx, i)) {
1118 1b293437 Justin Ruggles
            av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1119
            *data_size = 0;
1120 98a27a8a Justin Ruggles
            return ctx->frame_size;
1121 1b293437 Justin Ruggles
        }
1122 7bfd22f2 Justin Ruggles
        start = (ctx->output_mode & AC3_OUTPUT_LFEON) ? 0 : 1;
1123 8fbb368d Justin Ruggles
        for (k = 0; k < 256; k++)
1124 7bfd22f2 Justin Ruggles
            for (j = start; j <= ctx->nfchans; j++)
1125 d7bcc4ad Justin Ruggles
                *(out_samples++) = convert(int_ptr[j][k]);
1126 1b293437 Justin Ruggles
    }
1127 8fbb368d Justin Ruggles
    *data_size = NB_BLOCKS * 256 * avctx->channels * sizeof (int16_t);
1128 98a27a8a Justin Ruggles
    return ctx->frame_size;
1129 2aa2c5c4 Justin Ruggles
}
1130 1b293437 Justin Ruggles
1131 c7cfc48f Justin Ruggles
/* Uninitialize ac3 decoder.
1132
 */
1133
static int ac3_decode_end(AVCodecContext *avctx)
1134 1b293437 Justin Ruggles
{
1135 c7cfc48f Justin Ruggles
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1136
    ff_mdct_end(&ctx->imdct_512);
1137
    ff_mdct_end(&ctx->imdct_256);
1138
1139 1b293437 Justin Ruggles
    return 0;
1140
}
1141
1142 fa67992d Justin Ruggles
AVCodec ac3_decoder = {
1143 e6bca37c Justin Ruggles
    .name = "ac3",
1144
    .type = CODEC_TYPE_AUDIO,
1145
    .id = CODEC_ID_AC3,
1146
    .priv_data_size = sizeof (AC3DecodeContext),
1147
    .init = ac3_decode_init,
1148
    .close = ac3_decode_end,
1149
    .decode = ac3_decode_frame,
1150 1b293437 Justin Ruggles
};