allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / sound / core / oss / mulaw.c
blob2eb18807e6d04662f9d85bed3355d8ad2cd5b5e6
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>
6 * Based on reference implementation by Sun Microsystems, Inc.
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.
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.
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
24 #include <sound/driver.h>
26 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
28 #include <linux/time.h>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include "pcm_plugin.h"
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. */
39 static inline int val_seg(int val)
41 int r = 0;
42 val >>= 7;
43 if (val & 0xf0) {
44 val >>= 4;
45 r += 4;
47 if (val & 0x0c) {
48 val >>= 2;
49 r += 2;
51 if (val & 0x02)
52 r += 1;
53 return r;
56 #define BIAS (0x84) /* Bias for linear code. */
59 * linear2ulaw() - Convert a linear PCM value to u-law
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:
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
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.
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.
84 * For further information see John C. Bellamy's Digital Telephony, 1982,
85 * John Wiley & Sons, pps 98-111 and 472-476.
87 static unsigned char linear2ulaw(int pcm_val) /* 2's complement (16-bit range) */
89 int mask;
90 int seg;
91 unsigned char uval;
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;
101 if (pcm_val > 0x7FFF)
102 pcm_val = 0x7FFF;
104 /* Convert the scaled magnitude to segment number. */
105 seg = val_seg(pcm_val);
108 * Combine the sign, segment, quantization bits;
109 * and complement the code word.
111 uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF);
112 return uval ^ mask;
116 * ulaw2linear() - Convert a u-law value to 16-bit linear PCM
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.
121 * Note that this function expects to be passed the complement of the
122 * original code word. This is in keeping with ISDN conventions.
124 static int ulaw2linear(unsigned char u_val)
126 int t;
128 /* Complement to obtain normal u-law value. */
129 u_val = ~u_val;
132 * Extract and bias the quantization bits. Then
133 * shift up by the segment number and subtract out the bias.
135 t = ((u_val & QUANT_MASK) << 3) + BIAS;
136 t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
138 return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
142 * Basic Mu-Law plugin
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,
148 snd_pcm_uframes_t frames);
150 struct mulaw_priv {
151 mulaw_f func;
152 int conv;
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,
158 snd_pcm_uframes_t frames)
160 #define PUT_S16_LABELS
161 #include "plugin_ops.h"
162 #undef PUT_S16_LABELS
163 struct mulaw_priv *data = (struct mulaw_priv *)plugin->extra_data;
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;
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;
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,
200 snd_pcm_uframes_t frames)
202 #define GET_S16_LABELS
203 #include "plugin_ops.h"
204 #undef GET_S16_LABELS
205 struct mulaw_priv *data = (struct mulaw_priv *)plugin->extra_data;
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;
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;
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,
243 snd_pcm_uframes_t frames)
245 struct mulaw_priv *data;
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
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);
262 #endif
263 data = (struct mulaw_priv *)plugin->extra_data;
264 data->func(plugin, src_channels, dst_channels, frames);
265 return frames;
268 static int getput_index(int format)
270 int sign, width, endian;
271 sign = !snd_pcm_format_signed(format);
272 width = snd_pcm_format_width(format) / 8 - 1;
273 if (width < 0 || width > 3) {
274 snd_printk(KERN_ERR "snd-pcm-oss: invalid format %d\n", format);
275 width = 0;
277 #ifdef SNDRV_LITTLE_ENDIAN
278 endian = snd_pcm_format_big_endian(format);
279 #else
280 endian = snd_pcm_format_little_endian(format);
281 #endif
282 if (endian < 0)
283 endian = 0;
284 return width * 4 + endian * 2 + sign;
287 int snd_pcm_plugin_build_mulaw(struct snd_pcm_substream *plug,
288 struct snd_pcm_plugin_format *src_format,
289 struct snd_pcm_plugin_format *dst_format,
290 struct snd_pcm_plugin **r_plugin)
292 int err;
293 struct mulaw_priv *data;
294 struct snd_pcm_plugin *plugin;
295 struct snd_pcm_plugin_format *format;
296 mulaw_f func;
298 snd_assert(r_plugin != NULL, return -ENXIO);
299 *r_plugin = NULL;
301 snd_assert(src_format->rate == dst_format->rate, return -ENXIO);
302 snd_assert(src_format->channels == dst_format->channels, return -ENXIO);
304 if (dst_format->format == SNDRV_PCM_FORMAT_MU_LAW) {
305 format = src_format;
306 func = mulaw_encode;
308 else if (src_format->format == SNDRV_PCM_FORMAT_MU_LAW) {
309 format = dst_format;
310 func = mulaw_decode;
312 else {
313 snd_BUG();
314 return -EINVAL;
316 snd_assert(snd_pcm_format_linear(format->format) != 0, return -ENXIO);
318 err = snd_pcm_plugin_build(plug, "Mu-Law<->linear conversion",
319 src_format, dst_format,
320 sizeof(struct mulaw_priv), &plugin);
321 if (err < 0)
322 return err;
323 data = (struct mulaw_priv *)plugin->extra_data;
324 data->func = func;
325 data->conv = getput_index(format->format);
326 snd_assert(data->conv >= 0 && data->conv < 4*2*2, return -EINVAL);
327 plugin->transfer = mulaw_transfer;
328 *r_plugin = plugin;
329 return 0;
332 #endif