Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.4 KiB

  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. #include "vq.h"
  28. /*Finds the index of the entry in a codebook that best matches the input*/
  29. int vq_index(float *in, float *codebook, int len, int entries)
  30. {
  31. int i,j;
  32. float min_dist=0;
  33. int best_index=0;
  34. for (i=0;i<entries;i++)
  35. {
  36. float dist=0;
  37. for (j=0;j<len;j++)
  38. {
  39. float tmp = in[j]-*codebook++;
  40. dist += tmp*tmp;
  41. }
  42. if (i==0 || dist<min_dist)
  43. {
  44. min_dist=dist;
  45. best_index=i;
  46. }
  47. }
  48. return best_index;
  49. }
  50. /*Finds the indices of the n-best entries in a codebook*/
  51. void vq_nbest(float *in, float *codebook, int len, int entries, float *E, int N, int *nbest, float *best_dist)
  52. {
  53. int i,j,k,used;
  54. used = 0;
  55. for (i=0;i<entries;i++)
  56. {
  57. float dist=.5*E[i];
  58. for (j=0;j<len;j++)
  59. dist -= in[j]**codebook++;
  60. if (i<N || dist<best_dist[N-1])
  61. {
  62. for (k=N-1; (k >= 1) && (k > used || dist < best_dist[k-1]); k--)
  63. {
  64. best_dist[k]=best_dist[k-1];
  65. nbest[k] = nbest[k-1];
  66. }
  67. best_dist[k]=dist;
  68. nbest[k]=i;
  69. used++;
  70. }
  71. }
  72. }
  73. /*Finds the indices of the n-best entries in a codebook with sign*/
  74. void vq_nbest_sign(float *in, float *codebook, int len, int entries, float *E, int N, int *nbest, float *best_dist)
  75. {
  76. int i,j,k, sign, used;
  77. used=0;
  78. for (i=0;i<entries;i++)
  79. {
  80. float dist=0;
  81. for (j=0;j<len;j++)
  82. dist -= in[j]**codebook++;
  83. if (dist>0)
  84. {
  85. sign=1;
  86. dist=-dist;
  87. } else
  88. {
  89. sign=0;
  90. }
  91. dist += .5*E[i];
  92. if (i<N || dist<best_dist[N-1])
  93. {
  94. for (k=N-1; (k >= 1) && (k > used || dist < best_dist[k-1]); k--)
  95. {
  96. best_dist[k]=best_dist[k-1];
  97. nbest[k] = nbest[k-1];
  98. }
  99. best_dist[k]=dist;
  100. nbest[k]=i;
  101. used++;
  102. if (sign)
  103. nbest[k]+=entries;
  104. }
  105. }
  106. }