c_g_aver.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*************************************************************************
  2. *
  3. * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
  4. * R99 Version 3.3.0
  5. * REL-4 Version 4.1.0
  6. *
  7. ********************************************************************************
  8. *
  9. * File : c_g_aver.c
  10. * Purpose :
  11. *
  12. ********************************************************************************
  13. */
  14. /*
  15. ********************************************************************************
  16. * MODULE INCLUDE FILE AND VERSION ID
  17. ********************************************************************************
  18. */
  19. #include "c_g_aver.h"
  20. const char c_g_aver_id[] = "@(#)$Id $" c_g_aver_h;
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include "typedef.h"
  24. #include "mode.h"
  25. #include "basic_op.h"
  26. #include "count.h"
  27. #include "cnst.h"
  28. #include "set_zero.h"
  29. /*
  30. ********************************************************************************
  31. * LOCAL VARIABLES AND TABLES
  32. ********************************************************************************
  33. */
  34. /*-----------------------------------------------------------------*
  35. * Decoder constant parameters (defined in "cnst.h") *
  36. *-----------------------------------------------------------------*
  37. * L_FRAME : Frame size. *
  38. * L_SUBFR : Sub-frame size. *
  39. *-----------------------------------------------------------------*/
  40. /*
  41. ********************************************************************************
  42. * PUBLIC PROGRAM CODE
  43. ********************************************************************************
  44. */
  45. /*
  46. **************************************************************************
  47. *
  48. * Function : Cb_gain_average_init
  49. * Purpose : Allocates and initializes state memory
  50. *
  51. **************************************************************************
  52. */
  53. Word16 Cb_gain_average_init (Cb_gain_averageState **state)
  54. {
  55. Cb_gain_averageState* s;
  56. if (state == (Cb_gain_averageState **) NULL){
  57. wfprintf(stderr, "Cb_gain_average_init: invalid parameter\n");
  58. return -1;
  59. }
  60. *state = NULL;
  61. /* allocate memory */
  62. if ((s= (Cb_gain_averageState *) wmalloc(sizeof(Cb_gain_averageState))) == NULL){
  63. wfprintf(stderr, "Cb_gain_average_init: can not malloc state structure\n");
  64. return -1;
  65. }
  66. Cb_gain_average_reset(s);
  67. *state = s;
  68. return 0;
  69. }
  70. /*
  71. **************************************************************************
  72. *
  73. * Function : Cb_gain_average_reset
  74. * Purpose : Resets state memory
  75. *
  76. **************************************************************************
  77. */
  78. Word16 Cb_gain_average_reset (Cb_gain_averageState *state)
  79. {
  80. if (state == (Cb_gain_averageState *) NULL){
  81. wfprintf(stderr, "Cb_gain_average_reset: invalid parameter\n");
  82. return -1;
  83. }
  84. /* Static vectors to zero */
  85. Set_zero (state->cbGainHistory, L_CBGAINHIST);
  86. /* Initialize hangover handling */
  87. state->hangVar = 0;
  88. state->hangCount= 0;
  89. return 0;
  90. }
  91. /*
  92. **************************************************************************
  93. *
  94. * Function : Cb_gain_average_exit
  95. * Purpose : The memory used for state memory is freed
  96. *
  97. **************************************************************************
  98. */
  99. void Cb_gain_average_exit (Cb_gain_averageState **state)
  100. {
  101. if (state == NULL || *state == NULL)
  102. return;
  103. /* deallocate memory */
  104. wfree(*state);
  105. *state = NULL;
  106. return;
  107. }
  108. /*
  109. **************************************************************************
  110. *
  111. * Function : Cb_gain_average
  112. * Purpose :
  113. * Returns : The mix cb gains for MR475, MR515, MR59, MR67, MR102; gain_code other modes
  114. *
  115. **************************************************************************
  116. */
  117. Word16 Cb_gain_average (
  118. Cb_gain_averageState *st, /* i/o : State variables for CB gain avergeing */
  119. enum Mode mode, /* i : AMR mode */
  120. Word16 gain_code, /* i : CB gain Q1 */
  121. Word16 lsp[], /* i : The LSP for the current frame Q15 */
  122. Word16 lspAver[], /* i : The average of LSP for 8 frames Q15 */
  123. Word16 bfi, /* i : bad frame indication flag */
  124. Word16 prev_bf, /* i : previous bad frame indication flag */
  125. Word16 pdfi, /* i : potential degraded bad frame ind flag */
  126. Word16 prev_pdf, /* i : prev pot. degraded bad frame ind flag */
  127. Word16 inBackgroundNoise, /* i : background noise decision */
  128. Word16 voicedHangover /* i : # of frames after last voiced frame */
  129. )
  130. {
  131. /*---------------------------------------------------------*
  132. * Compute mixed cb gain, used to make cb gain more *
  133. * smooth in background noise for modes 5.15, 5.9 and 6.7 *
  134. * states that needs to be updated by all *
  135. *---------------------------------------------------------*/
  136. Word16 i;
  137. Word16 cbGainMix, diff, tmp_diff, bgMix, cbGainMean;
  138. Word32 L_sum;
  139. Word16 tmp[M], tmp1, tmp2, shift1, shift2, shift;
  140. /* set correct cbGainMix for MR74, MR795, MR122 */
  141. cbGainMix = gain_code; move16 ();
  142. /*-------------------------------------------------------*
  143. * Store list of CB gain needed in the CB gain *
  144. * averaging *
  145. *-------------------------------------------------------*/
  146. for (i = 0; i < (L_CBGAINHIST-1); i++)
  147. {
  148. st->cbGainHistory[i] = st->cbGainHistory[i+1]; move16 ();
  149. }
  150. st->cbGainHistory[L_CBGAINHIST-1] = gain_code; move16 ();
  151. /* compute lsp difference */
  152. for (i = 0; i < M; i++) {
  153. tmp1 = abs_s_ex(sub_ex(lspAver[i], lsp[i])); /* Q15 */
  154. shift1 = sub_ex(norm_s_ex(tmp1), 1); /* Qn */
  155. tmp1 = shl_ex(tmp1, shift1); /* Q15+Qn */
  156. shift2 = norm_s_ex(lspAver[i]); /* Qm */
  157. tmp2 = shl_ex(lspAver[i], shift2); /* Q15+Qm */
  158. tmp[i] = div_s(tmp1, tmp2); /* Q15+(Q15+Qn)-(Q15+Qm) */
  159. move16 ();
  160. shift = sub_ex(add_ex(2, shift1), shift2);
  161. test ();
  162. if (shift >= 0)
  163. {
  164. tmp[i] = shr_ex(tmp[i], shift); move16 (); /* Q15+Qn-Qm-Qx=Q13 */
  165. }
  166. else
  167. {
  168. tmp[i] = shl_ex(tmp[i], negate_ex(shift)); move16 (); /* Q15+Qn-Qm-Qx=Q13 */
  169. }
  170. }
  171. diff = tmp[0]; move16 ();
  172. for (i = 1; i < M; i++) {
  173. diff = add_ex(diff, tmp[i]); /* Q13 */
  174. }
  175. /* Compute hangover */
  176. test ();
  177. if (sub_ex(diff, 5325) > 0) /* 0.65 in Q11 */
  178. {
  179. st->hangVar = add_ex(st->hangVar, 1);
  180. }
  181. else
  182. {
  183. st->hangVar = 0; move16 ();
  184. }
  185. test ();
  186. if (sub_ex(st->hangVar, 10) > 0)
  187. {
  188. st->hangCount = 0; /* Speech period, reset hangover variable */ move16 ();
  189. }
  190. /* Compute mix constant (bgMix) */
  191. bgMix = 8192; /* 1 in Q13 */ move16 ();
  192. test ();
  193. if ((sub_ex(mode, MR67) <= 0) || (sub_ex(mode, MR102) == 0))
  194. /* MR475, MR515, MR59, MR67, MR102 */
  195. {
  196. /* if errors and presumed noise make smoothing probability stronger */
  197. test (); test (); test (); test (); test (); test(); test (); test (); test ();
  198. if (((((pdfi != 0) && (prev_pdf != 0)) || (bfi != 0) || (prev_bf != 0)) &&
  199. (sub_ex(voicedHangover, 1) > 0) && (inBackgroundNoise != 0) &&
  200. ((sub_ex(mode, MR475) == 0) ||
  201. (sub_ex(mode, MR515) == 0) ||
  202. (sub_ex(mode, MR59) == 0)) ))
  203. {
  204. /* bgMix = min(0.25, max(0.0, diff-0.55)) / 0.25; */
  205. tmp_diff = sub_ex(diff, 4506); /* 0.55 in Q13 */
  206. /* max(0.0, diff-0.55) */
  207. test ();
  208. if (tmp_diff > 0)
  209. {
  210. tmp1 = tmp_diff; move16 ();
  211. }
  212. else
  213. {
  214. tmp1 = 0; move16 ();
  215. }
  216. /* min(0.25, tmp1) */
  217. test ();
  218. if (sub_ex(2048, tmp1) < 0)
  219. {
  220. bgMix = 8192; move16 ();
  221. }
  222. else
  223. {
  224. bgMix = shl_ex(tmp1, 2);
  225. }
  226. }
  227. else
  228. {
  229. /* bgMix = min(0.25, max(0.0, diff-0.40)) / 0.25; */
  230. tmp_diff = sub_ex(diff, 3277); /* 0.4 in Q13 */
  231. /* max(0.0, diff-0.40) */
  232. test ();
  233. if (tmp_diff > 0)
  234. {
  235. tmp1 = tmp_diff; move16 ();
  236. }
  237. else
  238. {
  239. tmp1 = 0; move16 ();
  240. }
  241. /* min(0.25, tmp1) */
  242. test ();
  243. if (sub_ex(2048, tmp1) < 0)
  244. {
  245. bgMix = 8192; move16 ();
  246. }
  247. else
  248. {
  249. bgMix = shl_ex(tmp1, 2);
  250. }
  251. }
  252. test (); test ();
  253. if ((sub_ex(st->hangCount, 40) < 0) || (sub_ex(diff, 5325) > 0)) /* 0.65 in Q13 */
  254. {
  255. bgMix = 8192; /* disable mix if too short time since */ move16 ();
  256. }
  257. /* Smoothen the cb gain trajectory */
  258. /* smoothing depends on mix constant bgMix */
  259. L_sum = L_mult_ex(6554, st->cbGainHistory[2]); /* 0.2 in Q15; L_sum in Q17 */
  260. for (i = 3; i < L_CBGAINHIST; i++)
  261. {
  262. L_sum = L_mac_ex(L_sum, 6554, st->cbGainHistory[i]);
  263. }
  264. cbGainMean = round_ex(L_sum); /* Q1 */
  265. /* more smoothing in error and bg noise (NB no DFI used here) */
  266. test (); test (); test (); test (); test(); test();
  267. if (((bfi != 0) || (prev_bf != 0)) && (inBackgroundNoise != 0) &&
  268. ((sub_ex(mode, MR475) == 0) ||
  269. (sub_ex(mode, MR515) == 0) ||
  270. (sub_ex(mode, MR59) == 0)) )
  271. {
  272. L_sum = L_mult_ex(4681, st->cbGainHistory[0]); /* 0.143 in Q15; L_sum in Q17 */
  273. for (i = 1; i < L_CBGAINHIST; i++)
  274. {
  275. L_sum = L_mac_ex(L_sum, 4681, st->cbGainHistory[i]);
  276. }
  277. cbGainMean = round_ex(L_sum); /* Q1 */
  278. }
  279. /* cbGainMix = bgMix*cbGainMix + (1-bgMix)*cbGainMean; */
  280. L_sum = L_mult_ex(bgMix, cbGainMix); /* L_sum in Q15 */
  281. L_sum = L_mac_ex(L_sum, 8192, cbGainMean);
  282. L_sum = L_msu_ex(L_sum, bgMix, cbGainMean);
  283. cbGainMix = round_ex(L_shl_ex(L_sum, 2)); /* Q1 */
  284. }
  285. st->hangCount = add_ex(st->hangCount, 1);
  286. return cbGainMix;
  287. }