Coverage Report

Created: 2026-07-30 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pjsip/third_party/ilbc/helpfun.c
Line
Count
Source
1
2
   /******************************************************************
3
4
       iLBC Speech Coder ANSI-C Source Code
5
6
       helpfun.c
7
8
       Copyright (C) The Internet Society (2004).
9
       All Rights Reserved.
10
11
   ******************************************************************/
12
13
   #include <math.h>
14
15
   #include "iLBC_define.h"
16
   #include "constants.h"
17
18
   /*----------------------------------------------------------------*
19
    *  calculation of auto correlation
20
    *---------------------------------------------------------------*/
21
22
   void autocorr(
23
       float *r,       /* (o) autocorrelation vector */
24
       const float *x, /* (i) data vector */
25
       int N,          /* (i) length of data vector */
26
       int order       /* largest lag for calculated
27
                          autocorrelations */
28
922
   ){
29
922
       int     lag, n;
30
922
       float   sum;
31
32
11.0k
       for (lag = 0; lag <= order; lag++) {
33
10.1k
           sum = 0;
34
2.39M
           for (n = 0; n < N - lag; n++) {
35
2.38M
               sum += x[n] * x[n+lag];
36
2.38M
           }
37
10.1k
           r[lag] = sum;
38
10.1k
       }
39
40
41
42
43
44
922
   }
45
46
   /*----------------------------------------------------------------*
47
    *  window multiplication
48
    *---------------------------------------------------------------*/
49
50
   void window(
51
       float *z,       /* (o) the windowed data */
52
       const float *x, /* (i) the original data vector */
53
       const float *y, /* (i) the window */
54
       int N           /* (i) length of all vectors */
55
1.84k
   ){
56
1.84k
       int     i;
57
58
233k
       for (i = 0; i < N; i++) {
59
231k
           z[i] = x[i] * y[i];
60
231k
       }
61
1.84k
   }
62
63
   /*----------------------------------------------------------------*
64
    *  levinson-durbin solution for lpc coefficients
65
    *---------------------------------------------------------------*/
66
67
   void levdurb(
68
       float *a,       /* (o) lpc coefficient vector starting
69
                              with 1.0 */
70
       float *k,       /* (o) reflection coefficients */
71
       float *r,       /* (i) autocorrelation vector */
72
       int order       /* (i) order of lpc filter */
73
922
   ){
74
922
       float  sum, alpha;
75
922
       int     m, m_h, i;
76
77
922
       a[0] = 1.0;
78
79
922
       if (r[0] < EPS) { /* if r[0] <= 0, set LPC coeff. to zero */
80
22
           for (i = 0; i < order; i++) {
81
20
               k[i] = 0;
82
20
               a[i+1] = 0;
83
20
           }
84
920
       } else {
85
920
           a[1] = k[0] = -r[1]/r[0];
86
920
           alpha = r[0] + r[1] * k[0];
87
9.20k
           for (m = 1; m < order; m++){
88
8.28k
               sum = r[m + 1];
89
49.6k
               for (i = 0; i < m; i++){
90
41.4k
                   sum += a[i+1] * r[m - i];
91
41.4k
               }
92
93
94
95
96
97
8.28k
               k[m] = -sum / alpha;
98
8.28k
               alpha += k[m] * sum;
99
8.28k
               m_h = (m + 1) >> 1;
100
31.2k
               for (i = 0; i < m_h; i++){
101
23.0k
                   sum = a[i+1] + k[m] * a[m - i];
102
23.0k
                   a[m - i] += k[m] * a[i+1];
103
23.0k
                   a[i+1] = sum;
104
23.0k
               }
105
8.28k
               a[m+1] = k[m];
106
8.28k
           }
107
920
       }
108
922
   }
109
110
   /*----------------------------------------------------------------*
111
    *  interpolation between vectors
112
    *---------------------------------------------------------------*/
113
114
   void interpolate(
115
       float *out,      /* (o) the interpolated vector */
116
       float *in1,     /* (i) the first vector for the
117
                              interpolation */
118
       float *in2,     /* (i) the second vector for the
119
                              interpolation */
120
       float coef,      /* (i) interpolation weights */
121
       int length      /* (i) length of all vectors */
122
6.64k
   ){
123
6.64k
       int i;
124
6.64k
       float invcoef;
125
126
6.64k
       invcoef = (float)1.0 - coef;
127
73.0k
       for (i = 0; i < length; i++) {
128
66.4k
           out[i] = coef * in1[i] + invcoef * in2[i];
129
66.4k
       }
130
6.64k
   }
131
132
   /*----------------------------------------------------------------*
133
    *  lpc bandwidth expansion
134
    *---------------------------------------------------------------*/
135
136
   void bwexpand(
137
       float *out,      /* (o) the bandwidth expanded lpc
138
                              coefficients */
139
       float *in,      /* (i) the lpc coefficients before bandwidth
140
                              expansion */
141
       float coef,     /* (i) the bandwidth expansion factor */
142
       int length      /* (i) the length of lpc coefficient vectors */
143
4.79k
   ){
144
4.79k
       int i;
145
146
147
148
149
150
4.79k
       float  chirp;
151
152
4.79k
       chirp = coef;
153
154
4.79k
       out[0] = in[0];
155
52.7k
       for (i = 1; i < length; i++) {
156
47.9k
           out[i] = chirp * in[i];
157
47.9k
           chirp *= coef;
158
47.9k
       }
159
4.79k
   }
160
161
   /*----------------------------------------------------------------*
162
    *  vector quantization
163
    *---------------------------------------------------------------*/
164
165
   void vq(
166
       float *Xq,      /* (o) the quantized vector */
167
       int *index,     /* (o) the quantization index */
168
       const float *CB,/* (i) the vector quantization codebook */
169
       float *X,       /* (i) the vector to quantize */
170
       int n_cb,       /* (i) the number of vectors in the codebook */
171
       int dim         /* (i) the dimension of all vectors */
172
2.76k
   ){
173
2.76k
       int     i, j;
174
2.76k
       int     pos, minindex;
175
2.76k
       float   dist, tmp, mindist;
176
177
2.76k
       pos = 0;
178
2.76k
       mindist = FLOAT_MAX;
179
2.76k
       minindex = 0;
180
297k
       for (j = 0; j < n_cb; j++) {
181
295k
           dist = X[0] - CB[pos];
182
295k
           dist *= dist;
183
1.00M
           for (i = 1; i < dim; i++) {
184
708k
               tmp = X[i] - CB[pos + i];
185
708k
               dist += tmp*tmp;
186
708k
           }
187
188
295k
           if (dist < mindist) {
189
14.1k
               mindist = dist;
190
14.1k
               minindex = j;
191
14.1k
           }
192
295k
           pos += dim;
193
295k
       }
194
11.9k
       for (i = 0; i < dim; i++) {
195
9.22k
           Xq[i] = CB[minindex*dim + i];
196
9.22k
       }
197
2.76k
       *index = minindex;
198
199
200
201
202
203
2.76k
   }
204
205
   /*----------------------------------------------------------------*
206
    *  split vector quantization
207
    *---------------------------------------------------------------*/
208
209
   void SplitVQ(
210
       float *qX,      /* (o) the quantized vector */
211
       int *index,     /* (o) a vector of indexes for all vector
212
                              codebooks in the split */
213
       float *X,       /* (i) the vector to quantize */
214
       const float *CB,/* (i) the quantizer codebook */
215
       int nsplit,     /* the number of vector splits */
216
       const int *dim, /* the dimension of X and qX */
217
       const int *cbsize /* the number of vectors in the codebook */
218
922
   ){
219
922
       int    cb_pos, X_pos, i;
220
221
922
       cb_pos = 0;
222
922
       X_pos= 0;
223
3.68k
       for (i = 0; i < nsplit; i++) {
224
2.76k
           vq(qX + X_pos, index + i, CB + cb_pos, X + X_pos,
225
2.76k
               cbsize[i], dim[i]);
226
2.76k
           X_pos += dim[i];
227
2.76k
           cb_pos += dim[i] * cbsize[i];
228
2.76k
       }
229
922
   }
230
231
   /*----------------------------------------------------------------*
232
    *  scalar quantization
233
    *---------------------------------------------------------------*/
234
235
   void sort_sq(
236
       float *xq,      /* (o) the quantized value */
237
       int *index,     /* (o) the quantization index */
238
       float x,    /* (i) the value to quantize */
239
       const float *cb,/* (i) the quantization codebook */
240
       int cb_size      /* (i) the size of the quantization codebook */
241
27.1k
   ){
242
27.1k
       int i;
243
244
27.1k
       if (x <= cb[0]) {
245
4.88k
           *index = 0;
246
4.88k
           *xq = cb[0];
247
22.3k
       } else {
248
22.3k
           i = 0;
249
150k
           while ((x > cb[i]) && i < cb_size - 1) {
250
128k
               i++;
251
252
253
254
255
256
128k
           }
257
258
22.3k
           if (x > ((cb[i] + cb[i - 1])/2)) {
259
12.9k
               *index = i;
260
12.9k
               *xq = cb[i];
261
12.9k
           } else {
262
9.37k
               *index = i - 1;
263
9.37k
               *xq = cb[i - 1];
264
9.37k
           }
265
22.3k
       }
266
27.1k
   }
267
268
   /*----------------------------------------------------------------*
269
    *  check for stability of lsf coefficients
270
    *---------------------------------------------------------------*/
271
272
   int LSF_check(    /* (o) 1 for stable lsf vectors and 0 for
273
                              nonstable ones */
274
       float *lsf,     /* (i) a table of lsf vectors */
275
       int dim,    /* (i) the dimension of each lsf vector */
276
       int NoAn    /* (i) the number of lsf vectors in the
277
                              table */
278
646
   ){
279
646
       int k,n,m, Nit=2, change=0,pos;
280
       //float tmp;
281
646
       static float eps=(float)0.039; /* 50 Hz */
282
646
       static float eps2=(float)0.0195;
283
646
       static float maxlsf=(float)3.14; /* 4000 Hz */
284
646
       static float minlsf=(float)0.01; /* 0 Hz */
285
286
       /* LSF separation check*/
287
288
1.93k
       for (n=0; n<Nit; n++) { /* Run through a couple of times */
289
3.87k
           for (m=0; m<NoAn; m++) { /* Number of analyses per frame */
290
25.8k
               for (k=0; k<(dim-1); k++) {
291
23.2k
                   pos=m*dim+k;
292
293
23.2k
                   if ((lsf[pos+1]-lsf[pos])<eps) {
294
295
30
                       if (lsf[pos+1]<lsf[pos]) {
296
                           //tmp=lsf[pos+1];
297
12
                           lsf[pos+1]= lsf[pos]+eps2;
298
12
                           lsf[pos]= lsf[pos+1]-eps2;
299
18
                       } else {
300
18
                           lsf[pos]-=eps2;
301
18
                           lsf[pos+1]+=eps2;
302
18
                       }
303
30
                       change=1;
304
305
306
307
308
309
30
                   }
310
311
23.2k
                   if (lsf[pos]<minlsf) {
312
0
                       lsf[pos]=minlsf;
313
0
                       change=1;
314
0
                   }
315
316
23.2k
                   if (lsf[pos]>maxlsf) {
317
0
                       lsf[pos]=maxlsf;
318
0
                       change=1;
319
0
                   }
320
23.2k
               }
321
2.58k
           }
322
1.29k
       }
323
324
646
       return change;
325
646
   }
326