g72x.c 16 KB

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