codec_ilbc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * The iLBC code is from The IETF code base and is copyright The Internet Society (2004)
  5. *
  6. * Copyright (C) 1999 - 2005, Digium, Inc.
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*! \file
  21. *
  22. * \brief Translate between signed linear and Internet Low Bitrate Codec
  23. *
  24. * \ingroup codecs
  25. */
  26. /*** MODULEINFO
  27. <use>ilbc</use>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/translate.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/utils.h"
  35. #include "asterisk/linkedlists.h"
  36. #ifdef ILBC_WEBRTC
  37. #include <ilbc.h>
  38. typedef uint16_t ilbc_bytes;
  39. typedef int16_t ilbc_block;
  40. #define BUF_TYPE i16
  41. #else
  42. #include "ilbc/iLBC_encode.h"
  43. #include "ilbc/iLBC_decode.h"
  44. typedef unsigned char ilbc_bytes;
  45. typedef float ilbc_block;
  46. #define BUF_TYPE uc
  47. #endif
  48. #define USE_ILBC_ENHANCER 0
  49. #define ILBC_MS 30
  50. /* #define ILBC_MS 20 */
  51. #define ILBC_FRAME_LEN 50 /* apparently... */
  52. #define ILBC_SAMPLES 240 /* 30ms at 8000 hz */
  53. #define BUFFER_SAMPLES 8000
  54. /* Sample frame data */
  55. #include "asterisk/slin.h"
  56. #include "ex_ilbc.h"
  57. struct ilbc_coder_pvt {
  58. iLBC_Enc_Inst_t enc;
  59. iLBC_Dec_Inst_t dec;
  60. /* Enough to store a full second */
  61. int16_t buf[BUFFER_SAMPLES];
  62. };
  63. static int lintoilbc_new(struct ast_trans_pvt *pvt)
  64. {
  65. struct ilbc_coder_pvt *tmp = pvt->pvt;
  66. initEncode(&tmp->enc, ILBC_MS);
  67. return 0;
  68. }
  69. static int ilbctolin_new(struct ast_trans_pvt *pvt)
  70. {
  71. struct ilbc_coder_pvt *tmp = pvt->pvt;
  72. initDecode(&tmp->dec, ILBC_MS, USE_ILBC_ENHANCER);
  73. return 0;
  74. }
  75. /*! \brief decode a frame and store in outbuf */
  76. static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  77. {
  78. struct ilbc_coder_pvt *tmp = pvt->pvt;
  79. int plc_mode = 1; /* 1 = normal data, 0 = plc */
  80. /* Assuming there's space left, decode into the current buffer at
  81. the tail location. Read in as many frames as there are */
  82. int x,i;
  83. int datalen = f->datalen;
  84. int16_t *dst = pvt->outbuf.i16;
  85. ilbc_block tmpf[ILBC_SAMPLES];
  86. if (!f->data.ptr && datalen) {
  87. ast_debug(1, "issue 16070, ILIB ERROR. data = NULL datalen = %d src = %s\n", datalen, f->src ? f->src : "no src set");
  88. f->datalen = 0;
  89. datalen = 0;
  90. }
  91. if (datalen == 0) { /* native PLC, set fake datalen and clear plc_mode */
  92. datalen = ILBC_FRAME_LEN;
  93. f->samples = ILBC_SAMPLES;
  94. plc_mode = 0; /* do native plc */
  95. pvt->samples += ILBC_SAMPLES;
  96. }
  97. if (datalen % ILBC_FRAME_LEN) {
  98. ast_log(LOG_WARNING, "Huh? An ilbc frame that isn't a multiple of 50 bytes long from %s (%d)?\n", f->src, datalen);
  99. return -1;
  100. }
  101. for (x=0; x < datalen ; x += ILBC_FRAME_LEN) {
  102. if (pvt->samples + ILBC_SAMPLES > BUFFER_SAMPLES) {
  103. ast_log(LOG_WARNING, "Out of buffer space\n");
  104. return -1;
  105. }
  106. iLBC_decode(tmpf, plc_mode ? f->data.ptr + x : NULL, &tmp->dec, plc_mode);
  107. for ( i=0; i < ILBC_SAMPLES; i++)
  108. dst[pvt->samples + i] = tmpf[i];
  109. pvt->samples += ILBC_SAMPLES;
  110. pvt->datalen += 2*ILBC_SAMPLES;
  111. }
  112. return 0;
  113. }
  114. /*! \brief store a frame into a temporary buffer, for later decoding */
  115. static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  116. {
  117. struct ilbc_coder_pvt *tmp = pvt->pvt;
  118. /* Just add the frames to our stream */
  119. /* XXX We should look at how old the rest of our stream is, and if it
  120. is too old, then we should overwrite it entirely, otherwise we can
  121. get artifacts of earlier talk that do not belong */
  122. memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
  123. pvt->samples += f->samples;
  124. return 0;
  125. }
  126. /*! \brief encode the temporary buffer and generate a frame */
  127. static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt)
  128. {
  129. struct ilbc_coder_pvt *tmp = pvt->pvt;
  130. struct ast_frame *result = NULL;
  131. struct ast_frame *last = NULL;
  132. int samples = 0; /* output samples */
  133. while (pvt->samples >= ILBC_SAMPLES) {
  134. struct ast_frame *current;
  135. ilbc_block tmpf[ILBC_SAMPLES];
  136. int i;
  137. /* Encode a frame of data */
  138. for (i = 0 ; i < ILBC_SAMPLES ; i++)
  139. tmpf[i] = tmp->buf[samples + i];
  140. iLBC_encode((ilbc_bytes *) pvt->outbuf.BUF_TYPE, tmpf, &tmp->enc);
  141. samples += ILBC_SAMPLES;
  142. pvt->samples -= ILBC_SAMPLES;
  143. current = ast_trans_frameout(pvt, ILBC_FRAME_LEN, ILBC_SAMPLES);
  144. if (!current) {
  145. continue;
  146. } else if (last) {
  147. AST_LIST_NEXT(last, frame_list) = current;
  148. } else {
  149. result = current;
  150. }
  151. last = current;
  152. }
  153. /* Move the data at the end of the buffer to the front */
  154. if (samples) {
  155. memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
  156. }
  157. return result;
  158. }
  159. static struct ast_translator ilbctolin = {
  160. .name = "ilbctolin",
  161. .src_codec = {
  162. .name = "ilbc",
  163. .type = AST_MEDIA_TYPE_AUDIO,
  164. .sample_rate = 8000,
  165. },
  166. .dst_codec = {
  167. .name = "slin",
  168. .type = AST_MEDIA_TYPE_AUDIO,
  169. .sample_rate = 8000,
  170. },
  171. .format = "slin",
  172. .newpvt = ilbctolin_new,
  173. .framein = ilbctolin_framein,
  174. .sample = ilbc_sample,
  175. .desc_size = sizeof(struct ilbc_coder_pvt),
  176. .buf_size = BUFFER_SAMPLES * 2,
  177. .native_plc = 1,
  178. };
  179. static struct ast_translator lintoilbc = {
  180. .name = "lintoilbc",
  181. .src_codec = {
  182. .name = "slin",
  183. .type = AST_MEDIA_TYPE_AUDIO,
  184. .sample_rate = 8000,
  185. },
  186. .dst_codec = {
  187. .name = "ilbc",
  188. .type = AST_MEDIA_TYPE_AUDIO,
  189. .sample_rate = 8000,
  190. },
  191. .format = "ilbc",
  192. .newpvt = lintoilbc_new,
  193. .framein = lintoilbc_framein,
  194. .frameout = lintoilbc_frameout,
  195. .sample = slin8_sample,
  196. .desc_size = sizeof(struct ilbc_coder_pvt),
  197. .buf_size = (BUFFER_SAMPLES * ILBC_FRAME_LEN + ILBC_SAMPLES - 1) / ILBC_SAMPLES,
  198. };
  199. static int unload_module(void)
  200. {
  201. int res;
  202. res = ast_unregister_translator(&lintoilbc);
  203. res |= ast_unregister_translator(&ilbctolin);
  204. return res;
  205. }
  206. static int load_module(void)
  207. {
  208. int res;
  209. res = ast_register_translator(&ilbctolin);
  210. res |= ast_register_translator(&lintoilbc);
  211. if (res) {
  212. unload_module();
  213. return AST_MODULE_LOAD_DECLINE;
  214. }
  215. return AST_MODULE_LOAD_SUCCESS;
  216. }
  217. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "iLBC Coder/Decoder");