cbsearch.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 : cbsearch.c
  11. * Purpose : Inovative codebook search (find index and gain)
  12. *
  13. *****************************************************************************
  14. */
  15. /*
  16. *****************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. *****************************************************************************
  19. */
  20. #include "cbsearch.h"
  21. const char cbsearch_id[] = "@(#)$Id $" cbsearch_h;
  22. /*
  23. *****************************************************************************
  24. * INCLUDE FILES
  25. *****************************************************************************
  26. */
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include "typedef.h"
  30. #include "c2_9pf.h"
  31. #include "c2_11pf.h"
  32. #include "c3_14pf.h"
  33. #include "c4_17pf.h"
  34. #include "c8_31pf.h"
  35. #include "c1035pf.h"
  36. #include "mode.h"
  37. #include "basic_op.h"
  38. #include "count.h"
  39. #include "cnst.h"
  40. /*
  41. *****************************************************************************
  42. * PUBLIC PROGRAM CODE
  43. *****************************************************************************
  44. */
  45. int cbsearch(Word16 x[], /* i : target vector, Q0 */
  46. Word16 h[], /* i : impulse response of weighted synthesis */
  47. /* filter h[-L_subfr..-1] must be set to */
  48. /* zero. Q12 */
  49. Word16 T0, /* i : Pitch lag */
  50. Word16 pitch_sharp, /* i : Last quantized pitch gain, Q14 */
  51. Word16 gain_pit, /* i : Pitch gain, Q14 */
  52. Word16 res2[], /* i : Long term prediction residual, Q0 */
  53. Word16 code[], /* o : Innovative codebook, Q13 */
  54. Word16 y[], /* o : filtered fixed codebook excitation, Q12 */
  55. Word16 **anap, /* o : Signs of the pulses */
  56. enum Mode mode,/* i : coder mode */
  57. Word16 subNr) /* i : subframe number */
  58. {
  59. Word16 index;
  60. Word16 i, temp, pit_sharpTmp;
  61. /* For MR74, the pre and post CB pitch sharpening is included in the
  62. * codebook search routine, while for MR122 is it not.
  63. */
  64. test (); test ();
  65. if ((sub_ex (mode, MR475) == 0) || (sub_ex (mode, MR515) == 0))
  66. { /* MR475, MR515 */ move16 ();
  67. *(*anap)++ = code_2i40_9bits(subNr, x, h, T0, pitch_sharp,
  68. code, y, &index);
  69. *(*anap)++ = index; /* sign index */ move16 ();
  70. }
  71. else if (sub_ex (mode, MR59) == 0)
  72. { /* MR59 */
  73. test (); move16 ();
  74. *(*anap)++ = code_2i40_11bits(x, h, T0, pitch_sharp, code, y, &index);
  75. *(*anap)++ = index; /* sign index */ move16 ();
  76. }
  77. else if (sub_ex (mode, MR67) == 0)
  78. { /* MR67 */
  79. test (); test (); move16 ();
  80. *(*anap)++ = code_3i40_14bits(x, h, T0, pitch_sharp, code, y, &index);
  81. *(*anap)++ = index; /* sign index */ move16 ();
  82. }
  83. else if (sub_ex (mode, MR74) == 0 || sub_ex (mode, MR795) == 0)
  84. { /* MR74, MR795 */
  85. test (); test (); test (); move16 ();
  86. *(*anap)++ = code_4i40_17bits(x, h, T0, pitch_sharp, code, y, &index);
  87. *(*anap)++ = index; /* sign index */ move16 ();
  88. }
  89. else if (sub_ex (mode, MR102) == 0)
  90. { /* MR102 */
  91. test (); test (); test ();
  92. /*-------------------------------------------------------------*
  93. * - include pitch contribution into impulse resp. h1[] *
  94. *-------------------------------------------------------------*/
  95. /* pit_sharpTmp = pit_sharp; */
  96. /* if (pit_sharpTmp > 1.0) pit_sharpTmp = 1.0; */
  97. pit_sharpTmp = shl_ex (pitch_sharp, 1);
  98. for (i = T0; i < L_SUBFR; i++)
  99. {
  100. temp = mult_ex(h[i - T0], pit_sharpTmp);
  101. h[i] = add_ex(h[i], temp); move16 ();
  102. }
  103. /*--------------------------------------------------------------*
  104. * - Innovative codebook search (find index and gain) *
  105. *--------------------------------------------------------------*/
  106. code_8i40_31bits (x, res2, h, code, y, *anap);
  107. *anap += 7; add_ex(0,0);
  108. /*-------------------------------------------------------*
  109. * - Add the pitch contribution to code[]. *
  110. *-------------------------------------------------------*/
  111. for (i = T0; i < L_SUBFR; i++)
  112. {
  113. temp = mult_ex (code[i - T0], pit_sharpTmp);
  114. code[i] = add_ex (code[i], temp); move16 ();
  115. }
  116. }
  117. else
  118. { /* MR122 */
  119. /*-------------------------------------------------------------*
  120. * - include pitch contribution into impulse resp. h1[] *
  121. *-------------------------------------------------------------*/
  122. test (); test (); test ();
  123. /* pit_sharpTmp = gain_pit; */
  124. /* if (pit_sharpTmp > 1.0) pit_sharpTmp = 1.0; */
  125. pit_sharpTmp = shl_ex (gain_pit, 1);
  126. for (i = T0; i < L_SUBFR; i++)
  127. {
  128. temp = mult_ex(h[i - T0], pit_sharpTmp);
  129. h[i] = add_ex(h[i], temp); move16 ();
  130. }
  131. /*--------------------------------------------------------------*
  132. * - Innovative codebook search (find index and gain) *
  133. *--------------------------------------------------------------*/
  134. code_10i40_35bits (x, res2, h, code, y, *anap);
  135. *anap += 10; add_ex(0,0);
  136. /*-------------------------------------------------------*
  137. * - Add the pitch contribution to code[]. *
  138. *-------------------------------------------------------*/
  139. for (i = T0; i < L_SUBFR; i++)
  140. {
  141. temp = mult_ex (code[i - T0], pit_sharpTmp);
  142. code[i] = add_ex (code[i], temp); move16 ();
  143. }
  144. }
  145. return 0;
  146. }