s10_8pf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. ********************************************************************************
  3. *
  4. * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
  5. * R99 Version 3.3.0
  6. * REL-4 Version 4.1.0
  7. *
  8. ********************************************************************************
  9. *
  10. * File : s10_8pf.c
  11. * Purpose : Searches a 35/31 bit algebraic codebook containing
  12. * : 10/8 pulses in a frame of 40 samples.
  13. *
  14. ********************************************************************************
  15. */
  16. /*
  17. ********************************************************************************
  18. * MODULE INCLUDE FILE AND VERSION ID
  19. ********************************************************************************
  20. */
  21. #include "s10_8pf.h"
  22. const char s10_8pf_id[] = "@(#)$Id $" s10_8pf_h;
  23. /*
  24. ********************************************************************************
  25. * INCLUDE FILES
  26. ********************************************************************************
  27. */
  28. #include "typedef.h"
  29. #include "basic_op.h"
  30. #include "count.h"
  31. #include "cnst.h"
  32. /*
  33. ********************************************************************************
  34. * LOCAL VARIABLES AND TABLES
  35. ********************************************************************************
  36. */
  37. /*************************************************************************
  38. *
  39. * FUNCTION search_10and8i40()
  40. *
  41. * PURPOSE: Search the best codevector; determine positions of the 10/8
  42. * pulses in the 40-sample frame.
  43. *
  44. * search_10and8i40 (10,5,5,dn, rr, ipos, pos_max, codvec); for GSMEFR
  45. * search_10and8i40 (8, 4,4,dn, rr, ipos, pos_max, codvec); for 10.2
  46. *
  47. *************************************************************************/
  48. #define _1_2 (Word16)(32768L/2)
  49. #define _1_4 (Word16)(32768L/4)
  50. #define _1_8 (Word16)(32768L/8)
  51. #define _1_16 (Word16)(32768L/16)
  52. #define _1_32 (Word16)(32768L/32)
  53. #define _1_64 (Word16)(32768L/64)
  54. #define _1_128 (Word16)(32768L/128)
  55. void search_10and8i40 (
  56. Word16 nbPulse, /* i : nbpulses to find */
  57. Word16 step, /* i : stepsize */
  58. Word16 nbTracks, /* i : nbTracks */
  59. Word16 dn[], /* i : correlation between target and h[] */
  60. Word16 rr[][L_CODE], /* i : matrix of autocorrelation */
  61. Word16 ipos[], /* i : starting position for each pulse */
  62. Word16 pos_max[], /* i : position of maximum of dn[] */
  63. Word16 codvec[] /* o : algebraic codebook vector */
  64. )
  65. {
  66. Word16 i0, i1, i2, i3, i4, i5, i6, i7, i8, i9;
  67. Word16 i, j, k, pos, ia, ib;
  68. Word16 psk, ps, ps0, ps1, ps2, sq, sq2;
  69. Word16 alpk, alp, alp_16;
  70. Word16 rrv[L_CODE];
  71. Word32 s, alp0, alp1, alp2;
  72. Word16 gsmefrFlag;
  73. test();
  74. if (sub_ex(nbPulse, 10) == 0)
  75. {
  76. gsmefrFlag=1; move16 ();
  77. }
  78. else
  79. {
  80. gsmefrFlag=0; move16 ();
  81. }
  82. /* fix i0 on maximum of correlation position */
  83. i0 = pos_max[ipos[0]]; move16 ();
  84. /*------------------------------------------------------------------*
  85. * i1 loop: *
  86. *------------------------------------------------------------------*/
  87. /* Default value */
  88. psk = -1; move16 ();
  89. alpk = 1; move16 ();
  90. for (i = 0; i < nbPulse; i++)
  91. {
  92. codvec[i] = i; move16 ();
  93. }
  94. for (i = 1; i < nbTracks; i++)
  95. {
  96. i1 = pos_max[ipos[1]]; move16 ();
  97. ps0 = add_ex (dn[i0], dn[i1]);
  98. alp0 = L_mult_ex (rr[i0][i0], _1_16);
  99. alp0 = L_mac_ex (alp0, rr[i1][i1], _1_16);
  100. alp0 = L_mac_ex (alp0, rr[i0][i1], _1_8);
  101. /*----------------------------------------------------------------*
  102. * i2 and i3 loop: *
  103. *----------------------------------------------------------------*/
  104. /* initialize 4 indices for next loop. */
  105. move16 (); /* initialize "rr[i3][i3]" pointer */
  106. move16 (); /* initialize "rr[i0][i3]" pointer */
  107. move16 (); /* initialize "rr[i1][i3]" pointer */
  108. move16 (); /* initialize "rrv[i3]" pointer */
  109. for (i3 = ipos[3]; i3 < L_CODE; i3 += step)
  110. {
  111. s = L_mult_ex (rr[i3][i3], _1_8); /* index incr= step+L_CODE */
  112. s = L_mac_ex (s, rr[i0][i3], _1_4); /* index increment = step */
  113. s = L_mac_ex (s, rr[i1][i3], _1_4); /* index increment = step */
  114. rrv[i3] = round_ex (s); move16 ();
  115. }
  116. /* Default value */
  117. sq = -1; move16 ();
  118. alp = 1; move16 ();
  119. ps = 0; move16 ();
  120. ia = ipos[2]; move16 ();
  121. ib = ipos[3]; move16 ();
  122. /* initialize 4 indices for i2 loop. */
  123. move16 (); /* initialize "dn[i2]" pointer */
  124. move16 (); /* initialize "rr[i2][i2]" pointer */
  125. move16 (); /* initialize "rr[i0][i2]" pointer */
  126. move16 (); /* initialize "rr[i1][i2]" pointer */
  127. for (i2 = ipos[2]; i2 < L_CODE; i2 += step)
  128. {
  129. /* index increment = step */
  130. ps1 = add_ex (ps0, dn[i2]);
  131. /* index incr= step+L_CODE */
  132. alp1 = L_mac_ex (alp0, rr[i2][i2], _1_16);
  133. /* index increment = step */
  134. alp1 = L_mac_ex (alp1, rr[i0][i2], _1_8);
  135. /* index increment = step */
  136. alp1 = L_mac_ex (alp1, rr[i1][i2], _1_8);
  137. /* initialize 3 indices for i3 inner loop */
  138. move16 (); /* initialize "dn[i3]" pointer */
  139. move16 (); /* initialize "rrv[i3]" pointer */
  140. move16 (); /* initialize "rr[i2][i3]" pointer */
  141. for (i3 = ipos[3]; i3 < L_CODE; i3 += step)
  142. {
  143. /* index increment = step */
  144. ps2 = add_ex (ps1, dn[i3]);
  145. /* index increment = step */
  146. alp2 = L_mac_ex (alp1, rrv[i3], _1_2);
  147. /* index increment = step */
  148. alp2 = L_mac_ex (alp2, rr[i2][i3], _1_8);
  149. sq2 = mult_ex (ps2, ps2);
  150. alp_16 = round_ex (alp2);
  151. s = L_msu_ex (L_mult_ex (alp, sq2), sq, alp_16);
  152. test ();
  153. if (s > 0)
  154. {
  155. sq = sq2; move16 ();
  156. ps = ps2; move16 ();
  157. alp = alp_16; move16 ();
  158. ia = i2; move16 ();
  159. ib = i3; move16 ();
  160. }
  161. }
  162. }
  163. i2 = ia; move16 ();
  164. i3 = ib; move16 ();
  165. /*----------------------------------------------------------------*
  166. * i4 and i5 loop: *
  167. *----------------------------------------------------------------*/
  168. ps0 = ps; move16 ();
  169. alp0 = L_mult_ex (alp, _1_2);
  170. /* initialize 6 indices for next loop (see i2-i3 loop) */
  171. move16 (); move16 (); move16 (); move16 (); move16 (); move16 ();
  172. for (i5 = ipos[5]; i5 < L_CODE; i5 += step)
  173. {
  174. s = L_mult_ex (rr[i5][i5], _1_8);
  175. s = L_mac_ex (s, rr[i0][i5], _1_4);
  176. s = L_mac_ex (s, rr[i1][i5], _1_4);
  177. s = L_mac_ex (s, rr[i2][i5], _1_4);
  178. s = L_mac_ex (s, rr[i3][i5], _1_4);
  179. rrv[i5] = round_ex (s); move16 ();
  180. }
  181. /* Default value */
  182. sq = -1; move16 ();
  183. alp = 1; move16 ();
  184. ps = 0; move16 ();
  185. ia = ipos[4]; move16 ();
  186. ib = ipos[5]; move16 ();
  187. /* initialize 6 indices for i4 loop (see i2-i3 loop) */
  188. move16 (); move16 (); move16 (); move16 (); move16 (); move16 ();
  189. for (i4 = ipos[4]; i4 < L_CODE; i4 += step)
  190. {
  191. ps1 = add_ex (ps0, dn[i4]);
  192. alp1 = L_mac_ex (alp0, rr[i4][i4], _1_32);
  193. alp1 = L_mac_ex (alp1, rr[i0][i4], _1_16);
  194. alp1 = L_mac_ex (alp1, rr[i1][i4], _1_16);
  195. alp1 = L_mac_ex (alp1, rr[i2][i4], _1_16);
  196. alp1 = L_mac_ex (alp1, rr[i3][i4], _1_16);
  197. /* initialize 3 indices for i5 inner loop (see i2-i3 loop) */
  198. move16 (); move16 (); move16 ();
  199. for (i5 = ipos[5]; i5 < L_CODE; i5 += step)
  200. {
  201. ps2 = add_ex (ps1, dn[i5]);
  202. alp2 = L_mac_ex (alp1, rrv[i5], _1_4);
  203. alp2 = L_mac_ex (alp2, rr[i4][i5], _1_16);
  204. sq2 = mult_ex (ps2, ps2);
  205. alp_16 = round_ex (alp2);
  206. s = L_msu_ex (L_mult_ex (alp, sq2), sq, alp_16);
  207. test ();
  208. if (s > 0)
  209. {
  210. sq = sq2; move16 ();
  211. ps = ps2; move16 ();
  212. alp = alp_16; move16 ();
  213. ia = i4; move16 ();
  214. ib = i5; move16 ();
  215. }
  216. }
  217. }
  218. i4 = ia; move16 ();
  219. i5 = ib; move16 ();
  220. /*----------------------------------------------------------------*
  221. * i6 and i7 loop: *
  222. *----------------------------------------------------------------*/
  223. ps0 = ps; move16 ();
  224. alp0 = L_mult_ex (alp, _1_2);
  225. /* initialize 8 indices for next loop (see i2-i3 loop) */
  226. move16 (); move16 (); move16 (); move16 ();
  227. move16 (); move16 (); move16 (); move16 ();
  228. for (i7 = ipos[7]; i7 < L_CODE; i7 += step)
  229. {
  230. s = L_mult_ex (rr[i7][i7], _1_16);
  231. s = L_mac_ex (s, rr[i0][i7], _1_8);
  232. s = L_mac_ex (s, rr[i1][i7], _1_8);
  233. s = L_mac_ex (s, rr[i2][i7], _1_8);
  234. s = L_mac_ex (s, rr[i3][i7], _1_8);
  235. s = L_mac_ex (s, rr[i4][i7], _1_8);
  236. s = L_mac_ex (s, rr[i5][i7], _1_8);
  237. rrv[i7] = round_ex (s); move16 ();
  238. }
  239. /* Default value */
  240. sq = -1; move16 ();
  241. alp = 1; move16 ();
  242. ps = 0; move16 ();
  243. ia = ipos[6]; move16 ();
  244. ib = ipos[7]; move16 ();
  245. /* initialize 8 indices for i6 loop (see i2-i3 loop) */
  246. move16 (); move16 (); move16 (); move16 ();
  247. move16 (); move16 (); move16 (); move16 ();
  248. for (i6 = ipos[6]; i6 < L_CODE; i6 += step)
  249. {
  250. ps1 = add_ex (ps0, dn[i6]);
  251. alp1 = L_mac_ex (alp0, rr[i6][i6], _1_64);
  252. alp1 = L_mac_ex (alp1, rr[i0][i6], _1_32);
  253. alp1 = L_mac_ex (alp1, rr[i1][i6], _1_32);
  254. alp1 = L_mac_ex (alp1, rr[i2][i6], _1_32);
  255. alp1 = L_mac_ex (alp1, rr[i3][i6], _1_32);
  256. alp1 = L_mac_ex (alp1, rr[i4][i6], _1_32);
  257. alp1 = L_mac_ex (alp1, rr[i5][i6], _1_32);
  258. /* initialize 3 indices for i7 inner loop (see i2-i3 loop) */
  259. move16 (); move16 (); move16 ();
  260. for (i7 = ipos[7]; i7 < L_CODE; i7 += step)
  261. {
  262. ps2 = add_ex (ps1, dn[i7]);
  263. alp2 = L_mac_ex (alp1, rrv[i7], _1_4);
  264. alp2 = L_mac_ex (alp2, rr[i6][i7], _1_32);
  265. sq2 = mult_ex (ps2, ps2);
  266. alp_16 = round_ex (alp2);
  267. s = L_msu_ex (L_mult_ex (alp, sq2), sq, alp_16);
  268. test ();
  269. if (s > 0)
  270. {
  271. sq = sq2; move16 ();
  272. ps = ps2; move16 ();
  273. alp = alp_16; move16 ();
  274. ia = i6; move16 ();
  275. ib = i7; move16 ();
  276. }
  277. }
  278. }
  279. i6 = ia; move16 ();
  280. i7 = ib; move16 ();
  281. /* now finished searching a set of 8 pulses */
  282. test();
  283. if(gsmefrFlag != 0){
  284. /* go on with the two last pulses for GSMEFR */
  285. /*----------------------------------------------------------------*
  286. * i8 and i9 loop: *
  287. *----------------------------------------------------------------*/
  288. ps0 = ps; move16 ();
  289. alp0 = L_mult_ex (alp, _1_2);
  290. /* initialize 10 indices for next loop (see i2-i3 loop) */
  291. move16 (); move16 (); move16 (); move16 (); move16 ();
  292. move16 (); move16 (); move16 (); move16 (); move16 ();
  293. for (i9 = ipos[9]; i9 < L_CODE; i9 += step)
  294. {
  295. s = L_mult_ex (rr[i9][i9], _1_16);
  296. s = L_mac_ex (s, rr[i0][i9], _1_8);
  297. s = L_mac_ex (s, rr[i1][i9], _1_8);
  298. s = L_mac_ex (s, rr[i2][i9], _1_8);
  299. s = L_mac_ex (s, rr[i3][i9], _1_8);
  300. s = L_mac_ex (s, rr[i4][i9], _1_8);
  301. s = L_mac_ex (s, rr[i5][i9], _1_8);
  302. s = L_mac_ex (s, rr[i6][i9], _1_8);
  303. s = L_mac_ex (s, rr[i7][i9], _1_8);
  304. rrv[i9] = round_ex (s); move16 ();
  305. }
  306. /* Default value */
  307. sq = -1; move16 ();
  308. alp = 1; move16 ();
  309. ps = 0; move16 ();
  310. ia = ipos[8]; move16 ();
  311. ib = ipos[9]; move16 ();
  312. /* initialize 10 indices for i8 loop (see i2-i3 loop) */
  313. move16 (); move16 (); move16 (); move16 (); move16 ();
  314. move16 (); move16 (); move16 (); move16 (); move16 ();
  315. for (i8 = ipos[8]; i8 < L_CODE; i8 += step)
  316. {
  317. ps1 = add_ex (ps0, dn[i8]);
  318. alp1 = L_mac_ex (alp0, rr[i8][i8], _1_128);
  319. alp1 = L_mac_ex (alp1, rr[i0][i8], _1_64);
  320. alp1 = L_mac_ex (alp1, rr[i1][i8], _1_64);
  321. alp1 = L_mac_ex (alp1, rr[i2][i8], _1_64);
  322. alp1 = L_mac_ex (alp1, rr[i3][i8], _1_64);
  323. alp1 = L_mac_ex (alp1, rr[i4][i8], _1_64);
  324. alp1 = L_mac_ex (alp1, rr[i5][i8], _1_64);
  325. alp1 = L_mac_ex (alp1, rr[i6][i8], _1_64);
  326. alp1 = L_mac_ex (alp1, rr[i7][i8], _1_64);
  327. /* initialize 3 indices for i9 inner loop (see i2-i3 loop) */
  328. move16 (); move16 (); move16 ();
  329. for (i9 = ipos[9]; i9 < L_CODE; i9 += step)
  330. {
  331. ps2 = add_ex (ps1, dn[i9]);
  332. alp2 = L_mac_ex (alp1, rrv[i9], _1_8);
  333. alp2 = L_mac_ex (alp2, rr[i8][i9], _1_64);
  334. sq2 = mult_ex (ps2, ps2);
  335. alp_16 = round_ex (alp2);
  336. s = L_msu_ex (L_mult_ex (alp, sq2), sq, alp_16);
  337. test ();
  338. if (s > 0)
  339. {
  340. sq = sq2; move16 ();
  341. ps = ps2; move16 ();
  342. alp = alp_16; move16 ();
  343. ia = i8; move16 ();
  344. ib = i9; move16 ();
  345. }
  346. }
  347. }
  348. }/* end gsmefrFlag */
  349. /*---------------------------------------------------------------- *
  350. * test and memorise if this combination is better than the last one.*
  351. *----------------------------------------------------------------*/
  352. s = L_msu_ex (L_mult_ex (alpk, sq), psk, alp);
  353. test ();
  354. if (s > 0)
  355. {
  356. psk = sq; move16 ();
  357. alpk = alp; move16 ();
  358. codvec[0] = i0; move16 ();
  359. codvec[1] = i1; move16 ();
  360. codvec[2] = i2; move16 ();
  361. codvec[3] = i3; move16 ();
  362. codvec[4] = i4; move16 ();
  363. codvec[5] = i5; move16 ();
  364. codvec[6] = i6; move16 ();
  365. codvec[7] = i7; move16 ();
  366. test();
  367. if (gsmefrFlag != 0)
  368. {
  369. codvec[8] = ia; move16 ();
  370. codvec[9] = ib; move16 ();
  371. }
  372. }
  373. /*----------------------------------------------------------------*
  374. * Cyclic permutation of i1,i2,i3,i4,i5,i6,i7,(i8 and i9). *
  375. *----------------------------------------------------------------*/
  376. pos = ipos[1]; move16 ();
  377. for (j = 1, k = 2; k < nbPulse; j++, k++)
  378. {
  379. ipos[j] = ipos[k]; move16 ();
  380. }
  381. ipos[sub_ex(nbPulse,1)] = pos; move16 ();
  382. } /* end 1..nbTracks loop*/
  383. }