alaw.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. */
  23. #include "asterisk.h"
  24. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  25. #include "asterisk/alaw.h"
  26. #define AMI_MASK 0x55
  27. static inline unsigned char linear2alaw (short int linear)
  28. {
  29. int mask;
  30. int seg;
  31. int pcm_val;
  32. static int seg_end[8] =
  33. {
  34. 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF
  35. };
  36. pcm_val = linear;
  37. if (pcm_val >= 0)
  38. {
  39. /* Sign (7th) bit = 1 */
  40. mask = AMI_MASK | 0x80;
  41. }
  42. else
  43. {
  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. {
  51. if (pcm_val <= seg_end[seg])
  52. break;
  53. }
  54. /* Combine the sign, segment, and quantization bits. */
  55. return ((seg << 4) | ((pcm_val >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask;
  56. }
  57. /*- End of function --------------------------------------------------------*/
  58. static inline short int alaw2linear (unsigned char alaw)
  59. {
  60. int i;
  61. int seg;
  62. alaw ^= AMI_MASK;
  63. i = ((alaw & 0x0F) << 4);
  64. seg = (((int) alaw & 0x70) >> 4);
  65. if (seg)
  66. i = (i + 0x100) << (seg - 1);
  67. return (short int) ((alaw & 0x80) ? i : -i);
  68. }
  69. unsigned char __ast_lin2a[8192];
  70. short __ast_alaw[256];
  71. void ast_alaw_init(void)
  72. {
  73. int i;
  74. /*
  75. * Set up mu-law conversion table
  76. */
  77. for(i = 0;i < 256;i++)
  78. {
  79. __ast_alaw[i] = alaw2linear(i);
  80. }
  81. /* set up the reverse (mu-law) conversion table */
  82. for(i = -32768; i < 32768; i++)
  83. {
  84. __ast_lin2a[((unsigned short)i) >> 3] = linear2alaw(i);
  85. }
  86. }