16 #include "iLBC_define.h"
17 #include "gainquant.h"
20 #include "constants.h"
45 int i, j, icount, stage, best_index, range, counter;
46 float max_measure, gain, measure, crossDot, ftmp;
47 float gains[CB_NSTAGES];
49 int base_index, sInd, eInd, base_size;
50 int sIndAug=0, eIndAug=0;
51 float buf[CB_MEML+SUBL+2*LPC_FILTERORDER];
52 float invenergy[CB_EXPAND*128], energy[CB_EXPAND*128];
53 float *pp, *ppi=0, *ppo=0, *ppe=0;
54 float cbvectors[CB_MEML];
55 float tene, cene, cvec[SUBL];
58 memset(cvec,0,SUBL*
sizeof(
float));
62 base_size=lMem-lTarget+1;
65 base_size=lMem-lTarget+1+lTarget/2;
70 memcpy(buf,weightState,
sizeof(
float)*LPC_FILTERORDER);
71 memcpy(buf+LPC_FILTERORDER,mem,lMem*
sizeof(
float));
72 memcpy(buf+LPC_FILTERORDER+lMem,intarget,lTarget*
sizeof(
float));
76 AllPoleFilter(buf+LPC_FILTERORDER, weightDenum,
77 lMem+lTarget, LPC_FILTERORDER);
81 memcpy(target, buf+LPC_FILTERORDER+lMem, lTarget*
sizeof(
float));
89 for (i=0; i<lTarget; i++) {
90 tene+=target[i]*target[i];
96 filteredCBvecs(cbvectors, buf+LPC_FILTERORDER, lMem);
100 for (stage=0; stage<nStages; stage++) {
102 range = search_rangeTbl[block][stage];
106 max_measure = (float)-10000000.0;
114 pp=buf+LPC_FILTERORDER+lMem-lTarget;
115 for (j=0; j<lTarget; j++) {
116 crossDot += target[j]*(*pp++);
124 ppi = buf+LPC_FILTERORDER+lMem-lTarget-1;
125 ppo = buf+LPC_FILTERORDER+lMem-1;
128 pp=buf+LPC_FILTERORDER+lMem-lTarget;
129 for (j=0; j<lTarget; j++, pp++) {
134 invenergy[0] = (float) 1.0 / (*ppe + EPS);
136 invenergy[0] = (float) 0.0;
145 measure=(float)-10000000.0;
147 if (crossDot > 0.0) {
148 measure = crossDot*crossDot*invenergy[0];
152 measure = crossDot*crossDot*invenergy[0];
156 ftmp = crossDot*invenergy[0];
158 if ((measure>max_measure) && (fabs(ftmp)<CB_MAXGAIN)) {
160 max_measure = measure;
167 for (icount=1; icount<range; icount++) {
172 pp = buf+LPC_FILTERORDER+lMem-lTarget-icount;
174 for (j=0; j<lTarget; j++) {
175 crossDot += target[j]*(*pp++);
179 *ppe++ = energy[icount-1] + (*ppi)*(*ppi) -
184 if (energy[icount]>0.0) {
186 (float)1.0/(energy[icount]+EPS);
188 invenergy[icount] = (float) 0.0;
195 measure=(float)-10000000.0;
197 if (crossDot > 0.0) {
198 measure = crossDot*crossDot*invenergy[icount];
202 measure = crossDot*crossDot*invenergy[icount];
206 ftmp = crossDot*invenergy[icount];
208 if ((measure>max_measure) && (fabs(ftmp)<CB_MAXGAIN)) {
210 max_measure = measure;
224 searchAugmentedCB(20, 39, stage, base_size-lTarget/2,
225 target, buf+LPC_FILTERORDER+lMem,
226 &max_measure, &best_index, &gain, energy,
232 base_index=best_index;
236 if (CB_RESRANGE == -1) {
255 sInd=base_index-CB_RESRANGE/2;
256 eInd=sInd+CB_RESRANGE;
266 }
else if ( base_index < (base_size-20) ) {
269 sInd -= (eInd-range);
274 if (sInd < (base_size-20)) {
278 eIndAug = 19 + CB_RESRANGE;
285 sIndAug = 20 + sInd - (base_size-20);
288 eInd = CB_RESRANGE - (eIndAug-sIndAug+1);
305 sInd -= (eInd - range);
320 ppe = energy+base_size;
323 pp=cbvectors+lMem-lTarget;
324 for (j=0; j<lTarget; j++, pp++) {
328 ppi = cbvectors + lMem - 1 - lTarget;
329 ppo = cbvectors + lMem - 1;
331 for (j=0; j<(range-1); j++) {
332 *(ppe+1) = *ppe + (*ppi)*(*ppi) - (*ppo)*(*ppo);
341 for (icount=sInd; icount<eInd; icount++) {
346 pp=cbvectors + lMem - (counter++) - lTarget;
348 for (j=0;j<lTarget;j++) {
354 crossDot += target[j]*(*pp++);
357 if (energy[icount]>0.0) {
358 invenergy[icount] =(float)1.0/(energy[icount]+EPS);
360 invenergy[icount] =(float)0.0;
365 measure=(float)-10000000.0;
367 if (crossDot > 0.0) {
368 measure = crossDot*crossDot*
373 measure = crossDot*crossDot*invenergy[icount];
377 ftmp = crossDot*invenergy[icount];
379 if ((measure>max_measure) && (fabs(ftmp)<CB_MAXGAIN)) {
381 max_measure = measure;
388 if ((lTarget==SUBL)&&(sIndAug!=0)) {
389 searchAugmentedCB(sIndAug, eIndAug, stage,
390 2*base_size-20, target, cbvectors+lMem,
391 &max_measure, &best_index, &gain, energy,
397 index[stage] = best_index;
412 if (gain>CB_MAXGAIN) {
413 gain = (float)CB_MAXGAIN;
415 gain = gainquant(gain, 1.0, 32, &gain_index[stage]);
419 gain = gainquant(gain, (
float)fabs(gains[stage-1]),
420 16, &gain_index[stage]);
422 gain = gainquant(gain, (
float)fabs(gains[stage-1]),
423 8, &gain_index[stage]);
430 if (lTarget==(STATE_LEN-iLBCenc_inst->state_short_len)) {
432 if (index[stage]<base_size) {
433 pp=buf+LPC_FILTERORDER+lMem-lTarget-index[stage];
435 pp=cbvectors+lMem-lTarget-
436 index[stage]+base_size;
440 if (index[stage]<base_size) {
441 if (index[stage]<(base_size-20)) {
442 pp=buf+LPC_FILTERORDER+lMem-
443 lTarget-index[stage];
445 createAugmentedVec(index[stage]-base_size+40,
446 buf+LPC_FILTERORDER+lMem,aug_vec);
450 int filterno, position;
452 filterno=index[stage]/base_size;
453 position=index[stage]-filterno*base_size;
461 if (position<(base_size-20)) {
462 pp=cbvectors+filterno*lMem-lTarget-
463 index[stage]+filterno*base_size;
466 index[stage]-(filterno+1)*base_size+40,
467 cbvectors+filterno*lMem,aug_vec);
476 for (j=0;j<lTarget;j++) {
477 cvec[j] += gain*(*pp);
478 target[j] -= gain*(*pp++);
489 for (i=0; i<lTarget; i++) {
490 cene+=cvec[i]*cvec[i];
494 for (i=gain_index[0]; i<32; i++) {
495 ftmp=cene*gain_sq5Tbl[i]*gain_sq5Tbl[i];
497 if ((ftmp<(tene*gains[0]*gains[0])) &&
498 (gain_sq5Tbl[j]<(2.0*gains[0]))) {