ec_gains.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 : ec_gains.c
  11. * Purpose: : Error concealment for pitch and codebook gains
  12. *
  13. ********************************************************************************
  14. */
  15. /*
  16. ********************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. ********************************************************************************
  19. */
  20. #include "ec_gains.h"
  21. const char ec_gains_id[] = "@(#)$Id $" ec_gains_h;
  22. /*
  23. ********************************************************************************
  24. * INCLUDE FILES
  25. ********************************************************************************
  26. */
  27. #include <stdlib.h>
  28. #include <stdio.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. #include "gc_pred.h"
  36. /*
  37. ********************************************************************************
  38. * LOCAL VARIABLES AND TABLES
  39. ********************************************************************************
  40. */
  41. #include "gains.tab"
  42. /*
  43. ********************************************************************************
  44. * PUBLIC PROGRAM CODE
  45. ********************************************************************************
  46. */
  47. /*
  48. **************************************************************************
  49. *
  50. * Function : ec_gain_code_init
  51. * Purpose : Allocates memory and initializes state variables
  52. *
  53. **************************************************************************
  54. */
  55. int ec_gain_code_init (ec_gain_codeState **state)
  56. {
  57. ec_gain_codeState* s;
  58. if (state == (ec_gain_codeState **) NULL){
  59. wfprintf(stderr, "ec_gain_code_init: invalid parameter\n");
  60. return -1;
  61. }
  62. *state = NULL;
  63. /* allocate memory */
  64. if ((s= (ec_gain_codeState *) wmalloc(sizeof(ec_gain_codeState))) == NULL){
  65. wfprintf(stderr, "ec_gain_code_init: can not malloc state structure\n");
  66. return -1;
  67. }
  68. ec_gain_code_reset(s);
  69. *state = s;
  70. return 0;
  71. }
  72. /*
  73. **************************************************************************
  74. *
  75. * Function : ec_gain_code_reset
  76. * Purpose : Resets state memory
  77. *
  78. **************************************************************************
  79. */
  80. int ec_gain_code_reset (ec_gain_codeState *state)
  81. {
  82. Word16 i;
  83. if (state == (ec_gain_codeState *) NULL){
  84. wfprintf(stderr, "ec_gain_code_reset: invalid parameter\n");
  85. return -1;
  86. }
  87. for ( i = 0; i < 5; i++)
  88. state->gbuf[i] = 1;
  89. state->past_gain_code = 0;
  90. state->prev_gc = 1;
  91. return 0;
  92. }
  93. /*
  94. **************************************************************************
  95. *
  96. * Function : ec_gain_code_exit
  97. * Purpose : The memory used for state memory is freed
  98. *
  99. **************************************************************************
  100. */
  101. void ec_gain_code_exit (ec_gain_codeState **state)
  102. {
  103. if (state == NULL || *state == NULL)
  104. return;
  105. /* deallocate memory */
  106. wfree(*state);
  107. *state = NULL;
  108. return;
  109. }
  110. /*
  111. **************************************************************************
  112. *
  113. * Function : ec_gain_code
  114. * Purpose : conceal the codebook gain
  115. * Call this function only in BFI (instead of normal gain
  116. * decoding function)
  117. *
  118. **************************************************************************
  119. */
  120. void ec_gain_code (
  121. ec_gain_codeState *st, /* i/o : State struct */
  122. gc_predState *pred_state, /* i/o : MA predictor state */
  123. Word16 state, /* i : state of the state machine */
  124. Word16 *gain_code /* o : decoded innovation gain */
  125. )
  126. {
  127. static const Word16 cdown[7] =
  128. {
  129. 32767, 32112, 32112, 32112,
  130. 32112, 32112, 22937
  131. };
  132. Word16 tmp;
  133. Word16 qua_ener_MR122;
  134. Word16 qua_ener;
  135. /* calculate median of last five gain values */
  136. tmp = gmed_n (st->gbuf,5); move16 ();
  137. /* new gain = minimum(median, past_gain) * cdown[state] */
  138. test ();
  139. if (sub_ex (tmp, st->past_gain_code) > 0)
  140. {
  141. tmp = st->past_gain_code; move16 ();
  142. }
  143. tmp = mult_ex (tmp, cdown[state]);
  144. *gain_code = tmp; move16 ();
  145. /* update table of past quantized energies with average of
  146. * current values
  147. */
  148. gc_pred_average_limited(pred_state, &qua_ener_MR122, &qua_ener);
  149. gc_pred_update(pred_state, qua_ener_MR122, qua_ener);
  150. }
  151. /*
  152. **************************************************************************
  153. *
  154. * Function : ec_gain_code_update
  155. * Purpose : update the codebook gain concealment state;
  156. * limit gain_code if the previous frame was bad
  157. * Call this function always after decoding (or concealing)
  158. * the gain
  159. *
  160. **************************************************************************
  161. */
  162. void ec_gain_code_update (
  163. ec_gain_codeState *st, /* i/o : State struct */
  164. Word16 bfi, /* i : flag: frame is bad */
  165. Word16 prev_bf, /* i : flag: previous frame was bad */
  166. Word16 *gain_code /* i/o : decoded innovation gain */
  167. )
  168. {
  169. Word16 i;
  170. /* limit gain_code by previous good gain if previous frame was bad */
  171. test ();
  172. if (bfi == 0)
  173. {
  174. test ();
  175. if (prev_bf != 0)
  176. {
  177. test ();
  178. if (sub_ex (*gain_code, st->prev_gc) > 0)
  179. {
  180. *gain_code = st->prev_gc; move16 ();
  181. }
  182. }
  183. st->prev_gc = *gain_code; move16 ();
  184. }
  185. /* update EC states: previous gain, gain buffer */
  186. st->past_gain_code = *gain_code; move16 ();
  187. for (i = 1; i < 5; i++)
  188. {
  189. st->gbuf[i - 1] = st->gbuf[i]; move16 ();
  190. }
  191. st->gbuf[4] = *gain_code; move16 ();
  192. return;
  193. }
  194. /*
  195. **************************************************************************
  196. *
  197. * Function : ec_gain_pitch_init
  198. * Purpose : Allocates memory and initializes state memory.
  199. *
  200. **************************************************************************
  201. */
  202. int ec_gain_pitch_init (ec_gain_pitchState **state)
  203. {
  204. ec_gain_pitchState* s;
  205. if (state == (ec_gain_pitchState **) NULL){
  206. wfprintf(stderr, "ec_gain_pitch_init: invalid parameter\n");
  207. return -1;
  208. }
  209. *state = NULL;
  210. /* allocate memory */
  211. if ((s= (ec_gain_pitchState *) wmalloc(sizeof(ec_gain_pitchState))) == NULL){
  212. wfprintf(stderr, "ec_gain_pitch_init: can not malloc state structure\n");
  213. return -1;
  214. }
  215. ec_gain_pitch_reset(s);
  216. *state = s;
  217. return 0;
  218. }
  219. /*
  220. **************************************************************************
  221. *
  222. * Function: ec_gain_pitch_reset
  223. * Purpose: Resets state memory
  224. *
  225. **************************************************************************
  226. */
  227. int ec_gain_pitch_reset (ec_gain_pitchState *state)
  228. {
  229. Word16 i;
  230. if (state == (ec_gain_pitchState *) NULL){
  231. wfprintf(stderr, "ec_gain_pitch_reset: invalid parameter\n");
  232. return -1;
  233. }
  234. for(i = 0; i < 5; i++)
  235. state->pbuf[i] = 1640;
  236. state->past_gain_pit = 0;
  237. state->prev_gp = 16384;
  238. return 0;
  239. }
  240. /*************************************************************************
  241. *
  242. * Function : ec_gain_pitch_exit
  243. * Purpose : The memory used for state memory is freed
  244. *
  245. **************************************************************************
  246. */
  247. void ec_gain_pitch_exit (ec_gain_pitchState **state)
  248. {
  249. if (state == NULL || *state == NULL)
  250. return;
  251. /* deallocate memory */
  252. wfree(*state);
  253. *state = NULL;
  254. return;
  255. }
  256. /*
  257. **************************************************************************
  258. *
  259. * Function : ec_gain_pitch
  260. * Purpose : conceal the pitch gain
  261. * Call this function only in BFI (instead of normal gain
  262. * decoding function)
  263. *
  264. **************************************************************************
  265. */
  266. void ec_gain_pitch (
  267. ec_gain_pitchState *st, /* i/o : state variables */
  268. Word16 state, /* i : state of the state machine */
  269. Word16 *gain_pitch /* o : pitch gain (Q14) */
  270. )
  271. {
  272. static const Word16 pdown[7] =
  273. {
  274. 32767, 32112, 32112, 26214,
  275. 9830, 6553, 6553
  276. };
  277. Word16 tmp;
  278. /* calculate median of last five gains */
  279. tmp = gmed_n (st->pbuf, 5); move16 ();
  280. /* new gain = minimum(median, past_gain) * pdown[state] */
  281. test ();
  282. if (sub_ex (tmp, st->past_gain_pit) > 0)
  283. {
  284. tmp = st->past_gain_pit; move16 ();
  285. }
  286. *gain_pitch = mult_ex (tmp, pdown[state]);
  287. }
  288. /*
  289. **************************************************************************
  290. *
  291. * Function : ec_gain_pitch_update
  292. * Purpose : update the pitch gain concealment state;
  293. * limit gain_pitch if the previous frame was bad
  294. * Call this function always after decoding (or concealing)
  295. * the gain
  296. *
  297. **************************************************************************
  298. */
  299. void ec_gain_pitch_update (
  300. ec_gain_pitchState *st, /* i/o : state variables */
  301. Word16 bfi, /* i : flag: frame is bad */
  302. Word16 prev_bf, /* i : flag: previous frame was bad */
  303. Word16 *gain_pitch /* i/o : pitch gain */
  304. )
  305. {
  306. Word16 i;
  307. test ();
  308. if (bfi == 0)
  309. {
  310. test ();
  311. if (prev_bf != 0)
  312. {
  313. test ();
  314. if (sub_ex (*gain_pitch, st->prev_gp) > 0)
  315. {
  316. *gain_pitch = st->prev_gp;
  317. }
  318. }
  319. st->prev_gp = *gain_pitch; move16 ();
  320. }
  321. st->past_gain_pit = *gain_pitch; move16 ();
  322. test ();
  323. if (sub_ex (st->past_gain_pit, 16384) > 0) /* if (st->past_gain_pit > 1.0) */
  324. {
  325. st->past_gain_pit = 16384; move16 ();
  326. }
  327. for (i = 1; i < 5; i++)
  328. {
  329. st->pbuf[i - 1] = st->pbuf[i]; move16 ();
  330. }
  331. st->pbuf[4] = st->past_gain_pit; move16 ();
  332. }