xref: /reactos/sdk/lib/3rdparty/libmpg123/layer2.c (revision 34593d93)
1c2c66affSColin Finck /*
2c2c66affSColin Finck 	layer2.c: the layer 2 decoder, root of mpg123
3c2c66affSColin Finck 
4c2c66affSColin Finck 	copyright 1994-2009 by the mpg123 project - free software under the terms of the LGPL 2.1
5c2c66affSColin Finck 	see COPYING and AUTHORS files in distribution or http://mpg123.org
6c2c66affSColin Finck 	initially written by Michael Hipp
7c2c66affSColin Finck 
8c2c66affSColin Finck 	mpg123 started as mp2 decoder a long time ago...
9c2c66affSColin Finck 	part of this file is required for layer 1, too.
10c2c66affSColin Finck */
11c2c66affSColin Finck 
12c2c66affSColin Finck 
13c2c66affSColin Finck #include "mpg123lib_intern.h"
14c2c66affSColin Finck #ifndef NO_LAYER2
15c2c66affSColin Finck #include "l2tables.h"
16c2c66affSColin Finck #endif
17c2c66affSColin Finck #include "getbits.h"
18c2c66affSColin Finck 
19c2c66affSColin Finck #ifndef NO_LAYER12 /* Stuff  needed for layer I and II. */
20c2c66affSColin Finck 
21c2c66affSColin Finck static int grp_3tab[32 * 3] = { 0, };   /* used: 27 */
22c2c66affSColin Finck static int grp_5tab[128 * 3] = { 0, };  /* used: 125 */
23c2c66affSColin Finck static int grp_9tab[1024 * 3] = { 0, }; /* used: 729 */
24c2c66affSColin Finck 
25c2c66affSColin Finck #if defined(REAL_IS_FIXED) && defined(PRECALC_TABLES)
26c2c66affSColin Finck #include "l12_integer_tables.h"
27c2c66affSColin Finck #else
28c2c66affSColin Finck static const double mulmul[27] =
29c2c66affSColin Finck {
30c2c66affSColin Finck 	0.0 , -2.0/3.0 , 2.0/3.0 ,
31c2c66affSColin Finck 	2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 ,
32c2c66affSColin Finck 	2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 ,
33c2c66affSColin Finck 	2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 ,
34c2c66affSColin Finck 	-4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 ,
35c2c66affSColin Finck 	-8.0/9.0 , -4.0/9.0 , -2.0/9.0 , 2.0/9.0 , 4.0/9.0 , 8.0/9.0
36c2c66affSColin Finck };
37c2c66affSColin Finck #endif
38c2c66affSColin Finck 
init_layer12(void)39c2c66affSColin Finck void init_layer12(void)
40c2c66affSColin Finck {
41c2c66affSColin Finck 	const int base[3][9] =
42c2c66affSColin Finck 	{
43c2c66affSColin Finck 		{ 1 , 0, 2 , } ,
44c2c66affSColin Finck 		{ 17, 18, 0 , 19, 20 , } ,
45c2c66affSColin Finck 		{ 21, 1, 22, 23, 0, 24, 25, 2, 26 }
46c2c66affSColin Finck 	};
47c2c66affSColin Finck 	int i,j,k,l,len;
48c2c66affSColin Finck 	const int tablen[3] = { 3 , 5 , 9 };
49c2c66affSColin Finck 	int *itable;
50c2c66affSColin Finck 	int *tables[3] = { grp_3tab , grp_5tab , grp_9tab };
51c2c66affSColin Finck 
52c2c66affSColin Finck 	for(i=0;i<3;i++)
53c2c66affSColin Finck 	{
54c2c66affSColin Finck 		itable = tables[i];
55c2c66affSColin Finck 		len = tablen[i];
56c2c66affSColin Finck 		for(j=0;j<len;j++)
57c2c66affSColin Finck 		for(k=0;k<len;k++)
58c2c66affSColin Finck 		for(l=0;l<len;l++)
59c2c66affSColin Finck 		{
60c2c66affSColin Finck 			*itable++ = base[i][l];
61c2c66affSColin Finck 			*itable++ = base[i][k];
62c2c66affSColin Finck 			*itable++ = base[i][j];
63c2c66affSColin Finck 		}
64c2c66affSColin Finck 	}
65c2c66affSColin Finck }
66c2c66affSColin Finck 
init_layer12_stuff(mpg123_handle * fr,real * (* init_table)(mpg123_handle * fr,real * table,int m))67c2c66affSColin Finck void init_layer12_stuff(mpg123_handle *fr, real* (*init_table)(mpg123_handle *fr, real *table, int m))
68c2c66affSColin Finck {
69c2c66affSColin Finck 	int k;
70c2c66affSColin Finck 	real *table;
71c2c66affSColin Finck 	for(k=0;k<27;k++)
72c2c66affSColin Finck 	{
73c2c66affSColin Finck 		table = init_table(fr, fr->muls[k], k);
74c2c66affSColin Finck 		*table++ = 0.0;
75c2c66affSColin Finck 	}
76c2c66affSColin Finck }
77c2c66affSColin Finck 
init_layer12_table(mpg123_handle * fr,real * table,int m)78c2c66affSColin Finck real* init_layer12_table(mpg123_handle *fr, real *table, int m)
79c2c66affSColin Finck {
80c2c66affSColin Finck #if defined(REAL_IS_FIXED) && defined(PRECALC_TABLES)
81c2c66affSColin Finck 	int i;
82c2c66affSColin Finck 	for(i=0;i<63;i++)
83c2c66affSColin Finck 	*table++ = layer12_table[m][i];
84c2c66affSColin Finck #else
85c2c66affSColin Finck 	int i,j;
86c2c66affSColin Finck 	for(j=3,i=0;i<63;i++,j--)
87c2c66affSColin Finck 	*table++ = DOUBLE_TO_REAL_SCALE_LAYER12(mulmul[m] * pow(2.0,(double) j / 3.0));
88c2c66affSColin Finck #endif
89c2c66affSColin Finck 
90c2c66affSColin Finck 	return table;
91c2c66affSColin Finck }
92c2c66affSColin Finck 
93c2c66affSColin Finck #ifdef OPT_MMXORSSE
init_layer12_table_mmx(mpg123_handle * fr,real * table,int m)94c2c66affSColin Finck real* init_layer12_table_mmx(mpg123_handle *fr, real *table, int m)
95c2c66affSColin Finck {
96c2c66affSColin Finck 	int i,j;
97c2c66affSColin Finck 	if(!fr->p.down_sample)
98c2c66affSColin Finck 	{
99c2c66affSColin Finck 		for(j=3,i=0;i<63;i++,j--)
100c2c66affSColin Finck 			*table++ = DOUBLE_TO_REAL(16384 * mulmul[m] * pow(2.0,(double) j / 3.0));
101c2c66affSColin Finck 	}
102c2c66affSColin Finck 	else
103c2c66affSColin Finck 	{
104c2c66affSColin Finck 		for(j=3,i=0;i<63;i++,j--)
105c2c66affSColin Finck 		*table++ = DOUBLE_TO_REAL(mulmul[m] * pow(2.0,(double) j / 3.0));
106c2c66affSColin Finck 	}
107c2c66affSColin Finck 	return table;
108c2c66affSColin Finck }
109c2c66affSColin Finck #endif
110c2c66affSColin Finck 
111c2c66affSColin Finck #endif /* NO_LAYER12 */
112c2c66affSColin Finck 
113c2c66affSColin Finck /* The rest is the actual decoding of layer II data. */
114c2c66affSColin Finck 
115c2c66affSColin Finck #ifndef NO_LAYER2
116c2c66affSColin Finck 
II_step_one(unsigned int * bit_alloc,int * scale,mpg123_handle * fr)117*aa811c00SThomas Faber static int II_step_one(unsigned int *bit_alloc,int *scale,mpg123_handle *fr)
118c2c66affSColin Finck {
119c2c66affSColin Finck 	int stereo = fr->stereo-1;
120c2c66affSColin Finck 	int sblimit = fr->II_sblimit;
121c2c66affSColin Finck 	int jsbound = fr->jsbound;
122c2c66affSColin Finck 	int sblimit2 = fr->II_sblimit<<stereo;
123c2c66affSColin Finck 	const struct al_table *alloc1 = fr->alloc;
124c2c66affSColin Finck 	int i;
125c2c66affSColin Finck 	unsigned int scfsi_buf[64];
126c2c66affSColin Finck 	unsigned int *scfsi,*bita;
127c2c66affSColin Finck 	int sc,step;
128*aa811c00SThomas Faber 	/* Count the bits needed for getbits_fast(). */
129*aa811c00SThomas Faber 	unsigned int needbits = 0;
130*aa811c00SThomas Faber 	unsigned int scale_bits[4] = { 18, 12, 6, 12 };
131c2c66affSColin Finck 
132c2c66affSColin Finck 	bita = bit_alloc;
133c2c66affSColin Finck 	if(stereo)
134c2c66affSColin Finck 	{
135c2c66affSColin Finck 		for(i=jsbound;i;i--,alloc1+=(1<<step))
136c2c66affSColin Finck 		{
137c2c66affSColin Finck 			step=alloc1->bits;
138*aa811c00SThomas Faber 			bita[0] = (char) getbits(fr, step);
139*aa811c00SThomas Faber 			bita[1] = (char) getbits(fr, step);
140*aa811c00SThomas Faber 			needbits += ((bita[0]?1:0)+(bita[1]?1:0))*2;
141*aa811c00SThomas Faber 			bita+=2;
142c2c66affSColin Finck 		}
143c2c66affSColin Finck 		for(i=sblimit-jsbound;i;i--,alloc1+=(1<<step))
144c2c66affSColin Finck 		{
145c2c66affSColin Finck 			step=alloc1->bits;
146c2c66affSColin Finck 			bita[0] = (char) getbits(fr, step);
147c2c66affSColin Finck 			bita[1] = bita[0];
148*aa811c00SThomas Faber 			needbits += (bita[0]?1:0)*2*2;
149c2c66affSColin Finck 			bita+=2;
150c2c66affSColin Finck 		}
151c2c66affSColin Finck 		bita = bit_alloc;
152c2c66affSColin Finck 		scfsi=scfsi_buf;
153c2c66affSColin Finck 
154*aa811c00SThomas Faber 		if(fr->bits_avail < needbits)
155*aa811c00SThomas Faber 		{
156*aa811c00SThomas Faber 			if(NOQUIET)
157*aa811c00SThomas Faber 				error2("need %u bits, have %li", needbits, fr->bits_avail);
158*aa811c00SThomas Faber 			return -1;
159*aa811c00SThomas Faber 		}
160c2c66affSColin Finck 		for(i=sblimit2;i;i--)
161c2c66affSColin Finck 		if(*bita++) *scfsi++ = (char) getbits_fast(fr, 2);
162c2c66affSColin Finck 	}
163c2c66affSColin Finck 	else /* mono */
164c2c66affSColin Finck 	{
165c2c66affSColin Finck 		for(i=sblimit;i;i--,alloc1+=(1<<step))
166c2c66affSColin Finck 		{
167c2c66affSColin Finck 			step=alloc1->bits;
168*aa811c00SThomas Faber 			*bita = (char) getbits(fr, step);
169*aa811c00SThomas Faber 			if(*bita)
170*aa811c00SThomas Faber 				needbits += 2;
171*aa811c00SThomas Faber 			++bita;
172c2c66affSColin Finck 		}
173c2c66affSColin Finck 		bita = bit_alloc;
174c2c66affSColin Finck 		scfsi=scfsi_buf;
175*aa811c00SThomas Faber 		if(fr->bits_avail < needbits)
176*aa811c00SThomas Faber 		{
177*aa811c00SThomas Faber 			if(NOQUIET)
178*aa811c00SThomas Faber 				error2("need %u bits, have %li", needbits, fr->bits_avail);
179*aa811c00SThomas Faber 			return -1;
180*aa811c00SThomas Faber 		}
181c2c66affSColin Finck 		for(i=sblimit;i;i--)
182c2c66affSColin Finck 		if(*bita++) *scfsi++ = (char) getbits_fast(fr, 2);
183c2c66affSColin Finck 	}
184c2c66affSColin Finck 
185*aa811c00SThomas Faber 	needbits = 0;
186c2c66affSColin Finck 	bita = bit_alloc;
187c2c66affSColin Finck 	scfsi=scfsi_buf;
188*aa811c00SThomas Faber 	for(i=sblimit2;i;--i)
189*aa811c00SThomas Faber 		if(*bita++)
190*aa811c00SThomas Faber 			needbits += scale_bits[*scfsi++];
191*aa811c00SThomas Faber 	if(fr->bits_avail < needbits)
192*aa811c00SThomas Faber 	{
193*aa811c00SThomas Faber 		if(NOQUIET)
194*aa811c00SThomas Faber 			error2("need %u bits, have %li", needbits, fr->bits_avail);
195*aa811c00SThomas Faber 		return -1;
196*aa811c00SThomas Faber 	}
197*aa811c00SThomas Faber 
198*aa811c00SThomas Faber 	bita = bit_alloc;
199*aa811c00SThomas Faber 	scfsi=scfsi_buf;
200*aa811c00SThomas Faber 	for(i=sblimit2;i;--i)
201c2c66affSColin Finck 	if(*bita++)
202c2c66affSColin Finck 	switch(*scfsi++)
203c2c66affSColin Finck 	{
204c2c66affSColin Finck 		case 0:
205c2c66affSColin Finck 			*scale++ = getbits_fast(fr, 6);
206c2c66affSColin Finck 			*scale++ = getbits_fast(fr, 6);
207c2c66affSColin Finck 			*scale++ = getbits_fast(fr, 6);
208c2c66affSColin Finck 		break;
209c2c66affSColin Finck 		case 1 :
210c2c66affSColin Finck 			*scale++ = sc = getbits_fast(fr, 6);
211c2c66affSColin Finck 			*scale++ = sc;
212c2c66affSColin Finck 			*scale++ = getbits_fast(fr, 6);
213c2c66affSColin Finck 		break;
214c2c66affSColin Finck 		case 2:
215c2c66affSColin Finck 			*scale++ = sc = getbits_fast(fr, 6);
216c2c66affSColin Finck 			*scale++ = sc;
217c2c66affSColin Finck 			*scale++ = sc;
218c2c66affSColin Finck 		break;
219c2c66affSColin Finck 		default:              /* case 3 */
220c2c66affSColin Finck 			*scale++ = getbits_fast(fr, 6);
221c2c66affSColin Finck 			*scale++ = sc = getbits_fast(fr, 6);
222c2c66affSColin Finck 			*scale++ = sc;
223c2c66affSColin Finck 		break;
224c2c66affSColin Finck 	}
225*aa811c00SThomas Faber 
226*aa811c00SThomas Faber 	return 0;
227c2c66affSColin Finck }
228c2c66affSColin Finck 
229c2c66affSColin Finck 
II_step_two(unsigned int * bit_alloc,real fraction[2][4][SBLIMIT],int * scale,mpg123_handle * fr,int x1)230c2c66affSColin Finck static void II_step_two(unsigned int *bit_alloc,real fraction[2][4][SBLIMIT],int *scale,mpg123_handle *fr,int x1)
231c2c66affSColin Finck {
232c2c66affSColin Finck 	int i,j,k,ba;
233c2c66affSColin Finck 	int stereo = fr->stereo;
234c2c66affSColin Finck 	int sblimit = fr->II_sblimit;
235c2c66affSColin Finck 	int jsbound = fr->jsbound;
236c2c66affSColin Finck 	const struct al_table *alloc2,*alloc1 = fr->alloc;
237c2c66affSColin Finck 	unsigned int *bita=bit_alloc;
238c2c66affSColin Finck 	int d1,step;
239c2c66affSColin Finck 
240c2c66affSColin Finck 	for(i=0;i<jsbound;i++,alloc1+=(1<<step))
241c2c66affSColin Finck 	{
242c2c66affSColin Finck 		step = alloc1->bits;
243c2c66affSColin Finck 		for(j=0;j<stereo;j++)
244c2c66affSColin Finck 		{
245c2c66affSColin Finck 			if( (ba=*bita++) )
246c2c66affSColin Finck 			{
247c2c66affSColin Finck 				k=(alloc2 = alloc1+ba)->bits;
248c2c66affSColin Finck 				if( (d1=alloc2->d) < 0)
249c2c66affSColin Finck 				{
250c2c66affSColin Finck 					real cm=fr->muls[k][scale[x1]];
251c2c66affSColin Finck 					fraction[j][0][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm);
252c2c66affSColin Finck 					fraction[j][1][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm);
253c2c66affSColin Finck 					fraction[j][2][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm);
254c2c66affSColin Finck 				}
255c2c66affSColin Finck 				else
256c2c66affSColin Finck 				{
257c2c66affSColin Finck 					const int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
258c2c66affSColin Finck 					unsigned int idx,*tab,m=scale[x1];
259c2c66affSColin Finck 					idx = (unsigned int) getbits(fr, k);
260c2c66affSColin Finck 					tab = (unsigned int *) (table[d1] + idx + idx + idx);
261c2c66affSColin Finck 					fraction[j][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m]);
262c2c66affSColin Finck 					fraction[j][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m]);
263c2c66affSColin Finck 					fraction[j][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m]);
264c2c66affSColin Finck 				}
265c2c66affSColin Finck 				scale+=3;
266c2c66affSColin Finck 			}
267c2c66affSColin Finck 			else
268c2c66affSColin Finck 			fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = DOUBLE_TO_REAL(0.0);
269*aa811c00SThomas Faber 			if(fr->bits_avail < 0)
270*aa811c00SThomas Faber 				return; /* Caller checks that again. */
271c2c66affSColin Finck 		}
272c2c66affSColin Finck 	}
273c2c66affSColin Finck 
274c2c66affSColin Finck 	for(i=jsbound;i<sblimit;i++,alloc1+=(1<<step))
275c2c66affSColin Finck 	{
276c2c66affSColin Finck 		step = alloc1->bits;
277c2c66affSColin Finck 		bita++;	/* channel 1 and channel 2 bitalloc are the same */
278c2c66affSColin Finck 		if( (ba=*bita++) )
279c2c66affSColin Finck 		{
280c2c66affSColin Finck 			k=(alloc2 = alloc1+ba)->bits;
281c2c66affSColin Finck 			if( (d1=alloc2->d) < 0)
282c2c66affSColin Finck 			{
283c2c66affSColin Finck 				real cm;
284c2c66affSColin Finck 				cm=fr->muls[k][scale[x1+3]];
285c2c66affSColin Finck 				fraction[0][0][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1);
286c2c66affSColin Finck 				fraction[0][1][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1);
287c2c66affSColin Finck 				fraction[0][2][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1);
288c2c66affSColin Finck 				fraction[1][0][i] = REAL_MUL_SCALE_LAYER12(fraction[0][0][i], cm);
289c2c66affSColin Finck 				fraction[1][1][i] = REAL_MUL_SCALE_LAYER12(fraction[0][1][i], cm);
290c2c66affSColin Finck 				fraction[1][2][i] = REAL_MUL_SCALE_LAYER12(fraction[0][2][i], cm);
291c2c66affSColin Finck 				cm=fr->muls[k][scale[x1]];
292c2c66affSColin Finck 				fraction[0][0][i] = REAL_MUL_SCALE_LAYER12(fraction[0][0][i], cm);
293c2c66affSColin Finck 				fraction[0][1][i] = REAL_MUL_SCALE_LAYER12(fraction[0][1][i], cm);
294c2c66affSColin Finck 				fraction[0][2][i] = REAL_MUL_SCALE_LAYER12(fraction[0][2][i], cm);
295c2c66affSColin Finck 			}
296c2c66affSColin Finck 			else
297c2c66affSColin Finck 			{
298c2c66affSColin Finck 				const int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
299c2c66affSColin Finck 				unsigned int idx,*tab,m1,m2;
300c2c66affSColin Finck 				m1 = scale[x1]; m2 = scale[x1+3];
301c2c66affSColin Finck 				idx = (unsigned int) getbits(fr, k);
302c2c66affSColin Finck 				tab = (unsigned int *) (table[d1] + idx + idx + idx);
303c2c66affSColin Finck 				fraction[0][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m2]);
304c2c66affSColin Finck 				fraction[0][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m2]);
305c2c66affSColin Finck 				fraction[0][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m2]);
306c2c66affSColin Finck 			}
307c2c66affSColin Finck 			scale+=6;
308*aa811c00SThomas Faber 			if(fr->bits_avail < 0)
309*aa811c00SThomas Faber 				return; /* Caller checks that again. */
310c2c66affSColin Finck 		}
311c2c66affSColin Finck 		else
312c2c66affSColin Finck 		{
313c2c66affSColin Finck 			fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] =
314c2c66affSColin Finck 			fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = DOUBLE_TO_REAL(0.0);
315c2c66affSColin Finck 		}
316c2c66affSColin Finck /*
317c2c66affSColin Finck 	Historic comment...
318c2c66affSColin Finck 	should we use individual scalefac for channel 2 or
319c2c66affSColin Finck 	is the current way the right one , where we just copy channel 1 to
320c2c66affSColin Finck 	channel 2 ??
321c2c66affSColin Finck 	The current 'strange' thing is, that we throw away the scalefac
322c2c66affSColin Finck 	values for the second channel ...!!
323c2c66affSColin Finck 	-> changed .. now we use the scalefac values of channel one !!
324c2c66affSColin Finck */
325c2c66affSColin Finck 	}
326c2c66affSColin Finck 
327c2c66affSColin Finck 	if(sblimit > (fr->down_sample_sblimit) )
328c2c66affSColin Finck 	sblimit = fr->down_sample_sblimit;
329c2c66affSColin Finck 
330c2c66affSColin Finck 	for(i=sblimit;i<SBLIMIT;i++)
331c2c66affSColin Finck 	for (j=0;j<stereo;j++)
332c2c66affSColin Finck 	fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = DOUBLE_TO_REAL(0.0);
333c2c66affSColin Finck }
334c2c66affSColin Finck 
335c2c66affSColin Finck 
II_select_table(mpg123_handle * fr)336c2c66affSColin Finck static void II_select_table(mpg123_handle *fr)
337c2c66affSColin Finck {
338c2c66affSColin Finck 	const int translate[3][2][16] =
339c2c66affSColin Finck 	{
340c2c66affSColin Finck 		{
341c2c66affSColin Finck 			{ 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 },
342c2c66affSColin Finck 			{ 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 }
343c2c66affSColin Finck 		},
344c2c66affSColin Finck 		{
345c2c66affSColin Finck 			{ 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 },
346c2c66affSColin Finck 			{ 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 }
347c2c66affSColin Finck 		},
348c2c66affSColin Finck 		{
349c2c66affSColin Finck 			{ 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 },
350c2c66affSColin Finck 			{ 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 }
351c2c66affSColin Finck 		}
352c2c66affSColin Finck 	};
353c2c66affSColin Finck 
354c2c66affSColin Finck 	int table,sblim;
355c2c66affSColin Finck 	const struct al_table *tables[5] = { alloc_0, alloc_1, alloc_2, alloc_3 , alloc_4 };
356c2c66affSColin Finck 	const int sblims[5] = { 27 , 30 , 8, 12 , 30 };
357c2c66affSColin Finck 
358c2c66affSColin Finck 	if(fr->sampling_frequency >= 3)	/* Or equivalent: (fr->lsf == 1) */
359c2c66affSColin Finck 	table = 4;
360c2c66affSColin Finck 	else
361c2c66affSColin Finck 	table = translate[fr->sampling_frequency][2-fr->stereo][fr->bitrate_index];
362c2c66affSColin Finck 
363c2c66affSColin Finck 	sblim = sblims[table];
364c2c66affSColin Finck 	fr->alloc      = tables[table];
365c2c66affSColin Finck 	fr->II_sblimit = sblim;
366c2c66affSColin Finck }
367c2c66affSColin Finck 
368c2c66affSColin Finck 
do_layer2(mpg123_handle * fr)369c2c66affSColin Finck int do_layer2(mpg123_handle *fr)
370c2c66affSColin Finck {
371c2c66affSColin Finck 	int clip=0;
372c2c66affSColin Finck 	int i,j;
373c2c66affSColin Finck 	int stereo = fr->stereo;
374c2c66affSColin Finck 	/* pick_table clears unused subbands */
375c2c66affSColin Finck 	/* replacement for real fraction[2][4][SBLIMIT], needs alignment. */
376c2c66affSColin Finck 	real (*fraction)[4][SBLIMIT] = fr->layer2.fraction;
377c2c66affSColin Finck 	unsigned int bit_alloc[64];
378c2c66affSColin Finck 	int scale[192];
379c2c66affSColin Finck 	int single = fr->single;
380c2c66affSColin Finck 
381c2c66affSColin Finck 	II_select_table(fr);
382c2c66affSColin Finck 	fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : fr->II_sblimit;
383c2c66affSColin Finck 
384c2c66affSColin Finck 	if(fr->jsbound > fr->II_sblimit)
385c2c66affSColin Finck 	{
386c2c66affSColin Finck 		fprintf(stderr, "Truncating stereo boundary to sideband limit.\n");
387c2c66affSColin Finck 		fr->jsbound=fr->II_sblimit;
388c2c66affSColin Finck 	}
389c2c66affSColin Finck 
390c2c66affSColin Finck 	/* TODO: What happens with mono mixing, actually? */
391c2c66affSColin Finck 	if(stereo == 1 || single == SINGLE_MIX) /* also, mix not really handled */
392c2c66affSColin Finck 	single = SINGLE_LEFT;
393c2c66affSColin Finck 
394*aa811c00SThomas Faber 	if(II_step_one(bit_alloc, scale, fr))
395*aa811c00SThomas Faber 	{
396*aa811c00SThomas Faber 		if(NOQUIET)
397*aa811c00SThomas Faber 			error("first step of layer I decoding failed");
398*aa811c00SThomas Faber 		return clip;
399*aa811c00SThomas Faber 	}
400c2c66affSColin Finck 
401c2c66affSColin Finck 	for(i=0;i<SCALE_BLOCK;i++)
402c2c66affSColin Finck 	{
403c2c66affSColin Finck 		II_step_two(bit_alloc,fraction,scale,fr,i>>2);
404*aa811c00SThomas Faber 		if(fr->bits_avail < 0)
405*aa811c00SThomas Faber 		{
406*aa811c00SThomas Faber 			if(NOQUIET)
407*aa811c00SThomas Faber 				error("missing bits in layer II step two");
408*aa811c00SThomas Faber 			return clip;
409*aa811c00SThomas Faber 		}
410c2c66affSColin Finck 		for(j=0;j<3;j++)
411c2c66affSColin Finck 		{
412c2c66affSColin Finck 			if(single != SINGLE_STEREO)
413c2c66affSColin Finck 			clip += (fr->synth_mono)(fraction[single][j], fr);
414c2c66affSColin Finck 			else
415c2c66affSColin Finck 			clip += (fr->synth_stereo)(fraction[0][j], fraction[1][j], fr);
416c2c66affSColin Finck 		}
417c2c66affSColin Finck 	}
418c2c66affSColin Finck 
419c2c66affSColin Finck 	return clip;
420c2c66affSColin Finck }
421c2c66affSColin Finck 
422c2c66affSColin Finck #endif /* NO_LAYER2 */
423