codec_alaw.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. .srcfmt = AST_FORMAT_ALAW,
  65. .dstfmt = AST_FORMAT_SLINEAR,
  66. .framein = alawtolin_framein,
  67. .sample = alaw_sample,
  68. .buffer_samples = BUFFER_SAMPLES,
  69. .buf_size = BUFFER_SAMPLES * 2,
  70. };
  71. static struct ast_translator lintoalaw = {
  72. "lintoalaw",
  73. .srcfmt = AST_FORMAT_SLINEAR,
  74. .dstfmt = AST_FORMAT_ALAW,
  75. .framein = lintoalaw_framein,
  76. .sample = slin8_sample,
  77. .buffer_samples = BUFFER_SAMPLES,
  78. .buf_size = BUFFER_SAMPLES,
  79. };
  80. /*! \brief standard module stuff */
  81. static int reload(void)
  82. {
  83. return AST_MODULE_LOAD_SUCCESS;
  84. }
  85. static int unload_module(void)
  86. {
  87. int res;
  88. res = ast_unregister_translator(&lintoalaw);
  89. res |= ast_unregister_translator(&alawtolin);
  90. return res;
  91. }
  92. static int load_module(void)
  93. {
  94. int res;
  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. );