2
/******************************************************************
4
iLBC Speech Coder ANSI-C Source Code
8
Copyright (C) The Internet Society (2004).
11
******************************************************************/
15
#include "iLBC_define.h"
16
#include "constants.h"
19
/*----------------------------------------------------------------*
20
* Find index in array such that the array element with said
21
* index is the element of said array closest to "value"
22
* according to the squared-error criterion
23
*---------------------------------------------------------------*/
31
int *index, /* (o) index of array element closest
33
float *array, /* (i) data array */
34
float value,/* (i) value */
35
int arlength/* (i) dimension of data array */
43
for (i=1; i<arlength; i++) {
54
/*----------------------------------------------------------------*
55
* compute cross correlation between sequences
56
*---------------------------------------------------------------*/
59
float* corr, /* (o) correlation of seq1 and seq2 */
60
float* seq1, /* (i) first sequence */
61
int dim1, /* (i) dimension first seq1 */
62
const float *seq2, /* (i) second sequence */
63
int dim2 /* (i) dimension seq2 */
67
for (i=0; i<=dim1-dim2; i++) {
69
for (j=0; j<dim2; j++) {
70
corr[i] += seq1[i+j] * seq2[j];
75
/*----------------------------------------------------------------*
76
* upsample finite array assuming zeros outside bounds
77
*---------------------------------------------------------------*/
85
float* useq1, /* (o) upsampled output sequence */
86
float* seq1,/* (i) unupsampled sequence */
87
int dim1, /* (i) dimension seq1 */
88
int hfl /* (i) polyphase filter length=2*hfl+1 */
91
int i,j,k,q,filterlength,hfl2;
92
const float *polyp[ENH_UPS0]; /* pointers to
96
/* define pointers for filter */
100
if ( filterlength > dim1 ) {
102
for (j=0; j<ENH_UPS0; j++) {
103
polyp[j]=polyphaserTbl+j*filterlength+hfl-hfl2;
106
filterlength=2*hfl+1;
109
for (j=0; j<ENH_UPS0; j++) {
110
polyp[j]=polyphaserTbl+j*filterlength;
114
/* filtering: filter overhangs left side of sequence */
117
for (i=hfl; i<filterlength; i++) {
118
for (j=0; j<ENH_UPS0; j++) {
122
for (k=0; k<=i; k++) {
123
*pu += *ps-- * *pp++;
129
/* filtering: simple convolution=inner products */
131
for (i=filterlength; i<dim1; i++) {
137
for (j=0;j<ENH_UPS0; j++){
141
for (k=0; k<filterlength; k++) {
142
*pu += *ps-- * *pp++;
148
/* filtering: filter overhangs right side of sequence */
150
for (q=1; q<=hfl; q++) {
151
for (j=0; j<ENH_UPS0; j++) {
155
for (k=0; k<filterlength-q; k++) {
156
*pu += *ps-- * *pp++;
164
/*----------------------------------------------------------------*
165
* find segment starting near idata+estSegPos that has highest
166
* correlation with idata+centerStartPos through
167
* idata+centerStartPos+ENH_BLOCKL-1 segment is found at a
168
* resolution of ENH_UPSO times the original of the original
170
*---------------------------------------------------------------*/
173
float *seg, /* (o) segment array */
174
float *updStartPos, /* (o) updated start point */
175
float* idata, /* (i) original data buffer */
176
int idatal, /* (i) dimension of idata */
177
int centerStartPos, /* (i) beginning center segment */
178
float estSegPos,/* (i) estimated beginning other segment */
179
float period /* (i) estimated pitch period */
181
int estSegPosRounded,searchSegStartPos,searchSegEndPos,corrdim;
182
int tloc,tloc2,i,st,en,fraction;
183
float vect[ENH_VECTL],corrVec[ENH_CORRDIM],maxv;
184
float corrVecUps[ENH_CORRDIM*ENH_UPS0];
190
/* defining array bounds */
192
estSegPosRounded=(int)(estSegPos - 0.5);
194
searchSegStartPos=estSegPosRounded-ENH_SLOP;
196
if (searchSegStartPos<0) {
199
searchSegEndPos=estSegPosRounded+ENH_SLOP;
201
if (searchSegEndPos+ENH_BLOCKL >= idatal) {
202
searchSegEndPos=idatal-ENH_BLOCKL-1;
204
corrdim=searchSegEndPos-searchSegStartPos+1;
206
/* compute upsampled correlation (corr33) and find
209
mycorr1(corrVec,idata+searchSegStartPos,
210
corrdim+ENH_BLOCKL-1,idata+centerStartPos,ENH_BLOCKL);
211
enh_upsample(corrVecUps,corrVec,corrdim,ENH_FL0);
212
tloc=0; maxv=corrVecUps[0];
213
for (i=1; i<ENH_UPS0*corrdim; i++) {
215
if (corrVecUps[i]>maxv) {
221
/* make vector can be upsampled without ever running outside
224
*updStartPos= (float)searchSegStartPos +
225
(float)tloc/(float)ENH_UPS0+(float)1.0;
226
tloc2=(int)(tloc/ENH_UPS0);
228
if (tloc>tloc2*ENH_UPS0) {
231
st=searchSegStartPos+tloc2-ENH_FL0;
234
memset(vect,0,-st*sizeof(float));
235
memcpy(&vect[-st],idata, (ENH_VECTL+st)*sizeof(float));
246
memcpy(vect, &idata[st],
247
(ENH_VECTL-(en-idatal))*sizeof(float));
248
memset(&vect[ENH_VECTL-(en-idatal)], 0,
249
(en-idatal)*sizeof(float));
252
memcpy(vect, &idata[st], ENH_VECTL*sizeof(float));
255
fraction=tloc2*ENH_UPS0-tloc;
257
/* compute the segment (this is actually a convolution) */
259
mycorr1(seg,vect,ENH_VECTL,polyphaserTbl+(2*ENH_FL0+1)*fraction,
263
/*----------------------------------------------------------------*
264
* find the smoothed output data
265
*---------------------------------------------------------------*/
268
float *odata, /* (o) smoothed output */
269
float *sseq,/* (i) said second sequence of waveforms */
270
int hl, /* (i) 2*hl+1 is sseq dimension */
271
float alpha0/* (i) max smoothing energy fraction */
274
float w00,w10,w11,A,B,C,*psseq,err,errs;
275
float surround[BLOCKL_MAX]; /* shape contributed by other than
277
float wt[2*ENH_HL+1]; /* waveform weighting to get
281
/* create shape of contribution from all waveforms except the
284
for (i=1; i<=2*hl+1; i++) {
285
wt[i-1] = (float)0.5*(1 - (float)cos(2*PI*i/(2*hl+2)));
287
wt[hl]=0.0; /* for clarity, not used */
288
for (i=0; i<ENH_BLOCKL; i++) {
289
surround[i]=sseq[i]*wt[0];
296
for (k=1; k<hl; k++) {
297
psseq=sseq+k*ENH_BLOCKL;
298
for(i=0;i<ENH_BLOCKL; i++) {
299
surround[i]+=psseq[i]*wt[k];
302
for (k=hl+1; k<=2*hl; k++) {
303
psseq=sseq+k*ENH_BLOCKL;
304
for(i=0;i<ENH_BLOCKL; i++) {
305
surround[i]+=psseq[i]*wt[k];
309
/* compute some inner products */
311
w00 = w10 = w11 = 0.0;
312
psseq=sseq+hl*ENH_BLOCKL; /* current block */
313
for (i=0; i<ENH_BLOCKL;i++) {
314
w00+=psseq[i]*psseq[i];
315
w11+=surround[i]*surround[i];
316
w10+=surround[i]*psseq[i];
319
if (fabs(w11) < 1.0) {
322
C = (float)sqrt( w00/w11);
324
/* first try enhancement without power-constraint */
327
psseq=sseq+hl*ENH_BLOCKL;
328
for (i=0; i<ENH_BLOCKL; i++) {
329
odata[i]=C*surround[i];
330
err=psseq[i]-odata[i];
334
/* if constraint violated by first try, add constraint */
336
if (errs > alpha0 * w00) {
340
denom = (w11*w00-w10*w10)/(w00*w00);
342
if (denom > 0.0001) { /* eliminates numerical problems
349
A = (float)sqrt( (alpha0- alpha0*alpha0/4)/denom);
350
B = -alpha0/2 - A * w10/w00;
353
else { /* essentially no difference between cycles;
354
smoothing not needed */
359
/* create smoothed sequence */
361
psseq=sseq+hl*ENH_BLOCKL;
362
for (i=0; i<ENH_BLOCKL; i++) {
363
odata[i]=A*surround[i]+B*psseq[i];
368
/*----------------------------------------------------------------*
369
* get the pitch-synchronous sample sequence
370
*---------------------------------------------------------------*/
373
float *sseq, /* (o) the pitch-synchronous sequence */
374
float *idata, /* (i) original data */
375
int idatal, /* (i) dimension of data */
376
int centerStartPos, /* (i) where current block starts */
377
float *period, /* (i) rough-pitch-period array */
378
float *plocs, /* (i) where periods of period array
380
int periodl, /* (i) dimension period array */
381
int hl /* (i) 2*hl+1 is the number of sequences */
383
int i,centerEndPos,q;
384
float blockStartPos[2*ENH_HL+1];
385
int lagBlock[2*ENH_HL+1];
386
float plocs2[ENH_PLOCSL];
389
centerEndPos=centerStartPos+ENH_BLOCKL-1;
393
NearestNeighbor(lagBlock+hl,plocs,
394
(float)0.5*(centerStartPos+centerEndPos),periodl);
396
blockStartPos[hl]=(float)centerStartPos;
402
psseq=sseq+ENH_BLOCKL*hl;
403
memcpy(psseq, idata+centerStartPos, ENH_BLOCKL*sizeof(float));
407
for (q=hl-1; q>=0; q--) {
408
blockStartPos[q]=blockStartPos[q+1]-period[lagBlock[q+1]];
409
NearestNeighbor(lagBlock+q,plocs,
411
ENH_BLOCKL_HALF-period[lagBlock[q+1]], periodl);
414
if (blockStartPos[q]-ENH_OVERHANG>=0) {
415
refiner(sseq+q*ENH_BLOCKL, blockStartPos+q, idata,
416
idatal, centerStartPos, blockStartPos[q],
417
period[lagBlock[q+1]]);
419
psseq=sseq+q*ENH_BLOCKL;
420
memset(psseq, 0, ENH_BLOCKL*sizeof(float));
426
for (i=0; i<periodl; i++) {
427
plocs2[i]=plocs[i]-period[i];
429
for (q=hl+1; q<=2*hl; q++) {
430
NearestNeighbor(lagBlock+q,plocs2,
431
blockStartPos[q-1]+ENH_BLOCKL_HALF,periodl);
433
blockStartPos[q]=blockStartPos[q-1]+period[lagBlock[q]];
434
if (blockStartPos[q]+ENH_BLOCKL+ENH_OVERHANG<idatal) {
435
refiner(sseq+ENH_BLOCKL*q, blockStartPos+q, idata,
436
idatal, centerStartPos, blockStartPos[q],
437
period[lagBlock[q]]);
440
psseq=sseq+q*ENH_BLOCKL;
441
memset(psseq, 0, ENH_BLOCKL*sizeof(float));
446
/*----------------------------------------------------------------*
447
* perform enhancement on idata+centerStartPos through
448
* idata+centerStartPos+ENH_BLOCKL-1
449
*---------------------------------------------------------------*/
456
float *odata, /* (o) smoothed block, dimension blockl */
457
float *idata, /* (i) data buffer used for enhancing */
458
int idatal, /* (i) dimension idata */
459
int centerStartPos, /* (i) first sample current block
461
float alpha0, /* (i) max correction-energy-fraction
463
float *period, /* (i) pitch period array */
464
float *plocs, /* (i) locations where period array
466
int periodl /* (i) dimension of period and plocs */
468
float sseq[(2*ENH_HL+1)*ENH_BLOCKL];
470
/* get said second sequence of segments */
472
getsseq(sseq,idata,idatal,centerStartPos,period,
473
plocs,periodl,ENH_HL);
475
/* compute the smoothed output from said second sequence */
477
smath(odata,sseq,ENH_HL,alpha0);
481
/*----------------------------------------------------------------*
483
*---------------------------------------------------------------*/
486
float *target, /* (i) first array */
487
float *regressor, /* (i) second array */
488
int subl /* (i) dimension arrays */
495
for (i=0; i<subl; i++) {
496
ftmp1 += target[i]*regressor[i];
497
ftmp2 += regressor[i]*regressor[i];
501
return (float)(ftmp1*ftmp1/ftmp2);
513
/*----------------------------------------------------------------*
514
* interface for enhancer
515
*---------------------------------------------------------------*/
517
int enhancerInterface(
518
float *out, /* (o) enhanced signal */
519
float *in, /* (i) unenhanced signal */
520
iLBC_Dec_Inst_t *iLBCdec_inst /* (i) buffers etc */
522
float *enh_buf, *enh_period;
524
int lag=0, ilag, i, ioffset;
527
float *inPtr, *enh_bufPtr1, *enh_bufPtr2;
528
float plc_pred[ENH_BLOCKL];
530
float lpState[6], downsampled[(ENH_NBLOCKS*ENH_BLOCKL+120)/2];
531
int inLen=ENH_NBLOCKS*ENH_BLOCKL+120;
532
int start, plc_blockl, inlag;
534
enh_buf=iLBCdec_inst->enh_buf;
535
enh_period=iLBCdec_inst->enh_period;
537
memmove(enh_buf, &enh_buf[iLBCdec_inst->blockl],
538
(ENH_BUFL-iLBCdec_inst->blockl)*sizeof(float));
540
memcpy(&enh_buf[ENH_BUFL-iLBCdec_inst->blockl], in,
541
iLBCdec_inst->blockl*sizeof(float));
543
if (iLBCdec_inst->mode==30)
544
plc_blockl=ENH_BLOCKL;
548
/* when 20 ms frame, move processing one block */
550
if (iLBCdec_inst->mode==20) ioffset=1;
553
memmove(enh_period, &enh_period[i],
554
(ENH_NBLOCKS_TOT-i)*sizeof(float));
561
/* Set state information to the 6 samples right before
562
the samples to be downsampled. */
565
enh_buf+(ENH_NBLOCKS_EXTRA+ioffset)*ENH_BLOCKL-126,
568
/* Down sample a factor 2 to save computations */
570
DownSample(enh_buf+(ENH_NBLOCKS_EXTRA+ioffset)*ENH_BLOCKL-120,
571
lpFilt_coefsTbl, inLen-ioffset*ENH_BLOCKL,
572
lpState, downsampled);
574
/* Estimate the pitch in the down sampled domain. */
575
for (iblock = 0; iblock<ENH_NBLOCKS-ioffset; iblock++) {
578
maxcc = xCorrCoef(downsampled+60+iblock*
579
ENH_BLOCKL_HALF, downsampled+60+iblock*
580
ENH_BLOCKL_HALF-lag, ENH_BLOCKL_HALF);
581
for (ilag=11; ilag<60; ilag++) {
582
cc = xCorrCoef(downsampled+60+iblock*
583
ENH_BLOCKL_HALF, downsampled+60+iblock*
584
ENH_BLOCKL_HALF-ilag, ENH_BLOCKL_HALF);
592
/* Store the estimated lag in the non-downsampled domain */
593
enh_period[iblock+ENH_NBLOCKS_EXTRA+ioffset] = (float)lag*2;
599
/* PLC was performed on the previous packet */
600
if (iLBCdec_inst->prev_enh_pl==1) {
602
inlag=(int)enh_period[ENH_NBLOCKS_EXTRA+ioffset];
605
maxcc = xCorrCoef(in, in+lag, plc_blockl);
606
for (ilag=inlag; ilag<=inlag+1; ilag++) {
607
cc = xCorrCoef(in, in+ilag, plc_blockl);
620
enh_period[ENH_NBLOCKS_EXTRA+ioffset-1]=(float)lag;
622
/* compute new concealed residual for the old lookahead,
623
mix the forward PLC with a backward PLC from
628
enh_bufPtr1=&plc_pred[plc_blockl-1];
630
if (lag>plc_blockl) {
636
for (isample = start; isample>0; isample--) {
637
*enh_bufPtr1-- = *inPtr--;
640
enh_bufPtr2=&enh_buf[ENH_BUFL-1-iLBCdec_inst->blockl];
641
for (isample = (plc_blockl-1-lag); isample>=0; isample--) {
642
*enh_bufPtr1-- = *enh_bufPtr2--;
645
/* limit energy change */
648
for (i=0;i<plc_blockl;i++) {
649
ftmp2+=enh_buf[ENH_BUFL-1-iLBCdec_inst->blockl-i]*
650
enh_buf[ENH_BUFL-1-iLBCdec_inst->blockl-i];
651
ftmp1+=plc_pred[i]*plc_pred[i];
653
ftmp1=(float)sqrt(ftmp1/(float)plc_blockl);
654
ftmp2=(float)sqrt(ftmp2/(float)plc_blockl);
655
if (ftmp1>(float)2.0*ftmp2 && ftmp1>0.0) {
656
for (i=0;i<plc_blockl-10;i++) {
657
plc_pred[i]*=(float)2.0*ftmp2/ftmp1;
659
for (i=plc_blockl-10;i<plc_blockl;i++) {
660
plc_pred[i]*=(float)(i-plc_blockl+10)*
661
((float)1.0-(float)2.0*ftmp2/ftmp1)/(float)(10)+
667
(float)2.0*ftmp2/ftmp1;
671
enh_bufPtr1=&enh_buf[ENH_BUFL-1-iLBCdec_inst->blockl];
672
for (i=0; i<plc_blockl; i++) {
673
ftmp1 = (float) (i+1) / (float) (plc_blockl+1);
674
*enh_bufPtr1 *= ftmp1;
675
*enh_bufPtr1 += ((float)1.0-ftmp1)*
676
plc_pred[plc_blockl-1-i];
681
if (iLBCdec_inst->mode==20) {
682
/* Enhancer with 40 samples delay */
683
for (iblock = 0; iblock<2; iblock++) {
684
enhancer(out+iblock*ENH_BLOCKL, enh_buf,
685
ENH_BUFL, (5+iblock)*ENH_BLOCKL+40,
686
ENH_ALPHA0, enh_period, enh_plocsTbl,
689
} else if (iLBCdec_inst->mode==30) {
690
/* Enhancer with 80 samples delay */
691
for (iblock = 0; iblock<3; iblock++) {
692
enhancer(out+iblock*ENH_BLOCKL, enh_buf,
693
ENH_BUFL, (4+iblock)*ENH_BLOCKL,
694
ENH_ALPHA0, enh_period, enh_plocsTbl,