codec_adpcm.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Based on frompcm.c and topcm.c from the Emiliano MIPL browser/
  5. * interpreter. See http://www.bsdtelephony.com.mx
  6. *
  7. * Copyright (c) 2001 - 2005 Digium, Inc.
  8. * All rights reserved.
  9. *
  10. * Karl Sackett <krs@linux-support.net>, 2001-03-21
  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_adpcm.c - translate between signed linear and Dialogic ADPCM
  25. *
  26. * \ingroup codecs
  27. */
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include <fcntl.h>
  31. #include <netinet/in.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <unistd.h>
  36. #include "asterisk/lock.h"
  37. #include "asterisk/logger.h"
  38. #include "asterisk/linkedlists.h"
  39. #include "asterisk/module.h"
  40. #include "asterisk/config.h"
  41. #include "asterisk/options.h"
  42. #include "asterisk/translate.h"
  43. #include "asterisk/channel.h"
  44. #include "asterisk/utils.h"
  45. /* define NOT_BLI to use a faster but not bit-level identical version */
  46. /* #define NOT_BLI */
  47. #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
  48. /* Sample frame data */
  49. #include "slin_adpcm_ex.h"
  50. #include "adpcm_slin_ex.h"
  51. /*
  52. * Step size index shift table
  53. */
  54. static int indsft[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
  55. /*
  56. * Step size table, where stpsz[i]=floor[16*(11/10)^i]
  57. */
  58. static int stpsz[49] = {
  59. 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73,
  60. 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
  61. 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
  62. 1060, 1166, 1282, 1411, 1552
  63. };
  64. /*
  65. * Decoder/Encoder state
  66. * States for both encoder and decoder are synchronized
  67. */
  68. struct adpcm_state {
  69. int ssindex;
  70. int signal;
  71. int zero_count;
  72. int next_flag;
  73. };
  74. /*
  75. * Decode(encoded)
  76. * Decodes the encoded nibble from the adpcm file.
  77. *
  78. * Results:
  79. * Returns the encoded difference.
  80. *
  81. * Side effects:
  82. * Sets the index to the step size table for the next encode.
  83. */
  84. static inline short decode(int encoded, struct adpcm_state *state)
  85. {
  86. int diff;
  87. int step;
  88. int sign;
  89. step = stpsz[state->ssindex];
  90. sign = encoded & 0x08;
  91. encoded &= 0x07;
  92. #ifdef NOT_BLI
  93. diff = (((encoded << 1) + 1) * step) >> 3;
  94. #else /* BLI code */
  95. diff = step >> 3;
  96. if (encoded & 4)
  97. diff += step;
  98. if (encoded & 2)
  99. diff += step >> 1;
  100. if (encoded & 1)
  101. diff += step >> 2;
  102. if ((encoded >> 1) & step & 0x1)
  103. diff++;
  104. #endif
  105. if (sign)
  106. diff = -diff;
  107. if (state->next_flag & 0x1)
  108. state->signal -= 8;
  109. else if (state->next_flag & 0x2)
  110. state->signal += 8;
  111. state->signal += diff;
  112. if (state->signal > 2047)
  113. state->signal = 2047;
  114. else if (state->signal < -2047)
  115. state->signal = -2047;
  116. state->next_flag = 0;
  117. #ifdef AUTO_RETURN
  118. if (encoded)
  119. state->zero_count = 0;
  120. else if (++(state->zero_count) == 24) {
  121. state->zero_count = 0;
  122. if (state->signal > 0)
  123. state->next_flag = 0x1;
  124. else if (state->signal < 0)
  125. state->next_flag = 0x2;
  126. }
  127. #endif
  128. state->ssindex += indsft[encoded];
  129. if (state->ssindex < 0)
  130. state->ssindex = 0;
  131. else if (state->ssindex > 48)
  132. state->ssindex = 48;
  133. return state->signal << 4;
  134. }
  135. /*
  136. * Adpcm
  137. * Takes a signed linear signal and encodes it as ADPCM
  138. * For more information see http://support.dialogic.com/appnotes/adpcm.pdf
  139. *
  140. * Results:
  141. * Foo.
  142. *
  143. * Side effects:
  144. * signal gets updated with each pass.
  145. */
  146. static inline int adpcm(short csig, struct adpcm_state *state)
  147. {
  148. int diff;
  149. int step;
  150. int encoded;
  151. /*
  152. * Clip csig if too large or too small
  153. */
  154. csig >>= 4;
  155. step = stpsz[state->ssindex];
  156. diff = csig - state->signal;
  157. #ifdef NOT_BLI
  158. if (diff < 0) {
  159. encoded = (-diff << 2) / step;
  160. if (encoded > 7)
  161. encoded = 7;
  162. encoded |= 0x08;
  163. } else {
  164. encoded = (diff << 2) / step;
  165. if (encoded > 7)
  166. encoded = 7;
  167. }
  168. #else /* BLI code */
  169. if (diff < 0) {
  170. encoded = 8;
  171. diff = -diff;
  172. } else
  173. encoded = 0;
  174. if (diff >= step) {
  175. encoded |= 4;
  176. diff -= step;
  177. }
  178. step >>= 1;
  179. if (diff >= step) {
  180. encoded |= 2;
  181. diff -= step;
  182. }
  183. step >>= 1;
  184. if (diff >= step)
  185. encoded |= 1;
  186. #endif /* NOT_BLI */
  187. /* feedback to state */
  188. decode(encoded, state);
  189. return encoded;
  190. }
  191. /*----------------- Asterisk-codec glue ------------*/
  192. /*! \brief Workspace for translating signed linear signals to ADPCM. */
  193. struct adpcm_encoder_pvt {
  194. struct adpcm_state state;
  195. int16_t inbuf[BUFFER_SAMPLES]; /* Unencoded signed linear values */
  196. };
  197. /*! \brief Workspace for translating ADPCM signals to signed linear. */
  198. struct adpcm_decoder_pvt {
  199. struct adpcm_state state;
  200. };
  201. /*! \brief decode 4-bit adpcm frame data and store in output buffer */
  202. static int adpcmtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  203. {
  204. struct adpcm_decoder_pvt *tmp = pvt->pvt;
  205. int x = f->datalen;
  206. unsigned char *src = f->data;
  207. int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
  208. while (x--) {
  209. *dst++ = decode((*src >> 4) & 0xf, &tmp->state);
  210. *dst++ = decode(*src++ & 0x0f, &tmp->state);
  211. }
  212. pvt->samples += f->samples;
  213. pvt->datalen += 2*f->samples;
  214. return 0;
  215. }
  216. /*! \brief fill input buffer with 16-bit signed linear PCM values. */
  217. static int lintoadpcm_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  218. {
  219. struct adpcm_encoder_pvt *tmp = pvt->pvt;
  220. memcpy(&tmp->inbuf[pvt->samples], f->data, f->datalen);
  221. pvt->samples += f->samples;
  222. return 0;
  223. }
  224. /*! \brief convert inbuf and store into frame */
  225. static struct ast_frame *lintoadpcm_frameout(struct ast_trans_pvt *pvt)
  226. {
  227. struct adpcm_encoder_pvt *tmp = pvt->pvt;
  228. struct ast_frame *f;
  229. int i;
  230. int samples = pvt->samples; /* save original number */
  231. if (samples < 2)
  232. return NULL;
  233. pvt->samples &= ~1; /* atomic size is 2 samples */
  234. for (i = 0; i < pvt->samples; i += 2) {
  235. pvt->outbuf[i/2] =
  236. (adpcm(tmp->inbuf[i ], &tmp->state) << 4) |
  237. (adpcm(tmp->inbuf[i+1], &tmp->state) );
  238. };
  239. f = ast_trans_frameout(pvt, pvt->samples/2, 0);
  240. /*
  241. * If there is a left over sample, move it to the beginning
  242. * of the input buffer.
  243. */
  244. if (samples & 1) { /* move the leftover sample at beginning */
  245. tmp->inbuf[0] = tmp->inbuf[samples - 1];
  246. pvt->samples = 1;
  247. }
  248. return f;
  249. }
  250. /*! \brief AdpcmToLin_Sample */
  251. static struct ast_frame *adpcmtolin_sample(void)
  252. {
  253. static struct ast_frame f;
  254. f.frametype = AST_FRAME_VOICE;
  255. f.subclass = AST_FORMAT_ADPCM;
  256. f.datalen = sizeof(adpcm_slin_ex);
  257. f.samples = sizeof(adpcm_slin_ex) * 2;
  258. f.mallocd = 0;
  259. f.offset = 0;
  260. f.src = __PRETTY_FUNCTION__;
  261. f.data = adpcm_slin_ex;
  262. return &f;
  263. }
  264. /*! \brief LinToAdpcm_Sample */
  265. static struct ast_frame *lintoadpcm_sample(void)
  266. {
  267. static struct ast_frame f;
  268. f.frametype = AST_FRAME_VOICE;
  269. f.subclass = AST_FORMAT_SLINEAR;
  270. f.datalen = sizeof(slin_adpcm_ex);
  271. /* Assume 8000 Hz */
  272. f.samples = sizeof(slin_adpcm_ex) / 2;
  273. f.mallocd = 0;
  274. f.offset = 0;
  275. f.src = __PRETTY_FUNCTION__;
  276. f.data = slin_adpcm_ex;
  277. return &f;
  278. }
  279. static struct ast_translator adpcmtolin = {
  280. .name = "adpcmtolin",
  281. .srcfmt = AST_FORMAT_ADPCM,
  282. .dstfmt = AST_FORMAT_SLINEAR,
  283. .framein = adpcmtolin_framein,
  284. .sample = adpcmtolin_sample,
  285. .desc_size = sizeof(struct adpcm_decoder_pvt),
  286. .buffer_samples = BUFFER_SAMPLES,
  287. .buf_size = BUFFER_SAMPLES * 2,
  288. };
  289. static struct ast_translator lintoadpcm = {
  290. .name = "lintoadpcm",
  291. .srcfmt = AST_FORMAT_SLINEAR,
  292. .dstfmt = AST_FORMAT_ADPCM,
  293. .framein = lintoadpcm_framein,
  294. .frameout = lintoadpcm_frameout,
  295. .sample = lintoadpcm_sample,
  296. .desc_size = sizeof (struct adpcm_encoder_pvt),
  297. .buffer_samples = BUFFER_SAMPLES,
  298. .buf_size = BUFFER_SAMPLES/ 2, /* 2 samples per byte */
  299. };
  300. /*! \brief standard module glue */
  301. static int reload(void)
  302. {
  303. return 0;
  304. }
  305. static int unload_module(void)
  306. {
  307. int res;
  308. res = ast_unregister_translator(&lintoadpcm);
  309. res |= ast_unregister_translator(&adpcmtolin);
  310. return res;
  311. }
  312. static int load_module(void)
  313. {
  314. int res;
  315. res = ast_register_translator(&adpcmtolin);
  316. if (!res)
  317. res = ast_register_translator(&lintoadpcm);
  318. else
  319. ast_unregister_translator(&adpcmtolin);
  320. return res;
  321. }
  322. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Adaptive Differential PCM Coder/Decoder",
  323. .load = load_module,
  324. .unload = unload_module,
  325. .reload = reload,
  326. );