codec_adpcm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /* codec_adpcm.c - translate between signed linear and Dialogic ADPCM
  2. *
  3. * Asterisk -- A telephony toolkit for Linux.
  4. *
  5. * Based on frompcm.c and topcm.c from the Emiliano MIPL browser/
  6. * interpreter. See http://www.bsdtelephony.com.mx
  7. *
  8. * Copyright (c) 2001 Linux Support Services, Inc. All rights reserved.
  9. *
  10. * Karl Sackett <krs@linux-support.net>, 2001-3-21
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License
  14. */
  15. #include <asterisk/lock.h>
  16. #include <asterisk/logger.h>
  17. #include <asterisk/module.h>
  18. #include <asterisk/translate.h>
  19. #include <asterisk/channel.h>
  20. #include <fcntl.h>
  21. #include <netinet/in.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #define BUFFER_SIZE 8096 /* size for the translation buffers */
  27. AST_MUTEX_DEFINE_STATIC(localuser_lock);
  28. static int localusecnt = 0;
  29. static char *tdesc = "Adaptive Differential PCM Coder/Decoder";
  30. /* Sample frame data */
  31. #include "slin_adpcm_ex.h"
  32. #include "adpcm_slin_ex.h"
  33. /*
  34. * Step size index shift table
  35. */
  36. static short indsft[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
  37. /*
  38. * Step size table, where stpsz[i]=floor[16*(11/10)^i]
  39. */
  40. static short stpsz[49] = {
  41. 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73,
  42. 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
  43. 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
  44. 1060, 1166, 1282, 1411, 1552
  45. };
  46. /*
  47. * Nibble to bit map
  48. */
  49. static short nbl2bit[16][4] = {
  50. {1, 0, 0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1},
  51. {1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 1, 1, 1},
  52. {-1, 0, 0, 0}, {-1, 0, 0, 1}, {-1, 0, 1, 0}, {-1, 0, 1, 1},
  53. {-1, 1, 0, 0}, {-1, 1, 0, 1}, {-1, 1, 1, 0}, {-1, 1, 1, 1}
  54. };
  55. /*
  56. * Decode(encoded)
  57. * Decodes the encoded nibble from the adpcm file.
  58. *
  59. * Results:
  60. * Returns the encoded difference.
  61. *
  62. * Side effects:
  63. * Sets the index to the step size table for the next encode.
  64. */
  65. static inline void
  66. decode (unsigned char encoded, short *ssindex, short *signal, unsigned char *rkey, unsigned char *next)
  67. {
  68. short diff, step;
  69. step = stpsz[*ssindex];
  70. diff = step * nbl2bit[encoded][1] +
  71. (step >> 1) * nbl2bit[encoded][2] +
  72. (step >> 2) * nbl2bit[encoded][3] +
  73. (step >> 3);
  74. if (nbl2bit[encoded][2] && (step & 0x1))
  75. diff++;
  76. diff *= nbl2bit[encoded][0];
  77. if ( *next & 0x1 )
  78. *signal -= 8;
  79. else if ( *next & 0x2 )
  80. *signal += 8;
  81. *signal += diff;
  82. if (*signal > 2047)
  83. *signal = 2047;
  84. else if (*signal < -2047)
  85. *signal = -2047;
  86. *next = 0;
  87. #ifdef AUTO_RETURN
  88. if( encoded & 0x7 )
  89. *rkey = 0;
  90. else if ( ++(*rkey) == 24 ) {
  91. *rkey = 0;
  92. if (*signal > 0)
  93. *next = 0x1;
  94. else if (*signal < 0)
  95. *next = 0x2;
  96. }
  97. #endif
  98. *ssindex = *ssindex + indsft[(encoded & 7)];
  99. if (*ssindex < 0)
  100. *ssindex = 0;
  101. else if (*ssindex > 48)
  102. *ssindex = 48;
  103. }
  104. /*
  105. * Adpcm
  106. * Takes a signed linear signal and encodes it as ADPCM
  107. * For more information see http://support.dialogic.com/appnotes/adpcm.pdf
  108. *
  109. * Results:
  110. * Foo.
  111. *
  112. * Side effects:
  113. * signal gets updated with each pass.
  114. */
  115. static inline unsigned char
  116. adpcm (short csig, short *ssindex, short *signal, unsigned char *rkey, unsigned char *next)
  117. {
  118. short diff, step;
  119. unsigned char encoded;
  120. step = stpsz[*ssindex];
  121. /*
  122. * Clip csig if too large or too small
  123. */
  124. csig >>= 4;
  125. diff = csig - *signal;
  126. if (diff < 0)
  127. {
  128. encoded = 8;
  129. diff = -diff;
  130. }
  131. else
  132. encoded = 0;
  133. if (diff >= step)
  134. {
  135. encoded |= 4;
  136. diff -= step;
  137. }
  138. step >>= 1;
  139. if (diff >= step)
  140. {
  141. encoded |= 2;
  142. diff -= step;
  143. }
  144. step >>= 1;
  145. if (diff >= step)
  146. encoded |= 1;
  147. decode (encoded, ssindex, signal, rkey, next);
  148. return (encoded);
  149. }
  150. /*
  151. * Private workspace for translating signed linear signals to ADPCM.
  152. */
  153. struct adpcm_encoder_pvt
  154. {
  155. struct ast_frame f;
  156. char offset[AST_FRIENDLY_OFFSET]; /* Space to build offset */
  157. short inbuf[BUFFER_SIZE]; /* Unencoded signed linear values */
  158. unsigned char outbuf[BUFFER_SIZE]; /* Encoded ADPCM, two nibbles to a word */
  159. short ssindex;
  160. short signal;
  161. unsigned char zero_count;
  162. unsigned char next_flag;
  163. int tail;
  164. };
  165. /*
  166. * Private workspace for translating ADPCM signals to signed linear.
  167. */
  168. struct adpcm_decoder_pvt
  169. {
  170. struct ast_frame f;
  171. char offset[AST_FRIENDLY_OFFSET]; /* Space to build offset */
  172. short outbuf[BUFFER_SIZE]; /* Decoded signed linear values */
  173. short ssindex;
  174. short signal;
  175. unsigned char zero_count;
  176. unsigned char next_flag;
  177. int tail;
  178. };
  179. /*
  180. * AdpcmToLin_New
  181. * Create a new instance of adpcm_decoder_pvt.
  182. *
  183. * Results:
  184. * Returns a pointer to the new instance.
  185. *
  186. * Side effects:
  187. * None.
  188. */
  189. static struct ast_translator_pvt *
  190. adpcmtolin_new (void)
  191. {
  192. struct adpcm_decoder_pvt *tmp;
  193. tmp = malloc (sizeof (struct adpcm_decoder_pvt));
  194. if (tmp)
  195. {
  196. memset(tmp, 0, sizeof(*tmp));
  197. tmp->tail = 0;
  198. localusecnt++;
  199. ast_update_use_count ();
  200. }
  201. return (struct ast_translator_pvt *) tmp;
  202. }
  203. /*
  204. * LinToAdpcm_New
  205. * Create a new instance of adpcm_encoder_pvt.
  206. *
  207. * Results:
  208. * Returns a pointer to the new instance.
  209. *
  210. * Side effects:
  211. * None.
  212. */
  213. static struct ast_translator_pvt *
  214. lintoadpcm_new (void)
  215. {
  216. struct adpcm_encoder_pvt *tmp;
  217. tmp = malloc (sizeof (struct adpcm_encoder_pvt));
  218. if (tmp)
  219. {
  220. memset(tmp, 0, sizeof(*tmp));
  221. localusecnt++;
  222. ast_update_use_count ();
  223. tmp->tail = 0;
  224. }
  225. return (struct ast_translator_pvt *) tmp;
  226. }
  227. /*
  228. * AdpcmToLin_FrameIn
  229. * Fill an input buffer with packed 4-bit ADPCM values if there is room
  230. * left.
  231. *
  232. * Results:
  233. * Foo
  234. *
  235. * Side effects:
  236. * tmp->tail is the number of packed values in the buffer.
  237. */
  238. static int
  239. adpcmtolin_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
  240. {
  241. struct adpcm_decoder_pvt *tmp = (struct adpcm_decoder_pvt *) pvt;
  242. int x;
  243. unsigned char *b;
  244. if (f->datalen * 4 > sizeof(tmp->outbuf)) {
  245. ast_log(LOG_WARNING, "Out of buffer space\n");
  246. return -1;
  247. }
  248. b = f->data;
  249. for (x=0;x<f->datalen;x++) {
  250. decode((b[x] >> 4) & 0xf, &tmp->ssindex, &tmp->signal, &tmp->zero_count, &tmp->next_flag);
  251. tmp->outbuf[tmp->tail++] = tmp->signal << 4;
  252. decode(b[x] & 0x0f, &tmp->ssindex, &tmp->signal, &tmp->zero_count, &tmp->next_flag);
  253. tmp->outbuf[tmp->tail++] = tmp->signal << 4;
  254. }
  255. return 0;
  256. }
  257. /*
  258. * AdpcmToLin_FrameOut
  259. * Convert 4-bit ADPCM encoded signals to 16-bit signed linear.
  260. *
  261. * Results:
  262. * Converted signals are placed in tmp->f.data, tmp->f.datalen
  263. * and tmp->f.samples are calculated.
  264. *
  265. * Side effects:
  266. * None.
  267. */
  268. static struct ast_frame *
  269. adpcmtolin_frameout (struct ast_translator_pvt *pvt)
  270. {
  271. struct adpcm_decoder_pvt *tmp = (struct adpcm_decoder_pvt *) pvt;
  272. if (!tmp->tail)
  273. return NULL;
  274. tmp->f.frametype = AST_FRAME_VOICE;
  275. tmp->f.subclass = AST_FORMAT_SLINEAR;
  276. tmp->f.datalen = tmp->tail *2;
  277. tmp->f.samples = tmp->tail;
  278. tmp->f.mallocd = 0;
  279. tmp->f.offset = AST_FRIENDLY_OFFSET;
  280. tmp->f.src = __PRETTY_FUNCTION__;
  281. tmp->f.data = tmp->outbuf;
  282. tmp->tail = 0;
  283. return &tmp->f;
  284. }
  285. /*
  286. * LinToAdpcm_FrameIn
  287. * Fill an input buffer with 16-bit signed linear PCM values.
  288. *
  289. * Results:
  290. * None.
  291. *
  292. * Side effects:
  293. * tmp->tail is number of signal values in the input buffer.
  294. */
  295. static int
  296. lintoadpcm_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
  297. {
  298. struct adpcm_encoder_pvt *tmp = (struct adpcm_encoder_pvt *) pvt;
  299. if ((tmp->tail + f->datalen / 2) < (sizeof (tmp->inbuf) / 2))
  300. {
  301. memcpy (&tmp->inbuf[tmp->tail], f->data, f->datalen);
  302. tmp->tail += f->datalen / 2;
  303. }
  304. else
  305. {
  306. ast_log (LOG_WARNING, "Out of buffer space\n");
  307. return -1;
  308. }
  309. return 0;
  310. }
  311. /*
  312. * LinToAdpcm_FrameOut
  313. * Convert a buffer of raw 16-bit signed linear PCM to a buffer
  314. * of 4-bit ADPCM packed two to a byte (Big Endian).
  315. *
  316. * Results:
  317. * Foo
  318. *
  319. * Side effects:
  320. * Leftover inbuf data gets packed, tail gets updated.
  321. */
  322. static struct ast_frame *
  323. lintoadpcm_frameout (struct ast_translator_pvt *pvt)
  324. {
  325. struct adpcm_encoder_pvt *tmp = (struct adpcm_encoder_pvt *) pvt;
  326. unsigned char adpcm0, adpcm1;
  327. int i_max, i;
  328. if (tmp->tail < 2) return NULL;
  329. i_max = (tmp->tail / 2) * 2;
  330. tmp->outbuf[0] = tmp->ssindex & 0xff;
  331. tmp->outbuf[1] = (tmp->signal >> 8) & 0xff;
  332. tmp->outbuf[2] = (tmp->signal & 0xff);
  333. tmp->outbuf[3] = tmp->zero_count;
  334. tmp->outbuf[4] = tmp->next_flag;
  335. for (i = 0; i < i_max; i+=2)
  336. {
  337. adpcm0 = adpcm (tmp->inbuf[i], &tmp->ssindex, &tmp->signal, &tmp->zero_count, &tmp->next_flag);
  338. adpcm1 = adpcm (tmp->inbuf[i+1], &tmp->ssindex, &tmp->signal, &tmp->zero_count, &tmp->next_flag);
  339. tmp->outbuf[i/2] = (adpcm0 << 4) | adpcm1;
  340. };
  341. tmp->f.frametype = AST_FRAME_VOICE;
  342. tmp->f.subclass = AST_FORMAT_ADPCM;
  343. tmp->f.samples = i_max;
  344. tmp->f.mallocd = 0;
  345. tmp->f.offset = AST_FRIENDLY_OFFSET;
  346. tmp->f.src = __PRETTY_FUNCTION__;
  347. tmp->f.data = tmp->outbuf;
  348. tmp->f.datalen = i_max / 2;
  349. /*
  350. * If there is a signal left over (there should be no more than
  351. * one) move it to the beginning of the input buffer.
  352. */
  353. if (tmp->tail == i_max)
  354. tmp->tail = 0;
  355. else
  356. {
  357. tmp->inbuf[0] = tmp->inbuf[tmp->tail];
  358. tmp->tail = 1;
  359. }
  360. return &tmp->f;
  361. }
  362. /*
  363. * AdpcmToLin_Sample
  364. */
  365. static struct ast_frame *
  366. adpcmtolin_sample (void)
  367. {
  368. static struct ast_frame f;
  369. f.frametype = AST_FRAME_VOICE;
  370. f.subclass = AST_FORMAT_ADPCM;
  371. f.datalen = sizeof (adpcm_slin_ex);
  372. f.samples = sizeof(adpcm_slin_ex) * 2;
  373. f.mallocd = 0;
  374. f.offset = 0;
  375. f.src = __PRETTY_FUNCTION__;
  376. f.data = adpcm_slin_ex;
  377. return &f;
  378. }
  379. /*
  380. * LinToAdpcm_Sample
  381. */
  382. static struct ast_frame *
  383. lintoadpcm_sample (void)
  384. {
  385. static struct ast_frame f;
  386. f.frametype = AST_FRAME_VOICE;
  387. f.subclass = AST_FORMAT_SLINEAR;
  388. f.datalen = sizeof (slin_adpcm_ex);
  389. /* Assume 8000 Hz */
  390. f.samples = sizeof (slin_adpcm_ex) / 2;
  391. f.mallocd = 0;
  392. f.offset = 0;
  393. f.src = __PRETTY_FUNCTION__;
  394. f.data = slin_adpcm_ex;
  395. return &f;
  396. }
  397. /*
  398. * Adpcm_Destroy
  399. * Destroys a private workspace.
  400. *
  401. * Results:
  402. * It's gone!
  403. *
  404. * Side effects:
  405. * None.
  406. */
  407. static void
  408. adpcm_destroy (struct ast_translator_pvt *pvt)
  409. {
  410. free (pvt);
  411. localusecnt--;
  412. ast_update_use_count ();
  413. }
  414. /*
  415. * The complete translator for ADPCMToLin.
  416. */
  417. static struct ast_translator adpcmtolin = {
  418. "adpcmtolin",
  419. AST_FORMAT_ADPCM,
  420. AST_FORMAT_SLINEAR,
  421. adpcmtolin_new,
  422. adpcmtolin_framein,
  423. adpcmtolin_frameout,
  424. adpcm_destroy,
  425. /* NULL */
  426. adpcmtolin_sample
  427. };
  428. /*
  429. * The complete translator for LinToADPCM.
  430. */
  431. static struct ast_translator lintoadpcm = {
  432. "lintoadpcm",
  433. AST_FORMAT_SLINEAR,
  434. AST_FORMAT_ADPCM,
  435. lintoadpcm_new,
  436. lintoadpcm_framein,
  437. lintoadpcm_frameout,
  438. adpcm_destroy,
  439. /* NULL */
  440. lintoadpcm_sample
  441. };
  442. int
  443. unload_module (void)
  444. {
  445. int res;
  446. ast_mutex_lock (&localuser_lock);
  447. res = ast_unregister_translator (&lintoadpcm);
  448. if (!res)
  449. res = ast_unregister_translator (&adpcmtolin);
  450. if (localusecnt)
  451. res = -1;
  452. ast_mutex_unlock (&localuser_lock);
  453. return res;
  454. }
  455. int
  456. load_module (void)
  457. {
  458. int res;
  459. res = ast_register_translator (&adpcmtolin);
  460. if (!res)
  461. res = ast_register_translator (&lintoadpcm);
  462. else
  463. ast_unregister_translator (&adpcmtolin);
  464. return res;
  465. }
  466. /*
  467. * Return a description of this module.
  468. */
  469. char *
  470. description (void)
  471. {
  472. return tdesc;
  473. }
  474. int
  475. usecount (void)
  476. {
  477. int res;
  478. STANDARD_USECOUNT (res);
  479. return res;
  480. }
  481. char *
  482. key ()
  483. {
  484. return ASTERISK_GPL_KEY;
  485. }