comparison ac3dec.c @ 6430:de7502093922 libavcodec

Use 24-bit fixed-point transform coefficients until just before MDCT. This gives 7% faster decoding on average.
author jbr
date Sat, 01 Mar 2008 04:39:39 +0000
parents a35b838ab955
children 305c49259b59
comparison
equal deleted inserted replaced
6429:3c67363d6fa0 6430:de7502093922
43 * Table of bin locations for rematrixing bands 43 * Table of bin locations for rematrixing bands
44 * reference: Section 7.5.2 Rematrixing : Frequency Band Definitions 44 * reference: Section 7.5.2 Rematrixing : Frequency Band Definitions
45 */ 45 */
46 static const uint8_t rematrix_band_tab[5] = { 13, 25, 37, 61, 253 }; 46 static const uint8_t rematrix_band_tab[5] = { 13, 25, 37, 61, 253 };
47 47
48 /**
49 * table for exponent to scale_factor mapping
50 * scale_factors[i] = 2 ^ -i
51 */
52 static float scale_factors[25];
53
54 /** table for grouping exponents */ 48 /** table for grouping exponents */
55 static uint8_t exp_ungroup_tab[128][3]; 49 static uint8_t exp_ungroup_tab[128][3];
56 50
57 51
58 /** tables for ungrouping mantissas */ 52 /** tables for ungrouping mantissas */
59 static float b1_mantissas[32][3]; 53 static int b1_mantissas[32][3];
60 static float b2_mantissas[128][3]; 54 static int b2_mantissas[128][3];
61 static float b3_mantissas[8]; 55 static int b3_mantissas[8];
62 static float b4_mantissas[128][2]; 56 static int b4_mantissas[128][2];
63 static float b5_mantissas[16]; 57 static int b5_mantissas[16];
64 58
65 /** 59 /**
66 * Quantization table: levels for symmetric. bits for asymmetric. 60 * Quantization table: levels for symmetric. bits for asymmetric.
67 * reference: Table 7.18 Mapping of bap to Quantizer 61 * reference: Table 7.18 Mapping of bap to Quantizer
68 */ 62 */
159 153
160 int center_mix_level; ///< Center mix level index 154 int center_mix_level; ///< Center mix level index
161 int surround_mix_level; ///< Surround mix level index 155 int surround_mix_level; ///< Surround mix level index
162 float downmix_coeffs[AC3_MAX_CHANNELS][2]; ///< stereo downmix coefficients 156 float downmix_coeffs[AC3_MAX_CHANNELS][2]; ///< stereo downmix coefficients
163 float dynamic_range[2]; ///< dynamic range 157 float dynamic_range[2]; ///< dynamic range
164 float cpl_coords[AC3_MAX_CHANNELS][18]; ///< coupling coordinates 158 int cpl_coords[AC3_MAX_CHANNELS][18]; ///< coupling coordinates
165 int num_cpl_bands; ///< number of coupling bands 159 int num_cpl_bands; ///< number of coupling bands
166 int num_cpl_subbands; ///< number of coupling sub bands 160 int num_cpl_subbands; ///< number of coupling sub bands
167 int start_freq[AC3_MAX_CHANNELS]; ///< start frequency bin 161 int start_freq[AC3_MAX_CHANNELS]; ///< start frequency bin
168 int end_freq[AC3_MAX_CHANNELS]; ///< end frequency bin 162 int end_freq[AC3_MAX_CHANNELS]; ///< end frequency bin
169 AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters 163 AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
172 uint8_t bap[AC3_MAX_CHANNELS][256]; ///< bit allocation pointers 166 uint8_t bap[AC3_MAX_CHANNELS][256]; ///< bit allocation pointers
173 int16_t psd[AC3_MAX_CHANNELS][256]; ///< scaled exponents 167 int16_t psd[AC3_MAX_CHANNELS][256]; ///< scaled exponents
174 int16_t band_psd[AC3_MAX_CHANNELS][50]; ///< interpolated exponents 168 int16_t band_psd[AC3_MAX_CHANNELS][50]; ///< interpolated exponents
175 int16_t mask[AC3_MAX_CHANNELS][50]; ///< masking curve values 169 int16_t mask[AC3_MAX_CHANNELS][50]; ///< masking curve values
176 170
171 int fixed_coeffs[AC3_MAX_CHANNELS][256]; ///> fixed-point transform coefficients
177 DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][256]); ///< transform coefficients 172 DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][256]); ///< transform coefficients
178 173
179 /* For IMDCT. */ 174 /* For IMDCT. */
180 MDCTContext imdct_512; ///< for 512 sample IMDCT 175 MDCTContext imdct_512; ///< for 512 sample IMDCT
181 MDCTContext imdct_256; ///< for 256 sample IMDCT 176 MDCTContext imdct_256; ///< for 256 sample IMDCT
199 /** 194 /**
200 * Symmetrical Dequantization 195 * Symmetrical Dequantization
201 * reference: Section 7.3.3 Expansion of Mantissas for Symmetrical Quantization 196 * reference: Section 7.3.3 Expansion of Mantissas for Symmetrical Quantization
202 * Tables 7.19 to 7.23 197 * Tables 7.19 to 7.23
203 */ 198 */
204 static inline float 199 static inline int
205 symmetric_dequant(int code, int levels) 200 symmetric_dequant(int code, int levels)
206 { 201 {
207 return (code - (levels >> 1)) * (2.0f / levels); 202 return ((code - (levels >> 1)) << 24) / levels;
208 } 203 }
209 204
210 /* 205 /*
211 * Initialize tables at runtime. 206 * Initialize tables at runtime.
212 */ 207 */
247 reference: Section 7.7.1 Dynamic Range Control */ 242 reference: Section 7.7.1 Dynamic Range Control */
248 for(i=0; i<256; i++) { 243 for(i=0; i<256; i++) {
249 int v = (i >> 5) - ((i >> 7) << 3) - 5; 244 int v = (i >> 5) - ((i >> 7) << 3) - 5;
250 dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0x1F) | 0x20); 245 dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0x1F) | 0x20);
251 } 246 }
252
253 /* generate scale factors for exponents and asymmetrical dequantization
254 reference: Section 7.3.2 Expansion of Mantissas for Asymmetric Quantization */
255 for (i = 0; i < 25; i++)
256 scale_factors[i] = pow(2.0, -i);
257 247
258 /* generate exponent tables 248 /* generate exponent tables
259 reference: Section 7.1.3 Exponent Decoding */ 249 reference: Section 7.1.3 Exponent Decoding */
260 for(i=0; i<128; i++) { 250 for(i=0; i<128; i++) {
261 exp_ungroup_tab[i][0] = i / 25; 251 exp_ungroup_tab[i][0] = i / 25;
459 do { 449 do {
460 subbnd++; 450 subbnd++;
461 for(j=0; j<12; j++) { 451 for(j=0; j<12; j++) {
462 for(ch=1; ch<=s->fbw_channels; ch++) { 452 for(ch=1; ch<=s->fbw_channels; ch++) {
463 if(s->channel_in_cpl[ch]) { 453 if(s->channel_in_cpl[ch]) {
464 s->transform_coeffs[ch][i] = s->transform_coeffs[CPL_CH][i] * s->cpl_coords[ch][bnd] * 8.0f; 454 s->fixed_coeffs[ch][i] = ((int64_t)s->fixed_coeffs[CPL_CH][i] * (int64_t)s->cpl_coords[ch][bnd]) >> 23;
465 if (ch == 2 && s->phase_flags[bnd]) 455 if (ch == 2 && s->phase_flags[bnd])
466 s->transform_coeffs[ch][i] = -s->transform_coeffs[ch][i]; 456 s->fixed_coeffs[ch][i] = -s->fixed_coeffs[ch][i];
467 } 457 }
468 } 458 }
469 i++; 459 i++;
470 } 460 }
471 } while(s->cpl_band_struct[subbnd]); 461 } while(s->cpl_band_struct[subbnd]);
474 464
475 /** 465 /**
476 * Grouped mantissas for 3-level 5-level and 11-level quantization 466 * Grouped mantissas for 3-level 5-level and 11-level quantization
477 */ 467 */
478 typedef struct { 468 typedef struct {
479 float b1_mant[3]; 469 int b1_mant[3];
480 float b2_mant[3]; 470 int b2_mant[3];
481 float b4_mant[2]; 471 int b4_mant[2];
482 int b1ptr; 472 int b1ptr;
483 int b2ptr; 473 int b2ptr;
484 int b4ptr; 474 int b4ptr;
485 } mant_groups; 475 } mant_groups;
486 476
492 { 482 {
493 GetBitContext *gbc = &s->gbc; 483 GetBitContext *gbc = &s->gbc;
494 int i, gcode, tbap, start, end; 484 int i, gcode, tbap, start, end;
495 uint8_t *exps; 485 uint8_t *exps;
496 uint8_t *bap; 486 uint8_t *bap;
497 float *coeffs; 487 int *coeffs;
498 488
499 exps = s->dexps[ch_index]; 489 exps = s->dexps[ch_index];
500 bap = s->bap[ch_index]; 490 bap = s->bap[ch_index];
501 coeffs = s->transform_coeffs[ch_index]; 491 coeffs = s->fixed_coeffs[ch_index];
502 start = s->start_freq[ch_index]; 492 start = s->start_freq[ch_index];
503 end = s->end_freq[ch_index]; 493 end = s->end_freq[ch_index];
504 494
505 for (i = start; i < end; i++) { 495 for (i = start; i < end; i++) {
506 tbap = bap[i]; 496 tbap = bap[i];
507 switch (tbap) { 497 switch (tbap) {
508 case 0: 498 case 0:
509 coeffs[i] = ((av_random(&s->dith_state) & 0xFFFF) / 65535.0f) - 0.5f; 499 coeffs[i] = (av_random(&s->dith_state) & 0x7FFFFF) - 4194304;
510 break; 500 break;
511 501
512 case 1: 502 case 1:
513 if(m->b1ptr > 2) { 503 if(m->b1ptr > 2) {
514 gcode = get_bits(gbc, 5); 504 gcode = get_bits(gbc, 5);
547 537
548 case 5: 538 case 5:
549 coeffs[i] = b5_mantissas[get_bits(gbc, 4)]; 539 coeffs[i] = b5_mantissas[get_bits(gbc, 4)];
550 break; 540 break;
551 541
552 default: 542 default: {
553 /* asymmetric dequantization */ 543 /* asymmetric dequantization */
554 coeffs[i] = get_sbits(gbc, quantization_tab[tbap]) * scale_factors[quantization_tab[tbap]-1]; 544 int qlevel = quantization_tab[tbap];
545 coeffs[i] = get_sbits(gbc, qlevel) << (24 - qlevel);
555 break; 546 break;
556 } 547 }
557 coeffs[i] *= scale_factors[exps[i]]; 548 }
549 coeffs[i] >>= exps[i];
558 } 550 }
559 551
560 return 0; 552 return 0;
561 } 553 }
562 554
565 * reference: Section 7.3.4 Dither for Zero Bit Mantissas (bap=0) 557 * reference: Section 7.3.4 Dither for Zero Bit Mantissas (bap=0)
566 */ 558 */
567 static void remove_dithering(AC3DecodeContext *s) { 559 static void remove_dithering(AC3DecodeContext *s) {
568 int ch, i; 560 int ch, i;
569 int end=0; 561 int end=0;
570 float *coeffs; 562 int *coeffs;
571 uint8_t *bap; 563 uint8_t *bap;
572 564
573 for(ch=1; ch<=s->fbw_channels; ch++) { 565 for(ch=1; ch<=s->fbw_channels; ch++) {
574 if(!s->dither_flag[ch]) { 566 if(!s->dither_flag[ch]) {
575 coeffs = s->transform_coeffs[ch]; 567 coeffs = s->fixed_coeffs[ch];
576 bap = s->bap[ch]; 568 bap = s->bap[ch];
577 if(s->channel_in_cpl[ch]) 569 if(s->channel_in_cpl[ch])
578 end = s->start_freq[CPL_CH]; 570 end = s->start_freq[CPL_CH];
579 else 571 else
580 end = s->end_freq[ch]; 572 end = s->end_freq[ch];
581 for(i=0; i<end; i++) { 573 for(i=0; i<end; i++) {
582 if(!bap[i]) 574 if(!bap[i])
583 coeffs[i] = 0.0f; 575 coeffs[i] = 0;
584 } 576 }
585 if(s->channel_in_cpl[ch]) { 577 if(s->channel_in_cpl[ch]) {
586 bap = s->bap[CPL_CH]; 578 bap = s->bap[CPL_CH];
587 for(; i<s->end_freq[CPL_CH]; i++) { 579 for(; i<s->end_freq[CPL_CH]; i++) {
588 if(!bap[i]) 580 if(!bap[i])
589 coeffs[i] = 0.0f; 581 coeffs[i] = 0;
590 } 582 }
591 } 583 }
592 } 584 }
593 } 585 }
594 } 586 }
641 */ 633 */
642 static void do_rematrixing(AC3DecodeContext *s) 634 static void do_rematrixing(AC3DecodeContext *s)
643 { 635 {
644 int bnd, i; 636 int bnd, i;
645 int end, bndend; 637 int end, bndend;
646 float tmp0, tmp1; 638 int tmp0, tmp1;
647 639
648 end = FFMIN(s->end_freq[1], s->end_freq[2]); 640 end = FFMIN(s->end_freq[1], s->end_freq[2]);
649 641
650 for(bnd=0; bnd<s->num_rematrixing_bands; bnd++) { 642 for(bnd=0; bnd<s->num_rematrixing_bands; bnd++) {
651 if(s->rematrixing_flags[bnd]) { 643 if(s->rematrixing_flags[bnd]) {
652 bndend = FFMIN(end, rematrix_band_tab[bnd+1]); 644 bndend = FFMIN(end, rematrix_band_tab[bnd+1]);
653 for(i=rematrix_band_tab[bnd]; i<bndend; i++) { 645 for(i=rematrix_band_tab[bnd]; i<bndend; i++) {
654 tmp0 = s->transform_coeffs[1][i]; 646 tmp0 = s->fixed_coeffs[1][i];
655 tmp1 = s->transform_coeffs[2][i]; 647 tmp1 = s->fixed_coeffs[2][i];
656 s->transform_coeffs[1][i] = tmp0 + tmp1; 648 s->fixed_coeffs[1][i] = tmp0 + tmp1;
657 s->transform_coeffs[2][i] = tmp0 - tmp1; 649 s->fixed_coeffs[2][i] = tmp0 - tmp1;
658 } 650 }
659 } 651 }
660 } 652 }
661 } 653 }
662 654
849 master_cpl_coord = 3 * get_bits(gbc, 2); 841 master_cpl_coord = 3 * get_bits(gbc, 2);
850 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) { 842 for (bnd = 0; bnd < s->num_cpl_bands; bnd++) {
851 cpl_coord_exp = get_bits(gbc, 4); 843 cpl_coord_exp = get_bits(gbc, 4);
852 cpl_coord_mant = get_bits(gbc, 4); 844 cpl_coord_mant = get_bits(gbc, 4);
853 if (cpl_coord_exp == 15) 845 if (cpl_coord_exp == 15)
854 s->cpl_coords[ch][bnd] = cpl_coord_mant / 16.0f; 846 s->cpl_coords[ch][bnd] = cpl_coord_mant << 22;
855 else 847 else
856 s->cpl_coords[ch][bnd] = (cpl_coord_mant + 16.0f) / 32.0f; 848 s->cpl_coords[ch][bnd] = (cpl_coord_mant + 16) << 21;
857 s->cpl_coords[ch][bnd] *= scale_factors[cpl_coord_exp + master_cpl_coord]; 849 s->cpl_coords[ch][bnd] >>= (cpl_coord_exp + master_cpl_coord);
858 } 850 }
859 } 851 }
860 } 852 }
861 } 853 }
862 /* phase flags */ 854 /* phase flags */
1035 if(s->channel_mode == AC3_CHMODE_STEREO) 1027 if(s->channel_mode == AC3_CHMODE_STEREO)
1036 do_rematrixing(s); 1028 do_rematrixing(s);
1037 1029
1038 /* apply scaling to coefficients (headroom, dynrng) */ 1030 /* apply scaling to coefficients (headroom, dynrng) */
1039 for(ch=1; ch<=s->channels; ch++) { 1031 for(ch=1; ch<=s->channels; ch++) {
1040 float gain = 2.0f * s->mul_bias; 1032 float gain = s->mul_bias / 4194304.0f;
1041 if(s->channel_mode == AC3_CHMODE_DUALMONO) { 1033 if(s->channel_mode == AC3_CHMODE_DUALMONO) {
1042 gain *= s->dynamic_range[ch-1]; 1034 gain *= s->dynamic_range[ch-1];
1043 } else { 1035 } else {
1044 gain *= s->dynamic_range[0]; 1036 gain *= s->dynamic_range[0];
1045 } 1037 }
1046 for(i=0; i<s->end_freq[ch]; i++) { 1038 for(i=0; i<256; i++) {
1047 s->transform_coeffs[ch][i] *= gain; 1039 s->transform_coeffs[ch][i] = s->fixed_coeffs[ch][i] * gain;
1048 } 1040 }
1049 } 1041 }
1050 1042
1051 do_imdct(s); 1043 do_imdct(s);
1052 1044