codec_a_mu.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 codec_a_mu.c - translate between alaw and ulaw directly
  21. *
  22. * \ingroup codecs
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/module.h"
  30. #include "asterisk/translate.h"
  31. #include "asterisk/alaw.h"
  32. #include "asterisk/ulaw.h"
  33. #include "asterisk/utils.h"
  34. #define BUFFER_SAMPLES 8000 /* size for the translation buffers */
  35. static unsigned char mu2a[256];
  36. static unsigned char a2mu[256];
  37. /* Sample frame data */
  38. #include "ex_ulaw.h"
  39. #include "ex_alaw.h"
  40. /*! \brief convert frame data and store into the buffer */
  41. static int alawtoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  42. {
  43. int x = f->samples;
  44. unsigned char *src = f->data.ptr;
  45. unsigned char *dst = pvt->outbuf.uc + pvt->samples;
  46. pvt->samples += x;
  47. pvt->datalen += x;
  48. while (x--)
  49. *dst++ = a2mu[*src++];
  50. return 0;
  51. }
  52. /*! \brief convert frame data and store into the buffer */
  53. static int ulawtoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  54. {
  55. int x = f->samples;
  56. unsigned char *src = f->data.ptr;
  57. unsigned char *dst = pvt->outbuf.uc + pvt->samples;
  58. pvt->samples += x;
  59. pvt->datalen += x;
  60. while (x--)
  61. *dst++ = mu2a[*src++];
  62. return 0;
  63. }
  64. static struct ast_translator alawtoulaw = {
  65. .name = "alawtoulaw",
  66. .framein = alawtoulaw_framein,
  67. .sample = alaw_sample,
  68. .buffer_samples = BUFFER_SAMPLES,
  69. .buf_size = BUFFER_SAMPLES,
  70. };
  71. static struct ast_translator ulawtoalaw = {
  72. .name = "ulawtoalaw",
  73. .framein = ulawtoalaw_framein,
  74. .sample = ulaw_sample,
  75. .buffer_samples = BUFFER_SAMPLES,
  76. .buf_size = BUFFER_SAMPLES,
  77. };
  78. /*! \brief standard module glue */
  79. static int unload_module(void)
  80. {
  81. int res;
  82. res = ast_unregister_translator(&ulawtoalaw);
  83. res |= ast_unregister_translator(&alawtoulaw);
  84. return res;
  85. }
  86. static int load_module(void)
  87. {
  88. int res;
  89. int x;
  90. ast_format_set(&alawtoulaw.src_format, AST_FORMAT_ALAW, 0);
  91. ast_format_set(&alawtoulaw.dst_format, AST_FORMAT_ULAW, 0);
  92. ast_format_set(&ulawtoalaw.src_format, AST_FORMAT_ULAW, 0);
  93. ast_format_set(&ulawtoalaw.dst_format, AST_FORMAT_ALAW, 0);
  94. for (x=0;x<256;x++) {
  95. mu2a[x] = AST_LIN2A(AST_MULAW(x));
  96. a2mu[x] = AST_LIN2MU(AST_ALAW(x));
  97. }
  98. res = ast_register_translator(&alawtoulaw);
  99. if (!res)
  100. res = ast_register_translator(&ulawtoalaw);
  101. else
  102. ast_unregister_translator(&alawtoulaw);
  103. if (res)
  104. return AST_MODULE_LOAD_FAILURE;
  105. return AST_MODULE_LOAD_SUCCESS;
  106. }
  107. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "A-law and Mulaw direct Coder/Decoder");