Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
| 25 | #include <linux/time.h> |
| 26 | #include <sound/core.h> |
| 27 | #include <sound/pcm.h> |
| 28 | #include "pcm_plugin.h" |
| 29 | |
| 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. */ |
| 35 | |
| 36 | static inline int val_seg(int val) |
| 37 | { |
| 38 | int r = 0; |
| 39 | val >>= 7; |
| 40 | if (val & 0xf0) { |
| 41 | val >>= 4; |
| 42 | r += 4; |
| 43 | } |
| 44 | if (val & 0x0c) { |
| 45 | val >>= 2; |
| 46 | r += 2; |
| 47 | } |
| 48 | if (val & 0x02) |
| 49 | r += 1; |
| 50 | return r; |
| 51 | } |
| 52 | |
| 53 | #define BIAS (0x84) /* Bias for linear code. */ |
| 54 | |
| 55 | /* |
| 56 | * linear2ulaw() - Convert a linear PCM value to u-law |
| 57 | * |
| 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: |
| 61 | * |
| 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 |
| 72 | * |
| 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. |
| 77 | * |
| 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. |
| 80 | * |
| 81 | * For further information see John C. Bellamy's Digital Telephony, 1982, |
| 82 | * John Wiley & Sons, pps 98-111 and 472-476. |
| 83 | */ |
| 84 | static unsigned char linear2ulaw(int pcm_val) /* 2's complement (16-bit range) */ |
| 85 | { |
| 86 | int mask; |
| 87 | int seg; |
| 88 | unsigned char uval; |
| 89 | |
| 90 | /* Get the sign and the magnitude of the value. */ |
| 91 | if (pcm_val < 0) { |
| 92 | pcm_val = BIAS - pcm_val; |
| 93 | mask = 0x7F; |
| 94 | } else { |
| 95 | pcm_val += BIAS; |
| 96 | mask = 0xFF; |
| 97 | } |
| 98 | if (pcm_val > 0x7FFF) |
| 99 | pcm_val = 0x7FFF; |
| 100 | |
| 101 | /* Convert the scaled magnitude to segment number. */ |
| 102 | seg = val_seg(pcm_val); |
| 103 | |
| 104 | /* |
| 105 | * Combine the sign, segment, quantization bits; |
| 106 | * and complement the code word. |
| 107 | */ |
| 108 | uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF); |
| 109 | return uval ^ mask; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * ulaw2linear() - Convert a u-law value to 16-bit linear PCM |
| 114 | * |
| 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. |
| 117 | * |
| 118 | * Note that this function expects to be passed the complement of the |
| 119 | * original code word. This is in keeping with ISDN conventions. |
| 120 | */ |
| 121 | static int ulaw2linear(unsigned char u_val) |
| 122 | { |
| 123 | int t; |
| 124 | |
| 125 | /* Complement to obtain normal u-law value. */ |
| 126 | u_val = ~u_val; |
| 127 | |
| 128 | /* |
| 129 | * Extract and bias the quantization bits. Then |
| 130 | * shift up by the segment number and subtract out the bias. |
| 131 | */ |
| 132 | t = ((u_val & QUANT_MASK) << 3) + BIAS; |
| 133 | t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT; |
| 134 | |
| 135 | return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS)); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Basic Mu-Law plugin |
| 140 | */ |
| 141 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | snd_pcm_uframes_t frames); |
| 146 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 147 | struct mulaw_priv { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | mulaw_f func; |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame^] | 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 */ |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 155 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame^] | 157 | static inline void cvt_s16_to_native(struct mulaw_priv *data, |
| 158 | unsigned char *dst, u16 sample) |
| 159 | { |
| 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, |
| 166 | data->copy_bytes); |
| 167 | } |
| 168 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | snd_pcm_uframes_t frames) |
| 173 | { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 174 | struct mulaw_priv *data = (struct mulaw_priv *)plugin->extra_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | int channel; |
| 176 | int nchannels = plugin->src_format.channels; |
| 177 | for (channel = 0; channel < nchannels; ++channel) { |
| 178 | char *src; |
| 179 | char *dst; |
| 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; |
| 186 | continue; |
| 187 | } |
| 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; |
| 193 | frames1 = frames; |
| 194 | while (frames1-- > 0) { |
| 195 | signed short sample = ulaw2linear(*src); |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame^] | 196 | cvt_s16_to_native(data, dst, sample); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | src += src_step; |
| 198 | dst += dst_step; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame^] | 203 | static inline signed short cvt_native_to_s16(struct mulaw_priv *data, |
| 204 | unsigned char *src) |
| 205 | { |
| 206 | u16 sample = 0; |
| 207 | memcpy((char *)&sample + data->copy_ofs, src + data->native_ofs, |
| 208 | data->copy_bytes); |
| 209 | if (data->cvt_endian) |
| 210 | sample = swab16(sample); |
| 211 | sample ^= data->flip; |
| 212 | return (signed short)sample; |
| 213 | } |
| 214 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | snd_pcm_uframes_t frames) |
| 219 | { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 220 | struct mulaw_priv *data = (struct mulaw_priv *)plugin->extra_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | int channel; |
| 222 | int nchannels = plugin->src_format.channels; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | for (channel = 0; channel < nchannels; ++channel) { |
| 224 | char *src; |
| 225 | char *dst; |
| 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; |
| 232 | continue; |
| 233 | } |
| 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; |
| 239 | frames1 = frames; |
| 240 | while (frames1-- > 0) { |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame^] | 241 | signed short sample = cvt_native_to_s16(data, src); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | *dst = linear2ulaw(sample); |
| 243 | src += src_step; |
| 244 | dst += dst_step; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | snd_pcm_uframes_t frames) |
| 253 | { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 254 | struct mulaw_priv *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
| 256 | snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO); |
| 257 | if (frames == 0) |
| 258 | return 0; |
| 259 | #ifdef CONFIG_SND_DEBUG |
| 260 | { |
| 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, |
| 265 | return -ENXIO); |
| 266 | snd_assert(dst_channels[channel].area.first % 8 == 0 && |
| 267 | dst_channels[channel].area.step % 8 == 0, |
| 268 | return -ENXIO); |
| 269 | } |
| 270 | } |
| 271 | #endif |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 272 | data = (struct mulaw_priv *)plugin->extra_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | data->func(plugin, src_channels, dst_channels, frames); |
| 274 | return frames; |
| 275 | } |
| 276 | |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame^] | 277 | static void init_data(struct mulaw_priv *data, int format) |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 278 | { |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 279 | #ifdef SNDRV_LITTLE_ENDIAN |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame^] | 280 | data->cvt_endian = snd_pcm_format_big_endian(format) > 0; |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 281 | #else |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame^] | 282 | data->cvt_endian = snd_pcm_format_little_endian(format) > 0; |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 283 | #endif |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame^] | 284 | if (!snd_pcm_format_signed(format)) |
| 285 | data->flip = 0x8000; |
| 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; |
| 291 | } else { |
| 292 | /* S24 in 4bytes need an 1 byte offset */ |
| 293 | data->native_ofs = data->native_bytes - |
| 294 | snd_pcm_format_width(format) / 8; |
| 295 | } |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 296 | } |
| 297 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
| 303 | int err; |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 304 | struct mulaw_priv *data; |
| 305 | struct snd_pcm_plugin *plugin; |
| 306 | struct snd_pcm_plugin_format *format; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | mulaw_f func; |
| 308 | |
| 309 | snd_assert(r_plugin != NULL, return -ENXIO); |
| 310 | *r_plugin = NULL; |
| 311 | |
| 312 | snd_assert(src_format->rate == dst_format->rate, return -ENXIO); |
| 313 | snd_assert(src_format->channels == dst_format->channels, return -ENXIO); |
| 314 | |
| 315 | if (dst_format->format == SNDRV_PCM_FORMAT_MU_LAW) { |
| 316 | format = src_format; |
| 317 | func = mulaw_encode; |
| 318 | } |
| 319 | else if (src_format->format == SNDRV_PCM_FORMAT_MU_LAW) { |
| 320 | format = dst_format; |
| 321 | func = mulaw_decode; |
| 322 | } |
| 323 | else { |
| 324 | snd_BUG(); |
| 325 | return -EINVAL; |
| 326 | } |
| 327 | snd_assert(snd_pcm_format_linear(format->format) != 0, return -ENXIO); |
| 328 | |
| 329 | err = snd_pcm_plugin_build(plug, "Mu-Law<->linear conversion", |
| 330 | src_format, dst_format, |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 331 | sizeof(struct mulaw_priv), &plugin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | if (err < 0) |
| 333 | return err; |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 334 | data = (struct mulaw_priv *)plugin->extra_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | data->func = func; |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame^] | 336 | init_data(data, format->format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | plugin->transfer = mulaw_transfer; |
| 338 | *r_plugin = plugin; |
| 339 | return 0; |
| 340 | } |