codec_adpcm.c 7.7 KB

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