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 const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 };
44 /* table for exponent to scale_factor mapping
45 * scale_factor[i] = 2 ^ -(i + 15)
47 static float scale_factors[25];
49 /** table for grouping exponents */
50 static uint8_t exp_ungroup_tbl[128][3];
52 static int16_t l3_quantizers_1[32];
53 static int16_t l3_quantizers_2[32];
54 static int16_t l3_quantizers_3[32];
56 static int16_t l5_quantizers_1[128];
57 static int16_t l5_quantizers_2[128];
58 static int16_t l5_quantizers_3[128];
60 static int16_t l7_quantizers[7];
62 static int16_t l11_quantizers_1[128];
63 static int16_t l11_quantizers_2[128];
65 static int16_t l15_quantizers[15];
67 static const uint8_t qntztab[16] = { 0, 5, 7, 3, 7, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16 };
69 /* Adjustmens in dB gain */
70 #define LEVEL_MINUS_3DB 0.7071067811865476
71 #define LEVEL_MINUS_4POINT5DB 0.5946035575013605
72 #define LEVEL_MINUS_6DB 0.5000000000000000
73 #define LEVEL_PLUS_3DB 1.4142135623730951
74 #define LEVEL_PLUS_6DB 2.0000000000000000
75 #define LEVEL_ZERO 0.0000000000000000
77 static const float clevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB,
78 LEVEL_MINUS_6DB, LEVEL_MINUS_4POINT5DB };
80 static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB };
82 #define BLOCK_SIZE 256
84 /* Output and input configurations. */
85 #define AC3_OUTPUT_UNMODIFIED 0x01
86 #define AC3_OUTPUT_MONO 0x02
87 #define AC3_OUTPUT_STEREO 0x04
88 #define AC3_OUTPUT_DOLBY 0x08
89 #define AC3_OUTPUT_LFEON 0x10
120 uint8_t cplfsnroffst;
122 uint8_t fsnroffst[5];
124 uint8_t lfefsnroffst;
131 uint8_t cpldeltoffst[8];
132 uint8_t cpldeltlen[8];
133 uint8_t cpldeltba[8];
135 uint8_t deltoffst[5][8];
136 uint8_t deltlen[5][8];
137 uint8_t deltba[5][8];
139 /* Derived Attributes. */
144 int nfchans; //number of channels
145 int lfeon; //lfe channel in use
147 float dynrng; //dynamic range gain
148 float dynrng2; //dynamic range gain for 1+1 mode
149 float chcoeffs[6]; //normalized channel coefficients
150 float cplco[5][18]; //coupling coordinates
151 int ncplbnd; //number of coupling bands
152 int ncplsubnd; //number of coupling sub bands
153 int cplstrtmant; //coupling start mantissa
154 int cplendmant; //coupling end mantissa
155 int endmant[5]; //channel end mantissas
156 AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
158 uint8_t dcplexps[256]; //decoded coupling exponents
159 uint8_t dexps[5][256]; //decoded fbw channel exponents
160 uint8_t dlfeexps[256]; //decoded lfe channel exponents
161 uint8_t cplbap[256]; //coupling bit allocation pointers
162 uint8_t bap[5][256]; //fbw channel bit allocation pointers
163 uint8_t lfebap[256]; //lfe channel bit allocation pointers
165 int blkoutput; //output configuration for block
167 DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][BLOCK_SIZE]); //transform coefficients
170 MDCTContext imdct_512; //for 512 sample imdct transform
171 MDCTContext imdct_256; //for 256 sample imdct transform
172 DSPContext dsp; //for optimization
174 DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS][BLOCK_SIZE]); //output after imdct transform and windowing
175 DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS][BLOCK_SIZE]); //delay - added to the next block
176 DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]); //temporary storage for imdct transform
177 DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]); //temporary storage for output before windowing
178 DECLARE_ALIGNED_16(float, window[BLOCK_SIZE]); //window coefficients
182 AVRandomState dith_state; //for dither generation
185 /*********** BEGIN INIT HELPER FUNCTIONS ***********/
187 * Generate a Kaiser-Bessel Derived Window.
189 static void ac3_window_init(float *window)
192 double sum = 0.0, bessel, tmp;
193 double local_window[256];
194 double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
196 for (i = 0; i < 256; i++) {
197 tmp = i * (256 - i) * alpha2;
199 for (j = 100; j > 0; j--) /* defaul to 100 iterations */
200 bessel = bessel * tmp / (j * j) + 1;
202 local_window[i] = sum;
206 for (i = 0; i < 256; i++)
207 window[i] = sqrt(local_window[i] / sum);
211 * Generate quantizer tables.
213 static void generate_quantizers_table(int16_t quantizers[], int level, int length)
217 for (i = 0; i < length; i++)
218 quantizers[i] = ((2 * i - level + 1) << 15) / level;
221 static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
226 for (i = 0; i < length1; i++) {
227 v = ((2 * i - level + 1) << 15) / level;
228 for (j = 0; j < length2; j++)
229 quantizers[i * length2 + j] = v;
232 for (i = length1 * length2; i < size; i++)
236 static void generate_quantizers_table_2(int16_t quantizers[], int level, int length1, int length2, int size)
241 for (i = 0; i < length1; i++) {
242 v = ((2 * (i % level) - level + 1) << 15) / level;
243 for (j = 0; j < length2; j++)
244 quantizers[i * length2 + j] = v;
247 for (i = length1 * length2; i < size; i++)
252 static void generate_quantizers_table_3(int16_t quantizers[], int level, int length1, int length2, int size)
256 for (i = 0; i < length1; i++)
257 for (j = 0; j < length2; j++)
258 quantizers[i * length2 + j] = ((2 * (j % level) - level + 1) << 15) / level;
260 for (i = length1 * length2; i < size; i++)
265 * Initialize tables at runtime.
267 static void ac3_tables_init(void)
271 /* Quantizer ungrouping tables. */
272 // for level-3 quantizers
273 generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
274 generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
275 generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
277 //for level-5 quantizers
278 generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
279 generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
280 generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
282 //for level-7 quantizers
283 generate_quantizers_table(l7_quantizers, 7, 7);
285 //for level-4 quantizers
286 generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
287 generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
289 //for level-15 quantizers
290 generate_quantizers_table(l15_quantizers, 15, 15);
291 /* End Quantizer ungrouping tables. */
293 //generate scale factors
294 for (i = 0; i < 25; i++)
295 scale_factors[i] = pow(2.0, -(i + 15));
297 /* generate exponent tables
298 reference: Section 7.1.3 Exponent Decoding */
299 for(i=0; i<128; i++) {
300 exp_ungroup_tbl[i][0] = i / 25;
301 exp_ungroup_tbl[i][1] = (i % 25) / 5;
302 exp_ungroup_tbl[i][2] = (i % 25) % 5;
307 static int ac3_decode_init(AVCodecContext *avctx)
309 AC3DecodeContext *ctx = avctx->priv_data;
313 ff_mdct_init(&ctx->imdct_256, 8, 1);
314 ff_mdct_init(&ctx->imdct_512, 9, 1);
315 ac3_window_init(ctx->window);
316 dsputil_init(&ctx->dsp, avctx);
317 av_init_random(0, &ctx->dith_state);
321 /*********** END INIT FUNCTIONS ***********/
323 /* Synchronize to ac3 bitstream.
324 * This function searches for the syncword '0xb77'.
326 * @param buf Pointer to "probable" ac3 bitstream buffer
327 * @param buf_size Size of buffer
328 * @return Returns the position where syncword is found, -1 if no syncword is found
330 static int ac3_synchronize(uint8_t *buf, int buf_size)
334 for (i = 0; i < buf_size - 1; i++)
335 if (buf[i] == 0x0b && buf[i + 1] == 0x77)
341 /* Parse the 'sync_info' from the ac3 bitstream.
342 * This function extracts the sync_info from ac3 bitstream.
343 * GetBitContext within AC3DecodeContext must point to
344 * start of the synchronized ac3 bitstream.
346 * @param ctx AC3DecodeContext
347 * @return Returns framesize, returns 0 if fscod, frmsizecod or bsid is not valid
349 static int ac3_parse_sync_info(AC3DecodeContext *ctx)
351 GetBitContext *gb = &ctx->gb;
352 int frmsizecod, bsid;
354 skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
355 ctx->crc1 = get_bits(gb, 16);
356 ctx->fscod = get_bits(gb, 2);
357 if (ctx->fscod == 0x03)
359 frmsizecod = get_bits(gb, 6);
360 if (frmsizecod >= 38)
362 ctx->sampling_rate = ff_ac3_freqs[ctx->fscod];
363 ctx->bit_rate = ff_ac3_bitratetab[frmsizecod >> 1];
365 /* we include it here in order to determine validity of ac3 frame */
366 bsid = get_bits(gb, 5);
369 skip_bits(gb, 3); //skip the bsmod, bsi->bsmod = get_bits(gb, 3);
371 switch (ctx->fscod) {
373 ctx->frame_size = 4 * ctx->bit_rate;
374 return ctx->frame_size;
376 ctx->frame_size = 2 * (320 * ctx->bit_rate / 147 + (frmsizecod & 1));
377 return ctx->frame_size;
379 ctx->frame_size = 6 * ctx->bit_rate;
380 return ctx->frame_size;
387 /* Parse bsi from ac3 bitstream.
388 * This function extracts the bitstream information (bsi) from ac3 bitstream.
390 * @param ctx AC3DecodeContext after processed by ac3_parse_sync_info
392 static void ac3_parse_bsi(AC3DecodeContext *ctx)
394 GetBitContext *gb = &ctx->gb;
401 ctx->cpldeltbae = DBA_NONE;
402 ctx->cpldeltnseg = 0;
403 for (i = 0; i < 5; i++) {
404 ctx->deltbae[i] = DBA_NONE;
405 ctx->deltnseg[i] = 0;
410 ctx->acmod = get_bits(gb, 3);
411 ctx->nfchans = nfchans_tbl[ctx->acmod];
413 if (ctx->acmod & 0x01 && ctx->acmod != 0x01)
414 ctx->cmixlev = get_bits(gb, 2);
415 if (ctx->acmod & 0x04)
416 ctx->surmixlev = get_bits(gb, 2);
417 if (ctx->acmod == 0x02)
418 ctx->dsurmod = get_bits(gb, 2);
420 ctx->lfeon = get_bits1(gb);
424 skip_bits(gb, 5); //skip dialog normalization
426 skip_bits(gb, 8); //skip compression
428 skip_bits(gb, 8); //skip language code
430 skip_bits(gb, 7); //skip audio production information
433 skip_bits(gb, 2); //skip copyright bit and original bitstream bit
436 skip_bits(gb, 14); //skip timecode1
438 skip_bits(gb, 14); //skip timecode2
441 i = get_bits(gb, 6); //additional bsi length
449 * Decodes the grouped exponents.
450 * This function decodes the coded exponents according to exponent strategy
451 * and stores them in the decoded exponents buffer.
453 * @param[in] gb GetBitContext which points to start of coded exponents
454 * @param[in] expstr Exponent coding strategy
455 * @param[in] ngrps Number of grouped exponents
456 * @param[in] absexp Absolute exponent or DC exponent
457 * @param[out] dexps Decoded exponents are stored in dexps
459 static void decode_exponents(GetBitContext *gb, int expstr, int ngrps,
460 uint8_t absexp, uint8_t *dexps)
462 int i, j, grp, grpsize;
467 grpsize = expstr + (expstr == EXP_D45);
468 for(grp=0,i=0; grp<ngrps; grp++) {
469 expacc = get_bits(gb, 7);
470 dexp[i++] = exp_ungroup_tbl[expacc][0];
471 dexp[i++] = exp_ungroup_tbl[expacc][1];
472 dexp[i++] = exp_ungroup_tbl[expacc][2];
475 /* convert to absolute exps and expand groups */
477 for(i=0; i<ngrps*3; i++) {
478 prevexp = av_clip(prevexp + dexp[i]-2, 0, 24);
479 for(j=0; j<grpsize; j++) {
480 dexps[(i*grpsize)+j] = prevexp;
485 /* Performs bit allocation.
486 * This function performs bit allocation for the requested chanenl.
488 static void do_bit_allocation(AC3DecodeContext *ctx, int chnl)
490 int fgain, snroffset;
493 fgain = ff_fgaintab[ctx->cplfgaincod];
494 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->cplfsnroffst) << 2;
495 ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->cplbap,
496 ctx->dcplexps, ctx->cplstrtmant,
497 ctx->cplendmant, snroffset, fgain, 0,
498 ctx->cpldeltbae, ctx->cpldeltnseg,
499 ctx->cpldeltoffst, ctx->cpldeltlen,
502 else if (chnl == 6) {
503 fgain = ff_fgaintab[ctx->lfefgaincod];
504 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->lfefsnroffst) << 2;
505 ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->lfebap,
506 ctx->dlfeexps, 0, 7, snroffset, fgain, 1,
507 DBA_NONE, 0, NULL, NULL, NULL);
510 fgain = ff_fgaintab[ctx->fgaincod[chnl]];
511 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->fsnroffst[chnl]) << 2;
512 ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->bap[chnl],
513 ctx->dexps[chnl], 0, ctx->endmant[chnl],
514 snroffset, fgain, 0, ctx->deltbae[chnl],
515 ctx->deltnseg[chnl], ctx->deltoffst[chnl],
516 ctx->deltlen[chnl], ctx->deltba[chnl]);
520 typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
521 int16_t l3_quantizers[3];
522 int16_t l5_quantizers[3];
523 int16_t l11_quantizers[2];
529 #define TRANSFORM_COEFF(tc, m, e, f) (tc) = (m) * (f)[(e)]
531 /* Get the transform coefficients for coupling channel and uncouple channels.
532 * The coupling transform coefficients starts at the the cplstrtmant, which is
533 * equal to endmant[ch] for fbw channels. Hence we can uncouple channels before
534 * getting transform coefficients for the channel.
536 static int get_transform_coeffs_cpling(AC3DecodeContext *ctx, mant_groups *m)
538 GetBitContext *gb = &ctx->gb;
539 int ch, start, end, cplbndstrc, bnd, gcode, tbap;
540 float cplcos[5], cplcoeff;
541 uint8_t *exps = ctx->dcplexps;
542 uint8_t *bap = ctx->cplbap;
544 cplbndstrc = ctx->cplbndstrc;
545 start = ctx->cplstrtmant;
548 while (start < ctx->cplendmant) {
550 while (cplbndstrc & 1) {
555 for (ch = 0; ch < ctx->nfchans; ch++)
556 cplcos[ch] = ctx->chcoeffs[ch] * ctx->cplco[ch][bnd];
559 while (start < end) {
563 for (ch = 0; ch < ctx->nfchans; ch++)
564 if (((ctx->chincpl) >> ch) & 1) {
565 if ((ctx->dithflag >> ch) & 1) {
566 TRANSFORM_COEFF(cplcoeff, av_random(&ctx->dith_state) & 0xFFFF, exps[start], scale_factors);
567 ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch] * LEVEL_MINUS_3DB;
569 ctx->transform_coeffs[ch + 1][start] = 0;
575 gcode = get_bits(gb, 5);
576 m->l3_quantizers[0] = l3_quantizers_1[gcode];
577 m->l3_quantizers[1] = l3_quantizers_2[gcode];
578 m->l3_quantizers[2] = l3_quantizers_3[gcode];
581 TRANSFORM_COEFF(cplcoeff, m->l3_quantizers[m->l3ptr++], exps[start], scale_factors);
586 gcode = get_bits(gb, 7);
587 m->l5_quantizers[0] = l5_quantizers_1[gcode];
588 m->l5_quantizers[1] = l5_quantizers_2[gcode];
589 m->l5_quantizers[2] = l5_quantizers_3[gcode];
592 TRANSFORM_COEFF(cplcoeff, m->l5_quantizers[m->l5ptr++], exps[start], scale_factors);
596 TRANSFORM_COEFF(cplcoeff, l7_quantizers[get_bits(gb, 3)], exps[start], scale_factors);
601 gcode = get_bits(gb, 7);
602 m->l11_quantizers[0] = l11_quantizers_1[gcode];
603 m->l11_quantizers[1] = l11_quantizers_2[gcode];
606 TRANSFORM_COEFF(cplcoeff, m->l11_quantizers[m->l11ptr++], exps[start], scale_factors);
610 TRANSFORM_COEFF(cplcoeff, l15_quantizers[get_bits(gb, 4)], exps[start], scale_factors);
614 TRANSFORM_COEFF(cplcoeff, get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]),
615 exps[start], scale_factors);
617 for (ch = 0; ch < ctx->nfchans; ch++)
618 if ((ctx->chincpl >> ch) & 1)
619 ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
627 /* Get the transform coefficients for particular channel */
628 static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
630 GetBitContext *gb = &ctx->gb;
631 int i, gcode, tbap, dithflag, end;
637 for (i = 0; i < 25; i++)
638 factors[i] = scale_factors[i] * ctx->chcoeffs[ch_index];
640 if (ch_index != -1) { /* fbw channels */
641 dithflag = (ctx->dithflag >> ch_index) & 1;
642 exps = ctx->dexps[ch_index];
643 bap = ctx->bap[ch_index];
644 coeffs = ctx->transform_coeffs[ch_index + 1];
645 end = ctx->endmant[ch_index];
646 } else if (ch_index == -1) {
648 exps = ctx->dlfeexps;
650 coeffs = ctx->transform_coeffs[0];
655 for (i = 0; i < end; i++) {
664 TRANSFORM_COEFF(coeffs[i], av_random(&ctx->dith_state) & 0xFFFF, exps[i], factors);
665 coeffs[i] *= LEVEL_MINUS_3DB;
671 gcode = get_bits(gb, 5);
672 m->l3_quantizers[0] = l3_quantizers_1[gcode];
673 m->l3_quantizers[1] = l3_quantizers_2[gcode];
674 m->l3_quantizers[2] = l3_quantizers_3[gcode];
677 TRANSFORM_COEFF(coeffs[i], m->l3_quantizers[m->l3ptr++], exps[i], factors);
682 gcode = get_bits(gb, 7);
683 m->l5_quantizers[0] = l5_quantizers_1[gcode];
684 m->l5_quantizers[1] = l5_quantizers_2[gcode];
685 m->l5_quantizers[2] = l5_quantizers_3[gcode];
688 TRANSFORM_COEFF(coeffs[i], m->l5_quantizers[m->l5ptr++], exps[i], factors);
692 TRANSFORM_COEFF(coeffs[i], l7_quantizers[get_bits(gb, 3)], exps[i], factors);
697 gcode = get_bits(gb, 7);
698 m->l11_quantizers[0] = l11_quantizers_1[gcode];
699 m->l11_quantizers[1] = l11_quantizers_2[gcode];
702 TRANSFORM_COEFF(coeffs[i], m->l11_quantizers[m->l11ptr++], exps[i], factors);
706 TRANSFORM_COEFF(coeffs[i], l15_quantizers[get_bits(gb, 4)], exps[i], factors);
710 TRANSFORM_COEFF(coeffs[i], get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]), exps[i], factors);
718 /* Get the transform coefficients.
719 * This function extracts the tranform coefficients form the ac3 bitstream.
720 * This function is called after bit allocation is performed.
722 static int get_transform_coeffs(AC3DecodeContext * ctx)
728 m.l3ptr = m.l5ptr = m.l11ptr = 3;
730 for (i = 0; i < ctx->nfchans; i++) {
731 /* transform coefficients for individual channel */
732 if (get_transform_coeffs_ch(ctx, i, &m))
734 /* tranform coefficients for coupling channels */
735 if ((ctx->chincpl >> i) & 1) {
737 if (get_transform_coeffs_cpling(ctx, &m)) {
738 av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
743 end = ctx->cplendmant;
745 end = ctx->endmant[i];
747 ctx->transform_coeffs[i + 1][end] = 0;
751 if (get_transform_coeffs_ch(ctx, -1, &m))
753 for (i = 7; i < 256; i++) {
754 ctx->transform_coeffs[0][i] = 0;
761 /* Rematrixing routines. */
762 static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
766 while (start < end) {
767 tmp0 = ctx->transform_coeffs[1][start];
768 tmp1 = ctx->transform_coeffs[2][start];
769 ctx->transform_coeffs[1][start] = tmp0 + tmp1;
770 ctx->transform_coeffs[2][start] = tmp0 - tmp1;
775 static void do_rematrixing(AC3DecodeContext *ctx)
777 int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
780 end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
782 if (ctx->rematflg & 1)
783 do_rematrixing1(ctx, bnd1, bnd2);
785 if (ctx->rematflg & 2)
786 do_rematrixing1(ctx, bnd2, bnd3);
791 if (ctx->rematflg & 4)
792 do_rematrixing1(ctx, bnd3, bndend);
794 if (ctx->rematflg & 4)
795 do_rematrixing1(ctx, bnd3, bnd4);
796 if (ctx->rematflg & 8)
797 do_rematrixing1(ctx, bnd4, end);
801 /* This function sets the normalized channel coefficients.
802 * Transform coefficients are multipllied by the channel
803 * coefficients to get normalized transform coefficients.
805 static void get_downmix_coeffs(AC3DecodeContext *ctx)
807 int from = ctx->acmod;
808 int to = ctx->blkoutput;
809 float clev = clevs[ctx->cmixlev];
810 float slev = slevs[ctx->surmixlev];
811 float nf = 1.0; //normalization factor for downmix coeffs
815 ctx->chcoeffs[0] = 2 * ctx->dynrng;
816 ctx->chcoeffs[1] = 2 * ctx->dynrng2;
818 for (i = 0; i < ctx->nfchans; i++)
819 ctx->chcoeffs[i] = 2 * ctx->dynrng;
822 if (to == AC3_OUTPUT_UNMODIFIED)
826 case AC3_ACMOD_DUALMONO:
828 case AC3_OUTPUT_MONO:
829 case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */
831 ctx->chcoeffs[0] *= nf;
832 ctx->chcoeffs[1] *= nf;
838 case AC3_OUTPUT_STEREO:
839 nf = LEVEL_MINUS_3DB;
840 ctx->chcoeffs[0] *= nf;
844 case AC3_ACMOD_STEREO:
846 case AC3_OUTPUT_MONO:
847 nf = LEVEL_MINUS_3DB;
848 ctx->chcoeffs[0] *= nf;
849 ctx->chcoeffs[1] *= nf;
855 case AC3_OUTPUT_MONO:
856 nf = LEVEL_MINUS_3DB / (1.0 + clev);
857 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
858 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
859 ctx->chcoeffs[1] *= ((nf * clev * LEVEL_MINUS_3DB) / 2.0);
861 case AC3_OUTPUT_STEREO:
862 nf = 1.0 / (1.0 + clev);
863 ctx->chcoeffs[0] *= nf;
864 ctx->chcoeffs[2] *= nf;
865 ctx->chcoeffs[1] *= (nf * clev);
871 case AC3_OUTPUT_MONO:
872 nf = 2.0 * LEVEL_MINUS_3DB / (2.0 + slev);
873 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
874 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
875 ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
877 case AC3_OUTPUT_STEREO:
878 nf = 1.0 / (1.0 + (slev * LEVEL_MINUS_3DB));
879 ctx->chcoeffs[0] *= nf;
880 ctx->chcoeffs[1] *= nf;
881 ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
883 case AC3_OUTPUT_DOLBY:
884 nf = 1.0 / (1.0 + LEVEL_MINUS_3DB);
885 ctx->chcoeffs[0] *= nf;
886 ctx->chcoeffs[1] *= nf;
887 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
893 case AC3_OUTPUT_MONO:
894 nf = LEVEL_MINUS_3DB / (1.0 + clev + (slev / 2.0));
895 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
896 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
897 ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
898 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
900 case AC3_OUTPUT_STEREO:
901 nf = 1.0 / (1.0 + clev + (slev * LEVEL_MINUS_3DB));
902 ctx->chcoeffs[0] *= nf;
903 ctx->chcoeffs[2] *= nf;
904 ctx->chcoeffs[1] *= (nf * clev);
905 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
907 case AC3_OUTPUT_DOLBY:
908 nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
909 ctx->chcoeffs[0] *= nf;
910 ctx->chcoeffs[1] *= nf;
911 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
912 ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
918 case AC3_OUTPUT_MONO:
919 nf = LEVEL_MINUS_3DB / (1.0 + slev);
920 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
921 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
922 ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
923 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
925 case AC3_OUTPUT_STEREO:
926 nf = 1.0 / (1.0 + slev);
927 ctx->chcoeffs[0] *= nf;
928 ctx->chcoeffs[1] *= nf;
929 ctx->chcoeffs[2] *= (nf * slev);
930 ctx->chcoeffs[3] *= (nf * slev);
932 case AC3_OUTPUT_DOLBY:
933 nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
934 ctx->chcoeffs[0] *= nf;
935 ctx->chcoeffs[1] *= nf;
936 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
937 ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
943 case AC3_OUTPUT_MONO:
944 nf = LEVEL_MINUS_3DB / (1.0 + clev + slev);
945 ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
946 ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
947 ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
948 ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
949 ctx->chcoeffs[4] *= (nf * slev * LEVEL_MINUS_3DB);
951 case AC3_OUTPUT_STEREO:
952 nf = 1.0 / (1.0 + clev + slev);
953 ctx->chcoeffs[0] *= nf;
954 ctx->chcoeffs[2] *= nf;
955 ctx->chcoeffs[1] *= (nf * clev);
956 ctx->chcoeffs[3] *= (nf * slev);
957 ctx->chcoeffs[4] *= (nf * slev);
959 case AC3_OUTPUT_DOLBY:
960 nf = 1.0 / (1.0 + (3.0 * LEVEL_MINUS_3DB));
961 ctx->chcoeffs[0] *= nf;
962 ctx->chcoeffs[1] *= nf;
963 ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
964 ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
965 ctx->chcoeffs[4] *= (nf * LEVEL_MINUS_3DB);
972 /*********** BEGIN DOWNMIX FUNCTIONS ***********/
973 static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx)
976 float (*output)[BLOCK_SIZE] = ctx->output;
978 for (i = 0; i < 256; i++)
979 output[1][i] += output[2][i];
980 memset(output[2], 0, sizeof(output[2]));
983 static inline void mix_dualmono_to_stereo(AC3DecodeContext *ctx)
987 float (*output)[BLOCK_SIZE] = ctx->output;
989 for (i = 0; i < 256; i++) {
990 tmp = output[1][i] + output[2][i];
991 output[1][i] = output[2][i] = tmp;
995 static inline void upmix_mono_to_stereo(AC3DecodeContext *ctx)
998 float (*output)[BLOCK_SIZE] = ctx->output;
1000 for (i = 0; i < 256; i++)
1001 output[2][i] = output[1][i];
1004 static inline void mix_stereo_to_mono(AC3DecodeContext *ctx)
1007 float (*output)[BLOCK_SIZE] = ctx->output;
1009 for (i = 0; i < 256; i++)
1010 output[1][i] += output[2][i];
1011 memset(output[2], 0, sizeof(output[2]));
1014 static inline void mix_3f_to_mono(AC3DecodeContext *ctx)
1017 float (*output)[BLOCK_SIZE] = ctx->output;
1019 for (i = 0; i < 256; i++)
1020 output[1][i] += (output[2][i] + output[3][i]);
1021 memset(output[2], 0, sizeof(output[2]));
1022 memset(output[3], 0, sizeof(output[3]));
1025 static inline void mix_3f_to_stereo(AC3DecodeContext *ctx)
1028 float (*output)[BLOCK_SIZE] = ctx->output;
1030 for (i = 0; i < 256; i++) {
1031 output[1][i] += output[2][i];
1032 output[2][i] += output[3][i];
1034 memset(output[3], 0, sizeof(output[3]));
1037 static inline void mix_2f_1r_to_mono(AC3DecodeContext *ctx)
1040 float (*output)[BLOCK_SIZE] = ctx->output;
1042 for (i = 0; i < 256; i++)
1043 output[1][i] += (output[2][i] + output[3][i]);
1044 memset(output[2], 0, sizeof(output[2]));
1045 memset(output[3], 0, sizeof(output[3]));
1049 static inline void mix_2f_1r_to_stereo(AC3DecodeContext *ctx)
1052 float (*output)[BLOCK_SIZE] = ctx->output;
1054 for (i = 0; i < 256; i++) {
1055 output[1][i] += output[2][i];
1056 output[2][i] += output[3][i];
1058 memset(output[3], 0, sizeof(output[3]));
1061 static inline void mix_2f_1r_to_dolby(AC3DecodeContext *ctx)
1064 float (*output)[BLOCK_SIZE] = ctx->output;
1066 for (i = 0; i < 256; i++) {
1067 output[1][i] -= output[3][i];
1068 output[2][i] += output[3][i];
1070 memset(output[3], 0, sizeof(output[3]));
1073 static inline void mix_3f_1r_to_mono(AC3DecodeContext *ctx)
1076 float (*output)[BLOCK_SIZE] = ctx->output;
1078 for (i = 0; i < 256; i++)
1079 output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1080 memset(output[2], 0, sizeof(output[2]));
1081 memset(output[3], 0, sizeof(output[3]));
1082 memset(output[4], 0, sizeof(output[4]));
1085 static inline void mix_3f_1r_to_stereo(AC3DecodeContext *ctx)
1088 float (*output)[BLOCK_SIZE] = ctx->output;
1090 for (i = 0; i < 256; i++) {
1091 output[1][i] += (output[2][i] + output[4][i]);
1092 output[2][i] += (output[3][i] + output[4][i]);
1094 memset(output[3], 0, sizeof(output[3]));
1095 memset(output[4], 0, sizeof(output[4]));
1098 static inline void mix_3f_1r_to_dolby(AC3DecodeContext *ctx)
1101 float (*output)[BLOCK_SIZE] = ctx->output;
1103 for (i = 0; i < 256; i++) {
1104 output[1][i] += (output[2][i] - output[4][i]);
1105 output[2][i] += (output[3][i] + output[4][i]);
1107 memset(output[3], 0, sizeof(output[3]));
1108 memset(output[4], 0, sizeof(output[4]));
1111 static inline void mix_2f_2r_to_mono(AC3DecodeContext *ctx)
1114 float (*output)[BLOCK_SIZE] = ctx->output;
1116 for (i = 0; i < 256; i++)
1117 output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
1118 memset(output[2], 0, sizeof(output[2]));
1119 memset(output[3], 0, sizeof(output[3]));
1120 memset(output[4], 0, sizeof(output[4]));
1123 static inline void mix_2f_2r_to_stereo(AC3DecodeContext *ctx)
1126 float (*output)[BLOCK_SIZE] = ctx->output;
1128 for (i = 0; i < 256; i++) {
1129 output[1][i] += output[3][i];
1130 output[2][i] += output[4][i];
1132 memset(output[3], 0, sizeof(output[3]));
1133 memset(output[4], 0, sizeof(output[4]));
1136 static inline void mix_2f_2r_to_dolby(AC3DecodeContext *ctx)
1139 float (*output)[BLOCK_SIZE] = ctx->output;
1141 for (i = 0; i < 256; i++) {
1142 output[1][i] -= output[3][i];
1143 output[2][i] += output[4][i];
1145 memset(output[3], 0, sizeof(output[3]));
1146 memset(output[4], 0, sizeof(output[4]));
1149 static inline void mix_3f_2r_to_mono(AC3DecodeContext *ctx)
1152 float (*output)[BLOCK_SIZE] = ctx->output;
1154 for (i = 0; i < 256; i++)
1155 output[1][i] += (output[2][i] + output[3][i] + output[4][i] + output[5][i]);
1156 memset(output[2], 0, sizeof(output[2]));
1157 memset(output[3], 0, sizeof(output[3]));
1158 memset(output[4], 0, sizeof(output[4]));
1159 memset(output[5], 0, sizeof(output[5]));
1162 static inline void mix_3f_2r_to_stereo(AC3DecodeContext *ctx)
1165 float (*output)[BLOCK_SIZE] = ctx->output;
1167 for (i = 0; i < 256; i++) {
1168 output[1][i] += (output[2][i] + output[4][i]);
1169 output[2][i] += (output[3][i] + output[5][i]);
1171 memset(output[3], 0, sizeof(output[3]));
1172 memset(output[4], 0, sizeof(output[4]));
1173 memset(output[5], 0, sizeof(output[5]));
1176 static inline void mix_3f_2r_to_dolby(AC3DecodeContext *ctx)
1179 float (*output)[BLOCK_SIZE] = ctx->output;
1181 for (i = 0; i < 256; i++) {
1182 output[1][i] += (output[2][i] - output[4][i] - output[5][i]);
1183 output[2][i] += (output[3][i] + output[4][i] + output[5][i]);
1185 memset(output[3], 0, sizeof(output[3]));
1186 memset(output[4], 0, sizeof(output[4]));
1187 memset(output[5], 0, sizeof(output[5]));
1189 /*********** END DOWNMIX FUNCTIONS ***********/
1191 /* Downmix the output.
1192 * This function downmixes the output when the number of input
1193 * channels is not equal to the number of output channels requested.
1195 static void do_downmix(AC3DecodeContext *ctx)
1197 int from = ctx->acmod;
1198 int to = ctx->blkoutput;
1200 if (to == AC3_OUTPUT_UNMODIFIED)
1204 case AC3_ACMOD_DUALMONO:
1206 case AC3_OUTPUT_MONO:
1207 mix_dualmono_to_mono(ctx);
1209 case AC3_OUTPUT_STEREO: /* We assume that sum of both mono channels is requested */
1210 mix_dualmono_to_stereo(ctx);
1214 case AC3_ACMOD_MONO:
1216 case AC3_OUTPUT_STEREO:
1217 upmix_mono_to_stereo(ctx);
1221 case AC3_ACMOD_STEREO:
1223 case AC3_OUTPUT_MONO:
1224 mix_stereo_to_mono(ctx);
1230 case AC3_OUTPUT_MONO:
1231 mix_3f_to_mono(ctx);
1233 case AC3_OUTPUT_STEREO:
1234 mix_3f_to_stereo(ctx);
1238 case AC3_ACMOD_2F1R:
1240 case AC3_OUTPUT_MONO:
1241 mix_2f_1r_to_mono(ctx);
1243 case AC3_OUTPUT_STEREO:
1244 mix_2f_1r_to_stereo(ctx);
1246 case AC3_OUTPUT_DOLBY:
1247 mix_2f_1r_to_dolby(ctx);
1251 case AC3_ACMOD_3F1R:
1253 case AC3_OUTPUT_MONO:
1254 mix_3f_1r_to_mono(ctx);
1256 case AC3_OUTPUT_STEREO:
1257 mix_3f_1r_to_stereo(ctx);
1259 case AC3_OUTPUT_DOLBY:
1260 mix_3f_1r_to_dolby(ctx);
1264 case AC3_ACMOD_2F2R:
1266 case AC3_OUTPUT_MONO:
1267 mix_2f_2r_to_mono(ctx);
1269 case AC3_OUTPUT_STEREO:
1270 mix_2f_2r_to_stereo(ctx);
1272 case AC3_OUTPUT_DOLBY:
1273 mix_2f_2r_to_dolby(ctx);
1277 case AC3_ACMOD_3F2R:
1279 case AC3_OUTPUT_MONO:
1280 mix_3f_2r_to_mono(ctx);
1282 case AC3_OUTPUT_STEREO:
1283 mix_3f_2r_to_stereo(ctx);
1285 case AC3_OUTPUT_DOLBY:
1286 mix_3f_2r_to_dolby(ctx);
1293 /* This function performs the imdct on 256 sample transform
1296 static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
1300 FFTComplex z[2][64];
1301 float *o_ptr = ctx->tmp_output;
1303 for(i=0; i<2; i++) {
1304 /* de-interleave coefficients */
1305 for(k=0; k<128; k++) {
1306 x[k] = ctx->transform_coeffs[chindex][2*k+i];
1309 /* run standard IMDCT */
1310 ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, o_ptr, x, ctx->tmp_imdct);
1312 /* reverse the post-rotation & reordering from standard IMDCT */
1313 for(k=0; k<32; k++) {
1314 z[i][32+k].re = -o_ptr[128+2*k];
1315 z[i][32+k].im = -o_ptr[2*k];
1316 z[i][31-k].re = o_ptr[2*k+1];
1317 z[i][31-k].im = o_ptr[128+2*k+1];
1321 /* apply AC-3 post-rotation & reordering */
1322 for(k=0; k<64; k++) {
1323 o_ptr[ 2*k ] = -z[0][ k].im;
1324 o_ptr[ 2*k+1] = z[0][63-k].re;
1325 o_ptr[128+2*k ] = -z[0][ k].re;
1326 o_ptr[128+2*k+1] = z[0][63-k].im;
1327 o_ptr[256+2*k ] = -z[1][ k].re;
1328 o_ptr[256+2*k+1] = z[1][63-k].im;
1329 o_ptr[384+2*k ] = z[1][ k].im;
1330 o_ptr[384+2*k+1] = -z[1][63-k].re;
1334 /* IMDCT Transform. */
1335 static inline void do_imdct(AC3DecodeContext *ctx)
1339 if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
1340 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1341 ctx->transform_coeffs[0], ctx->tmp_imdct);
1343 for (ch=1; ch<=ctx->nfchans; ch++) {
1344 if ((ctx->blksw >> (ch-1)) & 1)
1345 do_imdct_256(ctx, ch);
1347 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1348 ctx->transform_coeffs[ch],
1351 ctx->dsp.vector_fmul_add_add(ctx->output[ch], ctx->tmp_output,
1352 ctx->window, ctx->delay[ch], 384, 256, 1);
1353 ctx->dsp.vector_fmul_reverse(ctx->delay[ch], ctx->tmp_output+256,
1358 /* Parse the audio block from ac3 bitstream.
1359 * This function extract the audio block from the ac3 bitstream
1360 * and produces the output for the block. This function must
1361 * be called for each of the six audio block in the ac3 bitstream.
1363 static int ac3_parse_audio_block(AC3DecodeContext * ctx)
1365 int nfchans = ctx->nfchans;
1366 int acmod = ctx->acmod;
1367 int i, bnd, rbnd, seg, grpsize;
1368 GetBitContext *gb = &ctx->gb;
1369 int bit_alloc_flags = 0;
1371 int mstrcplco, cplcoexp, cplcomant;
1372 int dynrng, chbwcod, ngrps, cplabsexp, skipl;
1375 for (i = 0; i < nfchans; i++) /*block switch flag */
1376 ctx->blksw |= get_bits1(gb) << i;
1379 for (i = 0; i < nfchans; i++) /* dithering flag */
1380 ctx->dithflag |= get_bits1(gb) << i;
1382 if (get_bits1(gb)) { /* dynamic range */
1383 dynrng = get_sbits(gb, 8);
1384 ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1387 if (acmod == 0x00 && get_bits1(gb)) { /* dynamic range 1+1 mode */
1388 dynrng = get_sbits(gb, 8);
1389 ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1392 get_downmix_coeffs(ctx);
1394 if (get_bits1(gb)) { /* coupling strategy */
1395 ctx->cplinu = get_bits1(gb);
1396 ctx->cplbndstrc = 0;
1398 if (ctx->cplinu) { /* coupling in use */
1399 for (i = 0; i < nfchans; i++)
1400 ctx->chincpl |= get_bits1(gb) << i;
1403 ctx->phsflginu = get_bits1(gb); //phase flag in use
1405 ctx->cplbegf = get_bits(gb, 4);
1406 ctx->cplendf = get_bits(gb, 4);
1408 if (3 + ctx->cplendf - ctx->cplbegf < 0) {
1409 av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", ctx->cplendf, ctx->cplbegf);
1413 ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf;
1414 ctx->cplstrtmant = ctx->cplbegf * 12 + 37;
1415 ctx->cplendmant = ctx->cplendf * 12 + 73;
1416 for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
1417 if (get_bits1(gb)) {
1418 ctx->cplbndstrc |= 1 << i;
1427 for (i = 0; i < nfchans; i++)
1428 if ((ctx->chincpl) >> i & 1)
1429 if (get_bits1(gb)) { /* coupling co-ordinates */
1430 ctx->cplcoe |= 1 << i;
1431 mstrcplco = 3 * get_bits(gb, 2);
1432 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
1433 cplcoexp = get_bits(gb, 4);
1434 cplcomant = get_bits(gb, 4);
1438 cplcomant = (cplcomant | 0x10) << 13;
1439 ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
1443 if (acmod == 0x02 && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
1444 for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
1446 ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
1449 if (acmod == 0x02) {/* rematrixing */
1450 ctx->rematstr = get_bits1(gb);
1451 if (ctx->rematstr) {
1454 if (!(ctx->cplinu) || ctx->cplbegf > 2)
1455 for (rbnd = 0; rbnd < 4; rbnd++)
1456 ctx->rematflg |= get_bits1(gb) << rbnd;
1457 if (ctx->cplbegf > 0 && ctx->cplbegf <= 2 && ctx->cplinu)
1458 for (rbnd = 0; rbnd < 3; rbnd++)
1459 ctx->rematflg |= get_bits1(gb) << rbnd;
1460 if (ctx->cplbegf == 0 && ctx->cplinu)
1461 for (rbnd = 0; rbnd < 2; rbnd++)
1462 ctx->rematflg |= get_bits1(gb) << rbnd;
1466 ctx->cplexpstr = EXP_REUSE;
1467 ctx->lfeexpstr = EXP_REUSE;
1468 if (ctx->cplinu) /* coupling exponent strategy */
1469 ctx->cplexpstr = get_bits(gb, 2);
1470 for (i = 0; i < nfchans; i++) /* channel exponent strategy */
1471 ctx->chexpstr[i] = get_bits(gb, 2);
1472 if (ctx->lfeon) /* lfe exponent strategy */
1473 ctx->lfeexpstr = get_bits1(gb);
1475 for (i = 0; i < nfchans; i++) /* channel bandwidth code */
1476 if (ctx->chexpstr[i] != EXP_REUSE) {
1477 if ((ctx->chincpl >> i) & 1)
1478 ctx->endmant[i] = ctx->cplstrtmant;
1480 chbwcod = get_bits(gb, 6);
1482 av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
1485 ctx->endmant[i] = chbwcod * 3 + 73;
1489 if (ctx->cplexpstr != EXP_REUSE) {/* coupling exponents */
1490 bit_alloc_flags = 64;
1491 cplabsexp = get_bits(gb, 4) << 1;
1492 ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
1493 decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant);
1496 for (i = 0; i < nfchans; i++) /* fbw channel exponents */
1497 if (ctx->chexpstr[i] != EXP_REUSE) {
1498 bit_alloc_flags |= 1 << i;
1499 grpsize = 3 << (ctx->chexpstr[i] - 1);
1500 ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
1501 dexps = ctx->dexps[i];
1502 dexps[0] = get_bits(gb, 4);
1503 decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1);
1504 skip_bits(gb, 2); /* skip gainrng */
1507 if (ctx->lfeexpstr != EXP_REUSE) { /* lfe exponents */
1508 bit_alloc_flags |= 32;
1509 ctx->dlfeexps[0] = get_bits(gb, 4);
1510 decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1);
1513 if (get_bits1(gb)) { /* bit allocation information */
1514 bit_alloc_flags = 127;
1515 ctx->sdcycod = get_bits(gb, 2);
1516 ctx->fdcycod = get_bits(gb, 2);
1517 ctx->sgaincod = get_bits(gb, 2);
1518 ctx->dbpbcod = get_bits(gb, 2);
1519 ctx->floorcod = get_bits(gb, 3);
1522 if (get_bits1(gb)) { /* snroffset */
1523 bit_alloc_flags = 127;
1524 ctx->csnroffst = get_bits(gb, 6);
1525 if (ctx->cplinu) { /* coupling fine snr offset and fast gain code */
1526 ctx->cplfsnroffst = get_bits(gb, 4);
1527 ctx->cplfgaincod = get_bits(gb, 3);
1529 for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
1530 ctx->fsnroffst[i] = get_bits(gb, 4);
1531 ctx->fgaincod[i] = get_bits(gb, 3);
1533 if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
1534 ctx->lfefsnroffst = get_bits(gb, 4);
1535 ctx->lfefgaincod = get_bits(gb, 3);
1539 if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
1540 bit_alloc_flags |= 64;
1541 ctx->cplfleak = get_bits(gb, 3);
1542 ctx->cplsleak = get_bits(gb, 3);
1545 if (get_bits1(gb)) { /* delta bit allocation information */
1546 bit_alloc_flags = 127;
1549 ctx->cpldeltbae = get_bits(gb, 2);
1550 if (ctx->cpldeltbae == DBA_RESERVED) {
1551 av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
1556 for (i = 0; i < nfchans; i++) {
1557 ctx->deltbae[i] = get_bits(gb, 2);
1558 if (ctx->deltbae[i] == DBA_RESERVED) {
1559 av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
1565 if (ctx->cpldeltbae == DBA_NEW) { /*coupling delta offset, len and bit allocation */
1566 ctx->cpldeltnseg = get_bits(gb, 3);
1567 for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
1568 ctx->cpldeltoffst[seg] = get_bits(gb, 5);
1569 ctx->cpldeltlen[seg] = get_bits(gb, 4);
1570 ctx->cpldeltba[seg] = get_bits(gb, 3);
1574 for (i = 0; i < nfchans; i++)
1575 if (ctx->deltbae[i] == DBA_NEW) {/*channel delta offset, len and bit allocation */
1576 ctx->deltnseg[i] = get_bits(gb, 3);
1577 for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
1578 ctx->deltoffst[i][seg] = get_bits(gb, 5);
1579 ctx->deltlen[i][seg] = get_bits(gb, 4);
1580 ctx->deltba[i][seg] = get_bits(gb, 3);
1585 if (bit_alloc_flags) {
1586 /* set bit allocation parameters */
1587 ctx->bit_alloc_params.fscod = ctx->fscod;
1588 ctx->bit_alloc_params.halfratecod = 0;
1589 ctx->bit_alloc_params.sdecay = ff_sdecaytab[ctx->sdcycod];
1590 ctx->bit_alloc_params.fdecay = ff_fdecaytab[ctx->fdcycod];
1591 ctx->bit_alloc_params.sgain = ff_sgaintab[ctx->sgaincod];
1592 ctx->bit_alloc_params.dbknee = ff_dbkneetab[ctx->dbpbcod];
1593 ctx->bit_alloc_params.floor = ff_floortab[ctx->floorcod];
1594 ctx->bit_alloc_params.cplfleak = ctx->cplfleak;
1595 ctx->bit_alloc_params.cplsleak = ctx->cplsleak;
1597 if (ctx->chincpl && (bit_alloc_flags & 64))
1598 do_bit_allocation(ctx, 5);
1599 for (i = 0; i < nfchans; i++)
1600 if ((bit_alloc_flags >> i) & 1)
1601 do_bit_allocation(ctx, i);
1602 if (ctx->lfeon && (bit_alloc_flags & 32))
1603 do_bit_allocation(ctx, 6);
1606 if (get_bits1(gb)) { /* unused dummy data */
1607 skipl = get_bits(gb, 9);
1611 /* unpack the transform coefficients
1612 * * this also uncouples channels if coupling is in use.
1614 if (get_transform_coeffs(ctx)) {
1615 av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
1619 /* recover coefficients if rematrixing is in use */
1621 do_rematrixing(ctx);
1630 static inline int16_t convert(int32_t i)
1634 else if (i <= 0x43bf8000)
1637 return (i - 0x43c00000);
1640 /* Decode ac3 frame.
1642 * @param avctx Pointer to AVCodecContext
1643 * @param data Pointer to pcm smaples
1644 * @param data_size Set to number of pcm samples produced by decoding
1645 * @param buf Data to be decoded
1646 * @param buf_size Size of the buffer
1648 static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1650 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1652 int16_t *out_samples = (int16_t *)data;
1654 int32_t *int_ptr[6];
1656 for (i = 0; i < 6; i++)
1657 int_ptr[i] = (int32_t *)(&ctx->output[i]);
1659 //Synchronize the frame.
1660 frame_start = ac3_synchronize(buf, buf_size);
1661 if (frame_start == -1) {
1662 av_log(avctx, AV_LOG_ERROR, "frame is not synchronized\n");
1667 //Initialize the GetBitContext with the start of valid AC3 Frame.
1668 init_get_bits(&(ctx->gb), buf + frame_start, (buf_size - frame_start) * 8);
1670 //Parse the syncinfo.
1671 //If 'fscod' or 'bsid' is not valid the decoder shall mute as per the standard.
1672 if (!ac3_parse_sync_info(ctx)) {
1673 av_log(avctx, AV_LOG_ERROR, "\n");
1679 //If 'bsid' is not valid decoder shall not decode the audio as per the standard.
1682 avctx->sample_rate = ctx->sampling_rate;
1683 avctx->bit_rate = ctx->bit_rate;
1685 if (avctx->channels == 0) {
1686 ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1688 ctx->blkoutput |= AC3_OUTPUT_LFEON;
1689 avctx->channels = ctx->nfchans + ctx->lfeon;
1691 else if (avctx->channels == 1)
1692 ctx->blkoutput |= AC3_OUTPUT_MONO;
1693 else if (avctx->channels == 2) {
1694 if (ctx->dsurmod == 0x02)
1695 ctx->blkoutput |= AC3_OUTPUT_DOLBY;
1697 ctx->blkoutput |= AC3_OUTPUT_STEREO;
1700 if (avctx->channels < (ctx->nfchans + ctx->lfeon))
1701 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);
1702 ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
1704 ctx->blkoutput |= AC3_OUTPUT_LFEON;
1705 avctx->channels = ctx->nfchans + ctx->lfeon;
1708 //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);
1710 //Parse the Audio Blocks.
1711 for (i = 0; i < NB_BLOCKS; i++) {
1712 if (ac3_parse_audio_block(ctx)) {
1713 av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1715 return ctx->frame_size;
1717 start = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1;
1718 for (k = 0; k < BLOCK_SIZE; k++)
1719 for (j = start; j <= avctx->channels; j++)
1720 *(out_samples++) = convert(int_ptr[j][k]);
1722 *data_size = NB_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);
1723 return ctx->frame_size;
1726 /* Uninitialize ac3 decoder.
1728 static int ac3_decode_end(AVCodecContext *avctx)
1730 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1731 ff_mdct_end(&ctx->imdct_512);
1732 ff_mdct_end(&ctx->imdct_256);
1737 AVCodec ac3_decoder = {
1739 .type = CODEC_TYPE_AUDIO,
1741 .priv_data_size = sizeof (AC3DecodeContext),
1742 .init = ac3_decode_init,
1743 .close = ac3_decode_end,
1744 .decode = ac3_decode_frame,