Coverage Report

Created: 2025-12-10 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/speex/libspeex/vq.c
Line
Count
Source
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 "arch.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
#ifndef DISABLE_ENCODER
51
int scal_quant(spx_word16_t in, const spx_word16_t *boundary, int entries)
52
45.3k
{
53
45.3k
   int i=0;
54
268k
   while (i<entries-1 && in>boundary[0])
55
222k
   {
56
222k
      boundary++;
57
222k
      i++;
58
222k
   }
59
45.3k
   return i;
60
45.3k
}
61
62
int scal_quant32(spx_word32_t in, const spx_word32_t *boundary, int entries)
63
14.5k
{
64
14.5k
   int i=0;
65
276k
   while (i<entries-1 && in>boundary[0])
66
261k
   {
67
261k
      boundary++;
68
261k
      i++;
69
261k
   }
70
14.5k
   return i;
71
14.5k
}
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
480k
{
78
480k
   int i,j,k,used;
79
480k
   used = 0;
80
48.2M
   for (i=0;i<entries;i++)
81
47.7M
   {
82
47.7M
      spx_word32_t dist=0;
83
331M
      for (j=0;j<len;j++)
84
284M
         dist = MAC16_16(dist,in[j],*codebook++);
85
47.7M
#ifdef FIXED_POINT
86
47.7M
      dist=SUB32(SHR32(E[i],1),dist);
87
#else
88
      dist=.5f*E[i]-dist;
89
#endif
90
47.7M
      if (i<N || dist<best_dist[N-1])
91
7.52M
      {
92
23.9M
         for (k=N-1; (k >= 1) && (k > used || dist < best_dist[k-1]); k--)
93
16.4M
         {
94
16.4M
            best_dist[k]=best_dist[k-1];
95
16.4M
            nbest[k] = nbest[k-1];
96
16.4M
         }
97
7.52M
         best_dist[k]=dist;
98
7.52M
         nbest[k]=i;
99
7.52M
         used++;
100
7.52M
      }
101
47.7M
   }
102
480k
}
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
76.4k
{
112
76.4k
   int i,j,k, sign, used;
113
76.4k
   used=0;
114
9.86M
   for (i=0;i<entries;i++)
115
9.78M
   {
116
9.78M
      spx_word32_t dist=0;
117
88.0M
      for (j=0;j<len;j++)
118
78.2M
         dist = MAC16_16(dist,in[j],*codebook++);
119
9.78M
      if (dist>0)
120
4.08M
      {
121
4.08M
         sign=0;
122
4.08M
         dist=-dist;
123
4.08M
      } else
124
5.69M
      {
125
5.69M
         sign=1;
126
5.69M
      }
127
9.78M
#ifdef FIXED_POINT
128
9.78M
      dist = ADD32(dist,SHR32(E[i],1));
129
#else
130
      dist = ADD32(dist,.5f*E[i]);
131
#endif
132
9.78M
      if (i<N || dist<best_dist[N-1])
133
1.20M
      {
134
3.62M
         for (k=N-1; (k >= 1) && (k > used || dist < best_dist[k-1]); k--)
135
2.42M
         {
136
2.42M
            best_dist[k]=best_dist[k-1];
137
2.42M
            nbest[k] = nbest[k-1];
138
2.42M
         }
139
1.20M
         best_dist[k]=dist;
140
1.20M
         nbest[k]=i;
141
1.20M
         used++;
142
1.20M
         if (sign)
143
703k
            nbest[k]+=entries;
144
1.20M
      }
145
9.78M
   }
146
76.4k
}
147
#endif /* !defined(OVERRIDE_VQ_NBEST_SIGN) && !defined(DISABLE_WIDEBAND) && !defined(DISABLE_ENCODER) */