g_adapt.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 : g_adapt.c
  11. * Purpose : gain adaptation for MR795 gain quantization
  12. *
  13. ********************************************************************************
  14. */
  15. /*
  16. ********************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. ********************************************************************************
  19. */
  20. #include "g_adapt.h"
  21. const char g_adapt_id[] = "@(#)$Id $" g_adapt_h;
  22. /*
  23. ********************************************************************************
  24. * INCLUDE FILES
  25. ********************************************************************************
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include "typedef.h"
  30. #include "basic_op.h"
  31. #include "oper_32b.h"
  32. #include "count.h"
  33. #include "cnst.h"
  34. #include "gmed_n.h"
  35. /*
  36. ********************************************************************************
  37. * LOCAL VARIABLES AND TABLES
  38. ********************************************************************************
  39. */
  40. #define LTP_GAIN_THR1 2721 /* 2721 Q13 = 0.3322 ~= 1.0 / (10*log10(2)) */
  41. #define LTP_GAIN_THR2 5443 /* 5443 Q13 = 0.6644 ~= 2.0 / (10*log10(2)) */
  42. /*
  43. ********************************************************************************
  44. * PUBLIC PROGRAM CODE
  45. ********************************************************************************
  46. */
  47. /*************************************************************************
  48. *
  49. * Function: gain_adapt_init
  50. * Purpose: Allocates state memory and initializes state memory
  51. *
  52. **************************************************************************
  53. */
  54. int gain_adapt_init (GainAdaptState **st)
  55. {
  56. GainAdaptState* s;
  57. if (st == (GainAdaptState **) NULL){
  58. wfprintf(stderr, "gain_adapt_init: invalid parameter\n");
  59. return -1;
  60. }
  61. *st = NULL;
  62. /* allocate memory */
  63. if ((s= (GainAdaptState *) wmalloc(sizeof(GainAdaptState))) == NULL){
  64. wfprintf(stderr, "gain_adapt_init: can't malloc state structure\n");
  65. return -1;
  66. }
  67. gain_adapt_reset(s);
  68. *st = s;
  69. return 0;
  70. }
  71. /*************************************************************************
  72. *
  73. * Function: gain_adapt_reset
  74. * Purpose: Initializes state memory to zero
  75. *
  76. **************************************************************************
  77. */
  78. int gain_adapt_reset (GainAdaptState *st)
  79. {
  80. Word16 i;
  81. if (st == (GainAdaptState *) NULL){
  82. wfprintf(stderr, "gain_adapt_reset: invalid parameter\n");
  83. return -1;
  84. }
  85. st->onset = 0;
  86. st->prev_alpha = 0;
  87. st->prev_gc = 0;
  88. for (i = 0; i < LTPG_MEM_SIZE; i++)
  89. {
  90. st->ltpg_mem[i] = 0;
  91. }
  92. return 0;
  93. }
  94. /*************************************************************************
  95. *
  96. * Function: gain_adapt_exit
  97. * Purpose: The memory used for state memory is freed
  98. *
  99. **************************************************************************
  100. */
  101. void gain_adapt_exit (GainAdaptState **st)
  102. {
  103. if (st == NULL || *st == NULL)
  104. return;
  105. /* deallocate memory */
  106. wfree(*st);
  107. *st = NULL;
  108. return;
  109. }
  110. /*************************************************************************
  111. *
  112. * Function: gain_adapt()
  113. * Purpose: calculate pitch/codebook gain adaptation factor alpha
  114. * (and update the adaptor state)
  115. *
  116. **************************************************************************
  117. */
  118. void gain_adapt(
  119. GainAdaptState *st, /* i : state struct */
  120. Word16 ltpg, /* i : ltp coding gain (log2()), Q13 */
  121. Word16 gain_cod, /* i : code gain, Q1 */
  122. Word16 *alpha /* o : gain adaptation factor, Q15 */
  123. )
  124. {
  125. Word16 adapt; /* adaptdation status; 0, 1, or 2 */
  126. Word16 result; /* alpha factor, Q13 */
  127. Word16 filt; /* median-filtered LTP coding gain, Q13 */
  128. Word16 tmp, i;
  129. /* basic adaptation */
  130. test ();
  131. if (sub_ex (ltpg, LTP_GAIN_THR1) <= 0)
  132. {
  133. adapt = 0; move16 ();
  134. }
  135. else
  136. {
  137. test ();
  138. if (sub_ex (ltpg, LTP_GAIN_THR2) <= 0)
  139. {
  140. adapt = 1; move16 ();
  141. }
  142. else
  143. {
  144. adapt = 2; move16 ();
  145. }
  146. }
  147. /*
  148. * // onset indicator
  149. * if ((cbGain > onFact * cbGainMem[0]) && (cbGain > 100.0))
  150. * onset = 8;
  151. * else
  152. * if (onset)
  153. * onset--;
  154. */
  155. /* tmp = cbGain / onFact; onFact = 2.0; 200 Q1 = 100.0 */
  156. tmp = shr_r_ex (gain_cod, 1);
  157. test (); test ();
  158. if ((sub_ex (tmp, st->prev_gc) > 0) && sub_ex(gain_cod, 200) > 0)
  159. {
  160. st->onset = 8; move16 ();
  161. }
  162. else
  163. {
  164. test ();
  165. if (st->onset != 0)
  166. {
  167. st->onset = sub_ex (st->onset, 1); move16 ();
  168. }
  169. }
  170. /*
  171. * // if onset, increase adaptor state
  172. * if (onset && (gainAdapt < 2)) gainAdapt++;
  173. */
  174. test(); test ();
  175. if ((st->onset != 0) && (sub_ex (adapt, 2) < 0))
  176. {
  177. adapt = add_ex (adapt, 1);
  178. }
  179. st->ltpg_mem[0] = ltpg; move16 ();
  180. filt = gmed_n (st->ltpg_mem, 5); move16 (); /* function result */
  181. test ();
  182. if (adapt == 0)
  183. {
  184. test ();
  185. if (sub_ex (filt, 5443) > 0) /* 5443 Q13 = 0.66443... */
  186. {
  187. result = 0; move16 ();
  188. }
  189. else
  190. {
  191. test ();
  192. if (filt < 0)
  193. {
  194. result = 16384; move16 (); /* 16384 Q15 = 0.5 */
  195. }
  196. else
  197. { /* result = 0.5 - 0.75257499*filt */
  198. /* result (Q15) = 16384 - 24660 * (filt << 2) */
  199. filt = shl_ex (filt, 2); /* Q15 */
  200. result = sub_ex (16384, mult_ex (24660, filt));
  201. }
  202. }
  203. }
  204. else
  205. {
  206. result = 0; move16 ();
  207. }
  208. /*
  209. * if (prevAlpha == 0.0) result = 0.5 * (result + prevAlpha);
  210. */
  211. test ();
  212. if (st->prev_alpha == 0)
  213. {
  214. result = shr_ex (result, 1);
  215. }
  216. /* store the result */
  217. *alpha = result; move16 ();
  218. /* update adapter state memory */
  219. st->prev_alpha = result; move16 ();
  220. st->prev_gc = gain_cod; move16 ();
  221. for (i = LTPG_MEM_SIZE-1; i > 0; i--)
  222. {
  223. st->ltpg_mem[i] = st->ltpg_mem[i-1]; move16 ();
  224. }
  225. /* mem[0] is just present for convenience in calling the gmed_n[5]
  226. * function above. The memory depth is really LTPG_MEM_SIZE-1.
  227. */
  228. }