c8_31pf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 : c8_31pf.c
  11. * Purpose : Searches a 31 bit algebraic codebook containing
  12. * : 8 pulses in a frame of 40 samples.
  13. * : in the same manner as GSM-EFR
  14. *
  15. ********************************************************************************
  16. */
  17. /*
  18. ********************************************************************************
  19. * MODULE INCLUDE FILE AND VERSION ID
  20. ********************************************************************************
  21. */
  22. #include "c8_31pf.h"
  23. const char c8_31pf_id[] = "@(#)$Id $" c8_31pf_h;
  24. /*
  25. ********************************************************************************
  26. * INCLUDE FILES
  27. ********************************************************************************
  28. */
  29. #include "typedef.h"
  30. #include "basic_op.h"
  31. #include "count.h"
  32. #include "cnst.h"
  33. #include "inv_sqrt_ex.h"
  34. #include "cor_h.h"
  35. #include "set_sign.h"
  36. #include "s10_8pf.h"
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. /*
  40. ********************************************************************************
  41. * LOCAL VARIABLES AND TABLES
  42. ********************************************************************************
  43. */
  44. #define NB_PULSE 8
  45. /* define values/representation for output codevector and sign */
  46. #define POS_CODE 8191
  47. #define NEG_CODE 8191
  48. #define POS_SIGN 32767
  49. #define NEG_SIGN (Word16) (-32768L)
  50. /*
  51. ********************************************************************************
  52. * LOCAL PROGRAM CODE
  53. ********************************************************************************
  54. */
  55. /*************************************************************************
  56. *
  57. * FUNCTION: build_code()
  58. *
  59. * PURPOSE: Builds the codeword, the filtered codeword and a
  60. * linear uncombined version of the index of the
  61. * codevector, based on the signs and positions of 8 pulses.
  62. *
  63. *************************************************************************/
  64. static void build_code (
  65. Word16 codvec[], /* i : position of pulses */
  66. Word16 sign[], /* i : sign of d[n] */
  67. Word16 cod[], /* o : innovative code vector */
  68. Word16 h[], /* i : impulse response of weighted synthesis filter*/
  69. Word16 y[], /* o : filtered innovative code */
  70. Word16 sign_indx[], /* o : signs of 4 pulses (signs only) */
  71. Word16 pos_indx[] /* o : position index of 8 pulses(position only) */
  72. )
  73. {
  74. Word16 i, j, k, track, sign_index, pos_index, _sign[NB_PULSE];
  75. Word16 *p0, *p1, *p2, *p3, *p4, *p5, *p6, *p7;
  76. Word32 s;
  77. for (i = 0; i < L_CODE; i++)
  78. {
  79. cod[i] = 0; move16 ();
  80. }
  81. for (i = 0; i < NB_TRACK_MR102; i++)
  82. {
  83. pos_indx[i] = -1; move16 ();
  84. sign_indx[i] = -1; move16 ();
  85. }
  86. for (k = 0; k < NB_PULSE; k++)
  87. {
  88. /* read pulse position */
  89. i = codvec[k]; move16 ();
  90. /* read sign */
  91. j = sign[i]; move16 ();
  92. pos_index = shr_ex(i, 2); /* index = pos/4 */
  93. track = i & 3; logic16 (); /* track = pos%4 */
  94. test ();
  95. if (j > 0)
  96. {
  97. cod[i] = add_ex (cod[i], POS_CODE); move16 ();
  98. _sign[k] = POS_SIGN; move16 ();
  99. sign_index = 0; /* bit=0 -> positive pulse */ move16 ();
  100. }
  101. else
  102. {
  103. cod[i] = sub_ex (cod[i], NEG_CODE); move16 ();
  104. _sign[k] = NEG_SIGN; move16 ();
  105. sign_index = 1; move16 (); /* bit=1 => negative pulse */
  106. /* index = add_ex (index, 8); 1 = negative old code */
  107. }
  108. test (); move16 ();
  109. if (pos_indx[track] < 0)
  110. { /* first set first NB_TRACK pulses */
  111. pos_indx[track] = pos_index; move16 ();
  112. sign_indx[track] = sign_index; move16 ();
  113. }
  114. else
  115. { /* 2nd row of pulses , test if positions needs to be switched */
  116. test (); logic16 (); logic16 ();
  117. if (((sign_index ^ sign_indx[track]) & 1) == 0)
  118. {
  119. /* sign of 1st pulse == sign of 2nd pulse */
  120. test ();
  121. if (sub_ex (pos_indx[track], pos_index) <= 0)
  122. { /* no swap */
  123. pos_indx[track + NB_TRACK_MR102] = pos_index; move16 ();
  124. }
  125. else
  126. { /* swap*/
  127. pos_indx[track + NB_TRACK_MR102] = pos_indx[track];
  128. move16 ();
  129. pos_indx[track] = pos_index; move16 ();
  130. sign_indx[track] = sign_index; move16 ();
  131. }
  132. }
  133. else
  134. {
  135. /* sign of 1st pulse != sign of 2nd pulse */
  136. test ();
  137. if (sub_ex (pos_indx[track], pos_index) <= 0)
  138. { /*swap*/
  139. pos_indx[track + NB_TRACK_MR102] = pos_indx[track];
  140. move16 ();
  141. pos_indx[track] = pos_index; move16 ();
  142. sign_indx[track] = sign_index; move16 ();
  143. }
  144. else
  145. { /*no swap */
  146. pos_indx[track + NB_TRACK_MR102] = pos_index; move16 ();
  147. }
  148. }
  149. }
  150. }
  151. p0 = h - codvec[0]; move16 ();
  152. p1 = h - codvec[1]; move16 ();
  153. p2 = h - codvec[2]; move16 ();
  154. p3 = h - codvec[3]; move16 ();
  155. p4 = h - codvec[4]; move16 ();
  156. p5 = h - codvec[5]; move16 ();
  157. p6 = h - codvec[6]; move16 ();
  158. p7 = h - codvec[7]; move16 ();
  159. for (i = 0; i < L_CODE; i++)
  160. {
  161. s = 0; move32 ();
  162. s = L_mac_ex (s, *p0++, _sign[0]);
  163. s = L_mac_ex (s, *p1++, _sign[1]);
  164. s = L_mac_ex (s, *p2++, _sign[2]);
  165. s = L_mac_ex (s, *p3++, _sign[3]);
  166. s = L_mac_ex (s, *p4++, _sign[4]);
  167. s = L_mac_ex (s, *p5++, _sign[5]);
  168. s = L_mac_ex (s, *p6++, _sign[6]);
  169. s = L_mac_ex (s, *p7++, _sign[7]);
  170. y[i] = round_ex (s); move16 ();
  171. }
  172. }
  173. /*************************************************************************
  174. *
  175. * FUNCTION: compress_code()
  176. *
  177. * PURPOSE: compression of three indeces [0..9] to one 10 bit index
  178. * minimizing the phase shift of a bit error.
  179. *
  180. *************************************************************************/
  181. static Word16 compress10 (
  182. Word16 pos_indxA, /* i : signs of 4 pulses (signs only) */
  183. Word16 pos_indxB, /* i : position index of 8 pulses (pos only) */
  184. Word16 pos_indxC) /* i : position and sign of 8 pulses (compressed) */
  185. {
  186. Word16 indx, ia,ib,ic;
  187. ia = shr_ex(pos_indxA, 1);
  188. ib = extract_l_ex(L_shr_ex(L_mult_ex(shr_ex(pos_indxB, 1), 5), 1));
  189. ic = extract_l_ex(L_shr_ex(L_mult_ex(shr_ex(pos_indxC, 1), 25), 1));
  190. indx = shl_ex(add_ex(ia, add_ex(ib, ic)), 3);
  191. ia = pos_indxA & 1; logic16 ();
  192. ib = shl_ex((pos_indxB & 1), 1); logic16 ();
  193. ic = shl_ex((pos_indxC & 1), 2); logic16 ();
  194. indx = add_ex(indx , add_ex(ia, add_ex(ib, ic)));
  195. return indx;
  196. }
  197. /*************************************************************************
  198. *
  199. * FUNCTION: compress_code()
  200. *
  201. * PURPOSE: compression of the linear codewords to 4+three indeces
  202. * one bit from each pulse is made robust to errors by
  203. * minimizing the phase shift of a bit error.
  204. * 4 signs (one for each track)
  205. * i0,i4,i1 => one index (7+3) bits, 3 LSBs more robust
  206. * i2,i6,i5 => one index (7+3) bits, 3 LSBs more robust
  207. * i3,i7 => one index (5+2) bits, 2-3 LSbs more robust
  208. *
  209. *************************************************************************/
  210. static void compress_code (
  211. Word16 sign_indx[], /* i : signs of 4 pulses (signs only) */
  212. Word16 pos_indx[], /* i : position index of 8 pulses (position only) */
  213. Word16 indx[]) /* o : position and sign of 8 pulses (compressed) */
  214. {
  215. Word16 i, ia, ib, ic;
  216. for (i = 0; i < NB_TRACK_MR102; i++)
  217. {
  218. indx[i] = sign_indx[i]; move16 ();
  219. }
  220. /* First index
  221. indx[NB_TRACK] = (ia/2+(ib/2)*5 +(ic/2)*25)*8 + ia%2 + (ib%2)*2 + (ic%2)*4; */
  222. move16 ();
  223. indx[NB_TRACK_MR102] = compress10(pos_indx[0],pos_indx[4],pos_indx[1]);
  224. /* Second index
  225. indx[NB_TRACK+1] = (ia/2+(ib/2)*5 +(ic/2)*25)*8 + ia%2 + (ib%2)*2 + (ic%2)*4; */
  226. move16 ();
  227. indx[NB_TRACK_MR102+1]= compress10(pos_indx[2],pos_indx[6],pos_indx[5]);
  228. /*
  229. Third index
  230. if ((ib/2)%2 == 1)
  231. indx[NB_TRACK+2] = ((((4-ia/2) + (ib/2)*5)*32+12)/25)*4 + ia%2 + (ib%2)*2;
  232. else
  233. indx[NB_TRACK+2] = ((((ia/2) + (ib/2)*5)*32+12)/25)*4 + ia%2 + (ib%2)*2;
  234. */
  235. ib = shr_ex(pos_indx[7], 1) & 1; logic16 ();
  236. test ();
  237. if (sub_ex(ib, 1) == 0)
  238. ia = sub_ex(4, shr_ex(pos_indx[3], 1));
  239. else
  240. ia = shr_ex(pos_indx[3], 1);
  241. ib = extract_l_ex(L_shr_ex(L_mult_ex(shr_ex(pos_indx[7], 1), 5), 1));
  242. ib = add_ex(shl_ex(add_ex(ia, ib), 5), 12);
  243. ic = shl_ex(mult_ex(ib, 1311), 2);
  244. ia = pos_indx[3] & 1; logic16 ();
  245. ib = shl_ex((pos_indx[7] & 1), 1); logic16 ();
  246. indx[NB_TRACK_MR102+2] = add_ex(ia, add_ex(ib, ic));
  247. }
  248. /*
  249. ********************************************************************************
  250. * PUBLIC PROGRAM CODE
  251. ********************************************************************************
  252. */
  253. /*************************************************************************
  254. *
  255. * FUNCTION: code_8i40_31bits()
  256. *
  257. * PURPOSE: Searches a 31 bit algebraic codebook containing 8 pulses
  258. * in a frame of 40 samples.
  259. *
  260. * DESCRIPTION:
  261. * The code contains 8 nonzero pulses: i0...i7.
  262. * All pulses can have two possible amplitudes: +1 or -1.
  263. * The 40 positions in a subframe are divided into 4 tracks of
  264. * interleaved positions. Each track contains two pulses.
  265. * The pulses can have the following possible positions:
  266. *
  267. * i0, i4 : 0, 4, 8, 12, 16, 20, 24, 28, 32, 36
  268. * i1, i5 : 1, 5, 9, 13, 17, 21, 25, 29, 33, 37
  269. * i2, i6 : 2, 6, 10, 14, 18, 22, 26, 30, 34, 38
  270. * i3, i7 : 3, 7, 11, 15, 19, 23, 27, 31, 35, 39
  271. *
  272. * Each pair of pulses require 1 bit for their signs. The positions
  273. * are encoded together 3,3 and 2 resulting in
  274. * (7+3) + (7+3) + (5+2) bits for their
  275. * positions. This results in a 31 (4 sign and 27 pos) bit codebook.
  276. * The function determines the optimal pulse signs and positions, builds
  277. * the codevector, and computes the filtered codevector.
  278. *
  279. *************************************************************************/
  280. void code_8i40_31bits (
  281. Word16 x[], /* i : target vector */
  282. Word16 cn[], /* i : residual after long term prediction */
  283. Word16 h[], /* i : impulse response of weighted synthesis
  284. filter */
  285. Word16 cod[], /* o : algebraic (fixed) codebook excitation */
  286. Word16 y[], /* o : filtered fixed codebook excitation */
  287. Word16 indx[] /* o : 7 Word16, index of 8 pulses (signs+positions) */
  288. )
  289. {
  290. Word16 ipos[NB_PULSE], pos_max[NB_TRACK_MR102], codvec[NB_PULSE];
  291. Word16 dn[L_CODE], sign[L_CODE];
  292. Word16 rr[L_CODE][L_CODE];
  293. Word16 linear_signs[NB_TRACK_MR102];
  294. Word16 linear_codewords[NB_PULSE];
  295. cor_h_x2 (h, x, dn, 2, NB_TRACK_MR102, STEP_MR102);
  296. /* 2 = use GSMEFR scaling */
  297. set_sign12k2 (dn, cn, sign, pos_max, NB_TRACK_MR102, ipos, STEP_MR102);
  298. /* same setsign alg as GSM-EFR new constants though*/
  299. cor_h (h, sign, rr);
  300. search_10and8i40 (NB_PULSE, STEP_MR102, NB_TRACK_MR102,
  301. dn, rr, ipos, pos_max, codvec);
  302. build_code (codvec, sign, cod, h, y, linear_signs, linear_codewords);
  303. compress_code (linear_signs, linear_codewords, indx);
  304. return;
  305. }