📄 g723codec.c
字号:
/* compute 2*sfsize x gain**2 x 2**(-2sh1+1) */ // sh1=sfs /* gain input = 2**5 gain */ L_z = gain * sfsize; L_z = L_z>>5; temp = (short)L_z; L_z = 2 * temp * gain; temp = sfs<<1; temp += 4; L_z = L_z>>temp; L_z = en_exc - L_z; L_c = MulC_32s(2979, L_z); /* * 1/NbPuls */ // L_c=(en_exc-2*sfsize*gain^2*2^(-10)*2^(-2sfs+1))/11/2^15 // adpt_energy-fixed_energy /* Solve EQ(X) = X**2 + 2 b0 X + c */ L_z = 2* b0 * b0 - L_c; if(L_z <= 0) { x1 = -b0; } else { delta = ownSqrt_32s(L_z>>1); x1 = delta - b0; x2 = delta + b0; a = abs(x2); b = abs(x1); if (a < b) x1 = -x2; // x1 get the less abs value } /* Update Excitation */ sfs++; if(sfs < 0) temp = x1>>(-sfs); else temp = x1<<sfs; if(temp > (2*Gexc_Max)) temp = (2*Gexc_Max); //Gexc_Max=5000 if(temp < -(2*Gexc_Max)) temp = -(2*Gexc_Max); for(i=0; i<NbPulsBlk; i++) { j = *ptr_TabPos++; curExc[j] = curExc[j] + (temp * (*ptr_TabSign++)>>15); // adpt_vector+fixed_vector } /* update PrevExc */ ippsCopy_16s(&pPrevExc[2*sfsize],pPrevExc,pitchmax-2*sfsize); ippsCopy_16s(curExc,&pPrevExc[pitchmax-2*sfsize],2*sfsize); curExc += 2*sfsize; i_subfr += 2; } }void ComfortNoiseExcitation_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; short mm; 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 & (short)0x1; temp >>= 1; offset[iblk+1] = sfsize + (temp & (short)0x1); for(i=0; i<NbPulsBlk; i++) { TabSign[i+NbPulsBlk*(iblk>>1)] = ((temp & (short)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)+(short)0x4000)>>15; // add all adpt_vec*sign(fixed) together divided by 11 /* excitation energy */ ippsDotProd_16s_Sfs(tempExc,tempExc,2*sfsize,&mm,14); // tempExc is <11 bits, energy is <22 bits, mm<22-7bits en_exc=mm<<15; /* compute 2*sfsize x gain**2 x 2**(-2sh1+1) */ // sh1=sfs /* gain input = 2**5 gain */ L_z = gain * sfsize; L_z = L_z>>5; temp = (short)L_z; L_z = 2 * temp * gain; temp = sfs<<1; temp += 4; L_z = L_z>>temp; L_z = en_exc - L_z; L_c = MulC_32s(2979,L_z); /* * 1/NbPuls */ // L_c=(en_exc-2*sfsize*gain^2*2^(-10)*2^(-2sfs+1))/11 /* Solve EQ(X) = X**2 + 2 b0 X + c */ // solve the equation x^2+2b*x+c=0 L_z = 2* b0 * b0 - L_c; // x1, -x2 are the values suitable for equation if(L_z <= 0) { x1 = -b0; } else { delta = ownSqrt_32s(L_z>>1); x1 = delta - b0; x2 = delta + b0; a = abs(x2); b = abs(x1); if (a < b) x1 = -x2; // x1 get the less abs value } /* Update Excitation */ sfs++; if(sfs < 0) temp = x1>>(-sfs); else temp = x1<<sfs; if(temp > (2*Gexc_Max)) temp = (2*Gexc_Max); //Gexc_Max=5000 if(temp < -(2*Gexc_Max)) temp = -(2*Gexc_Max); for(i=0; i<NbPulsBlk; i++) { j = *ptr_TabPos++; curExc[j] = curExc[j] + (temp * (*ptr_TabSign++)>>15); // adpt_vector+fixed_vector } /* update PrevExc */ ippsCopy_16s(&pPrevExc[2*sfsize],pPrevExc,pitchmax-2*sfsize); ippsCopy_16s(curExc,&pPrevExc[pitchmax-2*sfsize],2*sfsize); curExc += 2*sfsize; i_subfr += 2; } }void Comp_Info( short *Buff, short Olp, short *Gain, short *ShGain, short *Vdelay) // define voiced or unvoiced signal{ //Gain for SidGain int i ; // ShGain for CurGain int Acc0 ; short Tenr ; short Ccr,Enr ; short Indx ; short *src ; int Rez; /* Normalize the excitation */ Rez=3; ArrNorm_16s_I( Buff, pitchmax+fsize, &Rez ) ; *ShGain = Rez; *Vdelay=0; if ( Olp > (short) (pitchmax-3) ) Olp = (short) (pitchmax-3) ; Indx = Olp ; src = &Buff[pitchmax+fsize-2*sfsize]; AutoCorrLagMax_16s(src,2*sfsize,Olp-3,Olp+3,&Ccr,(short *)&i); //get auto relation and the interpolation index if(Ccr <= 0 ) //if auto relation is less than or equal to 0,define as unvoiced return ; Indx = i; /* Compute target energy */ ippsDotProd_16s_Sfs(src,src,2*sfsize,&Tenr,16); // calculate actual signal energy *Gain = Tenr; /* Compute best energy */ ippsDotProd_16s_Sfs(src+Indx,src+Indx,2*sfsize,&Enr,16); //calculate best energy Acc0 = Enr * Tenr; if ( Acc0 < 2 * Ccr * Ccr ) *Vdelay=Indx; return;}void ResidualInterpolation_G723_16s_I(Ipp16s *pSrcDst, Ipp16s *pDst, int lag, Ipp16s gain, Ipp16s *pSeed){ int i ; /* Interpolate accordingly to the voicing estimation */ if ( lag ) {/* Voiced */ for ( i = 0 ; i < lag ; i ++ ){ /* attenuation */ pSrcDst[pitchmax+i-lag] = (pSrcDst[pitchmax+i-lag] * (short)0x6000)>>15; //multiply 3/4,decrease by 2.5dB } for ( i = 0 ; i < fsize ; i ++ ){ /* generate periodic excitation */ pSrcDst[pitchmax+i] = pSrcDst[pitchmax-lag+i] ; } ippsCopy_16s(&pSrcDst[pitchmax],pDst,fsize); } else {/* Unvoiced */ for ( i = 0 ; i < fsize ; i ++ ) pDst[i] = (gain * Random_16s(pSeed))>>15; /* reset memory */ ippsZero_16s(pSrcDst,fsize+pitchmax); }}/*******Pitch post filte*****/void PitchPost_G723_Sfs(short *src,short *dst,short delay,short gain,short scaleGain,int sfs){ int tmp; int j; for(j=0;j<sfsize;j++) { tmp=(scaleGain*src[j]+gain*src[j+delay]+(int)(1<<(sfs-1)))>>sfs; dst[j]=Cnvrt_32s16s(tmp); }} /******Gain control********/void GainControl_G723_I(short *srcDst,int ener,short *gain){ short Acc1; int gs,k,tmp; short ggs; int sfs=3; short *buf; buf=(short *)malloc(sfsize*sizeof(short)); ippsCopy_16s(srcDst,buf,sfsize); ArrNorm_16s_I(srcDst,sfsize,&sfs); ippsDotProd_16s_Sfs(srcDst,srcDst,sfsize,&Acc1,15); //compute the formant_filtered energy if(Acc1==0) ggs=(short)0x1000; else { sfs=2*sfs; // gs is Q12 if(sfs>=0) gs=ShiftL_32s(ener,sfs)/(int)Acc1; else gs=(ener>>(-sfs))/Acc1; gs=ShiftL_32s(gs,12); ggs=ownSqrt_32s(gs); } tmp=(int)*gain; for(k=0;k<sfsize;k++) { tmp=ShiftL_32s(tmp,16)-ShiftL_32s(tmp,12)+ShiftL_32s(ggs,12); tmp=(tmp+(int)0x8000)>>16; gs=(buf[k]*(tmp+(tmp>>4))+(int)0x400)>>12; srcDst[k]=(gs>IPP_MAX_16S? IPP_MAX_16S:(gs<IPP_MIN_16S? IPP_MIN_16S:(short)gs)); } *gain=tmp; free(buf);} /******Formant postfilter*******/void FormantPostFilter_G723_I(short *srcDst,int *Ener,short *lpc,int *k_old,short *DelayLine) //*k_old is Q15{ int Acc0,Acc1; int *tmp,dng,shf; short exp; int k,i,j; Ipp16s *pTaps; short vec[sfsize]; short buf;// printf("Welcome to FormantPostFilter_G723_I\n"); tmp=(int *)malloc(sfsize*sizeof(int)); ippsCopy_16s(srcDst,vec,sfsize); shf=3; ArrNorm_16s_I(vec,sfsize,&shf); for(j=0;j<sfsize;j++) Acc0=Acc0+vec[j]*vec[j]; Acc0<<=1; for(j=1;j<sfsize;j++) Acc1=Acc1+vec[j-1]*vec[j]; Acc1<<=1; exp=(short)(2*shf+4); if(exp<0) *Ener=ShiftL_32s(Acc0, (unsigned short)(-exp)); else *Ener=Acc0>>exp; buf=DelayLine[lpclen]; pTaps=(Ipp16s *)malloc(lpclen*2*sizeof(short)); ippsMul_16s_Sfs(lpc,PostFiltTable,pTaps,lpclen,15); ippsMul_16s_Sfs(lpc,PostFiltTable+lpclen,&pTaps[lpclen],lpclen,15); for(i=0;i<sfsize;i++) { tmp[i]=srcDst[i]*(int)0x2000; for(j=0;j<lpclen;j++) tmp[i]-=pTaps[j]*DelayLine[j]; DelayLine[0]=srcDst[i]; } for(i=0;i<sfsize;i++) { dng=tmp[i]; for(j=0;j<lpclen;j++) dng+=pTaps[j+lpclen]*DelayLine[j+lpclen]; tmp[i]=dng; DelayLine[lpclen]=Cnvrt_32s16s((dng+(int)0x1000)>>13); } free(pTaps); exp = Acc0>>16; if (exp ) { /* Compute first parkor */ Acc1>>=1; Acc1 = abs(Acc1); exp = Acc1/(Acc0>>16) ; if ( Acc1 < 0 ) exp = -exp; } k=((*k_old)<<15)-((*k_old)<<13)+exp*(int)0x2000; *k_old=(k+(int)0x4000)>>15; k=(*k_old)>>2; for(j=0;j<sfsize;j++) { srcDst[j]=(short)(((tmp[j]<<2)-k*buf+(int)0x4000)>>15); buf=(short)((tmp[j]+(int)0x1000)>>13); } free(tmp);} /******fixed codebook vector****/void FixedCodebookVector_G723_16s( int pos, int amp, int ampindex, int grid, int gain, int subfr, int rate, Ipp16s *pDst, int *pLag, Ipp16s *pGain ){ int i,j ; int Acc0 ; short cdbk_gain, cdbk_sign, cdbk_shift, cdbk_pos; int offset, ipos; // printf("Welcome to FixedCodebookVector_G723_16s\n"); switch(rate) { case 0 /* Rate63*/: { ippsZero_16s(pDst,sfsize); if ( pos < MaxPosTable[subfr] ){ /* Decode the amplitudes and positions */ j = MaxPulseNum - Nb_puls[subfr] ; Acc0 = pos ; for ( i = 0 ; i < sfsize/2 ; i ++ ) { Acc0 -= CombinatorialTable[j][i]; if ( Acc0 < 0 ) { Acc0 += CombinatorialTable[j][i]; j++ ; if ( (amp & (1 << (MaxPulseNum-j) )) != 0 ) //1,0 represent -1,1 respectively pDst[grid + 2*i] = -FcbkGainTable[ampindex] ; //impulse 0-5 in amp from L to R. differnt from else //that in rate53 pDst[grid + 2*i] = FcbkGainTable[ampindex] ; if ( j == MaxPulseNum ) break ; } } } break; } case 1 /*Rate53*/: { ippsZero_16s(pDst,sfsize); cdbk_gain = FcbkGainTable[ampindex]; cdbk_shift = grid; cdbk_sign = amp<<1; // for 0,1 to -1,1 using 2*i-1 cdbk_pos = (short) pos; for(offset=0; offset<8; offset+=2) { ipos = (cdbk_pos & (short)0x7) ; ipos = (ipos<<3) + cdbk_shift + offset; if (ipos < sfsize) pDst[ipos] = cdbk_gain * ((cdbk_sign & 2 ) - 1); cdbk_pos >>= 3; cdbk_sign >>= 1; } *pLag = epsi170[gain]; *pGain = gain170[gain]; break; } }} /******Unpack the line******/void Unpack(char *bitstream,Line *dst_line,short *rate,short *Ftyp) { int j,bit,bound; int dd; char *data;// printf("Welcome to Unpack\n"); data=bitstream; bit=0; //read control bits ReadFromBitstream(&data,&bit,&dd,2); if(dd==3) { *Ftyp=0; dst_line->lspID=0; return; }else if(dd==2) { *Ftyp=2; //read lsp_index ReadFromBitstream(&data,&bit,&dd,24); dst_line->lspID=dd; ReadFromBitstream(&data,&bit,&dd,6); dst_line->ampindex[0]=dd; return; }else if(dd==0) //high rate *rate=0; else *rate=1; *Ftyp=1; //read lsp_index
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -