codec_codec2.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2016, Alexander Traud
  5. *
  6. * Alexander Traud <pabstraud@compuserve.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 Translate between signed linear and Codec 2
  21. *
  22. * \author Alexander Traud <pabstraud@compuserve.com>
  23. *
  24. * \note http://www.rowetel.com/codec2.html
  25. *
  26. * \ingroup codecs
  27. */
  28. /*** MODULEINFO
  29. <depend>codec2</depend>
  30. <support_level>core</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. #include "asterisk/codec.h" /* for AST_MEDIA_TYPE_AUDIO */
  34. #include "asterisk/frame.h" /* for ast_frame */
  35. #include "asterisk/linkedlists.h" /* for AST_LIST_NEXT, etc */
  36. #include "asterisk/logger.h" /* for ast_log, etc */
  37. #include "asterisk/module.h"
  38. #include "asterisk/rtp_engine.h" /* ast_rtp_engine_(un)load_format */
  39. #include "asterisk/translate.h" /* for ast_trans_pvt, etc */
  40. #include <codec2/codec2.h>
  41. #define BUFFER_SAMPLES 8000
  42. #define CODEC2_SAMPLES 160 /* consider codec2_samples_per_frame(.) */
  43. #define CODEC2_FRAME_LEN 6 /* consider codec2_bits_per_frame(.) */
  44. /* Sample frame data */
  45. #include "asterisk/slin.h"
  46. #include "ex_codec2.h"
  47. struct codec2_translator_pvt {
  48. struct CODEC2 *state; /* May be encoder or decoder */
  49. int16_t buf[BUFFER_SAMPLES];
  50. };
  51. static int codec2_new(struct ast_trans_pvt *pvt)
  52. {
  53. struct codec2_translator_pvt *tmp = pvt->pvt;
  54. tmp->state = codec2_create(CODEC2_MODE_2400);
  55. if (!tmp->state) {
  56. ast_log(LOG_ERROR, "Error creating Codec 2 conversion\n");
  57. return -1;
  58. }
  59. return 0;
  60. }
  61. /*! \brief decode and store in outbuf. */
  62. static int codec2tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  63. {
  64. struct codec2_translator_pvt *tmp = pvt->pvt;
  65. int x;
  66. for (x = 0; x < f->datalen; x += CODEC2_FRAME_LEN) {
  67. unsigned char *src = f->data.ptr + x;
  68. int16_t *dst = pvt->outbuf.i16 + pvt->samples;
  69. codec2_decode(tmp->state, dst, src);
  70. pvt->samples += CODEC2_SAMPLES;
  71. pvt->datalen += CODEC2_SAMPLES * 2;
  72. }
  73. return 0;
  74. }
  75. /*! \brief store samples into working buffer for later decode */
  76. static int lintocodec2_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  77. {
  78. struct codec2_translator_pvt *tmp = pvt->pvt;
  79. memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
  80. pvt->samples += f->samples;
  81. return 0;
  82. }
  83. /*! \brief encode and produce a frame */
  84. static struct ast_frame *lintocodec2_frameout(struct ast_trans_pvt *pvt)
  85. {
  86. struct codec2_translator_pvt *tmp = pvt->pvt;
  87. struct ast_frame *result = NULL;
  88. struct ast_frame *last = NULL;
  89. int samples = 0; /* output samples */
  90. while (pvt->samples >= CODEC2_SAMPLES) {
  91. struct ast_frame *current;
  92. /* Encode a frame of data */
  93. codec2_encode(tmp->state, pvt->outbuf.uc, tmp->buf + samples);
  94. samples += CODEC2_SAMPLES;
  95. pvt->samples -= CODEC2_SAMPLES;
  96. current = ast_trans_frameout(pvt, CODEC2_FRAME_LEN, CODEC2_SAMPLES);
  97. if (!current) {
  98. continue;
  99. } else if (last) {
  100. AST_LIST_NEXT(last, frame_list) = current;
  101. } else {
  102. result = current;
  103. }
  104. last = current;
  105. }
  106. /* Move the data at the end of the buffer to the front */
  107. if (samples) {
  108. memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
  109. }
  110. return result;
  111. }
  112. static void codec2_destroy_stuff(struct ast_trans_pvt *pvt)
  113. {
  114. struct codec2_translator_pvt *tmp = pvt->pvt;
  115. if (tmp->state) {
  116. codec2_destroy(tmp->state);
  117. }
  118. }
  119. static struct ast_translator codec2tolin = {
  120. .name = "codec2tolin",
  121. .src_codec = {
  122. .name = "codec2",
  123. .type = AST_MEDIA_TYPE_AUDIO,
  124. .sample_rate = 8000,
  125. },
  126. .dst_codec = {
  127. .name = "slin",
  128. .type = AST_MEDIA_TYPE_AUDIO,
  129. .sample_rate = 8000,
  130. },
  131. .format = "slin",
  132. .newpvt = codec2_new,
  133. .framein = codec2tolin_framein,
  134. .destroy = codec2_destroy_stuff,
  135. .sample = codec2_sample,
  136. .desc_size = sizeof(struct codec2_translator_pvt),
  137. .buffer_samples = BUFFER_SAMPLES,
  138. .buf_size = BUFFER_SAMPLES * 2,
  139. };
  140. static struct ast_translator lintocodec2 = {
  141. .name = "lintocodec2",
  142. .src_codec = {
  143. .name = "slin",
  144. .type = AST_MEDIA_TYPE_AUDIO,
  145. .sample_rate = 8000,
  146. },
  147. .dst_codec = {
  148. .name = "codec2",
  149. .type = AST_MEDIA_TYPE_AUDIO,
  150. .sample_rate = 8000,
  151. },
  152. .format = "codec2",
  153. .newpvt = codec2_new,
  154. .framein = lintocodec2_framein,
  155. .frameout = lintocodec2_frameout,
  156. .destroy = codec2_destroy_stuff,
  157. .sample = slin8_sample,
  158. .desc_size = sizeof(struct codec2_translator_pvt),
  159. .buffer_samples = BUFFER_SAMPLES,
  160. .buf_size = (BUFFER_SAMPLES * CODEC2_FRAME_LEN + CODEC2_SAMPLES - 1) / CODEC2_SAMPLES,
  161. };
  162. static int unload_module(void)
  163. {
  164. int res = 0;
  165. res |= ast_rtp_engine_unload_format(ast_format_codec2);
  166. res |= ast_unregister_translator(&lintocodec2);
  167. res |= ast_unregister_translator(&codec2tolin);
  168. return res;
  169. }
  170. static int load_module(void)
  171. {
  172. int res = 0;
  173. res |= ast_register_translator(&codec2tolin);
  174. res |= ast_register_translator(&lintocodec2);
  175. res |= ast_rtp_engine_load_format(ast_format_codec2);
  176. if (res) {
  177. unload_module();
  178. return AST_MODULE_LOAD_DECLINE;
  179. }
  180. return AST_MODULE_LOAD_SUCCESS;
  181. }
  182. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Codec 2 Coder/Decoder");