2 * This code is developed as part of Google Summer of Code 2006 Program.
6 * I would like to acknowledge my mentor Benjamin Larsson for his timely
7 * help and excelleng guidance throughout the project.
8 * Thanks a lot Benjamin.
10 * For exponent decoding the code is inspired by the code in liba52 by
11 * Michel Lespinasse and Aaron Holtzman.
12 * http://liba52.sourceforge.net
14 * Thanks Makoto Matsumoto and Takuji Nishimura for the Mersenne Twister.
16 * Copyright (c) 2006 Kartikey Mahendra BHATT (bhattkm at gmail dot com).
17 * Something is wrong up on cloud # 9!
19 * This library is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU Lesser General Public
21 * License as published by the Free Software Foundation; either
22 * version 2 of the License, or (at your option) any later version.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
39 #define ALT_BITSTREAM_READER
43 #include "bitstream.h"
46 static const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 };
48 /* table for exponent to scale_factor mapping
49 * scale_factor[i] = 2 ^ -(i + 15)
51 static float scale_factors[25];
53 static int16_t psdtab[25];
55 static int8_t exp_1[128];
56 static int8_t exp_2[128];
57 static int8_t exp_3[128];
59 static int16_t l3_quantizers_1[32];
60 static int16_t l3_quantizers_2[32];
61 static int16_t l3_quantizers_3[32];
63 static int16_t l5_quantizers_1[128];
64 static int16_t l5_quantizers_2[128];
65 static int16_t l5_quantizers_3[128];
67 static int16_t l7_quantizers[7];
69 static int16_t l11_quantizers_1[128];
70 static int16_t l11_quantizers_2[128];
72 static int16_t l15_quantizers[15];
74 static const uint8_t qntztab[16] = { 0, 5, 7, 3, 7, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16 };
76 /* Adjustmens in dB gain */
77 #define LEVEL_MINUS_3DB 0.7071067811865476
78 #define LEVEL_MINUS_4POINT5DB 0.5946035575013605
79 #define LEVEL_MINUS_6DB 0.5000000000000000
80 #define LEVEL_PLUS_3DB 1.4142135623730951
81 #define LEVEL_PLUS_6DB 2.0000000000000000
82 #define LEVEL_ZERO 0.0000000000000000
84 static const float clevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB,
85 LEVEL_MINUS_6DB, LEVEL_MINUS_4POINT5DB };
87 static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB };
89 #define N 512 /* constant for IMDCT Block size */
91 #define MAX_CHANNELS 6
92 #define BLOCK_SIZE 256
93 #define AUDIO_BLOCKS 6
95 /* Exponent strategies. */
96 #define AC3_EXPSTR_D15 0x01
97 #define AC3_EXPSTR_D25 0x02
98 #define AC3_EXPSTR_D45 0x03
99 #define AC3_EXPSTR_REUSE 0x00
101 /* Bit allocation strategies. */
102 #define AC3_DBASTR_NEW 0x01
103 #define AC3_DBASTR_NONE 0x02
104 #define AC3_DBASTR_RESERVED 0x03
105 #define AC3_DBASTR_REUSE 0x00
107 /* Output and input configurations. */
108 #define AC3_OUTPUT_UNMODIFIED 0x01
109 #define AC3_OUTPUT_MONO 0x02
110 #define AC3_OUTPUT_STEREO 0x04
111 #define AC3_OUTPUT_DOLBY 0x08
112 #define AC3_OUTPUT_LFEON 0x10
114 #define AC3_INPUT_DUALMONO 0x00
115 #define AC3_INPUT_MONO 0x01
116 #define AC3_INPUT_STEREO 0x02
117 #define AC3_INPUT_3F 0x03
118 #define AC3_INPUT_2F_1R 0x04
119 #define AC3_INPUT_3F_1R 0x05
120 #define AC3_INPUT_2F_2R 0x06
121 #define AC3_INPUT_3F_2R 0x07
123 /* Mersenne Twister */
126 #define MATRIX_A 0x9908b0df
127 #define UPPER_MASK 0x80000000
128 #define LOWER_MASK 0x7fffffff
135 /* Mersenne Twister */
166 uint8_t cplfsnroffst;
168 uint8_t fsnroffst[5];
170 uint8_t lfefsnroffst;
177 uint8_t cpldeltoffst[8];
178 uint8_t cpldeltlen[8];
179 uint8_t cpldeltba[8];
181 uint8_t deltoffst[5][8];
182 uint8_t deltlen[5][8];
183 uint8_t deltba[5][8];
185 /* Derived Attributes. */
190 int nfchans; //number of channels
191 int lfeon; //lfe channel in use
193 float dynrng; //dynamic range gain
194 float dynrng2; //dynamic range gain for 1+1 mode
195 float chcoeffs[6]; //normalized channel coefficients
196 float cplco[5][18]; //coupling coordinates
197 int ncplbnd; //number of coupling bands
198 int ncplsubnd; //number of coupling sub bands
199 int cplstrtmant; //coupling start mantissa
200 int cplendmant; //coupling end mantissa
201 int endmant[5]; //channel end mantissas
203 uint8_t dcplexps[256]; //decoded coupling exponents
204 uint8_t dexps[5][256]; //decoded fbw channel exponents
205 uint8_t dlfeexps[256]; //decoded lfe channel exponents
206 uint8_t cplbap[256]; //coupling bit allocation pointers
207 uint8_t bap[5][256]; //fbw channel bit allocation pointers
208 uint8_t lfebap[256]; //lfe channel bit allocation pointers
210 int blkoutput; //output configuration for block
212 DECLARE_ALIGNED_16(float, transform_coeffs[MAX_CHANNELS][BLOCK_SIZE]); //transform coefficients
215 MDCTContext imdct_512; //for 512 sample imdct transform
216 MDCTContext imdct_256; //for 256 sample imdct transform
217 DSPContext dsp; //for optimization
219 DECLARE_ALIGNED_16(float, output[MAX_CHANNELS][BLOCK_SIZE]); //output after imdct transform and windowing
220 DECLARE_ALIGNED_16(float, delay[MAX_CHANNELS][BLOCK_SIZE]); //delay - added to the next block
221 DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]); //temporary storage for imdct transform
222 DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]); //temporary storage for output before windowing
223 DECLARE_ALIGNED_16(float, window[BLOCK_SIZE]); //window coefficients
227 dither_state dith_state; //for dither generation
231 /* BEGIN Mersenne Twister Code. */
232 static void dither_seed(dither_state *state, uint32_t seed)
234 static const uint32_t mag01[2] = { 0x00, MATRIX_A };
239 seed = 0x7ba05e; //default seed to my birthday!
242 for (state->mti = 1; state->mti < NMT; state->mti++)
243 state->mt[state->mti] = ((69069 * state->mt[state->mti - 1]) + 1);
245 for (kk = 0; kk < NMT - MMT; kk++) {
246 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
247 state->mt[kk] = state->mt[kk + MMT] ^ (y >> 1) ^ mag01[y & 0x01];
249 for (;kk < NMT - 1; kk++) {
250 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
251 state->mt[kk] = state->mt[kk + (MMT - NMT)] ^ (y >> 1) ^ mag01[y & 0x01];
253 y = (state->mt[NMT - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK);
254 state->mt[NMT - 1] = state->mt[MMT - 1] ^ (y >> 1) ^ mag01[y & 0x01];
259 static int16_t dither_int16(dither_state *state)
263 if (state->mti >= NMT)
266 y = state->mt[state->mti++];
268 y ^= ((y << 7) & 0x9d2c5680);
269 y ^= ((y << 15) & 0xefc60000);
272 return ((y << 16) >> 16);
274 /* END Mersenne Twister */
276 /*********** BEGIN INIT HELPER FUNCTIONS ***********/
278 * Generate a Kaiser-Bessel Derived Window.
280 static void ac3_window_init(float *window)
283 double sum = 0.0, bessel, tmp;
284 double local_window[256];
285 double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
287 for (i = 0; i < 256; i++) {
288 tmp = i * (256 - i) * alpha2;
290 for (j = 100; j > 0; j--) /* defaul to 100 iterations */
291 bessel = bessel * tmp / (j * j) + 1;
293 local_window[i] = sum;
297 for (i = 0; i < 256; i++)
298 window[i] = sqrt(local_window[i] / sum);
302 * Generate quantizer tables.
304 static void generate_quantizers_table(int16_t quantizers[], int level, int length)
308 for (i = 0; i < length; i++)
309 quantizers[i] = ((2 * i - level + 1) << 15) / level;
312 static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
317 for (i = 0; i < length1; i++) {
318 v = ((2 * i - level + 1) << 15) / level;
319 for (j = 0; j < length2; j++)
320 quantizers[i * length2 + j] = v;
323 for (i = length1 * length2; i < size; i++)
327 static void generate_quantizers_table_2(int16_t quantizers[], int level, int length1, int length2, int size)
332 for (i = 0; i < length1; i++) {
333 v = ((2 * (i % level) - level + 1) << 15) / level;
334 for (j = 0; j < length2; j++)
335 quantizers[i * length2 + j] = v;
338 for (i = length1 * length2; i < size; i++)
343 static void generate_quantizers_table_3(int16_t quantizers[], int level, int length1, int length2, int size)
347 for (i = 0; i < length1; i++)
348 for (j = 0; j < length2; j++)
349 quantizers[i * length2 + j] = ((2 * (j % level) - level + 1) << 15) / level;
351 for (i = length1 * length2; i < size; i++)
356 * Initialize tables at runtime.
358 static void ac3_tables_init(void)
361 /* compute bndtab and masktab from bandsz */
367 for(j=0;j<v;j++) masktab[k++]=i;
370 masktab[253] = masktab[254] = masktab[255] = 0;
373 /* PSD Table For Mapping Exponents To PSD. */
374 for (i = 0; i < 25; i++)
375 psdtab[i] = 3072 - (i << 7);
377 /* Exponent Decoding Tables */
378 for (i = 0; i < 5; i++) {
380 for (j = 0; j < 25; j++)
381 exp_1[i * 25 + j] = v;
384 for (i = 0; i < 25; i++) {
386 for (j = 0; j < 5; j++)
387 exp_2[i * 5 + j] = v;
390 for (i = 0; i < 25; i++) {
392 for (j = 0; j < 5; j++)
393 exp_3[i * 5 + j] = v++;
396 for (i = 125; i < 128; i++)
397 exp_1[i] = exp_2[i] = exp_3[i] = 25;
398 /* End Exponent Decoding Tables */
400 /* Quantizer ungrouping tables. */
401 // for level-3 quantizers
402 generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
403 generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
404 generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
406 //for level-5 quantizers
407 generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
408 generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
409 generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
411 //for level-7 quantizers
412 generate_quantizers_table(l7_quantizers, 7, 7);
414 //for level-4 quantizers
415 generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
416 generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
418 //for level-15 quantizers
419 generate_quantizers_table(l15_quantizers, 15, 15);
420 /* End Quantizer ungrouping tables. */
422 //generate scale factors
423 for (i = 0; i < 25; i++)
424 scale_factors[i] = pow(2.0, -(i + 15));
428 static int ac3_decode_init(AVCodecContext *avctx)
430 AC3DecodeContext *ctx = avctx->priv_data;
433 ff_mdct_init(&ctx->imdct_256, 8, 1);
434 ff_mdct_init(&ctx->imdct_512, 9, 1);
435 ac3_window_init(ctx->window);
436 dsputil_init(&ctx->dsp, avctx);
437 dither_seed(&ctx->dith_state, 0);
441 /*********** END INIT FUNCTIONS ***********/
443 /* Synchronize to ac3 bitstream.
444 * This function searches for the syncword '0xb77'.
446 * @param buf Pointer to "probable" ac3 bitstream buffer
447 * @param buf_size Size of buffer
448 * @return Returns the position where syncword is found, -1 if no syncword is found
450 static int ac3_synchronize(uint8_t *buf, int buf_size)
454 for (i = 0; i < buf_size - 1; i++)
455 if (buf[i] == 0x0b && buf[i + 1] == 0x77)
461 /* Parse the 'sync_info' from the ac3 bitstream.
462 * This function extracts the sync_info from ac3 bitstream.
463 * GetBitContext within AC3DecodeContext must point to
464 * start of the synchronized ac3 bitstream.
466 * @param ctx AC3DecodeContext
467 * @return Returns framesize, returns 0 if fscod, frmsizecod or bsid is not valid
469 static int ac3_parse_sync_info(AC3DecodeContext *ctx)
471 GetBitContext *gb = &ctx->gb;
472 int frmsizecod, bsid;
474 skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
475 ctx->crc1 = get_bits(gb, 16);
476 ctx->fscod = get_bits(gb, 2);
477 if (ctx->fscod == 0x03)
479 frmsizecod = get_bits(gb, 6);
480 if (frmsizecod >= 38)
482 ctx->sampling_rate = ac3_freqs[ctx->fscod];
483 ctx->bit_rate = ac3_bitratetab[frmsizecod >> 1];
485 /* we include it here in order to determine validity of ac3 frame */
486 bsid = get_bits(gb, 5);
489 skip_bits(gb, 3); //skip the bsmod, bsi->bsmod = get_bits(gb, 3);
491 switch (ctx->fscod) {
493 ctx->frame_size = 4 * ctx->bit_rate;
494 return ctx->frame_size;
496 ctx->frame_size = 2 * (320 * ctx->bit_rate / 147 + (frmsizecod & 1));
497 return ctx->frame_size;
499 ctx->frame_size = 6 * ctx->bit_rate;
500 return ctx->frame_size;
507 /* Parse bsi from ac3 bitstream.
508 * This function extracts the bitstream information (bsi) from ac3 bitstream.
510 * @param ctx AC3DecodeContext after processed by ac3_parse_sync_info
512 static void ac3_parse_bsi(AC3DecodeContext *ctx)
514 GetBitContext *gb = &ctx->gb;
521 ctx->cpldeltbae = AC3_DBASTR_NONE;
522 ctx->cpldeltnseg = 0;
523 for (i = 0; i < 5; i++) {
524 ctx->deltbae[i] = AC3_DBASTR_NONE;
525 ctx->deltnseg[i] = 0;
530 ctx->acmod = get_bits(gb, 3);
531 ctx->nfchans = nfchans_tbl[ctx->acmod];
533 if (ctx->acmod & 0x01 && ctx->acmod != 0x01)
534 ctx->cmixlev = get_bits(gb, 2);
535 if (ctx->acmod & 0x04)
536 ctx->surmixlev = get_bits(gb, 2);
537 if (ctx->acmod == 0x02)
538 ctx->dsurmod = get_bits(gb, 2);
540 ctx->lfeon = get_bits1(gb);
544 skip_bits(gb, 5); //skip dialog normalization
546 skip_bits(gb, 8); //skip compression
548 skip_bits(gb, 8); //skip language code
550 skip_bits(gb, 7); //skip audio production information
553 skip_bits(gb, 2); //skip copyright bit and original bitstream bit
556 skip_bits(gb, 14); //skip timecode1
558 skip_bits(gb, 14); //skip timecode2
561 i = get_bits(gb, 6); //additional bsi length
568 /* Decodes the grouped exponents.
569 * This function decodes the coded exponents according to exponent strategy
570 * and stores them in the decoded exponents buffer.
572 * @param gb GetBitContext which points to start of coded exponents
573 * @param expstr Exponent coding strategy
574 * @param ngrps Number of grouped exponetns
575 * @param absexp Absolute exponent
576 * @param dexps Decoded exponents are stored in dexps
577 * @return Returns 0 if exponents are decoded successfully, -1 if error occurs
579 static int decode_exponents(GetBitContext *gb, int expstr, int ngrps, uint8_t absexp, uint8_t *dexps)
584 exps = get_bits(gb, 7);
586 absexp += exp_1[exps];
588 av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
601 absexp += exp_2[exps];
603 av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
616 absexp += exp_3[exps];
618 av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
635 /*********** HELPER FUNCTIONS FOR BIT ALLOCATION ***********/
636 static inline int logadd(int a, int b)
641 address = FFMIN((ABS(c) >> 1), 255);
644 return (a + latab[address]);
646 return (b + latab[address]);
649 static inline int calc_lowcomp(int a, int b0, int b1, int bin)
652 if ((b0 + 256) == b1)
655 a = FFMAX(0, (a - 64));
658 if ((b0 + 256) == b1)
661 a = FFMAX(0, (a - 64));
664 a = FFMAX(0, (a - 128));
668 /*********** END HELPER FUNCTIONS FOR BIT ALLOCATION ***********/
670 /* Performs bit allocation.
671 * This function performs bit allocation for the requested chanenl.
673 static void do_bit_allocation(AC3DecodeContext *ctx, int chnl)
675 int16_t psd[256], bndpsd[50], excite[50], mask[50], delta;
676 int sdecay, fdecay, sgain, dbknee, floor;
677 int lowcomp = 0, fgain = 0, snroffset = 0, fastleak = 0, slowleak = 0, do_delta = 0;
678 int start = 0, end = 0, bin = 0, i = 0, j = 0, k = 0, lastbin = 0, bndstrt = 0;
679 int bndend = 0, begin = 0, deltnseg = 0, band = 0, seg = 0, address = 0;
680 int fscod = ctx->fscod;
681 uint8_t *deltoffst = 0, *deltlen = 0, *deltba = 0;
682 uint8_t *exps = 0, *bap = 0;
685 sdecay = sdecaytab[ctx->sdcycod];
686 fdecay = fdecaytab[ctx->fdcycod];
687 sgain = sgaintab[ctx->sgaincod];
688 dbknee = dbkneetab[ctx->dbpbcod];
689 floor = floortab[ctx->floorcod];
692 start = ctx->cplstrtmant;
693 end = ctx->cplendmant;
694 fgain = fgaintab[ctx->cplfgaincod];
695 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->cplfsnroffst) << 2;
696 fastleak = (ctx->cplfleak << 8) + 768;
697 slowleak = (ctx->cplsleak << 8) + 768;
698 exps = ctx->dcplexps;
700 if (ctx->cpldeltbae == AC3_DBASTR_NEW || ctx->deltbae == AC3_DBASTR_REUSE) {
702 deltnseg = ctx->cpldeltnseg;
703 deltoffst = ctx->cpldeltoffst;
704 deltlen = ctx->cpldeltlen;
705 deltba = ctx->cpldeltba;
708 else if (chnl == 6) {
714 fgain = fgaintab[ctx->lfefgaincod];
715 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->lfefsnroffst) << 2;
716 exps = ctx->dlfeexps;
721 end = ctx->endmant[chnl];
725 fgain = fgaintab[ctx->fgaincod[chnl]];
726 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->fsnroffst[chnl]) << 2;
727 exps = ctx->dexps[chnl];
728 bap = ctx->bap[chnl];
729 if (ctx->deltbae[chnl] == AC3_DBASTR_NEW || ctx->deltbae[chnl] == AC3_DBASTR_REUSE) {
731 deltnseg = ctx->deltnseg[chnl];
732 deltoffst = ctx->deltoffst[chnl];
733 deltlen = ctx->deltlen[chnl];
734 deltba = ctx->deltba[chnl];
738 for (bin = start; bin < end; bin++) /* exponent mapping into psd */
739 psd[bin] = psdtab[exps[bin]];
741 /* psd integration */
745 lastbin = FFMIN((bndtab[k] + bndsz[k]), end);
748 for (i = j; i < lastbin; i++) {
749 bndpsd[k] = logadd(bndpsd[k], psd[j]);
753 } while (end > lastbin);
755 /* compute the excite function */
756 bndstrt = masktab[start];
757 bndend = masktab[end - 1] + 1;
759 lowcomp = calc_lowcomp(lowcomp, bndpsd[0], bndpsd[1], 0);
760 excite[0] = bndpsd[0] - fgain - lowcomp;
761 lowcomp = calc_lowcomp(lowcomp, bndpsd[1], bndpsd[2], 1);
762 excite[1] = bndpsd[1] - fgain - lowcomp;
764 for (bin = 2; bin < 7; bin++) {
765 if ((bndend != 7) || (bin != 6))
766 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
767 fastleak = bndpsd[bin] - fgain;
768 slowleak = bndpsd[bin] - sgain;
769 excite[bin] = fastleak - lowcomp;
770 if ((bndend != 7) || (bin != 6))
771 if (bndpsd[bin] <= bndpsd[bin + 1]) {
776 for (bin = begin; bin < FFMIN(bndend, 22); bin++) {
777 if ((bndend != 7) || (bin != 6))
778 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
780 fastleak = FFMAX(fastleak, (bndpsd[bin] - fgain));
782 slowleak = FFMAX(slowleak, (bndpsd[bin] - sgain));
783 excite[bin] = FFMAX((fastleak - lowcomp), slowleak);
790 for (bin = begin; bin < bndend; bin++) {
792 fastleak = FFMAX(fastleak, (bndpsd[bin] - fgain));
794 slowleak = FFMAX(slowleak, (bndpsd[bin] - sgain));
795 excite[bin] = FFMAX(fastleak, slowleak);
798 /* compute the masking curve */
799 for (bin = bndstrt; bin < bndend; bin++) {
800 if (bndpsd[bin] < dbknee)
801 excite[bin] += ((dbknee - bndpsd[bin]) >> 2);
802 mask[bin] = FFMAX(excite[bin], hth[bin][fscod]);
805 /* apply the delta bit allocation */
808 for (seg = 0; seg < deltnseg + 1; seg++) {
809 band += deltoffst[seg];
810 if (deltba[seg] >= 4)
811 delta = (deltba[seg] - 3) << 7;
813 delta = (deltba[seg] - 4) << 7;
814 for (k = 0; k < deltlen[seg]; k++) {
821 /*compute the bit allocation */
825 lastbin = FFMIN((bndtab[j] + bndsz[j]), end);
826 mask[j] -= snroffset;
832 for (k = i; k < lastbin; k++) {
833 address = (psd[i] - mask[j]) >> 5;
834 address = FFMIN(63, (FFMAX(0, address)));
835 bap[i] = baptab[address];
839 } while (end > lastbin);
842 /* Check if snroffsets are zero. */
843 static int is_snr_offsets_zero(AC3DecodeContext *ctx)
847 if ((ctx->csnroffst) || (ctx->cplinu && ctx->cplfsnroffst) ||
848 (ctx->lfeon && ctx->lfefsnroffst))
851 for (i = 0; i < ctx->nfchans; i++)
852 if (ctx->fsnroffst[i])
858 typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
859 int16_t l3_quantizers[3];
860 int16_t l5_quantizers[3];
861 int16_t l11_quantizers[2];
867 #define TRANSFORM_COEFF(tc, m, e, f) (tc) = (m) * (f)[(e)]
869 /* Get the transform coefficients for coupling channel and uncouple channels.
870 * The coupling transform coefficients starts at the the cplstrtmant, which is
871 * equal to endmant[ch] for fbw channels. Hence we can uncouple channels before
872 * getting transform coefficients for the channel.
874 static int get_transform_coeffs_cpling(AC3DecodeContext *ctx, mant_groups *m)
876 GetBitContext *gb = &ctx->gb;
877 int ch, start, end, cplbndstrc, bnd, gcode, tbap;
878 float cplcos[5], cplcoeff;
879 uint8_t *exps = ctx->dcplexps;
880 uint8_t *bap = ctx->cplbap;
882 cplbndstrc = ctx->cplbndstrc;
883 start = ctx->cplstrtmant;
886 while (start < ctx->cplendmant) {
888 while (cplbndstrc & 1) {
893 for (ch = 0; ch < ctx->nfchans; ch++)
894 cplcos[ch] = ctx->chcoeffs[ch] * ctx->cplco[ch][bnd];
897 while (start < end) {
901 for (ch = 0; ch < ctx->nfchans; ch++)
902 if (((ctx->chincpl) >> ch) & 1) {
903 if ((ctx->dithflag >> ch) & 1) {
904 TRANSFORM_COEFF(cplcoeff, dither_int16(&ctx->dith_state), exps[start], scale_factors);
905 ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch] * LEVEL_MINUS_3DB;
907 ctx->transform_coeffs[ch + 1][start] = 0;
913 gcode = get_bits(gb, 5);
914 m->l3_quantizers[0] = l3_quantizers_1[gcode];
915 m->l3_quantizers[1] = l3_quantizers_2[gcode];
916 m->l3_quantizers[2] = l3_quantizers_3[gcode];
919 TRANSFORM_COEFF(cplcoeff, m->l3_quantizers[m->l3ptr++], exps[start], scale_factors);
924 gcode = get_bits(gb, 7);
925 m->l5_quantizers[0] = l5_quantizers_1[gcode];
926 m->l5_quantizers[1] = l5_quantizers_2[gcode];
927 m->l5_quantizers[2] = l5_quantizers_3[gcode];
930 TRANSFORM_COEFF(cplcoeff, m->l5_quantizers[m->l5ptr++], exps[start], scale_factors);
934 TRANSFORM_COEFF(cplcoeff, l7_quantizers[get_bits(gb, 3)], exps[start], scale_factors);
939 gcode = get_bits(gb, 7);
940 m->l11_quantizers[0] = l11_quantizers_1[gcode];
941 m->l11_quantizers[1] = l11_quantizers_2[gcode];
944 TRANSFORM_COEFF(cplcoeff, m->l11_quantizers[m->l11ptr++], exps[start], scale_factors);
948 TRANSFORM_COEFF(cplcoeff, l15_quantizers[get_bits(gb, 4)], exps[start], scale_factors);
952 TRANSFORM_COEFF(cplcoeff, get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]),
953 exps[start], scale_factors);
955 for (ch = 0; ch < ctx->nfchans; ch++)
956 if ((ctx->chincpl >> ch) & 1)
957 ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
965 /* Get the transform coefficients for particular channel */
966 static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
968 GetBitContext *gb = &ctx->gb;
969 int i, gcode, tbap, dithflag, end;
975 for (i = 0; i < 25; i++)
976 factors[i] = scale_factors[i] * ctx->chcoeffs[ch_index];
978 if (ch_index != -1) { /* fbw channels */
979 dithflag = (ctx->dithflag >> ch_index) & 1;
980 exps = ctx->dexps[ch_index];
981 bap = ctx->bap[ch_index];
982 coeffs = ctx->transform_coeffs[ch_index + 1];
983 end = ctx->endmant[ch_index];
984 } else if (ch_index == -1) {
986 exps = ctx->dlfeexps;
988 coeffs = ctx->transform_coeffs[0];
993 for (i = 0; i < end; i++) {
1002 TRANSFORM_COEFF(coeffs[i], dither_int16(&ctx->dith_state), exps[i], factors);
1003 coeffs[i] *= LEVEL_MINUS_3DB;
1009 gcode = get_bits(gb, 5);
1010 m->l3_quantizers[0] = l3_quantizers_1[gcode];
1011 m->l3_quantizers[1] = l3_quantizers_2[gcode];
1012 m->l3_quantizers[2] = l3_quantizers_3[gcode];
1015 TRANSFORM_COEFF(coeffs[i], m->l3_quantizers[m->l3ptr++], exps[i], factors);
1020 gcode = get_bits(gb, 7);
1021 m->l5_quantizers[0] = l5_quantizers_1[gcode];
1022 m->l5_quantizers[1] = l5_quantizers_2[gcode];
1023 m->l5_quantizers[2] = l5_quantizers_3[gcode];
1026 TRANSFORM_COEFF(coeffs[i], m->l5_quantizers[m->l5ptr++], exps[i], factors);
1030 TRANSFORM_COEFF(coeffs[i], l7_quantizers[get_bits(gb, 3)], exps[i], factors);
1034 if (m->l11ptr > 1) {
1035 gcode = get_bits(gb, 7);
1036 m->l11_quantizers[0] = l11_quantizers_1[gcode];
1037 m->l11_quantizers[1] = l11_quantizers_2[gcode];
1040 TRANSFORM_COEFF(coeffs[i], m->l11_quantizers[m->l11ptr++], exps[i], factors);
1044 TRANSFORM_COEFF(coeffs[i], l15_quantizers[get_bits(gb, 4)], exps[i], factors);
1048 TRANSFORM_COEFF(coeffs[i], get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]), exps[i], factors);
1056 /* Get the transform coefficients.
1057 * This function extracts the tranform coefficients form the ac3 bitstream.
1058 * This function is called after bit allocation is performed.
1060 static int get_transform_coeffs(AC3DecodeContext * ctx)
1063 int got_cplchan = 0;
1066 m.l3ptr = m.l5ptr = m.l11ptr = 3;
1068 for (i = 0; i < ctx->nfchans; i++) {
1069 /* transform coefficients for individual channel */
1070 if (get_transform_coeffs_ch(ctx, i, &m))
1072 /* tranform coefficients for coupling channels */
1073 if ((ctx->chincpl >> i) & 1) {
1075 if (get_transform_coeffs_cpling(ctx, &m)) {
1076 av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
1081 end = ctx->cplendmant;
1083 end = ctx->endmant[i];
1085 ctx->transform_coeffs[i + 1][end] = 0;
1089 if (get_transform_coeffs_ch(ctx, -1, &m))
1091 for (i = 7; i < 256; i++) {
1092 ctx->transform_coeffs[0][i] = 0;
1099 /* Rematrixing routines. */
1100 static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
1104 while (start < end) {
1105 tmp0 = ctx->transform_coeffs[1][start];
1106 tmp1 = ctx->transform_coeffs[2][start];
1107 ctx->transform_coeffs[1][start] = tmp0 + tmp1;
1108 ctx->transform_coeffs[2][start] = tmp0 - tmp1;
1113 static void do_rematrixing(AC3DecodeContext *ctx)
1115 int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
1118 end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
1120 if (ctx->rematflg & 1)
1121 do_rematrixing1(ctx, bnd1, bnd2);
1123 if (ctx->rematflg & 2)
1124 do_rematrixing1(ctx, bnd2, bnd3);
1129 if (ctx->rematflg & 4)
1130 do_rematrixing1(ctx, bnd3, bndend);
1132 if (ctx->rematflg & 4)
1133 do_rematrixing1(ctx, bnd3, bnd4);
1134 if (ctx->rematflg & 8)
1135 do_rematrixing1(ctx, bnd4, end);
1139 /* This function sets the normalized channel coefficients.
1140 * Transform coefficients are multipllied by the channel
1141 * coefficients to get normalized transform coefficients.
1143 static void get_downmix_coeffs(AC3DecodeContext *ctx)
1145 int from = ctx->acmod;
1146 int to = ctx->blkoutput;
1147 float clev = clevs[ctx->cmixlev];
1148 float slev = slevs[ctx->surmixlev];
1149 float nf = 1.0; //normalization factor for downmix coeffs
1153 ctx->chcoeffs[0] = 2 * ctx->dynrng;
1154 ctx->chcoeffs[1] = 2 * ctx->dynrng2;
1156 for (i = 0; i < ctx->nfchans; i++)
1157 ctx->chcoeffs[i] = 2 * ctx->dynrng;
1160 if (to == AC3_OUTPUT_UNMODIFIED)
1164 case AC3_INPUT_DUALMONO:
1166 case AC3_OUTPUT_MONO:
1167 case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */
1169 ctx->chcoeffs[0] *= nf;
1170 ctx->chcoeffs[1] *= nf;
1174 case AC3_INPUT_MONO:
1176 case AC3_OUTPUT_STEREO:
1177 nf = LEVEL_MINUS_3DB;
1178 ctx->chcoeffs[0] *= nf;
1182 case AC3_INPUT_STEREO:
1184 case AC3_OUTPUT_MONO:
1185 nf = LEVEL_MINUS_3DB;
1186 ctx->chcoeffs[0] *= nf;
1187 ctx->chcoeffs[1] *= nf;
1193 case AC3_OUTPUT_MONO:
1194 nf = LEVEL_MINUS_3DB / (1.0 + clev);
1195 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1196 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1197 ctx->chcoeffs[1] *= ((nf * clev * LEVEL_MINUS_3DB) / 2.0);
1199 case AC3_OUTPUT_STEREO:
1200 nf = 1.0 / (1.0 + clev);
1201 ctx->chcoeffs[0] *= nf;
1202 ctx->chcoeffs[2] *= nf;
1203 ctx->chcoeffs[1] *= (nf * clev);
1207 case AC3_INPUT_2F_1R:
1209 case AC3_OUTPUT_MONO:
1210 nf = 2.0 * LEVEL_MINUS_3DB / (2.0 + slev);
1211 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1212 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1213 ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
1215 case AC3_OUTPUT_STEREO:
1216 nf = 1.0 / (1.0 + (slev * LEVEL_MINUS_3DB));
1217 ctx->chcoeffs[0] *= nf;
1218 ctx->chcoeffs[1] *= nf;
1219 ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
1221 case AC3_OUTPUT_DOLBY:
1222 nf = 1.0 / (1.0 + LEVEL_MINUS_3DB);
1223 ctx->chcoeffs[0] *= nf;
1224 ctx->chcoeffs[1] *= nf;
1225 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1229 case AC3_INPUT_3F_1R:
1231 case AC3_OUTPUT_MONO:
1232 nf = LEVEL_MINUS_3DB / (1.0 + clev + (slev / 2.0));
1233 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1234 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1235 ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
1236 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1238 case AC3_OUTPUT_STEREO:
1239 nf = 1.0 / (1.0 + clev + (slev * LEVEL_MINUS_3DB));
1240 ctx->chcoeffs[0] *= nf;
1241 ctx->chcoeffs[2] *= nf;
1242 ctx->chcoeffs[1] *= (nf * clev);
1243 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1245 case AC3_OUTPUT_DOLBY:
1246 nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
1247 ctx->chcoeffs[0] *= nf;
1248 ctx->chcoeffs[1] *= nf;
1249 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1250 ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
1254 case AC3_INPUT_2F_2R:
1256 case AC3_OUTPUT_MONO:
1257 nf = LEVEL_MINUS_3DB / (1.0 + slev);
1258 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1259 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1260 ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
1261 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1263 case AC3_OUTPUT_STEREO:
1264 nf = 1.0 / (1.0 + slev);
1265 ctx->chcoeffs[0] *= nf;
1266 ctx->chcoeffs[1] *= nf;
1267 ctx->chcoeffs[2] *= (nf * slev);
1268 ctx->chcoeffs[3] *= (nf * slev);
1270 case AC3_OUTPUT_DOLBY:
1271 nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
1272 ctx->chcoeffs[0] *= nf;
1273 ctx->chcoeffs[1] *= nf;
1274 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1275 ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
1279 case AC3_INPUT_3F_2R:
1281 case AC3_OUTPUT_MONO:
1282 nf = LEVEL_MINUS_3DB / (1.0 + clev + slev);
1283 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1284 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1285 ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
1286 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1287 ctx->chcoeffs[4] *= (nf * slev * LEVEL_MINUS_3DB);
1289 case AC3_OUTPUT_STEREO:
1290 nf = 1.0 / (1.0 + clev + slev);
1291 ctx->chcoeffs[0] *= nf;
1292 ctx->chcoeffs[2] *= nf;
1293 ctx->chcoeffs[1] *= (nf * clev);
1294 ctx->chcoeffs[3] *= (nf * slev);
1295 ctx->chcoeffs[4] *= (nf * slev);
1297 case AC3_OUTPUT_DOLBY:
1298 nf = 1.0 / (1.0 + (3.0 * LEVEL_MINUS_3DB));
1299 ctx->chcoeffs[0] *= nf;
1300 ctx->chcoeffs[1] *= nf;
1301 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1302 ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
1303 ctx->chcoeffs[4] *= (nf * LEVEL_MINUS_3DB);
1310 /*********** BEGIN DOWNMIX FUNCTIONS ***********/
1311 static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx)
1314 float (*output)[BLOCK_SIZE] = ctx->output;
1316 for (i = 0; i < 256; i++)
1317 output[1][i] += output[2][i];
1318 memset(output[2], 0, sizeof(output[2]));
1321 static inline void mix_dualmono_to_stereo(AC3DecodeContext *ctx)
1325 float (*output)[BLOCK_SIZE] = ctx->output;
1327 for (i = 0; i < 256; i++) {
1328 tmp = output[1][i] + output[2][i];
1329 output[1][i] = output[2][i] = tmp;
1333 static inline void upmix_mono_to_stereo(AC3DecodeContext *ctx)
1336 float (*output)[BLOCK_SIZE] = ctx->output;
1338 for (i = 0; i < 256; i++)
1339 output[2][i] = output[1][i];
1342 static inline void mix_stereo_to_mono(AC3DecodeContext *ctx)
1345 float (*output)[BLOCK_SIZE] = ctx->output;
1347 for (i = 0; i < 256; i++)
1348 output[1][i] += output[2][i];
1349 memset(output[2], 0, sizeof(output[2]));
1352 static inline void mix_3f_to_mono(AC3DecodeContext *ctx)
1355 float (*output)[BLOCK_SIZE] = ctx->output;
1357 for (i = 0; i < 256; i++)
1358 output[1][i] += (output[2][i] + output[3][i]);
1359 memset(output[2], 0, sizeof(output[2]));
1360 memset(output[3], 0, sizeof(output[3]));
1363 static inline void mix_3f_to_stereo(AC3DecodeContext *ctx)
1366 float (*output)[BLOCK_SIZE] = ctx->output;
1368 for (i = 0; i < 256; i++) {
1369 output[1][i] += output[2][i];
1370 output[2][i] += output[3][i];
1372 memset(output[3], 0, sizeof(output[3]));
1375 static inline void mix_2f_1r_to_mono(AC3DecodeContext *ctx)
1378 float (*output)[BLOCK_SIZE] = ctx->output;
1380 for (i = 0; i < 256; i++)
1381 output[1][i] += (output[2][i] + output[3][i]);
1382 memset(output[2], 0, sizeof(output[2]));
1383 memset(output[3], 0, sizeof(output[3]));
1387 static inline void mix_2f_1r_to_stereo(AC3DecodeContext *ctx)
1390 float (*output)[BLOCK_SIZE] = ctx->output;
1392 for (i = 0; i < 256; i++) {
1393 output[1][i] += output[2][i];
1394 output[2][i] += output[3][i];
1396 memset(output[3], 0, sizeof(output[3]));
1399 static inline void mix_2f_1r_to_dolby(AC3DecodeContext *ctx)
1402 float (*output)[BLOCK_SIZE] = ctx->output;
1404 for (i = 0; i < 256; i++) {
1405 output[1][i] -= output[3][i];
1406 output[2][i] += output[3][i];
1408 memset(output[3], 0, sizeof(output[3]));
1411 static inline void mix_3f_1r_to_mono(AC3DecodeContext *ctx)
1414 float (*output)[BLOCK_SIZE] = ctx->output;
1416 for (i = 0; i < 256; i++)
1417 output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1418 memset(output[2], 0, sizeof(output[2]));
1419 memset(output[3], 0, sizeof(output[3]));
1420 memset(output[4], 0, sizeof(output[4]));
1423 static inline void mix_3f_1r_to_stereo(AC3DecodeContext *ctx)
1426 float (*output)[BLOCK_SIZE] = ctx->output;
1428 for (i = 0; i < 256; i++) {
1429 output[1][i] += (output[2][i] + output[4][i]);
1430 output[2][i] += (output[3][i] + output[4][i]);
1432 memset(output[3], 0, sizeof(output[3]));
1433 memset(output[4], 0, sizeof(output[4]));
1436 static inline void mix_3f_1r_to_dolby(AC3DecodeContext *ctx)
1439 float (*output)[BLOCK_SIZE] = ctx->output;
1441 for (i = 0; i < 256; i++) {
1442 output[1][i] += (output[2][i] - output[4][i]);
1443 output[2][i] += (output[3][i] + output[4][i]);
1445 memset(output[3], 0, sizeof(output[3]));
1446 memset(output[4], 0, sizeof(output[4]));
1449 static inline void mix_2f_2r_to_mono(AC3DecodeContext *ctx)
1452 float (*output)[BLOCK_SIZE] = ctx->output;
1454 for (i = 0; i < 256; i++)
1455 output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1456 memset(output[2], 0, sizeof(output[2]));
1457 memset(output[3], 0, sizeof(output[3]));
1458 memset(output[4], 0, sizeof(output[4]));
1461 static inline void mix_2f_2r_to_stereo(AC3DecodeContext *ctx)
1464 float (*output)[BLOCK_SIZE] = ctx->output;
1466 for (i = 0; i < 256; i++) {
1467 output[1][i] += output[3][i];
1468 output[2][i] += output[4][i];
1470 memset(output[3], 0, sizeof(output[3]));
1471 memset(output[4], 0, sizeof(output[4]));
1474 static inline void mix_2f_2r_to_dolby(AC3DecodeContext *ctx)
1477 float (*output)[BLOCK_SIZE] = ctx->output;
1479 for (i = 0; i < 256; i++) {
1480 output[1][i] -= output[3][i];
1481 output[2][i] += output[4][i];
1483 memset(output[3], 0, sizeof(output[3]));
1484 memset(output[4], 0, sizeof(output[4]));
1487 static inline void mix_3f_2r_to_mono(AC3DecodeContext *ctx)
1490 float (*output)[BLOCK_SIZE] = ctx->output;
1492 for (i = 0; i < 256; i++)
1493 output[1][i] += (output[2][i] + output[3][i] + output[4][i] + output[5][i]);
1494 memset(output[2], 0, sizeof(output[2]));
1495 memset(output[3], 0, sizeof(output[3]));
1496 memset(output[4], 0, sizeof(output[4]));
1497 memset(output[5], 0, sizeof(output[5]));
1500 static inline void mix_3f_2r_to_stereo(AC3DecodeContext *ctx)
1503 float (*output)[BLOCK_SIZE] = ctx->output;
1505 for (i = 0; i < 256; i++) {
1506 output[1][i] += (output[2][i] + output[4][i]);
1507 output[2][i] += (output[3][i] + output[5][i]);
1509 memset(output[3], 0, sizeof(output[3]));
1510 memset(output[4], 0, sizeof(output[4]));
1511 memset(output[5], 0, sizeof(output[5]));
1514 static inline void mix_3f_2r_to_dolby(AC3DecodeContext *ctx)
1517 float (*output)[BLOCK_SIZE] = ctx->output;
1519 for (i = 0; i < 256; i++) {
1520 output[1][i] += (output[2][i] - output[4][i] - output[5][i]);
1521 output[2][i] += (output[3][i] + output[4][i] + output[5][i]);
1523 memset(output[3], 0, sizeof(output[3]));
1524 memset(output[4], 0, sizeof(output[4]));
1525 memset(output[5], 0, sizeof(output[5]));
1527 /*********** END DOWNMIX FUNCTIONS ***********/
1529 /* Downmix the output.
1530 * This function downmixes the output when the number of input
1531 * channels is not equal to the number of output channels requested.
1533 static void do_downmix(AC3DecodeContext *ctx)
1535 int from = ctx->acmod;
1536 int to = ctx->blkoutput;
1538 if (to == AC3_OUTPUT_UNMODIFIED)
1542 case AC3_INPUT_DUALMONO:
1544 case AC3_OUTPUT_MONO:
1545 mix_dualmono_to_mono(ctx);
1547 case AC3_OUTPUT_STEREO: /* We assume that sum of both mono channels is requested */
1548 mix_dualmono_to_stereo(ctx);
1552 case AC3_INPUT_MONO:
1554 case AC3_OUTPUT_STEREO:
1555 upmix_mono_to_stereo(ctx);
1559 case AC3_INPUT_STEREO:
1561 case AC3_OUTPUT_MONO:
1562 mix_stereo_to_mono(ctx);
1568 case AC3_OUTPUT_MONO:
1569 mix_3f_to_mono(ctx);
1571 case AC3_OUTPUT_STEREO:
1572 mix_3f_to_stereo(ctx);
1576 case AC3_INPUT_2F_1R:
1578 case AC3_OUTPUT_MONO:
1579 mix_2f_1r_to_mono(ctx);
1581 case AC3_OUTPUT_STEREO:
1582 mix_2f_1r_to_stereo(ctx);
1584 case AC3_OUTPUT_DOLBY:
1585 mix_2f_1r_to_dolby(ctx);
1589 case AC3_INPUT_3F_1R:
1591 case AC3_OUTPUT_MONO:
1592 mix_3f_1r_to_mono(ctx);
1594 case AC3_OUTPUT_STEREO:
1595 mix_3f_1r_to_stereo(ctx);
1597 case AC3_OUTPUT_DOLBY:
1598 mix_3f_1r_to_dolby(ctx);
1602 case AC3_INPUT_2F_2R:
1604 case AC3_OUTPUT_MONO:
1605 mix_2f_2r_to_mono(ctx);
1607 case AC3_OUTPUT_STEREO:
1608 mix_2f_2r_to_stereo(ctx);
1610 case AC3_OUTPUT_DOLBY:
1611 mix_2f_2r_to_dolby(ctx);
1615 case AC3_INPUT_3F_2R:
1617 case AC3_OUTPUT_MONO:
1618 mix_3f_2r_to_mono(ctx);
1620 case AC3_OUTPUT_STEREO:
1621 mix_3f_2r_to_stereo(ctx);
1623 case AC3_OUTPUT_DOLBY:
1624 mix_3f_2r_to_dolby(ctx);
1631 static void dump_floats(const char *name, int prec, const float *tab, int n)
1635 av_log(NULL, AV_LOG_INFO, "%s[%d]:\n", name, n);
1638 av_log(NULL, AV_LOG_INFO, "%4d: ", i);
1639 av_log(NULL, AV_LOG_INFO, " %8.*f", prec, tab[i]);
1641 av_log(NULL, AV_LOG_INFO, "\n");
1644 av_log(NULL, AV_LOG_INFO, "\n");
1647 /* This function performs the imdct on 256 sample transform
1650 static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
1653 float x1[128], x2[128];
1654 float *o_ptr, *d_ptr, *w;
1655 FFTComplex *ptr1, *ptr2;
1657 for (k = 0; k < N / 4; k++) {
1658 x1[k] = ctx->transform_coeffs[chindex][2 * k];
1659 x2[k] = ctx->transform_coeffs[chindex][2 * k + 1];
1662 ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, ctx->tmp_output, x1, ctx->tmp_imdct);
1663 ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, ctx->tmp_output + 256, x2, ctx->tmp_imdct);
1665 o_ptr = ctx->output[chindex];
1666 d_ptr = ctx->delay[chindex];
1667 ptr1 = (FFTComplex *)ctx->tmp_output;
1668 ptr2 = (FFTComplex *)ctx->tmp_output + 256;
1671 for (k = 0; k < N / 8; k++)
1673 o_ptr[2 * k] = -ptr1[k].im * w[2 * k] + d_ptr[2 * k] + 384.0;
1674 o_ptr[2 * k + 1] = ptr1[N / 8 - k - 1].re * w[2 * k + 1] + 384.0;
1675 o_ptr[N / 4 + 2 * k] = -ptr1[k].re * w[N / 4 + 2 * k] + d_ptr[N / 4 + 2 * k] + 384.0;
1676 o_ptr[N / 4 + 2 * k + 1] = ptr1[N / 8 - k - 1].im * w[N / 4 + 2 * k + 1] + d_ptr[N / 4 + 2 * k + 1] + 384.0;
1677 d_ptr[2 * k] = ptr2[k].re * w[k / 2 - 2 * k - 1];
1678 d_ptr[2 * k + 1] = -ptr2[N / 8 - k - 1].im * w[N / 2 - 2 * k - 2];
1679 d_ptr[N / 4 + 2 * k] = ptr2[k].im * w[N / 4 - 2 * k - 1];
1680 d_ptr[N / 4 + 2 * k + 1] = -ptr2[N / 8 - k - 1].re * w[N / 4 - 2 * k - 2];
1684 /* This function performs the imdct on 512 sample transform
1687 static void do_imdct_512(AC3DecodeContext *ctx, int chindex)
1691 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1692 ctx->transform_coeffs[chindex], ctx->tmp_imdct);
1693 ptr = ctx->output[chindex];
1694 ctx->dsp.vector_fmul_add_add(ptr, ctx->tmp_output, ctx->window, ctx->delay[chindex], 384, BLOCK_SIZE, 1);
1695 ptr = ctx->delay[chindex];
1696 ctx->dsp.vector_fmul_reverse(ptr, ctx->tmp_output + 256, ctx->window, BLOCK_SIZE);
1699 /* IMDCT Transform. */
1700 static inline void do_imdct(AC3DecodeContext *ctx)
1704 if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
1705 do_imdct_512(ctx, 0);
1707 for (i = 0; i < ctx->nfchans; i++) {
1708 if ((ctx->blksw >> i) & 1)
1709 do_imdct_256(ctx, i + 1);
1711 do_imdct_512(ctx, i + 1);
1715 /* Parse the audio block from ac3 bitstream.
1716 * This function extract the audio block from the ac3 bitstream
1717 * and produces the output for the block. This function must
1718 * be called for each of the six audio block in the ac3 bitstream.
1720 static int ac3_parse_audio_block(AC3DecodeContext * ctx)
1722 int nfchans = ctx->nfchans;
1723 int acmod = ctx->acmod;
1724 int i, bnd, rbnd, seg, grpsize;
1725 GetBitContext *gb = &ctx->gb;
1726 int bit_alloc_flags = 0;
1728 int mstrcplco, cplcoexp, cplcomant;
1729 int dynrng, chbwcod, ngrps, cplabsexp, skipl;
1732 for (i = 0; i < nfchans; i++) /*block switch flag */
1733 ctx->blksw |= get_bits1(gb) << i;
1736 for (i = 0; i < nfchans; i++) /* dithering flag */
1737 ctx->dithflag |= get_bits1(gb) << i;
1739 if (get_bits1(gb)) { /* dynamic range */
1740 dynrng = get_sbits(gb, 8);
1741 ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1744 if (acmod == 0x00 && get_bits1(gb)) { /* dynamic range 1+1 mode */
1745 dynrng = get_sbits(gb, 8);
1746 ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1749 get_downmix_coeffs(ctx);
1751 if (get_bits1(gb)) { /* coupling strategy */
1752 ctx->cplinu = get_bits1(gb);
1753 ctx->cplbndstrc = 0;
1755 if (ctx->cplinu) { /* coupling in use */
1756 for (i = 0; i < nfchans; i++)
1757 ctx->chincpl |= get_bits1(gb) << i;
1760 ctx->phsflginu = get_bits1(gb); //phase flag in use
1762 ctx->cplbegf = get_bits(gb, 4);
1763 ctx->cplendf = get_bits(gb, 4);
1765 if (3 + ctx->cplendf - ctx->cplbegf < 0) {
1766 av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", ctx->cplendf, ctx->cplbegf);
1770 ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf;
1771 ctx->cplstrtmant = ctx->cplbegf * 12 + 37;
1772 ctx->cplendmant = ctx->cplendf * 12 + 73;
1773 for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
1774 if (get_bits1(gb)) {
1775 ctx->cplbndstrc |= 1 << i;
1784 for (i = 0; i < nfchans; i++)
1785 if ((ctx->chincpl) >> i & 1)
1786 if (get_bits1(gb)) { /* coupling co-ordinates */
1787 ctx->cplcoe |= 1 << i;
1788 mstrcplco = 3 * get_bits(gb, 2);
1789 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
1790 cplcoexp = get_bits(gb, 4);
1791 cplcomant = get_bits(gb, 4);
1795 cplcomant = (cplcomant | 0x10) << 13;
1796 ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
1800 if (acmod == 0x02 && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
1801 for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
1803 ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
1806 if (acmod == 0x02) {/* rematrixing */
1807 ctx->rematstr = get_bits1(gb);
1808 if (ctx->rematstr) {
1811 if (!(ctx->cplinu) || ctx->cplbegf > 2)
1812 for (rbnd = 0; rbnd < 4; rbnd++)
1813 ctx->rematflg |= get_bits1(gb) << rbnd;
1814 if (ctx->cplbegf > 0 && ctx->cplbegf <= 2 && ctx->cplinu)
1815 for (rbnd = 0; rbnd < 3; rbnd++)
1816 ctx->rematflg |= get_bits1(gb) << rbnd;
1817 if (ctx->cplbegf == 0 && ctx->cplinu)
1818 for (rbnd = 0; rbnd < 2; rbnd++)
1819 ctx->rematflg |= get_bits1(gb) << rbnd;
1823 ctx->cplexpstr = AC3_EXPSTR_REUSE;
1824 ctx->lfeexpstr = AC3_EXPSTR_REUSE;
1825 if (ctx->cplinu) /* coupling exponent strategy */
1826 ctx->cplexpstr = get_bits(gb, 2);
1827 for (i = 0; i < nfchans; i++) /* channel exponent strategy */
1828 ctx->chexpstr[i] = get_bits(gb, 2);
1829 if (ctx->lfeon) /* lfe exponent strategy */
1830 ctx->lfeexpstr = get_bits1(gb);
1832 for (i = 0; i < nfchans; i++) /* channel bandwidth code */
1833 if (ctx->chexpstr[i] != AC3_EXPSTR_REUSE) {
1834 if ((ctx->chincpl >> i) & 1)
1835 ctx->endmant[i] = ctx->cplstrtmant;
1837 chbwcod = get_bits(gb, 6);
1839 av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
1842 ctx->endmant[i] = chbwcod * 3 + 73;
1846 if (ctx->cplexpstr != AC3_EXPSTR_REUSE) {/* coupling exponents */
1847 bit_alloc_flags = 64;
1848 cplabsexp = get_bits(gb, 4) << 1;
1849 ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
1850 if (decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant)) {
1851 av_log(NULL, AV_LOG_ERROR, "error decoding coupling exponents\n");
1856 for (i = 0; i < nfchans; i++) /* fbw channel exponents */
1857 if (ctx->chexpstr[i] != AC3_EXPSTR_REUSE) {
1858 bit_alloc_flags |= 1 << i;
1859 grpsize = 3 << (ctx->chexpstr[i] - 1);
1860 ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
1861 dexps = ctx->dexps[i];
1862 dexps[0] = get_bits(gb, 4);
1863 if (decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1)) {
1864 av_log(NULL, AV_LOG_ERROR, "error decoding channel %d exponents\n", i);
1867 skip_bits(gb, 2); /* skip gainrng */
1870 if (ctx->lfeexpstr != AC3_EXPSTR_REUSE) { /* lfe exponents */
1871 bit_alloc_flags |= 32;
1872 ctx->dlfeexps[0] = get_bits(gb, 4);
1873 if (decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1)) {
1874 av_log(NULL, AV_LOG_ERROR, "error decoding lfe exponents\n");
1879 if (get_bits1(gb)) { /* bit allocation information */
1880 bit_alloc_flags = 127;
1881 ctx->sdcycod = get_bits(gb, 2);
1882 ctx->fdcycod = get_bits(gb, 2);
1883 ctx->sgaincod = get_bits(gb, 2);
1884 ctx->dbpbcod = get_bits(gb, 2);
1885 ctx->floorcod = get_bits(gb, 3);
1888 if (get_bits1(gb)) { /* snroffset */
1889 bit_alloc_flags = 127;
1890 ctx->csnroffst = get_bits(gb, 6);
1891 if (ctx->cplinu) { /* coupling fine snr offset and fast gain code */
1892 ctx->cplfsnroffst = get_bits(gb, 4);
1893 ctx->cplfgaincod = get_bits(gb, 3);
1895 for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
1896 ctx->fsnroffst[i] = get_bits(gb, 4);
1897 ctx->fgaincod[i] = get_bits(gb, 3);
1899 if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
1900 ctx->lfefsnroffst = get_bits(gb, 4);
1901 ctx->lfefgaincod = get_bits(gb, 3);
1905 if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
1906 bit_alloc_flags |= 64;
1907 ctx->cplfleak = get_bits(gb, 3);
1908 ctx->cplsleak = get_bits(gb, 3);
1911 if (get_bits1(gb)) { /* delta bit allocation information */
1912 bit_alloc_flags = 127;
1915 ctx->cpldeltbae = get_bits(gb, 2);
1916 if (ctx->cpldeltbae == AC3_DBASTR_RESERVED) {
1917 av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
1922 for (i = 0; i < nfchans; i++) {
1923 ctx->deltbae[i] = get_bits(gb, 2);
1924 if (ctx->deltbae[i] == AC3_DBASTR_RESERVED) {
1925 av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
1931 if (ctx->cpldeltbae == AC3_DBASTR_NEW) { /*coupling delta offset, len and bit allocation */
1932 ctx->cpldeltnseg = get_bits(gb, 3);
1933 for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
1934 ctx->cpldeltoffst[seg] = get_bits(gb, 5);
1935 ctx->cpldeltlen[seg] = get_bits(gb, 4);
1936 ctx->cpldeltba[seg] = get_bits(gb, 3);
1940 for (i = 0; i < nfchans; i++)
1941 if (ctx->deltbae[i] == AC3_DBASTR_NEW) {/*channel delta offset, len and bit allocation */
1942 ctx->deltnseg[i] = get_bits(gb, 3);
1943 for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
1944 ctx->deltoffst[i][seg] = get_bits(gb, 5);
1945 ctx->deltlen[i][seg] = get_bits(gb, 4);
1946 ctx->deltba[i][seg] = get_bits(gb, 3);
1951 if (bit_alloc_flags) {
1952 if (is_snr_offsets_zero(ctx)) {
1953 memset(ctx->cplbap, 0, sizeof (ctx->cplbap));
1954 memset(ctx->lfebap, 0, sizeof (ctx->lfebap));
1955 for (i = 0; i < nfchans; i++)
1956 memset(ctx->bap[i], 0, sizeof(ctx->bap[i]));
1958 if (ctx->chincpl && (bit_alloc_flags & 64))
1959 do_bit_allocation(ctx, 5);
1960 for (i = 0; i < nfchans; i++)
1961 if ((bit_alloc_flags >> i) & 1)
1962 do_bit_allocation(ctx, i);
1963 if (ctx->lfeon && (bit_alloc_flags & 32))
1964 do_bit_allocation(ctx, 6);
1968 if (get_bits1(gb)) { /* unused dummy data */
1969 skipl = get_bits(gb, 9);
1973 /* unpack the transform coefficients
1974 * * this also uncouples channels if coupling is in use.
1976 if (get_transform_coeffs(ctx)) {
1977 av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1980 /*for (i = 0; i < nfchans; i++)
1981 dump_floats("channel transform coefficients", 10, ctx->transform_coeffs[i + 1], BLOCK_SIZE);*/
1983 /* recover coefficients if rematrixing is in use */
1985 do_rematrixing(ctx);
1990 /*for(i = 0; i < nfchans; i++)
1991 dump_floats("channel output", 10, ctx->output[i + 1], BLOCK_SIZE);*/
1996 static inline int16_t convert(int32_t i)
2000 else if (i <= 0x43bf8000)
2003 return (i - 0x43c00000);
2006 static int frame_count = 0;
2008 /* Decode ac3 frame.
2010 * @param avctx Pointer to AVCodecContext
2011 * @param data Pointer to pcm smaples
2012 * @param data_size Set to number of pcm samples produced by decoding
2013 * @param buf Data to be decoded
2014 * @param buf_size Size of the buffer
2016 static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
2018 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
2020 int16_t *out_samples = (int16_t *)data;
2022 int32_t *int_ptr[6];
2024 for (i = 0; i < 6; i++)
2025 int_ptr[i] = (int32_t *)(&ctx->output[i]);
2027 //av_log(NULL, AV_LOG_INFO, "decoding frame %d buf_size = %d\n", frame_count++, buf_size);
2029 //Synchronize the frame.
2030 frame_start = ac3_synchronize(buf, buf_size);
2031 if (frame_start == -1) {
2032 av_log(avctx, AV_LOG_ERROR, "frame is not synchronized\n");
2037 //Initialize the GetBitContext with the start of valid AC3 Frame.
2038 init_get_bits(&(ctx->gb), buf + frame_start, (buf_size - frame_start) * 8);
2040 //Parse the syncinfo.
2041 //If 'fscod' or 'bsid' is not valid the decoder shall mute as per the standard.
2042 if (!ac3_parse_sync_info(ctx)) {
2043 av_log(avctx, AV_LOG_ERROR, "\n");
2049 //If 'bsid' is not valid decoder shall not decode the audio as per the standard.
2052 avctx->sample_rate = ctx->sampling_rate;
2053 avctx->bit_rate = ctx->bit_rate;
2055 if (avctx->channels == 0) {
2056 ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
2058 ctx->blkoutput |= AC3_OUTPUT_LFEON;
2059 avctx->channels = ctx->nfchans + ctx->lfeon;
2061 else if (avctx->channels == 1)
2062 ctx->blkoutput |= AC3_OUTPUT_MONO;
2063 else if (avctx->channels == 2) {
2064 if (ctx->dsurmod == 0x02)
2065 ctx->blkoutput |= AC3_OUTPUT_DOLBY;
2067 ctx->blkoutput |= AC3_OUTPUT_STEREO;
2070 if (avctx->channels < (ctx->nfchans + ctx->lfeon))
2071 av_log(avctx, AV_LOG_INFO, "ac3_decoder: AC3 Source Channels Are Less Then Specified %d: Output to %d Channels\n",avctx->channels, ctx->nfchans + ctx->lfeon);
2072 ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
2074 ctx->blkoutput |= AC3_OUTPUT_LFEON;
2075 avctx->channels = ctx->nfchans + ctx->lfeon;
2078 //av_log(avctx, AV_LOG_INFO, "channels = %d \t bit rate = %d \t sampling rate = %d \n", avctx->channels, avctx->bit_rate * 1000, avctx->sample_rate);
2080 //Parse the Audio Blocks.
2081 for (i = 0; i < AUDIO_BLOCKS; i++) {
2082 if (ac3_parse_audio_block(ctx)) {
2083 av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
2085 return ctx->frame_size;
2087 start = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1;
2088 for (k = 0; k < BLOCK_SIZE; k++)
2089 for (j = start; j <= avctx->channels; j++)
2090 *(out_samples++) = convert(int_ptr[j][k]);
2092 *data_size = AUDIO_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);
2093 return ctx->frame_size;
2096 /* Uninitialize ac3 decoder.
2098 static int ac3_decode_end(AVCodecContext *avctx)
2100 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
2101 ff_mdct_end(&ctx->imdct_512);
2102 ff_mdct_end(&ctx->imdct_256);
2107 AVCodec lgpl_ac3_decoder = {
2109 .type = CODEC_TYPE_AUDIO,
2111 .priv_data_size = sizeof (AC3DecodeContext),
2112 .init = ac3_decode_init,
2113 .close = ac3_decode_end,
2114 .decode = ac3_decode_frame,