mulaw.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. #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)(snd_pcm_plugin_t *plugin,
  129. const snd_pcm_plugin_channel_t *src_channels,
  130. snd_pcm_plugin_channel_t *dst_channels,
  131. snd_pcm_uframes_t frames);
  132. typedef struct mulaw_private_data {
  133. mulaw_f func;
  134. int conv;
  135. } mulaw_t;
  136. static void mulaw_decode(snd_pcm_plugin_t *plugin,
  137. const snd_pcm_plugin_channel_t *src_channels,
  138. snd_pcm_plugin_channel_t *dst_channels,
  139. snd_pcm_uframes_t frames)
  140. {
  141. #define PUT_S16_LABELS
  142. #include "plugin_ops.h"
  143. #undef PUT_S16_LABELS
  144. mulaw_t *data = (mulaw_t *)plugin->extra_data;
  145. void *put = put_s16_labels[data->conv];
  146. int channel;
  147. int nchannels = plugin->src_format.channels;
  148. for (channel = 0; channel < nchannels; ++channel) {
  149. char *src;
  150. char *dst;
  151. int src_step, dst_step;
  152. snd_pcm_uframes_t frames1;
  153. if (!src_channels[channel].enabled) {
  154. if (dst_channels[channel].wanted)
  155. snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format);
  156. dst_channels[channel].enabled = 0;
  157. continue;
  158. }
  159. dst_channels[channel].enabled = 1;
  160. src = src_channels[channel].area.addr + src_channels[channel].area.first / 8;
  161. dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
  162. src_step = src_channels[channel].area.step / 8;
  163. dst_step = dst_channels[channel].area.step / 8;
  164. frames1 = frames;
  165. while (frames1-- > 0) {
  166. signed short sample = ulaw2linear(*src);
  167. goto *put;
  168. #define PUT_S16_END after
  169. #include "plugin_ops.h"
  170. #undef PUT_S16_END
  171. after:
  172. src += src_step;
  173. dst += dst_step;
  174. }
  175. }
  176. }
  177. static void mulaw_encode(snd_pcm_plugin_t *plugin,
  178. const snd_pcm_plugin_channel_t *src_channels,
  179. snd_pcm_plugin_channel_t *dst_channels,
  180. snd_pcm_uframes_t frames)
  181. {
  182. #define GET_S16_LABELS
  183. #include "plugin_ops.h"
  184. #undef GET_S16_LABELS
  185. mulaw_t *data = (mulaw_t *)plugin->extra_data;
  186. void *get = get_s16_labels[data->conv];
  187. int channel;
  188. int nchannels = plugin->src_format.channels;
  189. signed short sample = 0;
  190. for (channel = 0; channel < nchannels; ++channel) {
  191. char *src;
  192. char *dst;
  193. int src_step, dst_step;
  194. snd_pcm_uframes_t frames1;
  195. if (!src_channels[channel].enabled) {
  196. if (dst_channels[channel].wanted)
  197. snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format);
  198. dst_channels[channel].enabled = 0;
  199. continue;
  200. }
  201. dst_channels[channel].enabled = 1;
  202. src = src_channels[channel].area.addr + src_channels[channel].area.first / 8;
  203. dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
  204. src_step = src_channels[channel].area.step / 8;
  205. dst_step = dst_channels[channel].area.step / 8;
  206. frames1 = frames;
  207. while (frames1-- > 0) {
  208. goto *get;
  209. #define GET_S16_END after
  210. #include "plugin_ops.h"
  211. #undef GET_S16_END
  212. after:
  213. *dst = linear2ulaw(sample);
  214. src += src_step;
  215. dst += dst_step;
  216. }
  217. }
  218. }
  219. static snd_pcm_sframes_t mulaw_transfer(snd_pcm_plugin_t *plugin,
  220. const snd_pcm_plugin_channel_t *src_channels,
  221. snd_pcm_plugin_channel_t *dst_channels,
  222. snd_pcm_uframes_t frames)
  223. {
  224. mulaw_t *data;
  225. snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO);
  226. if (frames == 0)
  227. return 0;
  228. #ifdef CONFIG_SND_DEBUG
  229. {
  230. unsigned int channel;
  231. for (channel = 0; channel < plugin->src_format.channels; channel++) {
  232. snd_assert(src_channels[channel].area.first % 8 == 0 &&
  233. src_channels[channel].area.step % 8 == 0,
  234. return -ENXIO);
  235. snd_assert(dst_channels[channel].area.first % 8 == 0 &&
  236. dst_channels[channel].area.step % 8 == 0,
  237. return -ENXIO);
  238. }
  239. }
  240. #endif
  241. data = (mulaw_t *)plugin->extra_data;
  242. data->func(plugin, src_channels, dst_channels, frames);
  243. return frames;
  244. }
  245. int snd_pcm_plugin_build_mulaw(snd_pcm_plug_t *plug,
  246. snd_pcm_plugin_format_t *src_format,
  247. snd_pcm_plugin_format_t *dst_format,
  248. snd_pcm_plugin_t **r_plugin)
  249. {
  250. int err;
  251. mulaw_t *data;
  252. snd_pcm_plugin_t *plugin;
  253. snd_pcm_plugin_format_t *format;
  254. mulaw_f func;
  255. snd_assert(r_plugin != NULL, return -ENXIO);
  256. *r_plugin = NULL;
  257. snd_assert(src_format->rate == dst_format->rate, return -ENXIO);
  258. snd_assert(src_format->channels == dst_format->channels, return -ENXIO);
  259. if (dst_format->format == SNDRV_PCM_FORMAT_MU_LAW) {
  260. format = src_format;
  261. func = mulaw_encode;
  262. }
  263. else if (src_format->format == SNDRV_PCM_FORMAT_MU_LAW) {
  264. format = dst_format;
  265. func = mulaw_decode;
  266. }
  267. else {
  268. snd_BUG();
  269. return -EINVAL;
  270. }
  271. snd_assert(snd_pcm_format_linear(format->format) != 0, return -ENXIO);
  272. err = snd_pcm_plugin_build(plug, "Mu-Law<->linear conversion",
  273. src_format, dst_format,
  274. sizeof(mulaw_t), &plugin);
  275. if (err < 0)
  276. return err;
  277. data = (mulaw_t*)plugin->extra_data;
  278. data->func = func;
  279. data->conv = getput_index(format->format);
  280. snd_assert(data->conv >= 0 && data->conv < 4*2*2, return -EINVAL);
  281. plugin->transfer = mulaw_transfer;
  282. *r_plugin = plugin;
  283. return 0;
  284. }