]> git.sesse.net Git - ffmpeg/blob - libavcodec/ac3dec.c
fix random dithering of zero-bit mantissas
[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  * Copyright (c) 2007 Justin Ruggles
7  *
8  * Portions of this code are derived from liba52
9  * http://liba52.sourceforge.net
10  * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
11  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
12  *
13  * This file is part of FFmpeg.
14  *
15  * FFmpeg is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public
17  * License as published by the Free Software Foundation; either
18  * version 2 of the License, or (at your option) any later version.
19  *
20  * FFmpeg is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  * General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public
26  * License along with FFmpeg; if not, write to the Free Software
27  * 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 #include "avcodec.h"
36 #include "ac3_parser.h"
37 #include "bitstream.h"
38 #include "dsputil.h"
39 #include "random.h"
40
41 /**
42  * Table of bin locations for rematrixing bands
43  * reference: Section 7.5.2 Rematrixing : Frequency Band Definitions
44  */
45 static const uint8_t rematrix_band_tab[5] = { 13, 25, 37, 61, 253 };
46
47 /**
48  * table for exponent to scale_factor mapping
49  * scale_factors[i] = 2 ^ -i
50  */
51 static float scale_factors[25];
52
53 /** table for grouping exponents */
54 static uint8_t exp_ungroup_tab[128][3];
55
56
57 /** tables for ungrouping mantissas */
58 static float b1_mantissas[32][3];
59 static float b2_mantissas[128][3];
60 static float b3_mantissas[8];
61 static float b4_mantissas[128][2];
62 static float b5_mantissas[16];
63
64 /**
65  * Quantization table: levels for symmetric. bits for asymmetric.
66  * reference: Table 7.18 Mapping of bap to Quantizer
67  */
68 static const uint8_t qntztab[16] = {
69     0, 3, 5, 7, 11, 15,
70     5, 6, 7, 8, 9, 10, 11, 12, 14, 16
71 };
72
73 /** dynamic range table. converts codes to scale factors. */
74 static float dynrng_tab[256];
75
76 /** dialogue normalization table */
77 static float dialnorm_tab[32];
78
79 /** Adjustments in dB gain */
80 #define LEVEL_MINUS_3DB         0.7071067811865476
81 #define LEVEL_MINUS_4POINT5DB   0.5946035575013605
82 #define LEVEL_MINUS_6DB         0.5000000000000000
83 #define LEVEL_MINUS_9DB         0.3535533905932738
84 #define LEVEL_ZERO              0.0000000000000000
85 #define LEVEL_ONE               1.0000000000000000
86
87 static const float gain_levels[6] = {
88     LEVEL_ZERO,
89     LEVEL_ONE,
90     LEVEL_MINUS_3DB,
91     LEVEL_MINUS_4POINT5DB,
92     LEVEL_MINUS_6DB,
93     LEVEL_MINUS_9DB
94 };
95
96 /**
97  * Table for center mix levels
98  * reference: Section 5.4.2.4 cmixlev
99  */
100 static const uint8_t clevs[4] = { 2, 3, 4, 3 };
101
102 /**
103  * Table for surround mix levels
104  * reference: Section 5.4.2.5 surmixlev
105  */
106 static const uint8_t slevs[4] = { 2, 4, 0, 4 };
107
108 /**
109  * Table for default stereo downmixing coefficients
110  * reference: Section 7.8.2 Downmixing Into Two Channels
111  */
112 static const uint8_t ac3_default_coeffs[8][5][2] = {
113     { { 1, 0 }, { 0, 1 },                               },
114     { { 2, 2 },                                         },
115     { { 1, 0 }, { 0, 1 },                               },
116     { { 1, 0 }, { 3, 3 }, { 0, 1 },                     },
117     { { 1, 0 }, { 0, 1 }, { 4, 4 },                     },
118     { { 1, 0 }, { 3, 3 }, { 0, 1 }, { 5, 5 },           },
119     { { 1, 0 }, { 0, 1 }, { 4, 0 }, { 0, 4 },           },
120     { { 1, 0 }, { 3, 3 }, { 0, 1 }, { 4, 0 }, { 0, 4 }, },
121 };
122
123 /* override ac3.h to include coupling channel */
124 #undef AC3_MAX_CHANNELS
125 #define AC3_MAX_CHANNELS 7
126 #define CPL_CH 0
127
128 #define AC3_OUTPUT_LFEON  8
129
130 typedef struct {
131     int acmod;                              ///< audio coding mode
132     int dsurmod;                            ///< dolby surround mode
133     int blksw[AC3_MAX_CHANNELS];            ///< block switch flags
134     int dithflag[AC3_MAX_CHANNELS];         ///< dither flags
135     int dither_all;                         ///< true if all channels are dithered
136     int cplinu;                             ///< coupling in use
137     int chincpl[AC3_MAX_CHANNELS];          ///< channel in coupling
138     int phsflginu;                          ///< phase flags in use
139     int cplbndstrc[18];                     ///< coupling band structure
140     int rematstr;                           ///< rematrixing strategy
141     int nrematbnd;                          ///< number of rematrixing bands
142     int rematflg[4];                        ///< rematrixing flags
143     int expstr[AC3_MAX_CHANNELS];           ///< exponent strategies
144     int snroffst[AC3_MAX_CHANNELS];         ///< signal-to-noise ratio offsets
145     int fgain[AC3_MAX_CHANNELS];            ///< fast gain values (signal-to-mask ratio)
146     int deltbae[AC3_MAX_CHANNELS];          ///< delta bit allocation exists
147     int deltnseg[AC3_MAX_CHANNELS];         ///< number of delta segments
148     uint8_t deltoffst[AC3_MAX_CHANNELS][8]; ///< delta segment offsets
149     uint8_t deltlen[AC3_MAX_CHANNELS][8];   ///< delta segment lengths
150     uint8_t deltba[AC3_MAX_CHANNELS][8];    ///< delta values for each segment
151
152     int sampling_rate;                      ///< sample frequency, in Hz
153     int bit_rate;                           ///< stream bit rate, in bits-per-second
154     int frame_size;                         ///< current frame size, in bytes
155
156     int nchans;                             ///< number of total channels
157     int nfchans;                            ///< number of full-bandwidth channels
158     int lfeon;                              ///< lfe channel in use
159     int lfe_ch;                             ///< index of LFE channel
160     int output_mode;                        ///< output channel configuration
161     int out_channels;                       ///< number of output channels
162
163     float downmix_coeffs[AC3_MAX_CHANNELS][2];  ///< stereo downmix coefficients
164     float dialnorm[2];                      ///< dialogue normalization
165     float dynrng[2];                        ///< dynamic range
166     float cplco[AC3_MAX_CHANNELS][18];      ///< coupling coordinates
167     int   ncplbnd;                          ///< number of coupling bands
168     int   ncplsubnd;                        ///< number of coupling sub bands
169     int   startmant[AC3_MAX_CHANNELS];      ///< start frequency bin
170     int   endmant[AC3_MAX_CHANNELS];        ///< end frequency bin
171     AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
172
173     int8_t  dexps[AC3_MAX_CHANNELS][256];   ///< decoded exponents
174     uint8_t bap[AC3_MAX_CHANNELS][256];     ///< bit allocation pointers
175     int16_t psd[AC3_MAX_CHANNELS][256];     ///< scaled exponents
176     int16_t bndpsd[AC3_MAX_CHANNELS][50];   ///< interpolated exponents
177     int16_t mask[AC3_MAX_CHANNELS][50];     ///< masking curve values
178
179     DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][256]);  ///< transform coefficients
180
181     /* For IMDCT. */
182     MDCTContext imdct_512;                  ///< for 512 sample IMDCT
183     MDCTContext imdct_256;                  ///< for 256 sample IMDCT
184     DSPContext  dsp;                        ///< for optimization
185     float       add_bias;                   ///< offset for float_to_int16 conversion
186     float       mul_bias;                   ///< scaling for float_to_int16 conversion
187
188     DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS-1][256]);     ///< output after imdct transform and windowing
189     DECLARE_ALIGNED_16(short, int_output[AC3_MAX_CHANNELS-1][256]); ///< final 16-bit integer output
190     DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS-1][256]);      ///< delay - added to the next block
191     DECLARE_ALIGNED_16(float, tmp_imdct[256]);                      ///< temporary storage for imdct transform
192     DECLARE_ALIGNED_16(float, tmp_output[512]);                     ///< temporary storage for output before windowing
193     DECLARE_ALIGNED_16(float, window[256]);                         ///< window coefficients
194
195     /* Miscellaneous. */
196     GetBitContext gb;                       ///< bitstream reader
197     AVRandomState dith_state;               ///< for dither generation
198     AVCodecContext *avctx;                  ///< parent context
199 } AC3DecodeContext;
200
201 /**
202  * Generate a Kaiser-Bessel Derived Window.
203  */
204 static void ac3_window_init(float *window)
205 {
206    int i, j;
207    double sum = 0.0, bessel, tmp;
208    double local_window[256];
209    double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
210
211    for (i = 0; i < 256; i++) {
212        tmp = i * (256 - i) * alpha2;
213        bessel = 1.0;
214        for (j = 100; j > 0; j--) /* default to 100 iterations */
215            bessel = bessel * tmp / (j * j) + 1;
216        sum += bessel;
217        local_window[i] = sum;
218    }
219
220    sum++;
221    for (i = 0; i < 256; i++)
222        window[i] = sqrt(local_window[i] / sum);
223 }
224
225 /**
226  * Symmetrical Dequantization
227  * reference: Section 7.3.3 Expansion of Mantissas for Symmetrical Quantization
228  *            Tables 7.19 to 7.23
229  */
230 static inline float
231 symmetric_dequant(int code, int levels)
232 {
233     return (code - (levels >> 1)) * (2.0f / levels);
234 }
235
236 /*
237  * Initialize tables at runtime.
238  */
239 static void ac3_tables_init(void)
240 {
241     int i;
242
243     /* generate grouped mantissa tables
244        reference: Section 7.3.5 Ungrouping of Mantissas */
245     for(i=0; i<32; i++) {
246         /* bap=1 mantissas */
247         b1_mantissas[i][0] = symmetric_dequant( i / 9     , 3);
248         b1_mantissas[i][1] = symmetric_dequant((i % 9) / 3, 3);
249         b1_mantissas[i][2] = symmetric_dequant((i % 9) % 3, 3);
250     }
251     for(i=0; i<128; i++) {
252         /* bap=2 mantissas */
253         b2_mantissas[i][0] = symmetric_dequant( i / 25     , 5);
254         b2_mantissas[i][1] = symmetric_dequant((i % 25) / 5, 5);
255         b2_mantissas[i][2] = symmetric_dequant((i % 25) % 5, 5);
256
257         /* bap=4 mantissas */
258         b4_mantissas[i][0] = symmetric_dequant(i / 11, 11);
259         b4_mantissas[i][1] = symmetric_dequant(i % 11, 11);
260     }
261     /* generate ungrouped mantissa tables
262        reference: Tables 7.21 and 7.23 */
263     for(i=0; i<7; i++) {
264         /* bap=3 mantissas */
265         b3_mantissas[i] = symmetric_dequant(i, 7);
266     }
267     for(i=0; i<15; i++) {
268         /* bap=5 mantissas */
269         b5_mantissas[i] = symmetric_dequant(i, 15);
270     }
271
272     /* generate dynamic range table
273        reference: Section 7.7.1 Dynamic Range Control */
274     for(i=0; i<256; i++) {
275         int v = (i >> 5) - ((i >> 7) << 3) - 5;
276         dynrng_tab[i] = powf(2.0f, v) * ((i & 0x1F) | 0x20);
277     }
278
279     /* generate dialogue normalization table
280        references: Section 5.4.2.8 dialnorm
281                    Section 7.6 Dialogue Normalization */
282     for(i=1; i<32; i++) {
283         dialnorm_tab[i] = expf((i-31) * M_LN10 / 20.0f);
284     }
285     dialnorm_tab[0] = dialnorm_tab[31];
286
287     /* generate scale factors for exponents and asymmetrical dequantization
288        reference: Section 7.3.2 Expansion of Mantissas for Asymmetric Quantization */
289     for (i = 0; i < 25; i++)
290         scale_factors[i] = pow(2.0, -i);
291
292     /* generate exponent tables
293        reference: Section 7.1.3 Exponent Decoding */
294     for(i=0; i<128; i++) {
295         exp_ungroup_tab[i][0] =  i / 25;
296         exp_ungroup_tab[i][1] = (i % 25) / 5;
297         exp_ungroup_tab[i][2] = (i % 25) % 5;
298     }
299 }
300
301
302 /**
303  * AVCodec initialization
304  */
305 static int ac3_decode_init(AVCodecContext *avctx)
306 {
307     AC3DecodeContext *ctx = avctx->priv_data;
308     ctx->avctx = avctx;
309
310     ac3_common_init();
311     ac3_tables_init();
312     ff_mdct_init(&ctx->imdct_256, 8, 1);
313     ff_mdct_init(&ctx->imdct_512, 9, 1);
314     ac3_window_init(ctx->window);
315     dsputil_init(&ctx->dsp, avctx);
316     av_init_random(0, &ctx->dith_state);
317
318     /* set bias values for float to int16 conversion */
319     if(ctx->dsp.float_to_int16 == ff_float_to_int16_c) {
320         ctx->add_bias = 385.0f;
321         ctx->mul_bias = 1.0f;
322     } else {
323         ctx->add_bias = 0.0f;
324         ctx->mul_bias = 32767.0f;
325     }
326
327     return 0;
328 }
329
330 /**
331  * Parse the 'sync info' and 'bit stream info' from the AC-3 bitstream.
332  * GetBitContext within AC3DecodeContext must point to
333  * start of the synchronized ac3 bitstream.
334  */
335 static int ac3_parse_header(AC3DecodeContext *ctx)
336 {
337     AC3HeaderInfo hdr;
338     GetBitContext *gb = &ctx->gb;
339     float cmixlev, surmixlev;
340     int err, i;
341
342     err = ff_ac3_parse_header(gb->buffer, &hdr);
343     if(err)
344         return err;
345
346     /* get decoding parameters from header info */
347     ctx->bit_alloc_params.fscod       = hdr.fscod;
348     ctx->acmod                        = hdr.acmod;
349     cmixlev                           = gain_levels[clevs[hdr.cmixlev]];
350     surmixlev                         = gain_levels[slevs[hdr.surmixlev]];
351     ctx->dsurmod                      = hdr.dsurmod;
352     ctx->lfeon                        = hdr.lfeon;
353     ctx->bit_alloc_params.halfratecod = hdr.halfratecod;
354     ctx->sampling_rate                = hdr.sample_rate;
355     ctx->bit_rate                     = hdr.bit_rate;
356     ctx->nchans                       = hdr.channels;
357     ctx->nfchans                      = ctx->nchans - ctx->lfeon;
358     ctx->lfe_ch                       = ctx->nfchans + 1;
359     ctx->frame_size                   = hdr.frame_size;
360
361     /* set default output to all source channels */
362     ctx->out_channels = ctx->nchans;
363     ctx->output_mode = ctx->acmod;
364     if(ctx->lfeon)
365         ctx->output_mode |= AC3_OUTPUT_LFEON;
366
367     /* skip over portion of header which has already been read */
368     skip_bits(gb, 16); // skip the sync_word
369     skip_bits(gb, 16); // skip crc1
370     skip_bits(gb, 8);  // skip fscod and frmsizecod
371     skip_bits(gb, 11); // skip bsid, bsmod, and acmod
372     if(ctx->acmod == AC3_ACMOD_STEREO) {
373         skip_bits(gb, 2); // skip dsurmod
374     } else {
375         if((ctx->acmod & 1) && ctx->acmod != AC3_ACMOD_MONO)
376             skip_bits(gb, 2); // skip cmixlev
377         if(ctx->acmod & 4)
378             skip_bits(gb, 2); // skip surmixlev
379     }
380     skip_bits1(gb); // skip lfeon
381
382     /* read the rest of the bsi. read twice for dual mono mode. */
383     i = !(ctx->acmod);
384     do {
385         ctx->dialnorm[i] = dialnorm_tab[get_bits(gb, 5)]; // dialogue normalization
386         if (get_bits1(gb))
387             skip_bits(gb, 8); //skip compression
388         if (get_bits1(gb))
389             skip_bits(gb, 8); //skip language code
390         if (get_bits1(gb))
391             skip_bits(gb, 7); //skip audio production information
392     } while (i--);
393
394     skip_bits(gb, 2); //skip copyright bit and original bitstream bit
395
396     /* skip the timecodes (or extra bitstream information for Alternate Syntax)
397        TODO: read & use the xbsi1 downmix levels */
398     if (get_bits1(gb))
399         skip_bits(gb, 14); //skip timecode1 / xbsi1
400     if (get_bits1(gb))
401         skip_bits(gb, 14); //skip timecode2 / xbsi2
402
403     /* skip additional bitstream info */
404     if (get_bits1(gb)) {
405         i = get_bits(gb, 6);
406         do {
407             skip_bits(gb, 8);
408         } while(i--);
409     }
410
411     /* set stereo downmixing coefficients
412        reference: Section 7.8.2 Downmixing Into Two Channels */
413     for(i=0; i<ctx->nfchans; i++) {
414         ctx->downmix_coeffs[i][0] = gain_levels[ac3_default_coeffs[ctx->acmod][i][0]];
415         ctx->downmix_coeffs[i][1] = gain_levels[ac3_default_coeffs[ctx->acmod][i][1]];
416     }
417     if(ctx->acmod > 1 && ctx->acmod & 1) {
418         ctx->downmix_coeffs[1][0] = ctx->downmix_coeffs[1][1] = cmixlev;
419     }
420     if(ctx->acmod == AC3_ACMOD_2F1R || ctx->acmod == AC3_ACMOD_3F1R) {
421         int nf = ctx->acmod - 2;
422         ctx->downmix_coeffs[nf][0] = ctx->downmix_coeffs[nf][1] = surmixlev * LEVEL_MINUS_3DB;
423     }
424     if(ctx->acmod == AC3_ACMOD_2F2R || ctx->acmod == AC3_ACMOD_3F2R) {
425         int nf = ctx->acmod - 4;
426         ctx->downmix_coeffs[nf][0] = ctx->downmix_coeffs[nf+1][1] = surmixlev;
427     }
428
429     return 0;
430 }
431
432 /**
433  * Decode the grouped exponents according to exponent strategy.
434  * reference: Section 7.1.3 Exponent Decoding
435  */
436 static void decode_exponents(GetBitContext *gb, int expstr, int ngrps,
437                              uint8_t absexp, int8_t *dexps)
438 {
439     int i, j, grp, grpsize;
440     int dexp[256];
441     int expacc, prevexp;
442
443     /* unpack groups */
444     grpsize = expstr + (expstr == EXP_D45);
445     for(grp=0,i=0; grp<ngrps; grp++) {
446         expacc = get_bits(gb, 7);
447         dexp[i++] = exp_ungroup_tab[expacc][0];
448         dexp[i++] = exp_ungroup_tab[expacc][1];
449         dexp[i++] = exp_ungroup_tab[expacc][2];
450     }
451
452     /* convert to absolute exps and expand groups */
453     prevexp = absexp;
454     for(i=0; i<ngrps*3; i++) {
455         prevexp = av_clip(prevexp + dexp[i]-2, 0, 24);
456         for(j=0; j<grpsize; j++) {
457             dexps[(i*grpsize)+j] = prevexp;
458         }
459     }
460 }
461
462 /**
463  * Generate transform coefficients for each coupled channel in the coupling
464  * range using the coupling coefficients and coupling coordinates.
465  * reference: Section 7.4.3 Coupling Coordinate Format
466  */
467 static void uncouple_channels(AC3DecodeContext *ctx)
468 {
469     int i, j, ch, bnd, subbnd;
470
471     subbnd = -1;
472     i = ctx->startmant[CPL_CH];
473     for(bnd=0; bnd<ctx->ncplbnd; bnd++) {
474         do {
475             subbnd++;
476             for(j=0; j<12; j++) {
477                 for(ch=1; ch<=ctx->nfchans; ch++) {
478                     if(ctx->chincpl[ch])
479                         ctx->transform_coeffs[ch][i] = ctx->transform_coeffs[CPL_CH][i] * ctx->cplco[ch][bnd] * 8.0f;
480                 }
481                 i++;
482             }
483         } while(ctx->cplbndstrc[subbnd]);
484     }
485 }
486
487 /**
488  * Grouped mantissas for 3-level 5-level and 11-level quantization
489  */
490 typedef struct {
491     float b1_mant[3];
492     float b2_mant[3];
493     float b4_mant[2];
494     int b1ptr;
495     int b2ptr;
496     int b4ptr;
497 } mant_groups;
498
499 /**
500  * Get the transform coefficients for a particular channel
501  * reference: Section 7.3 Quantization and Decoding of Mantissas
502  */
503 static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
504 {
505     GetBitContext *gb = &ctx->gb;
506     int i, gcode, tbap, start, end;
507     uint8_t *exps;
508     uint8_t *bap;
509     float *coeffs;
510
511     exps = ctx->dexps[ch_index];
512     bap = ctx->bap[ch_index];
513     coeffs = ctx->transform_coeffs[ch_index];
514     start = ctx->startmant[ch_index];
515     end = ctx->endmant[ch_index];
516
517     for (i = start; i < end; i++) {
518         tbap = bap[i];
519         switch (tbap) {
520             case 0:
521                 coeffs[i] = ((av_random(&ctx->dith_state) & 0xFFFF) / 65535.0f) - 0.5f;
522                 break;
523
524             case 1:
525                 if(m->b1ptr > 2) {
526                     gcode = get_bits(gb, 5);
527                     m->b1_mant[0] = b1_mantissas[gcode][0];
528                     m->b1_mant[1] = b1_mantissas[gcode][1];
529                     m->b1_mant[2] = b1_mantissas[gcode][2];
530                     m->b1ptr = 0;
531                 }
532                 coeffs[i] = m->b1_mant[m->b1ptr++];
533                 break;
534
535             case 2:
536                 if(m->b2ptr > 2) {
537                     gcode = get_bits(gb, 7);
538                     m->b2_mant[0] = b2_mantissas[gcode][0];
539                     m->b2_mant[1] = b2_mantissas[gcode][1];
540                     m->b2_mant[2] = b2_mantissas[gcode][2];
541                     m->b2ptr = 0;
542                 }
543                 coeffs[i] = m->b2_mant[m->b2ptr++];
544                 break;
545
546             case 3:
547                 coeffs[i] = b3_mantissas[get_bits(gb, 3)];
548                 break;
549
550             case 4:
551                 if(m->b4ptr > 1) {
552                     gcode = get_bits(gb, 7);
553                     m->b4_mant[0] = b4_mantissas[gcode][0];
554                     m->b4_mant[1] = b4_mantissas[gcode][1];
555                     m->b4ptr = 0;
556                 }
557                 coeffs[i] = m->b4_mant[m->b4ptr++];
558                 break;
559
560             case 5:
561                 coeffs[i] = b5_mantissas[get_bits(gb, 4)];
562                 break;
563
564             default:
565                 /* asymmetric dequantization */
566                 coeffs[i] = get_sbits(gb, qntztab[tbap]) * scale_factors[qntztab[tbap]-1];
567                 break;
568         }
569         coeffs[i] *= scale_factors[exps[i]];
570     }
571
572     return 0;
573 }
574
575 /**
576  * Remove random dithering from coefficients with zero-bit mantissas
577  * reference: Section 7.3.4 Dither for Zero Bit Mantissas (bap=0)
578  */
579 static void remove_dithering(AC3DecodeContext *ctx) {
580     int ch, i;
581     int end=0;
582     float *coeffs;
583     uint8_t *bap;
584
585     for(ch=1; ch<=ctx->nfchans; ch++) {
586         if(!ctx->dithflag[ch]) {
587             coeffs = ctx->transform_coeffs[ch];
588             bap = ctx->bap[ch];
589             if(ctx->chincpl[ch])
590                 end = ctx->startmant[CPL_CH];
591             else
592                 end = ctx->endmant[ch];
593             for(i=0; i<end; i++) {
594                 if(bap[i] == 0)
595                     coeffs[i] = 0.0f;
596             }
597             if(ctx->chincpl[ch]) {
598                 bap = ctx->bap[CPL_CH];
599                 for(; i<ctx->endmant[CPL_CH]; i++) {
600                     if(bap[i] == 0)
601                         coeffs[i] = 0.0f;
602                 }
603             }
604         }
605     }
606 }
607
608 /**
609  * Get the transform coefficients.
610  */
611 static int get_transform_coeffs(AC3DecodeContext * ctx)
612 {
613     int ch, end;
614     int got_cplchan = 0;
615     mant_groups m;
616
617     m.b1ptr = m.b2ptr = m.b4ptr = 3;
618
619     for (ch = 1; ch <= ctx->nchans; ch++) {
620         /* transform coefficients for full-bandwidth channel */
621         if (get_transform_coeffs_ch(ctx, ch, &m))
622             return -1;
623         /* tranform coefficients for coupling channel come right after the
624            coefficients for the first coupled channel*/
625         if (ctx->chincpl[ch])  {
626             if (!got_cplchan) {
627                 if (get_transform_coeffs_ch(ctx, CPL_CH, &m)) {
628                     av_log(ctx->avctx, AV_LOG_ERROR, "error in decoupling channels\n");
629                     return -1;
630                 }
631                 uncouple_channels(ctx);
632                 got_cplchan = 1;
633             }
634             end = ctx->endmant[CPL_CH];
635         } else {
636             end = ctx->endmant[ch];
637         }
638         do
639             ctx->transform_coeffs[ch][end] = 0;
640         while(++end < 256);
641     }
642
643     /* if any channel doesn't use dithering, zero appropriate coefficients */
644     if(!ctx->dither_all)
645         remove_dithering(ctx);
646
647     return 0;
648 }
649
650 /**
651  * Stereo rematrixing.
652  * reference: Section 7.5.4 Rematrixing : Decoding Technique
653  */
654 static void do_rematrixing(AC3DecodeContext *ctx)
655 {
656     int bnd, i;
657     int end, bndend;
658     float tmp0, tmp1;
659
660     end = FFMIN(ctx->endmant[1], ctx->endmant[2]);
661
662     for(bnd=0; bnd<ctx->nrematbnd; bnd++) {
663         if(ctx->rematflg[bnd]) {
664             bndend = FFMIN(end, rematrix_band_tab[bnd+1]);
665             for(i=rematrix_band_tab[bnd]; i<bndend; i++) {
666                 tmp0 = ctx->transform_coeffs[1][i];
667                 tmp1 = ctx->transform_coeffs[2][i];
668                 ctx->transform_coeffs[1][i] = tmp0 + tmp1;
669                 ctx->transform_coeffs[2][i] = tmp0 - tmp1;
670             }
671         }
672     }
673 }
674
675 /**
676  * Perform the 256-point IMDCT
677  */
678 static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
679 {
680     int i, k;
681     DECLARE_ALIGNED_16(float, x[128]);
682     FFTComplex z[2][64];
683     float *o_ptr = ctx->tmp_output;
684
685     for(i=0; i<2; i++) {
686         /* de-interleave coefficients */
687         for(k=0; k<128; k++) {
688             x[k] = ctx->transform_coeffs[chindex][2*k+i];
689         }
690
691         /* run standard IMDCT */
692         ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, o_ptr, x, ctx->tmp_imdct);
693
694         /* reverse the post-rotation & reordering from standard IMDCT */
695         for(k=0; k<32; k++) {
696             z[i][32+k].re = -o_ptr[128+2*k];
697             z[i][32+k].im = -o_ptr[2*k];
698             z[i][31-k].re =  o_ptr[2*k+1];
699             z[i][31-k].im =  o_ptr[128+2*k+1];
700         }
701     }
702
703     /* apply AC-3 post-rotation & reordering */
704     for(k=0; k<64; k++) {
705         o_ptr[    2*k  ] = -z[0][   k].im;
706         o_ptr[    2*k+1] =  z[0][63-k].re;
707         o_ptr[128+2*k  ] = -z[0][   k].re;
708         o_ptr[128+2*k+1] =  z[0][63-k].im;
709         o_ptr[256+2*k  ] = -z[1][   k].re;
710         o_ptr[256+2*k+1] =  z[1][63-k].im;
711         o_ptr[384+2*k  ] =  z[1][   k].im;
712         o_ptr[384+2*k+1] = -z[1][63-k].re;
713     }
714 }
715
716 /**
717  * Inverse MDCT Transform.
718  * Convert frequency domain coefficients to time-domain audio samples.
719  * reference: Section 7.9.4 Transformation Equations
720  */
721 static inline void do_imdct(AC3DecodeContext *ctx)
722 {
723     int ch;
724     int nchans;
725
726     /* Don't perform the IMDCT on the LFE channel unless it's used in the output */
727     nchans = ctx->nfchans;
728     if(ctx->output_mode & AC3_OUTPUT_LFEON)
729         nchans++;
730
731     for (ch=1; ch<=nchans; ch++) {
732         if (ctx->blksw[ch]) {
733             do_imdct_256(ctx, ch);
734         } else {
735             ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
736                                           ctx->transform_coeffs[ch],
737                                           ctx->tmp_imdct);
738         }
739         /* For the first half of the block, apply the window, add the delay
740            from the previous block, and send to output */
741         ctx->dsp.vector_fmul_add_add(ctx->output[ch-1], ctx->tmp_output,
742                                      ctx->window, ctx->delay[ch-1], 0, 256, 1);
743         /* For the second half of the block, apply the window and store the
744            samples to delay, to be combined with the next block */
745         ctx->dsp.vector_fmul_reverse(ctx->delay[ch-1], ctx->tmp_output+256,
746                                      ctx->window, 256);
747     }
748 }
749
750 /**
751  * Downmix the output to mono or stereo.
752  */
753 static void ac3_downmix(float samples[AC3_MAX_CHANNELS][256], int nfchans,
754                         int output_mode, float coef[AC3_MAX_CHANNELS][2])
755 {
756     int i, j;
757     float v0, v1, s0, s1;
758
759     for(i=0; i<256; i++) {
760         v0 = v1 = s0 = s1 = 0.0f;
761         for(j=0; j<nfchans; j++) {
762             v0 += samples[j][i] * coef[j][0];
763             v1 += samples[j][i] * coef[j][1];
764             s0 += coef[j][0];
765             s1 += coef[j][1];
766         }
767         v0 /= s0;
768         v1 /= s1;
769         if(output_mode == AC3_ACMOD_MONO) {
770             samples[0][i] = (v0 + v1) * LEVEL_MINUS_3DB;
771         } else if(output_mode == AC3_ACMOD_STEREO) {
772             samples[0][i] = v0;
773             samples[1][i] = v1;
774         }
775     }
776 }
777
778 /**
779  * Parse an audio block from AC-3 bitstream.
780  */
781 static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
782 {
783     int nfchans = ctx->nfchans;
784     int acmod = ctx->acmod;
785     int i, bnd, seg, ch;
786     GetBitContext *gb = &ctx->gb;
787     uint8_t bit_alloc_stages[AC3_MAX_CHANNELS];
788
789     memset(bit_alloc_stages, 0, AC3_MAX_CHANNELS);
790
791     /* block switch flags */
792     for (ch = 1; ch <= nfchans; ch++)
793         ctx->blksw[ch] = get_bits1(gb);
794
795     /* dithering flags */
796     ctx->dither_all = 1;
797     for (ch = 1; ch <= nfchans; ch++) {
798         ctx->dithflag[ch] = get_bits1(gb);
799         if(!ctx->dithflag[ch])
800             ctx->dither_all = 0;
801     }
802
803     /* dynamic range */
804     i = !(ctx->acmod);
805     do {
806         if(get_bits1(gb)) {
807             ctx->dynrng[i] = dynrng_tab[get_bits(gb, 8)];
808         } else if(blk == 0) {
809             ctx->dynrng[i] = 1.0f;
810         }
811     } while(i--);
812
813     /* coupling strategy */
814     if (get_bits1(gb)) {
815         memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
816         ctx->cplinu = get_bits1(gb);
817         if (ctx->cplinu) {
818             /* coupling in use */
819             int cplbegf, cplendf;
820
821             /* determine which channels are coupled */
822             for (ch = 1; ch <= nfchans; ch++)
823                 ctx->chincpl[ch] = get_bits1(gb);
824
825             /* phase flags in use */
826             if (acmod == AC3_ACMOD_STEREO)
827                 ctx->phsflginu = get_bits1(gb);
828
829             /* coupling frequency range and band structure */
830             cplbegf = get_bits(gb, 4);
831             cplendf = get_bits(gb, 4);
832             if (3 + cplendf - cplbegf < 0) {
833                 av_log(ctx->avctx, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", cplendf, cplbegf);
834                 return -1;
835             }
836             ctx->ncplbnd = ctx->ncplsubnd = 3 + cplendf - cplbegf;
837             ctx->startmant[CPL_CH] = cplbegf * 12 + 37;
838             ctx->endmant[CPL_CH] = cplendf * 12 + 73;
839             for (bnd = 0; bnd < ctx->ncplsubnd - 1; bnd++) {
840                 if (get_bits1(gb)) {
841                     ctx->cplbndstrc[bnd] = 1;
842                     ctx->ncplbnd--;
843                 }
844             }
845         } else {
846             /* coupling not in use */
847             for (ch = 1; ch <= nfchans; ch++)
848                 ctx->chincpl[ch] = 0;
849         }
850     }
851
852     /* coupling coordinates */
853     if (ctx->cplinu) {
854         int cplcoe = 0;
855
856         for (ch = 1; ch <= nfchans; ch++) {
857             if (ctx->chincpl[ch]) {
858                 if (get_bits1(gb)) {
859                     int mstrcplco, cplcoexp, cplcomant;
860                     cplcoe = 1;
861                     mstrcplco = 3 * get_bits(gb, 2);
862                     for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
863                         cplcoexp = get_bits(gb, 4);
864                         cplcomant = get_bits(gb, 4);
865                         if (cplcoexp == 15)
866                             ctx->cplco[ch][bnd] = cplcomant / 16.0f;
867                         else
868                             ctx->cplco[ch][bnd] = (cplcomant + 16.0f) / 32.0f;
869                         ctx->cplco[ch][bnd] *= scale_factors[cplcoexp + mstrcplco];
870                     }
871                 }
872             }
873         }
874         /* phase flags */
875         if (acmod == AC3_ACMOD_STEREO && ctx->phsflginu && cplcoe) {
876             for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
877                 if (get_bits1(gb))
878                     ctx->cplco[2][bnd] = -ctx->cplco[2][bnd];
879             }
880         }
881     }
882
883     /* stereo rematrixing strategy and band structure */
884     if (acmod == AC3_ACMOD_STEREO) {
885         ctx->rematstr = get_bits1(gb);
886         if (ctx->rematstr) {
887             ctx->nrematbnd = 4;
888             if(ctx->cplinu && ctx->startmant[CPL_CH] <= 61)
889                 ctx->nrematbnd -= 1 + (ctx->startmant[CPL_CH] == 37);
890             for(bnd=0; bnd<ctx->nrematbnd; bnd++)
891                 ctx->rematflg[bnd] = get_bits1(gb);
892         }
893     }
894
895     /* exponent strategies for each channel */
896     ctx->expstr[CPL_CH] = EXP_REUSE;
897     ctx->expstr[ctx->lfe_ch] = EXP_REUSE;
898     for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) {
899         if(ch == ctx->lfe_ch)
900             ctx->expstr[ch] = get_bits(gb, 1);
901         else
902             ctx->expstr[ch] = get_bits(gb, 2);
903         if(ctx->expstr[ch] != EXP_REUSE)
904             bit_alloc_stages[ch] = 3;
905     }
906
907     /* channel bandwidth */
908     for (ch = 1; ch <= nfchans; ch++) {
909         ctx->startmant[ch] = 0;
910         if (ctx->expstr[ch] != EXP_REUSE) {
911             int prev = ctx->endmant[ch];
912             if (ctx->chincpl[ch])
913                 ctx->endmant[ch] = ctx->startmant[CPL_CH];
914             else {
915                 int chbwcod = get_bits(gb, 6);
916                 if (chbwcod > 60) {
917                     av_log(ctx->avctx, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
918                     return -1;
919                 }
920                 ctx->endmant[ch] = chbwcod * 3 + 73;
921             }
922             if(blk > 0 && ctx->endmant[ch] != prev)
923                 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
924         }
925     }
926     ctx->startmant[ctx->lfe_ch] = 0;
927     ctx->endmant[ctx->lfe_ch] = 7;
928
929     /* decode exponents for each channel */
930     for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) {
931         if (ctx->expstr[ch] != EXP_REUSE) {
932             int grpsize, ngrps;
933             grpsize = 3 << (ctx->expstr[ch] - 1);
934             if(ch == CPL_CH)
935                 ngrps = (ctx->endmant[ch] - ctx->startmant[ch]) / grpsize;
936             else if(ch == ctx->lfe_ch)
937                 ngrps = 2;
938             else
939                 ngrps = (ctx->endmant[ch] + grpsize - 4) / grpsize;
940             ctx->dexps[ch][0] = get_bits(gb, 4) << !ch;
941             decode_exponents(gb, ctx->expstr[ch], ngrps, ctx->dexps[ch][0],
942                              &ctx->dexps[ch][ctx->startmant[ch]+!!ch]);
943             if(ch != CPL_CH && ch != ctx->lfe_ch)
944                 skip_bits(gb, 2); /* skip gainrng */
945         }
946     }
947
948     /* bit allocation information */
949     if (get_bits1(gb)) {
950         ctx->bit_alloc_params.sdecay = ff_sdecaytab[get_bits(gb, 2)] >> ctx->bit_alloc_params.halfratecod;
951         ctx->bit_alloc_params.fdecay = ff_fdecaytab[get_bits(gb, 2)] >> ctx->bit_alloc_params.halfratecod;
952         ctx->bit_alloc_params.sgain  = ff_sgaintab[get_bits(gb, 2)];
953         ctx->bit_alloc_params.dbknee = ff_dbkneetab[get_bits(gb, 2)];
954         ctx->bit_alloc_params.floor  = ff_floortab[get_bits(gb, 3)];
955         for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) {
956             bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
957         }
958     }
959
960     /* signal-to-noise ratio offsets and fast gains (signal-to-mask ratios) */
961     if (get_bits1(gb)) {
962         int csnr;
963         csnr = (get_bits(gb, 6) - 15) << 4;
964         for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) { /* snr offset and fast gain */
965             ctx->snroffst[ch] = (csnr + get_bits(gb, 4)) << 2;
966             ctx->fgain[ch] = ff_fgaintab[get_bits(gb, 3)];
967         }
968         memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
969     }
970
971     /* coupling leak information */
972     if (ctx->cplinu && get_bits1(gb)) {
973         ctx->bit_alloc_params.cplfleak = get_bits(gb, 3);
974         ctx->bit_alloc_params.cplsleak = get_bits(gb, 3);
975         bit_alloc_stages[CPL_CH] = FFMAX(bit_alloc_stages[CPL_CH], 2);
976     }
977
978     /* delta bit allocation information */
979     if (get_bits1(gb)) {
980         /* delta bit allocation exists (strategy) */
981         for (ch = !ctx->cplinu; ch <= nfchans; ch++) {
982             ctx->deltbae[ch] = get_bits(gb, 2);
983             if (ctx->deltbae[ch] == DBA_RESERVED) {
984                 av_log(ctx->avctx, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
985                 return -1;
986             }
987             bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
988         }
989         /* channel delta offset, len and bit allocation */
990         for (ch = !ctx->cplinu; ch <= nfchans; ch++) {
991             if (ctx->deltbae[ch] == DBA_NEW) {
992                 ctx->deltnseg[ch] = get_bits(gb, 3);
993                 for (seg = 0; seg <= ctx->deltnseg[ch]; seg++) {
994                     ctx->deltoffst[ch][seg] = get_bits(gb, 5);
995                     ctx->deltlen[ch][seg] = get_bits(gb, 4);
996                     ctx->deltba[ch][seg] = get_bits(gb, 3);
997                 }
998             }
999         }
1000     } else if(blk == 0) {
1001         for(ch=0; ch<=ctx->nchans; ch++) {
1002             ctx->deltbae[ch] = DBA_NONE;
1003         }
1004     }
1005
1006     /* Bit allocation */
1007     for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) {
1008         if(bit_alloc_stages[ch] > 2) {
1009             /* Exponent mapping into PSD and PSD integration */
1010             ff_ac3_bit_alloc_calc_psd(ctx->dexps[ch],
1011                                       ctx->startmant[ch], ctx->endmant[ch],
1012                                       ctx->psd[ch], ctx->bndpsd[ch]);
1013         }
1014         if(bit_alloc_stages[ch] > 1) {
1015             /* Compute excitation function, Compute masking curve, and
1016                Apply delta bit allocation */
1017             ff_ac3_bit_alloc_calc_mask(&ctx->bit_alloc_params, ctx->bndpsd[ch],
1018                                        ctx->startmant[ch], ctx->endmant[ch],
1019                                        ctx->fgain[ch], (ch == ctx->lfe_ch),
1020                                        ctx->deltbae[ch], ctx->deltnseg[ch],
1021                                        ctx->deltoffst[ch], ctx->deltlen[ch],
1022                                        ctx->deltba[ch], ctx->mask[ch]);
1023         }
1024         if(bit_alloc_stages[ch] > 0) {
1025             /* Compute bit allocation */
1026             ff_ac3_bit_alloc_calc_bap(ctx->mask[ch], ctx->psd[ch],
1027                                       ctx->startmant[ch], ctx->endmant[ch],
1028                                       ctx->snroffst[ch],
1029                                       ctx->bit_alloc_params.floor,
1030                                       ctx->bap[ch]);
1031         }
1032     }
1033
1034     /* unused dummy data */
1035     if (get_bits1(gb)) {
1036         int skipl = get_bits(gb, 9);
1037         while(skipl--)
1038             skip_bits(gb, 8);
1039     }
1040
1041     /* unpack the transform coefficients
1042        this also uncouples channels if coupling is in use. */
1043     if (get_transform_coeffs(ctx)) {
1044         av_log(ctx->avctx, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1045         return -1;
1046     }
1047
1048     /* recover coefficients if rematrixing is in use */
1049     if(ctx->acmod == AC3_ACMOD_STEREO)
1050         do_rematrixing(ctx);
1051
1052     /* apply scaling to coefficients (headroom, dialnorm, dynrng) */
1053     for(ch=1; ch<=ctx->nchans; ch++) {
1054         float gain = 2.0f * ctx->mul_bias;
1055         if(ctx->acmod == AC3_ACMOD_DUALMONO) {
1056             gain *= ctx->dialnorm[ch-1] * ctx->dynrng[ch-1];
1057         } else {
1058             gain *= ctx->dialnorm[0] * ctx->dynrng[0];
1059         }
1060         for(i=0; i<ctx->endmant[ch]; i++) {
1061             ctx->transform_coeffs[ch][i] *= gain;
1062         }
1063     }
1064
1065     do_imdct(ctx);
1066
1067     /* downmix output if needed */
1068     if(ctx->nchans != ctx->out_channels && !((ctx->output_mode & AC3_OUTPUT_LFEON) &&
1069             ctx->nfchans == ctx->out_channels)) {
1070         ac3_downmix(ctx->output, ctx->nfchans, ctx->output_mode,
1071                     ctx->downmix_coeffs);
1072     }
1073
1074     /* convert float to 16-bit integer */
1075     for(ch=0; ch<ctx->out_channels; ch++) {
1076         for(i=0; i<256; i++) {
1077             ctx->output[ch][i] += ctx->add_bias;
1078         }
1079         ctx->dsp.float_to_int16(ctx->int_output[ch], ctx->output[ch], 256);
1080     }
1081
1082     return 0;
1083 }
1084
1085 /**
1086  * Decode a single AC-3 frame.
1087  */
1088 static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1089 {
1090     AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1091     int16_t *out_samples = (int16_t *)data;
1092     int i, blk, ch;
1093
1094     /* initialize the GetBitContext with the start of valid AC-3 Frame */
1095     init_get_bits(&ctx->gb, buf, buf_size * 8);
1096
1097     /* parse the syncinfo */
1098     if (ac3_parse_header(ctx)) {
1099         av_log(avctx, AV_LOG_ERROR, "\n");
1100         *data_size = 0;
1101         return buf_size;
1102     }
1103
1104     avctx->sample_rate = ctx->sampling_rate;
1105     avctx->bit_rate = ctx->bit_rate;
1106
1107     /* channel config */
1108     ctx->out_channels = ctx->nchans;
1109     if (avctx->channels == 0) {
1110         avctx->channels = ctx->out_channels;
1111     } else if(ctx->out_channels < avctx->channels) {
1112         av_log(avctx, AV_LOG_ERROR, "Cannot upmix AC3 from %d to %d channels.\n",
1113                ctx->out_channels, avctx->channels);
1114         return -1;
1115     }
1116     if(avctx->channels == 2) {
1117         ctx->output_mode = AC3_ACMOD_STEREO;
1118     } else if(avctx->channels == 1) {
1119         ctx->output_mode = AC3_ACMOD_MONO;
1120     } else if(avctx->channels != ctx->out_channels) {
1121         av_log(avctx, AV_LOG_ERROR, "Cannot downmix AC3 from %d to %d channels.\n",
1122                ctx->out_channels, avctx->channels);
1123         return -1;
1124     }
1125     ctx->out_channels = avctx->channels;
1126
1127     /* parse the audio blocks */
1128     for (blk = 0; blk < NB_BLOCKS; blk++) {
1129         if (ac3_parse_audio_block(ctx, blk)) {
1130             av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1131             *data_size = 0;
1132             return ctx->frame_size;
1133         }
1134         for (i = 0; i < 256; i++)
1135             for (ch = 0; ch < ctx->out_channels; ch++)
1136                 *(out_samples++) = ctx->int_output[ch][i];
1137     }
1138     *data_size = NB_BLOCKS * 256 * avctx->channels * sizeof (int16_t);
1139     return ctx->frame_size;
1140 }
1141
1142 /**
1143  * Uninitialize the AC-3 decoder.
1144  */
1145 static int ac3_decode_end(AVCodecContext *avctx)
1146 {
1147     AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1148     ff_mdct_end(&ctx->imdct_512);
1149     ff_mdct_end(&ctx->imdct_256);
1150
1151     return 0;
1152 }
1153
1154 AVCodec ac3_decoder = {
1155     .name = "ac3",
1156     .type = CODEC_TYPE_AUDIO,
1157     .id = CODEC_ID_AC3,
1158     .priv_data_size = sizeof (AC3DecodeContext),
1159     .init = ac3_decode_init,
1160     .close = ac3_decode_end,
1161     .decode = ac3_decode_frame,
1162 };