codec_ulaw.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. .src_codec = {
  69. .name = "ulaw",
  70. .type = AST_MEDIA_TYPE_AUDIO,
  71. .sample_rate = 8000,
  72. },
  73. .dst_codec = {
  74. .name = "slin",
  75. .type = AST_MEDIA_TYPE_AUDIO,
  76. .sample_rate = 8000,
  77. },
  78. .format = "slin",
  79. .framein = ulawtolin_framein,
  80. .sample = ulaw_sample,
  81. .buffer_samples = BUFFER_SAMPLES,
  82. .buf_size = BUFFER_SAMPLES * 2,
  83. };
  84. static struct ast_translator testlawtolin = {
  85. .name = "testlawtolin",
  86. .src_codec = {
  87. .name = "testlaw",
  88. .type = AST_MEDIA_TYPE_AUDIO,
  89. .sample_rate = 8000,
  90. },
  91. .dst_codec = {
  92. .name = "slin",
  93. .type = AST_MEDIA_TYPE_AUDIO,
  94. .sample_rate = 8000,
  95. },
  96. .format = "slin",
  97. .framein = ulawtolin_framein,
  98. .sample = ulaw_sample,
  99. .buffer_samples = BUFFER_SAMPLES,
  100. .buf_size = BUFFER_SAMPLES * 2,
  101. };
  102. /*!
  103. * \brief The complete translator for LinToulaw.
  104. */
  105. static struct ast_translator lintoulaw = {
  106. .name = "lintoulaw",
  107. .src_codec = {
  108. .name = "slin",
  109. .type = AST_MEDIA_TYPE_AUDIO,
  110. .sample_rate = 8000,
  111. },
  112. .dst_codec = {
  113. .name = "ulaw",
  114. .type = AST_MEDIA_TYPE_AUDIO,
  115. .sample_rate = 8000,
  116. },
  117. .format = "ulaw",
  118. .framein = lintoulaw_framein,
  119. .sample = slin8_sample,
  120. .buf_size = BUFFER_SAMPLES,
  121. .buffer_samples = BUFFER_SAMPLES,
  122. };
  123. static struct ast_translator lintotestlaw = {
  124. .name = "lintotestlaw",
  125. .src_codec = {
  126. .name = "slin",
  127. .type = AST_MEDIA_TYPE_AUDIO,
  128. .sample_rate = 8000,
  129. },
  130. .dst_codec = {
  131. .name = "testlaw",
  132. .type = AST_MEDIA_TYPE_AUDIO,
  133. .sample_rate = 8000,
  134. },
  135. .format = "testlaw",
  136. .framein = lintoulaw_framein,
  137. .sample = slin8_sample,
  138. .buf_size = BUFFER_SAMPLES,
  139. .buffer_samples = BUFFER_SAMPLES,
  140. };
  141. static int unload_module(void)
  142. {
  143. int res;
  144. res = ast_unregister_translator(&lintoulaw);
  145. res |= ast_unregister_translator(&ulawtolin);
  146. res |= ast_unregister_translator(&testlawtolin);
  147. res |= ast_unregister_translator(&lintotestlaw);
  148. return res;
  149. }
  150. static int load_module(void)
  151. {
  152. int res;
  153. res = ast_register_translator(&ulawtolin);
  154. res |= ast_register_translator(&lintoulaw);
  155. res |= ast_register_translator(&lintotestlaw);
  156. res |= ast_register_translator(&testlawtolin);
  157. if (res) {
  158. unload_module();
  159. return AST_MODULE_LOAD_DECLINE;
  160. }
  161. return AST_MODULE_LOAD_SUCCESS;
  162. }
  163. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
  164. .support_level = AST_MODULE_SUPPORT_CORE,
  165. .load = load_module,
  166. .unload = unload_module,
  167. );