q_gain_p.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_p.c
  11. * Purpose : Scalar quantization of the pitch gain
  12. *
  13. ********************************************************************************
  14. */
  15. /*
  16. ********************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. ********************************************************************************
  19. */
  20. #include "q_gain_p.h"
  21. const char q_gain_p_id[] = "@(#)$Id $" q_gain_p_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 "cnst.h"
  34. /*
  35. ********************************************************************************
  36. * LOCAL VARIABLES AND TABLES
  37. ********************************************************************************
  38. */
  39. #include "gains.tab"
  40. /*
  41. ********************************************************************************
  42. * PUBLIC PROGRAM CODE
  43. ********************************************************************************
  44. */
  45. Word16 q_gain_pitch ( /* Return index of quantization */
  46. enum Mode mode, /* i : AMR mode */
  47. Word16 gp_limit, /* i : pitch gain limit */
  48. Word16 *gain, /* i/o: Pitch gain (unquant/quant), Q14 */
  49. Word16 gain_cand[], /* o : pitch gain candidates (3), MR795 only, Q14 */
  50. Word16 gain_cind[] /* o : pitch gain cand. indices (3),MR795 only, Q0 */
  51. )
  52. {
  53. Word16 i, index, err, err_min;
  54. err_min = abs_s_ex (sub_ex (*gain, qua_gain_pitch[0]));
  55. index = 0; move16 ();
  56. for (i = 1; i < NB_QUA_PITCH; i++)
  57. {
  58. test ();
  59. if (sub_ex (qua_gain_pitch[i], gp_limit) <= 0)
  60. {
  61. err = abs_s_ex (sub_ex (*gain, qua_gain_pitch[i]));
  62. test ();
  63. if (sub_ex (err, err_min) < 0)
  64. {
  65. err_min = err; move16 ();
  66. index = i; move16 ();
  67. }
  68. }
  69. }
  70. test ();
  71. if (sub_ex (mode, MR795) == 0)
  72. {
  73. /* in MR795 mode, compute three gain_pit candidates around the index
  74. * found in the quantization loop: the index found and the two direct
  75. * neighbours, except for the extreme cases (i=0 or i=NB_QUA_PITCH-1),
  76. * where the direct neighbour and the neighbour to that is used.
  77. */
  78. Word16 ii;
  79. test ();
  80. if (index == 0)
  81. {
  82. ii = index; move16 ();
  83. }
  84. else
  85. {
  86. test (); test ();
  87. if ( sub_ex (index, NB_QUA_PITCH-1) == 0
  88. || sub_ex (qua_gain_pitch[index+1], gp_limit) > 0)
  89. {
  90. ii = sub_ex (index, 2);
  91. }
  92. else
  93. {
  94. ii = sub_ex (index, 1);
  95. }
  96. }
  97. /* store candidate indices and values */
  98. for (i = 0; i < 3; i++)
  99. {
  100. gain_cind[i] = ii; move16 ();
  101. gain_cand[i] = qua_gain_pitch[ii]; move16 ();
  102. ii = add_ex (ii, 1);
  103. }
  104. *gain = qua_gain_pitch[index]; move16 ();
  105. }
  106. else
  107. {
  108. /* in MR122 mode, just return the index and gain pitch found.
  109. * If bitexactness is required, mask away the two LSBs (because
  110. * in the original EFR, gain_pit was scaled Q12)
  111. */
  112. test ();
  113. if (sub_ex(mode, MR122) == 0)
  114. {
  115. /* clear 2 LSBits */
  116. *gain = qua_gain_pitch[index] & 0xFFFC; logic16 (); move16 ();
  117. }
  118. else
  119. {
  120. *gain = qua_gain_pitch[index]; move16 ();
  121. }
  122. }
  123. return index;
  124. }