codec_g722.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. .src_codec = {
  108. .name = "g722",
  109. .type = AST_MEDIA_TYPE_AUDIO,
  110. .sample_rate = 16000,
  111. },
  112. .dst_codec = {
  113. .name = "slin",
  114. .type = AST_MEDIA_TYPE_AUDIO,
  115. .sample_rate = 8000,
  116. },
  117. .format = "slin",
  118. .newpvt = g722tolin_new, /* same for both directions */
  119. .framein = g722tolin_framein,
  120. .sample = g722_sample,
  121. .desc_size = sizeof(struct g722_decoder_pvt),
  122. .buffer_samples = BUFFER_SAMPLES / sizeof(int16_t),
  123. .buf_size = BUFFER_SAMPLES,
  124. };
  125. static struct ast_translator lintog722 = {
  126. .name = "lintog722",
  127. .src_codec = {
  128. .name = "slin",
  129. .type = AST_MEDIA_TYPE_AUDIO,
  130. .sample_rate = 8000,
  131. },
  132. .dst_codec = {
  133. .name = "g722",
  134. .type = AST_MEDIA_TYPE_AUDIO,
  135. .sample_rate = 16000,
  136. },
  137. .format = "g722",
  138. .newpvt = lintog722_new, /* same for both directions */
  139. .framein = lintog722_framein,
  140. .sample = slin8_sample,
  141. .desc_size = sizeof(struct g722_encoder_pvt),
  142. .buffer_samples = BUFFER_SAMPLES * 2,
  143. .buf_size = BUFFER_SAMPLES,
  144. };
  145. static struct ast_translator g722tolin16 = {
  146. .name = "g722tolin16",
  147. .src_codec = {
  148. .name = "g722",
  149. .type = AST_MEDIA_TYPE_AUDIO,
  150. .sample_rate = 16000,
  151. },
  152. .dst_codec = {
  153. .name = "slin",
  154. .type = AST_MEDIA_TYPE_AUDIO,
  155. .sample_rate = 16000,
  156. },
  157. .format = "slin16",
  158. .newpvt = g722tolin16_new, /* same for both directions */
  159. .framein = g722tolin_framein,
  160. .sample = g722_sample,
  161. .desc_size = sizeof(struct g722_decoder_pvt),
  162. .buffer_samples = BUFFER_SAMPLES / sizeof(int16_t),
  163. .buf_size = BUFFER_SAMPLES,
  164. };
  165. static struct ast_translator lin16tog722 = {
  166. .name = "lin16tog722",
  167. .src_codec = {
  168. .name = "slin",
  169. .type = AST_MEDIA_TYPE_AUDIO,
  170. .sample_rate = 16000,
  171. },
  172. .dst_codec = {
  173. .name = "g722",
  174. .type = AST_MEDIA_TYPE_AUDIO,
  175. .sample_rate = 16000,
  176. },
  177. .format = "g722",
  178. .newpvt = lin16tog722_new, /* same for both directions */
  179. .framein = lintog722_framein,
  180. .sample = slin16_sample,
  181. .desc_size = sizeof(struct g722_encoder_pvt),
  182. .buffer_samples = BUFFER_SAMPLES * 2,
  183. .buf_size = BUFFER_SAMPLES,
  184. };
  185. static int unload_module(void)
  186. {
  187. int res = 0;
  188. res |= ast_unregister_translator(&g722tolin);
  189. res |= ast_unregister_translator(&lintog722);
  190. res |= ast_unregister_translator(&g722tolin16);
  191. res |= ast_unregister_translator(&lin16tog722);
  192. return res;
  193. }
  194. static int load_module(void)
  195. {
  196. int res = 0;
  197. res |= ast_register_translator(&g722tolin);
  198. res |= ast_register_translator(&lintog722);
  199. res |= ast_register_translator(&g722tolin16);
  200. res |= ast_register_translator(&lin16tog722);
  201. if (res) {
  202. unload_module();
  203. return AST_MODULE_LOAD_DECLINE;
  204. }
  205. return AST_MODULE_LOAD_SUCCESS;
  206. }
  207. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ITU G.722-64kbps G722 Transcoder",
  208. .support_level = AST_MODULE_SUPPORT_CORE,
  209. .load = load_module,
  210. .unload = unload_module,
  211. );