comparison src/mpg123/layer2.c @ 12:3da1b8942b8b trunk

[svn] - remove src/Input src/Output src/Effect src/General src/Visualization src/Container
author nenolod
date Mon, 18 Sep 2006 03:14:20 -0700
parents src/Input/mpg123/layer2.c@13389e613d67
children 914c96de3244
comparison
equal deleted inserted replaced
11:cff1d04026ae 12:3da1b8942b8b
1 /*
2 * Mpeg Layer-2 audio decoder
3 * --------------------------
4 * copyright (c) 1995 by Michael Hipp, All rights reserved. See also 'README'
5 *
6 */
7
8 #include "mpg123.h"
9 #include "audacious/output.h"
10 #include "l2tables.h"
11 #include "getbits.h"
12
13 #include <math.h>
14
15 static int grp_3tab[32 * 3] = { 0, }; /* used: 27 */
16 static int grp_5tab[128 * 3] = { 0, }; /* used: 125 */
17 static int grp_9tab[1024 * 3] = { 0, }; /* used: 729 */
18
19 mpgdec_real mpgdec_muls[27][64]; /* also used by layer 1 */
20
21 void
22 mpgdec_init_layer2 (gboolean mmx)
23 {
24 static double mulmul[27] = {
25 0.0, -2.0 / 3.0, 2.0 / 3.0, 2.0 / 7.0, 2.0 / 15.0,
26 2.0 / 31.0, 2.0 / 63.0, 2.0 / 127.0, 2.0 / 255.0,
27 2.0 / 511.0, 2.0 / 1023.0, 2.0 / 2047.0, 2.0 / 4095.0,
28 2.0 / 8191.0, 2.0 / 16383.0, 2.0 / 32767.0, 2.0 / 65535.0,
29 -4.0 / 5.0, -2.0 / 5.0, 2.0 / 5.0, 4.0 / 5.0, -8.0 / 9.0,
30 -4.0 / 9.0, -2.0 / 9.0, 2.0 / 9.0, 4.0 / 9.0, 8.0 / 9.0
31 };
32 static 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 };
37 int i, j, k, l, len;
38 mpgdec_real *table;
39 static int tablen[3] = { 3, 5, 9 };
40 static int *itable, *tables[3] = { grp_3tab, grp_5tab, grp_9tab };
41
42 for (i = 0; i < 3; i++)
43 {
44 itable = tables[i];
45 len = tablen[i];
46 for (j = 0; j < len; j++)
47 for (k = 0; k < len; k++)
48 for (l = 0; l < len; l++)
49 {
50 *itable++ = base[i][l];
51 *itable++ = base[i][k];
52 *itable++ = base[i][j];
53 }
54 }
55
56 for (k = 0; k < 27; k++)
57 {
58 double m = mulmul[k];
59 table = mpgdec_muls[k];
60 for (j = 3, i = 0; i < 63; i++, j--)
61 *table++ = m * pow (2.0, (double) j / 3.0);
62 *table++ = 0.0;
63 }
64 }
65
66 void
67 II_step_one (unsigned int *bit_alloc, int *scale, struct frame *fr)
68 {
69 int stereo = fr->stereo - 1;
70 int sblimit = fr->II_sblimit;
71 int jsbound = fr->jsbound;
72 int sblimit2 = fr->II_sblimit << stereo;
73 struct al_table *alloc1 = fr->alloc;
74 int i;
75 static unsigned int scfsi_buf[64];
76 unsigned int *scfsi, *bita;
77 int sc, step;
78
79 bita = bit_alloc;
80 if (stereo)
81 {
82 for (i = jsbound; i > 0; i--, alloc1 += (1 << step))
83 {
84 *bita++ = (char) mpgdec_getbits (&bsi, step = alloc1->bits);
85 *bita++ = (char) mpgdec_getbits (&bsi, step);
86 }
87 for (i = sblimit - jsbound; i > 0; i--, alloc1 += (1 << step))
88 {
89 bita[0] = (char) mpgdec_getbits (&bsi, step = alloc1->bits);
90 bita[1] = bita[0];
91 bita += 2;
92 }
93 bita = bit_alloc;
94 scfsi = scfsi_buf;
95 for (i = sblimit2; i; i--)
96 if (*bita++)
97 *scfsi++ = (char) mpgdec_getbits_fast (&bsi, 2);
98 }
99 else
100 /* mono */
101 {
102 for (i = sblimit; i; i--, alloc1 += (1 << step))
103 *bita++ = (char) mpgdec_getbits (&bsi, step = alloc1->bits);
104 bita = bit_alloc;
105 scfsi = scfsi_buf;
106 for (i = sblimit; i; i--)
107 if (*bita++)
108 *scfsi++ = (char) mpgdec_getbits_fast (&bsi, 2);
109 }
110
111 bita = bit_alloc;
112 scfsi = scfsi_buf;
113 for (i = sblimit2; i; i--)
114 if (*bita++)
115 switch (*scfsi++)
116 {
117 case 0:
118 *scale++ = mpgdec_getbits_fast (&bsi, 6);
119 *scale++ = mpgdec_getbits_fast (&bsi, 6);
120 *scale++ = mpgdec_getbits_fast (&bsi, 6);
121 break;
122 case 1:
123 *scale++ = sc = mpgdec_getbits_fast (&bsi, 6);
124 *scale++ = sc;
125 *scale++ = mpgdec_getbits_fast (&bsi, 6);
126 break;
127 case 2:
128 *scale++ = sc = mpgdec_getbits_fast (&bsi, 6);
129 *scale++ = sc;
130 *scale++ = sc;
131 break;
132 default: /* case 3 */
133 *scale++ = mpgdec_getbits_fast (&bsi, 6);
134 *scale++ = sc = mpgdec_getbits_fast (&bsi, 6);
135 *scale++ = sc;
136 break;
137 }
138
139 }
140
141 void
142 II_step_two (unsigned int *bit_alloc, mpgdec_real fraction[2][4][SBLIMIT],
143 int *scale, struct frame *fr, int x1)
144 {
145 int i, j, k, ba;
146 int stereo = fr->stereo;
147 int sblimit = fr->II_sblimit;
148 int jsbound = fr->jsbound;
149 struct al_table *alloc2, *alloc1 = fr->alloc;
150 unsigned int *bita = bit_alloc;
151 int d1, step;
152
153 for (i = 0; i < jsbound; i++, alloc1 += (1 << step))
154 {
155 step = alloc1->bits;
156 for (j = 0; j < stereo; j++)
157 {
158 if ((ba = *bita++))
159 {
160 k = (alloc2 = alloc1 + ba)->bits;
161 if ((d1 = alloc2->d) < 0)
162 {
163 mpgdec_real cm = mpgdec_muls[k][scale[x1]];
164
165 fraction[j][0][i] =
166 ((mpgdec_real) ((int) mpgdec_getbits (&bsi, k) + d1)) * cm;
167 fraction[j][1][i] =
168 ((mpgdec_real) ((int) mpgdec_getbits (&bsi, k) + d1)) * cm;
169 fraction[j][2][i] =
170 ((mpgdec_real) ((int) mpgdec_getbits (&bsi, k) + d1)) * cm;
171 }
172 else
173 {
174 static int *table[] =
175 { 0, 0, 0, grp_3tab, 0, grp_5tab, 0, 0, 0, grp_9tab };
176 unsigned int idx, *tab, m = scale[x1];
177
178 idx = (unsigned int) mpgdec_getbits (&bsi, k);
179 tab = (unsigned int *) (table[d1] + idx + idx + idx);
180 fraction[j][0][i] = mpgdec_muls[*tab++][m];
181 fraction[j][1][i] = mpgdec_muls[*tab++][m];
182 fraction[j][2][i] = mpgdec_muls[*tab][m];
183 }
184 scale += 3;
185 }
186 else
187 fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
188 }
189 }
190
191 for (i = jsbound; i < sblimit; i++, alloc1 += (1 << step))
192 {
193 step = alloc1->bits;
194 bita++; /* channel 1 and channel 2 bitalloc are the same */
195 if ((ba = *bita++))
196 {
197 k = (alloc2 = alloc1 + ba)->bits;
198 if ((d1 = alloc2->d) < 0)
199 {
200 mpgdec_real cm;
201
202 cm = mpgdec_muls[k][scale[x1 + 3]];
203 fraction[1][0][i] = (fraction[0][0][i] =
204 (mpgdec_real) ((int) mpgdec_getbits (&bsi, k) +
205 d1)) * cm;
206 fraction[1][1][i] = (fraction[0][1][i] =
207 (mpgdec_real) ((int) mpgdec_getbits (&bsi, k) +
208 d1)) * cm;
209 fraction[1][2][i] = (fraction[0][2][i] =
210 (mpgdec_real) ((int) mpgdec_getbits (&bsi, k) +
211 d1)) * cm;
212 cm = mpgdec_muls[k][scale[x1]];
213 fraction[0][0][i] *= cm;
214 fraction[0][1][i] *= cm;
215 fraction[0][2][i] *= cm;
216 }
217 else
218 {
219 static int *table[] =
220 { 0, 0, 0, grp_3tab, 0, grp_5tab, 0, 0, 0, grp_9tab };
221 unsigned int idx, *tab, m1, m2;
222
223 m1 = scale[x1];
224 m2 = scale[x1 + 3];
225 idx = (unsigned int) mpgdec_getbits (&bsi, k);
226 tab = (unsigned int *) (table[d1] + idx + idx + idx);
227 fraction[0][0][i] = mpgdec_muls[*tab][m1];
228 fraction[1][0][i] = mpgdec_muls[*tab++][m2];
229 fraction[0][1][i] = mpgdec_muls[*tab][m1];
230 fraction[1][1][i] = mpgdec_muls[*tab++][m2];
231 fraction[0][2][i] = mpgdec_muls[*tab][m1];
232 fraction[1][2][i] = mpgdec_muls[*tab][m2];
233 }
234 scale += 6;
235 }
236 else
237 {
238 fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] =
239 fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0;
240 }
241 /*
242 should we use individual scalefac for channel 2 or
243 is the current way the right one , where we just copy channel 1 to
244 channel 2 ??
245 The current 'strange' thing is, that we throw away the scalefac
246 values for the second channel ...!!
247 -> changed .. now we use the scalefac values of channel one !!
248 */
249 }
250
251 if (sblimit > (fr->down_sample_sblimit))
252 sblimit = fr->down_sample_sblimit;
253
254 for (i = sblimit; i < SBLIMIT; i++)
255 for (j = 0; j < stereo; j++)
256 fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
257
258 }
259
260 static void
261 II_select_table (struct frame *fr)
262 {
263 static int translate[3][2][16] = {
264 {{0, 2, 2, 2, 2, 2, 2, 0, 0, 0, 1, 1, 1, 1, 1, 0},
265 {0, 2, 2, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}},
266 {{0, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
267 {0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
268 {{0, 3, 3, 3, 3, 3, 3, 0, 0, 0, 1, 1, 1, 1, 1, 0},
269 {0, 3, 3, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}}
270 };
271
272 int table, sblim;
273 static struct al_table *tables[5] =
274 { alloc_0, alloc_1, alloc_2, alloc_3, alloc_4 };
275 static int sblims[5] = { 27, 30, 8, 12, 30 };
276
277 if (fr->lsf)
278 table = 4;
279 else
280 table =
281 translate[fr->sampling_frequency][2 - fr->stereo][fr->bitrate_index];
282 sblim = sblims[table];
283
284 fr->alloc = tables[table];
285 fr->II_sblimit = sblim;
286 }
287
288
289 int
290 mpgdec_do_layer2 (struct frame *fr)
291 {
292 int i, j;
293 int stereo = fr->stereo;
294 mpgdec_real fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */
295 unsigned int bit_alloc[64];
296 int scale[192];
297 int single = fr->single;
298
299 II_select_table (fr);
300 fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ?
301 (fr->mode_ext << 2) + 4 : fr->II_sblimit;
302 if (fr->jsbound > fr->II_sblimit)
303 fr->jsbound = fr->II_sblimit;
304
305 if (stereo == 1 || single == 3)
306 single = 0;
307
308 II_step_one (bit_alloc, scale, fr);
309
310 for (i = 0; i < SCALE_BLOCK; i++)
311 {
312 II_step_two (bit_alloc, fraction, scale, fr, i >> 2);
313 for (j = 0; j < 3; j++)
314 {
315 if (single >= 0)
316 {
317 (fr->synth_mono) (fraction[single][j], mpgdec_pcm_sample,
318 &mpgdec_pcm_point);
319 }
320 else
321 {
322 int p1 = mpgdec_pcm_point;
323
324 (fr->synth) (fraction[0][j], 0, mpgdec_pcm_sample, &p1);
325 (fr->synth) (fraction[1][j], 1, mpgdec_pcm_sample,
326 &mpgdec_pcm_point);
327 }
328
329 /* if(mpgdec_pcm_point >= audiobufsize)
330 audio_flush(outmode,ai); */
331 }
332 }
333 #ifdef PSYCHO
334 psycho_process(mpgdec_pcm_sample, mpgdec_pcm_point, mpgdec_cfg.channels == 2 ? fr->stereo : 1);
335 #endif
336 if (mpgdec_info->output_audio)
337 {
338 produce_audio (mpgdec_ip.output->written_time (),
339 mpgdec_cfg.resolution == 16 ? FMT_S16_NE : FMT_U8,
340 mpgdec_cfg.channels == 2 ? fr->stereo : 1,
341 mpgdec_pcm_point, mpgdec_pcm_sample, &mpgdec_pcm_point);
342 }
343 mpgdec_pcm_point = 0;
344
345 return 1;
346 }