Coverage Report

Created: 2026-08-13 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/celt/pitch.c
Line
Count
Source
1
/* Copyright (c) 2007-2008 CSIRO
2
   Copyright (c) 2007-2009 Xiph.Org Foundation
3
   Written by Jean-Marc Valin */
4
/**
5
   @file pitch.c
6
   @brief Pitch analysis
7
 */
8
9
/*
10
   Redistribution and use in source and binary forms, with or without
11
   modification, are permitted provided that the following conditions
12
   are met:
13
14
   - Redistributions of source code must retain the above copyright
15
   notice, this list of conditions and the following disclaimer.
16
17
   - Redistributions in binary form must reproduce the above copyright
18
   notice, this list of conditions and the following disclaimer in the
19
   documentation and/or other materials provided with the distribution.
20
21
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25
   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
*/
33
34
#ifdef HAVE_CONFIG_H
35
#include "config.h"
36
#endif
37
38
#include "pitch.h"
39
#include "os_support.h"
40
#include "modes.h"
41
#include "stack_alloc.h"
42
#include "mathops.h"
43
#include "celt_lpc.h"
44
45
static void find_best_pitch(opus_val32 *xcorr, opus_val16 *y, int len,
46
                            int max_pitch, int *best_pitch
47
#ifdef FIXED_POINT
48
                            , int yshift, opus_val32 maxcorr
49
#endif
50
                            )
51
9.85M
{
52
9.85M
   int i, j;
53
9.85M
   opus_val32 Syy=1;
54
9.85M
   opus_val16 best_num[2];
55
9.85M
   opus_val32 best_den[2];
56
#ifdef FIXED_POINT
57
   int xshift;
58
59
   xshift = celt_ilog2(maxcorr)-14;
60
#endif
61
62
9.85M
   best_num[0] = -1;
63
9.85M
   best_num[1] = -1;
64
9.85M
   best_den[0] = 0;
65
9.85M
   best_den[1] = 0;
66
9.85M
   best_pitch[0] = 0;
67
9.85M
   best_pitch[1] = 1;
68
2.49G
   for (j=0;j<len;j++)
69
2.48G
      Syy = ADD32(Syy, SHR32(MULT16_16(y[j],y[j]), yshift));
70
3.60G
   for (i=0;i<max_pitch;i++)
71
3.59G
   {
72
3.59G
      if (xcorr[i]>0)
73
928M
      {
74
928M
         opus_val16 num;
75
928M
         opus_val32 xcorr16;
76
928M
         xcorr16 = EXTRACT16(VSHR32(xcorr[i], xshift));
77
#ifndef FIXED_POINT
78
         /* Considering the range of xcorr16, this should avoid both underflows
79
            and overflows (inf) when squaring xcorr16 */
80
         xcorr16 *= 1e-12f;
81
#endif
82
928M
         num = MULT16_16_Q15(xcorr16,xcorr16);
83
928M
         if (MULT16_32_Q15(num,best_den[1]) > MULT16_32_Q15(best_num[1],Syy))
84
53.9M
         {
85
53.9M
            if (MULT16_32_Q15(num,best_den[0]) > MULT16_32_Q15(best_num[0],Syy))
86
35.1M
            {
87
35.1M
               best_num[1] = best_num[0];
88
35.1M
               best_den[1] = best_den[0];
89
35.1M
               best_pitch[1] = best_pitch[0];
90
35.1M
               best_num[0] = num;
91
35.1M
               best_den[0] = Syy;
92
35.1M
               best_pitch[0] = i;
93
35.1M
            } else {
94
18.7M
               best_num[1] = num;
95
18.7M
               best_den[1] = Syy;
96
18.7M
               best_pitch[1] = i;
97
18.7M
            }
98
53.9M
         }
99
928M
      }
100
3.59G
      Syy += SHR32(MULT16_16(y[i+len],y[i+len]),yshift) - SHR32(MULT16_16(y[i],y[i]),yshift);
101
3.59G
      Syy = MAX32(1, Syy);
102
3.59G
   }
103
9.85M
}
pitch.c:find_best_pitch
Line
Count
Source
51
7.28M
{
52
7.28M
   int i, j;
53
7.28M
   opus_val32 Syy=1;
54
7.28M
   opus_val16 best_num[2];
55
7.28M
   opus_val32 best_den[2];
56
7.28M
#ifdef FIXED_POINT
57
7.28M
   int xshift;
58
59
7.28M
   xshift = celt_ilog2(maxcorr)-14;
60
7.28M
#endif
61
62
7.28M
   best_num[0] = -1;
63
7.28M
   best_num[1] = -1;
64
7.28M
   best_den[0] = 0;
65
7.28M
   best_den[1] = 0;
66
7.28M
   best_pitch[0] = 0;
67
7.28M
   best_pitch[1] = 1;
68
1.99G
   for (j=0;j<len;j++)
69
1.98G
      Syy = ADD32(Syy, SHR32(MULT16_16(y[j],y[j]), yshift));
70
2.66G
   for (i=0;i<max_pitch;i++)
71
2.65G
   {
72
2.65G
      if (xcorr[i]>0)
73
758M
      {
74
758M
         opus_val16 num;
75
758M
         opus_val32 xcorr16;
76
758M
         xcorr16 = EXTRACT16(VSHR32(xcorr[i], xshift));
77
#ifndef FIXED_POINT
78
         /* Considering the range of xcorr16, this should avoid both underflows
79
            and overflows (inf) when squaring xcorr16 */
80
         xcorr16 *= 1e-12f;
81
#endif
82
758M
         num = MULT16_16_Q15(xcorr16,xcorr16);
83
758M
         if (MULT16_32_Q15(num,best_den[1]) > MULT16_32_Q15(best_num[1],Syy))
84
33.8M
         {
85
33.8M
            if (MULT16_32_Q15(num,best_den[0]) > MULT16_32_Q15(best_num[0],Syy))
86
21.7M
            {
87
21.7M
               best_num[1] = best_num[0];
88
21.7M
               best_den[1] = best_den[0];
89
21.7M
               best_pitch[1] = best_pitch[0];
90
21.7M
               best_num[0] = num;
91
21.7M
               best_den[0] = Syy;
92
21.7M
               best_pitch[0] = i;
93
21.7M
            } else {
94
12.0M
               best_num[1] = num;
95
12.0M
               best_den[1] = Syy;
96
12.0M
               best_pitch[1] = i;
97
12.0M
            }
98
33.8M
         }
99
758M
      }
100
2.65G
      Syy += SHR32(MULT16_16(y[i+len],y[i+len]),yshift) - SHR32(MULT16_16(y[i],y[i]),yshift);
101
2.65G
      Syy = MAX32(1, Syy);
102
2.65G
   }
103
7.28M
}
pitch.c:find_best_pitch
Line
Count
Source
51
2.57M
{
52
2.57M
   int i, j;
53
2.57M
   opus_val32 Syy=1;
54
2.57M
   opus_val16 best_num[2];
55
2.57M
   opus_val32 best_den[2];
56
#ifdef FIXED_POINT
57
   int xshift;
58
59
   xshift = celt_ilog2(maxcorr)-14;
60
#endif
61
62
2.57M
   best_num[0] = -1;
63
2.57M
   best_num[1] = -1;
64
2.57M
   best_den[0] = 0;
65
2.57M
   best_den[1] = 0;
66
2.57M
   best_pitch[0] = 0;
67
2.57M
   best_pitch[1] = 1;
68
505M
   for (j=0;j<len;j++)
69
502M
      Syy = ADD32(Syy, SHR32(MULT16_16(y[j],y[j]), yshift));
70
940M
   for (i=0;i<max_pitch;i++)
71
938M
   {
72
938M
      if (xcorr[i]>0)
73
170M
      {
74
170M
         opus_val16 num;
75
170M
         opus_val32 xcorr16;
76
170M
         xcorr16 = EXTRACT16(VSHR32(xcorr[i], xshift));
77
170M
#ifndef FIXED_POINT
78
         /* Considering the range of xcorr16, this should avoid both underflows
79
            and overflows (inf) when squaring xcorr16 */
80
170M
         xcorr16 *= 1e-12f;
81
170M
#endif
82
170M
         num = MULT16_16_Q15(xcorr16,xcorr16);
83
170M
         if (MULT16_32_Q15(num,best_den[1]) > MULT16_32_Q15(best_num[1],Syy))
84
20.0M
         {
85
20.0M
            if (MULT16_32_Q15(num,best_den[0]) > MULT16_32_Q15(best_num[0],Syy))
86
13.3M
            {
87
13.3M
               best_num[1] = best_num[0];
88
13.3M
               best_den[1] = best_den[0];
89
13.3M
               best_pitch[1] = best_pitch[0];
90
13.3M
               best_num[0] = num;
91
13.3M
               best_den[0] = Syy;
92
13.3M
               best_pitch[0] = i;
93
13.3M
            } else {
94
6.73M
               best_num[1] = num;
95
6.73M
               best_den[1] = Syy;
96
6.73M
               best_pitch[1] = i;
97
6.73M
            }
98
20.0M
         }
99
170M
      }
100
938M
      Syy += SHR32(MULT16_16(y[i+len],y[i+len]),yshift) - SHR32(MULT16_16(y[i],y[i]),yshift);
101
938M
      Syy = MAX32(1, Syy);
102
938M
   }
103
2.57M
}
104
105
static void celt_fir5(opus_val16 *x,
106
         const opus_val16 *num,
107
         int N)
108
9.85M
{
109
9.85M
   int i;
110
9.85M
   opus_val16 num0, num1, num2, num3, num4;
111
9.85M
   opus_val32 mem0, mem1, mem2, mem3, mem4;
112
9.85M
   num0=num[0];
113
9.85M
   num1=num[1];
114
9.85M
   num2=num[2];
115
9.85M
   num3=num[3];
116
9.85M
   num4=num[4];
117
9.85M
   mem0=0;
118
9.85M
   mem1=0;
119
9.85M
   mem2=0;
120
9.85M
   mem3=0;
121
9.85M
   mem4=0;
122
8.35G
   for (i=0;i<N;i++)
123
8.34G
   {
124
8.34G
      opus_val32 sum = SHL32(EXTEND32(x[i]), SIG_SHIFT);
125
8.34G
      sum = MAC16_16(sum,num0,mem0);
126
8.34G
      sum = MAC16_16(sum,num1,mem1);
127
8.34G
      sum = MAC16_16(sum,num2,mem2);
128
8.34G
      sum = MAC16_16(sum,num3,mem3);
129
8.34G
      sum = MAC16_16(sum,num4,mem4);
130
8.34G
      mem4 = mem3;
131
8.34G
      mem3 = mem2;
132
8.34G
      mem2 = mem1;
133
8.34G
      mem1 = mem0;
134
8.34G
      mem0 = x[i];
135
8.34G
      x[i] = ROUND16(sum, SIG_SHIFT);
136
8.34G
   }
137
9.85M
}
pitch.c:celt_fir5
Line
Count
Source
108
4.92M
{
109
4.92M
   int i;
110
4.92M
   opus_val16 num0, num1, num2, num3, num4;
111
4.92M
   opus_val32 mem0, mem1, mem2, mem3, mem4;
112
4.92M
   num0=num[0];
113
4.92M
   num1=num[1];
114
4.92M
   num2=num[2];
115
4.92M
   num3=num[3];
116
4.92M
   num4=num[4];
117
4.92M
   mem0=0;
118
4.92M
   mem1=0;
119
4.92M
   mem2=0;
120
4.92M
   mem3=0;
121
4.92M
   mem4=0;
122
4.17G
   for (i=0;i<N;i++)
123
4.17G
   {
124
4.17G
      opus_val32 sum = SHL32(EXTEND32(x[i]), SIG_SHIFT);
125
4.17G
      sum = MAC16_16(sum,num0,mem0);
126
4.17G
      sum = MAC16_16(sum,num1,mem1);
127
4.17G
      sum = MAC16_16(sum,num2,mem2);
128
4.17G
      sum = MAC16_16(sum,num3,mem3);
129
4.17G
      sum = MAC16_16(sum,num4,mem4);
130
4.17G
      mem4 = mem3;
131
4.17G
      mem3 = mem2;
132
4.17G
      mem2 = mem1;
133
4.17G
      mem1 = mem0;
134
4.17G
      mem0 = x[i];
135
4.17G
      x[i] = ROUND16(sum, SIG_SHIFT);
136
4.17G
   }
137
4.92M
}
pitch.c:celt_fir5
Line
Count
Source
108
4.92M
{
109
4.92M
   int i;
110
4.92M
   opus_val16 num0, num1, num2, num3, num4;
111
4.92M
   opus_val32 mem0, mem1, mem2, mem3, mem4;
112
4.92M
   num0=num[0];
113
4.92M
   num1=num[1];
114
4.92M
   num2=num[2];
115
4.92M
   num3=num[3];
116
4.92M
   num4=num[4];
117
4.92M
   mem0=0;
118
4.92M
   mem1=0;
119
4.92M
   mem2=0;
120
4.92M
   mem3=0;
121
4.92M
   mem4=0;
122
4.17G
   for (i=0;i<N;i++)
123
4.17G
   {
124
4.17G
      opus_val32 sum = SHL32(EXTEND32(x[i]), SIG_SHIFT);
125
4.17G
      sum = MAC16_16(sum,num0,mem0);
126
4.17G
      sum = MAC16_16(sum,num1,mem1);
127
4.17G
      sum = MAC16_16(sum,num2,mem2);
128
4.17G
      sum = MAC16_16(sum,num3,mem3);
129
4.17G
      sum = MAC16_16(sum,num4,mem4);
130
4.17G
      mem4 = mem3;
131
4.17G
      mem3 = mem2;
132
4.17G
      mem2 = mem1;
133
4.17G
      mem1 = mem0;
134
4.17G
      mem0 = x[i];
135
4.17G
      x[i] = ROUND16(sum, SIG_SHIFT);
136
4.17G
   }
137
4.92M
}
138
139
140
void pitch_downsample(celt_sig * OPUS_RESTRICT x[], opus_val16 * OPUS_RESTRICT x_lp,
141
      int len, int C, int factor, int arch)
142
4.92M
{
143
4.92M
   int i;
144
4.92M
   opus_val32 ac[5];
145
4.92M
   opus_val16 tmp=Q15ONE;
146
4.92M
   opus_val16 lpc[4];
147
4.92M
   opus_val16 lpc2[5];
148
4.92M
   opus_val16 c1 = QCONST16(.8f,15);
149
4.92M
   int offset;
150
#ifdef FIXED_POINT
151
   int shift;
152
   opus_val32 maxabs;
153
#endif
154
4.92M
   offset = factor/2;
155
#ifdef FIXED_POINT
156
   maxabs = celt_maxabs32(x[0], len*factor);
157
3.64M
   if (C==2)
158
936k
   {
159
936k
      opus_val32 maxabs_1 = celt_maxabs32(x[1], len*factor);
160
936k
      maxabs = MAX32(maxabs, maxabs_1);
161
936k
   }
162
3.64M
   if (maxabs<1)
163
1.46k
      maxabs=1;
164
   shift = celt_ilog2(maxabs)-10;
165
3.64M
   if (shift<0)
166
3.00M
      shift=0;
167
3.64M
   if (C==2)
168
936k
      shift++;
169
3.17G
   for (i=1;i<len;i++)
170
3.17G
      x_lp[i] = SHR32(x[0][(factor*i-offset)], shift+2) + SHR32(x[0][(factor*i+offset)], shift+2) + SHR32(x[0][factor*i], shift+1);
171
3.64M
   x_lp[0] = SHR32(x[0][offset], shift+2) + SHR32(x[0][0], shift+1);
172
3.64M
   if (C==2)
173
936k
   {
174
857M
      for (i=1;i<len;i++)
175
856M
         x_lp[i] += SHR32(x[1][(factor*i-offset)], shift+2) + SHR32(x[1][(factor*i+offset)], shift+2) + SHR32(x[1][factor*i], shift+1);
176
936k
      x_lp[0] += SHR32(x[1][offset], shift+2) + SHR32(x[1][0], shift+1);
177
936k
   }
178
#else
179
992M
   for (i=1;i<len;i++)
180
990M
      x_lp[i] = .25f*x[0][(factor*i-offset)] + .25f*x[0][(factor*i+offset)] + .5f*x[0][factor*i];
181
   x_lp[0] = .25f*x[0][offset] + .5f*x[0][0];
182
1.28M
   if (C==2)
183
313k
   {
184
256M
      for (i=1;i<len;i++)
185
256M
         x_lp[i] += .25f*x[1][(factor*i-offset)] + .25f*x[1][(factor*i+offset)] + .5f*x[1][factor*i];
186
313k
      x_lp[0] += .25f*x[1][offset] + .5f*x[1][0];
187
313k
   }
188
#endif
189
4.92M
   _celt_autocorr(x_lp, ac, NULL, 0,
190
4.92M
                  4, len, arch);
191
192
   /* Noise floor -40 dB */
193
#ifdef FIXED_POINT
194
3.64M
   ac[0] += SHR32(ac[0],13);
195
#else
196
   ac[0] *= 1.0001f;
197
#endif
198
   /* Lag windowing */
199
24.6M
   for (i=1;i<=4;i++)
200
19.7M
   {
201
      /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
202
#ifdef FIXED_POINT
203
14.5M
      ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
204
#else
205
      ac[i] -= ac[i]*(.008f*i)*(.008f*i);
206
#endif
207
19.7M
   }
208
209
4.92M
   _celt_lpc(lpc, ac, 4);
210
24.6M
   for (i=0;i<4;i++)
211
19.7M
   {
212
19.7M
      tmp = MULT16_16_Q15(QCONST16(.9f,15), tmp);
213
19.7M
      lpc[i] = MULT16_16_Q15(lpc[i], tmp);
214
19.7M
   }
215
   /* Add a zero */
216
4.92M
   lpc2[0] = lpc[0] + QCONST16(.8f,SIG_SHIFT);
217
4.92M
   lpc2[1] = lpc[1] + MULT16_16_Q15(c1,lpc[0]);
218
4.92M
   lpc2[2] = lpc[2] + MULT16_16_Q15(c1,lpc[1]);
219
4.92M
   lpc2[3] = lpc[3] + MULT16_16_Q15(c1,lpc[2]);
220
4.92M
   lpc2[4] = MULT16_16_Q15(c1,lpc[3]);
221
4.92M
   celt_fir5(x_lp, lpc2, len);
222
4.92M
}
pitch_downsample
Line
Count
Source
142
3.64M
{
143
3.64M
   int i;
144
3.64M
   opus_val32 ac[5];
145
3.64M
   opus_val16 tmp=Q15ONE;
146
3.64M
   opus_val16 lpc[4];
147
3.64M
   opus_val16 lpc2[5];
148
3.64M
   opus_val16 c1 = QCONST16(.8f,15);
149
3.64M
   int offset;
150
3.64M
#ifdef FIXED_POINT
151
3.64M
   int shift;
152
3.64M
   opus_val32 maxabs;
153
3.64M
#endif
154
3.64M
   offset = factor/2;
155
3.64M
#ifdef FIXED_POINT
156
3.64M
   maxabs = celt_maxabs32(x[0], len*factor);
157
3.64M
   if (C==2)
158
936k
   {
159
936k
      opus_val32 maxabs_1 = celt_maxabs32(x[1], len*factor);
160
936k
      maxabs = MAX32(maxabs, maxabs_1);
161
936k
   }
162
3.64M
   if (maxabs<1)
163
1.46k
      maxabs=1;
164
3.64M
   shift = celt_ilog2(maxabs)-10;
165
3.64M
   if (shift<0)
166
3.00M
      shift=0;
167
3.64M
   if (C==2)
168
936k
      shift++;
169
3.17G
   for (i=1;i<len;i++)
170
3.17G
      x_lp[i] = SHR32(x[0][(factor*i-offset)], shift+2) + SHR32(x[0][(factor*i+offset)], shift+2) + SHR32(x[0][factor*i], shift+1);
171
3.64M
   x_lp[0] = SHR32(x[0][offset], shift+2) + SHR32(x[0][0], shift+1);
172
3.64M
   if (C==2)
173
936k
   {
174
857M
      for (i=1;i<len;i++)
175
856M
         x_lp[i] += SHR32(x[1][(factor*i-offset)], shift+2) + SHR32(x[1][(factor*i+offset)], shift+2) + SHR32(x[1][factor*i], shift+1);
176
936k
      x_lp[0] += SHR32(x[1][offset], shift+2) + SHR32(x[1][0], shift+1);
177
936k
   }
178
#else
179
   for (i=1;i<len;i++)
180
      x_lp[i] = .25f*x[0][(factor*i-offset)] + .25f*x[0][(factor*i+offset)] + .5f*x[0][factor*i];
181
   x_lp[0] = .25f*x[0][offset] + .5f*x[0][0];
182
   if (C==2)
183
   {
184
      for (i=1;i<len;i++)
185
         x_lp[i] += .25f*x[1][(factor*i-offset)] + .25f*x[1][(factor*i+offset)] + .5f*x[1][factor*i];
186
      x_lp[0] += .25f*x[1][offset] + .5f*x[1][0];
187
   }
188
#endif
189
3.64M
   _celt_autocorr(x_lp, ac, NULL, 0,
190
3.64M
                  4, len, arch);
191
192
   /* Noise floor -40 dB */
193
3.64M
#ifdef FIXED_POINT
194
3.64M
   ac[0] += SHR32(ac[0],13);
195
#else
196
   ac[0] *= 1.0001f;
197
#endif
198
   /* Lag windowing */
199
18.2M
   for (i=1;i<=4;i++)
200
14.5M
   {
201
      /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
202
14.5M
#ifdef FIXED_POINT
203
14.5M
      ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
204
#else
205
      ac[i] -= ac[i]*(.008f*i)*(.008f*i);
206
#endif
207
14.5M
   }
208
209
3.64M
   _celt_lpc(lpc, ac, 4);
210
18.2M
   for (i=0;i<4;i++)
211
14.5M
   {
212
14.5M
      tmp = MULT16_16_Q15(QCONST16(.9f,15), tmp);
213
14.5M
      lpc[i] = MULT16_16_Q15(lpc[i], tmp);
214
14.5M
   }
215
   /* Add a zero */
216
3.64M
   lpc2[0] = lpc[0] + QCONST16(.8f,SIG_SHIFT);
217
3.64M
   lpc2[1] = lpc[1] + MULT16_16_Q15(c1,lpc[0]);
218
3.64M
   lpc2[2] = lpc[2] + MULT16_16_Q15(c1,lpc[1]);
219
3.64M
   lpc2[3] = lpc[3] + MULT16_16_Q15(c1,lpc[2]);
220
3.64M
   lpc2[4] = MULT16_16_Q15(c1,lpc[3]);
221
3.64M
   celt_fir5(x_lp, lpc2, len);
222
3.64M
}
pitch_downsample
Line
Count
Source
142
1.28M
{
143
1.28M
   int i;
144
1.28M
   opus_val32 ac[5];
145
1.28M
   opus_val16 tmp=Q15ONE;
146
1.28M
   opus_val16 lpc[4];
147
1.28M
   opus_val16 lpc2[5];
148
1.28M
   opus_val16 c1 = QCONST16(.8f,15);
149
1.28M
   int offset;
150
#ifdef FIXED_POINT
151
   int shift;
152
   opus_val32 maxabs;
153
#endif
154
1.28M
   offset = factor/2;
155
#ifdef FIXED_POINT
156
   maxabs = celt_maxabs32(x[0], len*factor);
157
   if (C==2)
158
   {
159
      opus_val32 maxabs_1 = celt_maxabs32(x[1], len*factor);
160
      maxabs = MAX32(maxabs, maxabs_1);
161
   }
162
   if (maxabs<1)
163
      maxabs=1;
164
   shift = celt_ilog2(maxabs)-10;
165
   if (shift<0)
166
      shift=0;
167
   if (C==2)
168
      shift++;
169
   for (i=1;i<len;i++)
170
      x_lp[i] = SHR32(x[0][(factor*i-offset)], shift+2) + SHR32(x[0][(factor*i+offset)], shift+2) + SHR32(x[0][factor*i], shift+1);
171
   x_lp[0] = SHR32(x[0][offset], shift+2) + SHR32(x[0][0], shift+1);
172
   if (C==2)
173
   {
174
      for (i=1;i<len;i++)
175
         x_lp[i] += SHR32(x[1][(factor*i-offset)], shift+2) + SHR32(x[1][(factor*i+offset)], shift+2) + SHR32(x[1][factor*i], shift+1);
176
      x_lp[0] += SHR32(x[1][offset], shift+2) + SHR32(x[1][0], shift+1);
177
   }
178
#else
179
992M
   for (i=1;i<len;i++)
180
990M
      x_lp[i] = .25f*x[0][(factor*i-offset)] + .25f*x[0][(factor*i+offset)] + .5f*x[0][factor*i];
181
1.28M
   x_lp[0] = .25f*x[0][offset] + .5f*x[0][0];
182
1.28M
   if (C==2)
183
313k
   {
184
256M
      for (i=1;i<len;i++)
185
256M
         x_lp[i] += .25f*x[1][(factor*i-offset)] + .25f*x[1][(factor*i+offset)] + .5f*x[1][factor*i];
186
313k
      x_lp[0] += .25f*x[1][offset] + .5f*x[1][0];
187
313k
   }
188
1.28M
#endif
189
1.28M
   _celt_autocorr(x_lp, ac, NULL, 0,
190
1.28M
                  4, len, arch);
191
192
   /* Noise floor -40 dB */
193
#ifdef FIXED_POINT
194
   ac[0] += SHR32(ac[0],13);
195
#else
196
1.28M
   ac[0] *= 1.0001f;
197
1.28M
#endif
198
   /* Lag windowing */
199
6.43M
   for (i=1;i<=4;i++)
200
5.14M
   {
201
      /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
202
#ifdef FIXED_POINT
203
      ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
204
#else
205
5.14M
      ac[i] -= ac[i]*(.008f*i)*(.008f*i);
206
5.14M
#endif
207
5.14M
   }
208
209
1.28M
   _celt_lpc(lpc, ac, 4);
210
6.43M
   for (i=0;i<4;i++)
211
5.14M
   {
212
5.14M
      tmp = MULT16_16_Q15(QCONST16(.9f,15), tmp);
213
5.14M
      lpc[i] = MULT16_16_Q15(lpc[i], tmp);
214
5.14M
   }
215
   /* Add a zero */
216
1.28M
   lpc2[0] = lpc[0] + QCONST16(.8f,SIG_SHIFT);
217
1.28M
   lpc2[1] = lpc[1] + MULT16_16_Q15(c1,lpc[0]);
218
1.28M
   lpc2[2] = lpc[2] + MULT16_16_Q15(c1,lpc[1]);
219
1.28M
   lpc2[3] = lpc[3] + MULT16_16_Q15(c1,lpc[2]);
220
1.28M
   lpc2[4] = MULT16_16_Q15(c1,lpc[3]);
221
1.28M
   celt_fir5(x_lp, lpc2, len);
222
1.28M
}
223
224
/* Pure C implementation. */
225
#ifdef FIXED_POINT
226
opus_val32
227
#else
228
void
229
#endif
230
celt_pitch_xcorr_c(const opus_val16 *_x, const opus_val16 *_y,
231
      opus_val32 *xcorr, int len, int max_pitch, int arch)
232
481M
{
233
234
#if 0 /* This is a simple version of the pitch correlation that should work
235
         well on DSPs like Blackfin and TI C5x/C6x */
236
   int i, j;
237
#ifdef FIXED_POINT
238
   opus_val32 maxcorr=1;
239
#endif
240
#if !defined(OVERRIDE_PITCH_XCORR)
241
   (void)arch;
242
#endif
243
   for (i=0;i<max_pitch;i++)
244
   {
245
      opus_val32 sum = 0;
246
      for (j=0;j<len;j++)
247
         sum = MAC16_16(sum, _x[j], _y[i+j]);
248
      xcorr[i] = sum;
249
#ifdef FIXED_POINT
250
      maxcorr = MAX32(maxcorr, sum);
251
#endif
252
   }
253
#ifdef FIXED_POINT
254
   return maxcorr;
255
#endif
256
257
#else /* Unrolled version of the pitch correlation -- runs faster on x86 and ARM */
258
481M
   int i;
259
   /*The EDSP version requires that max_pitch is at least 1, and that _x is
260
      32-bit aligned.
261
     Since it's hard to put asserts in assembly, put them here.*/
262
#ifdef FIXED_POINT
263
   opus_val32 maxcorr=1;
264
#endif
265
481M
   celt_assert(max_pitch>0);
266
481M
   celt_sig_assert(((size_t)_x&3)==0);
267
1.81G
   for (i=0;i<max_pitch-3;i+=4)
268
1.33G
   {
269
1.33G
      opus_val32 sum[4]={0,0,0,0};
270
#if defined(OPUS_CHECK_ASM) && defined(FIXED_POINT)
271
      {
272
         opus_val32 sum_c[4]={0,0,0,0};
273
         xcorr_kernel_c(_x, _y+i, sum_c, len);
274
#endif
275
1.33G
         xcorr_kernel(_x, _y+i, sum, len, arch);
276
#if defined(OPUS_CHECK_ASM) && defined(FIXED_POINT)
277
1.33G
         celt_assert(memcmp(sum, sum_c, sizeof(sum)) == 0);
278
1.33G
      }
279
0
#endif
280
0
      xcorr[i]=sum[0];
281
1.33G
      xcorr[i+1]=sum[1];
282
1.33G
      xcorr[i+2]=sum[2];
283
1.33G
      xcorr[i+3]=sum[3];
284
#ifdef FIXED_POINT
285
1.33G
      sum[0] = MAX32(sum[0], sum[1]);
286
1.33G
      sum[2] = MAX32(sum[2], sum[3]);
287
1.33G
      sum[0] = MAX32(sum[0], sum[2]);
288
1.33G
      maxcorr = MAX32(maxcorr, sum[0]);
289
#endif
290
1.33G
   }
291
   /* In case max_pitch isn't a multiple of 4, do non-unrolled version. */
292
1.34G
   for (;i<max_pitch;i++)
293
865M
   {
294
865M
      opus_val32 sum;
295
865M
      sum = celt_inner_prod(_x, _y+i, len, arch);
296
865M
      xcorr[i] = sum;
297
#ifdef FIXED_POINT
298
865M
      maxcorr = MAX32(maxcorr, sum);
299
#endif
300
865M
   }
301
#ifdef FIXED_POINT
302
   return maxcorr;
303
#endif
304
481M
#endif
305
481M
}
celt_pitch_xcorr_c
Line
Count
Source
232
481M
{
233
234
#if 0 /* This is a simple version of the pitch correlation that should work
235
         well on DSPs like Blackfin and TI C5x/C6x */
236
   int i, j;
237
#ifdef FIXED_POINT
238
   opus_val32 maxcorr=1;
239
#endif
240
#if !defined(OVERRIDE_PITCH_XCORR)
241
   (void)arch;
242
#endif
243
   for (i=0;i<max_pitch;i++)
244
   {
245
      opus_val32 sum = 0;
246
      for (j=0;j<len;j++)
247
         sum = MAC16_16(sum, _x[j], _y[i+j]);
248
      xcorr[i] = sum;
249
#ifdef FIXED_POINT
250
      maxcorr = MAX32(maxcorr, sum);
251
#endif
252
   }
253
#ifdef FIXED_POINT
254
   return maxcorr;
255
#endif
256
257
#else /* Unrolled version of the pitch correlation -- runs faster on x86 and ARM */
258
481M
   int i;
259
   /*The EDSP version requires that max_pitch is at least 1, and that _x is
260
      32-bit aligned.
261
     Since it's hard to put asserts in assembly, put them here.*/
262
481M
#ifdef FIXED_POINT
263
481M
   opus_val32 maxcorr=1;
264
481M
#endif
265
481M
   celt_assert(max_pitch>0);
266
481M
   celt_sig_assert(((size_t)_x&3)==0);
267
1.81G
   for (i=0;i<max_pitch-3;i+=4)
268
1.33G
   {
269
1.33G
      opus_val32 sum[4]={0,0,0,0};
270
1.33G
#if defined(OPUS_CHECK_ASM) && defined(FIXED_POINT)
271
1.33G
      {
272
1.33G
         opus_val32 sum_c[4]={0,0,0,0};
273
1.33G
         xcorr_kernel_c(_x, _y+i, sum_c, len);
274
1.33G
#endif
275
1.33G
         xcorr_kernel(_x, _y+i, sum, len, arch);
276
1.33G
#if defined(OPUS_CHECK_ASM) && defined(FIXED_POINT)
277
1.33G
         celt_assert(memcmp(sum, sum_c, sizeof(sum)) == 0);
278
1.33G
      }
279
0
#endif
280
0
      xcorr[i]=sum[0];
281
1.33G
      xcorr[i+1]=sum[1];
282
1.33G
      xcorr[i+2]=sum[2];
283
1.33G
      xcorr[i+3]=sum[3];
284
1.33G
#ifdef FIXED_POINT
285
1.33G
      sum[0] = MAX32(sum[0], sum[1]);
286
1.33G
      sum[2] = MAX32(sum[2], sum[3]);
287
1.33G
      sum[0] = MAX32(sum[0], sum[2]);
288
1.33G
      maxcorr = MAX32(maxcorr, sum[0]);
289
1.33G
#endif
290
1.33G
   }
291
   /* In case max_pitch isn't a multiple of 4, do non-unrolled version. */
292
1.34G
   for (;i<max_pitch;i++)
293
865M
   {
294
865M
      opus_val32 sum;
295
865M
      sum = celt_inner_prod(_x, _y+i, len, arch);
296
865M
      xcorr[i] = sum;
297
865M
#ifdef FIXED_POINT
298
865M
      maxcorr = MAX32(maxcorr, sum);
299
865M
#endif
300
865M
   }
301
481M
#ifdef FIXED_POINT
302
481M
   return maxcorr;
303
481M
#endif
304
481M
#endif
305
481M
}
Unexecuted instantiation: celt_pitch_xcorr_c
306
307
void pitch_search(const opus_val16 * OPUS_RESTRICT x_lp, opus_val16 * OPUS_RESTRICT y,
308
                  int len, int max_pitch, int *pitch, int arch)
309
4.92M
{
310
4.92M
   int i, j;
311
4.92M
   int lag;
312
4.92M
   int best_pitch[2]={0,0};
313
4.92M
   VARDECL(opus_val16, x_lp4);
314
4.92M
   VARDECL(opus_val16, y_lp4);
315
4.92M
   VARDECL(opus_val32, xcorr);
316
#ifdef FIXED_POINT
317
   opus_val32 maxcorr;
318
   opus_val32 xmax, ymax;
319
   int shift=0;
320
#endif
321
4.92M
   int offset;
322
323
4.92M
   SAVE_STACK;
324
325
4.92M
   celt_assert(len>0);
326
4.92M
   celt_assert(max_pitch>0);
327
4.92M
   lag = len+max_pitch;
328
329
4.92M
   ALLOC(x_lp4, len>>2, opus_val16);
330
4.92M
   ALLOC(y_lp4, lag>>2, opus_val16);
331
4.92M
   ALLOC(xcorr, max_pitch>>1, opus_val32);
332
333
   /* Downsample by 2 again */
334
834M
   for (j=0;j<len>>2;j++)
335
829M
      x_lp4[j] = x_lp[2*j];
336
2.03G
   for (j=0;j<lag>>2;j++)
337
2.02G
      y_lp4[j] = y[2*j];
338
339
#ifdef FIXED_POINT
340
   xmax = celt_maxabs16(x_lp4, len>>2);
341
   ymax = celt_maxabs16(y_lp4, lag>>2);
342
3.64M
   shift = celt_ilog2(MAX32(1, MAX32(xmax, ymax))) - 14 + celt_ilog2(len)/2;
343
3.64M
   if (shift>0)
344
7.41k
   {
345
2.48M
      for (j=0;j<len>>2;j++)
346
2.47M
         x_lp4[j] = SHR16(x_lp4[j], shift);
347
3.65M
      for (j=0;j<lag>>2;j++)
348
3.64M
         y_lp4[j] = SHR16(y_lp4[j], shift);
349
      /* Use double the shift for a MAC */
350
7.41k
      shift *= 2;
351
3.63M
   } else {
352
3.63M
      shift = 0;
353
3.63M
   }
354
#endif
355
356
   /* Coarse search with 4x decimation */
357
358
#ifdef FIXED_POINT
359
   maxcorr =
360
#endif
361
4.92M
   celt_pitch_xcorr(x_lp4, y_lp4, xcorr, len>>2, max_pitch>>2, arch);
362
363
4.92M
   find_best_pitch(xcorr, y_lp4, len>>2, max_pitch>>2, best_pitch
364
#ifdef FIXED_POINT
365
                   , 0, maxcorr
366
#endif
367
4.92M
                   );
368
369
   /* Finer search with 2x decimation */
370
#ifdef FIXED_POINT
371
   maxcorr=1;
372
#endif
373
2.40G
   for (i=0;i<max_pitch>>1;i++)
374
2.39G
   {
375
2.39G
      opus_val32 sum;
376
2.39G
      xcorr[i] = 0;
377
2.39G
      if (abs(i-2*best_pitch[0])>2 && abs(i-2*best_pitch[1])>2)
378
2.35G
         continue;
379
#ifdef FIXED_POINT
380
30.6M
      sum = 0;
381
11.2G
      for (j=0;j<len>>1;j++)
382
11.2G
         sum += SHR32(MULT16_16(x_lp[j],y[i+j]), shift);
383
#else
384
12.4M
      sum = celt_inner_prod(x_lp, y+i, len>>1, arch);
385
#endif
386
43.0M
      xcorr[i] = MAX32(-1, sum);
387
#ifdef FIXED_POINT
388
30.6M
      maxcorr = MAX32(maxcorr, sum);
389
#endif
390
12.4M
   }
391
4.92M
   find_best_pitch(xcorr, y, len>>1, max_pitch>>1, best_pitch
392
#ifdef FIXED_POINT
393
                   , shift+1, maxcorr
394
#endif
395
4.92M
                   );
396
397
   /* Refine by pseudo-interpolation */
398
4.92M
   if (best_pitch[0]>0 && best_pitch[0]<(max_pitch>>1)-1)
399
4.73M
   {
400
4.73M
      opus_val32 a, b, c;
401
4.73M
      a = xcorr[best_pitch[0]-1];
402
4.73M
      b = xcorr[best_pitch[0]];
403
4.73M
      c = xcorr[best_pitch[0]+1];
404
4.73M
      if ((c-a) > MULT16_32_Q15(QCONST16(.7f,15),b-a))
405
1.53M
         offset = 1;
406
3.20M
      else if ((a-c) > MULT16_32_Q15(QCONST16(.7f,15),b-c))
407
109k
         offset = -1;
408
3.09M
      else
409
3.09M
         offset = 0;
410
4.73M
   } else {
411
190k
      offset = 0;
412
190k
   }
413
4.92M
   *pitch = 2*best_pitch[0]-offset;
414
415
4.92M
   RESTORE_STACK;
416
4.92M
}
pitch_search
Line
Count
Source
309
3.64M
{
310
3.64M
   int i, j;
311
3.64M
   int lag;
312
3.64M
   int best_pitch[2]={0,0};
313
3.64M
   VARDECL(opus_val16, x_lp4);
314
3.64M
   VARDECL(opus_val16, y_lp4);
315
3.64M
   VARDECL(opus_val32, xcorr);
316
3.64M
#ifdef FIXED_POINT
317
3.64M
   opus_val32 maxcorr;
318
3.64M
   opus_val32 xmax, ymax;
319
3.64M
   int shift=0;
320
3.64M
#endif
321
3.64M
   int offset;
322
323
3.64M
   SAVE_STACK;
324
325
3.64M
   celt_assert(len>0);
326
3.64M
   celt_assert(max_pitch>0);
327
3.64M
   lag = len+max_pitch;
328
329
3.64M
   ALLOC(x_lp4, len>>2, opus_val16);
330
3.64M
   ALLOC(y_lp4, lag>>2, opus_val16);
331
3.64M
   ALLOC(xcorr, max_pitch>>1, opus_val32);
332
333
   /* Downsample by 2 again */
334
665M
   for (j=0;j<len>>2;j++)
335
661M
      x_lp4[j] = x_lp[2*j];
336
1.54G
   for (j=0;j<lag>>2;j++)
337
1.54G
      y_lp4[j] = y[2*j];
338
339
3.64M
#ifdef FIXED_POINT
340
3.64M
   xmax = celt_maxabs16(x_lp4, len>>2);
341
3.64M
   ymax = celt_maxabs16(y_lp4, lag>>2);
342
3.64M
   shift = celt_ilog2(MAX32(1, MAX32(xmax, ymax))) - 14 + celt_ilog2(len)/2;
343
3.64M
   if (shift>0)
344
7.41k
   {
345
2.48M
      for (j=0;j<len>>2;j++)
346
2.47M
         x_lp4[j] = SHR16(x_lp4[j], shift);
347
3.65M
      for (j=0;j<lag>>2;j++)
348
3.64M
         y_lp4[j] = SHR16(y_lp4[j], shift);
349
      /* Use double the shift for a MAC */
350
7.41k
      shift *= 2;
351
3.63M
   } else {
352
3.63M
      shift = 0;
353
3.63M
   }
354
3.64M
#endif
355
356
   /* Coarse search with 4x decimation */
357
358
3.64M
#ifdef FIXED_POINT
359
3.64M
   maxcorr =
360
3.64M
#endif
361
3.64M
   celt_pitch_xcorr(x_lp4, y_lp4, xcorr, len>>2, max_pitch>>2, arch);
362
363
3.64M
   find_best_pitch(xcorr, y_lp4, len>>2, max_pitch>>2, best_pitch
364
3.64M
#ifdef FIXED_POINT
365
3.64M
                   , 0, maxcorr
366
3.64M
#endif
367
3.64M
                   );
368
369
   /* Finer search with 2x decimation */
370
3.64M
#ifdef FIXED_POINT
371
3.64M
   maxcorr=1;
372
3.64M
#endif
373
1.77G
   for (i=0;i<max_pitch>>1;i++)
374
1.77G
   {
375
1.77G
      opus_val32 sum;
376
1.77G
      xcorr[i] = 0;
377
1.77G
      if (abs(i-2*best_pitch[0])>2 && abs(i-2*best_pitch[1])>2)
378
1.73G
         continue;
379
30.6M
#ifdef FIXED_POINT
380
30.6M
      sum = 0;
381
11.2G
      for (j=0;j<len>>1;j++)
382
11.2G
         sum += SHR32(MULT16_16(x_lp[j],y[i+j]), shift);
383
#else
384
      sum = celt_inner_prod(x_lp, y+i, len>>1, arch);
385
#endif
386
30.6M
      xcorr[i] = MAX32(-1, sum);
387
30.6M
#ifdef FIXED_POINT
388
30.6M
      maxcorr = MAX32(maxcorr, sum);
389
30.6M
#endif
390
30.6M
   }
391
3.64M
   find_best_pitch(xcorr, y, len>>1, max_pitch>>1, best_pitch
392
3.64M
#ifdef FIXED_POINT
393
3.64M
                   , shift+1, maxcorr
394
3.64M
#endif
395
3.64M
                   );
396
397
   /* Refine by pseudo-interpolation */
398
3.64M
   if (best_pitch[0]>0 && best_pitch[0]<(max_pitch>>1)-1)
399
3.49M
   {
400
3.49M
      opus_val32 a, b, c;
401
3.49M
      a = xcorr[best_pitch[0]-1];
402
3.49M
      b = xcorr[best_pitch[0]];
403
3.49M
      c = xcorr[best_pitch[0]+1];
404
3.49M
      if ((c-a) > MULT16_32_Q15(QCONST16(.7f,15),b-a))
405
1.45M
         offset = 1;
406
2.04M
      else if ((a-c) > MULT16_32_Q15(QCONST16(.7f,15),b-c))
407
39.5k
         offset = -1;
408
2.00M
      else
409
2.00M
         offset = 0;
410
3.49M
   } else {
411
142k
      offset = 0;
412
142k
   }
413
3.64M
   *pitch = 2*best_pitch[0]-offset;
414
415
3.64M
   RESTORE_STACK;
416
3.64M
}
pitch_search
Line
Count
Source
309
1.28M
{
310
1.28M
   int i, j;
311
1.28M
   int lag;
312
1.28M
   int best_pitch[2]={0,0};
313
1.28M
   VARDECL(opus_val16, x_lp4);
314
1.28M
   VARDECL(opus_val16, y_lp4);
315
1.28M
   VARDECL(opus_val32, xcorr);
316
#ifdef FIXED_POINT
317
   opus_val32 maxcorr;
318
   opus_val32 xmax, ymax;
319
   int shift=0;
320
#endif
321
1.28M
   int offset;
322
323
1.28M
   SAVE_STACK;
324
325
1.28M
   celt_assert(len>0);
326
1.28M
   celt_assert(max_pitch>0);
327
1.28M
   lag = len+max_pitch;
328
329
1.28M
   ALLOC(x_lp4, len>>2, opus_val16);
330
1.28M
   ALLOC(y_lp4, lag>>2, opus_val16);
331
1.28M
   ALLOC(xcorr, max_pitch>>1, opus_val32);
332
333
   /* Downsample by 2 again */
334
168M
   for (j=0;j<len>>2;j++)
335
167M
      x_lp4[j] = x_lp[2*j];
336
481M
   for (j=0;j<lag>>2;j++)
337
480M
      y_lp4[j] = y[2*j];
338
339
#ifdef FIXED_POINT
340
   xmax = celt_maxabs16(x_lp4, len>>2);
341
   ymax = celt_maxabs16(y_lp4, lag>>2);
342
   shift = celt_ilog2(MAX32(1, MAX32(xmax, ymax))) - 14 + celt_ilog2(len)/2;
343
   if (shift>0)
344
   {
345
      for (j=0;j<len>>2;j++)
346
         x_lp4[j] = SHR16(x_lp4[j], shift);
347
      for (j=0;j<lag>>2;j++)
348
         y_lp4[j] = SHR16(y_lp4[j], shift);
349
      /* Use double the shift for a MAC */
350
      shift *= 2;
351
   } else {
352
      shift = 0;
353
   }
354
#endif
355
356
   /* Coarse search with 4x decimation */
357
358
#ifdef FIXED_POINT
359
   maxcorr =
360
#endif
361
1.28M
   celt_pitch_xcorr(x_lp4, y_lp4, xcorr, len>>2, max_pitch>>2, arch);
362
363
1.28M
   find_best_pitch(xcorr, y_lp4, len>>2, max_pitch>>2, best_pitch
364
#ifdef FIXED_POINT
365
                   , 0, maxcorr
366
#endif
367
1.28M
                   );
368
369
   /* Finer search with 2x decimation */
370
#ifdef FIXED_POINT
371
   maxcorr=1;
372
#endif
373
627M
   for (i=0;i<max_pitch>>1;i++)
374
625M
   {
375
625M
      opus_val32 sum;
376
625M
      xcorr[i] = 0;
377
625M
      if (abs(i-2*best_pitch[0])>2 && abs(i-2*best_pitch[1])>2)
378
613M
         continue;
379
#ifdef FIXED_POINT
380
      sum = 0;
381
      for (j=0;j<len>>1;j++)
382
         sum += SHR32(MULT16_16(x_lp[j],y[i+j]), shift);
383
#else
384
12.4M
      sum = celt_inner_prod(x_lp, y+i, len>>1, arch);
385
12.4M
#endif
386
12.4M
      xcorr[i] = MAX32(-1, sum);
387
#ifdef FIXED_POINT
388
      maxcorr = MAX32(maxcorr, sum);
389
#endif
390
12.4M
   }
391
1.28M
   find_best_pitch(xcorr, y, len>>1, max_pitch>>1, best_pitch
392
#ifdef FIXED_POINT
393
                   , shift+1, maxcorr
394
#endif
395
1.28M
                   );
396
397
   /* Refine by pseudo-interpolation */
398
1.28M
   if (best_pitch[0]>0 && best_pitch[0]<(max_pitch>>1)-1)
399
1.23M
   {
400
1.23M
      opus_val32 a, b, c;
401
1.23M
      a = xcorr[best_pitch[0]-1];
402
1.23M
      b = xcorr[best_pitch[0]];
403
1.23M
      c = xcorr[best_pitch[0]+1];
404
1.23M
      if ((c-a) > MULT16_32_Q15(QCONST16(.7f,15),b-a))
405
76.7k
         offset = 1;
406
1.16M
      else if ((a-c) > MULT16_32_Q15(QCONST16(.7f,15),b-c))
407
70.2k
         offset = -1;
408
1.09M
      else
409
1.09M
         offset = 0;
410
1.23M
   } else {
411
48.5k
      offset = 0;
412
48.5k
   }
413
1.28M
   *pitch = 2*best_pitch[0]-offset;
414
415
1.28M
   RESTORE_STACK;
416
1.28M
}
417
418
#ifdef FIXED_POINT
419
static opus_val16 compute_pitch_gain(opus_val32 xy, opus_val32 xx, opus_val32 yy)
420
52.7M
{
421
52.7M
   opus_val32 x2y2;
422
52.7M
   int sx, sy, shift;
423
52.7M
   opus_val32 g;
424
52.7M
   opus_val16 den;
425
52.7M
   if (xy == 0 || xx == 0 || yy == 0)
426
1.95M
      return 0;
427
50.7M
   sx = celt_ilog2(xx)-14;
428
50.7M
   sy = celt_ilog2(yy)-14;
429
50.7M
   shift = sx + sy;
430
50.7M
   x2y2 = SHR32(MULT16_16(VSHR32(xx, sx), VSHR32(yy, sy)), 14);
431
50.7M
   if (shift & 1) {
432
887k
      if (x2y2 < 32768)
433
453k
      {
434
453k
         x2y2 <<= 1;
435
453k
         shift--;
436
453k
      } else {
437
434k
         x2y2 >>= 1;
438
434k
         shift++;
439
434k
      }
440
887k
   }
441
50.7M
   den = celt_rsqrt_norm(x2y2);
442
50.7M
   g = MULT16_32_Q15(den, xy);
443
50.7M
   g = VSHR32(g, (shift>>1)-1);
444
50.7M
   return EXTRACT16(MAX32(-Q15ONE, MIN32(g, Q15ONE)));
445
52.7M
}
446
#else
447
static opus_val16 compute_pitch_gain(opus_val32 xy, opus_val32 xx, opus_val32 yy)
448
16.7M
{
449
16.7M
   return xy/celt_sqrt(1+xx*yy);
450
16.7M
}
451
#endif
452
453
static const int second_check[16] = {0, 0, 3, 2, 3, 2, 5, 2, 3, 2, 3, 2, 5, 2, 3, 2};
454
opus_val16 remove_doubling(opus_val16 *x, int maxperiod, int minperiod,
455
      int N, int *T0_, int prev_period, opus_val16 prev_gain, int arch)
456
9.64M
{
457
9.64M
   int k, i, T, T0;
458
9.64M
   opus_val16 g, g0;
459
9.64M
   opus_val16 pg;
460
9.64M
   opus_val32 xy,xx,yy,xy2;
461
9.64M
   opus_val32 xcorr[3];
462
9.64M
   opus_val32 best_xy, best_yy;
463
9.64M
   int offset;
464
9.64M
   int minperiod0;
465
9.64M
   VARDECL(opus_val32, yy_lookup);
466
9.64M
   SAVE_STACK;
467
468
9.64M
   minperiod0 = minperiod;
469
9.64M
   maxperiod /= 2;
470
9.64M
   minperiod /= 2;
471
9.64M
   *T0_ /= 2;
472
9.64M
   prev_period /= 2;
473
9.64M
   N /= 2;
474
9.64M
   x += maxperiod;
475
9.64M
   if (*T0_>=maxperiod)
476
310k
      *T0_=maxperiod-1;
477
478
9.64M
   T = T0 = *T0_;
479
9.64M
   ALLOC(yy_lookup, maxperiod+1, opus_val32);
480
9.64M
   dual_inner_prod(x, x, x-T0, N, &xx, &xy, arch);
481
9.64M
   yy_lookup[0] = xx;
482
9.64M
   yy=xx;
483
4.95G
   for (i=1;i<=maxperiod;i++)
484
4.94G
   {
485
4.94G
      yy = yy+MULT16_16(x[-i],x[-i])-MULT16_16(x[N-i],x[N-i]);
486
4.94G
      yy_lookup[i] = MAX32(0, yy);
487
4.94G
   }
488
9.64M
   yy = yy_lookup[T0];
489
9.64M
   best_xy = xy;
490
9.64M
   best_yy = yy;
491
9.64M
   g = g0 = compute_pitch_gain(xy, xx, yy);
492
   /* Look for any pitch at T/k */
493
138M
   for (k=2;k<=15;k++)
494
130M
   {
495
130M
      int T1, T1b;
496
130M
      opus_val16 g1;
497
130M
      opus_val16 cont=0;
498
130M
      opus_val16 thresh;
499
130M
      T1 = celt_udiv(2*T0+k, 2*k);
500
130M
      if (T1 < minperiod)
501
765k
         break;
502
      /* Look for another strong correlation at T1b */
503
129M
      if (k==2)
504
9.64M
      {
505
9.64M
         if (T1+T0>maxperiod)
506
7.42M
            T1b = T0;
507
2.22M
         else
508
2.22M
            T1b = T0+T1;
509
9.64M
      } else
510
119M
      {
511
119M
         T1b = celt_udiv(2*second_check[k]*T0+k, 2*k);
512
119M
      }
513
129M
      dual_inner_prod(x, &x[-T1], &x[-T1b], N, &xy, &xy2, arch);
514
129M
      xy = HALF32(xy + xy2);
515
129M
      yy = HALF32(yy_lookup[T1] + yy_lookup[T1b]);
516
129M
      g1 = compute_pitch_gain(xy, xx, yy);
517
129M
      if (abs(T1-prev_period)<=1)
518
7.17M
         cont = prev_gain;
519
122M
      else if (abs(T1-prev_period)<=2 && 5*k*k < T0)
520
44.9k
         cont = HALF16(prev_gain);
521
122M
      else
522
122M
         cont = 0;
523
129M
      thresh = MAX16(QCONST16(.3f,15), MULT16_16_Q15(QCONST16(.7f,15),g0)-cont);
524
      /* Bias against very high pitch (very short period) to avoid false-positives
525
         due to short-term correlation */
526
129M
      if (T1<3*minperiod)
527
13.2M
         thresh = MAX16(QCONST16(.4f,15), MULT16_16_Q15(QCONST16(.85f,15),g0)-cont);
528
116M
      else if (T1<2*minperiod)
529
0
         thresh = MAX16(QCONST16(.5f,15), MULT16_16_Q15(QCONST16(.9f,15),g0)-cont);
530
129M
      if (g1 > thresh)
531
76.3M
      {
532
76.3M
         best_xy = xy;
533
76.3M
         best_yy = yy;
534
76.3M
         T = T1;
535
76.3M
         g = g1;
536
76.3M
      }
537
129M
   }
538
9.64M
   best_xy = MAX32(0, best_xy);
539
9.64M
   if (best_yy <= best_xy)
540
6.40M
      pg = Q15ONE;
541
3.24M
   else
542
3.24M
      pg = SHR32(frac_div32(best_xy,best_yy+1),16);
543
544
38.5M
   for (k=0;k<3;k++)
545
28.9M
      xcorr[k] = celt_inner_prod(x, x-(T+k-1), N, arch);
546
9.64M
   if ((xcorr[2]-xcorr[0]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[0]))
547
307k
      offset = 1;
548
9.33M
   else if ((xcorr[0]-xcorr[2]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[2]))
549
333k
      offset = -1;
550
9.00M
   else
551
9.00M
      offset = 0;
552
9.64M
   if (pg > g)
553
3.15M
      pg = g;
554
9.64M
   *T0_ = 2*T+offset;
555
556
9.64M
   if (*T0_<minperiod0)
557
44.2k
      *T0_=minperiod0;
558
9.64M
   RESTORE_STACK;
559
9.64M
   return pg;
560
9.64M
}
remove_doubling
Line
Count
Source
456
4.82M
{
457
4.82M
   int k, i, T, T0;
458
4.82M
   opus_val16 g, g0;
459
4.82M
   opus_val16 pg;
460
4.82M
   opus_val32 xy,xx,yy,xy2;
461
4.82M
   opus_val32 xcorr[3];
462
4.82M
   opus_val32 best_xy, best_yy;
463
4.82M
   int offset;
464
4.82M
   int minperiod0;
465
4.82M
   VARDECL(opus_val32, yy_lookup);
466
4.82M
   SAVE_STACK;
467
468
4.82M
   minperiod0 = minperiod;
469
4.82M
   maxperiod /= 2;
470
4.82M
   minperiod /= 2;
471
4.82M
   *T0_ /= 2;
472
4.82M
   prev_period /= 2;
473
4.82M
   N /= 2;
474
4.82M
   x += maxperiod;
475
4.82M
   if (*T0_>=maxperiod)
476
155k
      *T0_=maxperiod-1;
477
478
4.82M
   T = T0 = *T0_;
479
4.82M
   ALLOC(yy_lookup, maxperiod+1, opus_val32);
480
4.82M
   dual_inner_prod(x, x, x-T0, N, &xx, &xy, arch);
481
4.82M
   yy_lookup[0] = xx;
482
4.82M
   yy=xx;
483
2.47G
   for (i=1;i<=maxperiod;i++)
484
2.47G
   {
485
2.47G
      yy = yy+MULT16_16(x[-i],x[-i])-MULT16_16(x[N-i],x[N-i]);
486
2.47G
      yy_lookup[i] = MAX32(0, yy);
487
2.47G
   }
488
4.82M
   yy = yy_lookup[T0];
489
4.82M
   best_xy = xy;
490
4.82M
   best_yy = yy;
491
4.82M
   g = g0 = compute_pitch_gain(xy, xx, yy);
492
   /* Look for any pitch at T/k */
493
69.4M
   for (k=2;k<=15;k++)
494
65.0M
   {
495
65.0M
      int T1, T1b;
496
65.0M
      opus_val16 g1;
497
65.0M
      opus_val16 cont=0;
498
65.0M
      opus_val16 thresh;
499
65.0M
      T1 = celt_udiv(2*T0+k, 2*k);
500
65.0M
      if (T1 < minperiod)
501
382k
         break;
502
      /* Look for another strong correlation at T1b */
503
64.6M
      if (k==2)
504
4.82M
      {
505
4.82M
         if (T1+T0>maxperiod)
506
3.71M
            T1b = T0;
507
1.11M
         else
508
1.11M
            T1b = T0+T1;
509
4.82M
      } else
510
59.8M
      {
511
59.8M
         T1b = celt_udiv(2*second_check[k]*T0+k, 2*k);
512
59.8M
      }
513
64.6M
      dual_inner_prod(x, &x[-T1], &x[-T1b], N, &xy, &xy2, arch);
514
64.6M
      xy = HALF32(xy + xy2);
515
64.6M
      yy = HALF32(yy_lookup[T1] + yy_lookup[T1b]);
516
64.6M
      g1 = compute_pitch_gain(xy, xx, yy);
517
64.6M
      if (abs(T1-prev_period)<=1)
518
3.58M
         cont = prev_gain;
519
61.0M
      else if (abs(T1-prev_period)<=2 && 5*k*k < T0)
520
22.4k
         cont = HALF16(prev_gain);
521
61.0M
      else
522
61.0M
         cont = 0;
523
64.6M
      thresh = MAX16(QCONST16(.3f,15), MULT16_16_Q15(QCONST16(.7f,15),g0)-cont);
524
      /* Bias against very high pitch (very short period) to avoid false-positives
525
         due to short-term correlation */
526
64.6M
      if (T1<3*minperiod)
527
6.60M
         thresh = MAX16(QCONST16(.4f,15), MULT16_16_Q15(QCONST16(.85f,15),g0)-cont);
528
58.0M
      else if (T1<2*minperiod)
529
0
         thresh = MAX16(QCONST16(.5f,15), MULT16_16_Q15(QCONST16(.9f,15),g0)-cont);
530
64.6M
      if (g1 > thresh)
531
38.1M
      {
532
38.1M
         best_xy = xy;
533
38.1M
         best_yy = yy;
534
38.1M
         T = T1;
535
38.1M
         g = g1;
536
38.1M
      }
537
64.6M
   }
538
4.82M
   best_xy = MAX32(0, best_xy);
539
4.82M
   if (best_yy <= best_xy)
540
3.20M
      pg = Q15ONE;
541
1.62M
   else
542
1.62M
      pg = SHR32(frac_div32(best_xy,best_yy+1),16);
543
544
19.2M
   for (k=0;k<3;k++)
545
14.4M
      xcorr[k] = celt_inner_prod(x, x-(T+k-1), N, arch);
546
4.82M
   if ((xcorr[2]-xcorr[0]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[0]))
547
153k
      offset = 1;
548
4.66M
   else if ((xcorr[0]-xcorr[2]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[2]))
549
166k
      offset = -1;
550
4.50M
   else
551
4.50M
      offset = 0;
552
4.82M
   if (pg > g)
553
1.57M
      pg = g;
554
4.82M
   *T0_ = 2*T+offset;
555
556
4.82M
   if (*T0_<minperiod0)
557
22.1k
      *T0_=minperiod0;
558
4.82M
   RESTORE_STACK;
559
4.82M
   return pg;
560
4.82M
}
remove_doubling
Line
Count
Source
456
4.82M
{
457
4.82M
   int k, i, T, T0;
458
4.82M
   opus_val16 g, g0;
459
4.82M
   opus_val16 pg;
460
4.82M
   opus_val32 xy,xx,yy,xy2;
461
4.82M
   opus_val32 xcorr[3];
462
4.82M
   opus_val32 best_xy, best_yy;
463
4.82M
   int offset;
464
4.82M
   int minperiod0;
465
4.82M
   VARDECL(opus_val32, yy_lookup);
466
4.82M
   SAVE_STACK;
467
468
4.82M
   minperiod0 = minperiod;
469
4.82M
   maxperiod /= 2;
470
4.82M
   minperiod /= 2;
471
4.82M
   *T0_ /= 2;
472
4.82M
   prev_period /= 2;
473
4.82M
   N /= 2;
474
4.82M
   x += maxperiod;
475
4.82M
   if (*T0_>=maxperiod)
476
155k
      *T0_=maxperiod-1;
477
478
4.82M
   T = T0 = *T0_;
479
4.82M
   ALLOC(yy_lookup, maxperiod+1, opus_val32);
480
4.82M
   dual_inner_prod(x, x, x-T0, N, &xx, &xy, arch);
481
4.82M
   yy_lookup[0] = xx;
482
4.82M
   yy=xx;
483
2.47G
   for (i=1;i<=maxperiod;i++)
484
2.47G
   {
485
2.47G
      yy = yy+MULT16_16(x[-i],x[-i])-MULT16_16(x[N-i],x[N-i]);
486
2.47G
      yy_lookup[i] = MAX32(0, yy);
487
2.47G
   }
488
4.82M
   yy = yy_lookup[T0];
489
4.82M
   best_xy = xy;
490
4.82M
   best_yy = yy;
491
4.82M
   g = g0 = compute_pitch_gain(xy, xx, yy);
492
   /* Look for any pitch at T/k */
493
69.4M
   for (k=2;k<=15;k++)
494
65.0M
   {
495
65.0M
      int T1, T1b;
496
65.0M
      opus_val16 g1;
497
65.0M
      opus_val16 cont=0;
498
65.0M
      opus_val16 thresh;
499
65.0M
      T1 = celt_udiv(2*T0+k, 2*k);
500
65.0M
      if (T1 < minperiod)
501
382k
         break;
502
      /* Look for another strong correlation at T1b */
503
64.6M
      if (k==2)
504
4.82M
      {
505
4.82M
         if (T1+T0>maxperiod)
506
3.71M
            T1b = T0;
507
1.11M
         else
508
1.11M
            T1b = T0+T1;
509
4.82M
      } else
510
59.8M
      {
511
59.8M
         T1b = celt_udiv(2*second_check[k]*T0+k, 2*k);
512
59.8M
      }
513
64.6M
      dual_inner_prod(x, &x[-T1], &x[-T1b], N, &xy, &xy2, arch);
514
64.6M
      xy = HALF32(xy + xy2);
515
64.6M
      yy = HALF32(yy_lookup[T1] + yy_lookup[T1b]);
516
64.6M
      g1 = compute_pitch_gain(xy, xx, yy);
517
64.6M
      if (abs(T1-prev_period)<=1)
518
3.58M
         cont = prev_gain;
519
61.0M
      else if (abs(T1-prev_period)<=2 && 5*k*k < T0)
520
22.4k
         cont = HALF16(prev_gain);
521
61.0M
      else
522
61.0M
         cont = 0;
523
64.6M
      thresh = MAX16(QCONST16(.3f,15), MULT16_16_Q15(QCONST16(.7f,15),g0)-cont);
524
      /* Bias against very high pitch (very short period) to avoid false-positives
525
         due to short-term correlation */
526
64.6M
      if (T1<3*minperiod)
527
6.60M
         thresh = MAX16(QCONST16(.4f,15), MULT16_16_Q15(QCONST16(.85f,15),g0)-cont);
528
58.0M
      else if (T1<2*minperiod)
529
0
         thresh = MAX16(QCONST16(.5f,15), MULT16_16_Q15(QCONST16(.9f,15),g0)-cont);
530
64.6M
      if (g1 > thresh)
531
38.1M
      {
532
38.1M
         best_xy = xy;
533
38.1M
         best_yy = yy;
534
38.1M
         T = T1;
535
38.1M
         g = g1;
536
38.1M
      }
537
64.6M
   }
538
4.82M
   best_xy = MAX32(0, best_xy);
539
4.82M
   if (best_yy <= best_xy)
540
3.20M
      pg = Q15ONE;
541
1.62M
   else
542
1.62M
      pg = SHR32(frac_div32(best_xy,best_yy+1),16);
543
544
19.2M
   for (k=0;k<3;k++)
545
14.4M
      xcorr[k] = celt_inner_prod(x, x-(T+k-1), N, arch);
546
4.82M
   if ((xcorr[2]-xcorr[0]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[0]))
547
153k
      offset = 1;
548
4.66M
   else if ((xcorr[0]-xcorr[2]) > MULT16_32_Q15(QCONST16(.7f,15),xcorr[1]-xcorr[2]))
549
166k
      offset = -1;
550
4.50M
   else
551
4.50M
      offset = 0;
552
4.82M
   if (pg > g)
553
1.57M
      pg = g;
554
4.82M
   *T0_ = 2*T+offset;
555
556
4.82M
   if (*T0_<minperiod0)
557
22.1k
      *T0_=minperiod0;
558
4.82M
   RESTORE_STACK;
559
4.82M
   return pg;
560
4.82M
}