dec_gain.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 : dec_gain.c
  11. * Purpose : Decode the pitch and codebook gains
  12. *
  13. ********************************************************************************
  14. */
  15. /*
  16. ********************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. ********************************************************************************
  19. */
  20. #include "dec_gain.h"
  21. const char dec_gain_id[] = "@(#)$Id $" dec_gain_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 "mode.h"
  34. #include "cnst.h"
  35. #include "pow2.h"
  36. #include "log2.h"
  37. #include "gc_pred.h"
  38. /*
  39. ********************************************************************************
  40. * LOCAL VARIABLES AND TABLES
  41. ********************************************************************************
  42. */
  43. #include "qua_gain.tab"
  44. #include "qgain475.tab"
  45. /*
  46. ********************************************************************************
  47. * PUBLIC PROGRAM CODE
  48. ********************************************************************************
  49. */
  50. /*************************************************************************
  51. *
  52. * FUNCTION: Dec_gain()
  53. *
  54. * PURPOSE: Decode the pitch and codebook gains
  55. *
  56. ************************************************************************/
  57. void Dec_gain(
  58. gc_predState *pred_state, /* i/o: MA predictor state */
  59. enum Mode mode, /* i : AMR mode */
  60. Word16 index, /* i : index of quantization. */
  61. Word16 code[], /* i : Innovative vector. */
  62. Word16 evenSubfr, /* i : Flag for even subframes */
  63. Word16 * gain_pit, /* o : Pitch gain. */
  64. Word16 * gain_cod /* o : Code gain. */
  65. )
  66. {
  67. const Word16 *p;
  68. Word16 frac, gcode0, exp, qua_ener, qua_ener_MR122;
  69. Word16 g_code;
  70. Word32 L_tmp;
  71. /* Read the quantized gains (table depends on mode) */
  72. index = shl_ex (index, 2);
  73. test(); test(); test();
  74. if ( sub_ex (mode, MR102) == 0
  75. || sub_ex (mode, MR74) == 0
  76. || sub_ex (mode, MR67) == 0)
  77. {
  78. p = &table_gain_highrates[index]; move16 ();
  79. *gain_pit = *p++; move16 ();
  80. g_code = *p++; move16 ();
  81. qua_ener_MR122 = *p++; move16 ();
  82. qua_ener = *p; move16 ();
  83. }
  84. else
  85. {
  86. test();
  87. if (sub_ex (mode, MR475) == 0)
  88. {
  89. index = add_ex (index, shl_ex(sub_ex(1, evenSubfr), 1));
  90. p = &table_gain_MR475[index]; move16 ();
  91. *gain_pit = *p++; move16 ();
  92. g_code = *p++; move16 ();
  93. /*---------------------------------------------------------*
  94. * calculate predictor update values (not stored in 4.75 *
  95. * quantizer table to save space): *
  96. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
  97. * *
  98. * qua_ener = log2(g) *
  99. * qua_ener_MR122 = 20*log10(g) *
  100. *---------------------------------------------------------*/
  101. /* Log2(x Q12) = log2(x) + 12 */
  102. Log2 (L_deposit_l_ex (g_code), &exp, &frac);
  103. exp = sub_ex(exp, 12);
  104. qua_ener_MR122 = add_ex (shr_r_ex (frac, 5), shl_ex (exp, 10));
  105. /* 24660 Q12 ~= 6.0206 = 20*log10(2) */
  106. L_tmp = Mpy_32_16(exp, frac, 24660);
  107. qua_ener = round_ex (L_shl_ex (L_tmp, 13)); /* Q12 * Q0 = Q13 -> Q10 */
  108. }
  109. else
  110. {
  111. p = &table_gain_lowrates[index]; move16 ();
  112. *gain_pit = *p++; move16 ();
  113. g_code = *p++; move16 ();
  114. qua_ener_MR122 = *p++; move16 ();
  115. qua_ener = *p; move16 ();
  116. }
  117. }
  118. /*-------------------------------------------------------------------*
  119. * predict codebook gain *
  120. * ~~~~~~~~~~~~~~~~~~~~~ *
  121. * gc0 = Pow2(int(d)+frac(d)) *
  122. * = 2^exp + 2^frac *
  123. * *
  124. * gcode0 (Q14) = 2^14*2^frac = gc0 * 2^(14-exp) *
  125. *-------------------------------------------------------------------*/
  126. gc_pred(pred_state, mode, code, &exp, &frac, NULL, NULL);
  127. gcode0 = extract_l_ex(Pow2(14, frac));
  128. /*------------------------------------------------------------------*
  129. * read quantized gains, update table of past quantized energies *
  130. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
  131. * st->past_qua_en(Q10) = 20 * Log10(g_fac) / constant *
  132. * = Log2(g_fac) *
  133. * = qua_ener *
  134. * constant = 20*Log10(2) *
  135. *------------------------------------------------------------------*/
  136. L_tmp = L_mult_ex(g_code, gcode0);
  137. L_tmp = L_shr_ex(L_tmp, sub_ex(10, exp));
  138. *gain_cod = extract_h_ex(L_tmp);
  139. /* update table of past quantized energies */
  140. gc_pred_update(pred_state, qua_ener_MR122, qua_ener);
  141. return;
  142. }