codec_resample.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2011, Digium, Inc.
  5. *
  6. * Russell Bryant <russell@digium.com>
  7. * David Vossel <dvossel@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*!
  20. * \file
  21. *
  22. * \brief Resample slinear audio
  23. *
  24. * \ingroup codecs
  25. */
  26. /*** MODULEINFO
  27. <depend>resample</depend>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. #include "speex/speex_resampler.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "asterisk/module.h"
  34. #include "asterisk/translate.h"
  35. #include "asterisk/slin.h"
  36. #define OUTBUF_SIZE 8096
  37. static struct ast_translator *translators;
  38. static int trans_size;
  39. static int id_list[] = {
  40. AST_FORMAT_SLINEAR,
  41. AST_FORMAT_SLINEAR12,
  42. AST_FORMAT_SLINEAR16,
  43. AST_FORMAT_SLINEAR24,
  44. AST_FORMAT_SLINEAR32,
  45. AST_FORMAT_SLINEAR44,
  46. AST_FORMAT_SLINEAR48,
  47. AST_FORMAT_SLINEAR96,
  48. AST_FORMAT_SLINEAR192,
  49. };
  50. static int resamp_new(struct ast_trans_pvt *pvt)
  51. {
  52. int err;
  53. if (!(pvt->pvt = speex_resampler_init(1, ast_format_rate(&pvt->t->src_format), ast_format_rate(&pvt->t->dst_format), 5, &err))) {
  54. return -1;
  55. }
  56. return 0;
  57. }
  58. static void resamp_destroy(struct ast_trans_pvt *pvt)
  59. {
  60. SpeexResamplerState *resamp_pvt = pvt->pvt;
  61. speex_resampler_destroy(resamp_pvt);
  62. }
  63. static int resamp_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  64. {
  65. SpeexResamplerState *resamp_pvt = pvt->pvt;
  66. unsigned int out_samples = (OUTBUF_SIZE / sizeof(int16_t)) - pvt->samples;
  67. unsigned int in_samples;
  68. if (!f->datalen) {
  69. return -1;
  70. }
  71. in_samples = f->datalen / 2;
  72. speex_resampler_process_int(resamp_pvt,
  73. 0,
  74. f->data.ptr,
  75. &in_samples,
  76. pvt->outbuf.i16 + pvt->samples,
  77. &out_samples);
  78. pvt->samples += out_samples;
  79. pvt->datalen += out_samples * 2;
  80. return 0;
  81. }
  82. static int unload_module(void)
  83. {
  84. int res = 0;
  85. int idx;
  86. for (idx = 0; idx < trans_size; idx++) {
  87. res |= ast_unregister_translator(&translators[idx]);
  88. }
  89. ast_free(translators);
  90. return res;
  91. }
  92. static int load_module(void)
  93. {
  94. int res = 0;
  95. int x, y, idx = 0;
  96. trans_size = ARRAY_LEN(id_list) * ARRAY_LEN(id_list);
  97. if (!(translators = ast_calloc(1, sizeof(struct ast_translator) * trans_size))) {
  98. return AST_MODULE_LOAD_FAILURE;
  99. }
  100. for (x = 0; x < ARRAY_LEN(id_list); x++) {
  101. for (y = 0; y < ARRAY_LEN(id_list); y++) {
  102. if (x == y) {
  103. continue;
  104. }
  105. translators[idx].newpvt = resamp_new;
  106. translators[idx].destroy = resamp_destroy;
  107. translators[idx].framein = resamp_framein;
  108. translators[idx].desc_size = 0;
  109. translators[idx].buffer_samples = (OUTBUF_SIZE / sizeof(int16_t));
  110. translators[idx].buf_size = OUTBUF_SIZE;
  111. ast_format_set(&translators[idx].src_format, id_list[x], 0);
  112. ast_format_set(&translators[idx].dst_format, id_list[y], 0);
  113. snprintf(translators[idx].name, sizeof(translators[idx].name), "slin %dkhz -> %dkhz",
  114. ast_format_rate(&translators[idx].src_format), ast_format_rate(&translators[idx].dst_format));
  115. res |= ast_register_translator(&translators[idx]);
  116. idx++;
  117. }
  118. }
  119. return AST_MODULE_LOAD_SUCCESS;
  120. }
  121. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SLIN Resampling Codec");