From 9fa1f6563a431a73d7eb0c8a284e4cf419cbc2fa Mon Sep 17 00:00:00 2001 From: markster Date: Tue, 15 Apr 2003 04:36:52 +0000 Subject: Add iLBC codec git-svn-id: http://svn.digium.com/svn/asterisk/trunk@852 f38db490-d61c-443f-a65b-d21fe96a405b --- codecs/ilbc/iCBSearch.c | 461 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 461 insertions(+) create mode 100755 codecs/ilbc/iCBSearch.c (limited to 'codecs/ilbc/iCBSearch.c') diff --git a/codecs/ilbc/iCBSearch.c b/codecs/ilbc/iCBSearch.c new file mode 100755 index 000000000..a6f3030fa --- /dev/null +++ b/codecs/ilbc/iCBSearch.c @@ -0,0 +1,461 @@ + +/****************************************************************** + + iLBC Speech Coder ANSI-C Source Code + + iCBSearch.c + + Copyright (c) 2001, + Global IP Sound AB. + All rights reserved. + +******************************************************************/ + +#include +#include + +#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( + 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;i0.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)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) range) { + sInd -= (eInd-range); + eInd = range; + } + } else { /* base_index >= (base_size-20) */ + + if(sInd < (base_size-20)) { + sIndAug = 20; + sInd = 0; + eInd = 0; + eIndAug = 19 + CB_RESRANGE; + + if(eIndAug > 39) { + eInd = eIndAug-39; + eIndAug = 39; + } + } else { + sIndAug = 20 + sInd - (base_size-20); + eIndAug = 39; + sInd = 0; + eInd = CB_RESRANGE - (eIndAug-sIndAug+1); + } + } + + } else { /* lTarget = 22 */ + + if (sInd < 0) { + eInd -= sInd; + sInd = 0; + } + + if(eInd > range) { + sInd -= (eInd - range); + eInd = range; + } + } + } + + /* search of higher codebook section */ + + /* index search range */ + counter = sInd; + sInd += base_size; + eInd += base_size; + + + if(stage==0) { + ppe = energy+base_size; + *ppe=0.0; + + pp=cbvectors+lMem-lTarget; + for (j=0; j0.0) { + invenergy[icount] = (float) 1.0/(energy[icount]+EPS); + } else { + invenergy[icount] = (float) 0.0; + } + + if (stage==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) { + gain = (float)CB_MAXGAIN; + } + gain = gainquant(gain, 1.0, 32, &gain_index[stage]); + } + else { + if (stage==1) { + gain = gainquant(gain, (float)fabs(gains[stage-1]), + 16, &gain_index[stage]); + } else { + gain = gainquant(gain, (float)fabs(gains[stage-1]), + 8, &gain_index[stage]); + } + } + + /* Extract the best (according to measure) codebook vector */ + + if(lTarget==(STATE_LEN-STATE_SHORT_LEN)) { + + if(index[stage]