ulaw.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief u-Law to Signed linear conversion
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/ulaw.h"
  30. #include "asterisk/logger.h"
  31. #if 0
  32. /* ZEROTRAP is the military recommendation to improve the encryption
  33. * of u-Law traffic. It is irrelevant with modern encryption systems
  34. * like AES, and will simply degrade the signal quality.
  35. * ZEROTRAP is not implemented in AST_LIN2MU and so the coding table
  36. * tests will fail if you use it */
  37. #define ZEROTRAP /*!< turn on the trap as per the MIL-STD */
  38. #endif
  39. #define BIAS 0x84 /*!< define the add-in bias for 16 bit samples */
  40. #define CLIP 32635
  41. #ifndef G711_NEW_ALGORITHM
  42. unsigned char __ast_lin2mu[16384];
  43. short __ast_mulaw[256];
  44. static unsigned char linear2ulaw(short sample)
  45. {
  46. static int exp_lut[256] = {
  47. 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
  48. 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  49. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  50. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  51. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  52. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  53. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  54. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  55. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  56. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  57. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  58. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  59. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  60. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  61. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  62. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 };
  63. int sign, exponent, mantissa;
  64. unsigned char ulawbyte;
  65. /* Get the sample into sign-magnitude. */
  66. sign = (sample >> 8) & 0x80; /* set aside the sign */
  67. if (sign != 0)
  68. sample = -sample; /* get magnitude */
  69. if (sample > CLIP)
  70. sample = CLIP; /* clip the magnitude */
  71. /* Convert from 16 bit linear to ulaw. */
  72. sample = sample + BIAS;
  73. exponent = exp_lut[(sample >> 7) & 0xFF];
  74. mantissa = (sample >> (exponent + 3)) & 0x0F;
  75. ulawbyte = ~(sign | (exponent << 4) | mantissa);
  76. #ifdef ZEROTRAP
  77. if (ulawbyte == 0)
  78. ulawbyte = 0x02; /* optional CCITT trap */
  79. #endif
  80. return ulawbyte;
  81. }
  82. #else
  83. unsigned char __ast_lin2mu[AST_ULAW_TAB_SIZE];
  84. short __ast_mulaw[256];
  85. static unsigned char linear2ulaw(short sample, int full_coding)
  86. {
  87. static const unsigned exp_lut[256] = {
  88. 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
  89. 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  90. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  91. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  92. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  93. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  94. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  95. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  96. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  97. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  98. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  99. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  100. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  101. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  102. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  103. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 };
  104. unsigned sign, exponent, mantissa, mag;
  105. unsigned char ulawbyte;
  106. /* Get the sample into sign-magnitude. */
  107. ast_ulaw_get_sign_mag(sample, &sign, &mag);
  108. if (mag > CLIP)
  109. mag = CLIP; /* clip the magnitude */
  110. sign = (sample >> 8) & 0x80; /* set aside the sign */
  111. if (sign != 0)
  112. sample = -sample; /* get magnitude */
  113. if (sample > CLIP)
  114. sample = CLIP; /* clip the magnitude */
  115. /* Convert from 16 bit linear to ulaw. */
  116. mag += BIAS;
  117. exponent = exp_lut[(mag >> 7) & 0xFF];
  118. mantissa = (mag >> (exponent + 3)) & 0x0F;
  119. if (full_coding) {
  120. /* full encoding, with sign and xform */
  121. ulawbyte = ~(sign | (exponent << 4) | mantissa);
  122. #ifdef ZEROTRAP
  123. if (ulawbyte == 0)
  124. ulawbyte = 0x02; /* optional CCITT trap */
  125. #endif
  126. } else {
  127. /* half-cooked coding -- mantissa+exponent only (for lookup tab) */
  128. ulawbyte = (exponent << 4) | mantissa;
  129. }
  130. return ulawbyte;
  131. }
  132. static inline short ulaw2linear(unsigned char ulawbyte)
  133. {
  134. unsigned exponent, mantissa;
  135. short sample;
  136. static const short etab[]={0,132,396,924,1980,4092,8316,16764};
  137. ulawbyte = ~ulawbyte;
  138. exponent = (ulawbyte & 0x70) >> 4;
  139. mantissa = ulawbyte & 0x0f;
  140. sample = mantissa << (exponent + 3);
  141. sample += etab[exponent];
  142. if (ulawbyte & 0x80)
  143. sample = -sample;
  144. return sample;
  145. }
  146. #endif
  147. /*!
  148. * \brief Set up mu-law conversion table
  149. */
  150. void ast_ulaw_init(void)
  151. {
  152. int i;
  153. /*
  154. * Set up mu-law conversion table
  155. */
  156. #ifndef G711_NEW_ALGORITHM
  157. for (i = 0;i < 256;i++) {
  158. short mu,e,f,y;
  159. static const short etab[]={0,132,396,924,1980,4092,8316,16764};
  160. mu = 255-i;
  161. e = (mu & 0x70)/16;
  162. f = mu & 0x0f;
  163. y = f * (1 << (e + 3));
  164. y += etab[e];
  165. if (mu & 0x80) y = -y;
  166. __ast_mulaw[i] = y;
  167. }
  168. /* set up the reverse (mu-law) conversion table */
  169. for (i = -32768; i < 32768; i++) {
  170. __ast_lin2mu[((unsigned short)i) >> 2] = linear2ulaw(i);
  171. }
  172. #else
  173. for (i = 0; i < 256; i++) {
  174. __ast_mulaw[i] = ulaw2linear(i);
  175. }
  176. /* set up the reverse (mu-law) conversion table */
  177. for (i = 0; i <= 32768; i += AST_ULAW_STEP) {
  178. AST_LIN2MU_LOOKUP(i) = linear2ulaw(i, 0 /* half-cooked */);
  179. }
  180. #endif
  181. #ifdef TEST_CODING_TABLES
  182. for (i = -32768; i < 32768; ++i) {
  183. #ifndef G711_NEW_ALGORITHM
  184. unsigned char e1 = linear2ulaw(i);
  185. #else
  186. unsigned char e1 = linear2ulaw(i, 1);
  187. #endif
  188. short d1 = ulaw2linear(e1);
  189. unsigned char e2 = AST_LIN2MU(i);
  190. short d2 = ulaw2linear(e2);
  191. short d3 = AST_MULAW(e1);
  192. if (e1 != e2 || d1 != d3 || d2 != d3) {
  193. ast_log(LOG_WARNING, "u-Law coding tables test failed on %d: e1=%u, e2=%u, d1=%d, d2=%d\n",
  194. i, (unsigned)e1, (unsigned)e2, (int)d1, (int)d2);
  195. }
  196. }
  197. ast_log(LOG_NOTICE, "u-Law coding table test complete.\n");
  198. #endif /* TEST_CODING_TABLES */
  199. #ifdef TEST_TANDEM_TRANSCODING
  200. /* tandem transcoding test */
  201. for (i = -32768; i < 32768; ++i) {
  202. unsigned char e1 = AST_LIN2MU(i);
  203. short d1 = AST_MULAW(e1);
  204. unsigned char e2 = AST_LIN2MU(d1);
  205. short d2 = AST_MULAW(e2);
  206. unsigned char e3 = AST_LIN2MU(d2);
  207. short d3 = AST_MULAW(e3);
  208. if (i < 0 && e1 == 0x7f && e2 == 0xff && e3 == 0xff)
  209. continue; /* known and normal negative 0 case */
  210. if (e1 != e2 || e2 != e3 || d1 != d2 || d2 != d3) {
  211. ast_log(LOG_WARNING, "u-Law tandem transcoding test failed on %d: e1=%u, e2=%u, d1=%d, d2=%d, d3=%d\n",
  212. i, (unsigned)e1, (unsigned)e2, (int)d1, (int)d2, (int)d3);
  213. }
  214. }
  215. ast_log(LOG_NOTICE, "u-Law tandem transcoding test complete.\n");
  216. #endif /* TEST_TANDEM_TRANSCODING */
  217. }