c1035pf.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 : c1035pf.c
  11. * Purpose : Searches a 35 bit algebraic codebook containing
  12. * : 10 pulses in a frame of 40 samples.
  13. *
  14. ********************************************************************************
  15. */
  16. /*
  17. ********************************************************************************
  18. * MODULE INCLUDE FILE AND VERSION ID
  19. ********************************************************************************
  20. */
  21. #include "c1035pf.h"
  22. const char c1035pf_id[] = "@(#)$Id $" c1035pf_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. #include "inv_sqrt_ex.h"
  33. #include "set_sign.h"
  34. #include "cor_h.h"
  35. #include "s10_8pf.h"
  36. /*
  37. ********************************************************************************
  38. * LOCAL VARIABLES AND TABLES
  39. ********************************************************************************
  40. */
  41. #define NB_PULSE 10
  42. #include "gray.tab"
  43. /*
  44. ********************************************************************************
  45. * LOCAL PROGRAM CODE
  46. ********************************************************************************
  47. */
  48. void q_p (
  49. Word16 *ind, /* Pulse position */
  50. Word16 n /* Pulse number */
  51. )
  52. {
  53. Word16 tmp;
  54. tmp = *ind; move16 ();
  55. test ();
  56. if (sub_ex (n, 5) < 0)
  57. {
  58. *ind = (tmp & 0x8) | gray[tmp & 0x7]; logic16 (); logic16 ();
  59. logic16 (); move16 ();
  60. }
  61. else
  62. {
  63. *ind = gray[tmp & 0x7]; logic16 (); move16 ();
  64. }
  65. }
  66. /*************************************************************************
  67. *
  68. * FUNCTION: build_code()
  69. *
  70. * PURPOSE: Builds the codeword, the filtered codeword and index of the
  71. * codevector, based on the signs and positions of 10 pulses.
  72. *
  73. *************************************************************************/
  74. static void build_code (
  75. Word16 codvec[], /* (i) : position of pulses */
  76. Word16 sign[], /* (i) : sign of d[n] */
  77. Word16 cod[], /* (o) : innovative code vector */
  78. Word16 h[], /* (i) : impulse response of weighted synthesis filter*/
  79. Word16 y[], /* (o) : filtered innovative code */
  80. Word16 indx[] /* (o) : index of 10 pulses (sign+position) */
  81. )
  82. {
  83. Word16 i, j, k, track, index, _sign[NB_PULSE];
  84. Word16 *p0, *p1, *p2, *p3, *p4, *p5, *p6, *p7, *p8, *p9;
  85. Word32 s;
  86. for (i = 0; i < L_CODE; i++)
  87. {
  88. cod[i] = 0; move16 ();
  89. }
  90. for (i = 0; i < NB_TRACK; i++)
  91. {
  92. indx[i] = -1; move16 ();
  93. }
  94. for (k = 0; k < NB_PULSE; k++)
  95. {
  96. /* read pulse position */
  97. i = codvec[k]; move16 ();
  98. /* read sign */
  99. j = sign[i]; move16 ();
  100. index = mult_ex (i, 6554); /* index = pos/5 */
  101. /* track = pos%5 */
  102. track = sub_ex (i, extract_l_ex (L_shr_ex (L_mult_ex (index, 5), 1)));
  103. test ();
  104. if (j > 0)
  105. {
  106. cod[i] = add_ex (cod[i], 4096); move16 ();
  107. _sign[k] = 8192; move16 ();
  108. }
  109. else
  110. {
  111. cod[i] = sub_ex (cod[i], 4096); move16 ();
  112. _sign[k] = -8192; move16 ();
  113. index = add_ex (index, 8);
  114. }
  115. test (); move16 ();
  116. if (indx[track] < 0)
  117. {
  118. indx[track] = index; move16 ();
  119. }
  120. else
  121. {
  122. test (); logic16 (); logic16 ();
  123. if (((index ^ indx[track]) & 8) == 0)
  124. {
  125. /* sign of 1st pulse == sign of 2nd pulse */
  126. test ();
  127. if (sub_ex (indx[track], index) <= 0)
  128. {
  129. indx[track + 5] = index; move16 ();
  130. }
  131. else
  132. {
  133. indx[track + 5] = indx[track];
  134. move16 ();
  135. indx[track] = index; move16 ();
  136. }
  137. }
  138. else
  139. {
  140. /* sign of 1st pulse != sign of 2nd pulse */
  141. test (); logic16 (); logic16 ();
  142. if (sub_ex ((indx[track] & 7), (index & 7)) <= 0)
  143. {
  144. indx[track + 5] = indx[track];
  145. move16 ();
  146. indx[track] = index; move16 ();
  147. }
  148. else
  149. {
  150. indx[track + 5] = index; move16 ();
  151. }
  152. }
  153. }
  154. }
  155. p0 = h - codvec[0]; move16 ();
  156. p1 = h - codvec[1]; move16 ();
  157. p2 = h - codvec[2]; move16 ();
  158. p3 = h - codvec[3]; move16 ();
  159. p4 = h - codvec[4]; move16 ();
  160. p5 = h - codvec[5]; move16 ();
  161. p6 = h - codvec[6]; move16 ();
  162. p7 = h - codvec[7]; move16 ();
  163. p8 = h - codvec[8]; move16 ();
  164. p9 = h - codvec[9]; move16 ();
  165. for (i = 0; i < L_CODE; i++)
  166. {
  167. s = 0; move32 ();
  168. s = L_mac_ex (s, *p0++, _sign[0]);
  169. s = L_mac_ex (s, *p1++, _sign[1]);
  170. s = L_mac_ex (s, *p2++, _sign[2]);
  171. s = L_mac_ex (s, *p3++, _sign[3]);
  172. s = L_mac_ex (s, *p4++, _sign[4]);
  173. s = L_mac_ex (s, *p5++, _sign[5]);
  174. s = L_mac_ex (s, *p6++, _sign[6]);
  175. s = L_mac_ex (s, *p7++, _sign[7]);
  176. s = L_mac_ex (s, *p8++, _sign[8]);
  177. s = L_mac_ex (s, *p9++, _sign[9]);
  178. y[i] = round_ex (s); move16 ();
  179. }
  180. }
  181. /*
  182. ********************************************************************************
  183. * PUBLIC PROGRAM CODE
  184. ********************************************************************************
  185. */
  186. /*************************************************************************
  187. *
  188. * FUNCTION: code_10i40_35bits()
  189. *
  190. * PURPOSE: Searches a 35 bit algebraic codebook containing 10 pulses
  191. * in a frame of 40 samples.
  192. *
  193. * DESCRIPTION:
  194. * The code contains 10 nonzero pulses: i0...i9.
  195. * All pulses can have two possible amplitudes: +1 or -1.
  196. * The 40 positions in a subframe are divided into 5 tracks of
  197. * interleaved positions. Each track contains two pulses.
  198. * The pulses can have the following possible positions:
  199. *
  200. * i0, i5 : 0, 5, 10, 15, 20, 25, 30, 35.
  201. * i1, i6 : 1, 6, 11, 16, 21, 26, 31, 36.
  202. * i2, i7 : 2, 7, 12, 17, 22, 27, 32, 37.
  203. * i3, i8 : 3, 8, 13, 18, 23, 28, 33, 38.
  204. * i4, i9 : 4, 9, 14, 19, 24, 29, 34, 39.
  205. *
  206. * Each pair of pulses require 1 bit for their signs and 6 bits for their
  207. * positions (3 bits + 3 bits). This results in a 35 bit codebook.
  208. * The function determines the optimal pulse signs and positions, builds
  209. * the codevector, and computes the filtered codevector.
  210. *
  211. *************************************************************************/
  212. void code_10i40_35bits (
  213. Word16 x[], /* (i) : target vector */
  214. Word16 cn[], /* (i) : residual after long term prediction */
  215. Word16 h[], /* (i) : impulse response of weighted synthesis filter
  216. h[-L_subfr..-1] must be set to zero */
  217. Word16 cod[], /* (o) : algebraic (fixed) codebook excitation */
  218. Word16 y[], /* (o) : filtered fixed codebook excitation */
  219. Word16 indx[] /* (o) : index of 10 pulses (sign + position) */
  220. )
  221. {
  222. Word16 ipos[NB_PULSE], pos_max[NB_TRACK], codvec[NB_PULSE];
  223. Word16 dn[L_CODE], sign[L_CODE];
  224. Word16 rr[L_CODE][L_CODE], i;
  225. cor_h_x_ex (h, x, dn, 2);
  226. set_sign12k2 (dn, cn, sign, pos_max, NB_TRACK, ipos, STEP);
  227. cor_h (h, sign, rr);
  228. search_10and8i40 (NB_PULSE, STEP, NB_TRACK,
  229. dn, rr, ipos, pos_max, codvec);
  230. build_code (codvec, sign, cod, h, y, indx);
  231. for (i = 0; i < 10; i++)
  232. {
  233. q_p (&indx[i], i);
  234. }
  235. return;
  236. }