codec_alaw.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. .src_codec = {
  65. .name = "alaw",
  66. .type = AST_MEDIA_TYPE_AUDIO,
  67. .sample_rate = 8000,
  68. },
  69. .dst_codec = {
  70. .name = "slin",
  71. .type = AST_MEDIA_TYPE_AUDIO,
  72. .sample_rate = 8000,
  73. },
  74. .format = "slin",
  75. .framein = alawtolin_framein,
  76. .sample = alaw_sample,
  77. .buffer_samples = BUFFER_SAMPLES,
  78. .buf_size = BUFFER_SAMPLES * 2,
  79. };
  80. static struct ast_translator lintoalaw = {
  81. .name = "lintoalaw",
  82. .src_codec = {
  83. .name = "slin",
  84. .type = AST_MEDIA_TYPE_AUDIO,
  85. .sample_rate = 8000,
  86. },
  87. .dst_codec = {
  88. .name = "alaw",
  89. .type = AST_MEDIA_TYPE_AUDIO,
  90. .sample_rate = 8000,
  91. },
  92. .format = "alaw",
  93. .framein = lintoalaw_framein,
  94. .sample = slin8_sample,
  95. .buffer_samples = BUFFER_SAMPLES,
  96. .buf_size = BUFFER_SAMPLES,
  97. };
  98. static int unload_module(void)
  99. {
  100. int res;
  101. res = ast_unregister_translator(&lintoalaw);
  102. res |= ast_unregister_translator(&alawtolin);
  103. return res;
  104. }
  105. static int load_module(void)
  106. {
  107. int res;
  108. res = ast_register_translator(&alawtolin);
  109. res |= ast_register_translator(&lintoalaw);
  110. if (res) {
  111. unload_module();
  112. return AST_MODULE_LOAD_FAILURE;
  113. }
  114. return AST_MODULE_LOAD_SUCCESS;
  115. }
  116. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "A-law Coder/Decoder",
  117. .support_level = AST_MODULE_SUPPORT_CORE,
  118. .load = load_module,
  119. .unload = unload_module,
  120. );