codec_g722.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2008, Digium, Inc.
  5. *
  6. * Matthew Fredrickson <creslin@digium.com>
  7. * Russell Bryant <russell@digium.com>
  8. *
  9. * Special thanks to Steve Underwood for the implementation
  10. * and for doing the 8khz<->g.722 direct translation code.
  11. *
  12. * See http://www.asterisk.org for more information about
  13. * the Asterisk project. Please do not directly contact
  14. * any of the maintainers of this project for assistance;
  15. * the project provides a web site, mailing lists and IRC
  16. * channels for your use.
  17. *
  18. * This program is free software, distributed under the terms of
  19. * the GNU General Public License Version 2. See the LICENSE file
  20. * at the top of the source tree.
  21. */
  22. /*! \file
  23. *
  24. * \brief codec_g722.c - translate between signed linear and ITU G.722-64kbps
  25. *
  26. * \author Matthew Fredrickson <creslin@digium.com>
  27. * \author Russell Bryant <russell@digium.com>
  28. *
  29. * \arg http://soft-switch.org/downloads/non-gpl-bits.tgz
  30. * \arg http://lists.digium.com/pipermail/asterisk-dev/2006-September/022866.html
  31. *
  32. * \ingroup codecs
  33. */
  34. /*** MODULEINFO
  35. <support_level>core</support_level>
  36. ***/
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include "asterisk/linkedlists.h"
  40. #include "asterisk/module.h"
  41. #include "asterisk/config.h"
  42. #include "asterisk/translate.h"
  43. #include "asterisk/utils.h"
  44. #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
  45. #define BUF_SHIFT 5
  46. #include "g722/g722.h"
  47. /* Sample frame data */
  48. #include "asterisk/slin.h"
  49. #include "ex_g722.h"
  50. struct g722_encoder_pvt {
  51. g722_encode_state_t g722;
  52. };
  53. struct g722_decoder_pvt {
  54. g722_decode_state_t g722;
  55. };
  56. /*! \brief init a new instance of g722_encoder_pvt. */
  57. static int lintog722_new(struct ast_trans_pvt *pvt)
  58. {
  59. struct g722_encoder_pvt *tmp = pvt->pvt;
  60. g722_encode_init(&tmp->g722, 64000, G722_SAMPLE_RATE_8000);
  61. return 0;
  62. }
  63. static int lin16tog722_new(struct ast_trans_pvt *pvt)
  64. {
  65. struct g722_encoder_pvt *tmp = pvt->pvt;
  66. g722_encode_init(&tmp->g722, 64000, 0);
  67. return 0;
  68. }
  69. /*! \brief init a new instance of g722_encoder_pvt. */
  70. static int g722tolin_new(struct ast_trans_pvt *pvt)
  71. {
  72. struct g722_decoder_pvt *tmp = pvt->pvt;
  73. g722_decode_init(&tmp->g722, 64000, G722_SAMPLE_RATE_8000);
  74. return 0;
  75. }
  76. static int g722tolin16_new(struct ast_trans_pvt *pvt)
  77. {
  78. struct g722_decoder_pvt *tmp = pvt->pvt;
  79. g722_decode_init(&tmp->g722, 64000, 0);
  80. return 0;
  81. }
  82. static int g722tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  83. {
  84. struct g722_decoder_pvt *tmp = pvt->pvt;
  85. int out_samples;
  86. int in_samples;
  87. /* g722_decode expects the samples to be in the invalid samples / 2 format */
  88. in_samples = f->samples / 2;
  89. out_samples = g722_decode(&tmp->g722, &pvt->outbuf.i16[pvt->samples * sizeof(int16_t)],
  90. (uint8_t *) f->data.ptr, in_samples);
  91. pvt->samples += out_samples;
  92. pvt->datalen += (out_samples * sizeof(int16_t));
  93. return 0;
  94. }
  95. static int lintog722_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  96. {
  97. struct g722_encoder_pvt *tmp = pvt->pvt;
  98. int outlen;
  99. outlen = g722_encode(&tmp->g722, (&pvt->outbuf.ui8[pvt->datalen]),
  100. (int16_t *) f->data.ptr, f->samples);
  101. pvt->samples += outlen * 2;
  102. pvt->datalen += outlen;
  103. return 0;
  104. }
  105. static struct ast_translator g722tolin = {
  106. .name = "g722tolin",
  107. .srcfmt = AST_FORMAT_G722,
  108. .dstfmt = AST_FORMAT_SLINEAR,
  109. .newpvt = g722tolin_new, /* same for both directions */
  110. .framein = g722tolin_framein,
  111. .sample = g722_sample,
  112. .desc_size = sizeof(struct g722_decoder_pvt),
  113. .buffer_samples = BUFFER_SAMPLES / sizeof(int16_t),
  114. .buf_size = BUFFER_SAMPLES,
  115. };
  116. static struct ast_translator lintog722 = {
  117. .name = "lintog722",
  118. .srcfmt = AST_FORMAT_SLINEAR,
  119. .dstfmt = AST_FORMAT_G722,
  120. .newpvt = lintog722_new, /* same for both directions */
  121. .framein = lintog722_framein,
  122. .sample = slin8_sample,
  123. .desc_size = sizeof(struct g722_encoder_pvt),
  124. .buffer_samples = BUFFER_SAMPLES * 2,
  125. .buf_size = BUFFER_SAMPLES,
  126. };
  127. static struct ast_translator g722tolin16 = {
  128. .name = "g722tolin16",
  129. .srcfmt = AST_FORMAT_G722,
  130. .dstfmt = AST_FORMAT_SLINEAR16,
  131. .newpvt = g722tolin16_new, /* same for both directions */
  132. .framein = g722tolin_framein,
  133. .sample = g722_sample,
  134. .desc_size = sizeof(struct g722_decoder_pvt),
  135. .buffer_samples = BUFFER_SAMPLES / sizeof(int16_t),
  136. .buf_size = BUFFER_SAMPLES,
  137. };
  138. static struct ast_translator lin16tog722 = {
  139. .name = "lin16tog722",
  140. .srcfmt = AST_FORMAT_SLINEAR16,
  141. .dstfmt = AST_FORMAT_G722,
  142. .newpvt = lin16tog722_new, /* same for both directions */
  143. .framein = lintog722_framein,
  144. .sample = slin16_sample,
  145. .desc_size = sizeof(struct g722_encoder_pvt),
  146. .buffer_samples = BUFFER_SAMPLES * 2,
  147. .buf_size = BUFFER_SAMPLES,
  148. };
  149. static int reload(void)
  150. {
  151. return AST_MODULE_LOAD_SUCCESS;
  152. }
  153. static int unload_module(void)
  154. {
  155. int res = 0;
  156. res |= ast_unregister_translator(&g722tolin);
  157. res |= ast_unregister_translator(&lintog722);
  158. res |= ast_unregister_translator(&g722tolin16);
  159. res |= ast_unregister_translator(&lin16tog722);
  160. return res;
  161. }
  162. static int load_module(void)
  163. {
  164. int res = 0;
  165. res |= ast_register_translator(&g722tolin);
  166. res |= ast_register_translator(&lintog722);
  167. res |= ast_register_translator(&g722tolin16);
  168. res |= ast_register_translator(&lin16tog722);
  169. if (res) {
  170. unload_module();
  171. return AST_MODULE_LOAD_FAILURE;
  172. }
  173. return AST_MODULE_LOAD_SUCCESS;
  174. }
  175. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ITU G.722-64kbps G722 Transcoder",
  176. .load = load_module,
  177. .unload = unload_module,
  178. .reload = reload,
  179. );