mulaw.c 10 KB

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