Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3dec.c @ 8f58a4c9

History | View | Annotate | Download (54.9 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
static const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 };
42
43
/* table for exponent to scale_factor mapping
44
 * scale_factor[i] = 2 ^ -(i + 15)
45
 */
46
static float scale_factors[25];
47
48 4415076f Justin Ruggles
/** table for grouping exponents */
49
static uint8_t exp_ungroup_tbl[128][3];
50 967d397a Justin Ruggles
51
static int16_t l3_quantizers_1[32];
52
static int16_t l3_quantizers_2[32];
53
static int16_t l3_quantizers_3[32];
54
55
static int16_t l5_quantizers_1[128];
56
static int16_t l5_quantizers_2[128];
57
static int16_t l5_quantizers_3[128];
58
59
static int16_t l7_quantizers[7];
60
61
static int16_t l11_quantizers_1[128];
62
static int16_t l11_quantizers_2[128];
63
64
static int16_t l15_quantizers[15];
65
66
static const uint8_t qntztab[16] = { 0, 5, 7, 3, 7, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16 };
67
68
/* Adjustmens in dB gain */
69
#define LEVEL_MINUS_3DB         0.7071067811865476
70
#define LEVEL_MINUS_4POINT5DB   0.5946035575013605
71
#define LEVEL_MINUS_6DB         0.5000000000000000
72
#define LEVEL_PLUS_3DB          1.4142135623730951
73
#define LEVEL_PLUS_6DB          2.0000000000000000
74
#define LEVEL_ZERO              0.0000000000000000
75
76
static const float clevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB,
77
    LEVEL_MINUS_6DB, LEVEL_MINUS_4POINT5DB };
78
79
static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB };
80
81 486637af Justin Ruggles
#define BLOCK_SIZE    256
82 1b293437 Justin Ruggles
83 98a27a8a Justin Ruggles
/* Output and input configurations. */
84 486637af Justin Ruggles
#define AC3_OUTPUT_UNMODIFIED   0x01
85
#define AC3_OUTPUT_MONO         0x02
86
#define AC3_OUTPUT_STEREO       0x04
87
#define AC3_OUTPUT_DOLBY        0x08
88
#define AC3_OUTPUT_LFEON        0x10
89 1b293437 Justin Ruggles
90 98a27a8a Justin Ruggles
typedef struct {
91
    uint8_t  acmod;
92
    uint8_t  cmixlev;
93
    uint8_t  surmixlev;
94
    uint8_t  dsurmod;
95
96 878c40a1 Justin Ruggles
    uint8_t  blksw[AC3_MAX_CHANNELS];
97
    uint8_t  dithflag[AC3_MAX_CHANNELS];
98 98a27a8a Justin Ruggles
    uint8_t  cplinu;
99 878c40a1 Justin Ruggles
    uint8_t  chincpl[AC3_MAX_CHANNELS];
100 98a27a8a Justin Ruggles
    uint8_t  phsflginu;
101
    uint8_t  cplbegf;
102
    uint8_t  cplendf;
103
    uint8_t  cplcoe;
104
    uint32_t cplbndstrc;
105
    uint8_t  rematstr;
106 878c40a1 Justin Ruggles
    uint8_t  rematflg[AC3_MAX_CHANNELS];
107 98a27a8a Justin Ruggles
    uint8_t  cplexpstr;
108
    uint8_t  lfeexpstr;
109
    uint8_t  chexpstr[5];
110
    uint8_t  csnroffst;
111
    uint8_t  cplfsnroffst;
112
    uint8_t  cplfgaincod;
113
    uint8_t  fsnroffst[5];
114
    uint8_t  fgaincod[5];
115
    uint8_t  lfefsnroffst;
116
    uint8_t  lfefgaincod;
117
    uint8_t  cpldeltbae;
118
    uint8_t  deltbae[5];
119
    uint8_t  cpldeltnseg;
120
    uint8_t  cpldeltoffst[8];
121
    uint8_t  cpldeltlen[8];
122
    uint8_t  cpldeltba[8];
123 878c40a1 Justin Ruggles
    int deltnseg[5];
124 98a27a8a Justin Ruggles
    uint8_t  deltoffst[5][8];
125
    uint8_t  deltlen[5][8];
126
    uint8_t  deltba[5][8];
127
128
    /* Derived Attributes. */
129
    int      sampling_rate;
130
    int      bit_rate;
131
    int      frame_size;
132
133 9fc1ab72 Justin Ruggles
    int      nchans;            //number of total channels
134
    int      nfchans;           //number of full-bandwidth channels
135 c7cfc48f Justin Ruggles
    int      lfeon;             //lfe channel in use
136
137
    float    dynrng;            //dynamic range gain
138
    float    dynrng2;           //dynamic range gain for 1+1 mode
139
    float    chcoeffs[6];       //normalized channel coefficients
140
    float    cplco[5][18];      //coupling coordinates
141
    int      ncplbnd;           //number of coupling bands
142
    int      ncplsubnd;         //number of coupling sub bands
143
    int      cplstrtmant;       //coupling start mantissa
144
    int      cplendmant;        //coupling end mantissa
145
    int      endmant[5];        //channel end mantissas
146 623b7943 Justin Ruggles
    AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
147 c7cfc48f Justin Ruggles
148 bc8edb7e Justin Ruggles
    int8_t   dcplexps[256];     //decoded coupling exponents
149
    int8_t   dexps[5][256];     //decoded fbw channel exponents
150
    int8_t   dlfeexps[256];     //decoded lfe channel exponents
151 c7cfc48f Justin Ruggles
    uint8_t  cplbap[256];       //coupling bit allocation pointers
152
    uint8_t  bap[5][256];       //fbw channel bit allocation pointers
153
    uint8_t  lfebap[256];       //lfe channel bit allocation pointers
154
155
    int      blkoutput;         //output configuration for block
156
157 cc2a8443 Justin Ruggles
    DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][BLOCK_SIZE]);  //transform coefficients
158 98a27a8a Justin Ruggles
159
    /* For IMDCT. */
160 c7cfc48f Justin Ruggles
    MDCTContext imdct_512;  //for 512 sample imdct transform
161
    MDCTContext imdct_256;  //for 256 sample imdct transform
162 1d0a6f52 Justin Ruggles
    DSPContext  dsp;        //for optimization
163 c7cfc48f Justin Ruggles
164 cc2a8443 Justin Ruggles
    DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS][BLOCK_SIZE]);    //output after imdct transform and windowing
165
    DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS][BLOCK_SIZE]);     //delay - added to the next block
166 c7cfc48f Justin Ruggles
    DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]);               //temporary storage for imdct transform
167
    DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]);          //temporary storage for output before windowing
168
    DECLARE_ALIGNED_16(float, window[BLOCK_SIZE]);                  //window coefficients
169 98a27a8a Justin Ruggles
170
    /* Miscellaneous. */
171
    GetBitContext gb;
172 cb503702 Justin Ruggles
    AVRandomState dith_state;   //for dither generation
173 98a27a8a Justin Ruggles
} AC3DecodeContext;
174
175 c7cfc48f Justin Ruggles
/*********** BEGIN INIT HELPER FUNCTIONS ***********/
176 2fbbd087 Justin Ruggles
/**
177
 * Generate a Kaiser-Bessel Derived Window.
178
 */
179 860fe8c9 Justin Ruggles
static void ac3_window_init(float *window)
180 2fbbd087 Justin Ruggles
{
181 6dc5d71f Justin Ruggles
   int i, j;
182
   double sum = 0.0, bessel, tmp;
183
   double local_window[256];
184
   double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
185
186
   for (i = 0; i < 256; i++) {
187
       tmp = i * (256 - i) * alpha2;
188
       bessel = 1.0;
189
       for (j = 100; j > 0; j--) /* defaul to 100 iterations */
190
           bessel = bessel * tmp / (j * j) + 1;
191
       sum += bessel;
192
       local_window[i] = sum;
193
   }
194
195
   sum++;
196
   for (i = 0; i < 256; i++)
197
       window[i] = sqrt(local_window[i] / sum);
198 2fbbd087 Justin Ruggles
}
199
200 c7cfc48f Justin Ruggles
/*
201
 * Generate quantizer tables.
202
 */
203 98a27a8a Justin Ruggles
static void generate_quantizers_table(int16_t quantizers[], int level, int length)
204
{
205
    int i;
206
207
    for (i = 0; i < length; i++)
208
        quantizers[i] = ((2 * i - level + 1) << 15) / level;
209
}
210
211
static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
212
{
213
    int i, j;
214
    int16_t v;
215
216
    for (i = 0; i < length1; i++) {
217
        v = ((2 * i - level + 1) << 15) / level;
218
        for (j = 0; j < length2; j++)
219
            quantizers[i * length2 + j] = v;
220
    }
221
222
    for (i = length1 * length2; i < size; i++)
223
        quantizers[i] = 0;
224
}
225
226
static void generate_quantizers_table_2(int16_t quantizers[], int level, int length1, int length2, int size)
227
{
228
    int i, j;
229
    int16_t v;
230
231
    for (i = 0; i < length1; i++) {
232
        v = ((2 * (i % level) - level + 1) << 15) / level;
233
        for (j = 0; j < length2; j++)
234
            quantizers[i * length2 + j] = v;
235
    }
236
237
    for (i = length1 * length2; i < size; i++)
238
        quantizers[i] = 0;
239
240
}
241
242
static void generate_quantizers_table_3(int16_t quantizers[], int level, int length1, int length2, int size)
243
{
244
    int i, j;
245
246
    for (i = 0; i < length1; i++)
247
        for (j = 0; j < length2; j++)
248
            quantizers[i * length2 + j] = ((2 * (j % level) - level + 1) << 15) / level;
249 1b293437 Justin Ruggles
250 98a27a8a Justin Ruggles
    for (i = length1 * length2; i < size; i++)
251
        quantizers[i] = 0;
252
}
253
254 c7cfc48f Justin Ruggles
/*
255
 * Initialize tables at runtime.
256
 */
257 98a27a8a Justin Ruggles
static void ac3_tables_init(void)
258 00585845 Justin Ruggles
{
259 4415076f Justin Ruggles
    int i;
260 98a27a8a Justin Ruggles
261
    /* Quantizer ungrouping tables. */
262
    // for level-3 quantizers
263
    generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
264
    generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
265
    generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
266
267
    //for level-5 quantizers
268
    generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
269
    generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
270
    generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
271
272
    //for level-7 quantizers
273
    generate_quantizers_table(l7_quantizers, 7, 7);
274
275
    //for level-4 quantizers
276
    generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
277
    generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
278
279
    //for level-15 quantizers
280
    generate_quantizers_table(l15_quantizers, 15, 15);
281 c7cfc48f Justin Ruggles
    /* End Quantizer ungrouping tables. */
282
283
    //generate scale factors
284
    for (i = 0; i < 25; i++)
285
        scale_factors[i] = pow(2.0, -(i + 15));
286 4415076f Justin Ruggles
287
    /* generate exponent tables
288
       reference: Section 7.1.3 Exponent Decoding */
289
    for(i=0; i<128; i++) {
290
        exp_ungroup_tbl[i][0] =  i / 25;
291
        exp_ungroup_tbl[i][1] = (i % 25) / 5;
292
        exp_ungroup_tbl[i][2] = (i % 25) % 5;
293
    }
294 00585845 Justin Ruggles
}
295
296 1b293437 Justin Ruggles
297
static int ac3_decode_init(AVCodecContext *avctx)
298
{
299
    AC3DecodeContext *ctx = avctx->priv_data;
300
301 cc2a8443 Justin Ruggles
    ac3_common_init();
302 98a27a8a Justin Ruggles
    ac3_tables_init();
303 45b0ed13 Justin Ruggles
    ff_mdct_init(&ctx->imdct_256, 8, 1);
304
    ff_mdct_init(&ctx->imdct_512, 9, 1);
305 6dc5d71f Justin Ruggles
    ac3_window_init(ctx->window);
306 1d0a6f52 Justin Ruggles
    dsputil_init(&ctx->dsp, avctx);
307 cb503702 Justin Ruggles
    av_init_random(0, &ctx->dith_state);
308 2aa2c5c4 Justin Ruggles
309 1b293437 Justin Ruggles
    return 0;
310 2aa2c5c4 Justin Ruggles
}
311 c7cfc48f Justin Ruggles
/*********** END INIT FUNCTIONS ***********/
312 2aa2c5c4 Justin Ruggles
313 9fc1ab72 Justin Ruggles
/**
314
 * Parses the 'sync info' and 'bit stream info' from the AC-3 bitstream.
315 c7cfc48f Justin Ruggles
 * GetBitContext within AC3DecodeContext must point to
316
 * start of the synchronized ac3 bitstream.
317
 */
318 9fc1ab72 Justin Ruggles
static int ac3_parse_header(AC3DecodeContext *ctx)
319 2aa2c5c4 Justin Ruggles
{
320 9fc1ab72 Justin Ruggles
    AC3HeaderInfo hdr;
321 1b293437 Justin Ruggles
    GetBitContext *gb = &ctx->gb;
322 9fc1ab72 Justin Ruggles
    int err, i;
323
324
    err = ff_ac3_parse_header(gb->buffer, &hdr);
325
    if(err)
326
        return err;
327
328
    /* get decoding parameters from header info */
329
    ctx->bit_alloc_params.fscod       = hdr.fscod;
330
    ctx->acmod                        = hdr.acmod;
331
    ctx->cmixlev                      = hdr.cmixlev;
332
    ctx->surmixlev                    = hdr.surmixlev;
333
    ctx->dsurmod                      = hdr.dsurmod;
334
    ctx->lfeon                        = hdr.lfeon;
335
    ctx->bit_alloc_params.halfratecod = hdr.halfratecod;
336
    ctx->sampling_rate                = hdr.sample_rate;
337
    ctx->bit_rate                     = hdr.bit_rate;
338
    ctx->nchans                       = hdr.channels;
339
    ctx->nfchans                      = ctx->nchans - ctx->lfeon;
340
    ctx->frame_size                   = hdr.frame_size;
341
    ctx->blkoutput                    = nfchans_tbl[ctx->acmod];
342
    if(ctx->lfeon)
343
        ctx->blkoutput |= AC3_OUTPUT_LFEON;
344
345
    /* skip over portion of header which has already been read */
346 98a27a8a Justin Ruggles
    skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
347 9fc1ab72 Justin Ruggles
    skip_bits(gb, 16); // skip crc1
348
    skip_bits(gb, 8);  // skip fscod and frmsizecod
349
    skip_bits(gb, 11); // skip bsid, bsmod, and acmod
350
    if(ctx->acmod == AC3_ACMOD_STEREO) {
351
        skip_bits(gb, 2); // skip dsurmod
352
    } else {
353
        if((ctx->acmod & 1) && ctx->acmod != AC3_ACMOD_MONO)
354
            skip_bits(gb, 2); // skip cmixlev
355
        if(ctx->acmod & 4)
356
            skip_bits(gb, 2); // skip surmixlev
357 00585845 Justin Ruggles
    }
358 9fc1ab72 Justin Ruggles
    skip_bits1(gb); // skip lfeon
359 98a27a8a Justin Ruggles
360 9fc1ab72 Justin Ruggles
    /* read the rest of the bsi. read twice for dual mono mode. */
361 98a27a8a Justin Ruggles
    i = !(ctx->acmod);
362
    do {
363
        skip_bits(gb, 5); //skip dialog normalization
364
        if (get_bits1(gb))
365
            skip_bits(gb, 8); //skip compression
366
        if (get_bits1(gb))
367
            skip_bits(gb, 8); //skip language code
368
        if (get_bits1(gb))
369
            skip_bits(gb, 7); //skip audio production information
370
    } while (i--);
371
372
    skip_bits(gb, 2); //skip copyright bit and original bitstream bit
373
374 9fc1ab72 Justin Ruggles
    /* FIXME: read & use the xbsi1 downmix levels */
375 00585845 Justin Ruggles
    if (get_bits1(gb))
376 98a27a8a Justin Ruggles
        skip_bits(gb, 14); //skip timecode1
377 00585845 Justin Ruggles
    if (get_bits1(gb))
378 98a27a8a Justin Ruggles
        skip_bits(gb, 14); //skip timecode2
379
380 00585845 Justin Ruggles
    if (get_bits1(gb)) {
381 98a27a8a Justin Ruggles
        i = get_bits(gb, 6); //additional bsi length
382 486637af Justin Ruggles
        do {
383 00585845 Justin Ruggles
            skip_bits(gb, 8);
384 98a27a8a Justin Ruggles
        } while(i--);
385 1b293437 Justin Ruggles
    }
386 9fc1ab72 Justin Ruggles
387
    return 0;
388 2aa2c5c4 Justin Ruggles
}
389
390 3bb004fc Justin Ruggles
/**
391
 * Decodes the grouped exponents.
392 c7cfc48f Justin Ruggles
 * This function decodes the coded exponents according to exponent strategy
393
 * and stores them in the decoded exponents buffer.
394
 *
395 3bb004fc Justin Ruggles
 * @param[in]  gb      GetBitContext which points to start of coded exponents
396
 * @param[in]  expstr  Exponent coding strategy
397
 * @param[in]  ngrps   Number of grouped exponents
398
 * @param[in]  absexp  Absolute exponent or DC exponent
399
 * @param[out] dexps   Decoded exponents are stored in dexps
400 2aa2c5c4 Justin Ruggles
 */
401 4415076f Justin Ruggles
static void decode_exponents(GetBitContext *gb, int expstr, int ngrps,
402 bc8edb7e Justin Ruggles
                             uint8_t absexp, int8_t *dexps)
403 2aa2c5c4 Justin Ruggles
{
404 4415076f Justin Ruggles
    int i, j, grp, grpsize;
405
    int dexp[256];
406
    int expacc, prevexp;
407
408
    /* unpack groups */
409
    grpsize = expstr + (expstr == EXP_D45);
410
    for(grp=0,i=0; grp<ngrps; grp++) {
411
        expacc = get_bits(gb, 7);
412
        dexp[i++] = exp_ungroup_tbl[expacc][0];
413
        dexp[i++] = exp_ungroup_tbl[expacc][1];
414
        dexp[i++] = exp_ungroup_tbl[expacc][2];
415
    }
416 2aa2c5c4 Justin Ruggles
417 4415076f Justin Ruggles
    /* convert to absolute exps and expand groups */
418
    prevexp = absexp;
419
    for(i=0; i<ngrps*3; i++) {
420
        prevexp = av_clip(prevexp + dexp[i]-2, 0, 24);
421
        for(j=0; j<grpsize; j++) {
422
            dexps[(i*grpsize)+j] = prevexp;
423 1b293437 Justin Ruggles
        }
424 2aa2c5c4 Justin Ruggles
    }
425
}
426
427 c7cfc48f Justin Ruggles
/* Performs bit allocation.
428
 * This function performs bit allocation for the requested chanenl.
429 2aa2c5c4 Justin Ruggles
 */
430 2fbbd087 Justin Ruggles
static void do_bit_allocation(AC3DecodeContext *ctx, int chnl)
431 2aa2c5c4 Justin Ruggles
{
432 623b7943 Justin Ruggles
    int fgain, snroffset;
433 1b293437 Justin Ruggles
434
    if (chnl == 5) {
435 cc2a8443 Justin Ruggles
        fgain = ff_fgaintab[ctx->cplfgaincod];
436 98a27a8a Justin Ruggles
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->cplfsnroffst) << 2;
437 623b7943 Justin Ruggles
        ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->cplbap,
438
                                      ctx->dcplexps, ctx->cplstrtmant,
439
                                      ctx->cplendmant, snroffset, fgain, 0,
440
                                      ctx->cpldeltbae, ctx->cpldeltnseg,
441
                                      ctx->cpldeltoffst, ctx->cpldeltlen,
442
                                      ctx->cpldeltba);
443 2aa2c5c4 Justin Ruggles
    }
444 1b293437 Justin Ruggles
    else if (chnl == 6) {
445 cc2a8443 Justin Ruggles
        fgain = ff_fgaintab[ctx->lfefgaincod];
446 98a27a8a Justin Ruggles
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->lfefsnroffst) << 2;
447 623b7943 Justin Ruggles
        ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->lfebap,
448
                                      ctx->dlfeexps, 0, 7, snroffset, fgain, 1,
449
                                      DBA_NONE, 0, NULL, NULL, NULL);
450 1b293437 Justin Ruggles
    }
451
    else {
452 cc2a8443 Justin Ruggles
        fgain = ff_fgaintab[ctx->fgaincod[chnl]];
453 98a27a8a Justin Ruggles
        snroffset = (((ctx->csnroffst - 15) << 4) + ctx->fsnroffst[chnl]) << 2;
454 623b7943 Justin Ruggles
        ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->bap[chnl],
455
                                      ctx->dexps[chnl], 0, ctx->endmant[chnl],
456
                                      snroffset, fgain, 0, ctx->deltbae[chnl],
457
                                      ctx->deltnseg[chnl], ctx->deltoffst[chnl],
458
                                      ctx->deltlen[chnl], ctx->deltba[chnl]);
459 1b293437 Justin Ruggles
    }
460 2aa2c5c4 Justin Ruggles
}
461
462 486637af Justin Ruggles
typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
463 98a27a8a Justin Ruggles
    int16_t l3_quantizers[3];
464
    int16_t l5_quantizers[3];
465
    int16_t l11_quantizers[2];
466 486637af Justin Ruggles
    int l3ptr;
467
    int l5ptr;
468
    int l11ptr;
469
} mant_groups;
470
471
/* Get the transform coefficients for coupling channel and uncouple channels.
472
 * The coupling transform coefficients starts at the the cplstrtmant, which is
473
 * equal to endmant[ch] for fbw channels. Hence we can uncouple channels before
474
 * getting transform coefficients for the channel.
475
 */
476
static int get_transform_coeffs_cpling(AC3DecodeContext *ctx, mant_groups *m)
477 1b293437 Justin Ruggles
{
478 486637af Justin Ruggles
    GetBitContext *gb = &ctx->gb;
479 2fbbd087 Justin Ruggles
    int ch, start, end, cplbndstrc, bnd, gcode, tbap;
480 486637af Justin Ruggles
    float cplcos[5], cplcoeff;
481 98a27a8a Justin Ruggles
    uint8_t *exps = ctx->dcplexps;
482
    uint8_t *bap = ctx->cplbap;
483
484
    cplbndstrc = ctx->cplbndstrc;
485
    start = ctx->cplstrtmant;
486
    bnd = 0;
487
488
    while (start < ctx->cplendmant) {
489
        end = start + 12;
490
        while (cplbndstrc & 1) {
491
            end += 12;
492
            cplbndstrc >>= 1;
493
        }
494
        cplbndstrc >>= 1;
495
        for (ch = 0; ch < ctx->nfchans; ch++)
496
            cplcos[ch] = ctx->chcoeffs[ch] * ctx->cplco[ch][bnd];
497
        bnd++;
498
499
        while (start < end) {
500
            tbap = bap[start];
501
            switch(tbap) {
502 486637af Justin Ruggles
                case 0:
503 2fbbd087 Justin Ruggles
                    for (ch = 0; ch < ctx->nfchans; ch++)
504 878c40a1 Justin Ruggles
                        if (ctx->chincpl[ch]) {
505
                            if (ctx->dithflag[ch]) {
506 de34875c Justin Ruggles
                                cplcoeff = (av_random(&ctx->dith_state) & 0xFFFF) * scale_factors[exps[start]];
507 45b0ed13 Justin Ruggles
                                ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch] * LEVEL_MINUS_3DB;
508 486637af Justin Ruggles
                            } else
509 98a27a8a Justin Ruggles
                                ctx->transform_coeffs[ch + 1][start] = 0;
510 486637af Justin Ruggles
                        }
511 98a27a8a Justin Ruggles
                    start++;
512 486637af Justin Ruggles
                    continue;
513
                case 1:
514
                    if (m->l3ptr > 2) {
515
                        gcode = get_bits(gb, 5);
516 98a27a8a Justin Ruggles
                        m->l3_quantizers[0] = l3_quantizers_1[gcode];
517
                        m->l3_quantizers[1] = l3_quantizers_2[gcode];
518
                        m->l3_quantizers[2] = l3_quantizers_3[gcode];
519 486637af Justin Ruggles
                        m->l3ptr = 0;
520
                    }
521 de34875c Justin Ruggles
                    cplcoeff = m->l3_quantizers[m->l3ptr++] * scale_factors[exps[start]];
522 486637af Justin Ruggles
                    break;
523
524
                case 2:
525
                    if (m->l5ptr > 2) {
526
                        gcode = get_bits(gb, 7);
527 98a27a8a Justin Ruggles
                        m->l5_quantizers[0] = l5_quantizers_1[gcode];
528
                        m->l5_quantizers[1] = l5_quantizers_2[gcode];
529
                        m->l5_quantizers[2] = l5_quantizers_3[gcode];
530 486637af Justin Ruggles
                        m->l5ptr = 0;
531
                    }
532 de34875c Justin Ruggles
                    cplcoeff = m->l5_quantizers[m->l5ptr++] * scale_factors[exps[start]];
533 486637af Justin Ruggles
                    break;
534
535
                case 3:
536 de34875c Justin Ruggles
                    cplcoeff = l7_quantizers[get_bits(gb, 3)] * scale_factors[exps[start]];
537 486637af Justin Ruggles
                    break;
538
539
                case 4:
540
                    if (m->l11ptr > 1) {
541
                        gcode = get_bits(gb, 7);
542 98a27a8a Justin Ruggles
                        m->l11_quantizers[0] = l11_quantizers_1[gcode];
543
                        m->l11_quantizers[1] = l11_quantizers_2[gcode];
544 486637af Justin Ruggles
                        m->l11ptr = 0;
545
                    }
546 de34875c Justin Ruggles
                    cplcoeff = m->l11_quantizers[m->l11ptr++] * scale_factors[exps[start]];
547 486637af Justin Ruggles
                    break;
548
549
                case 5:
550 de34875c Justin Ruggles
                    cplcoeff = l15_quantizers[get_bits(gb, 4)] * scale_factors[exps[start]];
551 486637af Justin Ruggles
                    break;
552
553
                default:
554 de34875c Justin Ruggles
                    cplcoeff = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * scale_factors[exps[start]];
555 486637af Justin Ruggles
            }
556 98a27a8a Justin Ruggles
            for (ch = 0; ch < ctx->nfchans; ch++)
557 878c40a1 Justin Ruggles
                if (ctx->chincpl[ch])
558 2fbbd087 Justin Ruggles
                    ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
559 98a27a8a Justin Ruggles
            start++;
560 486637af Justin Ruggles
        }
561
    }
562
563
    return 0;
564
}
565 1b293437 Justin Ruggles
566
/* Get the transform coefficients for particular channel */
567 2fbbd087 Justin Ruggles
static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
568 1b293437 Justin Ruggles
{
569 2fbbd087 Justin Ruggles
    GetBitContext *gb = &ctx->gb;
570
    int i, gcode, tbap, dithflag, end;
571
    uint8_t *exps;
572
    uint8_t *bap;
573
    float *coeffs;
574 486637af Justin Ruggles
    float factors[25];
575 1b293437 Justin Ruggles
576 486637af Justin Ruggles
    for (i = 0; i < 25; i++)
577 2fbbd087 Justin Ruggles
        factors[i] = scale_factors[i] * ctx->chcoeffs[ch_index];
578
579
    if (ch_index != -1) { /* fbw channels */
580 878c40a1 Justin Ruggles
        dithflag = ctx->dithflag[ch_index];
581 2fbbd087 Justin Ruggles
        exps = ctx->dexps[ch_index];
582
        bap = ctx->bap[ch_index];
583
        coeffs = ctx->transform_coeffs[ch_index + 1];
584
        end = ctx->endmant[ch_index];
585
    } else if (ch_index == -1) {
586
        dithflag = 0;
587
        exps = ctx->dlfeexps;
588
        bap = ctx->lfebap;
589
        coeffs = ctx->transform_coeffs[0];
590
        end = 7;
591
    }
592 1b293437 Justin Ruggles
593 2fbbd087 Justin Ruggles
594
    for (i = 0; i < end; i++) {
595 98a27a8a Justin Ruggles
        tbap = bap[i];
596
        switch (tbap) {
597 1b293437 Justin Ruggles
            case 0:
598 45b0ed13 Justin Ruggles
                if (!dithflag) {
599 00585845 Justin Ruggles
                    coeffs[i] = 0;
600
                    continue;
601
                }
602
                else {
603 de34875c Justin Ruggles
                    coeffs[i] = (av_random(&ctx->dith_state) & 0xFFFF) * factors[exps[i]];
604 45b0ed13 Justin Ruggles
                    coeffs[i] *= LEVEL_MINUS_3DB;
605 00585845 Justin Ruggles
                    continue;
606
                }
607 1b293437 Justin Ruggles
608
            case 1:
609 486637af Justin Ruggles
                if (m->l3ptr > 2) {
610 00585845 Justin Ruggles
                    gcode = get_bits(gb, 5);
611 98a27a8a Justin Ruggles
                    m->l3_quantizers[0] = l3_quantizers_1[gcode];
612
                    m->l3_quantizers[1] = l3_quantizers_2[gcode];
613
                    m->l3_quantizers[2] = l3_quantizers_3[gcode];
614 486637af Justin Ruggles
                    m->l3ptr = 0;
615 1b293437 Justin Ruggles
                }
616 de34875c Justin Ruggles
                coeffs[i] = m->l3_quantizers[m->l3ptr++] * factors[exps[i]];
617 00585845 Justin Ruggles
                continue;
618 1b293437 Justin Ruggles
619
            case 2:
620 486637af Justin Ruggles
                if (m->l5ptr > 2) {
621 00585845 Justin Ruggles
                    gcode = get_bits(gb, 7);
622 98a27a8a Justin Ruggles
                    m->l5_quantizers[0] = l5_quantizers_1[gcode];
623
                    m->l5_quantizers[1] = l5_quantizers_2[gcode];
624
                    m->l5_quantizers[2] = l5_quantizers_3[gcode];
625 486637af Justin Ruggles
                    m->l5ptr = 0;
626 1b293437 Justin Ruggles
                }
627 de34875c Justin Ruggles
                coeffs[i] = m->l5_quantizers[m->l5ptr++] * factors[exps[i]];
628 00585845 Justin Ruggles
                continue;
629 1b293437 Justin Ruggles
630
            case 3:
631 de34875c Justin Ruggles
                coeffs[i] = l7_quantizers[get_bits(gb, 3)] * factors[exps[i]];
632 00585845 Justin Ruggles
                continue;
633 1b293437 Justin Ruggles
634
            case 4:
635 486637af Justin Ruggles
                if (m->l11ptr > 1) {
636 00585845 Justin Ruggles
                    gcode = get_bits(gb, 7);
637 98a27a8a Justin Ruggles
                    m->l11_quantizers[0] = l11_quantizers_1[gcode];
638
                    m->l11_quantizers[1] = l11_quantizers_2[gcode];
639 486637af Justin Ruggles
                    m->l11ptr = 0;
640 1b293437 Justin Ruggles
                }
641 de34875c Justin Ruggles
                coeffs[i] = m->l11_quantizers[m->l11ptr++] * factors[exps[i]];
642 00585845 Justin Ruggles
                continue;
643 1b293437 Justin Ruggles
644
            case 5:
645 de34875c Justin Ruggles
                coeffs[i] = l15_quantizers[get_bits(gb, 4)] * factors[exps[i]];
646 00585845 Justin Ruggles
                continue;
647 1b293437 Justin Ruggles
648
            default:
649 de34875c Justin Ruggles
                coeffs[i] = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * factors[exps[i]];
650 00585845 Justin Ruggles
                continue;
651 1b293437 Justin Ruggles
        }
652
    }
653
654
    return 0;
655
}
656
657 c7cfc48f Justin Ruggles
/* Get the transform coefficients.
658
 * This function extracts the tranform coefficients form the ac3 bitstream.
659
 * This function is called after bit allocation is performed.
660
 */
661 1b293437 Justin Ruggles
static int get_transform_coeffs(AC3DecodeContext * ctx)
662
{
663 486637af Justin Ruggles
    int i, end;
664 1b293437 Justin Ruggles
    int got_cplchan = 0;
665 486637af Justin Ruggles
    mant_groups m;
666
667
    m.l3ptr = m.l5ptr = m.l11ptr = 3;
668 1b293437 Justin Ruggles
669 98a27a8a Justin Ruggles
    for (i = 0; i < ctx->nfchans; i++) {
670 1b293437 Justin Ruggles
        /* transform coefficients for individual channel */
671 2fbbd087 Justin Ruggles
        if (get_transform_coeffs_ch(ctx, i, &m))
672 1b293437 Justin Ruggles
            return -1;
673
        /* tranform coefficients for coupling channels */
674 878c40a1 Justin Ruggles
        if (ctx->chincpl[i])  {
675 486637af Justin Ruggles
            if (!got_cplchan) {
676 98a27a8a Justin Ruggles
                if (get_transform_coeffs_cpling(ctx, &m)) {
677
                    av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
678 486637af Justin Ruggles
                    return -1;
679 98a27a8a Justin Ruggles
                }
680 486637af Justin Ruggles
                got_cplchan = 1;
681
            }
682 98a27a8a Justin Ruggles
            end = ctx->cplendmant;
683 486637af Justin Ruggles
        } else
684 98a27a8a Justin Ruggles
            end = ctx->endmant[i];
685 486637af Justin Ruggles
        do
686 98a27a8a Justin Ruggles
            ctx->transform_coeffs[i + 1][end] = 0;
687 486637af Justin Ruggles
        while(++end < 256);
688
    }
689 98a27a8a Justin Ruggles
    if (ctx->lfeon) {
690 2fbbd087 Justin Ruggles
        if (get_transform_coeffs_ch(ctx, -1, &m))
691 1b293437 Justin Ruggles
                return -1;
692 486637af Justin Ruggles
        for (i = 7; i < 256; i++) {
693 98a27a8a Justin Ruggles
            ctx->transform_coeffs[0][i] = 0;
694 1b293437 Justin Ruggles
        }
695
    }
696
697
    return 0;
698 2aa2c5c4 Justin Ruggles
}
699
700 c7cfc48f Justin Ruggles
/* Rematrixing routines. */
701 00585845 Justin Ruggles
static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
702 1b293437 Justin Ruggles
{
703
    float tmp0, tmp1;
704
705
    while (start < end) {
706 98a27a8a Justin Ruggles
        tmp0 = ctx->transform_coeffs[1][start];
707
        tmp1 = ctx->transform_coeffs[2][start];
708
        ctx->transform_coeffs[1][start] = tmp0 + tmp1;
709
        ctx->transform_coeffs[2][start] = tmp0 - tmp1;
710 1b293437 Justin Ruggles
        start++;
711
    }
712
}
713 2aa2c5c4 Justin Ruggles
714 1b293437 Justin Ruggles
static void do_rematrixing(AC3DecodeContext *ctx)
715
{
716 2fbbd087 Justin Ruggles
    int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
717
    int end, bndend;
718
719
    end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
720 1b293437 Justin Ruggles
721 878c40a1 Justin Ruggles
    if (ctx->rematflg[0])
722 00585845 Justin Ruggles
        do_rematrixing1(ctx, bnd1, bnd2);
723 2fbbd087 Justin Ruggles
724 878c40a1 Justin Ruggles
    if (ctx->rematflg[1])
725 00585845 Justin Ruggles
        do_rematrixing1(ctx, bnd2, bnd3);
726 2fbbd087 Justin Ruggles
727
    bndend = bnd4;
728
    if (bndend > end) {
729
        bndend = end;
730 878c40a1 Justin Ruggles
        if (ctx->rematflg[2])
731 00585845 Justin Ruggles
            do_rematrixing1(ctx, bnd3, bndend);
732 2fbbd087 Justin Ruggles
    } else {
733 878c40a1 Justin Ruggles
        if (ctx->rematflg[2])
734 00585845 Justin Ruggles
            do_rematrixing1(ctx, bnd3, bnd4);
735 878c40a1 Justin Ruggles
        if (ctx->rematflg[3])
736 2fbbd087 Justin Ruggles
            do_rematrixing1(ctx, bnd4, end);
737 1b293437 Justin Ruggles
    }
738
}
739 2aa2c5c4 Justin Ruggles
740 c7cfc48f Justin Ruggles
/* This function sets the normalized channel coefficients.
741
 * Transform coefficients are multipllied by the channel
742
 * coefficients to get normalized transform coefficients.
743
 */
744 1b293437 Justin Ruggles
static void get_downmix_coeffs(AC3DecodeContext *ctx)
745
{
746 98a27a8a Justin Ruggles
    int from = ctx->acmod;
747
    int to = ctx->blkoutput;
748
    float clev = clevs[ctx->cmixlev];
749
    float slev = slevs[ctx->surmixlev];
750 45b0ed13 Justin Ruggles
    float nf = 1.0; //normalization factor for downmix coeffs
751 6dc5d71f Justin Ruggles
    int i;
752
753
    if (!ctx->acmod) {
754
        ctx->chcoeffs[0] = 2 * ctx->dynrng;
755
        ctx->chcoeffs[1] = 2 * ctx->dynrng2;
756
    } else {
757
        for (i = 0; i < ctx->nfchans; i++)
758
            ctx->chcoeffs[i] = 2 * ctx->dynrng;
759
    }
760 1b293437 Justin Ruggles
761
    if (to == AC3_OUTPUT_UNMODIFIED)
762 00585845 Justin Ruggles
        return;
763 1b293437 Justin Ruggles
764
    switch (from) {
765 cc2a8443 Justin Ruggles
        case AC3_ACMOD_DUALMONO:
766 1b293437 Justin Ruggles
            switch (to) {
767
                case AC3_OUTPUT_MONO:
768
                case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */
769 45b0ed13 Justin Ruggles
                    nf = 0.5;
770
                    ctx->chcoeffs[0] *= nf;
771
                    ctx->chcoeffs[1] *= nf;
772 1b293437 Justin Ruggles
                    break;
773
            }
774
            break;
775 cc2a8443 Justin Ruggles
        case AC3_ACMOD_MONO:
776 1b293437 Justin Ruggles
            switch (to) {
777
                case AC3_OUTPUT_STEREO:
778 45b0ed13 Justin Ruggles
                    nf = LEVEL_MINUS_3DB;
779
                    ctx->chcoeffs[0] *= nf;
780 1b293437 Justin Ruggles
                    break;
781
            }
782
            break;
783 cc2a8443 Justin Ruggles
        case AC3_ACMOD_STEREO:
784 1b293437 Justin Ruggles
            switch (to) {
785
                case AC3_OUTPUT_MONO:
786 45b0ed13 Justin Ruggles
                    nf = LEVEL_MINUS_3DB;
787
                    ctx->chcoeffs[0] *= nf;
788
                    ctx->chcoeffs[1] *= nf;
789 1b293437 Justin Ruggles
                    break;
790
            }
791
            break;
792 cc2a8443 Justin Ruggles
        case AC3_ACMOD_3F:
793 1b293437 Justin Ruggles
            switch (to) {
794
                case AC3_OUTPUT_MONO:
795 45b0ed13 Justin Ruggles
                    nf = LEVEL_MINUS_3DB / (1.0 + clev);
796
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
797
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
798
                    ctx->chcoeffs[1] *= ((nf * clev * LEVEL_MINUS_3DB) / 2.0);
799 1b293437 Justin Ruggles
                    break;
800
                case AC3_OUTPUT_STEREO:
801 45b0ed13 Justin Ruggles
                    nf = 1.0 / (1.0 + clev);
802
                    ctx->chcoeffs[0] *= nf;
803
                    ctx->chcoeffs[2] *= nf;
804
                    ctx->chcoeffs[1] *= (nf * clev);
805 1b293437 Justin Ruggles
                    break;
806
            }
807
            break;
808 cc2a8443 Justin Ruggles
        case AC3_ACMOD_2F1R:
809 1b293437 Justin Ruggles
            switch (to) {
810
                case AC3_OUTPUT_MONO:
811 45b0ed13 Justin Ruggles
                    nf = 2.0 * LEVEL_MINUS_3DB / (2.0 + slev);
812
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
813
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
814
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
815 1b293437 Justin Ruggles
                    break;
816
                case AC3_OUTPUT_STEREO:
817 45b0ed13 Justin Ruggles
                    nf = 1.0 / (1.0 + (slev * LEVEL_MINUS_3DB));
818
                    ctx->chcoeffs[0] *= nf;
819
                    ctx->chcoeffs[1] *= nf;
820
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
821 1b293437 Justin Ruggles
                    break;
822
                case AC3_OUTPUT_DOLBY:
823 45b0ed13 Justin Ruggles
                    nf = 1.0 / (1.0 + LEVEL_MINUS_3DB);
824
                    ctx->chcoeffs[0] *= nf;
825
                    ctx->chcoeffs[1] *= nf;
826
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
827 1b293437 Justin Ruggles
                    break;
828
            }
829
            break;
830 cc2a8443 Justin Ruggles
        case AC3_ACMOD_3F1R:
831 1b293437 Justin Ruggles
            switch (to) {
832
                case AC3_OUTPUT_MONO:
833 45b0ed13 Justin Ruggles
                    nf = LEVEL_MINUS_3DB / (1.0 + clev + (slev / 2.0));
834
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
835
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
836
                    ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
837
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
838 1b293437 Justin Ruggles
                    break;
839
                case AC3_OUTPUT_STEREO:
840 45b0ed13 Justin Ruggles
                    nf = 1.0 / (1.0 + clev + (slev * LEVEL_MINUS_3DB));
841
                    ctx->chcoeffs[0] *= nf;
842
                    ctx->chcoeffs[2] *= nf;
843
                    ctx->chcoeffs[1] *= (nf * clev);
844
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
845 1b293437 Justin Ruggles
                    break;
846
                case AC3_OUTPUT_DOLBY:
847 45b0ed13 Justin Ruggles
                    nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
848
                    ctx->chcoeffs[0] *= nf;
849
                    ctx->chcoeffs[1] *= nf;
850
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
851
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
852 1b293437 Justin Ruggles
                    break;
853
            }
854
            break;
855 cc2a8443 Justin Ruggles
        case AC3_ACMOD_2F2R:
856 1b293437 Justin Ruggles
            switch (to) {
857
                case AC3_OUTPUT_MONO:
858 45b0ed13 Justin Ruggles
                    nf = LEVEL_MINUS_3DB / (1.0 + slev);
859
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
860
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
861
                    ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
862
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
863 1b293437 Justin Ruggles
                    break;
864
                case AC3_OUTPUT_STEREO:
865 45b0ed13 Justin Ruggles
                    nf = 1.0 / (1.0 + slev);
866
                    ctx->chcoeffs[0] *= nf;
867
                    ctx->chcoeffs[1] *= nf;
868
                    ctx->chcoeffs[2] *= (nf * slev);
869
                    ctx->chcoeffs[3] *= (nf * slev);
870 1b293437 Justin Ruggles
                    break;
871
                case AC3_OUTPUT_DOLBY:
872 45b0ed13 Justin Ruggles
                    nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
873
                    ctx->chcoeffs[0] *= nf;
874
                    ctx->chcoeffs[1] *= nf;
875
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
876
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
877 1b293437 Justin Ruggles
                    break;
878
            }
879
            break;
880 cc2a8443 Justin Ruggles
        case AC3_ACMOD_3F2R:
881 1b293437 Justin Ruggles
            switch (to) {
882
                case AC3_OUTPUT_MONO:
883 45b0ed13 Justin Ruggles
                    nf = LEVEL_MINUS_3DB / (1.0 + clev + slev);
884
                    ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
885
                    ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
886
                    ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
887
                    ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
888
                    ctx->chcoeffs[4] *= (nf * slev * LEVEL_MINUS_3DB);
889 1b293437 Justin Ruggles
                    break;
890
                case AC3_OUTPUT_STEREO:
891 45b0ed13 Justin Ruggles
                    nf = 1.0 / (1.0 + clev + slev);
892
                    ctx->chcoeffs[0] *= nf;
893
                    ctx->chcoeffs[2] *= nf;
894
                    ctx->chcoeffs[1] *= (nf * clev);
895
                    ctx->chcoeffs[3] *= (nf * slev);
896
                    ctx->chcoeffs[4] *= (nf * slev);
897 1b293437 Justin Ruggles
                    break;
898
                case AC3_OUTPUT_DOLBY:
899 45b0ed13 Justin Ruggles
                    nf = 1.0 / (1.0 + (3.0 * LEVEL_MINUS_3DB));
900
                    ctx->chcoeffs[0] *= nf;
901
                    ctx->chcoeffs[1] *= nf;
902
                    ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
903
                    ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
904
                    ctx->chcoeffs[4] *= (nf * LEVEL_MINUS_3DB);
905 1b293437 Justin Ruggles
                    break;
906
            }
907
            break;
908
    }
909
}
910 2aa2c5c4 Justin Ruggles
911 c7cfc48f Justin Ruggles
/*********** BEGIN DOWNMIX FUNCTIONS ***********/
912 00585845 Justin Ruggles
static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx)
913 1b293437 Justin Ruggles
{
914
    int i;
915 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
916 2aa2c5c4 Justin Ruggles
917 00585845 Justin Ruggles
    for (i = 0; i < 256; i++)
918
        output[1][i] += output[2][i];
919
    memset(output[2], 0, sizeof(output[2]));
920 1b293437 Justin Ruggles
}
921
922 00585845 Justin Ruggles
static inline void mix_dualmono_to_stereo(AC3DecodeContext *ctx)
923 2aa2c5c4 Justin Ruggles
{
924 1b293437 Justin Ruggles
    int i;
925
    float tmp;
926 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
927 1b293437 Justin Ruggles
928
    for (i = 0; i < 256; i++) {
929 00585845 Justin Ruggles
        tmp = output[1][i] + output[2][i];
930
        output[1][i] = output[2][i] = tmp;
931 1b293437 Justin Ruggles
    }
932 2aa2c5c4 Justin Ruggles
}
933
934 00585845 Justin Ruggles
static inline void upmix_mono_to_stereo(AC3DecodeContext *ctx)
935 1b293437 Justin Ruggles
{
936
    int i;
937 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
938 2aa2c5c4 Justin Ruggles
939 1b293437 Justin Ruggles
    for (i = 0; i < 256; i++)
940 00585845 Justin Ruggles
        output[2][i] = output[1][i];
941 1b293437 Justin Ruggles
}
942
943 00585845 Justin Ruggles
static inline void mix_stereo_to_mono(AC3DecodeContext *ctx)
944 2aa2c5c4 Justin Ruggles
{
945 1b293437 Justin Ruggles
    int i;
946 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
947 2aa2c5c4 Justin Ruggles
948 00585845 Justin Ruggles
    for (i = 0; i < 256; i++)
949
        output[1][i] += output[2][i];
950
    memset(output[2], 0, sizeof(output[2]));
951 1b293437 Justin Ruggles
}
952 2aa2c5c4 Justin Ruggles
953 00585845 Justin Ruggles
static inline void mix_3f_to_mono(AC3DecodeContext *ctx)
954 1b293437 Justin Ruggles
{
955
    int i;
956 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
957 2aa2c5c4 Justin Ruggles
958 00585845 Justin Ruggles
    for (i = 0; i < 256; i++)
959
        output[1][i] += (output[2][i] + output[3][i]);
960
    memset(output[2], 0, sizeof(output[2]));
961
    memset(output[3], 0, sizeof(output[3]));
962 2aa2c5c4 Justin Ruggles
}
963
964 00585845 Justin Ruggles
static inline void mix_3f_to_stereo(AC3DecodeContext *ctx)
965 2aa2c5c4 Justin Ruggles
{
966 1b293437 Justin Ruggles
    int i;
967 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
968 2aa2c5c4 Justin Ruggles
969 1b293437 Justin Ruggles
    for (i = 0; i < 256; i++) {
970 00585845 Justin Ruggles
        output[1][i] += output[2][i];
971
        output[2][i] += output[3][i];
972 1b293437 Justin Ruggles
    }
973 00585845 Justin Ruggles
    memset(output[3], 0, sizeof(output[3]));
974 1b293437 Justin Ruggles
}
975 2aa2c5c4 Justin Ruggles
976 00585845 Justin Ruggles
static inline void mix_2f_1r_to_mono(AC3DecodeContext *ctx)
977 1b293437 Justin Ruggles
{
978
    int i;
979 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
980 00585845 Justin Ruggles
981
    for (i = 0; i < 256; i++)
982
        output[1][i] += (output[2][i] + output[3][i]);
983
    memset(output[2], 0, sizeof(output[2]));
984
    memset(output[3], 0, sizeof(output[3]));
985 1b293437 Justin Ruggles
986 2aa2c5c4 Justin Ruggles
}
987
988 00585845 Justin Ruggles
static inline void mix_2f_1r_to_stereo(AC3DecodeContext *ctx)
989 2aa2c5c4 Justin Ruggles
{
990 1b293437 Justin Ruggles
    int i;
991 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
992 1b293437 Justin Ruggles
993
    for (i = 0; i < 256; i++) {
994 00585845 Justin Ruggles
        output[1][i] += output[2][i];
995
        output[2][i] += output[3][i];
996 2aa2c5c4 Justin Ruggles
    }
997 00585845 Justin Ruggles
    memset(output[3], 0, sizeof(output[3]));
998 1b293437 Justin Ruggles
}
999 2aa2c5c4 Justin Ruggles
1000 00585845 Justin Ruggles
static inline void mix_2f_1r_to_dolby(AC3DecodeContext *ctx)
1001 1b293437 Justin Ruggles
{
1002
    int i;
1003 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
1004 2aa2c5c4 Justin Ruggles
1005 1b293437 Justin Ruggles
    for (i = 0; i < 256; i++) {
1006 00585845 Justin Ruggles
        output[1][i] -= output[3][i];
1007
        output[2][i] += output[3][i];
1008 1b293437 Justin Ruggles
    }
1009 00585845 Justin Ruggles
    memset(output[3], 0, sizeof(output[3]));
1010 2aa2c5c4 Justin Ruggles
}
1011
1012 00585845 Justin Ruggles
static inline void mix_3f_1r_to_mono(AC3DecodeContext *ctx)
1013 2aa2c5c4 Justin Ruggles
{
1014 1b293437 Justin Ruggles
    int i;
1015 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
1016 1b293437 Justin Ruggles
1017 00585845 Justin Ruggles
    for (i = 0; i < 256; i++)
1018
        output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1019
    memset(output[2], 0, sizeof(output[2]));
1020
    memset(output[3], 0, sizeof(output[3]));
1021
    memset(output[4], 0, sizeof(output[4]));
1022 1b293437 Justin Ruggles
}
1023
1024 00585845 Justin Ruggles
static inline void mix_3f_1r_to_stereo(AC3DecodeContext *ctx)
1025 1b293437 Justin Ruggles
{
1026
    int i;
1027 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
1028 1b293437 Justin Ruggles
1029
    for (i = 0; i < 256; i++) {
1030 00585845 Justin Ruggles
        output[1][i] += (output[2][i] + output[4][i]);
1031
        output[2][i] += (output[3][i] + output[4][i]);
1032 1b293437 Justin Ruggles
    }
1033 00585845 Justin Ruggles
    memset(output[3], 0, sizeof(output[3]));
1034
    memset(output[4], 0, sizeof(output[4]));
1035 1b293437 Justin Ruggles
}
1036
1037 00585845 Justin Ruggles
static inline void mix_3f_1r_to_dolby(AC3DecodeContext *ctx)
1038 1b293437 Justin Ruggles
{
1039
    int i;
1040 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
1041 1b293437 Justin Ruggles
1042
    for (i = 0; i < 256; i++) {
1043 00585845 Justin Ruggles
        output[1][i] += (output[2][i] - output[4][i]);
1044
        output[2][i] += (output[3][i] + output[4][i]);
1045 1b293437 Justin Ruggles
    }
1046 00585845 Justin Ruggles
    memset(output[3], 0, sizeof(output[3]));
1047
    memset(output[4], 0, sizeof(output[4]));
1048 1b293437 Justin Ruggles
}
1049
1050 00585845 Justin Ruggles
static inline void mix_2f_2r_to_mono(AC3DecodeContext *ctx)
1051 1b293437 Justin Ruggles
{
1052
    int i;
1053 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
1054 1b293437 Justin Ruggles
1055 00585845 Justin Ruggles
    for (i = 0; i < 256; i++)
1056
        output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1057
    memset(output[2], 0, sizeof(output[2]));
1058
    memset(output[3], 0, sizeof(output[3]));
1059
    memset(output[4], 0, sizeof(output[4]));
1060 1b293437 Justin Ruggles
}
1061
1062 00585845 Justin Ruggles
static inline void mix_2f_2r_to_stereo(AC3DecodeContext *ctx)
1063 1b293437 Justin Ruggles
{
1064
    int i;
1065 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
1066 1b293437 Justin Ruggles
1067
    for (i = 0; i < 256; i++) {
1068 00585845 Justin Ruggles
        output[1][i] += output[3][i];
1069
        output[2][i] += output[4][i];
1070 1b293437 Justin Ruggles
    }
1071 00585845 Justin Ruggles
    memset(output[3], 0, sizeof(output[3]));
1072
    memset(output[4], 0, sizeof(output[4]));
1073 1b293437 Justin Ruggles
}
1074
1075 00585845 Justin Ruggles
static inline void mix_2f_2r_to_dolby(AC3DecodeContext *ctx)
1076 1b293437 Justin Ruggles
{
1077
    int i;
1078 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
1079 1b293437 Justin Ruggles
1080
    for (i = 0; i < 256; i++) {
1081 00585845 Justin Ruggles
        output[1][i] -= output[3][i];
1082
        output[2][i] += output[4][i];
1083 1b293437 Justin Ruggles
    }
1084 00585845 Justin Ruggles
    memset(output[3], 0, sizeof(output[3]));
1085
    memset(output[4], 0, sizeof(output[4]));
1086 1b293437 Justin Ruggles
}
1087
1088 00585845 Justin Ruggles
static inline void mix_3f_2r_to_mono(AC3DecodeContext *ctx)
1089 1b293437 Justin Ruggles
{
1090
    int i;
1091 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
1092 1b293437 Justin Ruggles
1093 00585845 Justin Ruggles
    for (i = 0; i < 256; i++)
1094
        output[1][i] += (output[2][i] + output[3][i] + output[4][i] + output[5][i]);
1095
    memset(output[2], 0, sizeof(output[2]));
1096
    memset(output[3], 0, sizeof(output[3]));
1097
    memset(output[4], 0, sizeof(output[4]));
1098
    memset(output[5], 0, sizeof(output[5]));
1099 1b293437 Justin Ruggles
}
1100
1101 00585845 Justin Ruggles
static inline void mix_3f_2r_to_stereo(AC3DecodeContext *ctx)
1102 1b293437 Justin Ruggles
{
1103
    int i;
1104 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
1105 1b293437 Justin Ruggles
1106
    for (i = 0; i < 256; i++) {
1107 00585845 Justin Ruggles
        output[1][i] += (output[2][i] + output[4][i]);
1108
        output[2][i] += (output[3][i] + output[5][i]);
1109 1b293437 Justin Ruggles
    }
1110 00585845 Justin Ruggles
    memset(output[3], 0, sizeof(output[3]));
1111
    memset(output[4], 0, sizeof(output[4]));
1112
    memset(output[5], 0, sizeof(output[5]));
1113 1b293437 Justin Ruggles
}
1114
1115 00585845 Justin Ruggles
static inline void mix_3f_2r_to_dolby(AC3DecodeContext *ctx)
1116 1b293437 Justin Ruggles
{
1117
    int i;
1118 98a27a8a Justin Ruggles
    float (*output)[BLOCK_SIZE] = ctx->output;
1119 2aa2c5c4 Justin Ruggles
1120 1b293437 Justin Ruggles
    for (i = 0; i < 256; i++) {
1121 00585845 Justin Ruggles
        output[1][i] += (output[2][i] - output[4][i] - output[5][i]);
1122
        output[2][i] += (output[3][i] + output[4][i] + output[5][i]);
1123 1b293437 Justin Ruggles
    }
1124 00585845 Justin Ruggles
    memset(output[3], 0, sizeof(output[3]));
1125
    memset(output[4], 0, sizeof(output[4]));
1126
    memset(output[5], 0, sizeof(output[5]));
1127 1b293437 Justin Ruggles
}
1128 c7cfc48f Justin Ruggles
/*********** END DOWNMIX FUNCTIONS ***********/
1129 1b293437 Justin Ruggles
1130 c7cfc48f Justin Ruggles
/* Downmix the output.
1131
 * This function downmixes the output when the number of input
1132
 * channels is not equal to the number of output channels requested.
1133
 */
1134 1b293437 Justin Ruggles
static void do_downmix(AC3DecodeContext *ctx)
1135
{
1136 98a27a8a Justin Ruggles
    int from = ctx->acmod;
1137
    int to = ctx->blkoutput;
1138 1b293437 Justin Ruggles
1139 c7cfc48f Justin Ruggles
    if (to == AC3_OUTPUT_UNMODIFIED)
1140
        return;
1141
1142 1b293437 Justin Ruggles
    switch (from) {
1143 cc2a8443 Justin Ruggles
        case AC3_ACMOD_DUALMONO:
1144 1b293437 Justin Ruggles
            switch (to) {
1145
                case AC3_OUTPUT_MONO:
1146 00585845 Justin Ruggles
                    mix_dualmono_to_mono(ctx);
1147 1b293437 Justin Ruggles
                    break;
1148 486637af Justin Ruggles
                case AC3_OUTPUT_STEREO: /* We assume that sum of both mono channels is requested */
1149 00585845 Justin Ruggles
                    mix_dualmono_to_stereo(ctx);
1150 1b293437 Justin Ruggles
                    break;
1151
            }
1152
            break;
1153 cc2a8443 Justin Ruggles
        case AC3_ACMOD_MONO:
1154 1b293437 Justin Ruggles
            switch (to) {
1155
                case AC3_OUTPUT_STEREO:
1156 00585845 Justin Ruggles
                    upmix_mono_to_stereo(ctx);
1157 1b293437 Justin Ruggles
                    break;
1158
            }
1159
            break;
1160 cc2a8443 Justin Ruggles
        case AC3_ACMOD_STEREO:
1161 1b293437 Justin Ruggles
            switch (to) {
1162
                case AC3_OUTPUT_MONO:
1163 00585845 Justin Ruggles
                    mix_stereo_to_mono(ctx);
1164 1b293437 Justin Ruggles
                    break;
1165
            }
1166
            break;
1167 cc2a8443 Justin Ruggles
        case AC3_ACMOD_3F:
1168 1b293437 Justin Ruggles
            switch (to) {
1169
                case AC3_OUTPUT_MONO:
1170 00585845 Justin Ruggles
                    mix_3f_to_mono(ctx);
1171 1b293437 Justin Ruggles
                    break;
1172
                case AC3_OUTPUT_STEREO:
1173 00585845 Justin Ruggles
                    mix_3f_to_stereo(ctx);
1174 1b293437 Justin Ruggles
                    break;
1175
            }
1176
            break;
1177 cc2a8443 Justin Ruggles
        case AC3_ACMOD_2F1R:
1178 1b293437 Justin Ruggles
            switch (to) {
1179
                case AC3_OUTPUT_MONO:
1180 00585845 Justin Ruggles
                    mix_2f_1r_to_mono(ctx);
1181 1b293437 Justin Ruggles
                    break;
1182
                case AC3_OUTPUT_STEREO:
1183 00585845 Justin Ruggles
                    mix_2f_1r_to_stereo(ctx);
1184 1b293437 Justin Ruggles
                    break;
1185
                case AC3_OUTPUT_DOLBY:
1186 00585845 Justin Ruggles
                    mix_2f_1r_to_dolby(ctx);
1187 1b293437 Justin Ruggles
                    break;
1188
            }
1189
            break;
1190 cc2a8443 Justin Ruggles
        case AC3_ACMOD_3F1R:
1191 1b293437 Justin Ruggles
            switch (to) {
1192
                case AC3_OUTPUT_MONO:
1193 00585845 Justin Ruggles
                    mix_3f_1r_to_mono(ctx);
1194 1b293437 Justin Ruggles
                    break;
1195
                case AC3_OUTPUT_STEREO:
1196 00585845 Justin Ruggles
                    mix_3f_1r_to_stereo(ctx);
1197 1b293437 Justin Ruggles
                    break;
1198
                case AC3_OUTPUT_DOLBY:
1199 00585845 Justin Ruggles
                    mix_3f_1r_to_dolby(ctx);
1200 1b293437 Justin Ruggles
                    break;
1201
            }
1202
            break;
1203 cc2a8443 Justin Ruggles
        case AC3_ACMOD_2F2R:
1204 1b293437 Justin Ruggles
            switch (to) {
1205
                case AC3_OUTPUT_MONO:
1206 00585845 Justin Ruggles
                    mix_2f_2r_to_mono(ctx);
1207 1b293437 Justin Ruggles
                    break;
1208
                case AC3_OUTPUT_STEREO:
1209 00585845 Justin Ruggles
                    mix_2f_2r_to_stereo(ctx);
1210 1b293437 Justin Ruggles
                    break;
1211
                case AC3_OUTPUT_DOLBY:
1212 00585845 Justin Ruggles
                    mix_2f_2r_to_dolby(ctx);
1213 1b293437 Justin Ruggles
                    break;
1214
            }
1215
            break;
1216 cc2a8443 Justin Ruggles
        case AC3_ACMOD_3F2R:
1217 1b293437 Justin Ruggles
            switch (to) {
1218
                case AC3_OUTPUT_MONO:
1219 00585845 Justin Ruggles
                    mix_3f_2r_to_mono(ctx);
1220 1b293437 Justin Ruggles
                    break;
1221
                case AC3_OUTPUT_STEREO:
1222 00585845 Justin Ruggles
                    mix_3f_2r_to_stereo(ctx);
1223 1b293437 Justin Ruggles
                    break;
1224
                case AC3_OUTPUT_DOLBY:
1225 00585845 Justin Ruggles
                    mix_3f_2r_to_dolby(ctx);
1226 1b293437 Justin Ruggles
                    break;
1227
            }
1228
            break;
1229
    }
1230 2aa2c5c4 Justin Ruggles
}
1231
1232 c7cfc48f Justin Ruggles
/* This function performs the imdct on 256 sample transform
1233
 * coefficients.
1234
 */
1235 45b0ed13 Justin Ruggles
static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
1236 486637af Justin Ruggles
{
1237 0de73a46 Justin Ruggles
    int i, k;
1238
    float x[128];
1239
    FFTComplex z[2][64];
1240
    float *o_ptr = ctx->tmp_output;
1241
1242
    for(i=0; i<2; i++) {
1243
        /* de-interleave coefficients */
1244
        for(k=0; k<128; k++) {
1245
            x[k] = ctx->transform_coeffs[chindex][2*k+i];
1246
        }
1247 98a27a8a Justin Ruggles
1248 0de73a46 Justin Ruggles
        /* run standard IMDCT */
1249
        ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, o_ptr, x, ctx->tmp_imdct);
1250
1251
        /* reverse the post-rotation & reordering from standard IMDCT */
1252
        for(k=0; k<32; k++) {
1253
            z[i][32+k].re = -o_ptr[128+2*k];
1254
            z[i][32+k].im = -o_ptr[2*k];
1255
            z[i][31-k].re =  o_ptr[2*k+1];
1256
            z[i][31-k].im =  o_ptr[128+2*k+1];
1257
        }
1258 1ea76064 Justin Ruggles
    }
1259 486637af Justin Ruggles
1260 0de73a46 Justin Ruggles
    /* apply AC-3 post-rotation & reordering */
1261
    for(k=0; k<64; k++) {
1262
        o_ptr[    2*k  ] = -z[0][   k].im;
1263
        o_ptr[    2*k+1] =  z[0][63-k].re;
1264
        o_ptr[128+2*k  ] = -z[0][   k].re;
1265
        o_ptr[128+2*k+1] =  z[0][63-k].im;
1266
        o_ptr[256+2*k  ] = -z[1][   k].re;
1267
        o_ptr[256+2*k+1] =  z[1][63-k].im;
1268
        o_ptr[384+2*k  ] =  z[1][   k].im;
1269
        o_ptr[384+2*k+1] = -z[1][63-k].re;
1270
    }
1271 98a27a8a Justin Ruggles
}
1272 486637af Justin Ruggles
1273 c7cfc48f Justin Ruggles
/* IMDCT Transform. */
1274 486637af Justin Ruggles
static inline void do_imdct(AC3DecodeContext *ctx)
1275
{
1276 0de73a46 Justin Ruggles
    int ch;
1277 486637af Justin Ruggles
1278 98a27a8a Justin Ruggles
    if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
1279 0de73a46 Justin Ruggles
        ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1280
                                      ctx->transform_coeffs[0], ctx->tmp_imdct);
1281 486637af Justin Ruggles
    }
1282 0de73a46 Justin Ruggles
    for (ch=1; ch<=ctx->nfchans; ch++) {
1283 878c40a1 Justin Ruggles
        if (ctx->blksw[ch-1])
1284 0de73a46 Justin Ruggles
            do_imdct_256(ctx, ch);
1285 45b0ed13 Justin Ruggles
        else
1286 0de73a46 Justin Ruggles
            ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1287
                                          ctx->transform_coeffs[ch],
1288
                                          ctx->tmp_imdct);
1289
1290
        ctx->dsp.vector_fmul_add_add(ctx->output[ch], ctx->tmp_output,
1291
                                     ctx->window, ctx->delay[ch], 384, 256, 1);
1292
        ctx->dsp.vector_fmul_reverse(ctx->delay[ch], ctx->tmp_output+256,
1293
                                     ctx->window, 256);
1294 486637af Justin Ruggles
    }
1295
}
1296
1297 c7cfc48f Justin Ruggles
/* Parse the audio block from ac3 bitstream.
1298
 * This function extract the audio block from the ac3 bitstream
1299
 * and produces the output for the block. This function must
1300
 * be called for each of the six audio block in the ac3 bitstream.
1301
 */
1302 9fc1ab72 Justin Ruggles
static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
1303 2aa2c5c4 Justin Ruggles
{
1304 98a27a8a Justin Ruggles
    int nfchans = ctx->nfchans;
1305
    int acmod = ctx->acmod;
1306 00585845 Justin Ruggles
    int i, bnd, rbnd, seg, grpsize;
1307 1b293437 Justin Ruggles
    GetBitContext *gb = &ctx->gb;
1308
    int bit_alloc_flags = 0;
1309 bc8edb7e Justin Ruggles
    int8_t *dexps;
1310 2fbbd087 Justin Ruggles
    int mstrcplco, cplcoexp, cplcomant;
1311 98a27a8a Justin Ruggles
    int dynrng, chbwcod, ngrps, cplabsexp, skipl;
1312 1b293437 Justin Ruggles
1313
    for (i = 0; i < nfchans; i++) /*block switch flag */
1314 878c40a1 Justin Ruggles
        ctx->blksw[i] = get_bits1(gb);
1315 98a27a8a Justin Ruggles
1316 1b293437 Justin Ruggles
    for (i = 0; i < nfchans; i++) /* dithering flag */
1317 878c40a1 Justin Ruggles
        ctx->dithflag[i] = get_bits1(gb);
1318 98a27a8a Justin Ruggles
1319 00585845 Justin Ruggles
    if (get_bits1(gb)) { /* dynamic range */
1320 45b0ed13 Justin Ruggles
        dynrng = get_sbits(gb, 8);
1321 6dc5d71f Justin Ruggles
        ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1322 9fc1ab72 Justin Ruggles
    } else if(blk == 0) {
1323
        ctx->dynrng = 1.0;
1324 1b293437 Justin Ruggles
    }
1325 98a27a8a Justin Ruggles
1326 9fc1ab72 Justin Ruggles
    if(acmod == AC3_ACMOD_DUALMONO) { /* dynamic range 1+1 mode */
1327
        if(get_bits1(gb)) {
1328 bec37145 Justin Ruggles
            dynrng = get_sbits(gb, 8);
1329
            ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1330 9fc1ab72 Justin Ruggles
        } else if(blk == 0) {
1331
            ctx->dynrng2 = 1.0;
1332
        }
1333 98a27a8a Justin Ruggles
    }
1334
1335 1b293437 Justin Ruggles
    get_downmix_coeffs(ctx);
1336 98a27a8a Justin Ruggles
1337 00585845 Justin Ruggles
    if (get_bits1(gb)) { /* coupling strategy */
1338 98a27a8a Justin Ruggles
        ctx->cplinu = get_bits1(gb);
1339 2fbbd087 Justin Ruggles
        ctx->cplbndstrc = 0;
1340 98a27a8a Justin Ruggles
        if (ctx->cplinu) { /* coupling in use */
1341 1b293437 Justin Ruggles
            for (i = 0; i < nfchans; i++)
1342 878c40a1 Justin Ruggles
                ctx->chincpl[i] = get_bits1(gb);
1343 98a27a8a Justin Ruggles
1344 e2cd6686 Justin Ruggles
            if (acmod == AC3_ACMOD_STEREO)
1345 98a27a8a Justin Ruggles
                ctx->phsflginu = get_bits1(gb); //phase flag in use
1346
1347
            ctx->cplbegf = get_bits(gb, 4);
1348
            ctx->cplendf = get_bits(gb, 4);
1349
1350
            if (3 + ctx->cplendf - ctx->cplbegf < 0) {
1351
                av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", ctx->cplendf, ctx->cplbegf);
1352 00585845 Justin Ruggles
                return -1;
1353 98a27a8a Justin Ruggles
            }
1354
1355
            ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf;
1356
            ctx->cplstrtmant = ctx->cplbegf * 12 + 37;
1357
            ctx->cplendmant = ctx->cplendf * 12 + 73;
1358
            for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
1359 00585845 Justin Ruggles
                if (get_bits1(gb)) {
1360 98a27a8a Justin Ruggles
                    ctx->cplbndstrc |= 1 << i;
1361
                    ctx->ncplbnd--;
1362 1b293437 Justin Ruggles
                }
1363 878c40a1 Justin Ruggles
        } else {
1364
            for (i = 0; i < nfchans; i++)
1365
                ctx->chincpl[i] = 0;
1366 1b293437 Justin Ruggles
        }
1367
    }
1368 98a27a8a Justin Ruggles
1369
    if (ctx->cplinu) {
1370
        ctx->cplcoe = 0;
1371
1372 1b293437 Justin Ruggles
        for (i = 0; i < nfchans; i++)
1373 878c40a1 Justin Ruggles
            if (ctx->chincpl[i])
1374 00585845 Justin Ruggles
                if (get_bits1(gb)) { /* coupling co-ordinates */
1375 2fbbd087 Justin Ruggles
                    ctx->cplcoe |= 1 << i;
1376 486637af Justin Ruggles
                    mstrcplco = 3 * get_bits(gb, 2);
1377 98a27a8a Justin Ruggles
                    for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
1378 486637af Justin Ruggles
                        cplcoexp = get_bits(gb, 4);
1379
                        cplcomant = get_bits(gb, 4);
1380
                        if (cplcoexp == 15)
1381
                            cplcomant <<= 14;
1382
                        else
1383
                            cplcomant = (cplcomant | 0x10) << 13;
1384 98a27a8a Justin Ruggles
                        ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
1385 486637af Justin Ruggles
                    }
1386
                }
1387 98a27a8a Justin Ruggles
1388 e2cd6686 Justin Ruggles
        if (acmod == AC3_ACMOD_STEREO && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
1389 98a27a8a Justin Ruggles
            for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
1390
                if (get_bits1(gb))
1391
                    ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
1392 2aa2c5c4 Justin Ruggles
    }
1393 98a27a8a Justin Ruggles
1394 e2cd6686 Justin Ruggles
    if (acmod == AC3_ACMOD_STEREO) {/* rematrixing */
1395 98a27a8a Justin Ruggles
        ctx->rematstr = get_bits1(gb);
1396
        if (ctx->rematstr) {
1397
            if (!(ctx->cplinu) || ctx->cplbegf > 2)
1398 1b293437 Justin Ruggles
                for (rbnd = 0; rbnd < 4; rbnd++)
1399 878c40a1 Justin Ruggles
                    ctx->rematflg[rbnd] = get_bits1(gb);
1400 98a27a8a Justin Ruggles
            if (ctx->cplbegf > 0 && ctx->cplbegf <= 2 && ctx->cplinu)
1401 1b293437 Justin Ruggles
                for (rbnd = 0; rbnd < 3; rbnd++)
1402 878c40a1 Justin Ruggles
                    ctx->rematflg[rbnd] = get_bits1(gb);
1403 98a27a8a Justin Ruggles
            if (ctx->cplbegf == 0 && ctx->cplinu)
1404 1b293437 Justin Ruggles
                for (rbnd = 0; rbnd < 2; rbnd++)
1405 878c40a1 Justin Ruggles
                    ctx->rematflg[rbnd] = get_bits1(gb);
1406 1b293437 Justin Ruggles
        }
1407 98a27a8a Justin Ruggles
    }
1408
1409 cc2a8443 Justin Ruggles
    ctx->cplexpstr = EXP_REUSE;
1410
    ctx->lfeexpstr = EXP_REUSE;
1411 98a27a8a Justin Ruggles
    if (ctx->cplinu) /* coupling exponent strategy */
1412
        ctx->cplexpstr = get_bits(gb, 2);
1413
    for (i = 0; i < nfchans; i++)  /* channel exponent strategy */
1414
        ctx->chexpstr[i] = get_bits(gb, 2);
1415
    if (ctx->lfeon)  /* lfe exponent strategy */
1416
        ctx->lfeexpstr = get_bits1(gb);
1417
1418 1b293437 Justin Ruggles
    for (i = 0; i < nfchans; i++) /* channel bandwidth code */
1419 cc2a8443 Justin Ruggles
        if (ctx->chexpstr[i] != EXP_REUSE) {
1420 878c40a1 Justin Ruggles
            if (ctx->chincpl[i])
1421 98a27a8a Justin Ruggles
                ctx->endmant[i] = ctx->cplstrtmant;
1422 00585845 Justin Ruggles
            else {
1423 98a27a8a Justin Ruggles
                chbwcod = get_bits(gb, 6);
1424
                if (chbwcod > 60) {
1425
                    av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
1426 00585845 Justin Ruggles
                    return -1;
1427
                }
1428 98a27a8a Justin Ruggles
                ctx->endmant[i] = chbwcod * 3 + 73;
1429 1b293437 Justin Ruggles
            }
1430 00585845 Justin Ruggles
        }
1431 98a27a8a Justin Ruggles
1432 cc2a8443 Justin Ruggles
    if (ctx->cplexpstr != EXP_REUSE) {/* coupling exponents */
1433 2fbbd087 Justin Ruggles
        bit_alloc_flags = 64;
1434 98a27a8a Justin Ruggles
        cplabsexp = get_bits(gb, 4) << 1;
1435
        ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
1436 4415076f Justin Ruggles
        decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant);
1437 486637af Justin Ruggles
    }
1438 98a27a8a Justin Ruggles
1439 1b293437 Justin Ruggles
    for (i = 0; i < nfchans; i++) /* fbw channel exponents */
1440 cc2a8443 Justin Ruggles
        if (ctx->chexpstr[i] != EXP_REUSE) {
1441 1b293437 Justin Ruggles
            bit_alloc_flags |= 1 << i;
1442 98a27a8a Justin Ruggles
            grpsize = 3 << (ctx->chexpstr[i] - 1);
1443
            ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
1444
            dexps = ctx->dexps[i];
1445 00585845 Justin Ruggles
            dexps[0] = get_bits(gb, 4);
1446 4415076f Justin Ruggles
            decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1);
1447 98a27a8a Justin Ruggles
            skip_bits(gb, 2); /* skip gainrng */
1448 1b293437 Justin Ruggles
        }
1449 98a27a8a Justin Ruggles
1450 cc2a8443 Justin Ruggles
    if (ctx->lfeexpstr != EXP_REUSE) { /* lfe exponents */
1451 486637af Justin Ruggles
        bit_alloc_flags |= 32;
1452 98a27a8a Justin Ruggles
        ctx->dlfeexps[0] = get_bits(gb, 4);
1453 4415076f Justin Ruggles
        decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1);
1454 486637af Justin Ruggles
    }
1455 1b293437 Justin Ruggles
1456 00585845 Justin Ruggles
    if (get_bits1(gb)) { /* bit allocation information */
1457 2fbbd087 Justin Ruggles
        bit_alloc_flags = 127;
1458 8f58a4c9 Justin Ruggles
        ctx->bit_alloc_params.sdecay = ff_sdecaytab[get_bits(gb, 2)];
1459
        ctx->bit_alloc_params.fdecay = ff_fdecaytab[get_bits(gb, 2)];
1460
        ctx->bit_alloc_params.sgain  = ff_sgaintab[get_bits(gb, 2)];
1461
        ctx->bit_alloc_params.dbknee = ff_dbkneetab[get_bits(gb, 2)];
1462
        ctx->bit_alloc_params.floor  = ff_floortab[get_bits(gb, 3)];
1463 1b293437 Justin Ruggles
    }
1464 98a27a8a Justin Ruggles
1465 00585845 Justin Ruggles
    if (get_bits1(gb)) { /* snroffset */
1466 2fbbd087 Justin Ruggles
        bit_alloc_flags = 127;
1467 98a27a8a Justin Ruggles
        ctx->csnroffst = get_bits(gb, 6);
1468 de17eee7 Justin Ruggles
        if (ctx->cplinu) { /* coupling fine snr offset and fast gain code */
1469 98a27a8a Justin Ruggles
            ctx->cplfsnroffst = get_bits(gb, 4);
1470
            ctx->cplfgaincod = get_bits(gb, 3);
1471 1b293437 Justin Ruggles
        }
1472
        for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
1473 98a27a8a Justin Ruggles
            ctx->fsnroffst[i] = get_bits(gb, 4);
1474
            ctx->fgaincod[i] = get_bits(gb, 3);
1475 1b293437 Justin Ruggles
        }
1476 98a27a8a Justin Ruggles
        if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
1477
            ctx->lfefsnroffst = get_bits(gb, 4);
1478
            ctx->lfefgaincod = get_bits(gb, 3);
1479 1b293437 Justin Ruggles
        }
1480
    }
1481 98a27a8a Justin Ruggles
1482
    if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
1483
        bit_alloc_flags |= 64;
1484 8f58a4c9 Justin Ruggles
        ctx->bit_alloc_params.cplfleak = get_bits(gb, 3);
1485
        ctx->bit_alloc_params.cplsleak = get_bits(gb, 3);
1486 98a27a8a Justin Ruggles
    }
1487
1488 00585845 Justin Ruggles
    if (get_bits1(gb)) { /* delta bit allocation information */
1489 2fbbd087 Justin Ruggles
        bit_alloc_flags = 127;
1490 98a27a8a Justin Ruggles
1491
        if (ctx->cplinu) {
1492
            ctx->cpldeltbae = get_bits(gb, 2);
1493 cc2a8443 Justin Ruggles
            if (ctx->cpldeltbae == DBA_RESERVED) {
1494 1b293437 Justin Ruggles
                av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
1495
                return -1;
1496
            }
1497
        }
1498 98a27a8a Justin Ruggles
1499 1b293437 Justin Ruggles
        for (i = 0; i < nfchans; i++) {
1500 98a27a8a Justin Ruggles
            ctx->deltbae[i] = get_bits(gb, 2);
1501 cc2a8443 Justin Ruggles
            if (ctx->deltbae[i] == DBA_RESERVED) {
1502 1b293437 Justin Ruggles
                av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
1503
                return -1;
1504
            }
1505
        }
1506 98a27a8a Justin Ruggles
1507
        if (ctx->cplinu)
1508 cc2a8443 Justin Ruggles
            if (ctx->cpldeltbae == DBA_NEW) { /*coupling delta offset, len and bit allocation */
1509 98a27a8a Justin Ruggles
                ctx->cpldeltnseg = get_bits(gb, 3);
1510
                for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
1511
                    ctx->cpldeltoffst[seg] = get_bits(gb, 5);
1512
                    ctx->cpldeltlen[seg] = get_bits(gb, 4);
1513
                    ctx->cpldeltba[seg] = get_bits(gb, 3);
1514 1b293437 Justin Ruggles
                }
1515
            }
1516 98a27a8a Justin Ruggles
1517 1b293437 Justin Ruggles
        for (i = 0; i < nfchans; i++)
1518 cc2a8443 Justin Ruggles
            if (ctx->deltbae[i] == DBA_NEW) {/*channel delta offset, len and bit allocation */
1519 98a27a8a Justin Ruggles
                ctx->deltnseg[i] = get_bits(gb, 3);
1520
                for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
1521
                    ctx->deltoffst[i][seg] = get_bits(gb, 5);
1522
                    ctx->deltlen[i][seg] = get_bits(gb, 4);
1523
                    ctx->deltba[i][seg] = get_bits(gb, 3);
1524 1b293437 Justin Ruggles
                }
1525
            }
1526 9fc1ab72 Justin Ruggles
    } else if(blk == 0) {
1527
        if(ctx->cplinu)
1528
            ctx->cpldeltbae = DBA_NONE;
1529
        for(i=0; i<nfchans; i++) {
1530
            ctx->deltbae[i] = DBA_NONE;
1531
        }
1532 1b293437 Justin Ruggles
    }
1533 00585845 Justin Ruggles
1534 2fbbd087 Justin Ruggles
    if (bit_alloc_flags) {
1535 878c40a1 Justin Ruggles
        if (ctx->cplinu && (bit_alloc_flags & 64))
1536 d699d383 Justin Ruggles
            do_bit_allocation(ctx, 5);
1537
        for (i = 0; i < nfchans; i++)
1538
            if ((bit_alloc_flags >> i) & 1)
1539
                do_bit_allocation(ctx, i);
1540
        if (ctx->lfeon && (bit_alloc_flags & 32))
1541
            do_bit_allocation(ctx, 6);
1542 2fbbd087 Justin Ruggles
    }
1543 98a27a8a Justin Ruggles
1544 00585845 Justin Ruggles
    if (get_bits1(gb)) { /* unused dummy data */
1545 98a27a8a Justin Ruggles
        skipl = get_bits(gb, 9);
1546
        while(skipl--)
1547 00585845 Justin Ruggles
            skip_bits(gb, 8);
1548 1b293437 Justin Ruggles
    }
1549
    /* unpack the transform coefficients
1550
     * * this also uncouples channels if coupling is in use.
1551
     */
1552
    if (get_transform_coeffs(ctx)) {
1553
        av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1554
        return -1;
1555
    }
1556 486637af Justin Ruggles
1557 1b293437 Justin Ruggles
    /* recover coefficients if rematrixing is in use */
1558 878c40a1 Justin Ruggles
    if(ctx->acmod == AC3_ACMOD_STEREO)
1559 1b293437 Justin Ruggles
        do_rematrixing(ctx);
1560
1561 d7bcc4ad Justin Ruggles
    do_downmix(ctx);
1562
1563 486637af Justin Ruggles
    do_imdct(ctx);
1564
1565 1b293437 Justin Ruggles
    return 0;
1566
}
1567
1568 d7bcc4ad Justin Ruggles
static inline int16_t convert(int32_t i)
1569
{
1570
    if (i > 0x43c07fff)
1571
        return 32767;
1572
    else if (i <= 0x43bf8000)
1573
        return -32768;
1574
    else
1575
        return (i - 0x43c00000);
1576 486637af Justin Ruggles
}
1577
1578 c7cfc48f Justin Ruggles
/* Decode ac3 frame.
1579
 *
1580
 * @param avctx Pointer to AVCodecContext
1581
 * @param data Pointer to pcm smaples
1582
 * @param data_size Set to number of pcm samples produced by decoding
1583
 * @param buf Data to be decoded
1584
 * @param buf_size Size of the buffer
1585
 */
1586 00585845 Justin Ruggles
static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1587 1b293437 Justin Ruggles
{
1588 00585845 Justin Ruggles
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1589
    int16_t *out_samples = (int16_t *)data;
1590 d7bcc4ad Justin Ruggles
    int i, j, k, start;
1591
    int32_t *int_ptr[6];
1592 1b293437 Justin Ruggles
1593 d7bcc4ad Justin Ruggles
    for (i = 0; i < 6; i++)
1594
        int_ptr[i] = (int32_t *)(&ctx->output[i]);
1595
1596 1b293437 Justin Ruggles
    //Initialize the GetBitContext with the start of valid AC3 Frame.
1597 9ef60390 Justin Ruggles
    init_get_bits(&ctx->gb, buf, buf_size * 8);
1598 00585845 Justin Ruggles
1599 1b293437 Justin Ruggles
    //Parse the syncinfo.
1600 9fc1ab72 Justin Ruggles
    if (ac3_parse_header(ctx)) {
1601 00585845 Justin Ruggles
        av_log(avctx, AV_LOG_ERROR, "\n");
1602 1b293437 Justin Ruggles
        *data_size = 0;
1603 486637af Justin Ruggles
        return buf_size;
1604 1b293437 Justin Ruggles
    }
1605 2aa2c5c4 Justin Ruggles
1606 98a27a8a Justin Ruggles
    avctx->sample_rate = ctx->sampling_rate;
1607
    avctx->bit_rate = ctx->bit_rate;
1608 45b0ed13 Justin Ruggles
1609 1b293437 Justin Ruggles
    if (avctx->channels == 0) {
1610 98a27a8a Justin Ruggles
        ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1611 45b0ed13 Justin Ruggles
        if (ctx->lfeon)
1612
            ctx->blkoutput |= AC3_OUTPUT_LFEON;
1613
        avctx->channels = ctx->nfchans + ctx->lfeon;
1614
    }
1615
    else if (avctx->channels == 1)
1616 98a27a8a Justin Ruggles
        ctx->blkoutput |= AC3_OUTPUT_MONO;
1617 45b0ed13 Justin Ruggles
    else if (avctx->channels == 2) {
1618 98a27a8a Justin Ruggles
        if (ctx->dsurmod == 0x02)
1619
            ctx->blkoutput |= AC3_OUTPUT_DOLBY;
1620 1b293437 Justin Ruggles
        else
1621 98a27a8a Justin Ruggles
            ctx->blkoutput |= AC3_OUTPUT_STEREO;
1622 486637af Justin Ruggles
    }
1623 45b0ed13 Justin Ruggles
    else {
1624
        if (avctx->channels < (ctx->nfchans + ctx->lfeon))
1625
            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);
1626
        ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1627
        if (ctx->lfeon)
1628
            ctx->blkoutput |= AC3_OUTPUT_LFEON;
1629
        avctx->channels = ctx->nfchans + ctx->lfeon;
1630 1b293437 Justin Ruggles
    }
1631
1632 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);
1633 1b293437 Justin Ruggles
1634
    //Parse the Audio Blocks.
1635 cc2a8443 Justin Ruggles
    for (i = 0; i < NB_BLOCKS; i++) {
1636 9fc1ab72 Justin Ruggles
        if (ac3_parse_audio_block(ctx, i)) {
1637 1b293437 Justin Ruggles
            av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1638
            *data_size = 0;
1639 98a27a8a Justin Ruggles
            return ctx->frame_size;
1640 1b293437 Justin Ruggles
        }
1641 d7bcc4ad Justin Ruggles
        start = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1;
1642
        for (k = 0; k < BLOCK_SIZE; k++)
1643
            for (j = start; j <= avctx->channels; j++)
1644
                *(out_samples++) = convert(int_ptr[j][k]);
1645 1b293437 Justin Ruggles
    }
1646 cc2a8443 Justin Ruggles
    *data_size = NB_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);
1647 98a27a8a Justin Ruggles
    return ctx->frame_size;
1648 2aa2c5c4 Justin Ruggles
}
1649 1b293437 Justin Ruggles
1650 c7cfc48f Justin Ruggles
/* Uninitialize ac3 decoder.
1651
 */
1652
static int ac3_decode_end(AVCodecContext *avctx)
1653 1b293437 Justin Ruggles
{
1654 c7cfc48f Justin Ruggles
    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1655
    ff_mdct_end(&ctx->imdct_512);
1656
    ff_mdct_end(&ctx->imdct_256);
1657
1658 1b293437 Justin Ruggles
    return 0;
1659
}
1660
1661 fa67992d Justin Ruggles
AVCodec ac3_decoder = {
1662 e6bca37c Justin Ruggles
    .name = "ac3",
1663
    .type = CODEC_TYPE_AUDIO,
1664
    .id = CODEC_ID_AC3,
1665
    .priv_data_size = sizeof (AC3DecodeContext),
1666
    .init = ac3_decode_init,
1667
    .close = ac3_decode_end,
1668
    .decode = ac3_decode_frame,
1669 1b293437 Justin Ruggles
};