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