Mercurial > SDL_sound_CoreAudio
comparison decoders/libmpg123/layer2.c @ 562:7e08477b0fc1
MP3 decoder upgrade work.
Ripped out SMPEG and mpglib support, replaced it with "mpg123.c" and libmpg123.
libmpg123 is a much better version of mpglib, so it should solve all the
problems about MP3's not seeking, or most modern MP3's not playing at all,
etc. Since you no longer have to make a tradeoff with SMPEG for features, and
SMPEG is basically rotting, I removed it from the project.
There is still work to be done with libmpg123...there are MMX, 3DNow, SSE,
Altivec, etc decoders which we don't have enabled at the moment, and the
build system could use some work to make this compile more cleanly, etc.
Still: huge win.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Fri, 30 Jan 2009 02:44:47 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
561:f2985e08589c | 562:7e08477b0fc1 |
---|---|
1 /* | |
2 layer2.c: the layer 2 decoder, root of mpg123 | |
3 | |
4 copyright 1994-2007 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 */ | |
10 | |
11 | |
12 #include "mpg123lib_intern.h" | |
13 #include "l2tables.h" | |
14 #include "getbits.h" | |
15 | |
16 static int grp_3tab[32 * 3] = { 0, }; /* used: 27 */ | |
17 static int grp_5tab[128 * 3] = { 0, }; /* used: 125 */ | |
18 static int grp_9tab[1024 * 3] = { 0, }; /* used: 729 */ | |
19 | |
20 static const double mulmul[27] = | |
21 { | |
22 0.0 , -2.0/3.0 , 2.0/3.0 , | |
23 2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 , | |
24 2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 , | |
25 2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 , | |
26 -4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 , | |
27 -8.0/9.0 , -4.0/9.0 , -2.0/9.0 , 2.0/9.0 , 4.0/9.0 , 8.0/9.0 | |
28 }; | |
29 | |
30 void init_layer2(void) | |
31 { | |
32 const int base[3][9] = { | |
33 { 1 , 0, 2 , } , | |
34 { 17, 18, 0 , 19, 20 , } , | |
35 { 21, 1, 22, 23, 0, 24, 25, 2, 26 } }; | |
36 int i,j,k,l,len; | |
37 const int tablen[3] = { 3 , 5 , 9 }; | |
38 int *itable; | |
39 int *tables[3] = { grp_3tab , grp_5tab , grp_9tab }; | |
40 | |
41 for(i=0;i<3;i++) | |
42 { | |
43 itable = tables[i]; | |
44 len = tablen[i]; | |
45 for(j=0;j<len;j++) | |
46 for(k=0;k<len;k++) | |
47 for(l=0;l<len;l++) | |
48 { | |
49 *itable++ = base[i][l]; | |
50 *itable++ = base[i][k]; | |
51 *itable++ = base[i][j]; | |
52 } | |
53 } | |
54 } | |
55 | |
56 void init_layer2_stuff(mpg123_handle *fr) | |
57 { | |
58 int k; | |
59 real *table; | |
60 for(k=0;k<27;k++) | |
61 { | |
62 table = opt_init_layer2_table(fr)(fr, fr->muls[k], mulmul[k]); | |
63 *table++ = 0.0; | |
64 } | |
65 } | |
66 | |
67 real* init_layer2_table(mpg123_handle *fr, real *table, double m) | |
68 { | |
69 int i,j; | |
70 for(j=3,i=0;i<63;i++,j--) | |
71 *table++ = m * pow(2.0,(double) j / 3.0); | |
72 | |
73 return table; | |
74 } | |
75 | |
76 #ifdef OPT_MMXORSSE | |
77 real* init_layer2_table_mmx(mpg123_handle *fr, real *table, double m) | |
78 { | |
79 int i,j; | |
80 if(!fr->p.down_sample) | |
81 for(j=3,i=0;i<63;i++,j--) | |
82 *table++ = 16384 * m * pow(2.0,(double) j / 3.0); | |
83 else | |
84 for(j=3,i=0;i<63;i++,j--) | |
85 *table++ = m * pow(2.0,(double) j / 3.0); | |
86 | |
87 return table; | |
88 } | |
89 #endif | |
90 | |
91 void II_step_one(unsigned int *bit_alloc,int *scale,mpg123_handle *fr) | |
92 { | |
93 int stereo = fr->stereo-1; | |
94 int sblimit = fr->II_sblimit; | |
95 int jsbound = fr->jsbound; | |
96 int sblimit2 = fr->II_sblimit<<stereo; | |
97 const struct al_table *alloc1 = fr->alloc; | |
98 int i; | |
99 /* static unsigned int scfsi_buf[64]; */ | |
100 unsigned int scfsi_buf[64]; | |
101 unsigned int *scfsi,*bita; | |
102 int sc,step; | |
103 | |
104 bita = bit_alloc; | |
105 if(stereo) | |
106 { | |
107 for (i=jsbound;i;i--,alloc1+=(1<<step)) | |
108 { | |
109 step=alloc1->bits; | |
110 *bita++ = (char) getbits(fr, step); | |
111 *bita++ = (char) getbits(fr, step); | |
112 } | |
113 for (i=sblimit-jsbound;i;i--,alloc1+=(1<<step)) | |
114 { | |
115 step=alloc1->bits; | |
116 bita[0] = (char) getbits(fr, step); | |
117 bita[1] = bita[0]; | |
118 bita+=2; | |
119 } | |
120 bita = bit_alloc; | |
121 scfsi=scfsi_buf; | |
122 for (i=sblimit2;i;i--) | |
123 if (*bita++) | |
124 *scfsi++ = (char) getbits_fast(fr, 2); | |
125 } | |
126 else /* mono */ | |
127 { | |
128 for (i=sblimit;i;i--,alloc1+=(1<<step)) | |
129 { | |
130 step=alloc1->bits; | |
131 *bita++ = (char) getbits(fr, step); | |
132 } | |
133 bita = bit_alloc; | |
134 scfsi=scfsi_buf; | |
135 for (i=sblimit;i;i--) | |
136 if (*bita++) | |
137 *scfsi++ = (char) getbits_fast(fr, 2); | |
138 } | |
139 | |
140 bita = bit_alloc; | |
141 scfsi=scfsi_buf; | |
142 for (i=sblimit2;i;i--) | |
143 if (*bita++) | |
144 switch (*scfsi++) | |
145 { | |
146 case 0: | |
147 *scale++ = getbits_fast(fr, 6); | |
148 *scale++ = getbits_fast(fr, 6); | |
149 *scale++ = getbits_fast(fr, 6); | |
150 break; | |
151 case 1 : | |
152 *scale++ = sc = getbits_fast(fr, 6); | |
153 *scale++ = sc; | |
154 *scale++ = getbits_fast(fr, 6); | |
155 break; | |
156 case 2: | |
157 *scale++ = sc = getbits_fast(fr, 6); | |
158 *scale++ = sc; | |
159 *scale++ = sc; | |
160 break; | |
161 default: /* case 3 */ | |
162 *scale++ = getbits_fast(fr, 6); | |
163 *scale++ = sc = getbits_fast(fr, 6); | |
164 *scale++ = sc; | |
165 break; | |
166 } | |
167 | |
168 } | |
169 | |
170 void II_step_two(unsigned int *bit_alloc,real fraction[2][4][SBLIMIT],int *scale,mpg123_handle *fr,int x1) | |
171 { | |
172 int i,j,k,ba; | |
173 int stereo = fr->stereo; | |
174 int sblimit = fr->II_sblimit; | |
175 int jsbound = fr->jsbound; | |
176 const struct al_table *alloc2,*alloc1 = fr->alloc; | |
177 unsigned int *bita=bit_alloc; | |
178 int d1,step; | |
179 | |
180 for (i=0;i<jsbound;i++,alloc1+=(1<<step)) | |
181 { | |
182 step = alloc1->bits; | |
183 for (j=0;j<stereo;j++) | |
184 { | |
185 if ( (ba=*bita++) ) | |
186 { | |
187 k=(alloc2 = alloc1+ba)->bits; | |
188 if( (d1=alloc2->d) < 0) | |
189 { | |
190 real cm=fr->muls[k][scale[x1]]; | |
191 fraction[j][0][i] = ((real) ((int)getbits(fr, k) + d1)) * cm; | |
192 fraction[j][1][i] = ((real) ((int)getbits(fr, k) + d1)) * cm; | |
193 fraction[j][2][i] = ((real) ((int)getbits(fr, k) + d1)) * cm; | |
194 } | |
195 else | |
196 { | |
197 const int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab }; | |
198 unsigned int idx,*tab,m=scale[x1]; | |
199 idx = (unsigned int) getbits(fr, k); | |
200 tab = (unsigned int *) (table[d1] + idx + idx + idx); | |
201 fraction[j][0][i] = fr->muls[*tab++][m]; | |
202 fraction[j][1][i] = fr->muls[*tab++][m]; | |
203 fraction[j][2][i] = fr->muls[*tab][m]; | |
204 } | |
205 scale+=3; | |
206 } | |
207 else | |
208 fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0; | |
209 } | |
210 } | |
211 | |
212 for (i=jsbound;i<sblimit;i++,alloc1+=(1<<step)) | |
213 { | |
214 step = alloc1->bits; | |
215 bita++; /* channel 1 and channel 2 bitalloc are the same */ | |
216 if ( (ba=*bita++) ) | |
217 { | |
218 k=(alloc2 = alloc1+ba)->bits; | |
219 if( (d1=alloc2->d) < 0) | |
220 { | |
221 real cm; | |
222 cm=fr->muls[k][scale[x1+3]]; | |
223 fraction[1][0][i] = (fraction[0][0][i] = (real) ((int)getbits(fr, k) + d1) ) * cm; | |
224 fraction[1][1][i] = (fraction[0][1][i] = (real) ((int)getbits(fr, k) + d1) ) * cm; | |
225 fraction[1][2][i] = (fraction[0][2][i] = (real) ((int)getbits(fr, k) + d1) ) * cm; | |
226 cm=fr->muls[k][scale[x1]]; | |
227 fraction[0][0][i] *= cm; fraction[0][1][i] *= cm; fraction[0][2][i] *= cm; | |
228 } | |
229 else | |
230 { | |
231 const int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab }; | |
232 unsigned int idx,*tab,m1,m2; | |
233 m1 = scale[x1]; m2 = scale[x1+3]; | |
234 idx = (unsigned int) getbits(fr, k); | |
235 tab = (unsigned int *) (table[d1] + idx + idx + idx); | |
236 fraction[0][0][i] = fr->muls[*tab][m1]; fraction[1][0][i] = fr->muls[*tab++][m2]; | |
237 fraction[0][1][i] = fr->muls[*tab][m1]; fraction[1][1][i] = fr->muls[*tab++][m2]; | |
238 fraction[0][2][i] = fr->muls[*tab][m1]; fraction[1][2][i] = fr->muls[*tab][m2]; | |
239 } | |
240 scale+=6; | |
241 } | |
242 else { | |
243 fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] = | |
244 fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0; | |
245 } | |
246 /* | |
247 should we use individual scalefac for channel 2 or | |
248 is the current way the right one , where we just copy channel 1 to | |
249 channel 2 ?? | |
250 The current 'strange' thing is, that we throw away the scalefac | |
251 values for the second channel ...!! | |
252 -> changed .. now we use the scalefac values of channel one !! | |
253 */ | |
254 } | |
255 | |
256 if(sblimit > (fr->down_sample_sblimit) ) | |
257 sblimit = fr->down_sample_sblimit; | |
258 | |
259 for(i=sblimit;i<SBLIMIT;i++) | |
260 for (j=0;j<stereo;j++) | |
261 fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0; | |
262 | |
263 } | |
264 | |
265 static void II_select_table(mpg123_handle *fr) | |
266 { | |
267 const int translate[3][2][16] = | |
268 { { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } , | |
269 { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } , | |
270 { { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } , | |
271 { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } , | |
272 { { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } , | |
273 { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } }; | |
274 | |
275 int table,sblim; | |
276 const struct al_table *tables[5] = | |
277 { alloc_0, alloc_1, alloc_2, alloc_3 , alloc_4 }; | |
278 const int sblims[5] = { 27 , 30 , 8, 12 , 30 }; | |
279 | |
280 if(fr->sampling_frequency >= 3) /* Or equivalent: (fr->lsf == 1) */ | |
281 table = 4; | |
282 else | |
283 table = translate[fr->sampling_frequency][2-fr->stereo][fr->bitrate_index]; | |
284 sblim = sblims[table]; | |
285 | |
286 fr->alloc = tables[table]; | |
287 fr->II_sblimit = sblim; | |
288 } | |
289 | |
290 | |
291 int do_layer2(mpg123_handle *fr) | |
292 { | |
293 int clip=0; | |
294 int i,j; | |
295 int stereo = fr->stereo; | |
296 ALIGNED(16) real fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */ | |
297 unsigned int bit_alloc[64]; | |
298 int scale[192]; | |
299 int single = fr->single; | |
300 | |
301 II_select_table(fr); | |
302 fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? | |
303 (fr->mode_ext<<2)+4 : fr->II_sblimit; | |
304 | |
305 if (fr->jsbound > fr->II_sblimit) { | |
306 fprintf(stderr, "Truncating stereo boundary to sideband limit.\n"); | |
307 fr->jsbound=fr->II_sblimit; | |
308 } | |
309 | |
310 if(stereo == 1 || single == SINGLE_MIX) /* also, mix not really handled */ | |
311 single = SINGLE_LEFT; | |
312 | |
313 II_step_one(bit_alloc, scale, fr); | |
314 | |
315 for (i=0;i<SCALE_BLOCK;i++) | |
316 { | |
317 II_step_two(bit_alloc,fraction,scale,fr,i>>2); | |
318 for (j=0;j<3;j++) | |
319 { | |
320 if(single != SINGLE_STEREO) | |
321 { | |
322 clip += (fr->synth_mono) (fraction[single][j], fr); | |
323 } | |
324 else | |
325 { | |
326 clip += (fr->synth) (fraction[0][j], 0, fr, 0); | |
327 clip += (fr->synth) (fraction[1][j], 1, fr, 1); | |
328 } | |
329 } | |
330 } | |
331 | |
332 return clip; | |
333 } | |
334 | |
335 |