xref: /reactos/sdk/lib/3rdparty/libmpg123/layer3.c (revision 5100859e)
1 /*
2 	layer3.c: the layer 3 decoder
3 
4 	copyright 1995-2017 by the mpg123 project - free software under the terms of the LGPL 2.1
5 	see COPYING and AUTHORS files in distribution or http://mpg123.org
6 	initially written by Michael Hipp
7 
8 	Dear visitor:
9 	If you feel you don't understand fully the works of this file, your feeling might be correct.
10 
11 	Optimize-TODO: put short bands into the band-field without the stride of 3 reals
12 	Length-optimze: unify long and short band code where it is possible
13 
14 	The int-vs-pointer situation has to be cleaned up.
15 */
16 
17 #include "mpg123lib_intern.h"
18 #ifdef USE_NEW_HUFFTABLE
19 #include "newhuffman.h"
20 #else
21 #include "huffman.h"
22 #endif
23 #include "getbits.h"
24 #include "debug.h"
25 
26 
27 
28 /* define CUT_SFB21 if you want to cut-off the frequency above 16kHz */
29 #if 0
30 #define CUT_SFB21
31 #endif
32 
33 #ifdef REAL_IS_FIXED
34 #define NEW_DCT9
35 #include "l3_integer_tables.h"
36 #else
37 /* static one-time calculated tables... or so */
38 static real ispow[8207];
39 static real aa_ca[8],aa_cs[8];
40 static ALIGNED(16) real win[4][36];
41 static ALIGNED(16) real win1[4][36];
42 real COS9[9]; /* dct36_3dnow wants to use that */
43 static real COS6_1,COS6_2;
44 real tfcos36[9]; /* dct36_3dnow wants to use that */
45 static real tfcos12[3];
46 #define NEW_DCT9
47 #ifdef NEW_DCT9
48 static real cos9[3],cos18[3];
49 static real tan1_1[16],tan2_1[16],tan1_2[16],tan2_2[16];
50 static real pow1_1[2][32],pow2_1[2][32],pow1_2[2][32],pow2_2[2][32];
51 #endif
52 #endif
53 
54 /* Decoder state data, living on the stack of do_layer3. */
55 
56 struct gr_info_s
57 {
58 	int scfsi;
59 	unsigned part2_3_length;
60 	unsigned big_values;
61 	unsigned scalefac_compress;
62 	unsigned block_type;
63 	unsigned mixed_block_flag;
64 	unsigned table_select[3];
65 	/* Making those two signed int as workaround for open64/pathscale/sun compilers, and also for consistency, since they're worked on together with other signed variables. */
66 	int maxband[3];
67 	int maxbandl;
68 	unsigned maxb;
69 	unsigned region1start;
70 	unsigned region2start;
71 	unsigned preflag;
72 	unsigned scalefac_scale;
73 	unsigned count1table_select;
74 	real *full_gain[3];
75 	real *pow2gain;
76 };
77 
78 struct III_sideinfo
79 {
80 	unsigned main_data_begin;
81 	unsigned private_bits;
82 	/* Hm, funny... struct inside struct... */
83 	struct { struct gr_info_s gr[2]; } ch[2];
84 };
85 
86 struct bandInfoStruct
87 {
88 	unsigned short longIdx[23];
89 	unsigned char longDiff[22];
90 	unsigned short shortIdx[14];
91 	unsigned char shortDiff[13];
92 };
93 
94 /* Techy details about our friendly MPEG data. Fairly constant over the years;-) */
95 static const struct bandInfoStruct bandInfo[9] =
96 {
97 	{ /* MPEG 1.0 */
98 		{0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
99 		{4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158},
100 		{0,4*3,8*3,12*3,16*3,22*3,30*3,40*3,52*3,66*3, 84*3,106*3,136*3,192*3},
101 		{4,4,4,4,6,8,10,12,14,18,22,30,56}
102 	},
103 	{
104 		{0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576},
105 		{4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192},
106 		{0,4*3,8*3,12*3,16*3,22*3,28*3,38*3,50*3,64*3, 80*3,100*3,126*3,192*3},
107 		{4,4,4,4,6,6,10,12,14,16,20,26,66}
108 	},
109 	{
110 		{0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
111 		{4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26},
112 		{0,4*3,8*3,12*3,16*3,22*3,30*3,42*3,58*3,78*3,104*3,138*3,180*3,192*3},
113 		{4,4,4,4,6,8,12,16,20,26,34,42,12}
114 	},
115 	{ /* MPEG 2.0 */
116 		{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
117 		{6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } ,
118 		{0,4*3,8*3,12*3,18*3,24*3,32*3,42*3,56*3,74*3,100*3,132*3,174*3,192*3} ,
119 		{4,4,4,6,6,8,10,14,18,26,32,42,18 }
120 	},
121 	{ /* Twiddling 3 values here (not just 330->332!) fixed bug 1895025. */
122 		{0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,332,394,464,540,576},
123 		{6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,54,62,70,76,36 },
124 		{0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,136*3,180*3,192*3},
125 		{4,4,4,6,8,10,12,14,18,24,32,44,12 }
126 	},
127 	{
128 		{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
129 		{6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 },
130 		{0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,134*3,174*3,192*3},
131 		{4,4,4,6,8,10,12,14,18,24,30,40,18 }
132 	},
133 	{ /* MPEG 2.5 */
134 		{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
135 		{6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
136 		{0,12,24,36,54,78,108,144,186,240,312,402,522,576},
137 		{4,4,4,6,8,10,12,14,18,24,30,40,18}
138 	},
139 	{
140 		{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
141 		{6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
142 		{0,12,24,36,54,78,108,144,186,240,312,402,522,576},
143 		{4,4,4,6,8,10,12,14,18,24,30,40,18}
144 	},
145 	{
146 		{0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576},
147 		{12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2},
148 		{0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576},
149 		{8,8,8,12,16,20,24,28,36,2,2,2,26}
150 	}
151 };
152 
153 static int mapbuf0[9][152];
154 static int mapbuf1[9][156];
155 static int mapbuf2[9][44];
156 static int *map[9][3];
157 static int *mapend[9][3];
158 
159 static unsigned int n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
160 static unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
161 
162 /* Some helpers used in init_layer3 */
163 
164 #ifdef OPT_MMXORSSE
165 real init_layer3_gainpow2_mmx(mpg123_handle *fr, int i)
166 {
167 	if(!fr->p.down_sample) return DOUBLE_TO_REAL(16384.0 * pow((double)2.0,-0.25 * (double) (i+210) ));
168 	else return DOUBLE_TO_REAL(pow((double)2.0,-0.25 * (double) (i+210)));
169 }
170 #endif
171 
172 real init_layer3_gainpow2(mpg123_handle *fr, int i)
173 {
174 #if defined(REAL_IS_FIXED) && defined(PRECALC_TABLES)
175 	return gainpow2[i+256];
176 #else
177 	return DOUBLE_TO_REAL_SCALE_LAYER3(pow((double)2.0,-0.25 * (double) (i+210)),i+256);
178 #endif
179 }
180 
181 
182 /* init tables for layer-3 ... specific with the downsampling... */
183 void init_layer3(void)
184 {
185 	int i,j,k,l;
186 
187 #if !defined(REAL_IS_FIXED) || !defined(PRECALC_TABLES)
188 	for(i=0;i<8207;i++)
189 	ispow[i] = DOUBLE_TO_REAL_POW43(pow((double)i,(double)4.0/3.0));
190 
191 	for(i=0;i<8;i++)
192 	{
193 		const double Ci[8] = {-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
194 		double sq = sqrt(1.0+Ci[i]*Ci[i]);
195 		aa_cs[i] = DOUBLE_TO_REAL(1.0/sq);
196 		aa_ca[i] = DOUBLE_TO_REAL(Ci[i]/sq);
197 	}
198 
199 	for(i=0;i<18;i++)
200 	{
201 		win[0][i]    = win[1][i]    =
202 			DOUBLE_TO_REAL( 0.5*sin(M_PI/72.0 * (double)(2*(i+0) +1)) / cos(M_PI * (double)(2*(i+0) +19) / 72.0) );
203 		win[0][i+18] = win[3][i+18] =
204 			DOUBLE_TO_REAL( 0.5*sin(M_PI/72.0 * (double)(2*(i+18)+1)) / cos(M_PI * (double)(2*(i+18)+19) / 72.0) );
205 	}
206 	for(i=0;i<6;i++)
207 	{
208 		win[1][i+18] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 ));
209 		win[3][i+12] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 ));
210 		win[1][i+24] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+13) ) / cos ( M_PI * (double) (2*(i+24)+19) / 72.0 ));
211 		win[1][i+30] = win[3][i] = DOUBLE_TO_REAL(0.0);
212 		win[3][i+6 ] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+1 ) ) / cos ( M_PI * (double) (2*(i+6 )+19) / 72.0 ));
213 	}
214 
215 	for(i=0;i<9;i++)
216 	COS9[i] = DOUBLE_TO_REAL(cos( M_PI / 18.0 * (double) i));
217 
218 	for(i=0;i<9;i++)
219 	tfcos36[i] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 ));
220 
221 	for(i=0;i<3;i++)
222 	tfcos12[i] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 ));
223 
224 	COS6_1 = DOUBLE_TO_REAL(cos( M_PI / 6.0 * (double) 1));
225 	COS6_2 = DOUBLE_TO_REAL(cos( M_PI / 6.0 * (double) 2));
226 
227 #ifdef NEW_DCT9
228 	cos9[0]  = DOUBLE_TO_REAL(cos(1.0*M_PI/9.0));
229 	cos9[1]  = DOUBLE_TO_REAL(cos(5.0*M_PI/9.0));
230 	cos9[2]  = DOUBLE_TO_REAL(cos(7.0*M_PI/9.0));
231 	cos18[0] = DOUBLE_TO_REAL(cos(1.0*M_PI/18.0));
232 	cos18[1] = DOUBLE_TO_REAL(cos(11.0*M_PI/18.0));
233 	cos18[2] = DOUBLE_TO_REAL(cos(13.0*M_PI/18.0));
234 #endif
235 
236 	for(i=0;i<12;i++)
237 	{
238 		win[2][i] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*i+7) / 24.0 ));
239 	}
240 
241 	for(i=0;i<16;i++)
242 	{
243 		double t = tan( (double) i * M_PI / 12.0 );
244 		tan1_1[i] = DOUBLE_TO_REAL_15(t / (1.0+t));
245 		tan2_1[i] = DOUBLE_TO_REAL_15(1.0 / (1.0 + t));
246 		tan1_2[i] = DOUBLE_TO_REAL_15(M_SQRT2 * t / (1.0+t));
247 		tan2_2[i] = DOUBLE_TO_REAL_15(M_SQRT2 / (1.0 + t));
248 	}
249 
250 	for(i=0;i<32;i++)
251 	{
252 		for(j=0;j<2;j++)
253 		{
254 			double base = pow(2.0,-0.25*(j+1.0));
255 			double p1=1.0,p2=1.0;
256 			if(i > 0)
257 			{
258 				if( i & 1 ) p1 = pow(base,(i+1.0)*0.5);
259 				else p2 = pow(base,i*0.5);
260 			}
261 			pow1_1[j][i] = DOUBLE_TO_REAL_15(p1);
262 			pow2_1[j][i] = DOUBLE_TO_REAL_15(p2);
263 			pow1_2[j][i] = DOUBLE_TO_REAL_15(M_SQRT2 * p1);
264 			pow2_2[j][i] = DOUBLE_TO_REAL_15(M_SQRT2 * p2);
265 		}
266 	}
267 #endif
268 
269 	for(j=0;j<4;j++)
270 	{
271 		const int len[4] = { 36,36,12,36 };
272 		for(i=0;i<len[j];i+=2) win1[j][i] = + win[j][i];
273 
274 		for(i=1;i<len[j];i+=2) win1[j][i] = - win[j][i];
275 	}
276 
277 	for(j=0;j<9;j++)
278 	{
279 		const struct bandInfoStruct *bi = &bandInfo[j];
280 		int *mp;
281 		int cb,lwin;
282 		const unsigned char *bdf;
283 		int switch_idx;
284 
285 		mp = map[j][0] = mapbuf0[j];
286 		bdf = bi->longDiff;
287 		switch_idx = (j < 3) ? 8 : 6;
288 		for(i=0,cb = 0; cb < switch_idx ; cb++,i+=*bdf++)
289 		{
290 			*mp++ = (*bdf) >> 1;
291 			*mp++ = i;
292 			*mp++ = 3;
293 			*mp++ = cb;
294 		}
295 		bdf = bi->shortDiff+3;
296 		for(cb=3;cb<13;cb++)
297 		{
298 			int l = (*bdf++) >> 1;
299 			for(lwin=0;lwin<3;lwin++)
300 			{
301 				*mp++ = l;
302 				*mp++ = i + lwin;
303 				*mp++ = lwin;
304 				*mp++ = cb;
305 			}
306 			i += 6*l;
307 		}
308 		mapend[j][0] = mp;
309 
310 		mp = map[j][1] = mapbuf1[j];
311 		bdf = bi->shortDiff+0;
312 		for(i=0,cb=0;cb<13;cb++)
313 		{
314 			int l = (*bdf++) >> 1;
315 			for(lwin=0;lwin<3;lwin++)
316 			{
317 				*mp++ = l;
318 				*mp++ = i + lwin;
319 				*mp++ = lwin;
320 				*mp++ = cb;
321 			}
322 			i += 6*l;
323 		}
324 		mapend[j][1] = mp;
325 
326 		mp = map[j][2] = mapbuf2[j];
327 		bdf = bi->longDiff;
328 		for(cb = 0; cb < 22 ; cb++)
329 		{
330 			*mp++ = (*bdf++) >> 1;
331 			*mp++ = cb;
332 		}
333 		mapend[j][2] = mp;
334 	}
335 
336 	/* Now for some serious loopings! */
337 	for(i=0;i<5;i++)
338 	for(j=0;j<6;j++)
339 	for(k=0;k<6;k++)
340 	{
341 		int n = k + j * 6 + i * 36;
342 		i_slen2[n] = i|(j<<3)|(k<<6)|(3<<12);
343 	}
344 	for(i=0;i<4;i++)
345 	for(j=0;j<4;j++)
346 	for(k=0;k<4;k++)
347 	{
348 		int n = k + j * 4 + i * 16;
349 		i_slen2[n+180] = i|(j<<3)|(k<<6)|(4<<12);
350 	}
351 	for(i=0;i<4;i++)
352 	for(j=0;j<3;j++)
353 	{
354 		int n = j + i * 3;
355 		i_slen2[n+244] = i|(j<<3) | (5<<12);
356 		n_slen2[n+500] = i|(j<<3) | (2<<12) | (1<<15);
357 	}
358 	for(i=0;i<5;i++)
359 	for(j=0;j<5;j++)
360 	for(k=0;k<4;k++)
361 	for(l=0;l<4;l++)
362 	{
363 		int n = l + k * 4 + j * 16 + i * 80;
364 		n_slen2[n] = i|(j<<3)|(k<<6)|(l<<9)|(0<<12);
365 	}
366 	for(i=0;i<5;i++)
367 	for(j=0;j<5;j++)
368 	for(k=0;k<4;k++)
369 	{
370 		int n = k + j * 4 + i * 20;
371 		n_slen2[n+400] = i|(j<<3)|(k<<6)|(1<<12);
372 	}
373 }
374 
375 
376 void init_layer3_stuff(mpg123_handle *fr, real (*gainpow2)(mpg123_handle *fr, int i))
377 {
378 	int i,j;
379 
380 	for(i=-256;i<118+4;i++)	fr->gainpow2[i+256] = gainpow2(fr,i);
381 
382 	for(j=0;j<9;j++)
383 	{
384 		for(i=0;i<23;i++)
385 		{
386 			fr->longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
387 			if(fr->longLimit[j][i] > (fr->down_sample_sblimit) )
388 			fr->longLimit[j][i] = fr->down_sample_sblimit;
389 		}
390 		for(i=0;i<14;i++)
391 		{
392 			fr->shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
393 			if(fr->shortLimit[j][i] > (fr->down_sample_sblimit) )
394 			fr->shortLimit[j][i] = fr->down_sample_sblimit;
395 		}
396 	}
397 }
398 
399 /*
400 	Observe!
401 	Now come the actualy decoding routines.
402 */
403 
404 /* read additional side information (for MPEG 1 and MPEG 2) */
405 static int III_get_side_info(mpg123_handle *fr, struct III_sideinfo *si,int stereo, int ms_stereo,long sfreq,int single)
406 {
407 	int ch, gr;
408 	int powdiff = (single == SINGLE_MIX) ? 4 : 0;
409 
410 	const int tabs[2][5] = { { 2,9,5,3,4 } , { 1,8,1,2,9 } };
411 	const int *tab = tabs[fr->lsf];
412 
413 	si->main_data_begin = getbits(fr, tab[1]);
414 
415 	if(si->main_data_begin > fr->bitreservoir)
416 	{
417 		if(!fr->to_ignore && VERBOSE2) fprintf(stderr, "Note: missing %d bytes in bit reservoir for frame %li\n", (int)(si->main_data_begin - fr->bitreservoir), (long)fr->num);
418 
419 		/*  overwrite main_data_begin for the really available bit reservoir */
420 		backbits(fr, tab[1]);
421 		if(fr->lsf == 0)
422 		{
423 			fr->wordpointer[0] = (unsigned char) (fr->bitreservoir >> 1);
424 			fr->wordpointer[1] = (unsigned char) ((fr->bitreservoir & 1) << 7);
425 		}
426 		else fr->wordpointer[0] = (unsigned char) fr->bitreservoir;
427 
428 		/* zero "side-info" data for a silence-frame
429 		without touching audio data used as bit reservoir for following frame */
430 		memset(fr->wordpointer+2, 0, fr->ssize-2);
431 
432 		/* reread the new bit reservoir offset */
433 		si->main_data_begin = getbits(fr, tab[1]);
434 	}
435 
436 	/* Keep track of the available data bytes for the bit reservoir.
437 	Think: Substract the 2 crc bytes in parser already? */
438 	fr->bitreservoir = fr->bitreservoir + fr->framesize - fr->ssize - (fr->error_protection ? 2 : 0);
439 	/* Limit the reservoir to the max for MPEG 1.0 or 2.x . */
440 	if(fr->bitreservoir > (unsigned int) (fr->lsf == 0 ? 511 : 255))
441 	fr->bitreservoir = (fr->lsf == 0 ? 511 : 255);
442 
443 	/* Now back into less commented territory. It's code. It works. */
444 
445 	if (stereo == 1)
446 	si->private_bits = getbits_fast(fr, tab[2]);
447 	else
448 	si->private_bits = getbits_fast(fr, tab[3]);
449 
450 	if(!fr->lsf) for(ch=0; ch<stereo; ch++)
451 	{
452 		si->ch[ch].gr[0].scfsi = -1;
453 		si->ch[ch].gr[1].scfsi = getbits_fast(fr, 4);
454 	}
455 
456 	for (gr=0; gr<tab[0]; gr++)
457 	for (ch=0; ch<stereo; ch++)
458 	{
459 		register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
460 
461 		gr_info->part2_3_length = getbits(fr, 12);
462 		gr_info->big_values = getbits(fr, 9);
463 		if(gr_info->big_values > 288)
464 		{
465 			if(NOQUIET) error("big_values too large!");
466 			gr_info->big_values = 288;
467 		}
468 		gr_info->pow2gain = fr->gainpow2+256 - getbits_fast(fr, 8) + powdiff;
469 		if(ms_stereo) gr_info->pow2gain += 2;
470 		gr_info->scalefac_compress = getbits(fr, tab[4]);
471 		if(gr_info->part2_3_length == 0)
472 		{
473 			if(gr_info->scalefac_compress > 0)
474 				debug1( "scalefac_compress _should_ be zero instead of %i"
475 				,	gr_info->scalefac_compress );
476 			gr_info->scalefac_compress = 0;
477 		}
478 
479 		if(get1bit(fr))
480 		{ /* window switch flag  */
481 			int i;
482 			gr_info->block_type       = getbits_fast(fr, 2);
483 			gr_info->mixed_block_flag = get1bit(fr);
484 			gr_info->table_select[0]  = getbits_fast(fr, 5);
485 			gr_info->table_select[1]  = getbits_fast(fr, 5);
486 			/*
487 				table_select[2] not needed, because there is no region2,
488 				but to satisfy some verification tools we set it either.
489 			*/
490 			gr_info->table_select[2] = 0;
491 			for(i=0;i<3;i++)
492 			gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(fr, 3)<<3);
493 
494 			if(gr_info->block_type == 0)
495 			{
496 				if(NOQUIET) error("Blocktype == 0 and window-switching == 1 not allowed.");
497 				return 1;
498 			}
499 
500 			/* region_count/start parameters are implicit in this case. */
501 			if( (!fr->lsf || (gr_info->block_type == 2)) && !fr->mpeg25)
502 			{
503 				gr_info->region1start = 36>>1;
504 				gr_info->region2start = 576>>1;
505 			}
506 			else
507 			{
508 				if(fr->mpeg25)
509 				{
510 					int r0c,r1c;
511 					if((gr_info->block_type == 2) && (!gr_info->mixed_block_flag) ) r0c = 5;
512 					else r0c = 7;
513 
514 					/* r0c+1+r1c+1 == 22, always. */
515 					r1c = 20 - r0c;
516 					gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
517 					gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
518 				}
519 				else
520 				{
521 					gr_info->region1start = 54>>1;
522 					gr_info->region2start = 576>>1;
523 				}
524 			}
525 		}
526 		else
527 		{
528 			int i,r0c,r1c;
529 			for (i=0; i<3; i++)
530 			gr_info->table_select[i] = getbits_fast(fr, 5);
531 
532 			r0c = getbits_fast(fr, 4); /* 0 .. 15 */
533 			r1c = getbits_fast(fr, 3); /* 0 .. 7 */
534 			gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
535 
536 			/* max(r0c+r1c+2) = 15+7+2 = 24 */
537 			if(r0c+1+r1c+1 > 22) gr_info->region2start = 576>>1;
538 			else gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
539 
540 			gr_info->block_type = 0;
541 			gr_info->mixed_block_flag = 0;
542 		}
543 		if(!fr->lsf) gr_info->preflag = get1bit(fr);
544 
545 		gr_info->scalefac_scale = get1bit(fr);
546 		gr_info->count1table_select = get1bit(fr);
547 	}
548 	return 0;
549 }
550 
551 
552 /* read scalefactors */
553 static int III_get_scale_factors_1(mpg123_handle *fr, int *scf,struct gr_info_s *gr_info,int ch,int gr)
554 {
555 	const unsigned char slen[2][16] =
556 	{
557 		{0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
558 		{0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
559 	};
560 	int numbits;
561 	int num0 = slen[0][gr_info->scalefac_compress];
562 	int num1 = slen[1][gr_info->scalefac_compress];
563 
564 	if(gr_info->part2_3_length == 0)
565 	{
566 		int i;
567 		for(i=0;i<39;i++)
568 			*scf++ = 0;
569 		return 0;
570 	}
571 
572 	if(gr_info->block_type == 2)
573 	{
574 		int i=18;
575 		numbits = (num0 + num1) * 18;
576 
577 		if(gr_info->mixed_block_flag)
578 		{
579 			for (i=8;i;i--)
580 			*scf++ = getbits_fast(fr, num0);
581 
582 			i = 9;
583 			numbits -= num0; /* num0 * 17 + num1 * 18 */
584 		}
585 
586 		for(;i;i--) *scf++ = getbits_fast(fr, num0);
587 
588 		for(i = 18; i; i--) *scf++ = getbits_fast(fr, num1);
589 
590 		*scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */
591 	}
592 	else
593 	{
594 		int i;
595 		int scfsi = gr_info->scfsi;
596 
597 		if(scfsi < 0)
598 		{ /* scfsi < 0 => granule == 0 */
599 			for(i=11;i;i--) *scf++ = getbits_fast(fr, num0);
600 
601 			for(i=10;i;i--) *scf++ = getbits_fast(fr, num1);
602 
603 			numbits = (num0 + num1) * 10 + num0;
604 			*scf++ = 0;
605 		}
606 		else
607 		{
608 			numbits = 0;
609 			if(!(scfsi & 0x8))
610 			{
611 				for (i=0;i<6;i++) *scf++ = getbits_fast(fr, num0);
612 
613 				numbits += num0 * 6;
614 			}
615 			else scf += 6;
616 
617 			if(!(scfsi & 0x4))
618 			{
619 				for (i=0;i<5;i++) *scf++ = getbits_fast(fr, num0);
620 
621 				numbits += num0 * 5;
622 			}
623 			else scf += 5;
624 
625 			if(!(scfsi & 0x2))
626 			{
627 				for(i=0;i<5;i++) *scf++ = getbits_fast(fr, num1);
628 
629 				numbits += num1 * 5;
630 			}
631 			else scf += 5;
632 
633 			if(!(scfsi & 0x1))
634 			{
635 				for (i=0;i<5;i++) *scf++ = getbits_fast(fr, num1);
636 
637 				numbits += num1 * 5;
638 			}
639 			else scf += 5;
640 
641 			*scf++ = 0;  /* no l[21] in original sources */
642 		}
643 	}
644 	return numbits;
645 }
646 
647 
648 static int III_get_scale_factors_2(mpg123_handle *fr, int *scf,struct gr_info_s *gr_info,int i_stereo)
649 {
650 	const unsigned char *pnt;
651 	int i,j,n=0,numbits=0;
652 	unsigned int slen;
653 
654 	const unsigned char stab[3][6][4] =
655 	{
656 		{
657 			{ 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0},
658 			{ 7, 7, 7,0 } , { 6, 6, 6,3 } , {  8, 8,5,0}
659 		},
660 		{
661 			{ 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0},
662 			{12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0}
663 		},
664 		{
665 			{ 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0},
666 			{ 6,15,12,0 } , { 6,12, 9,6 } , {  6,18,9,0}
667 		}
668 	};
669 
670 	if(i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */
671 	slen = i_slen2[gr_info->scalefac_compress>>1];
672 	else
673 	slen = n_slen2[gr_info->scalefac_compress];
674 
675 	gr_info->preflag = (slen>>15) & 0x1;
676 
677 	n = 0;
678 	if( gr_info->block_type == 2 )
679 	{
680 		n++;
681 		if(gr_info->mixed_block_flag) n++;
682 	}
683 
684 	pnt = stab[n][(slen>>12)&0x7];
685 
686 	if(gr_info->part2_3_length == 0)
687 	{
688 		int i;
689 		for(i=0;i<39;i++)
690 			*scf++ = 0;
691 		return 0;
692 	}
693 
694 	for(i=0;i<4;i++)
695 	{
696 		int num = slen & 0x7;
697 		slen >>= 3;
698 		if(num)
699 		{
700 			for(j=0;j<(int)(pnt[i]);j++) *scf++ = getbits_fast(fr, num);
701 
702 			numbits += pnt[i] * num;
703 		}
704 		else
705 		for(j=0;j<(int)(pnt[i]);j++) *scf++ = 0;
706 	}
707 
708 	n = (n << 1) + 1;
709 	for(i=0;i<n;i++) *scf++ = 0;
710 
711 	return numbits;
712 }
713 
714 static unsigned char pretab_choice[2][22] =
715 {
716 	{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
717 	{0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0}
718 };
719 
720 /*
721 	Dequantize samples
722 	...includes Huffman decoding
723 */
724 
725 /* 24 is enough because tab13 has max. a 19 bit huffvector */
726 /* The old code played games with shifting signed integers around in not quite */
727 /* legal ways. Also, it used long where just 32 bits are required. This could */
728 /* be good or bad on 64 bit architectures ... anyway, making clear that */
729 /* 32 bits suffice is a benefit. */
730 #if 0
731 /* To reconstruct old code, use this: */
732 #define MASK_STYPE long
733 #define MASK_UTYPE unsigned long
734 #define MASK_TYPE MASK_STYPE
735 #define MSB_MASK (mask < 0)
736 #else
737 /* This should be more proper: */
738 #define MASK_STYPE int32_t
739 #define MASK_UTYPE uint32_t
740 #define MASK_TYPE  MASK_UTYPE
741 #define MSB_MASK ((MASK_UTYPE)mask & (MASK_UTYPE)1<<(sizeof(MASK_TYPE)*8-1))
742 #endif
743 #define BITSHIFT ((sizeof(MASK_TYPE)-1)*8)
744 #define REFRESH_MASK \
745 	while(num < BITSHIFT) { \
746 		mask |= ((MASK_UTYPE)getbyte(fr))<<(BITSHIFT-num); \
747 		num += 8; \
748 		part2remain -= 8; }
749 /* Complicated way of checking for msb value. This used to be (mask < 0). */
750 
751 static int III_dequantize_sample(mpg123_handle *fr, real xr[SBLIMIT][SSLIMIT],int *scf, struct gr_info_s *gr_info,int sfreq,int part2bits)
752 {
753 	int shift = 1 + gr_info->scalefac_scale;
754 	real *xrpnt = (real *) xr;
755 	int l[3],l3;
756 	int part2remain = gr_info->part2_3_length - part2bits;
757 	int *me;
758 #ifdef REAL_IS_FIXED
759 	int gainpow2_scale_idx = 378;
760 #endif
761 
762 	/* Assumption: If there is some part2_3_length at all, there should be
763 	   enough of it to work with properly. In case of zero length we silently
764 	   zero things. */
765 	if(gr_info->part2_3_length > 0)
766 	{
767 
768 	/* mhipp tree has this split up a bit... */
769 	int num=getbitoffset(fr);
770 	MASK_TYPE mask;
771 	/* We must split this, because for num==0 the shift is undefined if you do it in one step. */
772 	mask  = ((MASK_UTYPE) getbits(fr, num))<<BITSHIFT;
773 	mask <<= 8-num;
774 	part2remain -= num;
775 
776 	{
777 		int bv       = gr_info->big_values;
778 		int region1  = gr_info->region1start;
779 		int region2  = gr_info->region2start;
780 		l3 = ((576>>1)-bv)>>1;
781 
782 		/* we may lose the 'odd' bit here !! check this later again */
783 		if(bv <= region1)
784 		{
785 			l[0] = bv;
786 			l[1] = 0;
787 			l[2] = 0;
788 		}
789 		else
790 		{
791 			l[0] = region1;
792 			if(bv <= region2)
793 			{
794 				l[1] = bv - l[0];
795 				l[2] = 0;
796 			}
797 			else
798 			{
799 				l[1] = region2 - l[0];
800 				l[2] = bv - region2;
801 			}
802 		}
803 	}
804 
805 #define CHECK_XRPNT if(xrpnt >= &xr[SBLIMIT][0]) \
806 { \
807 	if(NOQUIET) \
808 		error2("attempted xrpnt overflow (%p !< %p)", (void*) xrpnt, (void*) &xr[SBLIMIT][0]); \
809 	return 1; \
810 }
811 
812 	if(gr_info->block_type == 2)
813 	{
814 		/* decoding with short or mixed mode BandIndex table */
815 		int i,max[4];
816 		int step=0,lwin=3,cb=0;
817 		register real v = 0.0;
818 		register int *m,mc;
819 
820 		if(gr_info->mixed_block_flag)
821 		{
822 			max[3] = -1;
823 			max[0] = max[1] = max[2] = 2;
824 			m = map[sfreq][0];
825 			me = mapend[sfreq][0];
826 		}
827 		else
828 		{
829 			max[0] = max[1] = max[2] = max[3] = -1;
830 			/* max[3] not really needed in this case */
831 			m = map[sfreq][1];
832 			me = mapend[sfreq][1];
833 		}
834 
835 		mc = 0;
836 		for(i=0;i<2;i++)
837 		{
838 			int lp = l[i];
839 			const struct newhuff *h = ht+gr_info->table_select[i];
840 			for(;lp;lp--,mc--)
841 			{
842 				register MASK_STYPE x,y;
843 				if( (!mc) )
844 				{
845 					mc    = *m++;
846 					xrpnt = ((real *) xr) + (*m++);
847 					lwin  = *m++;
848 					cb    = *m++;
849 					if(lwin == 3)
850 					{
851 #ifdef REAL_IS_FIXED
852 						gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
853 #endif
854 						v = gr_info->pow2gain[(*scf++) << shift];
855 						step = 1;
856 					}
857 					else
858 					{
859 #ifdef REAL_IS_FIXED
860 						gainpow2_scale_idx = (int)(gr_info->full_gain[lwin] + (*scf << shift) - fr->gainpow2);
861 #endif
862 						v = gr_info->full_gain[lwin][(*scf++) << shift];
863 						step = 3;
864 					}
865 				}
866 				{
867 					const short *val = h->table;
868 					REFRESH_MASK;
869 #ifdef USE_NEW_HUFFTABLE
870 					while((y=val[(MASK_UTYPE)mask>>(BITSHIFT+4)])<0)
871 					{
872 						val -= y;
873 						num -= 4;
874 						mask <<= 4;
875 					}
876 					num -= (y >> 8);
877 					mask <<= (y >> 8);
878 					x = (y >> 4) & 0xf;
879 					y &= 0xf;
880 #else
881 					while((y=*val++)<0)
882 					{
883 						if (MSB_MASK) val -= y;
884 
885 						num--;
886 						mask <<= 1;
887 					}
888 					x = y >> 4;
889 					y &= 0xf;
890 #endif
891 				}
892 				CHECK_XRPNT;
893 				if(x == 15 && h->linbits)
894 				{
895 					max[lwin] = cb;
896 					REFRESH_MASK;
897 					x += ((MASK_UTYPE) mask) >> (BITSHIFT+8-h->linbits);
898 					num -= h->linbits+1;
899 					mask <<= h->linbits;
900 					if(MSB_MASK) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
901 					else         *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
902 
903 					mask <<= 1;
904 				}
905 				else if(x)
906 				{
907 					max[lwin] = cb;
908 					if(MSB_MASK) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
909 					else         *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
910 
911 					num--;
912 					mask <<= 1;
913 				}
914 				else *xrpnt = DOUBLE_TO_REAL(0.0);
915 
916 				xrpnt += step;
917 				CHECK_XRPNT;
918 				if(y == 15 && h->linbits)
919 				{
920 					max[lwin] = cb;
921 					REFRESH_MASK;
922 					y += ((MASK_UTYPE) mask) >> (BITSHIFT+8-h->linbits);
923 					num -= h->linbits+1;
924 					mask <<= h->linbits;
925 					if(MSB_MASK) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
926 					else         *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
927 
928 					mask <<= 1;
929 				}
930 				else if(y)
931 				{
932 					max[lwin] = cb;
933 					if(MSB_MASK) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
934 					else         *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
935 
936 					num--;
937 					mask <<= 1;
938 				}
939 				else *xrpnt = DOUBLE_TO_REAL(0.0);
940 
941 				xrpnt += step;
942 			}
943 		}
944 
945 		for(;l3 && (part2remain+num > 0);l3--)
946 		{
947 			const struct newhuff* h;
948 			const short* val;
949 			register short a;
950 
951 			h = htc+gr_info->count1table_select;
952 			val = h->table;
953 
954 			REFRESH_MASK;
955 			while((a=*val++)<0)
956 			{
957 				if(MSB_MASK) val -= a;
958 
959 				num--;
960 				mask <<= 1;
961 			}
962 			if(part2remain+num <= 0)
963 			{
964 				num -= part2remain+num;
965 				break;
966 			}
967 
968 			for(i=0;i<4;i++)
969 			{
970 				if(!(i & 1))
971 				{
972 					if(!mc)
973 					{
974 						mc = *m++;
975 						xrpnt = ((real *) xr) + (*m++);
976 						lwin = *m++;
977 						cb = *m++;
978 						if(lwin == 3)
979 						{
980 #ifdef REAL_IS_FIXED
981 							gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
982 #endif
983 							v = gr_info->pow2gain[(*scf++) << shift];
984 							step = 1;
985 						}
986 						else
987 						{
988 #ifdef REAL_IS_FIXED
989 							gainpow2_scale_idx = (int)(gr_info->full_gain[lwin] + (*scf << shift) - fr->gainpow2);
990 #endif
991 							v = gr_info->full_gain[lwin][(*scf++) << shift];
992 							step = 3;
993 						}
994 					}
995 					mc--;
996 				}
997 				CHECK_XRPNT;
998 				if( (a & (0x8>>i)) )
999 				{
1000 					max[lwin] = cb;
1001 					if(part2remain+num <= 0)
1002 					break;
1003 
1004 					if(MSB_MASK) *xrpnt = -REAL_SCALE_LAYER3(v, gainpow2_scale_idx);
1005 					else         *xrpnt =  REAL_SCALE_LAYER3(v, gainpow2_scale_idx);
1006 
1007 					num--;
1008 					mask <<= 1;
1009 				}
1010 				else *xrpnt = DOUBLE_TO_REAL(0.0);
1011 
1012 				xrpnt += step;
1013 			}
1014 		}
1015 
1016 		if(lwin < 3)
1017 		{ /* short band? */
1018 			while(1)
1019 			{
1020 				for(;mc > 0;mc--)
1021 				{
1022 					CHECK_XRPNT;
1023 					*xrpnt = DOUBLE_TO_REAL(0.0); xrpnt += 3; /* short band -> step=3 */
1024 					*xrpnt = DOUBLE_TO_REAL(0.0); xrpnt += 3;
1025 				}
1026 				if(m >= me)
1027 				break;
1028 
1029 				mc    = *m++;
1030 				xrpnt = ((real *) xr) + *m++;
1031 				if(*m++ == 0)
1032 				break; /* optimize: field will be set to zero at the end of the function */
1033 
1034 				m++; /* cb */
1035 			}
1036 		}
1037 
1038 		gr_info->maxband[0] = max[0]+1;
1039 		gr_info->maxband[1] = max[1]+1;
1040 		gr_info->maxband[2] = max[2]+1;
1041 		gr_info->maxbandl   = max[3]+1;
1042 
1043 		{
1044 			int rmax = max[0] > max[1] ? max[0] : max[1];
1045 			rmax = (rmax > max[2] ? rmax : max[2]) + 1;
1046 			gr_info->maxb = rmax ? fr->shortLimit[sfreq][rmax] : fr->longLimit[sfreq][max[3]+1];
1047 		}
1048 
1049 	}
1050 	else
1051 	{
1052 		/* decoding with 'long' BandIndex table (block_type != 2) */
1053 		const unsigned char *pretab = pretab_choice[gr_info->preflag];
1054 		int i,max = -1;
1055 		int cb = 0;
1056 		int *m = map[sfreq][2];
1057 		register real v = 0.0;
1058 		int mc = 0;
1059 
1060 		/* long hash table values */
1061 		for(i=0;i<3;i++)
1062 		{
1063 			int lp = l[i];
1064 			const struct newhuff *h = ht+gr_info->table_select[i];
1065 
1066 			for(;lp;lp--,mc--)
1067 			{
1068 				MASK_STYPE x,y;
1069 				if(!mc)
1070 				{
1071 					mc = *m++;
1072 					cb = *m++;
1073 #ifdef CUT_SFB21
1074 					if(cb == 21)
1075 						v = 0.0;
1076 					else
1077 #endif
1078 					{
1079 #ifdef REAL_IS_FIXED
1080 						gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
1081 #endif
1082 						v = gr_info->pow2gain[(*(scf++) + (*pretab++)) << shift];
1083 					}
1084 				}
1085 				{
1086 					const short *val = h->table;
1087 					REFRESH_MASK;
1088 #ifdef USE_NEW_HUFFTABLE
1089 					while((y=val[(MASK_UTYPE)mask>>(BITSHIFT+4)])<0)
1090 					{
1091 						val -= y;
1092 						num -= 4;
1093 						mask <<= 4;
1094 					}
1095 					num -= (y >> 8);
1096 					mask <<= (y >> 8);
1097 					x = (y >> 4) & 0xf;
1098 					y &= 0xf;
1099 #else
1100 					while((y=*val++)<0)
1101 					{
1102 						if (MSB_MASK) val -= y;
1103 
1104 						num--;
1105 						mask <<= 1;
1106 					}
1107 					x = y >> 4;
1108 					y &= 0xf;
1109 #endif
1110 				}
1111 
1112 				CHECK_XRPNT;
1113 				if(x == 15 && h->linbits)
1114 				{
1115 					max = cb;
1116 					REFRESH_MASK;
1117 					x += ((MASK_UTYPE) mask) >> (BITSHIFT+8-h->linbits);
1118 					num -= h->linbits+1;
1119 					mask <<= h->linbits;
1120 					if(MSB_MASK) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
1121 					else         *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
1122 
1123 					mask <<= 1;
1124 				}
1125 				else if(x)
1126 				{
1127 					max = cb;
1128 					if(MSB_MASK) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
1129 					else         *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
1130 					num--;
1131 
1132 					mask <<= 1;
1133 				}
1134 				else *xrpnt++ = DOUBLE_TO_REAL(0.0);
1135 
1136 				CHECK_XRPNT;
1137 				if(y == 15 && h->linbits)
1138 				{
1139 					max = cb;
1140 					REFRESH_MASK;
1141 					y += ((MASK_UTYPE) mask) >> (BITSHIFT+8-h->linbits);
1142 					num -= h->linbits+1;
1143 					mask <<= h->linbits;
1144 					if(MSB_MASK) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
1145 					else         *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
1146 
1147 					mask <<= 1;
1148 				}
1149 				else if(y)
1150 				{
1151 					max = cb;
1152 					if(MSB_MASK) *xrpnt++ = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
1153 					else         *xrpnt++ = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
1154 
1155 					num--;
1156 					mask <<= 1;
1157 				}
1158 				else *xrpnt++ = DOUBLE_TO_REAL(0.0);
1159 			}
1160 		}
1161 
1162 		/* short (count1table) values */
1163 		for(;l3 && (part2remain+num > 0);l3--)
1164 		{
1165 			const struct newhuff *h = htc+gr_info->count1table_select;
1166 			const short *val = h->table;
1167 			register short a;
1168 
1169 			REFRESH_MASK;
1170 			while((a=*val++)<0)
1171 			{
1172 				if (MSB_MASK) val -= a;
1173 
1174 				num--;
1175 				mask <<= 1;
1176 			}
1177 			if(part2remain+num <= 0)
1178 			{
1179 				num -= part2remain+num;
1180 				break;
1181 			}
1182 
1183 			for(i=0;i<4;i++)
1184 			{
1185 				if(!(i & 1))
1186 				{
1187 					if(!mc)
1188 					{
1189 						mc = *m++;
1190 						cb = *m++;
1191 #ifdef CUT_SFB21
1192 						if(cb == 21)
1193 							v = 0.0;
1194 						else
1195 #endif
1196 						{
1197 #ifdef REAL_IS_FIXED
1198 							gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
1199 #endif
1200 							v = gr_info->pow2gain[((*scf++) + (*pretab++)) << shift];
1201 						}
1202 					}
1203 					mc--;
1204 				}
1205 				CHECK_XRPNT;
1206 				if( (a & (0x8>>i)) )
1207 				{
1208 					max = cb;
1209 					if(part2remain+num <= 0)
1210 					break;
1211 
1212 					if(MSB_MASK) *xrpnt++ = -REAL_SCALE_LAYER3(v, gainpow2_scale_idx);
1213 					else         *xrpnt++ =  REAL_SCALE_LAYER3(v, gainpow2_scale_idx);
1214 
1215 					num--;
1216 					mask <<= 1;
1217 				}
1218 				else *xrpnt++ = DOUBLE_TO_REAL(0.0);
1219 			}
1220 		}
1221 
1222 		gr_info->maxbandl = max+1;
1223 		gr_info->maxb = fr->longLimit[sfreq][gr_info->maxbandl];
1224 	}
1225 
1226 	part2remain += num;
1227 	backbits(fr, num);
1228 	num = 0;
1229 
1230 	}
1231 	else
1232 	{
1233 		part2remain = 0;
1234 		/* Not entirely sure what good values are, must be > 0. */
1235 		gr_info->maxband[0] =
1236 		gr_info->maxband[1] =
1237 		gr_info->maxband[2] =
1238 		gr_info->maxbandl   = 0;
1239 		gr_info->maxb       = 1;
1240 	}
1241 
1242 	while(xrpnt < &xr[SBLIMIT][0])
1243 	*xrpnt++ = DOUBLE_TO_REAL(0.0);
1244 
1245 	while( part2remain > 16 )
1246 	{
1247 		skipbits(fr, 16); /* Dismiss stuffing Bits */
1248 		part2remain -= 16;
1249 	}
1250 	if(part2remain > 0) skipbits(fr, part2remain);
1251 	else if(part2remain < 0)
1252 	{
1253 		debug1("Can't rewind stream by %d bits!",-part2remain);
1254 		return 1; /* -> error */
1255 	}
1256 	return 0;
1257 }
1258 
1259 
1260 /* calculate real channel values for Joint-I-Stereo-mode */
1261 static void III_i_stereo(real xr_buf[2][SBLIMIT][SSLIMIT],int *scalefac, struct gr_info_s *gr_info,int sfreq,int ms_stereo,int lsf)
1262 {
1263 	real (*xr)[SBLIMIT*SSLIMIT] = (real (*)[SBLIMIT*SSLIMIT] ) xr_buf;
1264 	const struct bandInfoStruct *bi = &bandInfo[sfreq];
1265 
1266 	const real *tab1,*tab2;
1267 
1268 #if 1
1269 	int tab;
1270 /* TODO: optimize as static */
1271 	const real *tabs[3][2][2] =
1272 	{
1273 		{ { tan1_1,tan2_1 }       , { tan1_2,tan2_2 } },
1274 		{ { pow1_1[0],pow2_1[0] } , { pow1_2[0],pow2_2[0] } },
1275 		{ { pow1_1[1],pow2_1[1] } , { pow1_2[1],pow2_2[1] } }
1276 	};
1277 
1278 	tab = lsf + (gr_info->scalefac_compress & lsf);
1279 	tab1 = tabs[tab][ms_stereo][0];
1280 	tab2 = tabs[tab][ms_stereo][1];
1281 #else
1282 	if(lsf)
1283 	{
1284 		int p = gr_info->scalefac_compress & 0x1;
1285 		if(ms_stereo)
1286 		{
1287 			tab1 = pow1_2[p];
1288 			tab2 = pow2_2[p];
1289 		}
1290 		else
1291 		{
1292 			tab1 = pow1_1[p];
1293 			tab2 = pow2_1[p];
1294 		}
1295 	}
1296 	else
1297 	{
1298 		if(ms_stereo)
1299 		{
1300 			tab1 = tan1_2;
1301 			tab2 = tan2_2;
1302 		}
1303 		else
1304 		{
1305 			tab1 = tan1_1;
1306 			tab2 = tan2_1;
1307 		}
1308 	}
1309 #endif
1310 
1311 	if(gr_info->block_type == 2)
1312 	{
1313 		int lwin,do_l = 0;
1314 		if( gr_info->mixed_block_flag ) do_l = 1;
1315 
1316 		for(lwin=0;lwin<3;lwin++)
1317 		{ /* process each window */
1318 			/* get first band with zero values */
1319 			int is_p,sb,idx,sfb = gr_info->maxband[lwin];  /* sfb is minimal 3 for mixed mode */
1320 			if(sfb > 3) do_l = 0;
1321 
1322 			for(;sfb<12;sfb++)
1323 			{
1324 				is_p = scalefac[sfb*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1325 				if(is_p != 7)
1326 				{
1327 					real t1,t2;
1328 					sb  = bi->shortDiff[sfb];
1329 					idx = bi->shortIdx[sfb] + lwin;
1330 					t1  = tab1[is_p]; t2 = tab2[is_p];
1331 					for (; sb > 0; sb--,idx+=3)
1332 					{
1333 						real v = xr[0][idx];
1334 						xr[0][idx] = REAL_MUL_15(v, t1);
1335 						xr[1][idx] = REAL_MUL_15(v, t2);
1336 					}
1337 				}
1338 			}
1339 
1340 #if 1
1341 /* in the original: copy 10 to 11 , here: copy 11 to 12
1342 maybe still wrong??? (copy 12 to 13?) */
1343 			is_p = scalefac[11*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1344 			sb   = bi->shortDiff[12];
1345 			idx  = bi->shortIdx[12] + lwin;
1346 #else
1347 			is_p = scalefac[10*3+lwin-gr_info->mixed_block_flag]; /* scale: 0-15 */
1348 			sb   = bi->shortDiff[11];
1349 			idx  = bi->shortIdx[11] + lwin;
1350 #endif
1351 			if(is_p != 7)
1352 			{
1353 				real t1,t2;
1354 				t1 = tab1[is_p]; t2 = tab2[is_p];
1355 				for( ; sb > 0; sb--,idx+=3 )
1356 				{
1357 					real v = xr[0][idx];
1358 					xr[0][idx] = REAL_MUL_15(v, t1);
1359 					xr[1][idx] = REAL_MUL_15(v, t2);
1360 				}
1361 			}
1362 		} /* end for(lwin; .. ; . ) */
1363 
1364 		/* also check l-part, if ALL bands in the three windows are 'empty' and mode = mixed_mode */
1365 		if(do_l)
1366 		{
1367 			int sfb = gr_info->maxbandl;
1368 			int idx;
1369 			if(sfb > 21) return; /* similarity fix related to CVE-2006-1655 */
1370 
1371 			idx = bi->longIdx[sfb];
1372 			for( ; sfb<8; sfb++ )
1373 			{
1374 				int sb = bi->longDiff[sfb];
1375 				int is_p = scalefac[sfb]; /* scale: 0-15 */
1376 				if(is_p != 7)
1377 				{
1378 					real t1,t2;
1379 					t1 = tab1[is_p]; t2 = tab2[is_p];
1380 					for( ; sb > 0; sb--,idx++)
1381 					{
1382 						real v = xr[0][idx];
1383 						xr[0][idx] = REAL_MUL_15(v, t1);
1384 						xr[1][idx] = REAL_MUL_15(v, t2);
1385 					}
1386 				}
1387 				else idx += sb;
1388 			}
1389 		}
1390 	}
1391 	else
1392 	{ /* ((gr_info->block_type != 2)) */
1393 		int sfb = gr_info->maxbandl;
1394 		int is_p,idx;
1395 		if(sfb > 21) return; /* tightened fix for CVE-2006-1655 */
1396 
1397 		idx = bi->longIdx[sfb];
1398 		for ( ; sfb<21; sfb++)
1399 		{
1400 			int sb = bi->longDiff[sfb];
1401 			is_p = scalefac[sfb]; /* scale: 0-15 */
1402 			if(is_p != 7)
1403 			{
1404 				real t1,t2;
1405 				t1 = tab1[is_p]; t2 = tab2[is_p];
1406 				for( ; sb > 0; sb--,idx++)
1407 				{
1408 					 real v = xr[0][idx];
1409 					 xr[0][idx] = REAL_MUL_15(v, t1);
1410 					 xr[1][idx] = REAL_MUL_15(v, t2);
1411 				}
1412 			}
1413 			else idx += sb;
1414 		}
1415 
1416 		is_p = scalefac[20];
1417 		if(is_p != 7)
1418 		{  /* copy l-band 20 to l-band 21 */
1419 			int sb;
1420 			real t1 = tab1[is_p],t2 = tab2[is_p];
1421 
1422 			for( sb = bi->longDiff[21]; sb > 0; sb--,idx++ )
1423 			{
1424 				real v = xr[0][idx];
1425 				xr[0][idx] = REAL_MUL_15(v, t1);
1426 				xr[1][idx] = REAL_MUL_15(v, t2);
1427 			}
1428 		}
1429 	}
1430 }
1431 
1432 
1433 static void III_antialias(real xr[SBLIMIT][SSLIMIT],struct gr_info_s *gr_info)
1434 {
1435 	int sblim;
1436 
1437 	if(gr_info->block_type == 2)
1438 	{
1439 			if(!gr_info->mixed_block_flag) return;
1440 
1441 			sblim = 1;
1442 	}
1443 	else sblim = gr_info->maxb-1;
1444 
1445 	/* 31 alias-reduction operations between each pair of sub-bands */
1446 	/* with 8 butterflies between each pair                         */
1447 
1448 	{
1449 		int sb;
1450 		real *xr1=(real *) xr[1];
1451 
1452 		for(sb=sblim; sb; sb--,xr1+=10)
1453 		{
1454 			int ss;
1455 			real *cs=aa_cs,*ca=aa_ca;
1456 			real *xr2 = xr1;
1457 
1458 			for(ss=7;ss>=0;ss--)
1459 			{ /* upper and lower butterfly inputs */
1460 				register real bu = *--xr2,bd = *xr1;
1461 				*xr2   = REAL_MUL(bu, *cs) - REAL_MUL(bd, *ca);
1462 				*xr1++ = REAL_MUL(bd, *cs++) + REAL_MUL(bu, *ca++);
1463 			}
1464 		}
1465 	}
1466 }
1467 
1468 /*
1469 	This is an optimized DCT from Jeff Tsay's maplay 1.2+ package.
1470 	Saved one multiplication by doing the 'twiddle factor' stuff
1471 	together with the window mul. (MH)
1472 
1473 	This uses Byeong Gi Lee's Fast Cosine Transform algorithm, but the
1474 	9 point IDCT needs to be reduced further. Unfortunately, I don't
1475 	know how to do that, because 9 is not an even number. - Jeff.
1476 
1477 	Original Message:
1478 
1479 	9 Point Inverse Discrete Cosine Transform
1480 
1481 	This piece of code is Copyright 1997 Mikko Tommila and is freely usable
1482 	by anybody. The algorithm itself is of course in the public domain.
1483 
1484 	Again derived heuristically from the 9-point WFTA.
1485 
1486 	The algorithm is optimized (?) for speed, not for small rounding errors or
1487 	good readability.
1488 
1489 	36 additions, 11 multiplications
1490 
1491 	Again this is very likely sub-optimal.
1492 
1493 	The code is optimized to use a minimum number of temporary variables,
1494 	so it should compile quite well even on 8-register Intel x86 processors.
1495 	This makes the code quite obfuscated and very difficult to understand.
1496 
1497 	References:
1498 	[1] S. Winograd: "On Computing the Discrete Fourier Transform",
1499 	    Mathematics of Computation, Volume 32, Number 141, January 1978,
1500 	    Pages 175-199
1501 */
1502 
1503 /* Calculation of the inverse MDCT
1504    used to be static without 3dnow - does that really matter? */
1505 void dct36(real *inbuf,real *o1,real *o2,real *wintab,real *tsbuf)
1506 {
1507 #ifdef NEW_DCT9
1508 	real tmp[18];
1509 #endif
1510 
1511 	{
1512 		register real *in = inbuf;
1513 
1514 		in[17]+=in[16]; in[16]+=in[15]; in[15]+=in[14];
1515 		in[14]+=in[13]; in[13]+=in[12]; in[12]+=in[11];
1516 		in[11]+=in[10]; in[10]+=in[9];  in[9] +=in[8];
1517 		in[8] +=in[7];  in[7] +=in[6];  in[6] +=in[5];
1518 		in[5] +=in[4];  in[4] +=in[3];  in[3] +=in[2];
1519 		in[2] +=in[1];  in[1] +=in[0];
1520 
1521 		in[17]+=in[15]; in[15]+=in[13]; in[13]+=in[11]; in[11]+=in[9];
1522 		in[9] +=in[7];  in[7] +=in[5];  in[5] +=in[3];  in[3] +=in[1];
1523 
1524 
1525 #ifdef NEW_DCT9
1526 #if 1
1527 		{
1528 			real t3;
1529 			{
1530 				real t0, t1, t2;
1531 
1532 				t0 = REAL_MUL(COS6_2, (in[8] + in[16] - in[4]));
1533 				t1 = REAL_MUL(COS6_2, in[12]);
1534 
1535 				t3 = in[0];
1536 				t2 = t3 - t1 - t1;
1537 				tmp[1] = tmp[7] = t2 - t0;
1538 				tmp[4]          = t2 + t0 + t0;
1539 				t3 += t1;
1540 
1541 				t2 = REAL_MUL(COS6_1, (in[10] + in[14] - in[2]));
1542 				tmp[1] -= t2;
1543 				tmp[7] += t2;
1544 			}
1545 			{
1546 				real t0, t1, t2;
1547 
1548 				t0 = REAL_MUL(cos9[0], (in[4] + in[8] ));
1549 				t1 = REAL_MUL(cos9[1], (in[8] - in[16]));
1550 				t2 = REAL_MUL(cos9[2], (in[4] + in[16]));
1551 
1552 				tmp[2] = tmp[6] = t3 - t0      - t2;
1553 				tmp[0] = tmp[8] = t3 + t0 + t1;
1554 				tmp[3] = tmp[5] = t3      - t1 + t2;
1555 			}
1556 		}
1557 		{
1558 			real t1, t2, t3;
1559 
1560 			t1 = REAL_MUL(cos18[0], (in[2]  + in[10]));
1561 			t2 = REAL_MUL(cos18[1], (in[10] - in[14]));
1562 			t3 = REAL_MUL(COS6_1,    in[6]);
1563 
1564 			{
1565 				real t0 = t1 + t2 + t3;
1566 				tmp[0] += t0;
1567 				tmp[8] -= t0;
1568 			}
1569 
1570 			t2 -= t3;
1571 			t1 -= t3;
1572 
1573 			t3 = REAL_MUL(cos18[2], (in[2] + in[14]));
1574 
1575 			t1 += t3;
1576 			tmp[3] += t1;
1577 			tmp[5] -= t1;
1578 
1579 			t2 -= t3;
1580 			tmp[2] += t2;
1581 			tmp[6] -= t2;
1582 		}
1583 
1584 #else
1585 		{
1586 			real t0, t1, t2, t3, t4, t5, t6, t7;
1587 
1588 			t1 = REAL_MUL(COS6_2, in[12]);
1589 			t2 = REAL_MUL(COS6_2, (in[8] + in[16] - in[4]));
1590 
1591 			t3 = in[0] + t1;
1592 			t4 = in[0] - t1 - t1;
1593 			t5     = t4 - t2;
1594 			tmp[4] = t4 + t2 + t2;
1595 
1596 			t0 = REAL_MUL(cos9[0], (in[4] + in[8]));
1597 			t1 = REAL_MUL(cos9[1], (in[8] - in[16]));
1598 
1599 			t2 = REAL_MUL(cos9[2], (in[4] + in[16]));
1600 
1601 			t6 = t3 - t0 - t2;
1602 			t0 += t3 + t1;
1603 			t3 += t2 - t1;
1604 
1605 			t2 = REAL_MUL(cos18[0], (in[2]  + in[10]));
1606 			t4 = REAL_MUL(cos18[1], (in[10] - in[14]));
1607 			t7 = REAL_MUL(COS6_1, in[6]);
1608 
1609 			t1 = t2 + t4 + t7;
1610 			tmp[0] = t0 + t1;
1611 			tmp[8] = t0 - t1;
1612 			t1 = REAL_MUL(cos18[2], (in[2] + in[14]));
1613 			t2 += t1 - t7;
1614 
1615 			tmp[3] = t3 + t2;
1616 			t0 = REAL_MUL(COS6_1, (in[10] + in[14] - in[2]));
1617 			tmp[5] = t3 - t2;
1618 
1619 			t4 -= t1 + t7;
1620 
1621 			tmp[1] = t5 - t0;
1622 			tmp[7] = t5 + t0;
1623 			tmp[2] = t6 + t4;
1624 			tmp[6] = t6 - t4;
1625 		}
1626 #endif
1627 
1628 		{
1629 			real t0, t1, t2, t3, t4, t5, t6, t7;
1630 
1631 			t1 = REAL_MUL(COS6_2, in[13]);
1632 			t2 = REAL_MUL(COS6_2, (in[9] + in[17] - in[5]));
1633 
1634 			t3 = in[1] + t1;
1635 			t4 = in[1] - t1 - t1;
1636 			t5 = t4 - t2;
1637 
1638 			t0 = REAL_MUL(cos9[0], (in[5] + in[9]));
1639 			t1 = REAL_MUL(cos9[1], (in[9] - in[17]));
1640 
1641 			tmp[13] = REAL_MUL((t4 + t2 + t2), tfcos36[17-13]);
1642 			t2 = REAL_MUL(cos9[2], (in[5] + in[17]));
1643 
1644 			t6 = t3 - t0 - t2;
1645 			t0 += t3 + t1;
1646 			t3 += t2 - t1;
1647 
1648 			t2 = REAL_MUL(cos18[0], (in[3]  + in[11]));
1649 			t4 = REAL_MUL(cos18[1], (in[11] - in[15]));
1650 			t7 = REAL_MUL(COS6_1, in[7]);
1651 
1652 			t1 = t2 + t4 + t7;
1653 			tmp[17] = REAL_MUL((t0 + t1), tfcos36[17-17]);
1654 			tmp[9]  = REAL_MUL((t0 - t1), tfcos36[17-9]);
1655 			t1 = REAL_MUL(cos18[2], (in[3] + in[15]));
1656 			t2 += t1 - t7;
1657 
1658 			tmp[14] = REAL_MUL((t3 + t2), tfcos36[17-14]);
1659 			t0 = REAL_MUL(COS6_1, (in[11] + in[15] - in[3]));
1660 			tmp[12] = REAL_MUL((t3 - t2), tfcos36[17-12]);
1661 
1662 			t4 -= t1 + t7;
1663 
1664 			tmp[16] = REAL_MUL((t5 - t0), tfcos36[17-16]);
1665 			tmp[10] = REAL_MUL((t5 + t0), tfcos36[17-10]);
1666 			tmp[15] = REAL_MUL((t6 + t4), tfcos36[17-15]);
1667 			tmp[11] = REAL_MUL((t6 - t4), tfcos36[17-11]);
1668 		}
1669 
1670 #define MACRO(v) { \
1671 		real tmpval; \
1672 		tmpval = tmp[(v)] + tmp[17-(v)]; \
1673 		out2[9+(v)] = REAL_MUL(tmpval, w[27+(v)]); \
1674 		out2[8-(v)] = REAL_MUL(tmpval, w[26-(v)]); \
1675 		tmpval = tmp[(v)] - tmp[17-(v)]; \
1676 		ts[SBLIMIT*(8-(v))] = out1[8-(v)] + REAL_MUL(tmpval, w[8-(v)]); \
1677 		ts[SBLIMIT*(9+(v))] = out1[9+(v)] + REAL_MUL(tmpval, w[9+(v)]); }
1678 
1679 		{
1680 			register real *out2 = o2;
1681 			register real *w = wintab;
1682 			register real *out1 = o1;
1683 			register real *ts = tsbuf;
1684 
1685 			MACRO(0);
1686 			MACRO(1);
1687 			MACRO(2);
1688 			MACRO(3);
1689 			MACRO(4);
1690 			MACRO(5);
1691 			MACRO(6);
1692 			MACRO(7);
1693 			MACRO(8);
1694 		}
1695 
1696 #else
1697 
1698 		{
1699 
1700 #define MACRO0(v) { \
1701 	real tmp; \
1702 	out2[9+(v)] = REAL_MUL((tmp = sum0 + sum1), w[27+(v)]); \
1703 	out2[8-(v)] = REAL_MUL(tmp, w[26-(v)]);   } \
1704 	sum0 -= sum1; \
1705 	ts[SBLIMIT*(8-(v))] = out1[8-(v)] + REAL_MUL(sum0, w[8-(v)]); \
1706 	ts[SBLIMIT*(9+(v))] = out1[9+(v)] + REAL_MUL(sum0, w[9+(v)]);
1707 #define MACRO1(v) { \
1708 	real sum0,sum1; \
1709 	sum0 = tmp1a + tmp2a; \
1710 	sum1 = REAL_MUL((tmp1b + tmp2b), tfcos36[(v)]); \
1711 	MACRO0(v); }
1712 #define MACRO2(v) { \
1713 	real sum0,sum1; \
1714 	sum0 = tmp2a - tmp1a; \
1715 	sum1 = REAL_MUL((tmp2b - tmp1b), tfcos36[(v)]); \
1716 	MACRO0(v); }
1717 
1718 			register const real *c = COS9;
1719 			register real *out2 = o2;
1720 			register real *w = wintab;
1721 			register real *out1 = o1;
1722 			register real *ts = tsbuf;
1723 
1724 			real ta33,ta66,tb33,tb66;
1725 
1726 			ta33 = REAL_MUL(in[2*3+0], c[3]);
1727 			ta66 = REAL_MUL(in[2*6+0], c[6]);
1728 			tb33 = REAL_MUL(in[2*3+1], c[3]);
1729 			tb66 = REAL_MUL(in[2*6+1], c[6]);
1730 
1731 			{
1732 				real tmp1a,tmp2a,tmp1b,tmp2b;
1733 				tmp1a = REAL_MUL(in[2*1+0], c[1]) + ta33 + REAL_MUL(in[2*5+0], c[5]) + REAL_MUL(in[2*7+0], c[7]);
1734 				tmp1b = REAL_MUL(in[2*1+1], c[1]) + tb33 + REAL_MUL(in[2*5+1], c[5]) + REAL_MUL(in[2*7+1], c[7]);
1735 				tmp2a = REAL_MUL(in[2*2+0], c[2]) + REAL_MUL(in[2*4+0], c[4]) + ta66 + REAL_MUL(in[2*8+0], c[8]);
1736 				tmp2b = REAL_MUL(in[2*2+1], c[2]) + REAL_MUL(in[2*4+1], c[4]) + tb66 + REAL_MUL(in[2*8+1], c[8]);
1737 
1738 				MACRO1(0);
1739 				MACRO2(8);
1740 			}
1741 
1742 			{
1743 				real tmp1a,tmp2a,tmp1b,tmp2b;
1744 				tmp1a = REAL_MUL(( in[2*1+0] - in[2*5+0] - in[2*7+0] ), c[3]);
1745 				tmp1b = REAL_MUL(( in[2*1+1] - in[2*5+1] - in[2*7+1] ), c[3]);
1746 				tmp2a = REAL_MUL(( in[2*2+0] - in[2*4+0] - in[2*8+0] ), c[6]) - in[2*6+0] + in[2*0+0];
1747 				tmp2b = REAL_MUL(( in[2*2+1] - in[2*4+1] - in[2*8+1] ), c[6]) - in[2*6+1] + in[2*0+1];
1748 
1749 				MACRO1(1);
1750 				MACRO2(7);
1751 			}
1752 
1753 			{
1754 				real tmp1a,tmp2a,tmp1b,tmp2b;
1755 				tmp1a =   REAL_MUL(in[2*1+0], c[5]) - ta33 - REAL_MUL(in[2*5+0], c[7]) + REAL_MUL(in[2*7+0], c[1]);
1756 				tmp1b =   REAL_MUL(in[2*1+1], c[5]) - tb33 - REAL_MUL(in[2*5+1], c[7]) + REAL_MUL(in[2*7+1], c[1]);
1757 				tmp2a = - REAL_MUL(in[2*2+0], c[8]) - REAL_MUL(in[2*4+0], c[2]) + ta66 + REAL_MUL(in[2*8+0], c[4]);
1758 				tmp2b = - REAL_MUL(in[2*2+1], c[8]) - REAL_MUL(in[2*4+1], c[2]) + tb66 + REAL_MUL(in[2*8+1], c[4]);
1759 
1760 				MACRO1(2);
1761 				MACRO2(6);
1762 			}
1763 
1764 			{
1765 				real tmp1a,tmp2a,tmp1b,tmp2b;
1766 				tmp1a =   REAL_MUL(in[2*1+0], c[7]) - ta33 + REAL_MUL(in[2*5+0], c[1]) - REAL_MUL(in[2*7+0], c[5]);
1767 				tmp1b =   REAL_MUL(in[2*1+1], c[7]) - tb33 + REAL_MUL(in[2*5+1], c[1]) - REAL_MUL(in[2*7+1], c[5]);
1768 				tmp2a = - REAL_MUL(in[2*2+0], c[4]) + REAL_MUL(in[2*4+0], c[8]) + ta66 - REAL_MUL(in[2*8+0], c[2]);
1769 				tmp2b = - REAL_MUL(in[2*2+1], c[4]) + REAL_MUL(in[2*4+1], c[8]) + tb66 - REAL_MUL(in[2*8+1], c[2]);
1770 
1771 				MACRO1(3);
1772 				MACRO2(5);
1773 			}
1774 
1775 			{
1776 				real sum0,sum1;
1777 				sum0 =  in[2*0+0] - in[2*2+0] + in[2*4+0] - in[2*6+0] + in[2*8+0];
1778 				sum1 = REAL_MUL((in[2*0+1] - in[2*2+1] + in[2*4+1] - in[2*6+1] + in[2*8+1] ), tfcos36[4]);
1779 				MACRO0(4);
1780 			}
1781 		}
1782 #endif
1783 
1784 	}
1785 }
1786 
1787 
1788 /* new DCT12 */
1789 static void dct12(real *in,real *rawout1,real *rawout2,register real *wi,register real *ts)
1790 {
1791 #define DCT12_PART1 \
1792 	in5 = in[5*3];  \
1793 	in5 += (in4 = in[4*3]); \
1794 	in4 += (in3 = in[3*3]); \
1795 	in3 += (in2 = in[2*3]); \
1796 	in2 += (in1 = in[1*3]); \
1797 	in1 += (in0 = in[0*3]); \
1798 	\
1799 	in5 += in3; in3 += in1; \
1800 	\
1801 	in2 = REAL_MUL(in2, COS6_1); \
1802 	in3 = REAL_MUL(in3, COS6_1);
1803 
1804 #define DCT12_PART2 \
1805 	in0 += REAL_MUL(in4, COS6_2); \
1806 	\
1807 	in4 = in0 + in2; \
1808 	in0 -= in2;      \
1809 	\
1810 	in1 += REAL_MUL(in5, COS6_2); \
1811 	\
1812 	in5 = REAL_MUL((in1 + in3), tfcos12[0]); \
1813 	in1 = REAL_MUL((in1 - in3), tfcos12[2]); \
1814 	\
1815 	in3 = in4 + in5; \
1816 	in4 -= in5;      \
1817 	\
1818 	in2 = in0 + in1; \
1819 	in0 -= in1;
1820 
1821 	{
1822 		real in0,in1,in2,in3,in4,in5;
1823 		register real *out1 = rawout1;
1824 		ts[SBLIMIT*0] = out1[0]; ts[SBLIMIT*1] = out1[1]; ts[SBLIMIT*2] = out1[2];
1825 		ts[SBLIMIT*3] = out1[3]; ts[SBLIMIT*4] = out1[4]; ts[SBLIMIT*5] = out1[5];
1826 
1827 		DCT12_PART1
1828 
1829 		{
1830 			real tmp0,tmp1 = (in0 - in4);
1831 			{
1832 				real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]);
1833 				tmp0 = tmp1 + tmp2;
1834 				tmp1 -= tmp2;
1835 			}
1836 			ts[(17-1)*SBLIMIT] = out1[17-1] + REAL_MUL(tmp0, wi[11-1]);
1837 			ts[(12+1)*SBLIMIT] = out1[12+1] + REAL_MUL(tmp0, wi[6+1]);
1838 			ts[(6 +1)*SBLIMIT] = out1[6 +1] + REAL_MUL(tmp1, wi[1]);
1839 			ts[(11-1)*SBLIMIT] = out1[11-1] + REAL_MUL(tmp1, wi[5-1]);
1840 		}
1841 
1842 		DCT12_PART2
1843 
1844 		ts[(17-0)*SBLIMIT] = out1[17-0] + REAL_MUL(in2, wi[11-0]);
1845 		ts[(12+0)*SBLIMIT] = out1[12+0] + REAL_MUL(in2, wi[6+0]);
1846 		ts[(12+2)*SBLIMIT] = out1[12+2] + REAL_MUL(in3, wi[6+2]);
1847 		ts[(17-2)*SBLIMIT] = out1[17-2] + REAL_MUL(in3, wi[11-2]);
1848 
1849 		ts[(6 +0)*SBLIMIT]  = out1[6+0] + REAL_MUL(in0, wi[0]);
1850 		ts[(11-0)*SBLIMIT] = out1[11-0] + REAL_MUL(in0, wi[5-0]);
1851 		ts[(6 +2)*SBLIMIT]  = out1[6+2] + REAL_MUL(in4, wi[2]);
1852 		ts[(11-2)*SBLIMIT] = out1[11-2] + REAL_MUL(in4, wi[5-2]);
1853 	}
1854 
1855 	in++;
1856 
1857 	{
1858 		real in0,in1,in2,in3,in4,in5;
1859 		register real *out2 = rawout2;
1860 
1861 		DCT12_PART1
1862 
1863 		{
1864 			real tmp0,tmp1 = (in0 - in4);
1865 			{
1866 				real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]);
1867 				tmp0 = tmp1 + tmp2;
1868 				tmp1 -= tmp2;
1869 			}
1870 			out2[5-1] = REAL_MUL(tmp0, wi[11-1]);
1871 			out2[0+1] = REAL_MUL(tmp0, wi[6+1]);
1872 			ts[(12+1)*SBLIMIT] += REAL_MUL(tmp1, wi[1]);
1873 			ts[(17-1)*SBLIMIT] += REAL_MUL(tmp1, wi[5-1]);
1874 		}
1875 
1876 		DCT12_PART2
1877 
1878 		out2[5-0] = REAL_MUL(in2, wi[11-0]);
1879 		out2[0+0] = REAL_MUL(in2, wi[6+0]);
1880 		out2[0+2] = REAL_MUL(in3, wi[6+2]);
1881 		out2[5-2] = REAL_MUL(in3, wi[11-2]);
1882 
1883 		ts[(12+0)*SBLIMIT] += REAL_MUL(in0, wi[0]);
1884 		ts[(17-0)*SBLIMIT] += REAL_MUL(in0, wi[5-0]);
1885 		ts[(12+2)*SBLIMIT] += REAL_MUL(in4, wi[2]);
1886 		ts[(17-2)*SBLIMIT] += REAL_MUL(in4, wi[5-2]);
1887 	}
1888 
1889 	in++;
1890 
1891 	{
1892 		real in0,in1,in2,in3,in4,in5;
1893 		register real *out2 = rawout2;
1894 		out2[12]=out2[13]=out2[14]=out2[15]=out2[16]=out2[17]=0.0;
1895 
1896 		DCT12_PART1
1897 
1898 		{
1899 			real tmp0,tmp1 = (in0 - in4);
1900 			{
1901 				real tmp2 = REAL_MUL((in1 - in5), tfcos12[1]);
1902 				tmp0 = tmp1 + tmp2;
1903 				tmp1 -= tmp2;
1904 			}
1905 			out2[11-1] = REAL_MUL(tmp0, wi[11-1]);
1906 			out2[6 +1] = REAL_MUL(tmp0, wi[6+1]);
1907 			out2[0+1] += REAL_MUL(tmp1, wi[1]);
1908 			out2[5-1] += REAL_MUL(tmp1, wi[5-1]);
1909 		}
1910 
1911 		DCT12_PART2
1912 
1913 		out2[11-0] = REAL_MUL(in2, wi[11-0]);
1914 		out2[6 +0] = REAL_MUL(in2, wi[6+0]);
1915 		out2[6 +2] = REAL_MUL(in3, wi[6+2]);
1916 		out2[11-2] = REAL_MUL(in3, wi[11-2]);
1917 
1918 		out2[0+0] += REAL_MUL(in0, wi[0]);
1919 		out2[5-0] += REAL_MUL(in0, wi[5-0]);
1920 		out2[0+2] += REAL_MUL(in4, wi[2]);
1921 		out2[5-2] += REAL_MUL(in4, wi[5-2]);
1922 	}
1923 }
1924 
1925 
1926 static void III_hybrid(real fsIn[SBLIMIT][SSLIMIT], real tsOut[SSLIMIT][SBLIMIT], int ch,struct gr_info_s *gr_info, mpg123_handle *fr)
1927 {
1928 	real (*block)[2][SBLIMIT*SSLIMIT] = fr->hybrid_block;
1929 	int *blc = fr->hybrid_blc;
1930 
1931 	real *tspnt = (real *) tsOut;
1932 	real *rawout1,*rawout2;
1933 	int bt = 0;
1934 	size_t sb = 0;
1935 
1936 	{
1937 		int b = blc[ch];
1938 		rawout1=block[b][ch];
1939 		b=-b+1;
1940 		rawout2=block[b][ch];
1941 		blc[ch] = b;
1942 	}
1943 
1944 	if(gr_info->mixed_block_flag)
1945 	{
1946 		sb = 2;
1947 		opt_dct36(fr)(fsIn[0],rawout1,rawout2,win[0],tspnt);
1948 		opt_dct36(fr)(fsIn[1],rawout1+18,rawout2+18,win1[0],tspnt+1);
1949 		rawout1 += 36; rawout2 += 36; tspnt += 2;
1950 	}
1951 
1952 	bt = gr_info->block_type;
1953 	if(bt == 2)
1954 	{
1955 		for(; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36)
1956 		{
1957 			dct12(fsIn[sb]  ,rawout1   ,rawout2   ,win[2] ,tspnt);
1958 			dct12(fsIn[sb+1],rawout1+18,rawout2+18,win1[2],tspnt+1);
1959 		}
1960 	}
1961 	else
1962 	{
1963 		for(; sb<gr_info->maxb; sb+=2,tspnt+=2,rawout1+=36,rawout2+=36)
1964 		{
1965 			opt_dct36(fr)(fsIn[sb],rawout1,rawout2,win[bt],tspnt);
1966 			opt_dct36(fr)(fsIn[sb+1],rawout1+18,rawout2+18,win1[bt],tspnt+1);
1967 		}
1968 	}
1969 
1970 	for(;sb<SBLIMIT;sb++,tspnt++)
1971 	{
1972 		int i;
1973 		for(i=0;i<SSLIMIT;i++)
1974 		{
1975 			tspnt[i*SBLIMIT] = *rawout1++;
1976 			*rawout2++ = DOUBLE_TO_REAL(0.0);
1977 		}
1978 	}
1979 }
1980 
1981 
1982 /* And at the end... the main layer3 handler */
1983 int do_layer3(mpg123_handle *fr)
1984 {
1985 	int gr, ch, ss,clip=0;
1986 	int scalefacs[2][39]; /* max 39 for short[13][3] mode, mixed: 38, long: 22 */
1987 	struct III_sideinfo sideinfo;
1988 	int stereo = fr->stereo;
1989 	int single = fr->single;
1990 	int ms_stereo,i_stereo;
1991 	int sfreq = fr->sampling_frequency;
1992 	int stereo1,granules;
1993 
1994 	if(stereo == 1)
1995 	{ /* stream is mono */
1996 		stereo1 = 1;
1997 		single = SINGLE_LEFT;
1998 	}
1999 	else if(single != SINGLE_STEREO) /* stream is stereo, but force to mono */
2000 	stereo1 = 1;
2001 	else
2002 	stereo1 = 2;
2003 
2004 	if(fr->mode == MPG_MD_JOINT_STEREO)
2005 	{
2006 		ms_stereo = (fr->mode_ext & 0x2)>>1;
2007 		i_stereo  = fr->mode_ext & 0x1;
2008 	}
2009 	else ms_stereo = i_stereo = 0;
2010 
2011 	granules = fr->lsf ? 1 : 2;
2012 
2013 	/* quick hack to keep the music playing */
2014 	/* after having seen this nasty test file... */
2015 	if(III_get_side_info(fr, &sideinfo,stereo,ms_stereo,sfreq,single))
2016 	{
2017 		if(NOQUIET) error("bad frame - unable to get valid sideinfo");
2018 		return clip;
2019 	}
2020 
2021 	set_pointer(fr,sideinfo.main_data_begin);
2022 
2023 	for(gr=0;gr<granules;gr++)
2024 	{
2025 		/*  hybridIn[2][SBLIMIT][SSLIMIT] */
2026 		real (*hybridIn)[SBLIMIT][SSLIMIT] = fr->layer3.hybrid_in;
2027 		/*  hybridOut[2][SSLIMIT][SBLIMIT] */
2028 		real (*hybridOut)[SSLIMIT][SBLIMIT] = fr->layer3.hybrid_out;
2029 
2030 		{
2031 			struct gr_info_s *gr_info = &(sideinfo.ch[0].gr[gr]);
2032 			long part2bits;
2033 			if(fr->lsf)
2034 			part2bits = III_get_scale_factors_2(fr, scalefacs[0],gr_info,0);
2035 			else
2036 			part2bits = III_get_scale_factors_1(fr, scalefacs[0],gr_info,0,gr);
2037 
2038 			if(III_dequantize_sample(fr, hybridIn[0], scalefacs[0],gr_info,sfreq,part2bits))
2039 			{
2040 				if(VERBOSE2) error("dequantization failed!");
2041 				return clip;
2042 			}
2043 		}
2044 
2045 		if(stereo == 2)
2046 		{
2047 			struct gr_info_s *gr_info = &(sideinfo.ch[1].gr[gr]);
2048 			long part2bits;
2049 			if(fr->lsf)
2050 			part2bits = III_get_scale_factors_2(fr, scalefacs[1],gr_info,i_stereo);
2051 			else
2052 			part2bits = III_get_scale_factors_1(fr, scalefacs[1],gr_info,1,gr);
2053 
2054 			if(III_dequantize_sample(fr, hybridIn[1],scalefacs[1],gr_info,sfreq,part2bits))
2055 			{
2056 				if(VERBOSE2) error("dequantization failed!");
2057 				return clip;
2058 			}
2059 
2060 			if(ms_stereo)
2061 			{
2062 				int i;
2063 				unsigned int maxb = sideinfo.ch[0].gr[gr].maxb;
2064 				if(sideinfo.ch[1].gr[gr].maxb > maxb) maxb = sideinfo.ch[1].gr[gr].maxb;
2065 
2066 				for(i=0;i<SSLIMIT*(int)maxb;i++)
2067 				{
2068 					real tmp0 = ((real *)hybridIn[0])[i];
2069 					real tmp1 = ((real *)hybridIn[1])[i];
2070 					((real *)hybridIn[0])[i] = tmp0 + tmp1;
2071 					((real *)hybridIn[1])[i] = tmp0 - tmp1;
2072 				}
2073 			}
2074 
2075 			if(i_stereo) III_i_stereo(hybridIn,scalefacs[1],gr_info,sfreq,ms_stereo,fr->lsf);
2076 
2077 			if(ms_stereo || i_stereo || (single == SINGLE_MIX) )
2078 			{
2079 				if(gr_info->maxb > sideinfo.ch[0].gr[gr].maxb)
2080 				sideinfo.ch[0].gr[gr].maxb = gr_info->maxb;
2081 				else
2082 				gr_info->maxb = sideinfo.ch[0].gr[gr].maxb;
2083 			}
2084 
2085 			switch(single)
2086 			{
2087 				case SINGLE_MIX:
2088 				{
2089 					register int i;
2090 					register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
2091 					for(i=0;i<SSLIMIT*(int)gr_info->maxb;i++,in0++)
2092 					*in0 = (*in0 + *in1++); /* *0.5 done by pow-scale */
2093 				}
2094 				break;
2095 				case SINGLE_RIGHT:
2096 				{
2097 					register int i;
2098 					register real *in0 = (real *) hybridIn[0],*in1 = (real *) hybridIn[1];
2099 					for(i=0;i<SSLIMIT*(int)gr_info->maxb;i++)
2100 					*in0++ = *in1++;
2101 				}
2102 				break;
2103 			}
2104 		}
2105 
2106 		for(ch=0;ch<stereo1;ch++)
2107 		{
2108 			struct gr_info_s *gr_info = &(sideinfo.ch[ch].gr[gr]);
2109 			III_antialias(hybridIn[ch],gr_info);
2110 			III_hybrid(hybridIn[ch], hybridOut[ch], ch,gr_info, fr);
2111 		}
2112 
2113 #ifdef OPT_I486
2114 		if(single != SINGLE_STEREO || fr->af.encoding != MPG123_ENC_SIGNED_16 || fr->down_sample != 0)
2115 		{
2116 #endif
2117 		for(ss=0;ss<SSLIMIT;ss++)
2118 		{
2119 			if(single != SINGLE_STEREO)
2120 			clip += (fr->synth_mono)(hybridOut[0][ss], fr);
2121 			else
2122 			clip += (fr->synth_stereo)(hybridOut[0][ss], hybridOut[1][ss], fr);
2123 
2124 		}
2125 #ifdef OPT_I486
2126 		} else
2127 		{
2128 			/* Only stereo, 16 bits benefit from the 486 optimization. */
2129 			ss=0;
2130 			while(ss < SSLIMIT)
2131 			{
2132 				int n;
2133 				n=(fr->buffer.size - fr->buffer.fill) / (2*2*32);
2134 				if(n > (SSLIMIT-ss)) n=SSLIMIT-ss;
2135 
2136 				/* Clip counting makes no sense with this function. */
2137 				absynth_1to1_i486(hybridOut[0][ss], 0, fr, n);
2138 				absynth_1to1_i486(hybridOut[1][ss], 1, fr, n);
2139 				ss+=n;
2140 				fr->buffer.fill+=(2*2*32)*n;
2141 			}
2142 		}
2143 #endif
2144 	}
2145 
2146 	return clip;
2147 }
2148