enc_lag3.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 : enc_lag3.c
  11. * Purpose : Encoding of fractional pitch lag with 1/3 resolution.
  12. *
  13. ********************************************************************************
  14. */
  15. /*
  16. ********************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. ********************************************************************************
  19. */
  20. #include "enc_lag3.h"
  21. const char enc_lag3_id[] = "@(#)$Id $" enc_lag3_h;
  22. /*
  23. ********************************************************************************
  24. * INCLUDE FILES
  25. ********************************************************************************
  26. */
  27. #include "typedef.h"
  28. #include "basic_op.h"
  29. #include "count.h"
  30. #include "cnst.h"
  31. /*
  32. ********************************************************************************
  33. * PUBLIC PROGRAM CODE
  34. ********************************************************************************
  35. */
  36. /*************************************************************************
  37. *
  38. * FUNCTION: Enc_lag3
  39. *
  40. * PURPOSE: Encoding of fractional pitch lag with 1/3 resolution.
  41. *
  42. * DESCRIPTION:
  43. * First and third subframes:
  44. * --------------------------
  45. * The pitch range is divided as follows:
  46. * 19 1/3 to 84 2/3 resolution 1/3
  47. * 85 to 143 resolution 1
  48. *
  49. * The period is encoded with 8 bits.
  50. * For the range with fractions:
  51. * index = (T-19)*3 + frac - 1;
  52. * where T=[19..85] and frac=[-1,0,1]
  53. * and for the integer only range
  54. * index = (T - 85) + 197; where T=[86..143]
  55. *
  56. * Second and fourth subframes:
  57. * ----------------------------
  58. * For the 2nd and 4th subframes a resolution of 1/3 is always used,
  59. * and the search range is relative to the lag in previous subframe.
  60. * If t0 is the lag in the previous subframe then
  61. * t_min=t0-5 and t_max=t0+4 and the range is given by
  62. * t_min - 2/3 to t_max + 2/3
  63. *
  64. * The period in the 2nd (and 4th) subframe is encoded with 5 bits:
  65. * index = (T-(t_min-1))*3 + frac - 1;
  66. * where T=[t_min-1..t_max+1]
  67. *
  68. *************************************************************************/
  69. Word16
  70. Enc_lag3( /* o : Return index of encoding */
  71. Word16 T0, /* i : Pitch delay */
  72. Word16 T0_frac, /* i : Fractional pitch delay */
  73. Word16 T0_prev, /* i : Integer pitch delay of last subframe */
  74. Word16 T0_min, /* i : minimum of search range */
  75. Word16 T0_max, /* i : maximum of search range */
  76. Word16 delta_flag, /* i : Flag for 1st (or 3rd) subframe */
  77. Word16 flag4 /* i : Flag for encoding with 4 bits */
  78. )
  79. {
  80. Word16 index, i, tmp_ind, uplag;
  81. Word16 tmp_lag;
  82. test ();
  83. if (delta_flag == 0)
  84. { /* if 1st or 3rd subframe */
  85. /* encode pitch delay (with fraction) */
  86. test ();
  87. if (sub_ex (T0, 85) <= 0)
  88. {
  89. /* index = T0*3 - 58 + T0_frac */
  90. i = add_ex (add_ex (T0, T0), T0);
  91. index = add_ex (sub_ex (i, 58), T0_frac);
  92. }
  93. else
  94. {
  95. index = add_ex (T0, 112);
  96. }
  97. }
  98. else
  99. { /* if second or fourth subframe */
  100. test ();
  101. if (flag4 == 0) {
  102. /* 'normal' encoding: either with 5 or 6 bit resolution */
  103. /* index = 3*(T0 - T0_min) + 2 + T0_frac */
  104. i = sub_ex (T0, T0_min);
  105. i = add_ex (add_ex (i, i), i);
  106. index = add_ex (add_ex (i, 2), T0_frac);
  107. }
  108. else {
  109. /* encoding with 4 bit resolution */
  110. tmp_lag = T0_prev; move16 ();
  111. test ();
  112. if ( sub_ex( sub_ex(tmp_lag, T0_min), 5) > 0)
  113. tmp_lag = add_ex (T0_min, 5);
  114. test ();
  115. if ( sub_ex( sub_ex(T0_max, tmp_lag), 4) > 0)
  116. tmp_lag = sub_ex (T0_max, 4);
  117. uplag = add_ex (add_ex (add_ex (T0, T0), T0), T0_frac);
  118. i = sub_ex (tmp_lag, 2);
  119. tmp_ind = add_ex (add_ex (i, i), i);
  120. test ();
  121. if (sub_ex (tmp_ind, uplag) >= 0) {
  122. index = add_ex (sub_ex (T0, tmp_lag), 5);
  123. }
  124. else {
  125. i = add_ex (tmp_lag, 1);
  126. i = add_ex (add_ex (i, i), i);
  127. test ();
  128. if (sub_ex (i, uplag) > 0) {
  129. index = add_ex ( sub_ex (uplag, tmp_ind), 3);
  130. }
  131. else {
  132. index = add_ex (sub_ex (T0, tmp_lag), 11);
  133. }
  134. }
  135. } /* end if (encoding with 4 bit resolution) */
  136. } /* end if (second of fourth subframe) */
  137. return index;
  138. }