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