c2_11pf.c 14 KB

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