3 * Copyright (c) 2006 Kartikey Mahendra BHATT (bhattkm at gmail dot com).
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.
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.
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
26 #define ALT_BITSTREAM_READER
30 #include "ac3_decoder.h"
32 #include "bitstream.h"
37 /* Synchronization information. */
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
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)
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
63 /* Bit Stream Information. */
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
86 /* Dervied Attributes */
87 int nfchans; //number of full bandwidth channels - derived from acmod
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
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
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
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
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
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
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
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
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
206 #define AC3_OUTPUT_UNMODIFIED 0x00
207 #define AC3_OUTPUT_MONO 0x01
208 #define AC3_OUTPUT_STEREO 0x02
209 #define AC3_OUTPUT_DOLBY 0x03
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
220 /* BEGIN Mersenne Twister Code. */
223 #define MATRIX_A 0x9908b0df
224 #define UPPER_MASK 0x80000000
225 #define LOWER_MASK 0x7fffffff
232 static void dither_seed(dither_state *state, uint32_t seed)
238 for (state->mti = 1; state->mti < N; state->mti++)
239 state->mt[state->mti] = ((69069 * state->mt[state->mti - 1]) + 1);
242 static uint32_t dither_uint32(dither_state *state)
245 static const uint32_t mag01[2] = { 0x00, MATRIX_A };
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];
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];
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];
263 y = state->mt[state->mti++];
265 y ^= ((y << 7) & 0x9d2c5680);
266 y ^= ((y << 15) & 0xefc60000);
272 static inline int16_t dither_int16(dither_state *state)
274 return ((dither_uint32(state) << 16) >> 16);
277 /* END Mersenne Twister */
281 ac3_sync_info sync_info;
283 ac3_audio_block audio_block;
287 MDCTContext imdct_ctx_256;
288 MDCTContext imdct_ctx_512;
293 static int ac3_decode_init(AVCodecContext *avctx)
295 AC3DecodeContext *ctx = avctx->priv_data;
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));
303 av_log(avctx, AV_LOG_ERROR, "Cannot allocate memory for samples\n");
306 dither_seed(&ctx->state, 0);
311 static int ac3_synchronize(uint8_t *buf, int buf_size)
315 for (i = 0; i < buf_size - 1; i++)
316 if (buf[i] == 0x0b && buf[i + 1] == 0x77)
322 //Returns -1 when 'fscod' is not valid;
323 static int ac3_parse_sync_info(AC3DecodeContext *ctx)
325 ac3_sync_info *sync_info = &ctx->sync_info;
326 GetBitContext *gb = &ctx->gb;
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)
333 sync_info->frmsizecod = get_bits(gb, 6);
334 if (sync_info->frmsizecod >= 0x38)
336 sync_info->sampling_rate = ac3_freqs[sync_info->fscod];
337 sync_info->bit_rate = ac3_bitratetab[sync_info->frmsizecod >> 1];
343 static int ac3_parse_bsi(AC3DecodeContext *ctx)
345 ac3_bsi *bsi = &ctx->bsi;
346 uint32_t *flags = &bsi->flags;
347 GetBitContext *gb = &ctx->gb;
354 bsi->bsid = get_bits(gb, 5);
355 if (bsi->bsid > 0x08)
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);
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);
372 if (get_bits(gb, 1)) {
373 *flags |= AC3_BSI_LANGCODE;
374 bsi->langcod = get_bits(gb, 8);
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);
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);
387 if (get_bits(gb, 1)) {
388 *flags |= AC3_BSI_LANGCOD2E;
389 bsi->langcod2 = get_bits(gb, 8);
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);
398 *flags |= AC3_BSI_COPYRIGHTB;
400 *flags |= AC3_BSI_ORIGBS;
401 if (get_bits(gb, 1)) {
402 *flags |= AC3_BSI_TIMECOD1E;
403 bsi->timecod1 = get_bits(gb, 14);
405 if (get_bits(gb, 1)) {
406 *flags |= AC3_BSI_TIMECOD2E;
407 bsi->timecod2 = get_bits(gb, 14);
409 if (get_bits(gb, 1)) {
410 *flags |= AC3_BSI_ADDBSIE;
411 bsi->addbsil = get_bits(gb, 6);
414 } while (bsi->addbsil--);
417 bsi->nfchans = nfchans_tbl[bsi->acmod];
422 /* Decodes the grouped exponents (gexps) and stores them
423 * in decoded exponents (dexps).
424 * The code is derived from liba52.
425 * Uses liba52 tables.
427 static int _decode_exponents(int expstr, int ngrps, uint8_t absexp, uint8_t *gexps, uint8_t *dexps)
435 absexp += exp_1[exps];
436 assert(absexp <= 24);
446 absexp += exp_2[exps];
447 assert(absexp <= 24);
458 absexp += exp_3[exps];
459 assert(absexp <= 24);
474 static int decode_exponents(AC3DecodeContext *ctx)
476 ac3_audio_block *ab = &ctx->audio_block;
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))
485 for (i = 0; i < ctx->bsi.nfchans; i++)
486 if (ab->chexpstr[i] != AC3_EXPSTR_REUSE) {
488 dexps = ab->dexps[i];
489 if (_decode_exponents(ab->chexpstr[i], ab->nchgrps[i], exps[0], exps + 1, dexps + 1))
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))
498 static inline int16_t logadd(int16_t a, int16_t b)
501 uint8_t address = FFMIN((ABS(c) >> 1), 255);
503 return ((c >= 0) ? (a + latab[address]) : (b + latab[address]));
506 static inline int16_t calc_lowcomp(int16_t a, int16_t b0, int16_t b1, uint8_t bin)
509 if ((b0 + 256) == b1)
512 a = FFMAX(0, a - 64);
515 if ((b0 + 256) == b1)
518 a = FFMAX(0, a - 64);
521 a = FFMAX(0, a - 128);
527 /* do the bit allocation for chnl.
528 * chnl = 0 to 4 - fbw channel
529 * chnl = 5 coupling channel
530 * chnl = 6 lfe channel
532 static int _do_bit_allocation(AC3DecodeContext *ctx, int chnl)
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;
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];
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;
560 if (ab->cpldeltbae == 0 || ab->cpldeltbae == 1) {
562 deltnseg = ab->cpldeltnseg;
563 deltoffst = ab->cpldeltoffst;
564 deltlen = ab->cpldeltlen;
565 deltba = ab->cpldeltba;
568 else if (chnl == 6) {
572 fgain = fgaintab[ab->lfefgaincod];
573 snroffset = (((ab->csnroffst - 15) << 4) + ab->lfefsnroffst) << 2;
579 end = ab->endmant[chnl];
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) {
587 deltnseg = ab->deltnseg[chnl];
588 deltoffst = ab->deltoffst[chnl];
589 deltlen = ab->deltlen[chnl];
590 deltba = ab->deltba[chnl];
594 for (bin = start; bin < end; bin++) /* exponent mapping into psd */
595 psd[bin] = (3072 - ((int16_t) (exps[bin] << 7)));
597 /* psd integration */
601 lastbin = FFMIN(bndtab[k] + bndsz[k], end);
604 for (i = j; i < lastbin; i++) {
605 bndpsd[k] = logadd(bndpsd[k], psd[j]);
609 } while (end > lastbin);
611 /* compute the excite function */
612 bndstrt = masktab[start];
613 bndend = masktab[end - 1] + 1;
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;
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]) {
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);
636 fastleak = FFMAX(fastleak, bndpsd[bin] - fgain);
638 slowleak = FFMAX(slowleak, bndpsd[bin] - sgain);
639 excite[bin] = FFMAX(fastleak - lowcomp, slowleak);
646 for (bin = begin; bin < bndend; bin++) {
648 fastleak = FFMAX(fastleak, bndpsd[bin] - fgain);
650 slowleak = FFMAX(slowleak, bndpsd[bin] - sgain);
651 excite[bin] = FFMAX(fastleak, slowleak);
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]);
661 /* apply the delta bit allocation */
664 for (seg = 0; seg < deltnseg + 1; seg++) {
665 band += deltoffst[seg];
666 if (deltba[seg] >= 4)
667 delta = (deltba[seg] - 3) << 7;
669 delta = (deltba[seg] - 4) << 7;
670 for (k = 0; k < deltlen[seg]; k++) {
677 /*compute the bit allocation */
681 lastbin = FFMIN(bndtab[j] + bndsz[j], end);
682 mask[j] -= snroffset;
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];
695 } while (end > lastbin);
700 static int do_bit_allocation(AC3DecodeContext *ctx, int flags)
702 ac3_audio_block *ab = &ctx->audio_block;
705 if (!flags) /* bit allocation is not required */
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;
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));
726 /* perform bit allocation */
727 if ((ab->flags & AC3_AB_CPLINU) && (flags & 64))
728 if (_do_bit_allocation(ctx, 5))
730 for (i = 0; i < ctx->bsi.nfchans; i++)
731 if (flags & (1 << i))
732 if (_do_bit_allocation(ctx, i))
734 if ((ctx->bsi.flags & AC3_BSI_LFEON) && (flags & 32))
735 if (_do_bit_allocation(ctx, 6))
741 static inline float to_float(uint8_t exp, int16_t mantissa)
743 return ((float) (mantissa * scale_factors[exp]));
746 typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
751 /* Get the transform coefficients for particular channel */
752 static int _get_transform_coeffs(uint8_t *exps, uint8_t *bap, float chcoeff,
753 float *samples, int start, int end, int dith_flag, GetBitContext *gb,
759 mant_group l3_grp, l5_grp, l11_grp;
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;
770 for (i = start; i < end; i++) {
776 mantissa = dither_int16(state);
777 samples[i] = to_float(exps[i], mantissa) * chcoeff;
781 if (l3_grp.gcptr > 2) {
782 gcode = get_bits(gb, qntztab[1]);
785 l3_grp.gcodes[0] = gcode / 9;
786 l3_grp.gcodes[1] = (gcode % 9) / 3;
787 l3_grp.gcodes[2] = (gcode % 9) % 3;
790 mantissa = l3_q_tab[l3_grp.gcodes[l3_grp.gcptr++]];
791 samples[i] = to_float(exps[i], mantissa) * chcoeff;
795 if (l5_grp.gcptr > 2) {
796 gcode = get_bits(gb, qntztab[2]);
799 l5_grp.gcodes[0] = gcode / 25;
800 l5_grp.gcodes[1] = (gcode % 25) / 5;
801 l5_grp.gcodes[2] = (gcode % 25) % 5;
804 mantissa = l5_q_tab[l5_grp.gcodes[l5_grp.gcptr++]];
805 samples[i] = to_float(exps[i], mantissa) * chcoeff;
809 mantissa = get_bits(gb, qntztab[3]);
812 mantissa = l7_q_tab[mantissa];
813 samples[i] = to_float(exps[i], mantissa);
817 if (l11_grp.gcptr > 1) {
818 gcode = get_bits(gb, qntztab[4]);
821 l11_grp.gcodes[0] = gcode / 11;
822 l11_grp.gcodes[1] = gcode % 11;
824 mantissa = l11_q_tab[l11_grp.gcodes[l11_grp.gcptr++]];
825 samples[i] = to_float(exps[i], mantissa) * chcoeff;
829 mantissa = get_bits(gb, qntztab[5]);
832 mantissa = l15_q_tab[mantissa];
833 samples[i] = to_float(exps[i], mantissa) * chcoeff;
837 mantissa = get_bits(gb, qntztab[bap[i]]) << (16 - qntztab[bap[i]]);
838 samples[i] = to_float(exps[i], mantissa) * chcoeff;
850 static int uncouple_channels(AC3DecodeContext * ctx)
852 ac3_audio_block *ab = &ctx->audio_block;
855 float (*samples)[256];
858 samples = (float (*)[256])((ctx->bsi.flags & AC3_BSI_LFEON) ? (ctx->samples + 256) : (ctx->samples));
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];
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];
881 static int get_transform_coeffs(AC3DecodeContext * ctx)
884 ac3_audio_block *ab = &ctx->audio_block;
885 float *samples = ctx->samples;
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 */
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))
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))
907 if (ctx->bsi.flags & AC3_BSI_LFEON)
908 if (_get_transform_coeffs(ab->lfeexps, ab->lfebap, 1.0f, samples - 256, 0, 7, 0, &ctx->gb, &ctx->state))
911 /* uncouple the channels from the coupling channel */
912 if (ab->flags & AC3_AB_CPLINU)
913 if (uncouple_channels(ctx))
919 /* generate coupling co-ordinates for each coupling subband
920 * from coupling co-ordinates of each band and coupling band
921 * structure information
923 static int generate_coupling_coordinates(AC3DecodeContext * ctx)
925 ac3_audio_block *ab = &ctx->audio_block;
926 uint8_t exp, mstrcplco;
928 uint32_t cplbndstrc = (1 << ab->ncplsubnd) >> 1;
933 for (ch = 0; ch < ctx->bsi.nfchans; ch++)
934 if (ab->cplcoe & (1 << ch)) {
935 mstrcplco = 3 * ab->mstrcplco[ch];
937 for (bnd = 0; bnd < ab->ncplbnd; bnd++) {
938 exp = ab->cplcoexp[ch][bnd];
940 mant = ab->cplcomant[ch][bnd] <<= 14;
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) {
950 ab->cplco[ch][sbnd++] = cplco;
959 static int _do_rematrixing(AC3DecodeContext *ctx, int start, int end)
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;
974 static void do_rematrixing(AC3DecodeContext *ctx)
976 ac3_audio_block *ab = &ctx->audio_block;
977 uint8_t bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
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);
989 _do_rematrixing(ctx, bnd3, bnd4);
990 if (ab->rematflg & 8)
991 _do_rematrixing(ctx, bnd4, bndend);
996 static void get_downmix_coeffs(AC3DecodeContext *ctx)
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;
1004 if (to == AC3_OUTPUT_UNMODIFIED)
1008 case AC3_INPUT_DUALMONO:
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;
1017 case AC3_INPUT_MONO:
1019 case AC3_OUTPUT_STEREO:
1020 ab->chcoeffs[0] *= LEVEL_MINUS_3DB;
1024 case AC3_INPUT_STEREO:
1026 case AC3_OUTPUT_MONO:
1027 ab->chcoeffs[0] *= LEVEL_MINUS_3DB;
1028 ab->chcoeffs[1] *= LEVEL_MINUS_3DB;
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;
1039 case AC3_OUTPUT_STEREO:
1040 ab->chcoeffs[1] *= clev;
1044 case AC3_INPUT_2F_1R:
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;
1051 case AC3_OUTPUT_STEREO:
1052 ab->chcoeffs[2] *= slev * LEVEL_MINUS_3DB;
1054 case AC3_OUTPUT_DOLBY:
1055 ab->chcoeffs[2] *= LEVEL_MINUS_3DB;
1059 case AC3_INPUT_3F_1R:
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;
1067 case AC3_OUTPUT_STEREO:
1068 ab->chcoeffs[1] *= clev;
1069 ab->chcoeffs[3] *= slev * LEVEL_MINUS_3DB;
1071 case AC3_OUTPUT_DOLBY:
1072 ab->chcoeffs[1] *= LEVEL_MINUS_3DB;
1073 ab->chcoeffs[3] *= LEVEL_MINUS_3DB;
1077 case AC3_INPUT_2F_2R:
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;
1085 case AC3_OUTPUT_STEREO:
1086 ab->chcoeffs[2] *= slev;
1087 ab->chcoeffs[3] *= slev;
1089 case AC3_OUTPUT_DOLBY:
1090 ab->chcoeffs[2] *= LEVEL_MINUS_3DB;
1091 ab->chcoeffs[3] *= LEVEL_MINUS_3DB;
1095 case AC3_INPUT_3F_2R:
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;
1104 case AC3_OUTPUT_STEREO:
1105 ab->chcoeffs[1] *= clev;
1106 ab->chcoeffs[3] *= slev;
1107 ab->chcoeffs[4] *= slev;
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;
1119 static inline void downmix_dualmono_to_mono(float *samples)
1123 for (i = 0; i < 256; i++) {
1124 samples[i] += samples[i + 256];
1125 samples[i + 256] = 0;
1129 static inline void downmix_dualmono_to_stereo(float *samples)
1134 for (i = 0; i < 256; i++) {
1135 tmp = samples[i] + samples[i + 256];
1136 samples[i] = samples[i + 256] = tmp;
1140 static inline void downmix_mono_to_stereo(float *samples)
1144 for (i = 0; i < 256; i++)
1145 samples[i + 256] = samples[i];
1148 static inline void downmix_stereo_to_mono(float *samples)
1152 for (i = 0; i < 256; i++) {
1153 samples[i] += samples[i + 256];
1154 samples[i + 256] = 0;
1158 static inline void downmix_3f_to_mono(float *samples)
1162 for (i = 0; i < 256; i++) {
1163 samples[i] += (samples[i + 256] + samples[i + 512]);
1164 samples[i + 256] = samples[i + 512] = 0;
1168 static inline void downmix_3f_to_stereo(float *samples)
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;
1179 static inline void downmix_2f_1r_to_mono(float *samples)
1183 for (i = 0; i < 256; i++) {
1184 samples[i] += (samples[i + 256] + samples[i + 512]);
1185 samples[i + 256] = samples[i + 512] = 0;
1189 static inline void downmix_2f_1r_to_stereo(float *samples)
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;
1200 static inline void downmix_2f_1r_to_dolby(float *samples)
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;
1211 static inline void downmix_3f_1r_to_mono(float *samples)
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;
1221 static inline void downmix_3f_1r_to_stereo(float *samples)
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;
1232 static inline void downmix_3f_1r_to_dolby(float *samples)
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;
1243 static inline void downmix_2f_2r_to_mono(float *samples)
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;
1253 static inline void downmix_2f_2r_to_stereo(float *samples)
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;
1264 static inline void downmix_2f_2r_to_dolby(float *samples)
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;
1275 static inline void downmix_3f_2r_to_mono(float *samples)
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;
1285 static inline void downmix_3f_2r_to_stereo(float *samples)
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;
1296 static inline void downmix_3f_2r_to_dolby(float *samples)
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;
1307 static void do_downmix(AC3DecodeContext *ctx)
1309 int from = ctx->bsi.acmod;
1310 int to = ctx->output;
1311 float *samples = ctx->samples + ((ctx->bsi.flags & AC3_BSI_LFEON) ? 256 : 0);
1314 case AC3_INPUT_DUALMONO:
1316 case AC3_OUTPUT_MONO:
1317 downmix_dualmono_to_mono(samples);
1319 case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */
1320 downmix_dualmono_to_stereo(samples);
1324 case AC3_INPUT_MONO:
1326 case AC3_OUTPUT_STEREO:
1327 downmix_mono_to_stereo(samples);
1331 case AC3_INPUT_STEREO:
1333 case AC3_OUTPUT_MONO:
1334 downmix_stereo_to_mono(samples);
1340 case AC3_OUTPUT_MONO:
1341 downmix_3f_to_mono(samples);
1343 case AC3_OUTPUT_STEREO:
1344 downmix_3f_to_stereo(samples);
1348 case AC3_INPUT_2F_1R:
1350 case AC3_OUTPUT_MONO:
1351 downmix_2f_1r_to_mono(samples);
1353 case AC3_OUTPUT_STEREO:
1354 downmix_2f_1r_to_stereo(samples);
1356 case AC3_OUTPUT_DOLBY:
1357 downmix_2f_1r_to_dolby(samples);
1361 case AC3_INPUT_3F_1R:
1363 case AC3_OUTPUT_MONO:
1364 downmix_3f_1r_to_mono(samples);
1366 case AC3_OUTPUT_STEREO:
1367 downmix_3f_1r_to_stereo(samples);
1369 case AC3_OUTPUT_DOLBY:
1370 downmix_3f_1r_to_dolby(samples);
1374 case AC3_INPUT_2F_2R:
1376 case AC3_OUTPUT_MONO:
1377 downmix_2f_2r_to_mono(samples);
1379 case AC3_OUTPUT_STEREO:
1380 downmix_2f_2r_to_stereo(samples);
1382 case AC3_OUTPUT_DOLBY:
1383 downmix_2f_2r_to_dolby(samples);
1387 case AC3_INPUT_3F_2R:
1389 case AC3_OUTPUT_MONO:
1390 downmix_3f_2r_to_mono(samples);
1392 case AC3_OUTPUT_STEREO:
1393 downmix_3f_2r_to_stereo(samples);
1395 case AC3_OUTPUT_DOLBY:
1396 downmix_3f_2r_to_dolby(samples);
1403 static int ac3_parse_audio_block(AC3DecodeContext * ctx, int index)
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;
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;
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;
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;
1438 get_downmix_coeffs(ctx);
1440 if (get_bits(gb, 1)) { /* coupling strategy */
1441 *flags |= AC3_AB_CPLSTRE;
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;
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;
1461 if (*flags & AC3_AB_CPLINU) {
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);
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;
1480 generate_coupling_coordinates(ctx);
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;
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);
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);
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;
1523 ab->endmant[i] = ((ab->chbwcod[i] + 3) * 12) + 37;
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);
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);
1538 if (decode_exponents(ctx)) {/* decode the exponents for this block */
1539 av_log(NULL, AV_LOG_ERROR, "Error parsing exponents\n");
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);
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);
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);
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);
1569 if (*flags & AC3_AB_CPLINU)
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);
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");
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");
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);
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);
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");
1616 if (get_bits(gb, 1)) { /* unused dummy data */
1617 *flags |= AC3_AB_SKIPLE;
1618 ab->skipl = get_bits(gb, 9);
1624 /* unpack the transform coefficients
1625 * * this also uncouples channels if coupling is in use.
1627 if (get_transform_coeffs(ctx)) {
1628 av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1631 /* recover coefficients if rematrixing is in use */
1632 if (*flags & AC3_AB_REMATSTR)
1633 do_rematrixing(ctx);
1635 if (ctx->output != AC3_OUTPUT_UNMODIFIED)
1641 /**** the following two functions comes from ac3dec */
1642 static inline int blah (int32_t i)
1646 else if (i < 0x43bf8000)
1649 return i - 0x43c00000;
1652 static inline void float_to_int (float * _f, int16_t * s16, int samples)
1654 int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
1657 for (i = 0; i < samples; i++) {
1658 s16[i] = blah (f[i]);
1665 static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t * buf, int buf_size)
1667 AC3DecodeContext *ctx = avctx->priv_data;
1670 float tmp0[128], tmp1[128], tmp[512];
1671 short *out_samples = (short *)data;
1672 float *samples = ctx->samples;
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");
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");
1692 //Check for the errors.
1693 /* if (ac3_error_check(ctx)) {
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");
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;
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;
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;
1723 ctx->output = AC3_OUTPUT_STEREO;
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);
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");
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);
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];
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);
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));
1771 *data_size = 6 * ctx->bsi.nfchans * 256 * sizeof (int16_t);
1773 return (buf_size - frame_start);
1776 static int ac3_decode_end(AVCodecContext *ctx)
1781 AVCodec lgpl_ac3_decoder = {
1785 sizeof (AC3DecodeContext),