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)
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) */
93 /* Get the sign and the magnitude of the value. */
95 pcm_val = BIAS - pcm_val;
101 if (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);
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)
128 /* Complement to obtain normal u-law value. */
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);
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];
166 int nchannels = plugin->src_format.channels;
167 for (channel = 0; channel < nchannels; ++channel) {
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;
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;
184 while (frames1-- > 0) {
185 signed short sample = ulaw2linear(*src);
187 #define PUT_S16_END after
188 #include "plugin_ops.h"
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];
208 int nchannels = plugin->src_format.channels;
209 signed short sample = 0;
210 for (channel = 0; channel < nchannels; ++channel) {
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;
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;
227 while (frames1-- > 0) {
229 #define GET_S16_END after
230 #include "plugin_ops.h"
233 *dst = linear2ulaw(sample);
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);
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,
257 snd_assert(dst_channels[channel].area.first % 8 == 0 &&
258 dst_channels[channel].area.step % 8 == 0,
263 data = (struct mulaw_priv *)plugin->extra_data;
264 data->func(plugin, src_channels, dst_channels, 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);
277 #ifdef SNDRV_LITTLE_ENDIAN
278 endian = snd_pcm_format_big_endian(format);
280 endian = snd_pcm_format_little_endian(format);
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)
293 struct mulaw_priv *data;
294 struct snd_pcm_plugin *plugin;
295 struct snd_pcm_plugin_format *format;
298 snd_assert(r_plugin != NULL, return -ENXIO);
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) {
308 else if (src_format->format == SNDRV_PCM_FORMAT_MU_LAW) {
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);
323 data = (struct mulaw_priv *)plugin->extra_data;
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;