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