3 * This code is developed as part of Google Summer of Code 2006 Program.
5 * Copyright (c) 2006 Kartikey Mahendra BHATT (bhattkm at gmail dot com).
7 * For exponent decoding the code is inspired by the code in liba52 by
8 * Michel Lespinasse and Aaron Holtzman.
9 * http://liba52.sourceforge.net
11 * This file is part of FFmpeg.
13 * FFmpeg is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
18 * FFmpeg is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public
24 * License along with FFmpeg; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 #define ALT_BITSTREAM_READER
38 #include "bitstream.h"
42 static uint8_t bndtab[51];
43 static uint8_t masktab[253];
45 static const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 };
47 /* table for exponent to scale_factor mapping
48 * scale_factor[i] = 2 ^ -(i + 15)
50 static float scale_factors[25];
52 static int16_t psdtab[25];
54 static int8_t exp_1[128];
55 static int8_t exp_2[128];
56 static int8_t exp_3[128];
58 static int16_t l3_quantizers_1[32];
59 static int16_t l3_quantizers_2[32];
60 static int16_t l3_quantizers_3[32];
62 static int16_t l5_quantizers_1[128];
63 static int16_t l5_quantizers_2[128];
64 static int16_t l5_quantizers_3[128];
66 static int16_t l7_quantizers[7];
68 static int16_t l11_quantizers_1[128];
69 static int16_t l11_quantizers_2[128];
71 static int16_t l15_quantizers[15];
73 static const uint8_t qntztab[16] = { 0, 5, 7, 3, 7, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16 };
75 /* Adjustmens in dB gain */
76 #define LEVEL_MINUS_3DB 0.7071067811865476
77 #define LEVEL_MINUS_4POINT5DB 0.5946035575013605
78 #define LEVEL_MINUS_6DB 0.5000000000000000
79 #define LEVEL_PLUS_3DB 1.4142135623730951
80 #define LEVEL_PLUS_6DB 2.0000000000000000
81 #define LEVEL_ZERO 0.0000000000000000
83 static const float clevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB,
84 LEVEL_MINUS_6DB, LEVEL_MINUS_4POINT5DB };
86 static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB };
88 #define N 512 /* constant for IMDCT Block size */
90 #define BLOCK_SIZE 256
92 /* Output and input configurations. */
93 #define AC3_OUTPUT_UNMODIFIED 0x01
94 #define AC3_OUTPUT_MONO 0x02
95 #define AC3_OUTPUT_STEREO 0x04
96 #define AC3_OUTPUT_DOLBY 0x08
97 #define AC3_OUTPUT_LFEON 0x10
128 uint8_t cplfsnroffst;
130 uint8_t fsnroffst[5];
132 uint8_t lfefsnroffst;
139 uint8_t cpldeltoffst[8];
140 uint8_t cpldeltlen[8];
141 uint8_t cpldeltba[8];
143 uint8_t deltoffst[5][8];
144 uint8_t deltlen[5][8];
145 uint8_t deltba[5][8];
147 /* Derived Attributes. */
152 int nfchans; //number of channels
153 int lfeon; //lfe channel in use
155 float dynrng; //dynamic range gain
156 float dynrng2; //dynamic range gain for 1+1 mode
157 float chcoeffs[6]; //normalized channel coefficients
158 float cplco[5][18]; //coupling coordinates
159 int ncplbnd; //number of coupling bands
160 int ncplsubnd; //number of coupling sub bands
161 int cplstrtmant; //coupling start mantissa
162 int cplendmant; //coupling end mantissa
163 int endmant[5]; //channel end mantissas
165 uint8_t dcplexps[256]; //decoded coupling exponents
166 uint8_t dexps[5][256]; //decoded fbw channel exponents
167 uint8_t dlfeexps[256]; //decoded lfe channel exponents
168 uint8_t cplbap[256]; //coupling bit allocation pointers
169 uint8_t bap[5][256]; //fbw channel bit allocation pointers
170 uint8_t lfebap[256]; //lfe channel bit allocation pointers
172 int blkoutput; //output configuration for block
174 DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][BLOCK_SIZE]); //transform coefficients
177 MDCTContext imdct_512; //for 512 sample imdct transform
178 MDCTContext imdct_256; //for 256 sample imdct transform
179 DSPContext dsp; //for optimization
181 DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS][BLOCK_SIZE]); //output after imdct transform and windowing
182 DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS][BLOCK_SIZE]); //delay - added to the next block
183 DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]); //temporary storage for imdct transform
184 DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]); //temporary storage for output before windowing
185 DECLARE_ALIGNED_16(float, window[BLOCK_SIZE]); //window coefficients
189 AVRandomState dith_state; //for dither generation
192 /*********** BEGIN INIT HELPER FUNCTIONS ***********/
194 * Generate a Kaiser-Bessel Derived Window.
196 static void ac3_window_init(float *window)
199 double sum = 0.0, bessel, tmp;
200 double local_window[256];
201 double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
203 for (i = 0; i < 256; i++) {
204 tmp = i * (256 - i) * alpha2;
206 for (j = 100; j > 0; j--) /* defaul to 100 iterations */
207 bessel = bessel * tmp / (j * j) + 1;
209 local_window[i] = sum;
213 for (i = 0; i < 256; i++)
214 window[i] = sqrt(local_window[i] / sum);
218 * Generate quantizer tables.
220 static void generate_quantizers_table(int16_t quantizers[], int level, int length)
224 for (i = 0; i < length; i++)
225 quantizers[i] = ((2 * i - level + 1) << 15) / level;
228 static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
233 for (i = 0; i < length1; i++) {
234 v = ((2 * i - level + 1) << 15) / level;
235 for (j = 0; j < length2; j++)
236 quantizers[i * length2 + j] = v;
239 for (i = length1 * length2; i < size; i++)
243 static void generate_quantizers_table_2(int16_t quantizers[], int level, int length1, int length2, int size)
248 for (i = 0; i < length1; i++) {
249 v = ((2 * (i % level) - level + 1) << 15) / level;
250 for (j = 0; j < length2; j++)
251 quantizers[i * length2 + j] = v;
254 for (i = length1 * length2; i < size; i++)
259 static void generate_quantizers_table_3(int16_t quantizers[], int level, int length1, int length2, int size)
263 for (i = 0; i < length1; i++)
264 for (j = 0; j < length2; j++)
265 quantizers[i * length2 + j] = ((2 * (j % level) - level + 1) << 15) / level;
267 for (i = length1 * length2; i < size; i++)
272 * Initialize tables at runtime.
274 static void ac3_tables_init(void)
277 /* compute bndtab and masktab from bandsz */
283 for(j=0;j<v;j++) masktab[k++]=i;
286 masktab[253] = masktab[254] = masktab[255] = 0;
289 /* PSD Table For Mapping Exponents To PSD. */
290 for (i = 0; i < 25; i++)
291 psdtab[i] = 3072 - (i << 7);
293 /* Exponent Decoding Tables */
294 for (i = 0; i < 5; i++) {
296 for (j = 0; j < 25; j++)
297 exp_1[i * 25 + j] = v;
300 for (i = 0; i < 25; i++) {
302 for (j = 0; j < 5; j++)
303 exp_2[i * 5 + j] = v;
306 for (i = 0; i < 25; i++) {
308 for (j = 0; j < 5; j++)
309 exp_3[i * 5 + j] = v++;
312 for (i = 125; i < 128; i++)
313 exp_1[i] = exp_2[i] = exp_3[i] = 25;
314 /* End Exponent Decoding Tables */
316 /* Quantizer ungrouping tables. */
317 // for level-3 quantizers
318 generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
319 generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
320 generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
322 //for level-5 quantizers
323 generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
324 generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
325 generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
327 //for level-7 quantizers
328 generate_quantizers_table(l7_quantizers, 7, 7);
330 //for level-4 quantizers
331 generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
332 generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
334 //for level-15 quantizers
335 generate_quantizers_table(l15_quantizers, 15, 15);
336 /* End Quantizer ungrouping tables. */
338 //generate scale factors
339 for (i = 0; i < 25; i++)
340 scale_factors[i] = pow(2.0, -(i + 15));
344 static int ac3_decode_init(AVCodecContext *avctx)
346 AC3DecodeContext *ctx = avctx->priv_data;
350 ff_mdct_init(&ctx->imdct_256, 8, 1);
351 ff_mdct_init(&ctx->imdct_512, 9, 1);
352 ac3_window_init(ctx->window);
353 dsputil_init(&ctx->dsp, avctx);
354 av_init_random(0, &ctx->dith_state);
358 /*********** END INIT FUNCTIONS ***********/
360 /* Synchronize to ac3 bitstream.
361 * This function searches for the syncword '0xb77'.
363 * @param buf Pointer to "probable" ac3 bitstream buffer
364 * @param buf_size Size of buffer
365 * @return Returns the position where syncword is found, -1 if no syncword is found
367 static int ac3_synchronize(uint8_t *buf, int buf_size)
371 for (i = 0; i < buf_size - 1; i++)
372 if (buf[i] == 0x0b && buf[i + 1] == 0x77)
378 /* Parse the 'sync_info' from the ac3 bitstream.
379 * This function extracts the sync_info from ac3 bitstream.
380 * GetBitContext within AC3DecodeContext must point to
381 * start of the synchronized ac3 bitstream.
383 * @param ctx AC3DecodeContext
384 * @return Returns framesize, returns 0 if fscod, frmsizecod or bsid is not valid
386 static int ac3_parse_sync_info(AC3DecodeContext *ctx)
388 GetBitContext *gb = &ctx->gb;
389 int frmsizecod, bsid;
391 skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
392 ctx->crc1 = get_bits(gb, 16);
393 ctx->fscod = get_bits(gb, 2);
394 if (ctx->fscod == 0x03)
396 frmsizecod = get_bits(gb, 6);
397 if (frmsizecod >= 38)
399 ctx->sampling_rate = ff_ac3_freqs[ctx->fscod];
400 ctx->bit_rate = ff_ac3_bitratetab[frmsizecod >> 1];
402 /* we include it here in order to determine validity of ac3 frame */
403 bsid = get_bits(gb, 5);
406 skip_bits(gb, 3); //skip the bsmod, bsi->bsmod = get_bits(gb, 3);
408 switch (ctx->fscod) {
410 ctx->frame_size = 4 * ctx->bit_rate;
411 return ctx->frame_size;
413 ctx->frame_size = 2 * (320 * ctx->bit_rate / 147 + (frmsizecod & 1));
414 return ctx->frame_size;
416 ctx->frame_size = 6 * ctx->bit_rate;
417 return ctx->frame_size;
424 /* Parse bsi from ac3 bitstream.
425 * This function extracts the bitstream information (bsi) from ac3 bitstream.
427 * @param ctx AC3DecodeContext after processed by ac3_parse_sync_info
429 static void ac3_parse_bsi(AC3DecodeContext *ctx)
431 GetBitContext *gb = &ctx->gb;
438 ctx->cpldeltbae = DBA_NONE;
439 ctx->cpldeltnseg = 0;
440 for (i = 0; i < 5; i++) {
441 ctx->deltbae[i] = DBA_NONE;
442 ctx->deltnseg[i] = 0;
447 ctx->acmod = get_bits(gb, 3);
448 ctx->nfchans = nfchans_tbl[ctx->acmod];
450 if (ctx->acmod & 0x01 && ctx->acmod != 0x01)
451 ctx->cmixlev = get_bits(gb, 2);
452 if (ctx->acmod & 0x04)
453 ctx->surmixlev = get_bits(gb, 2);
454 if (ctx->acmod == 0x02)
455 ctx->dsurmod = get_bits(gb, 2);
457 ctx->lfeon = get_bits1(gb);
461 skip_bits(gb, 5); //skip dialog normalization
463 skip_bits(gb, 8); //skip compression
465 skip_bits(gb, 8); //skip language code
467 skip_bits(gb, 7); //skip audio production information
470 skip_bits(gb, 2); //skip copyright bit and original bitstream bit
473 skip_bits(gb, 14); //skip timecode1
475 skip_bits(gb, 14); //skip timecode2
478 i = get_bits(gb, 6); //additional bsi length
485 /* Decodes the grouped exponents.
486 * This function decodes the coded exponents according to exponent strategy
487 * and stores them in the decoded exponents buffer.
489 * @param gb GetBitContext which points to start of coded exponents
490 * @param expstr Exponent coding strategy
491 * @param ngrps Number of grouped exponetns
492 * @param absexp Absolute exponent
493 * @param dexps Decoded exponents are stored in dexps
494 * @return Returns 0 if exponents are decoded successfully, -1 if error occurs
496 static int decode_exponents(GetBitContext *gb, int expstr, int ngrps, uint8_t absexp, uint8_t *dexps)
501 exps = get_bits(gb, 7);
503 absexp += exp_1[exps];
505 av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
518 absexp += exp_2[exps];
520 av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
533 absexp += exp_3[exps];
535 av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
552 /*********** HELPER FUNCTIONS FOR BIT ALLOCATION ***********/
553 static inline int logadd(int a, int b)
558 address = FFMIN((ABS(c) >> 1), 255);
561 return (a + ff_ac3_latab[address]);
563 return (b + ff_ac3_latab[address]);
566 static inline int calc_lowcomp(int a, int b0, int b1, int bin)
569 if ((b0 + 256) == b1)
572 a = FFMAX(0, (a - 64));
575 if ((b0 + 256) == b1)
578 a = FFMAX(0, (a - 64));
581 a = FFMAX(0, (a - 128));
585 /*********** END HELPER FUNCTIONS FOR BIT ALLOCATION ***********/
587 /* Performs bit allocation.
588 * This function performs bit allocation for the requested chanenl.
590 static void do_bit_allocation(AC3DecodeContext *ctx, int chnl)
592 int16_t psd[256], bndpsd[50], excite[50], mask[50], delta;
593 int sdecay, fdecay, sgain, dbknee, floor;
594 int lowcomp = 0, fgain = 0, snroffset = 0, fastleak = 0, slowleak = 0, do_delta = 0;
595 int start = 0, end = 0, bin = 0, i = 0, j = 0, k = 0, lastbin = 0, bndstrt = 0;
596 int bndend = 0, begin = 0, deltnseg = 0, band = 0, seg = 0, address = 0;
597 int fscod = ctx->fscod;
598 uint8_t *deltoffst = 0, *deltlen = 0, *deltba = 0;
599 uint8_t *exps = 0, *bap = 0;
602 sdecay = ff_sdecaytab[ctx->sdcycod];
603 fdecay = ff_fdecaytab[ctx->fdcycod];
604 sgain = ff_sgaintab[ctx->sgaincod];
605 dbknee = ff_dbkneetab[ctx->dbpbcod];
606 floor = ff_floortab[ctx->floorcod];
609 start = ctx->cplstrtmant;
610 end = ctx->cplendmant;
611 fgain = ff_fgaintab[ctx->cplfgaincod];
612 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->cplfsnroffst) << 2;
613 fastleak = (ctx->cplfleak << 8) + 768;
614 slowleak = (ctx->cplsleak << 8) + 768;
615 exps = ctx->dcplexps;
617 if (ctx->cpldeltbae == DBA_NEW || ctx->deltbae == DBA_REUSE) {
619 deltnseg = ctx->cpldeltnseg;
620 deltoffst = ctx->cpldeltoffst;
621 deltlen = ctx->cpldeltlen;
622 deltba = ctx->cpldeltba;
625 else if (chnl == 6) {
631 fgain = ff_fgaintab[ctx->lfefgaincod];
632 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->lfefsnroffst) << 2;
633 exps = ctx->dlfeexps;
638 end = ctx->endmant[chnl];
642 fgain = ff_fgaintab[ctx->fgaincod[chnl]];
643 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->fsnroffst[chnl]) << 2;
644 exps = ctx->dexps[chnl];
645 bap = ctx->bap[chnl];
646 if (ctx->deltbae[chnl] == DBA_NEW || ctx->deltbae[chnl] == DBA_REUSE) {
648 deltnseg = ctx->deltnseg[chnl];
649 deltoffst = ctx->deltoffst[chnl];
650 deltlen = ctx->deltlen[chnl];
651 deltba = ctx->deltba[chnl];
655 for (bin = start; bin < end; bin++) /* exponent mapping into psd */
656 psd[bin] = psdtab[exps[bin]];
658 /* psd integration */
662 lastbin = FFMIN((bndtab[k] + ff_ac3_bndsz[k]), end);
665 for (i = j; i < lastbin; i++) {
666 bndpsd[k] = logadd(bndpsd[k], psd[j]);
670 } while (end > lastbin);
672 /* compute the excite function */
673 bndstrt = masktab[start];
674 bndend = masktab[end - 1] + 1;
676 lowcomp = calc_lowcomp(lowcomp, bndpsd[0], bndpsd[1], 0);
677 excite[0] = bndpsd[0] - fgain - lowcomp;
678 lowcomp = calc_lowcomp(lowcomp, bndpsd[1], bndpsd[2], 1);
679 excite[1] = bndpsd[1] - fgain - lowcomp;
681 for (bin = 2; bin < 7; bin++) {
682 if ((bndend != 7) || (bin != 6))
683 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
684 fastleak = bndpsd[bin] - fgain;
685 slowleak = bndpsd[bin] - sgain;
686 excite[bin] = fastleak - lowcomp;
687 if ((bndend != 7) || (bin != 6))
688 if (bndpsd[bin] <= bndpsd[bin + 1]) {
693 for (bin = begin; bin < FFMIN(bndend, 22); bin++) {
694 if ((bndend != 7) || (bin != 6))
695 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
697 fastleak = FFMAX(fastleak, (bndpsd[bin] - fgain));
699 slowleak = FFMAX(slowleak, (bndpsd[bin] - sgain));
700 excite[bin] = FFMAX((fastleak - lowcomp), slowleak);
707 for (bin = begin; bin < bndend; bin++) {
709 fastleak = FFMAX(fastleak, (bndpsd[bin] - fgain));
711 slowleak = FFMAX(slowleak, (bndpsd[bin] - sgain));
712 excite[bin] = FFMAX(fastleak, slowleak);
715 /* compute the masking curve */
716 for (bin = bndstrt; bin < bndend; bin++) {
717 if (bndpsd[bin] < dbknee)
718 excite[bin] += ((dbknee - bndpsd[bin]) >> 2);
719 mask[bin] = FFMAX(excite[bin], ff_ac3_hth[bin][fscod]);
722 /* apply the delta bit allocation */
725 for (seg = 0; seg < deltnseg + 1; seg++) {
726 band += deltoffst[seg];
727 if (deltba[seg] >= 4)
728 delta = (deltba[seg] - 3) << 7;
730 delta = (deltba[seg] - 4) << 7;
731 for (k = 0; k < deltlen[seg]; k++) {
738 /*compute the bit allocation */
742 lastbin = FFMIN((bndtab[j] + ff_ac3_bndsz[j]), end);
743 mask[j] -= snroffset;
749 for (k = i; k < lastbin; k++) {
750 address = (psd[i] - mask[j]) >> 5;
751 address = FFMIN(63, (FFMAX(0, address)));
752 bap[i] = ff_ac3_baptab[address];
756 } while (end > lastbin);
759 /* Check if snroffsets are zero. */
760 static int is_snr_offsets_zero(AC3DecodeContext *ctx)
764 if ((ctx->csnroffst) || (ctx->cplinu && ctx->cplfsnroffst) ||
765 (ctx->lfeon && ctx->lfefsnroffst))
768 for (i = 0; i < ctx->nfchans; i++)
769 if (ctx->fsnroffst[i])
775 typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
776 int16_t l3_quantizers[3];
777 int16_t l5_quantizers[3];
778 int16_t l11_quantizers[2];
784 #define TRANSFORM_COEFF(tc, m, e, f) (tc) = (m) * (f)[(e)]
786 /* Get the transform coefficients for coupling channel and uncouple channels.
787 * The coupling transform coefficients starts at the the cplstrtmant, which is
788 * equal to endmant[ch] for fbw channels. Hence we can uncouple channels before
789 * getting transform coefficients for the channel.
791 static int get_transform_coeffs_cpling(AC3DecodeContext *ctx, mant_groups *m)
793 GetBitContext *gb = &ctx->gb;
794 int ch, start, end, cplbndstrc, bnd, gcode, tbap;
795 float cplcos[5], cplcoeff;
796 uint8_t *exps = ctx->dcplexps;
797 uint8_t *bap = ctx->cplbap;
799 cplbndstrc = ctx->cplbndstrc;
800 start = ctx->cplstrtmant;
803 while (start < ctx->cplendmant) {
805 while (cplbndstrc & 1) {
810 for (ch = 0; ch < ctx->nfchans; ch++)
811 cplcos[ch] = ctx->chcoeffs[ch] * ctx->cplco[ch][bnd];
814 while (start < end) {
818 for (ch = 0; ch < ctx->nfchans; ch++)
819 if (((ctx->chincpl) >> ch) & 1) {
820 if ((ctx->dithflag >> ch) & 1) {
821 TRANSFORM_COEFF(cplcoeff, av_random(&ctx->dith_state) & 0xFFFF, exps[start], scale_factors);
822 ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch] * LEVEL_MINUS_3DB;
824 ctx->transform_coeffs[ch + 1][start] = 0;
830 gcode = get_bits(gb, 5);
831 m->l3_quantizers[0] = l3_quantizers_1[gcode];
832 m->l3_quantizers[1] = l3_quantizers_2[gcode];
833 m->l3_quantizers[2] = l3_quantizers_3[gcode];
836 TRANSFORM_COEFF(cplcoeff, m->l3_quantizers[m->l3ptr++], exps[start], scale_factors);
841 gcode = get_bits(gb, 7);
842 m->l5_quantizers[0] = l5_quantizers_1[gcode];
843 m->l5_quantizers[1] = l5_quantizers_2[gcode];
844 m->l5_quantizers[2] = l5_quantizers_3[gcode];
847 TRANSFORM_COEFF(cplcoeff, m->l5_quantizers[m->l5ptr++], exps[start], scale_factors);
851 TRANSFORM_COEFF(cplcoeff, l7_quantizers[get_bits(gb, 3)], exps[start], scale_factors);
856 gcode = get_bits(gb, 7);
857 m->l11_quantizers[0] = l11_quantizers_1[gcode];
858 m->l11_quantizers[1] = l11_quantizers_2[gcode];
861 TRANSFORM_COEFF(cplcoeff, m->l11_quantizers[m->l11ptr++], exps[start], scale_factors);
865 TRANSFORM_COEFF(cplcoeff, l15_quantizers[get_bits(gb, 4)], exps[start], scale_factors);
869 TRANSFORM_COEFF(cplcoeff, get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]),
870 exps[start], scale_factors);
872 for (ch = 0; ch < ctx->nfchans; ch++)
873 if ((ctx->chincpl >> ch) & 1)
874 ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
882 /* Get the transform coefficients for particular channel */
883 static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
885 GetBitContext *gb = &ctx->gb;
886 int i, gcode, tbap, dithflag, end;
892 for (i = 0; i < 25; i++)
893 factors[i] = scale_factors[i] * ctx->chcoeffs[ch_index];
895 if (ch_index != -1) { /* fbw channels */
896 dithflag = (ctx->dithflag >> ch_index) & 1;
897 exps = ctx->dexps[ch_index];
898 bap = ctx->bap[ch_index];
899 coeffs = ctx->transform_coeffs[ch_index + 1];
900 end = ctx->endmant[ch_index];
901 } else if (ch_index == -1) {
903 exps = ctx->dlfeexps;
905 coeffs = ctx->transform_coeffs[0];
910 for (i = 0; i < end; i++) {
919 TRANSFORM_COEFF(coeffs[i], av_random(&ctx->dith_state) & 0xFFFF, exps[i], factors);
920 coeffs[i] *= LEVEL_MINUS_3DB;
926 gcode = get_bits(gb, 5);
927 m->l3_quantizers[0] = l3_quantizers_1[gcode];
928 m->l3_quantizers[1] = l3_quantizers_2[gcode];
929 m->l3_quantizers[2] = l3_quantizers_3[gcode];
932 TRANSFORM_COEFF(coeffs[i], m->l3_quantizers[m->l3ptr++], exps[i], factors);
937 gcode = get_bits(gb, 7);
938 m->l5_quantizers[0] = l5_quantizers_1[gcode];
939 m->l5_quantizers[1] = l5_quantizers_2[gcode];
940 m->l5_quantizers[2] = l5_quantizers_3[gcode];
943 TRANSFORM_COEFF(coeffs[i], m->l5_quantizers[m->l5ptr++], exps[i], factors);
947 TRANSFORM_COEFF(coeffs[i], l7_quantizers[get_bits(gb, 3)], exps[i], factors);
952 gcode = get_bits(gb, 7);
953 m->l11_quantizers[0] = l11_quantizers_1[gcode];
954 m->l11_quantizers[1] = l11_quantizers_2[gcode];
957 TRANSFORM_COEFF(coeffs[i], m->l11_quantizers[m->l11ptr++], exps[i], factors);
961 TRANSFORM_COEFF(coeffs[i], l15_quantizers[get_bits(gb, 4)], exps[i], factors);
965 TRANSFORM_COEFF(coeffs[i], get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]), exps[i], factors);
973 /* Get the transform coefficients.
974 * This function extracts the tranform coefficients form the ac3 bitstream.
975 * This function is called after bit allocation is performed.
977 static int get_transform_coeffs(AC3DecodeContext * ctx)
983 m.l3ptr = m.l5ptr = m.l11ptr = 3;
985 for (i = 0; i < ctx->nfchans; i++) {
986 /* transform coefficients for individual channel */
987 if (get_transform_coeffs_ch(ctx, i, &m))
989 /* tranform coefficients for coupling channels */
990 if ((ctx->chincpl >> i) & 1) {
992 if (get_transform_coeffs_cpling(ctx, &m)) {
993 av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
998 end = ctx->cplendmant;
1000 end = ctx->endmant[i];
1002 ctx->transform_coeffs[i + 1][end] = 0;
1006 if (get_transform_coeffs_ch(ctx, -1, &m))
1008 for (i = 7; i < 256; i++) {
1009 ctx->transform_coeffs[0][i] = 0;
1016 /* Rematrixing routines. */
1017 static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
1021 while (start < end) {
1022 tmp0 = ctx->transform_coeffs[1][start];
1023 tmp1 = ctx->transform_coeffs[2][start];
1024 ctx->transform_coeffs[1][start] = tmp0 + tmp1;
1025 ctx->transform_coeffs[2][start] = tmp0 - tmp1;
1030 static void do_rematrixing(AC3DecodeContext *ctx)
1032 int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
1035 end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
1037 if (ctx->rematflg & 1)
1038 do_rematrixing1(ctx, bnd1, bnd2);
1040 if (ctx->rematflg & 2)
1041 do_rematrixing1(ctx, bnd2, bnd3);
1046 if (ctx->rematflg & 4)
1047 do_rematrixing1(ctx, bnd3, bndend);
1049 if (ctx->rematflg & 4)
1050 do_rematrixing1(ctx, bnd3, bnd4);
1051 if (ctx->rematflg & 8)
1052 do_rematrixing1(ctx, bnd4, end);
1056 /* This function sets the normalized channel coefficients.
1057 * Transform coefficients are multipllied by the channel
1058 * coefficients to get normalized transform coefficients.
1060 static void get_downmix_coeffs(AC3DecodeContext *ctx)
1062 int from = ctx->acmod;
1063 int to = ctx->blkoutput;
1064 float clev = clevs[ctx->cmixlev];
1065 float slev = slevs[ctx->surmixlev];
1066 float nf = 1.0; //normalization factor for downmix coeffs
1070 ctx->chcoeffs[0] = 2 * ctx->dynrng;
1071 ctx->chcoeffs[1] = 2 * ctx->dynrng2;
1073 for (i = 0; i < ctx->nfchans; i++)
1074 ctx->chcoeffs[i] = 2 * ctx->dynrng;
1077 if (to == AC3_OUTPUT_UNMODIFIED)
1081 case AC3_ACMOD_DUALMONO:
1083 case AC3_OUTPUT_MONO:
1084 case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */
1086 ctx->chcoeffs[0] *= nf;
1087 ctx->chcoeffs[1] *= nf;
1091 case AC3_ACMOD_MONO:
1093 case AC3_OUTPUT_STEREO:
1094 nf = LEVEL_MINUS_3DB;
1095 ctx->chcoeffs[0] *= nf;
1099 case AC3_ACMOD_STEREO:
1101 case AC3_OUTPUT_MONO:
1102 nf = LEVEL_MINUS_3DB;
1103 ctx->chcoeffs[0] *= nf;
1104 ctx->chcoeffs[1] *= nf;
1110 case AC3_OUTPUT_MONO:
1111 nf = LEVEL_MINUS_3DB / (1.0 + clev);
1112 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1113 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1114 ctx->chcoeffs[1] *= ((nf * clev * LEVEL_MINUS_3DB) / 2.0);
1116 case AC3_OUTPUT_STEREO:
1117 nf = 1.0 / (1.0 + clev);
1118 ctx->chcoeffs[0] *= nf;
1119 ctx->chcoeffs[2] *= nf;
1120 ctx->chcoeffs[1] *= (nf * clev);
1124 case AC3_ACMOD_2F1R:
1126 case AC3_OUTPUT_MONO:
1127 nf = 2.0 * LEVEL_MINUS_3DB / (2.0 + slev);
1128 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1129 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1130 ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
1132 case AC3_OUTPUT_STEREO:
1133 nf = 1.0 / (1.0 + (slev * LEVEL_MINUS_3DB));
1134 ctx->chcoeffs[0] *= nf;
1135 ctx->chcoeffs[1] *= nf;
1136 ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
1138 case AC3_OUTPUT_DOLBY:
1139 nf = 1.0 / (1.0 + LEVEL_MINUS_3DB);
1140 ctx->chcoeffs[0] *= nf;
1141 ctx->chcoeffs[1] *= nf;
1142 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1146 case AC3_ACMOD_3F1R:
1148 case AC3_OUTPUT_MONO:
1149 nf = LEVEL_MINUS_3DB / (1.0 + clev + (slev / 2.0));
1150 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1151 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1152 ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
1153 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1155 case AC3_OUTPUT_STEREO:
1156 nf = 1.0 / (1.0 + clev + (slev * LEVEL_MINUS_3DB));
1157 ctx->chcoeffs[0] *= nf;
1158 ctx->chcoeffs[2] *= nf;
1159 ctx->chcoeffs[1] *= (nf * clev);
1160 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1162 case AC3_OUTPUT_DOLBY:
1163 nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
1164 ctx->chcoeffs[0] *= nf;
1165 ctx->chcoeffs[1] *= nf;
1166 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1167 ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
1171 case AC3_ACMOD_2F2R:
1173 case AC3_OUTPUT_MONO:
1174 nf = LEVEL_MINUS_3DB / (1.0 + slev);
1175 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1176 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1177 ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
1178 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1180 case AC3_OUTPUT_STEREO:
1181 nf = 1.0 / (1.0 + slev);
1182 ctx->chcoeffs[0] *= nf;
1183 ctx->chcoeffs[1] *= nf;
1184 ctx->chcoeffs[2] *= (nf * slev);
1185 ctx->chcoeffs[3] *= (nf * slev);
1187 case AC3_OUTPUT_DOLBY:
1188 nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
1189 ctx->chcoeffs[0] *= nf;
1190 ctx->chcoeffs[1] *= nf;
1191 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1192 ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
1196 case AC3_ACMOD_3F2R:
1198 case AC3_OUTPUT_MONO:
1199 nf = LEVEL_MINUS_3DB / (1.0 + clev + slev);
1200 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
1201 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
1202 ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
1203 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
1204 ctx->chcoeffs[4] *= (nf * slev * LEVEL_MINUS_3DB);
1206 case AC3_OUTPUT_STEREO:
1207 nf = 1.0 / (1.0 + clev + slev);
1208 ctx->chcoeffs[0] *= nf;
1209 ctx->chcoeffs[2] *= nf;
1210 ctx->chcoeffs[1] *= (nf * clev);
1211 ctx->chcoeffs[3] *= (nf * slev);
1212 ctx->chcoeffs[4] *= (nf * slev);
1214 case AC3_OUTPUT_DOLBY:
1215 nf = 1.0 / (1.0 + (3.0 * LEVEL_MINUS_3DB));
1216 ctx->chcoeffs[0] *= nf;
1217 ctx->chcoeffs[1] *= nf;
1218 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
1219 ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
1220 ctx->chcoeffs[4] *= (nf * LEVEL_MINUS_3DB);
1227 /*********** BEGIN DOWNMIX FUNCTIONS ***********/
1228 static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx)
1231 float (*output)[BLOCK_SIZE] = ctx->output;
1233 for (i = 0; i < 256; i++)
1234 output[1][i] += output[2][i];
1235 memset(output[2], 0, sizeof(output[2]));
1238 static inline void mix_dualmono_to_stereo(AC3DecodeContext *ctx)
1242 float (*output)[BLOCK_SIZE] = ctx->output;
1244 for (i = 0; i < 256; i++) {
1245 tmp = output[1][i] + output[2][i];
1246 output[1][i] = output[2][i] = tmp;
1250 static inline void upmix_mono_to_stereo(AC3DecodeContext *ctx)
1253 float (*output)[BLOCK_SIZE] = ctx->output;
1255 for (i = 0; i < 256; i++)
1256 output[2][i] = output[1][i];
1259 static inline void mix_stereo_to_mono(AC3DecodeContext *ctx)
1262 float (*output)[BLOCK_SIZE] = ctx->output;
1264 for (i = 0; i < 256; i++)
1265 output[1][i] += output[2][i];
1266 memset(output[2], 0, sizeof(output[2]));
1269 static inline void mix_3f_to_mono(AC3DecodeContext *ctx)
1272 float (*output)[BLOCK_SIZE] = ctx->output;
1274 for (i = 0; i < 256; i++)
1275 output[1][i] += (output[2][i] + output[3][i]);
1276 memset(output[2], 0, sizeof(output[2]));
1277 memset(output[3], 0, sizeof(output[3]));
1280 static inline void mix_3f_to_stereo(AC3DecodeContext *ctx)
1283 float (*output)[BLOCK_SIZE] = ctx->output;
1285 for (i = 0; i < 256; i++) {
1286 output[1][i] += output[2][i];
1287 output[2][i] += output[3][i];
1289 memset(output[3], 0, sizeof(output[3]));
1292 static inline void mix_2f_1r_to_mono(AC3DecodeContext *ctx)
1295 float (*output)[BLOCK_SIZE] = ctx->output;
1297 for (i = 0; i < 256; i++)
1298 output[1][i] += (output[2][i] + output[3][i]);
1299 memset(output[2], 0, sizeof(output[2]));
1300 memset(output[3], 0, sizeof(output[3]));
1304 static inline void mix_2f_1r_to_stereo(AC3DecodeContext *ctx)
1307 float (*output)[BLOCK_SIZE] = ctx->output;
1309 for (i = 0; i < 256; i++) {
1310 output[1][i] += output[2][i];
1311 output[2][i] += output[3][i];
1313 memset(output[3], 0, sizeof(output[3]));
1316 static inline void mix_2f_1r_to_dolby(AC3DecodeContext *ctx)
1319 float (*output)[BLOCK_SIZE] = ctx->output;
1321 for (i = 0; i < 256; i++) {
1322 output[1][i] -= output[3][i];
1323 output[2][i] += output[3][i];
1325 memset(output[3], 0, sizeof(output[3]));
1328 static inline void mix_3f_1r_to_mono(AC3DecodeContext *ctx)
1331 float (*output)[BLOCK_SIZE] = ctx->output;
1333 for (i = 0; i < 256; i++)
1334 output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1335 memset(output[2], 0, sizeof(output[2]));
1336 memset(output[3], 0, sizeof(output[3]));
1337 memset(output[4], 0, sizeof(output[4]));
1340 static inline void mix_3f_1r_to_stereo(AC3DecodeContext *ctx)
1343 float (*output)[BLOCK_SIZE] = ctx->output;
1345 for (i = 0; i < 256; i++) {
1346 output[1][i] += (output[2][i] + output[4][i]);
1347 output[2][i] += (output[3][i] + output[4][i]);
1349 memset(output[3], 0, sizeof(output[3]));
1350 memset(output[4], 0, sizeof(output[4]));
1353 static inline void mix_3f_1r_to_dolby(AC3DecodeContext *ctx)
1356 float (*output)[BLOCK_SIZE] = ctx->output;
1358 for (i = 0; i < 256; i++) {
1359 output[1][i] += (output[2][i] - output[4][i]);
1360 output[2][i] += (output[3][i] + output[4][i]);
1362 memset(output[3], 0, sizeof(output[3]));
1363 memset(output[4], 0, sizeof(output[4]));
1366 static inline void mix_2f_2r_to_mono(AC3DecodeContext *ctx)
1369 float (*output)[BLOCK_SIZE] = ctx->output;
1371 for (i = 0; i < 256; i++)
1372 output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1373 memset(output[2], 0, sizeof(output[2]));
1374 memset(output[3], 0, sizeof(output[3]));
1375 memset(output[4], 0, sizeof(output[4]));
1378 static inline void mix_2f_2r_to_stereo(AC3DecodeContext *ctx)
1381 float (*output)[BLOCK_SIZE] = ctx->output;
1383 for (i = 0; i < 256; i++) {
1384 output[1][i] += output[3][i];
1385 output[2][i] += output[4][i];
1387 memset(output[3], 0, sizeof(output[3]));
1388 memset(output[4], 0, sizeof(output[4]));
1391 static inline void mix_2f_2r_to_dolby(AC3DecodeContext *ctx)
1394 float (*output)[BLOCK_SIZE] = ctx->output;
1396 for (i = 0; i < 256; i++) {
1397 output[1][i] -= output[3][i];
1398 output[2][i] += output[4][i];
1400 memset(output[3], 0, sizeof(output[3]));
1401 memset(output[4], 0, sizeof(output[4]));
1404 static inline void mix_3f_2r_to_mono(AC3DecodeContext *ctx)
1407 float (*output)[BLOCK_SIZE] = ctx->output;
1409 for (i = 0; i < 256; i++)
1410 output[1][i] += (output[2][i] + output[3][i] + output[4][i] + output[5][i]);
1411 memset(output[2], 0, sizeof(output[2]));
1412 memset(output[3], 0, sizeof(output[3]));
1413 memset(output[4], 0, sizeof(output[4]));
1414 memset(output[5], 0, sizeof(output[5]));
1417 static inline void mix_3f_2r_to_stereo(AC3DecodeContext *ctx)
1420 float (*output)[BLOCK_SIZE] = ctx->output;
1422 for (i = 0; i < 256; i++) {
1423 output[1][i] += (output[2][i] + output[4][i]);
1424 output[2][i] += (output[3][i] + output[5][i]);
1426 memset(output[3], 0, sizeof(output[3]));
1427 memset(output[4], 0, sizeof(output[4]));
1428 memset(output[5], 0, sizeof(output[5]));
1431 static inline void mix_3f_2r_to_dolby(AC3DecodeContext *ctx)
1434 float (*output)[BLOCK_SIZE] = ctx->output;
1436 for (i = 0; i < 256; i++) {
1437 output[1][i] += (output[2][i] - output[4][i] - output[5][i]);
1438 output[2][i] += (output[3][i] + output[4][i] + output[5][i]);
1440 memset(output[3], 0, sizeof(output[3]));
1441 memset(output[4], 0, sizeof(output[4]));
1442 memset(output[5], 0, sizeof(output[5]));
1444 /*********** END DOWNMIX FUNCTIONS ***********/
1446 /* Downmix the output.
1447 * This function downmixes the output when the number of input
1448 * channels is not equal to the number of output channels requested.
1450 static void do_downmix(AC3DecodeContext *ctx)
1452 int from = ctx->acmod;
1453 int to = ctx->blkoutput;
1455 if (to == AC3_OUTPUT_UNMODIFIED)
1459 case AC3_ACMOD_DUALMONO:
1461 case AC3_OUTPUT_MONO:
1462 mix_dualmono_to_mono(ctx);
1464 case AC3_OUTPUT_STEREO: /* We assume that sum of both mono channels is requested */
1465 mix_dualmono_to_stereo(ctx);
1469 case AC3_ACMOD_MONO:
1471 case AC3_OUTPUT_STEREO:
1472 upmix_mono_to_stereo(ctx);
1476 case AC3_ACMOD_STEREO:
1478 case AC3_OUTPUT_MONO:
1479 mix_stereo_to_mono(ctx);
1485 case AC3_OUTPUT_MONO:
1486 mix_3f_to_mono(ctx);
1488 case AC3_OUTPUT_STEREO:
1489 mix_3f_to_stereo(ctx);
1493 case AC3_ACMOD_2F1R:
1495 case AC3_OUTPUT_MONO:
1496 mix_2f_1r_to_mono(ctx);
1498 case AC3_OUTPUT_STEREO:
1499 mix_2f_1r_to_stereo(ctx);
1501 case AC3_OUTPUT_DOLBY:
1502 mix_2f_1r_to_dolby(ctx);
1506 case AC3_ACMOD_3F1R:
1508 case AC3_OUTPUT_MONO:
1509 mix_3f_1r_to_mono(ctx);
1511 case AC3_OUTPUT_STEREO:
1512 mix_3f_1r_to_stereo(ctx);
1514 case AC3_OUTPUT_DOLBY:
1515 mix_3f_1r_to_dolby(ctx);
1519 case AC3_ACMOD_2F2R:
1521 case AC3_OUTPUT_MONO:
1522 mix_2f_2r_to_mono(ctx);
1524 case AC3_OUTPUT_STEREO:
1525 mix_2f_2r_to_stereo(ctx);
1527 case AC3_OUTPUT_DOLBY:
1528 mix_2f_2r_to_dolby(ctx);
1532 case AC3_ACMOD_3F2R:
1534 case AC3_OUTPUT_MONO:
1535 mix_3f_2r_to_mono(ctx);
1537 case AC3_OUTPUT_STEREO:
1538 mix_3f_2r_to_stereo(ctx);
1540 case AC3_OUTPUT_DOLBY:
1541 mix_3f_2r_to_dolby(ctx);
1548 static void dump_floats(const char *name, int prec, const float *tab, int n)
1552 av_log(NULL, AV_LOG_INFO, "%s[%d]:\n", name, n);
1555 av_log(NULL, AV_LOG_INFO, "%4d: ", i);
1556 av_log(NULL, AV_LOG_INFO, " %8.*f", prec, tab[i]);
1558 av_log(NULL, AV_LOG_INFO, "\n");
1561 av_log(NULL, AV_LOG_INFO, "\n");
1564 /* This function performs the imdct on 256 sample transform
1567 static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
1570 float x1[128], x2[128];
1571 float *o_ptr, *d_ptr, *w;
1572 FFTComplex *ptr1, *ptr2;
1574 for (k = 0; k < N / 4; k++) {
1575 x1[k] = ctx->transform_coeffs[chindex][2 * k];
1576 x2[k] = ctx->transform_coeffs[chindex][2 * k + 1];
1579 ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, ctx->tmp_output, x1, ctx->tmp_imdct);
1580 ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, ctx->tmp_output + 256, x2, ctx->tmp_imdct);
1582 o_ptr = ctx->output[chindex];
1583 d_ptr = ctx->delay[chindex];
1584 ptr1 = (FFTComplex *)ctx->tmp_output;
1585 ptr2 = (FFTComplex *)ctx->tmp_output + 256;
1588 for (k = 0; k < N / 8; k++)
1590 o_ptr[2 * k] = -ptr1[k].im * w[2 * k] + d_ptr[2 * k] + 384.0;
1591 o_ptr[2 * k + 1] = ptr1[N / 8 - k - 1].re * w[2 * k + 1] + 384.0;
1592 o_ptr[N / 4 + 2 * k] = -ptr1[k].re * w[N / 4 + 2 * k] + d_ptr[N / 4 + 2 * k] + 384.0;
1593 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;
1594 d_ptr[2 * k] = ptr2[k].re * w[k / 2 - 2 * k - 1];
1595 d_ptr[2 * k + 1] = -ptr2[N / 8 - k - 1].im * w[N / 2 - 2 * k - 2];
1596 d_ptr[N / 4 + 2 * k] = ptr2[k].im * w[N / 4 - 2 * k - 1];
1597 d_ptr[N / 4 + 2 * k + 1] = -ptr2[N / 8 - k - 1].re * w[N / 4 - 2 * k - 2];
1601 /* This function performs the imdct on 512 sample transform
1604 static void do_imdct_512(AC3DecodeContext *ctx, int chindex)
1608 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1609 ctx->transform_coeffs[chindex], ctx->tmp_imdct);
1610 ptr = ctx->output[chindex];
1611 ctx->dsp.vector_fmul_add_add(ptr, ctx->tmp_output, ctx->window, ctx->delay[chindex], 384, BLOCK_SIZE, 1);
1612 ptr = ctx->delay[chindex];
1613 ctx->dsp.vector_fmul_reverse(ptr, ctx->tmp_output + 256, ctx->window, BLOCK_SIZE);
1616 /* IMDCT Transform. */
1617 static inline void do_imdct(AC3DecodeContext *ctx)
1621 if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
1622 do_imdct_512(ctx, 0);
1624 for (i = 0; i < ctx->nfchans; i++) {
1625 if ((ctx->blksw >> i) & 1)
1626 do_imdct_256(ctx, i + 1);
1628 do_imdct_512(ctx, i + 1);
1632 /* Parse the audio block from ac3 bitstream.
1633 * This function extract the audio block from the ac3 bitstream
1634 * and produces the output for the block. This function must
1635 * be called for each of the six audio block in the ac3 bitstream.
1637 static int ac3_parse_audio_block(AC3DecodeContext * ctx)
1639 int nfchans = ctx->nfchans;
1640 int acmod = ctx->acmod;
1641 int i, bnd, rbnd, seg, grpsize;
1642 GetBitContext *gb = &ctx->gb;
1643 int bit_alloc_flags = 0;
1645 int mstrcplco, cplcoexp, cplcomant;
1646 int dynrng, chbwcod, ngrps, cplabsexp, skipl;
1649 for (i = 0; i < nfchans; i++) /*block switch flag */
1650 ctx->blksw |= get_bits1(gb) << i;
1653 for (i = 0; i < nfchans; i++) /* dithering flag */
1654 ctx->dithflag |= get_bits1(gb) << i;
1656 if (get_bits1(gb)) { /* dynamic range */
1657 dynrng = get_sbits(gb, 8);
1658 ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1661 if (acmod == 0x00 && get_bits1(gb)) { /* dynamic range 1+1 mode */
1662 dynrng = get_sbits(gb, 8);
1663 ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1666 get_downmix_coeffs(ctx);
1668 if (get_bits1(gb)) { /* coupling strategy */
1669 ctx->cplinu = get_bits1(gb);
1670 ctx->cplbndstrc = 0;
1672 if (ctx->cplinu) { /* coupling in use */
1673 for (i = 0; i < nfchans; i++)
1674 ctx->chincpl |= get_bits1(gb) << i;
1677 ctx->phsflginu = get_bits1(gb); //phase flag in use
1679 ctx->cplbegf = get_bits(gb, 4);
1680 ctx->cplendf = get_bits(gb, 4);
1682 if (3 + ctx->cplendf - ctx->cplbegf < 0) {
1683 av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", ctx->cplendf, ctx->cplbegf);
1687 ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf;
1688 ctx->cplstrtmant = ctx->cplbegf * 12 + 37;
1689 ctx->cplendmant = ctx->cplendf * 12 + 73;
1690 for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
1691 if (get_bits1(gb)) {
1692 ctx->cplbndstrc |= 1 << i;
1701 for (i = 0; i < nfchans; i++)
1702 if ((ctx->chincpl) >> i & 1)
1703 if (get_bits1(gb)) { /* coupling co-ordinates */
1704 ctx->cplcoe |= 1 << i;
1705 mstrcplco = 3 * get_bits(gb, 2);
1706 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
1707 cplcoexp = get_bits(gb, 4);
1708 cplcomant = get_bits(gb, 4);
1712 cplcomant = (cplcomant | 0x10) << 13;
1713 ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
1717 if (acmod == 0x02 && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
1718 for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
1720 ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
1723 if (acmod == 0x02) {/* rematrixing */
1724 ctx->rematstr = get_bits1(gb);
1725 if (ctx->rematstr) {
1728 if (!(ctx->cplinu) || ctx->cplbegf > 2)
1729 for (rbnd = 0; rbnd < 4; rbnd++)
1730 ctx->rematflg |= get_bits1(gb) << rbnd;
1731 if (ctx->cplbegf > 0 && ctx->cplbegf <= 2 && ctx->cplinu)
1732 for (rbnd = 0; rbnd < 3; rbnd++)
1733 ctx->rematflg |= get_bits1(gb) << rbnd;
1734 if (ctx->cplbegf == 0 && ctx->cplinu)
1735 for (rbnd = 0; rbnd < 2; rbnd++)
1736 ctx->rematflg |= get_bits1(gb) << rbnd;
1740 ctx->cplexpstr = EXP_REUSE;
1741 ctx->lfeexpstr = EXP_REUSE;
1742 if (ctx->cplinu) /* coupling exponent strategy */
1743 ctx->cplexpstr = get_bits(gb, 2);
1744 for (i = 0; i < nfchans; i++) /* channel exponent strategy */
1745 ctx->chexpstr[i] = get_bits(gb, 2);
1746 if (ctx->lfeon) /* lfe exponent strategy */
1747 ctx->lfeexpstr = get_bits1(gb);
1749 for (i = 0; i < nfchans; i++) /* channel bandwidth code */
1750 if (ctx->chexpstr[i] != EXP_REUSE) {
1751 if ((ctx->chincpl >> i) & 1)
1752 ctx->endmant[i] = ctx->cplstrtmant;
1754 chbwcod = get_bits(gb, 6);
1756 av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
1759 ctx->endmant[i] = chbwcod * 3 + 73;
1763 if (ctx->cplexpstr != EXP_REUSE) {/* coupling exponents */
1764 bit_alloc_flags = 64;
1765 cplabsexp = get_bits(gb, 4) << 1;
1766 ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
1767 if (decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant)) {
1768 av_log(NULL, AV_LOG_ERROR, "error decoding coupling exponents\n");
1773 for (i = 0; i < nfchans; i++) /* fbw channel exponents */
1774 if (ctx->chexpstr[i] != EXP_REUSE) {
1775 bit_alloc_flags |= 1 << i;
1776 grpsize = 3 << (ctx->chexpstr[i] - 1);
1777 ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
1778 dexps = ctx->dexps[i];
1779 dexps[0] = get_bits(gb, 4);
1780 if (decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1)) {
1781 av_log(NULL, AV_LOG_ERROR, "error decoding channel %d exponents\n", i);
1784 skip_bits(gb, 2); /* skip gainrng */
1787 if (ctx->lfeexpstr != EXP_REUSE) { /* lfe exponents */
1788 bit_alloc_flags |= 32;
1789 ctx->dlfeexps[0] = get_bits(gb, 4);
1790 if (decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1)) {
1791 av_log(NULL, AV_LOG_ERROR, "error decoding lfe exponents\n");
1796 if (get_bits1(gb)) { /* bit allocation information */
1797 bit_alloc_flags = 127;
1798 ctx->sdcycod = get_bits(gb, 2);
1799 ctx->fdcycod = get_bits(gb, 2);
1800 ctx->sgaincod = get_bits(gb, 2);
1801 ctx->dbpbcod = get_bits(gb, 2);
1802 ctx->floorcod = get_bits(gb, 3);
1805 if (get_bits1(gb)) { /* snroffset */
1806 bit_alloc_flags = 127;
1807 ctx->csnroffst = get_bits(gb, 6);
1808 if (ctx->cplinu) { /* coupling fine snr offset and fast gain code */
1809 ctx->cplfsnroffst = get_bits(gb, 4);
1810 ctx->cplfgaincod = get_bits(gb, 3);
1812 for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
1813 ctx->fsnroffst[i] = get_bits(gb, 4);
1814 ctx->fgaincod[i] = get_bits(gb, 3);
1816 if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
1817 ctx->lfefsnroffst = get_bits(gb, 4);
1818 ctx->lfefgaincod = get_bits(gb, 3);
1822 if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
1823 bit_alloc_flags |= 64;
1824 ctx->cplfleak = get_bits(gb, 3);
1825 ctx->cplsleak = get_bits(gb, 3);
1828 if (get_bits1(gb)) { /* delta bit allocation information */
1829 bit_alloc_flags = 127;
1832 ctx->cpldeltbae = get_bits(gb, 2);
1833 if (ctx->cpldeltbae == DBA_RESERVED) {
1834 av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
1839 for (i = 0; i < nfchans; i++) {
1840 ctx->deltbae[i] = get_bits(gb, 2);
1841 if (ctx->deltbae[i] == DBA_RESERVED) {
1842 av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
1848 if (ctx->cpldeltbae == DBA_NEW) { /*coupling delta offset, len and bit allocation */
1849 ctx->cpldeltnseg = get_bits(gb, 3);
1850 for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
1851 ctx->cpldeltoffst[seg] = get_bits(gb, 5);
1852 ctx->cpldeltlen[seg] = get_bits(gb, 4);
1853 ctx->cpldeltba[seg] = get_bits(gb, 3);
1857 for (i = 0; i < nfchans; i++)
1858 if (ctx->deltbae[i] == DBA_NEW) {/*channel delta offset, len and bit allocation */
1859 ctx->deltnseg[i] = get_bits(gb, 3);
1860 for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
1861 ctx->deltoffst[i][seg] = get_bits(gb, 5);
1862 ctx->deltlen[i][seg] = get_bits(gb, 4);
1863 ctx->deltba[i][seg] = get_bits(gb, 3);
1868 if (bit_alloc_flags) {
1869 if (is_snr_offsets_zero(ctx)) {
1870 memset(ctx->cplbap, 0, sizeof (ctx->cplbap));
1871 memset(ctx->lfebap, 0, sizeof (ctx->lfebap));
1872 for (i = 0; i < nfchans; i++)
1873 memset(ctx->bap[i], 0, sizeof(ctx->bap[i]));
1875 if (ctx->chincpl && (bit_alloc_flags & 64))
1876 do_bit_allocation(ctx, 5);
1877 for (i = 0; i < nfchans; i++)
1878 if ((bit_alloc_flags >> i) & 1)
1879 do_bit_allocation(ctx, i);
1880 if (ctx->lfeon && (bit_alloc_flags & 32))
1881 do_bit_allocation(ctx, 6);
1885 if (get_bits1(gb)) { /* unused dummy data */
1886 skipl = get_bits(gb, 9);
1890 /* unpack the transform coefficients
1891 * * this also uncouples channels if coupling is in use.
1893 if (get_transform_coeffs(ctx)) {
1894 av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1897 /*for (i = 0; i < nfchans; i++)
1898 dump_floats("channel transform coefficients", 10, ctx->transform_coeffs[i + 1], BLOCK_SIZE);*/
1900 /* recover coefficients if rematrixing is in use */
1902 do_rematrixing(ctx);
1907 /*for(i = 0; i < nfchans; i++)
1908 dump_floats("channel output", 10, ctx->output[i + 1], BLOCK_SIZE);*/
1913 static inline int16_t convert(int32_t i)
1917 else if (i <= 0x43bf8000)
1920 return (i - 0x43c00000);
1923 static int frame_count = 0;
1925 /* Decode ac3 frame.
1927 * @param avctx Pointer to AVCodecContext
1928 * @param data Pointer to pcm smaples
1929 * @param data_size Set to number of pcm samples produced by decoding
1930 * @param buf Data to be decoded
1931 * @param buf_size Size of the buffer
1933 static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1935 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1937 int16_t *out_samples = (int16_t *)data;
1939 int32_t *int_ptr[6];
1941 for (i = 0; i < 6; i++)
1942 int_ptr[i] = (int32_t *)(&ctx->output[i]);
1944 //av_log(NULL, AV_LOG_INFO, "decoding frame %d buf_size = %d\n", frame_count++, buf_size);
1946 //Synchronize the frame.
1947 frame_start = ac3_synchronize(buf, buf_size);
1948 if (frame_start == -1) {
1949 av_log(avctx, AV_LOG_ERROR, "frame is not synchronized\n");
1954 //Initialize the GetBitContext with the start of valid AC3 Frame.
1955 init_get_bits(&(ctx->gb), buf + frame_start, (buf_size - frame_start) * 8);
1957 //Parse the syncinfo.
1958 //If 'fscod' or 'bsid' is not valid the decoder shall mute as per the standard.
1959 if (!ac3_parse_sync_info(ctx)) {
1960 av_log(avctx, AV_LOG_ERROR, "\n");
1966 //If 'bsid' is not valid decoder shall not decode the audio as per the standard.
1969 avctx->sample_rate = ctx->sampling_rate;
1970 avctx->bit_rate = ctx->bit_rate;
1972 if (avctx->channels == 0) {
1973 ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1975 ctx->blkoutput |= AC3_OUTPUT_LFEON;
1976 avctx->channels = ctx->nfchans + ctx->lfeon;
1978 else if (avctx->channels == 1)
1979 ctx->blkoutput |= AC3_OUTPUT_MONO;
1980 else if (avctx->channels == 2) {
1981 if (ctx->dsurmod == 0x02)
1982 ctx->blkoutput |= AC3_OUTPUT_DOLBY;
1984 ctx->blkoutput |= AC3_OUTPUT_STEREO;
1987 if (avctx->channels < (ctx->nfchans + ctx->lfeon))
1988 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);
1989 ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1991 ctx->blkoutput |= AC3_OUTPUT_LFEON;
1992 avctx->channels = ctx->nfchans + ctx->lfeon;
1995 //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);
1997 //Parse the Audio Blocks.
1998 for (i = 0; i < NB_BLOCKS; i++) {
1999 if (ac3_parse_audio_block(ctx)) {
2000 av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
2002 return ctx->frame_size;
2004 start = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1;
2005 for (k = 0; k < BLOCK_SIZE; k++)
2006 for (j = start; j <= avctx->channels; j++)
2007 *(out_samples++) = convert(int_ptr[j][k]);
2009 *data_size = NB_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);
2010 return ctx->frame_size;
2013 /* Uninitialize ac3 decoder.
2015 static int ac3_decode_end(AVCodecContext *avctx)
2017 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
2018 ff_mdct_end(&ctx->imdct_512);
2019 ff_mdct_end(&ctx->imdct_256);
2024 AVCodec lgpl_ac3_decoder = {
2026 .type = CODEC_TYPE_AUDIO,
2028 .priv_data_size = sizeof (AC3DecodeContext),
2029 .init = ac3_decode_init,
2030 .close = ac3_decode_end,
2031 .decode = ac3_decode_frame,