codec_g726.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /* codec_g726.c - translate between signed linear and ITU G.726-32kbps
  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) 2004, Digium
  9. *
  10. * Mark Spencer <markster@digium.com>
  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. #define BUF_SHIFT 5
  28. AST_MUTEX_DEFINE_STATIC(localuser_lock);
  29. static int localusecnt = 0;
  30. static char *tdesc = "ITU G.726-32kbps G726 Transcoder";
  31. /* Sample frame data */
  32. #include "slin_g726_ex.h"
  33. #include "g726_slin_ex.h"
  34. /*
  35. * The following is the definition of the state structure
  36. * used by the G.721/G.723 encoder and decoder to preserve their internal
  37. * state between successive calls. The meanings of the majority
  38. * of the state structure fields are explained in detail in the
  39. * CCITT Recommendation G.721. The field names are essentially indentical
  40. * to variable names in the bit level description of the coding algorithm
  41. * included in this Recommendation.
  42. */
  43. struct g726_state {
  44. long yl; /* Locked or steady state step size multiplier. */
  45. short yu; /* Unlocked or non-steady state step size multiplier. */
  46. short dms; /* Short term energy estimate. */
  47. short dml; /* Long term energy estimate. */
  48. short ap; /* Linear weighting coefficient of 'yl' and 'yu'. */
  49. short a[2]; /* Coefficients of pole portion of prediction filter. */
  50. short b[6]; /* Coefficients of zero portion of prediction filter. */
  51. short pk[2]; /*
  52. * Signs of previous two samples of a partially
  53. * reconstructed signal.
  54. */
  55. short dq[6]; /*
  56. * Previous 6 samples of the quantized difference
  57. * signal represented in an internal floating point
  58. * format.
  59. */
  60. short sr[2]; /*
  61. * Previous 2 samples of the quantized difference
  62. * signal represented in an internal floating point
  63. * format.
  64. */
  65. char td; /* delayed tone detect, new in 1988 version */
  66. };
  67. static short qtab_721[7] = {-124, 80, 178, 246, 300, 349, 400};
  68. /*
  69. * Maps G.721 code word to reconstructed scale factor normalized log
  70. * magnitude values.
  71. */
  72. static short _dqlntab[16] = {-2048, 4, 135, 213, 273, 323, 373, 425,
  73. 425, 373, 323, 273, 213, 135, 4, -2048};
  74. /* Maps G.721 code word to log of scale factor multiplier. */
  75. static short _witab[16] = {-12, 18, 41, 64, 112, 198, 355, 1122,
  76. 1122, 355, 198, 112, 64, 41, 18, -12};
  77. /*
  78. * Maps G.721 code words to a set of values whose long and short
  79. * term averages are computed and then compared to give an indication
  80. * how stationary (steady state) the signal is.
  81. */
  82. static short _fitab[16] = {0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
  83. 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0};
  84. static short power2[15] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
  85. 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000};
  86. /*
  87. * quan()
  88. *
  89. * quantizes the input val against the table of size short integers.
  90. * It returns i if table[i - 1] <= val < table[i].
  91. *
  92. * Using linear search for simple coding.
  93. */
  94. static int quan(int val, short *table, int size)
  95. {
  96. int i;
  97. for (i = 0; i < size; i++)
  98. if (val < *table++)
  99. break;
  100. return (i);
  101. }
  102. /*
  103. * fmult()
  104. *
  105. * returns the integer product of the 14-bit integer "an" and
  106. * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
  107. */
  108. static int fmult(int an, int srn)
  109. {
  110. short anmag, anexp, anmant;
  111. short wanexp, wanmant;
  112. short retval;
  113. anmag = (an > 0) ? an : ((-an) & 0x1FFF);
  114. anexp = quan(anmag, power2, 15) - 6;
  115. anmant = (anmag == 0) ? 32 :
  116. (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
  117. wanexp = anexp + ((srn >> 6) & 0xF) - 13;
  118. wanmant = (anmant * (srn & 077) + 0x30) >> 4;
  119. retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) :
  120. (wanmant >> -wanexp);
  121. return (((an ^ srn) < 0) ? -retval : retval);
  122. }
  123. /*
  124. * g72x_init_state()
  125. *
  126. * This routine initializes and/or resets the g726_state structure
  127. * pointed to by 'state_ptr'.
  128. * All the initial state values are specified in the CCITT G.721 document.
  129. */
  130. static void g726_init_state(struct g726_state *state_ptr)
  131. {
  132. int cnta;
  133. state_ptr->yl = 34816;
  134. state_ptr->yu = 544;
  135. state_ptr->dms = 0;
  136. state_ptr->dml = 0;
  137. state_ptr->ap = 0;
  138. for (cnta = 0; cnta < 2; cnta++) {
  139. state_ptr->a[cnta] = 0;
  140. state_ptr->pk[cnta] = 0;
  141. state_ptr->sr[cnta] = 32;
  142. }
  143. for (cnta = 0; cnta < 6; cnta++) {
  144. state_ptr->b[cnta] = 0;
  145. state_ptr->dq[cnta] = 32;
  146. }
  147. state_ptr->td = 0;
  148. }
  149. /*
  150. * predictor_zero()
  151. *
  152. * computes the estimated signal from 6-zero predictor.
  153. *
  154. */
  155. static int predictor_zero(struct g726_state *state_ptr)
  156. {
  157. int i;
  158. int sezi;
  159. sezi = fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]);
  160. for (i = 1; i < 6; i++) /* ACCUM */
  161. sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
  162. return (sezi);
  163. }
  164. /*
  165. * predictor_pole()
  166. *
  167. * computes the estimated signal from 2-pole predictor.
  168. *
  169. */
  170. static int predictor_pole(struct g726_state *state_ptr)
  171. {
  172. return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
  173. fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
  174. }
  175. /*
  176. * step_size()
  177. *
  178. * computes the quantization step size of the adaptive quantizer.
  179. *
  180. */
  181. static int step_size(struct g726_state *state_ptr)
  182. {
  183. int y;
  184. int dif;
  185. int al;
  186. if (state_ptr->ap >= 256)
  187. return (state_ptr->yu);
  188. else {
  189. y = state_ptr->yl >> 6;
  190. dif = state_ptr->yu - y;
  191. al = state_ptr->ap >> 2;
  192. if (dif > 0)
  193. y += (dif * al) >> 6;
  194. else if (dif < 0)
  195. y += (dif * al + 0x3F) >> 6;
  196. return (y);
  197. }
  198. }
  199. /*
  200. * quantize()
  201. *
  202. * Given a raw sample, 'd', of the difference signal and a
  203. * quantization step size scale factor, 'y', this routine returns the
  204. * ADPCM codeword to which that sample gets quantized. The step
  205. * size scale factor division operation is done in the log base 2 domain
  206. * as a subtraction.
  207. */
  208. static int quantize(
  209. int d, /* Raw difference signal sample */
  210. int y, /* Step size multiplier */
  211. short *table, /* quantization table */
  212. int size) /* table size of short integers */
  213. {
  214. short dqm; /* Magnitude of 'd' */
  215. short exp; /* Integer part of base 2 log of 'd' */
  216. short mant; /* Fractional part of base 2 log */
  217. short dl; /* Log of magnitude of 'd' */
  218. short dln; /* Step size scale factor normalized log */
  219. int i;
  220. /*
  221. * LOG
  222. *
  223. * Compute base 2 log of 'd', and store in 'dl'.
  224. */
  225. dqm = abs(d);
  226. exp = quan(dqm >> 1, power2, 15);
  227. mant = ((dqm << 7) >> exp) & 0x7F; /* Fractional portion. */
  228. dl = (exp << 7) + mant;
  229. /*
  230. * SUBTB
  231. *
  232. * "Divide" by step size multiplier.
  233. */
  234. dln = dl - (y >> 2);
  235. /*
  236. * QUAN
  237. *
  238. * Obtain codword i for 'd'.
  239. */
  240. i = quan(dln, table, size);
  241. if (d < 0) /* take 1's complement of i */
  242. return ((size << 1) + 1 - i);
  243. else if (i == 0) /* take 1's complement of 0 */
  244. return ((size << 1) + 1); /* new in 1988 */
  245. else
  246. return (i);
  247. }
  248. /*
  249. * reconstruct()
  250. *
  251. * Returns reconstructed difference signal 'dq' obtained from
  252. * codeword 'i' and quantization step size scale factor 'y'.
  253. * Multiplication is performed in log base 2 domain as addition.
  254. */
  255. static int reconstruct(
  256. int sign, /* 0 for non-negative value */
  257. int dqln, /* G.72x codeword */
  258. int y) /* Step size multiplier */
  259. {
  260. short dql; /* Log of 'dq' magnitude */
  261. short dex; /* Integer part of log */
  262. short dqt;
  263. short dq; /* Reconstructed difference signal sample */
  264. dql = dqln + (y >> 2); /* ADDA */
  265. if (dql < 0) {
  266. return ((sign) ? -0x8000 : 0);
  267. } else { /* ANTILOG */
  268. dex = (dql >> 7) & 15;
  269. dqt = 128 + (dql & 127);
  270. dq = (dqt << 7) >> (14 - dex);
  271. return ((sign) ? (dq - 0x8000) : dq);
  272. }
  273. }
  274. /*
  275. * update()
  276. *
  277. * updates the state variables for each output code
  278. */
  279. static void update(
  280. int code_size, /* distinguish 723_40 with others */
  281. int y, /* quantizer step size */
  282. int wi, /* scale factor multiplier */
  283. int fi, /* for long/short term energies */
  284. int dq, /* quantized prediction difference */
  285. int sr, /* reconstructed signal */
  286. int dqsez, /* difference from 2-pole predictor */
  287. struct g726_state *state_ptr) /* coder state pointer */
  288. {
  289. int cnt;
  290. short mag, exp; /* Adaptive predictor, FLOAT A */
  291. short a2p=0; /* LIMC */
  292. short a1ul; /* UPA1 */
  293. short pks1; /* UPA2 */
  294. short fa1;
  295. char tr; /* tone/transition detector */
  296. short ylint, thr2, dqthr;
  297. short ylfrac, thr1;
  298. short pk0;
  299. pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
  300. mag = dq & 0x7FFF; /* prediction difference magnitude */
  301. /* TRANS */
  302. ylint = state_ptr->yl >> 15; /* exponent part of yl */
  303. ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
  304. thr1 = (32 + ylfrac) << ylint; /* threshold */
  305. thr2 = (ylint > 9) ? 31 << 10 : thr1; /* limit thr2 to 31 << 10 */
  306. dqthr = (thr2 + (thr2 >> 1)) >> 1; /* dqthr = 0.75 * thr2 */
  307. if (state_ptr->td == 0) /* signal supposed voice */
  308. tr = 0;
  309. else if (mag <= dqthr) /* supposed data, but small mag */
  310. tr = 0; /* treated as voice */
  311. else /* signal is data (modem) */
  312. tr = 1;
  313. /*
  314. * Quantizer scale factor adaptation.
  315. */
  316. /* FUNCTW & FILTD & DELAY */
  317. /* update non-steady state step size multiplier */
  318. state_ptr->yu = y + ((wi - y) >> 5);
  319. /* LIMB */
  320. if (state_ptr->yu < 544) /* 544 <= yu <= 5120 */
  321. state_ptr->yu = 544;
  322. else if (state_ptr->yu > 5120)
  323. state_ptr->yu = 5120;
  324. /* FILTE & DELAY */
  325. /* update steady state step size multiplier */
  326. state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
  327. /*
  328. * Adaptive predictor coefficients.
  329. */
  330. if (tr == 1) { /* reset a's and b's for modem signal */
  331. state_ptr->a[0] = 0;
  332. state_ptr->a[1] = 0;
  333. state_ptr->b[0] = 0;
  334. state_ptr->b[1] = 0;
  335. state_ptr->b[2] = 0;
  336. state_ptr->b[3] = 0;
  337. state_ptr->b[4] = 0;
  338. state_ptr->b[5] = 0;
  339. } else { /* update a's and b's */
  340. pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */
  341. /* update predictor pole a[1] */
  342. a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
  343. if (dqsez != 0) {
  344. fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
  345. if (fa1 < -8191) /* a2p = function of fa1 */
  346. a2p -= 0x100;
  347. else if (fa1 > 8191)
  348. a2p += 0xFF;
  349. else
  350. a2p += fa1 >> 5;
  351. if (pk0 ^ state_ptr->pk[1])
  352. /* LIMC */
  353. if (a2p <= -12160)
  354. a2p = -12288;
  355. else if (a2p >= 12416)
  356. a2p = 12288;
  357. else
  358. a2p -= 0x80;
  359. else if (a2p <= -12416)
  360. a2p = -12288;
  361. else if (a2p >= 12160)
  362. a2p = 12288;
  363. else
  364. a2p += 0x80;
  365. }
  366. /* TRIGB & DELAY */
  367. state_ptr->a[1] = a2p;
  368. /* UPA1 */
  369. /* update predictor pole a[0] */
  370. state_ptr->a[0] -= state_ptr->a[0] >> 8;
  371. if (dqsez != 0) {
  372. if (pks1 == 0)
  373. state_ptr->a[0] += 192;
  374. else
  375. state_ptr->a[0] -= 192;
  376. }
  377. /* LIMD */
  378. a1ul = 15360 - a2p;
  379. if (state_ptr->a[0] < -a1ul)
  380. state_ptr->a[0] = -a1ul;
  381. else if (state_ptr->a[0] > a1ul)
  382. state_ptr->a[0] = a1ul;
  383. /* UPB : update predictor zeros b[6] */
  384. for (cnt = 0; cnt < 6; cnt++) {
  385. if (code_size == 5) /* for 40Kbps G.723 */
  386. state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
  387. else /* for G.721 and 24Kbps G.723 */
  388. state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
  389. if (dq & 0x7FFF) { /* XOR */
  390. if ((dq ^ state_ptr->dq[cnt]) >= 0)
  391. state_ptr->b[cnt] += 128;
  392. else
  393. state_ptr->b[cnt] -= 128;
  394. }
  395. }
  396. }
  397. for (cnt = 5; cnt > 0; cnt--)
  398. state_ptr->dq[cnt] = state_ptr->dq[cnt-1];
  399. /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
  400. if (mag == 0) {
  401. state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
  402. } else {
  403. exp = quan(mag, power2, 15);
  404. state_ptr->dq[0] = (dq >= 0) ?
  405. (exp << 6) + ((mag << 6) >> exp) :
  406. (exp << 6) + ((mag << 6) >> exp) - 0x400;
  407. }
  408. state_ptr->sr[1] = state_ptr->sr[0];
  409. /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
  410. if (sr == 0) {
  411. state_ptr->sr[0] = 0x20;
  412. } else if (sr > 0) {
  413. exp = quan(sr, power2, 15);
  414. state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
  415. } else if (sr > -32768) {
  416. mag = -sr;
  417. exp = quan(mag, power2, 15);
  418. state_ptr->sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400;
  419. } else
  420. state_ptr->sr[0] = 0xFC20;
  421. /* DELAY A */
  422. state_ptr->pk[1] = state_ptr->pk[0];
  423. state_ptr->pk[0] = pk0;
  424. /* TONE */
  425. if (tr == 1) /* this sample has been treated as data */
  426. state_ptr->td = 0; /* next one will be treated as voice */
  427. else if (a2p < -11776) /* small sample-to-sample correlation */
  428. state_ptr->td = 1; /* signal may be data */
  429. else /* signal is voice */
  430. state_ptr->td = 0;
  431. /*
  432. * Adaptation speed control.
  433. */
  434. state_ptr->dms += (fi - state_ptr->dms) >> 5; /* FILTA */
  435. state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7); /* FILTB */
  436. if (tr == 1)
  437. state_ptr->ap = 256;
  438. else if (y < 1536) /* SUBTC */
  439. state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
  440. else if (state_ptr->td == 1)
  441. state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
  442. else if (abs((state_ptr->dms << 2) - state_ptr->dml) >=
  443. (state_ptr->dml >> 3))
  444. state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
  445. else
  446. state_ptr->ap += (-state_ptr->ap) >> 4;
  447. }
  448. /*
  449. * g726_decode()
  450. *
  451. * Description:
  452. *
  453. * Decodes a 4-bit code of G.726-32 encoded data of i and
  454. * returns the resulting linear PCM, A-law or u-law value.
  455. * return -1 for unknown out_coding value.
  456. */
  457. static int g726_decode(int i, struct g726_state *state_ptr)
  458. {
  459. short sezi, sei, sez, se; /* ACCUM */
  460. short y; /* MIX */
  461. short sr; /* ADDB */
  462. short dq;
  463. short dqsez;
  464. i &= 0x0f; /* mask to get proper bits */
  465. sezi = predictor_zero(state_ptr);
  466. sez = sezi >> 1;
  467. sei = sezi + predictor_pole(state_ptr);
  468. se = sei >> 1; /* se = estimated signal */
  469. y = step_size(state_ptr); /* dynamic quantizer step size */
  470. dq = reconstruct(i & 0x08, _dqlntab[i], y); /* quantized diff. */
  471. sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq; /* reconst. signal */
  472. dqsez = sr - se + sez; /* pole prediction diff. */
  473. update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
  474. return (sr << 2); /* sr was 14-bit dynamic range */
  475. }
  476. /*
  477. * g726_encode()
  478. *
  479. * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
  480. * the resulting code. -1 is returned for unknown input coding value.
  481. */
  482. static int g726_encode(int sl, struct g726_state *state_ptr)
  483. {
  484. short sezi, se, sez; /* ACCUM */
  485. short d; /* SUBTA */
  486. short sr; /* ADDB */
  487. short y; /* MIX */
  488. short dqsez; /* ADDC */
  489. short dq, i;
  490. sl >>= 2; /* 14-bit dynamic range */
  491. sezi = predictor_zero(state_ptr);
  492. sez = sezi >> 1;
  493. se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
  494. d = sl - se; /* estimation difference */
  495. /* quantize the prediction difference */
  496. y = step_size(state_ptr); /* quantizer step size */
  497. i = quantize(d, y, qtab_721, 7); /* i = G726 code */
  498. dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized est diff */
  499. sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
  500. dqsez = sr + sez - se; /* pole prediction diff. */
  501. update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
  502. return (i);
  503. }
  504. /*
  505. * Private workspace for translating signed linear signals to G726.
  506. */
  507. struct g726_encoder_pvt
  508. {
  509. struct ast_frame f;
  510. char offset[AST_FRIENDLY_OFFSET]; /* Space to build offset */
  511. unsigned char outbuf[BUFFER_SIZE]; /* Encoded G726, two nibbles to a word */
  512. unsigned char next_flag;
  513. struct g726_state g726;
  514. int tail;
  515. };
  516. /*
  517. * Private workspace for translating G726 signals to signed linear.
  518. */
  519. struct g726_decoder_pvt
  520. {
  521. struct ast_frame f;
  522. char offset[AST_FRIENDLY_OFFSET]; /* Space to build offset */
  523. short outbuf[BUFFER_SIZE]; /* Decoded signed linear values */
  524. struct g726_state g726;
  525. int tail;
  526. };
  527. /*
  528. * G726ToLin_New
  529. * Create a new instance of g726_decoder_pvt.
  530. *
  531. * Results:
  532. * Returns a pointer to the new instance.
  533. *
  534. * Side effects:
  535. * None.
  536. */
  537. static struct ast_translator_pvt *
  538. g726tolin_new (void)
  539. {
  540. struct g726_decoder_pvt *tmp;
  541. tmp = malloc (sizeof (struct g726_decoder_pvt));
  542. if (tmp)
  543. {
  544. memset(tmp, 0, sizeof(*tmp));
  545. tmp->tail = 0;
  546. localusecnt++;
  547. g726_init_state(&tmp->g726);
  548. ast_update_use_count ();
  549. }
  550. return (struct ast_translator_pvt *) tmp;
  551. }
  552. /*
  553. * LinToG726_New
  554. * Create a new instance of g726_encoder_pvt.
  555. *
  556. * Results:
  557. * Returns a pointer to the new instance.
  558. *
  559. * Side effects:
  560. * None.
  561. */
  562. static struct ast_translator_pvt *
  563. lintog726_new (void)
  564. {
  565. struct g726_encoder_pvt *tmp;
  566. tmp = malloc (sizeof (struct g726_encoder_pvt));
  567. if (tmp)
  568. {
  569. memset(tmp, 0, sizeof(*tmp));
  570. localusecnt++;
  571. tmp->tail = 0;
  572. g726_init_state(&tmp->g726);
  573. ast_update_use_count ();
  574. }
  575. return (struct ast_translator_pvt *) tmp;
  576. }
  577. /*
  578. * G726ToLin_FrameIn
  579. * Fill an input buffer with packed 4-bit G726 values if there is room
  580. * left.
  581. *
  582. * Results:
  583. * Foo
  584. *
  585. * Side effects:
  586. * tmp->tail is the number of packed values in the buffer.
  587. */
  588. static int
  589. g726tolin_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
  590. {
  591. struct g726_decoder_pvt *tmp = (struct g726_decoder_pvt *) pvt;
  592. unsigned char *b;
  593. int x;
  594. b = f->data;
  595. for (x=0;x<f->datalen;x++) {
  596. if (tmp->tail >= BUFFER_SIZE) {
  597. ast_log(LOG_WARNING, "Out of buffer space!\n");
  598. return -1;
  599. }
  600. tmp->outbuf[tmp->tail++] = g726_decode((b[x] >> 4) & 0xf, &tmp->g726);
  601. if (tmp->tail >= BUFFER_SIZE) {
  602. ast_log(LOG_WARNING, "Out of buffer space!\n");
  603. return -1;
  604. }
  605. tmp->outbuf[tmp->tail++] = g726_decode(b[x] & 0x0f, &tmp->g726);
  606. }
  607. return 0;
  608. }
  609. /*
  610. * G726ToLin_FrameOut
  611. * Convert 4-bit G726 encoded signals to 16-bit signed linear.
  612. *
  613. * Results:
  614. * Converted signals are placed in tmp->f.data, tmp->f.datalen
  615. * and tmp->f.samples are calculated.
  616. *
  617. * Side effects:
  618. * None.
  619. */
  620. static struct ast_frame *
  621. g726tolin_frameout (struct ast_translator_pvt *pvt)
  622. {
  623. struct g726_decoder_pvt *tmp = (struct g726_decoder_pvt *) pvt;
  624. if (!tmp->tail)
  625. return NULL;
  626. tmp->f.frametype = AST_FRAME_VOICE;
  627. tmp->f.subclass = AST_FORMAT_SLINEAR;
  628. tmp->f.datalen = tmp->tail * 2;
  629. tmp->f.samples = tmp->tail;
  630. tmp->f.mallocd = 0;
  631. tmp->f.offset = AST_FRIENDLY_OFFSET;
  632. tmp->f.src = __PRETTY_FUNCTION__;
  633. tmp->f.data = tmp->outbuf;
  634. tmp->tail = 0;
  635. return &tmp->f;
  636. }
  637. /*
  638. * LinToG726_FrameIn
  639. * Fill an input buffer with 16-bit signed linear PCM values.
  640. *
  641. * Results:
  642. * None.
  643. *
  644. * Side effects:
  645. * tmp->tail is number of signal values in the input buffer.
  646. */
  647. static int
  648. lintog726_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
  649. {
  650. struct g726_encoder_pvt *tmp = (struct g726_encoder_pvt *) pvt;
  651. short *s = f->data;
  652. int samples = f->datalen / 2;
  653. int x;
  654. for (x=0;x<samples;x++) {
  655. if (tmp->next_flag & 0x80) {
  656. if (tmp->tail >= BUFFER_SIZE) {
  657. ast_log(LOG_WARNING, "Out of buffer space\n");
  658. return -1;
  659. }
  660. tmp->outbuf[tmp->tail++] = ((tmp->next_flag & 0xf)<< 4) | g726_encode(s[x], &tmp->g726);
  661. tmp->next_flag = 0;
  662. } else {
  663. tmp->next_flag = 0x80 | g726_encode(s[x], &tmp->g726);
  664. }
  665. }
  666. return 0;
  667. }
  668. /*
  669. * LinToG726_FrameOut
  670. * Convert a buffer of raw 16-bit signed linear PCM to a buffer
  671. * of 4-bit G726 packed two to a byte (Big Endian).
  672. *
  673. * Results:
  674. * Foo
  675. *
  676. * Side effects:
  677. * Leftover inbuf data gets packed, tail gets updated.
  678. */
  679. static struct ast_frame *
  680. lintog726_frameout (struct ast_translator_pvt *pvt)
  681. {
  682. struct g726_encoder_pvt *tmp = (struct g726_encoder_pvt *) pvt;
  683. if (!tmp->tail)
  684. return NULL;
  685. tmp->f.frametype = AST_FRAME_VOICE;
  686. tmp->f.subclass = AST_FORMAT_G726;
  687. tmp->f.samples = tmp->tail * 2;
  688. tmp->f.mallocd = 0;
  689. tmp->f.offset = AST_FRIENDLY_OFFSET;
  690. tmp->f.src = __PRETTY_FUNCTION__;
  691. tmp->f.data = tmp->outbuf;
  692. tmp->f.datalen = tmp->tail;
  693. tmp->tail = 0;
  694. return &tmp->f;
  695. }
  696. /*
  697. * G726ToLin_Sample
  698. */
  699. static struct ast_frame *
  700. g726tolin_sample (void)
  701. {
  702. static struct ast_frame f;
  703. f.frametype = AST_FRAME_VOICE;
  704. f.subclass = AST_FORMAT_G726;
  705. f.datalen = sizeof (g726_slin_ex);
  706. f.samples = sizeof(g726_slin_ex) * 2;
  707. f.mallocd = 0;
  708. f.offset = 0;
  709. f.src = __PRETTY_FUNCTION__;
  710. f.data = g726_slin_ex;
  711. return &f;
  712. }
  713. /*
  714. * LinToG726_Sample
  715. */
  716. static struct ast_frame *
  717. lintog726_sample (void)
  718. {
  719. static struct ast_frame f;
  720. f.frametype = AST_FRAME_VOICE;
  721. f.subclass = AST_FORMAT_SLINEAR;
  722. f.datalen = sizeof (slin_g726_ex);
  723. /* Assume 8000 Hz */
  724. f.samples = sizeof (slin_g726_ex) / 2;
  725. f.mallocd = 0;
  726. f.offset = 0;
  727. f.src = __PRETTY_FUNCTION__;
  728. f.data = slin_g726_ex;
  729. return &f;
  730. }
  731. /*
  732. * G726_Destroy
  733. * Destroys a private workspace.
  734. *
  735. * Results:
  736. * It's gone!
  737. *
  738. * Side effects:
  739. * None.
  740. */
  741. static void
  742. g726_destroy (struct ast_translator_pvt *pvt)
  743. {
  744. free (pvt);
  745. localusecnt--;
  746. ast_update_use_count ();
  747. }
  748. /*
  749. * The complete translator for G726ToLin.
  750. */
  751. static struct ast_translator g726tolin = {
  752. "g726tolin",
  753. AST_FORMAT_G726,
  754. AST_FORMAT_SLINEAR,
  755. g726tolin_new,
  756. g726tolin_framein,
  757. g726tolin_frameout,
  758. g726_destroy,
  759. /* NULL */
  760. g726tolin_sample
  761. };
  762. /*
  763. * The complete translator for LinToG726.
  764. */
  765. static struct ast_translator lintog726 = {
  766. "lintog726",
  767. AST_FORMAT_SLINEAR,
  768. AST_FORMAT_G726,
  769. lintog726_new,
  770. lintog726_framein,
  771. lintog726_frameout,
  772. g726_destroy,
  773. /* NULL */
  774. lintog726_sample
  775. };
  776. int
  777. unload_module (void)
  778. {
  779. int res;
  780. ast_mutex_lock (&localuser_lock);
  781. res = ast_unregister_translator (&lintog726);
  782. if (!res)
  783. res = ast_unregister_translator (&g726tolin);
  784. if (localusecnt)
  785. res = -1;
  786. ast_mutex_unlock (&localuser_lock);
  787. return res;
  788. }
  789. int
  790. load_module (void)
  791. {
  792. int res;
  793. res = ast_register_translator (&g726tolin);
  794. if (!res)
  795. res = ast_register_translator (&lintog726);
  796. else
  797. ast_unregister_translator (&g726tolin);
  798. return res;
  799. }
  800. /*
  801. * Return a description of this module.
  802. */
  803. char *
  804. description (void)
  805. {
  806. return tdesc;
  807. }
  808. int
  809. usecount (void)
  810. {
  811. int res;
  812. STANDARD_USECOUNT (res);
  813. return res;
  814. }
  815. char *
  816. key ()
  817. {
  818. return ASTERISK_GPL_KEY;
  819. }