Coverage Report

Created: 2026-09-14 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pjsip/third_party/ilbc/iLBC_encode.c
Line
Count
Source
1
2
   /******************************************************************
3
4
       iLBC Speech Coder ANSI-C Source Code
5
6
       iLBC_encode.c
7
8
       Copyright (C) The Internet Society (2004).
9
       All Rights Reserved.
10
11
   ******************************************************************/
12
13
   #include <math.h>
14
   #include <stdlib.h>
15
   #include <string.h>
16
17
   #include "iLBC_define.h"
18
   #include "LPCencode.h"
19
   #include "FrameClassify.h"
20
   #include "StateSearchW.h"
21
   #include "StateConstructW.h"
22
   #include "helpfun.h"
23
   #include "constants.h"
24
   #include "packing.h"
25
   #include "iCBSearch.h"
26
   #include "iCBConstruct.h"
27
   #include "hpInput.h"
28
   #include "anaFilter.h"
29
   #include "syntFilter.h"
30
31
   /*----------------------------------------------------------------*
32
    *  Initiation of encoder instance.
33
    *---------------------------------------------------------------*/
34
35
   short initEncode(                   /* (o) Number of bytes
36
                                              encoded */
37
       iLBC_Enc_Inst_t *iLBCenc_inst,  /* (i/o) Encoder instance */
38
       int mode                    /* (i) frame size mode */
39
183
   ){
40
183
       iLBCenc_inst->mode = mode;
41
183
       if (mode==30) {
42
183
           iLBCenc_inst->blockl = BLOCKL_30MS;
43
183
           iLBCenc_inst->nsub = NSUB_30MS;
44
183
           iLBCenc_inst->nasub = NASUB_30MS;
45
183
           iLBCenc_inst->lpc_n = LPC_N_30MS;
46
183
           iLBCenc_inst->no_of_bytes = NO_OF_BYTES_30MS;
47
183
           iLBCenc_inst->no_of_words = NO_OF_WORDS_30MS;
48
49
50
51
52
53
183
           iLBCenc_inst->state_short_len=STATE_SHORT_LEN_30MS;
54
           /* ULP init */
55
183
           iLBCenc_inst->ULP_inst=&ULP_30msTbl;
56
183
       }
57
0
       else if (mode==20) {
58
0
           iLBCenc_inst->blockl = BLOCKL_20MS;
59
0
           iLBCenc_inst->nsub = NSUB_20MS;
60
0
           iLBCenc_inst->nasub = NASUB_20MS;
61
0
           iLBCenc_inst->lpc_n = LPC_N_20MS;
62
0
           iLBCenc_inst->no_of_bytes = NO_OF_BYTES_20MS;
63
0
           iLBCenc_inst->no_of_words = NO_OF_WORDS_20MS;
64
0
           iLBCenc_inst->state_short_len=STATE_SHORT_LEN_20MS;
65
           /* ULP init */
66
0
           iLBCenc_inst->ULP_inst=&ULP_20msTbl;
67
0
       }
68
0
       else {
69
0
           exit(2);
70
0
       }
71
72
183
       memset((*iLBCenc_inst).anaMem, 0,
73
183
           LPC_FILTERORDER*sizeof(float));
74
183
       memcpy((*iLBCenc_inst).lsfold, lsfmeanTbl,
75
183
           LPC_FILTERORDER*sizeof(float));
76
183
       memcpy((*iLBCenc_inst).lsfdeqold, lsfmeanTbl,
77
183
           LPC_FILTERORDER*sizeof(float));
78
183
       memset((*iLBCenc_inst).lpc_buffer, 0,
79
183
           (LPC_LOOKBACK+BLOCKL_MAX)*sizeof(float));
80
183
       memset((*iLBCenc_inst).hpimem, 0, 4*sizeof(float));
81
82
183
       return (short)(iLBCenc_inst->no_of_bytes);
83
183
   }
84
85
   /*----------------------------------------------------------------*
86
    *  main encoder function
87
    *---------------------------------------------------------------*/
88
89
   void iLBC_encode(
90
       unsigned char *bytes,           /* (o) encoded data bits iLBC */
91
       float *block,                   /* (o) speech vector to
92
                                              encode */
93
       iLBC_Enc_Inst_t *iLBCenc_inst   /* (i/o) the general encoder
94
                                              state */
95
449
   ){
96
97
449
       float data[BLOCKL_MAX];
98
449
       float residual[BLOCKL_MAX], reverseResidual[BLOCKL_MAX];
99
100
449
       int start, idxForMax, idxVec[STATE_LEN];
101
102
103
104
105
106
449
       float reverseDecresidual[BLOCKL_MAX], mem[CB_MEML];
107
449
       int n, k, meml_gotten, Nfor, Nback, i, pos;
108
449
       int gain_index[CB_NSTAGES*NASUB_MAX],
109
449
           extra_gain_index[CB_NSTAGES];
110
449
       int cb_index[CB_NSTAGES*NASUB_MAX],extra_cb_index[CB_NSTAGES];
111
449
       int lsf_i[LSF_NSPLIT*LPC_N_MAX];
112
449
       unsigned char *pbytes;
113
449
       int diff, start_pos, state_first;
114
449
       float en1, en2;
115
449
       int index, ulp, firstpart;
116
449
       int subcount, subframe;
117
449
       float weightState[LPC_FILTERORDER];
118
449
       float syntdenum[NSUB_MAX*(LPC_FILTERORDER+1)];
119
449
       float weightdenum[NSUB_MAX*(LPC_FILTERORDER+1)];
120
449
       float decresidual[BLOCKL_MAX];
121
122
       /* high pass filtering of input signal if such is not done
123
              prior to calling this function */
124
125
449
       hpInput(block, iLBCenc_inst->blockl,
126
449
                   data, (*iLBCenc_inst).hpimem);
127
128
       /* otherwise simply copy */
129
130
       /*memcpy(data,block,iLBCenc_inst->blockl*sizeof(float));*/
131
132
       /* LPC of hp filtered input data */
133
134
449
       LPCencode(syntdenum, weightdenum, lsf_i, data, iLBCenc_inst);
135
136
137
       /* inverse filter to get residual */
138
139
3.14k
       for (n=0; n<iLBCenc_inst->nsub; n++) {
140
2.69k
           anaFilter(&data[n*SUBL], &syntdenum[n*(LPC_FILTERORDER+1)],
141
2.69k
               SUBL, &residual[n*SUBL], iLBCenc_inst->anaMem);
142
2.69k
       }
143
144
       /* find state location */
145
146
449
       start = FrameClassify(iLBCenc_inst, residual);
147
148
       /* check if state should be in first or last part of the
149
       two subframes */
150
151
449
       diff = STATE_LEN - iLBCenc_inst->state_short_len;
152
449
       en1 = 0;
153
449
       index = (start-1)*SUBL;
154
155
156
157
158
159
26.4k
       for (i = 0; i < iLBCenc_inst->state_short_len; i++) {
160
26.0k
           en1 += residual[index+i]*residual[index+i];
161
26.0k
       }
162
449
       en2 = 0;
163
449
       index = (start-1)*SUBL+diff;
164
26.4k
       for (i = 0; i < iLBCenc_inst->state_short_len; i++) {
165
26.0k
           en2 += residual[index+i]*residual[index+i];
166
26.0k
       }
167
168
169
449
       if (en1 > en2) {
170
196
           state_first = 1;
171
196
           start_pos = (start-1)*SUBL;
172
253
       } else {
173
253
           state_first = 0;
174
253
           start_pos = (start-1)*SUBL + diff;
175
253
       }
176
177
       /* scalar quantization of state */
178
179
449
       StateSearchW(iLBCenc_inst, &residual[start_pos],
180
449
           &syntdenum[(start-1)*(LPC_FILTERORDER+1)],
181
449
           &weightdenum[(start-1)*(LPC_FILTERORDER+1)], &idxForMax,
182
449
           idxVec, iLBCenc_inst->state_short_len, state_first);
183
184
449
       StateConstructW(idxForMax, idxVec,
185
449
           &syntdenum[(start-1)*(LPC_FILTERORDER+1)],
186
449
           &decresidual[start_pos], iLBCenc_inst->state_short_len);
187
188
       /* predictive quantization in state */
189
190
449
       if (state_first) { /* put adaptive part in the end */
191
192
           /* setup memory */
193
194
196
           memset(mem, 0,
195
196
               (CB_MEML-iLBCenc_inst->state_short_len)*sizeof(float));
196
196
           memcpy(mem+CB_MEML-iLBCenc_inst->state_short_len,
197
196
               decresidual+start_pos,
198
196
               iLBCenc_inst->state_short_len*sizeof(float));
199
196
           memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
200
201
           /* encode sub-frames */
202
203
196
           iCBSearch(iLBCenc_inst, extra_cb_index, extra_gain_index,
204
196
               &residual[start_pos+iLBCenc_inst->state_short_len],
205
196
               mem+CB_MEML-stMemLTbl,
206
196
               stMemLTbl, diff, CB_NSTAGES,
207
208
209
210
211
212
196
               &weightdenum[start*(LPC_FILTERORDER+1)],
213
196
               weightState, 0);
214
215
           /* construct decoded vector */
216
217
196
           iCBConstruct(
218
196
               &decresidual[start_pos+iLBCenc_inst->state_short_len],
219
196
               extra_cb_index, extra_gain_index,
220
196
               mem+CB_MEML-stMemLTbl,
221
196
               stMemLTbl, diff, CB_NSTAGES);
222
223
196
       }
224
253
       else { /* put adaptive part in the beginning */
225
226
           /* create reversed vectors for prediction */
227
228
5.81k
           for (k=0; k<diff; k++) {
229
5.56k
               reverseResidual[k] = residual[(start+1)*SUBL-1
230
5.56k
                   -(k+iLBCenc_inst->state_short_len)];
231
5.56k
           }
232
233
           /* setup memory */
234
235
253
           meml_gotten = iLBCenc_inst->state_short_len;
236
14.9k
           for (k=0; k<meml_gotten; k++) {
237
14.6k
               mem[CB_MEML-1-k] = decresidual[start_pos + k];
238
14.6k
           }
239
253
           memset(mem, 0, (CB_MEML-k)*sizeof(float));
240
253
           memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
241
242
           /* encode sub-frames */
243
244
253
           iCBSearch(iLBCenc_inst, extra_cb_index, extra_gain_index,
245
253
               reverseResidual, mem+CB_MEML-stMemLTbl, stMemLTbl,
246
253
               diff, CB_NSTAGES,
247
253
               &weightdenum[(start-1)*(LPC_FILTERORDER+1)],
248
253
               weightState, 0);
249
250
           /* construct decoded vector */
251
252
253
           iCBConstruct(reverseDecresidual, extra_cb_index,
253
253
               extra_gain_index, mem+CB_MEML-stMemLTbl, stMemLTbl,
254
253
               diff, CB_NSTAGES);
255
256
           /* get decoded residual from reversed vector */
257
258
5.81k
           for (k=0; k<diff; k++) {
259
5.56k
               decresidual[start_pos-1-k] = reverseDecresidual[k];
260
261
262
263
264
265
5.56k
           }
266
253
       }
267
268
       /* counter for predicted sub-frames */
269
270
449
       subcount=0;
271
272
       /* forward prediction of sub-frames */
273
274
449
       Nfor = iLBCenc_inst->nsub-start-1;
275
276
277
449
       if ( Nfor > 0 ) {
278
279
           /* setup memory */
280
281
297
           memset(mem, 0, (CB_MEML-STATE_LEN)*sizeof(float));
282
297
           memcpy(mem+CB_MEML-STATE_LEN, decresidual+(start-1)*SUBL,
283
297
               STATE_LEN*sizeof(float));
284
297
           memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
285
286
           /* loop over sub-frames to encode */
287
288
1.10k
           for (subframe=0; subframe<Nfor; subframe++) {
289
290
               /* encode sub-frame */
291
292
810
               iCBSearch(iLBCenc_inst, cb_index+subcount*CB_NSTAGES,
293
810
                   gain_index+subcount*CB_NSTAGES,
294
810
                   &residual[(start+1+subframe)*SUBL],
295
810
                   mem+CB_MEML-memLfTbl[subcount],
296
810
                   memLfTbl[subcount], SUBL, CB_NSTAGES,
297
810
                   &weightdenum[(start+1+subframe)*
298
810
                               (LPC_FILTERORDER+1)],
299
810
                   weightState, subcount+1);
300
301
               /* construct decoded vector */
302
303
810
               iCBConstruct(&decresidual[(start+1+subframe)*SUBL],
304
810
                   cb_index+subcount*CB_NSTAGES,
305
810
                   gain_index+subcount*CB_NSTAGES,
306
810
                   mem+CB_MEML-memLfTbl[subcount],
307
810
                   memLfTbl[subcount], SUBL, CB_NSTAGES);
308
309
               /* update memory */
310
311
810
               memmove(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
312
810
               memcpy(mem+CB_MEML-SUBL,
313
314
315
316
317
318
810
                   &decresidual[(start+1+subframe)*SUBL],
319
810
                   SUBL*sizeof(float));
320
810
               memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
321
322
810
               subcount++;
323
810
           }
324
297
       }
325
326
327
       /* backward prediction of sub-frames */
328
329
449
       Nback = start-1;
330
331
332
449
       if ( Nback > 0 ) {
333
334
           /* create reverse order vectors */
335
336
1.30k
           for (n=0; n<Nback; n++) {
337
40.4k
               for (k=0; k<SUBL; k++) {
338
39.4k
                   reverseResidual[n*SUBL+k] =
339
39.4k
                       residual[(start-1)*SUBL-1-n*SUBL-k];
340
39.4k
                   reverseDecresidual[n*SUBL+k] =
341
39.4k
                       decresidual[(start-1)*SUBL-1-n*SUBL-k];
342
39.4k
               }
343
986
           }
344
345
           /* setup memory */
346
347
321
           meml_gotten = SUBL*(iLBCenc_inst->nsub+1-start);
348
349
350
321
           if ( meml_gotten > CB_MEML ) {
351
118
               meml_gotten=CB_MEML;
352
118
           }
353
35.9k
           for (k=0; k<meml_gotten; k++) {
354
35.6k
               mem[CB_MEML-1-k] = decresidual[(start-1)*SUBL + k];
355
35.6k
           }
356
321
           memset(mem, 0, (CB_MEML-k)*sizeof(float));
357
321
           memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
358
359
           /* loop over sub-frames to encode */
360
361
1.30k
           for (subframe=0; subframe<Nback; subframe++) {
362
363
               /* encode sub-frame */
364
365
986
               iCBSearch(iLBCenc_inst, cb_index+subcount*CB_NSTAGES,
366
367
368
369
370
371
986
                   gain_index+subcount*CB_NSTAGES,
372
986
                   &reverseResidual[subframe*SUBL],
373
986
                   mem+CB_MEML-memLfTbl[subcount],
374
986
                   memLfTbl[subcount], SUBL, CB_NSTAGES,
375
986
                   &weightdenum[(start-2-subframe)*
376
986
                               (LPC_FILTERORDER+1)],
377
986
                   weightState, subcount+1);
378
379
               /* construct decoded vector */
380
381
986
               iCBConstruct(&reverseDecresidual[subframe*SUBL],
382
986
                   cb_index+subcount*CB_NSTAGES,
383
986
                   gain_index+subcount*CB_NSTAGES,
384
986
                   mem+CB_MEML-memLfTbl[subcount],
385
986
                   memLfTbl[subcount], SUBL, CB_NSTAGES);
386
387
               /* update memory */
388
389
986
               memmove(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
390
986
               memcpy(mem+CB_MEML-SUBL,
391
986
                   &reverseDecresidual[subframe*SUBL],
392
986
                   SUBL*sizeof(float));
393
986
               memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
394
395
986
               subcount++;
396
397
986
           }
398
399
           /* get decoded residual from reversed vector */
400
401
39.7k
           for (i=0; i<SUBL*Nback; i++) {
402
39.4k
               decresidual[SUBL*Nback - i - 1] =
403
39.4k
                   reverseDecresidual[i];
404
39.4k
           }
405
321
       }
406
       /* end encoding part */
407
408
       /* adjust index */
409
449
       index_conv_enc(cb_index);
410
411
       /* pack bytes */
412
413
449
       pbytes=bytes;
414
449
       pos=0;
415
416
       /* loop over the 3 ULP classes */
417
418
1.79k
       for (ulp=0; ulp<3; ulp++) {
419
420
421
422
423
424
425
           /* LSF */
426
9.42k
           for (k=0; k<LSF_NSPLIT*iLBCenc_inst->lpc_n; k++) {
427
8.08k
               packsplit(&lsf_i[k], &firstpart, &lsf_i[k],
428
8.08k
                   iLBCenc_inst->ULP_inst->lsf_bits[k][ulp],
429
8.08k
                   iLBCenc_inst->ULP_inst->lsf_bits[k][ulp]+
430
8.08k
                   iLBCenc_inst->ULP_inst->lsf_bits[k][ulp+1]+
431
8.08k
                   iLBCenc_inst->ULP_inst->lsf_bits[k][ulp+2]);
432
8.08k
               dopack( &pbytes, firstpart,
433
8.08k
                   iLBCenc_inst->ULP_inst->lsf_bits[k][ulp], &pos);
434
8.08k
           }
435
436
           /* Start block info */
437
438
1.34k
           packsplit(&start, &firstpart, &start,
439
1.34k
               iLBCenc_inst->ULP_inst->start_bits[ulp],
440
1.34k
               iLBCenc_inst->ULP_inst->start_bits[ulp]+
441
1.34k
               iLBCenc_inst->ULP_inst->start_bits[ulp+1]+
442
1.34k
               iLBCenc_inst->ULP_inst->start_bits[ulp+2]);
443
1.34k
           dopack( &pbytes, firstpart,
444
1.34k
               iLBCenc_inst->ULP_inst->start_bits[ulp], &pos);
445
446
1.34k
           packsplit(&state_first, &firstpart, &state_first,
447
1.34k
               iLBCenc_inst->ULP_inst->startfirst_bits[ulp],
448
1.34k
               iLBCenc_inst->ULP_inst->startfirst_bits[ulp]+
449
1.34k
               iLBCenc_inst->ULP_inst->startfirst_bits[ulp+1]+
450
1.34k
               iLBCenc_inst->ULP_inst->startfirst_bits[ulp+2]);
451
1.34k
           dopack( &pbytes, firstpart,
452
1.34k
               iLBCenc_inst->ULP_inst->startfirst_bits[ulp], &pos);
453
454
1.34k
           packsplit(&idxForMax, &firstpart, &idxForMax,
455
1.34k
               iLBCenc_inst->ULP_inst->scale_bits[ulp],
456
1.34k
               iLBCenc_inst->ULP_inst->scale_bits[ulp]+
457
1.34k
               iLBCenc_inst->ULP_inst->scale_bits[ulp+1]+
458
1.34k
               iLBCenc_inst->ULP_inst->scale_bits[ulp+2]);
459
1.34k
           dopack( &pbytes, firstpart,
460
1.34k
               iLBCenc_inst->ULP_inst->scale_bits[ulp], &pos);
461
462
79.4k
           for (k=0; k<iLBCenc_inst->state_short_len; k++) {
463
78.1k
               packsplit(idxVec+k, &firstpart, idxVec+k,
464
78.1k
                   iLBCenc_inst->ULP_inst->state_bits[ulp],
465
78.1k
                   iLBCenc_inst->ULP_inst->state_bits[ulp]+
466
78.1k
                   iLBCenc_inst->ULP_inst->state_bits[ulp+1]+
467
78.1k
                   iLBCenc_inst->ULP_inst->state_bits[ulp+2]);
468
78.1k
               dopack( &pbytes, firstpart,
469
78.1k
                   iLBCenc_inst->ULP_inst->state_bits[ulp], &pos);
470
78.1k
           }
471
472
473
474
475
476
477
           /* 23/22 (20ms/30ms) sample block */
478
479
5.38k
           for (k=0;k<CB_NSTAGES;k++) {
480
4.04k
               packsplit(extra_cb_index+k, &firstpart,
481
4.04k
                   extra_cb_index+k,
482
4.04k
                   iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp],
483
4.04k
                   iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp]+
484
4.04k
                   iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp+1]+
485
4.04k
                   iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp+2]);
486
4.04k
               dopack( &pbytes, firstpart,
487
4.04k
                   iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp],
488
4.04k
                   &pos);
489
4.04k
           }
490
491
5.38k
           for (k=0;k<CB_NSTAGES;k++) {
492
4.04k
               packsplit(extra_gain_index+k, &firstpart,
493
4.04k
                   extra_gain_index+k,
494
4.04k
                   iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp],
495
4.04k
                   iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp]+
496
4.04k
                   iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp+1]+
497
4.04k
                   iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp+2]);
498
4.04k
               dopack( &pbytes, firstpart,
499
4.04k
                   iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp],
500
4.04k
                   &pos);
501
4.04k
           }
502
503
           /* The two/four (20ms/30ms) 40 sample sub-blocks */
504
505
6.73k
           for (i=0; i<iLBCenc_inst->nasub; i++) {
506
21.5k
               for (k=0; k<CB_NSTAGES; k++) {
507
16.1k
                   packsplit(cb_index+i*CB_NSTAGES+k, &firstpart,
508
16.1k
                       cb_index+i*CB_NSTAGES+k,
509
16.1k
                       iLBCenc_inst->ULP_inst->cb_index[i][k][ulp],
510
16.1k
                       iLBCenc_inst->ULP_inst->cb_index[i][k][ulp]+
511
16.1k
                       iLBCenc_inst->ULP_inst->cb_index[i][k][ulp+1]+
512
16.1k
                       iLBCenc_inst->ULP_inst->cb_index[i][k][ulp+2]);
513
16.1k
                   dopack( &pbytes, firstpart,
514
16.1k
                       iLBCenc_inst->ULP_inst->cb_index[i][k][ulp],
515
16.1k
                       &pos);
516
16.1k
               }
517
5.38k
           }
518
519
6.73k
           for (i=0; i<iLBCenc_inst->nasub; i++) {
520
21.5k
               for (k=0; k<CB_NSTAGES; k++) {
521
16.1k
                   packsplit(gain_index+i*CB_NSTAGES+k, &firstpart,
522
16.1k
                       gain_index+i*CB_NSTAGES+k,
523
16.1k
                       iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp],
524
16.1k
                       iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp]+
525
526
527
528
529
530
16.1k
                       iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp+1]+
531
16.1k
                       iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp+2]);
532
16.1k
                   dopack( &pbytes, firstpart,
533
16.1k
                       iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp],
534
16.1k
                       &pos);
535
16.1k
               }
536
5.38k
           }
537
1.34k
       }
538
539
       /* set the last bit to zero (otherwise the decoder
540
          will treat it as a lost frame) */
541
449
       dopack( &pbytes, 0, 1, &pos);
542
449
   }
543