2
/******************************************************************
4
iLBC Speech Coder ANSI-C Source Code
8
Copyright (C) The Internet Society (2004).
11
******************************************************************/
17
#include "iLBC_define.h"
18
#include "LPCencode.h"
19
#include "FrameClassify.h"
20
#include "StateSearchW.h"
21
#include "StateConstructW.h"
23
#include "constants.h"
25
#include "iCBSearch.h"
26
#include "iCBConstruct.h"
28
#include "anaFilter.h"
29
#include "syntFilter.h"
31
/*----------------------------------------------------------------*
32
* Initiation of encoder instance.
33
*---------------------------------------------------------------*/
35
short initEncode( /* (o) Number of bytes
37
iLBC_Enc_Inst_t *iLBCenc_inst, /* (i/o) Encoder instance */
38
int mode /* (i) frame size mode */
40
iLBCenc_inst->mode = mode;
42
iLBCenc_inst->blockl = BLOCKL_30MS;
43
iLBCenc_inst->nsub = NSUB_30MS;
44
iLBCenc_inst->nasub = NASUB_30MS;
45
iLBCenc_inst->lpc_n = LPC_N_30MS;
46
iLBCenc_inst->no_of_bytes = NO_OF_BYTES_30MS;
47
iLBCenc_inst->no_of_words = NO_OF_WORDS_30MS;
53
iLBCenc_inst->state_short_len=STATE_SHORT_LEN_30MS;
55
iLBCenc_inst->ULP_inst=&ULP_30msTbl;
58
iLBCenc_inst->blockl = BLOCKL_20MS;
59
iLBCenc_inst->nsub = NSUB_20MS;
60
iLBCenc_inst->nasub = NASUB_20MS;
61
iLBCenc_inst->lpc_n = LPC_N_20MS;
62
iLBCenc_inst->no_of_bytes = NO_OF_BYTES_20MS;
63
iLBCenc_inst->no_of_words = NO_OF_WORDS_20MS;
64
iLBCenc_inst->state_short_len=STATE_SHORT_LEN_20MS;
66
iLBCenc_inst->ULP_inst=&ULP_20msTbl;
72
memset((*iLBCenc_inst).anaMem, 0,
73
LPC_FILTERORDER*sizeof(float));
74
memcpy((*iLBCenc_inst).lsfold, lsfmeanTbl,
75
LPC_FILTERORDER*sizeof(float));
76
memcpy((*iLBCenc_inst).lsfdeqold, lsfmeanTbl,
77
LPC_FILTERORDER*sizeof(float));
78
memset((*iLBCenc_inst).lpc_buffer, 0,
79
(LPC_LOOKBACK+BLOCKL_MAX)*sizeof(float));
80
memset((*iLBCenc_inst).hpimem, 0, 4*sizeof(float));
82
return (short)(iLBCenc_inst->no_of_bytes);
85
/*----------------------------------------------------------------*
86
* main encoder function
87
*---------------------------------------------------------------*/
90
unsigned char *bytes, /* (o) encoded data bits iLBC */
91
float *block, /* (o) speech vector to
93
iLBC_Enc_Inst_t *iLBCenc_inst /* (i/o) the general encoder
97
float data[BLOCKL_MAX];
98
float residual[BLOCKL_MAX], reverseResidual[BLOCKL_MAX];
100
int start, idxForMax, idxVec[STATE_LEN];
106
float reverseDecresidual[BLOCKL_MAX], mem[CB_MEML];
107
int n, k, meml_gotten, Nfor, Nback, i, pos;
108
int gain_index[CB_NSTAGES*NASUB_MAX],
109
extra_gain_index[CB_NSTAGES];
110
int cb_index[CB_NSTAGES*NASUB_MAX],extra_cb_index[CB_NSTAGES];
111
int lsf_i[LSF_NSPLIT*LPC_N_MAX];
112
unsigned char *pbytes;
113
int diff, start_pos, state_first;
115
int index, ulp, firstpart;
116
int subcount, subframe;
117
float weightState[LPC_FILTERORDER];
118
float syntdenum[NSUB_MAX*(LPC_FILTERORDER+1)];
119
float weightdenum[NSUB_MAX*(LPC_FILTERORDER+1)];
120
float decresidual[BLOCKL_MAX];
122
/* high pass filtering of input signal if such is not done
123
prior to calling this function */
125
hpInput(block, iLBCenc_inst->blockl,
126
data, (*iLBCenc_inst).hpimem);
128
/* otherwise simply copy */
130
/*memcpy(data,block,iLBCenc_inst->blockl*sizeof(float));*/
132
/* LPC of hp filtered input data */
134
LPCencode(syntdenum, weightdenum, lsf_i, data, iLBCenc_inst);
137
/* inverse filter to get residual */
139
for (n=0; n<iLBCenc_inst->nsub; n++) {
140
anaFilter(&data[n*SUBL], &syntdenum[n*(LPC_FILTERORDER+1)],
141
SUBL, &residual[n*SUBL], iLBCenc_inst->anaMem);
144
/* find state location */
146
start = FrameClassify(iLBCenc_inst, residual);
148
/* check if state should be in first or last part of the
151
diff = STATE_LEN - iLBCenc_inst->state_short_len;
153
index = (start-1)*SUBL;
159
for (i = 0; i < iLBCenc_inst->state_short_len; i++) {
160
en1 += residual[index+i]*residual[index+i];
163
index = (start-1)*SUBL+diff;
164
for (i = 0; i < iLBCenc_inst->state_short_len; i++) {
165
en2 += residual[index+i]*residual[index+i];
171
start_pos = (start-1)*SUBL;
174
start_pos = (start-1)*SUBL + diff;
177
/* scalar quantization of state */
179
StateSearchW(iLBCenc_inst, &residual[start_pos],
180
&syntdenum[(start-1)*(LPC_FILTERORDER+1)],
181
&weightdenum[(start-1)*(LPC_FILTERORDER+1)], &idxForMax,
182
idxVec, iLBCenc_inst->state_short_len, state_first);
184
StateConstructW(idxForMax, idxVec,
185
&syntdenum[(start-1)*(LPC_FILTERORDER+1)],
186
&decresidual[start_pos], iLBCenc_inst->state_short_len);
188
/* predictive quantization in state */
190
if (state_first) { /* put adaptive part in the end */
195
(CB_MEML-iLBCenc_inst->state_short_len)*sizeof(float));
196
memcpy(mem+CB_MEML-iLBCenc_inst->state_short_len,
197
decresidual+start_pos,
198
iLBCenc_inst->state_short_len*sizeof(float));
199
memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
201
/* encode sub-frames */
203
iCBSearch(iLBCenc_inst, extra_cb_index, extra_gain_index,
204
&residual[start_pos+iLBCenc_inst->state_short_len],
205
mem+CB_MEML-stMemLTbl,
206
stMemLTbl, diff, CB_NSTAGES,
212
&weightdenum[start*(LPC_FILTERORDER+1)],
215
/* construct decoded vector */
218
&decresidual[start_pos+iLBCenc_inst->state_short_len],
219
extra_cb_index, extra_gain_index,
220
mem+CB_MEML-stMemLTbl,
221
stMemLTbl, diff, CB_NSTAGES);
224
else { /* put adaptive part in the beginning */
226
/* create reversed vectors for prediction */
228
for (k=0; k<diff; k++) {
229
reverseResidual[k] = residual[(start+1)*SUBL-1
230
-(k+iLBCenc_inst->state_short_len)];
235
meml_gotten = iLBCenc_inst->state_short_len;
236
for (k=0; k<meml_gotten; k++) {
237
mem[CB_MEML-1-k] = decresidual[start_pos + k];
239
memset(mem, 0, (CB_MEML-k)*sizeof(float));
240
memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
242
/* encode sub-frames */
244
iCBSearch(iLBCenc_inst, extra_cb_index, extra_gain_index,
245
reverseResidual, mem+CB_MEML-stMemLTbl, stMemLTbl,
247
&weightdenum[(start-1)*(LPC_FILTERORDER+1)],
250
/* construct decoded vector */
252
iCBConstruct(reverseDecresidual, extra_cb_index,
253
extra_gain_index, mem+CB_MEML-stMemLTbl, stMemLTbl,
256
/* get decoded residual from reversed vector */
258
for (k=0; k<diff; k++) {
259
decresidual[start_pos-1-k] = reverseDecresidual[k];
268
/* counter for predicted sub-frames */
272
/* forward prediction of sub-frames */
274
Nfor = iLBCenc_inst->nsub-start-1;
281
memset(mem, 0, (CB_MEML-STATE_LEN)*sizeof(float));
282
memcpy(mem+CB_MEML-STATE_LEN, decresidual+(start-1)*SUBL,
283
STATE_LEN*sizeof(float));
284
memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
286
/* loop over sub-frames to encode */
288
for (subframe=0; subframe<Nfor; subframe++) {
290
/* encode sub-frame */
292
iCBSearch(iLBCenc_inst, cb_index+subcount*CB_NSTAGES,
293
gain_index+subcount*CB_NSTAGES,
294
&residual[(start+1+subframe)*SUBL],
295
mem+CB_MEML-memLfTbl[subcount],
296
memLfTbl[subcount], SUBL, CB_NSTAGES,
297
&weightdenum[(start+1+subframe)*
298
(LPC_FILTERORDER+1)],
299
weightState, subcount+1);
301
/* construct decoded vector */
303
iCBConstruct(&decresidual[(start+1+subframe)*SUBL],
304
cb_index+subcount*CB_NSTAGES,
305
gain_index+subcount*CB_NSTAGES,
306
mem+CB_MEML-memLfTbl[subcount],
307
memLfTbl[subcount], SUBL, CB_NSTAGES);
311
memcpy(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
312
memcpy(mem+CB_MEML-SUBL,
318
&decresidual[(start+1+subframe)*SUBL],
320
memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
327
/* backward prediction of sub-frames */
334
/* create reverse order vectors */
336
for (n=0; n<Nback; n++) {
337
for (k=0; k<SUBL; k++) {
338
reverseResidual[n*SUBL+k] =
339
residual[(start-1)*SUBL-1-n*SUBL-k];
340
reverseDecresidual[n*SUBL+k] =
341
decresidual[(start-1)*SUBL-1-n*SUBL-k];
347
meml_gotten = SUBL*(iLBCenc_inst->nsub+1-start);
350
if ( meml_gotten > CB_MEML ) {
353
for (k=0; k<meml_gotten; k++) {
354
mem[CB_MEML-1-k] = decresidual[(start-1)*SUBL + k];
356
memset(mem, 0, (CB_MEML-k)*sizeof(float));
357
memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
359
/* loop over sub-frames to encode */
361
for (subframe=0; subframe<Nback; subframe++) {
363
/* encode sub-frame */
365
iCBSearch(iLBCenc_inst, cb_index+subcount*CB_NSTAGES,
371
gain_index+subcount*CB_NSTAGES,
372
&reverseResidual[subframe*SUBL],
373
mem+CB_MEML-memLfTbl[subcount],
374
memLfTbl[subcount], SUBL, CB_NSTAGES,
375
&weightdenum[(start-2-subframe)*
376
(LPC_FILTERORDER+1)],
377
weightState, subcount+1);
379
/* construct decoded vector */
381
iCBConstruct(&reverseDecresidual[subframe*SUBL],
382
cb_index+subcount*CB_NSTAGES,
383
gain_index+subcount*CB_NSTAGES,
384
mem+CB_MEML-memLfTbl[subcount],
385
memLfTbl[subcount], SUBL, CB_NSTAGES);
389
memcpy(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
390
memcpy(mem+CB_MEML-SUBL,
391
&reverseDecresidual[subframe*SUBL],
393
memset(weightState, 0, LPC_FILTERORDER*sizeof(float));
399
/* get decoded residual from reversed vector */
401
for (i=0; i<SUBL*Nback; i++) {
402
decresidual[SUBL*Nback - i - 1] =
403
reverseDecresidual[i];
406
/* end encoding part */
409
index_conv_enc(cb_index);
416
/* loop over the 3 ULP classes */
418
for (ulp=0; ulp<3; ulp++) {
426
for (k=0; k<LSF_NSPLIT*iLBCenc_inst->lpc_n; k++) {
427
packsplit(&lsf_i[k], &firstpart, &lsf_i[k],
428
iLBCenc_inst->ULP_inst->lsf_bits[k][ulp],
429
iLBCenc_inst->ULP_inst->lsf_bits[k][ulp]+
430
iLBCenc_inst->ULP_inst->lsf_bits[k][ulp+1]+
431
iLBCenc_inst->ULP_inst->lsf_bits[k][ulp+2]);
432
dopack( &pbytes, firstpart,
433
iLBCenc_inst->ULP_inst->lsf_bits[k][ulp], &pos);
436
/* Start block info */
438
packsplit(&start, &firstpart, &start,
439
iLBCenc_inst->ULP_inst->start_bits[ulp],
440
iLBCenc_inst->ULP_inst->start_bits[ulp]+
441
iLBCenc_inst->ULP_inst->start_bits[ulp+1]+
442
iLBCenc_inst->ULP_inst->start_bits[ulp+2]);
443
dopack( &pbytes, firstpart,
444
iLBCenc_inst->ULP_inst->start_bits[ulp], &pos);
446
packsplit(&state_first, &firstpart, &state_first,
447
iLBCenc_inst->ULP_inst->startfirst_bits[ulp],
448
iLBCenc_inst->ULP_inst->startfirst_bits[ulp]+
449
iLBCenc_inst->ULP_inst->startfirst_bits[ulp+1]+
450
iLBCenc_inst->ULP_inst->startfirst_bits[ulp+2]);
451
dopack( &pbytes, firstpart,
452
iLBCenc_inst->ULP_inst->startfirst_bits[ulp], &pos);
454
packsplit(&idxForMax, &firstpart, &idxForMax,
455
iLBCenc_inst->ULP_inst->scale_bits[ulp],
456
iLBCenc_inst->ULP_inst->scale_bits[ulp]+
457
iLBCenc_inst->ULP_inst->scale_bits[ulp+1]+
458
iLBCenc_inst->ULP_inst->scale_bits[ulp+2]);
459
dopack( &pbytes, firstpart,
460
iLBCenc_inst->ULP_inst->scale_bits[ulp], &pos);
462
for (k=0; k<iLBCenc_inst->state_short_len; k++) {
463
packsplit(idxVec+k, &firstpart, idxVec+k,
464
iLBCenc_inst->ULP_inst->state_bits[ulp],
465
iLBCenc_inst->ULP_inst->state_bits[ulp]+
466
iLBCenc_inst->ULP_inst->state_bits[ulp+1]+
467
iLBCenc_inst->ULP_inst->state_bits[ulp+2]);
468
dopack( &pbytes, firstpart,
469
iLBCenc_inst->ULP_inst->state_bits[ulp], &pos);
477
/* 23/22 (20ms/30ms) sample block */
479
for (k=0;k<CB_NSTAGES;k++) {
480
packsplit(extra_cb_index+k, &firstpart,
482
iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp],
483
iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp]+
484
iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp+1]+
485
iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp+2]);
486
dopack( &pbytes, firstpart,
487
iLBCenc_inst->ULP_inst->extra_cb_index[k][ulp],
491
for (k=0;k<CB_NSTAGES;k++) {
492
packsplit(extra_gain_index+k, &firstpart,
494
iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp],
495
iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp]+
496
iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp+1]+
497
iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp+2]);
498
dopack( &pbytes, firstpart,
499
iLBCenc_inst->ULP_inst->extra_cb_gain[k][ulp],
503
/* The two/four (20ms/30ms) 40 sample sub-blocks */
505
for (i=0; i<iLBCenc_inst->nasub; i++) {
506
for (k=0; k<CB_NSTAGES; k++) {
507
packsplit(cb_index+i*CB_NSTAGES+k, &firstpart,
508
cb_index+i*CB_NSTAGES+k,
509
iLBCenc_inst->ULP_inst->cb_index[i][k][ulp],
510
iLBCenc_inst->ULP_inst->cb_index[i][k][ulp]+
511
iLBCenc_inst->ULP_inst->cb_index[i][k][ulp+1]+
512
iLBCenc_inst->ULP_inst->cb_index[i][k][ulp+2]);
513
dopack( &pbytes, firstpart,
514
iLBCenc_inst->ULP_inst->cb_index[i][k][ulp],
519
for (i=0; i<iLBCenc_inst->nasub; i++) {
520
for (k=0; k<CB_NSTAGES; k++) {
521
packsplit(gain_index+i*CB_NSTAGES+k, &firstpart,
522
gain_index+i*CB_NSTAGES+k,
523
iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp],
524
iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp]+
530
iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp+1]+
531
iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp+2]);
532
dopack( &pbytes, firstpart,
533
iLBCenc_inst->ULP_inst->cb_gain[i][k][ulp],
539
/* set the last bit to zero (otherwise the decoder
540
will treat it as a lost frame) */
541
dopack( &pbytes, 0, 1, &pos);