c3_14pf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 : c3_14pf.c
  11. * Purpose : Searches a 14 bit algebraic codebook containing 3 pulses
  12. * in a frame of 40 samples.
  13. *
  14. ********************************************************************************
  15. */
  16. /*
  17. ********************************************************************************
  18. * MODULE INCLUDE FILE AND VERSION ID
  19. ********************************************************************************
  20. */
  21. #include "c3_14pf.h"
  22. const char c3_14pf_id[] = "@(#)$Id $" c3_14pf_h;
  23. /*
  24. ********************************************************************************
  25. * INCLUDE FILES
  26. ********************************************************************************
  27. */
  28. #include "typedef.h"
  29. #include "basic_op.h"
  30. #include "count.h"
  31. #include "inv_sqrt_ex.h"
  32. #include "cnst.h"
  33. #include "cor_h.h"
  34. #include "set_sign.h"
  35. /*
  36. ********************************************************************************
  37. * LOCAL VARIABLES AND TABLES
  38. ********************************************************************************
  39. */
  40. #define NB_PULSE 3
  41. /*
  42. ********************************************************************************
  43. * DECLARATION OF PROTOTYPES
  44. ********************************************************************************
  45. */
  46. static void search_3i40(
  47. Word16 dn[], /* i : correlation between target and h[] */
  48. Word16 dn2[], /* i : maximum of corr. in each track. */
  49. Word16 rr[][L_CODE],/* i : matrix of autocorrelation */
  50. Word16 codvec[] /* o : algebraic codebook vector */
  51. );
  52. static Word16 build_code(
  53. Word16 codvec[], /* i : algebraic codebook vector */
  54. Word16 dn_sign[], /* i : sign of dn[] */
  55. Word16 cod[], /* o : algebraic (fixed) codebook excitation */
  56. Word16 h[], /* i : impulse response of weighted synthesis filter */
  57. Word16 y[], /* o : filtered fixed codebook excitation */
  58. Word16 sign[] /* o : sign of 3 pulses */
  59. );
  60. /*
  61. ********************************************************************************
  62. * PUBLIC PROGRAM CODE
  63. ********************************************************************************
  64. */
  65. /*************************************************************************
  66. *
  67. * FUNCTION: code_3i40_14bits()
  68. *
  69. * PURPOSE: Searches a 14 bit algebraic codebook containing 3 pulses
  70. * in a frame of 40 samples.
  71. *
  72. * DESCRIPTION:
  73. * The code length is 40, containing 3 nonzero pulses: i0...i2.
  74. * All pulses can have two possible amplitudes: +1 or -1.
  75. * Pulse i0 can have 8 possible positions, pulses i1 and i2 can have
  76. * 2x8=16 positions.
  77. *
  78. * i0 : 0, 5, 10, 15, 20, 25, 30, 35.
  79. * i1 : 1, 6, 11, 16, 21, 26, 31, 36.
  80. * 3, 8, 13, 18, 23, 28, 33, 38.
  81. * i2 : 2, 7, 12, 17, 22, 27, 32, 37.
  82. * 4, 9, 14, 19, 24, 29, 34, 39.
  83. *
  84. *************************************************************************/
  85. Word16 code_3i40_14bits(
  86. Word16 x[], /* i : target vector */
  87. Word16 h[], /* i : impulse response of weighted synthesis filter */
  88. /* h[-L_subfr..-1] must be set to zero. */
  89. Word16 T0, /* i : Pitch lag */
  90. Word16 pitch_sharp, /* i : Last quantized pitch gain */
  91. Word16 code[], /* o : Innovative codebook */
  92. Word16 y[], /* o : filtered fixed codebook excitation */
  93. Word16 * sign /* o : Signs of 3 pulses */
  94. )
  95. {
  96. Word16 codvec[NB_PULSE];
  97. Word16 dn[L_CODE], dn2[L_CODE], dn_sign[L_CODE];
  98. Word16 rr[L_CODE][L_CODE];
  99. Word16 i, index, sharp;
  100. sharp = shl_ex(pitch_sharp, 1);
  101. test ();
  102. if (sub_ex(T0, L_CODE) < 0)
  103. {
  104. for (i = T0; i < L_CODE; i++) {
  105. h[i] = add_ex(h[i], mult_ex(h[i - T0], sharp)); move16 ();
  106. }
  107. }
  108. cor_h_x_ex(h, x, dn, 1);
  109. set_sign(dn, dn_sign, dn2, 6);
  110. cor_h(h, dn_sign, rr);
  111. search_3i40(dn, dn2, rr, codvec);
  112. move16 (); /* function result */
  113. index = build_code(codvec, dn_sign, code, h, y, sign);
  114. /*-----------------------------------------------------------------*
  115. * Compute innovation vector gain. *
  116. * Include fixed-gain pitch contribution into code[]. *
  117. *-----------------------------------------------------------------*/
  118. test ();
  119. if (sub_ex(T0, L_CODE) < 0)
  120. {
  121. for (i = T0; i < L_CODE; i++) {
  122. code[i] = add_ex(code[i], mult_ex(code[i - T0], sharp)); move16 ();
  123. }
  124. }
  125. return index;
  126. }
  127. /*
  128. ********************************************************************************
  129. * PRIVATE PROGRAM CODE
  130. ********************************************************************************
  131. */
  132. /*************************************************************************
  133. *
  134. * FUNCTION search_3i40()
  135. *
  136. * PURPOSE: Search the best codevector; determine positions of the 3 pulses
  137. * in the 40-sample frame.
  138. *
  139. *************************************************************************/
  140. #define _1_2 (Word16)(32768L/2)
  141. #define _1_4 (Word16)(32768L/4)
  142. #define _1_8 (Word16)(32768L/8)
  143. #define _1_16 (Word16)(32768L/16)
  144. static void search_3i40(
  145. Word16 dn[], /* i : correlation between target and h[] */
  146. Word16 dn2[], /* i : maximum of corr. in each track. */
  147. Word16 rr[][L_CODE], /* i : matrix of autocorrelation */
  148. Word16 codvec[] /* o : algebraic codebook vector */
  149. )
  150. {
  151. Word16 i0, i1, i2;
  152. Word16 ix = 0; /* initialization only needed to keep gcc silent */
  153. Word16 ps = 0; /* initialization only needed to keep gcc silent */
  154. Word16 i, pos, track1, track2, ipos[NB_PULSE];
  155. Word16 psk, ps0, ps1, sq, sq1;
  156. Word16 alpk, alp, alp_16;
  157. Word32 s, alp0, alp1;
  158. psk = -1; move16 ();
  159. alpk = 1; move16 ();
  160. for (i = 0; i < NB_PULSE; i++)
  161. {
  162. codvec[i] = i; move16 ();
  163. }
  164. for (track1 = 1; track1 < 4; track1 += 2)
  165. {
  166. for (track2 = 2; track2 < 5; track2 += 2)
  167. {
  168. /* fix starting position */
  169. ipos[0] = 0; move16 ();
  170. ipos[1] = track1; move16 ();
  171. ipos[2] = track2; move16 ();
  172. /*------------------------------------------------------------------*
  173. * main loop: try 3 tracks. *
  174. *------------------------------------------------------------------*/
  175. for (i = 0; i < NB_PULSE; i++)
  176. {
  177. /*----------------------------------------------------------------*
  178. * i0 loop: try 8 positions. *
  179. *----------------------------------------------------------------*/
  180. move16 (); /* account for ptr. init. (rr[io]) */
  181. for (i0 = ipos[0]; i0 < L_CODE; i0 += STEP)
  182. {
  183. test ();
  184. if (dn2[i0] >= 0)
  185. {
  186. ps0 = dn[i0]; move16 ();
  187. alp0 = L_mult_ex(rr[i0][i0], _1_4);
  188. /*----------------------------------------------------------------*
  189. * i1 loop: 8 positions. *
  190. *----------------------------------------------------------------*/
  191. sq = -1; move16 ();
  192. alp = 1; move16 ();
  193. ps = 0; move16 ();
  194. ix = ipos[1]; move16 ();
  195. /* initialize 4 index for next loop. */
  196. /*-------------------------------------------------------------------*
  197. * These index have low complexity address computation because *
  198. * they are, in fact, pointers with fixed increment. For example, *
  199. * "rr[i0][i2]" is a pointer initialized to "&rr[i0][ipos[2]]" *
  200. * and incremented by "STEP". *
  201. *-------------------------------------------------------------------*/
  202. move16 (); /* account for ptr. init. (rr[i1]) */
  203. move16 (); /* account for ptr. init. (dn[i1]) */
  204. move16 (); /* account for ptr. init. (rr[io]) */
  205. for (i1 = ipos[1]; i1 < L_CODE; i1 += STEP)
  206. {
  207. ps1 = add_ex(ps0, dn[i1]); /* idx increment = STEP */
  208. /* alp1 = alp0 + rr[i0][i1] + 1/2*rr[i1][i1]; */
  209. alp1 = L_mac_ex(alp0, rr[i1][i1], _1_4); /* idx incr = STEP */
  210. alp1 = L_mac_ex(alp1, rr[i0][i1], _1_2); /* idx incr = STEP */
  211. sq1 = mult_ex(ps1, ps1);
  212. alp_16 = round_ex(alp1);
  213. s = L_msu_ex(L_mult_ex(alp, sq1), sq, alp_16);
  214. test ();
  215. if (s > 0)
  216. {
  217. sq = sq1; move16 ();
  218. ps = ps1; move16 ();
  219. alp = alp_16; move16 ();
  220. ix = i1; move16 ();
  221. }
  222. }
  223. i1 = ix; move16 ();
  224. /*----------------------------------------------------------------*
  225. * i2 loop: 8 positions. *
  226. *----------------------------------------------------------------*/
  227. ps0 = ps; move16 ();
  228. alp0 = L_mult_ex(alp, _1_4);
  229. sq = -1; move16 ();
  230. alp = 1; move16 ();
  231. ps = 0; move16 ();
  232. ix = ipos[2]; move16 ();
  233. /* initialize 4 index for next loop (see i1 loop) */
  234. move16 (); /* account for ptr. init. (rr[i2]) */
  235. move16 (); /* account for ptr. init. (rr[i1]) */
  236. move16 (); /* account for ptr. init. (dn[i2]) */
  237. move16 (); /* account for ptr. init. (rr[io]) */
  238. for (i2 = ipos[2]; i2 < L_CODE; i2 += STEP)
  239. {
  240. ps1 = add_ex(ps0, dn[i2]); /* index increment = STEP */
  241. /* alp1 = alp0 + rr[i0][i2] + rr[i1][i2] + 1/2*rr[i2][i2]; */
  242. alp1 = L_mac_ex(alp0, rr[i2][i2], _1_16); /* idx incr = STEP */
  243. alp1 = L_mac_ex(alp1, rr[i1][i2], _1_8); /* idx incr = STEP */
  244. alp1 = L_mac_ex(alp1, rr[i0][i2], _1_8); /* idx incr = STEP */
  245. sq1 = mult_ex(ps1, ps1);
  246. alp_16 = round_ex(alp1);
  247. s = L_msu_ex(L_mult_ex(alp, sq1), sq, alp_16);
  248. test ();
  249. if (s > 0)
  250. {
  251. sq = sq1; move16 ();
  252. ps = ps1; move16 ();
  253. alp = alp_16; move16 ();
  254. ix = i2; move16 ();
  255. }
  256. }
  257. i2 = ix; move16 ();
  258. /*----------------------------------------------------------------*
  259. * memorise codevector if this one is better than the last one. *
  260. *----------------------------------------------------------------*/
  261. s = L_msu_ex(L_mult_ex(alpk, sq), psk, alp);
  262. test ();
  263. if (s > 0)
  264. {
  265. psk = sq; move16 ();
  266. alpk = alp; move16 ();
  267. codvec[0] = i0; move16 ();
  268. codvec[1] = i1; move16 ();
  269. codvec[2] = i2; move16 ();
  270. }
  271. }
  272. }
  273. /*----------------------------------------------------------------*
  274. * Cyclic permutation of i0, i1 and i2. *
  275. *----------------------------------------------------------------*/
  276. pos = ipos[2]; move16 ();
  277. ipos[2] = ipos[1]; move16 ();
  278. ipos[1] = ipos[0]; move16 ();
  279. ipos[0] = pos; move16 ();
  280. }
  281. }
  282. }
  283. return;
  284. }
  285. /*************************************************************************
  286. *
  287. * FUNCTION: build_code()
  288. *
  289. * PURPOSE: Builds the codeword, the filtered codeword and index of the
  290. * codevector, based on the signs and positions of 3 pulses.
  291. *
  292. *************************************************************************/
  293. static Word16
  294. build_code(
  295. Word16 codvec[], /* i : position of pulses */
  296. Word16 dn_sign[], /* i : sign of pulses */
  297. Word16 cod[], /* o : innovative code vector */
  298. Word16 h[], /* i : impulse response of weighted synthesis filter */
  299. Word16 y[], /* o : filtered innovative code */
  300. Word16 sign[] /* o : sign of 3 pulses */
  301. )
  302. {
  303. Word16 i, j, k, track, index, _sign[NB_PULSE], indx, rsign;
  304. Word16 *p0, *p1, *p2;
  305. Word32 s;
  306. for (i = 0; i < L_CODE; i++) {
  307. cod[i] = 0; move16 ();
  308. }
  309. indx = 0; move16 ();
  310. rsign = 0; move16 ();
  311. for (k = 0; k < NB_PULSE; k++)
  312. {
  313. i = codvec[k]; move16 (); /* read pulse position */
  314. j = dn_sign[i]; move16 (); /* read sign */
  315. index = mult_ex(i, 6554); /* index = pos/5 */
  316. /* track = pos%5 */
  317. track = sub_ex(i, extract_l_ex(L_shr_ex(L_mult_ex(index, 5), 1)));
  318. test ();
  319. if (sub_ex(track, 1) == 0)
  320. index = shl_ex(index, 4);
  321. else if (sub_ex(track, 2) == 0)
  322. {
  323. test ();
  324. track = 2; move16 ();
  325. index = shl_ex(index, 8);
  326. }
  327. else if (sub_ex(track, 3) == 0)
  328. {
  329. test (); test ();
  330. track = 1; move16 ();
  331. index = add_ex(shl_ex(index, 4), 8);
  332. }
  333. else if (sub_ex(track, 4) == 0)
  334. {
  335. test (); test (); test ();
  336. track = 2; move16 ();
  337. index = add_ex(shl_ex(index, 8), 128);
  338. }
  339. test ();
  340. if (j > 0)
  341. {
  342. cod[i] = 8191; move16 ();
  343. _sign[k] = 32767; move16 ();
  344. rsign = add_ex(rsign, shl_ex(1, track));
  345. } else {
  346. cod[i] = -8192; move16 ();
  347. _sign[k] = (Word16) - 32768L; move16 ();
  348. }
  349. indx = add_ex(indx, index);
  350. }
  351. *sign = rsign; move16 ();
  352. p0 = h - codvec[0]; move16 ();
  353. p1 = h - codvec[1]; move16 ();
  354. p2 = h - codvec[2]; move16 ();
  355. for (i = 0; i < L_CODE; i++)
  356. {
  357. s = 0; move32 ();
  358. s = L_mac_ex(s, *p0++, _sign[0]);
  359. s = L_mac_ex(s, *p1++, _sign[1]);
  360. s = L_mac_ex(s, *p2++, _sign[2]);
  361. y[i] = round_ex(s); move16 ();
  362. }
  363. return indx;
  364. }