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