codec_gsm.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * The GSM code is from TOAST. Copyright information for that package is available
  5. * in the GSM directory.
  6. *
  7. * Copyright (C) 1999 - 2005, Digium, Inc.
  8. *
  9. * Mark Spencer <markster@digium.com>
  10. *
  11. * See http://www.asterisk.org for more information about
  12. * the Asterisk project. Please do not directly contact
  13. * any of the maintainers of this project for assistance;
  14. * the project provides a web site, mailing lists and IRC
  15. * channels for your use.
  16. *
  17. * This program is free software, distributed under the terms of
  18. * the GNU General Public License Version 2. See the LICENSE file
  19. * at the top of the source tree.
  20. */
  21. /*! \file
  22. *
  23. * \brief Translate between signed linear and Global System for Mobile Communications (GSM)
  24. *
  25. * \ingroup codecs
  26. */
  27. /*** MODULEINFO
  28. <depend>gsm</depend>
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include "asterisk/translate.h"
  34. #include "asterisk/config.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/utils.h"
  37. #include "asterisk/linkedlists.h"
  38. #ifdef HAVE_GSM_HEADER
  39. #include "gsm.h"
  40. #elif defined(HAVE_GSM_GSM_HEADER)
  41. #include <gsm/gsm.h>
  42. #endif
  43. #include "../formats/msgsm.h"
  44. #define BUFFER_SAMPLES 8000
  45. #define GSM_SAMPLES 160
  46. #define GSM_FRAME_LEN 33
  47. #define MSGSM_FRAME_LEN 65
  48. /* Sample frame data */
  49. #include "asterisk/slin.h"
  50. #include "ex_gsm.h"
  51. struct gsm_translator_pvt { /* both gsm2lin and lin2gsm */
  52. gsm gsm;
  53. int16_t buf[BUFFER_SAMPLES]; /* lin2gsm, temporary storage */
  54. };
  55. static int gsm_new(struct ast_trans_pvt *pvt)
  56. {
  57. struct gsm_translator_pvt *tmp = pvt->pvt;
  58. return (tmp->gsm = gsm_create()) ? 0 : -1;
  59. }
  60. /*! \brief decode and store in outbuf. */
  61. static int gsmtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  62. {
  63. struct gsm_translator_pvt *tmp = pvt->pvt;
  64. int x;
  65. int16_t *dst = pvt->outbuf.i16;
  66. /* guess format from frame len. 65 for MSGSM, 33 for regular GSM */
  67. int flen = (f->datalen % MSGSM_FRAME_LEN == 0) ?
  68. MSGSM_FRAME_LEN : GSM_FRAME_LEN;
  69. for (x=0; x < f->datalen; x += flen) {
  70. unsigned char data[2 * GSM_FRAME_LEN];
  71. unsigned char *src;
  72. int len;
  73. if (flen == MSGSM_FRAME_LEN) {
  74. len = 2*GSM_SAMPLES;
  75. src = data;
  76. /* Translate MSGSM format to Real GSM format before feeding in */
  77. /* XXX what's the point here! we should just work
  78. * on the full format.
  79. */
  80. conv65(f->data.ptr + x, data);
  81. } else {
  82. len = GSM_SAMPLES;
  83. src = f->data.ptr + x;
  84. }
  85. /* XXX maybe we don't need to check */
  86. if (pvt->samples + len > BUFFER_SAMPLES) {
  87. ast_log(LOG_WARNING, "Out of buffer space\n");
  88. return -1;
  89. }
  90. if (gsm_decode(tmp->gsm, src, dst + pvt->samples)) {
  91. ast_log(LOG_WARNING, "Invalid GSM data (1)\n");
  92. return -1;
  93. }
  94. pvt->samples += GSM_SAMPLES;
  95. pvt->datalen += 2 * GSM_SAMPLES;
  96. if (flen == MSGSM_FRAME_LEN) {
  97. if (gsm_decode(tmp->gsm, data + GSM_FRAME_LEN, dst + pvt->samples)) {
  98. ast_log(LOG_WARNING, "Invalid GSM data (2)\n");
  99. return -1;
  100. }
  101. pvt->samples += GSM_SAMPLES;
  102. pvt->datalen += 2 * GSM_SAMPLES;
  103. }
  104. }
  105. return 0;
  106. }
  107. /*! \brief store samples into working buffer for later decode */
  108. static int lintogsm_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  109. {
  110. struct gsm_translator_pvt *tmp = pvt->pvt;
  111. /* XXX We should look at how old the rest of our stream is, and if it
  112. is too old, then we should overwrite it entirely, otherwise we can
  113. get artifacts of earlier talk that do not belong */
  114. if (pvt->samples + f->samples > BUFFER_SAMPLES) {
  115. ast_log(LOG_WARNING, "Out of buffer space\n");
  116. return -1;
  117. }
  118. memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
  119. pvt->samples += f->samples;
  120. return 0;
  121. }
  122. /*! \brief encode and produce a frame */
  123. static struct ast_frame *lintogsm_frameout(struct ast_trans_pvt *pvt)
  124. {
  125. struct gsm_translator_pvt *tmp = pvt->pvt;
  126. struct ast_frame *result = NULL;
  127. struct ast_frame *last = NULL;
  128. int samples = 0; /* output samples */
  129. while (pvt->samples >= GSM_SAMPLES) {
  130. struct ast_frame *current;
  131. /* Encode a frame of data */
  132. gsm_encode(tmp->gsm, tmp->buf + samples, (gsm_byte *) pvt->outbuf.c);
  133. samples += GSM_SAMPLES;
  134. pvt->samples -= GSM_SAMPLES;
  135. current = ast_trans_frameout(pvt, GSM_FRAME_LEN, GSM_SAMPLES);
  136. if (!current) {
  137. continue;
  138. } else if (last) {
  139. AST_LIST_NEXT(last, frame_list) = current;
  140. } else {
  141. result = current;
  142. }
  143. last = current;
  144. }
  145. /* Move the data at the end of the buffer to the front */
  146. if (samples) {
  147. memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
  148. }
  149. return result;
  150. }
  151. static void gsm_destroy_stuff(struct ast_trans_pvt *pvt)
  152. {
  153. struct gsm_translator_pvt *tmp = pvt->pvt;
  154. if (tmp->gsm)
  155. gsm_destroy(tmp->gsm);
  156. }
  157. static struct ast_translator gsmtolin = {
  158. .name = "gsmtolin",
  159. .src_codec = {
  160. .name = "gsm",
  161. .type = AST_MEDIA_TYPE_AUDIO,
  162. .sample_rate = 8000,
  163. },
  164. .dst_codec = {
  165. .name = "slin",
  166. .type = AST_MEDIA_TYPE_AUDIO,
  167. .sample_rate = 8000,
  168. },
  169. .format = "slin",
  170. .newpvt = gsm_new,
  171. .framein = gsmtolin_framein,
  172. .destroy = gsm_destroy_stuff,
  173. .sample = gsm_sample,
  174. .buffer_samples = BUFFER_SAMPLES,
  175. .buf_size = BUFFER_SAMPLES * 2,
  176. .desc_size = sizeof (struct gsm_translator_pvt ),
  177. };
  178. static struct ast_translator lintogsm = {
  179. .name = "lintogsm",
  180. .src_codec = {
  181. .name = "slin",
  182. .type = AST_MEDIA_TYPE_AUDIO,
  183. .sample_rate = 8000,
  184. },
  185. .dst_codec = {
  186. .name = "gsm",
  187. .type = AST_MEDIA_TYPE_AUDIO,
  188. .sample_rate = 8000,
  189. },
  190. .format = "gsm",
  191. .newpvt = gsm_new,
  192. .framein = lintogsm_framein,
  193. .frameout = lintogsm_frameout,
  194. .destroy = gsm_destroy_stuff,
  195. .sample = slin8_sample,
  196. .desc_size = sizeof (struct gsm_translator_pvt ),
  197. .buf_size = (BUFFER_SAMPLES * GSM_FRAME_LEN + GSM_SAMPLES - 1)/GSM_SAMPLES,
  198. };
  199. static int unload_module(void)
  200. {
  201. int res;
  202. res = ast_unregister_translator(&lintogsm);
  203. res |= ast_unregister_translator(&gsmtolin);
  204. return res;
  205. }
  206. static int load_module(void)
  207. {
  208. int res;
  209. res = ast_register_translator(&gsmtolin);
  210. res |= ast_register_translator(&lintogsm);
  211. if (res) {
  212. unload_module();
  213. return AST_MODULE_LOAD_DECLINE;
  214. }
  215. return AST_MODULE_LOAD_SUCCESS;
  216. }
  217. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "GSM Coder/Decoder",
  218. .support_level = AST_MODULE_SUPPORT_CORE,
  219. .load = load_module,
  220. .unload = unload_module,
  221. );