alaw.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * \brief A-Law to Signed linear conversion
  20. */
  21. #ifndef _ASTERISK_ALAW_H
  22. #define _ASTERISK_ALAW_H
  23. /*! \brief
  24. * To init the alaw to slinear conversion stuff, this needs to be run.
  25. */
  26. void ast_alaw_init(void);
  27. #define AST_ALAW_BIT_LOSS 4
  28. #define AST_ALAW_STEP (1 << AST_ALAW_BIT_LOSS)
  29. #define AST_ALAW_TAB_SIZE (32768 / AST_ALAW_STEP + 1)
  30. #define AST_ALAW_SIGN_BIT 0x80
  31. #define AST_ALAW_AMI_MASK 0x55
  32. /*! \brief converts signed linear to alaw */
  33. #ifndef G711_NEW_ALGORITHM
  34. extern unsigned char __ast_lin2a[8192];
  35. #else
  36. extern unsigned char __ast_lin2a[AST_ALAW_TAB_SIZE];
  37. #endif
  38. /*! help */
  39. extern short __ast_alaw[256];
  40. #ifndef G711_NEW_ALGORITHM
  41. #define AST_LIN2A(a) (__ast_lin2a[((unsigned short)(a)) >> 3])
  42. #else
  43. #define AST_LIN2A_LOOKUP(mag) \
  44. __ast_lin2a[(mag) >> AST_ALAW_BIT_LOSS]
  45. /*! \brief Convert signed linear sample to sign-magnitude pair for a-Law */
  46. static inline void ast_alaw_get_sign_mag(short sample, unsigned *sign, unsigned *mag)
  47. {
  48. /* It may look illogical to retrive the sign this way in both cases,
  49. * but this helps gcc eliminate the branch below and produces
  50. * faster code */
  51. *sign = ((unsigned short)sample >> 8) & AST_ALAW_SIGN_BIT;
  52. #if defined(G711_REDUCED_BRANCHING)
  53. {
  54. unsigned dual_mag = (-sample << 16) | (unsigned short)sample;
  55. *mag = (dual_mag >> (*sign >> 3)) & 0xffffU;
  56. }
  57. #else
  58. if (sample < 0)
  59. *mag = -sample;
  60. else
  61. *mag = sample;
  62. #endif /* G711_REDUCED_BRANCHING */
  63. *sign ^= AST_ALAW_SIGN_BIT;
  64. }
  65. static inline unsigned char AST_LIN2A(short sample)
  66. {
  67. unsigned mag, sign;
  68. ast_alaw_get_sign_mag(sample, &sign, &mag);
  69. return (sign | AST_LIN2A_LOOKUP(mag)) ^ AST_ALAW_AMI_MASK;
  70. }
  71. #endif
  72. #define AST_ALAW(a) (__ast_alaw[(int)(a)])
  73. #endif /* _ASTERISK_ALAW_H */