📄 g723codec.c
字号:
e1=(int)s64; preErr[4] = preErr[2]; preErr[3] = preErr[1]; preErr[2] = preErr[0]; preErr[1] = e1; preErr[0] = e0; return; } /**********FixcdbkSearch_G723****/void FixcdbkSearch_G723(short olp,short Aclag,short AcGainIndex,short *i_resp,short *src,short *fixedvec,short *grid,short *diract,short *gain_index,int *pos,short *sign,short *times,short rate,int m) { short T0,T0_gain; short FixedCorr[sfsize]; short FixedVector[sfsize]; short Toepliz[toepsize]; short buffer[sfsize]; short i_sign[4]; short i_pos[4]; int i; short gain;// printf("Welcome to FixcdbkSearch_G723\n"); switch(rate) { case 0:/*Rate63*/ { ippsMPMLQFixedCodebookSearch_G723(olp,i_resp,src,fixedvec,grid,diract,gain_index,sign,pos,(short)m); break; } case 1:/*Rate53*/ { T0=olp-1+Aclag+epsi170[AcGainIndex]; T0_gain=gain170[AcGainIndex]; for(i=0;i<sfsize;i++) i_resp[i]>>=1; //Q13->Q12 if(T0<sfsize-2) HarmonicFilter2_I(&i_resp[T0],T0,T0_gain,sfsize-T0); ippsZero_16s(FixedCorr,sfsize); ippsZero_16s(Toepliz,toepsize); ippsCrossCorr_16s(i_resp,sfsize,src,sfsize,FixedCorr,sfsize,0); ippsToeplizMatrix_G723_16s(i_resp,Toepliz); ippsACELPFixedCodebookSearch_G723_16s(FixedCorr,Toepliz,i_sign,i_pos,grid,FixedVector,times); CodeWordImpConv_G723(i_resp,buffer,i_sign,i_pos,sign,pos); GetGain_G723(src,buffer,&gain,gain_index); ippsMulC_16s(gain,FixedVector,fixedvec,sfsize); if(T0<sfsize-2) HarmonicFilter2_I(&fixedvec[T0],T0,T0_gain,sfsize-T0); *diract=0; // printf("out from Fixedcdbk search!\n"); break; } default: { printf("Didn't use the right rate!\n"); break; } }}/********CodeWordImpConv_G723********/void CodeWordImpConv_G723(short *src,short *dst,short *src_sign,short *src_pos,short *dst_sign,int *dst_pos){ short p0,p1,p2,p3; short s0,s1,s2,s3; short i=0; // printf("Welcome to CodeWordImpConv_G723\n"); p0=src_pos[0]; p1=src_pos[1]; p2=src_pos[2]; p3=src_pos[3]; s0=src_sign[0]; s1=src_sign[1]; s2=src_sign[2]; s3=src_sign[3]; *dst_pos=p0>>3; *dst_pos+=p1&(int)0xfff8; //p1=0x00y2 p2=0x00y4 p3=0x00y6 *dst_pos+=(p2&(int)0xfff8)<<3; *dst_pos+=(p3&(int)0xfff8)<<6; if(s0>0) i=1; if(s1>0) i+=2; if(s2>0) i+=4; if(s3>0) i+=8; *dst_sign=i; if(p0>p1) {i=p0;p0=p1;p1=i;i=s0;s0=s1;s1=i;} if(p2>p3) {i=p2;p2=p3;p3=i;i=s2;s2=s3;s3=i;} if(p0>p2) {i=p0;p0=p2;p2=i;i=s0;s0=s2;s2=i;} if(p1>p3) {i=p1;p1=p3;p3=i;i=s1;s1=s3;s3=i;} if(p1>p2) {i=p1;p1=p2;p2=i;i=s1;s1=s2;s2=i;} ippsZero_16s(dst,sfsize); for(i=0;i<p0;i++) dst[i]=0; for(i=p0;i<p1;i++) dst[i]=s0*src[i-p0]; for(i=p1;i<p2;i++) dst[i]=s1*src[i-p1]+s0*src[i-p0]; for(i=p2;i<p3;i++) dst[i]=s2*src[i-p2]+s1*src[i-p1]+s0*src[i-p0]; for(i=p3;i<sfsize;i++) dst[i]=s3*src[i-p3]+s2*src[i-p2]+s1*src[i-p1]+s0*src[i-p0]; return;}/********GetGain_G723**************/void GetGain_G723(short *residual,short *src,short *gain,short *index){ short buffer[sfsize]; int i,xy,yy; short Ener_xy,Ener_yy; short exp,exp_xy,exp_yy,diffmin,dist; // printf("Welcome to GetGain_G723\n"); for(i=0;i<sfsize;i++) buffer[i]=src[i]>>3; ippsDotProd_16s(src,buffer,sfsize,&Ener_xy); exp_xy=Norm_16s(&Ener_xy); xy=(int)Ener_xy>>1; *index=0; if(xy>0) { ippsDotProd_16s(buffer,buffer,sfsize,&Ener_yy); exp_yy=Norm_16s(&Ener_yy); yy=(short)Ener_yy; if(yy>0) *gain=(xy<<15)/yy; else *gain=(short)0x7fff; exp=exp_xy-exp_yy+5; if(exp>0) *gain>>=exp; else *gain<<=(short)(-1*exp); diffmin=abs(*gain-FcbkGainTable[0]); for(i=1;i<FixedCodeGainLev;i++) { dist=abs(*gain-FcbkGainTable[i]); if(diffmin>dist) {diffmin=dist;*index=i;} } } *gain=FcbkGainTable[*index];} /*********Excitation Residual************/void ExcitationResidual_G723_16s(Ipp16s *pSrc1,Ipp16s *pSrc2, Ipp16s *pSrcDst){ short sconv[2*sfsize-1]; ippsConv_16s_Sfs(pSrc1,sfsize,pSrc2,sfsize,sconv,14); ippsSub_16s_I(sconv,pSrcDst,sfsize); return;} /*********harmonic filter 2---H(Z)=1/(1-bZ^(-1))**************/void HarmonicFilter2_I(short *srcDst,short delay,short gain,int len){ short *tmp; int j; tmp=srcDst-delay; for(j=0;j<len;j++) { srcDst[j]=srcDst[j]+(((*tmp)*gain)>>15); tmp++; } return;} /*********harmonic filter **************/void HarmonicFilter(short *src,short delay,short gain,short *dst,int len){ short *tmp; int j; tmp=src-delay; for(j=0;j<len;j++) { dst[j]=src[j]-(((*tmp)*gain)>>15); tmp++; } return;} /*********weight Filter ***************/void WeightFilter_G723(short *src,short *lpc,short *dst,int len,short *DelayLine){ int d,k; int tmp; short arg[2*lpclen];// printf("Welcome to WeightFilter_G723\n"); ippsMul_16s_Sfs(lpc,PerFiltZeroTable,arg,lpclen,15); ippsMul_16s_Sfs(lpc,PerFiltPoleTable,arg+lpclen,lpclen,15); for(k=0;k<len;k++) { // FIR part tmp=src[k]*(short)0x2000; for(d=0;d<lpclen;d++) tmp-=DelayLine[d]*arg[d]; for(d=lpclen-1;d>0;d--) DelayLine[d]=DelayLine[d-1]; DelayLine[0]=src[k]; // IIR part for(d=0;d<lpclen;d++) tmp+=arg[lpclen+d]*DelayLine[lpclen+d]; for(d=lpclen-1;d>0;d--) DelayLine[lpclen+d]=DelayLine[lpclen+d-1]; DelayLine[lpclen]=(tmp+(int)0x1000)>>13; dst[k]=DelayLine[lpclen]; } return ; }/*********Memory shift****************/void Mem_Shift(short *preData, short *curData ){ short Dpnt[fsize];// printf("Welcome to Mem_Shift\n"); ippsZero_16s(Dpnt,fsize); ippsCopy_16s(&preData[sfsize],Dpnt,sfsize); ippsCopy_16s(&curData[2*sfsize],preData,2*sfsize); ippsCopy_16s(curData,&Dpnt[sfsize],3*sfsize); ippsCopy_16s(Dpnt,curData,fsize); return; } /*********HighPassFilter****************/void HighPassFilter_G723_I(short *psrcDst,int len,int *DelayLine){ int tmp; Ipp64s buf; int i;// printf("Welcome to HighPassFilter_G723_I\n"); for(i=0;i<len;i++) { tmp=(psrcDst[i]<<15)-(DelayLine[0]<<15); buf=(Ipp64s)DelayLine[1]*(Ipp64s)0x7f00; tmp+=buf>>15; DelayLine[0]=psrcDst[i]; DelayLine[1]=tmp; tmp+=(int)0x8000; tmp>>=15; if(tmp>IPP_MAX_16S) psrcDst[i]=IPP_MAX_16S; else if(tmp<IPP_MIN_16S) psrcDst[i]=IPP_MIN_16S; else psrcDst[i]=(short)tmp; } return;}void QuantSIDGain_G723_16s(const Ipp16s *pSrc, const Ipp16s *pSrcSfs, int len, int *pIndx) { short temp, iseg, iseg_p1; short j, j2, k, exp; int L_x, L_y; short sfs; int L_z; int i;// printf("Welcome to QuantSIDGain_G723_16s\n"); if(len == 0) { /* Quantize energy */ temp = (*pSrcSfs)<<1; temp = 16 - temp; L_z = (*pSrc)<<temp; L_x = MulC_32s(fact[0], L_z); // L_x=2^16*(*pSrc)/120 real value in -sfs scaling } else { /* weighted average */ sfs = pSrcSfs[0]; for(i=1; i<len; i++) { if(pSrcSfs[i] < sfs) sfs = pSrcSfs[i]; // find the minimal sfs } for(i=0, L_x=0; i<len; i++) { temp = pSrcSfs[i] - sfs; temp = pSrc[i]>>temp; temp = ((fact[len]*temp)+(short)0x4000)>>15; // fact[0]=1/120 fact[1]=1/33 fact[2]=1/66 fact[3]=1/99 L_x += temp; // L_x get the sum value of the pSrc[i]*fact[len] in sfs scaling } temp = 15 - sfs; if(temp < 0) L_x >>= (-temp); else L_x <<= temp; } // L_x=the sum of pSrc[i]*fact[len]*2^15 no scaling /* Quantize */ *pIndx = 63; if(L_x < L_bseg[2]) { /* Compute segment number */ if(L_x >= L_bseg[1]) { exp = 4; iseg = 2; } else { exp = 3; if(L_x >= L_bseg[0]) iseg = 1; else iseg = 0; } iseg_p1 = iseg + 1; j = 1<<exp; k = j>>1; /* Binary search */ for(i=0; i<exp; i++) { // combine L_y to equal L_x, L_y is combined by an 6-bits code word temp = base[iseg] + (j<<iseg_p1); // bit(6,5) represents the area number, bit(4-1) represents the retail value L_y = 2 * temp * temp; // when bit(6,5)=11, bit(5-1) represent the retail value. if(L_x >= L_y) j += k; // L_y=2*{A(b5,4--b1)*2^(B(b6,b5)+1)+base[B(b6,b5)]}^2 else j -= k; k >>= 1; } temp = base[iseg] + (j<<iseg_p1); // handle the compute difference by finding the most precise value L_y = 2* temp * temp - L_x; // use j= j or j+1 or j-1 if(L_y <= 0) { j2 = j + 1; temp = base[iseg] + (j2<<iseg_p1); L_z = L_x - 2 * temp * temp; if(L_y > L_z) temp = (iseg<<4) + j; else temp = (iseg<<4)+j2; } else { j2 = j - 1; temp = base[iseg] + (j2<<iseg_p1); L_z = L_x - 2 * temp * temp; if(L_y < L_z) temp = (iseg<<4) + j; else temp = (iseg<<4) + j2; } *pIndx = temp; } }void DecodeSIDGain_G723_16s (int pIndx, Ipp16s *pGain) {/*** Decoding of quantized SID gain. Section A.4.3**** quantGainIndx index of quantized SID gain** pGain pouinter to the decoded gain value << 5***/ short i, iseg; short temp; // printf("Welcome to DecodeSIDGain_G723_16s\n"); iseg = pIndx>>4; if(iseg == 3) iseg = 2; i = pIndx - (iseg<<4); temp = iseg + 1; temp = i<<temp; temp += base[iseg]; /* SidGain */ temp <<= 5; /* << 5 */ *pGain = temp;}void C2omfortNoiseExcitation_G723_16s (Ipp16s gain, Ipp16s *pPrevExc, Ipp16s *pExc, Ipp16s *pSeed, Ipp16s *pOlp, Ipp16s *pLags, Ipp16s *pGains, int rat, char *buff){ int i, i_subfr, iblk; short temp, temp2; short j; short *TabPos, *TabSign; short *ptr_TabPos, *ptr_TabSign; short *curExc; short sfs, x1, x2, delta, b0; int L_z, L_c, en_exc; short *tmp; short *offset; short a, b; short *tempExc; TabPos = (short *)buff; TabSign = TabPos + 2*NbPulsBlk; // NbPulsBlk=11 tmp = TabSign + 2*NbPulsBlk; offset = tmp + sfsize/2; // 2=2 tempExc = offset + 4; /* generate LTP codes */ pOlp[0] = Random_number(21, pSeed) + 123; //generate long-term-predict period 123<LTP<144 pOlp[1] = Random_number(21, pSeed) + 123; for(i_subfr=0; i_subfr<4; i_subfr++) { /* in [1, NbFilt] */ pGains[i_subfr] = Random_number(NbFilt, pSeed) + 1 ; //NbFilt=50 1--51 } pLags[0] = 1; pLags[1] = 0; pLags[2] = 1; pLags[3] = 3; // genarate 22 pulse (6,5,6,5) /* Generate signs and grids */ for(iblk=0; iblk<4; iblk += 2) { temp = Random_number((1 << (NbPulsBlk+2)), pSeed); // 13bits, bit(0,1) for grid, bit(2-12) for sign offset[iblk] = temp & 0x1; temp >>= 1; offset[iblk+1] = sfsize + (temp & 0x1); for(i=0; i<NbPulsBlk; i++) { TabSign[i+NbPulsBlk*(iblk>>1)] = ((temp & 0x2) - 1)<<14; // 1<<14 or -1<<14 temp >>= 1; } } /* Generate positions */ ptr_TabPos = TabPos; for(i_subfr=0; i_subfr<4; i_subfr++) { for(i=0; i<(sfsize/2); i++) tmp[i] = i; temp = (sfsize/2); for(i=0; i<Nb_puls[i_subfr]; i++) { j = Random_number(temp, pSeed); *ptr_TabPos++ = (tmp[j]<<1) + offset[i_subfr]; temp --; // decrease total number, tmp[j]=tmp[temp] change the pos of tmp[j] = tmp[temp]; // tmp[temp] to the pos among 0--temp and discard the pos j, } // lest some pos relocated while some have small probability of } // of been allocated. /* Compute fixed codebook gains */ ptr_TabPos = TabPos; ptr_TabSign = TabSign; curExc = pExc; i_subfr = 0; for(iblk=0; iblk<2; iblk++) { /* decode LTP only */ { short lag1 = pOlp[iblk]; short lag2 = pLags[i_subfr]; ippsDecodeAdaptiveVector_G723_16s(lag1, lag2, pGains[i_subfr], &pPrevExc[0], curExc, rate_vec[rat]); lag2 = pLags[i_subfr+1]; ippsDecodeAdaptiveVector_G723_16s(lag1, lag2, pGains[i_subfr+1], &pPrevExc[sfsize], &curExc[sfsize], rate_vec[rat]); } /*ippsMaxAbs_16s(curExc,2*sfsize,&temp2);*/ ippsMax_16s(curExc,2*sfsize,&temp2); ippsMin_16s(curExc,2*sfsize,&temp); if(-temp > temp2) temp2 = -temp; if(temp2 == 0) sfs = 0; else { sfs = 4 - Exp_16s(temp2); /* 4 bits of margin */ // temp out as 0(1)00001----0 if(sfs < -2) sfs = -2; } if(sfs<0) ShiftL_16s(curExc,-sfs,tempExc,2*sfsize); else ShiftR_16s(curExc,sfs,tempExc,2*sfsize); L_z = tempExc[ptr_TabPos[0]] * ptr_TabSign[0]; for(i=1; i<NbPulsBlk; i++) { L_z += tempExc[ptr_TabPos[i]] * ptr_TabSign[i]; } temp = L_z>>14; b0 = ((temp*2979)+0x4000)>>15; // add all adpt_vec*sign(fixed) together divided by 11 /* excitation energy */ en_exc=0; for(i=0;i<2*sfsize;i++) en_exc+=tempExc[i]*tempExc[i]; en_exc<<=1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -