codec_alaw.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include "asterisk/module.h"
  29. #include "asterisk/config.h"
  30. #include "asterisk/translate.h"
  31. #include "asterisk/alaw.h"
  32. #include "asterisk/utils.h"
  33. #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
  34. /* Sample frame data */
  35. #include "asterisk/slin.h"
  36. #include "ex_alaw.h"
  37. /*! \brief decode frame into lin and fill output buffer. */
  38. static int alawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  39. {
  40. int i = f->samples;
  41. unsigned char *src = f->data.ptr;
  42. int16_t *dst = pvt->outbuf.i16 + pvt->samples;
  43. pvt->samples += i;
  44. pvt->datalen += i * 2; /* 2 bytes/sample */
  45. while (i--)
  46. *dst++ = AST_ALAW(*src++);
  47. return 0;
  48. }
  49. /*! \brief convert and store input samples in output buffer */
  50. static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  51. {
  52. int i = f->samples;
  53. char *dst = pvt->outbuf.c + pvt->samples;
  54. int16_t *src = f->data.ptr;
  55. pvt->samples += i;
  56. pvt->datalen += i; /* 1 byte/sample */
  57. while (i--)
  58. *dst++ = AST_LIN2A(*src++);
  59. return 0;
  60. }
  61. static struct ast_translator alawtolin = {
  62. .name = "alawtolin",
  63. .src_codec = {
  64. .name = "alaw",
  65. .type = AST_MEDIA_TYPE_AUDIO,
  66. .sample_rate = 8000,
  67. },
  68. .dst_codec = {
  69. .name = "slin",
  70. .type = AST_MEDIA_TYPE_AUDIO,
  71. .sample_rate = 8000,
  72. },
  73. .format = "slin",
  74. .framein = alawtolin_framein,
  75. .sample = alaw_sample,
  76. .buffer_samples = BUFFER_SAMPLES,
  77. .buf_size = BUFFER_SAMPLES * 2,
  78. };
  79. static struct ast_translator lintoalaw = {
  80. .name = "lintoalaw",
  81. .src_codec = {
  82. .name = "slin",
  83. .type = AST_MEDIA_TYPE_AUDIO,
  84. .sample_rate = 8000,
  85. },
  86. .dst_codec = {
  87. .name = "alaw",
  88. .type = AST_MEDIA_TYPE_AUDIO,
  89. .sample_rate = 8000,
  90. },
  91. .format = "alaw",
  92. .framein = lintoalaw_framein,
  93. .sample = slin8_sample,
  94. .buffer_samples = BUFFER_SAMPLES,
  95. .buf_size = BUFFER_SAMPLES,
  96. };
  97. static int unload_module(void)
  98. {
  99. int res;
  100. res = ast_unregister_translator(&lintoalaw);
  101. res |= ast_unregister_translator(&alawtolin);
  102. return res;
  103. }
  104. static int load_module(void)
  105. {
  106. int res;
  107. res = ast_register_translator(&alawtolin);
  108. res |= ast_register_translator(&lintoalaw);
  109. if (res) {
  110. unload_module();
  111. return AST_MODULE_LOAD_DECLINE;
  112. }
  113. return AST_MODULE_LOAD_SUCCESS;
  114. }
  115. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "A-law Coder/Decoder",
  116. .support_level = AST_MODULE_SUPPORT_CORE,
  117. .load = load_module,
  118. .unload = unload_module,
  119. );