codec_alaw.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_alaw.c - translate between signed linear and alaw
  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/alaw.h"
  30. #include "asterisk/utils.h"
  31. #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
  32. /* Sample frame data (Mu data is okay) */
  33. #include "slin_ulaw_ex.h"
  34. #include "ulaw_slin_ex.h"
  35. /*! \brief decode frame into lin and fill output buffer. */
  36. static int alawtolin_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. while (i--)
  44. *dst++ = AST_ALAW(*src++);
  45. return 0;
  46. }
  47. /*! \brief convert and store input samples in output buffer */
  48. static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  49. {
  50. int i = f->samples;
  51. char *dst = pvt->outbuf + pvt->samples;
  52. int16_t *src = f->data;
  53. pvt->samples += i;
  54. pvt->datalen += i; /* 1 byte/sample */
  55. while (i--)
  56. *dst++ = AST_LIN2A(*src++);
  57. return 0;
  58. }
  59. /*! \brief alawToLin_Sample */
  60. static struct ast_frame *alawtolin_sample(void)
  61. {
  62. static struct ast_frame f;
  63. f.frametype = AST_FRAME_VOICE;
  64. f.subclass = AST_FORMAT_ALAW;
  65. f.datalen = sizeof(ulaw_slin_ex);
  66. f.samples = sizeof(ulaw_slin_ex);
  67. f.mallocd = 0;
  68. f.offset = 0;
  69. f.src = __PRETTY_FUNCTION__;
  70. f.data = ulaw_slin_ex;
  71. return &f;
  72. }
  73. /*! \brief LinToalaw_Sample */
  74. static struct ast_frame *lintoalaw_sample(void)
  75. {
  76. static struct ast_frame f;
  77. f.frametype = AST_FRAME_VOICE;
  78. f.subclass = AST_FORMAT_SLINEAR;
  79. f.datalen = sizeof(slin_ulaw_ex);
  80. f.samples = sizeof(slin_ulaw_ex) / 2;
  81. f.mallocd = 0;
  82. f.offset = 0;
  83. f.src = __PRETTY_FUNCTION__;
  84. f.data = slin_ulaw_ex;
  85. return &f;
  86. }
  87. static struct ast_translator alawtolin = {
  88. .name = "alawtolin",
  89. .srcfmt = AST_FORMAT_ALAW,
  90. .dstfmt = AST_FORMAT_SLINEAR,
  91. .framein = alawtolin_framein,
  92. .sample = alawtolin_sample,
  93. .buffer_samples = BUFFER_SAMPLES,
  94. .buf_size = BUFFER_SAMPLES * 2,
  95. .plc_samples = 160,
  96. };
  97. static struct ast_translator lintoalaw = {
  98. "lintoalaw",
  99. .srcfmt = AST_FORMAT_SLINEAR,
  100. .dstfmt = AST_FORMAT_ALAW,
  101. .framein = lintoalaw_framein,
  102. .sample = lintoalaw_sample,
  103. .buffer_samples = BUFFER_SAMPLES,
  104. .buf_size = BUFFER_SAMPLES,
  105. };
  106. static int parse_config(int reload)
  107. {
  108. struct ast_variable *var;
  109. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  110. struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
  111. if (cfg == NULL)
  112. return 0;
  113. if (cfg == CONFIG_STATUS_FILEUNCHANGED)
  114. return 0;
  115. for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
  116. if (!strcasecmp(var->name, "genericplc")) {
  117. alawtolin.useplc = ast_true(var->value) ? 1 : 0;
  118. ast_verb(3, "codec_alaw: %susing generic PLC\n", alawtolin.useplc ? "" : "not ");
  119. }
  120. }
  121. ast_config_destroy(cfg);
  122. return 0;
  123. }
  124. /*! \brief standard module stuff */
  125. static int reload(void)
  126. {
  127. if (parse_config(1))
  128. return AST_MODULE_LOAD_DECLINE;
  129. return AST_MODULE_LOAD_SUCCESS;
  130. }
  131. static int unload_module(void)
  132. {
  133. int res;
  134. res = ast_unregister_translator(&lintoalaw);
  135. res |= ast_unregister_translator(&alawtolin);
  136. return res;
  137. }
  138. static int load_module(void)
  139. {
  140. int res;
  141. if (parse_config(0))
  142. return AST_MODULE_LOAD_DECLINE;
  143. res = ast_register_translator(&alawtolin);
  144. if (!res)
  145. res = ast_register_translator(&lintoalaw);
  146. else
  147. ast_unregister_translator(&alawtolin);
  148. if (res)
  149. return AST_MODULE_LOAD_FAILURE;
  150. return AST_MODULE_LOAD_SUCCESS;
  151. }
  152. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "A-law Coder/Decoder",
  153. .load = load_module,
  154. .unload = unload_module,
  155. .reload = reload,
  156. );