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