mulaw.c 9.5 KB

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