vq.c 4.29 KB
Newer Older
jmvalin's avatar
jmvalin committed
1
/* Copyright (C) 2002 Jean-Marc Valin
jmvalin's avatar
jmvalin committed
2 3 4
   File: vq.c
   Vector quantization

jm's avatar
jm committed
5 6 7
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
Tristan Matthews's avatar
Tristan Matthews committed
8

jm's avatar
jm committed
9 10
   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
Tristan Matthews's avatar
Tristan Matthews committed
11

jm's avatar
jm committed
12 13 14
   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
Tristan Matthews's avatar
Tristan Matthews committed
15

jm's avatar
jm committed
16 17 18
   - Neither the name of the Xiph.org Foundation nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.
Tristan Matthews's avatar
Tristan Matthews committed
19

jm's avatar
jm committed
20 21 22 23 24 25 26 27 28 29 30
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jmvalin's avatar
jmvalin committed
31 32
*/

33 34 35 36
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

jmvalin's avatar
jmvalin committed
37
#include "vq.h"
38
#include "stack_alloc.h"
jm's avatar
jm committed
39
#include "arch.h"
jm's avatar
jm committed
40 41 42

#ifdef _USE_SSE
#include <xmmintrin.h>
jm's avatar
jm committed
43
#include "vq_sse.h"
jm's avatar
jm committed
44 45
#elif defined(SHORTCUTS) && (defined(ARM4_ASM) || defined(ARM5E_ASM))
#include "vq_arm4.h"
46 47
#elif defined(BFIN_ASM)
#include "vq_bfin.h"
jm's avatar
jm committed
48 49
#endif

50
#ifndef DISABLE_ENCODER
jm's avatar
jm committed
51
int scal_quant(spx_word16_t in, const spx_word16_t *boundary, int entries)
52 53 54 55 56 57 58 59 60 61
{
   int i=0;
   while (i<entries-1 && in>boundary[0])
   {
      boundary++;
      i++;
   }
   return i;
}

jm's avatar
jm committed
62
int scal_quant32(spx_word32_t in, const spx_word32_t *boundary, int entries)
63 64 65 66 67 68 69 70 71
{
   int i=0;
   while (i<entries-1 && in>boundary[0])
   {
      boundary++;
      i++;
   }
   return i;
}
72
#endif /* DISABLE_ENCODER */
73

74
#if !defined(OVERRIDE_VQ_NBEST) && !defined(DISABLE_ENCODER)
75
/*Finds the indices of the n-best entries in a codebook*/
76
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)
77
{
78 79
   int i,j,k,used;
   used = 0;
80 81
   for (i=0;i<entries;i++)
   {
82 83 84
      spx_word32_t dist=0;
      for (j=0;j<len;j++)
         dist = MAC16_16(dist,in[j],*codebook++);
jm's avatar
jm committed
85
#ifdef FIXED_POINT
jm's avatar
jm committed
86
      dist=SUB32(SHR32(E[i],1),dist);
jm's avatar
jm committed
87
#else
88
      dist=.5f*E[i]-dist;
jm's avatar
jm committed
89
#endif
90 91
      if (i<N || dist<best_dist[N-1])
      {
92
         for (k=N-1; (k >= 1) && (k > used || dist < best_dist[k-1]); k--)
93
         {
94 95
            best_dist[k]=best_dist[k-1];
            nbest[k] = nbest[k-1];
96
         }
97 98 99
         best_dist[k]=dist;
         nbest[k]=i;
         used++;
100 101 102
      }
   }
}
103
#endif /* !defined(OVERRIDE_VQ_NBEST) && !defined(DISABLE_ENCODER) */
104

105 106 107



108
#if !defined(OVERRIDE_VQ_NBEST_SIGN) && !defined(DISABLE_WIDEBAND) && !defined(DISABLE_ENCODER)
109
/*Finds the indices of the n-best entries in a codebook with sign*/
110
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)
111
{
112 113
   int i,j,k, sign, used;
   used=0;
114 115
   for (i=0;i<entries;i++)
   {
116
      spx_word32_t dist=0;
117
      for (j=0;j<len;j++)
118
         dist = MAC16_16(dist,in[j],*codebook++);
119 120
      if (dist>0)
      {
121
         sign=0;
122 123 124
         dist=-dist;
      } else
      {
125
         sign=1;
126
      }
jm's avatar
jm committed
127
#ifdef FIXED_POINT
jm's avatar
jm committed
128
      dist = ADD32(dist,SHR32(E[i],1));
jm's avatar
jm committed
129
#else
130
      dist = ADD32(dist,.5f*E[i]);
jm's avatar
jm committed
131
#endif
132 133
      if (i<N || dist<best_dist[N-1])
      {
134
         for (k=N-1; (k >= 1) && (k > used || dist < best_dist[k-1]); k--)
135
         {
136 137
            best_dist[k]=best_dist[k-1];
            nbest[k] = nbest[k-1];
138
         }
139 140 141 142 143
         best_dist[k]=dist;
         nbest[k]=i;
         used++;
         if (sign)
            nbest[k]+=entries;
144 145 146
      }
   }
}
147
#endif /* !defined(OVERRIDE_VQ_NBEST_SIGN) && !defined(DISABLE_WIDEBAND) && !defined(DISABLE_ENCODER) */