g721.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 value of linear PCM from sl and returns
  73. * the resulting code.
  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. */
  105. int g721_decoder(int i, struct g72x_state* state_ptr)
  106. {
  107. short sezi, sei, sez, se; /* ACCUM */
  108. short y; /* MIX */
  109. short sr; /* ADDB */
  110. short dq;
  111. short dqsez;
  112. i &= 0x0f; /* mask to get proper bits */
  113. sezi = predictor_zero(state_ptr);
  114. sez = sezi >> 1;
  115. sei = sezi + predictor_pole(state_ptr);
  116. se = sei >> 1; /* se = estimated signal */
  117. y = step_size(state_ptr); /* dynamic quantizer step size */
  118. dq = reconstruct(i & 0x08, _dqlntab[i], y); /* quantized diff. */
  119. sr = (dq < 0) ? (se - (dq & 0x3FFF)) : se + dq; /* reconst. signal */
  120. dqsez = sr - se + sez; /* pole prediction diff. */
  121. update(4, y, _witab[i] << 5, _fitab[i], dq, sr, dqsez, state_ptr);
  122. return (sr << 2);
  123. }
  124. static short power2[15] = { 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
  125. 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000 };
  126. /*
  127. * quan()
  128. *
  129. * quantizes the input val against the table of size short integers.
  130. * It returns i if table[i - 1] <= val < table[i].
  131. *
  132. * Using linear search for simple coding.
  133. */
  134. static int quan(int val, short* table, int size)
  135. {
  136. int i;
  137. for (i = 0; i < size; i++)
  138. if (val < *table++)
  139. break;
  140. return (i);
  141. }
  142. /*
  143. * fmult()
  144. *
  145. * returns the integer product of the 14-bit integer "an" and
  146. * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
  147. */
  148. static int fmult(int an, int srn)
  149. {
  150. short anmag, anexp, anmant;
  151. short wanexp, wanmant;
  152. short retval;
  153. anmag = (an > 0) ? an : ((-an) & 0x1FFF);
  154. anexp = quan(anmag, power2, 15) - 6;
  155. anmant = (anmag == 0) ? 32 : (anexp >= 0) ? anmag >> anexp : anmag << -anexp;
  156. wanexp = anexp + ((srn >> 6) & 0xF) - 13;
  157. wanmant = (anmant * (srn & 077) + 0x30) >> 4;
  158. retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) : (wanmant >> -wanexp);
  159. return (((an ^ srn) < 0) ? -retval : retval);
  160. }
  161. /*
  162. * update()
  163. *
  164. * updates the state variables for each output code
  165. */
  166. void update(int code_size, /* distinguish 723_40 with others */
  167. int y, /* quantizer step size */
  168. int wi, /* scale factor multiplier */
  169. int fi, /* for long/short term energies */
  170. int dq, /* quantized prediction difference */
  171. int sr, /* reconstructed signal */
  172. int dqsez, /* difference from 2-pole predictor */
  173. struct g72x_state* state_ptr) /* coder state pointer */
  174. {
  175. int cnt;
  176. short mag, exp; /* Adaptive predictor, FLOAT A */
  177. short a2p = 0; /* LIMC */
  178. short a1ul; /* UPA1 */
  179. short pks1; /* UPA2 */
  180. short fa1;
  181. char tr; /* tone/transition detector */
  182. short ylint, thr2, dqthr;
  183. short ylfrac, thr1;
  184. short pk0;
  185. pk0 = (dqsez < 0) ? 1 : 0; /* needed in updating predictor poles */
  186. mag = dq & 0x7FFF; /* prediction difference magnitude */
  187. /* TRANS */
  188. ylint = state_ptr->yl >> 15; /* exponent part of yl */
  189. ylfrac = (state_ptr->yl >> 10) & 0x1F; /* fractional part of yl */
  190. thr1 = (32 + ylfrac) << ylint; /* threshold */
  191. thr2 = (ylint > 9) ? 31 << 10 : thr1; /* limit thr2 to 31 << 10 */
  192. dqthr = (thr2 + (thr2 >> 1)) >> 1; /* dqthr = 0.75 * thr2 */
  193. if (state_ptr->td == 0) /* signal supposed voice */
  194. tr = 0;
  195. else if (mag <= dqthr) /* supposed data, but small mag */
  196. tr = 0; /* treated as voice */
  197. else /* signal is data (modem) */
  198. tr = 1;
  199. /*
  200. * Quantizer scale factor adaptation.
  201. */
  202. /* FUNCTW & FILTD & DELAY */
  203. /* update non-steady state step size multiplier */
  204. state_ptr->yu = y + ((wi - y) >> 5);
  205. /* LIMB */
  206. if (state_ptr->yu < 544) /* 544 <= yu <= 5120 */
  207. state_ptr->yu = 544;
  208. else if (state_ptr->yu > 5120)
  209. state_ptr->yu = 5120;
  210. /* FILTE & DELAY */
  211. /* update steady state step size multiplier */
  212. state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6);
  213. /*
  214. * Adaptive predictor coefficients.
  215. */
  216. if (tr == 1) { /* reset a's and b's for modem signal */
  217. state_ptr->a[0] = 0;
  218. state_ptr->a[1] = 0;
  219. state_ptr->b[0] = 0;
  220. state_ptr->b[1] = 0;
  221. state_ptr->b[2] = 0;
  222. state_ptr->b[3] = 0;
  223. state_ptr->b[4] = 0;
  224. state_ptr->b[5] = 0;
  225. } else { /* update a's and b's */
  226. pks1 = pk0 ^ state_ptr->pk[0]; /* UPA2 */
  227. /* update predictor pole a[1] */
  228. a2p = state_ptr->a[1] - (state_ptr->a[1] >> 7);
  229. if (dqsez != 0) {
  230. fa1 = (pks1) ? state_ptr->a[0] : -state_ptr->a[0];
  231. if (fa1 < -8191) /* a2p = function of fa1 */
  232. a2p -= 0x100;
  233. else if (fa1 > 8191)
  234. a2p += 0xFF;
  235. else
  236. a2p += fa1 >> 5;
  237. if (pk0 ^ state_ptr->pk[1])
  238. /* LIMC */
  239. if (a2p <= -12160)
  240. a2p = -12288;
  241. else if (a2p >= 12416)
  242. a2p = 12288;
  243. else
  244. a2p -= 0x80;
  245. else if (a2p <= -12416)
  246. a2p = -12288;
  247. else if (a2p >= 12160)
  248. a2p = 12288;
  249. else
  250. a2p += 0x80;
  251. }
  252. /* TRIGB & DELAY */
  253. state_ptr->a[1] = a2p;
  254. /* UPA1 */
  255. /* update predictor pole a[0] */
  256. state_ptr->a[0] -= state_ptr->a[0] >> 8;
  257. if (dqsez != 0) {
  258. if (pks1 == 0)
  259. state_ptr->a[0] += 192;
  260. else
  261. state_ptr->a[0] -= 192;
  262. }
  263. /* LIMD */
  264. a1ul = 15360 - a2p;
  265. if (state_ptr->a[0] < -a1ul)
  266. state_ptr->a[0] = -a1ul;
  267. else if (state_ptr->a[0] > a1ul)
  268. state_ptr->a[0] = a1ul;
  269. /* UPB : update predictor zeros b[6] */
  270. for (cnt = 0; cnt < 6; cnt++) {
  271. if (code_size == 5) /* for 40Kbps G.723 */
  272. state_ptr->b[cnt] -= state_ptr->b[cnt] >> 9;
  273. else /* for G.721 and 24Kbps G.723 */
  274. state_ptr->b[cnt] -= state_ptr->b[cnt] >> 8;
  275. if (dq & 0x7FFF) { /* XOR */
  276. if ((dq ^ state_ptr->dq[cnt]) >= 0)
  277. state_ptr->b[cnt] += 128;
  278. else
  279. state_ptr->b[cnt] -= 128;
  280. }
  281. }
  282. }
  283. for (cnt = 5; cnt > 0; cnt--)
  284. state_ptr->dq[cnt] = state_ptr->dq[cnt - 1];
  285. /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
  286. if (mag == 0) {
  287. state_ptr->dq[0] = (dq >= 0) ? 0x20 : 0xFC20;
  288. } else {
  289. exp = quan(mag, power2, 15);
  290. state_ptr->dq[0] = (dq >= 0) ? (exp << 6) + ((mag << 6) >> exp)
  291. : (exp << 6) + ((mag << 6) >> exp) - 0x400;
  292. }
  293. state_ptr->sr[1] = state_ptr->sr[0];
  294. /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
  295. if (sr == 0) {
  296. state_ptr->sr[0] = 0x20;
  297. } else if (sr > 0) {
  298. exp = quan(sr, power2, 15);
  299. state_ptr->sr[0] = (exp << 6) + ((sr << 6) >> exp);
  300. } else if (sr > -32768) {
  301. mag = -sr;
  302. exp = quan(mag, power2, 15);
  303. state_ptr->sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400;
  304. } else
  305. state_ptr->sr[0] = 0xFC20;
  306. /* DELAY A */
  307. state_ptr->pk[1] = state_ptr->pk[0];
  308. state_ptr->pk[0] = pk0;
  309. /* TONE */
  310. if (tr == 1) /* this sample has been treated as data */
  311. state_ptr->td = 0; /* next one will be treated as voice */
  312. else if (a2p < -11776) /* small sample-to-sample correlation */
  313. state_ptr->td = 1; /* signal may be data */
  314. else /* signal is voice */
  315. state_ptr->td = 0;
  316. /*
  317. * Adaptation speed control.
  318. */
  319. state_ptr->dms += (fi - state_ptr->dms) >> 5; /* FILTA */
  320. state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7); /* FILTB */
  321. if (tr == 1)
  322. state_ptr->ap = 256;
  323. else if (y < 1536) /* SUBTC */
  324. state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
  325. else if (state_ptr->td == 1)
  326. state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
  327. else if (abs((state_ptr->dms << 2) - state_ptr->dml) >= (state_ptr->dml >> 3))
  328. state_ptr->ap += (0x200 - state_ptr->ap) >> 4;
  329. else
  330. state_ptr->ap += (-state_ptr->ap) >> 4;
  331. }
  332. /*
  333. * g72x_init_state()
  334. *
  335. * This routine initializes and/or resets the g72x_state structure
  336. * pointed to by 'state_ptr'.
  337. * All the initial state values are specified in the CCITT G.721 document.
  338. */
  339. void g72x_init_state(struct g72x_state* state_ptr)
  340. {
  341. int cnta;
  342. state_ptr->yl = 34816;
  343. state_ptr->yu = 544;
  344. state_ptr->dms = 0;
  345. state_ptr->dml = 0;
  346. state_ptr->ap = 0;
  347. for (cnta = 0; cnta < 2; cnta++) {
  348. state_ptr->a[cnta] = 0;
  349. state_ptr->pk[cnta] = 0;
  350. state_ptr->sr[cnta] = 32;
  351. }
  352. for (cnta = 0; cnta < 6; cnta++) {
  353. state_ptr->b[cnta] = 0;
  354. state_ptr->dq[cnta] = 32;
  355. }
  356. state_ptr->td = 0;
  357. }
  358. /*
  359. * predictor_zero()
  360. *
  361. * computes the estimated signal from 6-zero predictor.
  362. *
  363. */
  364. int predictor_zero(struct g72x_state* state_ptr)
  365. {
  366. int i;
  367. int sezi;
  368. sezi = fmult(state_ptr->b[0] >> 2, state_ptr->dq[0]);
  369. for (i = 1; i < 6; i++) /* ACCUM */
  370. sezi += fmult(state_ptr->b[i] >> 2, state_ptr->dq[i]);
  371. return (sezi);
  372. }
  373. /*
  374. * predictor_pole()
  375. *
  376. * computes the estimated signal from 2-pole predictor.
  377. *
  378. */
  379. int predictor_pole(struct g72x_state* state_ptr)
  380. {
  381. return (fmult(state_ptr->a[1] >> 2, state_ptr->sr[1]) +
  382. fmult(state_ptr->a[0] >> 2, state_ptr->sr[0]));
  383. }
  384. /*
  385. * step_size()
  386. *
  387. * computes the quantization step size of the adaptive quantizer.
  388. *
  389. */
  390. int step_size(struct g72x_state* state_ptr)
  391. {
  392. int y;
  393. int dif;
  394. int al;
  395. if (state_ptr->ap >= 256)
  396. return (state_ptr->yu);
  397. else {
  398. y = state_ptr->yl >> 6;
  399. dif = state_ptr->yu - y;
  400. al = state_ptr->ap >> 2;
  401. if (dif > 0)
  402. y += (dif * al) >> 6;
  403. else if (dif < 0)
  404. y += (dif * al + 0x3F) >> 6;
  405. return (y);
  406. }
  407. }
  408. /*
  409. * quantize()
  410. *
  411. * Given a raw sample, 'd', of the difference signal and a
  412. * quantization step size scale factor, 'y', this routine returns the
  413. * ADPCM codeword to which that sample gets quantized. The step
  414. * size scale factor division operation is done in the log base 2 domain
  415. * as a subtraction.
  416. */
  417. int quantize(int d, /* Raw difference signal sample */
  418. int y, /* Step size multiplier */
  419. short* table, /* quantization table */
  420. int size) /* table size of short integers */
  421. {
  422. short dqm; /* Magnitude of 'd' */
  423. short exp; /* Integer part of base 2 log of 'd' */
  424. short mant; /* Fractional part of base 2 log */
  425. short dl; /* Log of magnitude of 'd' */
  426. short dln; /* Step size scale factor normalized log */
  427. int i;
  428. /*
  429. * LOG
  430. *
  431. * Compute base 2 log of 'd', and store in 'dl'.
  432. */
  433. dqm = abs(d);
  434. exp = quan(dqm >> 1, power2, 15);
  435. mant = ((dqm << 7) >> exp) & 0x7F; /* Fractional portion. */
  436. dl = (exp << 7) + mant;
  437. /*
  438. * SUBTB
  439. *
  440. * "Divide" by step size multiplier.
  441. */
  442. dln = dl - (y >> 2);
  443. /*
  444. * QUAN
  445. *
  446. * Obtain codword i for 'd'.
  447. */
  448. i = quan(dln, table, size);
  449. if (d < 0) /* take 1's complement of i */
  450. return ((size << 1) + 1 - i);
  451. else if (i == 0) /* take 1's complement of 0 */
  452. return ((size << 1) + 1); /* new in 1988 */
  453. else
  454. return (i);
  455. }
  456. /*
  457. * reconstruct()
  458. *
  459. * Returns reconstructed difference signal 'dq' obtained from
  460. * codeword 'i' and quantization step size scale factor 'y'.
  461. * Multiplication is performed in log base 2 domain as addition.
  462. */
  463. int reconstruct(int sign, /* 0 for non-negative value */
  464. int dqln, /* G.72x codeword */
  465. int y) /* Step size multiplier */
  466. {
  467. short dql; /* Log of 'dq' magnitude */
  468. short dex; /* Integer part of log */
  469. short dqt;
  470. short dq; /* Reconstructed difference signal sample */
  471. dql = dqln + (y >> 2); /* ADDA */
  472. if (dql < 0) {
  473. return ((sign) ? -0x8000 : 0);
  474. } else { /* ANTILOG */
  475. dex = (dql >> 7) & 15;
  476. dqt = 128 + (dql & 127);
  477. dq = (dqt << 7) >> (14 - dex);
  478. return ((sign) ? (dq - 0x8000) : dq);
  479. }
  480. }