alaw.c 5.1 KB

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