xref: /reactos/sdk/lib/3rdparty/libmpg123/layer1.c (revision 34593d93)
1 /*
2 	layer1.c: the layer 1 decoder
3 
4 	copyright 1995-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 	may have a few bugs after last optimization ...
9 */
10 
11 #include "mpg123lib_intern.h"
12 #include "getbits.h"
13 #include "debug.h"
14 
15 /*
16 	Allocation value is not allowed to be 15. Initially, libmad showed me the
17 	error that mpg123 used to ignore. Then, I found a quote on that in
18 	Shlien, S. (1994): Guide to MPEG-1 Audio Standard.
19 	IEEE Transactions on Broadcasting 40, 4
20 
21 	"To avoid conflicts with the synchronization code, code '1111' is defined
22 	to be illegal."
23 */
check_balloc(mpg123_handle * fr,unsigned int * balloc,unsigned int * end)24 static int check_balloc(mpg123_handle *fr, unsigned int *balloc, unsigned int *end)
25 {
26 	unsigned int *ba;
27 	for(ba=balloc; ba != end; ++ba)
28 	if(*ba == 15)
29 	{
30 		if(NOQUIET) error("Illegal bit allocation value.");
31 		return -1;
32 	}
33 
34 	return 0;
35 }
36 
37 #define NEED_BITS(fr, num) \
38 	if((fr)->bits_avail < num) \
39 	{ \
40 		if(NOQUIET) \
41 			error2("%u bits needed, %li available", num, (fr)->bits_avail); \
42 		return -1; \
43 	} \
44 
I_step_one(unsigned int balloc[],unsigned int scale_index[2][SBLIMIT],mpg123_handle * fr)45 static int I_step_one(unsigned int balloc[], unsigned int scale_index[2][SBLIMIT],mpg123_handle *fr)
46 {
47 	unsigned int *ba=balloc;
48 	unsigned int *sca = (unsigned int *) scale_index;
49 
50 	if(fr->stereo == 2)
51 	{
52 		int i;
53 		int jsbound = fr->jsbound;
54 		unsigned int needbits = jsbound*2*4 + (SBLIMIT-jsbound)*4;
55 
56 		NEED_BITS(fr, needbits);
57 		needbits = 0;
58 		for(i=0;i<jsbound;i++)
59 		{
60 			ba[0] = getbits_fast(fr, 4);
61 			ba[1] = getbits_fast(fr, 4);
62 			needbits += ((ba[0]?1:0)+(ba[1]?1:0))*6;
63 			ba+=2;
64 		}
65 		for(i=jsbound;i<SBLIMIT;i++)
66 		{
67 			*ba = getbits_fast(fr, 4);
68 			needbits += (*ba?1:0)*12;
69 			++ba;
70 		}
71 
72 		if(check_balloc(fr, balloc, ba)) return -1;
73 
74 		ba = balloc;
75 		NEED_BITS(fr, needbits)
76 		for(i=0;i<jsbound;i++)
77 		{
78 			if ((*ba++))
79 				*sca++ = getbits_fast(fr, 6);
80 			if ((*ba++))
81 				*sca++ = getbits_fast(fr, 6);
82 		}
83 		for (i=jsbound;i<SBLIMIT;i++) if((*ba++))
84 		{
85 			*sca++ =  getbits_fast(fr, 6);
86 			*sca++ =  getbits_fast(fr, 6);
87 		}
88 	}
89 	else
90 	{
91 		int i;
92 		unsigned int needbits = SBLIMIT*4;
93 
94 		NEED_BITS(fr, needbits)
95 		needbits = 0;
96 		for(i=0;i<SBLIMIT;i++)
97 		{
98 			*ba = getbits_fast(fr, 4);
99 			needbits += (*ba?1:0)*6;
100 			++ba;
101 		}
102 
103 		if(check_balloc(fr, balloc, ba)) return -1;
104 
105 		ba = balloc;
106 		NEED_BITS(fr, needbits)
107 		for (i=0;i<SBLIMIT;i++)
108 			if ((*ba++))
109 				*sca++ = getbits_fast(fr, 6);
110 	}
111 
112 	return 0;
113 }
114 
115 /* Something sane in place of undefined (-1)<<n. Well, not really. */
116 #define MINUS_SHIFT(n) ( (int)(((unsigned int)-1)<<(n)) )
117 
I_step_two(real fraction[2][SBLIMIT],unsigned int balloc[2* SBLIMIT],unsigned int scale_index[2][SBLIMIT],mpg123_handle * fr)118 static int I_step_two(real fraction[2][SBLIMIT],unsigned int balloc[2*SBLIMIT], unsigned int scale_index[2][SBLIMIT],mpg123_handle *fr)
119 {
120 	int i,n;
121 	int smpb[2*SBLIMIT]; /* values: 0-65535 */
122 	int *sample;
123 	register unsigned int *ba;
124 	register unsigned int *sca = (unsigned int *) scale_index;
125 
126 	if(fr->stereo == 2)
127 	{
128 		unsigned int needbits = 0;
129 		int jsbound = fr->jsbound;
130 		register real *f0 = fraction[0];
131 		register real *f1 = fraction[1];
132 
133 		ba = balloc;
134 		for(sample=smpb,i=0;i<jsbound;i++)
135 		{
136 			if((n=*ba++))
137 				needbits += n+1;
138 			if((n=*ba++))
139 				needbits += n+1;
140 		}
141 		for(i=jsbound;i<SBLIMIT;i++)
142 			if((n = *ba++))
143 				needbits += n+1;
144 		NEED_BITS(fr, needbits)
145 
146 		ba = balloc;
147 		for(sample=smpb,i=0;i<jsbound;i++)
148 		{
149 			if((n = *ba++)) *sample++ = getbits(fr, n+1);
150 
151 			if((n = *ba++)) *sample++ = getbits(fr, n+1);
152 		}
153 		for(i=jsbound;i<SBLIMIT;i++)
154 		if((n = *ba++))
155 		*sample++ = getbits(fr, n+1);
156 
157 		ba = balloc;
158 		for(sample=smpb,i=0;i<jsbound;i++)
159 		{
160 			if((n=*ba++))
161 			*f0++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
162 			else *f0++ = DOUBLE_TO_REAL(0.0);
163 
164 			if((n=*ba++))
165 			*f1++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
166 			else *f1++ = DOUBLE_TO_REAL(0.0);
167 		}
168 		for(i=jsbound;i<SBLIMIT;i++)
169 		{
170 			if((n=*ba++))
171 			{
172 				real samp = DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1);
173 				*f0++ = REAL_MUL_SCALE_LAYER12(samp, fr->muls[n+1][*sca++]);
174 				*f1++ = REAL_MUL_SCALE_LAYER12(samp, fr->muls[n+1][*sca++]);
175 			}
176 			else *f0++ = *f1++ = DOUBLE_TO_REAL(0.0);
177 		}
178 		for(i=fr->down_sample_sblimit;i<32;i++)
179 		fraction[0][i] = fraction[1][i] = 0.0;
180 	}
181 	else
182 	{
183 		unsigned int needbits = 0;
184 		register real *f0 = fraction[0];
185 
186 		ba = balloc;
187 		for(sample=smpb,i=0;i<SBLIMIT;i++)
188 			if((n = *ba++))
189 				needbits += n+1;
190 		NEED_BITS(fr, needbits);
191 
192 		ba = balloc;
193 		for(sample=smpb,i=0;i<SBLIMIT;i++)
194 		if ((n = *ba++))
195 		*sample++ = getbits(fr, n+1);
196 
197 
198 		ba = balloc;
199 		for(sample=smpb,i=0;i<SBLIMIT;i++)
200 		{
201 			if((n=*ba++))
202 			*f0++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
203 			else *f0++ = DOUBLE_TO_REAL(0.0);
204 		}
205 		for(i=fr->down_sample_sblimit;i<32;i++)
206 		fraction[0][i] = DOUBLE_TO_REAL(0.0);
207 	}
208 	return 0;
209 }
210 
do_layer1(mpg123_handle * fr)211 int do_layer1(mpg123_handle *fr)
212 {
213 	int clip=0;
214 	int i,stereo = fr->stereo;
215 	unsigned int balloc[2*SBLIMIT];
216 	unsigned int scale_index[2][SBLIMIT];
217 	real (*fraction)[SBLIMIT] = fr->layer1.fraction; /* fraction[2][SBLIMIT] */
218 	int single = fr->single;
219 
220 	fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : 32;
221 
222 	if(stereo == 1 || single == SINGLE_MIX) /* I don't see mixing handled here */
223 	single = SINGLE_LEFT;
224 
225 	if(I_step_one(balloc,scale_index,fr))
226 	{
227 		if(NOQUIET)
228 			error("Aborting layer I decoding after step one.");
229 		return clip;
230 	}
231 
232 	for(i=0;i<SCALE_BLOCK;i++)
233 	{
234 		if(I_step_two(fraction,balloc,scale_index,fr))
235 		{
236 			if(NOQUIET)
237 				error("Aborting layer I decoding after step two.");
238 			return clip;
239 		}
240 
241 		if(single != SINGLE_STEREO)
242 		clip += (fr->synth_mono)(fraction[single], fr);
243 		else
244 		clip += (fr->synth_stereo)(fraction[0], fraction[1], fr);
245 	}
246 
247 	return clip;
248 }
249 
250 
251