rtoss - Diff between revs 14 and 26

Subversion Repositories:
Rev:
Only display areas with differences - Ignore whitespace
Rev 14 Rev 26
1 /* 1 /*
2  * libmad - MPEG audio decoder library 2  * libmad - MPEG audio decoder library
3  * Copyright (C) 2000-2003 Underbit Technologies, Inc. -  
-   3  * Copyright (C) 2000-2004 Underbit Technologies, Inc.
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: layer3.c,v 1.41 2003/05/28 04:24:36 rob Exp $ -  
-   19  * $Id: layer3.c,v 1.43 2004/01/23 09:41:32 rob Exp $
20  */ 20  */
21 21
22 # ifdef HAVE_CONFIG_H 22 # ifdef HAVE_CONFIG_H
23 #  include "config.h" 23 #  include "config.h"
24 # endif 24 # endif
25 25
26 # include "global.h" 26 # include "global.h"
27 27
28 # include <stdlib.h> 28 # include <stdlib.h>
29 # include <string.h> 29 # include <string.h>
30 30
31 # ifdef HAVE_ASSERT_H 31 # ifdef HAVE_ASSERT_H
32 #  include <assert.h> 32 #  include <assert.h>
33 # endif 33 # endif
34 34
35 # ifdef HAVE_LIMITS_H 35 # ifdef HAVE_LIMITS_H
36 #  include <limits.h> 36 #  include <limits.h>
37 # else 37 # else
38 #  define CHAR_BIT  8 38 #  define CHAR_BIT  8
39 # endif 39 # endif
40 40
41 # include "fixed.h" 41 # include "fixed.h"
42 # include "bit.h" 42 # include "bit.h"
43 # include "stream.h" 43 # include "stream.h"
44 # include "frame.h" 44 # include "frame.h"
45 # include "huffman.h" 45 # include "huffman.h"
46 # include "layer3.h" 46 # include "layer3.h"
47 47
48 /* --- Layer III ----------------------------------------------------------- */ 48 /* --- Layer III ----------------------------------------------------------- */
49 49
50 enum { 50 enum {
51   count1table_select = 0x01, 51   count1table_select = 0x01,
52   scalefac_scale     = 0x02, 52   scalefac_scale     = 0x02,
53   preflag            = 0x04, 53   preflag            = 0x04,
54   mixed_block_flag   = 0x08 54   mixed_block_flag   = 0x08
55 }; 55 };
56 56
57 enum { 57 enum {
58   I_STEREO  = 0x1, 58   I_STEREO  = 0x1,
59   MS_STEREO = 0x2 59   MS_STEREO = 0x2
60 }; 60 };
61 61
62 struct sideinfo { 62 struct sideinfo {
63   unsigned int main_data_begin; 63   unsigned int main_data_begin;
64   unsigned int private_bits; 64   unsigned int private_bits;
65 65
66   unsigned char scfsi[2]; 66   unsigned char scfsi[2];
67 67
68   struct granule { 68   struct granule {
69     struct channel { 69     struct channel {
70       /* from side info */ 70       /* from side info */
71       unsigned short part2_3_length; 71       unsigned short part2_3_length;
72       unsigned short big_values; 72       unsigned short big_values;
73       unsigned short global_gain; 73       unsigned short global_gain;
74       unsigned short scalefac_compress; 74       unsigned short scalefac_compress;
75 75
76       unsigned char flags; 76       unsigned char flags;
77       unsigned char block_type; 77       unsigned char block_type;
78       unsigned char table_select[3]; 78       unsigned char table_select[3];
79       unsigned char subblock_gain[3]; 79       unsigned char subblock_gain[3];
80       unsigned char region0_count; 80       unsigned char region0_count;
81       unsigned char region1_count; 81       unsigned char region1_count;
82 82
83       /* from main_data */ 83       /* from main_data */
84       unsigned char scalefac[39];       /* scalefac_l and/or scalefac_s */ 84       unsigned char scalefac[39];       /* scalefac_l and/or scalefac_s */
85     } ch[2]; 85     } ch[2];
86   } gr[2]; 86   } gr[2];
87 }; 87 };
88 88
89 /* 89 /*
90  * scalefactor bit lengths 90  * scalefactor bit lengths
91  * derived from section 2.4.2.7 of ISO/IEC 11172-3 91  * derived from section 2.4.2.7 of ISO/IEC 11172-3
92  */ 92  */
93 static 93 static
94 struct { 94 struct {
95   unsigned char slen1; 95   unsigned char slen1;
96   unsigned char slen2; 96   unsigned char slen2;
97 } const sflen_table[16] = { 97 } const sflen_table[16] = {
98   { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 }, 98   { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 },
99   { 3, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 }, 99   { 3, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 },
100   { 2, 1 }, { 2, 2 }, { 2, 3 }, { 3, 1 }, 100   { 2, 1 }, { 2, 2 }, { 2, 3 }, { 3, 1 },
101   { 3, 2 }, { 3, 3 }, { 4, 2 }, { 4, 3 } 101   { 3, 2 }, { 3, 3 }, { 4, 2 }, { 4, 3 }
102 }; 102 };
103 103
104 /* 104 /*
105  * number of LSF scalefactor band values 105  * number of LSF scalefactor band values
106  * derived from section 2.4.3.2 of ISO/IEC 13818-3 106  * derived from section 2.4.3.2 of ISO/IEC 13818-3
107  */ 107  */
108 static 108 static
109 unsigned char const nsfb_table[6][3][4] = { 109 unsigned char const nsfb_table[6][3][4] = {
110   { {  6,  5,  5, 5 }, 110   { {  6,  5,  5, 5 },
111     {  9,  9,  9, 9 }, 111     {  9,  9,  9, 9 },
112     {  6,  9,  9, 9 } }, 112     {  6,  9,  9, 9 } },
113 113
114   { {  6,  5,  7, 3 }, 114   { {  6,  5,  7, 3 },
115     {  9,  9, 12, 6 }, 115     {  9,  9, 12, 6 },
116     {  6,  9, 12, 6 } }, 116     {  6,  9, 12, 6 } },
117 117
118   { { 11, 10,  0, 0 }, 118   { { 11, 10,  0, 0 },
119     { 18, 18,  0, 0 }, 119     { 18, 18,  0, 0 },
120     { 15, 18,  0, 0 } }, 120     { 15, 18,  0, 0 } },
121 121
122   { {  7,  7,  7, 0 }, 122   { {  7,  7,  7, 0 },
123     { 12, 12, 12, 0 }, 123     { 12, 12, 12, 0 },
124     {  6, 15, 12, 0 } }, 124     {  6, 15, 12, 0 } },
125 125
126   { {  6,  6,  6, 3 }, 126   { {  6,  6,  6, 3 },
127     { 12,  9,  9, 6 }, 127     { 12,  9,  9, 6 },
128     {  6, 12,  9, 6 } }, 128     {  6, 12,  9, 6 } },
129 129
130   { {  8,  8,  5, 0 }, 130   { {  8,  8,  5, 0 },
131     { 15, 12,  9, 0 }, 131     { 15, 12,  9, 0 },
132     {  6, 18,  9, 0 } } 132     {  6, 18,  9, 0 } }
133 }; 133 };
134 134
135 /* 135 /*
136  * MPEG-1 scalefactor band widths 136  * MPEG-1 scalefactor band widths
137  * derived from Table B.8 of ISO/IEC 11172-3 137  * derived from Table B.8 of ISO/IEC 11172-3
138  */ 138  */
139 static 139 static
140 unsigned char const sfb_48000_long[] = { 140 unsigned char const sfb_48000_long[] = {
141    4,  4,  4,  4,  4,  4,  6,  6,  6,   8,  10, 141    4,  4,  4,  4,  4,  4,  6,  6,  6,   8,  10,
142   12, 16, 18, 22, 28, 34, 40, 46, 54,  54, 192 142   12, 16, 18, 22, 28, 34, 40, 46, 54,  54, 192
143 }; 143 };
144 144
145 static 145 static
146 unsigned char const sfb_44100_long[] = { 146 unsigned char const sfb_44100_long[] = {
147    4,  4,  4,  4,  4,  4,  6,  6,  8,   8,  10, 147    4,  4,  4,  4,  4,  4,  6,  6,  8,   8,  10,
148   12, 16, 20, 24, 28, 34, 42, 50, 54,  76, 158 148   12, 16, 20, 24, 28, 34, 42, 50, 54,  76, 158
149 }; 149 };
150 150
151 static 151 static
152 unsigned char const sfb_32000_long[] = { 152 unsigned char const sfb_32000_long[] = {
153    4,  4,  4,  4,  4,  4,  6,  6,  8,  10,  12, 153    4,  4,  4,  4,  4,  4,  6,  6,  8,  10,  12,
154   16, 20, 24, 30, 38, 46, 56, 68, 84, 102,  26 154   16, 20, 24, 30, 38, 46, 56, 68, 84, 102,  26
155 }; 155 };
156 156
157 static 157 static
158 unsigned char const sfb_48000_short[] = { 158 unsigned char const sfb_48000_short[] = {
159    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  6, 159    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  6,
160    6,  6,  6,  6,  6, 10, 10, 10, 12, 12, 12, 14, 14, 160    6,  6,  6,  6,  6, 10, 10, 10, 12, 12, 12, 14, 14,
161   14, 16, 16, 16, 20, 20, 20, 26, 26, 26, 66, 66, 66 161   14, 16, 16, 16, 20, 20, 20, 26, 26, 26, 66, 66, 66
162 }; 162 };
163 163
164 static 164 static
165 unsigned char const sfb_44100_short[] = { 165 unsigned char const sfb_44100_short[] = {
166    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  6, 166    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  6,
167    6,  6,  8,  8,  8, 10, 10, 10, 12, 12, 12, 14, 14, 167    6,  6,  8,  8,  8, 10, 10, 10, 12, 12, 12, 14, 14,
168   14, 18, 18, 18, 22, 22, 22, 30, 30, 30, 56, 56, 56 168   14, 18, 18, 18, 22, 22, 22, 30, 30, 30, 56, 56, 56
169 }; 169 };
170 170
171 static 171 static
172 unsigned char const sfb_32000_short[] = { 172 unsigned char const sfb_32000_short[] = {
173    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  6, 173    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  6,
174    6,  6,  8,  8,  8, 12, 12, 12, 16, 16, 16, 20, 20, 174    6,  6,  8,  8,  8, 12, 12, 12, 16, 16, 16, 20, 20,
175   20, 26, 26, 26, 34, 34, 34, 42, 42, 42, 12, 12, 12 175   20, 26, 26, 26, 34, 34, 34, 42, 42, 42, 12, 12, 12
176 }; 176 };
177 177
178 static 178 static
179 unsigned char const sfb_48000_mixed[] = { 179 unsigned char const sfb_48000_mixed[] = {
180   /* long */   4,  4,  4,  4,  4,  4,  6,  6, 180   /* long */   4,  4,  4,  4,  4,  4,  6,  6,
181   /* short */  4,  4,  4,  6,  6,  6,  6,  6,  6, 10, 181   /* short */  4,  4,  4,  6,  6,  6,  6,  6,  6, 10,
182               10, 10, 12, 12, 12, 14, 14, 14, 16, 16, 182               10, 10, 12, 12, 12, 14, 14, 14, 16, 16,
183               16, 20, 20, 20, 26, 26, 26, 66, 66, 66 183               16, 20, 20, 20, 26, 26, 26, 66, 66, 66
184 }; 184 };
185 185
186 static 186 static
187 unsigned char const sfb_44100_mixed[] = { 187 unsigned char const sfb_44100_mixed[] = {
188   /* long */   4,  4,  4,  4,  4,  4,  6,  6, 188   /* long */   4,  4,  4,  4,  4,  4,  6,  6,
189   /* short */  4,  4,  4,  6,  6,  6,  8,  8,  8, 10, 189   /* short */  4,  4,  4,  6,  6,  6,  8,  8,  8, 10,
190               10, 10, 12, 12, 12, 14, 14, 14, 18, 18, 190               10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
191               18, 22, 22, 22, 30, 30, 30, 56, 56, 56 191               18, 22, 22, 22, 30, 30, 30, 56, 56, 56
192 }; 192 };
193 193
194 static 194 static
195 unsigned char const sfb_32000_mixed[] = { 195 unsigned char const sfb_32000_mixed[] = {
196   /* long */   4,  4,  4,  4,  4,  4,  6,  6, 196   /* long */   4,  4,  4,  4,  4,  4,  6,  6,
197   /* short */  4,  4,  4,  6,  6,  6,  8,  8,  8, 12, 197   /* short */  4,  4,  4,  6,  6,  6,  8,  8,  8, 12,
198               12, 12, 16, 16, 16, 20, 20, 20, 26, 26, 198               12, 12, 16, 16, 16, 20, 20, 20, 26, 26,
199               26, 34, 34, 34, 42, 42, 42, 12, 12, 12 199               26, 34, 34, 34, 42, 42, 42, 12, 12, 12
200 }; 200 };
201 201
202 /* 202 /*
203  * MPEG-2 scalefactor band widths 203  * MPEG-2 scalefactor band widths
204  * derived from Table B.2 of ISO/IEC 13818-3 204  * derived from Table B.2 of ISO/IEC 13818-3
205  */ 205  */
206 static 206 static
207 unsigned char const sfb_24000_long[] = { 207 unsigned char const sfb_24000_long[] = {
208    6,  6,  6,  6,  6,  6,  8, 10, 12,  14,  16, 208    6,  6,  6,  6,  6,  6,  8, 10, 12,  14,  16,
209   18, 22, 26, 32, 38, 46, 54, 62, 70,  76,  36 209   18, 22, 26, 32, 38, 46, 54, 62, 70,  76,  36
210 }; 210 };
211 211
212 static 212 static
213 unsigned char const sfb_22050_long[] = { 213 unsigned char const sfb_22050_long[] = {
214    6,  6,  6,  6,  6,  6,  8, 10, 12,  14,  16, 214    6,  6,  6,  6,  6,  6,  8, 10, 12,  14,  16,
215   20, 24, 28, 32, 38, 46, 52, 60, 68,  58,  54 215   20, 24, 28, 32, 38, 46, 52, 60, 68,  58,  54
216 }; 216 };
217 217
218 # define sfb_16000_long  sfb_22050_long 218 # define sfb_16000_long  sfb_22050_long
219 219
220 static 220 static
221 unsigned char const sfb_24000_short[] = { 221 unsigned char const sfb_24000_short[] = {
222    4,  4,  4,  4,  4,  4,  4,  4,  4,  6,  6,  6,  8, 222    4,  4,  4,  4,  4,  4,  4,  4,  4,  6,  6,  6,  8,
223    8,  8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18, 223    8,  8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
224   18, 24, 24, 24, 32, 32, 32, 44, 44, 44, 12, 12, 12 224   18, 24, 24, 24, 32, 32, 32, 44, 44, 44, 12, 12, 12
225 }; 225 };
226 226
227 static 227 static
228 unsigned char const sfb_22050_short[] = { 228 unsigned char const sfb_22050_short[] = {
229    4,  4,  4,  4,  4,  4,  4,  4,  4,  6,  6,  6,  6, 229    4,  4,  4,  4,  4,  4,  4,  4,  4,  6,  6,  6,  6,
230    6,  6,  8,  8,  8, 10, 10, 10, 14, 14, 14, 18, 18, 230    6,  6,  8,  8,  8, 10, 10, 10, 14, 14, 14, 18, 18,
231   18, 26, 26, 26, 32, 32, 32, 42, 42, 42, 18, 18, 18 231   18, 26, 26, 26, 32, 32, 32, 42, 42, 42, 18, 18, 18
232 }; 232 };
233 233
234 static 234 static
235 unsigned char const sfb_16000_short[] = { 235 unsigned char const sfb_16000_short[] = {
236    4,  4,  4,  4,  4,  4,  4,  4,  4,  6,  6,  6,  8, 236    4,  4,  4,  4,  4,  4,  4,  4,  4,  6,  6,  6,  8,
237    8,  8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18, 237    8,  8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
238   18, 24, 24, 24, 30, 30, 30, 40, 40, 40, 18, 18, 18 238   18, 24, 24, 24, 30, 30, 30, 40, 40, 40, 18, 18, 18
239 }; 239 };
240 240
241 static 241 static
242 unsigned char const sfb_24000_mixed[] = { 242 unsigned char const sfb_24000_mixed[] = {
243   /* long */   6,  6,  6,  6,  6,  6, 243   /* long */   6,  6,  6,  6,  6,  6,
244   /* short */  6,  6,  6,  8,  8,  8, 10, 10, 10, 12, 244   /* short */  6,  6,  6,  8,  8,  8, 10, 10, 10, 12,
245               12, 12, 14, 14, 14, 18, 18, 18, 24, 24, 245               12, 12, 14, 14, 14, 18, 18, 18, 24, 24,
246               24, 32, 32, 32, 44, 44, 44, 12, 12, 12 246               24, 32, 32, 32, 44, 44, 44, 12, 12, 12
247 }; 247 };
248 248
249 static 249 static
250 unsigned char const sfb_22050_mixed[] = { 250 unsigned char const sfb_22050_mixed[] = {
251   /* long */   6,  6,  6,  6,  6,  6, 251   /* long */   6,  6,  6,  6,  6,  6,
252   /* short */  6,  6,  6,  6,  6,  6,  8,  8,  8, 10, 252   /* short */  6,  6,  6,  6,  6,  6,  8,  8,  8, 10,
253               10, 10, 14, 14, 14, 18, 18, 18, 26, 26, 253               10, 10, 14, 14, 14, 18, 18, 18, 26, 26,
254               26, 32, 32, 32, 42, 42, 42, 18, 18, 18 254               26, 32, 32, 32, 42, 42, 42, 18, 18, 18
255 }; 255 };
256 256
257 static 257 static
258 unsigned char const sfb_16000_mixed[] = { 258 unsigned char const sfb_16000_mixed[] = {
259   /* long */   6,  6,  6,  6,  6,  6, 259   /* long */   6,  6,  6,  6,  6,  6,
260   /* short */  6,  6,  6,  8,  8,  8, 10, 10, 10, 12, 260   /* short */  6,  6,  6,  8,  8,  8, 10, 10, 10, 12,
261               12, 12, 14, 14, 14, 18, 18, 18, 24, 24, 261               12, 12, 14, 14, 14, 18, 18, 18, 24, 24,
262               24, 30, 30, 30, 40, 40, 40, 18, 18, 18 262               24, 30, 30, 30, 40, 40, 40, 18, 18, 18
263 }; 263 };
264 264
265 /* 265 /*
266  * MPEG 2.5 scalefactor band widths 266  * MPEG 2.5 scalefactor band widths
267  * derived from public sources 267  * derived from public sources
268  */ 268  */
269 # define sfb_12000_long  sfb_16000_long 269 # define sfb_12000_long  sfb_16000_long
270 # define sfb_11025_long  sfb_12000_long 270 # define sfb_11025_long  sfb_12000_long
271 271
272 static 272 static
273 unsigned char const sfb_8000_long[] = { 273 unsigned char const sfb_8000_long[] = {
274   12, 12, 12, 12, 12, 12, 16, 20, 24,  28,  32, 274   12, 12, 12, 12, 12, 12, 16, 20, 24,  28,  32,
275   40, 48, 56, 64, 76, 90,  2,  2,  2,   2,   2 275   40, 48, 56, 64, 76, 90,  2,  2,  2,   2,   2
276 }; 276 };
277 277
278 # define sfb_12000_short  sfb_16000_short 278 # define sfb_12000_short  sfb_16000_short
279 # define sfb_11025_short  sfb_12000_short 279 # define sfb_11025_short  sfb_12000_short
280 280
281 static 281 static
282 unsigned char const sfb_8000_short[] = { 282 unsigned char const sfb_8000_short[] = {
283    8,  8,  8,  8,  8,  8,  8,  8,  8, 12, 12, 12, 16, 283    8,  8,  8,  8,  8,  8,  8,  8,  8, 12, 12, 12, 16,
284   16, 16, 20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36, 284   16, 16, 20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36,
285   36,  2,  2,  2,  2,  2,  2,  2,  2,  2, 26, 26, 26 285   36,  2,  2,  2,  2,  2,  2,  2,  2,  2, 26, 26, 26
286 }; 286 };
287 287
288 # define sfb_12000_mixed  sfb_16000_mixed 288 # define sfb_12000_mixed  sfb_16000_mixed
289 # define sfb_11025_mixed  sfb_12000_mixed 289 # define sfb_11025_mixed  sfb_12000_mixed
290 290
291 /* the 8000 Hz short block scalefactor bands do not break after 291 /* the 8000 Hz short block scalefactor bands do not break after
292    the first 36 frequency lines, so this is probably wrong */ 292    the first 36 frequency lines, so this is probably wrong */
293 static 293 static
294 unsigned char const sfb_8000_mixed[] = { 294 unsigned char const sfb_8000_mixed[] = {
295   /* long */  12, 12, 12, 295   /* long */  12, 12, 12,
296   /* short */  4,  4,  4,  8,  8,  8, 12, 12, 12, 16, 16, 16, 296   /* short */  4,  4,  4,  8,  8,  8, 12, 12, 12, 16, 16, 16,
297               20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36, 36, 297               20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36, 36,
298                2,  2,  2,  2,  2,  2,  2,  2,  2, 26, 26, 26 298                2,  2,  2,  2,  2,  2,  2,  2,  2, 26, 26, 26
299 }; 299 };
300 300
301 static 301 static
302 struct { 302 struct {
303   unsigned char const *l; 303   unsigned char const *l;
304   unsigned char const *s; 304   unsigned char const *s;
305   unsigned char const *m; 305   unsigned char const *m;
306 } const sfbwidth_table[9] = { 306 } const sfbwidth_table[9] = {
307   { sfb_48000_long, sfb_48000_short, sfb_48000_mixed }, 307   { sfb_48000_long, sfb_48000_short, sfb_48000_mixed },
308   { sfb_44100_long, sfb_44100_short, sfb_44100_mixed }, 308   { sfb_44100_long, sfb_44100_short, sfb_44100_mixed },
309   { sfb_32000_long, sfb_32000_short, sfb_32000_mixed }, 309   { sfb_32000_long, sfb_32000_short, sfb_32000_mixed },
310   { sfb_24000_long, sfb_24000_short, sfb_24000_mixed }, 310   { sfb_24000_long, sfb_24000_short, sfb_24000_mixed },
311   { sfb_22050_long, sfb_22050_short, sfb_22050_mixed }, 311   { sfb_22050_long, sfb_22050_short, sfb_22050_mixed },
312   { sfb_16000_long, sfb_16000_short, sfb_16000_mixed }, 312   { sfb_16000_long, sfb_16000_short, sfb_16000_mixed },
313   { sfb_12000_long, sfb_12000_short, sfb_12000_mixed }, 313   { sfb_12000_long, sfb_12000_short, sfb_12000_mixed },
314   { sfb_11025_long, sfb_11025_short, sfb_11025_mixed }, 314   { sfb_11025_long, sfb_11025_short, sfb_11025_mixed },
315   {  sfb_8000_long,  sfb_8000_short,  sfb_8000_mixed } 315   {  sfb_8000_long,  sfb_8000_short,  sfb_8000_mixed }
316 }; 316 };
317 317
318 /* 318 /*
319  * scalefactor band preemphasis (used only when preflag is set) 319  * scalefactor band preemphasis (used only when preflag is set)
320  * derived from Table B.6 of ISO/IEC 11172-3 320  * derived from Table B.6 of ISO/IEC 11172-3
321  */ 321  */
322 static 322 static
323 unsigned char const pretab[22] = { 323 unsigned char const pretab[22] = {
324   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0 324   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0
325 }; 325 };
326 326
327 /* 327 /*
328  * table for requantization 328  * table for requantization
329  * 329  *
330  * rq_table[x].mantissa * 2^(rq_table[x].exponent) = x^(4/3) 330  * rq_table[x].mantissa * 2^(rq_table[x].exponent) = x^(4/3)
331  */ 331  */
332 static 332 static
333 struct fixedfloat { 333 struct fixedfloat {
334   unsigned long mantissa  : 27; 334   unsigned long mantissa  : 27;
335   unsigned short exponent :  5; 335   unsigned short exponent :  5;
336 } const rq_table[8207] = { 336 } const rq_table[8207] = {
337 # include "rq_table.dat" 337 # include "rq_table.dat"
338 }; 338 };
339 339
340 /* 340 /*
341  * fractional powers of two 341  * fractional powers of two
342  * used for requantization and joint stereo decoding 342  * used for requantization and joint stereo decoding
343  * 343  *
344  * root_table[3 + x] = 2^(x/4) 344  * root_table[3 + x] = 2^(x/4)
345  */ 345  */
346 static 346 static
347 mad_fixed_t const root_table[7] = { 347 mad_fixed_t const root_table[7] = {
348   MAD_F(0x09837f05) /* 2^(-3/4) == 0.59460355750136 */, 348   MAD_F(0x09837f05) /* 2^(-3/4) == 0.59460355750136 */,
349   MAD_F(0x0b504f33) /* 2^(-2/4) == 0.70710678118655 */, 349   MAD_F(0x0b504f33) /* 2^(-2/4) == 0.70710678118655 */,
350   MAD_F(0x0d744fcd) /* 2^(-1/4) == 0.84089641525371 */, 350   MAD_F(0x0d744fcd) /* 2^(-1/4) == 0.84089641525371 */,
351   MAD_F(0x10000000) /* 2^( 0/4) == 1.00000000000000 */, 351   MAD_F(0x10000000) /* 2^( 0/4) == 1.00000000000000 */,
352   MAD_F(0x1306fe0a) /* 2^(+1/4) == 1.18920711500272 */, 352   MAD_F(0x1306fe0a) /* 2^(+1/4) == 1.18920711500272 */,
353   MAD_F(0x16a09e66) /* 2^(+2/4) == 1.41421356237310 */, 353   MAD_F(0x16a09e66) /* 2^(+2/4) == 1.41421356237310 */,
354   MAD_F(0x1ae89f99) /* 2^(+3/4) == 1.68179283050743 */ 354   MAD_F(0x1ae89f99) /* 2^(+3/4) == 1.68179283050743 */
355 }; 355 };
356 356
357 /* 357 /*
358  * coefficients for aliasing reduction 358  * coefficients for aliasing reduction
359  * derived from Table B.9 of ISO/IEC 11172-3 359  * derived from Table B.9 of ISO/IEC 11172-3
360  * 360  *
361  *  c[]  = { -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037 } 361  *  c[]  = { -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037 }
362  * cs[i] =    1 / sqrt(1 + c[i]^2) 362  * cs[i] =    1 / sqrt(1 + c[i]^2)
363  * ca[i] = c[i] / sqrt(1 + c[i]^2) 363  * ca[i] = c[i] / sqrt(1 + c[i]^2)
364  */ 364  */
365 static 365 static
366 mad_fixed_t const cs[8] = { 366 mad_fixed_t const cs[8] = {
367   +MAD_F(0x0db84a81) /* +0.857492926 */, +MAD_F(0x0e1b9d7f) /* +0.881741997 */, 367   +MAD_F(0x0db84a81) /* +0.857492926 */, +MAD_F(0x0e1b9d7f) /* +0.881741997 */,
368   +MAD_F(0x0f31adcf) /* +0.949628649 */, +MAD_F(0x0fbba815) /* +0.983314592 */, 368   +MAD_F(0x0f31adcf) /* +0.949628649 */, +MAD_F(0x0fbba815) /* +0.983314592 */,
369   +MAD_F(0x0feda417) /* +0.995517816 */, +MAD_F(0x0ffc8fc8) /* +0.999160558 */, 369   +MAD_F(0x0feda417) /* +0.995517816 */, +MAD_F(0x0ffc8fc8) /* +0.999160558 */,
370   +MAD_F(0x0fff964c) /* +0.999899195 */, +MAD_F(0x0ffff8d3) /* +0.999993155 */ 370   +MAD_F(0x0fff964c) /* +0.999899195 */, +MAD_F(0x0ffff8d3) /* +0.999993155 */
371 }; 371 };
372 372
373 static 373 static
374 mad_fixed_t const ca[8] = { 374 mad_fixed_t const ca[8] = {
375   -MAD_F(0x083b5fe7) /* -0.514495755 */, -MAD_F(0x078c36d2) /* -0.471731969 */, 375   -MAD_F(0x083b5fe7) /* -0.514495755 */, -MAD_F(0x078c36d2) /* -0.471731969 */,
376   -MAD_F(0x05039814) /* -0.313377454 */, -MAD_F(0x02e91dd1) /* -0.181913200 */, 376   -MAD_F(0x05039814) /* -0.313377454 */, -MAD_F(0x02e91dd1) /* -0.181913200 */,
377   -MAD_F(0x0183603a) /* -0.094574193 */, -MAD_F(0x00a7cb87) /* -0.040965583 */, 377   -MAD_F(0x0183603a) /* -0.094574193 */, -MAD_F(0x00a7cb87) /* -0.040965583 */,
378   -MAD_F(0x003a2847) /* -0.014198569 */, -MAD_F(0x000f27b4) /* -0.003699975 */ 378   -MAD_F(0x003a2847) /* -0.014198569 */, -MAD_F(0x000f27b4) /* -0.003699975 */
379 }; 379 };
380 380
381 /* 381 /*
382  * IMDCT coefficients for short blocks 382  * IMDCT coefficients for short blocks
383  * derived from section 2.4.3.4.10.2 of ISO/IEC 11172-3 383  * derived from section 2.4.3.4.10.2 of ISO/IEC 11172-3
384  * 384  *
385  * imdct_s[i/even][k] = cos((PI / 24) * (2 *       (i / 2) + 7) * (2 * k + 1)) 385  * imdct_s[i/even][k] = cos((PI / 24) * (2 *       (i / 2) + 7) * (2 * k + 1))
386  * imdct_s[i /odd][k] = cos((PI / 24) * (2 * (6 + (i-1)/2) + 7) * (2 * k + 1)) 386  * imdct_s[i /odd][k] = cos((PI / 24) * (2 * (6 + (i-1)/2) + 7) * (2 * k + 1))
387  */ 387  */
388 static 388 static
389 mad_fixed_t const imdct_s[6][6] = { 389 mad_fixed_t const imdct_s[6][6] = {
390 # include "imdct_s.dat" 390 # include "imdct_s.dat"
391 }; 391 };
392 392
393 # if !defined(ASO_IMDCT) 393 # if !defined(ASO_IMDCT)
394 /* 394 /*
395  * windowing coefficients for long blocks 395  * windowing coefficients for long blocks
396  * derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3 396  * derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
397  * 397  *
398  * window_l[i] = sin((PI / 36) * (i + 1/2)) 398  * window_l[i] = sin((PI / 36) * (i + 1/2))
399  */ 399  */
400 static 400 static
401 mad_fixed_t const window_l[36] = { 401 mad_fixed_t const window_l[36] = {
402   MAD_F(0x00b2aa3e) /* 0.043619387 */, MAD_F(0x0216a2a2) /* 0.130526192 */, 402   MAD_F(0x00b2aa3e) /* 0.043619387 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
403   MAD_F(0x03768962) /* 0.216439614 */, MAD_F(0x04cfb0e2) /* 0.300705800 */, 403   MAD_F(0x03768962) /* 0.216439614 */, MAD_F(0x04cfb0e2) /* 0.300705800 */,
404   MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x07635284) /* 0.461748613 */, 404   MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x07635284) /* 0.461748613 */,
405   MAD_F(0x0898c779) /* 0.537299608 */, MAD_F(0x09bd7ca0) /* 0.608761429 */, 405   MAD_F(0x0898c779) /* 0.537299608 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
406   MAD_F(0x0acf37ad) /* 0.675590208 */, MAD_F(0x0bcbe352) /* 0.737277337 */, 406   MAD_F(0x0acf37ad) /* 0.675590208 */, MAD_F(0x0bcbe352) /* 0.737277337 */,
407   MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x0d7e8807) /* 0.843391446 */, 407   MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x0d7e8807) /* 0.843391446 */,
408 408
409   MAD_F(0x0e313245) /* 0.887010833 */, MAD_F(0x0ec835e8) /* 0.923879533 */, 409   MAD_F(0x0e313245) /* 0.887010833 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
410   MAD_F(0x0f426cb5) /* 0.953716951 */, MAD_F(0x0f9ee890) /* 0.976296007 */, 410   MAD_F(0x0f426cb5) /* 0.953716951 */, MAD_F(0x0f9ee890) /* 0.976296007 */,
411   MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ffc19fd) /* 0.999048222 */, 411   MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ffc19fd) /* 0.999048222 */,
412   MAD_F(0x0ffc19fd) /* 0.999048222 */, MAD_F(0x0fdcf549) /* 0.991444861 */, 412   MAD_F(0x0ffc19fd) /* 0.999048222 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
413   MAD_F(0x0f9ee890) /* 0.976296007 */, MAD_F(0x0f426cb5) /* 0.953716951 */, 413   MAD_F(0x0f9ee890) /* 0.976296007 */, MAD_F(0x0f426cb5) /* 0.953716951 */,
414   MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0e313245) /* 0.887010833 */, 414   MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0e313245) /* 0.887010833 */,
415 415
416   MAD_F(0x0d7e8807) /* 0.843391446 */, MAD_F(0x0cb19346) /* 0.793353340 */, 416   MAD_F(0x0d7e8807) /* 0.843391446 */, MAD_F(0x0cb19346) /* 0.793353340 */,
417   MAD_F(0x0bcbe352) /* 0.737277337 */, MAD_F(0x0acf37ad) /* 0.675590208 */, 417   MAD_F(0x0bcbe352) /* 0.737277337 */, MAD_F(0x0acf37ad) /* 0.675590208 */,
418   MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0898c779) /* 0.537299608 */, 418   MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0898c779) /* 0.537299608 */,
419   MAD_F(0x07635284) /* 0.461748613 */, MAD_F(0x061f78aa) /* 0.382683432 */, 419   MAD_F(0x07635284) /* 0.461748613 */, MAD_F(0x061f78aa) /* 0.382683432 */,
420   MAD_F(0x04cfb0e2) /* 0.300705800 */, MAD_F(0x03768962) /* 0.216439614 */, 420   MAD_F(0x04cfb0e2) /* 0.300705800 */, MAD_F(0x03768962) /* 0.216439614 */,
421   MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x00b2aa3e) /* 0.043619387 */, 421   MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x00b2aa3e) /* 0.043619387 */,
422 }; 422 };
423 # endif  /* ASO_IMDCT */ 423 # endif  /* ASO_IMDCT */
424 424
425 /* 425 /*
426  * windowing coefficients for short blocks 426  * windowing coefficients for short blocks
427  * derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3 427  * derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
428  * 428  *
429  * window_s[i] = sin((PI / 12) * (i + 1/2)) 429  * window_s[i] = sin((PI / 12) * (i + 1/2))
430  */ 430  */
431 static 431 static
432 mad_fixed_t const window_s[12] = { 432 mad_fixed_t const window_s[12] = {
433   MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x061f78aa) /* 0.382683432 */, 433   MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x061f78aa) /* 0.382683432 */,
434   MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0cb19346) /* 0.793353340 */, 434   MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0cb19346) /* 0.793353340 */,
435   MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0fdcf549) /* 0.991444861 */, 435   MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
436   MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ec835e8) /* 0.923879533 */, 436   MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
437   MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x09bd7ca0) /* 0.608761429 */, 437   MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
438   MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x0216a2a2) /* 0.130526192 */, 438   MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
439 }; 439 };
440 440
441 /* 441 /*
442  * coefficients for intensity stereo processing 442  * coefficients for intensity stereo processing
443  * derived from section 2.4.3.4.9.3 of ISO/IEC 11172-3 443  * derived from section 2.4.3.4.9.3 of ISO/IEC 11172-3
444  * 444  *
445  * is_ratio[i] = tan(i * (PI / 12)) 445  * is_ratio[i] = tan(i * (PI / 12))
446  * is_table[i] = is_ratio[i] / (1 + is_ratio[i]) 446  * is_table[i] = is_ratio[i] / (1 + is_ratio[i])
447  */ 447  */
448 static 448 static
449 mad_fixed_t const is_table[7] = { 449 mad_fixed_t const is_table[7] = {
450   MAD_F(0x00000000) /* 0.000000000 */, 450   MAD_F(0x00000000) /* 0.000000000 */,
451   MAD_F(0x0361962f) /* 0.211324865 */, 451   MAD_F(0x0361962f) /* 0.211324865 */,
452   MAD_F(0x05db3d74) /* 0.366025404 */, 452   MAD_F(0x05db3d74) /* 0.366025404 */,
453   MAD_F(0x08000000) /* 0.500000000 */, 453   MAD_F(0x08000000) /* 0.500000000 */,
454   MAD_F(0x0a24c28c) /* 0.633974596 */, 454   MAD_F(0x0a24c28c) /* 0.633974596 */,
455   MAD_F(0x0c9e69d1) /* 0.788675135 */, 455   MAD_F(0x0c9e69d1) /* 0.788675135 */,
456   MAD_F(0x10000000) /* 1.000000000 */ 456   MAD_F(0x10000000) /* 1.000000000 */
457 }; 457 };
458 458
459 /* 459 /*
460  * coefficients for LSF intensity stereo processing 460  * coefficients for LSF intensity stereo processing
461  * derived from section 2.4.3.2 of ISO/IEC 13818-3 461  * derived from section 2.4.3.2 of ISO/IEC 13818-3
462  * 462  *
463  * is_lsf_table[0][i] = (1 / sqrt(sqrt(2)))^(i + 1) 463  * is_lsf_table[0][i] = (1 / sqrt(sqrt(2)))^(i + 1)
464  * is_lsf_table[1][i] = (1 /      sqrt(2)) ^(i + 1) 464  * is_lsf_table[1][i] = (1 /      sqrt(2)) ^(i + 1)
465  */ 465  */
466 static 466 static
467 mad_fixed_t const is_lsf_table[2][15] = { 467 mad_fixed_t const is_lsf_table[2][15] = {
468   { 468   {
469     MAD_F(0x0d744fcd) /* 0.840896415 */, 469     MAD_F(0x0d744fcd) /* 0.840896415 */,
470     MAD_F(0x0b504f33) /* 0.707106781 */, 470     MAD_F(0x0b504f33) /* 0.707106781 */,
471     MAD_F(0x09837f05) /* 0.594603558 */, 471     MAD_F(0x09837f05) /* 0.594603558 */,
472     MAD_F(0x08000000) /* 0.500000000 */, 472     MAD_F(0x08000000) /* 0.500000000 */,
473     MAD_F(0x06ba27e6) /* 0.420448208 */, 473     MAD_F(0x06ba27e6) /* 0.420448208 */,
474     MAD_F(0x05a8279a) /* 0.353553391 */, 474     MAD_F(0x05a8279a) /* 0.353553391 */,
475     MAD_F(0x04c1bf83) /* 0.297301779 */, 475     MAD_F(0x04c1bf83) /* 0.297301779 */,
476     MAD_F(0x04000000) /* 0.250000000 */, 476     MAD_F(0x04000000) /* 0.250000000 */,
477     MAD_F(0x035d13f3) /* 0.210224104 */, 477     MAD_F(0x035d13f3) /* 0.210224104 */,
478     MAD_F(0x02d413cd) /* 0.176776695 */, 478     MAD_F(0x02d413cd) /* 0.176776695 */,
479     MAD_F(0x0260dfc1) /* 0.148650889 */, 479     MAD_F(0x0260dfc1) /* 0.148650889 */,
480     MAD_F(0x02000000) /* 0.125000000 */, 480     MAD_F(0x02000000) /* 0.125000000 */,
481     MAD_F(0x01ae89fa) /* 0.105112052 */, 481     MAD_F(0x01ae89fa) /* 0.105112052 */,
482     MAD_F(0x016a09e6) /* 0.088388348 */, 482     MAD_F(0x016a09e6) /* 0.088388348 */,
483     MAD_F(0x01306fe1) /* 0.074325445 */ 483     MAD_F(0x01306fe1) /* 0.074325445 */
484   }, { 484   }, {
485     MAD_F(0x0b504f33) /* 0.707106781 */, 485     MAD_F(0x0b504f33) /* 0.707106781 */,
486     MAD_F(0x08000000) /* 0.500000000 */, 486     MAD_F(0x08000000) /* 0.500000000 */,
487     MAD_F(0x05a8279a) /* 0.353553391 */, 487     MAD_F(0x05a8279a) /* 0.353553391 */,
488     MAD_F(0x04000000) /* 0.250000000 */, 488     MAD_F(0x04000000) /* 0.250000000 */,
489     MAD_F(0x02d413cd) /* 0.176776695 */, 489     MAD_F(0x02d413cd) /* 0.176776695 */,
490     MAD_F(0x02000000) /* 0.125000000 */, 490     MAD_F(0x02000000) /* 0.125000000 */,
491     MAD_F(0x016a09e6) /* 0.088388348 */, 491     MAD_F(0x016a09e6) /* 0.088388348 */,
492     MAD_F(0x01000000) /* 0.062500000 */, 492     MAD_F(0x01000000) /* 0.062500000 */,
493     MAD_F(0x00b504f3) /* 0.044194174 */, 493     MAD_F(0x00b504f3) /* 0.044194174 */,
494     MAD_F(0x00800000) /* 0.031250000 */, 494     MAD_F(0x00800000) /* 0.031250000 */,
495     MAD_F(0x005a827a) /* 0.022097087 */, 495     MAD_F(0x005a827a) /* 0.022097087 */,
496     MAD_F(0x00400000) /* 0.015625000 */, 496     MAD_F(0x00400000) /* 0.015625000 */,
497     MAD_F(0x002d413d) /* 0.011048543 */, 497     MAD_F(0x002d413d) /* 0.011048543 */,
498     MAD_F(0x00200000) /* 0.007812500 */, 498     MAD_F(0x00200000) /* 0.007812500 */,
499     MAD_F(0x0016a09e) /* 0.005524272 */ 499     MAD_F(0x0016a09e) /* 0.005524272 */
500   } 500   }
501 }; 501 };
502 502
503 /* 503 /*
504  * NAME:        III_sideinfo() 504  * NAME:        III_sideinfo()
505  * DESCRIPTION: decode frame side information from a bitstream 505  * DESCRIPTION: decode frame side information from a bitstream
506  */ 506  */
507 static 507 static
508 enum mad_error III_sideinfo(struct mad_bitptr *ptr, unsigned int nch, 508 enum mad_error III_sideinfo(struct mad_bitptr *ptr, unsigned int nch,
509                             int lsf, struct sideinfo *si, 509                             int lsf, struct sideinfo *si,
510                             unsigned int *data_bitlen, 510                             unsigned int *data_bitlen,
511                             unsigned int *priv_bitlen) 511                             unsigned int *priv_bitlen)
512 { 512 {
513   unsigned int ngr, gr, ch, i; 513   unsigned int ngr, gr, ch, i;
514   enum mad_error result = MAD_ERROR_NONE; 514   enum mad_error result = MAD_ERROR_NONE;
515 515
516   *data_bitlen = 0; 516   *data_bitlen = 0;
517   *priv_bitlen = lsf ? ((nch == 1) ? 1 : 2) : ((nch == 1) ? 5 : 3); 517   *priv_bitlen = lsf ? ((nch == 1) ? 1 : 2) : ((nch == 1) ? 5 : 3);
518 518
519   si->main_data_begin = mad_bit_read(ptr, lsf ? 8 : 9); 519   si->main_data_begin = mad_bit_read(ptr, lsf ? 8 : 9);
520   si->private_bits    = mad_bit_read(ptr, *priv_bitlen); 520   si->private_bits    = mad_bit_read(ptr, *priv_bitlen);
521 521
522   ngr = 1; 522   ngr = 1;
523   if (!lsf) { 523   if (!lsf) {
524     ngr = 2; 524     ngr = 2;
525 525
526     for (ch = 0; ch < nch; ++ch) 526     for (ch = 0; ch < nch; ++ch)
527       si->scfsi[ch] = mad_bit_read(ptr, 4); 527       si->scfsi[ch] = mad_bit_read(ptr, 4);
528   } 528   }
529 529
530   for (gr = 0; gr < ngr; ++gr) { 530   for (gr = 0; gr < ngr; ++gr) {
531     struct granule *granule = &si->gr[gr]; 531     struct granule *granule = &si->gr[gr];
532 532
533     for (ch = 0; ch < nch; ++ch) { 533     for (ch = 0; ch < nch; ++ch) {
534       struct channel *channel = &granule->ch[ch]; 534       struct channel *channel = &granule->ch[ch];
535 535
536       channel->part2_3_length    = mad_bit_read(ptr, 12); 536       channel->part2_3_length    = mad_bit_read(ptr, 12);
537       channel->big_values        = mad_bit_read(ptr, 9); 537       channel->big_values        = mad_bit_read(ptr, 9);
538       channel->global_gain       = mad_bit_read(ptr, 8); 538       channel->global_gain       = mad_bit_read(ptr, 8);
539       channel->scalefac_compress = mad_bit_read(ptr, lsf ? 9 : 4); 539       channel->scalefac_compress = mad_bit_read(ptr, lsf ? 9 : 4);
540 540
541       *data_bitlen += channel->part2_3_length; 541       *data_bitlen += channel->part2_3_length;
542 542
543       if (channel->big_values > 288 && result == 0) 543       if (channel->big_values > 288 && result == 0)
544         result = MAD_ERROR_BADBIGVALUES; 544         result = MAD_ERROR_BADBIGVALUES;
545 545
546       channel->flags = 0; 546       channel->flags = 0;
547 547
548       /* window_switching_flag */ 548       /* window_switching_flag */
549       if (mad_bit_read(ptr, 1)) { 549       if (mad_bit_read(ptr, 1)) {
550         channel->block_type = mad_bit_read(ptr, 2); 550         channel->block_type = mad_bit_read(ptr, 2);
551 551
552         if (channel->block_type == 0 && result == 0) 552         if (channel->block_type == 0 && result == 0)
553           result = MAD_ERROR_BADBLOCKTYPE; 553           result = MAD_ERROR_BADBLOCKTYPE;
554 554
555         if (!lsf && channel->block_type == 2 && si->scfsi[ch] && result == 0) 555         if (!lsf && channel->block_type == 2 && si->scfsi[ch] && result == 0)
556           result = MAD_ERROR_BADSCFSI; 556           result = MAD_ERROR_BADSCFSI;
557 557
558         channel->region0_count = 7; 558         channel->region0_count = 7;
559         channel->region1_count = 36; 559         channel->region1_count = 36;
560 560
561         if (mad_bit_read(ptr, 1)) 561         if (mad_bit_read(ptr, 1))
562           channel->flags |= mixed_block_flag; 562           channel->flags |= mixed_block_flag;
563         else if (channel->block_type == 2) 563         else if (channel->block_type == 2)
564           channel->region0_count = 8; 564           channel->region0_count = 8;
565 565
566         for (i = 0; i < 2; ++i) 566         for (i = 0; i < 2; ++i)
567           channel->table_select[i] = mad_bit_read(ptr, 5); 567           channel->table_select[i] = mad_bit_read(ptr, 5);
568 568
569 # if 0 && defined(DEBUG) -  
-   569 # if defined(DEBUG)
570         channel->table_select[2] = 4;  /* not used */ 570         channel->table_select[2] = 4;  /* not used */
571 # endif 571 # endif
572 572
573         for (i = 0; i < 3; ++i) 573         for (i = 0; i < 3; ++i)
574           channel->subblock_gain[i] = mad_bit_read(ptr, 3); 574           channel->subblock_gain[i] = mad_bit_read(ptr, 3);
575       } 575       }
576       else { 576       else {
577         channel->block_type = 0; 577         channel->block_type = 0;
578 578
579         for (i = 0; i < 3; ++i) 579         for (i = 0; i < 3; ++i)
580           channel->table_select[i] = mad_bit_read(ptr, 5); 580           channel->table_select[i] = mad_bit_read(ptr, 5);
581 581
582         channel->region0_count = mad_bit_read(ptr, 4); 582         channel->region0_count = mad_bit_read(ptr, 4);
583         channel->region1_count = mad_bit_read(ptr, 3); 583         channel->region1_count = mad_bit_read(ptr, 3);
584       } 584       }
585 585
586       /* [preflag,] scalefac_scale, count1table_select */ 586       /* [preflag,] scalefac_scale, count1table_select */
587       channel->flags |= mad_bit_read(ptr, lsf ? 2 : 3); 587       channel->flags |= mad_bit_read(ptr, lsf ? 2 : 3);
588     } 588     }
589   } 589   }
590 590
591   return result; 591   return result;
592 } 592 }
593 593
594 /* 594 /*
595  * NAME:        III_scalefactors_lsf() 595  * NAME:        III_scalefactors_lsf()
596  * DESCRIPTION: decode channel scalefactors for LSF from a bitstream 596  * DESCRIPTION: decode channel scalefactors for LSF from a bitstream
597  */ 597  */
598 static 598 static
599 unsigned int III_scalefactors_lsf(struct mad_bitptr *ptr, 599 unsigned int III_scalefactors_lsf(struct mad_bitptr *ptr,
600                                   struct channel *channel, 600                                   struct channel *channel,
601                                   struct channel *gr1ch, int mode_extension) 601                                   struct channel *gr1ch, int mode_extension)
602 { 602 {
603   struct mad_bitptr start; 603   struct mad_bitptr start;
604   unsigned int scalefac_compress, index, slen[4], part, n, i; 604   unsigned int scalefac_compress, index, slen[4], part, n, i;
605   unsigned char const *nsfb; 605   unsigned char const *nsfb;
606 606
607   start = *ptr; 607   start = *ptr;
608 608
609   scalefac_compress = channel->scalefac_compress; 609   scalefac_compress = channel->scalefac_compress;
610   index = (channel->block_type == 2) ? 610   index = (channel->block_type == 2) ?
611     ((channel->flags & mixed_block_flag) ? 2 : 1) : 0; 611     ((channel->flags & mixed_block_flag) ? 2 : 1) : 0;
612 612
613   if (!((mode_extension & I_STEREO) && gr1ch)) { 613   if (!((mode_extension & I_STEREO) && gr1ch)) {
614     if (scalefac_compress < 400) { 614     if (scalefac_compress < 400) {
615       slen[0] = (scalefac_compress >> 4) / 5; 615       slen[0] = (scalefac_compress >> 4) / 5;
616       slen[1] = (scalefac_compress >> 4) % 5; 616       slen[1] = (scalefac_compress >> 4) % 5;
617       slen[2] = (scalefac_compress % 16) >> 2; 617       slen[2] = (scalefac_compress % 16) >> 2;
618       slen[3] =  scalefac_compress %  4; 618       slen[3] =  scalefac_compress %  4;
619 619
620       nsfb = nsfb_table[0][index]; 620       nsfb = nsfb_table[0][index];
621     } 621     }
622     else if (scalefac_compress < 500) { 622     else if (scalefac_compress < 500) {
623       scalefac_compress -= 400; 623       scalefac_compress -= 400;
624 624
625       slen[0] = (scalefac_compress >> 2) / 5; 625       slen[0] = (scalefac_compress >> 2) / 5;
626       slen[1] = (scalefac_compress >> 2) % 5; 626       slen[1] = (scalefac_compress >> 2) % 5;
627       slen[2] =  scalefac_compress %  4; 627       slen[2] =  scalefac_compress %  4;
628       slen[3] = 0; 628       slen[3] = 0;
629 629
630       nsfb = nsfb_table[1][index]; 630       nsfb = nsfb_table[1][index];
631     } 631     }
632     else { 632     else {
633       scalefac_compress -= 500; 633       scalefac_compress -= 500;
634 634
635       slen[0] = scalefac_compress / 3; 635       slen[0] = scalefac_compress / 3;
636       slen[1] = scalefac_compress % 3; 636       slen[1] = scalefac_compress % 3;
637       slen[2] = 0; 637       slen[2] = 0;
638       slen[3] = 0; 638       slen[3] = 0;
639 639
640       channel->flags |= preflag; 640       channel->flags |= preflag;
641 641
642       nsfb = nsfb_table[2][index]; 642       nsfb = nsfb_table[2][index];
643     } 643     }
644 644
645     n = 0; 645     n = 0;
646     for (part = 0; part < 4; ++part) { 646     for (part = 0; part < 4; ++part) {
647       for (i = 0; i < nsfb[part]; ++i) 647       for (i = 0; i < nsfb[part]; ++i)
648         channel->scalefac[n++] = mad_bit_read(ptr, slen[part]); 648         channel->scalefac[n++] = mad_bit_read(ptr, slen[part]);
649     } 649     }
650 650
651     while (n < 39) 651     while (n < 39)
652       channel->scalefac[n++] = 0; 652       channel->scalefac[n++] = 0;
653   } 653   }
654   else {  /* (mode_extension & I_STEREO) && gr1ch (i.e. ch == 1) */ 654   else {  /* (mode_extension & I_STEREO) && gr1ch (i.e. ch == 1) */
655     scalefac_compress >>= 1; 655     scalefac_compress >>= 1;
656 656
657     if (scalefac_compress < 180) { 657     if (scalefac_compress < 180) {
658       slen[0] =  scalefac_compress / 36; 658       slen[0] =  scalefac_compress / 36;
659       slen[1] = (scalefac_compress % 36) / 6; 659       slen[1] = (scalefac_compress % 36) / 6;
660       slen[2] = (scalefac_compress % 36) % 6; 660       slen[2] = (scalefac_compress % 36) % 6;
661       slen[3] = 0; 661       slen[3] = 0;
662 662
663       nsfb = nsfb_table[3][index]; 663       nsfb = nsfb_table[3][index];
664     } 664     }
665     else if (scalefac_compress < 244) { 665     else if (scalefac_compress < 244) {
666       scalefac_compress -= 180; 666       scalefac_compress -= 180;
667 667
668       slen[0] = (scalefac_compress % 64) >> 4; 668       slen[0] = (scalefac_compress % 64) >> 4;
669       slen[1] = (scalefac_compress % 16) >> 2; 669       slen[1] = (scalefac_compress % 16) >> 2;
670       slen[2] =  scalefac_compress %  4; 670       slen[2] =  scalefac_compress %  4;
671       slen[3] = 0; 671       slen[3] = 0;
672 672
673       nsfb = nsfb_table[4][index]; 673       nsfb = nsfb_table[4][index];
674     } 674     }
675     else { 675     else {
676       scalefac_compress -= 244; 676       scalefac_compress -= 244;
677 677
678       slen[0] = scalefac_compress / 3; 678       slen[0] = scalefac_compress / 3;
679       slen[1] = scalefac_compress % 3; 679       slen[1] = scalefac_compress % 3;
680       slen[2] = 0; 680       slen[2] = 0;
681       slen[3] = 0; 681       slen[3] = 0;
682 682
683       nsfb = nsfb_table[5][index]; 683       nsfb = nsfb_table[5][index];
684     } 684     }
685 685
686     n = 0; 686     n = 0;
687     for (part = 0; part < 4; ++part) { 687     for (part = 0; part < 4; ++part) {
688       unsigned int max, is_pos; 688       unsigned int max, is_pos;
689 689
690       max = (1 << slen[part]) - 1; 690       max = (1 << slen[part]) - 1;
691 691
692       for (i = 0; i < nsfb[part]; ++i) { 692       for (i = 0; i < nsfb[part]; ++i) {
693         is_pos = mad_bit_read(ptr, slen[part]); 693         is_pos = mad_bit_read(ptr, slen[part]);
694 694
695         channel->scalefac[n] = is_pos; 695         channel->scalefac[n] = is_pos;
696         gr1ch->scalefac[n++] = (is_pos == max); 696         gr1ch->scalefac[n++] = (is_pos == max);
697       } 697       }
698     } 698     }
699 699
700     while (n < 39) { 700     while (n < 39) {
701       channel->scalefac[n] = 0; 701       channel->scalefac[n] = 0;
702       gr1ch->scalefac[n++] = 0;  /* apparently not illegal */ 702       gr1ch->scalefac[n++] = 0;  /* apparently not illegal */
703     } 703     }
704   } 704   }
705 705
706   return mad_bit_length(&start, ptr); 706   return mad_bit_length(&start, ptr);
707 } 707 }
708 708
709 /* 709 /*
710  * NAME:        III_scalefactors() 710  * NAME:        III_scalefactors()
711  * DESCRIPTION: decode channel scalefactors of one granule from a bitstream 711  * DESCRIPTION: decode channel scalefactors of one granule from a bitstream
712  */ 712  */
713 static 713 static
714 unsigned int III_scalefactors(struct mad_bitptr *ptr, struct channel *channel, 714 unsigned int III_scalefactors(struct mad_bitptr *ptr, struct channel *channel,
715                               struct channel const *gr0ch, unsigned int scfsi) 715                               struct channel const *gr0ch, unsigned int scfsi)
716 { 716 {
717   struct mad_bitptr start; 717   struct mad_bitptr start;
718   unsigned int slen1, slen2, sfbi; 718   unsigned int slen1, slen2, sfbi;
719 719
720   start = *ptr; 720   start = *ptr;
721 721
722   slen1 = sflen_table[channel->scalefac_compress].slen1; 722   slen1 = sflen_table[channel->scalefac_compress].slen1;
723   slen2 = sflen_table[channel->scalefac_compress].slen2; 723   slen2 = sflen_table[channel->scalefac_compress].slen2;
724 724
725   if (channel->block_type == 2) { 725   if (channel->block_type == 2) {
726     unsigned int nsfb; 726     unsigned int nsfb;
727 727
728     sfbi = 0; 728     sfbi = 0;
729 729
730     nsfb = (channel->flags & mixed_block_flag) ? 8 + 3 * 3 : 6 * 3; 730     nsfb = (channel->flags & mixed_block_flag) ? 8 + 3 * 3 : 6 * 3;
731     while (nsfb--) 731     while (nsfb--)
732       channel->scalefac[sfbi++] = mad_bit_read(ptr, slen1); 732       channel->scalefac[sfbi++] = mad_bit_read(ptr, slen1);
733 733
734     nsfb = 6 * 3; 734     nsfb = 6 * 3;
735     while (nsfb--) 735     while (nsfb--)
736       channel->scalefac[sfbi++] = mad_bit_read(ptr, slen2); 736       channel->scalefac[sfbi++] = mad_bit_read(ptr, slen2);
737 737
738     nsfb = 1 * 3; 738     nsfb = 1 * 3;
739     while (nsfb--) 739     while (nsfb--)
740       channel->scalefac[sfbi++] = 0; 740       channel->scalefac[sfbi++] = 0;
741   } 741   }
742   else {  /* channel->block_type != 2 */ 742   else {  /* channel->block_type != 2 */
743     if (scfsi & 0x8) { 743     if (scfsi & 0x8) {
744       for (sfbi = 0; sfbi < 6; ++sfbi) 744       for (sfbi = 0; sfbi < 6; ++sfbi)
745         channel->scalefac[sfbi] = gr0ch->scalefac[sfbi]; 745         channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
746     } 746     }
747     else { 747     else {
748       for (sfbi = 0; sfbi < 6; ++sfbi) 748       for (sfbi = 0; sfbi < 6; ++sfbi)
749         channel->scalefac[sfbi] = mad_bit_read(ptr, slen1); 749         channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
750     } 750     }
751 751
752     if (scfsi & 0x4) { 752     if (scfsi & 0x4) {
753       for (sfbi = 6; sfbi < 11; ++sfbi) 753       for (sfbi = 6; sfbi < 11; ++sfbi)
754         channel->scalefac[sfbi] = gr0ch->scalefac[sfbi]; 754         channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
755     } 755     }
756     else { 756     else {
757       for (sfbi = 6; sfbi < 11; ++sfbi) 757       for (sfbi = 6; sfbi < 11; ++sfbi)
758         channel->scalefac[sfbi] = mad_bit_read(ptr, slen1); 758         channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
759     } 759     }
760 760
761     if (scfsi & 0x2) { 761     if (scfsi & 0x2) {
762       for (sfbi = 11; sfbi < 16; ++sfbi) 762       for (sfbi = 11; sfbi < 16; ++sfbi)
763         channel->scalefac[sfbi] = gr0ch->scalefac[sfbi]; 763         channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
764     } 764     }
765     else { 765     else {
766       for (sfbi = 11; sfbi < 16; ++sfbi) 766       for (sfbi = 11; sfbi < 16; ++sfbi)
767         channel->scalefac[sfbi] = mad_bit_read(ptr, slen2); 767         channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
768     } 768     }
769 769
770     if (scfsi & 0x1) { 770     if (scfsi & 0x1) {
771       for (sfbi = 16; sfbi < 21; ++sfbi) 771       for (sfbi = 16; sfbi < 21; ++sfbi)
772         channel->scalefac[sfbi] = gr0ch->scalefac[sfbi]; 772         channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
773     } 773     }
774     else { 774     else {
775       for (sfbi = 16; sfbi < 21; ++sfbi) 775       for (sfbi = 16; sfbi < 21; ++sfbi)
776         channel->scalefac[sfbi] = mad_bit_read(ptr, slen2); 776         channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
777     } 777     }
778 778
779     channel->scalefac[21] = 0; 779     channel->scalefac[21] = 0;
780   } 780   }
781 781
782   return mad_bit_length(&start, ptr); 782   return mad_bit_length(&start, ptr);
783 } 783 }
784 784
785 /* 785 /*
786  * The Layer III formula for requantization and scaling is defined by 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: 787  * section 2.4.3.4.7.1 of ISO/IEC 11172-3, as follows:
788  * 788  *
789  *   long blocks: 789  *   long blocks:
790  *   xr[i] = sign(is[i]) * abs(is[i])^(4/3) * 790  *   xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
791  *           2^((1/4) * (global_gain - 210)) * 791  *           2^((1/4) * (global_gain - 210)) *
792  *           2^-(scalefac_multiplier * 792  *           2^-(scalefac_multiplier *
793  *               (scalefac_l[sfb] + preflag * pretab[sfb])) 793  *               (scalefac_l[sfb] + preflag * pretab[sfb]))
794  * 794  *
795  *   short blocks: 795  *   short blocks:
796  *   xr[i] = sign(is[i]) * abs(is[i])^(4/3) * 796  *   xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
797  *           2^((1/4) * (global_gain - 210 - 8 * subblock_gain[w])) * 797  *           2^((1/4) * (global_gain - 210 - 8 * subblock_gain[w])) *
798  *           2^-(scalefac_multiplier * scalefac_s[sfb][w]) 798  *           2^-(scalefac_multiplier * scalefac_s[sfb][w])
799  * 799  *
800  *   where: 800  *   where:
801  *   scalefac_multiplier = (scalefac_scale + 1) / 2 801  *   scalefac_multiplier = (scalefac_scale + 1) / 2
802  * 802  *
803  * The routines III_exponents() and III_requantize() facilitate this 803  * The routines III_exponents() and III_requantize() facilitate this
804  * calculation. 804  * calculation.
805  */ 805  */
806 806
807 /* 807 /*
808  * NAME:        III_exponents() 808  * NAME:        III_exponents()
809  * DESCRIPTION: calculate scalefactor exponents 809  * DESCRIPTION: calculate scalefactor exponents
810  */ 810  */
811 static 811 static
812 void III_exponents(struct channel const *channel, 812 void III_exponents(struct channel const *channel,
813                    unsigned char const *sfbwidth, signed int exponents[39]) 813                    unsigned char const *sfbwidth, signed int exponents[39])
814 { 814 {
815   signed int gain; 815   signed int gain;
816   unsigned int scalefac_multiplier, sfbi; 816   unsigned int scalefac_multiplier, sfbi;
817 817
818   gain = (signed int) channel->global_gain - 210; 818   gain = (signed int) channel->global_gain - 210;
819   scalefac_multiplier = (channel->flags & scalefac_scale) ? 2 : 1; 819   scalefac_multiplier = (channel->flags & scalefac_scale) ? 2 : 1;
820 820
821   if (channel->block_type == 2) { 821   if (channel->block_type == 2) {
822     unsigned int l; 822     unsigned int l;
823     signed int gain0, gain1, gain2; 823     signed int gain0, gain1, gain2;
824 824
825     sfbi = l = 0; 825     sfbi = l = 0;
826 826
827     if (channel->flags & mixed_block_flag) { 827     if (channel->flags & mixed_block_flag) {
828       unsigned int premask; 828       unsigned int premask;
829 829
830       premask = (channel->flags & preflag) ? ~0 : 0; 830       premask = (channel->flags & preflag) ? ~0 : 0;
831 831
832       /* long block subbands 0-1 */ 832       /* long block subbands 0-1 */
833 833
834       while (l < 36) { 834       while (l < 36) {
835         exponents[sfbi] = gain - 835         exponents[sfbi] = gain -
836           (signed int) ((channel->scalefac[sfbi] + (pretab[sfbi] & premask)) << 836           (signed int) ((channel->scalefac[sfbi] + (pretab[sfbi] & premask)) <<
837                         scalefac_multiplier); 837                         scalefac_multiplier);
838 838
839         l += sfbwidth[sfbi++]; 839         l += sfbwidth[sfbi++];
840       } 840       }
841     } 841     }
842 842
843     /* this is probably wrong for 8000 Hz short/mixed blocks */ 843     /* this is probably wrong for 8000 Hz short/mixed blocks */
844 844
845     gain0 = gain - 8 * (signed int) channel->subblock_gain[0]; 845     gain0 = gain - 8 * (signed int) channel->subblock_gain[0];
846     gain1 = gain - 8 * (signed int) channel->subblock_gain[1]; 846     gain1 = gain - 8 * (signed int) channel->subblock_gain[1];
847     gain2 = gain - 8 * (signed int) channel->subblock_gain[2]; 847     gain2 = gain - 8 * (signed int) channel->subblock_gain[2];
848 848
849     while (l < 576) { 849     while (l < 576) {
850       exponents[sfbi + 0] = gain0 - 850       exponents[sfbi + 0] = gain0 -
851         (signed int) (channel->scalefac[sfbi + 0] << scalefac_multiplier); 851         (signed int) (channel->scalefac[sfbi + 0] << scalefac_multiplier);
852       exponents[sfbi + 1] = gain1 - 852       exponents[sfbi + 1] = gain1 -
853         (signed int) (channel->scalefac[sfbi + 1] << scalefac_multiplier); 853         (signed int) (channel->scalefac[sfbi + 1] << scalefac_multiplier);
854       exponents[sfbi + 2] = gain2 - 854       exponents[sfbi + 2] = gain2 -
855         (signed int) (channel->scalefac[sfbi + 2] << scalefac_multiplier); 855         (signed int) (channel->scalefac[sfbi + 2] << scalefac_multiplier);
856 856
857       l    += 3 * sfbwidth[sfbi]; 857       l    += 3 * sfbwidth[sfbi];
858       sfbi += 3; 858       sfbi += 3;
859     } 859     }
860   } 860   }
861   else {  /* channel->block_type != 2 */ 861   else {  /* channel->block_type != 2 */
862     if (channel->flags & preflag) { 862     if (channel->flags & preflag) {
863       for (sfbi = 0; sfbi < 22; ++sfbi) { 863       for (sfbi = 0; sfbi < 22; ++sfbi) {
864         exponents[sfbi] = gain - 864         exponents[sfbi] = gain -
865           (signed int) ((channel->scalefac[sfbi] + pretab[sfbi]) << 865           (signed int) ((channel->scalefac[sfbi] + pretab[sfbi]) <<
866                         scalefac_multiplier); 866                         scalefac_multiplier);
867       } 867       }
868     } 868     }
869     else { 869     else {
870       for (sfbi = 0; sfbi < 22; ++sfbi) { 870       for (sfbi = 0; sfbi < 22; ++sfbi) {
871         exponents[sfbi] = gain - 871         exponents[sfbi] = gain -
872           (signed int) (channel->scalefac[sfbi] << scalefac_multiplier); 872           (signed int) (channel->scalefac[sfbi] << scalefac_multiplier);
873       } 873       }
874     } 874     }
875   } 875   }
876 } 876 }
877 877
878 /* 878 /*
879  * NAME:        III_requantize() 879  * NAME:        III_requantize()
880  * DESCRIPTION: requantize one (positive) value 880  * DESCRIPTION: requantize one (positive) value
881  */ 881  */
882 static 882 static
883 mad_fixed_t III_requantize(unsigned int value, signed int exp) 883 mad_fixed_t III_requantize(unsigned int value, signed int exp)
884 { 884 {
885   mad_fixed_t requantized; 885   mad_fixed_t requantized;
886   signed int frac; 886   signed int frac;
887   struct fixedfloat const *power; 887   struct fixedfloat const *power;
888 888
889   frac = exp % 4;  /* assumes sign(frac) == sign(exp) */ 889   frac = exp % 4;  /* assumes sign(frac) == sign(exp) */
890   exp /= 4; 890   exp /= 4;
891 891
892   power = &rq_table[value]; 892   power = &rq_table[value];
893   requantized = power->mantissa; 893   requantized = power->mantissa;
894   exp += power->exponent; 894   exp += power->exponent;
895 895
896   if (exp < 0) { 896   if (exp < 0) {
897     if (-exp >= sizeof(mad_fixed_t) * CHAR_BIT) { 897     if (-exp >= sizeof(mad_fixed_t) * CHAR_BIT) {
898       /* underflow */ 898       /* underflow */
899       requantized = 0; 899       requantized = 0;
900     } 900     }
901     else { 901     else {
902       requantized += 1L << (-exp - 1); 902       requantized += 1L << (-exp - 1);
903       requantized >>= -exp; 903       requantized >>= -exp;
904     } 904     }
905   } 905   }
906   else { 906   else {
907     if (exp >= 5) { 907     if (exp >= 5) {
908       /* overflow */ 908       /* overflow */
909 # if 0 && defined(DEBUG) -  
-   909 # if defined(DEBUG)
910       fprintf(stderr, "requantize overflow (%f * 2^%d)\n", 910       fprintf(stderr, "requantize overflow (%f * 2^%d)\n",
911               mad_f_todouble(requantized), exp); 911               mad_f_todouble(requantized), exp);
912 # endif 912 # endif
913       requantized = MAD_F_MAX; 913       requantized = MAD_F_MAX;
914     } 914     }
915     else 915     else
916       requantized <<= exp; 916       requantized <<= exp;
917   } 917   }
918 918
919   return frac ? mad_f_mul(requantized, root_table[3 + frac]) : requantized; 919   return frac ? mad_f_mul(requantized, root_table[3 + frac]) : requantized;
920 } 920 }
921 921
922 /* 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 */
923 # define MASK(cache, sz, bits)  \ 923 # define MASK(cache, sz, bits)  \
924     (((cache) >> ((sz) - (bits))) & ((1 << (bits)) - 1)) 924     (((cache) >> ((sz) - (bits))) & ((1 << (bits)) - 1))
925 # define MASK1BIT(cache, sz)  \ 925 # define MASK1BIT(cache, sz)  \
926     ((cache) & (1 << ((sz) - 1))) 926     ((cache) & (1 << ((sz) - 1)))
927 927
928 /* 928 /*
929  * NAME:        III_huffdecode() 929  * NAME:        III_huffdecode()
930  * DESCRIPTION: decode Huffman code words of one channel of one granule 930  * DESCRIPTION: decode Huffman code words of one channel of one granule
931  */ 931  */
932 static 932 static
933 enum mad_error III_huffdecode(struct mad_bitptr *ptr, mad_fixed_t xr[576], 933 enum mad_error III_huffdecode(struct mad_bitptr *ptr, mad_fixed_t xr[576],
934                               struct channel *channel, 934                               struct channel *channel,
935                               unsigned char const *sfbwidth, 935                               unsigned char const *sfbwidth,
936                               unsigned int part2_length) 936                               unsigned int part2_length)
937 { 937 {
938   signed int exponents[39], exp; 938   signed int exponents[39], exp;
939   signed int const *expptr; 939   signed int const *expptr;
940   struct mad_bitptr peek; 940   struct mad_bitptr peek;
941   signed int bits_left, cachesz; 941   signed int bits_left, cachesz;
942   register mad_fixed_t *xrptr; 942   register mad_fixed_t *xrptr;
943   mad_fixed_t const *sfbound; 943   mad_fixed_t const *sfbound;
944   register unsigned long bitcache; 944   register unsigned long bitcache;
945 945
946   bits_left = (signed) channel->part2_3_length - (signed) part2_length; 946   bits_left = (signed) channel->part2_3_length - (signed) part2_length;
947   if (bits_left < 0) 947   if (bits_left < 0)
948     return MAD_ERROR_BADPART3LEN; 948     return MAD_ERROR_BADPART3LEN;
949 949
950   III_exponents(channel, sfbwidth, exponents); 950   III_exponents(channel, sfbwidth, exponents);
951 951
952   peek = *ptr; 952   peek = *ptr;
953   mad_bit_skip(ptr, bits_left); 953   mad_bit_skip(ptr, bits_left);
954 954
955   /* align bit reads to byte boundaries */ 955   /* align bit reads to byte boundaries */
956   cachesz  = mad_bit_bitsleft(&peek); 956   cachesz  = mad_bit_bitsleft(&peek);
957   cachesz += ((32 - 1 - 24) + (24 - cachesz)) & ~7; 957   cachesz += ((32 - 1 - 24) + (24 - cachesz)) & ~7;
958 958
959   bitcache   = mad_bit_read(&peek, cachesz); 959   bitcache   = mad_bit_read(&peek, cachesz);
960   bits_left -= cachesz; 960   bits_left -= cachesz;
961 961
962   xrptr = &xr[0]; 962   xrptr = &xr[0];
963 963
964   /* big_values */ 964   /* big_values */
965   { 965   {
966     unsigned int region, rcount; 966     unsigned int region, rcount;
967     struct hufftable const *entry; 967     struct hufftable const *entry;
968     union huffpair const *table; 968     union huffpair const *table;
969     unsigned int linbits, startbits, big_values, reqhits; 969     unsigned int linbits, startbits, big_values, reqhits;
970     mad_fixed_t reqcache[16]; 970     mad_fixed_t reqcache[16];
971 971
972     sfbound = xrptr + *sfbwidth++; 972     sfbound = xrptr + *sfbwidth++;
973     rcount  = channel->region0_count + 1; 973     rcount  = channel->region0_count + 1;
974 974
975     entry     = &mad_huff_pair_table[channel->table_select[region = 0]]; 975     entry     = &mad_huff_pair_table[channel->table_select[region = 0]];
976     table     = entry->table; 976     table     = entry->table;
977     linbits   = entry->linbits; 977     linbits   = entry->linbits;
978     startbits = entry->startbits; 978     startbits = entry->startbits;
979 979
980     if (table == 0) 980     if (table == 0)
981       return MAD_ERROR_BADHUFFTABLE; 981       return MAD_ERROR_BADHUFFTABLE;
982 982
983     expptr  = &exponents[0]; 983     expptr  = &exponents[0];
984     exp     = *expptr++; 984     exp     = *expptr++;
985     reqhits = 0; 985     reqhits = 0;
986 986
987     big_values = channel->big_values; 987     big_values = channel->big_values;
988 988
989     while (big_values-- && cachesz + bits_left > 0) { 989     while (big_values-- && cachesz + bits_left > 0) {
990       union huffpair const *pair; 990       union huffpair const *pair;
991       unsigned int clumpsz, value; 991       unsigned int clumpsz, value;
992       register mad_fixed_t requantized; 992       register mad_fixed_t requantized;
993 993
994       if (xrptr == sfbound) { 994       if (xrptr == sfbound) {
995         sfbound += *sfbwidth++; 995         sfbound += *sfbwidth++;
996 996
997         /* change table if region boundary */ 997         /* change table if region boundary */
998 998
999         if (--rcount == 0) { 999         if (--rcount == 0) {
1000           if (region == 0) 1000           if (region == 0)
1001             rcount = channel->region1_count + 1; 1001             rcount = channel->region1_count + 1;
1002           else 1002           else
1003             rcount = 0;  /* all remaining */ 1003             rcount = 0;  /* all remaining */
1004 1004
1005           entry     = &mad_huff_pair_table[channel->table_select[++region]]; 1005           entry     = &mad_huff_pair_table[channel->table_select[++region]];
1006           table     = entry->table; 1006           table     = entry->table;
1007           linbits   = entry->linbits; 1007           linbits   = entry->linbits;
1008           startbits = entry->startbits; 1008           startbits = entry->startbits;
1009 1009
1010           if (table == 0) 1010           if (table == 0)
1011             return MAD_ERROR_BADHUFFTABLE; 1011             return MAD_ERROR_BADHUFFTABLE;
1012         } 1012         }
1013 1013
1014         if (exp != *expptr) { 1014         if (exp != *expptr) {
1015           exp = *expptr; 1015           exp = *expptr;
1016           reqhits = 0; 1016           reqhits = 0;
1017         } 1017         }
1018 1018
1019         ++expptr; 1019         ++expptr;
1020       } 1020       }
1021 1021
1022       if (cachesz < 21) { 1022       if (cachesz < 21) {
1023         unsigned int bits; 1023         unsigned int bits;
1024 1024
1025         bits       = ((32 - 1 - 21) + (21 - cachesz)) & ~7; 1025         bits       = ((32 - 1 - 21) + (21 - cachesz)) & ~7;
1026         bitcache   = (bitcache << bits) | mad_bit_read(&peek, bits); 1026         bitcache   = (bitcache << bits) | mad_bit_read(&peek, bits);
1027         cachesz   += bits; 1027         cachesz   += bits;
1028         bits_left -= bits; 1028         bits_left -= bits;
1029       } 1029       }
1030 1030
1031       /* hcod (0..19) */ 1031       /* hcod (0..19) */
1032 1032
1033       clumpsz = startbits; 1033       clumpsz = startbits;
1034       pair    = &table[MASK(bitcache, cachesz, clumpsz)]; 1034       pair    = &table[MASK(bitcache, cachesz, clumpsz)];
1035 1035
1036       while (!pair->final) { 1036       while (!pair->final) {
1037         cachesz -= clumpsz; 1037         cachesz -= clumpsz;
1038 1038
1039         clumpsz = pair->ptr.bits; 1039         clumpsz = pair->ptr.bits;
1040         pair    = &table[pair->ptr.offset + MASK(bitcache, cachesz, clumpsz)]; 1040         pair    = &table[pair->ptr.offset + MASK(bitcache, cachesz, clumpsz)];
1041       } 1041       }
1042 1042
1043       cachesz -= pair->value.hlen; 1043       cachesz -= pair->value.hlen;
1044 1044
1045       if (linbits) { 1045       if (linbits) {
1046         /* x (0..14) */ 1046         /* x (0..14) */
1047 1047
1048         value = pair->value.x; 1048         value = pair->value.x;
1049 1049
1050         switch (value) { 1050         switch (value) {
1051         case 0: 1051         case 0:
1052           xrptr[0] = 0; 1052           xrptr[0] = 0;
1053           break; 1053           break;
1054 1054
1055         case 15: 1055         case 15:
1056           if (cachesz < linbits + 2) { 1056           if (cachesz < linbits + 2) {
1057             bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16); 1057             bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16);
1058             cachesz   += 16; 1058             cachesz   += 16;
1059             bits_left -= 16; 1059             bits_left -= 16;
1060           } 1060           }
1061 1061
1062           value += MASK(bitcache, cachesz, linbits); 1062           value += MASK(bitcache, cachesz, linbits);
1063           cachesz -= linbits; 1063           cachesz -= linbits;
1064 1064
1065           requantized = III_requantize(value, exp); 1065           requantized = III_requantize(value, exp);
1066           goto x_final; 1066           goto x_final;
1067 1067
1068         default: 1068         default:
1069           if (reqhits & (1 << value)) 1069           if (reqhits & (1 << value))
1070             requantized = reqcache[value]; 1070             requantized = reqcache[value];
1071           else { 1071           else {
1072             reqhits |= (1 << value); 1072             reqhits |= (1 << value);
1073             requantized = reqcache[value] = III_requantize(value, exp); 1073             requantized = reqcache[value] = III_requantize(value, exp);
1074           } 1074           }
1075 1075
1076         x_final: 1076         x_final:
1077           xrptr[0] = MASK1BIT(bitcache, cachesz--) ? 1077           xrptr[0] = MASK1BIT(bitcache, cachesz--) ?
1078             -requantized : requantized; 1078             -requantized : requantized;
1079         } 1079         }
1080 1080
1081         /* y (0..14) */ 1081         /* y (0..14) */
1082 1082
1083         value = pair->value.y; 1083         value = pair->value.y;
1084 1084
1085         switch (value) { 1085         switch (value) {
1086         case 0: 1086         case 0:
1087           xrptr[1] = 0; 1087           xrptr[1] = 0;
1088           break; 1088           break;
1089 1089
1090         case 15: 1090         case 15:
1091           if (cachesz < linbits + 1) { 1091           if (cachesz < linbits + 1) {
1092             bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16); 1092             bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16);
1093             cachesz   += 16; 1093             cachesz   += 16;
1094             bits_left -= 16; 1094             bits_left -= 16;
1095           } 1095           }
1096 1096
1097           value += MASK(bitcache, cachesz, linbits); 1097           value += MASK(bitcache, cachesz, linbits);
1098           cachesz -= linbits; 1098           cachesz -= linbits;
1099 1099
1100           requantized = III_requantize(value, exp); 1100           requantized = III_requantize(value, exp);
1101           goto y_final; 1101           goto y_final;
1102 1102
1103         default: 1103         default:
1104           if (reqhits & (1 << value)) 1104           if (reqhits & (1 << value))
1105             requantized = reqcache[value]; 1105             requantized = reqcache[value];
1106           else { 1106           else {
1107             reqhits |= (1 << value); 1107             reqhits |= (1 << value);
1108             requantized = reqcache[value] = III_requantize(value, exp); 1108             requantized = reqcache[value] = III_requantize(value, exp);
1109           } 1109           }
1110 1110
1111         y_final: 1111         y_final:
1112           xrptr[1] = MASK1BIT(bitcache, cachesz--) ? 1112           xrptr[1] = MASK1BIT(bitcache, cachesz--) ?
1113             -requantized : requantized; 1113             -requantized : requantized;
1114         } 1114         }
1115       } 1115       }
1116       else { 1116       else {
1117         /* x (0..1) */ 1117         /* x (0..1) */
1118 1118
1119         value = pair->value.x; 1119         value = pair->value.x;
1120 1120
1121         if (value == 0) 1121         if (value == 0)
1122           xrptr[0] = 0; 1122           xrptr[0] = 0;
1123         else { 1123         else {
1124           if (reqhits & (1 << value)) 1124           if (reqhits & (1 << value))
1125             requantized = reqcache[value]; 1125             requantized = reqcache[value];
1126           else { 1126           else {
1127             reqhits |= (1 << value); 1127             reqhits |= (1 << value);
1128             requantized = reqcache[value] = III_requantize(value, exp); 1128             requantized = reqcache[value] = III_requantize(value, exp);
1129           } 1129           }
1130 1130
1131           xrptr[0] = MASK1BIT(bitcache, cachesz--) ? 1131           xrptr[0] = MASK1BIT(bitcache, cachesz--) ?
1132             -requantized : requantized; 1132             -requantized : requantized;
1133         } 1133         }
1134 1134
1135         /* y (0..1) */ 1135         /* y (0..1) */
1136 1136
1137         value = pair->value.y; 1137         value = pair->value.y;
1138 1138
1139         if (value == 0) 1139         if (value == 0)
1140           xrptr[1] = 0; 1140           xrptr[1] = 0;
1141         else { 1141         else {
1142           if (reqhits & (1 << value)) 1142           if (reqhits & (1 << value))
1143             requantized = reqcache[value]; 1143             requantized = reqcache[value];
1144           else { 1144           else {
1145             reqhits |= (1 << value); 1145             reqhits |= (1 << value);
1146             requantized = reqcache[value] = III_requantize(value, exp); 1146             requantized = reqcache[value] = III_requantize(value, exp);
1147           } 1147           }
1148 1148
1149           xrptr[1] = MASK1BIT(bitcache, cachesz--) ? 1149           xrptr[1] = MASK1BIT(bitcache, cachesz--) ?
1150             -requantized : requantized; 1150             -requantized : requantized;
1151         } 1151         }
1152       } 1152       }
1153 1153
1154       xrptr += 2; 1154       xrptr += 2;
1155     } 1155     }
1156   } 1156   }
1157 1157
1158   if (cachesz + bits_left < 0) 1158   if (cachesz + bits_left < 0)
1159     return MAD_ERROR_BADHUFFDATA;  /* big_values overrun */ 1159     return MAD_ERROR_BADHUFFDATA;  /* big_values overrun */
1160 1160
1161   /* count1 */ 1161   /* count1 */
1162   { 1162   {
1163     union huffquad const *table; 1163     union huffquad const *table;
1164     register mad_fixed_t requantized; 1164     register mad_fixed_t requantized;
1165 1165
1166     table = mad_huff_quad_table[channel->flags & count1table_select]; 1166     table = mad_huff_quad_table[channel->flags & count1table_select];
1167 1167
1168     requantized = III_requantize(1, exp); 1168     requantized = III_requantize(1, exp);
1169 1169
1170     while (cachesz + bits_left > 0 && xrptr <= &xr[572]) { 1170     while (cachesz + bits_left > 0 && xrptr <= &xr[572]) {
1171       union huffquad const *quad; 1171       union huffquad const *quad;
1172 1172
1173       /* hcod (1..6) */ 1173       /* hcod (1..6) */
1174 1174
1175       if (cachesz < 10) { 1175       if (cachesz < 10) {
1176         bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16); 1176         bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16);
1177         cachesz   += 16; 1177         cachesz   += 16;
1178         bits_left -= 16; 1178         bits_left -= 16;
1179       } 1179       }
1180 1180
1181       quad = &table[MASK(bitcache, cachesz, 4)]; 1181       quad = &table[MASK(bitcache, cachesz, 4)];
1182 1182
1183       /* quad tables guaranteed to have at most one extra lookup */ 1183       /* quad tables guaranteed to have at most one extra lookup */
1184       if (!quad->final) { 1184       if (!quad->final) {
1185         cachesz -= 4; 1185         cachesz -= 4;
1186 1186
1187         quad = &table[quad->ptr.offset + 1187         quad = &table[quad->ptr.offset +
1188                       MASK(bitcache, cachesz, quad->ptr.bits)]; 1188                       MASK(bitcache, cachesz, quad->ptr.bits)];
1189       } 1189       }
1190 1190
1191       cachesz -= quad->value.hlen; 1191       cachesz -= quad->value.hlen;
1192 1192
1193       if (xrptr == sfbound) { 1193       if (xrptr == sfbound) {
1194         sfbound += *sfbwidth++; 1194         sfbound += *sfbwidth++;
1195 1195
1196         if (exp != *expptr) { 1196         if (exp != *expptr) {
1197           exp = *expptr; 1197           exp = *expptr;
1198           requantized = III_requantize(1, exp); 1198           requantized = III_requantize(1, exp);
1199         } 1199         }
1200 1200
1201         ++expptr; 1201         ++expptr;
1202       } 1202       }
1203 1203
1204       /* v (0..1) */ 1204       /* v (0..1) */
1205 1205
1206       xrptr[0] = quad->value.v ? 1206       xrptr[0] = quad->value.v ?
1207         (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0; 1207         (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
1208 1208
1209       /* w (0..1) */ 1209       /* w (0..1) */
1210 1210
1211       xrptr[1] = quad->value.w ? 1211       xrptr[1] = quad->value.w ?
1212         (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0; 1212         (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
1213 1213
1214       xrptr += 2; 1214       xrptr += 2;
1215 1215
1216       if (xrptr == sfbound) { 1216       if (xrptr == sfbound) {
1217         sfbound += *sfbwidth++; 1217         sfbound += *sfbwidth++;
1218 1218
1219         if (exp != *expptr) { 1219         if (exp != *expptr) {
1220           exp = *expptr; 1220           exp = *expptr;
1221           requantized = III_requantize(1, exp); 1221           requantized = III_requantize(1, exp);
1222         } 1222         }
1223 1223
1224         ++expptr; 1224         ++expptr;
1225       } 1225       }
1226 1226
1227       /* x (0..1) */ 1227       /* x (0..1) */
1228 1228
1229       xrptr[0] = quad->value.x ? 1229       xrptr[0] = quad->value.x ?
1230         (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0; 1230         (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
1231 1231
1232       /* y (0..1) */ 1232       /* y (0..1) */
1233 1233
1234       xrptr[1] = quad->value.y ? 1234       xrptr[1] = quad->value.y ?
1235         (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0; 1235         (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
1236 1236
1237       xrptr += 2; 1237       xrptr += 2;
1238     } 1238     }
1239 1239
1240     if (cachesz + bits_left < 0) { 1240     if (cachesz + bits_left < 0) {
1241 # if 0 && defined(DEBUG) 1241 # if 0 && defined(DEBUG)
1242       fprintf(stderr, "huffman count1 overrun (%d bits)\n", 1242       fprintf(stderr, "huffman count1 overrun (%d bits)\n",
1243               -(cachesz + bits_left)); 1243               -(cachesz + bits_left));
1244 # endif 1244 # endif
1245 1245
1246       /* technically the bitstream is misformatted, but apparently 1246       /* technically the bitstream is misformatted, but apparently
1247          some encoders are just a bit sloppy with stuffing bits */ 1247          some encoders are just a bit sloppy with stuffing bits */
1248 1248
1249       xrptr -= 4; 1249       xrptr -= 4;
1250     } 1250     }
1251   } 1251   }
1252 1252
1253   assert(-bits_left <= MAD_BUFFER_GUARD * CHAR_BIT); 1253   assert(-bits_left <= MAD_BUFFER_GUARD * CHAR_BIT);
1254 1254
1255 # if 0 && defined(DEBUG) 1255 # if 0 && defined(DEBUG)
1256   if (bits_left < 0) 1256   if (bits_left < 0)
1257     fprintf(stderr, "read %d bits too many\n", -bits_left); 1257     fprintf(stderr, "read %d bits too many\n", -bits_left);
1258   else if (cachesz + bits_left > 0) 1258   else if (cachesz + bits_left > 0)
1259     fprintf(stderr, "%d stuffing bits\n", cachesz + bits_left); 1259     fprintf(stderr, "%d stuffing bits\n", cachesz + bits_left);
1260 # endif 1260 # endif
1261 1261
1262   /* rzero */ 1262   /* rzero */
1263   while (xrptr < &xr[576]) { 1263   while (xrptr < &xr[576]) {
1264     xrptr[0] = 0; 1264     xrptr[0] = 0;
1265     xrptr[1] = 0; 1265     xrptr[1] = 0;
1266 1266
1267     xrptr += 2; 1267     xrptr += 2;
1268   } 1268   }
1269 1269
1270   return MAD_ERROR_NONE; 1270   return MAD_ERROR_NONE;
1271 } 1271 }
1272 1272
1273 # undef MASK 1273 # undef MASK
1274 # undef MASK1BIT 1274 # undef MASK1BIT
1275 1275
1276 /* 1276 /*
1277  * NAME:        III_reorder() 1277  * NAME:        III_reorder()
1278  * DESCRIPTION: reorder frequency lines of a short block into subband order 1278  * DESCRIPTION: reorder frequency lines of a short block into subband order
1279  */ 1279  */
1280 static 1280 static
1281 void III_reorder(mad_fixed_t xr[576], struct channel const *channel, 1281 void III_reorder(mad_fixed_t xr[576], struct channel const *channel,
1282                  unsigned char const sfbwidth[39]) 1282                  unsigned char const sfbwidth[39])
1283 { 1283 {
1284   mad_fixed_t tmp[32][3][6]; 1284   mad_fixed_t tmp[32][3][6];
1285   unsigned int sb, l, f, w, sbw[3], sw[3]; 1285   unsigned int sb, l, f, w, sbw[3], sw[3];
1286 1286
1287   /* this is probably wrong for 8000 Hz mixed blocks */ 1287   /* this is probably wrong for 8000 Hz mixed blocks */
1288 1288
1289   sb = 0; 1289   sb = 0;
1290   if (channel->flags & mixed_block_flag) { 1290   if (channel->flags & mixed_block_flag) {
1291     sb = 2; 1291     sb = 2;
1292 1292
1293     l = 0; 1293     l = 0;
1294     while (l < 36) 1294     while (l < 36)
1295       l += *sfbwidth++; 1295       l += *sfbwidth++;
1296   } 1296   }
1297 1297
1298   for (w = 0; w < 3; ++w) { 1298   for (w = 0; w < 3; ++w) {
1299     sbw[w] = sb; 1299     sbw[w] = sb;
1300     sw[w]  = 0; 1300     sw[w]  = 0;
1301   } 1301   }
1302 1302
1303   f = *sfbwidth++; 1303   f = *sfbwidth++;
1304   w = 0; 1304   w = 0;
1305 1305
1306   for (l = 18 * sb; l < 576; ++l) { 1306   for (l = 18 * sb; l < 576; ++l) {
1307     if (f-- == 0) { 1307     if (f-- == 0) {
1308       f = *sfbwidth++ - 1; 1308       f = *sfbwidth++ - 1;
1309       w = (w + 1) % 3; 1309       w = (w + 1) % 3;
1310     } 1310     }
1311 1311
1312     tmp[sbw[w]][w][sw[w]++] = xr[l]; 1312     tmp[sbw[w]][w][sw[w]++] = xr[l];
1313 1313
1314     if (sw[w] == 6) { 1314     if (sw[w] == 6) {
1315       sw[w] = 0; 1315       sw[w] = 0;
1316       ++sbw[w]; 1316       ++sbw[w];
1317     } 1317     }
1318   } 1318   }
1319 1319
1320   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));
1321 } 1321 }
1322 1322
1323 /* 1323 /*
1324  * NAME:        III_stereo() 1324  * NAME:        III_stereo()
1325  * DESCRIPTION: perform joint stereo processing on a granule 1325  * DESCRIPTION: perform joint stereo processing on a granule
1326  */ 1326  */
1327 static 1327 static
1328 enum mad_error III_stereo(mad_fixed_t xr[2][576], 1328 enum mad_error III_stereo(mad_fixed_t xr[2][576],
1329                           struct granule const *granule, 1329                           struct granule const *granule,
1330                           struct mad_header *header, 1330                           struct mad_header *header,
1331                           unsigned char const *sfbwidth) 1331                           unsigned char const *sfbwidth)
1332 { 1332 {
1333   short modes[39]; 1333   short modes[39];
1334   unsigned int sfbi, l, n, i; 1334   unsigned int sfbi, l, n, i;
1335 1335
1336   if (granule->ch[0].block_type != 1336   if (granule->ch[0].block_type !=
1337       granule->ch[1].block_type || 1337       granule->ch[1].block_type ||
1338       (granule->ch[0].flags & mixed_block_flag) != 1338       (granule->ch[0].flags & mixed_block_flag) !=
1339       (granule->ch[1].flags & mixed_block_flag)) 1339       (granule->ch[1].flags & mixed_block_flag))
1340     return MAD_ERROR_BADSTEREO; 1340     return MAD_ERROR_BADSTEREO;
1341 1341
1342   for (i = 0; i < 39; ++i) 1342   for (i = 0; i < 39; ++i)
1343     modes[i] = header->mode_extension; 1343     modes[i] = header->mode_extension;
1344 1344
1345   /* intensity stereo */ 1345   /* intensity stereo */
1346 1346
1347   if (header->mode_extension & I_STEREO) { 1347   if (header->mode_extension & I_STEREO) {
1348     struct channel const *right_ch = &granule->ch[1]; 1348     struct channel const *right_ch = &granule->ch[1];
1349     mad_fixed_t const *right_xr = xr[1]; 1349     mad_fixed_t const *right_xr = xr[1];
1350     unsigned int is_pos; 1350     unsigned int is_pos;
1351 1351
1352     header->flags |= MAD_FLAG_I_STEREO; 1352     header->flags |= MAD_FLAG_I_STEREO;
1353 1353
1354     /* first determine which scalefactor bands are to be processed */ 1354     /* first determine which scalefactor bands are to be processed */
1355 1355
1356     if (right_ch->block_type == 2) { 1356     if (right_ch->block_type == 2) {
1357       unsigned int lower, start, max, bound[3], w; 1357       unsigned int lower, start, max, bound[3], w;
1358 1358
1359       lower = start = max = bound[0] = bound[1] = bound[2] = 0; 1359       lower = start = max = bound[0] = bound[1] = bound[2] = 0;
1360 1360
1361       sfbi = l = 0; 1361       sfbi = l = 0;
1362 1362
1363       if (right_ch->flags & mixed_block_flag) { 1363       if (right_ch->flags & mixed_block_flag) {
1364         while (l < 36) { 1364         while (l < 36) {
1365           n = sfbwidth[sfbi++]; 1365           n = sfbwidth[sfbi++];
1366 1366
1367           for (i = 0; i < n; ++i) { 1367           for (i = 0; i < n; ++i) {
1368             if (right_xr[i]) { 1368             if (right_xr[i]) {
1369               lower = sfbi; 1369               lower = sfbi;
1370               break; 1370               break;
1371             } 1371             }
1372           } 1372           }
1373 1373
1374           right_xr += n; 1374           right_xr += n;
1375           l += n; 1375           l += n;
1376         } 1376         }
1377 1377
1378         start = sfbi; 1378         start = sfbi;
1379       } 1379       }
1380 1380
1381       w = 0; 1381       w = 0;
1382       while (l < 576) { 1382       while (l < 576) {
1383         n = sfbwidth[sfbi++]; 1383         n = sfbwidth[sfbi++];
1384 1384
1385         for (i = 0; i < n; ++i) { 1385         for (i = 0; i < n; ++i) {
1386           if (right_xr[i]) { 1386           if (right_xr[i]) {
1387             max = bound[w] = sfbi; 1387             max = bound[w] = sfbi;
1388             break; 1388             break;
1389           } 1389           }
1390         } 1390         }
1391 1391
1392         right_xr += n; 1392         right_xr += n;
1393         l += n; 1393         l += n;
1394         w = (w + 1) % 3; 1394         w = (w + 1) % 3;
1395       } 1395       }
1396 1396
1397       if (max) 1397       if (max)
1398         lower = start; 1398         lower = start;
1399 1399
1400       /* long blocks */ 1400       /* long blocks */
1401 1401
1402       for (i = 0; i < lower; ++i) 1402       for (i = 0; i < lower; ++i)
1403         modes[i] = header->mode_extension & ~I_STEREO; 1403         modes[i] = header->mode_extension & ~I_STEREO;
1404 1404
1405       /* short blocks */ 1405       /* short blocks */
1406 1406
1407       w = 0; 1407       w = 0;
1408       for (i = start; i < max; ++i) { 1408       for (i = start; i < max; ++i) {
1409         if (i < bound[w]) 1409         if (i < bound[w])
1410           modes[i] = header->mode_extension & ~I_STEREO; 1410           modes[i] = header->mode_extension & ~I_STEREO;
1411 1411
1412         w = (w + 1) % 3; 1412         w = (w + 1) % 3;
1413       } 1413       }
1414     } 1414     }
1415     else {  /* right_ch->block_type != 2 */ 1415     else {  /* right_ch->block_type != 2 */
1416       unsigned int bound; 1416       unsigned int bound;
1417 1417
1418       bound = 0; 1418       bound = 0;
1419       for (sfbi = l = 0; l < 576; l += n) { 1419       for (sfbi = l = 0; l < 576; l += n) {
1420         n = sfbwidth[sfbi++]; 1420         n = sfbwidth[sfbi++];
1421 1421
1422         for (i = 0; i < n; ++i) { 1422         for (i = 0; i < n; ++i) {
1423           if (right_xr[i]) { 1423           if (right_xr[i]) {
1424             bound = sfbi; 1424             bound = sfbi;
1425             break; 1425             break;
1426           } 1426           }
1427         } 1427         }
1428 1428
1429         right_xr += n; 1429         right_xr += n;
1430       } 1430       }
1431 1431
1432       for (i = 0; i < bound; ++i) 1432       for (i = 0; i < bound; ++i)
1433         modes[i] = header->mode_extension & ~I_STEREO; 1433         modes[i] = header->mode_extension & ~I_STEREO;
1434     } 1434     }
1435 1435
1436     /* now do the actual processing */ 1436     /* now do the actual processing */
1437 1437
1438     if (header->flags & MAD_FLAG_LSF_EXT) { 1438     if (header->flags & MAD_FLAG_LSF_EXT) {
1439       unsigned char const *illegal_pos = granule[1].ch[1].scalefac; 1439       unsigned char const *illegal_pos = granule[1].ch[1].scalefac;
1440       mad_fixed_t const *lsf_scale; 1440       mad_fixed_t const *lsf_scale;
1441 1441
1442       /* intensity_scale */ 1442       /* intensity_scale */
1443       lsf_scale = is_lsf_table[right_ch->scalefac_compress & 0x1]; 1443       lsf_scale = is_lsf_table[right_ch->scalefac_compress & 0x1];
1444 1444
1445       for (sfbi = l = 0; l < 576; ++sfbi, l += n) { 1445       for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
1446         n = sfbwidth[sfbi]; 1446         n = sfbwidth[sfbi];
1447 1447
1448         if (!(modes[sfbi] & I_STEREO)) 1448         if (!(modes[sfbi] & I_STEREO))
1449           continue; 1449           continue;
1450 1450
1451         if (illegal_pos[sfbi]) { 1451         if (illegal_pos[sfbi]) {
1452           modes[sfbi] &= ~I_STEREO; 1452           modes[sfbi] &= ~I_STEREO;
1453           continue; 1453           continue;
1454         } 1454         }
1455 1455
1456         is_pos = right_ch->scalefac[sfbi]; 1456         is_pos = right_ch->scalefac[sfbi];
1457 1457
1458         for (i = 0; i < n; ++i) { 1458         for (i = 0; i < n; ++i) {
1459           register mad_fixed_t left; 1459           register mad_fixed_t left;
1460 1460
1461           left = xr[0][l + i]; 1461           left = xr[0][l + i];
1462 1462
1463           if (is_pos == 0) 1463           if (is_pos == 0)
1464             xr[1][l + i] = left; 1464             xr[1][l + i] = left;
1465           else { 1465           else {
1466             register mad_fixed_t opposite; 1466             register mad_fixed_t opposite;
1467 1467
1468             opposite = mad_f_mul(left, lsf_scale[(is_pos - 1) / 2]); 1468             opposite = mad_f_mul(left, lsf_scale[(is_pos - 1) / 2]);
1469 1469
1470             if (is_pos & 1) { 1470             if (is_pos & 1) {
1471               xr[0][l + i] = opposite; 1471               xr[0][l + i] = opposite;
1472               xr[1][l + i] = left; 1472               xr[1][l + i] = left;
1473             } 1473             }
1474             else 1474             else
1475               xr[1][l + i] = opposite; 1475               xr[1][l + i] = opposite;
1476           } 1476           }
1477         } 1477         }
1478       } 1478       }
1479     } 1479     }
1480     else {  /* !(header->flags & MAD_FLAG_LSF_EXT) */ 1480     else {  /* !(header->flags & MAD_FLAG_LSF_EXT) */
1481       for (sfbi = l = 0; l < 576; ++sfbi, l += n) { 1481       for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
1482         n = sfbwidth[sfbi]; 1482         n = sfbwidth[sfbi];
1483 1483
1484         if (!(modes[sfbi] & I_STEREO)) 1484         if (!(modes[sfbi] & I_STEREO))
1485           continue; 1485           continue;
1486 1486
1487         is_pos = right_ch->scalefac[sfbi]; 1487         is_pos = right_ch->scalefac[sfbi];
1488 1488
1489         if (is_pos >= 7) {  /* illegal intensity position */ 1489         if (is_pos >= 7) {  /* illegal intensity position */
1490           modes[sfbi] &= ~I_STEREO; 1490           modes[sfbi] &= ~I_STEREO;
1491           continue; 1491           continue;
1492         } 1492         }
1493 1493
1494         for (i = 0; i < n; ++i) { 1494         for (i = 0; i < n; ++i) {
1495           register mad_fixed_t left; 1495           register mad_fixed_t left;
1496 1496
1497           left = xr[0][l + i]; 1497           left = xr[0][l + i];
1498 1498
1499           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]);
1500           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]);
1501         } 1501         }
1502       } 1502       }
1503     } 1503     }
1504   } 1504   }
1505 1505
1506   /* middle/side stereo */ 1506   /* middle/side stereo */
1507 1507
1508   if (header->mode_extension & MS_STEREO) { 1508   if (header->mode_extension & MS_STEREO) {
1509     register mad_fixed_t invsqrt2; 1509     register mad_fixed_t invsqrt2;
1510 1510
1511     header->flags |= MAD_FLAG_MS_STEREO; 1511     header->flags |= MAD_FLAG_MS_STEREO;
1512 1512
1513     invsqrt2 = root_table[3 + -2]; 1513     invsqrt2 = root_table[3 + -2];
1514 1514
1515     for (sfbi = l = 0; l < 576; ++sfbi, l += n) { 1515     for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
1516       n = sfbwidth[sfbi]; 1516       n = sfbwidth[sfbi];
1517 1517
1518       if (modes[sfbi] != MS_STEREO) 1518       if (modes[sfbi] != MS_STEREO)
1519         continue; 1519         continue;
1520 1520
1521       for (i = 0; i < n; ++i) { 1521       for (i = 0; i < n; ++i) {
1522         register mad_fixed_t m, s; 1522         register mad_fixed_t m, s;
1523 1523
1524         m = xr[0][l + i]; 1524         m = xr[0][l + i];
1525         s = xr[1][l + i]; 1525         s = xr[1][l + i];
1526 1526
1527         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) */
1528         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) */
1529       } 1529       }
1530     } 1530     }
1531   } 1531   }
1532 1532
1533   return MAD_ERROR_NONE; 1533   return MAD_ERROR_NONE;
1534 } 1534 }
1535 1535
1536 /* 1536 /*
1537  * NAME:        III_aliasreduce() 1537  * NAME:        III_aliasreduce()
1538  * DESCRIPTION: perform frequency line alias reduction 1538  * DESCRIPTION: perform frequency line alias reduction
1539  */ 1539  */
1540 static 1540 static
1541 void III_aliasreduce(mad_fixed_t xr[576], int lines) 1541 void III_aliasreduce(mad_fixed_t xr[576], int lines)
1542 { 1542 {
1543   mad_fixed_t const *bound; 1543   mad_fixed_t const *bound;
1544   int i; 1544   int i;
1545 1545
1546   bound = &xr[lines]; 1546   bound = &xr[lines];
1547   for (xr += 18; xr < bound; xr += 18) { 1547   for (xr += 18; xr < bound; xr += 18) {
1548     for (i = 0; i < 8; ++i) { 1548     for (i = 0; i < 8; ++i) {
1549       register mad_fixed_t a, b; 1549       register mad_fixed_t a, b;
1550       register mad_fixed64hi_t hi; 1550       register mad_fixed64hi_t hi;
1551       register mad_fixed64lo_t lo; 1551       register mad_fixed64lo_t lo;
1552 1552
1553       a = xr[-1 - i]; 1553       a = xr[-1 - i];
1554       b = xr[     i]; 1554       b = xr[     i];
1555 1555
1556 # if defined(ASO_ZEROCHECK) 1556 # if defined(ASO_ZEROCHECK)
1557       if (a | b) { 1557       if (a | b) {
1558 # endif 1558 # endif
1559         MAD_F_ML0(hi, lo,  a, cs[i]); 1559         MAD_F_ML0(hi, lo,  a, cs[i]);
1560         MAD_F_MLA(hi, lo, -b, ca[i]); 1560         MAD_F_MLA(hi, lo, -b, ca[i]);
1561 1561
1562         xr[-1 - i] = MAD_F_MLZ(hi, lo); 1562         xr[-1 - i] = MAD_F_MLZ(hi, lo);
1563 1563
1564         MAD_F_ML0(hi, lo,  b, cs[i]); 1564         MAD_F_ML0(hi, lo,  b, cs[i]);
1565         MAD_F_MLA(hi, lo,  a, ca[i]); 1565         MAD_F_MLA(hi, lo,  a, ca[i]);
1566 1566
1567         xr[     i] = MAD_F_MLZ(hi, lo); 1567         xr[     i] = MAD_F_MLZ(hi, lo);
1568 # if defined(ASO_ZEROCHECK) 1568 # if defined(ASO_ZEROCHECK)
1569       } 1569       }
1570 # endif 1570 # endif
1571     } 1571     }
1572   } 1572   }
1573 } 1573 }
1574 1574
1575 # if defined(ASO_IMDCT) 1575 # if defined(ASO_IMDCT)
1576 void III_imdct_l(mad_fixed_t const [18], mad_fixed_t [36], unsigned int); 1576 void III_imdct_l(mad_fixed_t const [18], mad_fixed_t [36], unsigned int);
1577 # else 1577 # else
-   1578 #  if 1
-   1579 static
-   1580 void fastsdct(mad_fixed_t const x[9], mad_fixed_t y[18])
-   1581 {
-   1582   mad_fixed_t a0,  a1,  a2,  a3,  a4,  a5,  a6,  a7,  a8,  a9,  a10, a11, a12;
-   1583   mad_fixed_t a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23, a24, a25;
-   1584   mad_fixed_t m0,  m1,  m2,  m3,  m4,  m5,  m6,  m7;
-   1585
-   1586   enum {
-   1587     c0 =  MAD_F(0x1f838b8d),  /* 2 * cos( 1 * PI / 18) */
-   1588     c1 =  MAD_F(0x1bb67ae8),  /* 2 * cos( 3 * PI / 18) */
-   1589     c2 =  MAD_F(0x18836fa3),  /* 2 * cos( 4 * PI / 18) */
-   1590     c3 =  MAD_F(0x1491b752),  /* 2 * cos( 5 * PI / 18) */
-   1591     c4 =  MAD_F(0x0af1d43a),  /* 2 * cos( 7 * PI / 18) */
-   1592     c5 =  MAD_F(0x058e86a0),  /* 2 * cos( 8 * PI / 18) */
-   1593     c6 = -MAD_F(0x1e11f642)   /* 2 * cos(16 * PI / 18) */
-   1594   };
-   1595
-   1596   a0 = x[3] + x[5];
-   1597   a1 = x[3] - x[5];
-   1598   a2 = x[6] + x[2];
-   1599   a3 = x[6] - x[2];
-   1600   a4 = x[1] + x[7];
-   1601   a5 = x[1] - x[7];
-   1602   a6 = x[8] + x[0];
-   1603   a7 = x[8] - x[0];
-   1604
-   1605   a8  = a0  + a2;
-   1606   a9  = a0  - a2;
-   1607   a10 = a0  - a6;
-   1608   a11 = a2  - a6;
-   1609   a12 = a8  + a6;
-   1610   a13 = a1  - a3;
-   1611   a14 = a13 + a7;
-   1612   a15 = a3  + a7;
-   1613   a16 = a1  - a7;
-   1614   a17 = a1  + a3;
-   1615
-   1616   m0 = mad_f_mul(a17, -c3);
-   1617   m1 = mad_f_mul(a16, -c0);
-   1618   m2 = mad_f_mul(a15, -c4);
-   1619   m3 = mad_f_mul(a14, -c1);
-   1620   m4 = mad_f_mul(a5,  -c1);
-   1621   m5 = mad_f_mul(a11, -c6);
-   1622   m6 = mad_f_mul(a10, -c5);
-   1623   m7 = mad_f_mul(a9,  -c2);
-   1624
-   1625   a18 =     x[4] + a4;
-   1626   a19 = 2 * x[4] - a4;
-   1627   a20 = a19 + m5;
-   1628   a21 = a19 - m5;
-   1629   a22 = a19 + m6;
-   1630   a23 = m4  + m2;
-   1631   a24 = m4  - m2;
-   1632   a25 = m4  + m1;
-   1633
-   1634   /* output to every other slot for convenience */
-   1635
-   1636   y[ 0] = a18 + a12;
-   1637   y[ 2] = m0  - a25;
-   1638   y[ 4] = m7  - a20;
-   1639   y[ 6] = m3;
-   1640   y[ 8] = a21 - m6;
-   1641   y[10] = a24 - m1;
-   1642   y[12] = a12 - 2 * a18;
-   1643   y[14] = a23 + m0;
-   1644   y[16] = a22 + m7;
-   1645 }
-   1646
-   1647 static inline
-   1648 void sdctII(mad_fixed_t const x[18], mad_fixed_t X[18])
-   1649 {
-   1650   mad_fixed_t tmp[9];
-   1651   int i;
-   1652
-   1653   /* scale[i] = 2 * cos(PI * (2 * i + 1) / (2 * 18)) */
-   1654   static mad_fixed_t const scale[9] = {
-   1655     MAD_F(0x1fe0d3b4), MAD_F(0x1ee8dd47), MAD_F(0x1d007930),
-   1656     MAD_F(0x1a367e59), MAD_F(0x16a09e66), MAD_F(0x125abcf8),
-   1657     MAD_F(0x0d8616bc), MAD_F(0x08483ee1), MAD_F(0x02c9fad7)
-   1658   };
-   1659
-   1660   /* divide the 18-point SDCT-II into two 9-point SDCT-IIs */
-   1661
-   1662   /* even input butterfly */
-   1663
-   1664   for (i = 0; i < 9; i += 3) {
-   1665     tmp[i + 0] = x[i + 0] + x[18 - (i + 0) - 1];
-   1666     tmp[i + 1] = x[i + 1] + x[18 - (i + 1) - 1];
-   1667     tmp[i + 2] = x[i + 2] + x[18 - (i + 2) - 1];
-   1668   }
-   1669
-   1670   fastsdct(tmp, &X[0]);
-   1671
-   1672   /* odd input butterfly and scaling */
-   1673
-   1674   for (i = 0; i < 9; i += 3) {
-   1675     tmp[i + 0] = mad_f_mul(x[i + 0] - x[18 - (i + 0) - 1], scale[i + 0]);
-   1676     tmp[i + 1] = mad_f_mul(x[i + 1] - x[18 - (i + 1) - 1], scale[i + 1]);
-   1677     tmp[i + 2] = mad_f_mul(x[i + 2] - x[18 - (i + 2) - 1], scale[i + 2]);
-   1678   }
-   1679
-   1680   fastsdct(tmp, &X[1]);
-   1681
-   1682   /* output accumulation */
-   1683
-   1684   for (i = 3; i < 18; i += 8) {
-   1685     X[i + 0] -= X[(i + 0) - 2];
-   1686     X[i + 2] -= X[(i + 2) - 2];
-   1687     X[i + 4] -= X[(i + 4) - 2];
-   1688     X[i + 6] -= X[(i + 6) - 2];
-   1689   }
-   1690 }
-   1691
-   1692 static inline
-   1693 void dctIV(mad_fixed_t const y[18], mad_fixed_t X[18])
-   1694 {
-   1695   mad_fixed_t tmp[18];
-   1696   int i;
-   1697
-   1698   /* scale[i] = 2 * cos(PI * (2 * i + 1) / (4 * 18)) */
-   1699   static mad_fixed_t const scale[18] = {
-   1700     MAD_F(0x1ff833fa), MAD_F(0x1fb9ea93), MAD_F(0x1f3dd120),
-   1701     MAD_F(0x1e84d969), MAD_F(0x1d906bcf), MAD_F(0x1c62648b),
-   1702     MAD_F(0x1afd100f), MAD_F(0x1963268b), MAD_F(0x1797c6a4),
-   1703     MAD_F(0x159e6f5b), MAD_F(0x137af940), MAD_F(0x11318ef3),
-   1704     MAD_F(0x0ec6a507), MAD_F(0x0c3ef153), MAD_F(0x099f61c5),
-   1705     MAD_F(0x06ed12c5), MAD_F(0x042d4544), MAD_F(0x0165547c)
-   1706   };
-   1707
-   1708   /* scaling */
-   1709
-   1710   for (i = 0; i < 18; i += 3) {
-   1711     tmp[i + 0] = mad_f_mul(y[i + 0], scale[i + 0]);
-   1712     tmp[i + 1] = mad_f_mul(y[i + 1], scale[i + 1]);
-   1713     tmp[i + 2] = mad_f_mul(y[i + 2], scale[i + 2]);
-   1714   }
-   1715
-   1716   /* SDCT-II */
-   1717
-   1718   sdctII(tmp, X);
-   1719
-   1720   /* scale reduction and output accumulation */
-   1721
-   1722   X[0] /= 2;
-   1723   for (i = 1; i < 17; i += 4) {
-   1724     X[i + 0] = X[i + 0] / 2 - X[(i + 0) - 1];
-   1725     X[i + 1] = X[i + 1] / 2 - X[(i + 1) - 1];
-   1726     X[i + 2] = X[i + 2] / 2 - X[(i + 2) - 1];
-   1727     X[i + 3] = X[i + 3] / 2 - X[(i + 3) - 1];
-   1728   }
-   1729   X[17] = X[17] / 2 - X[16];
-   1730 }
-   1731
-   1732 /*
-   1733  * NAME:        imdct36
-   1734  * DESCRIPTION: perform X[18]->x[36] IMDCT using Szu-Wei Lee's fast algorithm
-   1735  */
-   1736 static inline
-   1737 void imdct36(mad_fixed_t const x[18], mad_fixed_t y[36])
-   1738 {
-   1739   mad_fixed_t tmp[18];
-   1740   int i;
-   1741
-   1742   /* DCT-IV */
-   1743
-   1744   dctIV(x, tmp);
-   1745
-   1746   /* convert 18-point DCT-IV to 36-point IMDCT */
-   1747
-   1748   for (i =  0; i <  9; i += 3) {
-   1749     y[i + 0] =  tmp[9 + (i + 0)];
-   1750     y[i + 1] =  tmp[9 + (i + 1)];
-   1751     y[i + 2] =  tmp[9 + (i + 2)];
-   1752   }
-   1753   for (i =  9; i < 27; i += 3) {
-   1754     y[i + 0] = -tmp[36 - (9 + (i + 0)) - 1];
-   1755     y[i + 1] = -tmp[36 - (9 + (i + 1)) - 1];
-   1756     y[i + 2] = -tmp[36 -