codec_alaw.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /*** 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/alaw.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_alaw.h"
  38. /*! \brief decode frame into lin and fill output buffer. */
  39. static int alawtolin_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. while (i--)
  47. *dst++ = AST_ALAW(*src++);
  48. return 0;
  49. }
  50. /*! \brief convert and store input samples in output buffer */
  51. static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  52. {
  53. int i = f->samples;
  54. char *dst = pvt->outbuf.c + pvt->samples;
  55. int16_t *src = f->data.ptr;
  56. pvt->samples += i;
  57. pvt->datalen += i; /* 1 byte/sample */
  58. while (i--)
  59. *dst++ = AST_LIN2A(*src++);
  60. return 0;
  61. }
  62. static struct ast_translator alawtolin = {
  63. .name = "alawtolin",
  64. .framein = alawtolin_framein,
  65. .sample = alaw_sample,
  66. .buffer_samples = BUFFER_SAMPLES,
  67. .buf_size = BUFFER_SAMPLES * 2,
  68. };
  69. static struct ast_translator lintoalaw = {
  70. "lintoalaw",
  71. .framein = lintoalaw_framein,
  72. .sample = slin8_sample,
  73. .buffer_samples = BUFFER_SAMPLES,
  74. .buf_size = BUFFER_SAMPLES,
  75. };
  76. /*! \brief standard module stuff */
  77. static int reload(void)
  78. {
  79. return AST_MODULE_LOAD_SUCCESS;
  80. }
  81. static int unload_module(void)
  82. {
  83. int res;
  84. res = ast_unregister_translator(&lintoalaw);
  85. res |= ast_unregister_translator(&alawtolin);
  86. return res;
  87. }
  88. static int load_module(void)
  89. {
  90. int res;
  91. ast_format_set(&lintoalaw.src_format, AST_FORMAT_SLINEAR, 0);
  92. ast_format_set(&lintoalaw.dst_format, AST_FORMAT_ALAW, 0);
  93. ast_format_set(&alawtolin.src_format, AST_FORMAT_ALAW, 0);
  94. ast_format_set(&alawtolin.dst_format, AST_FORMAT_SLINEAR, 0);
  95. res = ast_register_translator(&alawtolin);
  96. if (!res)
  97. res = ast_register_translator(&lintoalaw);
  98. else
  99. ast_unregister_translator(&alawtolin);
  100. if (res)
  101. return AST_MODULE_LOAD_FAILURE;
  102. return AST_MODULE_LOAD_SUCCESS;
  103. }
  104. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "A-law Coder/Decoder",
  105. .load = load_module,
  106. .unload = unload_module,
  107. .reload = reload,
  108. );