Coverage Report

Created: 2026-03-12 06:59

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
430
   ){
29
430
       int     lag, n;
30
430
       float   sum;
31
32
5.16k
       for (lag = 0; lag <= order; lag++) {
33
4.73k
           sum = 0;
34
1.11M
           for (n = 0; n < N - lag; n++) {
35
1.11M
               sum += x[n] * x[n+lag];
36
1.11M
           }
37
4.73k
           r[lag] = sum;
38
4.73k
       }
39
40
41
42
43
44
430
   }
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
860
   ){
56
860
       int     i;
57
58
108k
       for (i = 0; i < N; i++) {
59
107k
           z[i] = x[i] * y[i];
60
107k
       }
61
860
   }
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
430
   ){
74
430
       float  sum, alpha;
75
430
       int     m, m_h, i;
76
77
430
       a[0] = 1.0;
78
79
430
       if (r[0] < EPS) { /* if r[0] <= 0, set LPC coeff. to zero */
80
11
           for (i = 0; i < order; i++) {
81
10
               k[i] = 0;
82
10
               a[i+1] = 0;
83
10
           }
84
429
       } else {
85
429
           a[1] = k[0] = -r[1]/r[0];
86
429
           alpha = r[0] + r[1] * k[0];
87
4.29k
           for (m = 1; m < order; m++){
88
3.86k
               sum = r[m + 1];
89
23.1k
               for (i = 0; i < m; i++){
90
19.3k
                   sum += a[i+1] * r[m - i];
91
19.3k
               }
92
93
94
95
96
97
3.86k
               k[m] = -sum / alpha;
98
3.86k
               alpha += k[m] * sum;
99
3.86k
               m_h = (m + 1) >> 1;
100
14.5k
               for (i = 0; i < m_h; i++){
101
10.7k
                   sum = a[i+1] + k[m] * a[m - i];
102
10.7k
                   a[m - i] += k[m] * a[i+1];
103
10.7k
                   a[i+1] = sum;
104
10.7k
               }
105
3.86k
               a[m+1] = k[m];
106
3.86k
           }
107
429
       }
108
430
   }
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
3.87k
   ){
123
3.87k
       int i;
124
3.87k
       float invcoef;
125
126
3.87k
       invcoef = (float)1.0 - coef;
127
42.5k
       for (i = 0; i < length; i++) {
128
38.7k
           out[i] = coef * in1[i] + invcoef * in2[i];
129
38.7k
       }
130
3.87k
   }
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
3.01k
   ){
144
3.01k
       int i;
145
146
147
148
149
150
3.01k
       float  chirp;
151
152
3.01k
       chirp = coef;
153
154
3.01k
       out[0] = in[0];
155
33.1k
       for (i = 1; i < length; i++) {
156
30.1k
           out[i] = chirp * in[i];
157
30.1k
           chirp *= coef;
158
30.1k
       }
159
3.01k
   }
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
1.29k
   ){
173
1.29k
       int     i, j;
174
1.29k
       int     pos, minindex;
175
1.29k
       float   dist, tmp, mindist;
176
177
1.29k
       pos = 0;
178
1.29k
       mindist = FLOAT_MAX;
179
1.29k
       minindex = 0;
180
138k
       for (j = 0; j < n_cb; j++) {
181
137k
           dist = X[0] - CB[pos];
182
137k
           dist *= dist;
183
467k
           for (i = 1; i < dim; i++) {
184
330k
               tmp = X[i] - CB[pos + i];
185
330k
               dist += tmp*tmp;
186
330k
           }
187
188
137k
           if (dist < mindist) {
189
6.90k
               mindist = dist;
190
6.90k
               minindex = j;
191
6.90k
           }
192
137k
           pos += dim;
193
137k
       }
194
5.59k
       for (i = 0; i < dim; i++) {
195
4.30k
           Xq[i] = CB[minindex*dim + i];
196
4.30k
       }
197
1.29k
       *index = minindex;
198
199
200
201
202
203
1.29k
   }
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
430
   ){
219
430
       int    cb_pos, X_pos, i;
220
221
430
       cb_pos = 0;
222
430
       X_pos= 0;
223
1.72k
       for (i = 0; i < nsplit; i++) {
224
1.29k
           vq(qX + X_pos, index + i, CB + cb_pos, X + X_pos,
225
1.29k
               cbsize[i], dim[i]);
226
1.29k
           X_pos += dim[i];
227
1.29k
           cb_pos += dim[i] * cbsize[i];
228
1.29k
       }
229
430
   }
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
12.6k
   ){
242
12.6k
       int i;
243
244
12.6k
       if (x <= cb[0]) {
245
1.60k
           *index = 0;
246
1.60k
           *xq = cb[0];
247
11.0k
       } else {
248
11.0k
           i = 0;
249
72.6k
           while ((x > cb[i]) && i < cb_size - 1) {
250
61.5k
               i++;
251
252
253
254
255
256
61.5k
           }
257
258
11.0k
           if (x > ((cb[i] + cb[i - 1])/2)) {
259
6.24k
               *index = i;
260
6.24k
               *xq = cb[i];
261
6.24k
           } else {
262
4.83k
               *index = i - 1;
263
4.83k
               *xq = cb[i - 1];
264
4.83k
           }
265
11.0k
       }
266
12.6k
   }
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
430
   ){
279
430
       int k,n,m, Nit=2, change=0,pos;
280
       //float tmp;
281
430
       static float eps=(float)0.039; /* 50 Hz */
282
430
       static float eps2=(float)0.0195;
283
430
       static float maxlsf=(float)3.14; /* 4000 Hz */
284
430
       static float minlsf=(float)0.01; /* 0 Hz */
285
286
       /* LSF separation check*/
287
288
1.29k
       for (n=0; n<Nit; n++) { /* Run through a couple of times */
289
2.58k
           for (m=0; m<NoAn; m++) { /* Number of analyses per frame */
290
17.2k
               for (k=0; k<(dim-1); k++) {
291
15.4k
                   pos=m*dim+k;
292
293
15.4k
                   if ((lsf[pos+1]-lsf[pos])<eps) {
294
295
22
                       if (lsf[pos+1]<lsf[pos]) {
296
                           //tmp=lsf[pos+1];
297
8
                           lsf[pos+1]= lsf[pos]+eps2;
298
8
                           lsf[pos]= lsf[pos+1]-eps2;
299
14
                       } else {
300
14
                           lsf[pos]-=eps2;
301
14
                           lsf[pos+1]+=eps2;
302
14
                       }
303
22
                       change=1;
304
305
306
307
308
309
22
                   }
310
311
15.4k
                   if (lsf[pos]<minlsf) {
312
0
                       lsf[pos]=minlsf;
313
0
                       change=1;
314
0
                   }
315
316
15.4k
                   if (lsf[pos]>maxlsf) {
317
0
                       lsf[pos]=maxlsf;
318
0
                       change=1;
319
0
                   }
320
15.4k
               }
321
1.72k
           }
322
860
       }
323
324
430
       return change;
325
430
   }
326