alaw.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 a-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/alaw.h"
  30. #include "asterisk/logger.h"
  31. #ifndef G711_NEW_ALGORITHM
  32. #define AMI_MASK 0x55
  33. static inline unsigned char linear2alaw(short int linear)
  34. {
  35. int mask;
  36. int seg;
  37. int pcm_val;
  38. static int seg_end[8] =
  39. {
  40. 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF
  41. };
  42. pcm_val = linear;
  43. if (pcm_val >= 0) {
  44. /* Sign (7th) bit = 1 */
  45. mask = AMI_MASK | 0x80;
  46. } else {
  47. /* Sign bit = 0 */
  48. mask = AMI_MASK;
  49. pcm_val = -pcm_val;
  50. }
  51. /* Convert the scaled magnitude to segment number. */
  52. for (seg = 0; seg < 8; seg++) {
  53. if (pcm_val <= seg_end[seg]) {
  54. break;
  55. }
  56. }
  57. /* Combine the sign, segment, and quantization bits. */
  58. return ((seg << 4) | ((pcm_val >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask;
  59. }
  60. #else
  61. static unsigned char linear2alaw(short sample, int full_coding)
  62. {
  63. static const unsigned exp_lut[128] = {
  64. 1,1,2,2,3,3,3,3,
  65. 4,4,4,4,4,4,4,4,
  66. 5,5,5,5,5,5,5,5,
  67. 5,5,5,5,5,5,5,5,
  68. 6,6,6,6,6,6,6,6,
  69. 6,6,6,6,6,6,6,6,
  70. 6,6,6,6,6,6,6,6,
  71. 6,6,6,6,6,6,6,6,
  72. 7,7,7,7,7,7,7,7,
  73. 7,7,7,7,7,7,7,7,
  74. 7,7,7,7,7,7,7,7,
  75. 7,7,7,7,7,7,7,7,
  76. 7,7,7,7,7,7,7,7,
  77. 7,7,7,7,7,7,7,7,
  78. 7,7,7,7,7,7,7,7,
  79. 7,7,7,7,7,7,7,7 };
  80. unsigned sign, exponent, mantissa, mag;
  81. unsigned char alawbyte;
  82. ast_alaw_get_sign_mag(sample, &sign, &mag);
  83. if (mag > 32767)
  84. mag = 32767; /* clip the magnitude for -32768 */
  85. exponent = exp_lut[(mag >> 8) & 0x7f];
  86. mantissa = (mag >> (exponent + 3)) & 0x0f;
  87. if (mag < 0x100)
  88. exponent = 0;
  89. if (full_coding) {
  90. /* full encoding, with sign and xform */
  91. alawbyte = (unsigned char)(sign | (exponent << 4) | mantissa);
  92. alawbyte ^= AST_ALAW_AMI_MASK;
  93. } else {
  94. /* half-cooked coding -- mantissa+exponent only (for lookup tab) */
  95. alawbyte = (exponent << 4) | mantissa;
  96. }
  97. return alawbyte;
  98. }
  99. #endif
  100. #ifndef G711_NEW_ALGORITHM
  101. static inline short int alaw2linear (unsigned char alaw)
  102. {
  103. int i;
  104. int seg;
  105. alaw ^= AMI_MASK;
  106. i = ((alaw & 0x0F) << 4) + 8 /* rounding error */;
  107. seg = (((int) alaw & 0x70) >> 4);
  108. if (seg) {
  109. i = (i + 0x100) << (seg - 1);
  110. }
  111. return (short int) ((alaw & 0x80) ? i : -i);
  112. }
  113. #else
  114. static inline short alaw2linear(unsigned char alawbyte)
  115. {
  116. unsigned exponent, mantissa;
  117. short sample;
  118. alawbyte ^= AST_ALAW_AMI_MASK;
  119. exponent = (alawbyte & 0x70) >> 4;
  120. mantissa = alawbyte & 0x0f;
  121. sample = (mantissa << 4) + 8 /* rounding error */;
  122. if (exponent)
  123. sample = (sample + 0x100) << (exponent - 1);
  124. if (!(alawbyte & 0x80))
  125. sample = -sample;
  126. return sample;
  127. }
  128. #endif
  129. #ifndef G711_NEW_ALGORITHM
  130. unsigned char __ast_lin2a[8192];
  131. #else
  132. unsigned char __ast_lin2a[AST_ALAW_TAB_SIZE];
  133. #endif
  134. short __ast_alaw[256];
  135. void ast_alaw_init(void)
  136. {
  137. int i;
  138. /*
  139. * Set up mu-law conversion table
  140. */
  141. #ifndef G711_NEW_ALGORITHM
  142. for (i = 0; i < 256; i++) {
  143. __ast_alaw[i] = alaw2linear(i);
  144. }
  145. /* set up the reverse (mu-law) conversion table */
  146. for (i = -32768; i < 32768; i++) {
  147. __ast_lin2a[((unsigned short)i) >> 3] = linear2alaw(i);
  148. }
  149. #else
  150. for (i = 0; i < 256; i++) {
  151. __ast_alaw[i] = alaw2linear(i);
  152. }
  153. /* set up the reverse (a-law) conversion table */
  154. for (i = 0; i <= 32768; i += AST_ALAW_STEP) {
  155. AST_LIN2A_LOOKUP(i) = linear2alaw(i, 0 /* half-cooked */);
  156. }
  157. #endif
  158. #ifdef TEST_CODING_TABLES
  159. for (i = -32768; i < 32768; ++i) {
  160. #ifndef G711_NEW_ALGORITHM
  161. unsigned char e1 = linear2alaw(i);
  162. #else
  163. unsigned char e1 = linear2alaw(i, 1);
  164. #endif
  165. short d1 = alaw2linear(e1);
  166. unsigned char e2 = AST_LIN2A(i);
  167. short d2 = alaw2linear(e2);
  168. short d3 = AST_ALAW(e1);
  169. if (e1 != e2 || d1 != d3 || d2 != d3) {
  170. ast_log(LOG_WARNING, "a-Law coding tables test failed on %d: e1=%u, e2=%u, d1=%d, d2=%d\n",
  171. i, (unsigned)e1, (unsigned)e2, (int)d1, (int)d2);
  172. }
  173. }
  174. ast_log(LOG_NOTICE, "a-Law coding tables test complete.\n");
  175. #endif /* TEST_CODING_TABLES */
  176. #ifdef TEST_TANDEM_TRANSCODING
  177. /* tandem transcoding test */
  178. for (i = -32768; i < 32768; ++i) {
  179. unsigned char e1 = AST_LIN2A(i);
  180. short d1 = AST_ALAW(e1);
  181. unsigned char e2 = AST_LIN2A(d1);
  182. short d2 = AST_ALAW(e2);
  183. unsigned char e3 = AST_LIN2A(d2);
  184. short d3 = AST_ALAW(e3);
  185. if (e1 != e2 || e2 != e3 || d1 != d2 || d2 != d3) {
  186. ast_log(LOG_WARNING, "a-Law tandem transcoding test failed on %d: e1=%u, e2=%u, d1=%d, d2=%d, d3=%d\n",
  187. i, (unsigned)e1, (unsigned)e2, (int)d1, (int)d2, (int)d3);
  188. }
  189. }
  190. ast_log(LOG_NOTICE, "a-Law tandem transcoding test complete.\n");
  191. #endif /* TEST_TANDEM_TRANSCODING */
  192. }