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