61
|
1
|
|
2 /*
|
|
3 * Mpeg Layer-1 audio decoder
|
|
4 * --------------------------
|
|
5 * copyright (c) 1995 by Michael Hipp, All rights reserved. See also 'README'
|
|
6 * near unoptimzed ...
|
|
7 *
|
|
8 * may have a few bugs after last optimization ...
|
|
9 *
|
|
10 */
|
|
11
|
|
12
|
|
13 #include "audacious/output.h"
|
|
14 #include "mpg123.h"
|
|
15 #include "getbits.h"
|
|
16
|
|
17
|
|
18 /* Used by the getbits macros */
|
|
19 static unsigned long rval;
|
|
20
|
|
21 void
|
|
22 I_step_one(unsigned int balloc[],
|
|
23 unsigned int scale_index[2][SBLIMIT], struct frame *fr)
|
|
24 {
|
|
25 unsigned int *ba = balloc;
|
|
26 unsigned int *sca = (unsigned int *) scale_index;
|
|
27
|
|
28 if (fr->stereo) {
|
|
29 int i;
|
|
30 int jsbound = fr->jsbound;
|
|
31
|
|
32 for (i = 0; i < jsbound; i++) {
|
|
33 *ba++ = mpg123_getbits(4);
|
|
34 *ba++ = mpg123_getbits(4);
|
|
35 }
|
|
36 for (i = jsbound; i < SBLIMIT; i++)
|
|
37 *ba++ = mpg123_getbits(4);
|
|
38
|
|
39 ba = balloc;
|
|
40
|
|
41 for (i = 0; i < jsbound; i++) {
|
|
42 if ((*ba++))
|
|
43 *sca++ = mpg123_getbits(6);
|
|
44 if ((*ba++))
|
|
45 *sca++ = mpg123_getbits(6);
|
|
46 }
|
|
47 for (i = jsbound; i < SBLIMIT; i++)
|
|
48 if ((*ba++)) {
|
|
49 *sca++ = mpg123_getbits(6);
|
|
50 *sca++ = mpg123_getbits(6);
|
|
51 }
|
|
52 }
|
|
53 else {
|
|
54 int i;
|
|
55
|
|
56 for (i = 0; i < SBLIMIT; i++)
|
|
57 *ba++ = mpg123_getbits(4);
|
|
58 ba = balloc;
|
|
59 for (i = 0; i < SBLIMIT; i++)
|
|
60 if ((*ba++))
|
|
61 *sca++ = mpg123_getbits(6);
|
|
62 }
|
|
63 }
|
|
64
|
|
65 void
|
|
66 I_step_two(real fraction[2][SBLIMIT],
|
|
67 unsigned int balloc[2 * SBLIMIT],
|
|
68 unsigned int scale_index[2][SBLIMIT], struct frame *fr)
|
|
69 {
|
|
70 int i, n;
|
|
71 int smpb[2 * SBLIMIT]; /* values: 0-65535 */
|
|
72 int *sample;
|
|
73 register unsigned int *ba;
|
|
74 register unsigned int *sca = (unsigned int *) scale_index;
|
|
75
|
|
76 if (fr->stereo) {
|
|
77 int jsbound = fr->jsbound;
|
|
78 register real *f0 = fraction[0];
|
|
79 register real *f1 = fraction[1];
|
|
80
|
|
81 ba = balloc;
|
|
82 for (sample = smpb, i = 0; i < jsbound; i++) {
|
|
83 if ((n = *ba++))
|
|
84 *sample++ = mpg123_getbits(n + 1);
|
|
85 if ((n = *ba++))
|
|
86 *sample++ = mpg123_getbits(n + 1);
|
|
87 }
|
|
88 for (i = jsbound; i < SBLIMIT; i++)
|
|
89 if ((n = *ba++))
|
|
90 *sample++ = mpg123_getbits(n + 1);
|
|
91
|
|
92 ba = balloc;
|
|
93 for (sample = smpb, i = 0; i < jsbound; i++) {
|
|
94 if ((n = *ba++))
|
|
95 *f0++ =
|
|
96 (real) (((-1) << n) + (*sample++) +
|
|
97 1) * mpg123_muls[n + 1][*sca++];
|
|
98 else
|
|
99 *f0++ = 0.0;
|
|
100 if ((n = *ba++))
|
|
101 *f1++ =
|
|
102 (real) (((-1) << n) + (*sample++) +
|
|
103 1) * mpg123_muls[n + 1][*sca++];
|
|
104 else
|
|
105 *f1++ = 0.0;
|
|
106 }
|
|
107 for (i = jsbound; i < SBLIMIT; i++) {
|
|
108 if ((n = *ba++)) {
|
|
109 real samp = (((-1) << n) + (*sample++) + 1);
|
|
110
|
|
111 *f0++ = samp * mpg123_muls[n + 1][*sca++];
|
|
112 *f1++ = samp * mpg123_muls[n + 1][*sca++];
|
|
113 }
|
|
114 else
|
|
115 *f0++ = *f1++ = 0.0;
|
|
116 }
|
|
117 for (i = fr->down_sample_sblimit; i < 32; i++)
|
|
118 fraction[0][i] = fraction[1][i] = 0.0;
|
|
119 }
|
|
120 else {
|
|
121 register real *f0 = fraction[0];
|
|
122
|
|
123 ba = balloc;
|
|
124 for (sample = smpb, i = 0; i < SBLIMIT; i++)
|
|
125 if ((n = *ba++))
|
|
126 *sample++ = mpg123_getbits(n + 1);
|
|
127 ba = balloc;
|
|
128 for (sample = smpb, i = 0; i < SBLIMIT; i++) {
|
|
129 if ((n = *ba++))
|
|
130 *f0++ =
|
|
131 (real) (((-1) << n) + (*sample++) +
|
|
132 1) * mpg123_muls[n + 1][*sca++];
|
|
133 else
|
|
134 *f0++ = 0.0;
|
|
135 }
|
|
136 for (i = fr->down_sample_sblimit; i < 32; i++)
|
|
137 fraction[0][i] = 0.0;
|
|
138 }
|
|
139 }
|
|
140
|
|
141 int
|
|
142 mpg123_do_layer1(struct frame *fr)
|
|
143 {
|
|
144 int i, stereo = fr->stereo;
|
|
145 unsigned int balloc[2 * SBLIMIT];
|
|
146 unsigned int scale_index[2][SBLIMIT];
|
|
147 real fraction[2][SBLIMIT];
|
|
148 int single = fr->single;
|
|
149
|
|
150 fr->jsbound =
|
|
151 (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : 32;
|
|
152
|
|
153 if (stereo == 1 || single == 3)
|
|
154 single = 0;
|
|
155
|
|
156 I_step_one(balloc, scale_index, fr);
|
|
157
|
|
158 for (i = 0; i < SCALE_BLOCK; i++) {
|
|
159 I_step_two(fraction, balloc, scale_index, fr);
|
|
160
|
|
161 if (single >= 0) {
|
|
162 (fr->synth_mono) ((real *) fraction[single], mpg123_pcm_sample,
|
|
163 &mpg123_pcm_point);
|
|
164 }
|
|
165 else {
|
|
166 int p1 = mpg123_pcm_point;
|
|
167
|
|
168 (fr->synth) ((real *) fraction[0], 0, mpg123_pcm_sample, &p1);
|
|
169 (fr->synth) ((real *) fraction[1], 1, mpg123_pcm_sample,
|
|
170 &mpg123_pcm_point);
|
|
171 }
|
|
172 if (mpg123_info->output_audio && mpg123_info->jump_to_time == -1) {
|
|
173 produce_audio(mpg123_ip.output->written_time(),
|
|
174 mpg123_cfg.resolution ==
|
|
175 16 ? FMT_S16_NE : FMT_U8,
|
|
176 mpg123_cfg.channels ==
|
|
177 2 ? fr->stereo : 1, mpg123_pcm_point,
|
|
178 mpg123_pcm_sample, &mpg123_info->going);
|
|
179 }
|
|
180
|
|
181 mpg123_pcm_point = 0;
|
|
182 }
|
|
183
|
|
184 return 1;
|
|
185 }
|