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).
6 * Copyright (c) 2007 Justin Ruggles
8 * Portions of this code are derived from liba52
9 * http://liba52.sourceforge.net
10 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
11 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
13 * This file is part of FFmpeg.
15 * FFmpeg is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public
17 * License as published by the Free Software Foundation; either
18 * version 2 of the License, or (at your option) any later version.
20 * FFmpeg is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
25 * You should have received a copy of the GNU General Public
26 * License along with FFmpeg; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
36 #include "ac3_parser.h"
37 #include "bitstream.h"
41 static const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 };
43 /* table for exponent to scale_factor mapping
44 * scale_factor[i] = 2 ^ -(i + 15)
46 static float scale_factors[25];
48 /** table for grouping exponents */
49 static uint8_t exp_ungroup_tbl[128][3];
51 static int16_t l3_quantizers_1[32];
52 static int16_t l3_quantizers_2[32];
53 static int16_t l3_quantizers_3[32];
55 static int16_t l5_quantizers_1[128];
56 static int16_t l5_quantizers_2[128];
57 static int16_t l5_quantizers_3[128];
59 static int16_t l7_quantizers[7];
61 static int16_t l11_quantizers_1[128];
62 static int16_t l11_quantizers_2[128];
64 static int16_t l15_quantizers[15];
66 static const uint8_t qntztab[16] = { 0, 5, 7, 3, 7, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16 };
68 /* Adjustmens in dB gain */
69 #define LEVEL_MINUS_3DB 0.7071067811865476
70 #define LEVEL_MINUS_4POINT5DB 0.5946035575013605
71 #define LEVEL_MINUS_6DB 0.5000000000000000
72 #define LEVEL_PLUS_3DB 1.4142135623730951
73 #define LEVEL_PLUS_6DB 2.0000000000000000
74 #define LEVEL_ZERO 0.0000000000000000
76 static const float clevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB,
77 LEVEL_MINUS_6DB, LEVEL_MINUS_4POINT5DB };
79 static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB };
81 #define BLOCK_SIZE 256
83 /* Output and input configurations. */
84 #define AC3_OUTPUT_UNMODIFIED 0x01
85 #define AC3_OUTPUT_MONO 0x02
86 #define AC3_OUTPUT_STEREO 0x04
87 #define AC3_OUTPUT_DOLBY 0x08
88 #define AC3_OUTPUT_LFEON 0x10
96 uint8_t blksw[AC3_MAX_CHANNELS];
97 uint8_t dithflag[AC3_MAX_CHANNELS];
99 uint8_t chincpl[AC3_MAX_CHANNELS];
106 uint8_t rematflg[AC3_MAX_CHANNELS];
116 uint8_t cplfsnroffst;
118 uint8_t fsnroffst[5];
120 uint8_t lfefsnroffst;
127 uint8_t cpldeltoffst[8];
128 uint8_t cpldeltlen[8];
129 uint8_t cpldeltba[8];
131 uint8_t deltoffst[5][8];
132 uint8_t deltlen[5][8];
133 uint8_t deltba[5][8];
135 /* Derived Attributes. */
140 int nchans; //number of total channels
141 int nfchans; //number of full-bandwidth channels
142 int lfeon; //lfe channel in use
144 float dynrng; //dynamic range gain
145 float dynrng2; //dynamic range gain for 1+1 mode
146 float chcoeffs[6]; //normalized channel coefficients
147 float cplco[5][18]; //coupling coordinates
148 int ncplbnd; //number of coupling bands
149 int ncplsubnd; //number of coupling sub bands
150 int cplstrtmant; //coupling start mantissa
151 int cplendmant; //coupling end mantissa
152 int endmant[5]; //channel end mantissas
153 AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
155 int8_t dcplexps[256]; //decoded coupling exponents
156 int8_t dexps[5][256]; //decoded fbw channel exponents
157 int8_t dlfeexps[256]; //decoded lfe channel exponents
158 uint8_t cplbap[256]; //coupling bit allocation pointers
159 uint8_t bap[5][256]; //fbw channel bit allocation pointers
160 uint8_t lfebap[256]; //lfe channel bit allocation pointers
162 int blkoutput; //output configuration for block
164 DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][BLOCK_SIZE]); //transform coefficients
167 MDCTContext imdct_512; //for 512 sample imdct transform
168 MDCTContext imdct_256; //for 256 sample imdct transform
169 DSPContext dsp; //for optimization
171 DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS][BLOCK_SIZE]); //output after imdct transform and windowing
172 DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS][BLOCK_SIZE]); //delay - added to the next block
173 DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]); //temporary storage for imdct transform
174 DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]); //temporary storage for output before windowing
175 DECLARE_ALIGNED_16(float, window[BLOCK_SIZE]); //window coefficients
179 AVRandomState dith_state; //for dither generation
182 /*********** BEGIN INIT HELPER FUNCTIONS ***********/
184 * Generate a Kaiser-Bessel Derived Window.
186 static void ac3_window_init(float *window)
189 double sum = 0.0, bessel, tmp;
190 double local_window[256];
191 double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
193 for (i = 0; i < 256; i++) {
194 tmp = i * (256 - i) * alpha2;
196 for (j = 100; j > 0; j--) /* defaul to 100 iterations */
197 bessel = bessel * tmp / (j * j) + 1;
199 local_window[i] = sum;
203 for (i = 0; i < 256; i++)
204 window[i] = sqrt(local_window[i] / sum);
208 * Generate quantizer tables.
210 static void generate_quantizers_table(int16_t quantizers[], int level, int length)
214 for (i = 0; i < length; i++)
215 quantizers[i] = ((2 * i - level + 1) << 15) / level;
218 static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
223 for (i = 0; i < length1; i++) {
224 v = ((2 * i - level + 1) << 15) / level;
225 for (j = 0; j < length2; j++)
226 quantizers[i * length2 + j] = v;
229 for (i = length1 * length2; i < size; i++)
233 static void generate_quantizers_table_2(int16_t quantizers[], int level, int length1, int length2, int size)
238 for (i = 0; i < length1; i++) {
239 v = ((2 * (i % level) - level + 1) << 15) / level;
240 for (j = 0; j < length2; j++)
241 quantizers[i * length2 + j] = v;
244 for (i = length1 * length2; i < size; i++)
249 static void generate_quantizers_table_3(int16_t quantizers[], int level, int length1, int length2, int size)
253 for (i = 0; i < length1; i++)
254 for (j = 0; j < length2; j++)
255 quantizers[i * length2 + j] = ((2 * (j % level) - level + 1) << 15) / level;
257 for (i = length1 * length2; i < size; i++)
262 * Initialize tables at runtime.
264 static void ac3_tables_init(void)
268 /* Quantizer ungrouping tables. */
269 // for level-3 quantizers
270 generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
271 generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
272 generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
274 //for level-5 quantizers
275 generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
276 generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
277 generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
279 //for level-7 quantizers
280 generate_quantizers_table(l7_quantizers, 7, 7);
282 //for level-4 quantizers
283 generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
284 generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
286 //for level-15 quantizers
287 generate_quantizers_table(l15_quantizers, 15, 15);
288 /* End Quantizer ungrouping tables. */
290 //generate scale factors
291 for (i = 0; i < 25; i++)
292 scale_factors[i] = pow(2.0, -(i + 15));
294 /* generate exponent tables
295 reference: Section 7.1.3 Exponent Decoding */
296 for(i=0; i<128; i++) {
297 exp_ungroup_tbl[i][0] = i / 25;
298 exp_ungroup_tbl[i][1] = (i % 25) / 5;
299 exp_ungroup_tbl[i][2] = (i % 25) % 5;
304 static int ac3_decode_init(AVCodecContext *avctx)
306 AC3DecodeContext *ctx = avctx->priv_data;
310 ff_mdct_init(&ctx->imdct_256, 8, 1);
311 ff_mdct_init(&ctx->imdct_512, 9, 1);
312 ac3_window_init(ctx->window);
313 dsputil_init(&ctx->dsp, avctx);
314 av_init_random(0, &ctx->dith_state);
318 /*********** END INIT FUNCTIONS ***********/
321 * Parses the 'sync info' and 'bit stream info' from the AC-3 bitstream.
322 * GetBitContext within AC3DecodeContext must point to
323 * start of the synchronized ac3 bitstream.
325 static int ac3_parse_header(AC3DecodeContext *ctx)
328 GetBitContext *gb = &ctx->gb;
331 err = ff_ac3_parse_header(gb->buffer, &hdr);
335 /* get decoding parameters from header info */
336 ctx->bit_alloc_params.fscod = hdr.fscod;
337 ctx->acmod = hdr.acmod;
338 ctx->cmixlev = hdr.cmixlev;
339 ctx->surmixlev = hdr.surmixlev;
340 ctx->dsurmod = hdr.dsurmod;
341 ctx->lfeon = hdr.lfeon;
342 ctx->bit_alloc_params.halfratecod = hdr.halfratecod;
343 ctx->sampling_rate = hdr.sample_rate;
344 ctx->bit_rate = hdr.bit_rate;
345 ctx->nchans = hdr.channels;
346 ctx->nfchans = ctx->nchans - ctx->lfeon;
347 ctx->frame_size = hdr.frame_size;
348 ctx->blkoutput = nfchans_tbl[ctx->acmod];
350 ctx->blkoutput |= AC3_OUTPUT_LFEON;
352 /* skip over portion of header which has already been read */
353 skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
354 skip_bits(gb, 16); // skip crc1
355 skip_bits(gb, 8); // skip fscod and frmsizecod
356 skip_bits(gb, 11); // skip bsid, bsmod, and acmod
357 if(ctx->acmod == AC3_ACMOD_STEREO) {
358 skip_bits(gb, 2); // skip dsurmod
360 if((ctx->acmod & 1) && ctx->acmod != AC3_ACMOD_MONO)
361 skip_bits(gb, 2); // skip cmixlev
363 skip_bits(gb, 2); // skip surmixlev
365 skip_bits1(gb); // skip lfeon
367 /* read the rest of the bsi. read twice for dual mono mode. */
370 skip_bits(gb, 5); //skip dialog normalization
372 skip_bits(gb, 8); //skip compression
374 skip_bits(gb, 8); //skip language code
376 skip_bits(gb, 7); //skip audio production information
379 skip_bits(gb, 2); //skip copyright bit and original bitstream bit
381 /* FIXME: read & use the xbsi1 downmix levels */
383 skip_bits(gb, 14); //skip timecode1
385 skip_bits(gb, 14); //skip timecode2
388 i = get_bits(gb, 6); //additional bsi length
398 * Decodes the grouped exponents.
399 * This function decodes the coded exponents according to exponent strategy
400 * and stores them in the decoded exponents buffer.
402 * @param[in] gb GetBitContext which points to start of coded exponents
403 * @param[in] expstr Exponent coding strategy
404 * @param[in] ngrps Number of grouped exponents
405 * @param[in] absexp Absolute exponent or DC exponent
406 * @param[out] dexps Decoded exponents are stored in dexps
408 static void decode_exponents(GetBitContext *gb, int expstr, int ngrps,
409 uint8_t absexp, int8_t *dexps)
411 int i, j, grp, grpsize;
416 grpsize = expstr + (expstr == EXP_D45);
417 for(grp=0,i=0; grp<ngrps; grp++) {
418 expacc = get_bits(gb, 7);
419 dexp[i++] = exp_ungroup_tbl[expacc][0];
420 dexp[i++] = exp_ungroup_tbl[expacc][1];
421 dexp[i++] = exp_ungroup_tbl[expacc][2];
424 /* convert to absolute exps and expand groups */
426 for(i=0; i<ngrps*3; i++) {
427 prevexp = av_clip(prevexp + dexp[i]-2, 0, 24);
428 for(j=0; j<grpsize; j++) {
429 dexps[(i*grpsize)+j] = prevexp;
434 /* Performs bit allocation.
435 * This function performs bit allocation for the requested chanenl.
437 static void do_bit_allocation(AC3DecodeContext *ctx, int chnl)
439 int fgain, snroffset;
442 fgain = ff_fgaintab[ctx->cplfgaincod];
443 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->cplfsnroffst) << 2;
444 ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->cplbap,
445 ctx->dcplexps, ctx->cplstrtmant,
446 ctx->cplendmant, snroffset, fgain, 0,
447 ctx->cpldeltbae, ctx->cpldeltnseg,
448 ctx->cpldeltoffst, ctx->cpldeltlen,
451 else if (chnl == 6) {
452 fgain = ff_fgaintab[ctx->lfefgaincod];
453 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->lfefsnroffst) << 2;
454 ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->lfebap,
455 ctx->dlfeexps, 0, 7, snroffset, fgain, 1,
456 DBA_NONE, 0, NULL, NULL, NULL);
459 fgain = ff_fgaintab[ctx->fgaincod[chnl]];
460 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->fsnroffst[chnl]) << 2;
461 ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->bap[chnl],
462 ctx->dexps[chnl], 0, ctx->endmant[chnl],
463 snroffset, fgain, 0, ctx->deltbae[chnl],
464 ctx->deltnseg[chnl], ctx->deltoffst[chnl],
465 ctx->deltlen[chnl], ctx->deltba[chnl]);
469 typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
470 int16_t l3_quantizers[3];
471 int16_t l5_quantizers[3];
472 int16_t l11_quantizers[2];
478 /* Get the transform coefficients for coupling channel and uncouple channels.
479 * The coupling transform coefficients starts at the the cplstrtmant, which is
480 * equal to endmant[ch] for fbw channels. Hence we can uncouple channels before
481 * getting transform coefficients for the channel.
483 static int get_transform_coeffs_cpling(AC3DecodeContext *ctx, mant_groups *m)
485 GetBitContext *gb = &ctx->gb;
486 int ch, start, end, cplbndstrc, bnd, gcode, tbap;
487 float cplcos[5], cplcoeff;
488 uint8_t *exps = ctx->dcplexps;
489 uint8_t *bap = ctx->cplbap;
491 cplbndstrc = ctx->cplbndstrc;
492 start = ctx->cplstrtmant;
495 while (start < ctx->cplendmant) {
497 while (cplbndstrc & 1) {
502 for (ch = 0; ch < ctx->nfchans; ch++)
503 cplcos[ch] = ctx->chcoeffs[ch] * ctx->cplco[ch][bnd];
506 while (start < end) {
510 for (ch = 0; ch < ctx->nfchans; ch++)
511 if (ctx->chincpl[ch]) {
512 if (ctx->dithflag[ch]) {
513 cplcoeff = (av_random(&ctx->dith_state) & 0xFFFF) * scale_factors[exps[start]];
514 ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch] * LEVEL_MINUS_3DB;
516 ctx->transform_coeffs[ch + 1][start] = 0;
522 gcode = get_bits(gb, 5);
523 m->l3_quantizers[0] = l3_quantizers_1[gcode];
524 m->l3_quantizers[1] = l3_quantizers_2[gcode];
525 m->l3_quantizers[2] = l3_quantizers_3[gcode];
528 cplcoeff = m->l3_quantizers[m->l3ptr++] * scale_factors[exps[start]];
533 gcode = get_bits(gb, 7);
534 m->l5_quantizers[0] = l5_quantizers_1[gcode];
535 m->l5_quantizers[1] = l5_quantizers_2[gcode];
536 m->l5_quantizers[2] = l5_quantizers_3[gcode];
539 cplcoeff = m->l5_quantizers[m->l5ptr++] * scale_factors[exps[start]];
543 cplcoeff = l7_quantizers[get_bits(gb, 3)] * scale_factors[exps[start]];
548 gcode = get_bits(gb, 7);
549 m->l11_quantizers[0] = l11_quantizers_1[gcode];
550 m->l11_quantizers[1] = l11_quantizers_2[gcode];
553 cplcoeff = m->l11_quantizers[m->l11ptr++] * scale_factors[exps[start]];
557 cplcoeff = l15_quantizers[get_bits(gb, 4)] * scale_factors[exps[start]];
561 cplcoeff = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * scale_factors[exps[start]];
563 for (ch = 0; ch < ctx->nfchans; ch++)
564 if (ctx->chincpl[ch])
565 ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
573 /* Get the transform coefficients for particular channel */
574 static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
576 GetBitContext *gb = &ctx->gb;
577 int i, gcode, tbap, dithflag, end;
583 for (i = 0; i < 25; i++)
584 factors[i] = scale_factors[i] * ctx->chcoeffs[ch_index];
586 if (ch_index != -1) { /* fbw channels */
587 dithflag = ctx->dithflag[ch_index];
588 exps = ctx->dexps[ch_index];
589 bap = ctx->bap[ch_index];
590 coeffs = ctx->transform_coeffs[ch_index + 1];
591 end = ctx->endmant[ch_index];
592 } else if (ch_index == -1) {
594 exps = ctx->dlfeexps;
596 coeffs = ctx->transform_coeffs[0];
601 for (i = 0; i < end; i++) {
610 coeffs[i] = (av_random(&ctx->dith_state) & 0xFFFF) * factors[exps[i]];
611 coeffs[i] *= LEVEL_MINUS_3DB;
617 gcode = get_bits(gb, 5);
618 m->l3_quantizers[0] = l3_quantizers_1[gcode];
619 m->l3_quantizers[1] = l3_quantizers_2[gcode];
620 m->l3_quantizers[2] = l3_quantizers_3[gcode];
623 coeffs[i] = m->l3_quantizers[m->l3ptr++] * factors[exps[i]];
628 gcode = get_bits(gb, 7);
629 m->l5_quantizers[0] = l5_quantizers_1[gcode];
630 m->l5_quantizers[1] = l5_quantizers_2[gcode];
631 m->l5_quantizers[2] = l5_quantizers_3[gcode];
634 coeffs[i] = m->l5_quantizers[m->l5ptr++] * factors[exps[i]];
638 coeffs[i] = l7_quantizers[get_bits(gb, 3)] * factors[exps[i]];
643 gcode = get_bits(gb, 7);
644 m->l11_quantizers[0] = l11_quantizers_1[gcode];
645 m->l11_quantizers[1] = l11_quantizers_2[gcode];
648 coeffs[i] = m->l11_quantizers[m->l11ptr++] * factors[exps[i]];
652 coeffs[i] = l15_quantizers[get_bits(gb, 4)] * factors[exps[i]];
656 coeffs[i] = (get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap])) * factors[exps[i]];
664 /* Get the transform coefficients.
665 * This function extracts the tranform coefficients form the ac3 bitstream.
666 * This function is called after bit allocation is performed.
668 static int get_transform_coeffs(AC3DecodeContext * ctx)
674 m.l3ptr = m.l5ptr = m.l11ptr = 3;
676 for (i = 0; i < ctx->nfchans; i++) {
677 /* transform coefficients for individual channel */
678 if (get_transform_coeffs_ch(ctx, i, &m))
680 /* tranform coefficients for coupling channels */
681 if (ctx->chincpl[i]) {
683 if (get_transform_coeffs_cpling(ctx, &m)) {
684 av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
689 end = ctx->cplendmant;
691 end = ctx->endmant[i];
693 ctx->transform_coeffs[i + 1][end] = 0;
697 if (get_transform_coeffs_ch(ctx, -1, &m))
699 for (i = 7; i < 256; i++) {
700 ctx->transform_coeffs[0][i] = 0;
707 /* Rematrixing routines. */
708 static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
712 while (start < end) {
713 tmp0 = ctx->transform_coeffs[1][start];
714 tmp1 = ctx->transform_coeffs[2][start];
715 ctx->transform_coeffs[1][start] = tmp0 + tmp1;
716 ctx->transform_coeffs[2][start] = tmp0 - tmp1;
721 static void do_rematrixing(AC3DecodeContext *ctx)
723 int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
726 end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
728 if (ctx->rematflg[0])
729 do_rematrixing1(ctx, bnd1, bnd2);
731 if (ctx->rematflg[1])
732 do_rematrixing1(ctx, bnd2, bnd3);
737 if (ctx->rematflg[2])
738 do_rematrixing1(ctx, bnd3, bndend);
740 if (ctx->rematflg[2])
741 do_rematrixing1(ctx, bnd3, bnd4);
742 if (ctx->rematflg[3])
743 do_rematrixing1(ctx, bnd4, end);
747 /* This function sets the normalized channel coefficients.
748 * Transform coefficients are multipllied by the channel
749 * coefficients to get normalized transform coefficients.
751 static void get_downmix_coeffs(AC3DecodeContext *ctx)
753 int from = ctx->acmod;
754 int to = ctx->blkoutput;
755 float clev = clevs[ctx->cmixlev];
756 float slev = slevs[ctx->surmixlev];
757 float nf = 1.0; //normalization factor for downmix coeffs
761 ctx->chcoeffs[0] = 2 * ctx->dynrng;
762 ctx->chcoeffs[1] = 2 * ctx->dynrng2;
764 for (i = 0; i < ctx->nfchans; i++)
765 ctx->chcoeffs[i] = 2 * ctx->dynrng;
768 if (to == AC3_OUTPUT_UNMODIFIED)
772 case AC3_ACMOD_DUALMONO:
774 case AC3_OUTPUT_MONO:
775 case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */
777 ctx->chcoeffs[0] *= nf;
778 ctx->chcoeffs[1] *= nf;
784 case AC3_OUTPUT_STEREO:
785 nf = LEVEL_MINUS_3DB;
786 ctx->chcoeffs[0] *= nf;
790 case AC3_ACMOD_STEREO:
792 case AC3_OUTPUT_MONO:
793 nf = LEVEL_MINUS_3DB;
794 ctx->chcoeffs[0] *= nf;
795 ctx->chcoeffs[1] *= nf;
801 case AC3_OUTPUT_MONO:
802 nf = LEVEL_MINUS_3DB / (1.0 + clev);
803 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
804 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
805 ctx->chcoeffs[1] *= ((nf * clev * LEVEL_MINUS_3DB) / 2.0);
807 case AC3_OUTPUT_STEREO:
808 nf = 1.0 / (1.0 + clev);
809 ctx->chcoeffs[0] *= nf;
810 ctx->chcoeffs[2] *= nf;
811 ctx->chcoeffs[1] *= (nf * clev);
817 case AC3_OUTPUT_MONO:
818 nf = 2.0 * LEVEL_MINUS_3DB / (2.0 + slev);
819 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
820 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
821 ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
823 case AC3_OUTPUT_STEREO:
824 nf = 1.0 / (1.0 + (slev * LEVEL_MINUS_3DB));
825 ctx->chcoeffs[0] *= nf;
826 ctx->chcoeffs[1] *= nf;
827 ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
829 case AC3_OUTPUT_DOLBY:
830 nf = 1.0 / (1.0 + LEVEL_MINUS_3DB);
831 ctx->chcoeffs[0] *= nf;
832 ctx->chcoeffs[1] *= nf;
833 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
839 case AC3_OUTPUT_MONO:
840 nf = LEVEL_MINUS_3DB / (1.0 + clev + (slev / 2.0));
841 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
842 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
843 ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
844 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
846 case AC3_OUTPUT_STEREO:
847 nf = 1.0 / (1.0 + clev + (slev * LEVEL_MINUS_3DB));
848 ctx->chcoeffs[0] *= nf;
849 ctx->chcoeffs[2] *= nf;
850 ctx->chcoeffs[1] *= (nf * clev);
851 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
853 case AC3_OUTPUT_DOLBY:
854 nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
855 ctx->chcoeffs[0] *= nf;
856 ctx->chcoeffs[1] *= nf;
857 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
858 ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
864 case AC3_OUTPUT_MONO:
865 nf = LEVEL_MINUS_3DB / (1.0 + slev);
866 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
867 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
868 ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
869 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
871 case AC3_OUTPUT_STEREO:
872 nf = 1.0 / (1.0 + slev);
873 ctx->chcoeffs[0] *= nf;
874 ctx->chcoeffs[1] *= nf;
875 ctx->chcoeffs[2] *= (nf * slev);
876 ctx->chcoeffs[3] *= (nf * slev);
878 case AC3_OUTPUT_DOLBY:
879 nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
880 ctx->chcoeffs[0] *= nf;
881 ctx->chcoeffs[1] *= nf;
882 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
883 ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
889 case AC3_OUTPUT_MONO:
890 nf = LEVEL_MINUS_3DB / (1.0 + clev + slev);
891 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
892 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
893 ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
894 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
895 ctx->chcoeffs[4] *= (nf * slev * LEVEL_MINUS_3DB);
897 case AC3_OUTPUT_STEREO:
898 nf = 1.0 / (1.0 + clev + slev);
899 ctx->chcoeffs[0] *= nf;
900 ctx->chcoeffs[2] *= nf;
901 ctx->chcoeffs[1] *= (nf * clev);
902 ctx->chcoeffs[3] *= (nf * slev);
903 ctx->chcoeffs[4] *= (nf * slev);
905 case AC3_OUTPUT_DOLBY:
906 nf = 1.0 / (1.0 + (3.0 * LEVEL_MINUS_3DB));
907 ctx->chcoeffs[0] *= nf;
908 ctx->chcoeffs[1] *= nf;
909 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
910 ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
911 ctx->chcoeffs[4] *= (nf * LEVEL_MINUS_3DB);
918 /*********** BEGIN DOWNMIX FUNCTIONS ***********/
919 static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx)
922 float (*output)[BLOCK_SIZE] = ctx->output;
924 for (i = 0; i < 256; i++)
925 output[1][i] += output[2][i];
926 memset(output[2], 0, sizeof(output[2]));
929 static inline void mix_dualmono_to_stereo(AC3DecodeContext *ctx)
933 float (*output)[BLOCK_SIZE] = ctx->output;
935 for (i = 0; i < 256; i++) {
936 tmp = output[1][i] + output[2][i];
937 output[1][i] = output[2][i] = tmp;
941 static inline void upmix_mono_to_stereo(AC3DecodeContext *ctx)
944 float (*output)[BLOCK_SIZE] = ctx->output;
946 for (i = 0; i < 256; i++)
947 output[2][i] = output[1][i];
950 static inline void mix_stereo_to_mono(AC3DecodeContext *ctx)
953 float (*output)[BLOCK_SIZE] = ctx->output;
955 for (i = 0; i < 256; i++)
956 output[1][i] += output[2][i];
957 memset(output[2], 0, sizeof(output[2]));
960 static inline void mix_3f_to_mono(AC3DecodeContext *ctx)
963 float (*output)[BLOCK_SIZE] = ctx->output;
965 for (i = 0; i < 256; i++)
966 output[1][i] += (output[2][i] + output[3][i]);
967 memset(output[2], 0, sizeof(output[2]));
968 memset(output[3], 0, sizeof(output[3]));
971 static inline void mix_3f_to_stereo(AC3DecodeContext *ctx)
974 float (*output)[BLOCK_SIZE] = ctx->output;
976 for (i = 0; i < 256; i++) {
977 output[1][i] += output[2][i];
978 output[2][i] += output[3][i];
980 memset(output[3], 0, sizeof(output[3]));
983 static inline void mix_2f_1r_to_mono(AC3DecodeContext *ctx)
986 float (*output)[BLOCK_SIZE] = ctx->output;
988 for (i = 0; i < 256; i++)
989 output[1][i] += (output[2][i] + output[3][i]);
990 memset(output[2], 0, sizeof(output[2]));
991 memset(output[3], 0, sizeof(output[3]));
995 static inline void mix_2f_1r_to_stereo(AC3DecodeContext *ctx)
998 float (*output)[BLOCK_SIZE] = ctx->output;
1000 for (i = 0; i < 256; i++) {
1001 output[1][i] += output[2][i];
1002 output[2][i] += output[3][i];
1004 memset(output[3], 0, sizeof(output[3]));
1007 static inline void mix_2f_1r_to_dolby(AC3DecodeContext *ctx)
1010 float (*output)[BLOCK_SIZE] = ctx->output;
1012 for (i = 0; i < 256; i++) {
1013 output[1][i] -= output[3][i];
1014 output[2][i] += output[3][i];
1016 memset(output[3], 0, sizeof(output[3]));
1019 static inline void mix_3f_1r_to_mono(AC3DecodeContext *ctx)
1022 float (*output)[BLOCK_SIZE] = ctx->output;
1024 for (i = 0; i < 256; i++)
1025 output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1026 memset(output[2], 0, sizeof(output[2]));
1027 memset(output[3], 0, sizeof(output[3]));
1028 memset(output[4], 0, sizeof(output[4]));
1031 static inline void mix_3f_1r_to_stereo(AC3DecodeContext *ctx)
1034 float (*output)[BLOCK_SIZE] = ctx->output;
1036 for (i = 0; i < 256; i++) {
1037 output[1][i] += (output[2][i] + output[4][i]);
1038 output[2][i] += (output[3][i] + output[4][i]);
1040 memset(output[3], 0, sizeof(output[3]));
1041 memset(output[4], 0, sizeof(output[4]));
1044 static inline void mix_3f_1r_to_dolby(AC3DecodeContext *ctx)
1047 float (*output)[BLOCK_SIZE] = ctx->output;
1049 for (i = 0; i < 256; i++) {
1050 output[1][i] += (output[2][i] - output[4][i]);
1051 output[2][i] += (output[3][i] + output[4][i]);
1053 memset(output[3], 0, sizeof(output[3]));
1054 memset(output[4], 0, sizeof(output[4]));
1057 static inline void mix_2f_2r_to_mono(AC3DecodeContext *ctx)
1060 float (*output)[BLOCK_SIZE] = ctx->output;
1062 for (i = 0; i < 256; i++)
1063 output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1064 memset(output[2], 0, sizeof(output[2]));
1065 memset(output[3], 0, sizeof(output[3]));
1066 memset(output[4], 0, sizeof(output[4]));
1069 static inline void mix_2f_2r_to_stereo(AC3DecodeContext *ctx)
1072 float (*output)[BLOCK_SIZE] = ctx->output;
1074 for (i = 0; i < 256; i++) {
1075 output[1][i] += output[3][i];
1076 output[2][i] += output[4][i];
1078 memset(output[3], 0, sizeof(output[3]));
1079 memset(output[4], 0, sizeof(output[4]));
1082 static inline void mix_2f_2r_to_dolby(AC3DecodeContext *ctx)
1085 float (*output)[BLOCK_SIZE] = ctx->output;
1087 for (i = 0; i < 256; i++) {
1088 output[1][i] -= output[3][i];
1089 output[2][i] += output[4][i];
1091 memset(output[3], 0, sizeof(output[3]));
1092 memset(output[4], 0, sizeof(output[4]));
1095 static inline void mix_3f_2r_to_mono(AC3DecodeContext *ctx)
1098 float (*output)[BLOCK_SIZE] = ctx->output;
1100 for (i = 0; i < 256; i++)
1101 output[1][i] += (output[2][i] + output[3][i] + output[4][i] + output[5][i]);
1102 memset(output[2], 0, sizeof(output[2]));
1103 memset(output[3], 0, sizeof(output[3]));
1104 memset(output[4], 0, sizeof(output[4]));
1105 memset(output[5], 0, sizeof(output[5]));
1108 static inline void mix_3f_2r_to_stereo(AC3DecodeContext *ctx)
1111 float (*output)[BLOCK_SIZE] = ctx->output;
1113 for (i = 0; i < 256; i++) {
1114 output[1][i] += (output[2][i] + output[4][i]);
1115 output[2][i] += (output[3][i] + output[5][i]);
1117 memset(output[3], 0, sizeof(output[3]));
1118 memset(output[4], 0, sizeof(output[4]));
1119 memset(output[5], 0, sizeof(output[5]));
1122 static inline void mix_3f_2r_to_dolby(AC3DecodeContext *ctx)
1125 float (*output)[BLOCK_SIZE] = ctx->output;
1127 for (i = 0; i < 256; i++) {
1128 output[1][i] += (output[2][i] - output[4][i] - output[5][i]);
1129 output[2][i] += (output[3][i] + output[4][i] + output[5][i]);
1131 memset(output[3], 0, sizeof(output[3]));
1132 memset(output[4], 0, sizeof(output[4]));
1133 memset(output[5], 0, sizeof(output[5]));
1135 /*********** END DOWNMIX FUNCTIONS ***********/
1137 /* Downmix the output.
1138 * This function downmixes the output when the number of input
1139 * channels is not equal to the number of output channels requested.
1141 static void do_downmix(AC3DecodeContext *ctx)
1143 int from = ctx->acmod;
1144 int to = ctx->blkoutput;
1146 if (to == AC3_OUTPUT_UNMODIFIED)
1150 case AC3_ACMOD_DUALMONO:
1152 case AC3_OUTPUT_MONO:
1153 mix_dualmono_to_mono(ctx);
1155 case AC3_OUTPUT_STEREO: /* We assume that sum of both mono channels is requested */
1156 mix_dualmono_to_stereo(ctx);
1160 case AC3_ACMOD_MONO:
1162 case AC3_OUTPUT_STEREO:
1163 upmix_mono_to_stereo(ctx);
1167 case AC3_ACMOD_STEREO:
1169 case AC3_OUTPUT_MONO:
1170 mix_stereo_to_mono(ctx);
1176 case AC3_OUTPUT_MONO:
1177 mix_3f_to_mono(ctx);
1179 case AC3_OUTPUT_STEREO:
1180 mix_3f_to_stereo(ctx);
1184 case AC3_ACMOD_2F1R:
1186 case AC3_OUTPUT_MONO:
1187 mix_2f_1r_to_mono(ctx);
1189 case AC3_OUTPUT_STEREO:
1190 mix_2f_1r_to_stereo(ctx);
1192 case AC3_OUTPUT_DOLBY:
1193 mix_2f_1r_to_dolby(ctx);
1197 case AC3_ACMOD_3F1R:
1199 case AC3_OUTPUT_MONO:
1200 mix_3f_1r_to_mono(ctx);
1202 case AC3_OUTPUT_STEREO:
1203 mix_3f_1r_to_stereo(ctx);
1205 case AC3_OUTPUT_DOLBY:
1206 mix_3f_1r_to_dolby(ctx);
1210 case AC3_ACMOD_2F2R:
1212 case AC3_OUTPUT_MONO:
1213 mix_2f_2r_to_mono(ctx);
1215 case AC3_OUTPUT_STEREO:
1216 mix_2f_2r_to_stereo(ctx);
1218 case AC3_OUTPUT_DOLBY:
1219 mix_2f_2r_to_dolby(ctx);
1223 case AC3_ACMOD_3F2R:
1225 case AC3_OUTPUT_MONO:
1226 mix_3f_2r_to_mono(ctx);
1228 case AC3_OUTPUT_STEREO:
1229 mix_3f_2r_to_stereo(ctx);
1231 case AC3_OUTPUT_DOLBY:
1232 mix_3f_2r_to_dolby(ctx);
1239 /* This function performs the imdct on 256 sample transform
1242 static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
1246 FFTComplex z[2][64];
1247 float *o_ptr = ctx->tmp_output;
1249 for(i=0; i<2; i++) {
1250 /* de-interleave coefficients */
1251 for(k=0; k<128; k++) {
1252 x[k] = ctx->transform_coeffs[chindex][2*k+i];
1255 /* run standard IMDCT */
1256 ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, o_ptr, x, ctx->tmp_imdct);
1258 /* reverse the post-rotation & reordering from standard IMDCT */
1259 for(k=0; k<32; k++) {
1260 z[i][32+k].re = -o_ptr[128+2*k];
1261 z[i][32+k].im = -o_ptr[2*k];
1262 z[i][31-k].re = o_ptr[2*k+1];
1263 z[i][31-k].im = o_ptr[128+2*k+1];
1267 /* apply AC-3 post-rotation & reordering */
1268 for(k=0; k<64; k++) {
1269 o_ptr[ 2*k ] = -z[0][ k].im;
1270 o_ptr[ 2*k+1] = z[0][63-k].re;
1271 o_ptr[128+2*k ] = -z[0][ k].re;
1272 o_ptr[128+2*k+1] = z[0][63-k].im;
1273 o_ptr[256+2*k ] = -z[1][ k].re;
1274 o_ptr[256+2*k+1] = z[1][63-k].im;
1275 o_ptr[384+2*k ] = z[1][ k].im;
1276 o_ptr[384+2*k+1] = -z[1][63-k].re;
1280 /* IMDCT Transform. */
1281 static inline void do_imdct(AC3DecodeContext *ctx)
1285 if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
1286 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1287 ctx->transform_coeffs[0], ctx->tmp_imdct);
1289 for (ch=1; ch<=ctx->nfchans; ch++) {
1290 if (ctx->blksw[ch-1])
1291 do_imdct_256(ctx, ch);
1293 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1294 ctx->transform_coeffs[ch],
1297 ctx->dsp.vector_fmul_add_add(ctx->output[ch], ctx->tmp_output,
1298 ctx->window, ctx->delay[ch], 384, 256, 1);
1299 ctx->dsp.vector_fmul_reverse(ctx->delay[ch], ctx->tmp_output+256,
1304 /* Parse the audio block from ac3 bitstream.
1305 * This function extract the audio block from the ac3 bitstream
1306 * and produces the output for the block. This function must
1307 * be called for each of the six audio block in the ac3 bitstream.
1309 static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
1311 int nfchans = ctx->nfchans;
1312 int acmod = ctx->acmod;
1313 int i, bnd, rbnd, seg, grpsize;
1314 GetBitContext *gb = &ctx->gb;
1315 int bit_alloc_flags = 0;
1317 int mstrcplco, cplcoexp, cplcomant;
1318 int dynrng, chbwcod, ngrps, cplabsexp, skipl;
1320 for (i = 0; i < nfchans; i++) /*block switch flag */
1321 ctx->blksw[i] = get_bits1(gb);
1323 for (i = 0; i < nfchans; i++) /* dithering flag */
1324 ctx->dithflag[i] = get_bits1(gb);
1326 if (get_bits1(gb)) { /* dynamic range */
1327 dynrng = get_sbits(gb, 8);
1328 ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1329 } else if(blk == 0) {
1333 if(acmod == AC3_ACMOD_DUALMONO) { /* dynamic range 1+1 mode */
1335 dynrng = get_sbits(gb, 8);
1336 ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1337 } else if(blk == 0) {
1342 get_downmix_coeffs(ctx);
1344 if (get_bits1(gb)) { /* coupling strategy */
1345 ctx->cplinu = get_bits1(gb);
1346 ctx->cplbndstrc = 0;
1347 if (ctx->cplinu) { /* coupling in use */
1348 for (i = 0; i < nfchans; i++)
1349 ctx->chincpl[i] = get_bits1(gb);
1351 if (acmod == AC3_ACMOD_STEREO)
1352 ctx->phsflginu = get_bits1(gb); //phase flag in use
1354 ctx->cplbegf = get_bits(gb, 4);
1355 ctx->cplendf = get_bits(gb, 4);
1357 if (3 + ctx->cplendf - ctx->cplbegf < 0) {
1358 av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", ctx->cplendf, ctx->cplbegf);
1362 ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf;
1363 ctx->cplstrtmant = ctx->cplbegf * 12 + 37;
1364 ctx->cplendmant = ctx->cplendf * 12 + 73;
1365 for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
1366 if (get_bits1(gb)) {
1367 ctx->cplbndstrc |= 1 << i;
1371 for (i = 0; i < nfchans; i++)
1372 ctx->chincpl[i] = 0;
1379 for (i = 0; i < nfchans; i++)
1380 if (ctx->chincpl[i])
1381 if (get_bits1(gb)) { /* coupling co-ordinates */
1382 ctx->cplcoe |= 1 << i;
1383 mstrcplco = 3 * get_bits(gb, 2);
1384 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
1385 cplcoexp = get_bits(gb, 4);
1386 cplcomant = get_bits(gb, 4);
1390 cplcomant = (cplcomant | 0x10) << 13;
1391 ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
1395 if (acmod == AC3_ACMOD_STEREO && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
1396 for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
1398 ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
1401 if (acmod == AC3_ACMOD_STEREO) {/* rematrixing */
1402 ctx->rematstr = get_bits1(gb);
1403 if (ctx->rematstr) {
1404 if (!(ctx->cplinu) || ctx->cplbegf > 2)
1405 for (rbnd = 0; rbnd < 4; rbnd++)
1406 ctx->rematflg[rbnd] = get_bits1(gb);
1407 if (ctx->cplbegf > 0 && ctx->cplbegf <= 2 && ctx->cplinu)
1408 for (rbnd = 0; rbnd < 3; rbnd++)
1409 ctx->rematflg[rbnd] = get_bits1(gb);
1410 if (ctx->cplbegf == 0 && ctx->cplinu)
1411 for (rbnd = 0; rbnd < 2; rbnd++)
1412 ctx->rematflg[rbnd] = get_bits1(gb);
1416 ctx->cplexpstr = EXP_REUSE;
1417 ctx->lfeexpstr = EXP_REUSE;
1418 if (ctx->cplinu) /* coupling exponent strategy */
1419 ctx->cplexpstr = get_bits(gb, 2);
1420 for (i = 0; i < nfchans; i++) /* channel exponent strategy */
1421 ctx->chexpstr[i] = get_bits(gb, 2);
1422 if (ctx->lfeon) /* lfe exponent strategy */
1423 ctx->lfeexpstr = get_bits1(gb);
1425 for (i = 0; i < nfchans; i++) /* channel bandwidth code */
1426 if (ctx->chexpstr[i] != EXP_REUSE) {
1427 if (ctx->chincpl[i])
1428 ctx->endmant[i] = ctx->cplstrtmant;
1430 chbwcod = get_bits(gb, 6);
1432 av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
1435 ctx->endmant[i] = chbwcod * 3 + 73;
1439 if (ctx->cplexpstr != EXP_REUSE) {/* coupling exponents */
1440 bit_alloc_flags = 64;
1441 cplabsexp = get_bits(gb, 4) << 1;
1442 ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
1443 decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant);
1446 for (i = 0; i < nfchans; i++) /* fbw channel exponents */
1447 if (ctx->chexpstr[i] != EXP_REUSE) {
1448 bit_alloc_flags |= 1 << i;
1449 grpsize = 3 << (ctx->chexpstr[i] - 1);
1450 ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
1451 dexps = ctx->dexps[i];
1452 dexps[0] = get_bits(gb, 4);
1453 decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1);
1454 skip_bits(gb, 2); /* skip gainrng */
1457 if (ctx->lfeexpstr != EXP_REUSE) { /* lfe exponents */
1458 bit_alloc_flags |= 32;
1459 ctx->dlfeexps[0] = get_bits(gb, 4);
1460 decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1);
1463 if (get_bits1(gb)) { /* bit allocation information */
1464 bit_alloc_flags = 127;
1465 ctx->sdcycod = get_bits(gb, 2);
1466 ctx->fdcycod = get_bits(gb, 2);
1467 ctx->sgaincod = get_bits(gb, 2);
1468 ctx->dbpbcod = get_bits(gb, 2);
1469 ctx->floorcod = get_bits(gb, 3);
1472 if (get_bits1(gb)) { /* snroffset */
1473 bit_alloc_flags = 127;
1474 ctx->csnroffst = get_bits(gb, 6);
1475 if (ctx->cplinu) { /* coupling fine snr offset and fast gain code */
1476 ctx->cplfsnroffst = get_bits(gb, 4);
1477 ctx->cplfgaincod = get_bits(gb, 3);
1479 for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
1480 ctx->fsnroffst[i] = get_bits(gb, 4);
1481 ctx->fgaincod[i] = get_bits(gb, 3);
1483 if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
1484 ctx->lfefsnroffst = get_bits(gb, 4);
1485 ctx->lfefgaincod = get_bits(gb, 3);
1489 if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
1490 bit_alloc_flags |= 64;
1491 ctx->cplfleak = get_bits(gb, 3);
1492 ctx->cplsleak = get_bits(gb, 3);
1495 if (get_bits1(gb)) { /* delta bit allocation information */
1496 bit_alloc_flags = 127;
1499 ctx->cpldeltbae = get_bits(gb, 2);
1500 if (ctx->cpldeltbae == DBA_RESERVED) {
1501 av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
1506 for (i = 0; i < nfchans; i++) {
1507 ctx->deltbae[i] = get_bits(gb, 2);
1508 if (ctx->deltbae[i] == DBA_RESERVED) {
1509 av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
1515 if (ctx->cpldeltbae == DBA_NEW) { /*coupling delta offset, len and bit allocation */
1516 ctx->cpldeltnseg = get_bits(gb, 3);
1517 for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
1518 ctx->cpldeltoffst[seg] = get_bits(gb, 5);
1519 ctx->cpldeltlen[seg] = get_bits(gb, 4);
1520 ctx->cpldeltba[seg] = get_bits(gb, 3);
1524 for (i = 0; i < nfchans; i++)
1525 if (ctx->deltbae[i] == DBA_NEW) {/*channel delta offset, len and bit allocation */
1526 ctx->deltnseg[i] = get_bits(gb, 3);
1527 for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
1528 ctx->deltoffst[i][seg] = get_bits(gb, 5);
1529 ctx->deltlen[i][seg] = get_bits(gb, 4);
1530 ctx->deltba[i][seg] = get_bits(gb, 3);
1533 } else if(blk == 0) {
1535 ctx->cpldeltbae = DBA_NONE;
1536 for(i=0; i<nfchans; i++) {
1537 ctx->deltbae[i] = DBA_NONE;
1541 if (bit_alloc_flags) {
1542 /* set bit allocation parameters */
1543 ctx->bit_alloc_params.sdecay = ff_sdecaytab[ctx->sdcycod];
1544 ctx->bit_alloc_params.fdecay = ff_fdecaytab[ctx->fdcycod];
1545 ctx->bit_alloc_params.sgain = ff_sgaintab[ctx->sgaincod];
1546 ctx->bit_alloc_params.dbknee = ff_dbkneetab[ctx->dbpbcod];
1547 ctx->bit_alloc_params.floor = ff_floortab[ctx->floorcod];
1548 ctx->bit_alloc_params.cplfleak = ctx->cplfleak;
1549 ctx->bit_alloc_params.cplsleak = ctx->cplsleak;
1551 if (ctx->cplinu && (bit_alloc_flags & 64))
1552 do_bit_allocation(ctx, 5);
1553 for (i = 0; i < nfchans; i++)
1554 if ((bit_alloc_flags >> i) & 1)
1555 do_bit_allocation(ctx, i);
1556 if (ctx->lfeon && (bit_alloc_flags & 32))
1557 do_bit_allocation(ctx, 6);
1560 if (get_bits1(gb)) { /* unused dummy data */
1561 skipl = get_bits(gb, 9);
1565 /* unpack the transform coefficients
1566 * * this also uncouples channels if coupling is in use.
1568 if (get_transform_coeffs(ctx)) {
1569 av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1573 /* recover coefficients if rematrixing is in use */
1574 if(ctx->acmod == AC3_ACMOD_STEREO)
1575 do_rematrixing(ctx);
1584 static inline int16_t convert(int32_t i)
1588 else if (i <= 0x43bf8000)
1591 return (i - 0x43c00000);
1594 /* Decode ac3 frame.
1596 * @param avctx Pointer to AVCodecContext
1597 * @param data Pointer to pcm smaples
1598 * @param data_size Set to number of pcm samples produced by decoding
1599 * @param buf Data to be decoded
1600 * @param buf_size Size of the buffer
1602 static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1604 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1605 int16_t *out_samples = (int16_t *)data;
1607 int32_t *int_ptr[6];
1609 for (i = 0; i < 6; i++)
1610 int_ptr[i] = (int32_t *)(&ctx->output[i]);
1612 //Initialize the GetBitContext with the start of valid AC3 Frame.
1613 init_get_bits(&ctx->gb, buf, buf_size * 8);
1615 //Parse the syncinfo.
1616 if (ac3_parse_header(ctx)) {
1617 av_log(avctx, AV_LOG_ERROR, "\n");
1622 avctx->sample_rate = ctx->sampling_rate;
1623 avctx->bit_rate = ctx->bit_rate;
1625 if (avctx->channels == 0) {
1626 ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1628 ctx->blkoutput |= AC3_OUTPUT_LFEON;
1629 avctx->channels = ctx->nfchans + ctx->lfeon;
1631 else if (avctx->channels == 1)
1632 ctx->blkoutput |= AC3_OUTPUT_MONO;
1633 else if (avctx->channels == 2) {
1634 if (ctx->dsurmod == 0x02)
1635 ctx->blkoutput |= AC3_OUTPUT_DOLBY;
1637 ctx->blkoutput |= AC3_OUTPUT_STEREO;
1640 if (avctx->channels < (ctx->nfchans + ctx->lfeon))
1641 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);
1642 ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1644 ctx->blkoutput |= AC3_OUTPUT_LFEON;
1645 avctx->channels = ctx->nfchans + ctx->lfeon;
1648 //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);
1650 //Parse the Audio Blocks.
1651 for (i = 0; i < NB_BLOCKS; i++) {
1652 if (ac3_parse_audio_block(ctx, i)) {
1653 av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1655 return ctx->frame_size;
1657 start = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1;
1658 for (k = 0; k < BLOCK_SIZE; k++)
1659 for (j = start; j <= avctx->channels; j++)
1660 *(out_samples++) = convert(int_ptr[j][k]);
1662 *data_size = NB_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);
1663 return ctx->frame_size;
1666 /* Uninitialize ac3 decoder.
1668 static int ac3_decode_end(AVCodecContext *avctx)
1670 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1671 ff_mdct_end(&ctx->imdct_512);
1672 ff_mdct_end(&ctx->imdct_256);
1677 AVCodec ac3_decoder = {
1679 .type = CODEC_TYPE_AUDIO,
1681 .priv_data_size = sizeof (AC3DecodeContext),
1682 .init = ac3_decode_init,
1683 .close = ac3_decode_end,
1684 .decode = ac3_decode_frame,