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