codec_ulaw.c 4.4 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. #include "asterisk.h"
  25. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  26. #include "asterisk/module.h"
  27. #include "asterisk/config.h"
  28. #include "asterisk/translate.h"
  29. #include "asterisk/ulaw.h"
  30. #include "asterisk/utils.h"
  31. #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
  32. /* Sample frame data */
  33. #include "slin_ulaw_ex.h"
  34. #include "ulaw_slin_ex.h"
  35. /*! \brief convert and store samples in outbuf */
  36. static int ulawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  37. {
  38. int i = f->samples;
  39. unsigned char *src = f->data;
  40. int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
  41. pvt->samples += i;
  42. pvt->datalen += i * 2; /* 2 bytes/sample */
  43. /* convert and copy in outbuf */
  44. while (i--)
  45. *dst++ = AST_MULAW(*src++);
  46. return 0;
  47. }
  48. /*! \brief convert and store samples in outbuf */
  49. static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  50. {
  51. int i = f->samples;
  52. char *dst = pvt->outbuf + pvt->samples;
  53. int16_t *src = f->data;
  54. pvt->samples += i;
  55. pvt->datalen += i; /* 1 byte/sample */
  56. while (i--)
  57. *dst++ = AST_LIN2MU(*src++);
  58. return 0;
  59. }
  60. /*! * \brief ulawToLin_Sample */
  61. static struct ast_frame *ulawtolin_sample(void)
  62. {
  63. static struct ast_frame f;
  64. f.frametype = AST_FRAME_VOICE;
  65. f.subclass = AST_FORMAT_ULAW;
  66. f.datalen = sizeof(ulaw_slin_ex);
  67. f.samples = sizeof(ulaw_slin_ex);
  68. f.mallocd = 0;
  69. f.offset = 0;
  70. f.src = __PRETTY_FUNCTION__;
  71. f.data = ulaw_slin_ex;
  72. return &f;
  73. }
  74. /*!
  75. * \brief LinToulaw_Sample
  76. */
  77. static struct ast_frame *lintoulaw_sample(void)
  78. {
  79. static struct ast_frame f;
  80. f.frametype = AST_FRAME_VOICE;
  81. f.subclass = AST_FORMAT_SLINEAR;
  82. f.datalen = sizeof(slin_ulaw_ex);
  83. /* Assume 8000 Hz */
  84. f.samples = sizeof(slin_ulaw_ex) / 2;
  85. f.mallocd = 0;
  86. f.offset = 0;
  87. f.src = __PRETTY_FUNCTION__;
  88. f.data = slin_ulaw_ex;
  89. return &f;
  90. }
  91. /*!
  92. * \brief The complete translator for ulawToLin.
  93. */
  94. static struct ast_translator ulawtolin = {
  95. .name = "ulawtolin",
  96. .srcfmt = AST_FORMAT_ULAW,
  97. .dstfmt = AST_FORMAT_SLINEAR,
  98. .framein = ulawtolin_framein,
  99. .sample = ulawtolin_sample,
  100. .buffer_samples = BUFFER_SAMPLES,
  101. .buf_size = BUFFER_SAMPLES * 2,
  102. .plc_samples = 160,
  103. };
  104. /*!
  105. * \brief The complete translator for LinToulaw.
  106. */
  107. static struct ast_translator lintoulaw = {
  108. .name = "lintoulaw",
  109. .srcfmt = AST_FORMAT_SLINEAR,
  110. .dstfmt = AST_FORMAT_ULAW,
  111. .framein = lintoulaw_framein,
  112. .sample = lintoulaw_sample,
  113. .buf_size = BUFFER_SAMPLES,
  114. .buffer_samples = BUFFER_SAMPLES,
  115. };
  116. static int parse_config(int reload)
  117. {
  118. struct ast_variable *var;
  119. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  120. struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
  121. if (cfg == NULL)
  122. return 0;
  123. if (cfg == CONFIG_STATUS_FILEUNCHANGED)
  124. return 0;
  125. for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
  126. if (!strcasecmp(var->name, "genericplc")) {
  127. ulawtolin.useplc = ast_true(var->value) ? 1 : 0;
  128. ast_verb(3, "codec_ulaw: %susing generic PLC\n", ulawtolin.useplc ? "" : "not ");
  129. }
  130. }
  131. ast_config_destroy(cfg);
  132. return 0;
  133. }
  134. static int reload(void)
  135. {
  136. if (parse_config(1))
  137. return AST_MODULE_LOAD_DECLINE;
  138. return AST_MODULE_LOAD_SUCCESS;
  139. }
  140. static int unload_module(void)
  141. {
  142. int res;
  143. res = ast_unregister_translator(&lintoulaw);
  144. res |= ast_unregister_translator(&ulawtolin);
  145. return res;
  146. }
  147. static int load_module(void)
  148. {
  149. int res;
  150. if (parse_config(0))
  151. return AST_MODULE_LOAD_DECLINE;
  152. res = ast_register_translator(&ulawtolin);
  153. if (!res)
  154. res = ast_register_translator(&lintoulaw);
  155. else
  156. ast_unregister_translator(&ulawtolin);
  157. if (res)
  158. return AST_MODULE_LOAD_FAILURE;
  159. return AST_MODULE_LOAD_SUCCESS;
  160. }
  161. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
  162. .load = load_module,
  163. .unload = unload_module,
  164. .reload = reload,
  165. );