c3_10pf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. #include "typedefs.h"
  75. //#include "mathevrc.h"
  76. #include "dsp_math.h"
  77. #include "mathadv.h"
  78. #define L_SUBFR 54
  79. #define NB_PULSE 3
  80. #define NB_TRACK 3
  81. #define STEP 7
  82. #define NB_POS 8
  83. #define MSIZE 64
  84. #define Q15_1_7 4682
  85. Longword Inv_sqrt( /* (o) Q30 : output value (range: 0<=val<1) */
  86. Longword L_x /* (i) Q0 : input value (range: 0<=val<=7fffffff) */
  87. );
  88. /*-------------------------------------------------------------------*
  89. * Function code_3i54_10bits() *
  90. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
  91. * Algebraic codebook; 10 bits: 3 pulses in a frame of 54 samples. *
  92. *-------------------------------------------------------------------*
  93. * The code length is 54, containing 3 nonzero pulses: i0, i1 and i2.*
  94. * All pulses have a fixed amplitude: +1 for i0 and i2, -1 for i1. *
  95. * 1 bit is used to change sign of all pulses. *
  96. * Each pulse can have 8 possible positions: *
  97. * *
  98. * track0 (i0): 0, 7, 14, ... 49 *
  99. * track1 (i1): 2, 9, 16, ... 51 *
  100. * track2 (i2): 4, 11, 18, ... 53 *
  101. *-------------------------------------------------------------------*/
  102. void code_3i54_10bits(
  103. Shortword dn[], /* (i) Q0 : dn[54] correlation between target and h[] */
  104. Shortword l_subfr, /* (i) : lenght of subframe (53 or 54) */
  105. Shortword H[], /* (i) Q12: impulse response of weighted synthesis filter */
  106. Shortword code[], /* (o) Q12: algebraic (fixed) codebook excitation */
  107. Shortword y[], /* (o) Q10: filtered fixed codebook excitation */
  108. Shortword * indx /* (o) : index 1 words of 10 bits */
  109. )
  110. {
  111. Shortword i, k, i0, i1, i2, pos, index;
  112. Shortword psk, ps0, ps1, ps2, alpk, alp0, alp1, alp2;
  113. Longword s, cor;
  114. Shortword *p0, *p1, *p2, *p3, *p4, *p5;
  115. Shortword *h, *h_inv, *ptr_h1, *ptr_h2, *ptr_hf;
  116. /* these vectors are not static */
  117. Shortword codvec[NB_PULSE];
  118. Shortword cor_l2[NB_POS];
  119. Shortword h_buf[4 * L_SUBFR];
  120. Shortword rrixix[NB_PULSE][NB_POS], rrixiy[NB_PULSE][MSIZE];
  121. for (i=l_subfr; i<L_SUBFR; i++)
  122. {
  123. dn[i] = 0;
  124. }
  125. /*------------------------------------------------------------*
  126. * normalize h[] for maximum precision on correlation. *
  127. *------------------------------------------------------------*/
  128. h = h_buf;
  129. h_inv = h_buf + (2 * L_SUBFR);
  130. for (i = 0; i < ((2 * L_SUBFR) - l_subfr); i++)
  131. {
  132. *h++ = 0;
  133. *h_inv++ = 0;
  134. }
  135. cor = 0;
  136. for (i = 0; i < l_subfr; i++)
  137. cor = L_mac(cor, H[i], H[i]);
  138. /* scale h[] with shift operation */
  139. k = norm_l(cor);
  140. k = shr(k, 1);
  141. for (i = 0; i < l_subfr; i++)
  142. {
  143. h[i] = shl(H[i], k);
  144. }
  145. cor = L_shl(cor, add(k, k));
  146. /*------------------------------------------------------------*
  147. * Scaling h[] with a factor (0.5 < fac < 0.25) *
  148. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
  149. * extract_h(cor) = 8192 .. 32768 --> we want 4096 (1/8 Q15) *
  150. * *
  151. * 4096 (1/8) = fac^2 * extract_h(cor) *
  152. * fac = sqrt(4096/extract_h(cor)) *
  153. * *
  154. * fac = 1/sqrt(cor/4096) * 256 = 0.125 to 0.5 *
  155. *------------------------------------------------------------*/
  156. cor = L_shr(cor, 12);
  157. k = extract_h(L_shl(Inv_sqrt(cor), 8));
  158. for (i = 0; i < l_subfr; i++)
  159. {
  160. h[i] = mult(h[i], k);
  161. h_inv[i] = negate(h[i]);
  162. }
  163. h -= (L_SUBFR - l_subfr);
  164. h_inv -= (L_SUBFR - l_subfr);
  165. /*------------------------------------------------------------*
  166. * Compute rrixix[][] needed for the codebook search. *
  167. * This algorithm compute impulse response energy of all *
  168. * positions (8) in each track (5). Total = 5x8 = 40. *
  169. *------------------------------------------------------------*/
  170. /* storage order --> i2i2, i1i1, i0i0 */
  171. /* Init pointers to last position of rrixix[] */
  172. p0 = &rrixix[0][NB_POS - 1];
  173. p1 = &rrixix[1][NB_POS - 1];
  174. p2 = &rrixix[2][NB_POS - 1];
  175. ptr_h1 = h;
  176. cor = 0x00010000L; /* 1.0 (for rounding) */
  177. for (i = 0; i < NB_POS; i++)
  178. {
  179. cor = L_mac(cor, *ptr_h1, *ptr_h1);
  180. ptr_h1++;
  181. *p2-- = extract_h(cor);
  182. cor = L_mac(cor, *ptr_h1, *ptr_h1);
  183. ptr_h1++;
  184. cor = L_mac(cor, *ptr_h1, *ptr_h1);
  185. ptr_h1++;
  186. *p1-- = extract_h(cor);
  187. cor = L_mac(cor, *ptr_h1, *ptr_h1);
  188. ptr_h1++;
  189. cor = L_mac(cor, *ptr_h1, *ptr_h1);
  190. ptr_h1++;
  191. *p0-- = extract_h(cor);
  192. cor = L_mac(cor, *ptr_h1, *ptr_h1);
  193. ptr_h1++;
  194. cor = L_mac(cor, *ptr_h1, *ptr_h1);
  195. ptr_h1++;
  196. }
  197. /* Divide all elements of rrixix[][] by 2. */
  198. p0 = &rrixix[0][0];
  199. for (i = 0; i < NB_PULSE*NB_POS ; i++)
  200. *p0++ = shr(*p0, 1);
  201. /*------------------------------------------------------------*
  202. * Compute rrixiy[][] needed for the codebook search. *
  203. * This algorithm compute correlation between 2 pulses. *
  204. *------------------------------------------------------------*/
  205. /* storage order --> i1i2, i0i1 */
  206. pos = MSIZE - 1;
  207. ptr_hf = h + 2;
  208. for (k = 0; k < NB_POS; k++)
  209. {
  210. p1 = &rrixiy[1][pos];
  211. p0 = &rrixiy[0][pos];
  212. cor = 0x00008000L; /* 0.5 (for rounding) */
  213. ptr_h1 = h;
  214. ptr_h2 = ptr_hf;
  215. for (i = k + (Shortword) 1; i < NB_POS; i++)
  216. {
  217. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  218. ptr_h1++;
  219. ptr_h2++;
  220. *p1 = extract_h(cor);
  221. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  222. ptr_h1++;
  223. ptr_h2++;
  224. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  225. ptr_h1++;
  226. ptr_h2++;
  227. *p0 = extract_h(cor);
  228. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  229. ptr_h1++;
  230. ptr_h2++;
  231. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  232. ptr_h1++;
  233. ptr_h2++;
  234. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  235. ptr_h1++;
  236. ptr_h2++;
  237. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  238. ptr_h1++;
  239. ptr_h2++;
  240. p1 -= (NB_POS + 1);
  241. p0 -= (NB_POS + 1);
  242. }
  243. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  244. ptr_h1++;
  245. ptr_h2++;
  246. *p1 = extract_h(cor);
  247. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  248. ptr_h1++;
  249. ptr_h2++;
  250. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  251. ptr_h1++;
  252. ptr_h2++;
  253. *p0 = extract_h(cor);
  254. pos -= NB_POS;
  255. ptr_hf += STEP;
  256. }
  257. /* storage order --> i1i2, i0i1 */
  258. pos = MSIZE - 1;
  259. ptr_hf = h + 5;
  260. for (k = 0; k < NB_POS; k++)
  261. {
  262. p1 = &rrixiy[1][pos - 1];
  263. p0 = &rrixiy[0][pos - 1];
  264. cor = 0x00008000L; /* 0.5 (for rounding) */
  265. ptr_h1 = h;
  266. ptr_h2 = ptr_hf;
  267. for (i = k + (Shortword) 1; i < NB_POS; i++)
  268. {
  269. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  270. ptr_h1++;
  271. ptr_h2++;
  272. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  273. ptr_h1++;
  274. ptr_h2++;
  275. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  276. ptr_h1++;
  277. ptr_h2++;
  278. *p1 = extract_h(cor);
  279. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  280. ptr_h1++;
  281. ptr_h2++;
  282. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  283. ptr_h1++;
  284. ptr_h2++;
  285. *p0 = extract_h(cor);
  286. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  287. ptr_h1++;
  288. ptr_h2++;
  289. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  290. ptr_h1++;
  291. ptr_h2++;
  292. p1 -= (NB_POS + 1);
  293. p0 -= (NB_POS + 1);
  294. }
  295. pos--;
  296. ptr_hf += STEP;
  297. }
  298. /* storage order --> i0i2 */
  299. pos = MSIZE - 1;
  300. ptr_hf = h + 4;
  301. for (k = 0; k < NB_POS; k++)
  302. {
  303. p0 = &rrixiy[2][pos];
  304. cor = 0x00008000L; /* 0.5 (for rounding) */
  305. ptr_h1 = h;
  306. ptr_h2 = ptr_hf;
  307. for (i = k + (Shortword) 1; i < NB_POS; i++)
  308. {
  309. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  310. ptr_h1++;
  311. ptr_h2++;
  312. *p0 = extract_h(cor);
  313. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  314. ptr_h1++;
  315. ptr_h2++;
  316. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  317. ptr_h1++;
  318. ptr_h2++;
  319. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  320. ptr_h1++;
  321. ptr_h2++;
  322. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  323. ptr_h1++;
  324. ptr_h2++;
  325. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  326. ptr_h1++;
  327. ptr_h2++;
  328. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  329. ptr_h1++;
  330. ptr_h2++;
  331. p0 -= (NB_POS + 1);
  332. }
  333. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  334. ptr_h1++;
  335. ptr_h2++;
  336. *p0 = extract_h(cor);
  337. pos -= NB_POS;
  338. ptr_hf += STEP;
  339. }
  340. /* storage order --> i0i2 */
  341. pos = MSIZE - 1;
  342. ptr_hf = h + 3;
  343. for (k = 0; k < NB_POS; k++)
  344. {
  345. p0 = &rrixiy[2][pos - 1];
  346. cor = 0x00008000L; /* 0.5 (for rounding) */
  347. ptr_h1 = h;
  348. ptr_h2 = ptr_hf;
  349. for (i = k + (Shortword) 1; i < NB_POS; i++)
  350. {
  351. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  352. ptr_h1++;
  353. ptr_h2++;
  354. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  355. ptr_h1++;
  356. ptr_h2++;
  357. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  358. ptr_h1++;
  359. ptr_h2++;
  360. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  361. ptr_h1++;
  362. ptr_h2++;
  363. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  364. ptr_h1++;
  365. ptr_h2++;
  366. *p0 = extract_h(cor);
  367. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  368. ptr_h1++;
  369. ptr_h2++;
  370. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  371. ptr_h1++;
  372. ptr_h2++;
  373. p0 -= (NB_POS + 1);
  374. }
  375. pos--;
  376. ptr_hf += STEP;
  377. }
  378. /*-------------------------------------------------------------------*
  379. * SEARCH THE BEST CODEVECTOR. *
  380. * *
  381. * complexity: 8x8x8 = 512 tests *
  382. *-------------------------------------------------------------------*/
  383. /* default value */
  384. psk = -1;
  385. alpk = 1;
  386. codvec[0] = 0;
  387. codvec[1] = 2;
  388. codvec[2] = 4;
  389. p0 = rrixix[0];
  390. p2 = rrixiy[0];
  391. p4 = rrixiy[2];
  392. for (i0 = 0; i0 < L_SUBFR; i0 += STEP)
  393. {
  394. ps0 = dn[i0];
  395. alp0 = *p0++;
  396. p1 = rrixix[1];
  397. p5 = rrixiy[1];
  398. for (i = 0; i < NB_POS; i++)
  399. cor_l2[i] = add(rrixix[2][i], *p4++);
  400. for (i1 = 2; i1 < L_SUBFR; i1 += STEP)
  401. {
  402. ps1 = sub(ps0, dn[i1]);
  403. /* alp1 = alp0 + rr[i1][i1] - 2.0 * rr[i0][i1]; */
  404. alp1 = add(alp0, sub(*p1++, *p2++));
  405. p3 = cor_l2;
  406. for (i2 = 4; i2 < L_SUBFR; i2 += STEP)
  407. {
  408. ps2 = add(ps1, dn[i2]);
  409. /* alp2 = alp1 + rr[i2][i2] + 2.0 * (rr[i0][i2] - rr[i1][i2]); */
  410. alp2 = add(alp1, sub(*p3++, *p5++));
  411. ps2 = mult(ps2, ps2);
  412. s = L_msu(L_mult(alpk, ps2), psk, alp2);
  413. if (s > 0)
  414. {
  415. psk = ps2;
  416. alpk = alp2;
  417. codvec[0] = i0;
  418. codvec[1] = i1;
  419. codvec[2] = i2;
  420. }
  421. }
  422. }
  423. }
  424. /*-------------------------------------------------------------------*
  425. * Build the codeword, the filtered codeword and index of codevector.*
  426. *-------------------------------------------------------------------*/
  427. ps2 = sub(add(dn[codvec[0]], dn[codvec[2]]), dn[codvec[1]]);
  428. for (i = 0; i < l_subfr; i++)
  429. {
  430. code[i] = 0;
  431. }
  432. if (ps2 < 0)
  433. index = 1; /* global sign */
  434. else
  435. index = 0;
  436. for (k = 0; k < NB_PULSE; k++)
  437. {
  438. i = codvec[k]; /* read pulse position */
  439. pos = mult(i, Q15_1_7); /* pos = position/7 */
  440. index = add(shl(index, 3), pos);
  441. if (((k != 1) && (ps2 >= 0)) || ((k == 1) && (ps2 < 0)))
  442. {
  443. if (i < l_subfr)
  444. code[i] = 4096; /* codeword in Q12 format */
  445. codvec[k] += (2 * L_SUBFR);
  446. }
  447. else
  448. {
  449. if (i < l_subfr)
  450. code[i] = -4096; /* codeword in Q12 format */
  451. }
  452. }
  453. for (i = 0; i < l_subfr; i++)
  454. {
  455. h[i] = H[i];
  456. h_inv[i] = negate(h[i]);
  457. }
  458. p0 = h_inv - codvec[0];
  459. p1 = h_inv - codvec[1];
  460. p2 = h_inv - codvec[2];
  461. for (i = 0; i < l_subfr; i++)
  462. {
  463. s = L_mult(*p0++, 8192); /* Q12 --> Q10 */
  464. s = L_mac(s, *p1++, 8192);
  465. s = L_mac(s, *p2++, 8192);
  466. y[i] = round32(s);
  467. }
  468. *indx = index;
  469. return;
  470. }