gc_pred.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 : gc_pred.c
  11. * Purpose : codebook gain MA prediction
  12. *
  13. *****************************************************************************
  14. */
  15. /*
  16. *****************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. *****************************************************************************
  19. */
  20. #include "gc_pred.h"
  21. const char gc_pred_id[] = "@(#)$Id $" gc_pred_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 "cnst.h"
  33. #include "count.h"
  34. #include "log2.h"
  35. #include "copy.h"
  36. /*
  37. *****************************************************************************
  38. * LOCAL VARIABLES AND TABLES
  39. *****************************************************************************
  40. */
  41. #define NPRED 4 /* number of prediction taps */
  42. /* MA prediction coefficients (Q13) */
  43. static const Word16 pred[NPRED] = {5571, 4751, 2785, 1556};
  44. /* average innovation energy. */
  45. /* MEAN_ENER = 36.0/constant, constant = 20*Log10(2) */
  46. #define MEAN_ENER_MR122 783741L /* 36/(20*log10(2)) (Q17) */
  47. /* MA prediction coefficients (Q6) */
  48. static const Word16 pred_MR122[NPRED] = {44, 37, 22, 12};
  49. /* minimum quantized energy: -14 dB */
  50. #define MIN_ENERGY -14336 /* 14 Q10 */
  51. #define MIN_ENERGY_MR122 -2381 /* 14 / (20*log10(2)) Q10 */
  52. /*
  53. *****************************************************************************
  54. * PUBLIC PROGRAM CODE
  55. *****************************************************************************
  56. */
  57. /*************************************************************************
  58. *
  59. * Function: qua_gain_init
  60. * Purpose: Allocates state memory and initializes state memory
  61. *
  62. **************************************************************************
  63. */
  64. int gc_pred_init (gc_predState **state)
  65. {
  66. gc_predState* s;
  67. if (state == (gc_predState **) NULL){
  68. wfprintf(stderr, "gc_pred_init: invalid parameter\n");
  69. return -1;
  70. }
  71. *state = NULL;
  72. /* allocate memory */
  73. if ((s= (gc_predState *) wmalloc(sizeof(gc_predState))) == NULL){
  74. wfprintf(stderr, "gc_pred_init: can not malloc state structure\n");
  75. return -1;
  76. }
  77. gc_pred_reset(s);
  78. *state = s;
  79. return 0;
  80. }
  81. /*************************************************************************
  82. *
  83. * Function: gc_pred_reset
  84. * Purpose: Initializes state memory to zero
  85. *
  86. **************************************************************************
  87. */
  88. int gc_pred_reset (gc_predState *state)
  89. {
  90. Word16 i;
  91. if (state == (gc_predState *) NULL){
  92. wfprintf(stderr, "gc_pred_reset: invalid parameter\n");
  93. return -1;
  94. }
  95. for(i = 0; i < NPRED; i++)
  96. {
  97. state->past_qua_en[i] = MIN_ENERGY;
  98. state->past_qua_en_MR122[i] = MIN_ENERGY_MR122;
  99. }
  100. return 0;
  101. }
  102. /*************************************************************************
  103. *
  104. * Function: gc_pred_exit
  105. * Purpose: The memory used for state memory is freed
  106. *
  107. **************************************************************************
  108. */
  109. void gc_pred_exit (gc_predState **state)
  110. {
  111. if (state == NULL || *state == NULL)
  112. return;
  113. /* deallocate memory */
  114. wfree(*state);
  115. *state = NULL;
  116. return;
  117. }
  118. /*************************************************************************
  119. *
  120. * FUNCTION: gc_pred_copy()
  121. *
  122. * PURPOSE: Copy MA predictor state variable
  123. *
  124. *************************************************************************/
  125. void
  126. gc_pred_copy(
  127. gc_predState *st_src, /* i : State struct */
  128. gc_predState *st_dest /* o : State struct */
  129. )
  130. {
  131. Copy (st_src->past_qua_en, st_dest->past_qua_en, NPRED);
  132. Copy (st_src->past_qua_en_MR122, st_dest->past_qua_en_MR122, NPRED);
  133. }
  134. /*************************************************************************
  135. *
  136. * FUNCTION: gc_pred()
  137. *
  138. * PURPOSE: MA prediction of the innovation energy
  139. * (in dB/(20*log10(2))) with mean removed).
  140. *
  141. *************************************************************************/
  142. void
  143. gc_pred(
  144. gc_predState *st, /* i/o: State struct */
  145. enum Mode mode, /* i : AMR mode */
  146. Word16 *code, /* i : innovative codebook vector (L_SUBFR) */
  147. /* MR122: Q12, other modes: Q13 */
  148. Word16 *exp_gcode0, /* o : exponent of predicted gain factor, Q0 */
  149. Word16 *frac_gcode0,/* o : fraction of predicted gain factor Q15 */
  150. Word16 *exp_en, /* o : exponent of innovation energy, Q0 */
  151. /* (only calculated for MR795) */
  152. Word16 *frac_en /* o : fraction of innovation energy, Q15 */
  153. /* (only calculated for MR795) */
  154. )
  155. {
  156. Word16 i;
  157. Word32 ener_code;
  158. Word16 exp, frac;
  159. /*-------------------------------------------------------------------*
  160. * energy of code: *
  161. * ~~~~~~~~~~~~~~~ *
  162. * ener_code = sum(code[i]^2) *
  163. *-------------------------------------------------------------------*/
  164. ener_code = L_mac_ex((Word32) 0, code[0], code[0]);
  165. /* MR122: Q12*Q12 -> Q25 */
  166. /* others: Q13*Q13 -> Q27 */
  167. for (i = 1; i < L_SUBFR; i++)
  168. ener_code = L_mac_ex(ener_code, code[i], code[i]);
  169. test ();
  170. if (sub_ex (mode, MR122) == 0)
  171. {
  172. Word32 ener;
  173. /* ener_code = ener_code / lcode; lcode = 40; 1/40 = 26214 Q20 */
  174. ener_code = L_mult_ex (round_ex (ener_code), 26214); /* Q9 * Q20 -> Q30 */
  175. /*-------------------------------------------------------------------*
  176. * energy of code: *
  177. * ~~~~~~~~~~~~~~~ *
  178. * ener_code(Q17) = 10 * Log10(energy) / constant *
  179. * = 1/2 * Log2(energy) *
  180. * constant = 20*Log10(2) *
  181. *-------------------------------------------------------------------*/
  182. /* ener_code = 1/2 * Log2(ener_code); Note: Log2=log2+30 */
  183. Log2(ener_code, &exp, &frac);
  184. ener_code = L_Comp (sub_ex (exp, 30), frac); /* Q16 for log() */
  185. /* ->Q17 for 1/2 log()*/
  186. /*-------------------------------------------------------------------*
  187. * predicted energy: *
  188. * ~~~~~~~~~~~~~~~~~ *
  189. * ener(Q24) = (Emean + sum{pred[i]*past_en[i]})/constant *
  190. * = MEAN_ENER + sum(pred[i]*past_qua_en[i]) *
  191. * constant = 20*Log10(2) *
  192. *-------------------------------------------------------------------*/
  193. ener = MEAN_ENER_MR122; move32 (); /* Q24 (Q17) */
  194. for (i = 0; i < NPRED; i++)
  195. {
  196. ener = L_mac_ex (ener, st->past_qua_en_MR122[i], pred_MR122[i]);
  197. /* Q10 * Q13 -> Q24 */
  198. /* Q10 * Q6 -> Q17 */
  199. }
  200. /*-------------------------------------------------------------------*
  201. * predicted codebook gain *
  202. * ~~~~~~~~~~~~~~~~~~~~~~~ *
  203. * gc0 = Pow10( (ener*constant - ener_code*constant) / 20 ) *
  204. * = Pow2(ener-ener_code) *
  205. * = Pow2(int(d)+frac(d)) *
  206. * *
  207. * (store exp and frac for pow2()) *
  208. *-------------------------------------------------------------------*/
  209. ener = L_shr_ex (L_sub_ex (ener, ener_code), 1); /* Q16 */
  210. L_Extract(ener, exp_gcode0, frac_gcode0);
  211. }
  212. else /* all modes except 12.2 */
  213. {
  214. Word32 L_tmp;
  215. Word16 exp_code, gcode0;
  216. /*-----------------------------------------------------------------*
  217. * Compute: means_ener - 10log10(ener_code/ L_sufr) *
  218. *-----------------------------------------------------------------*/
  219. exp_code = norm_l_ex (ener_code);
  220. ener_code = L_shl_ex (ener_code, exp_code);
  221. /* Log2 = log2 + 27 */
  222. Log2_norm (ener_code, exp_code, &exp, &frac);
  223. /* fact = 10/log2(10) = 3.01 = 24660 Q13 */
  224. L_tmp = Mpy_32_16(exp, frac, -24660); /* Q0.Q15 * Q13 -> Q14 */
  225. /* L_tmp = means_ener - 10log10(ener_code/L_SUBFR)
  226. * = means_ener - 10log10(ener_code) + 10log10(L_SUBFR)
  227. * = K - fact * Log2(ener_code)
  228. * = K - fact * log2(ener_code) - fact*27
  229. *
  230. * ==> K = means_ener + fact*27 + 10log10(L_SUBFR)
  231. *
  232. * means_ener = 33 = 540672 Q14 (MR475, MR515, MR59)
  233. * means_ener = 28.75 = 471040 Q14 (MR67)
  234. * means_ener = 30 = 491520 Q14 (MR74)
  235. * means_ener = 36 = 589824 Q14 (MR795)
  236. * means_ener = 33 = 540672 Q14 (MR102)
  237. * 10log10(L_SUBFR) = 16.02 = 262481.51 Q14
  238. * fact * 27 = 1331640 Q14
  239. * -----------------------------------------
  240. * (MR475, MR515, MR59) K = 2134793.51 Q14 ~= 16678 * 64 * 2
  241. * (MR67) K = 2065161.51 Q14 ~= 32268 * 32 * 2
  242. * (MR74) K = 2085641.51 Q14 ~= 32588 * 32 * 2
  243. * (MR795) K = 2183945.51 Q14 ~= 17062 * 64 * 2
  244. * (MR102) K = 2134793.51 Q14 ~= 16678 * 64 * 2
  245. */
  246. if (test (), sub_ex (mode, MR102) == 0)
  247. {
  248. /* mean = 33 dB */
  249. L_tmp = L_mac_ex(L_tmp, 16678, 64); /* Q14 */
  250. }
  251. else if (test (), sub_ex (mode, MR795) == 0)
  252. {
  253. /* ener_code = <xn xn> * 2^27*2^exp_code
  254. frac_en = ener_code / 2^16
  255. = <xn xn> * 2^11*2^exp_code
  256. <xn xn> = <xn xn>*2^11*2^exp * 2^exp_en
  257. := frac_en * 2^exp_en
  258. ==> exp_en = -11-exp_code;
  259. */
  260. *frac_en = extract_h_ex (ener_code); move16 ();
  261. *exp_en = sub_ex (-11, exp_code); move16 ();
  262. /* mean = 36 dB */
  263. L_tmp = L_mac_ex(L_tmp, 17062, 64); /* Q14 */
  264. }
  265. else if (test (), sub_ex (mode, MR74) == 0)
  266. {
  267. /* mean = 30 dB */
  268. L_tmp = L_mac_ex(L_tmp, 32588, 32); /* Q14 */
  269. }
  270. else if (test (), sub_ex (mode, MR67) == 0)
  271. {
  272. /* mean = 28.75 dB */
  273. L_tmp = L_mac_ex(L_tmp, 32268, 32); /* Q14 */
  274. }
  275. else /* MR59, MR515, MR475 */
  276. {
  277. /* mean = 33 dB */
  278. L_tmp = L_mac_ex(L_tmp, 16678, 64); /* Q14 */
  279. }
  280. /*-----------------------------------------------------------------*
  281. * Compute gcode0. *
  282. * = Sum(i=0,3) pred[i]*past_qua_en[i] - ener_code + mean_ener *
  283. *-----------------------------------------------------------------*/
  284. L_tmp = L_shl_ex(L_tmp, 10); /* Q24 */
  285. for (i = 0; i < 4; i++)
  286. L_tmp = L_mac_ex(L_tmp, pred[i], st->past_qua_en[i]);
  287. /* Q13 * Q10 -> Q24 */
  288. gcode0 = extract_h_ex(L_tmp); /* Q8 */
  289. /*-----------------------------------------------------------------*
  290. * gcode0 = pow(10.0, gcode0/20) *
  291. * = pow(2, 3.3219*gcode0/20) *
  292. * = pow(2, 0.166*gcode0) *
  293. *-----------------------------------------------------------------*/
  294. /* 5439 Q15 = 0.165985 */
  295. /* (correct: 1/(20*log10(2)) 0.166096 = 5443 Q15) */
  296. test ();
  297. if (sub_ex (mode, MR74) == 0) /* For IS641 bitexactness */
  298. L_tmp = L_mult_ex(gcode0, 5439); /* Q8 * Q15 -> Q24 */
  299. else
  300. L_tmp = L_mult_ex(gcode0, 5443); /* Q8 * Q15 -> Q24 */
  301. L_tmp = L_shr_ex(L_tmp, 8); /* -> Q16 */
  302. L_Extract(L_tmp, exp_gcode0, frac_gcode0); /* -> Q0.Q15 */
  303. }
  304. }
  305. /*************************************************************************
  306. *
  307. * FUNCTION: gc_pred_update()
  308. *
  309. * PURPOSE: update MA predictor with last quantized energy
  310. *
  311. *************************************************************************/
  312. void gc_pred_update(
  313. gc_predState *st, /* i/o: State struct */
  314. Word16 qua_ener_MR122, /* i : quantized energy for update, Q10 */
  315. /* (log2(qua_err)) */
  316. Word16 qua_ener /* i : quantized energy for update, Q10 */
  317. /* (20*log10(qua_err)) */
  318. )
  319. {
  320. Word16 i;
  321. for (i = 3; i > 0; i--)
  322. {
  323. st->past_qua_en[i] = st->past_qua_en[i - 1]; move16 ();
  324. st->past_qua_en_MR122[i] = st->past_qua_en_MR122[i - 1]; move16 ();
  325. }
  326. st->past_qua_en_MR122[0] = qua_ener_MR122; /* log2 (qua_err), Q10 */
  327. move16 ();
  328. st->past_qua_en[0] = qua_ener; /* 20*log10(qua_err), Q10 */
  329. move16 ();
  330. }
  331. /*************************************************************************
  332. *
  333. * FUNCTION: gc_pred_average_limited()
  334. *
  335. * PURPOSE: get average of MA predictor state values (with a lower limit)
  336. * [used in error concealment]
  337. *
  338. *************************************************************************/
  339. void gc_pred_average_limited(
  340. gc_predState *st, /* i: State struct */
  341. Word16 *ener_avg_MR122, /* o: everaged quantized energy, Q10 */
  342. /* (log2(qua_err)) */
  343. Word16 *ener_avg /* o: averaged quantized energy, Q10 */
  344. /* (20*log10(qua_err)) */
  345. )
  346. {
  347. Word16 av_pred_en;
  348. Word16 i;
  349. /* do average in MR122 mode (log2() domain) */
  350. av_pred_en = 0; move16 ();
  351. for (i = 0; i < NPRED; i++)
  352. {
  353. av_pred_en = add_ex (av_pred_en, st->past_qua_en_MR122[i]);
  354. }
  355. /* av_pred_en = 0.25*av_pred_en */
  356. av_pred_en = mult_ex (av_pred_en, 8192);
  357. /* if (av_pred_en < -14/(20Log10(2))) av_pred_en = .. */
  358. test ();
  359. if (sub_ex (av_pred_en, MIN_ENERGY_MR122) < 0)
  360. {
  361. av_pred_en = MIN_ENERGY_MR122; move16 ();
  362. }
  363. *ener_avg_MR122 = av_pred_en; move16 ();
  364. /* do average for other modes (20*log10() domain) */
  365. av_pred_en = 0; move16 ();
  366. for (i = 0; i < NPRED; i++)
  367. {
  368. av_pred_en = add_ex (av_pred_en, st->past_qua_en[i]);
  369. }
  370. /* av_pred_en = 0.25*av_pred_en */
  371. av_pred_en = mult_ex (av_pred_en, 8192);
  372. /* if (av_pred_en < -14) av_pred_en = .. */
  373. test ();
  374. if (sub_ex (av_pred_en, MIN_ENERGY) < 0)
  375. {
  376. av_pred_en = MIN_ENERGY; move16 ();
  377. }
  378. *ener_avg = av_pred_en; move16 ();
  379. }