0
|
1
|
|
2 /*
|
|
3 * Mpeg Layer-1,2,3 audio decoder
|
|
4 * ------------------------------
|
|
5 * copyright (c) 1995,1996,1997 by Michael Hipp, All rights reserved.
|
|
6 * See also 'README'
|
|
7 *
|
|
8 */
|
|
9
|
|
10 #include <stdlib.h>
|
|
11 #include <math.h>
|
|
12 #include <string.h>
|
|
13
|
|
14 #include "mpg123.h"
|
|
15
|
|
16 #define WRITE_SAMPLE(samples,sum,clip) \
|
|
17 if( (sum) > 32767.0) { *(samples) = 0x7fff; (clip)++; } \
|
|
18 else if( (sum) < -32768.0) { *(samples) = -0x8000; (clip)++; } \
|
|
19 else { *(samples) = sum; }
|
|
20
|
|
21 int
|
|
22 mpg123_synth_1to1_8bit(real * bandPtr, int channel,
|
|
23 unsigned char *samples, int *pnt)
|
|
24 {
|
|
25 short samples_tmp[64];
|
|
26 short *tmp1 = samples_tmp + channel;
|
|
27 int i, ret;
|
|
28 int pnt1 = 0;
|
|
29
|
|
30 ret =
|
|
31 mpg123_synth_1to1(bandPtr, channel, (unsigned char *) samples_tmp,
|
|
32 &pnt1);
|
|
33 samples += channel + *pnt;
|
|
34
|
|
35 for (i = 0; i < 32; i++) {
|
|
36 *samples = (*tmp1 >> 8) ^ 128;
|
|
37 samples += 2;
|
|
38 tmp1 += 2;
|
|
39 }
|
|
40 *pnt += 64;
|
|
41
|
|
42 return ret;
|
|
43 }
|
|
44
|
|
45 int
|
|
46 mpg123_synth_1to1_8bit_mono(real * bandPtr, unsigned char *samples, int *pnt)
|
|
47 {
|
|
48 short samples_tmp[64];
|
|
49 short *tmp1 = samples_tmp;
|
|
50 int i, ret;
|
|
51 int pnt1 = 0;
|
|
52
|
|
53 ret = mpg123_synth_1to1(bandPtr, 0, (unsigned char *) samples_tmp, &pnt1);
|
|
54 samples += *pnt;
|
|
55
|
|
56 for (i = 0; i < 32; i++) {
|
|
57 *samples++ = (*tmp1 >> 8) ^ 128;
|
|
58 tmp1 += 2;
|
|
59 }
|
|
60 *pnt += 32;
|
|
61
|
|
62 return ret;
|
|
63 }
|
|
64
|
|
65 #if 0
|
|
66 int
|
|
67 mpg123_synth_1to1_8bit_mono2stereo(real * bandPtr,
|
|
68 unsigned char *samples, int *pnt)
|
|
69 {
|
|
70 short samples_tmp[64];
|
|
71 short *tmp1 = samples_tmp;
|
|
72 int i, ret;
|
|
73 int pnt1 = 0;
|
|
74
|
|
75 ret = mpg123_synth_1to1(bandPtr, 0, (unsigned char *) samples_tmp, &pnt1);
|
|
76 samples += *pnt;
|
|
77
|
|
78 for (i = 0; i < 32; i++) {
|
|
79 *samples++ = (*tmp1 >> 8) ^ 128;
|
|
80 *samples++ = (*tmp1 >> 8) ^ 128;
|
|
81 tmp1 += 2;
|
|
82 }
|
|
83 *pnt += 64;
|
|
84
|
|
85 return ret;
|
|
86 }
|
|
87 #endif
|
|
88
|
|
89 int
|
|
90 mpg123_synth_1to1_mono(real * bandPtr, unsigned char *samples, int *pnt)
|
|
91 {
|
|
92 short samples_tmp[64];
|
|
93 short *tmp1 = samples_tmp;
|
|
94 int i, ret;
|
|
95 int pnt1 = 0;
|
|
96
|
|
97 ret = mpg123_synth_1to1(bandPtr, 0, (unsigned char *) samples_tmp, &pnt1);
|
|
98 samples += *pnt;
|
|
99
|
|
100 for (i = 0; i < 32; i++) {
|
|
101 *((short *) samples) = *tmp1;
|
|
102 samples += 2;
|
|
103 tmp1 += 2;
|
|
104 }
|
|
105 *pnt += 64;
|
|
106
|
|
107 return ret;
|
|
108 }
|
|
109
|
|
110 #if 0
|
|
111 int
|
|
112 mpg123_synth_1to1_mono2stereo(real * bandPtr, unsigned char *samples,
|
|
113 int *pnt)
|
|
114 {
|
|
115 int i, ret;
|
|
116
|
|
117 ret = mpg123_synth_1to1(bandPtr, 0, samples, pnt);
|
|
118 samples = samples + *pnt - 128;
|
|
119
|
|
120 for (i = 0; i < 32; i++) {
|
|
121 ((short *) samples)[1] = ((short *) samples)[0];
|
|
122 samples += 4;
|
|
123 }
|
|
124
|
|
125 return ret;
|
|
126 }
|
|
127 #endif
|
|
128
|
|
129 int
|
|
130 mpg123_synth_1to1(real * bandPtr, int channel, unsigned char *out, int *pnt)
|
|
131 {
|
|
132 static real buffs[2][2][0x110];
|
|
133 static const int step = 2;
|
|
134 static int bo = 1;
|
|
135 short *samples = (short *) (out + *pnt);
|
|
136
|
|
137 real *b0, (*buf)[0x110];
|
|
138 int clip = 0;
|
|
139 int bo1;
|
|
140 int i = 0;
|
|
141
|
|
142 /* if(param.equalizer)
|
|
143 do_equalizer(bandPtr,channel); */
|
|
144
|
|
145 if (!channel) {
|
|
146 bo--;
|
|
147 bo &= 0xf;
|
|
148 buf = buffs[0];
|
|
149 }
|
|
150 else {
|
|
151 samples++;
|
|
152 buf = buffs[1];
|
|
153 }
|
|
154
|
|
155 if (bo & 0x1) {
|
|
156 b0 = buf[0];
|
|
157 bo1 = bo;
|
|
158 mpg123_dct64(buf[1] + ((bo + 1) & 0xf), buf[0] + bo, bandPtr);
|
|
159 }
|
|
160 else {
|
|
161 b0 = buf[1];
|
|
162 bo1 = bo + 1;
|
|
163 mpg123_dct64(buf[0] + bo, buf[1] + bo + 1, bandPtr);
|
|
164 }
|
|
165
|
|
166 {
|
|
167 register int j;
|
|
168 real *window = mpg123_decwin + 16 - bo1;
|
|
169
|
|
170 for (j = 16; j; j--, window += 0x10, samples += step) {
|
|
171 real sum;
|
|
172
|
|
173 sum = *window++ * *b0++;
|
|
174 sum -= *window++ * *b0++;
|
|
175 sum += *window++ * *b0++;
|
|
176 sum -= *window++ * *b0++;
|
|
177 sum += *window++ * *b0++;
|
|
178 sum -= *window++ * *b0++;
|
|
179 sum += *window++ * *b0++;
|
|
180 sum -= *window++ * *b0++;
|
|
181 sum += *window++ * *b0++;
|
|
182 sum -= *window++ * *b0++;
|
|
183 sum += *window++ * *b0++;
|
|
184 sum -= *window++ * *b0++;
|
|
185 sum += *window++ * *b0++;
|
|
186 sum -= *window++ * *b0++;
|
|
187 sum += *window++ * *b0++;
|
|
188 sum -= *window++ * *b0++;
|
|
189 i++;
|
|
190 WRITE_SAMPLE(samples, sum, clip);
|
|
191 }
|
|
192
|
|
193 {
|
|
194 real sum;
|
|
195
|
|
196 sum = window[0x0] * b0[0x0];
|
|
197 sum += window[0x2] * b0[0x2];
|
|
198 sum += window[0x4] * b0[0x4];
|
|
199 sum += window[0x6] * b0[0x6];
|
|
200 sum += window[0x8] * b0[0x8];
|
|
201 sum += window[0xA] * b0[0xA];
|
|
202 sum += window[0xC] * b0[0xC];
|
|
203 sum += window[0xE] * b0[0xE];
|
|
204 WRITE_SAMPLE(samples, sum, clip);
|
|
205
|
|
206 b0 -= 0x10, window -= 0x20, samples += step;
|
|
207 }
|
|
208 window += bo1 << 1;
|
|
209
|
|
210 for (j = 15; j; j--, b0 -= 0x20, window -= 0x10, samples += step) {
|
|
211 real sum;
|
|
212
|
|
213 sum = -*(--window) * *b0++;
|
|
214 sum -= *(--window) * *b0++;
|
|
215 sum -= *(--window) * *b0++;
|
|
216 sum -= *(--window) * *b0++;
|
|
217 sum -= *(--window) * *b0++;
|
|
218 sum -= *(--window) * *b0++;
|
|
219 sum -= *(--window) * *b0++;
|
|
220 sum -= *(--window) * *b0++;
|
|
221 sum -= *(--window) * *b0++;
|
|
222 sum -= *(--window) * *b0++;
|
|
223 sum -= *(--window) * *b0++;
|
|
224 sum -= *(--window) * *b0++;
|
|
225 sum -= *(--window) * *b0++;
|
|
226 sum -= *(--window) * *b0++;
|
|
227 sum -= *(--window) * *b0++;
|
|
228 sum -= *(--window) * *b0++;
|
|
229 WRITE_SAMPLE(samples, sum, clip);
|
|
230 }
|
|
231 }
|
|
232
|
|
233 *pnt += 128;
|
|
234
|
|
235 return clip;
|
|
236 }
|