1 | /********************************************************************** <BR>
|
---|
2 | This file is part of Crack dot Com's free source code release of
|
---|
3 | Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
|
---|
4 | information about compiling & licensing issues visit this URL</a>
|
---|
5 | <PRE> If that doesn't help, contact Jonathan Clark at
|
---|
6 | golgotha_source@usa.net (Subject should have "GOLG" in it)
|
---|
7 | ***********************************************************************/
|
---|
8 |
|
---|
9 | /*
|
---|
10 | * Mpeg Layer-2 audio decoder
|
---|
11 | * --------------------------
|
---|
12 | * copyright (c) 1995 by Michael Hipp, All rights reserved. See also 'README'
|
---|
13 | *
|
---|
14 | */
|
---|
15 |
|
---|
16 | #include "loaders/mp3/mpg123.hh"
|
---|
17 |
|
---|
18 | static int grp_3tab[32 * 3] = { 0, }; /* used: 27 */
|
---|
19 | static int grp_5tab[128 * 3] = { 0, }; /* used: 125 */
|
---|
20 | static int grp_9tab[1024 * 3] = { 0, }; /* used: 729 */
|
---|
21 |
|
---|
22 | real muls[27][64]; /* also used by layer 1 */
|
---|
23 |
|
---|
24 | void init_layer2(void)
|
---|
25 | {
|
---|
26 | static double mulmul[27] = {
|
---|
27 | 0.0 , -2.0/3.0 , 2.0/3.0 ,
|
---|
28 | 2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 ,
|
---|
29 | 2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 ,
|
---|
30 | 2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 ,
|
---|
31 | -4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 ,
|
---|
32 | -8.0/9.0 , -4.0/9.0 , -2.0/9.0 , 2.0/9.0 , 4.0/9.0 , 8.0/9.0 };
|
---|
33 | static int base[3][9] = {
|
---|
34 | { 1 , 0, 2 , } ,
|
---|
35 | { 17, 18, 0 , 19, 20 , } ,
|
---|
36 | { 21, 1, 22, 23, 0, 24, 25, 2, 26 } };
|
---|
37 | int i,j,k,l,len;
|
---|
38 | real *table;
|
---|
39 | static int tablen[3] = { 3 , 5 , 9 };
|
---|
40 | static int *itable,*tables[3] = { grp_3tab , grp_5tab , grp_9tab };
|
---|
41 |
|
---|
42 | memset(grp_3tab, 0, sizeof(grp_3tab));
|
---|
43 | memset(grp_5tab, 0, sizeof(grp_5tab));
|
---|
44 | memset(grp_9tab, 0, sizeof(grp_9tab));
|
---|
45 | memset(grp_9tab, 0, sizeof(grp_9tab));
|
---|
46 |
|
---|
47 | for(i=0;i<3;i++)
|
---|
48 | {
|
---|
49 | itable = tables[i];
|
---|
50 | len = tablen[i];
|
---|
51 | for(j=0;j<len;j++)
|
---|
52 | for(k=0;k<len;k++)
|
---|
53 | for(l=0;l<len;l++)
|
---|
54 | {
|
---|
55 | *itable++ = base[i][l];
|
---|
56 | *itable++ = base[i][k];
|
---|
57 | *itable++ = base[i][j];
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | for(k=0;k<27;k++)
|
---|
62 | {
|
---|
63 | double m=mulmul[k];
|
---|
64 | table = muls[k];
|
---|
65 | for(j=3,i=0;i<63;i++,j--)
|
---|
66 | *table++ = m * pow(2.0,(double) j / 3.0);
|
---|
67 | *table++ = 0.0;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | void II_step_one(unsigned int *bit_alloc,int *scale,struct frame *fr)
|
---|
73 | {
|
---|
74 | int stereo = fr->stereo-1;
|
---|
75 | int sblimit = fr->II_sblimit;
|
---|
76 | int jsbound = fr->jsbound;
|
---|
77 | int sblimit2 = fr->II_sblimit<<stereo;
|
---|
78 | struct al_table *alloc1 = fr->alloc;
|
---|
79 | int i;
|
---|
80 | unsigned int *scfsi,*bita;
|
---|
81 | int sc,step;
|
---|
82 | static unsigned int scfsi_buf[64];
|
---|
83 |
|
---|
84 | bita = bit_alloc;
|
---|
85 | if(stereo)
|
---|
86 | {
|
---|
87 | for (i=jsbound;i;i--,alloc1+=(1<<step))
|
---|
88 | {
|
---|
89 | *bita++ = (char) getbits(step=alloc1->bits);
|
---|
90 | *bita++ = (char) getbits(step);
|
---|
91 | }
|
---|
92 | for (i=sblimit-jsbound;i;i--,alloc1+=(1<<step))
|
---|
93 | {
|
---|
94 | bita[0] = (char) getbits(step=alloc1->bits);
|
---|
95 | bita[1] = bita[0];
|
---|
96 | bita+=2;
|
---|
97 | }
|
---|
98 | bita = bit_alloc;
|
---|
99 | scfsi=scfsi_buf;
|
---|
100 | for (i=sblimit2;i;i--)
|
---|
101 | if (*bita++)
|
---|
102 | *scfsi++ = (char) getbits_fast(2);
|
---|
103 | }
|
---|
104 | else /* mono */
|
---|
105 | {
|
---|
106 | for (i=sblimit;i;i--,alloc1+=(1<<step))
|
---|
107 | *bita++ = (char) getbits(step=alloc1->bits);
|
---|
108 | bita = bit_alloc;
|
---|
109 | scfsi=scfsi_buf;
|
---|
110 | for (i=sblimit;i;i--)
|
---|
111 | if (*bita++)
|
---|
112 | *scfsi++ = (char) getbits_fast(2);
|
---|
113 | }
|
---|
114 |
|
---|
115 | bita = bit_alloc;
|
---|
116 | scfsi=scfsi_buf;
|
---|
117 | for (i=sblimit2;i;i--)
|
---|
118 | if (*bita++)
|
---|
119 | switch (*scfsi++)
|
---|
120 | {
|
---|
121 | case 0:
|
---|
122 | *scale++ = getbits_fast(6);
|
---|
123 | *scale++ = getbits_fast(6);
|
---|
124 | *scale++ = getbits_fast(6);
|
---|
125 | break;
|
---|
126 | case 1 :
|
---|
127 | *scale++ = sc = getbits_fast(6);
|
---|
128 | *scale++ = sc;
|
---|
129 | *scale++ = getbits_fast(6);
|
---|
130 | break;
|
---|
131 | case 2:
|
---|
132 | *scale++ = sc = getbits_fast(6);
|
---|
133 | *scale++ = sc;
|
---|
134 | *scale++ = sc;
|
---|
135 | break;
|
---|
136 | default: /* case 3 */
|
---|
137 | *scale++ = getbits_fast(6);
|
---|
138 | *scale++ = sc = getbits_fast(6);
|
---|
139 | *scale++ = sc;
|
---|
140 | break;
|
---|
141 | }
|
---|
142 |
|
---|
143 | }
|
---|
144 |
|
---|
145 | void II_step_two(unsigned int *bit_alloc,real fraction[2][4][SBLIMIT],int *scale,struct frame *fr,int x1)
|
---|
146 | {
|
---|
147 | int i,j,k,ba;
|
---|
148 | int stereo = fr->stereo;
|
---|
149 | int sblimit = fr->II_sblimit;
|
---|
150 | int jsbound = fr->jsbound;
|
---|
151 | struct al_table *alloc2,*alloc1 = fr->alloc;
|
---|
152 | unsigned int *bita=bit_alloc;
|
---|
153 | int d1,step;
|
---|
154 |
|
---|
155 | for (i=0;i<jsbound;i++,alloc1+=(1<<step))
|
---|
156 | {
|
---|
157 | step = alloc1->bits;
|
---|
158 | for (j=0;j<stereo;j++)
|
---|
159 | {
|
---|
160 | if ( (ba=*bita++) )
|
---|
161 | {
|
---|
162 | k=(alloc2 = alloc1+ba)->bits;
|
---|
163 | if( (d1=alloc2->d) < 0)
|
---|
164 | {
|
---|
165 | real cm=muls[k][scale[x1]];
|
---|
166 | fraction[j][0][i] = ((real) ((int)getbits(k) + d1)) * cm;
|
---|
167 | fraction[j][1][i] = ((real) ((int)getbits(k) + d1)) * cm;
|
---|
168 | fraction[j][2][i] = ((real) ((int)getbits(k) + d1)) * cm;
|
---|
169 | }
|
---|
170 | else
|
---|
171 | {
|
---|
172 | static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
|
---|
173 | unsigned int idx,*tab,m=scale[x1];
|
---|
174 | idx = (unsigned int) getbits(k);
|
---|
175 | tab = (unsigned int *) (table[d1] + idx + idx + idx);
|
---|
176 | fraction[j][0][i] = muls[*tab++][m];
|
---|
177 | fraction[j][1][i] = muls[*tab++][m];
|
---|
178 | fraction[j][2][i] = muls[*tab][m];
|
---|
179 | }
|
---|
180 | scale+=3;
|
---|
181 | }
|
---|
182 | else
|
---|
183 | fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | for (i=jsbound;i<sblimit;i++,alloc1+=(1<<step))
|
---|
188 | {
|
---|
189 | step = alloc1->bits;
|
---|
190 | bita++; /* channel 1 and channel 2 bitalloc are the same */
|
---|
191 | if ( (ba=*bita++) )
|
---|
192 | {
|
---|
193 | k=(alloc2 = alloc1+ba)->bits;
|
---|
194 | if( (d1=alloc2->d) < 0)
|
---|
195 | {
|
---|
196 | real cm;
|
---|
197 | cm=muls[k][scale[x1+3]];
|
---|
198 | fraction[1][0][i] = (fraction[0][0][i] = (real) ((int)getbits(k) + d1) ) * cm;
|
---|
199 | fraction[1][1][i] = (fraction[0][1][i] = (real) ((int)getbits(k) + d1) ) * cm;
|
---|
200 | fraction[1][2][i] = (fraction[0][2][i] = (real) ((int)getbits(k) + d1) ) * cm;
|
---|
201 | cm=muls[k][scale[x1]];
|
---|
202 | fraction[0][0][i] *= cm; fraction[0][1][i] *= cm; fraction[0][2][i] *= cm;
|
---|
203 | }
|
---|
204 | else
|
---|
205 | {
|
---|
206 | static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
|
---|
207 | unsigned int idx,*tab,m1,m2;
|
---|
208 | m1 = scale[x1]; m2 = scale[x1+3];
|
---|
209 | idx = (unsigned int) getbits(k);
|
---|
210 | tab = (unsigned int *) (table[d1] + idx + idx + idx);
|
---|
211 | fraction[0][0][i] = muls[*tab][m1]; fraction[1][0][i] = muls[*tab++][m2];
|
---|
212 | fraction[0][1][i] = muls[*tab][m1]; fraction[1][1][i] = muls[*tab++][m2];
|
---|
213 | fraction[0][2][i] = muls[*tab][m1]; fraction[1][2][i] = muls[*tab][m2];
|
---|
214 | }
|
---|
215 | scale+=6;
|
---|
216 | }
|
---|
217 | else {
|
---|
218 | fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] =
|
---|
219 | fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0;
|
---|
220 | }
|
---|
221 | /*
|
---|
222 | should we use individual scalefac for channel 2 or
|
---|
223 | is the current way the right one , where we just copy channel 1 to
|
---|
224 | channel 2 ??
|
---|
225 | The current 'strange' thing is, that we throw away the scalefac
|
---|
226 | values for the second channel ...!!
|
---|
227 | -> changed .. now we use the scalefac values of channel one !!
|
---|
228 | */
|
---|
229 | }
|
---|
230 |
|
---|
231 | if(sblimit > (SBLIMIT>>fr->down_sample) )
|
---|
232 | sblimit = SBLIMIT>>fr->down_sample;
|
---|
233 |
|
---|
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;
|
---|
237 |
|
---|
238 | }
|
---|
239 |
|
---|
240 | int do_layer2(struct frame *fr,int outmode,struct audio_info_struct *ai)
|
---|
241 | {
|
---|
242 | int clip=0;
|
---|
243 | int i,j;
|
---|
244 | int stereo = fr->stereo;
|
---|
245 | real fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */
|
---|
246 | unsigned int bit_alloc[64];
|
---|
247 | int scale[192];
|
---|
248 | int single = fr->single;
|
---|
249 |
|
---|
250 | if(stereo == 1 || single == 3)
|
---|
251 | single = 0;
|
---|
252 |
|
---|
253 | II_step_one(bit_alloc, scale, fr);
|
---|
254 |
|
---|
255 | for (i=0;i<SCALE_BLOCK;i++)
|
---|
256 | {
|
---|
257 | II_step_two(bit_alloc,fraction,scale,fr,i>>2);
|
---|
258 | for (j=0;j<3;j++)
|
---|
259 | {
|
---|
260 | if(single >= 0)
|
---|
261 | {
|
---|
262 | clip += (fr->synth_mono) (fraction[single][j],pcm_sample+pcm_point);
|
---|
263 | }
|
---|
264 | else {
|
---|
265 | clip += (fr->synth) (fraction[0][j],0,pcm_sample+pcm_point);
|
---|
266 | clip += (fr->synth) (fraction[1][j],1,pcm_sample+pcm_point);
|
---|
267 | }
|
---|
268 | pcm_point += fr->block_size;
|
---|
269 |
|
---|
270 | if(pcm_point == audiobufsize)
|
---|
271 | audio_flush(outmode,ai);
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | return clip;
|
---|
276 | }
|
---|
277 |
|
---|
278 |
|
---|