vq.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Copyright (C) 2002 Jean-Marc Valin
  2. File: vq.c
  3. Vector quantization
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. - Neither the name of the Xiph.org Foundation nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "vq.h"
  31. #include "stack_alloc.h"
  32. #include "arch.h"
  33. #ifdef _USE_SSE
  34. #include <xmmintrin.h>
  35. #include "vq_sse.h"
  36. #elif defined(SHORTCUTS) && (defined(ARM4_ASM) || defined(ARM5E_ASM))
  37. #include "vq_arm4.h"
  38. #elif defined(BFIN_ASM)
  39. #include "vq_bfin.h"
  40. #endif
  41. #ifndef DISABLE_ENCODER
  42. int scal_quant(spx_word16_t in, const spx_word16_t *boundary, int entries)
  43. {
  44. int i=0;
  45. while (i<entries-1 && in>boundary[0])
  46. {
  47. boundary++;
  48. i++;
  49. }
  50. return i;
  51. }
  52. int scal_quant32(spx_word32_t in, const spx_word32_t *boundary, int entries)
  53. {
  54. int i=0;
  55. while (i<entries-1 && in>boundary[0])
  56. {
  57. boundary++;
  58. i++;
  59. }
  60. return i;
  61. }
  62. #endif /* DISABLE_ENCODER */
  63. #if !defined(OVERRIDE_VQ_NBEST) && !defined(DISABLE_ENCODER)
  64. /*Finds the indices of the n-best entries in a codebook*/
  65. void vq_nbest(spx_word16_t *in, const spx_word16_t *codebook, int len, int entries, spx_word32_t *E, int N, int *nbest, spx_word32_t *best_dist, char *stack)
  66. {
  67. int i,j,k,used;
  68. used = 0;
  69. for (i=0;i<entries;i++)
  70. {
  71. spx_word32_t dist=0;
  72. for (j=0;j<len;j++)
  73. dist = MAC16_16(dist,in[j],*codebook++);
  74. #ifdef FIXED_POINT
  75. dist=SUB32(SHR32(E[i],1),dist);
  76. #else
  77. dist=.5f*E[i]-dist;
  78. #endif
  79. if (i<N || dist<best_dist[N-1])
  80. {
  81. for (k=N-1; (k >= 1) && (k > used || dist < best_dist[k-1]); k--)
  82. {
  83. best_dist[k]=best_dist[k-1];
  84. nbest[k] = nbest[k-1];
  85. }
  86. best_dist[k]=dist;
  87. nbest[k]=i;
  88. used++;
  89. }
  90. }
  91. }
  92. #endif /* !defined(OVERRIDE_VQ_NBEST) && !defined(DISABLE_ENCODER) */
  93. #if !defined(OVERRIDE_VQ_NBEST_SIGN) && !defined(DISABLE_WIDEBAND) && !defined(DISABLE_ENCODER)
  94. /*Finds the indices of the n-best entries in a codebook with sign*/
  95. void vq_nbest_sign(spx_word16_t *in, const spx_word16_t *codebook, int len, int entries, spx_word32_t *E, int N, int *nbest, spx_word32_t *best_dist, char *stack)
  96. {
  97. int i,j,k, sign, used;
  98. used=0;
  99. for (i=0;i<entries;i++)
  100. {
  101. spx_word32_t dist=0;
  102. for (j=0;j<len;j++)
  103. dist = MAC16_16(dist,in[j],*codebook++);
  104. if (dist>0)
  105. {
  106. sign=0;
  107. dist=-dist;
  108. } else
  109. {
  110. sign=1;
  111. }
  112. #ifdef FIXED_POINT
  113. dist = ADD32(dist,SHR32(E[i],1));
  114. #else
  115. dist = ADD32(dist,.5f*E[i]);
  116. #endif
  117. if (i<N || dist<best_dist[N-1])
  118. {
  119. for (k=N-1; (k >= 1) && (k > used || dist < best_dist[k-1]); k--)
  120. {
  121. best_dist[k]=best_dist[k-1];
  122. nbest[k] = nbest[k-1];
  123. }
  124. best_dist[k]=dist;
  125. nbest[k]=i;
  126. used++;
  127. if (sign)
  128. nbest[k]+=entries;
  129. }
  130. }
  131. }
  132. #endif /* !defined(OVERRIDE_VQ_NBEST_SIGN) && !defined(DISABLE_WIDEBAND) && !defined(DISABLE_ENCODER) */