d8_31pf.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 : d8_31pf.c
  11. * Purpose : Builds the innovative codevector
  12. *
  13. ********************************************************************************
  14. */
  15. /*
  16. ********************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. ********************************************************************************
  19. */
  20. #include "d8_31pf.h"
  21. const char d8_31pf_id[] = "@(#)$Id $" d8_31pf_h;
  22. /*
  23. ********************************************************************************
  24. * INCLUDE FILES
  25. ********************************************************************************
  26. */
  27. #include "typedef.h"
  28. #include "basic_op.h"
  29. #include "count.h"
  30. #include "cnst.h"
  31. /*
  32. ********************************************************************************
  33. * LOCAL VARIABLES AND TABLES
  34. ********************************************************************************
  35. */
  36. #define NB_PULSE 8 /* number of pulses */
  37. /* define values/representation for output codevector and sign */
  38. #define POS_CODE 8191
  39. #define NEG_CODE 8191
  40. static void decompress10 (
  41. Word16 MSBs, /* i : MSB part of the index */
  42. Word16 LSBs, /* i : LSB part of the index */
  43. Word16 index1, /* i : index for first pos in pos_index[] */
  44. Word16 index2, /* i : index for second pos in pos_index[] */
  45. Word16 index3, /* i : index for third pos in pos_index[] */
  46. Word16 pos_indx[]) /* o : position of 3 pulses (decompressed) */
  47. {
  48. Word16 ia, ib, ic;
  49. /*
  50. pos_indx[index1] = ((MSBs-25*(MSBs/25))%5)*2 + (LSBs-4*(LSBs/4))%2;
  51. pos_indx[index2] = ((MSBs-25*(MSBs/25))/5)*2 + (LSBs-4*(LSBs/4))/2;
  52. pos_indx[index3] = (MSBs/25)*2 + LSBs/4;
  53. */
  54. test ();
  55. if (sub_ex(MSBs, 124) > 0)
  56. {
  57. MSBs = 124; move16 ();
  58. }
  59. ia = mult_ex(MSBs, 1311);
  60. ia = sub_ex(MSBs, extract_l_ex(L_shr_ex(L_mult_ex(ia, 25), 1)));
  61. ib = shl_ex(sub_ex(ia, extract_l_ex(L_shr_ex(L_mult_ex(mult_ex(ia, 6554), 5), 1))), 1);
  62. ic = shl_ex(shr_ex(LSBs, 2), 2);
  63. ic = sub_ex(LSBs, ic);
  64. pos_indx[index1] = add_ex(ib, (ic & 1)); logic16 ();
  65. ib = shl_ex(mult_ex(ia, 6554), 1);
  66. pos_indx[index2] = add_ex(ib, shr_ex(ic, 1));
  67. pos_indx[index3] = add_ex(shl_ex(mult_ex(MSBs, 1311), 1), shr_ex(LSBs, 2));
  68. return;
  69. }
  70. /*************************************************************************
  71. *
  72. * FUNCTION: decompress_code()
  73. *
  74. * PURPOSE: decompression of the linear codewords to 4+three indeces
  75. * one bit from each pulse is made robust to errors by
  76. * minimizing the phase shift of a bit error.
  77. * 4 signs (one for each track)
  78. * i0,i4,i1 => one index (7+3) bits, 3 LSBs more robust
  79. * i2,i6,i5 => one index (7+3) bits, 3 LSBs more robust
  80. * i3,i7 => one index (5+2) bits, 2-3 LSbs more robust
  81. *
  82. *************************************************************************/
  83. static void decompress_code (
  84. Word16 indx[], /* i : position and sign of 8 pulses (compressed) */
  85. Word16 sign_indx[], /* o : signs of 4 pulses (signs only) */
  86. Word16 pos_indx[] /* o : position index of 8 pulses (position only) */
  87. )
  88. {
  89. Word16 i, ia, ib, MSBs, LSBs, MSBs0_24;
  90. for (i = 0; i < NB_TRACK_MR102; i++)
  91. {
  92. sign_indx[i] = indx[i]; move16 ();
  93. }
  94. /*
  95. First index: 10x10x10 -> 2x5x2x5x2x5-> 125x2x2x2 -> 7+1x3 bits
  96. MSBs = indx[NB_TRACK]/8;
  97. LSBs = indx[NB_TRACK]%8;
  98. */
  99. MSBs = shr_ex(indx[NB_TRACK_MR102], 3);
  100. LSBs = indx[NB_TRACK_MR102] & 7; logic16 ();
  101. decompress10 (MSBs, LSBs, 0, 4, 1, pos_indx);
  102. /*
  103. Second index: 10x10x10 -> 2x5x2x5x2x5-> 125x2x2x2 -> 7+1x3 bits
  104. MSBs = indx[NB_TRACK+1]/8;
  105. LSBs = indx[NB_TRACK+1]%8;
  106. */
  107. MSBs = shr_ex(indx[NB_TRACK_MR102+1], 3);
  108. LSBs = indx[NB_TRACK_MR102+1] & 7; logic16 ();
  109. decompress10 (MSBs, LSBs, 2, 6, 5, pos_indx);
  110. /*
  111. Third index: 10x10 -> 2x5x2x5-> 25x2x2 -> 5+1x2 bits
  112. MSBs = indx[NB_TRACK+2]/4;
  113. LSBs = indx[NB_TRACK+2]%4;
  114. MSBs0_24 = (MSBs*25+12)/32;
  115. if ((MSBs0_24/5)%2==1)
  116. pos_indx[3] = (4-(MSBs0_24%5))*2 + LSBs%2;
  117. else
  118. pos_indx[3] = (MSBs0_24%5)*2 + LSBs%2;
  119. pos_indx[7] = (MSBs0_24/5)*2 + LSBs/2;
  120. */
  121. MSBs = shr_ex(indx[NB_TRACK_MR102+2], 2);
  122. LSBs = indx[NB_TRACK_MR102+2] & 3; logic16 ();
  123. MSBs0_24 = shr_ex(add_ex(extract_l_ex(L_shr_ex(L_mult_ex(MSBs, 25), 1)), 12), 5);
  124. ia = mult_ex(MSBs0_24, 6554) & 1;
  125. ib = sub_ex(MSBs0_24, extract_l_ex(L_shr_ex(L_mult_ex(mult_ex(MSBs0_24, 6554), 5), 1)));
  126. test ();
  127. if (sub_ex(ia, 1) == 0)
  128. {
  129. ib = sub_ex(4, ib);
  130. }
  131. pos_indx[3] = add_ex(shl_ex(ib, 1), (LSBs & 1)); logic16 ();
  132. ia = shl_ex(mult_ex(MSBs0_24, 6554), 1);
  133. pos_indx[7] = add_ex(ia, shr_ex(LSBs, 1));
  134. }
  135. /*
  136. ********************************************************************************
  137. * PUBLIC PROGRAM CODE
  138. ********************************************************************************
  139. */
  140. /*************************************************************************
  141. *
  142. * FUNCTION: dec_8i40_31bits()
  143. *
  144. * PURPOSE: Builds the innovative codevector from the received
  145. * index of algebraic codebook.
  146. *
  147. * See c8_31pf.c for more details about the algebraic codebook structure.
  148. *
  149. *************************************************************************/
  150. void dec_8i40_31bits (
  151. Word16 index[], /* i : index of 8 pulses (sign+position) */
  152. Word16 cod[] /* o : algebraic (fixed) codebook excitation */
  153. )
  154. {
  155. Word16 i, j, pos1, pos2, sign;
  156. Word16 linear_signs[NB_TRACK_MR102];
  157. Word16 linear_codewords[NB_PULSE];
  158. for (i = 0; i < L_CODE; i++)
  159. {
  160. cod[i] = 0; move16 ();
  161. }
  162. decompress_code (index, linear_signs, linear_codewords);
  163. /* decode the positions and signs of pulses and build the codeword */
  164. for (j = 0; j < NB_TRACK_MR102; j++)
  165. {
  166. /* compute index i */
  167. i = linear_codewords[j];
  168. i = extract_l_ex (L_shr_ex (L_mult_ex (i, 4), 1));
  169. pos1 = add_ex (i, j); /* position of pulse "j" */
  170. test ();
  171. if (linear_signs[j] == 0)
  172. {
  173. sign = POS_CODE; move16 (); /* +1.0 */
  174. }
  175. else
  176. {
  177. sign = -NEG_CODE; move16 (); /* -1.0 */
  178. }
  179. cod[pos1] = sign; move16 ();
  180. /* compute index i */
  181. i = linear_codewords[add_ex (j, 4)];
  182. i = extract_l_ex (L_shr_ex (L_mult_ex (i, 4), 1));
  183. pos2 = add_ex (i, j); /* position of pulse "j+4" */
  184. test ();
  185. if (sub_ex (pos2, pos1) < 0)
  186. {
  187. sign = negate_ex (sign);
  188. }
  189. cod[pos2] = add_ex (cod[pos2], sign); move16 ();
  190. }
  191. return;
  192. }