enc_lag6.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_lag6.c
  11. * Purpose : Encoding of fractional pitch lag with 1/6 resolution.
  12. *
  13. ********************************************************************************
  14. */
  15. /*
  16. ********************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. ********************************************************************************
  19. */
  20. #include "enc_lag6.h"
  21. const char enc_lag6_id[] = "@(#)$Id $" enc_lag6_h;
  22. /*
  23. ********************************************************************************
  24. * INCLUDE FILES
  25. ********************************************************************************
  26. */
  27. #include "typedef.h"
  28. #include "basic_op.h"
  29. #include "count.h"
  30. /*
  31. ********************************************************************************
  32. * LOCAL VARIABLES AND TABLES
  33. ********************************************************************************
  34. */
  35. /*
  36. ********************************************************************************
  37. * PUBLIC PROGRAM CODE
  38. ********************************************************************************
  39. */
  40. /*************************************************************************
  41. *
  42. * FUNCTION: Enc_lag6
  43. *
  44. * PURPOSE: Encoding of fractional pitch lag with 1/6 resolution.
  45. *
  46. * DESCRIPTION:
  47. * First and third subframes:
  48. * --------------------------
  49. * The pitch range is divided as follows:
  50. * 17 3/6 to 94 3/6 resolution 1/6
  51. * 95 to 143 resolution 1
  52. *
  53. * The period is encoded with 9 bits.
  54. * For the range with fractions:
  55. * index = (T-17)*6 + frac - 3;
  56. * where T=[17..94] and frac=[-2,-1,0,1,2,3]
  57. * and for the integer only range
  58. * index = (T - 95) + 463; where T=[95..143]
  59. *
  60. * Second and fourth subframes:
  61. * ----------------------------
  62. * For the 2nd and 4th subframes a resolution of 1/6 is always used,
  63. * and the search range is relative to the lag in previous subframe.
  64. * If t0 is the lag in the previous subframe then
  65. * t_min=t0-5 and t_max=t0+4 and the range is given by
  66. * (t_min-1) 3/6 to (t_max) 3/6
  67. *
  68. * The period in the 2nd (and 4th) subframe is encoded with 6 bits:
  69. * index = (T-(t_min-1))*6 + frac - 3;
  70. * where T=[t_min-1..t_max] and frac=[-2,-1,0,1,2,3]
  71. *
  72. * Note that only 61 values are used. If the decoder receives 61, 62,
  73. * or 63 as the relative pitch index, it means that a transmission
  74. * error occurred and the pitch from previous subframe should be used.
  75. *
  76. *************************************************************************/
  77. Word16 Enc_lag6 ( /* o : Return index of encoding */
  78. Word16 T0, /* i : Pitch delay */
  79. Word16 T0_frac, /* i : Fractional pitch delay */
  80. Word16 T0_min, /* i : minimum of search range */
  81. Word16 delta_flag /* i : Flag for 1st (or 3rd) subframe */
  82. )
  83. {
  84. Word16 index, i;
  85. test ();
  86. if (delta_flag == 0) /* if 1st or 3rd subframe */
  87. {
  88. /* encode pitch delay (with fraction) */
  89. test ();
  90. if (sub_ex (T0, 94) <= 0)
  91. {
  92. /* index = T0*6 - 105 + T0_frac */
  93. i = add_ex (add_ex (T0, T0), T0);
  94. index = add_ex (sub_ex (add_ex (i, i), 105), T0_frac);
  95. }
  96. else
  97. {
  98. index = add_ex (T0, 368);
  99. }
  100. }
  101. else
  102. /* if second or fourth subframe */
  103. {
  104. /* index = 6*(T0-T0_min) + 3 + T0_frac */
  105. i = sub_ex (T0, T0_min);
  106. i = add_ex (add_ex (i, i), i);
  107. index = add_ex (add_ex (add_ex (i, i), 3), T0_frac);
  108. }
  109. return index;
  110. }