rtoss - Blame information for rev 14

Subversion Repositories:
Rev:
Rev Author Line No. Line
14 roytam 1 /*
2  * libmad - MPEG audio decoder library
3  * Copyright (C) 2000-2003 Underbit Technologies, Inc.
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  *
19  * $Id: layer3.c,v 1.41 2003/05/28 04:24:36 rob Exp $
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  
569 # if 0 && defined(DEBUG)
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 */
909 # if 0 && defined(DEBUG)
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
1578 /*
1579  * NAME:        imdct36
1580  * DESCRIPTION: perform X[18]->x[36] IMDCT
1581  */
1582 static __inline
1583 void imdct36(mad_fixed_t const X[18], mad_fixed_t x[36])
1584 {
1585   mad_fixed_t t0, t1, t2,  t3,  t4,  t5,  t6,  t7;
1586   mad_fixed_t t8, t9, t10, t11, t12, t13, t14, t15;
1587   register mad_fixed64hi_t hi;
1588   register mad_fixed64lo_t lo;
1589  
1590   MAD_F_ML0(hi, lo, X[4],  MAD_F(0x0ec835e8));
1591   MAD_F_MLA(hi, lo, X[13], MAD_F(0x061f78aa));
1592  
1593   t6 = MAD_F_MLZ(hi, lo);
1594  
1595   MAD_F_MLA(hi, lo, (t14 = X[1] - X[10]), -MAD_F(0x061f78aa));
1596   MAD_F_MLA(hi, lo, (t15 = X[7] + X[16]), -MAD_F(0x0ec835e8));
1597  
1598   t0 = MAD_F_MLZ(hi, lo);
1599  
1600   MAD_F_MLA(hi, lo, (t8  = X[0] - X[11] - X[12]),  MAD_F(0x0216a2a2));
1601   MAD_F_MLA(hi, lo, (t9  = X[2] - X[9]  - X[14]),  MAD_F(0x09bd7ca0));
1602   MAD_F_MLA(hi, lo, (t10 = X[3] - X[8]  - X[15]), -MAD_F(0x0cb19346));
1603   MAD_F_MLA(hi, lo, (t11 = X[5] - X[6]  - X[17]), -MAD_F(0x0fdcf549));
1604  
1605   x[7]  = MAD_F_MLZ(hi, lo);
1606   x[10] = -x[7];
1607  
1608   MAD_F_ML0(hi, lo, t8,  -MAD_F(0x0cb19346));
1609   MAD_F_MLA(hi, lo, t9,   MAD_F(0x0fdcf549));
1610   MAD_F_MLA(hi, lo, t10,  MAD_F(0x0216a2a2));
1611   MAD_F_MLA(hi, lo, t11, -MAD_F(0x09bd7ca0));
1612  
1613   x[19] = x[34] = MAD_F_MLZ(hi, lo) - t0;
1614  
1615   t12 = X[0] - X[3] + X[8] - X[11] - X[12] + X[15];
1616   t13 = X[2] + X[5] - X[6] - X[9]  - X[14] - X[17];
1617  
1618   MAD_F_ML0(hi, lo, t12, -MAD_F(0x0ec835e8));
1619   MAD_F_MLA(hi, lo, t13,  MAD_F(0x061f78aa));
1620  
1621   x[22] = x[31] = MAD_F_MLZ(hi, lo) + t0;
1622  
1623   MAD_F_ML0(hi, lo, X[1],  -MAD_F(0x09bd7ca0));
1624   MAD_F_MLA(hi, lo, X[7],   MAD_F(0x0216a2a2));
1625   MAD_F_MLA(hi, lo, X[10], -MAD_F(0x0fdcf549));
1626   MAD_F_MLA(hi, lo, X[16],  MAD_F(0x0cb19346));
1627  
1628   t1 = MAD_F_MLZ(hi, lo) + t6;
1629  
1630   MAD_F_ML0(hi, lo, X[0],   MAD_F(0x03768962));
1631   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0e313245));
1632   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x0ffc19fd));
1633   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0acf37ad));
1634   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x04cfb0e2));
1635   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x0898c779));
1636   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x0d7e8807));
1637   MAD_F_MLA(hi, lo, X[11],  MAD_F(0x0f426cb5));
1638   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0bcbe352));
1639   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x00b2aa3e));
1640   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x07635284));
1641   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0f9ee890));
1642  
1643   x[6]  = MAD_F_MLZ(hi, lo) + t1;
1644   x[11] = -x[6];
1645  
1646   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0f426cb5));
1647   MAD_F_MLA(hi, lo, X[2],  -MAD_F(0x00b2aa3e));
1648   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0898c779));
1649   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x0f9ee890));
1650   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0acf37ad));
1651   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x07635284));
1652   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x0e313245));
1653   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0bcbe352));
1654   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x03768962));
1655   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0d7e8807));
1656   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0ffc19fd));
1657   MAD_F_MLA(hi, lo, X[17],  MAD_F(0x04cfb0e2));
1658  
1659   x[23] = x[30] = MAD_F_MLZ(hi, lo) + t1;
1660  
1661   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0bcbe352));
1662   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0d7e8807));
1663   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x07635284));
1664   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x04cfb0e2));
1665   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0f9ee890));
1666   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x0ffc19fd));
1667   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x00b2aa3e));
1668   MAD_F_MLA(hi, lo, X[11],  MAD_F(0x03768962));
1669   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0f426cb5));
1670   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0e313245));
1671   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0898c779));
1672   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0acf37ad));
1673  
1674   x[18] = x[35] = MAD_F_MLZ(hi, lo) - t1;
1675  
1676   MAD_F_ML0(hi, lo, X[4],   MAD_F(0x061f78aa));
1677   MAD_F_MLA(hi, lo, X[13], -MAD_F(0x0ec835e8));
1678  
1679   t7 = MAD_F_MLZ(hi, lo);
1680  
1681   MAD_F_MLA(hi, lo, X[1],  -MAD_F(0x0cb19346));
1682   MAD_F_MLA(hi, lo, X[7],   MAD_F(0x0fdcf549));
1683   MAD_F_MLA(hi, lo, X[10],  MAD_F(0x0216a2a2));
1684   MAD_F_MLA(hi, lo, X[16], -MAD_F(0x09bd7ca0));
1685  
1686   t2 = MAD_F_MLZ(hi, lo);
1687  
1688   MAD_F_MLA(hi, lo, X[0],   MAD_F(0x04cfb0e2));
1689   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0ffc19fd));
1690   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x0d7e8807));
1691   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x03768962));
1692   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x0bcbe352));
1693   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x0e313245));
1694   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x07635284));
1695   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0acf37ad));
1696   MAD_F_MLA(hi, lo, X[12],  MAD_F(0x0f9ee890));
1697   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0898c779));
1698   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x00b2aa3e));
1699   MAD_F_MLA(hi, lo, X[17],  MAD_F(0x0f426cb5));
1700  
1701   x[5]  = MAD_F_MLZ(hi, lo);
1702   x[12] = -x[5];
1703  
1704   MAD_F_ML0(hi, lo, X[0],   MAD_F(0x0acf37ad));
1705   MAD_F_MLA(hi, lo, X[2],  -MAD_F(0x0898c779));
1706   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0e313245));
1707   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0f426cb5));
1708   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x03768962));
1709   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x00b2aa3e));
1710   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x0ffc19fd));
1711   MAD_F_MLA(hi, lo, X[11],  MAD_F(0x0f9ee890));
1712   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x04cfb0e2));
1713   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x07635284));
1714   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0d7e8807));
1715   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0bcbe352));
1716  
1717   x[0]  = MAD_F_MLZ(hi, lo) + t2;
1718   x[17] = -x[0];
1719  
1720   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0f9ee890));
1721   MAD_F_MLA(hi, lo, X[2],  -MAD_F(0x07635284));
1722   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x00b2aa3e));
1723   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x0bcbe352));
1724   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0f426cb5));
1725   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x0d7e8807));
1726   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x0898c779));
1727   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x04cfb0e2));
1728   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0acf37ad));
1729   MAD_F_MLA(hi, lo, X[14], -MAD_F(0x0ffc19fd));
1730   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0e313245));
1731   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x03768962));
1732  
1733   x[24] = x[29] = MAD_F_MLZ(hi, lo) + t2;
1734  
1735   MAD_F_ML0(hi, lo, X[1],  -MAD_F(0x0216a2a2));
1736   MAD_F_MLA(hi, lo, X[7],  -MAD_F(0x09bd7ca0));
1737   MAD_F_MLA(hi, lo, X[10],  MAD_F(0x0cb19346));
1738   MAD_F_MLA(hi, lo, X[16],  MAD_F(0x0fdcf549));
1739  
1740   t3 = MAD_F_MLZ(hi, lo) + t7;
1741  
1742   MAD_F_ML0(hi, lo, X[0],   MAD_F(0x00b2aa3e));
1743   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x03768962));
1744   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x04cfb0e2));
1745   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x07635284));
1746   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0898c779));
1747   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x0acf37ad));
1748   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x0bcbe352));
1749   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0d7e8807));
1750   MAD_F_MLA(hi, lo, X[12],  MAD_F(0x0e313245));
1751   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0f426cb5));
1752   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0f9ee890));
1753   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0ffc19fd));
1754  
1755   x[8] = MAD_F_MLZ(hi, lo) + t3;
1756   x[9] = -x[8];
1757  
1758   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0e313245));
1759   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0bcbe352));
1760   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0f9ee890));
1761   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0898c779));
1762   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x0ffc19fd));
1763   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x04cfb0e2));
1764   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x0f426cb5));
1765   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x00b2aa3e));
1766   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0d7e8807));
1767   MAD_F_MLA(hi, lo, X[14], -MAD_F(0x03768962));
1768   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0acf37ad));
1769   MAD_F_MLA(hi, lo, X[17],  MAD_F(0x07635284));
1770  
1771   x[21] = x[32] = MAD_F_MLZ(hi, lo) + t3;
1772  
1773   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0d7e8807));
1774   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0f426cb5));
1775   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0acf37ad));
1776   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0ffc19fd));
1777   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x07635284));
1778   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x0f9ee890));
1779   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x03768962));
1780   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0e313245));
1781   MAD_F_MLA(hi, lo, X[12],  MAD_F(0x00b2aa3e));
1782   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0bcbe352));
1783   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x04cfb0e2));
1784   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0898c779));
1785  
1786   x[20] = x[33] = MAD_F_MLZ(hi, lo) - t3;
1787  
1788   MAD_F_ML0(hi, lo, t14, -MAD_F(0x0ec835e8));
1789   MAD_F_MLA(hi, lo, t15,  MAD_F(0x061f78aa));
1790  
1791   t4 = MAD_F_MLZ(hi, lo) - t7;
1792  
1793   MAD_F_ML0(hi, lo, t12, MAD_F(0x061f78aa));
1794   MAD_F_MLA(hi, lo, t13, MAD_F(0x0ec835e8));
1795  
1796   x[4]  = MAD_F_MLZ(hi, lo) + t4;
1797   x[13] = -x[4];
1798  
1799   MAD_F_ML0(hi, lo, t8,   MAD_F(0x09bd7ca0));
1800   MAD_F_MLA(hi, lo, t9,  -MAD_F(0x0216a2a2));
1801   MAD_F_MLA(hi, lo, t10,  MAD_F(0x0fdcf549));
1802   MAD_F_MLA(hi, lo, t11, -MAD_F(0x0cb19346));
1803  
1804   x[1]  = MAD_F_MLZ(hi, lo) + t4;
1805   x[16] = -x[1];
1806  
1807   MAD_F_ML0(hi, lo, t8,  -MAD_F(0x0fdcf549));
1808   MAD_F_MLA(hi, lo, t9,  -MAD_F(0x0cb19346));
1809   MAD_F_MLA(hi, lo, t10, -MAD_F(0x09bd7ca0));
1810   MAD_F_MLA(hi, lo, t11, -MAD_F(0x0216a2a2));
1811  
1812   x[25] = x[28] = MAD_F_MLZ(hi, lo) + t4;
1813  
1814   MAD_F_ML0(hi, lo, X[1],  -MAD_F(0x0fdcf549));
1815   MAD_F_MLA(hi, lo, X[7],  -MAD_F(0x0cb19346));
1816   MAD_F_MLA(hi, lo, X[10], -MAD_F(0x09bd7ca0));
1817   MAD_F_MLA(hi, lo, X[16], -MAD_F(0x0216a2a2));
1818  
1819   t5 = MAD_F_MLZ(hi, lo) - t6;
1820  
1821   MAD_F_ML0(hi, lo, X[0],   MAD_F(0x0898c779));
1822   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x04cfb0e2));
1823   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0bcbe352));
1824   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x00b2aa3e));
1825   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0e313245));
1826   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x03768962));
1827   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x0f9ee890));
1828   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x07635284));
1829   MAD_F_MLA(hi, lo, X[12],  MAD_F(0x0ffc19fd));
1830   MAD_F_MLA(hi, lo, X[14], -MAD_F(0x0acf37ad));
1831   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0f426cb5));
1832   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0d7e8807));
1833  
1834   x[2]  = MAD_F_MLZ(hi, lo) + t5;
1835   x[15] = -x[2];
1836  
1837   MAD_F_ML0(hi, lo, X[0],   MAD_F(0x07635284));
1838   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0acf37ad));
1839   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x03768962));
1840   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x0d7e8807));
1841   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x00b2aa3e));
1842   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x0f426cb5));
1843   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x04cfb0e2));
1844   MAD_F_MLA(hi, lo, X[11],  MAD_F(0x0ffc19fd));
1845   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0898c779));
1846   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0f9ee890));
1847   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0bcbe352));
1848   MAD_F_MLA(hi, lo, X[17],  MAD_F(0x0e313245));
1849  
1850   x[3]  = MAD_F_MLZ(hi, lo) + t5;
1851   x[14] = -x[3];
1852  
1853   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0ffc19fd));
1854   MAD_F_MLA(hi, lo, X[2],  -MAD_F(0x0f9ee890));
1855   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x0f426cb5));
1856   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0e313245));
1857   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x0d7e8807));
1858   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x0bcbe352));
1859   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x0acf37ad));
1860   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0898c779));
1861   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x07635284));
1862   MAD_F_MLA(hi, lo, X[14], -MAD_F(0x04cfb0e2));
1863   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x03768962));
1864   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x00b2aa3e));
1865  
1866   x[26] = x[27] = MAD_F_MLZ(hi, lo) + t5;
1867 }
1868  
1869 /*
1870  * NAME:        III_imdct_l()
1871  * DESCRIPTION: perform IMDCT and windowing for long blocks
1872  */
1873 static
1874 void III_imdct_l(mad_fixed_t const X[18], mad_fixed_t z[36],
1875                  unsigned int block_type)
1876 {
1877   unsigned int i;
1878  
1879   /* IMDCT */
1880  
1881   imdct36(X, z);
1882  
1883   /* windowing */
1884  
1885   switch (block_type) {
1886   case 0:  /* normal window */
1887 # if defined(ASO_INTERLEAVE1)
1888     {
1889       register mad_fixed_t tmp1, tmp2;
1890  
1891       tmp1 = window_l[0];
1892       tmp2 = window_l[1];
1893  
1894       for (i = 0; i < 34; i += 2) {
1895         z[i + 0] = mad_f_mul(z[i + 0], tmp1);
1896         tmp1 = window_l[i + 2];
1897         z[i + 1] = mad_f_mul(z[i + 1], tmp2);
1898         tmp2 = window_l[i + 3];
1899       }
1900  
1901       z[34] = mad_f_mul(z[34], tmp1);
1902       z[35] = mad_f_mul(z[35], tmp2);
1903     }
1904 # elif defined(ASO_INTERLEAVE2)
1905     {
1906       register mad_fixed_t tmp1, tmp2;
1907  
1908       tmp1 = z[0];
1909       tmp2 = window_l[0];
1910  
1911       for (i = 0; i < 35; ++i) {
1912         z[i] = mad_f_mul(tmp1, tmp2);
1913         tmp1 = z[i + 1];
1914         tmp2 = window_l[i + 1];
1915       }
1916  
1917       z[35] = mad_f_mul(tmp1, tmp2);
1918     }
1919 # elif 1
1920     for (i = 0; i < 36; i += 4) {
1921       z[i + 0] = mad_f_mul(z[i + 0], window_l[i + 0]);
1922       z[i + 1] = mad_f_mul(z[i + 1], window_l[i + 1]);
1923       z[i + 2] = mad_f_mul(z[i + 2], window_l[i + 2]);
1924       z[i + 3] = mad_f_mul(z[i + 3], window_l[i + 3]);
1925     }
1926 # else
1927     for (i =  0; i < 36; ++i) z[i] = mad_f_mul(z[i], window_l[i]);
1928 # endif
1929     break;
1930  
1931   case 1:  /* start block */
1932     for (i =  0; i < 18; ++i) z[i] = mad_f_mul(z[i], window_l[i]);
1933     /*  (i = 18; i < 24; ++i) z[i] unchanged */
1934     for (i = 24; i < 30; ++i) z[i] = mad_f_mul(z[i], window_s[i - 18]);
1935     for (i = 30; i < 36; ++i) z[i] = 0;
1936     break;
1937  
1938   case 3:  /* stop block */
1939     for (i =  0; i <  6; ++i) z[i] = 0;
1940     for (i =  6; i < 12; ++i) z[i] = mad_f_mul(z[i], window_s[i - 6]);
1941     /*  (i = 12; i < 18; ++i) z[i] unchanged */
1942     for (i = 18; i < 36; ++i) z[i] = mad_f_mul(z[i], window_l[i]);
1943     break;
1944   }
1945 }
1946 # endif  /* ASO_IMDCT */
1947  
1948 /*
1949  * NAME:        III_imdct_s()
1950  * DESCRIPTION: perform IMDCT and windowing for short blocks
1951  */
1952 static
1953 void III_imdct_s(mad_fixed_t const X[18], mad_fixed_t z[36])
1954 {
1955   mad_fixed_t y[36], *yptr;
1956   mad_fixed_t const *wptr;
1957   int w, i;
1958   register mad_fixed64hi_t hi;
1959   register mad_fixed64lo_t lo;
1960  
1961   /* IMDCT */
1962  
1963   yptr = &y[0];
1964  
1965   for (w = 0; w < 3; ++w) {
1966     register mad_fixed_t const (*s)[6];
1967  
1968     s = imdct_s;
1969  
1970     for (i = 0; i < 3; ++i) {
1971       MAD_F_ML0(hi, lo, X[0], (*s)[0]);
1972       MAD_F_MLA(hi, lo, X[1], (*s)[1]);
1973       MAD_F_MLA(hi, lo, X[2], (*s)[2]);
1974       MAD_F_MLA(hi, lo, X[3], (*s)[3]);
1975       MAD_F_MLA(hi, lo, X[4], (*s)[4]);
1976       MAD_F_MLA(hi, lo, X[5], (*s)[5]);
1977  
1978       yptr[i + 0] = MAD_F_MLZ(hi, lo);
1979       yptr[5 - i] = -yptr[i + 0];
1980  
1981       ++s;
1982  
1983       MAD_F_ML0(hi, lo, X[0], (*s)[0]);
1984       MAD_F_MLA(hi, lo, X[1], (*s)[1]);
1985       MAD_F_MLA(hi, lo, X[2], (*s)[2]);
1986       MAD_F_MLA(hi, lo, X[3], (*s)[3]);
1987       MAD_F_MLA(hi, lo, X[4], (*s)[4]);
1988       MAD_F_MLA(hi, lo, X[5], (*s)[5]);
1989  
1990       yptr[ i + 6] = MAD_F_MLZ(hi, lo);
1991       yptr[11 - i] = yptr[i + 6];
1992  
1993       ++s;
1994     }
1995  
1996     yptr += 12;
1997     X    += 6;
1998   }
1999  
2000   /* windowing, overlapping and concatenation */
2001  
2002   yptr = &y[0];
2003   wptr = &window_s[0];
2004  
2005   for (i = 0; i < 6; ++i) {
2006     z[i +  0] = 0;
2007     z[i +  6] = mad_f_mul(yptr[ 0 + 0], wptr[0]);
2008  
2009     MAD_F_ML0(hi, lo, yptr[ 0 + 6], wptr[6]);
2010     MAD_F_MLA(hi, lo, yptr[12 + 0], wptr[0]);
2011  
2012     z[i + 12] = MAD_F_MLZ(hi, lo);
2013  
2014     MAD_F_ML0(hi, lo, yptr[12 + 6], wptr[6]);
2015     MAD_F_MLA(hi, lo, yptr[24 + 0], wptr[0]);
2016  
2017     z[i + 18] = MAD_F_MLZ(hi, lo);
2018  
2019     z[i + 24] = mad_f_mul(yptr[24 + 6], wptr[6]);
2020     z[i + 30] = 0;
2021  
2022     ++yptr;
2023     ++wptr;
2024   }
2025 }
2026  
2027 /*
2028  * NAME:        III_overlap()
2029  * DESCRIPTION: perform overlap-add of windowed IMDCT outputs
2030  */
2031 static
2032 void III_overlap(mad_fixed_t const output[36], mad_fixed_t overlap[18],
2033                  mad_fixed_t sample[18][32], unsigned int sb)
2034 {
2035   unsigned int i;
2036  
2037 # if defined(ASO_INTERLEAVE2)
2038   {
2039     register mad_fixed_t tmp1, tmp2;
2040  
2041     tmp1 = overlap[0];
2042     tmp2 = overlap[1];
2043  
2044     for (i = 0; i < 16; i += 2) {
2045       sample[i + 0][sb] = output[i + 0 +  0] + tmp1;
2046       overlap[i + 0]    = output[i + 0 + 18];
2047       tmp1 = overlap[i + 2];
2048  
2049       sample[i + 1][sb] = output[i + 1 +  0] + tmp2;
2050       overlap[i + 1]    = output[i + 1 + 18];
2051       tmp2 = overlap[i + 3];
2052     }
2053  
2054     sample[16][sb] = output[16 +  0] + tmp1;
2055     overlap[16]    = output[16 + 18];
2056     sample[17][sb] = output[17 +  0] + tmp2;
2057     overlap[17]    = output[17 + 18];
2058   }
2059 # elif 0
2060   for (i = 0; i < 18; i += 2) {
2061     sample[i + 0][sb] = output[i + 0 +  0] + overlap[i + 0];
2062     overlap[i + 0]    = output[i + 0 + 18];
2063  
2064     sample[i + 1][sb] = output[i + 1 +  0] + overlap[i + 1];
2065     overlap[i + 1]    = output[i + 1 + 18];
2066   }
2067 # else
2068   for (i = 0; i < 18; ++i) {
2069     sample[i][sb] = output[i +  0] + overlap[i];
2070     overlap[i]    = output[i + 18];
2071   }
2072 # endif
2073 }
2074  
2075 /*
2076  * NAME:        III_overlap_z()
2077  * DESCRIPTION: perform "overlap-add" of zero IMDCT outputs
2078  */
2079 static __inline
2080 void III_overlap_z(mad_fixed_t overlap[18],
2081                    mad_fixed_t sample[18][32], unsigned int sb)
2082 {
2083   unsigned int i;
2084  
2085 # if defined(ASO_INTERLEAVE2)
2086   {
2087     register mad_fixed_t tmp1, tmp2;
2088  
2089     tmp1 = overlap[0];
2090     tmp2 = overlap[1];
2091  
2092     for (i = 0; i < 16; i += 2) {
2093       sample[i + 0][sb] = tmp1;
2094       overlap[i + 0]    = 0;
2095       tmp1 = overlap[i + 2];
2096  
2097       sample[i + 1][sb] = tmp2;
2098       overlap[i + 1]    = 0;
2099       tmp2 = overlap[i + 3];
2100     }
2101  
2102     sample[16][sb] = tmp1;
2103     overlap[16]    = 0;
2104     sample[17][sb] = tmp2;
2105     overlap[17]    = 0;
2106   }
2107 # else
2108   for (i = 0; i < 18; ++i) {
2109     sample[i][sb] = overlap[i];
2110     overlap[i]    = 0;
2111   }
2112 # endif
2113 }
2114  
2115 /*
2116  * NAME:        III_freqinver()
2117  * DESCRIPTION: perform subband frequency inversion for odd sample lines
2118  */
2119 static
2120 void III_freqinver(mad_fixed_t sample[18][32], unsigned int sb)
2121 {
2122   unsigned int i;
2123  
2124 # if 1 || defined(ASO_INTERLEAVE1) || defined(ASO_INTERLEAVE2)
2125   {
2126     register mad_fixed_t tmp1, tmp2;
2127  
2128     tmp1 = sample[1][sb];
2129     tmp2 = sample[3][sb];
2130  
2131     for (i = 1; i < 13; i += 4) {
2132       sample[i + 0][sb] = -tmp1;
2133       tmp1 = sample[i + 4][sb];
2134       sample[i + 2][sb] = -tmp2;
2135       tmp2 = sample[i + 6][sb];
2136     }
2137  
2138     sample[13][sb] = -tmp1;
2139     tmp1 = sample[17][sb];
2140     sample[15][sb] = -tmp2;
2141     sample[17][sb] = -tmp1;
2142   }
2143 # else
2144   for (i = 1; i < 18; i += 2)
2145     sample[i][sb] = -sample[i][sb];
2146 # endif
2147 }
2148  
2149 /*
2150  * NAME:        III_decode()
2151  * DESCRIPTION: decode frame main_data
2152  */
2153 static
2154 enum mad_error III_decode(struct mad_bitptr *ptr, struct mad_frame *frame,
2155                           struct sideinfo *si, unsigned int nch)
2156 {
2157   struct mad_header *header = &frame->header;
2158   unsigned int sfreqi, ngr, gr;
2159  
2160   {
2161     unsigned int sfreq;
2162  
2163     sfreq = header->samplerate;
2164     if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
2165       sfreq *= 2;
2166  
2167     /* 48000 => 0, 44100 => 1, 32000 => 2,
2168        24000 => 3, 22050 => 4, 16000 => 5 */
2169     sfreqi = ((sfreq >>  7) & 0x000f) +
2170              ((sfreq >> 15) & 0x0001) - 8;
2171  
2172     if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
2173       sfreqi += 3;
2174   }
2175  
2176   /* scalefactors, Huffman decoding, requantization */
2177  
2178   ngr = (header->flags & MAD_FLAG_LSF_EXT) ? 1 : 2;
2179  
2180   for (gr = 0; gr < ngr; ++gr) {
2181     struct granule *granule = &si->gr[gr];
2182     unsigned char const *sfbwidth[2];
2183     mad_fixed_t xr[2][576];
2184     unsigned int ch;
2185     enum mad_error error;
2186  
2187     for (ch = 0; ch < nch; ++ch) {
2188       struct channel *channel = &granule->ch[ch];
2189       unsigned int part2_length;
2190  
2191       sfbwidth[ch] = sfbwidth_table[sfreqi].l;
2192       if (channel->block_type == 2) {
2193         sfbwidth[ch] = (channel->flags & mixed_block_flag) ?
2194           sfbwidth_table[sfreqi].m : sfbwidth_table[sfreqi].s;
2195       }
2196  
2197       if (header->flags & MAD_FLAG_LSF_EXT) {
2198         part2_length = III_scalefactors_lsf(ptr, channel,
2199                                             ch == 0 ? 0 : &si->gr[1].ch[1],
2200                                             header->mode_extension);
2201       }
2202       else {
2203         part2_length = III_scalefactors(ptr, channel, &si->gr[0].ch[ch],
2204                                         gr == 0 ? 0 : si->scfsi[ch]);
2205       }
2206  
2207       error = III_huffdecode(ptr, xr[ch], channel, sfbwidth[ch], part2_length);
2208       if (error)
2209         return error;
2210     }
2211  
2212     /* joint stereo processing */
2213  
2214     if (header->mode == MAD_MODE_JOINT_STEREO && header->mode_extension) {
2215       error = III_stereo(xr, granule, header, sfbwidth[0]);
2216       if (error)
2217         return error;
2218     }
2219  
2220     /* reordering, alias reduction, IMDCT, overlap-add, frequency inversion */
2221  
2222     for (ch = 0; ch < nch; ++ch) {
2223       struct channel const *channel = &granule->ch[ch];
2224       mad_fixed_t (*sample)[32] = &frame->sbsample[ch][18 * gr];
2225       unsigned int sb, l, i, sblimit;
2226       mad_fixed_t output[36];
2227  
2228       if (channel->block_type == 2) {
2229         III_reorder(xr[ch], channel, sfbwidth[ch]);
2230  
2231 # if !defined(OPT_STRICT)
2232         /*
2233          * According to ISO/IEC 11172-3, "Alias reduction is not applied for
2234          * granules with block_type == 2 (short block)." However, other
2235          * sources suggest alias reduction should indeed be performed on the
2236          * lower two subbands of mixed blocks. Most other implementations do
2237          * this, so by default we will too.
2238          */
2239         if (channel->flags & mixed_block_flag)
2240           III_aliasreduce(xr[ch], 36);
2241 # endif
2242       }
2243       else
2244         III_aliasreduce(xr[ch], 576);
2245  
2246       l = 0;
2247  
2248       /* subbands 0-1 */
2249  
2250       if (channel->block_type != 2 || (channel->flags & mixed_block_flag)) {
2251         unsigned int block_type;
2252  
2253         block_type = channel->block_type;
2254         if (channel->flags & mixed_block_flag)
2255           block_type = 0;
2256  
2257         /* long blocks */
2258         for (sb = 0; sb < 2; ++sb, l += 18) {
2259           III_imdct_l(&xr[ch][l], output, block_type);
2260           III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2261         }
2262       }
2263       else {
2264         /* short blocks */
2265         for (sb = 0; sb < 2; ++sb, l += 18) {
2266           III_imdct_s(&xr[ch][l], output);
2267           III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2268         }
2269       }
2270  
2271       III_freqinver(sample, 1);
2272  
2273       /* (nonzero) subbands 2-31 */
2274  
2275       i = 576;
2276       while (i > 36 && xr[ch][i - 1] == 0)
2277         --i;
2278  
2279       sblimit = 32 - (576 - i) / 18;
2280  
2281       if (channel->block_type != 2) {
2282         /* long blocks */
2283         for (sb = 2; sb < sblimit; ++sb, l += 18) {
2284           III_imdct_l(&xr[ch][l], output, channel->block_type);
2285           III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2286  
2287           if (sb & 1)
2288             III_freqinver(sample, sb);
2289         }
2290       }
2291       else {
2292         /* short blocks */
2293         for (sb = 2; sb < sblimit; ++sb, l += 18) {
2294           III_imdct_s(&xr[ch][l], output);
2295           III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2296  
2297           if (sb & 1)
2298             III_freqinver(sample, sb);
2299         }
2300       }
2301  
2302       /* remaining (zero) subbands */
2303  
2304       for (sb = sblimit; sb < 32; ++sb) {
2305         III_overlap_z((*frame->overlap)[ch][sb], sample, sb);
2306  
2307         if (sb & 1)
2308           III_freqinver(sample, sb);
2309       }
2310     }
2311   }
2312  
2313   return MAD_ERROR_NONE;
2314 }
2315  
2316 /*
2317  * NAME:        layer->III()
2318  * DESCRIPTION: decode a single Layer III frame
2319  */
2320 int mad_layer_III(struct mad_stream *stream, struct mad_frame *frame)
2321 {
2322   struct mad_header *header = &frame->header;
2323   unsigned int nch, priv_bitlen, next_md_begin = 0;
2324   unsigned int si_len, data_bitlen, md_len;
2325   unsigned int frame_space, frame_used, frame_free;
2326   struct mad_bitptr ptr;
2327   struct sideinfo si;
2328   enum mad_error error;
2329   int result = 0;
2330  
2331   /* allocate Layer III dynamic structures */
2332  
2333   if (stream->main_data == 0) {
2334     stream->main_data = malloc(MAD_BUFFER_MDLEN);
2335     if (stream->main_data == 0) {
2336       stream->error = MAD_ERROR_NOMEM;
2337       return -1;
2338     }
2339   }
2340  
2341   if (frame->overlap == 0) {
2342     //frame->overlap = calloc(2 * 32 * 18, sizeof(mad_fixed_t));
2343         frame->overlap = malloc(2 * 32 * 18 * sizeof(mad_fixed_t));
2344         memset(frame->overlap, 0, 2 * 32 * 18 * sizeof(mad_fixed_t));
2345     if (frame->overlap == 0) {
2346       stream->error = MAD_ERROR_NOMEM;
2347       return -1;
2348     }
2349   }
2350  
2351   nch = MAD_NCHANNELS(header);
2352   si_len = (header->flags & MAD_FLAG_LSF_EXT) ?
2353     (nch == 1 ? 9 : 17) : (nch == 1 ? 17 : 32);
2354  
2355   /* check frame sanity */
2356  
2357   if (stream->next_frame - mad_bit_nextbyte(&stream->ptr) <
2358       (signed int) si_len) {
2359     stream->error = MAD_ERROR_BADFRAMELEN;
2360     stream->md_len = 0;
2361     return -1;
2362   }
2363  
2364   /* check CRC word */
2365  
2366   if (header->flags & MAD_FLAG_PROTECTION) {
2367     header->crc_check =
2368       mad_bit_crc(stream->ptr, si_len * CHAR_BIT, header->crc_check);
2369  
2370     if (header->crc_check != header->crc_target &&
2371         !(frame->options & MAD_OPTION_IGNORECRC)) {
2372       stream->error = MAD_ERROR_BADCRC;
2373       result = -1;
2374     }
2375   }
2376  
2377   /* decode frame side information */
2378  
2379   error = III_sideinfo(&stream->ptr, nch, header->flags & MAD_FLAG_LSF_EXT,
2380                        &si, &data_bitlen, &priv_bitlen);
2381   if (error && result == 0) {
2382     stream->error = error;
2383     result = -1;
2384   }
2385  
2386   header->flags        |= priv_bitlen;
2387   header->private_bits |= si.private_bits;
2388  
2389   /* find main_data of next frame */
2390  
2391   {
2392     struct mad_bitptr peek;
2393     unsigned long header;
2394  
2395     mad_bit_init(&peek, stream->next_frame);
2396  
2397     header = mad_bit_read(&peek, 32);
2398     if ((header & 0xffe60000L) /* syncword | layer */ == 0xffe20000L) {
2399       if (!(header & 0x00010000L))  /* protection_bit */
2400         mad_bit_skip(&peek, 16);  /* crc_check */
2401  
2402       next_md_begin =
2403         mad_bit_read(&peek, (header & 0x00080000L) /* ID */ ? 9 : 8);
2404     }
2405  
2406     mad_bit_finish(&peek);
2407   }
2408  
2409   /* find main_data of this frame */
2410  
2411   frame_space = stream->next_frame - mad_bit_nextbyte(&stream->ptr);
2412  
2413   if (next_md_begin > si.main_data_begin + frame_space)
2414     next_md_begin = 0;
2415  
2416   md_len = si.main_data_begin + frame_space - next_md_begin;
2417  
2418   frame_used = 0;
2419  
2420   if (si.main_data_begin == 0) {
2421     ptr = stream->ptr;
2422     stream->md_len = 0;
2423  
2424     frame_used = md_len;
2425   }
2426   else {
2427     if (si.main_data_begin > stream->md_len) {
2428       if (result == 0) {
2429         stream->error = MAD_ERROR_BADDATAPTR;
2430         result = -1;
2431       }
2432     }
2433     else {
2434       mad_bit_init(&ptr,
2435                    *stream->main_data + stream->md_len - si.main_data_begin);
2436  
2437       if (md_len > si.main_data_begin) {
2438         assert(stream->md_len + md_len -
2439                si.main_data_begin <= MAD_BUFFER_MDLEN);
2440  
2441         memcpy(*stream->main_data + stream->md_len,
2442                mad_bit_nextbyte(&stream->ptr),
2443                frame_used = md_len - si.main_data_begin);
2444         stream->md_len += frame_used;
2445       }
2446     }
2447   }
2448  
2449   frame_free = frame_space - frame_used;
2450  
2451   /* decode main_data */
2452  
2453   if (result == 0) {
2454     error = III_decode(&ptr, frame, &si, nch);
2455     if (error) {
2456       stream->error = error;
2457       result = -1;
2458     }
2459  
2460     /* designate ancillary bits */
2461  
2462     stream->anc_ptr    = ptr;
2463     stream->anc_bitlen = md_len * CHAR_BIT - data_bitlen;
2464   }
2465  
2466 # if 0 && defined(DEBUG)
2467   fprintf(stderr,
2468           "main_data_begin:%u, md_len:%u, frame_free:%u, "
2469           "data_bitlen:%u, anc_bitlen: %u\n",
2470           si.main_data_begin, md_len, frame_free,
2471           data_bitlen, stream->anc_bitlen);
2472 # endif
2473  
2474   /* preload main_data buffer with up to 511 bytes for next frame(s) */
2475  
2476   if (frame_free >= next_md_begin) {
2477     memcpy(*stream->main_data,
2478            stream->next_frame - next_md_begin, next_md_begin);
2479     stream->md_len = next_md_begin;
2480   }
2481   else {
2482     if (md_len < si.main_data_begin) {
2483       unsigned int extra;
2484  
2485       extra = si.main_data_begin - md_len;
2486       if (extra + frame_free > next_md_begin)
2487         extra = next_md_begin - frame_free;
2488  
2489       if (extra < stream->md_len) {
2490         memmove(*stream->main_data,
2491                 *stream->main_data + stream->md_len - extra, extra);
2492         stream->md_len = extra;
2493       }
2494     }
2495     else
2496       stream->md_len = 0;
2497  
2498     memcpy(*stream->main_data + stream->md_len,
2499            stream->next_frame - frame_free, frame_free);
2500     stream->md_len += frame_free;
2501   }
2502  
2503   return result;
2504 }