cl_ltp.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 : cl_ltp.c
  11. *
  12. *****************************************************************************
  13. */
  14. /*
  15. *****************************************************************************
  16. * MODULE INCLUDE FILE AND VERSION ID
  17. *****************************************************************************
  18. */
  19. #include "cl_ltp.h"
  20. const char cl_ltp_id[] = "@(#)$Id $" cl_ltp_h;
  21. /*
  22. *****************************************************************************
  23. * INCLUDE FILES
  24. *****************************************************************************
  25. */
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include "typedef.h"
  29. #include "basic_op.h"
  30. #include "count.h"
  31. #include "oper_32b.h"
  32. #include "cnst.h"
  33. #include "convolve.h"
  34. #include "g_pitch.h"
  35. #include "pred_lt.h"
  36. #include "pitch_fr.h"
  37. #include "enc_lag3.h"
  38. #include "enc_lag6.h"
  39. #include "q_gain_p.h"
  40. #include "ton_stab.h"
  41. /*
  42. *****************************************************************************
  43. * LOCAL VARIABLES AND TABLES
  44. *****************************************************************************
  45. */
  46. /*
  47. *****************************************************************************
  48. * PUBLIC PROGRAM CODE
  49. *****************************************************************************
  50. */
  51. /*************************************************************************
  52. *
  53. * Function: cl_ltp_init
  54. * Purpose: Allocates state memory and initializes state memory
  55. *
  56. **************************************************************************
  57. */
  58. int cl_ltp_init (clLtpState **state)
  59. {
  60. clLtpState* s;
  61. if (state == (clLtpState **) NULL){
  62. wfprintf(stderr, "cl_ltp_init: invalid parameter\n");
  63. return -1;
  64. }
  65. *state = NULL;
  66. /* allocate memory */
  67. if ((s= (clLtpState *) wmalloc(sizeof(clLtpState))) == NULL){
  68. wfprintf(stderr, "cl_ltp_init: can not malloc state structure\n");
  69. return -1;
  70. }
  71. /* init the sub_ex state */
  72. if (Pitch_fr_init(&s->pitchSt)) {
  73. cl_ltp_exit(&s);
  74. return -1;
  75. }
  76. cl_ltp_reset(s);
  77. *state = s;
  78. return 0;
  79. }
  80. /*************************************************************************
  81. *
  82. * Function: cl_ltp_reset
  83. * Purpose: Initializes state memory to zero
  84. *
  85. **************************************************************************
  86. */
  87. int cl_ltp_reset (clLtpState *state)
  88. {
  89. if (state == (clLtpState *) NULL){
  90. wfprintf(stderr, "cl_ltp_reset: invalid parameter\n");
  91. return -1;
  92. }
  93. /* Reset pitch search states */
  94. Pitch_fr_reset (state->pitchSt);
  95. return 0;
  96. }
  97. /*************************************************************************
  98. *
  99. * Function: cl_ltp_exit
  100. * Purpose: The memory used for state memory is freed
  101. *
  102. **************************************************************************
  103. */
  104. void cl_ltp_exit (clLtpState **state)
  105. {
  106. if (state == NULL || *state == NULL)
  107. return;
  108. /* dealloc members */
  109. Pitch_fr_exit(&(*state)->pitchSt);
  110. /* deallocate memory */
  111. wfree(*state);
  112. *state = NULL;
  113. return;
  114. }
  115. /*************************************************************************
  116. *
  117. * Function: cl_ltp
  118. * Purpose: closed-loop fractional pitch search
  119. *
  120. **************************************************************************
  121. */
  122. int cl_ltp (
  123. clLtpState *clSt, /* i/o : State struct */
  124. tonStabState *tonSt, /* i/o : State struct */
  125. enum Mode mode, /* i : coder mode */
  126. Word16 frameOffset, /* i : Offset to subframe */
  127. Word16 T_op[], /* i : Open loop pitch lags */
  128. Word16 *h1, /* i : Impulse response vector Q12 */
  129. Word16 *exc, /* i/o : Excitation vector Q0 */
  130. Word16 res2[], /* i/o : Long term prediction residual Q0 */
  131. Word16 xn[], /* i : Target vector for pitch search Q0 */
  132. Word16 lsp_flag, /* i : LSP resonance flag */
  133. Word16 xn2[], /* o : Target vector for codebook search Q0 */
  134. Word16 y1[], /* o : Filtered adaptive excitation Q0 */
  135. Word16 *T0, /* o : Pitch delay (integer part) */
  136. Word16 *T0_frac, /* o : Pitch delay (fractional part) */
  137. Word16 *gain_pit, /* o : Pitch gain Q14 */
  138. Word16 g_coeff[], /* o : Correlations between xn, y1, & y2 */
  139. Word16 **anap, /* o : Analysis parameters */
  140. Word16 *gp_limit /* o : pitch gain limit */
  141. )
  142. {
  143. Word16 i;
  144. Word16 index;
  145. Word32 L_temp; /* temporarily variable */
  146. Word16 resu3; /* flag for upsample resolution */
  147. Word16 gpc_flag;
  148. /*----------------------------------------------------------------------*
  149. * Closed-loop fractional pitch search *
  150. *----------------------------------------------------------------------*/
  151. *T0 = Pitch_fr(clSt->pitchSt,
  152. mode, T_op, exc, xn, h1,
  153. L_SUBFR, frameOffset,
  154. T0_frac, &resu3, &index); move16 ();
  155. *(*anap)++ = index; move16 ();
  156. /*-----------------------------------------------------------------*
  157. * - find unity gain pitch excitation (adapitve codebook entry) *
  158. * with fractional interpolation. *
  159. * - find filtered pitch exc. y1[]=exc[] convolve with h1[]) *
  160. * - compute pitch gain and limit between 0 and 1.2 *
  161. * - update target vector for codebook search *
  162. * - find LTP residual. *
  163. *-----------------------------------------------------------------*/
  164. Pred_lt_3or6(exc, *T0, *T0_frac, L_SUBFR, resu3);
  165. Convolve(exc, h1, y1, L_SUBFR);
  166. /* gain_pit is Q14 for all modes */
  167. *gain_pit = G_pitch(mode, xn, y1, g_coeff, L_SUBFR); move16 ();
  168. /* check if the pitch gain should be limit due to resonance in LPC filter */
  169. gpc_flag = 0; move16 ();
  170. *gp_limit = MAX_16; move16 ();
  171. test (); test ();
  172. if ((lsp_flag != 0) &&
  173. (sub_ex(*gain_pit, GP_CLIP) > 0))
  174. {
  175. gpc_flag = check_gp_clipping(tonSt, *gain_pit); move16 ();
  176. }
  177. /* special for the MR475, MR515 mode; limit the gain to 0.85 to */
  178. /* cope with bit errors in the decoder in a better way. */
  179. test (); test ();
  180. if ((sub_ex (mode, MR475) == 0) || (sub_ex (mode, MR515) == 0)) {
  181. test ();
  182. if ( sub_ex (*gain_pit, 13926) > 0) {
  183. *gain_pit = 13926; /* 0.85 in Q14 */ move16 ();
  184. }
  185. test ();
  186. if (gpc_flag != 0) {
  187. *gp_limit = GP_CLIP; move16 ();
  188. }
  189. }
  190. else
  191. {
  192. test ();
  193. if (gpc_flag != 0)
  194. {
  195. *gp_limit = GP_CLIP; move16 ();
  196. *gain_pit = GP_CLIP; move16 ();
  197. }
  198. /* For MR122, gain_pit is quantized here and not in gainQuant */
  199. if (test (), sub_ex(mode, MR122)==0)
  200. {
  201. *(*anap)++ = q_gain_pitch(MR122, *gp_limit, gain_pit,
  202. NULL, NULL);
  203. move16 ();
  204. }
  205. }
  206. /* update target vector und evaluate LTP residual */
  207. for (i = 0; i < L_SUBFR; i++) {
  208. L_temp = L_mult_ex(y1[i], *gain_pit);
  209. L_temp = L_shl_ex(L_temp, 1);
  210. xn2[i] = sub_ex(xn[i], extract_h_ex(L_temp)); move16 ();
  211. L_temp = L_mult_ex(exc[i], *gain_pit);
  212. L_temp = L_shl_ex(L_temp, 1);
  213. res2[i] = sub_ex(res2[i], extract_h_ex(L_temp)); move16 ();
  214. }
  215. return 0;
  216. }