codec_g726.c 23 KB

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