fndppf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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: */
  71. /* Static/Global RAM: */
  72. /* Stack/Local RAM: */
  73. /*----------------------------------------------------------------------*/
  74. /************************************************************************
  75. * Routine name: fndppf *
  76. * Function: forward pitch predictor. *
  77. * Inputs: buf - data buffer. *
  78. * dmin - minimum delay value. *
  79. * dmax - maximum delay value. *
  80. * prevdelay - previous frame delay value. *
  81. * length - size of pitch window. *
  82. * Outputs: delay - predicted delay. *
  83. * beta - gain value. *
  84. * *
  85. ************************************************************************/
  86. #include "e_globs.h"
  87. #include "macro.h"
  88. //#include "mathevrc.h"
  89. #include "dsp_math.h"
  90. #include "mathdp31.h"
  91. #include "mathadv.h"
  92. void fndppf(INT16 *delay, INT16 *beta, INT16 *buf, INT16 dmin, INT16 dmax, INT16 length)
  93. {
  94. static INT16 b = -10224; /* rom storage */
  95. static INT16 a[3] =
  96. {-18739, 16024, -4882}; /* a[] scaled down by 4 */
  97. INT16 dnew = 0;
  98. INT16 sum;
  99. INT32 Lsum;
  100. register INT16 m, i, n;
  101. static INT16 DECbuf[FrameSize / 4];
  102. INT32 Lcorrmax, Lcmax, Ltmp;
  103. INT16 tap1;
  104. INT16 M1, M2, dnewtmp = 0;
  105. static INT16 lastgoodpitch = 0;
  106. static INT16 lastbeta = 0;
  107. static INT16 memory[3];
  108. static int FirstTime = 1;
  109. INT16 Lsum_scale;
  110. INT16 shift, Lcorr_scale, Lcmax_scale;
  111. INT16 n1, n2, nq, nq1;
  112. INT32 Ltempf;
  113. /* init static variables (should be in init routine for implementation) */
  114. if (FirstTime)
  115. {
  116. FirstTime = 0;
  117. n1 = (shr(FrameSize, 2));
  118. for (i = 0; i < n1; i++)
  119. DECbuf[i] = 0;
  120. memory[0] = memory[1] = memory[2] = 0;
  121. }
  122. /* Shift memory of DECbuf */
  123. for (i = 0; i < shr(length, 3); i++)
  124. {
  125. DECbuf[i] = DECbuf[i + shr(length, 3)];
  126. }
  127. /* filter signal and decimate */
  128. for (i = 0, n = shr(length, 3); i < shr(length, 1); i++)
  129. {
  130. Ltempf = L_shr(L_deposit_h(buf[i + shr(length, 1)]), 4);
  131. Ltempf = L_msu(Ltempf, memory[0], a[0]);
  132. Ltempf = L_msu(Ltempf, memory[1], a[1]);
  133. Ltempf = L_msu(Ltempf, memory[2], a[2]);
  134. Ltempf = L_shl(Ltempf, 2);
  135. shift = 0;
  136. if ((i + 1) % 4 == 0)
  137. {
  138. Lsum = L_add(Ltempf, L_deposit_h(memory[2]));
  139. Lsum = L_mac(Lsum, memory[0], b);
  140. Lsum = L_mac(Lsum, memory[1], b);
  141. DECbuf[n++] = round32(L_shl(Lsum, 1));
  142. }
  143. memory[2] = memory[1];
  144. memory[1] = memory[0];
  145. memory[0] = round32(Ltempf);
  146. }
  147. /* perform first search for best delay value in decimated domain */
  148. Lcorrmax = (LW_MIN);
  149. Lcorr_scale = 1;
  150. for (m = shr(dmin, 2); m <= shr(dmax, 2); m++)
  151. {
  152. n1 = 1;
  153. for (i = 0, Lsum = 0; i < sub(shr(length, 2), m); i++)
  154. {
  155. Ltempf = L_mult(DECbuf[i], DECbuf[i + m]);
  156. Ltempf = L_shr(Ltempf, n1);
  157. Lsum = L_add(Lsum, Ltempf);
  158. if (L_abs(Lsum) >= 0x40000000)
  159. {
  160. Lsum = L_shr(Lsum, 1);
  161. n1++;
  162. }
  163. }
  164. if (
  165. ((Lcorr_scale >= n1) && (L_shr(Lsum, sub(Lcorr_scale, n1)) > Lcorrmax))
  166. || ((Lcorr_scale < n1) && (Lsum > L_shr(Lcorrmax, sub(n1, Lcorr_scale))))
  167. )
  168. {
  169. Lcorrmax = Lsum;
  170. Lcorr_scale = n1;
  171. dnew = m;
  172. }
  173. }
  174. /* Compare against lastgoodpitch */
  175. if (lastgoodpitch != 0 && (abs_s(sub(lastgoodpitch, shl(dnew, 2))) > 2))
  176. {
  177. M1 = sub(shr(lastgoodpitch, 2), 2);
  178. if (M1 < shr(dmin, 2))
  179. M1 = shr(dmin, 2);
  180. M2 = add(M1, 4);
  181. if (M2 > shr(dmax, 2))
  182. M2 = shr(dmax, 2);
  183. Lcmax = LW_MIN;
  184. Lcmax_scale = 1;
  185. for (m = M1; m <= M2; m++)
  186. {
  187. n1 = 1;
  188. for (i = 0, Lsum = 0; i < sub(shr(length, 2), m); i++)
  189. {
  190. Ltempf = L_mult(DECbuf[i], DECbuf[i + m]);
  191. Ltempf = L_shr(Ltempf, n1);
  192. Lsum = L_add(Lsum, Ltempf);
  193. if (L_abs(Lsum) >= 0x40000000)
  194. {
  195. Lsum = L_shr(Lsum, 1);
  196. n1++;
  197. }
  198. }
  199. if (
  200. ((Lcmax_scale >= n1) && (L_shr(Lsum, sub(Lcmax_scale, n1)) > Lcmax))
  201. || ((Lcmax_scale < n1) && (Lsum > L_shr(Lcmax, sub(n1, Lcmax_scale))))
  202. )
  203. { /* Gives some bias to low delays */
  204. Lcmax = Lsum;
  205. Lcmax_scale = n1;
  206. dnewtmp = m;
  207. }
  208. }
  209. Lsum = L_mpy_ls(Lcorrmax, 27361);
  210. if (
  211. ((Lcmax_scale >= Lcorr_scale) && (L_shr(Lsum, sub(Lcmax_scale, Lcorr_scale)) < Lcmax))
  212. || ((Lcmax_scale < Lcorr_scale) && (Lsum < L_shr(Lcmax, sub(Lcorr_scale, Lcmax_scale))))
  213. )
  214. {
  215. dnew = dnewtmp;
  216. }
  217. }
  218. /* perform first search for best delay value in non-decimated buffer */
  219. M1 = Max(sub(shl(dnew, 2), 3), dmin);
  220. if (M1 < dmin)
  221. M1 = dmin;
  222. M2 = Min(add(shl(dnew, 2), 3), dmax);
  223. if (M2 > dmax)
  224. M2 = dmax;
  225. Lcorrmax = LW_MIN;
  226. Lcorr_scale = 1;
  227. for (m = M1; m <= M2; m++)
  228. {
  229. n1 = 1;
  230. for (i = 0, Lsum = 0; i < sub(length, m); i++)
  231. {
  232. Ltempf = L_mult(buf[i], buf[i + m]);
  233. Ltempf = L_shr(Ltempf, n1);
  234. Lsum = L_add(Lsum, Ltempf);
  235. if (L_abs(Lsum) >= 0x40000000)
  236. {
  237. Lsum = L_shr(Lsum, 1);
  238. n1++;
  239. }
  240. }
  241. if (
  242. ((Lcorr_scale >= n1) && (L_shr(Lsum, sub(Lcorr_scale, n1)) > Lcorrmax))
  243. || ((Lcorr_scale < n1) && (Lsum > L_shr(Lcorrmax, sub(n1, Lcorr_scale))))
  244. )
  245. {
  246. Lcorrmax = Lsum;
  247. Lcorr_scale = n1;
  248. dnew = m;
  249. }
  250. }
  251. Lsum_scale = 1;
  252. for (i = 0, Lsum = 0; i < sub(length, dnew); i++)
  253. {
  254. Ltempf = L_mult(buf[i + dnew], buf[i + dnew]);
  255. Ltempf = L_shr(Ltempf, Lsum_scale);
  256. Lsum = L_add(Lsum, Ltempf);
  257. if (L_abs(Lsum) >= 0x40000000)
  258. {
  259. Lsum = L_shr(Lsum, 1);
  260. Lsum_scale++;
  261. }
  262. }
  263. Lcmax_scale = 1;
  264. for (i = 0, Lcmax = 0; i < length - dnew; i++)
  265. {
  266. Ltempf = L_mult(buf[i], buf[i]);
  267. Ltempf = L_shr(Ltempf, Lcmax_scale);
  268. Lcmax = L_add(Lcmax, Ltempf);
  269. if (L_abs(Lcmax) >= 0x40000000)
  270. {
  271. Lcmax = L_shr(Lcmax, 1);
  272. Lcmax_scale++;
  273. }
  274. }
  275. nq = norm_l(Lsum);
  276. Lsum = L_shl(Lsum, nq);
  277. nq1 = norm_l(Lcmax);
  278. Lcmax = L_shl(Lcmax, nq1);
  279. Lsum = L_mpy_ll(Lsum, Lcmax);
  280. n1 = norm_l(Lsum);
  281. Lsum = L_shl(Lsum, n1);
  282. sum = sqroot(Lsum);
  283. n1 = add(add(n1, nq), nq1);
  284. n1 = sub(sub(n1, Lcmax_scale), Lsum_scale);
  285. n2 = shr(n1, 1);
  286. if (n1 & 1)
  287. Lsum = L_mult(sum, 23170);
  288. else
  289. Lsum = L_deposit_h(sum);
  290. n2 = add(n2, Lcorr_scale);
  291. Lcorrmax = L_shl(Lcorrmax, n2);
  292. if ((Lsum == 0) || (Lcorrmax <= 0))
  293. *beta = 0;
  294. else if (Lcorrmax > Lsum)
  295. *beta = 0x7fff;
  296. else
  297. *beta = round32(L_divide(Lcorrmax, Lsum));
  298. /* perform search for best delay value in around old pitch delay */
  299. if (lastgoodpitch != 0)
  300. {
  301. M1 = lastgoodpitch - 6;
  302. M2 = lastgoodpitch + 6;
  303. if (M1 < dmin)
  304. M1 = dmin;
  305. if (M2 > dmax)
  306. M2 = dmax;
  307. if (dnew > M2 || dnew < M1)
  308. {
  309. Lcmax = LW_MIN;
  310. Lcmax_scale = 1;
  311. for (m = M1; m <= M2; m++)
  312. {
  313. n1 = 1;
  314. for (i = 0, Lsum = 0; i < length - m; i++)
  315. {
  316. Ltempf = L_mult(buf[i], buf[i + m]);
  317. Ltempf = L_shr(Ltempf, n1);
  318. Lsum = L_add(Lsum, Ltempf);
  319. if (L_abs(Lsum) >= 0x40000000)
  320. {
  321. Lsum = L_shr(Lsum, 1);
  322. n1++;
  323. }
  324. }
  325. if (
  326. ((Lcmax_scale >= n1) && (L_shr(Lsum, sub(Lcmax_scale, n1)) > Lcmax))
  327. || ((Lcmax_scale < n1) && (Lsum > L_shr(Lcmax, sub(n1, Lcmax_scale))))
  328. )
  329. {
  330. Lcmax = Lsum;
  331. dnewtmp = m;
  332. Lcmax_scale = n1;
  333. }
  334. }
  335. Lcorr_scale = 1;
  336. for (i = 0, Ltmp = 0; i < length - dnewtmp; i++)
  337. {
  338. Ltempf = L_mult(buf[i + dnewtmp], buf[i + dnewtmp]);
  339. Ltempf = L_shr(Ltempf, Lcorr_scale);
  340. Ltmp = L_add(Ltmp, Ltempf);
  341. if (L_abs(Ltmp) >= 0x40000000)
  342. {
  343. Ltmp = L_shr(Ltmp, 1);
  344. Lcorr_scale++;
  345. }
  346. }
  347. Lsum_scale = 1;
  348. for (i = 0, Lsum = 0; i < length - dnewtmp; i++)
  349. {
  350. Ltempf = L_mult(buf[i], buf[i]);
  351. Ltempf = L_shr(Ltempf, Lsum_scale);
  352. Lsum = L_add(Lsum, Ltempf);
  353. if (L_abs(Lsum) >= 0x40000000)
  354. {
  355. Lsum = L_shr(Lsum, 1);
  356. Lsum_scale++;
  357. }
  358. }
  359. nq = norm_l(Ltmp);
  360. Ltmp = L_shl(Ltmp, nq);
  361. nq1 = norm_l(Lsum);
  362. Lsum = L_shl(Lsum, nq1);
  363. Ltmp = L_mpy_ll(Ltmp, Lsum);
  364. n1 = norm_l(Ltmp);
  365. Ltmp = L_shl(Ltmp, n1);
  366. sum = sqroot(Ltmp);
  367. n1 = add(add(n1, nq), nq1);
  368. n1 = sub(sub(n1, Lsum_scale), Lcorr_scale);
  369. n2 = shr(n1, 1);
  370. if (n1 & 1)
  371. Ltmp = L_mult(sum, 23170);
  372. else
  373. Ltmp = L_deposit_h(sum);
  374. n2 = add(n2, Lcmax_scale);
  375. Lcmax = L_shl(Lcmax, n2);
  376. if ((Ltmp == 0) || (Lcmax <= 0))
  377. tap1 = 0;
  378. else if (Lcmax >= Ltmp)
  379. tap1 = 0x7fff;
  380. else
  381. tap1 = round32(L_divide(Lcmax, Ltmp));
  382. /* Replace dnew with dnewtmp if tap1 is large enough */
  383. if ((dnew > M2 && (shr(tap1, 1) > mult_r(9830, *beta))) ||
  384. (dnew < M1 && (shr(tap1, 1) > mult_r(19661, *beta))))
  385. {
  386. dnew = dnewtmp;
  387. *beta = (tap1);
  388. }
  389. }
  390. }
  391. *delay = dnew;
  392. if (*beta > 13107)
  393. {
  394. lastgoodpitch = dnew;
  395. lastbeta = *beta;
  396. }
  397. else
  398. {
  399. lastbeta = mult_r(24576, lastbeta);
  400. if (lastbeta < 9830)
  401. lastgoodpitch = 0;
  402. }
  403. }