codec_a_mu.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. .src_codec = {
  67. .name = "alaw",
  68. .type = AST_MEDIA_TYPE_AUDIO,
  69. .sample_rate = 8000,
  70. },
  71. .dst_codec = {
  72. .name = "ulaw",
  73. .type = AST_MEDIA_TYPE_AUDIO,
  74. .sample_rate = 8000,
  75. },
  76. .format = "ulaw",
  77. .framein = alawtoulaw_framein,
  78. .sample = alaw_sample,
  79. .buffer_samples = BUFFER_SAMPLES,
  80. .buf_size = BUFFER_SAMPLES,
  81. };
  82. static struct ast_translator ulawtoalaw = {
  83. .name = "ulawtoalaw",
  84. .src_codec = {
  85. .name = "ulaw",
  86. .type = AST_MEDIA_TYPE_AUDIO,
  87. .sample_rate = 8000,
  88. },
  89. .dst_codec = {
  90. .name = "alaw",
  91. .type = AST_MEDIA_TYPE_AUDIO,
  92. .sample_rate = 8000,
  93. },
  94. .format = "alaw",
  95. .framein = ulawtoalaw_framein,
  96. .sample = ulaw_sample,
  97. .buffer_samples = BUFFER_SAMPLES,
  98. .buf_size = BUFFER_SAMPLES,
  99. };
  100. /*! \brief standard module glue */
  101. static int unload_module(void)
  102. {
  103. int res;
  104. res = ast_unregister_translator(&ulawtoalaw);
  105. res |= ast_unregister_translator(&alawtoulaw);
  106. return res;
  107. }
  108. static int load_module(void)
  109. {
  110. int res;
  111. int x;
  112. for (x=0;x<256;x++) {
  113. mu2a[x] = AST_LIN2A(AST_MULAW(x));
  114. a2mu[x] = AST_LIN2MU(AST_ALAW(x));
  115. }
  116. res = ast_register_translator(&alawtoulaw);
  117. res |= ast_register_translator(&ulawtoalaw);
  118. if (res) {
  119. unload_module();
  120. return AST_MODULE_LOAD_DECLINE;
  121. }
  122. return AST_MODULE_LOAD_SUCCESS;
  123. }
  124. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "A-law and Mulaw direct Coder/Decoder");