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>
25 #include <linux/time.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include "pcm_plugin.h"
30 #define SIGN_BIT (0x80) /* Sign bit for a u-law byte. */
31 #define QUANT_MASK (0xf) /* Quantization field mask. */
32 #define NSEGS (8) /* Number of u-law segments. */
33 #define SEG_SHIFT (4) /* Left shift for segment number. */
34 #define SEG_MASK (0x70) /* Segment field mask. */
36 static inline int val_seg(int val)
53 #define BIAS (0x84) /* Bias for linear code. */
56 * linear2ulaw() - Convert a linear PCM value to u-law
58 * In order to simplify the encoding process, the original linear magnitude
59 * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
60 * (33 - 8191). The result can be seen in the following encoding table:
62 * Biased Linear Input Code Compressed Code
63 * ------------------------ ---------------
64 * 00000001wxyza 000wxyz
65 * 0000001wxyzab 001wxyz
66 * 000001wxyzabc 010wxyz
67 * 00001wxyzabcd 011wxyz
68 * 0001wxyzabcde 100wxyz
69 * 001wxyzabcdef 101wxyz
70 * 01wxyzabcdefg 110wxyz
71 * 1wxyzabcdefgh 111wxyz
73 * Each biased linear code has a leading 1 which identifies the segment
74 * number. The value of the segment number is equal to 7 minus the number
75 * of leading 0's. The quantization interval is directly available as the
76 * four bits wxyz. * The trailing bits (a - h) are ignored.
78 * Ordinarily the complement of the resulting code word is used for
79 * transmission, and so the code word is complemented before it is returned.
81 * For further information see John C. Bellamy's Digital Telephony, 1982,
82 * John Wiley & Sons, pps 98-111 and 472-476.
84 static unsigned char linear2ulaw(int pcm_val) /* 2's complement (16-bit range) */
90 /* Get the sign and the magnitude of the value. */
92 pcm_val = BIAS - pcm_val;
101 /* Convert the scaled magnitude to segment number. */
102 seg = val_seg(pcm_val);
105 * Combine the sign, segment, quantization bits;
106 * and complement the code word.
108 uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF);
113 * ulaw2linear() - Convert a u-law value to 16-bit linear PCM
115 * First, a biased linear code is derived from the code word. An unbiased
116 * output can then be obtained by subtracting 33 from the biased code.
118 * Note that this function expects to be passed the complement of the
119 * original code word. This is in keeping with ISDN conventions.
121 static int ulaw2linear(unsigned char u_val)
125 /* Complement to obtain normal u-law value. */
129 * Extract and bias the quantization bits. Then
130 * shift up by the segment number and subtract out the bias.
132 t = ((u_val & QUANT_MASK) << 3) + BIAS;
133 t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
135 return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
139 * Basic Mu-Law plugin
142 typedef void (*mulaw_f)(snd_pcm_plugin_t *plugin,
143 const snd_pcm_plugin_channel_t *src_channels,
144 snd_pcm_plugin_channel_t *dst_channels,
145 snd_pcm_uframes_t frames);
147 typedef struct mulaw_private_data {
152 static void mulaw_decode(snd_pcm_plugin_t *plugin,
153 const snd_pcm_plugin_channel_t *src_channels,
154 snd_pcm_plugin_channel_t *dst_channels,
155 snd_pcm_uframes_t frames)
157 #define PUT_S16_LABELS
158 #include "plugin_ops.h"
159 #undef PUT_S16_LABELS
160 mulaw_t *data = (mulaw_t *)plugin->extra_data;
161 void *put = put_s16_labels[data->conv];
163 int nchannels = plugin->src_format.channels;
164 for (channel = 0; channel < nchannels; ++channel) {
167 int src_step, dst_step;
168 snd_pcm_uframes_t frames1;
169 if (!src_channels[channel].enabled) {
170 if (dst_channels[channel].wanted)
171 snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format);
172 dst_channels[channel].enabled = 0;
175 dst_channels[channel].enabled = 1;
176 src = src_channels[channel].area.addr + src_channels[channel].area.first / 8;
177 dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
178 src_step = src_channels[channel].area.step / 8;
179 dst_step = dst_channels[channel].area.step / 8;
181 while (frames1-- > 0) {
182 signed short sample = ulaw2linear(*src);
184 #define PUT_S16_END after
185 #include "plugin_ops.h"
194 static void mulaw_encode(snd_pcm_plugin_t *plugin,
195 const snd_pcm_plugin_channel_t *src_channels,
196 snd_pcm_plugin_channel_t *dst_channels,
197 snd_pcm_uframes_t frames)
199 #define GET_S16_LABELS
200 #include "plugin_ops.h"
201 #undef GET_S16_LABELS
202 mulaw_t *data = (mulaw_t *)plugin->extra_data;
203 void *get = get_s16_labels[data->conv];
205 int nchannels = plugin->src_format.channels;
206 signed short sample = 0;
207 for (channel = 0; channel < nchannels; ++channel) {
210 int src_step, dst_step;
211 snd_pcm_uframes_t frames1;
212 if (!src_channels[channel].enabled) {
213 if (dst_channels[channel].wanted)
214 snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format);
215 dst_channels[channel].enabled = 0;
218 dst_channels[channel].enabled = 1;
219 src = src_channels[channel].area.addr + src_channels[channel].area.first / 8;
220 dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
221 src_step = src_channels[channel].area.step / 8;
222 dst_step = dst_channels[channel].area.step / 8;
224 while (frames1-- > 0) {
226 #define GET_S16_END after
227 #include "plugin_ops.h"
230 *dst = linear2ulaw(sample);
237 static snd_pcm_sframes_t mulaw_transfer(snd_pcm_plugin_t *plugin,
238 const snd_pcm_plugin_channel_t *src_channels,
239 snd_pcm_plugin_channel_t *dst_channels,
240 snd_pcm_uframes_t frames)
244 snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO);
247 #ifdef CONFIG_SND_DEBUG
249 unsigned int channel;
250 for (channel = 0; channel < plugin->src_format.channels; channel++) {
251 snd_assert(src_channels[channel].area.first % 8 == 0 &&
252 src_channels[channel].area.step % 8 == 0,
254 snd_assert(dst_channels[channel].area.first % 8 == 0 &&
255 dst_channels[channel].area.step % 8 == 0,
260 data = (mulaw_t *)plugin->extra_data;
261 data->func(plugin, src_channels, dst_channels, frames);
265 int snd_pcm_plugin_build_mulaw(snd_pcm_plug_t *plug,
266 snd_pcm_plugin_format_t *src_format,
267 snd_pcm_plugin_format_t *dst_format,
268 snd_pcm_plugin_t **r_plugin)
272 snd_pcm_plugin_t *plugin;
273 snd_pcm_plugin_format_t *format;
276 snd_assert(r_plugin != NULL, return -ENXIO);
279 snd_assert(src_format->rate == dst_format->rate, return -ENXIO);
280 snd_assert(src_format->channels == dst_format->channels, return -ENXIO);
282 if (dst_format->format == SNDRV_PCM_FORMAT_MU_LAW) {
286 else if (src_format->format == SNDRV_PCM_FORMAT_MU_LAW) {
294 snd_assert(snd_pcm_format_linear(format->format) != 0, return -ENXIO);
296 err = snd_pcm_plugin_build(plug, "Mu-Law<->linear conversion",
297 src_format, dst_format,
298 sizeof(mulaw_t), &plugin);
301 data = (mulaw_t*)plugin->extra_data;
303 data->conv = getput_index(format->format);
304 snd_assert(data->conv >= 0 && data->conv < 4*2*2, return -EINVAL);
305 plugin->transfer = mulaw_transfer;