comparison ac3dec.c @ 6006:ed7a232d570a libavcodec

cosmetics: rename ac3 decoder variables
author jbr
date Sun, 09 Dec 2007 03:28:10 +0000
parents 7d9dddd54817
children 90b36c43c4ef
comparison
equal deleted inserted replaced
6005:7d9dddd54817 6006:ed7a232d570a
63 63
64 /** 64 /**
65 * Quantization table: levels for symmetric. bits for asymmetric. 65 * Quantization table: levels for symmetric. bits for asymmetric.
66 * reference: Table 7.18 Mapping of bap to Quantizer 66 * reference: Table 7.18 Mapping of bap to Quantizer
67 */ 67 */
68 static const uint8_t qntztab[16] = { 68 static const uint8_t quantization_tab[16] = {
69 0, 3, 5, 7, 11, 15, 69 0, 3, 5, 7, 11, 15,
70 5, 6, 7, 8, 9, 10, 11, 12, 14, 16 70 5, 6, 7, 8, 9, 10, 11, 12, 14, 16
71 }; 71 };
72 72
73 /** dynamic range table. converts codes to scale factors. */ 73 /** dynamic range table. converts codes to scale factors. */
74 static float dynrng_tab[256]; 74 static float dynamic_range_tab[256];
75 75
76 /** dialog normalization table */ 76 /** dialog normalization table */
77 static float dialnorm_tab[32]; 77 static float dialog_norm_tab[32];
78 78
79 /** Adjustments in dB gain */ 79 /** Adjustments in dB gain */
80 #define LEVEL_MINUS_3DB 0.7071067811865476 80 #define LEVEL_MINUS_3DB 0.7071067811865476
81 #define LEVEL_MINUS_4POINT5DB 0.5946035575013605 81 #define LEVEL_MINUS_4POINT5DB 0.5946035575013605
82 #define LEVEL_MINUS_6DB 0.5000000000000000 82 #define LEVEL_MINUS_6DB 0.5000000000000000
128 #define AC3_OUTPUT_LFEON 8 128 #define AC3_OUTPUT_LFEON 8
129 129
130 typedef struct { 130 typedef struct {
131 int channel_mode; ///< channel mode (acmod) 131 int channel_mode; ///< channel mode (acmod)
132 int dolby_surround_mode; ///< dolby surround mode 132 int dolby_surround_mode; ///< dolby surround mode
133 int blksw[AC3_MAX_CHANNELS]; ///< block switch flags 133 int block_switch[AC3_MAX_CHANNELS]; ///< block switch flags
134 int dithflag[AC3_MAX_CHANNELS]; ///< dither flags 134 int dither_flag[AC3_MAX_CHANNELS]; ///< dither flags
135 int dither_all; ///< true if all channels are dithered 135 int dither_all; ///< true if all channels are dithered
136 int cplinu; ///< coupling in use 136 int cpl_in_use; ///< coupling in use
137 int chincpl[AC3_MAX_CHANNELS]; ///< channel in coupling 137 int channel_in_cpl[AC3_MAX_CHANNELS]; ///< channel in coupling
138 int phsflginu; ///< phase flags in use 138 int phase_flags_in_use; ///< phase flags in use
139 int cplbndstrc[18]; ///< coupling band structure 139 int cpl_band_struct[18]; ///< coupling band structure
140 int rematstr; ///< rematrixing strategy 140 int rematrixing_strategy; ///< rematrixing strategy
141 int nrematbnd; ///< number of rematrixing bands 141 int num_rematrixing_bands; ///< number of rematrixing bands
142 int rematflg[4]; ///< rematrixing flags 142 int rematrixing_flags[4]; ///< rematrixing flags
143 int expstr[AC3_MAX_CHANNELS]; ///< exponent strategies 143 int exp_strategy[AC3_MAX_CHANNELS]; ///< exponent strategies
144 int snr_offset[AC3_MAX_CHANNELS]; ///< signal-to-noise ratio offsets 144 int snr_offset[AC3_MAX_CHANNELS]; ///< signal-to-noise ratio offsets
145 int fast_gain[AC3_MAX_CHANNELS]; ///< fast gain values (signal-to-mask ratio) 145 int fast_gain[AC3_MAX_CHANNELS]; ///< fast gain values (signal-to-mask ratio)
146 int dba_mode[AC3_MAX_CHANNELS]; ///< delta bit allocation mode 146 int dba_mode[AC3_MAX_CHANNELS]; ///< delta bit allocation mode
147 int dba_nsegs[AC3_MAX_CHANNELS]; ///< number of delta segments 147 int dba_nsegs[AC3_MAX_CHANNELS]; ///< number of delta segments
148 uint8_t dba_offsets[AC3_MAX_CHANNELS][8]; ///< delta segment offsets 148 uint8_t dba_offsets[AC3_MAX_CHANNELS][8]; ///< delta segment offsets
151 151
152 int sampling_rate; ///< sample frequency, in Hz 152 int sampling_rate; ///< sample frequency, in Hz
153 int bit_rate; ///< stream bit rate, in bits-per-second 153 int bit_rate; ///< stream bit rate, in bits-per-second
154 int frame_size; ///< current frame size, in bytes 154 int frame_size; ///< current frame size, in bytes
155 155
156 int nchans; ///< number of total channels 156 int channels; ///< number of total channels
157 int nfchans; ///< number of full-bandwidth channels 157 int fbw_channels; ///< number of full-bandwidth channels
158 int lfe_on; ///< lfe channel in use 158 int lfe_on; ///< lfe channel in use
159 int lfe_ch; ///< index of LFE channel 159 int lfe_ch; ///< index of LFE channel
160 int output_mode; ///< output channel configuration 160 int output_mode; ///< output channel configuration
161 int out_channels; ///< number of output channels 161 int out_channels; ///< number of output channels
162 162
163 float downmix_coeffs[AC3_MAX_CHANNELS][2]; ///< stereo downmix coefficients 163 float downmix_coeffs[AC3_MAX_CHANNELS][2]; ///< stereo downmix coefficients
164 float dialnorm[2]; ///< dialog normalization 164 float dialog_norm[2]; ///< dialog normalization
165 float dynrng[2]; ///< dynamic range 165 float dynamic_range[2]; ///< dynamic range
166 float cplco[AC3_MAX_CHANNELS][18]; ///< coupling coordinates 166 float cpl_coords[AC3_MAX_CHANNELS][18]; ///< coupling coordinates
167 int ncplbnd; ///< number of coupling bands 167 int num_cpl_bands; ///< number of coupling bands
168 int ncplsubnd; ///< number of coupling sub bands 168 int num_cpl_subbands; ///< number of coupling sub bands
169 int startmant[AC3_MAX_CHANNELS]; ///< start frequency bin 169 int start_freq[AC3_MAX_CHANNELS]; ///< start frequency bin
170 int endmant[AC3_MAX_CHANNELS]; ///< end frequency bin 170 int end_freq[AC3_MAX_CHANNELS]; ///< end frequency bin
171 AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters 171 AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
172 172
173 int8_t dexps[AC3_MAX_CHANNELS][256]; ///< decoded exponents 173 int8_t dexps[AC3_MAX_CHANNELS][256]; ///< decoded exponents
174 uint8_t bap[AC3_MAX_CHANNELS][256]; ///< bit allocation pointers 174 uint8_t bap[AC3_MAX_CHANNELS][256]; ///< bit allocation pointers
175 int16_t psd[AC3_MAX_CHANNELS][256]; ///< scaled exponents 175 int16_t psd[AC3_MAX_CHANNELS][256]; ///< scaled exponents
271 271
272 /* generate dynamic range table 272 /* generate dynamic range table
273 reference: Section 7.7.1 Dynamic Range Control */ 273 reference: Section 7.7.1 Dynamic Range Control */
274 for(i=0; i<256; i++) { 274 for(i=0; i<256; i++) {
275 int v = (i >> 5) - ((i >> 7) << 3) - 5; 275 int v = (i >> 5) - ((i >> 7) << 3) - 5;
276 dynrng_tab[i] = powf(2.0f, v) * ((i & 0x1F) | 0x20); 276 dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0x1F) | 0x20);
277 } 277 }
278 278
279 /* generate dialog normalization table 279 /* generate dialog normalization table
280 references: Section 5.4.2.8 dialnorm 280 references: Section 5.4.2.8 dialnorm
281 Section 7.6 Dialogue Normalization */ 281 Section 7.6 Dialogue Normalization */
282 for(i=1; i<32; i++) { 282 for(i=1; i<32; i++) {
283 dialnorm_tab[i] = expf((i-31) * M_LN10 / 20.0f); 283 dialog_norm_tab[i] = expf((i-31) * M_LN10 / 20.0f);
284 } 284 }
285 dialnorm_tab[0] = dialnorm_tab[31]; 285 dialog_norm_tab[0] = dialog_norm_tab[31];
286 286
287 /* generate scale factors for exponents and asymmetrical dequantization 287 /* generate scale factors for exponents and asymmetrical dequantization
288 reference: Section 7.3.2 Expansion of Mantissas for Asymmetric Quantization */ 288 reference: Section 7.3.2 Expansion of Mantissas for Asymmetric Quantization */
289 for (i = 0; i < 25; i++) 289 for (i = 0; i < 25; i++)
290 scale_factors[i] = pow(2.0, -i); 290 scale_factors[i] = pow(2.0, -i);
351 ctx->dolby_surround_mode = hdr.dolby_surround_mode; 351 ctx->dolby_surround_mode = hdr.dolby_surround_mode;
352 ctx->lfe_on = hdr.lfe_on; 352 ctx->lfe_on = hdr.lfe_on;
353 ctx->bit_alloc_params.sr_shift = hdr.sr_shift; 353 ctx->bit_alloc_params.sr_shift = hdr.sr_shift;
354 ctx->sampling_rate = hdr.sample_rate; 354 ctx->sampling_rate = hdr.sample_rate;
355 ctx->bit_rate = hdr.bit_rate; 355 ctx->bit_rate = hdr.bit_rate;
356 ctx->nchans = hdr.channels; 356 ctx->channels = hdr.channels;
357 ctx->nfchans = ctx->nchans - ctx->lfe_on; 357 ctx->fbw_channels = ctx->channels - ctx->lfe_on;
358 ctx->lfe_ch = ctx->nfchans + 1; 358 ctx->lfe_ch = ctx->fbw_channels + 1;
359 ctx->frame_size = hdr.frame_size; 359 ctx->frame_size = hdr.frame_size;
360 360
361 /* set default output to all source channels */ 361 /* set default output to all source channels */
362 ctx->out_channels = ctx->nchans; 362 ctx->out_channels = ctx->channels;
363 ctx->output_mode = ctx->channel_mode; 363 ctx->output_mode = ctx->channel_mode;
364 if(ctx->lfe_on) 364 if(ctx->lfe_on)
365 ctx->output_mode |= AC3_OUTPUT_LFEON; 365 ctx->output_mode |= AC3_OUTPUT_LFEON;
366 366
367 /* skip over portion of header which has already been read */ 367 /* skip over portion of header which has already been read */
380 skip_bits1(gb); // skip lfeon 380 skip_bits1(gb); // skip lfeon
381 381
382 /* read the rest of the bsi. read twice for dual mono mode. */ 382 /* read the rest of the bsi. read twice for dual mono mode. */
383 i = !(ctx->channel_mode); 383 i = !(ctx->channel_mode);
384 do { 384 do {
385 ctx->dialnorm[i] = dialnorm_tab[get_bits(gb, 5)]; // dialog normalization 385 ctx->dialog_norm[i] = dialog_norm_tab[get_bits(gb, 5)]; // dialog normalization
386 if (get_bits1(gb)) 386 if (get_bits1(gb))
387 skip_bits(gb, 8); //skip compression 387 skip_bits(gb, 8); //skip compression
388 if (get_bits1(gb)) 388 if (get_bits1(gb))
389 skip_bits(gb, 8); //skip language code 389 skip_bits(gb, 8); //skip language code
390 if (get_bits1(gb)) 390 if (get_bits1(gb))
408 } while(i--); 408 } while(i--);
409 } 409 }
410 410
411 /* set stereo downmixing coefficients 411 /* set stereo downmixing coefficients
412 reference: Section 7.8.2 Downmixing Into Two Channels */ 412 reference: Section 7.8.2 Downmixing Into Two Channels */
413 for(i=0; i<ctx->nfchans; i++) { 413 for(i=0; i<ctx->fbw_channels; i++) {
414 ctx->downmix_coeffs[i][0] = gain_levels[ac3_default_coeffs[ctx->channel_mode][i][0]]; 414 ctx->downmix_coeffs[i][0] = gain_levels[ac3_default_coeffs[ctx->channel_mode][i][0]];
415 ctx->downmix_coeffs[i][1] = gain_levels[ac3_default_coeffs[ctx->channel_mode][i][1]]; 415 ctx->downmix_coeffs[i][1] = gain_levels[ac3_default_coeffs[ctx->channel_mode][i][1]];
416 } 416 }
417 if(ctx->channel_mode > 1 && ctx->channel_mode & 1) { 417 if(ctx->channel_mode > 1 && ctx->channel_mode & 1) {
418 ctx->downmix_coeffs[1][0] = ctx->downmix_coeffs[1][1] = center_mix_level; 418 ctx->downmix_coeffs[1][0] = ctx->downmix_coeffs[1][1] = center_mix_level;
431 431
432 /** 432 /**
433 * Decode the grouped exponents according to exponent strategy. 433 * Decode the grouped exponents according to exponent strategy.
434 * reference: Section 7.1.3 Exponent Decoding 434 * reference: Section 7.1.3 Exponent Decoding
435 */ 435 */
436 static void decode_exponents(GetBitContext *gb, int expstr, int ngrps, 436 static void decode_exponents(GetBitContext *gb, int exp_strategy, int ngrps,
437 uint8_t absexp, int8_t *dexps) 437 uint8_t absexp, int8_t *dexps)
438 { 438 {
439 int i, j, grp, grpsize; 439 int i, j, grp, group_size;
440 int dexp[256]; 440 int dexp[256];
441 int expacc, prevexp; 441 int expacc, prevexp;
442 442
443 /* unpack groups */ 443 /* unpack groups */
444 grpsize = expstr + (expstr == EXP_D45); 444 group_size = exp_strategy + (exp_strategy == EXP_D45);
445 for(grp=0,i=0; grp<ngrps; grp++) { 445 for(grp=0,i=0; grp<ngrps; grp++) {
446 expacc = get_bits(gb, 7); 446 expacc = get_bits(gb, 7);
447 dexp[i++] = exp_ungroup_tab[expacc][0]; 447 dexp[i++] = exp_ungroup_tab[expacc][0];
448 dexp[i++] = exp_ungroup_tab[expacc][1]; 448 dexp[i++] = exp_ungroup_tab[expacc][1];
449 dexp[i++] = exp_ungroup_tab[expacc][2]; 449 dexp[i++] = exp_ungroup_tab[expacc][2];
451 451
452 /* convert to absolute exps and expand groups */ 452 /* convert to absolute exps and expand groups */
453 prevexp = absexp; 453 prevexp = absexp;
454 for(i=0; i<ngrps*3; i++) { 454 for(i=0; i<ngrps*3; i++) {
455 prevexp = av_clip(prevexp + dexp[i]-2, 0, 24); 455 prevexp = av_clip(prevexp + dexp[i]-2, 0, 24);
456 for(j=0; j<grpsize; j++) { 456 for(j=0; j<group_size; j++) {
457 dexps[(i*grpsize)+j] = prevexp; 457 dexps[(i*group_size)+j] = prevexp;
458 } 458 }
459 } 459 }
460 } 460 }
461 461
462 /** 462 /**
467 static void uncouple_channels(AC3DecodeContext *ctx) 467 static void uncouple_channels(AC3DecodeContext *ctx)
468 { 468 {
469 int i, j, ch, bnd, subbnd; 469 int i, j, ch, bnd, subbnd;
470 470
471 subbnd = -1; 471 subbnd = -1;
472 i = ctx->startmant[CPL_CH]; 472 i = ctx->start_freq[CPL_CH];
473 for(bnd=0; bnd<ctx->ncplbnd; bnd++) { 473 for(bnd=0; bnd<ctx->num_cpl_bands; bnd++) {
474 do { 474 do {
475 subbnd++; 475 subbnd++;
476 for(j=0; j<12; j++) { 476 for(j=0; j<12; j++) {
477 for(ch=1; ch<=ctx->nfchans; ch++) { 477 for(ch=1; ch<=ctx->fbw_channels; ch++) {
478 if(ctx->chincpl[ch]) 478 if(ctx->channel_in_cpl[ch])
479 ctx->transform_coeffs[ch][i] = ctx->transform_coeffs[CPL_CH][i] * ctx->cplco[ch][bnd] * 8.0f; 479 ctx->transform_coeffs[ch][i] = ctx->transform_coeffs[CPL_CH][i] * ctx->cpl_coords[ch][bnd] * 8.0f;
480 } 480 }
481 i++; 481 i++;
482 } 482 }
483 } while(ctx->cplbndstrc[subbnd]); 483 } while(ctx->cpl_band_struct[subbnd]);
484 } 484 }
485 } 485 }
486 486
487 /** 487 /**
488 * Grouped mantissas for 3-level 5-level and 11-level quantization 488 * Grouped mantissas for 3-level 5-level and 11-level quantization
509 float *coeffs; 509 float *coeffs;
510 510
511 exps = ctx->dexps[ch_index]; 511 exps = ctx->dexps[ch_index];
512 bap = ctx->bap[ch_index]; 512 bap = ctx->bap[ch_index];
513 coeffs = ctx->transform_coeffs[ch_index]; 513 coeffs = ctx->transform_coeffs[ch_index];
514 start = ctx->startmant[ch_index]; 514 start = ctx->start_freq[ch_index];
515 end = ctx->endmant[ch_index]; 515 end = ctx->end_freq[ch_index];
516 516
517 for (i = start; i < end; i++) { 517 for (i = start; i < end; i++) {
518 tbap = bap[i]; 518 tbap = bap[i];
519 switch (tbap) { 519 switch (tbap) {
520 case 0: 520 case 0:
561 coeffs[i] = b5_mantissas[get_bits(gb, 4)]; 561 coeffs[i] = b5_mantissas[get_bits(gb, 4)];
562 break; 562 break;
563 563
564 default: 564 default:
565 /* asymmetric dequantization */ 565 /* asymmetric dequantization */
566 coeffs[i] = get_sbits(gb, qntztab[tbap]) * scale_factors[qntztab[tbap]-1]; 566 coeffs[i] = get_sbits(gb, quantization_tab[tbap]) * scale_factors[quantization_tab[tbap]-1];
567 break; 567 break;
568 } 568 }
569 coeffs[i] *= scale_factors[exps[i]]; 569 coeffs[i] *= scale_factors[exps[i]];
570 } 570 }
571 571
580 int ch, i; 580 int ch, i;
581 int end=0; 581 int end=0;
582 float *coeffs; 582 float *coeffs;
583 uint8_t *bap; 583 uint8_t *bap;
584 584
585 for(ch=1; ch<=ctx->nfchans; ch++) { 585 for(ch=1; ch<=ctx->fbw_channels; ch++) {
586 if(!ctx->dithflag[ch]) { 586 if(!ctx->dither_flag[ch]) {
587 coeffs = ctx->transform_coeffs[ch]; 587 coeffs = ctx->transform_coeffs[ch];
588 bap = ctx->bap[ch]; 588 bap = ctx->bap[ch];
589 if(ctx->chincpl[ch]) 589 if(ctx->channel_in_cpl[ch])
590 end = ctx->startmant[CPL_CH]; 590 end = ctx->start_freq[CPL_CH];
591 else 591 else
592 end = ctx->endmant[ch]; 592 end = ctx->end_freq[ch];
593 for(i=0; i<end; i++) { 593 for(i=0; i<end; i++) {
594 if(bap[i] == 0) 594 if(bap[i] == 0)
595 coeffs[i] = 0.0f; 595 coeffs[i] = 0.0f;
596 } 596 }
597 if(ctx->chincpl[ch]) { 597 if(ctx->channel_in_cpl[ch]) {
598 bap = ctx->bap[CPL_CH]; 598 bap = ctx->bap[CPL_CH];
599 for(; i<ctx->endmant[CPL_CH]; i++) { 599 for(; i<ctx->end_freq[CPL_CH]; i++) {
600 if(bap[i] == 0) 600 if(bap[i] == 0)
601 coeffs[i] = 0.0f; 601 coeffs[i] = 0.0f;
602 } 602 }
603 } 603 }
604 } 604 }
614 int got_cplchan = 0; 614 int got_cplchan = 0;
615 mant_groups m; 615 mant_groups m;
616 616
617 m.b1ptr = m.b2ptr = m.b4ptr = 3; 617 m.b1ptr = m.b2ptr = m.b4ptr = 3;
618 618
619 for (ch = 1; ch <= ctx->nchans; ch++) { 619 for (ch = 1; ch <= ctx->channels; ch++) {
620 /* transform coefficients for full-bandwidth channel */ 620 /* transform coefficients for full-bandwidth channel */
621 if (get_transform_coeffs_ch(ctx, ch, &m)) 621 if (get_transform_coeffs_ch(ctx, ch, &m))
622 return -1; 622 return -1;
623 /* tranform coefficients for coupling channel come right after the 623 /* tranform coefficients for coupling channel come right after the
624 coefficients for the first coupled channel*/ 624 coefficients for the first coupled channel*/
625 if (ctx->chincpl[ch]) { 625 if (ctx->channel_in_cpl[ch]) {
626 if (!got_cplchan) { 626 if (!got_cplchan) {
627 if (get_transform_coeffs_ch(ctx, CPL_CH, &m)) { 627 if (get_transform_coeffs_ch(ctx, CPL_CH, &m)) {
628 av_log(ctx->avctx, AV_LOG_ERROR, "error in decoupling channels\n"); 628 av_log(ctx->avctx, AV_LOG_ERROR, "error in decoupling channels\n");
629 return -1; 629 return -1;
630 } 630 }
631 uncouple_channels(ctx); 631 uncouple_channels(ctx);
632 got_cplchan = 1; 632 got_cplchan = 1;
633 } 633 }
634 end = ctx->endmant[CPL_CH]; 634 end = ctx->end_freq[CPL_CH];
635 } else { 635 } else {
636 end = ctx->endmant[ch]; 636 end = ctx->end_freq[ch];
637 } 637 }
638 do 638 do
639 ctx->transform_coeffs[ch][end] = 0; 639 ctx->transform_coeffs[ch][end] = 0;
640 while(++end < 256); 640 while(++end < 256);
641 } 641 }
655 { 655 {
656 int bnd, i; 656 int bnd, i;
657 int end, bndend; 657 int end, bndend;
658 float tmp0, tmp1; 658 float tmp0, tmp1;
659 659
660 end = FFMIN(ctx->endmant[1], ctx->endmant[2]); 660 end = FFMIN(ctx->end_freq[1], ctx->end_freq[2]);
661 661
662 for(bnd=0; bnd<ctx->nrematbnd; bnd++) { 662 for(bnd=0; bnd<ctx->num_rematrixing_bands; bnd++) {
663 if(ctx->rematflg[bnd]) { 663 if(ctx->rematrixing_flags[bnd]) {
664 bndend = FFMIN(end, rematrix_band_tab[bnd+1]); 664 bndend = FFMIN(end, rematrix_band_tab[bnd+1]);
665 for(i=rematrix_band_tab[bnd]; i<bndend; i++) { 665 for(i=rematrix_band_tab[bnd]; i<bndend; i++) {
666 tmp0 = ctx->transform_coeffs[1][i]; 666 tmp0 = ctx->transform_coeffs[1][i];
667 tmp1 = ctx->transform_coeffs[2][i]; 667 tmp1 = ctx->transform_coeffs[2][i];
668 ctx->transform_coeffs[1][i] = tmp0 + tmp1; 668 ctx->transform_coeffs[1][i] = tmp0 + tmp1;
719 * reference: Section 7.9.4 Transformation Equations 719 * reference: Section 7.9.4 Transformation Equations
720 */ 720 */
721 static inline void do_imdct(AC3DecodeContext *ctx) 721 static inline void do_imdct(AC3DecodeContext *ctx)
722 { 722 {
723 int ch; 723 int ch;
724 int nchans; 724 int channels;
725 725
726 /* Don't perform the IMDCT on the LFE channel unless it's used in the output */ 726 /* Don't perform the IMDCT on the LFE channel unless it's used in the output */
727 nchans = ctx->nfchans; 727 channels = ctx->fbw_channels;
728 if(ctx->output_mode & AC3_OUTPUT_LFEON) 728 if(ctx->output_mode & AC3_OUTPUT_LFEON)
729 nchans++; 729 channels++;
730 730
731 for (ch=1; ch<=nchans; ch++) { 731 for (ch=1; ch<=channels; ch++) {
732 if (ctx->blksw[ch]) { 732 if (ctx->block_switch[ch]) {
733 do_imdct_256(ctx, ch); 733 do_imdct_256(ctx, ch);
734 } else { 734 } else {
735 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output, 735 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
736 ctx->transform_coeffs[ch], 736 ctx->transform_coeffs[ch],
737 ctx->tmp_imdct); 737 ctx->tmp_imdct);
748 } 748 }
749 749
750 /** 750 /**
751 * Downmix the output to mono or stereo. 751 * Downmix the output to mono or stereo.
752 */ 752 */
753 static void ac3_downmix(float samples[AC3_MAX_CHANNELS][256], int nfchans, 753 static void ac3_downmix(float samples[AC3_MAX_CHANNELS][256], int fbw_channels,
754 int output_mode, float coef[AC3_MAX_CHANNELS][2]) 754 int output_mode, float coef[AC3_MAX_CHANNELS][2])
755 { 755 {
756 int i, j; 756 int i, j;
757 float v0, v1, s0, s1; 757 float v0, v1, s0, s1;
758 758
759 for(i=0; i<256; i++) { 759 for(i=0; i<256; i++) {
760 v0 = v1 = s0 = s1 = 0.0f; 760 v0 = v1 = s0 = s1 = 0.0f;
761 for(j=0; j<nfchans; j++) { 761 for(j=0; j<fbw_channels; j++) {
762 v0 += samples[j][i] * coef[j][0]; 762 v0 += samples[j][i] * coef[j][0];
763 v1 += samples[j][i] * coef[j][1]; 763 v1 += samples[j][i] * coef[j][1];
764 s0 += coef[j][0]; 764 s0 += coef[j][0];
765 s1 += coef[j][1]; 765 s1 += coef[j][1];
766 } 766 }
778 /** 778 /**
779 * Parse an audio block from AC-3 bitstream. 779 * Parse an audio block from AC-3 bitstream.
780 */ 780 */
781 static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk) 781 static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
782 { 782 {
783 int nfchans = ctx->nfchans; 783 int fbw_channels = ctx->fbw_channels;
784 int channel_mode = ctx->channel_mode; 784 int channel_mode = ctx->channel_mode;
785 int i, bnd, seg, ch; 785 int i, bnd, seg, ch;
786 GetBitContext *gb = &ctx->gb; 786 GetBitContext *gb = &ctx->gb;
787 uint8_t bit_alloc_stages[AC3_MAX_CHANNELS]; 787 uint8_t bit_alloc_stages[AC3_MAX_CHANNELS];
788 788
789 memset(bit_alloc_stages, 0, AC3_MAX_CHANNELS); 789 memset(bit_alloc_stages, 0, AC3_MAX_CHANNELS);
790 790
791 /* block switch flags */ 791 /* block switch flags */
792 for (ch = 1; ch <= nfchans; ch++) 792 for (ch = 1; ch <= fbw_channels; ch++)
793 ctx->blksw[ch] = get_bits1(gb); 793 ctx->block_switch[ch] = get_bits1(gb);
794 794
795 /* dithering flags */ 795 /* dithering flags */
796 ctx->dither_all = 1; 796 ctx->dither_all = 1;
797 for (ch = 1; ch <= nfchans; ch++) { 797 for (ch = 1; ch <= fbw_channels; ch++) {
798 ctx->dithflag[ch] = get_bits1(gb); 798 ctx->dither_flag[ch] = get_bits1(gb);
799 if(!ctx->dithflag[ch]) 799 if(!ctx->dither_flag[ch])
800 ctx->dither_all = 0; 800 ctx->dither_all = 0;
801 } 801 }
802 802
803 /* dynamic range */ 803 /* dynamic range */
804 i = !(ctx->channel_mode); 804 i = !(ctx->channel_mode);
805 do { 805 do {
806 if(get_bits1(gb)) { 806 if(get_bits1(gb)) {
807 ctx->dynrng[i] = dynrng_tab[get_bits(gb, 8)]; 807 ctx->dynamic_range[i] = dynamic_range_tab[get_bits(gb, 8)];
808 } else if(blk == 0) { 808 } else if(blk == 0) {
809 ctx->dynrng[i] = 1.0f; 809 ctx->dynamic_range[i] = 1.0f;
810 } 810 }
811 } while(i--); 811 } while(i--);
812 812
813 /* coupling strategy */ 813 /* coupling strategy */
814 if (get_bits1(gb)) { 814 if (get_bits1(gb)) {
815 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS); 815 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
816 ctx->cplinu = get_bits1(gb); 816 ctx->cpl_in_use = get_bits1(gb);
817 if (ctx->cplinu) { 817 if (ctx->cpl_in_use) {
818 /* coupling in use */ 818 /* coupling in use */
819 int cplbegf, cplendf; 819 int cpl_begin_freq, cpl_end_freq;
820 820
821 /* determine which channels are coupled */ 821 /* determine which channels are coupled */
822 for (ch = 1; ch <= nfchans; ch++) 822 for (ch = 1; ch <= fbw_channels; ch++)
823 ctx->chincpl[ch] = get_bits1(gb); 823 ctx->channel_in_cpl[ch] = get_bits1(gb);
824 824
825 /* phase flags in use */ 825 /* phase flags in use */
826 if (channel_mode == AC3_CHMODE_STEREO) 826 if (channel_mode == AC3_CHMODE_STEREO)
827 ctx->phsflginu = get_bits1(gb); 827 ctx->phase_flags_in_use = get_bits1(gb);
828 828
829 /* coupling frequency range and band structure */ 829 /* coupling frequency range and band structure */
830 cplbegf = get_bits(gb, 4); 830 cpl_begin_freq = get_bits(gb, 4);
831 cplendf = get_bits(gb, 4); 831 cpl_end_freq = get_bits(gb, 4);
832 if (3 + cplendf - cplbegf < 0) { 832 if (3 + cpl_end_freq - cpl_begin_freq < 0) {
833 av_log(ctx->avctx, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", cplendf, cplbegf); 833 av_log(ctx->avctx, AV_LOG_ERROR, "3+cplendf = %d < cplbegf = %d\n", 3+cpl_end_freq, cpl_begin_freq);
834 return -1; 834 return -1;
835 } 835 }
836 ctx->ncplbnd = ctx->ncplsubnd = 3 + cplendf - cplbegf; 836 ctx->num_cpl_bands = ctx->num_cpl_subbands = 3 + cpl_end_freq - cpl_begin_freq;
837 ctx->startmant[CPL_CH] = cplbegf * 12 + 37; 837 ctx->start_freq[CPL_CH] = cpl_begin_freq * 12 + 37;
838 ctx->endmant[CPL_CH] = cplendf * 12 + 73; 838 ctx->end_freq[CPL_CH] = cpl_end_freq * 12 + 73;
839 for (bnd = 0; bnd < ctx->ncplsubnd - 1; bnd++) { 839 for (bnd = 0; bnd < ctx->num_cpl_subbands - 1; bnd++) {
840 if (get_bits1(gb)) { 840 if (get_bits1(gb)) {
841 ctx->cplbndstrc[bnd] = 1; 841 ctx->cpl_band_struct[bnd] = 1;
842 ctx->ncplbnd--; 842 ctx->num_cpl_bands--;
843 } 843 }
844 } 844 }
845 } else { 845 } else {
846 /* coupling not in use */ 846 /* coupling not in use */
847 for (ch = 1; ch <= nfchans; ch++) 847 for (ch = 1; ch <= fbw_channels; ch++)
848 ctx->chincpl[ch] = 0; 848 ctx->channel_in_cpl[ch] = 0;
849 } 849 }
850 } 850 }
851 851
852 /* coupling coordinates */ 852 /* coupling coordinates */
853 if (ctx->cplinu) { 853 if (ctx->cpl_in_use) {
854 int cplcoe = 0; 854 int cpl_coords_exist = 0;
855 855
856 for (ch = 1; ch <= nfchans; ch++) { 856 for (ch = 1; ch <= fbw_channels; ch++) {
857 if (ctx->chincpl[ch]) { 857 if (ctx->channel_in_cpl[ch]) {
858 if (get_bits1(gb)) { 858 if (get_bits1(gb)) {
859 int mstrcplco, cplcoexp, cplcomant; 859 int master_cpl_coord, cpl_coord_exp, cpl_coord_mant;
860 cplcoe = 1; 860 cpl_coords_exist = 1;
861 mstrcplco = 3 * get_bits(gb, 2); 861 master_cpl_coord = 3 * get_bits(gb, 2);
862 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) { 862 for (bnd = 0; bnd < ctx->num_cpl_bands; bnd++) {
863 cplcoexp = get_bits(gb, 4); 863 cpl_coord_exp = get_bits(gb, 4);
864 cplcomant = get_bits(gb, 4); 864 cpl_coord_mant = get_bits(gb, 4);
865 if (cplcoexp == 15) 865 if (cpl_coord_exp == 15)
866 ctx->cplco[ch][bnd] = cplcomant / 16.0f; 866 ctx->cpl_coords[ch][bnd] = cpl_coord_mant / 16.0f;
867 else 867 else
868 ctx->cplco[ch][bnd] = (cplcomant + 16.0f) / 32.0f; 868 ctx->cpl_coords[ch][bnd] = (cpl_coord_mant + 16.0f) / 32.0f;
869 ctx->cplco[ch][bnd] *= scale_factors[cplcoexp + mstrcplco]; 869 ctx->cpl_coords[ch][bnd] *= scale_factors[cpl_coord_exp + master_cpl_coord];
870 } 870 }
871 } 871 }
872 } 872 }
873 } 873 }
874 /* phase flags */ 874 /* phase flags */
875 if (channel_mode == AC3_CHMODE_STEREO && ctx->phsflginu && cplcoe) { 875 if (channel_mode == AC3_CHMODE_STEREO && ctx->phase_flags_in_use && cpl_coords_exist) {
876 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) { 876 for (bnd = 0; bnd < ctx->num_cpl_bands; bnd++) {
877 if (get_bits1(gb)) 877 if (get_bits1(gb))
878 ctx->cplco[2][bnd] = -ctx->cplco[2][bnd]; 878 ctx->cpl_coords[2][bnd] = -ctx->cpl_coords[2][bnd];
879 } 879 }
880 } 880 }
881 } 881 }
882 882
883 /* stereo rematrixing strategy and band structure */ 883 /* stereo rematrixing strategy and band structure */
884 if (channel_mode == AC3_CHMODE_STEREO) { 884 if (channel_mode == AC3_CHMODE_STEREO) {
885 ctx->rematstr = get_bits1(gb); 885 ctx->rematrixing_strategy = get_bits1(gb);
886 if (ctx->rematstr) { 886 if (ctx->rematrixing_strategy) {
887 ctx->nrematbnd = 4; 887 ctx->num_rematrixing_bands = 4;
888 if(ctx->cplinu && ctx->startmant[CPL_CH] <= 61) 888 if(ctx->cpl_in_use && ctx->start_freq[CPL_CH] <= 61)
889 ctx->nrematbnd -= 1 + (ctx->startmant[CPL_CH] == 37); 889 ctx->num_rematrixing_bands -= 1 + (ctx->start_freq[CPL_CH] == 37);
890 for(bnd=0; bnd<ctx->nrematbnd; bnd++) 890 for(bnd=0; bnd<ctx->num_rematrixing_bands; bnd++)
891 ctx->rematflg[bnd] = get_bits1(gb); 891 ctx->rematrixing_flags[bnd] = get_bits1(gb);
892 } 892 }
893 } 893 }
894 894
895 /* exponent strategies for each channel */ 895 /* exponent strategies for each channel */
896 ctx->expstr[CPL_CH] = EXP_REUSE; 896 ctx->exp_strategy[CPL_CH] = EXP_REUSE;
897 ctx->expstr[ctx->lfe_ch] = EXP_REUSE; 897 ctx->exp_strategy[ctx->lfe_ch] = EXP_REUSE;
898 for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) { 898 for (ch = !ctx->cpl_in_use; ch <= ctx->channels; ch++) {
899 if(ch == ctx->lfe_ch) 899 if(ch == ctx->lfe_ch)
900 ctx->expstr[ch] = get_bits(gb, 1); 900 ctx->exp_strategy[ch] = get_bits(gb, 1);
901 else 901 else
902 ctx->expstr[ch] = get_bits(gb, 2); 902 ctx->exp_strategy[ch] = get_bits(gb, 2);
903 if(ctx->expstr[ch] != EXP_REUSE) 903 if(ctx->exp_strategy[ch] != EXP_REUSE)
904 bit_alloc_stages[ch] = 3; 904 bit_alloc_stages[ch] = 3;
905 } 905 }
906 906
907 /* channel bandwidth */ 907 /* channel bandwidth */
908 for (ch = 1; ch <= nfchans; ch++) { 908 for (ch = 1; ch <= fbw_channels; ch++) {
909 ctx->startmant[ch] = 0; 909 ctx->start_freq[ch] = 0;
910 if (ctx->expstr[ch] != EXP_REUSE) { 910 if (ctx->exp_strategy[ch] != EXP_REUSE) {
911 int prev = ctx->endmant[ch]; 911 int prev = ctx->end_freq[ch];
912 if (ctx->chincpl[ch]) 912 if (ctx->channel_in_cpl[ch])
913 ctx->endmant[ch] = ctx->startmant[CPL_CH]; 913 ctx->end_freq[ch] = ctx->start_freq[CPL_CH];
914 else { 914 else {
915 int chbwcod = get_bits(gb, 6); 915 int bandwidth_code = get_bits(gb, 6);
916 if (chbwcod > 60) { 916 if (bandwidth_code > 60) {
917 av_log(ctx->avctx, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod); 917 av_log(ctx->avctx, AV_LOG_ERROR, "bandwidth code = %d > 60", bandwidth_code);
918 return -1; 918 return -1;
919 } 919 }
920 ctx->endmant[ch] = chbwcod * 3 + 73; 920 ctx->end_freq[ch] = bandwidth_code * 3 + 73;
921 } 921 }
922 if(blk > 0 && ctx->endmant[ch] != prev) 922 if(blk > 0 && ctx->end_freq[ch] != prev)
923 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS); 923 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
924 } 924 }
925 } 925 }
926 ctx->startmant[ctx->lfe_ch] = 0; 926 ctx->start_freq[ctx->lfe_ch] = 0;
927 ctx->endmant[ctx->lfe_ch] = 7; 927 ctx->end_freq[ctx->lfe_ch] = 7;
928 928
929 /* decode exponents for each channel */ 929 /* decode exponents for each channel */
930 for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) { 930 for (ch = !ctx->cpl_in_use; ch <= ctx->channels; ch++) {
931 if (ctx->expstr[ch] != EXP_REUSE) { 931 if (ctx->exp_strategy[ch] != EXP_REUSE) {
932 int grpsize, ngrps; 932 int group_size, num_groups;
933 grpsize = 3 << (ctx->expstr[ch] - 1); 933 group_size = 3 << (ctx->exp_strategy[ch] - 1);
934 if(ch == CPL_CH) 934 if(ch == CPL_CH)
935 ngrps = (ctx->endmant[ch] - ctx->startmant[ch]) / grpsize; 935 num_groups = (ctx->end_freq[ch] - ctx->start_freq[ch]) / group_size;
936 else if(ch == ctx->lfe_ch) 936 else if(ch == ctx->lfe_ch)
937 ngrps = 2; 937 num_groups = 2;
938 else 938 else
939 ngrps = (ctx->endmant[ch] + grpsize - 4) / grpsize; 939 num_groups = (ctx->end_freq[ch] + group_size - 4) / group_size;
940 ctx->dexps[ch][0] = get_bits(gb, 4) << !ch; 940 ctx->dexps[ch][0] = get_bits(gb, 4) << !ch;
941 decode_exponents(gb, ctx->expstr[ch], ngrps, ctx->dexps[ch][0], 941 decode_exponents(gb, ctx->exp_strategy[ch], num_groups, ctx->dexps[ch][0],
942 &ctx->dexps[ch][ctx->startmant[ch]+!!ch]); 942 &ctx->dexps[ch][ctx->start_freq[ch]+!!ch]);
943 if(ch != CPL_CH && ch != ctx->lfe_ch) 943 if(ch != CPL_CH && ch != ctx->lfe_ch)
944 skip_bits(gb, 2); /* skip gainrng */ 944 skip_bits(gb, 2); /* skip gainrng */
945 } 945 }
946 } 946 }
947 947
950 ctx->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[get_bits(gb, 2)] >> ctx->bit_alloc_params.sr_shift; 950 ctx->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[get_bits(gb, 2)] >> ctx->bit_alloc_params.sr_shift;
951 ctx->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[get_bits(gb, 2)] >> ctx->bit_alloc_params.sr_shift; 951 ctx->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[get_bits(gb, 2)] >> ctx->bit_alloc_params.sr_shift;
952 ctx->bit_alloc_params.slow_gain = ff_ac3_slow_gain_tab[get_bits(gb, 2)]; 952 ctx->bit_alloc_params.slow_gain = ff_ac3_slow_gain_tab[get_bits(gb, 2)];
953 ctx->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[get_bits(gb, 2)]; 953 ctx->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[get_bits(gb, 2)];
954 ctx->bit_alloc_params.floor = ff_ac3_floor_tab[get_bits(gb, 3)]; 954 ctx->bit_alloc_params.floor = ff_ac3_floor_tab[get_bits(gb, 3)];
955 for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) { 955 for(ch=!ctx->cpl_in_use; ch<=ctx->channels; ch++) {
956 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2); 956 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
957 } 957 }
958 } 958 }
959 959
960 /* signal-to-noise ratio offsets and fast gains (signal-to-mask ratios) */ 960 /* signal-to-noise ratio offsets and fast gains (signal-to-mask ratios) */
961 if (get_bits1(gb)) { 961 if (get_bits1(gb)) {
962 int csnr; 962 int csnr;
963 csnr = (get_bits(gb, 6) - 15) << 4; 963 csnr = (get_bits(gb, 6) - 15) << 4;
964 for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) { /* snr offset and fast gain */ 964 for (ch = !ctx->cpl_in_use; ch <= ctx->channels; ch++) { /* snr offset and fast gain */
965 ctx->snr_offset[ch] = (csnr + get_bits(gb, 4)) << 2; 965 ctx->snr_offset[ch] = (csnr + get_bits(gb, 4)) << 2;
966 ctx->fast_gain[ch] = ff_ac3_fast_gain_tab[get_bits(gb, 3)]; 966 ctx->fast_gain[ch] = ff_ac3_fast_gain_tab[get_bits(gb, 3)];
967 } 967 }
968 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS); 968 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
969 } 969 }
970 970
971 /* coupling leak information */ 971 /* coupling leak information */
972 if (ctx->cplinu && get_bits1(gb)) { 972 if (ctx->cpl_in_use && get_bits1(gb)) {
973 ctx->bit_alloc_params.cpl_fast_leak = get_bits(gb, 3); 973 ctx->bit_alloc_params.cpl_fast_leak = get_bits(gb, 3);
974 ctx->bit_alloc_params.cpl_slow_leak = get_bits(gb, 3); 974 ctx->bit_alloc_params.cpl_slow_leak = get_bits(gb, 3);
975 bit_alloc_stages[CPL_CH] = FFMAX(bit_alloc_stages[CPL_CH], 2); 975 bit_alloc_stages[CPL_CH] = FFMAX(bit_alloc_stages[CPL_CH], 2);
976 } 976 }
977 977
978 /* delta bit allocation information */ 978 /* delta bit allocation information */
979 if (get_bits1(gb)) { 979 if (get_bits1(gb)) {
980 /* delta bit allocation exists (strategy) */ 980 /* delta bit allocation exists (strategy) */
981 for (ch = !ctx->cplinu; ch <= nfchans; ch++) { 981 for (ch = !ctx->cpl_in_use; ch <= fbw_channels; ch++) {
982 ctx->dba_mode[ch] = get_bits(gb, 2); 982 ctx->dba_mode[ch] = get_bits(gb, 2);
983 if (ctx->dba_mode[ch] == DBA_RESERVED) { 983 if (ctx->dba_mode[ch] == DBA_RESERVED) {
984 av_log(ctx->avctx, AV_LOG_ERROR, "delta bit allocation strategy reserved\n"); 984 av_log(ctx->avctx, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
985 return -1; 985 return -1;
986 } 986 }
987 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2); 987 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
988 } 988 }
989 /* channel delta offset, len and bit allocation */ 989 /* channel delta offset, len and bit allocation */
990 for (ch = !ctx->cplinu; ch <= nfchans; ch++) { 990 for (ch = !ctx->cpl_in_use; ch <= fbw_channels; ch++) {
991 if (ctx->dba_mode[ch] == DBA_NEW) { 991 if (ctx->dba_mode[ch] == DBA_NEW) {
992 ctx->dba_nsegs[ch] = get_bits(gb, 3); 992 ctx->dba_nsegs[ch] = get_bits(gb, 3);
993 for (seg = 0; seg <= ctx->dba_nsegs[ch]; seg++) { 993 for (seg = 0; seg <= ctx->dba_nsegs[ch]; seg++) {
994 ctx->dba_offsets[ch][seg] = get_bits(gb, 5); 994 ctx->dba_offsets[ch][seg] = get_bits(gb, 5);
995 ctx->dba_lengths[ch][seg] = get_bits(gb, 4); 995 ctx->dba_lengths[ch][seg] = get_bits(gb, 4);
996 ctx->dba_values[ch][seg] = get_bits(gb, 3); 996 ctx->dba_values[ch][seg] = get_bits(gb, 3);
997 } 997 }
998 } 998 }
999 } 999 }
1000 } else if(blk == 0) { 1000 } else if(blk == 0) {
1001 for(ch=0; ch<=ctx->nchans; ch++) { 1001 for(ch=0; ch<=ctx->channels; ch++) {
1002 ctx->dba_mode[ch] = DBA_NONE; 1002 ctx->dba_mode[ch] = DBA_NONE;
1003 } 1003 }
1004 } 1004 }
1005 1005
1006 /* Bit allocation */ 1006 /* Bit allocation */
1007 for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) { 1007 for(ch=!ctx->cpl_in_use; ch<=ctx->channels; ch++) {
1008 if(bit_alloc_stages[ch] > 2) { 1008 if(bit_alloc_stages[ch] > 2) {
1009 /* Exponent mapping into PSD and PSD integration */ 1009 /* Exponent mapping into PSD and PSD integration */
1010 ff_ac3_bit_alloc_calc_psd(ctx->dexps[ch], 1010 ff_ac3_bit_alloc_calc_psd(ctx->dexps[ch],
1011 ctx->startmant[ch], ctx->endmant[ch], 1011 ctx->start_freq[ch], ctx->end_freq[ch],
1012 ctx->psd[ch], ctx->band_psd[ch]); 1012 ctx->psd[ch], ctx->band_psd[ch]);
1013 } 1013 }
1014 if(bit_alloc_stages[ch] > 1) { 1014 if(bit_alloc_stages[ch] > 1) {
1015 /* Compute excitation function, Compute masking curve, and 1015 /* Compute excitation function, Compute masking curve, and
1016 Apply delta bit allocation */ 1016 Apply delta bit allocation */
1017 ff_ac3_bit_alloc_calc_mask(&ctx->bit_alloc_params, ctx->band_psd[ch], 1017 ff_ac3_bit_alloc_calc_mask(&ctx->bit_alloc_params, ctx->band_psd[ch],
1018 ctx->startmant[ch], ctx->endmant[ch], 1018 ctx->start_freq[ch], ctx->end_freq[ch],
1019 ctx->fast_gain[ch], (ch == ctx->lfe_ch), 1019 ctx->fast_gain[ch], (ch == ctx->lfe_ch),
1020 ctx->dba_mode[ch], ctx->dba_nsegs[ch], 1020 ctx->dba_mode[ch], ctx->dba_nsegs[ch],
1021 ctx->dba_offsets[ch], ctx->dba_lengths[ch], 1021 ctx->dba_offsets[ch], ctx->dba_lengths[ch],
1022 ctx->dba_values[ch], ctx->mask[ch]); 1022 ctx->dba_values[ch], ctx->mask[ch]);
1023 } 1023 }
1024 if(bit_alloc_stages[ch] > 0) { 1024 if(bit_alloc_stages[ch] > 0) {
1025 /* Compute bit allocation */ 1025 /* Compute bit allocation */
1026 ff_ac3_bit_alloc_calc_bap(ctx->mask[ch], ctx->psd[ch], 1026 ff_ac3_bit_alloc_calc_bap(ctx->mask[ch], ctx->psd[ch],
1027 ctx->startmant[ch], ctx->endmant[ch], 1027 ctx->start_freq[ch], ctx->end_freq[ch],
1028 ctx->snr_offset[ch], 1028 ctx->snr_offset[ch],
1029 ctx->bit_alloc_params.floor, 1029 ctx->bit_alloc_params.floor,
1030 ctx->bap[ch]); 1030 ctx->bap[ch]);
1031 } 1031 }
1032 } 1032 }
1048 /* recover coefficients if rematrixing is in use */ 1048 /* recover coefficients if rematrixing is in use */
1049 if(ctx->channel_mode == AC3_CHMODE_STEREO) 1049 if(ctx->channel_mode == AC3_CHMODE_STEREO)
1050 do_rematrixing(ctx); 1050 do_rematrixing(ctx);
1051 1051
1052 /* apply scaling to coefficients (headroom, dialnorm, dynrng) */ 1052 /* apply scaling to coefficients (headroom, dialnorm, dynrng) */
1053 for(ch=1; ch<=ctx->nchans; ch++) { 1053 for(ch=1; ch<=ctx->channels; ch++) {
1054 float gain = 2.0f * ctx->mul_bias; 1054 float gain = 2.0f * ctx->mul_bias;
1055 if(ctx->channel_mode == AC3_CHMODE_DUALMONO) { 1055 if(ctx->channel_mode == AC3_CHMODE_DUALMONO) {
1056 gain *= ctx->dialnorm[ch-1] * ctx->dynrng[ch-1]; 1056 gain *= ctx->dialog_norm[ch-1] * ctx->dynamic_range[ch-1];
1057 } else { 1057 } else {
1058 gain *= ctx->dialnorm[0] * ctx->dynrng[0]; 1058 gain *= ctx->dialog_norm[0] * ctx->dynamic_range[0];
1059 } 1059 }
1060 for(i=0; i<ctx->endmant[ch]; i++) { 1060 for(i=0; i<ctx->end_freq[ch]; i++) {
1061 ctx->transform_coeffs[ch][i] *= gain; 1061 ctx->transform_coeffs[ch][i] *= gain;
1062 } 1062 }
1063 } 1063 }
1064 1064
1065 do_imdct(ctx); 1065 do_imdct(ctx);
1066 1066
1067 /* downmix output if needed */ 1067 /* downmix output if needed */
1068 if(ctx->nchans != ctx->out_channels && !((ctx->output_mode & AC3_OUTPUT_LFEON) && 1068 if(ctx->channels != ctx->out_channels && !((ctx->output_mode & AC3_OUTPUT_LFEON) &&
1069 ctx->nfchans == ctx->out_channels)) { 1069 ctx->fbw_channels == ctx->out_channels)) {
1070 ac3_downmix(ctx->output, ctx->nfchans, ctx->output_mode, 1070 ac3_downmix(ctx->output, ctx->fbw_channels, ctx->output_mode,
1071 ctx->downmix_coeffs); 1071 ctx->downmix_coeffs);
1072 } 1072 }
1073 1073
1074 /* convert float to 16-bit integer */ 1074 /* convert float to 16-bit integer */
1075 for(ch=0; ch<ctx->out_channels; ch++) { 1075 for(ch=0; ch<ctx->out_channels; ch++) {
1125 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n"); 1125 av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
1126 return -1; 1126 return -1;
1127 } 1127 }
1128 1128
1129 /* channel config */ 1129 /* channel config */
1130 ctx->out_channels = ctx->nchans; 1130 ctx->out_channels = ctx->channels;
1131 if (avctx->channels == 0) { 1131 if (avctx->channels == 0) {
1132 avctx->channels = ctx->out_channels; 1132 avctx->channels = ctx->out_channels;
1133 } else if(ctx->out_channels < avctx->channels) { 1133 } else if(ctx->out_channels < avctx->channels) {
1134 av_log(avctx, AV_LOG_ERROR, "Cannot upmix AC3 from %d to %d channels.\n", 1134 av_log(avctx, AV_LOG_ERROR, "Cannot upmix AC3 from %d to %d channels.\n",
1135 ctx->out_channels, avctx->channels); 1135 ctx->out_channels, avctx->channels);