summaryrefslogtreecommitdiff
path: root/core/multimedia/opieplayer/libmad/layer3.c
Unidiff
Diffstat (limited to 'core/multimedia/opieplayer/libmad/layer3.c') (more/less context) (ignore whitespace changes)
-rw-r--r--core/multimedia/opieplayer/libmad/layer3.c150
1 files changed, 80 insertions, 70 deletions
diff --git a/core/multimedia/opieplayer/libmad/layer3.c b/core/multimedia/opieplayer/libmad/layer3.c
index 194fc7e..03f13fe 100644
--- a/core/multimedia/opieplayer/libmad/layer3.c
+++ b/core/multimedia/opieplayer/libmad/layer3.c
@@ -1,77 +1,85 @@
1/* 1/*
2 * mad - MPEG audio decoder 2 * libmad - MPEG audio decoder library
3 * Copyright (C) 2000-2001 Robert Leslie 3 * Copyright (C) 2000-2001 Robert Leslie
4 * 4 *
5 * This program is free software; you can redistribute it and/or modify 5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by 6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or 7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version. 8 * (at your option) any later version.
9 * 9 *
10 * This program is distributed in the hope that it will be useful, 10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details. 13 * GNU General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU General Public License 15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software 16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * 18 *
19 * $Id$ 19 * $Id$
20 */ 20 */
21 21
22# ifdef HAVE_CONFIG_H 22# ifdef HAVE_CONFIG_H
23# include "libmad_config.h" 23# include "libmad_config.h"
24# endif 24# endif
25 25
26# include "libmad_global.h" 26# include "libmad_global.h"
27 27
28# include <stdlib.h> 28# include <stdlib.h>
29# include <string.h> 29# include <string.h>
30# include <assert.h> 30
31# ifdef HAVE_ASSERT_H
32# include <assert.h>
33# endif
31 34
32# ifdef HAVE_LIMITS_H 35# ifdef HAVE_LIMITS_H
33# include <limits.h> 36# include <limits.h>
34# else 37# else
35# define CHAR_BIT 8 38# define CHAR_BIT 8
36# endif 39# endif
37 40
38# include "fixed.h" 41# include "fixed.h"
39# include "bit.h" 42# include "bit.h"
40# include "stream.h" 43# include "stream.h"
41# include "frame.h" 44# include "frame.h"
42# include "huffman.h" 45# include "huffman.h"
43# include "layer3.h" 46# include "layer3.h"
44 47
45/* --- Layer III ----------------------------------------------------------- */ 48/* --- Layer III ----------------------------------------------------------- */
46 49
47enum { 50enum {
48 count1table_select = 0x01, 51 count1table_select = 0x01,
49 scalefac_scale = 0x02, 52 scalefac_scale = 0x02,
50 preflag = 0x04, 53 preflag = 0x04,
51 mixed_block_flag = 0x08 54 mixed_block_flag = 0x08
52}; 55};
53 56
57enum {
58 I_STEREO = 0x1,
59 MS_STEREO = 0x2
60};
61
54struct sideinfo { 62struct sideinfo {
55 unsigned int main_data_begin; 63 unsigned int main_data_begin;
56 unsigned int private_bits; 64 unsigned int private_bits;
57 65
58 unsigned char scfsi[2]; 66 unsigned char scfsi[2];
59 67
60 struct granule { 68 struct granule {
61 struct channel { 69 struct channel {
62 /* from side info */ 70 /* from side info */
63 unsigned short part2_3_length; 71 unsigned short part2_3_length;
64 unsigned short big_values; 72 unsigned short big_values;
65 unsigned short global_gain; 73 unsigned short global_gain;
66 unsigned short scalefac_compress; 74 unsigned short scalefac_compress;
67 75
68 unsigned char flags; 76 unsigned char flags;
69 unsigned char block_type; 77 unsigned char block_type;
70 unsigned char table_select[3]; 78 unsigned char table_select[3];
71 unsigned char subblock_gain[3]; 79 unsigned char subblock_gain[3];
72 unsigned char region0_count; 80 unsigned char region0_count;
73 unsigned char region1_count; 81 unsigned char region1_count;
74 82
75 /* from main_data */ 83 /* from main_data */
76 unsigned char scalefac[39];/* scalefac_l and/or scalefac_s */ 84 unsigned char scalefac[39];/* scalefac_l and/or scalefac_s */
77 } ch[2]; 85 } ch[2];
@@ -482,49 +490,49 @@ mad_fixed_t const is_lsf_table[2][15] = {
482 MAD_F(0x02000000) /* 0.125000000 */, 490 MAD_F(0x02000000) /* 0.125000000 */,
483 MAD_F(0x016a09e6) /* 0.088388348 */, 491 MAD_F(0x016a09e6) /* 0.088388348 */,
484 MAD_F(0x01000000) /* 0.062500000 */, 492 MAD_F(0x01000000) /* 0.062500000 */,
485 MAD_F(0x00b504f3) /* 0.044194174 */, 493 MAD_F(0x00b504f3) /* 0.044194174 */,
486 MAD_F(0x00800000) /* 0.031250000 */, 494 MAD_F(0x00800000) /* 0.031250000 */,
487 MAD_F(0x005a827a) /* 0.022097087 */, 495 MAD_F(0x005a827a) /* 0.022097087 */,
488 MAD_F(0x00400000) /* 0.015625000 */, 496 MAD_F(0x00400000) /* 0.015625000 */,
489 MAD_F(0x002d413d) /* 0.011048543 */, 497 MAD_F(0x002d413d) /* 0.011048543 */,
490 MAD_F(0x00200000) /* 0.007812500 */, 498 MAD_F(0x00200000) /* 0.007812500 */,
491 MAD_F(0x0016a09e) /* 0.005524272 */ 499 MAD_F(0x0016a09e) /* 0.005524272 */
492 } 500 }
493}; 501};
494 502
495/* 503/*
496 * NAME:III_sideinfo() 504 * NAME:III_sideinfo()
497 * DESCRIPTION:decode frame side information from a bitstream 505 * DESCRIPTION:decode frame side information from a bitstream
498 */ 506 */
499static 507static
500enum mad_error III_sideinfo(struct mad_bitptr *ptr, unsigned int nch, 508enum mad_error III_sideinfo(struct mad_bitptr *ptr, unsigned int nch,
501 int lsf, struct sideinfo *si, 509 int lsf, struct sideinfo *si,
502 unsigned int *data_bitlen, 510 unsigned int *data_bitlen,
503 unsigned int *priv_bitlen) 511 unsigned int *priv_bitlen)
504{ 512{
505 unsigned int ngr, gr, ch, i; 513 unsigned int ngr, gr, ch, i;
506 enum mad_error result = 0; 514 enum mad_error result = MAD_ERROR_NONE;
507 515
508 *data_bitlen = 0; 516 *data_bitlen = 0;
509 *priv_bitlen = lsf ? ((nch == 1) ? 1 : 2) : ((nch == 1) ? 5 : 3); 517 *priv_bitlen = lsf ? ((nch == 1) ? 1 : 2) : ((nch == 1) ? 5 : 3);
510 518
511 si->main_data_begin = mad_bit_read(ptr, lsf ? 8 : 9); 519 si->main_data_begin = mad_bit_read(ptr, lsf ? 8 : 9);
512 si->private_bits = mad_bit_read(ptr, *priv_bitlen); 520 si->private_bits = mad_bit_read(ptr, *priv_bitlen);
513 521
514 ngr = 1; 522 ngr = 1;
515 if (!lsf) { 523 if (!lsf) {
516 ngr = 2; 524 ngr = 2;
517 525
518 for (ch = 0; ch < nch; ++ch) 526 for (ch = 0; ch < nch; ++ch)
519 si->scfsi[ch] = mad_bit_read(ptr, 4); 527 si->scfsi[ch] = mad_bit_read(ptr, 4);
520 } 528 }
521 529
522 for (gr = 0; gr < ngr; ++gr) { 530 for (gr = 0; gr < ngr; ++gr) {
523 struct granule *granule = &si->gr[gr]; 531 struct granule *granule = &si->gr[gr];
524 532
525 for (ch = 0; ch < nch; ++ch) { 533 for (ch = 0; ch < nch; ++ch) {
526 struct channel *channel = &granule->ch[ch]; 534 struct channel *channel = &granule->ch[ch];
527 535
528 channel->part2_3_length = mad_bit_read(ptr, 12); 536 channel->part2_3_length = mad_bit_read(ptr, 12);
529 channel->big_values = mad_bit_read(ptr, 9); 537 channel->big_values = mad_bit_read(ptr, 9);
530 channel->global_gain = mad_bit_read(ptr, 8); 538 channel->global_gain = mad_bit_read(ptr, 8);
@@ -581,90 +589,90 @@ enum mad_error III_sideinfo(struct mad_bitptr *ptr, unsigned int nch,
581 } 589 }
582 590
583 return result; 591 return result;
584} 592}
585 593
586/* 594/*
587 * NAME:III_scalefactors_lsf() 595 * NAME:III_scalefactors_lsf()
588 * DESCRIPTION:decode channel scalefactors for LSF from a bitstream 596 * DESCRIPTION:decode channel scalefactors for LSF from a bitstream
589 */ 597 */
590static 598static
591unsigned int III_scalefactors_lsf(struct mad_bitptr *ptr, 599unsigned int III_scalefactors_lsf(struct mad_bitptr *ptr,
592 struct channel *channel, 600 struct channel *channel,
593 struct channel *gr1ch, int mode_extension) 601 struct channel *gr1ch, int mode_extension)
594{ 602{
595 struct mad_bitptr start; 603 struct mad_bitptr start;
596 unsigned int scalefac_compress, index, slen[4], part, n, i; 604 unsigned int scalefac_compress, index, slen[4], part, n, i;
597 unsigned char const *nsfb; 605 unsigned char const *nsfb;
598 606
599 start = *ptr; 607 start = *ptr;
600 608
601 scalefac_compress = channel->scalefac_compress; 609 scalefac_compress = channel->scalefac_compress;
602 index = (channel->block_type == 2) ? 610 index = (channel->block_type == 2) ?
603 ((channel->flags & mixed_block_flag) ? 2 : 1) : 0; 611 ((channel->flags & mixed_block_flag) ? 2 : 1) : 0;
604 612
605 if (!((mode_extension & 0x1) && gr1ch)) { 613 if (!((mode_extension & I_STEREO) && gr1ch)) {
606 if (scalefac_compress < 400) { 614 if (scalefac_compress < 400) {
607 slen[0] = (scalefac_compress >> 4) / 5; 615 slen[0] = (scalefac_compress >> 4) / 5;
608 slen[1] = (scalefac_compress >> 4) % 5; 616 slen[1] = (scalefac_compress >> 4) % 5;
609 slen[2] = (scalefac_compress % 16) >> 2; 617 slen[2] = (scalefac_compress % 16) >> 2;
610 slen[3] = scalefac_compress % 4; 618 slen[3] = scalefac_compress % 4;
611 619
612 nsfb = nsfb_table[0][index]; 620 nsfb = nsfb_table[0][index];
613 } 621 }
614 else if (scalefac_compress < 500) { 622 else if (scalefac_compress < 500) {
615 scalefac_compress -= 400; 623 scalefac_compress -= 400;
616 624
617 slen[0] = (scalefac_compress >> 2) / 5; 625 slen[0] = (scalefac_compress >> 2) / 5;
618 slen[1] = (scalefac_compress >> 2) % 5; 626 slen[1] = (scalefac_compress >> 2) % 5;
619 slen[2] = scalefac_compress % 4; 627 slen[2] = scalefac_compress % 4;
620 slen[3] = 0; 628 slen[3] = 0;
621 629
622 nsfb = nsfb_table[1][index]; 630 nsfb = nsfb_table[1][index];
623 } 631 }
624 else { 632 else {
625 scalefac_compress -= 500; 633 scalefac_compress -= 500;
626 634
627 slen[0] = scalefac_compress / 3; 635 slen[0] = scalefac_compress / 3;
628 slen[1] = scalefac_compress % 3; 636 slen[1] = scalefac_compress % 3;
629 slen[2] = 0; 637 slen[2] = 0;
630 slen[3] = 0; 638 slen[3] = 0;
631 639
632 channel->flags |= preflag; 640 channel->flags |= preflag;
633 641
634 nsfb = nsfb_table[2][index]; 642 nsfb = nsfb_table[2][index];
635 } 643 }
636 644
637 n = 0; 645 n = 0;
638 for (part = 0; part < 4; ++part) { 646 for (part = 0; part < 4; ++part) {
639 for (i = 0; i < nsfb[part]; ++i) 647 for (i = 0; i < nsfb[part]; ++i)
640 channel->scalefac[n++] = mad_bit_read(ptr, slen[part]); 648 channel->scalefac[n++] = mad_bit_read(ptr, slen[part]);
641 } 649 }
642 650
643 while (n < 39) 651 while (n < 39)
644 channel->scalefac[n++] = 0; 652 channel->scalefac[n++] = 0;
645 } 653 }
646 else { /* (mode_extension & 0x1) && gr1ch (i.e. ch == 1) */ 654 else { /* (mode_extension & I_STEREO) && gr1ch (i.e. ch == 1) */
647 scalefac_compress >>= 1; 655 scalefac_compress >>= 1;
648 656
649 if (scalefac_compress < 180) { 657 if (scalefac_compress < 180) {
650 slen[0] = scalefac_compress / 36; 658 slen[0] = scalefac_compress / 36;
651 slen[1] = (scalefac_compress % 36) / 6; 659 slen[1] = (scalefac_compress % 36) / 6;
652 slen[2] = (scalefac_compress % 36) % 6; 660 slen[2] = (scalefac_compress % 36) % 6;
653 slen[3] = 0; 661 slen[3] = 0;
654 662
655 nsfb = nsfb_table[3][index]; 663 nsfb = nsfb_table[3][index];
656 } 664 }
657 else if (scalefac_compress < 244) { 665 else if (scalefac_compress < 244) {
658 scalefac_compress -= 180; 666 scalefac_compress -= 180;
659 667
660 slen[0] = (scalefac_compress % 64) >> 4; 668 slen[0] = (scalefac_compress % 64) >> 4;
661 slen[1] = (scalefac_compress % 16) >> 2; 669 slen[1] = (scalefac_compress % 16) >> 2;
662 slen[2] = scalefac_compress % 4; 670 slen[2] = scalefac_compress % 4;
663 slen[3] = 0; 671 slen[3] = 0;
664 672
665 nsfb = nsfb_table[4][index]; 673 nsfb = nsfb_table[4][index];
666 } 674 }
667 else { 675 else {
668 scalefac_compress -= 244; 676 scalefac_compress -= 244;
669 677
670 slen[0] = scalefac_compress / 3; 678 slen[0] = scalefac_compress / 3;
@@ -754,48 +762,70 @@ unsigned int III_scalefactors(struct mad_bitptr *ptr, struct channel *channel,
754 for (sfbi = 11; sfbi < 16; ++sfbi) 762 for (sfbi = 11; sfbi < 16; ++sfbi)
755 channel->scalefac[sfbi] = gr0ch->scalefac[sfbi]; 763 channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
756 } 764 }
757 else { 765 else {
758 for (sfbi = 11; sfbi < 16; ++sfbi) 766 for (sfbi = 11; sfbi < 16; ++sfbi)
759 channel->scalefac[sfbi] = mad_bit_read(ptr, slen2); 767 channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
760 } 768 }
761 769
762 if (scfsi & 0x1) { 770 if (scfsi & 0x1) {
763 for (sfbi = 16; sfbi < 21; ++sfbi) 771 for (sfbi = 16; sfbi < 21; ++sfbi)
764 channel->scalefac[sfbi] = gr0ch->scalefac[sfbi]; 772 channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
765 } 773 }
766 else { 774 else {
767 for (sfbi = 16; sfbi < 21; ++sfbi) 775 for (sfbi = 16; sfbi < 21; ++sfbi)
768 channel->scalefac[sfbi] = mad_bit_read(ptr, slen2); 776 channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
769 } 777 }
770 778
771 channel->scalefac[21] = 0; 779 channel->scalefac[21] = 0;
772 } 780 }
773 781
774 return mad_bit_length(&start, ptr); 782 return mad_bit_length(&start, ptr);
775} 783}
776 784
777/* 785/*
786 * The Layer III formula for requantization and scaling is defined by
787 * section 2.4.3.4.7.1 of ISO/IEC 11172-3, as follows:
788 *
789 * long blocks:
790 * xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
791 * 2^((1/4) * (global_gain - 210)) *
792 * 2^-(scalefac_multiplier *
793 * (scalefac_l[sfb] + preflag * pretab[sfb]))
794 *
795 * short blocks:
796 * xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
797 * 2^((1/4) * (global_gain - 210 - 8 * subblock_gain[w])) *
798 * 2^-(scalefac_multiplier * scalefac_s[sfb][w])
799 *
800 * where:
801 * scalefac_multiplier = (scalefac_scale + 1) / 2
802 *
803 * The routines III_exponents() and III_requantize() facilitate this
804 * calculation.
805 */
806
807/*
778 * NAME:III_exponents() 808 * NAME:III_exponents()
779 * DESCRIPTION:calculate scalefactor exponents 809 * DESCRIPTION:calculate scalefactor exponents
780 */ 810 */
781static 811static
782void III_exponents(struct channel const *channel, 812void III_exponents(struct channel const *channel,
783 unsigned char const *sfbwidth, signed int exponents[39]) 813 unsigned char const *sfbwidth, signed int exponents[39])
784{ 814{
785 signed int gain; 815 signed int gain;
786 unsigned int scalefac_multiplier, sfbi; 816 unsigned int scalefac_multiplier, sfbi;
787 817
788 gain = (signed int) channel->global_gain - 210; 818 gain = (signed int) channel->global_gain - 210;
789 scalefac_multiplier = (channel->flags & scalefac_scale) ? 2 : 1; 819 scalefac_multiplier = (channel->flags & scalefac_scale) ? 2 : 1;
790 820
791 if (channel->block_type == 2) { 821 if (channel->block_type == 2) {
792 unsigned int l; 822 unsigned int l;
793 signed int gain0, gain1, gain2; 823 signed int gain0, gain1, gain2;
794 824
795 sfbi = l = 0; 825 sfbi = l = 0;
796 826
797 if (channel->flags & mixed_block_flag) { 827 if (channel->flags & mixed_block_flag) {
798 unsigned int premask; 828 unsigned int premask;
799 829
800 premask = (channel->flags & preflag) ? ~0 : 0; 830 premask = (channel->flags & preflag) ? ~0 : 0;
801 831
@@ -835,78 +865,64 @@ void III_exponents(struct channel const *channel,
835 (signed int) ((channel->scalefac[sfbi] + pretab[sfbi]) << 865 (signed int) ((channel->scalefac[sfbi] + pretab[sfbi]) <<
836 scalefac_multiplier); 866 scalefac_multiplier);
837 } 867 }
838 } 868 }
839 else { 869 else {
840 for (sfbi = 0; sfbi < 22; ++sfbi) { 870 for (sfbi = 0; sfbi < 22; ++sfbi) {
841 exponents[sfbi] = gain - 871 exponents[sfbi] = gain -
842 (signed int) (channel->scalefac[sfbi] << scalefac_multiplier); 872 (signed int) (channel->scalefac[sfbi] << scalefac_multiplier);
843 } 873 }
844 } 874 }
845 } 875 }
846} 876}
847 877
848/* 878/*
849 * NAME:III_requantize() 879 * NAME:III_requantize()
850 * DESCRIPTION:requantize one (positive) value 880 * DESCRIPTION:requantize one (positive) value
851 */ 881 */
852static 882static
853mad_fixed_t III_requantize(unsigned int value, signed int exp) 883mad_fixed_t III_requantize(unsigned int value, signed int exp)
854{ 884{
855 mad_fixed_t requantized; 885 mad_fixed_t requantized;
856 signed int frac; 886 signed int frac;
857 struct fixedfloat const *power; 887 struct fixedfloat const *power;
858 888
859 /* 889 frac = exp % 4; /* assumes sign(frac) == sign(exp) */
860 * long blocks:
861 * xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
862 * 2^((1/4) * (global_gain - 210)) *
863 * 2^-(scalefac_multiplier *
864 * (scalefac_l[sfb] + preflag * pretab[sfb]))
865 *
866 * short blocks:
867 * xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
868 * 2^((1/4) * (global_gain - 210 - 8 * subblock_gain[w])) *
869 * 2^-(scalefac_multiplier * scalefac_s[sfb][w])
870 *
871 * where:
872 * scalefac_multiplier = (scalefac_scale + 1) / 2
873 */
874
875 frac = exp % 4;
876 exp /= 4; 890 exp /= 4;
877 891
878 power = &rq_table[value]; 892 power = &rq_table[value];
879 requantized = power->mantissa; 893 requantized = power->mantissa;
880 exp += power->exponent; 894 exp += power->exponent;
881 895
882 if (exp < 0) { 896 if (exp < 0) {
883 if (-exp >= sizeof(mad_fixed_t) * CHAR_BIT) { 897 if (-exp >= sizeof(mad_fixed_t) * CHAR_BIT) {
884 /* underflow */ 898 /* underflow */
885 requantized = 0; 899 requantized = 0;
886 } 900 }
887 else 901 else {
902 requantized += 1L << (-exp - 1);
888 requantized >>= -exp; 903 requantized >>= -exp;
904 }
889 } 905 }
890 else { 906 else {
891 if (exp >= 5) { 907 if (exp >= 5) {
892 /* overflow */ 908 /* overflow */
893# if defined(DEBUG) 909# if defined(DEBUG)
894 fprintf(stderr, "requantize overflow (%f * 2^%d)\n", 910 fprintf(stderr, "requantize overflow (%f * 2^%d)\n",
895 mad_f_todouble(requantized), exp); 911 mad_f_todouble(requantized), exp);
896# endif 912# endif
897 requantized = MAD_F_MAX; 913 requantized = MAD_F_MAX;
898 } 914 }
899 else 915 else
900 requantized <<= exp; 916 requantized <<= exp;
901 } 917 }
902 918
903 return frac ? mad_f_mul(requantized, root_table[3 + frac]) : requantized; 919 return frac ? mad_f_mul(requantized, root_table[3 + frac]) : requantized;
904} 920}
905 921
906/* we must take care that sz >= bits and sz < sizeof(long) lest bits == 0 */ 922/* we must take care that sz >= bits and sz < sizeof(long) lest bits == 0 */
907 # define MASK(cache, sz, bits)\ 923 # define MASK(cache, sz, bits)\
908 (((cache) >> ((sz) - (bits))) & ((1 << (bits)) - 1)) 924 (((cache) >> ((sz) - (bits))) & ((1 << (bits)) - 1))
909# define MASK1BIT(cache, sz) \ 925# define MASK1BIT(cache, sz) \
910 ((cache) & (1 << ((sz) - 1))) 926 ((cache) & (1 << ((sz) - 1)))
911 927
912/* 928/*
@@ -1230,129 +1246,126 @@ enum mad_error III_huffdecode(struct mad_bitptr *ptr, mad_fixed_t xr[576],
1230 /* technically the bitstream is misformatted, but apparently 1246 /* technically the bitstream is misformatted, but apparently
1231 some encoders are just a bit sloppy with stuffing bits */ 1247 some encoders are just a bit sloppy with stuffing bits */
1232 1248
1233 xrptr -= 4; 1249 xrptr -= 4;
1234 } 1250 }
1235 } 1251 }
1236 1252
1237 assert(-bits_left <= MAD_BUFFER_GUARD * CHAR_BIT); 1253 assert(-bits_left <= MAD_BUFFER_GUARD * CHAR_BIT);
1238 1254
1239# if 0 && defined(DEBUG) 1255# if 0 && defined(DEBUG)
1240 if (bits_left < 0) 1256 if (bits_left < 0)
1241 fprintf(stderr, "read %d bits too many\n", -bits_left); 1257 fprintf(stderr, "read %d bits too many\n", -bits_left);
1242 else if (cachesz + bits_left > 0) 1258 else if (cachesz + bits_left > 0)
1243 fprintf(stderr, "%d stuffing bits\n", cachesz + bits_left); 1259 fprintf(stderr, "%d stuffing bits\n", cachesz + bits_left);
1244# endif 1260# endif
1245 1261
1246 /* rzero */ 1262 /* rzero */
1247 while (xrptr < &xr[576]) { 1263 while (xrptr < &xr[576]) {
1248 xrptr[0] = 0; 1264 xrptr[0] = 0;
1249 xrptr[1] = 0; 1265 xrptr[1] = 0;
1250 1266
1251 xrptr += 2; 1267 xrptr += 2;
1252 } 1268 }
1253 1269
1254 return 0; 1270 return MAD_ERROR_NONE;
1255} 1271}
1256 1272
1257# undef MASK 1273# undef MASK
1258# undef MASK1BIT 1274# undef MASK1BIT
1259 1275
1260/* 1276/*
1261 * NAME:III_reorder() 1277 * NAME:III_reorder()
1262 * DESCRIPTION:reorder frequency lines of a short block into subband order 1278 * DESCRIPTION:reorder frequency lines of a short block into subband order
1263 */ 1279 */
1264static 1280static
1265void III_reorder(mad_fixed_t xr[576], struct channel const *channel, 1281void III_reorder(mad_fixed_t xr[576], struct channel const *channel,
1266 unsigned char const sfbwidth[39]) 1282 unsigned char const sfbwidth[39])
1267{ 1283{
1268 mad_fixed_t tmp[32][3][6]; 1284 mad_fixed_t tmp[32][3][6];
1269 unsigned int sb, l, sfbi, f, w, sbw[3], sw[3]; 1285 unsigned int sb, l, f, w, sbw[3], sw[3];
1270 1286
1271 /* this is probably wrong for 8000 Hz mixed blocks */ 1287 /* this is probably wrong for 8000 Hz mixed blocks */
1272 1288
1273 if (channel->flags & mixed_block_flag) 1289 sb = 0;
1274 sb = 2, sfbi = 3 * 3; 1290 if (channel->flags & mixed_block_flag) {
1275 else 1291 sb = 2;
1276 sb = 0, sfbi = 0; 1292
1293 l = 0;
1294 while (l < 36)
1295 l += *sfbwidth++;
1296 }
1277 1297
1278 for (w = 0; w < 3; ++w) { 1298 for (w = 0; w < 3; ++w) {
1279 sbw[w] = sb; 1299 sbw[w] = sb;
1280 sw[w] = 0; 1300 sw[w] = 0;
1281 } 1301 }
1282 1302
1283 f = sfbwidth[sfbi]; 1303 f = *sfbwidth++;
1284 w = 0; 1304 w = 0;
1285 1305
1286 for (l = 18 * sb; l < 576; ++l) { 1306 for (l = 18 * sb; l < 576; ++l) {
1307 if (f-- == 0) {
1308 f = *sfbwidth++ - 1;
1309 w = (w + 1) % 3;
1310 }
1311
1287 tmp[sbw[w]][w][sw[w]++] = xr[l]; 1312 tmp[sbw[w]][w][sw[w]++] = xr[l];
1288 1313
1289 if (sw[w] == 6) { 1314 if (sw[w] == 6) {
1290 sw[w] = 0; 1315 sw[w] = 0;
1291 ++sbw[w]; 1316 ++sbw[w];
1292 } 1317 }
1293
1294 if (--f == 0) {
1295 if (++w == 3)
1296 w = 0;
1297
1298 f = sfbwidth[++sfbi];
1299 }
1300 } 1318 }
1301 1319
1302 memcpy(&xr[18 * sb], &tmp[sb], (576 - 18 * sb) * sizeof(mad_fixed_t)); 1320 memcpy(&xr[18 * sb], &tmp[sb], (576 - 18 * sb) * sizeof(mad_fixed_t));
1303} 1321}
1304 1322
1305/* 1323/*
1306 * NAME:III_stereo() 1324 * NAME:III_stereo()
1307 * DESCRIPTION:perform joint stereo processing on a granule 1325 * DESCRIPTION:perform joint stereo processing on a granule
1308 */ 1326 */
1309static 1327static
1310enum mad_error III_stereo(mad_fixed_t xr[2][576], 1328enum mad_error III_stereo(mad_fixed_t xr[2][576],
1311 struct granule const *granule, 1329 struct granule const *granule,
1312 struct mad_header *header, 1330 struct mad_header *header,
1313 unsigned char const *sfbwidth) 1331 unsigned char const *sfbwidth)
1314{ 1332{
1315 short modes[39]; 1333 short modes[39];
1316 unsigned int sfbi, l, n, i; 1334 unsigned int sfbi, l, n, i;
1317 1335
1318 enum {
1319 i_stereo = 0x1,
1320 ms_stereo = 0x2
1321 };
1322
1323 if (granule->ch[0].block_type != 1336 if (granule->ch[0].block_type !=
1324 granule->ch[1].block_type || 1337 granule->ch[1].block_type ||
1325 (granule->ch[0].flags & mixed_block_flag) != 1338 (granule->ch[0].flags & mixed_block_flag) !=
1326 (granule->ch[1].flags & mixed_block_flag)) 1339 (granule->ch[1].flags & mixed_block_flag))
1327 return MAD_ERROR_BADSTEREO; 1340 return MAD_ERROR_BADSTEREO;
1328 1341
1329 for (i = 0; i < 39; ++i) 1342 for (i = 0; i < 39; ++i)
1330 modes[i] = header->mode_extension; 1343 modes[i] = header->mode_extension;
1331 1344
1332 /* intensity stereo */ 1345 /* intensity stereo */
1333 1346
1334 if (header->mode_extension & i_stereo) { 1347 if (header->mode_extension & I_STEREO) {
1335 struct channel const *right_ch = &granule->ch[1]; 1348 struct channel const *right_ch = &granule->ch[1];
1336 mad_fixed_t const *right_xr = xr[1]; 1349 mad_fixed_t const *right_xr = xr[1];
1337 unsigned int is_pos; 1350 unsigned int is_pos;
1338 1351
1339 header->flags |= MAD_FLAG_I_STEREO; 1352 header->flags |= MAD_FLAG_I_STEREO;
1340 1353
1341 /* first determine which scalefactor bands are to be processed */ 1354 /* first determine which scalefactor bands are to be processed */
1342 1355
1343 if (right_ch->block_type == 2) { 1356 if (right_ch->block_type == 2) {
1344 unsigned int lower, start, max, bound[3], w; 1357 unsigned int lower, start, max, bound[3], w;
1345 1358
1346 lower = start = max = bound[0] = bound[1] = bound[2] = 0; 1359 lower = start = max = bound[0] = bound[1] = bound[2] = 0;
1347 1360
1348 sfbi = l = 0; 1361 sfbi = l = 0;
1349 1362
1350 if (right_ch->flags & mixed_block_flag) { 1363 if (right_ch->flags & mixed_block_flag) {
1351 while (l < 36) { 1364 while (l < 36) {
1352 n = sfbwidth[sfbi++]; 1365 n = sfbwidth[sfbi++];
1353 1366
1354 for (i = 0; i < n; ++i) { 1367 for (i = 0; i < n; ++i) {
1355 if (right_xr[i]) { 1368 if (right_xr[i]) {
1356 lower = sfbi; 1369 lower = sfbi;
1357 break; 1370 break;
1358 } 1371 }
@@ -1366,216 +1379,213 @@ enum mad_error III_stereo(mad_fixed_t xr[2][576],
1366 } 1379 }
1367 1380
1368 w = 0; 1381 w = 0;
1369 while (l < 576) { 1382 while (l < 576) {
1370 n = sfbwidth[sfbi++]; 1383 n = sfbwidth[sfbi++];
1371 1384
1372 for (i = 0; i < n; ++i) { 1385 for (i = 0; i < n; ++i) {
1373 if (right_xr[i]) { 1386 if (right_xr[i]) {
1374 max = bound[w] = sfbi; 1387 max = bound[w] = sfbi;
1375 break; 1388 break;
1376 } 1389 }
1377 } 1390 }
1378 1391
1379 right_xr += n; 1392 right_xr += n;
1380 l += n; 1393 l += n;
1381 w = (w + 1) % 3; 1394 w = (w + 1) % 3;
1382 } 1395 }
1383 1396
1384 if (max) 1397 if (max)
1385 lower = start; 1398 lower = start;
1386 1399
1387 /* long blocks */ 1400 /* long blocks */
1388 1401
1389 for (i = 0; i < lower; ++i) 1402 for (i = 0; i < lower; ++i)
1390 modes[i] = header->mode_extension & ~i_stereo; 1403 modes[i] = header->mode_extension & ~I_STEREO;
1391 1404
1392 /* short blocks */ 1405 /* short blocks */
1393 1406
1394 w = 0; 1407 w = 0;
1395 for (i = start; i < max; ++i) { 1408 for (i = start; i < max; ++i) {
1396 if (i < bound[w]) 1409 if (i < bound[w])
1397 modes[i] = header->mode_extension & ~i_stereo; 1410 modes[i] = header->mode_extension & ~I_STEREO;
1398 1411
1399 w = (w + 1) % 3; 1412 w = (w + 1) % 3;
1400 } 1413 }
1401 } 1414 }
1402 else { /* right_ch->block_type != 2 */ 1415 else { /* right_ch->block_type != 2 */
1403 unsigned int bound; 1416 unsigned int bound;
1404 1417
1405 bound = 0; 1418 bound = 0;
1406 for (sfbi = l = 0; l < 576; l += n) { 1419 for (sfbi = l = 0; l < 576; l += n) {
1407 n = sfbwidth[sfbi++]; 1420 n = sfbwidth[sfbi++];
1408 1421
1409 for (i = 0; i < n; ++i) { 1422 for (i = 0; i < n; ++i) {
1410 if (right_xr[i]) { 1423 if (right_xr[i]) {
1411 bound = sfbi; 1424 bound = sfbi;
1412 break; 1425 break;
1413 } 1426 }
1414 } 1427 }
1415 1428
1416 right_xr += n; 1429 right_xr += n;
1417 } 1430 }
1418 1431
1419 for (i = 0; i < bound; ++i) 1432 for (i = 0; i < bound; ++i)
1420 modes[i] = header->mode_extension & ~i_stereo; 1433 modes[i] = header->mode_extension & ~I_STEREO;
1421 } 1434 }
1422 1435
1423 /* now do the actual processing */ 1436 /* now do the actual processing */
1424 1437
1425 if (header->flags & MAD_FLAG_LSF_EXT) { 1438 if (header->flags & MAD_FLAG_LSF_EXT) {
1426 unsigned char const *illegal_pos = granule[1].ch[1].scalefac; 1439 unsigned char const *illegal_pos = granule[1].ch[1].scalefac;
1427 mad_fixed_t const *lsf_scale; 1440 mad_fixed_t const *lsf_scale;
1428 1441
1429 /* intensity_scale */ 1442 /* intensity_scale */
1430 lsf_scale = is_lsf_table[right_ch->scalefac_compress & 0x1]; 1443 lsf_scale = is_lsf_table[right_ch->scalefac_compress & 0x1];
1431 1444
1432 for (sfbi = l = 0; l < 576; ++sfbi, l += n) { 1445 for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
1433 n = sfbwidth[sfbi]; 1446 n = sfbwidth[sfbi];
1434 1447
1435 if (!(modes[sfbi] & i_stereo)) 1448 if (!(modes[sfbi] & I_STEREO))
1436 continue; 1449 continue;
1437 1450
1438 if (illegal_pos[sfbi]) { 1451 if (illegal_pos[sfbi]) {
1439 modes[sfbi] &= ~i_stereo; 1452 modes[sfbi] &= ~I_STEREO;
1440 continue; 1453 continue;
1441 } 1454 }
1442 1455
1443 is_pos = right_ch->scalefac[sfbi]; 1456 is_pos = right_ch->scalefac[sfbi];
1444 1457
1445 for (i = 0; i < n; ++i) { 1458 for (i = 0; i < n; ++i) {
1446 register mad_fixed_t left; 1459 register mad_fixed_t left;
1447 1460
1448 left = xr[0][l + i]; 1461 left = xr[0][l + i];
1449 1462
1450 if (is_pos == 0) 1463 if (is_pos == 0)
1451 xr[1][l + i] = left; 1464 xr[1][l + i] = left;
1452 else { 1465 else {
1453 register mad_fixed_t opposite; 1466 register mad_fixed_t opposite;
1454 1467
1455 opposite = mad_f_mul(left, lsf_scale[(is_pos - 1) / 2]); 1468 opposite = mad_f_mul(left, lsf_scale[(is_pos - 1) / 2]);
1456 1469
1457 if (is_pos & 1) { 1470 if (is_pos & 1) {
1458 xr[0][l + i] = opposite; 1471 xr[0][l + i] = opposite;
1459 xr[1][l + i] = left; 1472 xr[1][l + i] = left;
1460 } 1473 }
1461 else 1474 else
1462 xr[1][l + i] = opposite; 1475 xr[1][l + i] = opposite;
1463 } 1476 }
1464 } 1477 }
1465 } 1478 }
1466 } 1479 }
1467 else { /* !(header->flags & MAD_FLAG_LSF_EXT) */ 1480 else { /* !(header->flags & MAD_FLAG_LSF_EXT) */
1468 for (sfbi = l = 0; l < 576; ++sfbi, l += n) { 1481 for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
1469 n = sfbwidth[sfbi]; 1482 n = sfbwidth[sfbi];
1470 1483
1471 if (!(modes[sfbi] & i_stereo)) 1484 if (!(modes[sfbi] & I_STEREO))
1472 continue; 1485 continue;
1473 1486
1474 is_pos = right_ch->scalefac[sfbi]; 1487 is_pos = right_ch->scalefac[sfbi];
1475 1488
1476 if (is_pos >= 7) { /* illegal intensity position */ 1489 if (is_pos >= 7) { /* illegal intensity position */
1477 modes[sfbi] &= ~i_stereo; 1490 modes[sfbi] &= ~I_STEREO;
1478 continue; 1491 continue;
1479 } 1492 }
1480 1493
1481 for (i = 0; i < n; ++i) { 1494 for (i = 0; i < n; ++i) {
1482 register mad_fixed_t left; 1495 register mad_fixed_t left;
1483 1496
1484 left = xr[0][l + i]; 1497 left = xr[0][l + i];
1485 1498
1486 xr[0][l + i] = mad_f_mul(left, is_table[ is_pos]); 1499 xr[0][l + i] = mad_f_mul(left, is_table[ is_pos]);
1487 xr[1][l + i] = mad_f_mul(left, is_table[6 - is_pos]); 1500 xr[1][l + i] = mad_f_mul(left, is_table[6 - is_pos]);
1488 } 1501 }
1489 } 1502 }
1490 } 1503 }
1491 } 1504 }
1492 1505
1493 /* middle/side stereo */ 1506 /* middle/side stereo */
1494 1507
1495 if (header->mode_extension & ms_stereo) { 1508 if (header->mode_extension & MS_STEREO) {
1496 register mad_fixed_t invsqrt2; 1509 register mad_fixed_t invsqrt2;
1497 1510
1498 header->flags |= MAD_FLAG_MS_STEREO; 1511 header->flags |= MAD_FLAG_MS_STEREO;
1499 1512
1500 invsqrt2 = root_table[3 + -2]; 1513 invsqrt2 = root_table[3 + -2];
1501 1514
1502 for (sfbi = l = 0; l < 576; ++sfbi, l += n) { 1515 for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
1503 n = sfbwidth[sfbi]; 1516 n = sfbwidth[sfbi];
1504 1517
1505 if (modes[sfbi] != ms_stereo) 1518 if (modes[sfbi] != MS_STEREO)
1506 continue; 1519 continue;
1507 1520
1508 for (i = 0; i < n; ++i) { 1521 for (i = 0; i < n; ++i) {
1509 register mad_fixed_t m, s; 1522 register mad_fixed_t m, s;
1510 1523
1511 m = xr[0][l + i]; 1524 m = xr[0][l + i];
1512 s = xr[1][l + i]; 1525 s = xr[1][l + i];
1513 1526
1514 xr[0][l + i] = mad_f_mul(m + s, invsqrt2); /* l = (m + s) / sqrt(2) */ 1527 xr[0][l + i] = mad_f_mul(m + s, invsqrt2); /* l = (m + s) / sqrt(2) */
1515 xr[1][l + i] = mad_f_mul(m - s, invsqrt2); /* r = (m - s) / sqrt(2) */ 1528 xr[1][l + i] = mad_f_mul(m - s, invsqrt2); /* r = (m - s) / sqrt(2) */
1516 } 1529 }
1517 } 1530 }
1518 } 1531 }
1519 1532
1520 return 0; 1533 return MAD_ERROR_NONE;
1521} 1534}
1522 1535
1523/* 1536/*
1524 * NAME:III_aliasreduce() 1537 * NAME:III_aliasreduce()
1525 * DESCRIPTION:perform frequency line alias reduction 1538 * DESCRIPTION:perform frequency line alias reduction
1526 */ 1539 */
1527static 1540static
1528void III_aliasreduce(mad_fixed_t xr[576], int lines) 1541void III_aliasreduce(mad_fixed_t xr[576], int lines)
1529{ 1542{
1530 mad_fixed_t const *bound; 1543 mad_fixed_t const *bound;
1531 int i; 1544 int i;
1532 1545
1533 bound = &xr[lines]; 1546 bound = &xr[lines];
1534 for (xr += 18; xr < bound; xr += 18) { 1547 for (xr += 18; xr < bound; xr += 18) {
1535 for (i = 0; i < 8; ++i) { 1548 for (i = 0; i < 8; ++i) {
1536 register mad_fixed_t *aptr, *bptr, a, b; 1549 register mad_fixed_t a, b;
1537 register mad_fixed64hi_t hi; 1550 register mad_fixed64hi_t hi;
1538 register mad_fixed64lo_t lo; 1551 register mad_fixed64lo_t lo;
1539 1552
1540 aptr = &xr[-1 - i]; 1553 a = xr[-1 - i];
1541 bptr = &xr[ i]; 1554 b = xr[ i];
1542
1543 a = *aptr;
1544 b = *bptr;
1545 1555
1546# if defined(ASO_ZEROCHECK) 1556# if defined(ASO_ZEROCHECK)
1547 if (a | b) { 1557 if (a | b) {
1548# endif 1558# endif
1549 MAD_F_ML0(hi, lo, a, cs[i]); 1559 MAD_F_ML0(hi, lo, a, cs[i]);
1550 MAD_F_MLA(hi, lo, -b, ca[i]); 1560 MAD_F_MLA(hi, lo, -b, ca[i]);
1551 1561
1552 *aptr = MAD_F_MLZ(hi, lo); 1562 xr[-1 - i] = MAD_F_MLZ(hi, lo);
1553 1563
1554 MAD_F_ML0(hi, lo, b, cs[i]); 1564 MAD_F_ML0(hi, lo, b, cs[i]);
1555 MAD_F_MLA(hi, lo, a, ca[i]); 1565 MAD_F_MLA(hi, lo, a, ca[i]);
1556 1566
1557 *bptr = MAD_F_MLZ(hi, lo); 1567 xr[ i] = MAD_F_MLZ(hi, lo);
1558# if defined(ASO_ZEROCHECK) 1568# if defined(ASO_ZEROCHECK)
1559 } 1569 }
1560# endif 1570# endif
1561 } 1571 }
1562 } 1572 }
1563} 1573}
1564 1574
1565# if defined(ASO_IMDCT) 1575# if defined(ASO_IMDCT)
1566void III_imdct_l(mad_fixed_t const [18], mad_fixed_t [36], unsigned int); 1576void III_imdct_l(mad_fixed_t const [18], mad_fixed_t [36], unsigned int);
1567# else 1577# else
1568/* 1578/*
1569 * NAME:imdct36 1579 * NAME:imdct36
1570 * DESCRIPTION:perform X[18]->x[36] IMDCT 1580 * DESCRIPTION:perform X[18]->x[36] IMDCT
1571 */ 1581 */
1572static inline 1582static inline
1573void imdct36(mad_fixed_t const X[18], mad_fixed_t x[36]) 1583void imdct36(mad_fixed_t const X[18], mad_fixed_t x[36])
1574{ 1584{
1575 mad_fixed_t t0, t1, t2, t3, t4, t5, t6, t7; 1585 mad_fixed_t t0, t1, t2, t3, t4, t5, t6, t7;
1576 mad_fixed_t t8, t9, t10, t11, t12, t13, t14, t15; 1586 mad_fixed_t t8, t9, t10, t11, t12, t13, t14, t15;
1577 register mad_fixed64hi_t hi; 1587 register mad_fixed64hi_t hi;
1578 register mad_fixed64lo_t lo; 1588 register mad_fixed64lo_t lo;
1579 1589
1580 MAD_F_ML0(hi, lo, X[4], MAD_F(0x0ec835e8)); 1590 MAD_F_ML0(hi, lo, X[4], MAD_F(0x0ec835e8));
1581 MAD_F_MLA(hi, lo, X[13], MAD_F(0x061f78aa)); 1591 MAD_F_MLA(hi, lo, X[13], MAD_F(0x061f78aa));
@@ -2120,124 +2130,124 @@ void III_freqinver(mad_fixed_t sample[18][32], unsigned int sb)
2120 2130
2121 for (i = 1; i < 13; i += 4) { 2131 for (i = 1; i < 13; i += 4) {
2122 sample[i + 0][sb] = -tmp1; 2132 sample[i + 0][sb] = -tmp1;
2123 tmp1 = sample[i + 4][sb]; 2133 tmp1 = sample[i + 4][sb];
2124 sample[i + 2][sb] = -tmp2; 2134 sample[i + 2][sb] = -tmp2;
2125 tmp2 = sample[i + 6][sb]; 2135 tmp2 = sample[i + 6][sb];
2126 } 2136 }
2127 2137
2128 sample[13][sb] = -tmp1; 2138 sample[13][sb] = -tmp1;
2129 tmp1 = sample[17][sb]; 2139 tmp1 = sample[17][sb];
2130 sample[15][sb] = -tmp2; 2140 sample[15][sb] = -tmp2;
2131 sample[17][sb] = -tmp1; 2141 sample[17][sb] = -tmp1;
2132 } 2142 }
2133# else 2143# else
2134 for (i = 1; i < 18; i += 2) 2144 for (i = 1; i < 18; i += 2)
2135 sample[i][sb] = -sample[i][sb]; 2145 sample[i][sb] = -sample[i][sb];
2136# endif 2146# endif
2137} 2147}
2138 2148
2139/* 2149/*
2140 * NAME:III_decode() 2150 * NAME:III_decode()
2141 * DESCRIPTION:decode frame main_data 2151 * DESCRIPTION:decode frame main_data
2142 */ 2152 */
2143static 2153static
2144int III_decode(struct mad_bitptr *ptr, struct mad_frame *frame, 2154enum mad_error III_decode(struct mad_bitptr *ptr, struct mad_frame *frame,
2145 struct sideinfo *si, unsigned int nch) 2155 struct sideinfo *si, unsigned int nch)
2146{ 2156{
2147 struct mad_header *header = &frame->header; 2157 struct mad_header *header = &frame->header;
2148 unsigned int sfreqi, ngr, gr; 2158 unsigned int sfreqi, ngr, gr;
2149 2159
2150 { 2160 {
2151 unsigned int sfreq; 2161 unsigned int sfreq;
2152 2162
2153 sfreq = header->samplerate; 2163 sfreq = header->samplerate;
2154 if (header->flags & MAD_FLAG_MPEG_2_5_EXT) 2164 if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
2155 sfreq *= 2; 2165 sfreq *= 2;
2156 2166
2157 /* 48000 => 0, 44100 => 1, 32000 => 2, 2167 /* 48000 => 0, 44100 => 1, 32000 => 2,
2158 24000 => 3, 22050 => 4, 16000 => 5 */ 2168 24000 => 3, 22050 => 4, 16000 => 5 */
2159 sfreqi = ((sfreq >> 7) & 0x000f) + 2169 sfreqi = ((sfreq >> 7) & 0x000f) +
2160 ((sfreq >> 15) & 0x0001) - 8; 2170 ((sfreq >> 15) & 0x0001) - 8;
2161 2171
2162 if (header->flags & MAD_FLAG_MPEG_2_5_EXT) 2172 if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
2163 sfreqi += 3; 2173 sfreqi += 3;
2164 } 2174 }
2165 2175
2166 /* scalefactors, Huffman decoding, requantization */ 2176 /* scalefactors, Huffman decoding, requantization */
2167 2177
2168 ngr = (header->flags & MAD_FLAG_LSF_EXT) ? 1 : 2; 2178 ngr = (header->flags & MAD_FLAG_LSF_EXT) ? 1 : 2;
2169 2179
2170 for (gr = 0; gr < ngr; ++gr) { 2180 for (gr = 0; gr < ngr; ++gr) {
2171 struct granule *granule = &si->gr[gr]; 2181 struct granule *granule = &si->gr[gr];
2172 unsigned char const *sfbwidth = 0; 2182 unsigned char const *sfbwidth[2];
2173 mad_fixed_t xr[2][576]; 2183 mad_fixed_t xr[2][576];
2174 unsigned int ch; 2184 unsigned int ch;
2175 enum mad_error error; 2185 enum mad_error error;
2176 2186
2177 for (ch = 0; ch < nch; ++ch) { 2187 for (ch = 0; ch < nch; ++ch) {
2178 struct channel *channel = &granule->ch[ch]; 2188 struct channel *channel = &granule->ch[ch];
2179 unsigned int part2_length; 2189 unsigned int part2_length;
2180 2190
2181 sfbwidth = sfbwidth_table[sfreqi].l; 2191 sfbwidth[ch] = sfbwidth_table[sfreqi].l;
2182 if (channel->block_type == 2) { 2192 if (channel->block_type == 2) {
2183 sfbwidth = (channel->flags & mixed_block_flag) ? 2193 sfbwidth[ch] = (channel->flags & mixed_block_flag) ?
2184 sfbwidth_table[sfreqi].m : sfbwidth_table[sfreqi].s; 2194 sfbwidth_table[sfreqi].m : sfbwidth_table[sfreqi].s;
2185 } 2195 }
2186 2196
2187 if (header->flags & MAD_FLAG_LSF_EXT) { 2197 if (header->flags & MAD_FLAG_LSF_EXT) {
2188 part2_length = III_scalefactors_lsf(ptr, channel, 2198 part2_length = III_scalefactors_lsf(ptr, channel,
2189 ch == 0 ? 0 : &si->gr[1].ch[1], 2199 ch == 0 ? 0 : &si->gr[1].ch[1],
2190 header->mode_extension); 2200 header->mode_extension);
2191 } 2201 }
2192 else { 2202 else {
2193 part2_length = III_scalefactors(ptr, channel, &si->gr[0].ch[ch], 2203 part2_length = III_scalefactors(ptr, channel, &si->gr[0].ch[ch],
2194 gr == 0 ? 0 : si->scfsi[ch]); 2204 gr == 0 ? 0 : si->scfsi[ch]);
2195 } 2205 }
2196 2206
2197 error = III_huffdecode(ptr, xr[ch], channel, sfbwidth, part2_length); 2207 error = III_huffdecode(ptr, xr[ch], channel, sfbwidth[ch], part2_length);
2198 if (error) 2208 if (error)
2199 return error; 2209 return error;
2200 } 2210 }
2201 2211
2202 /* joint stereo processing */ 2212 /* joint stereo processing */
2203 2213
2204 if (header->mode == MAD_MODE_JOINT_STEREO && header->mode_extension) { 2214 if (header->mode == MAD_MODE_JOINT_STEREO && header->mode_extension) {
2205 error = III_stereo(xr, granule, header, sfbwidth); 2215 error = III_stereo(xr, granule, header, sfbwidth[0]);
2206 if (error) 2216 if (error)
2207 return error; 2217 return error;
2208 } 2218 }
2209 2219
2210 /* reordering, alias reduction, IMDCT, overlap-add, frequency inversion */ 2220 /* reordering, alias reduction, IMDCT, overlap-add, frequency inversion */
2211 2221
2212 for (ch = 0; ch < nch; ++ch) { 2222 for (ch = 0; ch < nch; ++ch) {
2213 struct channel const *channel = &granule->ch[ch]; 2223 struct channel const *channel = &granule->ch[ch];
2214 mad_fixed_t (*sample)[32] = &frame->sbsample[ch][18 * gr]; 2224 mad_fixed_t (*sample)[32] = &frame->sbsample[ch][18 * gr];
2215 unsigned int sb, l, i, sblimit; 2225 unsigned int sb, l, i, sblimit;
2216 mad_fixed_t output[36]; 2226 mad_fixed_t output[36];
2217 2227
2218 if (channel->block_type == 2) { 2228 if (channel->block_type == 2) {
2219 III_reorder(xr[ch], channel, sfbwidth_table[sfreqi].s); 2229 III_reorder(xr[ch], channel, sfbwidth[ch]);
2220 2230
2221# if !defined(OPT_STRICT) 2231# if !defined(OPT_STRICT)
2222 /* 2232 /*
2223 * According to ISO/IEC 11172-3, "Alias reduction is not applied for 2233 * According to ISO/IEC 11172-3, "Alias reduction is not applied for
2224 * granules with block_type == 2 (short block)." However, other 2234 * granules with block_type == 2 (short block)." However, other
2225 * sources suggest alias reduction should indeed be performed on the 2235 * sources suggest alias reduction should indeed be performed on the
2226 * lower two subbands of mixed blocks. Most other implementations do 2236 * lower two subbands of mixed blocks. Most other implementations do
2227 * this, so by default we will too. 2237 * this, so by default we will too.
2228 */ 2238 */
2229 if (channel->flags & mixed_block_flag) 2239 if (channel->flags & mixed_block_flag)
2230 III_aliasreduce(xr[ch], 36); 2240 III_aliasreduce(xr[ch], 36);
2231# endif 2241# endif
2232 } 2242 }
2233 else 2243 else
2234 III_aliasreduce(xr[ch], 576); 2244 III_aliasreduce(xr[ch], 576);
2235 2245
2236 l = 0; 2246 l = 0;
2237 2247
2238 /* subbands 0-1 */ 2248 /* subbands 0-1 */
2239 2249
2240 if (channel->block_type != 2 || (channel->flags & mixed_block_flag)) { 2250 if (channel->block_type != 2 || (channel->flags & mixed_block_flag)) {
2241 unsigned int block_type; 2251 unsigned int block_type;
2242 2252
2243 block_type = channel->block_type; 2253 block_type = channel->block_type;
@@ -2279,49 +2289,49 @@ int III_decode(struct mad_bitptr *ptr, struct mad_frame *frame,
2279 } 2289 }
2280 } 2290 }
2281 else { 2291 else {
2282 /* short blocks */ 2292 /* short blocks */
2283 for (sb = 2; sb < sblimit; ++sb, l += 18) { 2293 for (sb = 2; sb < sblimit; ++sb, l += 18) {
2284 III_imdct_s(&xr[ch][l], output); 2294 III_imdct_s(&xr[ch][l], output);
2285 III_overlap(output, (*frame->overlap)[ch][sb], sample, sb); 2295 III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2286 2296
2287 if (sb & 1) 2297 if (sb & 1)
2288 III_freqinver(sample, sb); 2298 III_freqinver(sample, sb);
2289 } 2299 }
2290 } 2300 }
2291 2301
2292 /* remaining (zero) subbands */ 2302 /* remaining (zero) subbands */
2293 2303
2294 for (sb = sblimit; sb < 32; ++sb) { 2304 for (sb = sblimit; sb < 32; ++sb) {
2295 III_overlap_z((*frame->overlap)[ch][sb], sample, sb); 2305 III_overlap_z((*frame->overlap)[ch][sb], sample, sb);
2296 2306
2297 if (sb & 1) 2307 if (sb & 1)
2298 III_freqinver(sample, sb); 2308 III_freqinver(sample, sb);
2299 } 2309 }
2300 } 2310 }
2301 } 2311 }
2302 2312
2303 return 0; 2313 return MAD_ERROR_NONE;
2304} 2314}
2305 2315
2306/* 2316/*
2307 * NAME:layer->III() 2317 * NAME:layer->III()
2308 * DESCRIPTION:decode a single Layer III frame 2318 * DESCRIPTION:decode a single Layer III frame
2309 */ 2319 */
2310int mad_layer_III(struct mad_stream *stream, struct mad_frame *frame) 2320int mad_layer_III(struct mad_stream *stream, struct mad_frame *frame)
2311{ 2321{
2312 struct mad_header *header = &frame->header; 2322 struct mad_header *header = &frame->header;
2313 unsigned int nch, priv_bitlen, next_md_begin = 0; 2323 unsigned int nch, priv_bitlen, next_md_begin = 0;
2314 unsigned int si_len, data_bitlen, md_len; 2324 unsigned int si_len, data_bitlen, md_len;
2315 unsigned int frame_space, frame_used, frame_free; 2325 unsigned int frame_space, frame_used, frame_free;
2316 struct mad_bitptr ptr; 2326 struct mad_bitptr ptr;
2317 struct sideinfo si; 2327 struct sideinfo si;
2318 enum mad_error error; 2328 enum mad_error error;
2319 int result = 0; 2329 int result = 0;
2320 2330
2321 /* allocate Layer III dynamic structures */ 2331 /* allocate Layer III dynamic structures */
2322 2332
2323 if (stream->main_data == 0) { 2333 if (stream->main_data == 0) {
2324 stream->main_data = malloc(MAD_BUFFER_MDLEN); 2334 stream->main_data = malloc(MAD_BUFFER_MDLEN);
2325 if (stream->main_data == 0) { 2335 if (stream->main_data == 0) {
2326 stream->error = MAD_ERROR_NOMEM; 2336 stream->error = MAD_ERROR_NOMEM;
2327 return -1; 2337 return -1;