codec_adpcm.c 8.9 KB

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