Coverage Report

Created: 2026-06-15 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pjsip/third_party/ilbc/createCB.c
Line
Count
Source
1
2
3
   /******************************************************************
4
5
       iLBC Speech Coder ANSI-C Source Code
6
7
       createCB.c
8
9
       Copyright (C) The Internet Society (2004).
10
       All Rights Reserved.
11
12
   ******************************************************************/
13
14
15
16
17
18
   #include "iLBC_define.h"
19
   #include "constants.h"
20
   #include <string.h>
21
   #include <math.h>
22
23
   /*----------------------------------------------------------------*
24
    *  Construct an additional codebook vector by filtering the
25
    *  initial codebook buffer. This vector is then used to expand
26
    *  the codebook with an additional section.
27
    *---------------------------------------------------------------*/
28
29
   void filteredCBvecs(
30
       float *cbvectors,   /* (o) Codebook vectors for the
31
                                  higher section */
32
       float *mem,         /* (i) Buffer to create codebook
33
                                  vector from */
34
       int lMem        /* (i) Length of buffer */
35
1.90k
   ){
36
1.90k
       int j, k;
37
1.90k
       float *pp, *pp1;
38
1.90k
       float tempbuff2[CB_MEML+CB_FILTERLEN];
39
1.90k
       float *pos;
40
41
1.90k
       memset(tempbuff2, 0, (CB_HALFFILTERLEN-1)*sizeof(float));
42
1.90k
       memcpy(&tempbuff2[CB_HALFFILTERLEN-1], mem, lMem*sizeof(float));
43
1.90k
       memset(&tempbuff2[lMem+CB_HALFFILTERLEN-1], 0,
44
1.90k
           (CB_HALFFILTERLEN+1)*sizeof(float));
45
46
       /* Create codebook vector for higher section by filtering */
47
48
       /* do filtering */
49
1.90k
       pos=cbvectors;
50
1.90k
       memset(pos, 0, lMem*sizeof(float));
51
257k
       for (k=0; k<lMem; k++) {
52
255k
           pp=&tempbuff2[k];
53
255k
           pp1=&cbfiltersTbl[CB_FILTERLEN-1];
54
2.30M
           for (j=0;j<CB_FILTERLEN;j++) {
55
2.04M
               (*pos)+=(*pp++)*(*pp1--);
56
2.04M
           }
57
255k
           pos++;
58
255k
       }
59
1.90k
   }
60
61
   /*----------------------------------------------------------------*
62
    *  Search the augmented part of the codebook to find the best
63
    *  measure.
64
    *----------------------------------------------------------------*/
65
66
67
68
69
70
71
   void searchAugmentedCB(
72
       int low,        /* (i) Start index for the search */
73
       int high,           /* (i) End index for the search */
74
       int stage,          /* (i) Current stage */
75
       int startIndex,     /* (i) Codebook index for the first
76
                                  aug vector */
77
       float *target,      /* (i) Target vector for encoding */
78
       float *buffer,      /* (i) Pointer to the end of the buffer for
79
                                  augmented codebook construction */
80
       float *max_measure, /* (i/o) Currently maximum measure */
81
       int *best_index,/* (o) Currently the best index */
82
       float *gain,    /* (o) Currently the best gain */
83
       float *energy,      /* (o) Energy of augmented codebook
84
                                  vectors */
85
       float *invenergy/* (o) Inv energy of augmented codebook
86
                                  vectors */
87
6.48k
   ) {
88
6.48k
       int icount, ilow, j, tmpIndex;
89
6.48k
       float *pp, *ppo, *ppi, *ppe, crossDot, alfa;
90
6.48k
       float weighted, measure, nrjRecursive;
91
6.48k
       float ftmp;
92
93
       /* Compute the energy for the first (low-5)
94
          noninterpolated samples */
95
6.48k
       nrjRecursive = (float) 0.0;
96
6.48k
       pp = buffer - low + 1;
97
111k
       for (j=0; j<(low-5); j++) {
98
104k
           nrjRecursive += ( (*pp)*(*pp) );
99
104k
           pp++;
100
104k
       }
101
6.48k
       ppe = buffer - low;
102
103
104
128k
       for (icount=low; icount<=high; icount++) {
105
106
           /* Index of the codebook vector used for retrieving
107
              energy values */
108
122k
           tmpIndex = startIndex+icount-20;
109
110
122k
           ilow = icount-4;
111
112
           /* Update the energy recursively to save complexity */
113
122k
           nrjRecursive = nrjRecursive + (*ppe)*(*ppe);
114
122k
           ppe--;
115
122k
           energy[tmpIndex] = nrjRecursive;
116
117
           /* Compute cross dot product for the first (low-5)
118
              samples */
119
120
121
122
123
124
122k
           crossDot = (float) 0.0;
125
122k
           pp = buffer-icount;
126
3.26M
           for (j=0; j<ilow; j++) {
127
3.14M
               crossDot += target[j]*(*pp++);
128
3.14M
           }
129
130
           /* interpolation */
131
122k
           alfa = (float) 0.2;
132
122k
           ppo = buffer-4;
133
122k
           ppi = buffer-icount-4;
134
610k
           for (j=ilow; j<icount; j++) {
135
488k
               weighted = ((float)1.0-alfa)*(*ppo)+alfa*(*ppi);
136
488k
               ppo++;
137
488k
               ppi++;
138
488k
               energy[tmpIndex] += weighted*weighted;
139
488k
               crossDot += target[j]*weighted;
140
488k
               alfa += (float)0.2;
141
488k
           }
142
143
           /* Compute energy and cross dot product for the
144
              remaining samples */
145
122k
           pp = buffer - icount;
146
1.37M
           for (j=icount; j<SUBL; j++) {
147
1.25M
               energy[tmpIndex] += (*pp)*(*pp);
148
1.25M
               crossDot += target[j]*(*pp++);
149
1.25M
           }
150
151
122k
           if (energy[tmpIndex]>0.0) {
152
121k
               invenergy[tmpIndex]=(float)1.0/(energy[tmpIndex]+EPS);
153
121k
           } else {
154
180
               invenergy[tmpIndex] = (float) 0.0;
155
180
           }
156
157
122k
           if (stage==0) {
158
40.3k
               measure = (float)-10000000.0;
159
160
40.3k
               if (crossDot > 0.0) {
161
18.4k
                   measure = crossDot*crossDot*invenergy[tmpIndex];
162
18.4k
               }
163
40.3k
           }
164
81.8k
           else {
165
81.8k
               measure = crossDot*crossDot*invenergy[tmpIndex];
166
81.8k
           }
167
168
           /* check if measure is better */
169
122k
           ftmp = crossDot*invenergy[tmpIndex];
170
171
122k
           if ((measure>*max_measure) && (fabs(ftmp)<CB_MAXGAIN)) {
172
173
174
175
176
177
2.58k
               *best_index = tmpIndex;
178
2.58k
               *max_measure = measure;
179
2.58k
               *gain = ftmp;
180
2.58k
           }
181
122k
       }
182
6.48k
   }
183
184
185
   /*----------------------------------------------------------------*
186
    *  Recreate a specific codebook vector from the augmented part.
187
    *
188
    *----------------------------------------------------------------*/
189
190
   void createAugmentedVec(
191
       int index,      /* (i) Index for the augmented vector
192
                              to be created */
193
       float *buffer,  /* (i) Pointer to the end of the buffer for
194
                              augmented codebook construction */
195
       float *cbVec/* (o) The construced codebook vector */
196
1.19k
   ) {
197
1.19k
       int ilow, j;
198
1.19k
       float *pp, *ppo, *ppi, alfa, alfa1, weighted;
199
200
1.19k
       ilow = index-5;
201
202
       /* copy the first noninterpolated part */
203
204
1.19k
       pp = buffer-index;
205
1.19k
       memcpy(cbVec,pp,sizeof(float)*index);
206
207
       /* interpolation */
208
209
1.19k
       alfa1 = (float)0.2;
210
1.19k
       alfa = 0.0;
211
1.19k
       ppo = buffer-5;
212
1.19k
       ppi = buffer-index-5;
213
7.18k
       for (j=ilow; j<index; j++) {
214
5.98k
           weighted = ((float)1.0-alfa)*(*ppo)+alfa*(*ppi);
215
5.98k
           ppo++;
216
5.98k
           ppi++;
217
5.98k
           cbVec[j] = weighted;
218
5.98k
           alfa += alfa1;
219
5.98k
       }
220
221
       /* copy the second noninterpolated part */
222
223
1.19k
       pp = buffer - index;
224
1.19k
       memcpy(cbVec+index,pp,sizeof(float)*(SUBL-index));
225
226
227
228
229
230
1.19k
   }
231