AC-3 decoder, soc revision 70, Aug 31 18:04:23 2006 UTC by banan
[libav.git] / libavcodec / ac3dec.c
CommitLineData
2aa2c5c4 1/* AC3 Audio Decoder.
032732d4
JR
2 * This code is developed as part of Google Summer of Code 2006 Program.
3 *
4 * Acknowledgements:
5 *
6 * I would like to acknowledge my mentor Benjamin Larsson for his timely
7 * help and excelleng guidance throughout the project.
8 * Thanks a lot Benjamin.
9 *
10 * For exponent decoding the code is reused from liba52 by Michel Lespinasse
11 * and Aaron Holtzman.
12 * http://liba52.sourceforge.net
13 *
14 * Thanks Makoto Matsumoto and Takuji Nishimura for the Mersenne Twister.
15 *
16 * Kaiser-Bessel derived window by Justin Ruggles.
2aa2c5c4
JR
17 *
18 * Copyright (c) 2006 Kartikey Mahendra BHATT (bhattkm at gmail dot com).
032732d4 19 * Something is wrong up on cloud # 9!
2aa2c5c4
JR
20 *
21 * This library is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU Lesser General Public
23 * License as published by the Free Software Foundation; either
24 * version 2 of the License, or (at your option) any later version.
25 *
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
30 *
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library; if not, write to the Free Software
33 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 */
35
36#include <stdio.h>
37#include <stddef.h>
38#include <math.h>
2aa2c5c4
JR
39#include <string.h>
40
41#define ALT_BITSTREAM_READER
1b293437 42
e6bca37c 43#include "avcodec.h"
1b293437 44#include "ac3tab.h"
2aa2c5c4 45#include "ac3_decoder.h"
2aa2c5c4
JR
46#include "bitstream.h"
47#include "dsputil.h"
98a27a8a
JR
48
49#define N 512 /* constant for IMDCT Block size */
1b293437 50
00585845 51#define MAX_CHANNELS 6
486637af
JR
52#define BLOCK_SIZE 256
53#define AUDIO_BLOCKS 6
00585845 54
1b293437
JR
55/* Exponent strategies. */
56#define AC3_EXPSTR_D15 0x01
57#define AC3_EXPSTR_D25 0x02
58#define AC3_EXPSTR_D45 0x03
59#define AC3_EXPSTR_REUSE 0x00
60
98a27a8a 61/* Bit allocation strategies. */
1b293437
JR
62#define AC3_DBASTR_NEW 0x01
63#define AC3_DBASTR_NONE 0x02
64#define AC3_DBASTR_RESERVED 0x03
65#define AC3_DBASTR_REUSE 0x00
66
98a27a8a 67/* Output and input configurations. */
486637af
JR
68#define AC3_OUTPUT_UNMODIFIED 0x01
69#define AC3_OUTPUT_MONO 0x02
70#define AC3_OUTPUT_STEREO 0x04
71#define AC3_OUTPUT_DOLBY 0x08
72#define AC3_OUTPUT_LFEON 0x10
1b293437
JR
73
74#define AC3_INPUT_DUALMONO 0x00
75#define AC3_INPUT_MONO 0x01
76#define AC3_INPUT_STEREO 0x02
77#define AC3_INPUT_3F 0x03
78#define AC3_INPUT_2F_1R 0x04
79#define AC3_INPUT_3F_1R 0x05
80#define AC3_INPUT_2F_2R 0x06
81#define AC3_INPUT_3F_2R 0x07
82
98a27a8a
JR
83/* Mersenne Twister */
84#define NMT 624
85#define MMT 397
1b293437
JR
86#define MATRIX_A 0x9908b0df
87#define UPPER_MASK 0x80000000
88#define LOWER_MASK 0x7fffffff
89
98a27a8a 90
1b293437 91typedef struct {
98a27a8a 92 uint32_t mt[NMT];
1b293437
JR
93 int mti;
94} dither_state;
98a27a8a
JR
95/* Mersenne Twister */
96
97typedef struct {
98 uint32_t flags;
99 uint16_t crc1;
100 uint8_t fscod;
101
102 uint8_t acmod;
103 uint8_t cmixlev;
104 uint8_t surmixlev;
105 uint8_t dsurmod;
106
107 uint8_t blksw;
108 uint8_t dithflag;
109 uint8_t cplinu;
110 uint8_t chincpl;
111 uint8_t phsflginu;
112 uint8_t cplbegf;
113 uint8_t cplendf;
114 uint8_t cplcoe;
115 uint32_t cplbndstrc;
116 uint8_t rematstr;
117 uint8_t rematflg;
118 uint8_t cplexpstr;
119 uint8_t lfeexpstr;
120 uint8_t chexpstr[5];
121 uint8_t sdcycod;
122 uint8_t fdcycod;
123 uint8_t sgaincod;
124 uint8_t dbpbcod;
125 uint8_t floorcod;
126 uint8_t csnroffst;
127 uint8_t cplfsnroffst;
128 uint8_t cplfgaincod;
129 uint8_t fsnroffst[5];
130 uint8_t fgaincod[5];
131 uint8_t lfefsnroffst;
132 uint8_t lfefgaincod;
133 uint8_t cplfleak;
134 uint8_t cplsleak;
135 uint8_t cpldeltbae;
136 uint8_t deltbae[5];
137 uint8_t cpldeltnseg;
138 uint8_t cpldeltoffst[8];
139 uint8_t cpldeltlen[8];
140 uint8_t cpldeltba[8];
141 uint8_t deltnseg[5];
142 uint8_t deltoffst[5][8];
143 uint8_t deltlen[5][8];
144 uint8_t deltba[5][8];
145
146 /* Derived Attributes. */
147 int sampling_rate;
148 int bit_rate;
149 int frame_size;
150
151 int nfchans;
152 int lfeon;
153
6dc5d71f
JR
154 float dynrng;
155 float dynrng2;
98a27a8a
JR
156 float chcoeffs[6];
157 float cplco[5][18];
158 int ncplbnd;
159 int ncplsubnd;
160 int cplstrtmant;
161 int cplendmant;
162 int endmant[5];
163 uint8_t dcplexps[256];
164 uint8_t dexps[5][256];
165 uint8_t dlfeexps[256];
166 uint8_t cplbap[256];
167 uint8_t bap[5][256];
168 uint8_t lfebap[256];
169 int blkoutput;
170
171 DECLARE_ALIGNED_16(float, transform_coeffs[MAX_CHANNELS][BLOCK_SIZE]);
172
173 /* For IMDCT. */
45b0ed13
JR
174 MDCTContext imdct_512; //N/8 point IFFT context
175 MDCTContext imdct_256; //N/4 point IFFT context
1d0a6f52 176 DSPContext dsp; //for optimization
98a27a8a
JR
177 DECLARE_ALIGNED_16(float, output[MAX_CHANNELS][BLOCK_SIZE]);
178 DECLARE_ALIGNED_16(float, delay[MAX_CHANNELS][BLOCK_SIZE]);
179 DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]);
180 DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]);
6dc5d71f 181 DECLARE_ALIGNED_16(float, window[BLOCK_SIZE]);
98a27a8a
JR
182
183 /* Miscellaneous. */
184 GetBitContext gb;
185 dither_state dith_state;
186} AC3DecodeContext;
187
1b293437 188
98a27a8a 189/* BEGIN Mersenne Twister Code. */
1b293437
JR
190static void dither_seed(dither_state *state, uint32_t seed)
191{
860fe8c9 192 if (seed == 0) seed = 0x1f2e3d4c;
2aa2c5c4 193
1b293437 194 state->mt[0] = seed;
98a27a8a 195 for (state->mti = 1; state->mti < NMT; state->mti++)
1b293437
JR
196 state->mt[state->mti] = ((69069 * state->mt[state->mti - 1]) + 1);
197}
2aa2c5c4 198
1b293437 199static uint32_t dither_uint32(dither_state *state)
2aa2c5c4 200{
1b293437
JR
201 uint32_t y;
202 static const uint32_t mag01[2] = { 0x00, MATRIX_A };
203 int kk;
204
98a27a8a
JR
205 if (state->mti >= NMT) {
206 for (kk = 0; kk < NMT - MMT; kk++) {
1b293437 207 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
98a27a8a 208 state->mt[kk] = state->mt[kk + MMT] ^ (y >> 1) ^ mag01[y & 0x01];
1b293437 209 }
98a27a8a 210 for (;kk < NMT - 1; kk++) {
1b293437 211 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
98a27a8a 212 state->mt[kk] = state->mt[kk + (MMT - NMT)] ^ (y >> 1) ^ mag01[y & 0x01];
1b293437 213 }
98a27a8a
JR
214 y = (state->mt[NMT - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK);
215 state->mt[NMT - 1] = state->mt[MMT - 1] ^ (y >> 1) ^ mag01[y & 0x01];
1b293437
JR
216
217 state->mti = 0;
218 }
2aa2c5c4 219
1b293437
JR
220 y = state->mt[state->mti++];
221 y ^= (y >> 11);
222 y ^= ((y << 7) & 0x9d2c5680);
223 y ^= ((y << 15) & 0xefc60000);
224 y ^= (y >> 18);
225
226 return y;
227}
228
229static inline int16_t dither_int16(dither_state *state)
2aa2c5c4 230{
1b293437
JR
231 return ((dither_uint32(state) << 16) >> 16);
232}
2aa2c5c4 233
1b293437
JR
234/* END Mersenne Twister */
235
2fbbd087 236/**
2fbbd087 237 * Generate a Kaiser-Bessel Derived Window.
2fbbd087 238 */
860fe8c9 239static void ac3_window_init(float *window)
2fbbd087 240{
6dc5d71f
JR
241 int i, j;
242 double sum = 0.0, bessel, tmp;
243 double local_window[256];
244 double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
245
246 for (i = 0; i < 256; i++) {
247 tmp = i * (256 - i) * alpha2;
248 bessel = 1.0;
249 for (j = 100; j > 0; j--) /* defaul to 100 iterations */
250 bessel = bessel * tmp / (j * j) + 1;
251 sum += bessel;
252 local_window[i] = sum;
253 }
254
255 sum++;
256 for (i = 0; i < 256; i++)
257 window[i] = sqrt(local_window[i] / sum);
2fbbd087
JR
258}
259
98a27a8a
JR
260static void generate_quantizers_table(int16_t quantizers[], int level, int length)
261{
262 int i;
263
264 for (i = 0; i < length; i++)
265 quantizers[i] = ((2 * i - level + 1) << 15) / level;
266}
267
268static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
269{
270 int i, j;
271 int16_t v;
272
273 for (i = 0; i < length1; i++) {
274 v = ((2 * i - level + 1) << 15) / level;
275 for (j = 0; j < length2; j++)
276 quantizers[i * length2 + j] = v;
277 }
278
279 for (i = length1 * length2; i < size; i++)
280 quantizers[i] = 0;
281}
282
283static void generate_quantizers_table_2(int16_t quantizers[], int level, int length1, int length2, int size)
284{
285 int i, j;
286 int16_t v;
287
288 for (i = 0; i < length1; i++) {
289 v = ((2 * (i % level) - level + 1) << 15) / level;
290 for (j = 0; j < length2; j++)
291 quantizers[i * length2 + j] = v;
292 }
293
294 for (i = length1 * length2; i < size; i++)
295 quantizers[i] = 0;
296
297}
298
299static void generate_quantizers_table_3(int16_t quantizers[], int level, int length1, int length2, int size)
300{
301 int i, j;
302
303 for (i = 0; i < length1; i++)
304 for (j = 0; j < length2; j++)
305 quantizers[i * length2 + j] = ((2 * (j % level) - level + 1) << 15) / level;
1b293437 306
98a27a8a
JR
307 for (i = length1 * length2; i < size; i++)
308 quantizers[i] = 0;
309}
310
311static void ac3_tables_init(void)
00585845
JR
312{
313 int i, j, k, l, v;
314 /* compute bndtab and masktab from bandsz */
315 k = 0;
316 l = 0;
317 for(i=0;i<50;i++) {
318 bndtab[i] = l;
319 v = bndsz[i];
320 for(j=0;j<v;j++) masktab[k++]=i;
321 l += v;
322 }
323 masktab[253] = masktab[254] = masktab[255] = 0;
324 bndtab[50] = 0;
98a27a8a
JR
325
326 /* Exponent Decoding Tables */
327 for (i = 0; i < 5; i++) {
328 v = i - 2;
329 for (j = 0; j < 25; j++)
330 exp_1[i * 25 + j] = v;
331 }
332
333 for (i = 0; i < 25; i++) {
334 v = (i % 5) - 2;
335 for (j = 0; j < 5; j++)
336 exp_2[i * 5 + j] = v;
337 }
338
339 for (i = 0; i < 25; i++) {
340 v = -2;
341 for (j = 0; j < 5; j++)
342 exp_3[i * 5 + j] = v++;
343 }
344
345 for (i = 125; i < 128; i++)
346 exp_1[i] = exp_2[i] = exp_3[i] = 25;
347 /* End Exponent Decoding Tables */
348
349 /* Quantizer ungrouping tables. */
350 // for level-3 quantizers
351 generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
352 generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
353 generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
354
355 //for level-5 quantizers
356 generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
357 generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
358 generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
359
360 //for level-7 quantizers
361 generate_quantizers_table(l7_quantizers, 7, 7);
362
363 //for level-4 quantizers
364 generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
365 generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
366
367 //for level-15 quantizers
368 generate_quantizers_table(l15_quantizers, 15, 15);
00585845
JR
369}
370
1b293437
JR
371
372static int ac3_decode_init(AVCodecContext *avctx)
373{
374 AC3DecodeContext *ctx = avctx->priv_data;
1b293437 375
98a27a8a 376 ac3_tables_init();
45b0ed13
JR
377 ff_mdct_init(&ctx->imdct_256, 8, 1);
378 ff_mdct_init(&ctx->imdct_512, 9, 1);
6dc5d71f
JR
379 /* Kaiser-Bessel derived window. */
380 ac3_window_init(ctx->window);
1d0a6f52 381 dsputil_init(&ctx->dsp, avctx);
98a27a8a 382 dither_seed(&ctx->dith_state, 0);
2aa2c5c4 383
1b293437 384 return 0;
2aa2c5c4
JR
385}
386
1b293437 387static int ac3_synchronize(uint8_t *buf, int buf_size)
2aa2c5c4 388{
1b293437 389 int i;
2aa2c5c4 390
1b293437
JR
391 for (i = 0; i < buf_size - 1; i++)
392 if (buf[i] == 0x0b && buf[i + 1] == 0x77)
393 return i;
2aa2c5c4 394
1b293437 395 return -1;
2aa2c5c4
JR
396}
397
398//Returns -1 when 'fscod' is not valid;
1b293437 399static int ac3_parse_sync_info(AC3DecodeContext *ctx)
2aa2c5c4 400{
1b293437 401 GetBitContext *gb = &ctx->gb;
98a27a8a 402 int frmsizecod, bsid;
2aa2c5c4 403
98a27a8a
JR
404 skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
405 ctx->crc1 = get_bits(gb, 16);
406 ctx->fscod = get_bits(gb, 2);
407 if (ctx->fscod == 0x03)
00585845 408 return 0;
98a27a8a
JR
409 frmsizecod = get_bits(gb, 6);
410 if (frmsizecod >= 38)
00585845 411 return 0;
98a27a8a
JR
412 ctx->sampling_rate = ac3_freqs[ctx->fscod];
413 ctx->bit_rate = ac3_bitratetab[frmsizecod >> 1];
2aa2c5c4 414
00585845 415 /* we include it here in order to determine validity of ac3 frame */
98a27a8a
JR
416 bsid = get_bits(gb, 5);
417 if (bsid > 0x08)
00585845 418 return 0;
98a27a8a 419 skip_bits(gb, 3); //skip the bsmod, bsi->bsmod = get_bits(gb, 3);
00585845 420
98a27a8a 421 switch (ctx->fscod) {
00585845 422 case 0x00:
98a27a8a
JR
423 ctx->frame_size = 4 * ctx->bit_rate;
424 return ctx->frame_size;
00585845 425 case 0x01:
98a27a8a
JR
426 ctx->frame_size = 2 * (320 * ctx->bit_rate / 147 + (frmsizecod & 1));
427 return ctx->frame_size;
00585845 428 case 0x02:
98a27a8a
JR
429 ctx->frame_size = 6 * ctx->bit_rate;
430 return ctx->frame_size;
00585845
JR
431 }
432
433 /* never reached */
1b293437 434 return 0;
2aa2c5c4
JR
435}
436
98a27a8a 437static void ac3_parse_bsi(AC3DecodeContext *ctx)
2aa2c5c4 438{
1b293437 439 GetBitContext *gb = &ctx->gb;
00585845 440 int i;
2aa2c5c4 441
98a27a8a
JR
442 ctx->cmixlev = 0;
443 ctx->surmixlev = 0;
444 ctx->dsurmod = 0;
445 ctx->nfchans = 0;
446 ctx->cpldeltbae = AC3_DBASTR_NONE;
447 ctx->cpldeltnseg = 0;
00585845 448 for (i = 0; i < 5; i++) {
98a27a8a
JR
449 ctx->deltbae[i] = AC3_DBASTR_NONE;
450 ctx->deltnseg[i] = 0;
00585845 451 }
6dc5d71f
JR
452 ctx->dynrng = 1.0;
453 ctx->dynrng2 = 1.0;
2aa2c5c4 454
98a27a8a
JR
455 ctx->acmod = get_bits(gb, 3);
456 ctx->nfchans = nfchans_tbl[ctx->acmod];
457
458 if (ctx->acmod & 0x01 && ctx->acmod != 0x01)
459 ctx->cmixlev = get_bits(gb, 2);
460 if (ctx->acmod & 0x04)
461 ctx->surmixlev = get_bits(gb, 2);
462 if (ctx->acmod == 0x02)
463 ctx->dsurmod = get_bits(gb, 2);
464
465 ctx->lfeon = get_bits1(gb);
466
467 i = !(ctx->acmod);
468 do {
469 skip_bits(gb, 5); //skip dialog normalization
470 if (get_bits1(gb))
471 skip_bits(gb, 8); //skip compression
472 if (get_bits1(gb))
473 skip_bits(gb, 8); //skip language code
474 if (get_bits1(gb))
475 skip_bits(gb, 7); //skip audio production information
476 } while (i--);
477
478 skip_bits(gb, 2); //skip copyright bit and original bitstream bit
479
00585845 480 if (get_bits1(gb))
98a27a8a 481 skip_bits(gb, 14); //skip timecode1
00585845 482 if (get_bits1(gb))
98a27a8a
JR
483 skip_bits(gb, 14); //skip timecode2
484
00585845 485 if (get_bits1(gb)) {
98a27a8a 486 i = get_bits(gb, 6); //additional bsi length
486637af 487 do {
00585845 488 skip_bits(gb, 8);
98a27a8a 489 } while(i--);
1b293437 490 }
2aa2c5c4
JR
491}
492
98a27a8a 493/* Decodes the grouped exponents and stores them
2aa2c5c4 494 * in decoded exponents (dexps).
1b293437
JR
495 * The code is derived from liba52.
496 * Uses liba52 tables.
2aa2c5c4 497 */
00585845 498static int decode_exponents(GetBitContext *gb, int expstr, int ngrps, uint8_t absexp, uint8_t *dexps)
2aa2c5c4 499{
1b293437 500 int exps;
1b293437
JR
501
502 while (ngrps--) {
00585845 503 exps = get_bits(gb, 7);
1b293437
JR
504
505 absexp += exp_1[exps];
00585845
JR
506 if (absexp > 24) {
507 av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
508 return -ngrps;
509 }
1b293437
JR
510 switch (expstr) {
511 case AC3_EXPSTR_D45:
512 *(dexps++) = absexp;
513 *(dexps++) = absexp;
514 case AC3_EXPSTR_D25:
515 *(dexps++) = absexp;
516 case AC3_EXPSTR_D15:
517 *(dexps++) = absexp;
518 }
00585845 519
1b293437 520 absexp += exp_2[exps];
00585845
JR
521 if (absexp > 24) {
522 av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
523 return -ngrps;
524 }
1b293437
JR
525 switch (expstr) {
526 case AC3_EXPSTR_D45:
527 *(dexps++) = absexp;
528 *(dexps++) = absexp;
529 case AC3_EXPSTR_D25:
530 *(dexps++) = absexp;
531 case AC3_EXPSTR_D15:
532 *(dexps++) = absexp;
533 }
2aa2c5c4 534
1b293437 535 absexp += exp_3[exps];
00585845
JR
536 if (absexp > 24) {
537 av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
538 return -ngrps;
539 }
1b293437
JR
540 switch (expstr) {
541 case AC3_EXPSTR_D45:
542 *(dexps++) = absexp;
543 *(dexps++) = absexp;
544 case AC3_EXPSTR_D25:
545 *(dexps++) = absexp;
546 case AC3_EXPSTR_D15:
547 *(dexps++) = absexp;
548 }
2aa2c5c4 549 }
2aa2c5c4 550
1b293437 551 return 0;
2aa2c5c4
JR
552}
553
00585845 554static inline int logadd(int a, int b)
2aa2c5c4 555{
00585845
JR
556 int c = a - b;
557 int address;
1b293437 558
2fbbd087 559 address = FFMIN((ABS(c) >> 1), 255);
2aa2c5c4 560
00585845
JR
561 if (c >= 0)
562 return (a + latab[address]);
563 else
564 return (b + latab[address]);
2aa2c5c4
JR
565}
566
00585845 567static inline int calc_lowcomp(int a, int b0, int b1, int bin)
2aa2c5c4 568{
1b293437
JR
569 if (bin < 7) {
570 if ((b0 + 256) == b1)
571 a = 384;
572 else if (b0 > b1)
2fbbd087 573 a = FFMAX(0, (a - 64));
1b293437
JR
574 }
575 else if (bin < 20) {
576 if ((b0 + 256) == b1)
577 a = 320;
578 else if (b0 > b1)
2fbbd087 579 a = FFMAX(0, (a - 64));
1b293437 580 }
00585845 581 else
2fbbd087 582 a = FFMAX(0, (a - 128));
1b293437
JR
583
584 return a;
2aa2c5c4
JR
585}
586
587/* do the bit allocation for chnl.
588 * chnl = 0 to 4 - fbw channel
589 * chnl = 5 coupling channel
590 * chnl = 6 lfe channel
591 */
2fbbd087 592static void do_bit_allocation(AC3DecodeContext *ctx, int chnl)
2aa2c5c4 593{
2fbbd087 594 int16_t psd[256], bndpsd[50], excite[50], mask[50], delta;
00585845 595 int sdecay, fdecay, sgain, dbknee, floor;
2fbbd087 596 int lowcomp = 0, fgain = 0, snroffset = 0, fastleak = 0, slowleak = 0, do_delta = 0;
00585845
JR
597 int start = 0, end = 0, bin = 0, i = 0, j = 0, k = 0, lastbin = 0, bndstrt = 0;
598 int bndend = 0, begin = 0, deltnseg = 0, band = 0, seg = 0, address = 0;
98a27a8a 599 int fscod = ctx->fscod;
2fbbd087
JR
600 uint8_t *deltoffst = 0, *deltlen = 0, *deltba = 0;
601 uint8_t *exps = 0, *bap = 0;
1b293437
JR
602
603 /* initialization */
98a27a8a
JR
604 sdecay = sdecaytab[ctx->sdcycod];
605 fdecay = fdecaytab[ctx->fdcycod];
606 sgain = sgaintab[ctx->sgaincod];
607 dbknee = dbkneetab[ctx->dbpbcod];
608 floor = floortab[ctx->floorcod];
1b293437
JR
609
610 if (chnl == 5) {
98a27a8a
JR
611 start = ctx->cplstrtmant;
612 end = ctx->cplendmant;
613 fgain = fgaintab[ctx->cplfgaincod];
614 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->cplfsnroffst) << 2;
615 fastleak = (ctx->cplfleak << 8) + 768;
616 slowleak = (ctx->cplsleak << 8) + 768;
617 exps = ctx->dcplexps;
2fbbd087
JR
618 bap = ctx->cplbap;
619 if (ctx->cpldeltbae == AC3_DBASTR_NEW || ctx->deltbae == AC3_DBASTR_REUSE) {
1b293437 620 do_delta = 1;
98a27a8a
JR
621 deltnseg = ctx->cpldeltnseg;
622 deltoffst = ctx->cpldeltoffst;
623 deltlen = ctx->cpldeltlen;
624 deltba = ctx->cpldeltba;
2aa2c5c4
JR
625 }
626 }
1b293437
JR
627 else if (chnl == 6) {
628 start = 0;
629 end = 7;
630 lowcomp = 0;
00585845
JR
631 fastleak = 0;
632 slowleak = 0;
98a27a8a
JR
633 fgain = fgaintab[ctx->lfefgaincod];
634 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->lfefsnroffst) << 2;
635 exps = ctx->dlfeexps;
2fbbd087 636 bap = ctx->lfebap;
1b293437
JR
637 }
638 else {
639 start = 0;
98a27a8a 640 end = ctx->endmant[chnl];
1b293437 641 lowcomp = 0;
00585845
JR
642 fastleak = 0;
643 slowleak = 0;
98a27a8a
JR
644 fgain = fgaintab[ctx->fgaincod[chnl]];
645 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->fsnroffst[chnl]) << 2;
646 exps = ctx->dexps[chnl];
2fbbd087
JR
647 bap = ctx->bap[chnl];
648 if (ctx->deltbae[chnl] == AC3_DBASTR_NEW || ctx->deltbae[chnl] == AC3_DBASTR_REUSE) {
1b293437 649 do_delta = 1;
98a27a8a
JR
650 deltnseg = ctx->deltnseg[chnl];
651 deltoffst = ctx->deltoffst[chnl];
652 deltlen = ctx->deltlen[chnl];
653 deltba = ctx->deltba[chnl];
1b293437
JR
654 }
655 }
656
657 for (bin = start; bin < end; bin++) /* exponent mapping into psd */
2fbbd087 658 psd[bin] = (3072 - (exps[bin] << 7));
1b293437
JR
659
660 /* psd integration */
661 j = start;
662 k = masktab[start];
663 do {
2fbbd087 664 lastbin = FFMIN((bndtab[k] + bndsz[k]), end);
1b293437
JR
665 bndpsd[k] = psd[j];
666 j++;
667 for (i = j; i < lastbin; i++) {
668 bndpsd[k] = logadd(bndpsd[k], psd[j]);
669 j++;
670 }
671 k++;
672 } while (end > lastbin);
673
674 /* compute the excite function */
675 bndstrt = masktab[start];
676 bndend = masktab[end - 1] + 1;
677 if (bndstrt == 0) {
678 lowcomp = calc_lowcomp(lowcomp, bndpsd[0], bndpsd[1], 0);
679 excite[0] = bndpsd[0] - fgain - lowcomp;
680 lowcomp = calc_lowcomp(lowcomp, bndpsd[1], bndpsd[2], 1);
681 excite[1] = bndpsd[1] - fgain - lowcomp;
682 begin = 7;
683 for (bin = 2; bin < 7; bin++) {
2fbbd087 684 if ((bndend != 7) || (bin != 6))
1b293437
JR
685 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
686 fastleak = bndpsd[bin] - fgain;
687 slowleak = bndpsd[bin] - sgain;
688 excite[bin] = fastleak - lowcomp;
2fbbd087 689 if ((bndend != 7) || (bin != 6))
1b293437
JR
690 if (bndpsd[bin] <= bndpsd[bin + 1]) {
691 begin = bin + 1;
692 break;
693 }
694 }
00585845 695 for (bin = begin; bin < FFMIN(bndend, 22); bin++) {
2fbbd087 696 if ((bndend != 7) || (bin != 6))
1b293437
JR
697 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
698 fastleak -= fdecay;
2fbbd087 699 fastleak = FFMAX(fastleak, (bndpsd[bin] - fgain));
1b293437 700 slowleak -= sdecay;
2fbbd087
JR
701 slowleak = FFMAX(slowleak, (bndpsd[bin] - sgain));
702 excite[bin] = FFMAX((fastleak - lowcomp), slowleak);
1b293437
JR
703 }
704 begin = 22;
705 }
706 else {
707 begin = bndstrt;
708 }
709 for (bin = begin; bin < bndend; bin++) {
710 fastleak -= fdecay;
2fbbd087 711 fastleak = FFMAX(fastleak, (bndpsd[bin] - fgain));
1b293437 712 slowleak -= sdecay;
2fbbd087 713 slowleak = FFMAX(slowleak, (bndpsd[bin] - sgain));
1b293437
JR
714 excite[bin] = FFMAX(fastleak, slowleak);
715 }
716
717 /* compute the masking curve */
718 for (bin = bndstrt; bin < bndend; bin++) {
719 if (bndpsd[bin] < dbknee)
720 excite[bin] += ((dbknee - bndpsd[bin]) >> 2);
721 mask[bin] = FFMAX(excite[bin], hth[bin][fscod]);
722 }
723
724 /* apply the delta bit allocation */
725 if (do_delta) {
726 band = 0;
727 for (seg = 0; seg < deltnseg + 1; seg++) {
2fbbd087
JR
728 band += deltoffst[seg];
729 if (deltba[seg] >= 4)
730 delta = (deltba[seg] - 3) << 7;
1b293437 731 else
2fbbd087
JR
732 delta = (deltba[seg] - 4) << 7;
733 for (k = 0; k < deltlen[seg]; k++) {
1b293437
JR
734 mask[band] += delta;
735 band++;
736 }
737 }
738 }
739
740 /*compute the bit allocation */
741 i = start;
742 j = masktab[start];
743 do {
2fbbd087 744 lastbin = FFMIN((bndtab[j] + bndsz[j]), end);
1b293437
JR
745 mask[j] -= snroffset;
746 mask[j] -= floor;
747 if (mask[j] < 0)
748 mask[j] = 0;
749 mask[j] &= 0x1fe0;
750 mask[j] += floor;
751 for (k = i; k < lastbin; k++) {
752 address = (psd[i] - mask[j]) >> 5;
2fbbd087
JR
753 address = FFMIN(63, (FFMAX(0, address)));
754 bap[i] = baptab[address];
1b293437
JR
755 i++;
756 }
757 j++;
758 } while (end > lastbin);
2aa2c5c4
JR
759}
760
2fbbd087
JR
761/* Check if snroffsets are zero. */
762static int is_snr_offsets_zero(AC3DecodeContext *ctx)
2aa2c5c4 763{
2fbbd087 764 int i;
1b293437 765
2fbbd087 766 if ((ctx->csnroffst) || (ctx->cplinu && ctx->cplfsnroffst) ||
98a27a8a 767 (ctx->lfeon && ctx->lfefsnroffst))
2fbbd087 768 return 0;
1b293437 769
98a27a8a 770 for (i = 0; i < ctx->nfchans; i++)
2fbbd087
JR
771 if (ctx->fsnroffst[i])
772 return 0;
773
774 return 1;
1b293437 775}
2aa2c5c4 776
486637af 777typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
98a27a8a
JR
778 int16_t l3_quantizers[3];
779 int16_t l5_quantizers[3];
780 int16_t l11_quantizers[2];
486637af
JR
781 int l3ptr;
782 int l5ptr;
783 int l11ptr;
784} mant_groups;
785
786#define TRANSFORM_COEFF(tc, m, e, f) (tc) = (m) * (f)[(e)]
787
788/* Get the transform coefficients for coupling channel and uncouple channels.
789 * The coupling transform coefficients starts at the the cplstrtmant, which is
790 * equal to endmant[ch] for fbw channels. Hence we can uncouple channels before
791 * getting transform coefficients for the channel.
792 */
793static int get_transform_coeffs_cpling(AC3DecodeContext *ctx, mant_groups *m)
1b293437 794{
486637af 795 GetBitContext *gb = &ctx->gb;
2fbbd087 796 int ch, start, end, cplbndstrc, bnd, gcode, tbap;
486637af 797 float cplcos[5], cplcoeff;
98a27a8a
JR
798 uint8_t *exps = ctx->dcplexps;
799 uint8_t *bap = ctx->cplbap;
800
98a27a8a
JR
801 cplbndstrc = ctx->cplbndstrc;
802 start = ctx->cplstrtmant;
803 bnd = 0;
804
805 while (start < ctx->cplendmant) {
806 end = start + 12;
807 while (cplbndstrc & 1) {
808 end += 12;
809 cplbndstrc >>= 1;
810 }
811 cplbndstrc >>= 1;
812 for (ch = 0; ch < ctx->nfchans; ch++)
813 cplcos[ch] = ctx->chcoeffs[ch] * ctx->cplco[ch][bnd];
814 bnd++;
815
816 while (start < end) {
817 tbap = bap[start];
818 switch(tbap) {
486637af 819 case 0:
2fbbd087 820 for (ch = 0; ch < ctx->nfchans; ch++)
98a27a8a 821 if (((ctx->chincpl) >> ch) & 1) {
45b0ed13 822 if ((ctx->dithflag >> ch) & 1) {
98a27a8a 823 TRANSFORM_COEFF(cplcoeff, dither_int16(&ctx->dith_state), exps[start], scale_factors);
45b0ed13 824 ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch] * LEVEL_MINUS_3DB;
486637af 825 } else
98a27a8a 826 ctx->transform_coeffs[ch + 1][start] = 0;
486637af 827 }
98a27a8a 828 start++;
486637af
JR
829 continue;
830 case 1:
831 if (m->l3ptr > 2) {
832 gcode = get_bits(gb, 5);
98a27a8a
JR
833 m->l3_quantizers[0] = l3_quantizers_1[gcode];
834 m->l3_quantizers[1] = l3_quantizers_2[gcode];
835 m->l3_quantizers[2] = l3_quantizers_3[gcode];
486637af
JR
836 m->l3ptr = 0;
837 }
98a27a8a 838 TRANSFORM_COEFF(cplcoeff, m->l3_quantizers[m->l3ptr++], exps[start], scale_factors);
486637af
JR
839 break;
840
841 case 2:
842 if (m->l5ptr > 2) {
843 gcode = get_bits(gb, 7);
98a27a8a
JR
844 m->l5_quantizers[0] = l5_quantizers_1[gcode];
845 m->l5_quantizers[1] = l5_quantizers_2[gcode];
846 m->l5_quantizers[2] = l5_quantizers_3[gcode];
486637af
JR
847 m->l5ptr = 0;
848 }
98a27a8a 849 TRANSFORM_COEFF(cplcoeff, m->l5_quantizers[m->l5ptr++], exps[start], scale_factors);
486637af
JR
850 break;
851
852 case 3:
2fbbd087 853 TRANSFORM_COEFF(cplcoeff, l7_quantizers[get_bits(gb, 3)], exps[start], scale_factors);
486637af
JR
854 break;
855
856 case 4:
857 if (m->l11ptr > 1) {
858 gcode = get_bits(gb, 7);
98a27a8a
JR
859 m->l11_quantizers[0] = l11_quantizers_1[gcode];
860 m->l11_quantizers[1] = l11_quantizers_2[gcode];
486637af
JR
861 m->l11ptr = 0;
862 }
98a27a8a 863 TRANSFORM_COEFF(cplcoeff, m->l11_quantizers[m->l11ptr++], exps[start], scale_factors);
486637af
JR
864 break;
865
866 case 5:
2fbbd087 867 TRANSFORM_COEFF(cplcoeff, l15_quantizers[get_bits(gb, 4)], exps[start], scale_factors);
486637af
JR
868 break;
869
870 default:
45b0ed13 871 TRANSFORM_COEFF(cplcoeff, get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]),
2fbbd087 872 exps[start], scale_factors);
486637af 873 }
98a27a8a
JR
874 for (ch = 0; ch < ctx->nfchans; ch++)
875 if ((ctx->chincpl >> ch) & 1)
2fbbd087 876 ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
98a27a8a 877 start++;
486637af
JR
878 }
879 }
880
881 return 0;
882}
1b293437
JR
883
884/* Get the transform coefficients for particular channel */
2fbbd087 885static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
1b293437 886{
2fbbd087
JR
887 GetBitContext *gb = &ctx->gb;
888 int i, gcode, tbap, dithflag, end;
889 uint8_t *exps;
890 uint8_t *bap;
891 float *coeffs;
486637af 892 float factors[25];
1b293437 893
486637af 894 for (i = 0; i < 25; i++)
2fbbd087
JR
895 factors[i] = scale_factors[i] * ctx->chcoeffs[ch_index];
896
897 if (ch_index != -1) { /* fbw channels */
898 dithflag = (ctx->dithflag >> ch_index) & 1;
899 exps = ctx->dexps[ch_index];
900 bap = ctx->bap[ch_index];
901 coeffs = ctx->transform_coeffs[ch_index + 1];
902 end = ctx->endmant[ch_index];
903 } else if (ch_index == -1) {
904 dithflag = 0;
905 exps = ctx->dlfeexps;
906 bap = ctx->lfebap;
907 coeffs = ctx->transform_coeffs[0];
908 end = 7;
909 }
1b293437 910
2fbbd087
JR
911
912 for (i = 0; i < end; i++) {
98a27a8a
JR
913 tbap = bap[i];
914 switch (tbap) {
1b293437 915 case 0:
45b0ed13 916 if (!dithflag) {
00585845
JR
917 coeffs[i] = 0;
918 continue;
919 }
920 else {
2fbbd087 921 TRANSFORM_COEFF(coeffs[i], dither_int16(&ctx->dith_state), exps[i], factors);
45b0ed13 922 coeffs[i] *= LEVEL_MINUS_3DB;
00585845
JR
923 continue;
924 }
1b293437
JR
925
926 case 1:
486637af 927 if (m->l3ptr > 2) {
00585845 928 gcode = get_bits(gb, 5);
98a27a8a
JR
929 m->l3_quantizers[0] = l3_quantizers_1[gcode];
930 m->l3_quantizers[1] = l3_quantizers_2[gcode];
931 m->l3_quantizers[2] = l3_quantizers_3[gcode];
486637af 932 m->l3ptr = 0;
1b293437 933 }
98a27a8a 934 TRANSFORM_COEFF(coeffs[i], m->l3_quantizers[m->l3ptr++], exps[i], factors);
00585845 935 continue;
1b293437
JR
936
937 case 2:
486637af 938 if (m->l5ptr > 2) {
00585845 939 gcode = get_bits(gb, 7);
98a27a8a
JR
940 m->l5_quantizers[0] = l5_quantizers_1[gcode];
941 m->l5_quantizers[1] = l5_quantizers_2[gcode];
942 m->l5_quantizers[2] = l5_quantizers_3[gcode];
486637af 943 m->l5ptr = 0;
1b293437 944 }
98a27a8a 945 TRANSFORM_COEFF(coeffs[i], m->l5_quantizers[m->l5ptr++], exps[i], factors);
00585845 946 continue;
1b293437
JR
947
948 case 3:
2fbbd087 949 TRANSFORM_COEFF(coeffs[i], l7_quantizers[get_bits(gb, 3)], exps[i], factors);
00585845 950 continue;
1b293437
JR
951
952 case 4:
486637af 953 if (m->l11ptr > 1) {
00585845 954 gcode = get_bits(gb, 7);
98a27a8a
JR
955 m->l11_quantizers[0] = l11_quantizers_1[gcode];
956 m->l11_quantizers[1] = l11_quantizers_2[gcode];
486637af 957 m->l11ptr = 0;
1b293437 958 }
98a27a8a 959 TRANSFORM_COEFF(coeffs[i], m->l11_quantizers[m->l11ptr++], exps[i], factors);
00585845 960 continue;
1b293437
JR
961
962 case 5:
2fbbd087 963 TRANSFORM_COEFF(coeffs[i], l15_quantizers[get_bits(gb, 4)], exps[i], factors);
00585845 964 continue;
1b293437
JR
965
966 default:
45b0ed13 967 TRANSFORM_COEFF(coeffs[i], get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]), exps[i], factors);
00585845 968 continue;
1b293437
JR
969 }
970 }
971
1b293437
JR
972 return 0;
973}
974
1b293437
JR
975static int get_transform_coeffs(AC3DecodeContext * ctx)
976{
486637af 977 int i, end;
1b293437 978 int got_cplchan = 0;
486637af
JR
979 mant_groups m;
980
981 m.l3ptr = m.l5ptr = m.l11ptr = 3;
1b293437 982
98a27a8a 983 for (i = 0; i < ctx->nfchans; i++) {
1b293437 984 /* transform coefficients for individual channel */
2fbbd087 985 if (get_transform_coeffs_ch(ctx, i, &m))
1b293437
JR
986 return -1;
987 /* tranform coefficients for coupling channels */
98a27a8a 988 if ((ctx->chincpl >> i) & 1) {
486637af 989 if (!got_cplchan) {
98a27a8a
JR
990 if (get_transform_coeffs_cpling(ctx, &m)) {
991 av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
486637af 992 return -1;
98a27a8a 993 }
486637af
JR
994 got_cplchan = 1;
995 }
98a27a8a 996 end = ctx->cplendmant;
486637af 997 } else
98a27a8a 998 end = ctx->endmant[i];
486637af 999 do
98a27a8a 1000 ctx->transform_coeffs[i + 1][end] = 0;
486637af
JR
1001 while(++end < 256);
1002 }
98a27a8a 1003 if (ctx->lfeon) {
2fbbd087 1004 if (get_transform_coeffs_ch(ctx, -1, &m))
1b293437 1005 return -1;
486637af 1006 for (i = 7; i < 256; i++) {
98a27a8a 1007 ctx->transform_coeffs[0][i] = 0;
1b293437
JR
1008 }
1009 }
1b293437
JR
1010
1011 return 0;
2aa2c5c4
JR
1012}
1013
00585845 1014static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
1b293437
JR
1015{
1016 float tmp0, tmp1;
1017
1018 while (start < end) {
98a27a8a
JR
1019 tmp0 = ctx->transform_coeffs[1][start];
1020 tmp1 = ctx->transform_coeffs[2][start];
1021 ctx->transform_coeffs[1][start] = tmp0 + tmp1;
1022 ctx->transform_coeffs[2][start] = tmp0 - tmp1;
1b293437
JR
1023 start++;
1024 }
1b293437 1025}
2aa2c5c4 1026
1b293437
JR
1027static void do_rematrixing(AC3DecodeContext *ctx)
1028{
2fbbd087
JR
1029 int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
1030 int end, bndend;
1031
1032 end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
1b293437 1033
98a27a8a 1034 if (ctx->rematflg & 1)
00585845 1035 do_rematrixing1(ctx, bnd1, bnd2);
2fbbd087 1036
98a27a8a 1037 if (ctx->rematflg & 2)
00585845 1038 do_rematrixing1(ctx, bnd2, bnd3);
2fbbd087
JR
1039
1040 bndend = bnd4;
1041 if (bndend > end) {
1042 bndend = end;
1043 if (ctx->rematflg & 4)
00585845 1044 do_rematrixing1(ctx, bnd3, bndend);
2fbbd087
JR
1045 } else {
1046 if (ctx->rematflg & 4)
00585845 1047 do_rematrixing1(ctx, bnd3, bnd4);
2fbbd087
JR
1048 if (ctx->rematflg & 8)
1049 do_rematrixing1(ctx, bnd4, end);
1b293437
JR
1050 }
1051}
2aa2c5c4 1052
1b293437
JR
1053static void get_downmix_coeffs(AC3DecodeContext *ctx)
1054{
98a27a8a
JR
1055 int from = ctx->acmod;
1056 int to = ctx->blkoutput;
1057 float clev = clevs[ctx->cmixlev];
1058 float slev = slevs[ctx->surmixlev];
45b0ed13 1059 float nf = 1.0; //normalization factor for downmix coeffs
6dc5d71f
JR
1060 int i;
1061
1062 if (!ctx->acmod) {
1063 ctx->chcoeffs[0] = 2 * ctx->dynrng;
1064 ctx->chcoeffs[1] = 2 * ctx->dynrng2;
1065 } else {
1066 for (i = 0; i < ctx->nfchans; i++)
1067 ctx->chcoeffs[i] = 2 * ctx->dynrng;
1068 }
1b293437
JR
1069
1070 if (to == AC3_OUTPUT_UNMODIFIED)
00585845 1071 return;
1b293437
JR
1072
1073 switch (from) {
1074 case AC3_INPUT_DUALMONO:
1075 switch (to) {
1076 case AC3_OUTPUT_MONO:
1077 case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */
45b0ed13
JR
1078 nf = 0.5;
1079 ctx->chcoeffs[0] *= nf;
1080 ctx->chcoeffs[1] *= nf;
1b293437
JR
1081 break;
1082 }
1083 break;
1084 case AC3_INPUT_MONO:
1085 switch (to) {
1086 case AC3_OUTPUT_STEREO:
45b0ed13
JR
1087 nf = LEVEL_MINUS_3DB;
1088 ctx->chcoeffs[0] *= nf;
1b293437
JR
1089 break;
1090 }
1091 break;
1092 case AC3_INPUT_STEREO:
1093 switch (to) {
1094 case AC3_OUTPUT_MONO:
45b0ed13
JR
1095 nf = LEVEL_MINUS_3DB;
1096 ctx->chcoeffs[0] *= nf;
1097 ctx->chcoeffs[1] *= nf;
1b293437
JR
1098 break;
1099 }
1100 break;
1101 case AC3_INPUT_3F:
1102 switch (to) {
1103 case AC3_OUTPUT_MONO:
45b0ed13
JR
1104 nf = LEVEL_MINUS_3DB / (1.0 + clev);
1105 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1106 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1107 ctx->chcoeffs[1] *= ((nf * clev * LEVEL_MINUS_3DB) / 2.0);
1b293437
JR
1108 break;
1109 case AC3_OUTPUT_STEREO:
45b0ed13
JR
1110 nf = 1.0 / (1.0 + clev);
1111 ctx->chcoeffs[0] *= nf;
1112 ctx->chcoeffs[2] *= nf;
1113 ctx->chcoeffs[1] *= (nf * clev);
1b293437
JR
1114 break;
1115 }
1116 break;
1117 case AC3_INPUT_2F_1R:
1118 switch (to) {
1119 case AC3_OUTPUT_MONO:
45b0ed13
JR
1120 nf = 2.0 * LEVEL_MINUS_3DB / (2.0 + slev);
1121 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1122 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1123 ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
1b293437
JR
1124 break;
1125 case AC3_OUTPUT_STEREO:
45b0ed13
JR
1126 nf = 1.0 / (1.0 + (slev * LEVEL_MINUS_3DB));
1127 ctx->chcoeffs[0] *= nf;
1128 ctx->chcoeffs[1] *= nf;
1129 ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
1b293437
JR
1130 break;
1131 case AC3_OUTPUT_DOLBY:
45b0ed13
JR
1132 nf = 1.0 / (1.0 + LEVEL_MINUS_3DB);
1133 ctx->chcoeffs[0] *= nf;
1134 ctx->chcoeffs[1] *= nf;
1135 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1b293437
JR
1136 break;
1137 }
1138 break;
1139 case AC3_INPUT_3F_1R:
1140 switch (to) {
1141 case AC3_OUTPUT_MONO:
45b0ed13
JR
1142 nf = LEVEL_MINUS_3DB / (1.0 + clev + (slev / 2.0));
1143 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1144 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1145 ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
1146 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1b293437
JR
1147 break;
1148 case AC3_OUTPUT_STEREO:
45b0ed13
JR
1149 nf = 1.0 / (1.0 + clev + (slev * LEVEL_MINUS_3DB));
1150 ctx->chcoeffs[0] *= nf;
1151 ctx->chcoeffs[2] *= nf;
1152 ctx->chcoeffs[1] *= (nf * clev);
1153 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1b293437
JR
1154 break;
1155 case AC3_OUTPUT_DOLBY:
45b0ed13
JR
1156 nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
1157 ctx->chcoeffs[0] *= nf;
1158 ctx->chcoeffs[1] *= nf;
1159 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1160 ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
1b293437
JR
1161 break;
1162 }
1163 break;
1164 case AC3_INPUT_2F_2R:
1165 switch (to) {
1166 case AC3_OUTPUT_MONO:
45b0ed13
JR
1167 nf = LEVEL_MINUS_3DB / (1.0 + slev);
1168 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1169 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1170 ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
1171 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1b293437
JR
1172 break;
1173 case AC3_OUTPUT_STEREO:
45b0ed13
JR
1174 nf = 1.0 / (1.0 + slev);
1175 ctx->chcoeffs[0] *= nf;
1176 ctx->chcoeffs[1] *= nf;
1177 ctx->chcoeffs[2] *= (nf * slev);
1178 ctx->chcoeffs[3] *= (nf * slev);
1b293437
JR
1179 break;
1180 case AC3_OUTPUT_DOLBY:
45b0ed13
JR
1181 nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
1182 ctx->chcoeffs[0] *= nf;
1183 ctx->chcoeffs[1] *= nf;
1184 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1185 ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
1b293437
JR
1186 break;
1187 }
1188 break;
1189 case AC3_INPUT_3F_2R:
1190 switch (to) {
1191 case AC3_OUTPUT_MONO:
45b0ed13
JR
1192 nf = LEVEL_MINUS_3DB / (1.0 + clev + slev);
1193 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1194 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1195 ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
1196 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1197 ctx->chcoeffs[4] *= (nf * slev * LEVEL_MINUS_3DB);
1b293437
JR
1198 break;
1199 case AC3_OUTPUT_STEREO:
45b0ed13
JR
1200 nf = 1.0 / (1.0 + clev + slev);
1201 ctx->chcoeffs[0] *= nf;
1202 ctx->chcoeffs[2] *= nf;
1203 ctx->chcoeffs[1] *= (nf * clev);
1204 ctx->chcoeffs[3] *= (nf * slev);
1205 ctx->chcoeffs[4] *= (nf * slev);
1b293437
JR
1206 break;
1207 case AC3_OUTPUT_DOLBY:
45b0ed13
JR
1208 nf = 1.0 / (1.0 + (3.0 * LEVEL_MINUS_3DB));
1209 ctx->chcoeffs[0] *= nf;
1210 ctx->chcoeffs[1] *= nf;
1211 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1212 ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
1213 ctx->chcoeffs[4] *= (nf * LEVEL_MINUS_3DB);
1b293437
JR
1214 break;
1215 }
1216 break;
1217 }
1218}
2aa2c5c4 1219
00585845 1220static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx)
1b293437
JR
1221{
1222 int i;
98a27a8a 1223 float (*output)[BLOCK_SIZE] = ctx->output;
2aa2c5c4 1224
00585845
JR
1225 for (i = 0; i < 256; i++)
1226 output[1][i] += output[2][i];
1227 memset(output[2], 0, sizeof(output[2]));
1b293437
JR
1228}
1229
00585845 1230static inline void mix_dualmono_to_stereo(AC3DecodeContext *ctx)
2aa2c5c4 1231{
1b293437
JR
1232 int i;
1233 float tmp;
98a27a8a 1234 float (*output)[BLOCK_SIZE] = ctx->output;
1b293437
JR
1235
1236 for (i = 0; i < 256; i++) {
00585845
JR
1237 tmp = output[1][i] + output[2][i];
1238 output[1][i] = output[2][i] = tmp;
1b293437 1239 }
2aa2c5c4
JR
1240}
1241
00585845 1242static inline void upmix_mono_to_stereo(AC3DecodeContext *ctx)
1b293437
JR
1243{
1244 int i;
98a27a8a 1245 float (*output)[BLOCK_SIZE] = ctx->output;
2aa2c5c4 1246
1b293437 1247 for (i = 0; i < 256; i++)
00585845 1248 output[2][i] = output[1][i];
1b293437
JR
1249}
1250
00585845 1251static inline void mix_stereo_to_mono(AC3DecodeContext *ctx)
2aa2c5c4 1252{
1b293437 1253 int i;
98a27a8a 1254 float (*output)[BLOCK_SIZE] = ctx->output;
2aa2c5c4 1255
00585845
JR
1256 for (i = 0; i < 256; i++)
1257 output[1][i] += output[2][i];
1258 memset(output[2], 0, sizeof(output[2]));
1b293437 1259}
2aa2c5c4 1260
00585845 1261static inline void mix_3f_to_mono(AC3DecodeContext *ctx)
1b293437
JR
1262{
1263 int i;
98a27a8a 1264 float (*output)[BLOCK_SIZE] = ctx->output;
2aa2c5c4 1265
00585845
JR
1266 for (i = 0; i < 256; i++)
1267 output[1][i] += (output[2][i] + output[3][i]);
1268 memset(output[2], 0, sizeof(output[2]));
1269 memset(output[3], 0, sizeof(output[3]));
2aa2c5c4
JR
1270}
1271
00585845 1272static inline void mix_3f_to_stereo(AC3DecodeContext *ctx)
2aa2c5c4 1273{
1b293437 1274 int i;
98a27a8a 1275 float (*output)[BLOCK_SIZE] = ctx->output;
2aa2c5c4 1276
1b293437 1277 for (i = 0; i < 256; i++) {
00585845
JR
1278 output[1][i] += output[2][i];
1279 output[2][i] += output[3][i];
1b293437 1280 }
00585845 1281 memset(output[3], 0, sizeof(output[3]));
1b293437 1282}
2aa2c5c4 1283
00585845 1284static inline void mix_2f_1r_to_mono(AC3DecodeContext *ctx)
1b293437
JR
1285{
1286 int i;
98a27a8a 1287 float (*output)[BLOCK_SIZE] = ctx->output;
00585845
JR
1288
1289 for (i = 0; i < 256; i++)
1290 output[1][i] += (output[2][i] + output[3][i]);
1291 memset(output[2], 0, sizeof(output[2]));
1292 memset(output[3], 0, sizeof(output[3]));
1b293437 1293
2aa2c5c4
JR
1294}
1295
00585845 1296static inline void mix_2f_1r_to_stereo(AC3DecodeContext *ctx)
2aa2c5c4 1297{
1b293437 1298 int i;
98a27a8a 1299 float (*output)[BLOCK_SIZE] = ctx->output;
1b293437
JR
1300
1301 for (i = 0; i < 256; i++) {
00585845
JR
1302 output[1][i] += output[2][i];
1303 output[2][i] += output[3][i];
2aa2c5c4 1304 }
00585845 1305 memset(output[3], 0, sizeof(output[3]));
1b293437 1306}
2aa2c5c4 1307
00585845 1308static inline void mix_2f_1r_to_dolby(AC3DecodeContext *ctx)
1b293437
JR
1309{
1310 int i;
98a27a8a 1311 float (*output)[BLOCK_SIZE] = ctx->output;
2aa2c5c4 1312
1b293437 1313 for (i = 0; i < 256; i++) {
00585845
JR
1314 output[1][i] -= output[3][i];
1315 output[2][i] += output[3][i];
1b293437 1316 }
00585845 1317 memset(output[3], 0, sizeof(output[3]));
2aa2c5c4
JR
1318}
1319
00585845 1320static inline void mix_3f_1r_to_mono(AC3DecodeContext *ctx)
2aa2c5c4 1321{
1b293437 1322 int i;
98a27a8a 1323 float (*output)[BLOCK_SIZE] = ctx->output;
1b293437 1324
00585845
JR
1325 for (i = 0; i < 256; i++)
1326 output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1327 memset(output[2], 0, sizeof(output[2]));
1328 memset(output[3], 0, sizeof(output[3]));
1329 memset(output[4], 0, sizeof(output[4]));
1b293437
JR
1330}
1331
00585845 1332static inline void mix_3f_1r_to_stereo(AC3DecodeContext *ctx)
1b293437
JR
1333{
1334 int i;
98a27a8a 1335 float (*output)[BLOCK_SIZE] = ctx->output;
1b293437
JR
1336
1337 for (i = 0; i < 256; i++) {
00585845
JR
1338 output[1][i] += (output[2][i] + output[4][i]);
1339 output[2][i] += (output[3][i] + output[4][i]);
1b293437 1340 }
00585845
JR
1341 memset(output[3], 0, sizeof(output[3]));
1342 memset(output[4], 0, sizeof(output[4]));
1b293437
JR
1343}
1344
00585845 1345static inline void mix_3f_1r_to_dolby(AC3DecodeContext *ctx)
1b293437
JR
1346{
1347 int i;
98a27a8a 1348 float (*output)[BLOCK_SIZE] = ctx->output;
1b293437
JR
1349
1350 for (i = 0; i < 256; i++) {
00585845
JR
1351 output[1][i] += (output[2][i] - output[4][i]);
1352 output[2][i] += (output[3][i] + output[4][i]);
1b293437 1353 }
00585845
JR
1354 memset(output[3], 0, sizeof(output[3]));
1355 memset(output[4], 0, sizeof(output[4]));
1b293437
JR
1356}
1357
00585845 1358static inline void mix_2f_2r_to_mono(AC3DecodeContext *ctx)
1b293437
JR
1359{
1360 int i;
98a27a8a 1361 float (*output)[BLOCK_SIZE] = ctx->output;
1b293437 1362
00585845
JR
1363 for (i = 0; i < 256; i++)
1364 output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1365 memset(output[2], 0, sizeof(output[2]));
1366 memset(output[3], 0, sizeof(output[3]));
1367 memset(output[4], 0, sizeof(output[4]));
1b293437
JR
1368}
1369
00585845 1370static inline void mix_2f_2r_to_stereo(AC3DecodeContext *ctx)
1b293437
JR
1371{
1372 int i;
98a27a8a 1373 float (*output)[BLOCK_SIZE] = ctx->output;
1b293437
JR
1374
1375 for (i = 0; i < 256; i++) {
00585845
JR
1376 output[1][i] += output[3][i];
1377 output[2][i] += output[4][i];
1b293437 1378 }
00585845
JR
1379 memset(output[3], 0, sizeof(output[3]));
1380 memset(output[4], 0, sizeof(output[4]));
1b293437
JR
1381}
1382
00585845 1383static inline void mix_2f_2r_to_dolby(AC3DecodeContext *ctx)
1b293437
JR
1384{
1385 int i;
98a27a8a 1386 float (*output)[BLOCK_SIZE] = ctx->output;
1b293437
JR
1387
1388 for (i = 0; i < 256; i++) {
00585845
JR
1389 output[1][i] -= output[3][i];
1390 output[2][i] += output[4][i];
1b293437 1391 }
00585845
JR
1392 memset(output[3], 0, sizeof(output[3]));
1393 memset(output[4], 0, sizeof(output[4]));
1b293437
JR
1394}
1395
00585845 1396static inline void mix_3f_2r_to_mono(AC3DecodeContext *ctx)
1b293437
JR
1397{
1398 int i;
98a27a8a 1399 float (*output)[BLOCK_SIZE] = ctx->output;
1b293437 1400
00585845
JR
1401 for (i = 0; i < 256; i++)
1402 output[1][i] += (output[2][i] + output[3][i] + output[4][i] + output[5][i]);
1403 memset(output[2], 0, sizeof(output[2]));
1404 memset(output[3], 0, sizeof(output[3]));
1405 memset(output[4], 0, sizeof(output[4]));
1406 memset(output[5], 0, sizeof(output[5]));
1b293437
JR
1407}
1408
00585845 1409static inline void mix_3f_2r_to_stereo(AC3DecodeContext *ctx)
1b293437
JR
1410{
1411 int i;
98a27a8a 1412 float (*output)[BLOCK_SIZE] = ctx->output;
1b293437
JR
1413
1414 for (i = 0; i < 256; i++) {
00585845
JR
1415 output[1][i] += (output[2][i] + output[4][i]);
1416 output[2][i] += (output[3][i] + output[5][i]);
1b293437 1417 }
00585845
JR
1418 memset(output[3], 0, sizeof(output[3]));
1419 memset(output[4], 0, sizeof(output[4]));
1420 memset(output[5], 0, sizeof(output[5]));
1b293437
JR
1421}
1422
00585845 1423static inline void mix_3f_2r_to_dolby(AC3DecodeContext *ctx)
1b293437
JR
1424{
1425 int i;
98a27a8a 1426 float (*output)[BLOCK_SIZE] = ctx->output;
2aa2c5c4 1427
1b293437 1428 for (i = 0; i < 256; i++) {
00585845
JR
1429 output[1][i] += (output[2][i] - output[4][i] - output[5][i]);
1430 output[2][i] += (output[3][i] + output[4][i] + output[5][i]);
1b293437 1431 }
00585845
JR
1432 memset(output[3], 0, sizeof(output[3]));
1433 memset(output[4], 0, sizeof(output[4]));
1434 memset(output[5], 0, sizeof(output[5]));
1b293437
JR
1435}
1436
1437static void do_downmix(AC3DecodeContext *ctx)
1438{
98a27a8a
JR
1439 int from = ctx->acmod;
1440 int to = ctx->blkoutput;
1b293437
JR
1441
1442 switch (from) {
1443 case AC3_INPUT_DUALMONO:
1444 switch (to) {
1445 case AC3_OUTPUT_MONO:
00585845 1446 mix_dualmono_to_mono(ctx);
1b293437 1447 break;
486637af 1448 case AC3_OUTPUT_STEREO: /* We assume that sum of both mono channels is requested */
00585845 1449 mix_dualmono_to_stereo(ctx);
1b293437
JR
1450 break;
1451 }
1452 break;
1453 case AC3_INPUT_MONO:
1454 switch (to) {
1455 case AC3_OUTPUT_STEREO:
00585845 1456 upmix_mono_to_stereo(ctx);
1b293437
JR
1457 break;
1458 }
1459 break;
1460 case AC3_INPUT_STEREO:
1461 switch (to) {
1462 case AC3_OUTPUT_MONO:
00585845 1463 mix_stereo_to_mono(ctx);
1b293437
JR
1464 break;
1465 }
1466 break;
1467 case AC3_INPUT_3F:
1468 switch (to) {
1469 case AC3_OUTPUT_MONO:
00585845 1470 mix_3f_to_mono(ctx);
1b293437
JR
1471 break;
1472 case AC3_OUTPUT_STEREO:
00585845 1473 mix_3f_to_stereo(ctx);
1b293437
JR
1474 break;
1475 }
1476 break;
1477 case AC3_INPUT_2F_1R:
1478 switch (to) {
1479 case AC3_OUTPUT_MONO:
00585845 1480 mix_2f_1r_to_mono(ctx);
1b293437
JR
1481 break;
1482 case AC3_OUTPUT_STEREO:
00585845 1483 mix_2f_1r_to_stereo(ctx);
1b293437
JR
1484 break;
1485 case AC3_OUTPUT_DOLBY:
00585845 1486 mix_2f_1r_to_dolby(ctx);
1b293437
JR
1487 break;
1488 }
1489 break;
1490 case AC3_INPUT_3F_1R:
1491 switch (to) {
1492 case AC3_OUTPUT_MONO:
00585845 1493 mix_3f_1r_to_mono(ctx);
1b293437
JR
1494 break;
1495 case AC3_OUTPUT_STEREO:
00585845 1496 mix_3f_1r_to_stereo(ctx);
1b293437
JR
1497 break;
1498 case AC3_OUTPUT_DOLBY:
00585845 1499 mix_3f_1r_to_dolby(ctx);
1b293437
JR
1500 break;
1501 }
1502 break;
1503 case AC3_INPUT_2F_2R:
1504 switch (to) {
1505 case AC3_OUTPUT_MONO:
00585845 1506 mix_2f_2r_to_mono(ctx);
1b293437
JR
1507 break;
1508 case AC3_OUTPUT_STEREO:
00585845 1509 mix_2f_2r_to_stereo(ctx);
1b293437
JR
1510 break;
1511 case AC3_OUTPUT_DOLBY:
00585845 1512 mix_2f_2r_to_dolby(ctx);
1b293437
JR
1513 break;
1514 }
1515 break;
1516 case AC3_INPUT_3F_2R:
1517 switch (to) {
1518 case AC3_OUTPUT_MONO:
00585845 1519 mix_3f_2r_to_mono(ctx);
1b293437
JR
1520 break;
1521 case AC3_OUTPUT_STEREO:
00585845 1522 mix_3f_2r_to_stereo(ctx);
1b293437
JR
1523 break;
1524 case AC3_OUTPUT_DOLBY:
00585845 1525 mix_3f_2r_to_dolby(ctx);
1b293437
JR
1526 break;
1527 }
1528 break;
1529 }
2aa2c5c4
JR
1530}
1531
486637af
JR
1532static void dump_floats(const char *name, int prec, const float *tab, int n)
1533{
1534 int i;
1535
1536 av_log(NULL, AV_LOG_INFO, "%s[%d]:\n", name, n);
1537 for(i=0;i<n;i++) {
1538 if ((i & 7) == 0)
1539 av_log(NULL, AV_LOG_INFO, "%4d: ", i);
1540 av_log(NULL, AV_LOG_INFO, " %8.*f", prec, tab[i]);
1541 if ((i & 7) == 7)
1542 av_log(NULL, AV_LOG_INFO, "\n");
1543 }
1544 if ((i & 7) != 0)
1545 av_log(NULL, AV_LOG_INFO, "\n");
1546}
1547
98a27a8a
JR
1548#define CMUL(pre, pim, are, aim, bre, bim) \
1549{\
1550 float _are = (are);\
1551 float _aim = (aim);\
1552 float _bre = (bre);\
1553 float _bim = (bim);\
1554 (pre) = _are * _bre - _aim * _bim;\
1555 (pim) = _are * _bim + _aim * _bre;\
1556}
1557
45b0ed13 1558static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
486637af 1559{
45b0ed13 1560 int k;
98a27a8a 1561 float x1[128], x2[128];
1d0a6f52 1562 float *ptr;
98a27a8a 1563
45b0ed13
JR
1564 for (k = 0; k < N / 4; k++) {
1565 x1[k] = ctx->transform_coeffs[chindex][2 * k];
1566 x2[k] = ctx->transform_coeffs[chindex][2 * k + 1];
98a27a8a
JR
1567 }
1568
45b0ed13
JR
1569 ff_imdct_calc(&ctx->imdct_256, ctx->tmp_output, x1, ctx->tmp_imdct);
1570 ff_imdct_calc(&ctx->imdct_256, ctx->tmp_output + 256, x2, ctx->tmp_imdct);
486637af 1571
1d0a6f52 1572 ptr = ctx->output[chindex];
6dc5d71f 1573 ctx->dsp.vector_fmul_add_add(ptr, ctx->tmp_output, ctx->window, ctx->delay[chindex], 384, BLOCK_SIZE, 1);
1d0a6f52 1574 ptr = ctx->delay[chindex];
6dc5d71f 1575 ctx->dsp.vector_fmul_reverse(ptr, ctx->tmp_output + 256, ctx->window, BLOCK_SIZE);
486637af
JR
1576}
1577
45b0ed13 1578static void do_imdct_512(AC3DecodeContext *ctx, int chindex)
98a27a8a 1579{
1d0a6f52 1580 float *ptr;
98a27a8a 1581
45b0ed13
JR
1582 ff_imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1583 ctx->transform_coeffs[chindex], ctx->tmp_imdct);
1d0a6f52 1584 ptr = ctx->output[chindex];
6dc5d71f 1585 ctx->dsp.vector_fmul_add_add(ptr, ctx->tmp_output, ctx->window, ctx->delay[chindex], 384, BLOCK_SIZE, 1);
1d0a6f52 1586 ptr = ctx->delay[chindex];
6dc5d71f 1587 ctx->dsp.vector_fmul_reverse(ptr, ctx->tmp_output + 256, ctx->window, BLOCK_SIZE);
98a27a8a 1588}
486637af
JR
1589
1590static inline void do_imdct(AC3DecodeContext *ctx)
1591{
486637af
JR
1592 int i;
1593
98a27a8a 1594 if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
45b0ed13 1595 do_imdct_512(ctx, 0);
486637af 1596 }
2fbbd087 1597 for (i = 0; i < ctx->nfchans; i++) {
45b0ed13
JR
1598 if ((ctx->blksw >> i) & 1)
1599 do_imdct_256(ctx, i + 1);
1600 else
1601 do_imdct_512(ctx, i + 1);
486637af
JR
1602 }
1603}
1604
98a27a8a 1605static int ac3_parse_audio_block(AC3DecodeContext * ctx)
2aa2c5c4 1606{
98a27a8a
JR
1607 int nfchans = ctx->nfchans;
1608 int acmod = ctx->acmod;
00585845 1609 int i, bnd, rbnd, seg, grpsize;
1b293437 1610 GetBitContext *gb = &ctx->gb;
1b293437 1611 int bit_alloc_flags = 0;
2fbbd087 1612 float drange;
00585845 1613 uint8_t *dexps;
2fbbd087 1614 int mstrcplco, cplcoexp, cplcomant;
98a27a8a 1615 int dynrng, chbwcod, ngrps, cplabsexp, skipl;
1b293437 1616
67414da7 1617 ctx->blksw = 0;
1b293437 1618 for (i = 0; i < nfchans; i++) /*block switch flag */
98a27a8a
JR
1619 ctx->blksw |= get_bits1(gb) << i;
1620
67414da7 1621 ctx->dithflag = 0;
1b293437 1622 for (i = 0; i < nfchans; i++) /* dithering flag */
98a27a8a
JR
1623 ctx->dithflag |= get_bits1(gb) << i;
1624
00585845 1625 if (get_bits1(gb)) { /* dynamic range */
45b0ed13 1626 dynrng = get_sbits(gb, 8);
6dc5d71f 1627 ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1b293437 1628 }
98a27a8a
JR
1629
1630 if (acmod == 0x00 && get_bits1(gb)) { /* dynamic range 1+1 mode */
45b0ed13 1631 dynrng = get_sbits(gb, 8);
6dc5d71f 1632 ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
98a27a8a
JR
1633 }
1634
1b293437 1635 get_downmix_coeffs(ctx);
98a27a8a 1636
00585845 1637 if (get_bits1(gb)) { /* coupling strategy */
98a27a8a 1638 ctx->cplinu = get_bits1(gb);
2fbbd087
JR
1639 ctx->cplbndstrc = 0;
1640 ctx->chincpl = 0;
98a27a8a 1641 if (ctx->cplinu) { /* coupling in use */
1b293437 1642 for (i = 0; i < nfchans; i++)
98a27a8a
JR
1643 ctx->chincpl |= get_bits1(gb) << i;
1644
1b293437 1645 if (acmod == 0x02)
98a27a8a
JR
1646 ctx->phsflginu = get_bits1(gb); //phase flag in use
1647
1648 ctx->cplbegf = get_bits(gb, 4);
1649 ctx->cplendf = get_bits(gb, 4);
1650
1651 if (3 + ctx->cplendf - ctx->cplbegf < 0) {
1652 av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", ctx->cplendf, ctx->cplbegf);
00585845 1653 return -1;
98a27a8a
JR
1654 }
1655
1656 ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf;
1657 ctx->cplstrtmant = ctx->cplbegf * 12 + 37;
1658 ctx->cplendmant = ctx->cplendf * 12 + 73;
98a27a8a 1659 for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
00585845 1660 if (get_bits1(gb)) {
98a27a8a
JR
1661 ctx->cplbndstrc |= 1 << i;
1662 ctx->ncplbnd--;
1b293437
JR
1663 }
1664 }
1665 }
98a27a8a
JR
1666
1667 if (ctx->cplinu) {
1668 ctx->cplcoe = 0;
1669
1b293437 1670 for (i = 0; i < nfchans; i++)
98a27a8a 1671 if ((ctx->chincpl) >> i & 1)
00585845 1672 if (get_bits1(gb)) { /* coupling co-ordinates */
2fbbd087 1673 ctx->cplcoe |= 1 << i;
486637af 1674 mstrcplco = 3 * get_bits(gb, 2);
98a27a8a 1675 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
486637af
JR
1676 cplcoexp = get_bits(gb, 4);
1677 cplcomant = get_bits(gb, 4);
1678 if (cplcoexp == 15)
1679 cplcomant <<= 14;
1680 else
1681 cplcomant = (cplcomant | 0x10) << 13;
98a27a8a 1682 ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
486637af 1683 }
486637af 1684 }
98a27a8a 1685
2fbbd087 1686 if (acmod == 0x02 && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
98a27a8a
JR
1687 for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
1688 if (get_bits1(gb))
1689 ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
2aa2c5c4 1690 }
98a27a8a
JR
1691
1692 if (acmod == 0x02) {/* rematrixing */
1693 ctx->rematstr = get_bits1(gb);
1694 if (ctx->rematstr) {
1695 ctx->rematflg = 0;
1696
1697 if (!(ctx->cplinu) || ctx->cplbegf > 2)
1b293437 1698 for (rbnd = 0; rbnd < 4; rbnd++)
98a27a8a
JR
1699 ctx->rematflg |= get_bits1(gb) << rbnd;
1700 if (ctx->cplbegf > 0 && ctx->cplbegf <= 2 && ctx->cplinu)
1b293437 1701 for (rbnd = 0; rbnd < 3; rbnd++)
98a27a8a
JR
1702 ctx->rematflg |= get_bits1(gb) << rbnd;
1703 if (ctx->cplbegf == 0 && ctx->cplinu)
1b293437 1704 for (rbnd = 0; rbnd < 2; rbnd++)
98a27a8a 1705 ctx->rematflg |= get_bits1(gb) << rbnd;
1b293437 1706 }
98a27a8a
JR
1707 }
1708
1709 ctx->cplexpstr = AC3_EXPSTR_REUSE;
1710 ctx->lfeexpstr = AC3_EXPSTR_REUSE;
1711 if (ctx->cplinu) /* coupling exponent strategy */
1712 ctx->cplexpstr = get_bits(gb, 2);
1713 for (i = 0; i < nfchans; i++) /* channel exponent strategy */
1714 ctx->chexpstr[i] = get_bits(gb, 2);
1715 if (ctx->lfeon) /* lfe exponent strategy */
1716 ctx->lfeexpstr = get_bits1(gb);
1717
1b293437 1718 for (i = 0; i < nfchans; i++) /* channel bandwidth code */
98a27a8a
JR
1719 if (ctx->chexpstr[i] != AC3_EXPSTR_REUSE) {
1720 if ((ctx->chincpl >> i) & 1)
1721 ctx->endmant[i] = ctx->cplstrtmant;
00585845 1722 else {
98a27a8a
JR
1723 chbwcod = get_bits(gb, 6);
1724 if (chbwcod > 60) {
1725 av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
00585845
JR
1726 return -1;
1727 }
98a27a8a 1728 ctx->endmant[i] = chbwcod * 3 + 73;
1b293437 1729 }
00585845 1730 }
98a27a8a
JR
1731
1732 if (ctx->cplexpstr != AC3_EXPSTR_REUSE) {/* coupling exponents */
2fbbd087 1733 bit_alloc_flags = 64;
98a27a8a
JR
1734 cplabsexp = get_bits(gb, 4) << 1;
1735 ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
1736 if (decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant)) {
486637af
JR
1737 av_log(NULL, AV_LOG_ERROR, "error decoding coupling exponents\n");
1738 return -1;
1b293437 1739 }
486637af 1740 }
98a27a8a 1741
1b293437 1742 for (i = 0; i < nfchans; i++) /* fbw channel exponents */
98a27a8a 1743 if (ctx->chexpstr[i] != AC3_EXPSTR_REUSE) {
1b293437 1744 bit_alloc_flags |= 1 << i;
98a27a8a
JR
1745 grpsize = 3 << (ctx->chexpstr[i] - 1);
1746 ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
1747 dexps = ctx->dexps[i];
00585845 1748 dexps[0] = get_bits(gb, 4);
98a27a8a 1749 if (decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1)) {
00585845
JR
1750 av_log(NULL, AV_LOG_ERROR, "error decoding channel %d exponents\n", i);
1751 return -1;
1752 }
98a27a8a 1753 skip_bits(gb, 2); /* skip gainrng */
1b293437 1754 }
98a27a8a
JR
1755
1756 if (ctx->lfeexpstr != AC3_EXPSTR_REUSE) { /* lfe exponents */
486637af 1757 bit_alloc_flags |= 32;
98a27a8a
JR
1758 ctx->dlfeexps[0] = get_bits(gb, 4);
1759 if (decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1)) {
486637af
JR
1760 av_log(NULL, AV_LOG_ERROR, "error decoding lfe exponents\n");
1761 return -1;
1b293437 1762 }
486637af 1763 }
1b293437 1764
00585845 1765 if (get_bits1(gb)) { /* bit allocation information */
2fbbd087 1766 bit_alloc_flags = 127;
98a27a8a
JR
1767 ctx->sdcycod = get_bits(gb, 2);
1768 ctx->fdcycod = get_bits(gb, 2);
1769 ctx->sgaincod = get_bits(gb, 2);
1770 ctx->dbpbcod = get_bits(gb, 2);
1771 ctx->floorcod = get_bits(gb, 3);
1b293437 1772 }
98a27a8a 1773
00585845 1774 if (get_bits1(gb)) { /* snroffset */
2fbbd087 1775 bit_alloc_flags = 127;
98a27a8a
JR
1776 ctx->csnroffst = get_bits(gb, 6);
1777 if (ctx->cplinu) { /* couling fine snr offset and fast gain code */
1778 ctx->cplfsnroffst = get_bits(gb, 4);
1779 ctx->cplfgaincod = get_bits(gb, 3);
1b293437
JR
1780 }
1781 for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
98a27a8a
JR
1782 ctx->fsnroffst[i] = get_bits(gb, 4);
1783 ctx->fgaincod[i] = get_bits(gb, 3);
1b293437 1784 }
98a27a8a
JR
1785 if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
1786 ctx->lfefsnroffst = get_bits(gb, 4);
1787 ctx->lfefgaincod = get_bits(gb, 3);
1b293437
JR
1788 }
1789 }
98a27a8a 1790
98a27a8a
JR
1791 if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
1792 bit_alloc_flags |= 64;
1793 ctx->cplfleak = get_bits(gb, 3);
1794 ctx->cplsleak = get_bits(gb, 3);
1795 }
1796
00585845 1797 if (get_bits1(gb)) { /* delta bit allocation information */
2fbbd087 1798 bit_alloc_flags = 127;
98a27a8a
JR
1799
1800 if (ctx->cplinu) {
1801 ctx->cpldeltbae = get_bits(gb, 2);
1802 if (ctx->cpldeltbae == AC3_DBASTR_RESERVED) {
1b293437
JR
1803 av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
1804 return -1;
1805 }
1806 }
98a27a8a 1807
1b293437 1808 for (i = 0; i < nfchans; i++) {
98a27a8a
JR
1809 ctx->deltbae[i] = get_bits(gb, 2);
1810 if (ctx->deltbae[i] == AC3_DBASTR_RESERVED) {
1b293437
JR
1811 av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
1812 return -1;
1813 }
1814 }
98a27a8a
JR
1815
1816 if (ctx->cplinu)
1817 if (ctx->cpldeltbae == AC3_DBASTR_NEW) { /*coupling delta offset, len and bit allocation */
1818 ctx->cpldeltnseg = get_bits(gb, 3);
1819 for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
1820 ctx->cpldeltoffst[seg] = get_bits(gb, 5);
1821 ctx->cpldeltlen[seg] = get_bits(gb, 4);
1822 ctx->cpldeltba[seg] = get_bits(gb, 3);
1b293437
JR
1823 }
1824 }
98a27a8a 1825
1b293437 1826 for (i = 0; i < nfchans; i++)
98a27a8a
JR
1827 if (ctx->deltbae[i] == AC3_DBASTR_NEW) {/*channel delta offset, len and bit allocation */
1828 ctx->deltnseg[i] = get_bits(gb, 3);
1829 for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
1830 ctx->deltoffst[i][seg] = get_bits(gb, 5);
1831 ctx->deltlen[i][seg] = get_bits(gb, 4);
1832 ctx->deltba[i][seg] = get_bits(gb, 3);
1b293437
JR
1833 }
1834 }
1835 }
00585845 1836
2fbbd087
JR
1837 if (bit_alloc_flags) {
1838 if (is_snr_offsets_zero(ctx)) {
1839 memset(ctx->cplbap, 0, sizeof (ctx->cplbap));
1840 memset(ctx->lfebap, 0, sizeof (ctx->lfebap));
1841 for (i = 0; i < nfchans; i++)
1842 memset(ctx->bap[i], 0, sizeof(ctx->bap[i]));
1843 } else {
1844 if (ctx->chincpl && (bit_alloc_flags & 64))
1845 do_bit_allocation(ctx, 5);
1846 for (i = 0; i < nfchans; i++)
1847 if ((bit_alloc_flags >> i) & 1)
1848 do_bit_allocation(ctx, i);
1849 if (ctx->lfeon && (bit_alloc_flags & 32))
1850 do_bit_allocation(ctx, 6);
1851 }
1852 }
98a27a8a 1853
00585845 1854 if (get_bits1(gb)) { /* unused dummy data */
98a27a8a
JR
1855 skipl = get_bits(gb, 9);
1856 while(skipl--)
00585845 1857 skip_bits(gb, 8);
1b293437
JR
1858 }
1859 /* unpack the transform coefficients
1860 * * this also uncouples channels if coupling is in use.
1861 */
1862 if (get_transform_coeffs(ctx)) {
1863 av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1864 return -1;
1865 }
486637af 1866 /*for (i = 0; i < nfchans; i++)
98a27a8a 1867 dump_floats("channel transform coefficients", 10, ctx->transform_coeffs[i + 1], BLOCK_SIZE);*/
486637af 1868
1b293437 1869 /* recover coefficients if rematrixing is in use */
67414da7 1870 if (ctx->rematflg)
1b293437
JR
1871 do_rematrixing(ctx);
1872
d7bcc4ad
JR
1873 do_downmix(ctx);
1874
486637af 1875 do_imdct(ctx);
6bd4660c 1876 /*for(i = 0; i < nfchans; i++)
98a27a8a 1877 dump_floats("channel output", 10, ctx->output[i + 1], BLOCK_SIZE);*/
486637af 1878
1b293437
JR
1879 return 0;
1880}
1881
d7bcc4ad 1882/*static inline int16_t convert(float f)
486637af 1883{
45b0ed13
JR
1884 if (f >= 1.0)
1885 return 32767;
1886 else if (f <= -1.0)
1887 return -32768;
1888 else
1889 return (lrintf(f * 32767.0));
d7bcc4ad
JR
1890}*/
1891
1892static inline int16_t convert(int32_t i)
1893{
1894 if (i > 0x43c07fff)
1895 return 32767;
1896 else if (i <= 0x43bf8000)
1897 return -32768;
1898 else
1899 return (i - 0x43c00000);
486637af
JR
1900}
1901
41277578
JR
1902static int frame_count = 0;
1903
00585845 1904static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1b293437 1905{
00585845 1906 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1b293437 1907 int frame_start;
00585845 1908 int16_t *out_samples = (int16_t *)data;
d7bcc4ad
JR
1909 int i, j, k, start;
1910 int32_t *int_ptr[6];
1b293437 1911
d7bcc4ad
JR
1912 for (i = 0; i < 6; i++)
1913 int_ptr[i] = (int32_t *)(&ctx->output[i]);
1914
1915 //av_log(NULL, AV_LOG_INFO, "decoding frame %d buf_size = %d\n", frame_count++, buf_size);
41277578 1916
1b293437
JR
1917 //Synchronize the frame.
1918 frame_start = ac3_synchronize(buf, buf_size);
1919 if (frame_start == -1) {
1920 av_log(avctx, AV_LOG_ERROR, "frame is not synchronized\n");
1921 *data_size = 0;
00585845 1922 return buf_size;
1b293437 1923 }
2aa2c5c4 1924
1b293437
JR
1925 //Initialize the GetBitContext with the start of valid AC3 Frame.
1926 init_get_bits(&(ctx->gb), buf + frame_start, (buf_size - frame_start) * 8);
00585845 1927
1b293437 1928 //Parse the syncinfo.
00585845
JR
1929 //If 'fscod' or 'bsid' is not valid the decoder shall mute as per the standard.
1930 if (!ac3_parse_sync_info(ctx)) {
1931 av_log(avctx, AV_LOG_ERROR, "\n");
1b293437 1932 *data_size = 0;
486637af 1933 return buf_size;
1b293437 1934 }
2aa2c5c4 1935
1b293437
JR
1936 //Parse the BSI.
1937 //If 'bsid' is not valid decoder shall not decode the audio as per the standard.
486637af 1938 ac3_parse_bsi(ctx);
2aa2c5c4 1939
98a27a8a
JR
1940 avctx->sample_rate = ctx->sampling_rate;
1941 avctx->bit_rate = ctx->bit_rate;
45b0ed13 1942
1b293437 1943 if (avctx->channels == 0) {
98a27a8a 1944 ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
45b0ed13
JR
1945 if (ctx->lfeon)
1946 ctx->blkoutput |= AC3_OUTPUT_LFEON;
1947 avctx->channels = ctx->nfchans + ctx->lfeon;
1948 }
1949 else if (avctx->channels == 1)
98a27a8a 1950 ctx->blkoutput |= AC3_OUTPUT_MONO;
45b0ed13 1951 else if (avctx->channels == 2) {
98a27a8a
JR
1952 if (ctx->dsurmod == 0x02)
1953 ctx->blkoutput |= AC3_OUTPUT_DOLBY;
1b293437 1954 else
98a27a8a 1955 ctx->blkoutput |= AC3_OUTPUT_STEREO;
486637af 1956 }
45b0ed13
JR
1957 else {
1958 if (avctx->channels < (ctx->nfchans + ctx->lfeon))
1959 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);
1960 ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1961 if (ctx->lfeon)
1962 ctx->blkoutput |= AC3_OUTPUT_LFEON;
1963 avctx->channels = ctx->nfchans + ctx->lfeon;
1b293437
JR
1964 }
1965
d7bcc4ad 1966 //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
1967
1968 //Parse the Audio Blocks.
486637af 1969 for (i = 0; i < AUDIO_BLOCKS; i++) {
98a27a8a 1970 if (ac3_parse_audio_block(ctx)) {
1b293437
JR
1971 av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1972 *data_size = 0;
98a27a8a 1973 return ctx->frame_size;
1b293437 1974 }
d7bcc4ad
JR
1975 start = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1;
1976 for (k = 0; k < BLOCK_SIZE; k++)
1977 for (j = start; j <= avctx->channels; j++)
1978 *(out_samples++) = convert(int_ptr[j][k]);
1b293437 1979 }
486637af 1980 *data_size = AUDIO_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);
98a27a8a 1981 return ctx->frame_size;
2aa2c5c4 1982}
1b293437
JR
1983
1984static int ac3_decode_end(AVCodecContext *ctx)
1985{
1986 return 0;
1987}
1988
1989AVCodec lgpl_ac3_decoder = {
e6bca37c
JR
1990 .name = "ac3",
1991 .type = CODEC_TYPE_AUDIO,
1992 .id = CODEC_ID_AC3,
1993 .priv_data_size = sizeof (AC3DecodeContext),
1994 .init = ac3_decode_init,
1995 .close = ac3_decode_end,
1996 .decode = ac3_decode_frame,
1b293437
JR
1997};
1998