qua_gain.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 : qua_gain.c
  11. * Purpose : Quantization of pitch and codebook gains.
  12. *
  13. ********************************************************************************
  14. */
  15. /*
  16. ********************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. ********************************************************************************
  19. */
  20. #include "qua_gain.h"
  21. const char qua_gain_id[] = "@(#)$Id $" qua_gain_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 "mode.h"
  33. #include "count.h"
  34. #include "cnst.h"
  35. #include "pow2.h"
  36. #include "gc_pred.h"
  37. /*
  38. ********************************************************************************
  39. * LOCAL VARIABLES AND TABLES
  40. ********************************************************************************
  41. */
  42. #include "qua_gain.tab"
  43. /*
  44. ********************************************************************************
  45. * PUBLIC PROGRAM CODE
  46. ********************************************************************************
  47. */
  48. /*************************************************************************
  49. *
  50. * FUNCTION: Qua_gain()
  51. *
  52. * PURPOSE: Quantization of pitch and codebook gains.
  53. * (using predicted codebook gain)
  54. *
  55. *************************************************************************/
  56. Word16
  57. Qua_gain( /* o : index of quantization. */
  58. enum Mode mode, /* i : AMR mode */
  59. Word16 exp_gcode0, /* i : predicted CB gain (exponent), Q0 */
  60. Word16 frac_gcode0, /* i : predicted CB gain (fraction), Q15 */
  61. Word16 frac_coeff[], /* i : energy coeff. (5), fraction part, Q15 */
  62. Word16 exp_coeff[], /* i : energy coeff. (5), exponent part, Q0 */
  63. /* (frac_coeff and exp_coeff computed in */
  64. /* calc_filt_energies()) */
  65. Word16 gp_limit, /* i : pitch gain limit */
  66. Word16 *gain_pit, /* o : Pitch gain, Q14 */
  67. Word16 *gain_cod, /* o : Code gain, Q1 */
  68. Word16 *qua_ener_MR122, /* o : quantized energy error, Q10 */
  69. /* (for MR122 MA predictor update) */
  70. Word16 *qua_ener /* o : quantized energy error, Q10 */
  71. /* (for other MA predictor update) */
  72. )
  73. {
  74. const Word16 *p;
  75. Word16 i, j, index = 0;
  76. Word16 gcode0, e_max, exp_code;
  77. Word16 g_pitch, g2_pitch, g_code, g2_code, g_pit_cod;
  78. Word16 coeff[5], coeff_lo[5];
  79. Word16 exp_max[5];
  80. Word32 L_tmp, dist_min;
  81. const Word16 *table_gain;
  82. Word16 table_len;
  83. test(); test(); test();
  84. if ( sub_ex (mode, MR102) == 0 || sub_ex (mode, MR74) == 0 || sub_ex (mode, MR67) == 0)
  85. {
  86. table_len = VQ_SIZE_HIGHRATES; move16 ();
  87. table_gain = table_gain_highrates; move16 ();
  88. }
  89. else
  90. {
  91. table_len = VQ_SIZE_LOWRATES; move16 ();
  92. table_gain = table_gain_lowrates; move16 ();
  93. }
  94. /*-------------------------------------------------------------------*
  95. * predicted codebook gain *
  96. * ~~~~~~~~~~~~~~~~~~~~~~~ *
  97. * gc0 = 2^exp_gcode0 + 2^frac_gcode0 *
  98. * *
  99. * gcode0 (Q14) = 2^14*2^frac_gcode0 = gc0 * 2^(14-exp_gcode0) *
  100. *-------------------------------------------------------------------*/
  101. gcode0 = extract_l_ex(Pow2(14, frac_gcode0));
  102. /*-------------------------------------------------------------------*
  103. * Scaling considerations: *
  104. * ~~~~~~~~~~~~~~~~~~~~~~~ *
  105. *-------------------------------------------------------------------*/
  106. /*
  107. * The error energy (sum) to be minimized consists of five terms, t[0..4].
  108. *
  109. * t[0] = gp^2 * <y1 y1>
  110. * t[1] = -2*gp * <xn y1>
  111. * t[2] = gc^2 * <y2 y2>
  112. * t[3] = -2*gc * <xn y2>
  113. * t[4] = 2*gp*gc * <y1 y2>
  114. *
  115. */
  116. /* determine the scaling exponent for g_code: ec = ec0 - 11 */
  117. exp_code = sub_ex(exp_gcode0, 11);
  118. /* calculate exp_max[i] = s[i]-1 */
  119. exp_max[0] = sub_ex(exp_coeff[0], 13); move16 ();
  120. exp_max[1] = sub_ex(exp_coeff[1], 14); move16 ();
  121. exp_max[2] = add_ex(exp_coeff[2], add_ex(15, shl_ex(exp_code, 1))); move16 ();
  122. exp_max[3] = add_ex(exp_coeff[3], exp_code); move16 ();
  123. exp_max[4] = add_ex(exp_coeff[4], add_ex(1, exp_code)); move16 ();
  124. /*-------------------------------------------------------------------*
  125. * Find maximum exponent: *
  126. * ~~~~~~~~~~~~~~~~~~~~~~ *
  127. * *
  128. * For the sum operation, all terms must have the same scaling; *
  129. * that scaling should be low enough to prevent overflow. There- *
  130. * fore, the maximum scale is determined and all coefficients are *
  131. * re-scaled: *
  132. * *
  133. * e_max = max(exp_max[i]) + 1; *
  134. * e = exp_max[i]-e_max; e <= 0! *
  135. * c[i] = c[i]*2^e *
  136. *-------------------------------------------------------------------*/
  137. e_max = exp_max[0]; move16 ();
  138. for (i = 1; i < 5; i++)
  139. {
  140. move16(); test();
  141. if (sub_ex(exp_max[i], e_max) > 0)
  142. {
  143. e_max = exp_max[i]; move16 ();
  144. }
  145. }
  146. e_max = add_ex(e_max, 1); /* To avoid overflow */
  147. for (i = 0; i < 5; i++) {
  148. j = sub_ex(e_max, exp_max[i]);
  149. L_tmp = L_deposit_h_ex(frac_coeff[i]);
  150. L_tmp = L_shr_ex(L_tmp, j);
  151. L_Extract(L_tmp, &coeff[i], &coeff_lo[i]);
  152. }
  153. /*-------------------------------------------------------------------*
  154. * Codebook search: *
  155. * ~~~~~~~~~~~~~~~~ *
  156. * *
  157. * For each pair (g_pitch, g_fac) in the table calculate the *
  158. * terms t[0..4] and sum them up; the result is the mean squared *
  159. * error for the quantized gains from the table. The index for the *
  160. * minimum MSE is stored and finally used to retrieve the quantized *
  161. * gains *
  162. *-------------------------------------------------------------------*/
  163. /* start with "infinite" MSE */
  164. dist_min = MAX_32; move32();
  165. p = &table_gain[0]; move16 ();
  166. for (i = 0; i < table_len; i++)
  167. {
  168. g_pitch = *p++; move16 ();
  169. g_code = *p++; move16 (); /* this is g_fac */
  170. p++; /* skip log2(g_fac) */
  171. p++; /* skip 20*log10(g_fac) */
  172. test ();
  173. if (sub_ex(g_pitch, gp_limit) <= 0)
  174. {
  175. g_code = mult_ex(g_code, gcode0);
  176. g2_pitch = mult_ex(g_pitch, g_pitch);
  177. g2_code = mult_ex(g_code, g_code);
  178. g_pit_cod = mult_ex(g_code, g_pitch);
  179. L_tmp = Mpy_32_16(coeff[0], coeff_lo[0], g2_pitch);
  180. L_tmp = L_add_ex(L_tmp, Mpy_32_16(coeff[1], coeff_lo[1], g_pitch));
  181. L_tmp = L_add_ex(L_tmp, Mpy_32_16(coeff[2], coeff_lo[2], g2_code));
  182. L_tmp = L_add_ex(L_tmp, Mpy_32_16(coeff[3], coeff_lo[3], g_code));
  183. L_tmp = L_add_ex(L_tmp, Mpy_32_16(coeff[4], coeff_lo[4], g_pit_cod));
  184. /* store table index if MSE for this index is lower
  185. than the minimum MSE seen so far */
  186. test ();
  187. if (L_sub_ex(L_tmp, dist_min) < (Word32) 0)
  188. {
  189. dist_min = L_tmp; move32 ();
  190. index = i; move16 ();
  191. }
  192. }
  193. }
  194. /*------------------------------------------------------------------*
  195. * read quantized gains and new values for MA predictor memories *
  196. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
  197. *------------------------------------------------------------------*/
  198. /* Read the quantized gains */
  199. p = &table_gain[shl_ex (index, 2)]; move16 ();
  200. *gain_pit = *p++; move16();
  201. g_code = *p++; move16();
  202. *qua_ener_MR122 = *p++; move16();
  203. *qua_ener = *p; move16();
  204. /*------------------------------------------------------------------*
  205. * calculate final fixed codebook gain: *
  206. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
  207. * *
  208. * gc = gc0 * g *
  209. *------------------------------------------------------------------*/
  210. L_tmp = L_mult_ex(g_code, gcode0);
  211. L_tmp = L_shr_ex(L_tmp, sub_ex(10, exp_gcode0));
  212. *gain_cod = extract_h_ex(L_tmp);
  213. return index;
  214. }