codec_ulaw.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_ulaw.c - translate between signed linear and ulaw
  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/config.h"
  31. #include "asterisk/translate.h"
  32. #include "asterisk/ulaw.h"
  33. #include "asterisk/utils.h"
  34. #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
  35. /* Sample frame data */
  36. #include "asterisk/slin.h"
  37. #include "ex_ulaw.h"
  38. /*! \brief convert and store samples in outbuf */
  39. static int ulawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  40. {
  41. int i = f->samples;
  42. unsigned char *src = f->data.ptr;
  43. int16_t *dst = pvt->outbuf.i16 + pvt->samples;
  44. pvt->samples += i;
  45. pvt->datalen += i * 2; /* 2 bytes/sample */
  46. /* convert and copy in outbuf */
  47. while (i--)
  48. *dst++ = AST_MULAW(*src++);
  49. return 0;
  50. }
  51. /*! \brief convert and store samples in outbuf */
  52. static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  53. {
  54. int i = f->samples;
  55. char *dst = pvt->outbuf.c + pvt->samples;
  56. int16_t *src = f->data.ptr;
  57. pvt->samples += i;
  58. pvt->datalen += i; /* 1 byte/sample */
  59. while (i--)
  60. *dst++ = AST_LIN2MU(*src++);
  61. return 0;
  62. }
  63. /*!
  64. * \brief The complete translator for ulawToLin.
  65. */
  66. static struct ast_translator ulawtolin = {
  67. .name = "ulawtolin",
  68. .srcfmt = AST_FORMAT_ULAW,
  69. .dstfmt = AST_FORMAT_SLINEAR,
  70. .framein = ulawtolin_framein,
  71. .sample = ulaw_sample,
  72. .buffer_samples = BUFFER_SAMPLES,
  73. .buf_size = BUFFER_SAMPLES * 2,
  74. };
  75. static struct ast_translator testlawtolin = {
  76. .name = "testlawtolin",
  77. .srcfmt = AST_FORMAT_TESTLAW,
  78. .dstfmt = AST_FORMAT_SLINEAR,
  79. .framein = ulawtolin_framein,
  80. .sample = ulaw_sample,
  81. .buffer_samples = BUFFER_SAMPLES,
  82. .buf_size = BUFFER_SAMPLES * 2,
  83. };
  84. /*!
  85. * \brief The complete translator for LinToulaw.
  86. */
  87. static struct ast_translator lintoulaw = {
  88. .name = "lintoulaw",
  89. .srcfmt = AST_FORMAT_SLINEAR,
  90. .dstfmt = AST_FORMAT_ULAW,
  91. .framein = lintoulaw_framein,
  92. .sample = slin8_sample,
  93. .buf_size = BUFFER_SAMPLES,
  94. .buffer_samples = BUFFER_SAMPLES,
  95. };
  96. static struct ast_translator lintotestlaw = {
  97. .name = "lintotestlaw",
  98. .srcfmt = AST_FORMAT_SLINEAR,
  99. .dstfmt = AST_FORMAT_TESTLAW,
  100. .framein = lintoulaw_framein,
  101. .sample = slin8_sample,
  102. .buf_size = BUFFER_SAMPLES,
  103. .buffer_samples = BUFFER_SAMPLES,
  104. };
  105. static int reload(void)
  106. {
  107. return AST_MODULE_LOAD_SUCCESS;
  108. }
  109. static int unload_module(void)
  110. {
  111. int res;
  112. res = ast_unregister_translator(&lintoulaw);
  113. res |= ast_unregister_translator(&ulawtolin);
  114. res |= ast_unregister_translator(&testlawtolin);
  115. res |= ast_unregister_translator(&lintotestlaw);
  116. return res;
  117. }
  118. static int load_module(void)
  119. {
  120. int res;
  121. res = ast_register_translator(&ulawtolin);
  122. if (!res) {
  123. res = ast_register_translator(&lintoulaw);
  124. res |= ast_register_translator(&lintotestlaw);
  125. res |= ast_register_translator(&testlawtolin);
  126. } else
  127. ast_unregister_translator(&ulawtolin);
  128. if (res)
  129. return AST_MODULE_LOAD_FAILURE;
  130. return AST_MODULE_LOAD_SUCCESS;
  131. }
  132. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
  133. .load = load_module,
  134. .unload = unload_module,
  135. .reload = reload,
  136. );