getext1k.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /**********************************************************************
  2. Each of the companies; Lucent, Motorola, Nokia, and Qualcomm (hereinafter
  3. referred to individually as "Source" or collectively as "Sources") do
  4. hereby state:
  5. To the extent to which the Source(s) may legally and freely do so, the
  6. Source(s), upon submission of a Contribution, grant(s) a free,
  7. irrevocable, non-exclusive, license to the Third Generation Partnership
  8. Project 2 (3GPP2) and its Organizational Partners: ARIB, CCSA, TIA, TTA,
  9. and TTC, under the Source's copyright or copyright license rights in the
  10. Contribution, to, in whole or in part, copy, make derivative works,
  11. perform, display and distribute the Contribution and derivative works
  12. thereof consistent with 3GPP2's and each Organizational Partner's
  13. policies and procedures, with the right to (i) sublicense the foregoing
  14. rights consistent with 3GPP2's and each Organizational Partner's policies
  15. and procedures and (ii) copyright and sell, if applicable) in 3GPP2's name
  16. or each Organizational Partner's name any 3GPP2 or transposed Publication
  17. even though this Publication may contain the Contribution or a derivative
  18. work thereof. The Contribution shall disclose any known limitations on
  19. the Source's rights to license as herein provided.
  20. When a Contribution is submitted by the Source(s) to assist the
  21. formulating groups of 3GPP2 or any of its Organizational Partners, it
  22. is proposed to the Committee as a basis for discussion and is not to
  23. be construed as a binding proposal on the Source(s). The Source(s)
  24. specifically reserve(s) the right to amend or modify the material
  25. contained in the Contribution. Nothing contained in the Contribution
  26. shall, except as herein expressly provided, be construed as conferring
  27. by implication, estoppel or otherwise, any license or right under (i)
  28. any existing or later issuing patent, whether or not the use of
  29. information in the document necessarily employs an invention of any
  30. existing or later issued patent, (ii) any copyright, (iii) any
  31. trademark, or (iv) any other intellectual property right.
  32. With respect to the Software necessary for the practice of any or
  33. all Normative portions of the Enhanced Variable Rate Codec (EVRC) as
  34. it exists on the date of submittal of this form, should the EVRC be
  35. approved as a Specification or Report by 3GPP2, or as a transposed
  36. Standard by any of the 3GPP2's Organizational Partners, the Source(s)
  37. state(s) that a worldwide license to reproduce, use and distribute the
  38. Software, the license rights to which are held by the Source(s), will
  39. be made available to applicants under terms and conditions that are
  40. reasonable and non-discriminatory, which may include monetary compensation,
  41. and only to the extent necessary for the practice of any or all of the
  42. Normative portions of the EVRC or the field of use of practice of the
  43. EVRC Specification, Report, or Standard. The statement contained above
  44. is irrevocable and shall be binding upon the Source(s). In the event
  45. the rights of the Source(s) in and to copyright or copyright license
  46. rights subject to such commitment are assigned or transferred, the
  47. Source(s) shall notify the assignee or transferee of the existence of
  48. such commitments.
  49. *******************************************************************/
  50. /*======================================================================*/
  51. /* Enhanced Variable Rate Codec - Bit-Exact C Specification */
  52. /* Copyright (C) 1997-1998 Telecommunications Industry Association. */
  53. /* All rights reserved. */
  54. /*----------------------------------------------------------------------*/
  55. /* Note: Reproduction and use of this software for the design and */
  56. /* development of North American Wideband CDMA Digital */
  57. /* Cellular Telephony Standards is authorized by the TIA. */
  58. /* The TIA does not authorize the use of this software for any */
  59. /* other purpose. */
  60. /* */
  61. /* The availability of this software does not provide any license */
  62. /* by implication, estoppel, or otherwise under any patent rights */
  63. /* of TIA member companies or others covering any use of the */
  64. /* contents herein. */
  65. /* */
  66. /* Any copies of this software or derivative works must include */
  67. /* this and all other proprietary notices. */
  68. /*======================================================================*/
  69. /* Memory Usage: */
  70. /* ROM: 0 */
  71. /* Static/Global RAM: 9 */
  72. /* Stack/Local RAM: 4 */
  73. /*----------------------------------------------------------------------*/
  74. /****************************************************************************
  75. * Routine name: GetExc800bps. *
  76. * Function: Energy quantization of the residual signal. *
  77. * Inputs: input - signal array. *
  78. * length - size of signal array. *
  79. * Output: output - quantized signal. *
  80. ****************************************************************************/
  81. #include "macro.h"
  82. #include "rom.h"
  83. #include <stdio.h>
  84. //#include <math.h>
  85. //#include "mathevrc.h"
  86. #include "dsp_math.h"
  87. #include "mathdp31.h"
  88. #include "mathadv.h"
  89. INT16 ran0(INT16 *seed0)
  90. {
  91. INT32 Ltemp;
  92. Ltemp = 0.0;
  93. Ltemp = L_mac(27698, 25173, *seed0);
  94. Ltemp = L_shr(Ltemp, 1);
  95. Ltemp = Ltemp & 0x0000ffffL;
  96. *seed0 = extract_l(Ltemp);
  97. return (extract_h(L_shl(Ltemp,15)));
  98. }
  99. INT16 ran_g(INT16 *seed0)
  100. {
  101. static int iset = 0;
  102. static INT32 gset;
  103. INT32 rsq, ltemp1, ltemp2;
  104. INT16 sv1, sv2, rsq_s;
  105. INT16 shft_fctr, stemp1;
  106. INT16 shft_fctr1;
  107. /* ======================================================================== */
  108. INT32 ltmp1, ltmp2;
  109. INT16 ans = 0;
  110. INT16 stmp2;
  111. /* ======================================================================== */
  112. if (iset == 0)
  113. {
  114. sv1 = shl(sub(ran0(seed0), 16384), 1);
  115. sv2 = shl(sub(ran0(seed0), 16384), 1);
  116. /* rsq = sv1 * sv1 + sv2 * sv2; */
  117. ltemp1 = L_mult(sv1, sv1);
  118. ltemp2 = L_mult(sv2, sv2);
  119. rsq = L_add(L_shr(ltemp1, 1), L_shr(ltemp2, 1));
  120. if (rsq >= 1073741824 || rsq == 0){
  121. /* If condition not met, don't iterate; use */
  122. /* rough approximation. */
  123. ans = shr(sv1,3);
  124. ans = add(ans, shr(sv2,3));
  125. ans = add(ans, shr(sub(ran0(seed0), 16384),2));
  126. return (ans);
  127. }
  128. /*
  129. * error in rsq doesn't seem to contribute to the final error in ran_g
  130. */
  131. /*
  132. * rsq scale down by two: input to fnLog must be scaled up by 2.
  133. */
  134. rsq = L_shl(rsq, 1);
  135. /* stemp1 = round32(L_negate(fnLog(rsq))); */
  136. ltmp1 = L_negate(fnLog(rsq));
  137. /*
  138. * rsq must be greater than the log of lsq for the fractional
  139. * divide to work. therfore normalize rsq.
  140. */
  141. shft_fctr = norm_l(rsq);
  142. rsq_s = round32(L_shl(rsq, shft_fctr));
  143. stmp2 = (divide_s(round32(ltmp1), rsq_s));
  144. /*
  145. * stemp2 must be normalized before taking its square root.
  146. * (increases precision).
  147. */
  148. shft_fctr1 = norm_s(stmp2);
  149. ltmp2 = L_deposit_h(shl(stmp2, shft_fctr1));
  150. stemp1 = sqroot(ltmp2);
  151. /*
  152. * shifting involved before taking the square root:
  153. * LEFT << shft_fctr. (LEFT because rsq is in the denominator
  154. * of ltemp2 quotion).
  155. * LEFT << 6. (multiply by 2 in original code and multiply by 32
  156. * because output of fnLog scaled down by 32).
  157. * RIGHT >> shft_fctr1. (normalization taken before sqroot).
  158. */
  159. shft_fctr = shft_fctr + 6 - shft_fctr1;
  160. /*
  161. * PROPERTY: sqrt(2^n) = 2^(n/2)
  162. * if shft_fctr is odd; multiply stemp1 by sqrt(2)/2 and
  163. * increment number of shifts by 1. Can now use shft_fctr / 2.
  164. */
  165. if (shft_fctr & 0x0001)
  166. {
  167. stemp1 = mult(stemp1, 23170);
  168. shft_fctr++;
  169. }
  170. shft_fctr = shr(shft_fctr, 1);
  171. /*
  172. * normalize stemp1 for the following multiplication.
  173. * adjust shft_fctr accordingly.
  174. */
  175. shft_fctr1 = norm_s(stemp1);
  176. stemp1 = shl(stemp1, shft_fctr1);
  177. shft_fctr = shft_fctr - shft_fctr1;
  178. gset = L_mult(sv1, stemp1);
  179. /*
  180. * final output is scaled down by 4, therefore shift up by
  181. * shft_fctr - 2.
  182. */
  183. gset = L_shl(gset, shft_fctr - 2);
  184. iset = 1;
  185. return round32(L_shl(L_mult(sv2, stemp1), shft_fctr - 2));
  186. }
  187. else
  188. {
  189. iset = 0;
  190. return round32(gset);
  191. }
  192. }
  193. INT16 e_ran_g(INT16 *seed0)
  194. {
  195. static int iset = 0;
  196. static INT32 gset;
  197. INT32 rsq, ltemp1, ltemp2;
  198. INT16 sv1, sv2, rsq_s;
  199. INT16 shft_fctr, stemp1;
  200. INT16 shft_fctr1;
  201. /* ======================================================================== */
  202. INT32 ltmp1, ltmp2;
  203. INT16 ans = 0;
  204. INT16 stmp2;
  205. /* ======================================================================== */
  206. if (iset == 0)
  207. {
  208. sv1 = shl(sub(ran0(seed0), 16384), 1);
  209. sv2 = shl(sub(ran0(seed0), 16384), 1);
  210. /* rsq = sv1 * sv1 + sv2 * sv2; */
  211. ltemp1 = L_mult(sv1, sv1);
  212. ltemp2 = L_mult(sv2, sv2);
  213. rsq = L_add(L_shr(ltemp1, 1), L_shr(ltemp2, 1));
  214. if (rsq >= 1073741824 || rsq == 0){
  215. /* If condition not met, don't iterate; use */
  216. /* rough approximation. */
  217. ans = shr(sv1,3);
  218. ans = add(ans, shr(sv2,3));
  219. ans = add(ans, shr(sub(ran0(seed0), 16384),2));
  220. return (ans);
  221. }
  222. /*
  223. * error in rsq doesn't seem to contribute to the final error in e_ran_g
  224. */
  225. /*
  226. * rsq scale down by two: input to fnLog must be scaled up by 2.
  227. */
  228. rsq = L_shl(rsq, 1);
  229. /* stemp1 = round32(L_negate(fnLog(rsq))); */
  230. ltmp1 = L_negate(fnLog(rsq));
  231. /*
  232. * rsq must be greater than the log of lsq for the fractional
  233. * divide to work. therfore normalize rsq.
  234. */
  235. shft_fctr = norm_l(rsq);
  236. rsq_s = round32(L_shl(rsq, shft_fctr));
  237. stmp2 = (divide_s(round32(ltmp1), rsq_s));
  238. /*
  239. * stemp2 must be normalized before taking its square root.
  240. * (increases precision).
  241. */
  242. shft_fctr1 = norm_s(stmp2);
  243. ltmp2 = L_deposit_h(shl(stmp2, shft_fctr1));
  244. stemp1 = sqroot(ltmp2);
  245. /*
  246. * shifting involved before taking the square root:
  247. * LEFT << shft_fctr. (LEFT because rsq is in the denominator
  248. * of ltemp2 quotion).
  249. * LEFT << 6. (multiply by 2 in original code and multiply by 32
  250. * because output of fnLog scaled down by 32).
  251. * RIGHT >> shft_fctr1. (normalization taken before sqroot).
  252. */
  253. shft_fctr = shft_fctr + 6 - shft_fctr1;
  254. /*
  255. * PROPERTY: sqrt(2^n) = 2^(n/2)
  256. * if shft_fctr is odd; multiply stemp1 by sqrt(2)/2 and
  257. * increment number of shifts by 1. Can now use shft_fctr / 2.
  258. */
  259. if (shft_fctr & 0x0001)
  260. {
  261. stemp1 = mult(stemp1, 23170);
  262. shft_fctr++;
  263. }
  264. shft_fctr = shr(shft_fctr, 1);
  265. /*
  266. * normalize stemp1 for the following multiplication.
  267. * adjust shft_fctr accordingly.
  268. */
  269. shft_fctr1 = norm_s(stemp1);
  270. stemp1 = shl(stemp1, shft_fctr1);
  271. shft_fctr = shft_fctr - shft_fctr1;
  272. gset = L_mult(sv1, stemp1);
  273. /*
  274. * final output is scaled down by 4, therefore shift up by
  275. * shft_fctr - 2.
  276. */
  277. gset = L_shl(gset, shft_fctr - 2);
  278. iset = 1;
  279. return round32(L_shl(L_mult(sv2, stemp1), shft_fctr - 2));
  280. }
  281. else
  282. {
  283. iset = 0;
  284. return round32(gset);
  285. }
  286. }
  287. void GetExc800bps(
  288. /* 1 */ INT16 *output,
  289. /* 2 */ INT16 *best,
  290. /* 3 */ INT16 scale,
  291. /* 4 */ INT16 *input,
  292. /* 5 */ INT16 length,
  293. /* 6 */ INT16 flag,
  294. /* 7 */ INT16 n)
  295. {
  296. INT16 k, j;
  297. INT16 D;
  298. INT32 tmp;
  299. INT32 ltmp;
  300. INT16 stmp;
  301. INT16 *ptr;
  302. INT32 sum, lscale;
  303. INT16 ssum, sscale;
  304. static INT16 Seed = 1234;
  305. static INT16 Sum[NoOfSubFrames];
  306. INT16 shft_scale;
  307. INT16 shft_sum;
  308. INT16 stemp1;
  309. if (!flag)
  310. Seed = 1234;
  311. /*
  312. * sum is an integer value, not a fractional value.
  313. */
  314. /* Get energy of next sub frame */
  315. for (k = 0, sum = 0; k < length; k++)
  316. {
  317. sum = L_add(sum, L_deposit_l(abs_s(input[k])));
  318. }
  319. sum = L_shl(sum, 1); /* correct for scaling */
  320. if (sum < SubFrameSize)
  321. {
  322. sum = fnLog10(L_deposit_h(scale));
  323. sum = L_negate(L_add(L_shl(sum, 3), 484942745)); /* add (log8)/4 = 484842745 (VALUE ADJUSTED TO BETTER MATCH FLOAT MODEL) */
  324. }
  325. else
  326. {
  327. lscale = L_mult(SubFrameSize, scale);
  328. shft_scale = norm_l(lscale);
  329. sscale = round32(L_shl(lscale, shft_scale));
  330. shft_sum = norm_l(sum) - 1;
  331. ssum = round32(L_shl(sum, shft_sum));
  332. /*
  333. * The following divide/L_shl produced sum scaled down by 64.
  334. * sum can have a max value of 40.xx in the sample data.
  335. */
  336. /* sum = L_shl(L_divide(sum, lscale), (shft_scale + 7 - shft_sum)); */
  337. sum = L_deposit_h(shl(divide_s(ssum, sscale), (shft_scale - shft_sum)));
  338. /* sum = fnLog10(sum); */
  339. sum = L_add(fnLog10(sum), 141412467); /* add log (2^7) scaled down by 32 */
  340. sum = L_add(L_shl(sum, 3), 969485490); /* add (log8)/2 = 969685490 (VALUE ADJUSTED TO BETTER MATCH FLOAT MODEL) */
  341. }
  342. /*
  343. * Sum scaled down by 4.
  344. */
  345. Sum[n] = round32(sum);
  346. /* Quantize if last frame */
  347. if (n == NoOfSubFrames - 1)
  348. {
  349. /* Quantize to 8 bits */
  350. for (k = 0, sum = 2147483647, ptr = Logqtbl; k < 256; k++)
  351. {
  352. for (j = 0, tmp = 0; j < 3; j++)
  353. {
  354. /*
  355. * Sum and Logqtbl both scaled down by 4.
  356. * Change Logqtbl to INT16 if Lw not required.
  357. */
  358. D = sub(Sum[j], (*ptr++));
  359. tmp = L_mac(tmp, D, D);
  360. }
  361. if (tmp < sum)
  362. {
  363. ltmp = sum;
  364. sum = tmp;
  365. *best = k;
  366. }
  367. }
  368. for (j = 0; j < 3; j++)
  369. {
  370. Sum[j] = Powqtbl[*best * 3 + j];
  371. }
  372. /* Get excitation */
  373. j = FrameSize - ACBMemSize;
  374. for (k = 0; k < FrameSize - 1; k++)
  375. {
  376. if (k >= j)
  377. {
  378. stmp = e_ran_g(&Seed);
  379. stemp1 = k / (length - 1);
  380. output[k - j] = round32(L_shr(L_mult(stmp, Sum[stemp1]), 5));
  381. }
  382. }
  383. stmp = e_ran_g(&Seed);
  384. output[k - j] = round32(L_shr(L_mult(stmp, Sum[2]), 5)); /* last excitation */
  385. }
  386. }
  387. void GetExc800bps_dec(INT16 *output, INT16 length, INT16 best, INT16 flag, INT16 n, INT16 fer_flag)
  388. {
  389. INT16 i, j;
  390. INT16 sum;
  391. INT32 Ltemp;
  392. INT16 temp;
  393. static INT16 Seed = 1234;
  394. static INT16 Sum[NoOfSubFrames];
  395. static INT16 PrevBest = 0;
  396. #define P333 10923 /* (1/3) */
  397. if (!flag && !n)
  398. Seed = 1234;
  399. if (n == 0)
  400. {
  401. /* De-quantize */
  402. if (fer_flag == 0)
  403. {
  404. for (j = 0; j < 3; j++)
  405. Sum[j] = Powqtbl[best * 3 + j];
  406. PrevBest = best;
  407. }
  408. else
  409. {
  410. for (j = 0, Ltemp = 0; j < 3; j++)
  411. Ltemp = L_mac(Ltemp, Powqtbl[PrevBest * 3 + j], P333);
  412. for (j = 0; j < 3; j++)
  413. Sum[j] = round32(Ltemp);
  414. }
  415. }
  416. /* Convert to linear domain */
  417. sum = Sum[n];
  418. /* NOTE: Logqtbl[] and pow() function has been replaced with Powqtbl[] */
  419. for (i = 0; i < length; i++)
  420. {
  421. temp = ran_g(&Seed);
  422. Ltemp = L_mult(sum, temp);
  423. Ltemp = L_shr(Ltemp, 5);
  424. output[i] = round32(Ltemp);
  425. }
  426. }