Coverage Report

Created: 2024-03-26 07:23

/src/opus/celt/vq.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (c) 2007-2008 CSIRO
2
   Copyright (c) 2007-2009 Xiph.Org Foundation
3
   Written by Jean-Marc Valin */
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
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20
   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
*/
28
29
#ifdef HAVE_CONFIG_H
30
#include "config.h"
31
#endif
32
33
#include "mathops.h"
34
#include "cwrs.h"
35
#include "vq.h"
36
#include "arch.h"
37
#include "os_support.h"
38
#include "bands.h"
39
#include "rate.h"
40
#include "pitch.h"
41
42
#if defined(MIPSr1_ASM)
43
#include "mips/vq_mipsr1.h"
44
#endif
45
46
#ifndef OVERRIDE_vq_exp_rotation1
47
static void exp_rotation1(celt_norm *X, int len, int stride, opus_val16 c, opus_val16 s)
48
40.4M
{
49
40.4M
   int i;
50
40.4M
   opus_val16 ms;
51
40.4M
   celt_norm *Xptr;
52
40.4M
   Xptr = X;
53
40.4M
   ms = NEG16(s);
54
470M
   for (i=0;i<len-stride;i++)
55
430M
   {
56
430M
      celt_norm x1, x2;
57
430M
      x1 = Xptr[0];
58
430M
      x2 = Xptr[stride];
59
430M
      Xptr[stride] = EXTRACT16(PSHR32(MAC16_16(MULT16_16(c, x2),  s, x1), 15));
60
430M
      *Xptr++      = EXTRACT16(PSHR32(MAC16_16(MULT16_16(c, x1), ms, x2), 15));
61
430M
   }
62
40.4M
   Xptr = &X[len-2*stride-1];
63
393M
   for (i=len-2*stride-1;i>=0;i--)
64
352M
   {
65
352M
      celt_norm x1, x2;
66
352M
      x1 = Xptr[0];
67
352M
      x2 = Xptr[stride];
68
352M
      Xptr[stride] = EXTRACT16(PSHR32(MAC16_16(MULT16_16(c, x2),  s, x1), 15));
69
352M
      *Xptr--      = EXTRACT16(PSHR32(MAC16_16(MULT16_16(c, x1), ms, x2), 15));
70
352M
   }
71
40.4M
}
72
#endif /* OVERRIDE_vq_exp_rotation1 */
73
74
void exp_rotation(celt_norm *X, int len, int dir, int stride, int K, int spread)
75
119M
{
76
119M
   static const int SPREAD_FACTOR[3]={15,10,5};
77
119M
   int i;
78
119M
   opus_val16 c, s;
79
119M
   opus_val16 gain, theta;
80
119M
   int stride2=0;
81
119M
   int factor;
82
83
119M
   if (2*K>=len || spread==SPREAD_NONE)
84
104M
      return;
85
14.8M
   factor = SPREAD_FACTOR[spread-1];
86
87
14.8M
   gain = celt_div((opus_val32)MULT16_16(Q15_ONE,len),(opus_val32)(len+factor*K));
88
14.8M
   theta = HALF16(MULT16_16_Q15(gain,gain));
89
90
14.8M
   c = celt_cos_norm(EXTEND32(theta));
91
14.8M
   s = celt_cos_norm(EXTEND32(SUB16(Q15ONE,theta))); /*  sin(theta) */
92
93
14.8M
   if (len>=8*stride)
94
11.9M
   {
95
11.9M
      stride2 = 1;
96
      /* This is just a simple (equivalent) way of computing sqrt(len/stride) with rounding.
97
         It's basically incrementing long as (stride2+0.5)^2 < len/stride. */
98
50.1M
      while ((stride2*stride2+stride2)*stride + (stride>>2) < len)
99
38.2M
         stride2++;
100
11.9M
   }
101
   /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for
102
      extract_collapse_mask().*/
103
14.8M
   len = celt_udiv(len, stride);
104
42.1M
   for (i=0;i<stride;i++)
105
27.2M
   {
106
27.2M
      if (dir < 0)
107
3.65M
      {
108
3.65M
         if (stride2)
109
1.19M
            exp_rotation1(X+i*len, len, stride2, s, c);
110
3.65M
         exp_rotation1(X+i*len, len, 1, c, s);
111
23.6M
      } else {
112
23.6M
         exp_rotation1(X+i*len, len, 1, c, -s);
113
23.6M
         if (stride2)
114
11.9M
            exp_rotation1(X+i*len, len, stride2, s, -c);
115
23.6M
      }
116
27.2M
   }
117
14.8M
}
118
119
/** Takes the pitch vector and the decoded residual vector, computes the gain
120
    that will give ||p+g*y||=1 and mixes the residual with the pitch. */
121
static void normalise_residual(int * OPUS_RESTRICT iy, celt_norm * OPUS_RESTRICT X,
122
      int N, opus_val32 Ryy, opus_val16 gain)
123
13.3M
{
124
13.3M
   int i;
125
13.3M
#ifdef FIXED_POINT
126
13.3M
   int k;
127
13.3M
#endif
128
13.3M
   opus_val32 t;
129
13.3M
   opus_val16 g;
130
131
13.3M
#ifdef FIXED_POINT
132
13.3M
   k = celt_ilog2(Ryy)>>1;
133
13.3M
#endif
134
13.3M
   t = VSHR32(Ryy, 2*(k-7));
135
13.3M
   g = MULT16_16_P15(celt_rsqrt_norm(t),gain);
136
137
13.3M
   i=0;
138
13.3M
   do
139
80.2M
      X[i] = EXTRACT16(PSHR32(MULT16_16(g, iy[i]), k+1));
140
80.2M
   while (++i < N);
141
13.3M
}
142
143
static unsigned extract_collapse_mask(int *iy, int N, int B)
144
106M
{
145
106M
   unsigned collapse_mask;
146
106M
   int N0;
147
106M
   int i;
148
106M
   if (B<=1)
149
90.4M
      return 1;
150
   /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for
151
      exp_rotation().*/
152
15.8M
   N0 = celt_udiv(N, B);
153
15.8M
   collapse_mask = 0;
154
53.3M
   i=0; do {
155
53.3M
      int j;
156
53.3M
      unsigned tmp=0;
157
129M
      j=0; do {
158
129M
         tmp |= iy[i*N0+j];
159
129M
      } while (++j<N0);
160
53.3M
      collapse_mask |= (tmp!=0)<<i;
161
53.3M
   } while (++i<B);
162
15.8M
   return collapse_mask;
163
106M
}
164
165
opus_val16 op_pvq_search_c(celt_norm *X, int *iy, int K, int N, int arch)
166
106M
{
167
106M
   VARDECL(celt_norm, y);
168
106M
   VARDECL(int, signx);
169
106M
   int i, j;
170
106M
   int pulsesLeft;
171
106M
   opus_val32 sum;
172
106M
   opus_val32 xy;
173
106M
   opus_val16 yy;
174
106M
   SAVE_STACK;
175
176
106M
   (void)arch;
177
106M
   ALLOC(y, N, celt_norm);
178
106M
   ALLOC(signx, N, int);
179
180
   /* Get rid of the sign */
181
106M
   sum = 0;
182
781M
   j=0; do {
183
781M
      signx[j] = X[j]<0;
184
      /* OPT: Make sure the compiler doesn't use a branch on ABS16(). */
185
781M
      X[j] = ABS16(X[j]);
186
781M
      iy[j] = 0;
187
781M
      y[j] = 0;
188
781M
   } while (++j<N);
189
190
106M
   xy = yy = 0;
191
192
106M
   pulsesLeft = K;
193
194
   /* Do a pre-search by projecting on the pyramid */
195
106M
   if (K > (N>>1))
196
85.4M
   {
197
85.4M
      opus_val16 rcp;
198
458M
      j=0; do {
199
458M
         sum += X[j];
200
458M
      }  while (++j<N);
201
202
      /* If X is too small, just replace it with a pulse at 0 */
203
85.4M
#ifdef FIXED_POINT
204
85.4M
      if (sum <= K)
205
#else
206
      /* Prevents infinities and NaNs from causing too many pulses
207
         to be allocated. 64 is an approximation of infinity here. */
208
      if (!(sum > EPSILON && sum < 64))
209
#endif
210
8.62M
      {
211
8.62M
         X[0] = QCONST16(1.f,14);
212
8.62M
         j=1; do
213
62.5M
            X[j]=0;
214
62.5M
         while (++j<N);
215
8.62M
         sum = QCONST16(1.f,14);
216
8.62M
      }
217
85.4M
#ifdef FIXED_POINT
218
85.4M
      rcp = EXTRACT16(MULT16_32_Q16(K, celt_rcp(sum)));
219
#else
220
      /* Using K+e with e < 1 guarantees we cannot get more than K pulses. */
221
      rcp = EXTRACT16(MULT16_32_Q16(K+0.8f, celt_rcp(sum)));
222
#endif
223
458M
      j=0; do {
224
458M
#ifdef FIXED_POINT
225
         /* It's really important to round *towards zero* here */
226
458M
         iy[j] = MULT16_16_Q15(X[j],rcp);
227
#else
228
         iy[j] = (int)floor(rcp*X[j]);
229
#endif
230
458M
         y[j] = (celt_norm)iy[j];
231
458M
         yy = MAC16_16(yy, y[j],y[j]);
232
458M
         xy = MAC16_16(xy, X[j],y[j]);
233
458M
         y[j] *= 2;
234
458M
         pulsesLeft -= iy[j];
235
458M
      }  while (++j<N);
236
85.4M
   }
237
106M
   celt_sig_assert(pulsesLeft>=0);
238
239
   /* This should never happen, but just in case it does (e.g. on silence)
240
      we fill the first bin with pulses. */
241
#ifdef FIXED_POINT_DEBUG
242
   celt_sig_assert(pulsesLeft<=N+3);
243
#endif
244
106M
   if (pulsesLeft > N+3)
245
0
   {
246
0
      opus_val16 tmp = (opus_val16)pulsesLeft;
247
0
      yy = MAC16_16(yy, tmp, tmp);
248
0
      yy = MAC16_16(yy, tmp, y[0]);
249
0
      iy[0] += pulsesLeft;
250
0
      pulsesLeft=0;
251
0
   }
252
253
399M
   for (i=0;i<pulsesLeft;i++)
254
292M
   {
255
292M
      opus_val16 Rxy, Ryy;
256
292M
      int best_id;
257
292M
      opus_val32 best_num;
258
292M
      opus_val16 best_den;
259
292M
#ifdef FIXED_POINT
260
292M
      int rshift;
261
292M
#endif
262
292M
#ifdef FIXED_POINT
263
292M
      rshift = 1+celt_ilog2(K-pulsesLeft+i+1);
264
292M
#endif
265
292M
      best_id = 0;
266
      /* The squared magnitude term gets added anyway, so we might as well
267
         add it outside the loop */
268
292M
      yy = ADD16(yy, 1);
269
270
      /* Calculations for position 0 are out of the loop, in part to reduce
271
         mispredicted branches (since the if condition is usually false)
272
         in the loop. */
273
      /* Temporary sums of the new pulse(s) */
274
292M
      Rxy = EXTRACT16(SHR32(ADD32(xy, EXTEND32(X[0])),rshift));
275
      /* We're multiplying y[j] by two so we don't have to do it here */
276
292M
      Ryy = ADD16(yy, y[0]);
277
278
      /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that
279
         Rxy is positive because the sign is pre-computed) */
280
292M
      Rxy = MULT16_16_Q15(Rxy,Rxy);
281
292M
      best_den = Ryy;
282
292M
      best_num = Rxy;
283
292M
      j=1;
284
2.70G
      do {
285
         /* Temporary sums of the new pulse(s) */
286
2.70G
         Rxy = EXTRACT16(SHR32(ADD32(xy, EXTEND32(X[j])),rshift));
287
         /* We're multiplying y[j] by two so we don't have to do it here */
288
2.70G
         Ryy = ADD16(yy, y[j]);
289
290
         /* Approximate score: we maximise Rxy/sqrt(Ryy) (we're guaranteed that
291
            Rxy is positive because the sign is pre-computed) */
292
2.70G
         Rxy = MULT16_16_Q15(Rxy,Rxy);
293
         /* The idea is to check for num/den >= best_num/best_den, but that way
294
            we can do it without any division */
295
         /* OPT: It's not clear whether a cmov is faster than a branch here
296
            since the condition is more often false than true and using
297
            a cmov introduces data dependencies across iterations. The optimal
298
            choice may be architecture-dependent. */
299
2.70G
         if (opus_unlikely(MULT16_16(best_den, Rxy) > MULT16_16(Ryy, best_num)))
300
448M
         {
301
448M
            best_den = Ryy;
302
448M
            best_num = Rxy;
303
448M
            best_id = j;
304
448M
         }
305
2.70G
      } while (++j<N);
306
307
      /* Updating the sums of the new pulse(s) */
308
292M
      xy = ADD32(xy, EXTEND32(X[best_id]));
309
      /* We're multiplying y[j] by two so we don't have to do it here */
310
292M
      yy = ADD16(yy, y[best_id]);
311
312
      /* Only now that we've made the final choice, update y/iy */
313
      /* Multiplying y[j] by 2 so we don't have to do it everywhere else */
314
292M
      y[best_id] += 2;
315
292M
      iy[best_id]++;
316
292M
   }
317
318
   /* Put the original sign back */
319
106M
   j=0;
320
781M
   do {
321
      /*iy[j] = signx[j] ? -iy[j] : iy[j];*/
322
      /* OPT: The is more likely to be compiled without a branch than the code above
323
         but has the same performance otherwise. */
324
781M
      iy[j] = (iy[j]^-signx[j]) + signx[j];
325
781M
   } while (++j<N);
326
106M
   RESTORE_STACK;
327
106M
   return yy;
328
106M
}
329
330
unsigned alg_quant(celt_norm *X, int N, int K, int spread, int B, ec_enc *enc,
331
      opus_val16 gain, int resynth, int arch)
332
106M
{
333
106M
   VARDECL(int, iy);
334
106M
   opus_val16 yy;
335
106M
   unsigned collapse_mask;
336
106M
   SAVE_STACK;
337
338
106M
   celt_assert2(K>0, "alg_quant() needs at least one pulse");
339
106M
   celt_assert2(N>1, "alg_quant() needs at least two dimensions");
340
341
   /* Covers vectorization by up to 4. */
342
106M
   ALLOC(iy, N+3, int);
343
344
106M
   exp_rotation(X, N, 1, B, K, spread);
345
346
106M
   yy = op_pvq_search(X, iy, K, N, arch);
347
348
106M
   encode_pulses(iy, N, K, enc);
349
350
106M
   if (resynth)
351
13.3M
   {
352
13.3M
      normalise_residual(iy, X, N, yy, gain);
353
13.3M
      exp_rotation(X, N, -1, B, K, spread);
354
13.3M
   }
355
356
106M
   collapse_mask = extract_collapse_mask(iy, N, B);
357
106M
   RESTORE_STACK;
358
106M
   return collapse_mask;
359
106M
}
360
361
/** Decode pulse vector and combine the result with the pitch vector to produce
362
    the final normalised signal in the current band. */
363
unsigned alg_unquant(celt_norm *X, int N, int K, int spread, int B,
364
      ec_dec *dec, opus_val16 gain)
365
0
{
366
0
   opus_val32 Ryy;
367
0
   unsigned collapse_mask;
368
0
   VARDECL(int, iy);
369
0
   SAVE_STACK;
370
371
0
   celt_assert2(K>0, "alg_unquant() needs at least one pulse");
372
0
   celt_assert2(N>1, "alg_unquant() needs at least two dimensions");
373
0
   ALLOC(iy, N, int);
374
0
   Ryy = decode_pulses(iy, N, K, dec);
375
0
   normalise_residual(iy, X, N, Ryy, gain);
376
0
   exp_rotation(X, N, -1, B, K, spread);
377
0
   collapse_mask = extract_collapse_mask(iy, N, B);
378
0
   RESTORE_STACK;
379
0
   return collapse_mask;
380
0
}
381
382
#ifndef OVERRIDE_renormalise_vector
383
void renormalise_vector(celt_norm *X, int N, opus_val16 gain, int arch)
384
108M
{
385
108M
   int i;
386
108M
#ifdef FIXED_POINT
387
108M
   int k;
388
108M
#endif
389
108M
   opus_val32 E;
390
108M
   opus_val16 g;
391
108M
   opus_val32 t;
392
108M
   celt_norm *xptr;
393
108M
   E = EPSILON + celt_inner_prod(X, X, N, arch);
394
108M
#ifdef FIXED_POINT
395
108M
   k = celt_ilog2(E)>>1;
396
108M
#endif
397
108M
   t = VSHR32(E, 2*(k-7));
398
108M
   g = MULT16_16_P15(celt_rsqrt_norm(t),gain);
399
400
108M
   xptr = X;
401
993M
   for (i=0;i<N;i++)
402
885M
   {
403
885M
      *xptr = EXTRACT16(PSHR32(MULT16_16(g, *xptr), k+1));
404
885M
      xptr++;
405
885M
   }
406
   /*return celt_sqrt(E);*/
407
108M
}
408
#endif /* OVERRIDE_renormalise_vector */
409
410
int stereo_itheta(const celt_norm *X, const celt_norm *Y, int stereo, int N, int arch)
411
235M
{
412
235M
   int i;
413
235M
   int itheta;
414
235M
   opus_val16 mid, side;
415
235M
   opus_val32 Emid, Eside;
416
417
235M
   Emid = Eside = EPSILON;
418
235M
   if (stereo)
419
192M
   {
420
1.75G
      for (i=0;i<N;i++)
421
1.56G
      {
422
1.56G
         celt_norm m, s;
423
1.56G
         m = ADD16(SHR16(X[i],1),SHR16(Y[i],1));
424
1.56G
         s = SUB16(SHR16(X[i],1),SHR16(Y[i],1));
425
1.56G
         Emid = MAC16_16(Emid, m, m);
426
1.56G
         Eside = MAC16_16(Eside, s, s);
427
1.56G
      }
428
192M
   } else {
429
43.0M
      Emid += celt_inner_prod(X, X, N, arch);
430
43.0M
      Eside += celt_inner_prod(Y, Y, N, arch);
431
43.0M
   }
432
235M
   mid = celt_sqrt(Emid);
433
235M
   side = celt_sqrt(Eside);
434
235M
#ifdef FIXED_POINT
435
   /* 0.63662 = 2/pi */
436
235M
   itheta = MULT16_16_Q15(QCONST16(0.63662f,15),celt_atan2p(side, mid));
437
#else
438
   itheta = (int)floor(.5f+16384*0.63662f*fast_atan2f(side,mid));
439
#endif
440
441
235M
   return itheta;
442
235M
}