c8_35pf.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. #include "typedefs.h"
  70. //#include "mathevrc.h"
  71. #include "dsp_math.h"
  72. #include "mathadv.h"
  73. #define L_SUBFR 55
  74. #define NB_PULSE 8
  75. #define NB_TRACK 5
  76. #define STEP 5
  77. #define NB_POS 11
  78. #define MSIZE 121
  79. #define NB_ITER 4
  80. #define Q15_1_5 6554
  81. const Shortword ipos[8 + 3] =
  82. {0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0};
  83. Longword Inv_sqrt( /* (o) Q30 : output value (range: 0<=val<1) */
  84. Longword L_x /* (i) Q0 : input value (range: 0<=val<=7fffffff) */
  85. );
  86. /* locals functions */
  87. static void cor_h_vec(
  88. Shortword h[], /* (i) scaled impulse response */
  89. Shortword vec[], /* (i) vector to correlate with h[] */
  90. Shortword track, /* (i) track to use */
  91. Shortword sign[], /* (i) sign vector */
  92. Shortword rrixix[][NB_POS], /* (i) correlation of h[x] with h[x] */
  93. Shortword cor[] /* (o) result of correlation (NB_POS elements) */
  94. );
  95. static void search_ixiy(
  96. Shortword track_x, /* (i) track of pulse 1 */
  97. Shortword track_y, /* (i) track of pulse 2 */
  98. Shortword * ps, /* (i/o) correlation of all fixed pulses */
  99. Shortword * alp, /* (i/o) energy of all fixed pulses */
  100. Shortword * ix, /* (o) position of pulse 1 */
  101. Shortword * iy, /* (o) position of pulse 2 */
  102. Shortword dn[], /* (i) corr. between target and h[] */
  103. Shortword cn[], /* (i) corr. vector (search if cn[]>=0) */
  104. Shortword cor_x[], /* (i) corr. of pulse 1 with fixed pulses */
  105. Shortword cor_y[], /* (i) corr. of pulse 2 with fixed pulses */
  106. Shortword rrixiy[][MSIZE] /* (i) corr. of pulse 1 with pulse 2 */
  107. );
  108. /*-------------------------------------------------------------------*
  109. * Function code_8i55_35bits() *
  110. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
  111. * Algebraic codebook; 35 bits: 8 pulses in a frame of 55 samples. *
  112. *-------------------------------------------------------------------*
  113. * The code length is 55, containing 8 nonzero pulses: i0...i7. *
  114. * All pulses can have two (2) possible amplitudes: +1 or -1. *
  115. * Each pulse can have 11 possible positions. *
  116. * The 8 pulses can be on the following 5 tracks: *
  117. * track0 : 0, 5, .. 50 *
  118. * track1 : 1, 6, .. 51 *
  119. * track2 : 2, 7, .. 52 *
  120. * track3 : 3, 8, .. 53 *
  121. * track4 : 4, 9, .. 54 *
  122. * 3 tracks has 2 pulses (i0..i5), 2 tracks has 1 pulses (i6,i7) *
  123. * *
  124. * NOTE: A pulse is unused (amplitude = 0) when its position is *
  125. * quantized outside l_subfr (l_subfr = 53 or 54). *
  126. *-------------------------------------------------------------------*/
  127. void code_8i55_35bits(
  128. Shortword dn[], /* (i) Q0 : dn[55] correlation between target and h[] */
  129. Shortword cn[], /* (i) Q0 : cn[55] residual after INT32 term prediction */
  130. Shortword l_subfr, /* (i) : lenght of subframe (53 or 54) */
  131. Shortword H[], /* (i) Q12: impulse response of weighted synthesis filter */
  132. Shortword code[], /* (o) Q12: algebraic (fixed) codebook excitation */
  133. Shortword y[], /* (o) Q10: filtered fixed codebook excitation */
  134. Shortword indx[] /* (o) : index 4 words: 8 + 8 + 8 + 11 = 35 bits */
  135. )
  136. {
  137. Shortword i, j, k, ix, iy, pos, index, track;
  138. Shortword psk, ps, alpk, alp;
  139. Shortword val, snorm, snorm1, snorm2, s_short, ps1_short, ps2_short;
  140. Longword s, cor, ps1, ps2;
  141. Shortword *p0, *p1, *p2, *p3, *p4, *p5, *p6, *p7, *psign;
  142. Shortword *h, *h_inv, *ptr_h1, *ptr_h2, *ptr_hf;
  143. Shortword sign[L_SUBFR], vec[L_SUBFR];
  144. Shortword ip[NB_PULSE], codvec[NB_PULSE + 1], pos_max[NB_TRACK];
  145. Shortword cor_x[NB_POS], cor_y[NB_POS];
  146. Shortword h_buf[4 * L_SUBFR];
  147. Shortword rrixix[NB_PULSE][NB_POS], rrixiy[NB_PULSE][MSIZE];
  148. pos = -1; /* initialize to nonsense position to eliminate warning */
  149. /* Does not effect code since pos will not be used */
  150. for (i=l_subfr; i<L_SUBFR; i++)
  151. {
  152. cn[i] = dn[i] = 0;
  153. }
  154. /* calculate energy for normalization of cn[] and dn[] */
  155. ps1 = 0;
  156. for (i = 0; i < L_SUBFR; i++)
  157. ps1 = L_mac(ps1, cn[i], cn[i]); /* LT -- TJ Fixed 7/1/96 */
  158. ps2 = 0;
  159. for (i = 0; i < L_SUBFR; i++)
  160. ps2 = L_mac(ps2, dn[i], dn[i]); /* LT -- TJ Fixed 7/1/96 */
  161. /* NOTE: The equation for determination the sign of the excitation */
  162. /* is changed to an equivalent equation to avoid the dividition */
  163. /* operation */
  164. /* Normalize the ps1 and ps2 */
  165. snorm1 = norm_l(ps1);
  166. snorm2 = norm_l(ps2);
  167. ps1_short = extract_h(L_shl(ps1, snorm1));
  168. ps2_short = extract_h(L_shl(ps2, snorm2));
  169. /* Compute s = sqrt(ps1*ps2) */
  170. snorm1 = add(snorm1, snorm2);
  171. s = L_mult(ps1_short, ps2_short);
  172. snorm = norm_l(s);
  173. s_short = sqroot(L_shl(s, snorm));
  174. snorm = add(snorm, snorm1);
  175. if ((snorm) & 0x0001) /*if snorm is odd, multiply by sqrt(2)/2 */
  176. s_short = mult(s_short, 23170);
  177. snorm = shr(snorm, 1);
  178. snorm = add(snorm, 1);
  179. snorm2 = add(snorm2, 2);
  180. /* Find the common dynamic shift snorm1 */
  181. if (snorm2 > snorm)
  182. snorm1 = snorm;
  183. else
  184. snorm1 = snorm2;
  185. snorm2 = sub(snorm2, snorm1);
  186. snorm = sub(snorm, snorm1);
  187. /* set sign according to en[] = ps1*cn[] + sqrt(ps1*ps2)*dn[] */
  188. /* find position of maximum of correlation in each track */
  189. for (k = 0; k < NB_TRACK; k++)
  190. {
  191. cor = -1;
  192. for (i = k; i < L_SUBFR; i += STEP)
  193. {
  194. val = dn[i];
  195. s = L_add(L_shr(L_mult(ps2_short, cn[i]), snorm2), L_shr(L_mult(s_short, dn[i]), snorm));
  196. if (s >= 0)
  197. {
  198. sign[i] = 32767; /* sign = +1 (Q15) */
  199. vec[i] = -32768;
  200. }
  201. else
  202. {
  203. sign[i] = -32768; /* sign = -1 (Q15) */
  204. vec[i] = 32767;
  205. val = negate(val);
  206. }
  207. dn[i] = val; /* modify dn[] according to the fixed sign */
  208. s = L_abs(s);
  209. cn[i] = extract_h(L_shl(s, 8));
  210. if (s > cor)
  211. {
  212. cor = s;
  213. pos = i;
  214. }
  215. }
  216. pos_max[k] = pos;
  217. }
  218. /* select 6 positions per track (criterion: max of corr.) */
  219. for (i = 0; i < NB_TRACK; i++)
  220. {
  221. for (k = 0; k < 5; k++)
  222. {
  223. ps = 32767;
  224. pos = i;
  225. for (j = i; j < L_SUBFR; j += STEP)
  226. {
  227. if ((cn[j] <= ps) && (cn[j] >= 0))
  228. {
  229. ps = cn[j];
  230. pos = j;
  231. }
  232. }
  233. cn[pos] = -1; /* position not selected */
  234. }
  235. }
  236. /*------------------------------------------------------------*
  237. * normalize h[] for maximum precision on correlation. *
  238. *------------------------------------------------------------*/
  239. h = h_buf;
  240. h_inv = h_buf + (2 * L_SUBFR);
  241. /* for (i = 0; i < ((2 * L_SUBFR) - l_subfr); i++) */
  242. for (i = 0; i < L_SUBFR ; i++)
  243. {
  244. *h++ = 0;
  245. *h_inv++ = 0;
  246. }
  247. cor = 0;
  248. for (i = 0; i < l_subfr; i++)
  249. cor = L_mac(cor, H[i], H[i]);
  250. /* scale h[] with shift operation */
  251. k = norm_l(cor);
  252. k = shr(k, 1);
  253. for (i = 0; i < l_subfr; i++)
  254. {
  255. h[i] = shl(H[i], k);
  256. }
  257. cor = L_shl(cor, add(k, k));
  258. /*------------------------------------------------------------*
  259. * Scaling h[] with a factor (0.5 < fac < 0.25) *
  260. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
  261. * extract_h(cor) = 8192 .. 32768 --> we want 4096 (1/8 Q15) *
  262. * *
  263. * 4096 (1/8) = fac^2 * extract_h(cor) *
  264. * fac = sqrt(4096/extract_h(cor)) *
  265. * *
  266. * fac = 1/sqrt(cor/4096) * 256 = 0.125 to 0.5 *
  267. *------------------------------------------------------------*/
  268. cor = L_shr(cor, 12);
  269. k = extract_h(L_shl(Inv_sqrt(cor), 8));
  270. for (i = 0; i < l_subfr; i++)
  271. {
  272. h[i] = mult(h[i], k);
  273. h_inv[i] = negate(h[i]);
  274. }
  275. /* Add to set other be zero */
  276. for (i=l_subfr; i < L_SUBFR; i++)
  277. {
  278. h[i]=0.0;
  279. h_inv[i]=0.0;
  280. }
  281. /* h -= (L_SUBFR - l_subfr);
  282. h_inv -= (L_SUBFR - l_subfr); */
  283. /*------------------------------------------------------------*
  284. * Compute rrixix[][] needed for the codebook search. *
  285. * This algorithm compute impulse response energy of all *
  286. * positions 11 in each track (5). Total = 5x11 = 55. *
  287. *------------------------------------------------------------*/
  288. /* storage order --> i4i4, i3i3, i2i2, i1i1, i0i0 */
  289. /* Init pointers to last position of rrixix[] */
  290. p0 = &rrixix[0][NB_POS - 1];
  291. p1 = &rrixix[1][NB_POS - 1];
  292. p2 = &rrixix[2][NB_POS - 1];
  293. p3 = &rrixix[3][NB_POS - 1];
  294. p4 = &rrixix[4][NB_POS - 1];
  295. ptr_h1 = h;
  296. cor = 0x00010000L; /* 1.0 (for rounding) */
  297. for (i = 0; i < NB_POS; i++)
  298. {
  299. cor = L_mac(cor, *ptr_h1, *ptr_h1);
  300. ptr_h1++;
  301. *p4-- = extract_h(cor);
  302. cor = L_mac(cor, *ptr_h1, *ptr_h1);
  303. ptr_h1++;
  304. *p3-- = extract_h(cor);
  305. cor = L_mac(cor, *ptr_h1, *ptr_h1);
  306. ptr_h1++;
  307. *p2-- = extract_h(cor);
  308. cor = L_mac(cor, *ptr_h1, *ptr_h1);
  309. ptr_h1++;
  310. *p1-- = extract_h(cor);
  311. cor = L_mac(cor, *ptr_h1, *ptr_h1);
  312. ptr_h1++;
  313. *p0-- = extract_h(cor);
  314. }
  315. /* Divide all elements of rrixix[][] by 2. */
  316. p0 = &rrixix[0][0];
  317. for (i = 0; i < L_SUBFR; i++)
  318. *p0++ = shr(*p0, 1);
  319. /*------------------------------------------------------------*
  320. * Compute rrixiy[][] needed for the codebook search. *
  321. * This algorithm compute correlation between 2 pulses *
  322. * (2 impulses responses) in 5 possible adjacents tracks. *
  323. * (track 0-1, 1-2, 2-3, 3-4 and 4-0). Total = 5x11x11 =605.*
  324. *------------------------------------------------------------*/
  325. /* storage order --> i3i4, i2i3, i1i2, i0i1, i4i0 */
  326. pos = MSIZE - 1;
  327. ptr_hf = h + 1;
  328. for (k = 0; k < NB_POS; k++)
  329. {
  330. p4 = &rrixiy[3][pos];
  331. p3 = &rrixiy[2][pos];
  332. p2 = &rrixiy[1][pos];
  333. p1 = &rrixiy[0][pos];
  334. p0 = &rrixiy[4][pos - NB_POS];
  335. cor = 0x00008000L; /* 0.5 (for rounding) */
  336. ptr_h1 = h;
  337. ptr_h2 = ptr_hf;
  338. for (i = k + (Shortword) 1; i < NB_POS; i++)
  339. {
  340. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  341. ptr_h1++;
  342. ptr_h2++;
  343. *p4 = extract_h(cor);
  344. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  345. ptr_h1++;
  346. ptr_h2++;
  347. *p3 = extract_h(cor);
  348. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  349. ptr_h1++;
  350. ptr_h2++;
  351. *p2 = extract_h(cor);
  352. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  353. ptr_h1++;
  354. ptr_h2++;
  355. *p1 = extract_h(cor);
  356. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  357. ptr_h1++;
  358. ptr_h2++;
  359. *p0 = extract_h(cor);
  360. p4 -= (NB_POS + 1);
  361. p3 -= (NB_POS + 1);
  362. p2 -= (NB_POS + 1);
  363. p1 -= (NB_POS + 1);
  364. p0 -= (NB_POS + 1);
  365. }
  366. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  367. ptr_h1++;
  368. ptr_h2++;
  369. *p4 = extract_h(cor);
  370. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  371. ptr_h1++;
  372. ptr_h2++;
  373. *p3 = extract_h(cor);
  374. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  375. ptr_h1++;
  376. ptr_h2++;
  377. *p2 = extract_h(cor);
  378. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  379. ptr_h1++;
  380. ptr_h2++;
  381. *p1 = extract_h(cor);
  382. pos -= NB_POS;
  383. ptr_hf += STEP;
  384. }
  385. /* storage order --> i4i0, i3i4, i2i3, i1i2, i0i1 */
  386. pos = MSIZE - 1;
  387. ptr_hf = h + 4;
  388. for (k = 0; k < NB_POS; k++)
  389. {
  390. p4 = &rrixiy[4][pos];
  391. p3 = &rrixiy[3][pos - 1];
  392. p2 = &rrixiy[2][pos - 1];
  393. p1 = &rrixiy[1][pos - 1];
  394. p0 = &rrixiy[0][pos - 1];
  395. cor = 0x00008000L; /* 0.5 (for rounding) */
  396. ptr_h1 = h;
  397. ptr_h2 = ptr_hf;
  398. for (i = k + (Shortword) 1; i < NB_POS; i++)
  399. {
  400. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  401. ptr_h1++;
  402. ptr_h2++;
  403. *p4 = extract_h(cor);
  404. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  405. ptr_h1++;
  406. ptr_h2++;
  407. *p3 = extract_h(cor);
  408. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  409. ptr_h1++;
  410. ptr_h2++;
  411. *p2 = extract_h(cor);
  412. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  413. ptr_h1++;
  414. ptr_h2++;
  415. *p1 = extract_h(cor);
  416. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  417. ptr_h1++;
  418. ptr_h2++;
  419. *p0 = extract_h(cor);
  420. p4 -= (NB_POS + 1);
  421. p3 -= (NB_POS + 1);
  422. p2 -= (NB_POS + 1);
  423. p1 -= (NB_POS + 1);
  424. p0 -= (NB_POS + 1);
  425. }
  426. cor = L_mac(cor, *ptr_h1, *ptr_h2);
  427. ptr_h1++;
  428. ptr_h2++;
  429. *p4 = extract_h(cor);
  430. pos--;
  431. ptr_hf += STEP;
  432. }
  433. /*------------------------------------------------------------*
  434. * Modification of rrixiy[][] to take signs into account. *
  435. *------------------------------------------------------------*/
  436. p0 = &rrixiy[0][0];
  437. for (k = 0; k < NB_TRACK; k++)
  438. {
  439. for (i = k; i < L_SUBFR; i += STEP)
  440. {
  441. psign=sign;
  442. if (psign[i] < 0)
  443. psign = vec;
  444. for (j = ((k + 1) % NB_TRACK); j < L_SUBFR; j += STEP)
  445. {
  446. *p0++ = mult(*p0, psign[j]);
  447. }
  448. }
  449. }
  450. /*-------------------------------------------------------------------*
  451. * Deep first search: 4 iterations of 264 tests = 1056 tests. *
  452. * *
  453. * Stages of deep first search: *
  454. * stage 1 : fix i0 and i1 --> try 6x11 = 66 positions *
  455. * stage 2 : fix i2 and i3 --> try 6x11 = 66 positions. *
  456. * stage 3 : fix i4 and i5 --> try 6x11 = 66 positions. *
  457. * stage 4 : fix i6 and i7 --> try 6x11 = 66 positions. *
  458. *-------------------------------------------------------------------*/
  459. /* default value */
  460. psk = -1;
  461. alpk = 1;
  462. for (i = 0; i < NB_PULSE; i++)
  463. codvec[i] = i;
  464. for (k = 0; k < NB_ITER; k++)
  465. {
  466. /* Change back to the search strategy used in the float point simulation */
  467. alp = 0;
  468. ps = 0;
  469. search_ixiy(ipos[k], ipos[k + 1], &ps, &alp, &ix, &iy,
  470. dn, cn, rrixix[ipos[k]], rrixix[ipos[k+1]], rrixiy);
  471. ip[0] = ix;
  472. ip[1] = iy;
  473. for (i = 0; i < L_SUBFR; i++)
  474. {
  475. vec[i] = 0;
  476. }
  477. /* stage 2..5: fix pulse i2,i3,i4,i5,i6 and i7 */
  478. for (j = 2; j < NB_PULSE; j += 2)
  479. {
  480. /*--------------------------------------------------*
  481. * Store all impulse response of all fixed pulses *
  482. * in vector vec[] for the "cor_h_vec()" function. *
  483. *--------------------------------------------------*/
  484. if (sign[ix] < 0)
  485. p0 = h_inv - ix;
  486. else
  487. p0 = h - ix;
  488. if (sign[iy] < 0)
  489. p1 = h_inv - iy;
  490. else
  491. p1 = h - iy;
  492. for (i = 0; i < L_SUBFR; i++)
  493. {
  494. vec[i] = add(vec[i], add(*p0++, *p1++));
  495. }
  496. /*--------------------------------------------------*
  497. * Calculate correlation of all possible positions *
  498. * of the next 2 pulses with previous fixed pulses. *
  499. * Each pulse can have 8 possible positions *
  500. *--------------------------------------------------*/
  501. cor_h_vec(h, vec, ipos[k + j], sign, rrixix, cor_x);
  502. cor_h_vec(h, vec, ipos[k + j + 1], sign, rrixix, cor_y);
  503. /*--------------------------------------------------*
  504. * Fix 2 pulses, try 6x11 = 66 positions. *
  505. *--------------------------------------------------*/
  506. search_ixiy(ipos[k + j], ipos[k + j + 1], &ps, &alp, &ix, &iy,
  507. dn, cn, cor_x, cor_y, rrixiy);
  508. ip[j] = ix;
  509. ip[j + 1] = iy;
  510. }
  511. /* memorise new codevector if it's better than the last one. */
  512. ps = mult(ps, ps);
  513. s = L_msu(L_mult(alpk, ps), psk, alp);
  514. if (s > 0)
  515. {
  516. psk = ps;
  517. alpk = alp;
  518. for (i = 0; i < NB_PULSE; i++)
  519. codvec[i] = ip[i];
  520. codvec[8] = k;
  521. }
  522. } /* end of for (k=0; k<NB_ITER; k++) */
  523. /*-------------------------------------------------------------------*
  524. * Build the codeword, the filtered codeword and index of codevector.*
  525. *-------------------------------------------------------------------*/
  526. for (i = 0; i < l_subfr; i++)
  527. {
  528. code[i] = 0;
  529. }
  530. for (i = 0; i < NB_TRACK; i++)
  531. {
  532. indx[i] = -1;
  533. }
  534. for (k = 0; k < NB_PULSE; k++)
  535. {
  536. i = codvec[k]; /* read pulse position */
  537. j = sign[i]; /* read sign */
  538. index = mult(i, Q15_1_5); /* index = pos/5 */
  539. /* track = pos%5 */
  540. track = sub(i, extract_l(L_shr(L_mult(index, 5), 1)));
  541. if (j > 0)
  542. {
  543. if (i < l_subfr)
  544. code[i] = add(code[i], 4096); /* codeword in Q12 format */
  545. codvec[k] += (2 * L_SUBFR);
  546. }
  547. else
  548. {
  549. if (i < l_subfr)
  550. code[i] = sub(code[i], 4096); /* codeword in Q12 format */
  551. index = add(index, 16);
  552. }
  553. /*-------------------------------------------------------*
  554. * Quantization of 2 pulses with 8 or 9 bits: *
  555. * 1 bit for 2 pulses sign. (b8) *
  556. * 7 bits for 2 pulses in same track (b7..b4) *
  557. * 8 bits for 2 pulses in different track (b7..b4) *
  558. *-------------------------------------------------------*/
  559. if (indx[track] < 0)
  560. {
  561. indx[track] = index;
  562. }
  563. else
  564. {
  565. if (((index ^ indx[track]) & 16) == 0)
  566. {
  567. /* sign of 1st pulse == sign of 2th pulse */
  568. if (sub(indx[track], index) <= 0)
  569. {
  570. indx[track] = shl((indx[track] & 16), 3)
  571. + shr(extract_l(L_mult((indx[track] & 15), NB_POS)), 1)
  572. + (index & 15);
  573. }
  574. else
  575. {
  576. indx[track] = shl((index & 16), 3)
  577. + shr(extract_l(L_mult((index & 15), NB_POS)), 1)
  578. + (indx[track] & 15);
  579. }
  580. }
  581. else
  582. {
  583. /* sign of 1st pulse != sign of 2th pulse */
  584. if (sub((indx[track] & 15), (index & 15)) <= 0)
  585. {
  586. indx[track] = shl((index & 16), 3)
  587. + shr(extract_l(L_mult((index & 15), NB_POS)), 1)
  588. + (indx[track] & 15);
  589. }
  590. else
  591. {
  592. indx[track] = shl((indx[track] & 16), 3)
  593. + shr(extract_l(L_mult((indx[track] & 15), NB_POS)), 1)
  594. + (index & 15);
  595. }
  596. }
  597. }
  598. }
  599. if (codvec[8] == 0)
  600. {
  601. i = indx[3];
  602. j = indx[4];
  603. }
  604. else if (codvec[8] == 1)
  605. {
  606. i = indx[4];
  607. j = indx[0];
  608. indx[0] = indx[1];
  609. indx[1] = indx[2];
  610. indx[2] = indx[3];
  611. }
  612. else if (codvec[8] == 2)
  613. {
  614. i = indx[0];
  615. j = indx[1];
  616. indx[0] = indx[2];
  617. indx[1] = indx[3];
  618. indx[2] = indx[4];
  619. }
  620. else if (codvec[8] == 3)
  621. {
  622. i = indx[1];
  623. j = indx[2];
  624. indx[1] = indx[4];
  625. indx[2] = indx[0];
  626. indx[0] = indx[3];
  627. }
  628. indx[3] = shl(codvec[8], 9)
  629. + shl((i & 16), 4)
  630. + shl((j & 16), 3)
  631. + shr(extract_l(L_mult((i & 15), NB_POS)), 1)
  632. + (j & 15);
  633. for (i = 0; i < l_subfr; i++)
  634. {
  635. h[i] = H[i];
  636. h_inv[i] = negate(h[i]);
  637. }
  638. p0 = h_inv - codvec[0];
  639. p1 = h_inv - codvec[1];
  640. p2 = h_inv - codvec[2];
  641. p3 = h_inv - codvec[3];
  642. p4 = h_inv - codvec[4];
  643. p5 = h_inv - codvec[5];
  644. p6 = h_inv - codvec[6];
  645. p7 = h_inv - codvec[7];
  646. for (i = 0; i < l_subfr; i++)
  647. {
  648. s = L_mult(*p0++, 8192); /* Q12 --> Q10 */
  649. s = L_mac(s, *p1++, 8192);
  650. s = L_mac(s, *p2++, 8192);
  651. s = L_mac(s, *p3++, 8192);
  652. s = L_mac(s, *p4++, 8192);
  653. s = L_mac(s, *p5++, 8192);
  654. s = L_mac(s, *p6++, 8192);
  655. s = L_mac(s, *p7++, 8192);
  656. y[i] = round32(s);
  657. }
  658. return;
  659. }
  660. /*-------------------------------------------------------------------*
  661. * Function cor_h_vec() *
  662. * ~~~~~~~~~~~~~~~~~~~~~ *
  663. * Compute correlations of h[] with vec[] for the specified track. *
  664. *-------------------------------------------------------------------*/
  665. static void cor_h_vec(
  666. Shortword h[], /* (i) scaled impulse response */
  667. Shortword vec[], /* (i) vector to correlate with h[] */
  668. Shortword track, /* (i) track to use */
  669. Shortword sign[], /* (i) sign vector */
  670. Shortword rrixix[][NB_POS], /* (i) correlation of h[x] with h[x] */
  671. Shortword cor[] /* (o) result of correlation (NB_POS elements) */
  672. )
  673. {
  674. Shortword i, j, pos;
  675. Shortword *p0, *p1, *p2;
  676. Longword s;
  677. p0 = rrixix[track];
  678. pos = track;
  679. for (i = 0; i < NB_POS; i++, pos += STEP)
  680. {
  681. s = 0;
  682. p1 = h;
  683. p2 = &vec[pos];
  684. for (j = pos; j < L_SUBFR; j++)
  685. s = L_mac(s, *p1++, *p2++);
  686. cor[i] = add(mult(round32(s), sign[pos]), *p0++);
  687. }
  688. return;
  689. }
  690. /*-------------------------------------------------------------------*
  691. * Function search_ixiy() *
  692. * ~~~~~~~~~~~~~~~~~~~~~~~ *
  693. * Find the best positions of 2 pulses in a subframe. *
  694. *-------------------------------------------------------------------*/
  695. static void search_ixiy(
  696. Shortword track_x, /* (i) track of pulse 1 */
  697. Shortword track_y, /* (i) track of pulse 2 */
  698. Shortword * ps, /* (i/o) correlation of all fixed pulses */
  699. Shortword * alp, /* (i/o) energy of all fixed pulses */
  700. Shortword * ix, /* (o) position of pulse 1 */
  701. Shortword * iy, /* (o) position of pulse 2 */
  702. Shortword dn[], /* (i) corr. between target and h[] */
  703. Shortword cn[], /* (i) corr. vector (search if cn[]>=0) */
  704. Shortword cor_x[], /* (i) corr. of pulse 1 with fixed pulses */
  705. Shortword cor_y[], /* (i) corr. of pulse 2 with fixed pulses */
  706. Shortword rrixiy[][MSIZE] /* (i) corr. of pulse 1 with pulse 2 */
  707. )
  708. {
  709. Shortword x, y, pos;
  710. Shortword ps1, ps2, sq, sqk;
  711. Shortword alp1, alp2, alpk;
  712. Shortword *p0, *p1, *p2;
  713. Longword s;
  714. p0 = cor_x;
  715. p1 = cor_y;
  716. p2 = rrixiy[track_x];
  717. /* default value */
  718. sqk = -1;
  719. alpk = 1;
  720. *ix = track_x;
  721. *iy = track_y;
  722. for (x = track_x; x < L_SUBFR; x += STEP)
  723. {
  724. ps1 = add(*ps, dn[x]);
  725. alp1 = add(*alp, *p0++);
  726. if (cn[x] >= 0)
  727. {
  728. pos = -1;
  729. for (y = track_y; y < L_SUBFR; y += STEP)
  730. {
  731. ps2 = add(ps1, dn[y]);
  732. alp2 = add(alp1, add(*p1++, *p2++));
  733. sq = mult(ps2, ps2);
  734. s = L_msu(L_mult(alpk, sq), sqk, alp2);
  735. if (s > 0)
  736. {
  737. sqk = sq;
  738. alpk = alp2;
  739. pos = y;
  740. }
  741. }
  742. p1 -= NB_POS;
  743. if (pos >= 0)
  744. {
  745. *ix = x;
  746. *iy = pos;
  747. }
  748. }
  749. else
  750. {
  751. p2 += NB_POS;
  752. }
  753. }
  754. *ps = add(*ps, add(dn[*ix], dn[*iy]));
  755. *alp = alpk;
  756. return;
  757. }