codec_lpc10.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * The lpc10 code is from a library used by nautilus, modified to be a bit
  9. * nicer to the compiler.
  10. * See http://www.arl.wustl.edu/~jaf/
  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 Translate between signed linear and LPC10 (Linear Predictor Code)
  25. *
  26. * \ingroup codecs
  27. */
  28. /*** MODULEINFO
  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. #include "lpc10/lpc10.h"
  39. /* Sample frame data */
  40. #include "asterisk/slin.h"
  41. #include "ex_lpc10.h"
  42. /* We use a very strange format here... I have no idea why... The frames are 180
  43. samples long, which isn't even an even number of milliseconds... Not only that
  44. but we hvae to waste two bits of each frame to keep them ending on a byte boundary
  45. because the frames are 54 bits long */
  46. #define LPC10_BYTES_IN_COMPRESSED_FRAME (LPC10_BITS_IN_COMPRESSED_FRAME + 7)/8
  47. #define BUFFER_SAMPLES 8000
  48. struct lpc10_coder_pvt {
  49. union {
  50. struct lpc10_encoder_state *enc;
  51. struct lpc10_decoder_state *dec;
  52. } lpc10;
  53. /* Enough to store a full second */
  54. short buf[BUFFER_SAMPLES];
  55. int longer;
  56. };
  57. static int lpc10_enc_new(struct ast_trans_pvt *pvt)
  58. {
  59. struct lpc10_coder_pvt *tmp = pvt->pvt;
  60. return (tmp->lpc10.enc = create_lpc10_encoder_state()) ? 0 : -1;
  61. }
  62. static int lpc10_dec_new(struct ast_trans_pvt *pvt)
  63. {
  64. struct lpc10_coder_pvt *tmp = pvt->pvt;
  65. return (tmp->lpc10.dec = create_lpc10_decoder_state()) ? 0 : -1;
  66. }
  67. static void extract_bits(INT32 *bits, unsigned char *c)
  68. {
  69. int x;
  70. for (x=0;x<LPC10_BITS_IN_COMPRESSED_FRAME;x++) {
  71. if (*c & (0x80 >> (x & 7)))
  72. bits[x] = 1;
  73. else
  74. bits[x] = 0;
  75. if ((x & 7) == 7)
  76. c++;
  77. }
  78. }
  79. /* XXX note lpc10_encode() produces one bit per word in bits[] */
  80. static void build_bits(unsigned char *c, INT32 *bits)
  81. {
  82. unsigned char mask=0x80;
  83. int x;
  84. *c = 0;
  85. for (x=0;x<LPC10_BITS_IN_COMPRESSED_FRAME;x++) {
  86. if (bits[x])
  87. *c |= mask;
  88. mask = mask >> 1;
  89. if ((x % 8)==7) {
  90. c++;
  91. *c = 0;
  92. mask = 0x80;
  93. }
  94. }
  95. }
  96. static int lpc10tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  97. {
  98. struct lpc10_coder_pvt *tmp = pvt->pvt;
  99. int16_t *dst = pvt->outbuf.i16;
  100. int len = 0;
  101. while (len + LPC10_BYTES_IN_COMPRESSED_FRAME <= f->datalen) {
  102. int x;
  103. float tmpbuf[LPC10_SAMPLES_PER_FRAME];
  104. INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME]; /* XXX see note */
  105. if (pvt->samples + LPC10_SAMPLES_PER_FRAME > BUFFER_SAMPLES) {
  106. ast_log(LOG_WARNING, "Out of buffer space\n");
  107. return -1;
  108. }
  109. extract_bits(bits, f->data.ptr + len);
  110. if (lpc10_decode(bits, tmpbuf, tmp->lpc10.dec)) {
  111. ast_log(LOG_WARNING, "Invalid lpc10 data\n");
  112. return -1;
  113. }
  114. for (x=0;x<LPC10_SAMPLES_PER_FRAME;x++) {
  115. /* Convert to a short between -1.0 and 1.0 */
  116. dst[pvt->samples + x] = (int16_t)(32768.0 * tmpbuf[x]);
  117. }
  118. pvt->samples += LPC10_SAMPLES_PER_FRAME;
  119. pvt->datalen += 2*LPC10_SAMPLES_PER_FRAME;
  120. len += LPC10_BYTES_IN_COMPRESSED_FRAME;
  121. }
  122. if (len != f->datalen)
  123. printf("Decoded %d, expected %d\n", len, f->datalen);
  124. return 0;
  125. }
  126. static int lintolpc10_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  127. {
  128. struct lpc10_coder_pvt *tmp = pvt->pvt;
  129. /* Just add the frames to our stream */
  130. if (pvt->samples + f->samples > BUFFER_SAMPLES) {
  131. ast_log(LOG_WARNING, "Out of buffer space\n");
  132. return -1;
  133. }
  134. memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
  135. pvt->samples += f->samples;
  136. return 0;
  137. }
  138. static struct ast_frame *lintolpc10_frameout(struct ast_trans_pvt *pvt)
  139. {
  140. struct lpc10_coder_pvt *tmp = pvt->pvt;
  141. struct ast_frame *result = NULL;
  142. struct ast_frame *last = NULL;
  143. int samples = 0; /* output samples */
  144. while (pvt->samples >= LPC10_SAMPLES_PER_FRAME) {
  145. struct ast_frame *current;
  146. float tmpbuf[LPC10_SAMPLES_PER_FRAME];
  147. INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME]; /* XXX what ??? */
  148. int x;
  149. /* Encode a frame of data */
  150. for (x=0;x<LPC10_SAMPLES_PER_FRAME;x++)
  151. tmpbuf[x] = (float)tmp->buf[x + samples] / 32768.0;
  152. lpc10_encode(tmpbuf, bits, tmp->lpc10.enc);
  153. build_bits(pvt->outbuf.uc, bits);
  154. samples += LPC10_SAMPLES_PER_FRAME;
  155. pvt->samples -= LPC10_SAMPLES_PER_FRAME;
  156. /* Use one of the two left over bits to record if this is a 22 or 23 ms frame...
  157. important for IAX use */
  158. tmp->longer = 1 - tmp->longer;
  159. current = ast_trans_frameout(pvt, LPC10_BYTES_IN_COMPRESSED_FRAME, LPC10_SAMPLES_PER_FRAME);
  160. if (!current) {
  161. continue;
  162. } else if (last) {
  163. AST_LIST_NEXT(last, frame_list) = current;
  164. } else {
  165. result = current;
  166. }
  167. last = current;
  168. }
  169. /* Move the data at the end of the buffer to the front */
  170. if (samples) {
  171. memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
  172. }
  173. return result;
  174. }
  175. static void lpc10_destroy(struct ast_trans_pvt *arg)
  176. {
  177. struct lpc10_coder_pvt *pvt = arg->pvt;
  178. /* Enc and DEC are both just allocated, so they can be freed */
  179. ast_free(pvt->lpc10.enc);
  180. }
  181. static struct ast_translator lpc10tolin = {
  182. .name = "lpc10tolin",
  183. .src_codec = {
  184. .name = "lpc10",
  185. .type = AST_MEDIA_TYPE_AUDIO,
  186. .sample_rate = 8000,
  187. },
  188. .dst_codec = {
  189. .name = "slin",
  190. .type = AST_MEDIA_TYPE_AUDIO,
  191. .sample_rate = 8000,
  192. },
  193. .format = "slin",
  194. .newpvt = lpc10_dec_new,
  195. .framein = lpc10tolin_framein,
  196. .destroy = lpc10_destroy,
  197. .sample = lpc10_sample,
  198. .desc_size = sizeof(struct lpc10_coder_pvt),
  199. .buffer_samples = BUFFER_SAMPLES,
  200. .buf_size = BUFFER_SAMPLES * 2,
  201. };
  202. static struct ast_translator lintolpc10 = {
  203. .name = "lintolpc10",
  204. .src_codec = {
  205. .name = "slin",
  206. .type = AST_MEDIA_TYPE_AUDIO,
  207. .sample_rate = 8000,
  208. },
  209. .dst_codec = {
  210. .name = "lpc10",
  211. .type = AST_MEDIA_TYPE_AUDIO,
  212. .sample_rate = 8000,
  213. },
  214. .format = "lpc10",
  215. .newpvt = lpc10_enc_new,
  216. .framein = lintolpc10_framein,
  217. .frameout = lintolpc10_frameout,
  218. .destroy = lpc10_destroy,
  219. .sample = slin8_sample,
  220. .desc_size = sizeof(struct lpc10_coder_pvt),
  221. .buffer_samples = BUFFER_SAMPLES,
  222. .buf_size = LPC10_BYTES_IN_COMPRESSED_FRAME * (1 + BUFFER_SAMPLES / LPC10_SAMPLES_PER_FRAME),
  223. };
  224. static int unload_module(void)
  225. {
  226. int res;
  227. res = ast_unregister_translator(&lintolpc10);
  228. res |= ast_unregister_translator(&lpc10tolin);
  229. return res;
  230. }
  231. static int load_module(void)
  232. {
  233. int res;
  234. res = ast_register_translator(&lpc10tolin);
  235. res |= ast_register_translator(&lintolpc10);
  236. if (res) {
  237. unload_module();
  238. return AST_MODULE_LOAD_DECLINE;
  239. }
  240. return AST_MODULE_LOAD_SUCCESS;
  241. }
  242. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "LPC10 2.4kbps Coder/Decoder",
  243. .support_level = AST_MODULE_SUPPORT_CORE,
  244. .load = load_module,
  245. .unload = unload_module,
  246. );