📄 icbsearch.c
字号:
/******************************************************************
iLBC Speech Coder ANSI-C Source Code
iCBSearch.c
Copyright (c) 2001,
Global IP Sound AB.
All rights reserved.
******************************************************************/
#include <math.h>
#include <string.h>
#include "iLBC_define.h"
#include "gainquant.h"
#include "createCB.h"
#include "filter.h"
#include "constants.h"
/*----------------------------------------------------------------*
* Search routine for codebook encoding and gain quantization.
*---------------------------------------------------------------*/
void iCBSearch(
iLBC_Enc_Inst_t *iLBCenc_inst,
/* (i) the encoder state structure */
int *index, /* (o) Codebook indices */
int *gain_index,/* (o) Gain quantization indices */
float *intarget,/* (i) Target vector for encoding */
float *mem, /* (i) Buffer for codebook construction */
int lMem, /* (i) Length of buffer */
int lTarget, /* (i) Length of vector */
int nStages, /* (i) Number of codebook stages */
float *weightDenum, /* (i) weighting filter coefficients */
float *weightState, /* (i) weighting filter state */
int block /* (i) the subblock number */
){
int i, j, icount, stage, best_index, range, counter;
float max_measure, gain, measure, crossDot, ftmp;
float gains[CB_NSTAGES];
float target[SUBL];
int base_index, sInd, eInd, base_size;
int sIndAug=0, eIndAug=0;
float buf[CB_MEML+SUBL+2*LPC_FILTERORDER];
float invenergy[CB_EXPAND*128], energy[CB_EXPAND*128];
float *pp, *ppi=0, *ppo=0, *ppe=0;
float cbvectors[CB_MEML];
float tene, cene, cvec[SUBL];
float aug_vec[SUBL];
memset(cvec,0,SUBL*sizeof(float));
/* Determine size of codebook sections */
base_size=lMem-lTarget+1;
if (lTarget==SUBL) {
base_size=lMem-lTarget+1+lTarget/2;
}
/* setup buffer for weighting */
memcpy(buf,weightState,sizeof(float)*LPC_FILTERORDER);
memcpy(buf+LPC_FILTERORDER,mem,lMem*sizeof(float));
memcpy(buf+LPC_FILTERORDER+lMem,intarget,lTarget*sizeof(float));
/* weighting */
AllPoleFilter(buf+LPC_FILTERORDER, weightDenum,
lMem+lTarget, LPC_FILTERORDER);
/* Construct the codebook and target needed */
memcpy(target, buf+LPC_FILTERORDER+lMem, lTarget*sizeof(float));
tene=0.0;
for (i=0; i<lTarget; i++) {
tene+=target[i]*target[i];
}
/* Prepare search over one more codebook section. This section
is created by filtering the original buffer with a filter. */
filteredCBvecs(cbvectors, buf+LPC_FILTERORDER, lMem);
/* The Main Loop over stages */
for (stage=0; stage<nStages; stage++) {
range = search_rangeTbl[block][stage];
/* initialize search measure */
max_measure = (float)-10000000.0;
gain = (float)0.0;
best_index = 0;
/* Compute cross dot product between the target
and the CB memory */
crossDot=0.0;
pp=buf+LPC_FILTERORDER+lMem-lTarget;
for (j=0; j<lTarget; j++) {
crossDot += target[j]*(*pp++);
}
if (stage==0) {
/* Calculate energy in the first block of
'lTarget' sampels. */
ppe = energy;
ppi = buf+LPC_FILTERORDER+lMem-lTarget-1;
ppo = buf+LPC_FILTERORDER+lMem-1;
*ppe=0.0;
pp=buf+LPC_FILTERORDER+lMem-lTarget;
for (j=0; j<lTarget; j++) {
*ppe+=(*pp)*(*pp++);
}
if (*ppe>0.0) {
invenergy[0] = (float) 1.0 / (*ppe + EPS);
} else {
invenergy[0] = (float) 0.0;
}
ppe++;
measure=(float)-10000000.0;
if (crossDot > 0.0) {
measure = crossDot*crossDot*invenergy[0];
}
}
else {
measure = crossDot*crossDot*invenergy[0];
}
/* check if measure is better */
ftmp = crossDot*invenergy[0];
if ((measure>max_measure) && (fabs(ftmp)<CB_MAXGAIN)) {
best_index = 0;
max_measure = measure;
gain = ftmp;
}
/* loop over lags 40+ in the first codebook section,
full search */
for (icount=1; icount<range; icount++) {
/* calculate measure */
crossDot=0.0;
pp = buf+LPC_FILTERORDER+lMem-lTarget-icount;
for (j=0; j<lTarget; j++) {
crossDot += target[j]*(*pp++);
}
if (stage==0) {
*ppe++ = energy[icount-1] + (*ppi)*(*ppi) -
(*ppo)*(*ppo);
ppo--;
ppi--;
if (energy[icount]>0.0) {
invenergy[icount] =
(float)1.0/(energy[icount]+EPS);
} else {
invenergy[icount] = (float) 0.0;
}
measure=(float)-10000000.0;
if (crossDot > 0.0) {
measure = crossDot*crossDot*invenergy[icount];
}
}
else {
measure = crossDot*crossDot*invenergy[icount];
}
/* check if measure is better */
ftmp = crossDot*invenergy[icount];
if ((measure>max_measure) && (fabs(ftmp)<CB_MAXGAIN)){
best_index = icount;
max_measure = measure;
gain = ftmp;
}
}
/* Loop over lags 20-39 in the first codebook section,
* full search.
* The vectors are interpolated.
*/
if (lTarget==SUBL) {
/* Search for best possible lag and compute
the CB-vectors' energy. */
searchAugmentedCB(20, 39, stage, base_size-lTarget/2,
target, buf+LPC_FILTERORDER+lMem,
&max_measure, &best_index, &gain, energy, invenergy);
}
/* set search range for following codebook sections */
base_index=best_index;
/* unrestricted search */
if (CB_RESRANGE == -1) {
sInd=0;
eInd=range-1;
sIndAug=20;
eIndAug=39;
}
/* restriced search around best index from first
codebook section */
else {
/* Initialize search indices */
sIndAug=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -