q_gain_c.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 : q_gain_c.c
  11. * Purpose : Scalar quantization of the innovative
  12. * : codebook gain.
  13. *
  14. ********************************************************************************
  15. */
  16. /*
  17. ********************************************************************************
  18. * MODULE INCLUDE FILE AND VERSION ID
  19. ********************************************************************************
  20. */
  21. #include "q_gain_c.h"
  22. const char q_gain_c_id[] = "@(#)$Id $" q_gain_c_h;
  23. /*
  24. ********************************************************************************
  25. * INCLUDE FILES
  26. ********************************************************************************
  27. */
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include "typedef.h"
  31. #include "mode.h"
  32. #include "basic_op.h"
  33. #include "oper_32b.h"
  34. #include "count.h"
  35. #include "log2.h"
  36. #include "pow2.h"
  37. /*
  38. ********************************************************************************
  39. * LOCAL VARIABLES AND TABLES
  40. ********************************************************************************
  41. */
  42. #include "gains.tab"
  43. /*
  44. ********************************************************************************
  45. * PUBLIC PROGRAM CODE
  46. ********************************************************************************
  47. */
  48. /*--------------------------------------------------------------------------*
  49. * Function q_gain_code() *
  50. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
  51. * Scalar quantization of the innovative codebook gain. *
  52. * *
  53. *--------------------------------------------------------------------------*/
  54. Word16 q_gain_code ( /* o : quantization index, Q0 */
  55. enum Mode mode, /* i : AMR mode */
  56. Word16 exp_gcode0, /* i : predicted CB gain (exponent), Q0 */
  57. Word16 frac_gcode0, /* i : predicted CB gain (fraction), Q15 */
  58. Word16 *gain, /* i/o: quantized fixed codebook gain, Q1 */
  59. Word16 *qua_ener_MR122, /* o : quantized energy error, Q10 */
  60. /* (for MR122 MA predictor update) */
  61. Word16 *qua_ener /* o : quantized energy error, Q10 */
  62. /* (for other MA predictor update) */
  63. )
  64. {
  65. const Word16 *p;
  66. Word16 i, index;
  67. Word16 gcode0, err, err_min;
  68. Word16 g_q0;
  69. test ();
  70. g_q0 = 0; move16 ();
  71. test ();
  72. if (sub_ex(mode, MR122) == 0)
  73. {
  74. g_q0 = shr_ex (*gain, 1); /* Q1 -> Q0 */
  75. }
  76. /*-------------------------------------------------------------------*
  77. * predicted codebook gain *
  78. * ~~~~~~~~~~~~~~~~~~~~~~~ *
  79. * gc0 = Pow2(int(d)+frac(d)) *
  80. * = 2^exp + 2^frac *
  81. * *
  82. *-------------------------------------------------------------------*/
  83. gcode0 = extract_l_ex (Pow2 (exp_gcode0, frac_gcode0)); /* predicted gain */
  84. test ();
  85. if (sub_ex(mode, MR122) == 0)
  86. {
  87. gcode0 = shl_ex (gcode0, 4);
  88. }
  89. else
  90. {
  91. gcode0 = shl_ex (gcode0, 5);
  92. }
  93. /*-------------------------------------------------------------------*
  94. * Search for best quantizer *
  95. *-------------------------------------------------------------------*/
  96. p = &qua_gain_code[0]; move16 ();
  97. test ();
  98. if (sub_ex(mode, MR122) == 0)
  99. {
  100. err_min = abs_s_ex (sub_ex (g_q0, mult_ex (gcode0, *p++)));
  101. }
  102. else
  103. {
  104. err_min = abs_s_ex (sub_ex (*gain, mult_ex (gcode0, *p++)));
  105. }
  106. p += 2; /* skip quantized energy errors */
  107. index = 0; move16 ();
  108. for (i = 1; i < NB_QUA_CODE; i++)
  109. {
  110. test ();
  111. if (sub_ex(mode, MR122) == 0)
  112. {
  113. err = abs_s_ex (sub_ex (g_q0, mult_ex (gcode0, *p++)));
  114. }
  115. else
  116. {
  117. err = abs_s_ex (sub_ex (*gain, mult_ex (gcode0, *p++)));
  118. }
  119. p += 2; /* skip quantized energy error */
  120. test ();
  121. if (sub_ex (err, err_min) < 0)
  122. {
  123. err_min = err; move16 ();
  124. index = i; move16 ();
  125. }
  126. }
  127. p = &qua_gain_code[add_ex (add_ex (index,index), index)]; move16 ();
  128. test ();
  129. if (sub_ex(mode, MR122) == 0)
  130. {
  131. *gain = shl_ex (mult_ex (gcode0, *p++), 1);
  132. }
  133. else
  134. {
  135. *gain = mult_ex (gcode0, *p++);
  136. }
  137. move16 ();
  138. /* quantized error energies (for MA predictor update) */
  139. *qua_ener_MR122 = *p++; move16 ();
  140. *qua_ener = *p; move16 ();
  141. return index;
  142. }