g721.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * This source code is a product of Sun Microsystems, Inc. and is provided
  3. * for unrestricted use. Users may copy or modify this source code without
  4. * charge.
  5. *
  6. * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
  7. * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  8. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  9. *
  10. * Sun source code is provided with no support and without any obligation on
  11. * the part of Sun Microsystems, Inc. to assist in its use, correction,
  12. * modification or enhancement.
  13. *
  14. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  15. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
  16. * OR ANY PART THEREOF.
  17. *
  18. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  19. * or profits or other special, indirect and consequential damages, even if
  20. * Sun has been advised of the possibility of such damages.
  21. *
  22. * Sun Microsystems, Inc.
  23. * 2550 Garcia Avenue
  24. * Mountain View, California 94043
  25. */
  26. /*
  27. * g721.c
  28. *
  29. * Description:
  30. *
  31. * g721_encoder(), g721_decoder()
  32. *
  33. * These routines comprise an implementation of the CCITT G.721 ADPCM
  34. * coding algorithm. Essentially, this implementation is identical to
  35. * the bit level description except for a few deviations which
  36. * take advantage of work station attributes, such as hardware 2's
  37. * complement arithmetic and large memory. Specifically, certain time
  38. * consuming operations such as multiplications are replaced
  39. * with lookup tables and software 2's complement operations are
  40. * replaced with hardware 2's complement.
  41. *
  42. * The deviation from the bit level specification (lookup tables)
  43. * preserves the bit level performance specifications.
  44. *
  45. * As outlined in the G.721 Recommendation, the algorithm is broken
  46. * down into modules. Each section of code below is preceded by
  47. * the name of the module which it is implementing.
  48. *
  49. */
  50. #include "g721.h"
  51. #include <stdlib.h>
  52. static short qtab_721[7] = { -124, 80, 178, 246, 300, 349, 400 };
  53. /*
  54. * Maps G.721 code word to reconstructed scale factor normalized log
  55. * magnitude values.
  56. */
  57. static short _dqlntab[16] = { -2048, 4, 135, 213, 273, 323, 373, 425,
  58. 425, 373, 323, 273, 213, 135, 4, -2048 };
  59. /* Maps G.721 code word to log of scale factor multiplier. */
  60. static short _witab[16] = { -12, 18, 41, 64, 112, 198, 355, 1122,
  61. 1122, 355, 198, 112, 64, 41, 18, -12 };
  62. /*
  63. * Maps G.721 code words to a set of values whose long and short
  64. * term averages are computed and then compared to give an indication
  65. * how stationary (steady state) the signal is.
  66. */
  67. static short _fitab[16] = { 0, 0, 0, 0x200, 0x200, 0x200, 0x600, 0xE00,
  68. 0xE00, 0x600, 0x200, 0x200, 0x200, 0, 0, 0 };
  69. /*
  70. * g721_encoder()
  71. *
  72. * Encodes the input vale of linear PCM, A-law or u-law data sl and returns
  73. * the resulting code. -1 is returned for unknown input coding value.
  74. */
  75. int g721_encoder(int sl, struct g72x_state* state_ptr)
  76. {
  77. short sezi, se, sez; /* ACCUM */
  78. short d; /* SUBTA */
  79. short sr; /* ADDB */
  80. short y; /* MIX */
  81. short dqsez; /* ADDC */
  82. short dq, i;
  83. sl >>= 2; /* linearize input sample to 14-bit PCM */
  84. sezi = predictor_zero(state_ptr);
  85. sez = sezi >> 1;
  86. se = (sezi + predictor_pole(state_ptr)) >> 1; /* estimated signal */
  87. d = sl - se; /* estimation difference */
  88. /* quantize the prediction difference */
  89. y = step_size(state_ptr); /* quantizer step size */
  90. i = quantize(d, y, qtab_721, 7); /* i = ADPCM code */
  91. dq = reconstruct(i & 8, _dqlntab[i], y); /* quantized est diff */
  92. sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconst. signal */
  93. dqsez = sr + sez - se; /* pole prediction diff. */
  94. update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
  95. return (i);
  96. }
  97. /*
  98. * g721_decoder()
  99. *
  100. * Description:
  101. *
  102. * Decodes a 4-bit code of G.721 encoded data of i and
  103. * returns the resulting linear PCM
  104. * return -1 for unknown out_coding value.
  105. */
  106. int g721_decoder(int i, struct g72x_state* state_ptr)
  107. {
  108. short sezi, sei, sez, se; /* ACCUM */
  109. short y; /* MIX */
  110. short sr; /* ADDB */
  111. short dq;
  112. short dqsez;
  113. i &= 0x0f; /* mask to get proper bits */
  114. sezi = predictor_zero(state_ptr);
  115. sez = sezi >> 1;
  116. sei = sezi + predictor_pole(state_ptr);
  117. se = sei >> 1; /* se = estimated signal */
  118. y = step_size(state_ptr); /* dynamic quantizer step size */
  119. dq = reconstruct(i & 0x08, _dqlntab[i], y); /* quantized diff. */
  120. sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq; /* reconst. signal */
  121. dqsez = sr - se + sez; /* pole prediction diff. */
  122. update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
  123. return (sr << 2);
  124. }
  125. static short power2[15] = { 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
  126. 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000 };
  127. /*
  128. * quan()
  129. *
  130. * quantizes the input val against the table of size short integers.
  131. * It returns i if table[i - 1] <= val < table[i].
  132. *
  133. * Using linear search for simple coding.
  134. */
  135. static int quan(int val, short* table, int size)
  136. {
  137. int i;
  138. for (i = 0; i < size; i++)
  139. if (val < *table++)
  140. break;
  141. return (i);
  142. }
  143. /*
  144. * fmult()
  145. *
  146. * returns the integer product of the 14-bit integer "an" and
  147. * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
  148. */
  149. static int fmult(int an, int srn)
  150. {
  151. short anmag, anexp, anmant;
  152. short wanexp, wanmant;
  153. short retval;
  154. anmag = (an > 0) ? an : ((-an) & 0x1FFF);
  155. anexp = quan(anmag, power2, 15) - 6;
  156. anmant = (anmag == 0) ? 32 : (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
  157. wanexp = anexp + ((srn >> 6) & 0xF) - 13;
  158. wanmant = (anmant * (srn & 077) + 0x30) >> 4;
  159. retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) : (wanmant >> -wanexp);
  160. return (((an ^ srn) < 0) ? -retval : retval);
  161. }
  162. /*
  163. * update()
  164. *
  165. * updates the state variables for each output code
  166. */
  167. void update(int code_size, /* distinguish 723_40 with others */
  168. int y, /* quantizer step size */
  169. int wi, /* scale factor multiplier */
  170. int fi, /* for long/short term energies */
  171. int dq, /* quantized prediction difference */
  172. int sr, /* reconstructed signal */
  173. int dqsez, /* difference from 2-pole predictor */
  174. struct g72x_state* state_ptr) /* coder state pointer */
  175. {
  176. int cnt;
  177. short mag, exp; /* Adaptive predictor, FLOAT A */
  178. short a2p = 0; /* LIMC */
  179. short a1ul; /* UPA1 */
  180. short pks1; /* UPA2 */
  181. short fa1;
  182. char tr; /* tone/transition detector */
  183. short ylint, thr2, dqthr;
  184. short ylfrac, thr1;
  185. short pk0;
  186. pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
  187. mag = dq & 0x7FFF; /* prediction difference magnitude */
  188. /* TRANS */
  189. ylint = state_ptr->yl >> 15; /* exponent part of yl */
  190. ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
  191. thr1 = (32 + ylfrac) << ylint; /* threshold */
  192. thr2 = (ylint > 9) ? 31 << 10 : thr1; /* limit thr2 to 31 << 10 */
  193. dqthr = (thr2 + (thr2 >> 1)) >> 1; /* dqthr = 0.75 * thr2 */
  194. if (state_ptr->td == 0) /* signal supposed voice */
  195. tr = 0;
  196. else if (mag <= dqthr) /* supposed data, but small mag */
  197. tr = 0; /* treated as voice */
  198. else /* signal is data (modem) */
  199. tr = 1;
  200. /*
  201. * Quantizer scale factor adaptation.
  202. */
  203. /* FUNCTW & FILTD & DELAY */
  204. /* update non-steady state step size multiplier */
  205. state_ptr->yu = y + ((wi - y) >> 5);
  206. /* LIMB */
  207. if (state_ptr->yu < 544) /* 544 <= yu <= 5120 */
  208. state_ptr->yu = 544;
  209. else if (state_ptr->yu > 5120)
  210. state_ptr->yu = 5120;
  211. /* FILTE & DELAY */
  212. /* update steady state step size multiplier */
  213. state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
  214. /*
  215. * Adaptive predictor coefficients.
  216. */
  217. if (tr == 1) { /* reset a's and b's for modem signal */
  218. state_ptr->a[0] = 0;
  219. state_ptr->a[1] = 0;
  220. state_ptr->b[0] = 0;
  221. state_ptr->b[1] = 0;
  222. state_ptr->b[2] = 0;
  223. state_ptr->b[3] = 0;
  224. state_ptr->b[4] = 0;
  225. state_ptr->b[5] = 0;
  226. } else { /* update a's and b's */
  227. pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */
  228. /* update predictor pole a[1] */
  229. a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
  230. if (dqsez != 0) {
  231. fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
  232. if (fa1 < -8191) /* a2p = function of fa1 */
  233. a2p -= 0x100;
  234. else if (fa1 > 8191)
  235. a2p += 0xFF;
  236. else
  237. a2p += fa1 >> 5;
  238. if (pk0 ^ state_ptr->pk[1])
  239. /* LIMC */
  240. if (a2p <= -12160)
  241. a2p = -12288;
  242. else if (a2p >= 12416)
  243. a2p = 12288;
  244. else
  245. a2p -= 0x80;
  246. else if (a2p <= -12416)
  247. a2p = -12288;
  248. else if (a2p >= 12160)
  249. a2p = 12288;
  250. else
  251. a2p += 0x80;
  252. }
  253. /* TRIGB & DELAY */
  254. state_ptr->a[1] = a2p;
  255. /* UPA1 */
  256. /* update predictor pole a[0] */
  257. state_ptr->a[0] -= state_ptr->a[0] >> 8;
  258. if (dqsez != 0) {
  259. if (pks1 == 0)
  260. state_ptr->a[0] += 192;
  261. else
  262. state_ptr->a[0] -= 192;
  263. }
  264. /* LIMD */
  265. a1ul = 15360 - a2p;
  266. if (state_ptr->a[0] < -a1ul)
  267. state_ptr->a[0] = -a1ul;
  268. else if (state_ptr->a[0] > a1ul)
  269. state_ptr->a[0] = a1ul;
  270. /* UPB : update predictor zeros b[6] */
  271. for (cnt = 0; cnt < 6; cnt++) {
  272. if (code_size == 5) /* for 40Kbps G.723 */
  273. state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
  274. else /* for G.721 and 24Kbps G.723 */
  275. state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
  276. if (dq & 0x7FFF) { /* XOR */
  277. if ((dq ^ state_ptr->dq[cnt]) >= 0)
  278. state_ptr->b[cnt] += 128;
  279. else
  280. state_ptr->b[cnt] -= 128;
  281. }
  282. }
  283. }
  284. for (cnt = 5; cnt > 0; cnt--)
  285. state_ptr->dq[cnt] = state_ptr->dq[cnt - 1];
  286. /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
  287. if (mag == 0) {
  288. state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
  289. } else {
  290. exp = quan(mag, power2, 15);
  291. state_ptr->dq[0] = (dq >= 0) ? (exp << 6) + ((mag << 6) >> exp)
  292. : (exp << 6) + ((mag << 6) >> exp) - 0x400;
  293. }
  294. state_ptr->sr[1] = state_ptr->sr[0];
  295. /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
  296. if (sr == 0) {
  297. state_ptr->sr[0] = 0x20;
  298. } else if (sr > 0) {
  299. exp = quan(sr, power2, 15);
  300. state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
  301. } else if (sr > -32768) {
  302. mag = -sr;
  303. exp = quan(mag, power2, 15);
  304. state_ptr->sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400;
  305. } else
  306. state_ptr->sr[0] = 0xFC20;
  307. /* DELAY A */
  308. state_ptr->pk[1] = state_ptr->pk[0];
  309. state_ptr->pk[0] = pk0;
  310. /* TONE */
  311. if (tr == 1) /* this sample has been treated as data */
  312. state_ptr->td = 0; /* next one will be treated as voice */
  313. else if (a2p < -11776) /* small sample-to-sample correlation */
  314. state_ptr->td = 1; /* signal may be data */
  315. else /* signal is voice */
  316. state_ptr->td = 0;
  317. /*
  318. * Adaptation speed control.
  319. */
  320. state_ptr->dms += (fi - state_ptr->dms) >> 5; /* FILTA */
  321. state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7); /* FILTB */
  322. if (tr == 1)
  323. state_ptr->ap = 256;
  324. else if (y < 1536) /* SUBTC */
  325. state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
  326. else if (state_ptr->td == 1)
  327. state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
  328. else if (abs((state_ptr->dms << 2) - state_ptr->dml) >= (state_ptr->dml >> 3))
  329. state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
  330. else
  331. state_ptr->ap += (-state_ptr->ap) >> 4;
  332. }
  333. /*
  334. * g72x_init_state()
  335. *
  336. * This routine initializes and/or resets the g72x_state structure
  337. * pointed to by 'state_ptr'.
  338. * All the initial state values are specified in the CCITT G.721 document.
  339. */
  340. void g72x_init_state(struct g72x_state* state_ptr)
  341. {
  342. int cnta;
  343. state_ptr->yl = 34816;
  344. state_ptr->yu = 544;
  345. state_ptr->dms = 0;
  346. state_ptr->dml = 0;
  347. state_ptr->ap = 0;
  348. for (cnta = 0; cnta < 2; cnta++) {
  349. state_ptr->a[cnta] = 0;
  350. state_ptr->pk[cnta] = 0;
  351. state_ptr->sr[cnta] = 32;
  352. }
  353. for (cnta = 0; cnta < 6; cnta++) {
  354. state_ptr->b[cnta] = 0;
  355. state_ptr->dq[cnta] = 32;
  356. }
  357. state_ptr->td = 0;
  358. }
  359. /*
  360. * predictor_zero()
  361. *
  362. * computes the estimated signal from 6-zero predictor.
  363. *
  364. */
  365. int predictor_zero(struct g72x_state* state_ptr)
  366. {
  367. int i;
  368. int sezi;
  369. sezi = fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]);
  370. for (i = 1; i < 6; i++) /* ACCUM */
  371. sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
  372. return (sezi);
  373. }
  374. /*
  375. * predictor_pole()
  376. *
  377. * computes the estimated signal from 2-pole predictor.
  378. *
  379. */
  380. int predictor_pole(struct g72x_state* state_ptr)
  381. {
  382. return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
  383. fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
  384. }
  385. /*
  386. * step_size()
  387. *
  388. * computes the quantization step size of the adaptive quantizer.
  389. *
  390. */
  391. int step_size(struct g72x_state* state_ptr)
  392. {
  393. int y;
  394. int dif;
  395. int al;
  396. if (state_ptr->ap >= 256)
  397. return (state_ptr->yu);
  398. else {
  399. y = state_ptr->yl >> 6;
  400. dif = state_ptr->yu - y;
  401. al = state_ptr->ap >> 2;
  402. if (dif > 0)
  403. y += (dif * al) >> 6;
  404. else if (dif < 0)
  405. y += (dif * al + 0x3F) >> 6;
  406. return (y);
  407. }
  408. }
  409. /*
  410. * quantize()
  411. *
  412. * Given a raw sample, 'd', of the difference signal and a
  413. * quantization step size scale factor, 'y', this routine returns the
  414. * ADPCM codeword to which that sample gets quantized. The step
  415. * size scale factor division operation is done in the log base 2 domain
  416. * as a subtraction.
  417. */
  418. int quantize(int d, /* Raw difference signal sample */
  419. int y, /* Step size multiplier */
  420. short* table, /* quantization table */
  421. int size) /* table size of short integers */
  422. {
  423. short dqm; /* Magnitude of 'd' */
  424. short exp; /* Integer part of base 2 log of 'd' */
  425. short mant; /* Fractional part of base 2 log */
  426. short dl; /* Log of magnitude of 'd' */
  427. short dln; /* Step size scale factor normalized log */
  428. int i;
  429. /*
  430. * LOG
  431. *
  432. * Compute base 2 log of 'd', and store in 'dl'.
  433. */
  434. dqm = abs(d);
  435. exp = quan(dqm >> 1, power2, 15);
  436. mant = ((dqm << 7) >> exp) & 0x7F; /* Fractional portion. */
  437. dl = (exp << 7) + mant;
  438. /*
  439. * SUBTB
  440. *
  441. * "Divide" by step size multiplier.
  442. */
  443. dln = dl - (y >> 2);
  444. /*
  445. * QUAN
  446. *
  447. * Obtain codword i for 'd'.
  448. */
  449. i = quan(dln, table, size);
  450. if (d < 0) /* take 1's complement of i */
  451. return ((size << 1) + 1 - i);
  452. else if (i == 0) /* take 1's complement of 0 */
  453. return ((size << 1) + 1); /* new in 1988 */
  454. else
  455. return (i);
  456. }
  457. /*
  458. * reconstruct()
  459. *
  460. * Returns reconstructed difference signal 'dq' obtained from
  461. * codeword 'i' and quantization step size scale factor 'y'.
  462. * Multiplication is performed in log base 2 domain as addition.
  463. */
  464. int reconstruct(int sign, /* 0 for non-negative value */
  465. int dqln, /* G.72x codeword */
  466. int y) /* Step size multiplier */
  467. {
  468. short dql; /* Log of 'dq' magnitude */
  469. short dex; /* Integer part of log */
  470. short dqt;
  471. short dq; /* Reconstructed difference signal sample */
  472. dql = dqln + (y >> 2); /* ADDA */
  473. if (dql < 0) {
  474. return ((sign) ? -0x8000 : 0);
  475. } else { /* ANTILOG */
  476. dex = (dql >> 7) & 15;
  477. dqt = 128 + (dql & 127);
  478. dq = (dqt << 7) >> (14 - dex);
  479. return ((sign) ? (dq - 0x8000) : dq);
  480. }
  481. }