ton_stab.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 : ton_stab.c
  11. *
  12. *****************************************************************************
  13. */
  14. /*
  15. *****************************************************************************
  16. * MODULE INCLUDE FILE AND VERSION ID
  17. *****************************************************************************
  18. */
  19. #include "ton_stab.h"
  20. const char ton_stab_id[] = "@(#)$Id $" ton_stab_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 "set_zero.h"
  34. #include "copy.h"
  35. /*
  36. *****************************************************************************
  37. * LOCAL VARIABLES AND TABLES
  38. *****************************************************************************
  39. */
  40. /*
  41. *****************************************************************************
  42. * PUBLIC PROGRAM CODE
  43. *****************************************************************************
  44. */
  45. /*************************************************************************
  46. *
  47. * Function: ton_stab_init
  48. * Purpose: Allocates state memory and initializes state memory
  49. *
  50. **************************************************************************
  51. */
  52. int ton_stab_init (tonStabState **state)
  53. {
  54. tonStabState* s;
  55. if (state == (tonStabState **) NULL){
  56. wfprintf(stderr, "ton_stab_init: invalid parameter\n");
  57. return -1;
  58. }
  59. *state = NULL;
  60. /* allocate memory */
  61. if ((s= (tonStabState *) wmalloc(sizeof(tonStabState))) == NULL){
  62. wfprintf(stderr, "ton_stab_init: can not malloc state structure\n");
  63. return -1;
  64. }
  65. ton_stab_reset(s);
  66. *state = s;
  67. return 0;
  68. }
  69. /*************************************************************************
  70. *
  71. * Function: ton_stab_reset
  72. * Purpose: Initializes state memory to zero
  73. *
  74. **************************************************************************
  75. */
  76. int ton_stab_reset (tonStabState *st)
  77. {
  78. if (st == (tonStabState *) NULL){
  79. wfprintf(stderr, "ton_stab_init: invalid parameter\n");
  80. return -1;
  81. }
  82. /* initialize tone stabilizer state */
  83. st->count = 0;
  84. Set_zero(st->gp, N_FRAME); /* Init Gp_Clipping */
  85. return 0;
  86. }
  87. /*************************************************************************
  88. *
  89. * Function: ton_stab_exit
  90. * Purpose: The memory used for state memory is freed
  91. *
  92. **************************************************************************
  93. */
  94. void ton_stab_exit (tonStabState **state)
  95. {
  96. if (state == NULL || *state == NULL)
  97. return;
  98. /* deallocate memory */
  99. wfree(*state);
  100. *state = NULL;
  101. return;
  102. }
  103. /***************************************************************************
  104. * *
  105. * Function: check_lsp() *
  106. * Purpose: Check the LSP's to detect resonances *
  107. * *
  108. ****************************************************************************
  109. */
  110. Word16 check_lsp(tonStabState *st, /* i/o : State struct */
  111. Word16 *lsp /* i : unquantized LSP's */
  112. )
  113. {
  114. Word16 i, dist, dist_min1, dist_min2, dist_th;
  115. /* Check for a resonance: */
  116. /* Find minimum distance between lsp[i] and lsp[i+1] */
  117. dist_min1 = MAX_16; move16 ();
  118. for (i = 3; i < M-2; i++)
  119. {
  120. dist = sub_ex(lsp[i], lsp[i+1]);
  121. test ();
  122. if (sub_ex(dist, dist_min1) < 0)
  123. {
  124. dist_min1 = dist; move16 ();
  125. }
  126. }
  127. dist_min2 = MAX_16; move16 ();
  128. for (i = 1; i < 3; i++)
  129. {
  130. dist = sub_ex(lsp[i], lsp[i+1]);
  131. test ();
  132. if (sub_ex(dist, dist_min2) < 0)
  133. {
  134. dist_min2 = dist; move16 ();
  135. }
  136. }
  137. if (test (), sub_ex(lsp[1], 32000) > 0)
  138. {
  139. dist_th = 600; move16 ();
  140. }
  141. else if (test (), sub_ex(lsp[1], 30500) > 0)
  142. {
  143. dist_th = 800; move16 ();
  144. }
  145. else
  146. {
  147. dist_th = 1100; move16 ();
  148. }
  149. test (); test ();
  150. if (sub_ex(dist_min1, 1500) < 0 ||
  151. sub_ex(dist_min2, dist_th) < 0)
  152. {
  153. st->count = add_ex(st->count, 1);
  154. }
  155. else
  156. {
  157. st->count = 0; move16 ();
  158. }
  159. /* Need 12 consecutive frames to set the flag */
  160. test ();
  161. if (sub_ex(st->count, 12) >= 0)
  162. {
  163. st->count = 12; move16 ();
  164. return 1;
  165. }
  166. else
  167. {
  168. return 0;
  169. }
  170. }
  171. /***************************************************************************
  172. *
  173. * Function: Check_Gp_Clipping()
  174. * Purpose: Verify that the sum of the last (N_FRAME+1) pitch
  175. * gains is under a certain threshold.
  176. *
  177. ***************************************************************************
  178. */
  179. Word16 check_gp_clipping(tonStabState *st, /* i/o : State struct */
  180. Word16 g_pitch /* i : pitch gain */
  181. )
  182. {
  183. Word16 i, sum;
  184. sum = shr_ex(g_pitch, 3); /* Division by 8 */
  185. for (i = 0; i < N_FRAME; i++)
  186. {
  187. sum = add_ex(sum, st->gp[i]);
  188. }
  189. test ();
  190. if (sub_ex(sum, GP_CLIP) > 0)
  191. {
  192. return 1;
  193. }
  194. else
  195. {
  196. return 0;
  197. }
  198. }
  199. /***************************************************************************
  200. *
  201. * Function: Update_Gp_Clipping()
  202. * Purpose: Update past pitch gain memory
  203. *
  204. ***************************************************************************
  205. */
  206. void update_gp_clipping(tonStabState *st, /* i/o : State struct */
  207. Word16 g_pitch /* i : pitch gain */
  208. )
  209. {
  210. Copy(&st->gp[1], &st->gp[0], N_FRAME-1);
  211. st->gp[N_FRAME-1] = shr_ex(g_pitch, 3);
  212. }