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