rtoss - Blame information for rev 26

Subversion Repositories:
Rev:
Rev Author Line No. Line
14 roytam 1 /*
2  * libmad - MPEG audio decoder library
26 roytam 3  * Copyright (C) 2000-2004 Underbit Technologies, Inc.
14 roytam 4  *
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
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
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
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
26 roytam 19  * $Id: layer3.c,v 1.43 2004/01/23 09:41:32 rob Exp $
14 roytam 20  */
21  
22 # ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 # endif
25  
26 # include "global.h"
27  
28 # include <stdlib.h>
29 # include <string.h>
30  
31 # ifdef HAVE_ASSERT_H
32 #  include <assert.h>
33 # endif
34  
35 # ifdef HAVE_LIMITS_H
36 #  include <limits.h>
37 # else
38 #  define CHAR_BIT  8
39 # endif
40  
41 # include "fixed.h"
42 # include "bit.h"
43 # include "stream.h"
44 # include "frame.h"
45 # include "huffman.h"
46 # include "layer3.h"
47  
48 /* --- Layer III ----------------------------------------------------------- */
49  
50 enum {
51   count1table_select = 0x01,
52   scalefac_scale     = 0x02,
53   preflag            = 0x04,
54   mixed_block_flag   = 0x08
55 };
56  
57 enum {
58   I_STEREO  = 0x1,
59   MS_STEREO = 0x2
60 };
61  
62 struct sideinfo {
63   unsigned int main_data_begin;
64   unsigned int private_bits;
65  
66   unsigned char scfsi[2];
67  
68   struct granule {
69     struct channel {
70       /* from side info */
71       unsigned short part2_3_length;
72       unsigned short big_values;
73       unsigned short global_gain;
74       unsigned short scalefac_compress;
75  
76       unsigned char flags;
77       unsigned char block_type;
78       unsigned char table_select[3];
79       unsigned char subblock_gain[3];
80       unsigned char region0_count;
81       unsigned char region1_count;
82  
83       /* from main_data */
84       unsigned char scalefac[39];       /* scalefac_l and/or scalefac_s */
85     } ch[2];
86   } gr[2];
87 };
88  
89 /*
90  * scalefactor bit lengths
91  * derived from section 2.4.2.7 of ISO/IEC 11172-3
92  */
93 static
94 struct {
95   unsigned char slen1;
96   unsigned char slen2;
97 } const sflen_table[16] = {
98   { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 },
99   { 3, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 },
100   { 2, 1 }, { 2, 2 }, { 2, 3 }, { 3, 1 },
101   { 3, 2 }, { 3, 3 }, { 4, 2 }, { 4, 3 }
102 };
103  
104 /*
105  * number of LSF scalefactor band values
106  * derived from section 2.4.3.2 of ISO/IEC 13818-3
107  */
108 static
109 unsigned char const nsfb_table[6][3][4] = {
110   { {  6,  5,  5, 5 },
111     {  9,  9,  9, 9 },
112     {  6,  9,  9, 9 } },
113  
114   { {  6,  5,  7, 3 },
115     {  9,  9, 12, 6 },
116     {  6,  9, 12, 6 } },
117  
118   { { 11, 10,  0, 0 },
119     { 18, 18,  0, 0 },
120     { 15, 18,  0, 0 } },
121  
122   { {  7,  7,  7, 0 },
123     { 12, 12, 12, 0 },
124     {  6, 15, 12, 0 } },
125  
126   { {  6,  6,  6, 3 },
127     { 12,  9,  9, 6 },
128     {  6, 12,  9, 6 } },
129  
130   { {  8,  8,  5, 0 },
131     { 15, 12,  9, 0 },
132     {  6, 18,  9, 0 } }
133 };
134  
135 /*
136  * MPEG-1 scalefactor band widths
137  * derived from Table B.8 of ISO/IEC 11172-3
138  */
139 static
140 unsigned char const sfb_48000_long[] = {
141    4,  4,  4,  4,  4,  4,  6,  6,  6,   8,  10,
142   12, 16, 18, 22, 28, 34, 40, 46, 54,  54, 192
143 };
144  
145 static
146 unsigned char const sfb_44100_long[] = {
147    4,  4,  4,  4,  4,  4,  6,  6,  8,   8,  10,
148   12, 16, 20, 24, 28, 34, 42, 50, 54,  76, 158
149 };
150  
151 static
152 unsigned char const sfb_32000_long[] = {
153    4,  4,  4,  4,  4,  4,  6,  6,  8,  10,  12,
154   16, 20, 24, 30, 38, 46, 56, 68, 84, 102,  26
155 };
156  
157 static
158 unsigned char const sfb_48000_short[] = {
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,
161   14, 16, 16, 16, 20, 20, 20, 26, 26, 26, 66, 66, 66
162 };
163  
164 static
165 unsigned char const sfb_44100_short[] = {
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,
168   14, 18, 18, 18, 22, 22, 22, 30, 30, 30, 56, 56, 56
169 };
170  
171 static
172 unsigned char const sfb_32000_short[] = {
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,
175   20, 26, 26, 26, 34, 34, 34, 42, 42, 42, 12, 12, 12
176 };
177  
178 static
179 unsigned char const sfb_48000_mixed[] = {
180   /* long */   4,  4,  4,  4,  4,  4,  6,  6,
181   /* short */  4,  4,  4,  6,  6,  6,  6,  6,  6, 10,
182               10, 10, 12, 12, 12, 14, 14, 14, 16, 16,
183               16, 20, 20, 20, 26, 26, 26, 66, 66, 66
184 };
185  
186 static
187 unsigned char const sfb_44100_mixed[] = {
188   /* long */   4,  4,  4,  4,  4,  4,  6,  6,
189   /* short */  4,  4,  4,  6,  6,  6,  8,  8,  8, 10,
190               10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
191               18, 22, 22, 22, 30, 30, 30, 56, 56, 56
192 };
193  
194 static
195 unsigned char const sfb_32000_mixed[] = {
196   /* long */   4,  4,  4,  4,  4,  4,  6,  6,
197   /* short */  4,  4,  4,  6,  6,  6,  8,  8,  8, 12,
198               12, 12, 16, 16, 16, 20, 20, 20, 26, 26,
199               26, 34, 34, 34, 42, 42, 42, 12, 12, 12
200 };
201  
202 /*
203  * MPEG-2 scalefactor band widths
204  * derived from Table B.2 of ISO/IEC 13818-3
205  */
206 static
207 unsigned char const sfb_24000_long[] = {
208    6,  6,  6,  6,  6,  6,  8, 10, 12,  14,  16,
209   18, 22, 26, 32, 38, 46, 54, 62, 70,  76,  36
210 };
211  
212 static
213 unsigned char const sfb_22050_long[] = {
214    6,  6,  6,  6,  6,  6,  8, 10, 12,  14,  16,
215   20, 24, 28, 32, 38, 46, 52, 60, 68,  58,  54
216 };
217  
218 # define sfb_16000_long  sfb_22050_long
219  
220 static
221 unsigned char const sfb_24000_short[] = {
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,
224   18, 24, 24, 24, 32, 32, 32, 44, 44, 44, 12, 12, 12
225 };
226  
227 static
228 unsigned char const sfb_22050_short[] = {
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,
231   18, 26, 26, 26, 32, 32, 32, 42, 42, 42, 18, 18, 18
232 };
233  
234 static
235 unsigned char const sfb_16000_short[] = {
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,
238   18, 24, 24, 24, 30, 30, 30, 40, 40, 40, 18, 18, 18
239 };
240  
241 static
242 unsigned char const sfb_24000_mixed[] = {
243   /* long */   6,  6,  6,  6,  6,  6,
244   /* short */  6,  6,  6,  8,  8,  8, 10, 10, 10, 12,
245               12, 12, 14, 14, 14, 18, 18, 18, 24, 24,
246               24, 32, 32, 32, 44, 44, 44, 12, 12, 12
247 };
248  
249 static
250 unsigned char const sfb_22050_mixed[] = {
251   /* long */   6,  6,  6,  6,  6,  6,
252   /* short */  6,  6,  6,  6,  6,  6,  8,  8,  8, 10,
253               10, 10, 14, 14, 14, 18, 18, 18, 26, 26,
254               26, 32, 32, 32, 42, 42, 42, 18, 18, 18
255 };
256  
257 static
258 unsigned char const sfb_16000_mixed[] = {
259   /* long */   6,  6,  6,  6,  6,  6,
260   /* short */  6,  6,  6,  8,  8,  8, 10, 10, 10, 12,
261               12, 12, 14, 14, 14, 18, 18, 18, 24, 24,
262               24, 30, 30, 30, 40, 40, 40, 18, 18, 18
263 };
264  
265 /*
266  * MPEG 2.5 scalefactor band widths
267  * derived from public sources
268  */
269 # define sfb_12000_long  sfb_16000_long
270 # define sfb_11025_long  sfb_12000_long
271  
272 static
273 unsigned char const sfb_8000_long[] = {
274   12, 12, 12, 12, 12, 12, 16, 20, 24,  28,  32,
275   40, 48, 56, 64, 76, 90,  2,  2,  2,   2,   2
276 };
277  
278 # define sfb_12000_short  sfb_16000_short
279 # define sfb_11025_short  sfb_12000_short
280  
281 static
282 unsigned char const sfb_8000_short[] = {
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,
285   36,  2,  2,  2,  2,  2,  2,  2,  2,  2, 26, 26, 26
286 };
287  
288 # define sfb_12000_mixed  sfb_16000_mixed
289 # define sfb_11025_mixed  sfb_12000_mixed
290  
291 /* the 8000 Hz short block scalefactor bands do not break after
292    the first 36 frequency lines, so this is probably wrong */
293 static
294 unsigned char const sfb_8000_mixed[] = {
295   /* long */  12, 12, 12,
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,
298                2,  2,  2,  2,  2,  2,  2,  2,  2, 26, 26, 26
299 };
300  
301 static
302 struct {
303   unsigned char const *l;
304   unsigned char const *s;
305   unsigned char const *m;
306 } const sfbwidth_table[9] = {
307   { sfb_48000_long, sfb_48000_short, sfb_48000_mixed },
308   { sfb_44100_long, sfb_44100_short, sfb_44100_mixed },
309   { sfb_32000_long, sfb_32000_short, sfb_32000_mixed },
310   { sfb_24000_long, sfb_24000_short, sfb_24000_mixed },
311   { sfb_22050_long, sfb_22050_short, sfb_22050_mixed },
312   { sfb_16000_long, sfb_16000_short, sfb_16000_mixed },
313   { sfb_12000_long, sfb_12000_short, sfb_12000_mixed },
314   { sfb_11025_long, sfb_11025_short, sfb_11025_mixed },
315   {  sfb_8000_long,  sfb_8000_short,  sfb_8000_mixed }
316 };
317  
318 /*
319  * scalefactor band preemphasis (used only when preflag is set)
320  * derived from Table B.6 of ISO/IEC 11172-3
321  */
322 static
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
325 };
326  
327 /*
328  * table for requantization
329  *
330  * rq_table[x].mantissa * 2^(rq_table[x].exponent) = x^(4/3)
331  */
332 static
333 struct fixedfloat {
334   unsigned long mantissa  : 27;
335   unsigned short exponent :  5;
336 } const rq_table[8207] = {
337 # include "rq_table.dat"
338 };
339  
340 /*
341  * fractional powers of two
342  * used for requantization and joint stereo decoding
343  *
344  * root_table[3 + x] = 2^(x/4)
345  */
346 static
347 mad_fixed_t const root_table[7] = {
348   MAD_F(0x09837f05) /* 2^(-3/4) == 0.59460355750136 */,
349   MAD_F(0x0b504f33) /* 2^(-2/4) == 0.70710678118655 */,
350   MAD_F(0x0d744fcd) /* 2^(-1/4) == 0.84089641525371 */,
351   MAD_F(0x10000000) /* 2^( 0/4) == 1.00000000000000 */,
352   MAD_F(0x1306fe0a) /* 2^(+1/4) == 1.18920711500272 */,
353   MAD_F(0x16a09e66) /* 2^(+2/4) == 1.41421356237310 */,
354   MAD_F(0x1ae89f99) /* 2^(+3/4) == 1.68179283050743 */
355 };
356  
357 /*
358  * coefficients for aliasing reduction
359  * derived from Table B.9 of ISO/IEC 11172-3
360  *
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)
363  * ca[i] = c[i] / sqrt(1 + c[i]^2)
364  */
365 static
366 mad_fixed_t const cs[8] = {
367   +MAD_F(0x0db84a81) /* +0.857492926 */, +MAD_F(0x0e1b9d7f) /* +0.881741997 */,
368   +MAD_F(0x0f31adcf) /* +0.949628649 */, +MAD_F(0x0fbba815) /* +0.983314592 */,
369   +MAD_F(0x0feda417) /* +0.995517816 */, +MAD_F(0x0ffc8fc8) /* +0.999160558 */,
370   +MAD_F(0x0fff964c) /* +0.999899195 */, +MAD_F(0x0ffff8d3) /* +0.999993155 */
371 };
372  
373 static
374 mad_fixed_t const ca[8] = {
375   -MAD_F(0x083b5fe7) /* -0.514495755 */, -MAD_F(0x078c36d2) /* -0.471731969 */,
376   -MAD_F(0x05039814) /* -0.313377454 */, -MAD_F(0x02e91dd1) /* -0.181913200 */,
377   -MAD_F(0x0183603a) /* -0.094574193 */, -MAD_F(0x00a7cb87) /* -0.040965583 */,
378   -MAD_F(0x003a2847) /* -0.014198569 */, -MAD_F(0x000f27b4) /* -0.003699975 */
379 };
380  
381 /*
382  * IMDCT coefficients for short blocks
383  * derived from section 2.4.3.4.10.2 of ISO/IEC 11172-3
384  *
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))
387  */
388 static
389 mad_fixed_t const imdct_s[6][6] = {
390 # include "imdct_s.dat"
391 };
392  
393 # if !defined(ASO_IMDCT)
394 /*
395  * windowing coefficients for long blocks
396  * derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
397  *
398  * window_l[i] = sin((PI / 36) * (i + 1/2))
399  */
400 static
401 mad_fixed_t const window_l[36] = {
402   MAD_F(0x00b2aa3e) /* 0.043619387 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
403   MAD_F(0x03768962) /* 0.216439614 */, MAD_F(0x04cfb0e2) /* 0.300705800 */,
404   MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x07635284) /* 0.461748613 */,
405   MAD_F(0x0898c779) /* 0.537299608 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
406   MAD_F(0x0acf37ad) /* 0.675590208 */, MAD_F(0x0bcbe352) /* 0.737277337 */,
407   MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x0d7e8807) /* 0.843391446 */,
408  
409   MAD_F(0x0e313245) /* 0.887010833 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
410   MAD_F(0x0f426cb5) /* 0.953716951 */, MAD_F(0x0f9ee890) /* 0.976296007 */,
411   MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ffc19fd) /* 0.999048222 */,
412   MAD_F(0x0ffc19fd) /* 0.999048222 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
413   MAD_F(0x0f9ee890) /* 0.976296007 */, MAD_F(0x0f426cb5) /* 0.953716951 */,
414   MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0e313245) /* 0.887010833 */,
415  
416   MAD_F(0x0d7e8807) /* 0.843391446 */, MAD_F(0x0cb19346) /* 0.793353340 */,
417   MAD_F(0x0bcbe352) /* 0.737277337 */, MAD_F(0x0acf37ad) /* 0.675590208 */,
418   MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0898c779) /* 0.537299608 */,
419   MAD_F(0x07635284) /* 0.461748613 */, MAD_F(0x061f78aa) /* 0.382683432 */,
420   MAD_F(0x04cfb0e2) /* 0.300705800 */, MAD_F(0x03768962) /* 0.216439614 */,
421   MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x00b2aa3e) /* 0.043619387 */,
422 };
423 # endif  /* ASO_IMDCT */
424  
425 /*
426  * windowing coefficients for short blocks
427  * derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
428  *
429  * window_s[i] = sin((PI / 12) * (i + 1/2))
430  */
431 static
432 mad_fixed_t const window_s[12] = {
433   MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x061f78aa) /* 0.382683432 */,
434   MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0cb19346) /* 0.793353340 */,
435   MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
436   MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
437   MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
438   MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
439 };
440  
441 /*
442  * coefficients for intensity stereo processing
443  * derived from section 2.4.3.4.9.3 of ISO/IEC 11172-3
444  *
445  * is_ratio[i] = tan(i * (PI / 12))
446  * is_table[i] = is_ratio[i] / (1 + is_ratio[i])
447  */
448 static
449 mad_fixed_t const is_table[7] = {
450   MAD_F(0x00000000) /* 0.000000000 */,
451   MAD_F(0x0361962f) /* 0.211324865 */,
452   MAD_F(0x05db3d74) /* 0.366025404 */,
453   MAD_F(0x08000000) /* 0.500000000 */,
454   MAD_F(0x0a24c28c) /* 0.633974596 */,
455   MAD_F(0x0c9e69d1) /* 0.788675135 */,
456   MAD_F(0x10000000) /* 1.000000000 */
457 };
458  
459 /*
460  * coefficients for LSF intensity stereo processing
461  * derived from section 2.4.3.2 of ISO/IEC 13818-3
462  *
463  * is_lsf_table[0][i] = (1 / sqrt(sqrt(2)))^(i + 1)
464  * is_lsf_table[1][i] = (1 /      sqrt(2)) ^(i + 1)
465  */
466 static
467 mad_fixed_t const is_lsf_table[2][15] = {
468   {
469     MAD_F(0x0d744fcd) /* 0.840896415 */,
470     MAD_F(0x0b504f33) /* 0.707106781 */,
471     MAD_F(0x09837f05) /* 0.594603558 */,
472     MAD_F(0x08000000) /* 0.500000000 */,
473     MAD_F(0x06ba27e6) /* 0.420448208 */,
474     MAD_F(0x05a8279a) /* 0.353553391 */,
475     MAD_F(0x04c1bf83) /* 0.297301779 */,
476     MAD_F(0x04000000) /* 0.250000000 */,
477     MAD_F(0x035d13f3) /* 0.210224104 */,
478     MAD_F(0x02d413cd) /* 0.176776695 */,
479     MAD_F(0x0260dfc1) /* 0.148650889 */,
480     MAD_F(0x02000000) /* 0.125000000 */,
481     MAD_F(0x01ae89fa) /* 0.105112052 */,
482     MAD_F(0x016a09e6) /* 0.088388348 */,
483     MAD_F(0x01306fe1) /* 0.074325445 */
484   }, {
485     MAD_F(0x0b504f33) /* 0.707106781 */,
486     MAD_F(0x08000000) /* 0.500000000 */,
487     MAD_F(0x05a8279a) /* 0.353553391 */,
488     MAD_F(0x04000000) /* 0.250000000 */,
489     MAD_F(0x02d413cd) /* 0.176776695 */,
490     MAD_F(0x02000000) /* 0.125000000 */,
491     MAD_F(0x016a09e6) /* 0.088388348 */,
492     MAD_F(0x01000000) /* 0.062500000 */,
493     MAD_F(0x00b504f3) /* 0.044194174 */,
494     MAD_F(0x00800000) /* 0.031250000 */,
495     MAD_F(0x005a827a) /* 0.022097087 */,
496     MAD_F(0x00400000) /* 0.015625000 */,
497     MAD_F(0x002d413d) /* 0.011048543 */,
498     MAD_F(0x00200000) /* 0.007812500 */,
499     MAD_F(0x0016a09e) /* 0.005524272 */
500   }
501 };
502  
503 /*
504  * NAME:        III_sideinfo()
505  * DESCRIPTION: decode frame side information from a bitstream
506  */
507 static
508 enum mad_error III_sideinfo(struct mad_bitptr *ptr, unsigned int nch,
509                             int lsf, struct sideinfo *si,
510                             unsigned int *data_bitlen,
511                             unsigned int *priv_bitlen)
512 {
513   unsigned int ngr, gr, ch, i;
514   enum mad_error result = MAD_ERROR_NONE;
515  
516   *data_bitlen = 0;
517   *priv_bitlen = lsf ? ((nch == 1) ? 1 : 2) : ((nch == 1) ? 5 : 3);
518  
519   si->main_data_begin = mad_bit_read(ptr, lsf ? 8 : 9);
520   si->private_bits    = mad_bit_read(ptr, *priv_bitlen);
521  
522   ngr = 1;
523   if (!lsf) {
524     ngr = 2;
525  
526     for (ch = 0; ch < nch; ++ch)
527       si->scfsi[ch] = mad_bit_read(ptr, 4);
528   }
529  
530   for (gr = 0; gr < ngr; ++gr) {
531     struct granule *granule = &si->gr[gr];
532  
533     for (ch = 0; ch < nch; ++ch) {
534       struct channel *channel = &granule->ch[ch];
535  
536       channel->part2_3_length    = mad_bit_read(ptr, 12);
537       channel->big_values        = mad_bit_read(ptr, 9);
538       channel->global_gain       = mad_bit_read(ptr, 8);
539       channel->scalefac_compress = mad_bit_read(ptr, lsf ? 9 : 4);
540  
541       *data_bitlen += channel->part2_3_length;
542  
543       if (channel->big_values > 288 && result == 0)
544         result = MAD_ERROR_BADBIGVALUES;
545  
546       channel->flags = 0;
547  
548       /* window_switching_flag */
549       if (mad_bit_read(ptr, 1)) {
550         channel->block_type = mad_bit_read(ptr, 2);
551  
552         if (channel->block_type == 0 && result == 0)
553           result = MAD_ERROR_BADBLOCKTYPE;
554  
555         if (!lsf && channel->block_type == 2 && si->scfsi[ch] && result == 0)
556           result = MAD_ERROR_BADSCFSI;
557  
558         channel->region0_count = 7;
559         channel->region1_count = 36;
560  
561         if (mad_bit_read(ptr, 1))
562           channel->flags |= mixed_block_flag;
563         else if (channel->block_type == 2)
564           channel->region0_count = 8;
565  
566         for (i = 0; i < 2; ++i)
567           channel->table_select[i] = mad_bit_read(ptr, 5);
568  
26 roytam 569 # if defined(DEBUG)
14 roytam 570         channel->table_select[2] = 4;  /* not used */
571 # endif
572  
573         for (i = 0; i < 3; ++i)
574           channel->subblock_gain[i] = mad_bit_read(ptr, 3);
575       }
576       else {
577         channel->block_type = 0;
578  
579         for (i = 0; i < 3; ++i)
580           channel->table_select[i] = mad_bit_read(ptr, 5);
581  
582         channel->region0_count = mad_bit_read(ptr, 4);
583         channel->region1_count = mad_bit_read(ptr, 3);
584       }
585  
586       /* [preflag,] scalefac_scale, count1table_select */
587       channel->flags |= mad_bit_read(ptr, lsf ? 2 : 3);
588     }
589   }
590  
591   return result;
592 }
593  
594 /*
595  * NAME:        III_scalefactors_lsf()
596  * DESCRIPTION: decode channel scalefactors for LSF from a bitstream
597  */
598 static
599 unsigned int III_scalefactors_lsf(struct mad_bitptr *ptr,
600                                   struct channel *channel,
601                                   struct channel *gr1ch, int mode_extension)
602 {
603   struct mad_bitptr start;
604   unsigned int scalefac_compress, index, slen[4], part, n, i;
605   unsigned char const *nsfb;
606  
607   start = *ptr;
608  
609   scalefac_compress = channel->scalefac_compress;
610   index = (channel->block_type == 2) ?
611     ((channel->flags & mixed_block_flag) ? 2 : 1) : 0;
612  
613   if (!((mode_extension & I_STEREO) && gr1ch)) {
614     if (scalefac_compress < 400) {
615       slen[0] = (scalefac_compress >> 4) / 5;
616       slen[1] = (scalefac_compress >> 4) % 5;
617       slen[2] = (scalefac_compress % 16) >> 2;
618       slen[3] =  scalefac_compress %  4;
619  
620       nsfb = nsfb_table[0][index];
621     }
622     else if (scalefac_compress < 500) {
623       scalefac_compress -= 400;
624  
625       slen[0] = (scalefac_compress >> 2) / 5;
626       slen[1] = (scalefac_compress >> 2) % 5;
627       slen[2] =  scalefac_compress %  4;
628       slen[3] = 0;
629  
630       nsfb = nsfb_table[1][index];
631     }
632     else {
633       scalefac_compress -= 500;
634  
635       slen[0] = scalefac_compress / 3;
636       slen[1] = scalefac_compress % 3;
637       slen[2] = 0;
638       slen[3] = 0;
639  
640       channel->flags |= preflag;
641  
642       nsfb = nsfb_table[2][index];
643     }
644  
645     n = 0;
646     for (part = 0; part < 4; ++part) {
647       for (i = 0; i < nsfb[part]; ++i)
648         channel->scalefac[n++] = mad_bit_read(ptr, slen[part]);
649     }
650  
651     while (n < 39)
652       channel->scalefac[n++] = 0;
653   }
654   else {  /* (mode_extension & I_STEREO) && gr1ch (i.e. ch == 1) */
655     scalefac_compress >>= 1;
656  
657     if (scalefac_compress < 180) {
658       slen[0] =  scalefac_compress / 36;
659       slen[1] = (scalefac_compress % 36) / 6;
660       slen[2] = (scalefac_compress % 36) % 6;
661       slen[3] = 0;
662  
663       nsfb = nsfb_table[3][index];
664     }
665     else if (scalefac_compress < 244) {
666       scalefac_compress -= 180;
667  
668       slen[0] = (scalefac_compress % 64) >> 4;
669       slen[1] = (scalefac_compress % 16) >> 2;
670       slen[2] =  scalefac_compress %  4;
671       slen[3] = 0;
672  
673       nsfb = nsfb_table[4][index];
674     }
675     else {
676       scalefac_compress -= 244;
677  
678       slen[0] = scalefac_compress / 3;
679       slen[1] = scalefac_compress % 3;
680       slen[2] = 0;
681       slen[3] = 0;
682  
683       nsfb = nsfb_table[5][index];
684     }
685  
686     n = 0;
687     for (part = 0; part < 4; ++part) {
688       unsigned int max, is_pos;
689  
690       max = (1 << slen[part]) - 1;
691  
692       for (i = 0; i < nsfb[part]; ++i) {
693         is_pos = mad_bit_read(ptr, slen[part]);
694  
695         channel->scalefac[n] = is_pos;
696         gr1ch->scalefac[n++] = (is_pos == max);
697       }
698     }
699  
700     while (n < 39) {
701       channel->scalefac[n] = 0;
702       gr1ch->scalefac[n++] = 0;  /* apparently not illegal */
703     }
704   }
705  
706   return mad_bit_length(&start, ptr);
707 }
708  
709 /*
710  * NAME:        III_scalefactors()
711  * DESCRIPTION: decode channel scalefactors of one granule from a bitstream
712  */
713 static
714 unsigned int III_scalefactors(struct mad_bitptr *ptr, struct channel *channel,
715                               struct channel const *gr0ch, unsigned int scfsi)
716 {
717   struct mad_bitptr start;
718   unsigned int slen1, slen2, sfbi;
719  
720   start = *ptr;
721  
722   slen1 = sflen_table[channel->scalefac_compress].slen1;
723   slen2 = sflen_table[channel->scalefac_compress].slen2;
724  
725   if (channel->block_type == 2) {
726     unsigned int nsfb;
727  
728     sfbi = 0;
729  
730     nsfb = (channel->flags & mixed_block_flag) ? 8 + 3 * 3 : 6 * 3;
731     while (nsfb--)
732       channel->scalefac[sfbi++] = mad_bit_read(ptr, slen1);
733  
734     nsfb = 6 * 3;
735     while (nsfb--)
736       channel->scalefac[sfbi++] = mad_bit_read(ptr, slen2);
737  
738     nsfb = 1 * 3;
739     while (nsfb--)
740       channel->scalefac[sfbi++] = 0;
741   }
742   else {  /* channel->block_type != 2 */
743     if (scfsi & 0x8) {
744       for (sfbi = 0; sfbi < 6; ++sfbi)
745         channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
746     }
747     else {
748       for (sfbi = 0; sfbi < 6; ++sfbi)
749         channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
750     }
751  
752     if (scfsi & 0x4) {
753       for (sfbi = 6; sfbi < 11; ++sfbi)
754         channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
755     }
756     else {
757       for (sfbi = 6; sfbi < 11; ++sfbi)
758         channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
759     }
760  
761     if (scfsi & 0x2) {
762       for (sfbi = 11; sfbi < 16; ++sfbi)
763         channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
764     }
765     else {
766       for (sfbi = 11; sfbi < 16; ++sfbi)
767         channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
768     }
769  
770     if (scfsi & 0x1) {
771       for (sfbi = 16; sfbi < 21; ++sfbi)
772         channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
773     }
774     else {
775       for (sfbi = 16; sfbi < 21; ++sfbi)
776         channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
777     }
778  
779     channel->scalefac[21] = 0;
780   }
781  
782   return mad_bit_length(&start, ptr);
783 }
784  
785 /*
786  * The Layer III formula for requantization and scaling is defined by
787  * section 2.4.3.4.7.1 of ISO/IEC 11172-3, as follows:
788  *
789  *   long blocks:
790  *   xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
791  *           2^((1/4) * (global_gain - 210)) *
792  *           2^-(scalefac_multiplier *
793  *               (scalefac_l[sfb] + preflag * pretab[sfb]))
794  *
795  *   short blocks:
796  *   xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
797  *           2^((1/4) * (global_gain - 210 - 8 * subblock_gain[w])) *
798  *           2^-(scalefac_multiplier * scalefac_s[sfb][w])
799  *
800  *   where:
801  *   scalefac_multiplier = (scalefac_scale + 1) / 2
802  *
803  * The routines III_exponents() and III_requantize() facilitate this
804  * calculation.
805  */
806  
807 /*
808  * NAME:        III_exponents()
809  * DESCRIPTION: calculate scalefactor exponents
810  */
811 static
812 void III_exponents(struct channel const *channel,
813                    unsigned char const *sfbwidth, signed int exponents[39])
814 {
815   signed int gain;
816   unsigned int scalefac_multiplier, sfbi;
817  
818   gain = (signed int) channel->global_gain - 210;
819   scalefac_multiplier = (channel->flags & scalefac_scale) ? 2 : 1;
820  
821   if (channel->block_type == 2) {
822     unsigned int l;
823     signed int gain0, gain1, gain2;
824  
825     sfbi = l = 0;
826  
827     if (channel->flags & mixed_block_flag) {
828       unsigned int premask;
829  
830       premask = (channel->flags & preflag) ? ~0 : 0;
831  
832       /* long block subbands 0-1 */
833  
834       while (l < 36) {
835         exponents[sfbi] = gain -
836           (signed int) ((channel->scalefac[sfbi] + (pretab[sfbi] & premask)) <<
837                         scalefac_multiplier);
838  
839         l += sfbwidth[sfbi++];
840       }
841     }
842  
843     /* this is probably wrong for 8000 Hz short/mixed blocks */
844  
845     gain0 = gain - 8 * (signed int) channel->subblock_gain[0];
846     gain1 = gain - 8 * (signed int) channel->subblock_gain[1];
847     gain2 = gain - 8 * (signed int) channel->subblock_gain[2];
848  
849     while (l < 576) {
850       exponents[sfbi + 0] = gain0 -
851         (signed int) (channel->scalefac[sfbi + 0] << scalefac_multiplier);
852       exponents[sfbi + 1] = gain1 -
853         (signed int) (channel->scalefac[sfbi + 1] << scalefac_multiplier);
854       exponents[sfbi + 2] = gain2 -
855         (signed int) (channel->scalefac[sfbi + 2] << scalefac_multiplier);
856  
857       l    += 3 * sfbwidth[sfbi];
858       sfbi += 3;
859     }
860   }
861   else {  /* channel->block_type != 2 */
862     if (channel->flags & preflag) {
863       for (sfbi = 0; sfbi < 22; ++sfbi) {
864         exponents[sfbi] = gain -
865           (signed int) ((channel->scalefac[sfbi] + pretab[sfbi]) <<
866                         scalefac_multiplier);
867       }
868     }
869     else {
870       for (sfbi = 0; sfbi < 22; ++sfbi) {
871         exponents[sfbi] = gain -
872           (signed int) (channel->scalefac[sfbi] << scalefac_multiplier);
873       }
874     }
875   }
876 }
877  
878 /*
879  * NAME:        III_requantize()
880  * DESCRIPTION: requantize one (positive) value
881  */
882 static
883 mad_fixed_t III_requantize(unsigned int value, signed int exp)
884 {
885   mad_fixed_t requantized;
886   signed int frac;
887   struct fixedfloat const *power;
888  
889   frac = exp % 4;  /* assumes sign(frac) == sign(exp) */
890   exp /= 4;
891  
892   power = &rq_table[value];
893   requantized = power->mantissa;
894   exp += power->exponent;
895  
896   if (exp < 0) {
897     if (-exp >= sizeof(mad_fixed_t) * CHAR_BIT) {
898       /* underflow */
899       requantized = 0;
900     }
901     else {
902       requantized += 1L << (-exp - 1);
903       requantized >>= -exp;
904     }
905   }
906   else {
907     if (exp >= 5) {
908       /* overflow */
26 roytam 909 # if defined(DEBUG)
14 roytam 910       fprintf(stderr, "requantize overflow (%f * 2^%d)\n",
911               mad_f_todouble(requantized), exp);
912 # endif
913       requantized = MAD_F_MAX;
914     }
915     else
916       requantized <<= exp;
917   }
918  
919   return frac ? mad_f_mul(requantized, root_table[3 + frac]) : requantized;
920 }
921  
922 /* we must take care that sz >= bits and sz < sizeof(long) lest bits == 0 */
923 # define MASK(cache, sz, bits)  \
924     (((cache) >> ((sz) - (bits))) & ((1 << (bits)) - 1))
925 # define MASK1BIT(cache, sz)  \
926     ((cache) & (1 << ((sz) - 1)))
927  
928 /*
929  * NAME:        III_huffdecode()
930  * DESCRIPTION: decode Huffman code words of one channel of one granule
931  */
932 static
933 enum mad_error III_huffdecode(struct mad_bitptr *ptr, mad_fixed_t xr[576],
934                               struct channel *channel,
935                               unsigned char const *sfbwidth,
936                               unsigned int part2_length)
937 {
938   signed int exponents[39], exp;
939   signed int const *expptr;
940   struct mad_bitptr peek;
941   signed int bits_left, cachesz;
942   register mad_fixed_t *xrptr;
943   mad_fixed_t const *sfbound;
944   register unsigned long bitcache;
945  
946   bits_left = (signed) channel->part2_3_length - (signed) part2_length;
947   if (bits_left < 0)
948     return MAD_ERROR_BADPART3LEN;
949  
950   III_exponents(channel, sfbwidth, exponents);
951  
952   peek = *ptr;
953   mad_bit_skip(ptr, bits_left);
954  
955   /* align bit reads to byte boundaries */
956   cachesz  = mad_bit_bitsleft(&peek);
957   cachesz += ((32 - 1 - 24) + (24 - cachesz)) & ~7;
958  
959   bitcache   = mad_bit_read(&peek, cachesz);
960   bits_left -= cachesz;
961  
962   xrptr = &xr[0];
963  
964   /* big_values */
965   {
966     unsigned int region, rcount;
967     struct hufftable const *entry;
968     union huffpair const *table;
969     unsigned int linbits, startbits, big_values, reqhits;
970     mad_fixed_t reqcache[16];
971  
972     sfbound = xrptr + *sfbwidth++;
973     rcount  = channel->region0_count + 1;
974  
975     entry     = &mad_huff_pair_table[channel->table_select[region = 0]];
976     table     = entry->table;
977     linbits   = entry->linbits;
978     startbits = entry->startbits;
979  
980     if (table == 0)
981       return MAD_ERROR_BADHUFFTABLE;
982  
983     expptr  = &exponents[0];
984     exp     = *expptr++;
985     reqhits = 0;
986  
987     big_values = channel->big_values;
988  
989     while (big_values-- && cachesz + bits_left > 0) {
990       union huffpair const *pair;
991       unsigned int clumpsz, value;
992       register mad_fixed_t requantized;
993  
994       if (xrptr == sfbound) {
995         sfbound += *sfbwidth++;
996  
997         /* change table if region boundary */
998  
999         if (--rcount == 0) {
1000           if (region == 0)
1001             rcount = channel->region1_count + 1;
1002           else
1003             rcount = 0;  /* all remaining */
1004  
1005           entry     = &mad_huff_pair_table[channel->table_select[++region]];
1006           table     = entry->table;
1007           linbits   = entry->linbits;
1008           startbits = entry->startbits;
1009  
1010           if (table == 0)
1011             return MAD_ERROR_BADHUFFTABLE;
1012         }
1013  
1014         if (exp != *expptr) {
1015           exp = *expptr;
1016           reqhits = 0;
1017         }
1018  
1019         ++expptr;
1020       }
1021  
1022       if (cachesz < 21) {
1023         unsigned int bits;
1024  
1025         bits       = ((32 - 1 - 21) + (21 - cachesz)) & ~7;
1026         bitcache   = (bitcache << bits) | mad_bit_read(&peek, bits);
1027         cachesz   += bits;
1028         bits_left -= bits;
1029       }
1030  
1031       /* hcod (0..19) */
1032  
1033       clumpsz = startbits;
1034       pair    = &table[MASK(bitcache, cachesz, clumpsz)];
1035  
1036       while (!pair->final) {
1037         cachesz -= clumpsz;
1038  
1039         clumpsz = pair->ptr.bits;
1040         pair    = &table[pair->ptr.offset + MASK(bitcache, cachesz, clumpsz)];
1041       }
1042  
1043       cachesz -= pair->value.hlen;
1044  
1045       if (linbits) {
1046         /* x (0..14) */
1047  
1048         value = pair->value.x;
1049  
1050         switch (value) {
1051         case 0:
1052           xrptr[0] = 0;
1053           break;
1054  
1055         case 15:
1056           if (cachesz < linbits + 2) {
1057             bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16);
1058             cachesz   += 16;
1059             bits_left -= 16;
1060           }
1061  
1062           value += MASK(bitcache, cachesz, linbits);
1063           cachesz -= linbits;
1064  
1065           requantized = III_requantize(value, exp);
1066           goto x_final;
1067  
1068         default:
1069           if (reqhits & (1 << value))
1070             requantized = reqcache[value];
1071           else {
1072             reqhits |= (1 << value);
1073             requantized = reqcache[value] = III_requantize(value, exp);
1074           }
1075  
1076         x_final:
1077           xrptr[0] = MASK1BIT(bitcache, cachesz--) ?
1078             -requantized : requantized;
1079         }
1080  
1081         /* y (0..14) */
1082  
1083         value = pair->value.y;
1084  
1085         switch (value) {
1086         case 0:
1087           xrptr[1] = 0;
1088           break;
1089  
1090         case 15:
1091           if (cachesz < linbits + 1) {
1092             bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16);
1093             cachesz   += 16;
1094             bits_left -= 16;
1095           }
1096  
1097           value += MASK(bitcache, cachesz, linbits);
1098           cachesz -= linbits;
1099  
1100           requantized = III_requantize(value, exp);
1101           goto y_final;
1102  
1103         default:
1104           if (reqhits & (1 << value))
1105             requantized = reqcache[value];
1106           else {
1107             reqhits |= (1 << value);
1108             requantized = reqcache[value] = III_requantize(value, exp);
1109           }
1110  
1111         y_final:
1112           xrptr[1] = MASK1BIT(bitcache, cachesz--) ?
1113             -requantized : requantized;
1114         }
1115       }
1116       else {
1117         /* x (0..1) */
1118  
1119         value = pair->value.x;
1120  
1121         if (value == 0)
1122           xrptr[0] = 0;
1123         else {
1124           if (reqhits & (1 << value))
1125             requantized = reqcache[value];
1126           else {
1127             reqhits |= (1 << value);
1128             requantized = reqcache[value] = III_requantize(value, exp);
1129           }
1130  
1131           xrptr[0] = MASK1BIT(bitcache, cachesz--) ?
1132             -requantized : requantized;
1133         }
1134  
1135         /* y (0..1) */
1136  
1137         value = pair->value.y;
1138  
1139         if (value == 0)
1140           xrptr[1] = 0;
1141         else {
1142           if (reqhits & (1 << value))
1143             requantized = reqcache[value];
1144           else {
1145             reqhits |= (1 << value);
1146             requantized = reqcache[value] = III_requantize(value, exp);
1147           }
1148  
1149           xrptr[1] = MASK1BIT(bitcache, cachesz--) ?
1150             -requantized : requantized;
1151         }
1152       }
1153  
1154       xrptr += 2;
1155     }
1156   }
1157  
1158   if (cachesz + bits_left < 0)
1159     return MAD_ERROR_BADHUFFDATA;  /* big_values overrun */
1160  
1161   /* count1 */
1162   {
1163     union huffquad const *table;
1164     register mad_fixed_t requantized;
1165  
1166     table = mad_huff_quad_table[channel->flags & count1table_select];
1167  
1168     requantized = III_requantize(1, exp);
1169  
1170     while (cachesz + bits_left > 0 && xrptr <= &xr[572]) {
1171       union huffquad const *quad;
1172  
1173       /* hcod (1..6) */
1174  
1175       if (cachesz < 10) {
1176         bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16);
1177         cachesz   += 16;
1178         bits_left -= 16;
1179       }
1180  
1181       quad = &table[MASK(bitcache, cachesz, 4)];
1182  
1183       /* quad tables guaranteed to have at most one extra lookup */
1184       if (!quad->final) {
1185         cachesz -= 4;
1186  
1187         quad = &table[quad->ptr.offset +
1188                       MASK(bitcache, cachesz, quad->ptr.bits)];
1189       }
1190  
1191       cachesz -= quad->value.hlen;
1192  
1193       if (xrptr == sfbound) {
1194         sfbound += *sfbwidth++;
1195  
1196         if (exp != *expptr) {
1197           exp = *expptr;
1198           requantized = III_requantize(1, exp);
1199         }
1200  
1201         ++expptr;
1202       }
1203  
1204       /* v (0..1) */
1205  
1206       xrptr[0] = quad->value.v ?
1207         (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
1208  
1209       /* w (0..1) */
1210  
1211       xrptr[1] = quad->value.w ?
1212         (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
1213  
1214       xrptr += 2;
1215  
1216       if (xrptr == sfbound) {
1217         sfbound += *sfbwidth++;
1218  
1219         if (exp != *expptr) {
1220           exp = *expptr;
1221           requantized = III_requantize(1, exp);
1222         }
1223  
1224         ++expptr;
1225       }
1226  
1227       /* x (0..1) */
1228  
1229       xrptr[0] = quad->value.x ?
1230         (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
1231  
1232       /* y (0..1) */
1233  
1234       xrptr[1] = quad->value.y ?
1235         (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
1236  
1237       xrptr += 2;
1238     }
1239  
1240     if (cachesz + bits_left < 0) {
1241 # if 0 && defined(DEBUG)
1242       fprintf(stderr, "huffman count1 overrun (%d bits)\n",
1243               -(cachesz + bits_left));
1244 # endif
1245  
1246       /* technically the bitstream is misformatted, but apparently
1247          some encoders are just a bit sloppy with stuffing bits */
1248  
1249       xrptr -= 4;
1250     }
1251   }
1252  
1253   assert(-bits_left <= MAD_BUFFER_GUARD * CHAR_BIT);
1254  
1255 # if 0 && defined(DEBUG)
1256   if (bits_left < 0)
1257     fprintf(stderr, "read %d bits too many\n", -bits_left);
1258   else if (cachesz + bits_left > 0)
1259     fprintf(stderr, "%d stuffing bits\n", cachesz + bits_left);
1260 # endif
1261  
1262   /* rzero */
1263   while (xrptr < &xr[576]) {
1264     xrptr[0] = 0;
1265     xrptr[1] = 0;
1266  
1267     xrptr += 2;
1268   }
1269  
1270   return MAD_ERROR_NONE;
1271 }
1272  
1273 # undef MASK
1274 # undef MASK1BIT
1275  
1276 /*
1277  * NAME:        III_reorder()
1278  * DESCRIPTION: reorder frequency lines of a short block into subband order
1279  */
1280 static
1281 void III_reorder(mad_fixed_t xr[576], struct channel const *channel,
1282                  unsigned char const sfbwidth[39])
1283 {
1284   mad_fixed_t tmp[32][3][6];
1285   unsigned int sb, l, f, w, sbw[3], sw[3];
1286  
1287   /* this is probably wrong for 8000 Hz mixed blocks */
1288  
1289   sb = 0;
1290   if (channel->flags & mixed_block_flag) {
1291     sb = 2;
1292  
1293     l = 0;
1294     while (l < 36)
1295       l += *sfbwidth++;
1296   }
1297  
1298   for (w = 0; w < 3; ++w) {
1299     sbw[w] = sb;
1300     sw[w]  = 0;
1301   }
1302  
1303   f = *sfbwidth++;
1304   w = 0;
1305  
1306   for (l = 18 * sb; l < 576; ++l) {
1307     if (f-- == 0) {
1308       f = *sfbwidth++ - 1;
1309       w = (w + 1) % 3;
1310     }
1311  
1312     tmp[sbw[w]][w][sw[w]++] = xr[l];
1313  
1314     if (sw[w] == 6) {
1315       sw[w] = 0;
1316       ++sbw[w];
1317     }
1318   }
1319  
1320   memcpy(&xr[18 * sb], &tmp[sb], (576 - 18 * sb) * sizeof(mad_fixed_t));
1321 }
1322  
1323 /*
1324  * NAME:        III_stereo()
1325  * DESCRIPTION: perform joint stereo processing on a granule
1326  */
1327 static
1328 enum mad_error III_stereo(mad_fixed_t xr[2][576],
1329                           struct granule const *granule,
1330                           struct mad_header *header,
1331                           unsigned char const *sfbwidth)
1332 {
1333   short modes[39];
1334   unsigned int sfbi, l, n, i;
1335  
1336   if (granule->ch[0].block_type !=
1337       granule->ch[1].block_type ||
1338       (granule->ch[0].flags & mixed_block_flag) !=
1339       (granule->ch[1].flags & mixed_block_flag))
1340     return MAD_ERROR_BADSTEREO;
1341  
1342   for (i = 0; i < 39; ++i)
1343     modes[i] = header->mode_extension;
1344  
1345   /* intensity stereo */
1346  
1347   if (header->mode_extension & I_STEREO) {
1348     struct channel const *right_ch = &granule->ch[1];
1349     mad_fixed_t const *right_xr = xr[1];
1350     unsigned int is_pos;
1351  
1352     header->flags |= MAD_FLAG_I_STEREO;
1353  
1354     /* first determine which scalefactor bands are to be processed */
1355  
1356     if (right_ch->block_type == 2) {
1357       unsigned int lower, start, max, bound[3], w;
1358  
1359       lower = start = max = bound[0] = bound[1] = bound[2] = 0;
1360  
1361       sfbi = l = 0;
1362  
1363       if (right_ch->flags & mixed_block_flag) {
1364         while (l < 36) {
1365           n = sfbwidth[sfbi++];
1366  
1367           for (i = 0; i < n; ++i) {
1368             if (right_xr[i]) {
1369               lower = sfbi;
1370               break;
1371             }
1372           }
1373  
1374           right_xr += n;
1375           l += n;
1376         }
1377  
1378         start = sfbi;
1379       }
1380  
1381       w = 0;
1382       while (l < 576) {
1383         n = sfbwidth[sfbi++];
1384  
1385         for (i = 0; i < n; ++i) {
1386           if (right_xr[i]) {
1387             max = bound[w] = sfbi;
1388             break;
1389           }
1390         }
1391  
1392         right_xr += n;
1393         l += n;
1394         w = (w + 1) % 3;
1395       }
1396  
1397       if (max)
1398         lower = start;
1399  
1400       /* long blocks */
1401  
1402       for (i = 0; i < lower; ++i)
1403         modes[i] = header->mode_extension & ~I_STEREO;
1404  
1405       /* short blocks */
1406  
1407       w = 0;
1408       for (i = start; i < max; ++i) {
1409         if (i < bound[w])
1410           modes[i] = header->mode_extension & ~I_STEREO;
1411  
1412         w = (w + 1) % 3;
1413       }
1414     }
1415     else {  /* right_ch->block_type != 2 */
1416       unsigned int bound;
1417  
1418       bound = 0;
1419       for (sfbi = l = 0; l < 576; l += n) {
1420         n = sfbwidth[sfbi++];
1421  
1422         for (i = 0; i < n; ++i) {
1423           if (right_xr[i]) {
1424             bound = sfbi;
1425             break;
1426           }
1427         }
1428  
1429         right_xr += n;
1430       }
1431  
1432       for (i = 0; i < bound; ++i)
1433         modes[i] = header->mode_extension & ~I_STEREO;
1434     }
1435  
1436     /* now do the actual processing */
1437  
1438     if (header->flags & MAD_FLAG_LSF_EXT) {
1439       unsigned char const *illegal_pos = granule[1].ch[1].scalefac;
1440       mad_fixed_t const *lsf_scale;
1441  
1442       /* intensity_scale */
1443       lsf_scale = is_lsf_table[right_ch->scalefac_compress & 0x1];
1444  
1445       for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
1446         n = sfbwidth[sfbi];
1447  
1448         if (!(modes[sfbi] & I_STEREO))
1449           continue;
1450  
1451         if (illegal_pos[sfbi]) {
1452           modes[sfbi] &= ~I_STEREO;
1453           continue;
1454         }
1455  
1456         is_pos = right_ch->scalefac[sfbi];
1457  
1458         for (i = 0; i < n; ++i) {
1459           register mad_fixed_t left;
1460  
1461           left = xr[0][l + i];
1462  
1463           if (is_pos == 0)
1464             xr[1][l + i] = left;
1465           else {
1466             register mad_fixed_t opposite;
1467  
1468             opposite = mad_f_mul(left, lsf_scale[(is_pos - 1) / 2]);
1469  
1470             if (is_pos & 1) {
1471               xr[0][l + i] = opposite;
1472               xr[1][l + i] = left;
1473             }
1474             else
1475               xr[1][l + i] = opposite;
1476           }
1477         }
1478       }
1479     }
1480     else {  /* !(header->flags & MAD_FLAG_LSF_EXT) */
1481       for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
1482         n = sfbwidth[sfbi];
1483  
1484         if (!(modes[sfbi] & I_STEREO))
1485           continue;
1486  
1487         is_pos = right_ch->scalefac[sfbi];
1488  
1489         if (is_pos >= 7) {  /* illegal intensity position */
1490           modes[sfbi] &= ~I_STEREO;
1491           continue;
1492         }
1493  
1494         for (i = 0; i < n; ++i) {
1495           register mad_fixed_t left;
1496  
1497           left = xr[0][l + i];
1498  
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]);
1501         }
1502       }
1503     }
1504   }
1505  
1506   /* middle/side stereo */
1507  
1508   if (header->mode_extension & MS_STEREO) {
1509     register mad_fixed_t invsqrt2;
1510  
1511     header->flags |= MAD_FLAG_MS_STEREO;
1512  
1513     invsqrt2 = root_table[3 + -2];
1514  
1515     for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
1516       n = sfbwidth[sfbi];
1517  
1518       if (modes[sfbi] != MS_STEREO)
1519         continue;
1520  
1521       for (i = 0; i < n; ++i) {
1522         register mad_fixed_t m, s;
1523  
1524         m = xr[0][l + i];
1525         s = xr[1][l + i];
1526  
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) */
1529       }
1530     }
1531   }
1532  
1533   return MAD_ERROR_NONE;
1534 }
1535  
1536 /*
1537  * NAME:        III_aliasreduce()
1538  * DESCRIPTION: perform frequency line alias reduction
1539  */
1540 static
1541 void III_aliasreduce(mad_fixed_t xr[576], int lines)
1542 {
1543   mad_fixed_t const *bound;
1544   int i;
1545  
1546   bound = &xr[lines];
1547   for (xr += 18; xr < bound; xr += 18) {
1548     for (i = 0; i < 8; ++i) {
1549       register mad_fixed_t a, b;
1550       register mad_fixed64hi_t hi;
1551       register mad_fixed64lo_t lo;
1552  
1553       a = xr[-1 - i];
1554       b = xr[     i];
1555  
1556 # if defined(ASO_ZEROCHECK)
1557       if (a | b) {
1558 # endif
1559         MAD_F_ML0(hi, lo,  a, cs[i]);
1560         MAD_F_MLA(hi, lo, -b, ca[i]);
1561  
1562         xr[-1 - i] = MAD_F_MLZ(hi, lo);
1563  
1564         MAD_F_ML0(hi, lo,  b, cs[i]);
1565         MAD_F_MLA(hi, lo,  a, ca[i]);
1566  
1567         xr[     i] = MAD_F_MLZ(hi, lo);
1568 # if defined(ASO_ZEROCHECK)
1569       }
1570 # endif
1571     }
1572   }
1573 }
1574  
1575 # if defined(ASO_IMDCT)
1576 void III_imdct_l(mad_fixed_t const [18], mad_fixed_t [36], unsigned int);
1577 # else
26 roytam 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  
14 roytam 1732 /*
1733  * NAME:        imdct36
26 roytam 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 - (9 + (i + 2)) - 1];
1757   }
1758   for (i = 27; i < 36; i += 3) {
1759     y[i + 0] = -tmp[(i + 0) - 27];
1760     y[i + 1] = -tmp[(i + 1) - 27];
1761     y[i + 2] = -tmp[(i + 2) - 27];
1762   }
1763 }
1764 #  else
1765 /*
1766  * NAME:        imdct36
14 roytam 1767  * DESCRIPTION: perform X[18]->x[36] IMDCT
1768  */
26 roytam 1769 static inline
14 roytam 1770 void imdct36(mad_fixed_t const X[18], mad_fixed_t x[36])
1771 {
1772   mad_fixed_t t0, t1, t2,  t3,  t4,  t5,  t6,  t7;
1773   mad_fixed_t t8, t9, t10, t11, t12, t13, t14, t15;
1774   register mad_fixed64hi_t hi;
1775   register mad_fixed64lo_t lo;
1776  
1777   MAD_F_ML0(hi, lo, X[4],  MAD_F(0x0ec835e8));
1778   MAD_F_MLA(hi, lo, X[13], MAD_F(0x061f78aa));
1779  
1780   t6 = MAD_F_MLZ(hi, lo);
1781  
1782   MAD_F_MLA(hi, lo, (t14 = X[1] - X[10]), -MAD_F(0x061f78aa));
1783   MAD_F_MLA(hi, lo, (t15 = X[7] + X[16]), -MAD_F(0x0ec835e8));
1784  
1785   t0 = MAD_F_MLZ(hi, lo);
1786  
1787   MAD_F_MLA(hi, lo, (t8  = X[0] - X[11] - X[12]),  MAD_F(0x0216a2a2));
1788   MAD_F_MLA(hi, lo, (t9  = X[2] - X[9]  - X[14]),  MAD_F(0x09bd7ca0));
1789   MAD_F_MLA(hi, lo, (t10 = X[3] - X[8]  - X[15]), -MAD_F(0x0cb19346));
1790   MAD_F_MLA(hi, lo, (t11 = X[5] - X[6]  - X[17]), -MAD_F(0x0fdcf549));
1791  
1792   x[7]  = MAD_F_MLZ(hi, lo);
1793   x[10] = -x[7];
1794  
1795   MAD_F_ML0(hi, lo, t8,  -MAD_F(0x0cb19346));
1796   MAD_F_MLA(hi, lo, t9,   MAD_F(0x0fdcf549));
1797   MAD_F_MLA(hi, lo, t10,  MAD_F(0x0216a2a2));
1798   MAD_F_MLA(hi, lo, t11, -MAD_F(0x09bd7ca0));
1799  
1800   x[19] = x[34] = MAD_F_MLZ(hi, lo) - t0;
1801  
1802   t12 = X[0] - X[3] + X[8] - X[11] - X[12] + X[15];
1803   t13 = X[2] + X[5] - X[6] - X[9]  - X[14] - X[17];
1804  
1805   MAD_F_ML0(hi, lo, t12, -MAD_F(0x0ec835e8));
1806   MAD_F_MLA(hi, lo, t13,  MAD_F(0x061f78aa));
1807  
1808   x[22] = x[31] = MAD_F_MLZ(hi, lo) + t0;
1809  
1810   MAD_F_ML0(hi, lo, X[1],  -MAD_F(0x09bd7ca0));
1811   MAD_F_MLA(hi, lo, X[7],   MAD_F(0x0216a2a2));
1812   MAD_F_MLA(hi, lo, X[10], -MAD_F(0x0fdcf549));
1813   MAD_F_MLA(hi, lo, X[16],  MAD_F(0x0cb19346));
1814  
1815   t1 = MAD_F_MLZ(hi, lo) + t6;
1816  
1817   MAD_F_ML0(hi, lo, X[0],   MAD_F(0x03768962));
1818   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0e313245));
1819   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x0ffc19fd));
1820   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0acf37ad));
1821   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x04cfb0e2));
1822   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x0898c779));
1823   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x0d7e8807));
1824   MAD_F_MLA(hi, lo, X[11],  MAD_F(0x0f426cb5));
1825   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0bcbe352));
1826   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x00b2aa3e));
1827   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x07635284));
1828   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0f9ee890));
1829  
1830   x[6]  = MAD_F_MLZ(hi, lo) + t1;
1831   x[11] = -x[6];
1832  
1833   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0f426cb5));
1834   MAD_F_MLA(hi, lo, X[2],  -MAD_F(0x00b2aa3e));
1835   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0898c779));
1836   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x0f9ee890));
1837   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0acf37ad));
1838   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x07635284));
1839   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x0e313245));
1840   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0bcbe352));
1841   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x03768962));
1842   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0d7e8807));
1843   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0ffc19fd));
1844   MAD_F_MLA(hi, lo, X[17],  MAD_F(0x04cfb0e2));
1845  
1846   x[23] = x[30] = MAD_F_MLZ(hi, lo) + t1;
1847  
1848   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0bcbe352));
1849   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0d7e8807));
1850   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x07635284));
1851   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x04cfb0e2));
1852   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0f9ee890));
1853   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x0ffc19fd));
1854   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x00b2aa3e));
1855   MAD_F_MLA(hi, lo, X[11],  MAD_F(0x03768962));
1856   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0f426cb5));
1857   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0e313245));
1858   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0898c779));
1859   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0acf37ad));
1860  
1861   x[18] = x[35] = MAD_F_MLZ(hi, lo) - t1;
1862  
1863   MAD_F_ML0(hi, lo, X[4],   MAD_F(0x061f78aa));
1864   MAD_F_MLA(hi, lo, X[13], -MAD_F(0x0ec835e8));
1865  
1866   t7 = MAD_F_MLZ(hi, lo);
1867  
1868   MAD_F_MLA(hi, lo, X[1],  -MAD_F(0x0cb19346));
1869   MAD_F_MLA(hi, lo, X[7],   MAD_F(0x0fdcf549));
1870   MAD_F_MLA(hi, lo, X[10],  MAD_F(0x0216a2a2));
1871   MAD_F_MLA(hi, lo, X[16], -MAD_F(0x09bd7ca0));
1872  
1873   t2 = MAD_F_MLZ(hi, lo);
1874  
1875   MAD_F_MLA(hi, lo, X[0],   MAD_F(0x04cfb0e2));
1876   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0ffc19fd));
1877   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x0d7e8807));
1878   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x03768962));
1879   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x0bcbe352));
1880   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x0e313245));
1881   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x07635284));
1882   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0acf37ad));
1883   MAD_F_MLA(hi, lo, X[12],  MAD_F(0x0f9ee890));
1884   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0898c779));
1885   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x00b2aa3e));
1886   MAD_F_MLA(hi, lo, X[17],  MAD_F(0x0f426cb5));
1887  
1888   x[5]  = MAD_F_MLZ(hi, lo);
1889   x[12] = -x[5];
1890  
1891   MAD_F_ML0(hi, lo, X[0],   MAD_F(0x0acf37ad));
1892   MAD_F_MLA(hi, lo, X[2],  -MAD_F(0x0898c779));
1893   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0e313245));
1894   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0f426cb5));
1895   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x03768962));
1896   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x00b2aa3e));
1897   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x0ffc19fd));
1898   MAD_F_MLA(hi, lo, X[11],  MAD_F(0x0f9ee890));
1899   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x04cfb0e2));
1900   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x07635284));
1901   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0d7e8807));
1902   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0bcbe352));
1903  
1904   x[0]  = MAD_F_MLZ(hi, lo) + t2;
1905   x[17] = -x[0];
1906  
1907   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0f9ee890));
1908   MAD_F_MLA(hi, lo, X[2],  -MAD_F(0x07635284));
1909   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x00b2aa3e));
1910   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x0bcbe352));
1911   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0f426cb5));
1912   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x0d7e8807));
1913   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x0898c779));
1914   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x04cfb0e2));
1915   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0acf37ad));
1916   MAD_F_MLA(hi, lo, X[14], -MAD_F(0x0ffc19fd));
1917   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0e313245));
1918   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x03768962));
1919  
1920   x[24] = x[29] = MAD_F_MLZ(hi, lo) + t2;
1921  
1922   MAD_F_ML0(hi, lo, X[1],  -MAD_F(0x0216a2a2));
1923   MAD_F_MLA(hi, lo, X[7],  -MAD_F(0x09bd7ca0));
1924   MAD_F_MLA(hi, lo, X[10],  MAD_F(0x0cb19346));
1925   MAD_F_MLA(hi, lo, X[16],  MAD_F(0x0fdcf549));
1926  
1927   t3 = MAD_F_MLZ(hi, lo) + t7;
1928  
1929   MAD_F_ML0(hi, lo, X[0],   MAD_F(0x00b2aa3e));
1930   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x03768962));
1931   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x04cfb0e2));
1932   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x07635284));
1933   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0898c779));
1934   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x0acf37ad));
1935   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x0bcbe352));
1936   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0d7e8807));
1937   MAD_F_MLA(hi, lo, X[12],  MAD_F(0x0e313245));
1938   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0f426cb5));
1939   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0f9ee890));
1940   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0ffc19fd));
1941  
1942   x[8] = MAD_F_MLZ(hi, lo) + t3;
1943   x[9] = -x[8];
1944  
1945   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0e313245));
1946   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0bcbe352));
1947   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0f9ee890));
1948   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0898c779));
1949   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x0ffc19fd));
1950   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x04cfb0e2));
1951   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x0f426cb5));
1952   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x00b2aa3e));
1953   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0d7e8807));
1954   MAD_F_MLA(hi, lo, X[14], -MAD_F(0x03768962));
1955   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0acf37ad));
1956   MAD_F_MLA(hi, lo, X[17],  MAD_F(0x07635284));
1957  
1958   x[21] = x[32] = MAD_F_MLZ(hi, lo) + t3;
1959  
1960   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0d7e8807));
1961   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0f426cb5));
1962   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0acf37ad));
1963   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0ffc19fd));
1964   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x07635284));
1965   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x0f9ee890));
1966   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x03768962));
1967   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0e313245));
1968   MAD_F_MLA(hi, lo, X[12],  MAD_F(0x00b2aa3e));
1969   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0bcbe352));
1970   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x04cfb0e2));
1971   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0898c779));
1972  
1973   x[20] = x[33] = MAD_F_MLZ(hi, lo) - t3;
1974  
1975   MAD_F_ML0(hi, lo, t14, -MAD_F(0x0ec835e8));
1976   MAD_F_MLA(hi, lo, t15,  MAD_F(0x061f78aa));
1977  
1978   t4 = MAD_F_MLZ(hi, lo) - t7;
1979  
1980   MAD_F_ML0(hi, lo, t12, MAD_F(0x061f78aa));
1981   MAD_F_MLA(hi, lo, t13, MAD_F(0x0ec835e8));
1982  
1983   x[4]  = MAD_F_MLZ(hi, lo) + t4;
1984   x[13] = -x[4];
1985  
1986   MAD_F_ML0(hi, lo, t8,   MAD_F(0x09bd7ca0));
1987   MAD_F_MLA(hi, lo, t9,  -MAD_F(0x0216a2a2));
1988   MAD_F_MLA(hi, lo, t10,  MAD_F(0x0fdcf549));
1989   MAD_F_MLA(hi, lo, t11, -MAD_F(0x0cb19346));
1990  
1991   x[1]  = MAD_F_MLZ(hi, lo) + t4;
1992   x[16] = -x[1];
1993  
1994   MAD_F_ML0(hi, lo, t8,  -MAD_F(0x0fdcf549));
1995   MAD_F_MLA(hi, lo, t9,  -MAD_F(0x0cb19346));
1996   MAD_F_MLA(hi, lo, t10, -MAD_F(0x09bd7ca0));
1997   MAD_F_MLA(hi, lo, t11, -MAD_F(0x0216a2a2));
1998  
1999   x[25] = x[28] = MAD_F_MLZ(hi, lo) + t4;
2000  
2001   MAD_F_ML0(hi, lo, X[1],  -MAD_F(0x0fdcf549));
2002   MAD_F_MLA(hi, lo, X[7],  -MAD_F(0x0cb19346));
2003   MAD_F_MLA(hi, lo, X[10], -MAD_F(0x09bd7ca0));
2004   MAD_F_MLA(hi, lo, X[16], -MAD_F(0x0216a2a2));
2005  
2006   t5 = MAD_F_MLZ(hi, lo) - t6;
2007  
2008   MAD_F_ML0(hi, lo, X[0],   MAD_F(0x0898c779));
2009   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x04cfb0e2));
2010   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0bcbe352));
2011   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x00b2aa3e));
2012   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0e313245));
2013   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x03768962));
2014   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x0f9ee890));
2015   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x07635284));
2016   MAD_F_MLA(hi, lo, X[12],  MAD_F(0x0ffc19fd));
2017   MAD_F_MLA(hi, lo, X[14], -MAD_F(0x0acf37ad));
2018   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0f426cb5));
2019   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0d7e8807));
2020  
2021   x[2]  = MAD_F_MLZ(hi, lo) + t5;
2022   x[15] = -x[2];
2023  
2024   MAD_F_ML0(hi, lo, X[0],   MAD_F(0x07635284));
2025   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0acf37ad));
2026   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x03768962));
2027   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x0d7e8807));
2028   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x00b2aa3e));
2029   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x0f426cb5));
2030   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x04cfb0e2));
2031   MAD_F_MLA(hi, lo, X[11],  MAD_F(0x0ffc19fd));
2032   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0898c779));
2033   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0f9ee890));
2034   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0bcbe352));
2035   MAD_F_MLA(hi, lo, X[17],  MAD_F(0x0e313245));
2036  
2037   x[3]  = MAD_F_MLZ(hi, lo) + t5;
2038   x[14] = -x[3];
2039  
2040   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0ffc19fd));
2041   MAD_F_MLA(hi, lo, X[2],  -MAD_F(0x0f9ee890));
2042   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x0f426cb5));
2043   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0e313245));
2044   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x0d7e8807));
2045   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x0bcbe352));
2046   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x0acf37ad));
2047   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0898c779));
2048   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x07635284));
2049   MAD_F_MLA(hi, lo, X[14], -MAD_F(0x04cfb0e2));
2050   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x03768962));
2051   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x00b2aa3e));
2052  
2053   x[26] = x[27] = MAD_F_MLZ(hi, lo) + t5;
2054 }
26 roytam 2055 #  endif
14 roytam 2056  
2057 /*
2058  * NAME:        III_imdct_l()
2059  * DESCRIPTION: perform IMDCT and windowing for long blocks
2060  */
2061 static
2062 void III_imdct_l(mad_fixed_t const X[18], mad_fixed_t z[36],
2063                  unsigned int block_type)
2064 {
2065   unsigned int i;
2066  
2067   /* IMDCT */
2068  
2069   imdct36(X, z);
2070  
2071   /* windowing */
2072  
2073   switch (block_type) {
2074   case 0:  /* normal window */
2075 # if defined(ASO_INTERLEAVE1)
2076     {
2077       register mad_fixed_t tmp1, tmp2;
2078  
2079       tmp1 = window_l[0];
2080       tmp2 = window_l[1];
2081  
2082       for (i = 0; i < 34; i += 2) {
2083         z[i + 0] = mad_f_mul(z[i + 0], tmp1);
2084         tmp1 = window_l[i + 2];
2085         z[i + 1] = mad_f_mul(z[i + 1], tmp2);
2086         tmp2 = window_l[i + 3];
2087       }
2088  
2089       z[34] = mad_f_mul(z[34], tmp1);
2090       z[35] = mad_f_mul(z[35], tmp2);
2091     }
2092 # elif defined(ASO_INTERLEAVE2)
2093     {
2094       register mad_fixed_t tmp1, tmp2;
2095  
2096       tmp1 = z[0];
2097       tmp2 = window_l[0];
2098  
2099       for (i = 0; i < 35; ++i) {
2100         z[i] = mad_f_mul(tmp1, tmp2);
2101         tmp1 = z[i + 1];
2102         tmp2 = window_l[i + 1];
2103       }
2104  
2105       z[35] = mad_f_mul(tmp1, tmp2);
2106     }
2107 # elif 1
2108     for (i = 0; i < 36; i += 4) {
2109       z[i + 0] = mad_f_mul(z[i + 0], window_l[i + 0]);
2110       z[i + 1] = mad_f_mul(z[i + 1], window_l[i + 1]);
2111       z[i + 2] = mad_f_mul(z[i + 2], window_l[i + 2]);
2112       z[i + 3] = mad_f_mul(z[i + 3], window_l[i + 3]);
2113     }
2114 # else
2115     for (i =  0; i < 36; ++i) z[i] = mad_f_mul(z[i], window_l[i]);
2116 # endif
2117     break;
2118  
2119   case 1:  /* start block */
26 roytam 2120     for (i =  0; i < 18; i += 3) {
2121       z[i + 0] = mad_f_mul(z[i + 0], window_l[i + 0]);
2122       z[i + 1] = mad_f_mul(z[i + 1], window_l[i + 1]);
2123       z[i + 2] = mad_f_mul(z[i + 2], window_l[i + 2]);
2124     }
14 roytam 2125     /*  (i = 18; i < 24; ++i) z[i] unchanged */
2126     for (i = 24; i < 30; ++i) z[i] = mad_f_mul(z[i], window_s[i - 18]);
2127     for (i = 30; i < 36; ++i) z[i] = 0;
2128     break;
2129  
2130   case 3:  /* stop block */
2131     for (i =  0; i <  6; ++i) z[i] = 0;
2132     for (i =  6; i < 12; ++i) z[i] = mad_f_mul(z[i], window_s[i - 6]);
2133     /*  (i = 12; i < 18; ++i) z[i] unchanged */
26 roytam 2134     for (i = 18; i < 36; i += 3) {
2135       z[i + 0] = mad_f_mul(z[i + 0], window_l[i + 0]);
2136       z[i + 1] = mad_f_mul(z[i + 1], window_l[i + 1]);
2137       z[i + 2] = mad_f_mul(z[i + 2], window_l[i + 2]);
2138     }
14 roytam 2139     break;
2140   }
2141 }
2142 # endif  /* ASO_IMDCT */
2143  
2144 /*
2145  * NAME:        III_imdct_s()
2146  * DESCRIPTION: perform IMDCT and windowing for short blocks
2147  */
2148 static
2149 void III_imdct_s(mad_fixed_t const X[18], mad_fixed_t z[36])
2150 {
2151   mad_fixed_t y[36], *yptr;
2152   mad_fixed_t const *wptr;
2153   int w, i;
2154   register mad_fixed64hi_t hi;
2155   register mad_fixed64lo_t lo;
2156  
2157   /* IMDCT */
2158  
2159   yptr = &y[0];
2160  
2161   for (w = 0; w < 3; ++w) {
2162     register mad_fixed_t const (*s)[6];
2163  
2164     s = imdct_s;
2165  
2166     for (i = 0; i < 3; ++i) {
2167       MAD_F_ML0(hi, lo, X[0], (*s)[0]);
2168       MAD_F_MLA(hi, lo, X[1], (*s)[1]);
2169       MAD_F_MLA(hi, lo, X[2], (*s)[2]);
2170       MAD_F_MLA(hi, lo, X[3], (*s)[3]);
2171       MAD_F_MLA(hi, lo, X[4], (*s)[4]);
2172       MAD_F_MLA(hi, lo, X[5], (*s)[5]);
2173  
2174       yptr[i + 0] = MAD_F_MLZ(hi, lo);
2175       yptr[5 - i] = -yptr[i + 0];
2176  
2177       ++s;
2178  
2179       MAD_F_ML0(hi, lo, X[0], (*s)[0]);
2180       MAD_F_MLA(hi, lo, X[1], (*s)[1]);
2181       MAD_F_MLA(hi, lo, X[2], (*s)[2]);
2182       MAD_F_MLA(hi, lo, X[3], (*s)[3]);
2183       MAD_F_MLA(hi, lo, X[4], (*s)[4]);
2184       MAD_F_MLA(hi, lo, X[5], (*s)[5]);
2185  
2186       yptr[ i + 6] = MAD_F_MLZ(hi, lo);
2187       yptr[11 - i] = yptr[i + 6];
2188  
2189       ++s;
2190     }
2191  
2192     yptr += 12;
2193     X    += 6;
2194   }
2195  
2196   /* windowing, overlapping and concatenation */
2197  
2198   yptr = &y[0];
2199   wptr = &window_s[0];
2200  
2201   for (i = 0; i < 6; ++i) {
2202     z[i +  0] = 0;
2203     z[i +  6] = mad_f_mul(yptr[ 0 + 0], wptr[0]);
2204  
2205     MAD_F_ML0(hi, lo, yptr[ 0 + 6], wptr[6]);
2206     MAD_F_MLA(hi, lo, yptr[12 + 0], wptr[0]);
2207  
2208     z[i + 12] = MAD_F_MLZ(hi, lo);
2209  
2210     MAD_F_ML0(hi, lo, yptr[12 + 6], wptr[6]);
2211     MAD_F_MLA(hi, lo, yptr[24 + 0], wptr[0]);
2212  
2213     z[i + 18] = MAD_F_MLZ(hi, lo);
2214  
2215     z[i + 24] = mad_f_mul(yptr[24 + 6], wptr[6]);
2216     z[i + 30] = 0;
2217  
2218     ++yptr;
2219     ++wptr;
2220   }
2221 }
2222  
2223 /*
2224  * NAME:        III_overlap()
2225  * DESCRIPTION: perform overlap-add of windowed IMDCT outputs
2226  */
2227 static
2228 void III_overlap(mad_fixed_t const output[36], mad_fixed_t overlap[18],
2229                  mad_fixed_t sample[18][32], unsigned int sb)
2230 {
2231   unsigned int i;
2232  
2233 # if defined(ASO_INTERLEAVE2)
2234   {
2235     register mad_fixed_t tmp1, tmp2;
2236  
2237     tmp1 = overlap[0];
2238     tmp2 = overlap[1];
2239  
2240     for (i = 0; i < 16; i += 2) {
2241       sample[i + 0][sb] = output[i + 0 +  0] + tmp1;
2242       overlap[i + 0]    = output[i + 0 + 18];
2243       tmp1 = overlap[i + 2];
2244  
2245       sample[i + 1][sb] = output[i + 1 +  0] + tmp2;
2246       overlap[i + 1]    = output[i + 1 + 18];
2247       tmp2 = overlap[i + 3];
2248     }
2249  
2250     sample[16][sb] = output[16 +  0] + tmp1;
2251     overlap[16]    = output[16 + 18];
2252     sample[17][sb] = output[17 +  0] + tmp2;
2253     overlap[17]    = output[17 + 18];
2254   }
2255 # elif 0
2256   for (i = 0; i < 18; i += 2) {
2257     sample[i + 0][sb] = output[i + 0 +  0] + overlap[i + 0];
2258     overlap[i + 0]    = output[i + 0 + 18];
2259  
2260     sample[i + 1][sb] = output[i + 1 +  0] + overlap[i + 1];
2261     overlap[i + 1]    = output[i + 1 + 18];
2262   }
2263 # else
2264   for (i = 0; i < 18; ++i) {
2265     sample[i][sb] = output[i +  0] + overlap[i];
2266     overlap[i]    = output[i + 18];
2267   }
2268 # endif
2269 }
2270  
2271 /*
2272  * NAME:        III_overlap_z()
2273  * DESCRIPTION: perform "overlap-add" of zero IMDCT outputs
2274  */
26 roytam 2275 static inline
14 roytam 2276 void III_overlap_z(mad_fixed_t overlap[18],
2277                    mad_fixed_t sample[18][32], unsigned int sb)
2278 {
2279   unsigned int i;
2280  
2281 # if defined(ASO_INTERLEAVE2)
2282   {
2283     register mad_fixed_t tmp1, tmp2;
2284  
2285     tmp1 = overlap[0];
2286     tmp2 = overlap[1];
2287  
2288     for (i = 0; i < 16; i += 2) {
2289       sample[i + 0][sb] = tmp1;
2290       overlap[i + 0]    = 0;
2291       tmp1 = overlap[i + 2];
2292  
2293       sample[i + 1][sb] = tmp2;
2294       overlap[i + 1]    = 0;
2295       tmp2 = overlap[i + 3];
2296     }
2297  
2298     sample[16][sb] = tmp1;
2299     overlap[16]    = 0;
2300     sample[17][sb] = tmp2;
2301     overlap[17]    = 0;
2302   }
2303 # else
2304   for (i = 0; i < 18; ++i) {
2305     sample[i][sb] = overlap[i];
2306     overlap[i]    = 0;
2307   }
2308 # endif
2309 }
2310  
2311 /*
2312  * NAME:        III_freqinver()
2313  * DESCRIPTION: perform subband frequency inversion for odd sample lines
2314  */
2315 static
2316 void III_freqinver(mad_fixed_t sample[18][32], unsigned int sb)
2317 {
2318   unsigned int i;
2319  
2320 # if 1 || defined(ASO_INTERLEAVE1) || defined(ASO_INTERLEAVE2)
2321   {
2322     register mad_fixed_t tmp1, tmp2;
2323  
2324     tmp1 = sample[1][sb];
2325     tmp2 = sample[3][sb];
2326  
2327     for (i = 1; i < 13; i += 4) {
2328       sample[i + 0][sb] = -tmp1;
2329       tmp1 = sample[i + 4][sb];
2330       sample[i + 2][sb] = -tmp2;
2331       tmp2 = sample[i + 6][sb];
2332     }
2333  
2334     sample[13][sb] = -tmp1;
2335     tmp1 = sample[17][sb];
2336     sample[15][sb] = -tmp2;
2337     sample[17][sb] = -tmp1;
2338   }
2339 # else
2340   for (i = 1; i < 18; i += 2)
2341     sample[i][sb] = -sample[i][sb];
2342 # endif
2343 }
2344  
2345 /*
2346  * NAME:        III_decode()
2347  * DESCRIPTION: decode frame main_data
2348  */
2349 static
2350 enum mad_error III_decode(struct mad_bitptr *ptr, struct mad_frame *frame,
2351                           struct sideinfo *si, unsigned int nch)
2352 {
2353   struct mad_header *header = &frame->header;
2354   unsigned int sfreqi, ngr, gr;
2355  
2356   {
2357     unsigned int sfreq;
2358  
2359     sfreq = header->samplerate;
2360     if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
2361       sfreq *= 2;
2362  
2363     /* 48000 => 0, 44100 => 1, 32000 => 2,
2364        24000 => 3, 22050 => 4, 16000 => 5 */
2365     sfreqi = ((sfreq >>  7) & 0x000f) +
2366              ((sfreq >> 15) & 0x0001) - 8;
2367  
2368     if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
2369       sfreqi += 3;
2370   }
2371  
2372   /* scalefactors, Huffman decoding, requantization */
2373  
2374   ngr = (header->flags & MAD_FLAG_LSF_EXT) ? 1 : 2;
2375  
2376   for (gr = 0; gr < ngr; ++gr) {
2377     struct granule *granule = &si->gr[gr];
2378     unsigned char const *sfbwidth[2];
2379     mad_fixed_t xr[2][576];
2380     unsigned int ch;
2381     enum mad_error error;
2382  
2383     for (ch = 0; ch < nch; ++ch) {
2384       struct channel *channel = &granule->ch[ch];
2385       unsigned int part2_length;
2386  
2387       sfbwidth[ch] = sfbwidth_table[sfreqi].l;
2388       if (channel->block_type == 2) {
2389         sfbwidth[ch] = (channel->flags & mixed_block_flag) ?
2390           sfbwidth_table[sfreqi].m : sfbwidth_table[sfreqi].s;
2391       }
2392  
2393       if (header->flags & MAD_FLAG_LSF_EXT) {
2394         part2_length = III_scalefactors_lsf(ptr, channel,
2395                                             ch == 0 ? 0 : &si->gr[1].ch[1],
2396                                             header->mode_extension);
2397       }
2398       else {
2399         part2_length = III_scalefactors(ptr, channel, &si->gr[0].ch[ch],
2400                                         gr == 0 ? 0 : si->scfsi[ch]);
2401       }
2402  
2403       error = III_huffdecode(ptr, xr[ch], channel, sfbwidth[ch], part2_length);
2404       if (error)
2405         return error;
2406     }
2407  
2408     /* joint stereo processing */
2409  
2410     if (header->mode == MAD_MODE_JOINT_STEREO && header->mode_extension) {
2411       error = III_stereo(xr, granule, header, sfbwidth[0]);
2412       if (error)
2413         return error;
2414     }
2415  
2416     /* reordering, alias reduction, IMDCT, overlap-add, frequency inversion */
2417  
2418     for (ch = 0; ch < nch; ++ch) {
2419       struct channel const *channel = &granule->ch[ch];
2420       mad_fixed_t (*sample)[32] = &frame->sbsample[ch][18 * gr];
2421       unsigned int sb, l, i, sblimit;
2422       mad_fixed_t output[36];
2423  
2424       if (channel->block_type == 2) {
2425         III_reorder(xr[ch], channel, sfbwidth[ch]);
2426  
2427 # if !defined(OPT_STRICT)
2428         /*
2429          * According to ISO/IEC 11172-3, "Alias reduction is not applied for
2430          * granules with block_type == 2 (short block)." However, other
2431          * sources suggest alias reduction should indeed be performed on the
2432          * lower two subbands of mixed blocks. Most other implementations do
2433          * this, so by default we will too.
2434          */
2435         if (channel->flags & mixed_block_flag)
2436           III_aliasreduce(xr[ch], 36);
2437 # endif
2438       }
2439       else
2440         III_aliasreduce(xr[ch], 576);
2441  
2442       l = 0;
2443  
2444       /* subbands 0-1 */
2445  
2446       if (channel->block_type != 2 || (channel->flags & mixed_block_flag)) {
2447         unsigned int block_type;
2448  
2449         block_type = channel->block_type;
2450         if (channel->flags & mixed_block_flag)
2451           block_type = 0;
2452  
2453         /* long blocks */
2454         for (sb = 0; sb < 2; ++sb, l += 18) {
2455           III_imdct_l(&xr[ch][l], output, block_type);
2456           III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2457         }
2458       }
2459       else {
2460         /* short blocks */
2461         for (sb = 0; sb < 2; ++sb, l += 18) {
2462           III_imdct_s(&xr[ch][l], output);
2463           III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2464         }
2465       }
2466  
2467       III_freqinver(sample, 1);
2468  
2469       /* (nonzero) subbands 2-31 */
2470  
2471       i = 576;
2472       while (i > 36 && xr[ch][i - 1] == 0)
2473         --i;
2474  
2475       sblimit = 32 - (576 - i) / 18;
2476  
2477       if (channel->block_type != 2) {
2478         /* long blocks */
2479         for (sb = 2; sb < sblimit; ++sb, l += 18) {
2480           III_imdct_l(&xr[ch][l], output, channel->block_type);
2481           III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2482  
2483           if (sb & 1)
2484             III_freqinver(sample, sb);
2485         }
2486       }
2487       else {
2488         /* short blocks */
2489         for (sb = 2; sb < sblimit; ++sb, l += 18) {
2490           III_imdct_s(&xr[ch][l], output);
2491           III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2492  
2493           if (sb & 1)
2494             III_freqinver(sample, sb);
2495         }
2496       }
2497  
2498       /* remaining (zero) subbands */
2499  
2500       for (sb = sblimit; sb < 32; ++sb) {
2501         III_overlap_z((*frame->overlap)[ch][sb], sample, sb);
2502  
2503         if (sb & 1)
2504           III_freqinver(sample, sb);
2505       }
2506     }
2507   }
2508  
2509   return MAD_ERROR_NONE;
2510 }
2511  
2512 /*
2513  * NAME:        layer->III()
2514  * DESCRIPTION: decode a single Layer III frame
2515  */
2516 int mad_layer_III(struct mad_stream *stream, struct mad_frame *frame)
2517 {
2518   struct mad_header *header = &frame->header;
2519   unsigned int nch, priv_bitlen, next_md_begin = 0;
2520   unsigned int si_len, data_bitlen, md_len;
2521   unsigned int frame_space, frame_used, frame_free;
2522   struct mad_bitptr ptr;
2523   struct sideinfo si;
2524   enum mad_error error;
2525   int result = 0;
2526  
2527   /* allocate Layer III dynamic structures */
2528  
2529   if (stream->main_data == 0) {
2530     stream->main_data = malloc(MAD_BUFFER_MDLEN);
2531     if (stream->main_data == 0) {
2532       stream->error = MAD_ERROR_NOMEM;
2533       return -1;
2534     }
2535   }
2536  
2537   if (frame->overlap == 0) {
26 roytam 2538     frame->overlap = calloc(2 * 32 * 18, sizeof(mad_fixed_t));
14 roytam 2539     if (frame->overlap == 0) {
2540       stream->error = MAD_ERROR_NOMEM;
2541       return -1;
2542     }
2543   }
2544  
2545   nch = MAD_NCHANNELS(header);
2546   si_len = (header->flags & MAD_FLAG_LSF_EXT) ?
2547     (nch == 1 ? 9 : 17) : (nch == 1 ? 17 : 32);
2548  
2549   /* check frame sanity */
2550  
2551   if (stream->next_frame - mad_bit_nextbyte(&stream->ptr) <
2552       (signed int) si_len) {
2553     stream->error = MAD_ERROR_BADFRAMELEN;
2554     stream->md_len = 0;
2555     return -1;
2556   }
2557  
2558   /* check CRC word */
2559  
2560   if (header->flags & MAD_FLAG_PROTECTION) {
2561     header->crc_check =
2562       mad_bit_crc(stream->ptr, si_len * CHAR_BIT, header->crc_check);
2563  
2564     if (header->crc_check != header->crc_target &&
2565         !(frame->options & MAD_OPTION_IGNORECRC)) {
2566       stream->error = MAD_ERROR_BADCRC;
2567       result = -1;
2568     }
2569   }
2570  
2571   /* decode frame side information */
2572  
2573   error = III_sideinfo(&stream->ptr, nch, header->flags & MAD_FLAG_LSF_EXT,
2574                        &si, &data_bitlen, &priv_bitlen);
2575   if (error && result == 0) {
2576     stream->error = error;
2577     result = -1;
2578   }
2579  
2580   header->flags        |= priv_bitlen;
2581   header->private_bits |= si.private_bits;
2582  
2583   /* find main_data of next frame */
2584  
2585   {
2586     struct mad_bitptr peek;
2587     unsigned long header;
2588  
2589     mad_bit_init(&peek, stream->next_frame);
2590  
2591     header = mad_bit_read(&peek, 32);
2592     if ((header & 0xffe60000L) /* syncword | layer */ == 0xffe20000L) {
2593       if (!(header & 0x00010000L))  /* protection_bit */
2594         mad_bit_skip(&peek, 16);  /* crc_check */
2595  
2596       next_md_begin =
2597         mad_bit_read(&peek, (header & 0x00080000L) /* ID */ ? 9 : 8);
2598     }
2599  
2600     mad_bit_finish(&peek);
2601   }
2602  
2603   /* find main_data of this frame */
2604  
2605   frame_space = stream->next_frame - mad_bit_nextbyte(&stream->ptr);
2606  
2607   if (next_md_begin > si.main_data_begin + frame_space)
2608     next_md_begin = 0;
2609  
2610   md_len = si.main_data_begin + frame_space - next_md_begin;
2611  
2612   frame_used = 0;
2613  
2614   if (si.main_data_begin == 0) {
2615     ptr = stream->ptr;
2616     stream->md_len = 0;
2617  
2618     frame_used = md_len;
2619   }
2620   else {
2621     if (si.main_data_begin > stream->md_len) {
2622       if (result == 0) {
2623         stream->error = MAD_ERROR_BADDATAPTR;
2624         result = -1;
2625       }
2626     }
2627     else {
2628       mad_bit_init(&ptr,
2629                    *stream->main_data + stream->md_len - si.main_data_begin);
2630  
2631       if (md_len > si.main_data_begin) {
2632         assert(stream->md_len + md_len -
2633                si.main_data_begin <= MAD_BUFFER_MDLEN);
2634  
2635         memcpy(*stream->main_data + stream->md_len,
2636                mad_bit_nextbyte(&stream->ptr),
2637                frame_used = md_len - si.main_data_begin);
2638         stream->md_len += frame_used;
2639       }
2640     }
2641   }
2642  
2643   frame_free = frame_space - frame_used;
2644  
2645   /* decode main_data */
2646  
2647   if (result == 0) {
2648     error = III_decode(&ptr, frame, &si, nch);
2649     if (error) {
2650       stream->error = error;
2651       result = -1;
2652     }
2653  
2654     /* designate ancillary bits */
2655  
2656     stream->anc_ptr    = ptr;
2657     stream->anc_bitlen = md_len * CHAR_BIT - data_bitlen;
2658   }
2659  
2660 # if 0 && defined(DEBUG)
2661   fprintf(stderr,
2662           "main_data_begin:%u, md_len:%u, frame_free:%u, "
2663           "data_bitlen:%u, anc_bitlen: %u\n",
2664           si.main_data_begin, md_len, frame_free,
2665           data_bitlen, stream->anc_bitlen);
2666 # endif
2667  
2668   /* preload main_data buffer with up to 511 bytes for next frame(s) */
2669  
2670   if (frame_free >= next_md_begin) {
2671     memcpy(*stream->main_data,
2672            stream->next_frame - next_md_begin, next_md_begin);
2673     stream->md_len = next_md_begin;
2674   }
2675   else {
2676     if (md_len < si.main_data_begin) {
2677       unsigned int extra;
2678  
2679       extra = si.main_data_begin - md_len;
2680       if (extra + frame_free > next_md_begin)
2681         extra = next_md_begin - frame_free;
2682  
2683       if (extra < stream->md_len) {
2684         memmove(*stream->main_data,
2685                 *stream->main_data + stream->md_len - extra, extra);
2686         stream->md_len = extra;
2687       }
2688     }
2689     else
2690       stream->md_len = 0;
2691  
2692     memcpy(*stream->main_data + stream->md_len,
2693            stream->next_frame - frame_free, frame_free);
2694     stream->md_len += frame_free;
2695   }
2696  
2697   return result;
2698 }