mulaw.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Mu-Law conversion Plug-In Interface
  3. * Copyright (c) 1999 by Jaroslav Kysela <perex@perex.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. #include <sound/driver.h>
  24. #include <linux/time.h>
  25. #include <sound/core.h>
  26. #include <sound/pcm.h>
  27. #include "pcm_plugin.h"
  28. #define SIGN_BIT (0x80) /* Sign bit for a u-law byte. */
  29. #define QUANT_MASK (0xf) /* Quantization field mask. */
  30. #define NSEGS (8) /* Number of u-law segments. */
  31. #define SEG_SHIFT (4) /* Left shift for segment number. */
  32. #define SEG_MASK (0x70) /* Segment field mask. */
  33. static inline int val_seg(int val)
  34. {
  35. int r = 0;
  36. val >>= 7;
  37. if (val & 0xf0) {
  38. val >>= 4;
  39. r += 4;
  40. }
  41. if (val & 0x0c) {
  42. val >>= 2;
  43. r += 2;
  44. }
  45. if (val & 0x02)
  46. r += 1;
  47. return r;
  48. }
  49. #define BIAS (0x84) /* Bias for linear code. */
  50. /*
  51. * linear2ulaw() - Convert a linear PCM value to u-law
  52. *
  53. * In order to simplify the encoding process, the original linear magnitude
  54. * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
  55. * (33 - 8191). The result can be seen in the following encoding table:
  56. *
  57. * Biased Linear Input Code Compressed Code
  58. * ------------------------ ---------------
  59. * 00000001wxyza 000wxyz
  60. * 0000001wxyzab 001wxyz
  61. * 000001wxyzabc 010wxyz
  62. * 00001wxyzabcd 011wxyz
  63. * 0001wxyzabcde 100wxyz
  64. * 001wxyzabcdef 101wxyz
  65. * 01wxyzabcdefg 110wxyz
  66. * 1wxyzabcdefgh 111wxyz
  67. *
  68. * Each biased linear code has a leading 1 which identifies the segment
  69. * number. The value of the segment number is equal to 7 minus the number
  70. * of leading 0's. The quantization interval is directly available as the
  71. * four bits wxyz. * The trailing bits (a - h) are ignored.
  72. *
  73. * Ordinarily the complement of the resulting code word is used for
  74. * transmission, and so the code word is complemented before it is returned.
  75. *
  76. * For further information see John C. Bellamy's Digital Telephony, 1982,
  77. * John Wiley & Sons, pps 98-111 and 472-476.
  78. */
  79. static unsigned char linear2ulaw(int pcm_val) /* 2's complement (16-bit range) */
  80. {
  81. int mask;
  82. int seg;
  83. unsigned char uval;
  84. /* Get the sign and the magnitude of the value. */
  85. if (pcm_val < 0) {
  86. pcm_val = BIAS - pcm_val;
  87. mask = 0x7F;
  88. } else {
  89. pcm_val += BIAS;
  90. mask = 0xFF;
  91. }
  92. if (pcm_val > 0x7FFF)
  93. pcm_val = 0x7FFF;
  94. /* Convert the scaled magnitude to segment number. */
  95. seg = val_seg(pcm_val);
  96. /*
  97. * Combine the sign, segment, quantization bits;
  98. * and complement the code word.
  99. */
  100. uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF);
  101. return uval ^ mask;
  102. }
  103. /*
  104. * ulaw2linear() - Convert a u-law value to 16-bit linear PCM
  105. *
  106. * First, a biased linear code is derived from the code word. An unbiased
  107. * output can then be obtained by subtracting 33 from the biased code.
  108. *
  109. * Note that this function expects to be passed the complement of the
  110. * original code word. This is in keeping with ISDN conventions.
  111. */
  112. static int ulaw2linear(unsigned char u_val)
  113. {
  114. int t;
  115. /* Complement to obtain normal u-law value. */
  116. u_val = ~u_val;
  117. /*
  118. * Extract and bias the quantization bits. Then
  119. * shift up by the segment number and subtract out the bias.
  120. */
  121. t = ((u_val & QUANT_MASK) << 3) + BIAS;
  122. t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
  123. return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
  124. }
  125. /*
  126. * Basic Mu-Law plugin
  127. */
  128. typedef void (*mulaw_f)(struct snd_pcm_plugin *plugin,
  129. const struct snd_pcm_plugin_channel *src_channels,
  130. struct snd_pcm_plugin_channel *dst_channels,
  131. snd_pcm_uframes_t frames);
  132. struct mulaw_priv {
  133. mulaw_f func;
  134. int cvt_endian; /* need endian conversion? */
  135. unsigned int native_ofs; /* byte offset in native format */
  136. unsigned int copy_ofs; /* byte offset in s16 format */
  137. unsigned int native_bytes; /* byte size of the native format */
  138. unsigned int copy_bytes; /* bytes to copy per conversion */
  139. u16 flip; /* MSB flip for signedness, done after endian conversion */
  140. };
  141. static inline void cvt_s16_to_native(struct mulaw_priv *data,
  142. unsigned char *dst, u16 sample)
  143. {
  144. sample ^= data->flip;
  145. if (data->cvt_endian)
  146. sample = swab16(sample);
  147. if (data->native_bytes > data->copy_bytes)
  148. memset(dst, 0, data->native_bytes);
  149. memcpy(dst + data->native_ofs, (char *)&sample + data->copy_ofs,
  150. data->copy_bytes);
  151. }
  152. static void mulaw_decode(struct snd_pcm_plugin *plugin,
  153. const struct snd_pcm_plugin_channel *src_channels,
  154. struct snd_pcm_plugin_channel *dst_channels,
  155. snd_pcm_uframes_t frames)
  156. {
  157. struct mulaw_priv *data = (struct mulaw_priv *)plugin->extra_data;
  158. int channel;
  159. int nchannels = plugin->src_format.channels;
  160. for (channel = 0; channel < nchannels; ++channel) {
  161. char *src;
  162. char *dst;
  163. int src_step, dst_step;
  164. snd_pcm_uframes_t frames1;
  165. if (!src_channels[channel].enabled) {
  166. if (dst_channels[channel].wanted)
  167. snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format);
  168. dst_channels[channel].enabled = 0;
  169. continue;
  170. }
  171. dst_channels[channel].enabled = 1;
  172. src = src_channels[channel].area.addr + src_channels[channel].area.first / 8;
  173. dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
  174. src_step = src_channels[channel].area.step / 8;
  175. dst_step = dst_channels[channel].area.step / 8;
  176. frames1 = frames;
  177. while (frames1-- > 0) {
  178. signed short sample = ulaw2linear(*src);
  179. cvt_s16_to_native(data, dst, sample);
  180. src += src_step;
  181. dst += dst_step;
  182. }
  183. }
  184. }
  185. static inline signed short cvt_native_to_s16(struct mulaw_priv *data,
  186. unsigned char *src)
  187. {
  188. u16 sample = 0;
  189. memcpy((char *)&sample + data->copy_ofs, src + data->native_ofs,
  190. data->copy_bytes);
  191. if (data->cvt_endian)
  192. sample = swab16(sample);
  193. sample ^= data->flip;
  194. return (signed short)sample;
  195. }
  196. static void mulaw_encode(struct snd_pcm_plugin *plugin,
  197. const struct snd_pcm_plugin_channel *src_channels,
  198. struct snd_pcm_plugin_channel *dst_channels,
  199. snd_pcm_uframes_t frames)
  200. {
  201. struct mulaw_priv *data = (struct mulaw_priv *)plugin->extra_data;
  202. int channel;
  203. int nchannels = plugin->src_format.channels;
  204. for (channel = 0; channel < nchannels; ++channel) {
  205. char *src;
  206. char *dst;
  207. int src_step, dst_step;
  208. snd_pcm_uframes_t frames1;
  209. if (!src_channels[channel].enabled) {
  210. if (dst_channels[channel].wanted)
  211. snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format);
  212. dst_channels[channel].enabled = 0;
  213. continue;
  214. }
  215. dst_channels[channel].enabled = 1;
  216. src = src_channels[channel].area.addr + src_channels[channel].area.first / 8;
  217. dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
  218. src_step = src_channels[channel].area.step / 8;
  219. dst_step = dst_channels[channel].area.step / 8;
  220. frames1 = frames;
  221. while (frames1-- > 0) {
  222. signed short sample = cvt_native_to_s16(data, src);
  223. *dst = linear2ulaw(sample);
  224. src += src_step;
  225. dst += dst_step;
  226. }
  227. }
  228. }
  229. static snd_pcm_sframes_t mulaw_transfer(struct snd_pcm_plugin *plugin,
  230. const struct snd_pcm_plugin_channel *src_channels,
  231. struct snd_pcm_plugin_channel *dst_channels,
  232. snd_pcm_uframes_t frames)
  233. {
  234. struct mulaw_priv *data;
  235. snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO);
  236. if (frames == 0)
  237. return 0;
  238. #ifdef CONFIG_SND_DEBUG
  239. {
  240. unsigned int channel;
  241. for (channel = 0; channel < plugin->src_format.channels; channel++) {
  242. snd_assert(src_channels[channel].area.first % 8 == 0 &&
  243. src_channels[channel].area.step % 8 == 0,
  244. return -ENXIO);
  245. snd_assert(dst_channels[channel].area.first % 8 == 0 &&
  246. dst_channels[channel].area.step % 8 == 0,
  247. return -ENXIO);
  248. }
  249. }
  250. #endif
  251. data = (struct mulaw_priv *)plugin->extra_data;
  252. data->func(plugin, src_channels, dst_channels, frames);
  253. return frames;
  254. }
  255. static void init_data(struct mulaw_priv *data, int format)
  256. {
  257. #ifdef SNDRV_LITTLE_ENDIAN
  258. data->cvt_endian = snd_pcm_format_big_endian(format) > 0;
  259. #else
  260. data->cvt_endian = snd_pcm_format_little_endian(format) > 0;
  261. #endif
  262. if (!snd_pcm_format_signed(format))
  263. data->flip = 0x8000;
  264. data->native_bytes = snd_pcm_format_physical_width(format) / 8;
  265. data->copy_bytes = data->native_bytes < 2 ? 1 : 2;
  266. if (snd_pcm_format_little_endian(format)) {
  267. data->native_ofs = data->native_bytes - data->copy_bytes;
  268. data->copy_ofs = 2 - data->copy_bytes;
  269. } else {
  270. /* S24 in 4bytes need an 1 byte offset */
  271. data->native_ofs = data->native_bytes -
  272. snd_pcm_format_width(format) / 8;
  273. }
  274. }
  275. int snd_pcm_plugin_build_mulaw(struct snd_pcm_substream *plug,
  276. struct snd_pcm_plugin_format *src_format,
  277. struct snd_pcm_plugin_format *dst_format,
  278. struct snd_pcm_plugin **r_plugin)
  279. {
  280. int err;
  281. struct mulaw_priv *data;
  282. struct snd_pcm_plugin *plugin;
  283. struct snd_pcm_plugin_format *format;
  284. mulaw_f func;
  285. snd_assert(r_plugin != NULL, return -ENXIO);
  286. *r_plugin = NULL;
  287. snd_assert(src_format->rate == dst_format->rate, return -ENXIO);
  288. snd_assert(src_format->channels == dst_format->channels, return -ENXIO);
  289. if (dst_format->format == SNDRV_PCM_FORMAT_MU_LAW) {
  290. format = src_format;
  291. func = mulaw_encode;
  292. }
  293. else if (src_format->format == SNDRV_PCM_FORMAT_MU_LAW) {
  294. format = dst_format;
  295. func = mulaw_decode;
  296. }
  297. else {
  298. snd_BUG();
  299. return -EINVAL;
  300. }
  301. snd_assert(snd_pcm_format_linear(format->format) != 0, return -ENXIO);
  302. err = snd_pcm_plugin_build(plug, "Mu-Law<->linear conversion",
  303. src_format, dst_format,
  304. sizeof(struct mulaw_priv), &plugin);
  305. if (err < 0)
  306. return err;
  307. data = (struct mulaw_priv *)plugin->extra_data;
  308. data->func = func;
  309. init_data(data, format->format);
  310. plugin->transfer = mulaw_transfer;
  311. *r_plugin = plugin;
  312. return 0;
  313. }