wined3d: Request alpha in backbuffer mode, to work correctly with multiple opengl...
[wine.git] / dlls / winemp3.acm / layer2.c
blob535d90c1f40bba915d21e4a13e462eb00742a6bd
1 /*
2 * Mpeg Layer-2 audio decoder
3 * --------------------------
4 * copyright (c) 1995 by Michael Hipp, All rights reserved. See also 'README'
5 * This file has been copied from mpglib.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "mpg123.h"
23 #include "l2tables.h"
25 static int grp_3tab[32 * 3] = { 0, }; /* used: 27 */
26 static int grp_5tab[128 * 3] = { 0, }; /* used: 125 */
27 static int grp_9tab[1024 * 3] = { 0, }; /* used: 729 */
29 real muls[27][64]; /* also used by layer 1 */
31 void init_layer2(void)
33 static const double mulmul[27] = {
34 0.0 , -2.0/3.0 , 2.0/3.0 ,
35 2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 ,
36 2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 ,
37 2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 ,
38 -4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 ,
39 -8.0/9.0 , -4.0/9.0 , -2.0/9.0 , 2.0/9.0 , 4.0/9.0 , 8.0/9.0 };
40 static const int base[3][9] = {
41 { 1 , 0, 2 , } ,
42 { 17, 18, 0 , 19, 20 , } ,
43 { 21, 1, 22, 23, 0, 24, 25, 2, 26 } };
44 int i,j,k,l,len;
45 real *table;
46 static const int tablen[3] = { 3 , 5 , 9 };
47 static int *itable,*tables[3] = { grp_3tab , grp_5tab , grp_9tab };
49 for(i=0;i<3;i++)
51 itable = tables[i];
52 len = tablen[i];
53 for(j=0;j<len;j++)
54 for(k=0;k<len;k++)
55 for(l=0;l<len;l++)
57 *itable++ = base[i][l];
58 *itable++ = base[i][k];
59 *itable++ = base[i][j];
63 for(k=0;k<27;k++)
65 double m=mulmul[k];
66 table = muls[k];
67 for(j=3,i=0;i<63;i++,j--)
68 *table++ = m * pow(2.0,(double) j / 3.0);
69 *table++ = 0.0;
74 static void II_step_one(unsigned int *bit_alloc,int *scale,struct frame *fr)
76 int stereo = fr->stereo-1;
77 int sblimit = fr->II_sblimit;
78 int jsbound = fr->jsbound;
79 int sblimit2 = fr->II_sblimit<<stereo;
80 const struct al_table *alloc1 = fr->alloc;
81 int i;
82 static unsigned int scfsi_buf[64];
83 unsigned int *scfsi,*bita;
84 int sc,step;
86 bita = bit_alloc;
87 if(stereo)
89 for (i=jsbound;i;i--,alloc1+=(1<<step))
91 *bita++ = (char) getbits(step=alloc1->bits);
92 *bita++ = (char) getbits(step);
94 for (i=sblimit-jsbound;i;i--,alloc1+=(1<<step))
96 bita[0] = (char) getbits(step=alloc1->bits);
97 bita[1] = bita[0];
98 bita+=2;
100 bita = bit_alloc;
101 scfsi=scfsi_buf;
102 for (i=sblimit2;i;i--)
103 if (*bita++)
104 *scfsi++ = (char) getbits_fast(2);
106 else /* mono */
108 for (i=sblimit;i;i--,alloc1+=(1<<step))
109 *bita++ = (char) getbits(step=alloc1->bits);
110 bita = bit_alloc;
111 scfsi=scfsi_buf;
112 for (i=sblimit;i;i--)
113 if (*bita++)
114 *scfsi++ = (char) getbits_fast(2);
117 bita = bit_alloc;
118 scfsi=scfsi_buf;
119 for (i=sblimit2;i;i--)
120 if (*bita++)
121 switch (*scfsi++)
123 case 0:
124 *scale++ = getbits_fast(6);
125 *scale++ = getbits_fast(6);
126 *scale++ = getbits_fast(6);
127 break;
128 case 1 :
129 *scale++ = sc = getbits_fast(6);
130 *scale++ = sc;
131 *scale++ = getbits_fast(6);
132 break;
133 case 2:
134 *scale++ = sc = getbits_fast(6);
135 *scale++ = sc;
136 *scale++ = sc;
137 break;
138 default: /* case 3 */
139 *scale++ = getbits_fast(6);
140 *scale++ = sc = getbits_fast(6);
141 *scale++ = sc;
142 break;
147 static void II_step_two(unsigned int *bit_alloc, real fraction[2][4][SBLIMIT],
148 int *scale, struct frame *fr, int x1)
150 int i,j,k,ba;
151 int stereo = fr->stereo;
152 int sblimit = fr->II_sblimit;
153 int jsbound = fr->jsbound;
154 const struct al_table *alloc2,*alloc1 = fr->alloc;
155 unsigned int *bita=bit_alloc;
156 int d1,step;
158 for (i=0;i<jsbound;i++,alloc1+=(1<<step))
160 step = alloc1->bits;
161 for (j=0;j<stereo;j++)
163 if ( (ba=*bita++) )
165 k=(alloc2 = alloc1+ba)->bits;
166 if( (d1=alloc2->d) < 0)
168 real cm=muls[k][scale[x1]];
169 fraction[j][0][i] = ((real) ((int)getbits(k) + d1)) * cm;
170 fraction[j][1][i] = ((real) ((int)getbits(k) + d1)) * cm;
171 fraction[j][2][i] = ((real) ((int)getbits(k) + d1)) * cm;
173 else
175 static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
176 unsigned int idx,*tab,m=scale[x1];
177 idx = getbits(k);
178 tab = (unsigned int *) (table[d1] + idx + idx + idx);
179 fraction[j][0][i] = muls[*tab++][m];
180 fraction[j][1][i] = muls[*tab++][m];
181 fraction[j][2][i] = muls[*tab][m];
183 scale+=3;
185 else
186 fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
190 for (i=jsbound;i<sblimit;i++,alloc1+=(1<<step))
192 step = alloc1->bits;
193 bita++; /* channel 1 and channel 2 bitalloc are the same */
194 if ( (ba=*bita++) )
196 k=(alloc2 = alloc1+ba)->bits;
197 if( (d1=alloc2->d) < 0)
199 real cm;
200 cm=muls[k][scale[x1+3]];
201 fraction[1][0][i] = (fraction[0][0][i] = (real) ((int)getbits(k) + d1) ) * cm;
202 fraction[1][1][i] = (fraction[0][1][i] = (real) ((int)getbits(k) + d1) ) * cm;
203 fraction[1][2][i] = (fraction[0][2][i] = (real) ((int)getbits(k) + d1) ) * cm;
204 cm=muls[k][scale[x1]];
205 fraction[0][0][i] *= cm; fraction[0][1][i] *= cm; fraction[0][2][i] *= cm;
207 else
209 static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
210 unsigned int idx,*tab,m1,m2;
211 m1 = scale[x1]; m2 = scale[x1+3];
212 idx = getbits(k);
213 tab = (unsigned int *) (table[d1] + idx + idx + idx);
214 fraction[0][0][i] = muls[*tab][m1]; fraction[1][0][i] = muls[*tab++][m2];
215 fraction[0][1][i] = muls[*tab][m1]; fraction[1][1][i] = muls[*tab++][m2];
216 fraction[0][2][i] = muls[*tab][m1]; fraction[1][2][i] = muls[*tab][m2];
218 scale+=6;
220 else {
221 fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] =
222 fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0;
225 should we use individual scalefac for channel 2 or
226 is the current way the right one , where we just copy channel 1 to
227 channel 2 ??
228 The current 'strange' thing is, that we throw away the scalefac
229 values for the second channel ...!!
230 -> changed .. now we use the scalefac values of channel one !!
234 for(i=sblimit;i<SBLIMIT;i++)
235 for (j=0;j<stereo;j++)
236 fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
240 static void II_select_table(struct frame *fr)
242 static const int translate[3][2][16] =
243 { { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } ,
244 { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } ,
245 { { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } ,
246 { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } ,
247 { { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } ,
248 { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } };
250 int table,sblim;
251 static const struct al_table *tables[5] =
252 { alloc_0, alloc_1, alloc_2, alloc_3 , alloc_4 };
253 static const int sblims[5] = { 27 , 30 , 8, 12 , 30 };
255 if(fr->lsf)
256 table = 4;
257 else
258 table = translate[fr->sampling_frequency][2-fr->stereo][fr->bitrate_index];
259 sblim = sblims[table];
261 fr->alloc = tables[table];
262 fr->II_sblimit = sblim;
265 int do_layer2(struct frame *fr,unsigned char *pcm_sample,int *pcm_point)
267 int clip=0;
268 int i,j;
269 int stereo = fr->stereo;
270 real fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */
271 unsigned int bit_alloc[64];
272 int scale[192];
273 int single = fr->single;
275 II_select_table(fr);
276 fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ?
277 (fr->mode_ext<<2)+4 : fr->II_sblimit;
279 if(stereo == 1 || single == 3)
280 single = 0;
282 II_step_one(bit_alloc, scale, fr);
284 for (i=0;i<SCALE_BLOCK;i++)
286 II_step_two(bit_alloc,fraction,scale,fr,i>>2);
287 for (j=0;j<3;j++) {
288 if(single >= 0) {
289 clip += synth_1to1_mono(fr->mp,fraction[0][j],pcm_sample,pcm_point);
291 else {
292 int p1 = *pcm_point;
293 clip += synth_1to1(fr->mp,fraction[0][j],0,pcm_sample,&p1);
294 clip += synth_1to1(fr->mp,fraction[1][j],1,pcm_sample,pcm_point);
300 return clip;