comparison wmadec.c @ 4785:4ae9ab738aec libavcodec

WMA decoder improvement, output closer to the dmo binary. Patch by Ian Braithwaite ian braithwaite dot dk [Ffmpeg-devel] WMA decoder improvement, 2007-03-28 15:50
author banan
date Wed, 04 Apr 2007 13:49:55 +0000
parents 99d9dd34903b
children a10ebd496bd9
comparison
equal deleted inserted replaced
4784:7738ad31b6e2 4785:4ae9ab738aec
128 128
129 return 0; 129 return 0;
130 } 130 }
131 131
132 /** 132 /**
133 * interpolate values for a bigger or smaller block. The block must
134 * have multiple sizes
135 */
136 static void interpolate_array(float *scale, int old_size, int new_size)
137 {
138 int i, j, jincr, k;
139 float v;
140
141 if (new_size > old_size) {
142 jincr = new_size / old_size;
143 j = new_size;
144 for(i = old_size - 1; i >=0; i--) {
145 v = scale[i];
146 k = jincr;
147 do {
148 scale[--j] = v;
149 } while (--k);
150 }
151 } else if (new_size < old_size) {
152 j = 0;
153 jincr = old_size / new_size;
154 for(i = 0; i < new_size; i++) {
155 scale[i] = scale[j];
156 j += jincr;
157 }
158 }
159 }
160
161 /**
162 * compute x^-0.25 with an exponent and mantissa table. We use linear 133 * compute x^-0.25 with an exponent and mantissa table. We use linear
163 * interpolation to reduce the mantissa table size at a small speed 134 * interpolation to reduce the mantissa table size at a small speed
164 * expense (linear interpolation approximately doubles the number of 135 * expense (linear interpolation approximately doubles the number of
165 * bits of precision). 136 * bits of precision).
166 */ 137 */
376 * unrecorrable error. 347 * unrecorrable error.
377 */ 348 */
378 static int wma_decode_block(WMACodecContext *s) 349 static int wma_decode_block(WMACodecContext *s)
379 { 350 {
380 int n, v, a, ch, code, bsize; 351 int n, v, a, ch, code, bsize;
381 int coef_nb_bits, total_gain, parse_exponents; 352 int coef_nb_bits, total_gain;
382 int nb_coefs[MAX_CHANNELS]; 353 int nb_coefs[MAX_CHANNELS];
383 float mdct_norm; 354 float mdct_norm;
384 355
385 #ifdef TRACE 356 #ifdef TRACE
386 tprintf(s->avctx, "***decode_block: %d:%d\n", s->frame_count - 1, s->block_num); 357 tprintf(s->avctx, "***decode_block: %d:%d\n", s->frame_count - 1, s->block_num);
491 } 462 }
492 } 463 }
493 } 464 }
494 } 465 }
495 466
496 /* exposant can be interpolated in short blocks. */ 467 /* exponents can be reused in short blocks. */
497 parse_exponents = 1; 468 if ((s->block_len_bits == s->frame_len_bits) ||
498 if (s->block_len_bits != s->frame_len_bits) { 469 get_bits(&s->gb, 1)) {
499 parse_exponents = get_bits(&s->gb, 1);
500 }
501
502 if (parse_exponents) {
503 for(ch = 0; ch < s->nb_channels; ch++) { 470 for(ch = 0; ch < s->nb_channels; ch++) {
504 if (s->channel_coded[ch]) { 471 if (s->channel_coded[ch]) {
505 if (s->use_exp_vlc) { 472 if (s->use_exp_vlc) {
506 if (decode_exp_vlc(s, ch) < 0) 473 if (decode_exp_vlc(s, ch) < 0)
507 return -1; 474 return -1;
508 } else { 475 } else {
509 decode_exp_lsp(s, ch); 476 decode_exp_lsp(s, ch);
510 } 477 }
511 } 478 s->exponents_bsize[ch] = bsize;
512 }
513 } else {
514 for(ch = 0; ch < s->nb_channels; ch++) {
515 if (s->channel_coded[ch]) {
516 interpolate_array(s->exponents[ch], 1 << s->prev_block_len_bits,
517 s->block_len);
518 } 479 }
519 } 480 }
520 } 481 }
521 482
522 /* parse spectral coefficients : just RLE encoding */ 483 /* parse spectral coefficients : just RLE encoding */
586 547
587 /* finally compute the MDCT coefficients */ 548 /* finally compute the MDCT coefficients */
588 for(ch = 0; ch < s->nb_channels; ch++) { 549 for(ch = 0; ch < s->nb_channels; ch++) {
589 if (s->channel_coded[ch]) { 550 if (s->channel_coded[ch]) {
590 int16_t *coefs1; 551 int16_t *coefs1;
591 float *coefs, *exponents, mult, mult1, noise, *exp_ptr; 552 float *coefs, *exponents, mult, mult1, noise;
592 int i, j, n, n1, last_high_band; 553 int i, j, n, n1, last_high_band, esize;
593 float exp_power[HIGH_BAND_MAX_SIZE]; 554 float exp_power[HIGH_BAND_MAX_SIZE];
594 555
595 coefs1 = s->coefs1[ch]; 556 coefs1 = s->coefs1[ch];
596 exponents = s->exponents[ch]; 557 exponents = s->exponents[ch];
558 esize = s->exponents_bsize[ch];
597 mult = pow(10, total_gain * 0.05) / s->max_exponent[ch]; 559 mult = pow(10, total_gain * 0.05) / s->max_exponent[ch];
598 mult *= mdct_norm; 560 mult *= mdct_norm;
599 coefs = s->coefs[ch]; 561 coefs = s->coefs[ch];
600 if (s->use_noise_coding) { 562 if (s->use_noise_coding) {
601 mult1 = mult; 563 mult1 = mult;
602 /* very low freqs : noise */ 564 /* very low freqs : noise */
603 for(i = 0;i < s->coefs_start; i++) { 565 for(i = 0;i < s->coefs_start; i++) {
604 *coefs++ = s->noise_table[s->noise_index] * (*exponents++) * mult1; 566 *coefs++ = s->noise_table[s->noise_index] *
567 exponents[i<<bsize>>esize] * mult1;
605 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); 568 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
606 } 569 }
607 570
608 n1 = s->exponent_high_sizes[bsize]; 571 n1 = s->exponent_high_sizes[bsize];
609 572
610 /* compute power of high bands */ 573 /* compute power of high bands */
611 exp_ptr = exponents + 574 exponents = s->exponents[ch] +
612 s->high_band_start[bsize] - 575 (s->high_band_start[bsize]<<bsize);
613 s->coefs_start;
614 last_high_band = 0; /* avoid warning */ 576 last_high_band = 0; /* avoid warning */
615 for(j=0;j<n1;j++) { 577 for(j=0;j<n1;j++) {
616 n = s->exponent_high_bands[s->frame_len_bits - 578 n = s->exponent_high_bands[s->frame_len_bits -
617 s->block_len_bits][j]; 579 s->block_len_bits][j];
618 if (s->high_band_coded[ch][j]) { 580 if (s->high_band_coded[ch][j]) {
619 float e2, v; 581 float e2, v;
620 e2 = 0; 582 e2 = 0;
621 for(i = 0;i < n; i++) { 583 for(i = 0;i < n; i++) {
622 v = exp_ptr[i]; 584 v = exponents[i<<bsize>>esize];
623 e2 += v * v; 585 e2 += v * v;
624 } 586 }
625 exp_power[j] = e2 / n; 587 exp_power[j] = e2 / n;
626 last_high_band = j; 588 last_high_band = j;
627 tprintf(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n); 589 tprintf(s->avctx, "%d: power=%f (%d)\n", j, exp_power[j], n);
628 } 590 }
629 exp_ptr += n; 591 exponents += n<<bsize;
630 } 592 }
631 593
632 /* main freqs and high freqs */ 594 /* main freqs and high freqs */
595 exponents = s->exponents[ch] + (s->coefs_start<<bsize);
633 for(j=-1;j<n1;j++) { 596 for(j=-1;j<n1;j++) {
634 if (j < 0) { 597 if (j < 0) {
635 n = s->high_band_start[bsize] - 598 n = s->high_band_start[bsize] -
636 s->coefs_start; 599 s->coefs_start;
637 } else { 600 } else {
646 mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult); 609 mult1 = mult1 / (s->max_exponent[ch] * s->noise_mult);
647 mult1 *= mdct_norm; 610 mult1 *= mdct_norm;
648 for(i = 0;i < n; i++) { 611 for(i = 0;i < n; i++) {
649 noise = s->noise_table[s->noise_index]; 612 noise = s->noise_table[s->noise_index];
650 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); 613 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
651 *coefs++ = (*exponents++) * noise * mult1; 614 *coefs++ = noise *
615 exponents[i<<bsize>>esize] * mult1;
652 } 616 }
617 exponents += n<<bsize;
653 } else { 618 } else {
654 /* coded values + small noise */ 619 /* coded values + small noise */
655 for(i = 0;i < n; i++) { 620 for(i = 0;i < n; i++) {
656 noise = s->noise_table[s->noise_index]; 621 noise = s->noise_table[s->noise_index];
657 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); 622 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
658 *coefs++ = ((*coefs1++) + noise) * (*exponents++) * mult; 623 *coefs++ = ((*coefs1++) + noise) *
624 exponents[i<<bsize>>esize] * mult;
659 } 625 }
626 exponents += n<<bsize;
660 } 627 }
661 } 628 }
662 629
663 /* very high freqs : noise */ 630 /* very high freqs : noise */
664 n = s->block_len - s->coefs_end[bsize]; 631 n = s->block_len - s->coefs_end[bsize];
665 mult1 = mult * exponents[-1]; 632 mult1 = mult * exponents[((-1<<bsize))>>esize];
666 for(i = 0; i < n; i++) { 633 for(i = 0; i < n; i++) {
667 *coefs++ = s->noise_table[s->noise_index] * mult1; 634 *coefs++ = s->noise_table[s->noise_index] * mult1;
668 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1); 635 s->noise_index = (s->noise_index + 1) & (NOISE_TAB_SIZE - 1);
669 } 636 }
670 } else { 637 } else {
671 /* XXX: optimize more */ 638 /* XXX: optimize more */
672 for(i = 0;i < s->coefs_start; i++) 639 for(i = 0;i < s->coefs_start; i++)
673 *coefs++ = 0.0; 640 *coefs++ = 0.0;
674 n = nb_coefs[ch]; 641 n = nb_coefs[ch];
675 for(i = 0;i < n; i++) { 642 for(i = 0;i < n; i++) {
676 *coefs++ = coefs1[i] * exponents[i] * mult; 643 *coefs++ = coefs1[i] * exponents[i<<bsize>>esize] * mult;
677 } 644 }
678 n = s->block_len - s->coefs_end[bsize]; 645 n = s->block_len - s->coefs_end[bsize];
679 for(i = 0;i < n; i++) 646 for(i = 0;i < n; i++)
680 *coefs++ = 0.0; 647 *coefs++ = 0.0;
681 } 648 }