Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Mu-Law conversion Plug-In Interface | |
3 | * Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz> | |
4 | * Uros Bizjak <uros@kss-loka.si> | |
5 | * | |
6 | * Based on reference implementation by Sun Microsystems, Inc. | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU Library General Public License as | |
10 | * published by the Free Software Foundation; either version 2 of | |
11 | * the License, or (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU Library General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Library General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | */ | |
23 | ||
24 | #include <sound/driver.h> | |
21a3479a JK |
25 | |
26 | #ifdef CONFIG_SND_PCM_OSS_PLUGINS | |
27 | ||
1da177e4 LT |
28 | #include <linux/time.h> |
29 | #include <sound/core.h> | |
30 | #include <sound/pcm.h> | |
31 | #include "pcm_plugin.h" | |
32 | ||
33 | #define SIGN_BIT (0x80) /* Sign bit for a u-law byte. */ | |
34 | #define QUANT_MASK (0xf) /* Quantization field mask. */ | |
35 | #define NSEGS (8) /* Number of u-law segments. */ | |
36 | #define SEG_SHIFT (4) /* Left shift for segment number. */ | |
37 | #define SEG_MASK (0x70) /* Segment field mask. */ | |
38 | ||
39 | static inline int val_seg(int val) | |
40 | { | |
41 | int r = 0; | |
42 | val >>= 7; | |
43 | if (val & 0xf0) { | |
44 | val >>= 4; | |
45 | r += 4; | |
46 | } | |
47 | if (val & 0x0c) { | |
48 | val >>= 2; | |
49 | r += 2; | |
50 | } | |
51 | if (val & 0x02) | |
52 | r += 1; | |
53 | return r; | |
54 | } | |
55 | ||
56 | #define BIAS (0x84) /* Bias for linear code. */ | |
57 | ||
58 | /* | |
59 | * linear2ulaw() - Convert a linear PCM value to u-law | |
60 | * | |
61 | * In order to simplify the encoding process, the original linear magnitude | |
62 | * is biased by adding 33 which shifts the encoding range from (0 - 8158) to | |
63 | * (33 - 8191). The result can be seen in the following encoding table: | |
64 | * | |
65 | * Biased Linear Input Code Compressed Code | |
66 | * ------------------------ --------------- | |
67 | * 00000001wxyza 000wxyz | |
68 | * 0000001wxyzab 001wxyz | |
69 | * 000001wxyzabc 010wxyz | |
70 | * 00001wxyzabcd 011wxyz | |
71 | * 0001wxyzabcde 100wxyz | |
72 | * 001wxyzabcdef 101wxyz | |
73 | * 01wxyzabcdefg 110wxyz | |
74 | * 1wxyzabcdefgh 111wxyz | |
75 | * | |
76 | * Each biased linear code has a leading 1 which identifies the segment | |
77 | * number. The value of the segment number is equal to 7 minus the number | |
78 | * of leading 0's. The quantization interval is directly available as the | |
79 | * four bits wxyz. * The trailing bits (a - h) are ignored. | |
80 | * | |
81 | * Ordinarily the complement of the resulting code word is used for | |
82 | * transmission, and so the code word is complemented before it is returned. | |
83 | * | |
84 | * For further information see John C. Bellamy's Digital Telephony, 1982, | |
85 | * John Wiley & Sons, pps 98-111 and 472-476. | |
86 | */ | |
87 | static unsigned char linear2ulaw(int pcm_val) /* 2's complement (16-bit range) */ | |
88 | { | |
89 | int mask; | |
90 | int seg; | |
91 | unsigned char uval; | |
92 | ||
93 | /* Get the sign and the magnitude of the value. */ | |
94 | if (pcm_val < 0) { | |
95 | pcm_val = BIAS - pcm_val; | |
96 | mask = 0x7F; | |
97 | } else { | |
98 | pcm_val += BIAS; | |
99 | mask = 0xFF; | |
100 | } | |
101 | if (pcm_val > 0x7FFF) | |
102 | pcm_val = 0x7FFF; | |
103 | ||
104 | /* Convert the scaled magnitude to segment number. */ | |
105 | seg = val_seg(pcm_val); | |
106 | ||
107 | /* | |
108 | * Combine the sign, segment, quantization bits; | |
109 | * and complement the code word. | |
110 | */ | |
111 | uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF); | |
112 | return uval ^ mask; | |
113 | } | |
114 | ||
115 | /* | |
116 | * ulaw2linear() - Convert a u-law value to 16-bit linear PCM | |
117 | * | |
118 | * First, a biased linear code is derived from the code word. An unbiased | |
119 | * output can then be obtained by subtracting 33 from the biased code. | |
120 | * | |
121 | * Note that this function expects to be passed the complement of the | |
122 | * original code word. This is in keeping with ISDN conventions. | |
123 | */ | |
124 | static int ulaw2linear(unsigned char u_val) | |
125 | { | |
126 | int t; | |
127 | ||
128 | /* Complement to obtain normal u-law value. */ | |
129 | u_val = ~u_val; | |
130 | ||
131 | /* | |
132 | * Extract and bias the quantization bits. Then | |
133 | * shift up by the segment number and subtract out the bias. | |
134 | */ | |
135 | t = ((u_val & QUANT_MASK) << 3) + BIAS; | |
136 | t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT; | |
137 | ||
138 | return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS)); | |
139 | } | |
140 | ||
141 | /* | |
142 | * Basic Mu-Law plugin | |
143 | */ | |
144 | ||
6ac77bc1 TI |
145 | typedef void (*mulaw_f)(struct snd_pcm_plugin *plugin, |
146 | const struct snd_pcm_plugin_channel *src_channels, | |
147 | struct snd_pcm_plugin_channel *dst_channels, | |
1da177e4 LT |
148 | snd_pcm_uframes_t frames); |
149 | ||
6ac77bc1 | 150 | struct mulaw_priv { |
1da177e4 LT |
151 | mulaw_f func; |
152 | int conv; | |
6ac77bc1 | 153 | }; |
1da177e4 | 154 | |
6ac77bc1 TI |
155 | static void mulaw_decode(struct snd_pcm_plugin *plugin, |
156 | const struct snd_pcm_plugin_channel *src_channels, | |
157 | struct snd_pcm_plugin_channel *dst_channels, | |
1da177e4 LT |
158 | snd_pcm_uframes_t frames) |
159 | { | |
160 | #define PUT_S16_LABELS | |
161 | #include "plugin_ops.h" | |
162 | #undef PUT_S16_LABELS | |
6ac77bc1 | 163 | struct mulaw_priv *data = (struct mulaw_priv *)plugin->extra_data; |
1da177e4 LT |
164 | void *put = put_s16_labels[data->conv]; |
165 | int channel; | |
166 | int nchannels = plugin->src_format.channels; | |
167 | for (channel = 0; channel < nchannels; ++channel) { | |
168 | char *src; | |
169 | char *dst; | |
170 | int src_step, dst_step; | |
171 | snd_pcm_uframes_t frames1; | |
172 | if (!src_channels[channel].enabled) { | |
173 | if (dst_channels[channel].wanted) | |
174 | snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format); | |
175 | dst_channels[channel].enabled = 0; | |
176 | continue; | |
177 | } | |
178 | dst_channels[channel].enabled = 1; | |
179 | src = src_channels[channel].area.addr + src_channels[channel].area.first / 8; | |
180 | dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8; | |
181 | src_step = src_channels[channel].area.step / 8; | |
182 | dst_step = dst_channels[channel].area.step / 8; | |
183 | frames1 = frames; | |
184 | while (frames1-- > 0) { | |
185 | signed short sample = ulaw2linear(*src); | |
186 | goto *put; | |
187 | #define PUT_S16_END after | |
188 | #include "plugin_ops.h" | |
189 | #undef PUT_S16_END | |
190 | after: | |
191 | src += src_step; | |
192 | dst += dst_step; | |
193 | } | |
194 | } | |
195 | } | |
196 | ||
6ac77bc1 TI |
197 | static void mulaw_encode(struct snd_pcm_plugin *plugin, |
198 | const struct snd_pcm_plugin_channel *src_channels, | |
199 | struct snd_pcm_plugin_channel *dst_channels, | |
1da177e4 LT |
200 | snd_pcm_uframes_t frames) |
201 | { | |
202 | #define GET_S16_LABELS | |
203 | #include "plugin_ops.h" | |
204 | #undef GET_S16_LABELS | |
6ac77bc1 | 205 | struct mulaw_priv *data = (struct mulaw_priv *)plugin->extra_data; |
1da177e4 LT |
206 | void *get = get_s16_labels[data->conv]; |
207 | int channel; | |
208 | int nchannels = plugin->src_format.channels; | |
209 | signed short sample = 0; | |
210 | for (channel = 0; channel < nchannels; ++channel) { | |
211 | char *src; | |
212 | char *dst; | |
213 | int src_step, dst_step; | |
214 | snd_pcm_uframes_t frames1; | |
215 | if (!src_channels[channel].enabled) { | |
216 | if (dst_channels[channel].wanted) | |
217 | snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format); | |
218 | dst_channels[channel].enabled = 0; | |
219 | continue; | |
220 | } | |
221 | dst_channels[channel].enabled = 1; | |
222 | src = src_channels[channel].area.addr + src_channels[channel].area.first / 8; | |
223 | dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8; | |
224 | src_step = src_channels[channel].area.step / 8; | |
225 | dst_step = dst_channels[channel].area.step / 8; | |
226 | frames1 = frames; | |
227 | while (frames1-- > 0) { | |
228 | goto *get; | |
229 | #define GET_S16_END after | |
230 | #include "plugin_ops.h" | |
231 | #undef GET_S16_END | |
232 | after: | |
233 | *dst = linear2ulaw(sample); | |
234 | src += src_step; | |
235 | dst += dst_step; | |
236 | } | |
237 | } | |
238 | } | |
239 | ||
6ac77bc1 TI |
240 | static snd_pcm_sframes_t mulaw_transfer(struct snd_pcm_plugin *plugin, |
241 | const struct snd_pcm_plugin_channel *src_channels, | |
242 | struct snd_pcm_plugin_channel *dst_channels, | |
1da177e4 LT |
243 | snd_pcm_uframes_t frames) |
244 | { | |
6ac77bc1 | 245 | struct mulaw_priv *data; |
1da177e4 LT |
246 | |
247 | snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO); | |
248 | if (frames == 0) | |
249 | return 0; | |
250 | #ifdef CONFIG_SND_DEBUG | |
251 | { | |
252 | unsigned int channel; | |
253 | for (channel = 0; channel < plugin->src_format.channels; channel++) { | |
254 | snd_assert(src_channels[channel].area.first % 8 == 0 && | |
255 | src_channels[channel].area.step % 8 == 0, | |
256 | return -ENXIO); | |
257 | snd_assert(dst_channels[channel].area.first % 8 == 0 && | |
258 | dst_channels[channel].area.step % 8 == 0, | |
259 | return -ENXIO); | |
260 | } | |
261 | } | |
262 | #endif | |
6ac77bc1 | 263 | data = (struct mulaw_priv *)plugin->extra_data; |
1da177e4 LT |
264 | data->func(plugin, src_channels, dst_channels, frames); |
265 | return frames; | |
266 | } | |
267 | ||
6ac77bc1 TI |
268 | int snd_pcm_plugin_build_mulaw(struct snd_pcm_substream *plug, |
269 | struct snd_pcm_plugin_format *src_format, | |
270 | struct snd_pcm_plugin_format *dst_format, | |
271 | struct snd_pcm_plugin **r_plugin) | |
1da177e4 LT |
272 | { |
273 | int err; | |
6ac77bc1 TI |
274 | struct mulaw_priv *data; |
275 | struct snd_pcm_plugin *plugin; | |
276 | struct snd_pcm_plugin_format *format; | |
1da177e4 LT |
277 | mulaw_f func; |
278 | ||
279 | snd_assert(r_plugin != NULL, return -ENXIO); | |
280 | *r_plugin = NULL; | |
281 | ||
282 | snd_assert(src_format->rate == dst_format->rate, return -ENXIO); | |
283 | snd_assert(src_format->channels == dst_format->channels, return -ENXIO); | |
284 | ||
285 | if (dst_format->format == SNDRV_PCM_FORMAT_MU_LAW) { | |
286 | format = src_format; | |
287 | func = mulaw_encode; | |
288 | } | |
289 | else if (src_format->format == SNDRV_PCM_FORMAT_MU_LAW) { | |
290 | format = dst_format; | |
291 | func = mulaw_decode; | |
292 | } | |
293 | else { | |
294 | snd_BUG(); | |
295 | return -EINVAL; | |
296 | } | |
297 | snd_assert(snd_pcm_format_linear(format->format) != 0, return -ENXIO); | |
298 | ||
299 | err = snd_pcm_plugin_build(plug, "Mu-Law<->linear conversion", | |
300 | src_format, dst_format, | |
6ac77bc1 | 301 | sizeof(struct mulaw_priv), &plugin); |
1da177e4 LT |
302 | if (err < 0) |
303 | return err; | |
6ac77bc1 | 304 | data = (struct mulaw_priv *)plugin->extra_data; |
1da177e4 LT |
305 | data->func = func; |
306 | data->conv = getput_index(format->format); | |
307 | snd_assert(data->conv >= 0 && data->conv < 4*2*2, return -EINVAL); | |
308 | plugin->transfer = mulaw_transfer; | |
309 | *r_plugin = plugin; | |
310 | return 0; | |
311 | } | |
21a3479a JK |
312 | |
313 | #endif |