calc_en.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 : calc_en.c
  11. * Purpose : (pre-) quantization of pitch gain for MR795
  12. *
  13. ********************************************************************************
  14. */
  15. /*
  16. ********************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. ********************************************************************************
  19. */
  20. #include "calc_en.h"
  21. const char calc_en_id[] = "@(#)$Id $" calc_en_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 "log2.h"
  35. /*
  36. ********************************************************************************
  37. * PUBLIC PROGRAM CODE
  38. ********************************************************************************
  39. */
  40. /*************************************************************************
  41. *
  42. * FUNCTION: calc_unfilt_energies
  43. *
  44. * PURPOSE: calculation of several energy coefficients for unfiltered
  45. * excitation signals and the LTP coding gain
  46. *
  47. * frac_en[0]*2^exp_en[0] = <res res> // LP residual energy
  48. * frac_en[1]*2^exp_en[1] = <exc exc> // LTP residual energy
  49. * frac_en[2]*2^exp_en[2] = <exc code> // LTP/CB innovation dot product
  50. * frac_en[3]*2^exp_en[3] = <lres lres> // LTP residual energy
  51. * // (lres = res - gain_pit*exc)
  52. * ltpg = log2(LP_res_en / LTP_res_en)
  53. *
  54. *************************************************************************/
  55. void
  56. calc_unfilt_energies(
  57. Word16 res[], /* i : LP residual, Q0 */
  58. Word16 exc[], /* i : LTP excitation (unfiltered), Q0 */
  59. Word16 code[], /* i : CB innovation (unfiltered), Q13 */
  60. Word16 gain_pit, /* i : pitch gain, Q14 */
  61. Word16 L_subfr, /* i : Subframe length */
  62. Word16 frac_en[], /* o : energy coefficients (4), fraction part, Q15 */
  63. Word16 exp_en[], /* o : energy coefficients (4), exponent part, Q0 */
  64. Word16 *ltpg /* o : LTP coding gain (log2()), Q13 */
  65. )
  66. {
  67. Word32 s, L_temp;
  68. Word16 i, exp, tmp;
  69. Word16 ltp_res_en, pred_gain;
  70. Word16 ltpg_exp, ltpg_frac;
  71. /* Compute residual energy */
  72. s = L_mac_ex((Word32) 0, res[0], res[0]);
  73. for (i = 1; i < L_subfr; i++)
  74. s = L_mac_ex(s, res[i], res[i]);
  75. /* ResEn := 0 if ResEn < 200.0 (= 400 Q1) */
  76. test();
  77. if (L_sub_ex (s, 400L) < 0)
  78. {
  79. frac_en[0] = 0; move16 ();
  80. exp_en[0] = -15; move16 ();
  81. }
  82. else
  83. {
  84. exp = norm_l_ex(s);
  85. frac_en[0] = extract_h_ex(L_shl_ex(s, exp)); move16 ();
  86. exp_en[0] = sub_ex(15, exp); move16 ();
  87. }
  88. /* Compute ltp excitation energy */
  89. s = L_mac_ex((Word32) 0, exc[0], exc[0]);
  90. for (i = 1; i < L_subfr; i++)
  91. s = L_mac_ex(s, exc[i], exc[i]);
  92. exp = norm_l_ex(s);
  93. frac_en[1] = extract_h_ex(L_shl_ex(s, exp)); move16 ();
  94. exp_en[1] = sub_ex(15, exp); move16 ();
  95. /* Compute scalar product <exc[],code[]> */
  96. s = L_mac_ex((Word32) 0, exc[0], code[0]);
  97. for (i = 1; i < L_subfr; i++)
  98. s = L_mac_ex(s, exc[i], code[i]);
  99. exp = norm_l_ex(s);
  100. frac_en[2] = extract_h_ex(L_shl_ex(s, exp)); move16 ();
  101. exp_en[2] = sub_ex(16-14, exp); move16 ();
  102. /* Compute energy of LTP residual */
  103. s = 0L; move32 ();
  104. for (i = 0; i < L_subfr; i++)
  105. {
  106. L_temp = L_mult_ex(exc[i], gain_pit);
  107. L_temp = L_shl_ex(L_temp, 1);
  108. tmp = sub_ex(res[i], round_ex(L_temp)); /* LTP residual, Q0 */
  109. s = L_mac_ex (s, tmp, tmp);
  110. }
  111. exp = norm_l_ex(s);
  112. ltp_res_en = extract_h_ex (L_shl_ex (s, exp));
  113. exp = sub_ex (15, exp);
  114. frac_en[3] = ltp_res_en; move16 ();
  115. exp_en[3] = exp; move16 ();
  116. /* calculate LTP coding gain, i.e. energy reduction LP res -> LTP res */
  117. test (); test ();
  118. if (ltp_res_en > 0 && frac_en[0] != 0)
  119. {
  120. /* gain = ResEn / LTPResEn */
  121. pred_gain = div_s (shr_ex (frac_en[0], 1), ltp_res_en);
  122. exp = sub_ex (exp, exp_en[0]);
  123. /* L_temp = ltpGain * 2^(30 + exp) */
  124. L_temp = L_deposit_h_ex (pred_gain);
  125. /* L_temp = ltpGain * 2^27 */
  126. L_temp = L_shr_ex (L_temp, add_ex (exp, 3));
  127. /* Log2 = log2() + 27 */
  128. Log2(L_temp, &ltpg_exp, &ltpg_frac);
  129. /* ltpg = log2(LtpGain) * 2^13 --> range: +- 4 = +- 12 dB */
  130. L_temp = L_Comp (sub_ex (ltpg_exp, 27), ltpg_frac);
  131. *ltpg = round_ex (L_shl_ex (L_temp, 13)); /* Q13 */
  132. }
  133. else
  134. {
  135. *ltpg = 0; move16 ();
  136. }
  137. }
  138. /*************************************************************************
  139. *
  140. * FUNCTION: calc_filt_energies
  141. *
  142. * PURPOSE: calculation of several energy coefficients for filtered
  143. * excitation signals
  144. *
  145. * Compute coefficients need for the quantization and the optimum
  146. * codebook gain gcu (for MR475 only).
  147. *
  148. * coeff[0] = y1 y1
  149. * coeff[1] = -2 xn y1
  150. * coeff[2] = y2 y2
  151. * coeff[3] = -2 xn y2
  152. * coeff[4] = 2 y1 y2
  153. *
  154. *
  155. * gcu = <xn2, y2> / <y2, y2> (0 if <xn2, y2> <= 0)
  156. *
  157. * Product <y1 y1> and <xn y1> have been computed in G_pitch() and
  158. * are in vector g_coeff[].
  159. *
  160. *************************************************************************/
  161. void
  162. calc_filt_energies(
  163. enum Mode mode, /* i : coder mode */
  164. Word16 xn[], /* i : LTP target vector, Q0 */
  165. Word16 xn2[], /* i : CB target vector, Q0 */
  166. Word16 y1[], /* i : Adaptive codebook, Q0 */
  167. Word16 Y2[], /* i : Filtered innovative vector, Q12 */
  168. Word16 g_coeff[], /* i : Correlations <xn y1> <y1 y1> */
  169. /* computed in G_pitch() */
  170. Word16 frac_coeff[],/* o : energy coefficients (5), fraction part, Q15 */
  171. Word16 exp_coeff[], /* o : energy coefficients (5), exponent part, Q0 */
  172. Word16 *cod_gain_frac,/* o: optimum codebook gain (fraction part), Q15 */
  173. Word16 *cod_gain_exp /* o: optimum codebook gain (exponent part), Q0 */
  174. )
  175. {
  176. Word32 s, ener_init;
  177. Word16 i, exp, frac;
  178. Word16 y2[L_SUBFR];
  179. if (test(), sub_ex(mode, MR795) == 0 || sub_ex(mode, MR475) == 0)
  180. {
  181. ener_init = 0L; move32 ();
  182. }
  183. else
  184. {
  185. ener_init = 1L; move32 ();
  186. }
  187. for (i = 0; i < L_SUBFR; i++) {
  188. y2[i] = shr_ex(Y2[i], 3); move16 ();
  189. }
  190. frac_coeff[0] = g_coeff[0]; move16 ();
  191. exp_coeff[0] = g_coeff[1]; move16 ();
  192. frac_coeff[1] = negate_ex(g_coeff[2]); move16 (); /* coeff[1] = -2 xn y1 */
  193. exp_coeff[1] = add_ex(g_coeff[3], 1); move16 ();
  194. /* Compute scalar product <y2[],y2[]> */
  195. s = L_mac_ex(ener_init, y2[0], y2[0]);
  196. for (i = 1; i < L_SUBFR; i++)
  197. s = L_mac_ex(s, y2[i], y2[i]);
  198. exp = norm_l_ex(s);
  199. frac_coeff[2] = extract_h_ex(L_shl_ex(s, exp)); move16 ();
  200. exp_coeff[2] = sub_ex(15 - 18, exp); move16();
  201. /* Compute scalar product -2*<xn[],y2[]> */
  202. s = L_mac_ex(ener_init, xn[0], y2[0]);
  203. for (i = 1; i < L_SUBFR; i++)
  204. s = L_mac_ex(s, xn[i], y2[i]);
  205. exp = norm_l_ex(s);
  206. frac_coeff[3] = negate_ex(extract_h_ex(L_shl_ex(s, exp))); move16 ();
  207. exp_coeff[3] = sub_ex(15 - 9 + 1, exp); move16 ();
  208. /* Compute scalar product 2*<y1[],y2[]> */
  209. s = L_mac_ex(ener_init, y1[0], y2[0]);
  210. for (i = 1; i < L_SUBFR; i++)
  211. s = L_mac_ex(s, y1[i], y2[i]);
  212. exp = norm_l_ex(s);
  213. frac_coeff[4] = extract_h_ex(L_shl_ex(s, exp)); move16 ();
  214. exp_coeff[4] = sub_ex(15 - 9 + 1, exp); move16();
  215. if (test(), test (), sub_ex(mode, MR475) == 0 || sub_ex(mode, MR795) == 0)
  216. {
  217. /* Compute scalar product <xn2[],y2[]> */
  218. s = L_mac_ex(ener_init, xn2[0], y2[0]);
  219. for (i = 1; i < L_SUBFR; i++)
  220. s = L_mac_ex(s, xn2[i], y2[i]);
  221. exp = norm_l_ex(s);
  222. frac = extract_h_ex(L_shl_ex(s, exp));
  223. exp = sub_ex(15 - 9, exp);
  224. if (test (), frac <= 0)
  225. {
  226. *cod_gain_frac = 0; move16 ();
  227. *cod_gain_exp = 0; move16 ();
  228. }
  229. else
  230. {
  231. /*
  232. gcu = <xn2, y2> / c[2]
  233. = (frac>>1)/frac[2] * 2^(exp+1-exp[2])
  234. = div_s(frac>>1, frac[2])*2^-15 * 2^(exp+1-exp[2])
  235. = div_s * 2^(exp-exp[2]-14)
  236. */
  237. *cod_gain_frac = div_s (shr_ex (frac,1), frac_coeff[2]); move16 ();
  238. *cod_gain_exp = sub_ex (sub_ex (exp, exp_coeff[2]), 14); move16 ();
  239. }
  240. }
  241. }
  242. /*************************************************************************
  243. *
  244. * FUNCTION: calc_target_energy
  245. *
  246. * PURPOSE: calculation of target energy
  247. *
  248. * en = <xn, xn>
  249. *
  250. *************************************************************************/
  251. void
  252. calc_target_energy(
  253. Word16 xn[], /* i: LTP target vector, Q0 */
  254. Word16 *en_exp, /* o: optimum codebook gain (exponent part), Q0 */
  255. Word16 *en_frac /* o: optimum codebook gain (fraction part), Q15 */
  256. )
  257. {
  258. Word32 s;
  259. Word16 i, exp;
  260. /* Compute scalar product <xn[], xn[]> */
  261. s = L_mac_ex(0L, xn[0], xn[0]);
  262. for (i = 1; i < L_SUBFR; i++)
  263. s = L_mac_ex(s, xn[i], xn[i]);
  264. /* s = SUM 2*xn(i) * xn(i) = <xn xn> * 2 */
  265. exp = norm_l_ex(s);
  266. *en_frac = extract_h_ex(L_shl_ex(s, exp));
  267. *en_exp = sub_ex(16, exp); move16();
  268. }