2 * Mu-Law conversion Plug-In Interface
3 * Copyright (c) 1999 by Jaroslav Kysela <perex@perex.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
)(struct snd_pcm_plugin
*plugin
,
143 const struct snd_pcm_plugin_channel
*src_channels
,
144 struct snd_pcm_plugin_channel
*dst_channels
,
145 snd_pcm_uframes_t frames
);
149 int cvt_endian
; /* need endian conversion? */
150 unsigned int native_ofs
; /* byte offset in native format */
151 unsigned int copy_ofs
; /* byte offset in s16 format */
152 unsigned int native_bytes
; /* byte size of the native format */
153 unsigned int copy_bytes
; /* bytes to copy per conversion */
154 u16 flip
; /* MSB flip for signedness, done after endian conversion */
157 static inline void cvt_s16_to_native(struct mulaw_priv
*data
,
158 unsigned char *dst
, u16 sample
)
160 sample
^= data
->flip
;
161 if (data
->cvt_endian
)
162 sample
= swab16(sample
);
163 if (data
->native_bytes
> data
->copy_bytes
)
164 memset(dst
, 0, data
->native_bytes
);
165 memcpy(dst
+ data
->native_ofs
, (char *)&sample
+ data
->copy_ofs
,
169 static void mulaw_decode(struct snd_pcm_plugin
*plugin
,
170 const struct snd_pcm_plugin_channel
*src_channels
,
171 struct snd_pcm_plugin_channel
*dst_channels
,
172 snd_pcm_uframes_t frames
)
174 struct mulaw_priv
*data
= (struct mulaw_priv
*)plugin
->extra_data
;
176 int nchannels
= plugin
->src_format
.channels
;
177 for (channel
= 0; channel
< nchannels
; ++channel
) {
180 int src_step
, dst_step
;
181 snd_pcm_uframes_t frames1
;
182 if (!src_channels
[channel
].enabled
) {
183 if (dst_channels
[channel
].wanted
)
184 snd_pcm_area_silence(&dst_channels
[channel
].area
, 0, frames
, plugin
->dst_format
.format
);
185 dst_channels
[channel
].enabled
= 0;
188 dst_channels
[channel
].enabled
= 1;
189 src
= src_channels
[channel
].area
.addr
+ src_channels
[channel
].area
.first
/ 8;
190 dst
= dst_channels
[channel
].area
.addr
+ dst_channels
[channel
].area
.first
/ 8;
191 src_step
= src_channels
[channel
].area
.step
/ 8;
192 dst_step
= dst_channels
[channel
].area
.step
/ 8;
194 while (frames1
-- > 0) {
195 signed short sample
= ulaw2linear(*src
);
196 cvt_s16_to_native(data
, dst
, sample
);
203 static inline signed short cvt_native_to_s16(struct mulaw_priv
*data
,
207 memcpy((char *)&sample
+ data
->copy_ofs
, src
+ data
->native_ofs
,
209 if (data
->cvt_endian
)
210 sample
= swab16(sample
);
211 sample
^= data
->flip
;
212 return (signed short)sample
;
215 static void mulaw_encode(struct snd_pcm_plugin
*plugin
,
216 const struct snd_pcm_plugin_channel
*src_channels
,
217 struct snd_pcm_plugin_channel
*dst_channels
,
218 snd_pcm_uframes_t frames
)
220 struct mulaw_priv
*data
= (struct mulaw_priv
*)plugin
->extra_data
;
222 int nchannels
= plugin
->src_format
.channels
;
223 for (channel
= 0; channel
< nchannels
; ++channel
) {
226 int src_step
, dst_step
;
227 snd_pcm_uframes_t frames1
;
228 if (!src_channels
[channel
].enabled
) {
229 if (dst_channels
[channel
].wanted
)
230 snd_pcm_area_silence(&dst_channels
[channel
].area
, 0, frames
, plugin
->dst_format
.format
);
231 dst_channels
[channel
].enabled
= 0;
234 dst_channels
[channel
].enabled
= 1;
235 src
= src_channels
[channel
].area
.addr
+ src_channels
[channel
].area
.first
/ 8;
236 dst
= dst_channels
[channel
].area
.addr
+ dst_channels
[channel
].area
.first
/ 8;
237 src_step
= src_channels
[channel
].area
.step
/ 8;
238 dst_step
= dst_channels
[channel
].area
.step
/ 8;
240 while (frames1
-- > 0) {
241 signed short sample
= cvt_native_to_s16(data
, src
);
242 *dst
= linear2ulaw(sample
);
249 static snd_pcm_sframes_t
mulaw_transfer(struct snd_pcm_plugin
*plugin
,
250 const struct snd_pcm_plugin_channel
*src_channels
,
251 struct snd_pcm_plugin_channel
*dst_channels
,
252 snd_pcm_uframes_t frames
)
254 struct mulaw_priv
*data
;
256 snd_assert(plugin
!= NULL
&& src_channels
!= NULL
&& dst_channels
!= NULL
, return -ENXIO
);
259 #ifdef CONFIG_SND_DEBUG
261 unsigned int channel
;
262 for (channel
= 0; channel
< plugin
->src_format
.channels
; channel
++) {
263 snd_assert(src_channels
[channel
].area
.first
% 8 == 0 &&
264 src_channels
[channel
].area
.step
% 8 == 0,
266 snd_assert(dst_channels
[channel
].area
.first
% 8 == 0 &&
267 dst_channels
[channel
].area
.step
% 8 == 0,
272 data
= (struct mulaw_priv
*)plugin
->extra_data
;
273 data
->func(plugin
, src_channels
, dst_channels
, frames
);
277 static void init_data(struct mulaw_priv
*data
, int format
)
279 #ifdef SNDRV_LITTLE_ENDIAN
280 data
->cvt_endian
= snd_pcm_format_big_endian(format
) > 0;
282 data
->cvt_endian
= snd_pcm_format_little_endian(format
) > 0;
284 if (!snd_pcm_format_signed(format
))
286 data
->native_bytes
= snd_pcm_format_physical_width(format
) / 8;
287 data
->copy_bytes
= data
->native_bytes
< 2 ? 1 : 2;
288 if (snd_pcm_format_little_endian(format
)) {
289 data
->native_ofs
= data
->native_bytes
- data
->copy_bytes
;
290 data
->copy_ofs
= 2 - data
->copy_bytes
;
292 /* S24 in 4bytes need an 1 byte offset */
293 data
->native_ofs
= data
->native_bytes
-
294 snd_pcm_format_width(format
) / 8;
298 int snd_pcm_plugin_build_mulaw(struct snd_pcm_substream
*plug
,
299 struct snd_pcm_plugin_format
*src_format
,
300 struct snd_pcm_plugin_format
*dst_format
,
301 struct snd_pcm_plugin
**r_plugin
)
304 struct mulaw_priv
*data
;
305 struct snd_pcm_plugin
*plugin
;
306 struct snd_pcm_plugin_format
*format
;
309 snd_assert(r_plugin
!= NULL
, return -ENXIO
);
312 snd_assert(src_format
->rate
== dst_format
->rate
, return -ENXIO
);
313 snd_assert(src_format
->channels
== dst_format
->channels
, return -ENXIO
);
315 if (dst_format
->format
== SNDRV_PCM_FORMAT_MU_LAW
) {
319 else if (src_format
->format
== SNDRV_PCM_FORMAT_MU_LAW
) {
327 snd_assert(snd_pcm_format_linear(format
->format
) != 0, return -ENXIO
);
329 err
= snd_pcm_plugin_build(plug
, "Mu-Law<->linear conversion",
330 src_format
, dst_format
,
331 sizeof(struct mulaw_priv
), &plugin
);
334 data
= (struct mulaw_priv
*)plugin
->extra_data
;
336 init_data(data
, format
->format
);
337 plugin
->transfer
= mulaw_transfer
;