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