⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 g722.cpp

📁 G722语音压缩算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	  else if (szl < -32768) szl = -32768;
	*/
	szl += (*++blp * *++dltp)>>14;
	/*
	  if (szl > 32767) szl = 32767;
	  else if (szl < -32768) szl = -32768;
	*/
	szl += (*++blp * *++dltp)>>14;
	/*
	  if (szl > 32767) szl = 32767;
	  else if (szl < -32768) szl = -32768;
	*/
	szl += (*++blp * *++dltp)>>14;
	/*
	  if (szl > 32767) szl = 32767;
	  else if (szl < -32768) szl = -32768;
	*/
    
    /*********************************BLOCK 4L, PREDIC  *******************/
    
    
    sl = spl + szl ;
    /*
      if (sl > 32767) sl = 32767;
      if (sl < -32768) sl = -32768;
    */
    return (sl) ;
}
int decoder_block4h(int d)		//高子带自适应预测
{	static int sh = 0 ;
    int wd1, wd2, wd3;
    static int sph = 0 ;
    static int szh = 0 ;
    
    static int rh0=0, rh1=0, rh2=0 ;
    static int ah1=0, ah2=0 ;
    static int ph0=0, ph1=0, ph2=0 ;
    static int dh  [7] = { 0, 0, 0, 0, 0, 0, 0 } ;
    static int bh   [7] = { 0, 0, 0, 0, 0, 0, 0 } ;
    static int bph  [7] = { 0, 0, 0, 0, 0, 0, 0 } ;
    static int sg0=0, sg1=0, sg2=0;
    static int sg   [7] = { 0, 0, 0, 0, 0, 0, 0 } ;
    int *dhp, *dhp_1, *bhp, *bphp, *sgp;
    dh[0] = d;
    /*************************************** BLOCK 4H, RECONS  ***********/ 
    rh0 = sh + d ;
    /*
      if (rh0 > 32767) rh0 = 32767;
      if (rh0 < -32768) rh0 = -32768;
    */
    /*************************************** BLOCK 4H, PARREC  ***********/
    ph0 = d + szh ;
    /*
      if (ph0 > 32767) ph0 = 32767;
      if (ph0 < -32768) ph0 = -32768;
    */
    /*****************************BLOCK 4H,  UPPOL2*************************/
    sg0 = ph0 >> 15 ;
    sg1 = ph1 >> 15 ;
    sg2 = ph2 >> 15 ;
    wd1 = ah1 << 2;
    if (wd1 > 32767)
		wd1 = 32767;
    else if (wd1 < -32768)
		wd1 = -32768;
    wd2= ( sg0 == sg1 )? - wd1 : wd1 ;
    if (wd2 > 32767) wd2 = 32767;
    wd2 = wd2 >> 7 ;
    wd3= ( sg0 == sg2 )? 128 : - 128 ;
    wd2 = wd2 + wd3 ;
    wd3 = (ah2 * 127) >> 7 ;
    ah2 = wd2 + wd3 ;
    if ( ah2 >  12288 )
		ah2 =  12288 ;
    else if ( ah2 < -12288 )
		ah2 = -12288 ;
    /************************************* BLOCK 4H, UPPOL1  ***************/
    sg0 = ph0 >> 15 ;
    sg1 = ph1 >> 15 ;
    wd1= ( sg0 == sg1 )? 192 : - 192 ;
    wd2 = (ah1 * 255) >> 8 ;
    ah1 = wd1 + wd2 ;
    /*
      if (ah2 > 32767) ah2 = 32767;
      if (ah2 < -32768) ah2 = -32768;
    */ 
    wd3 = (15360 - ah2) ;
    /*
      if (wd3 > 32767) wd3 = 32767;
      if (wd3 < -32768) wd3 = -32768;
    */
    if ( ah1 >  wd3)
		ah1 =  wd3 ;
    else if ( ah1 < -wd3)
		ah1 = -wd3 ;
    /*************************************** BLOCK 4H, UPZERO  ************/
    wd1= ( d == 0 )? 0 : 128 ;
    sg0 = d >> 15 ;
	sgp = sg, dhp = dh, bhp = bh, bphp = bph;
	*++sgp = *++dhp >> 15 ;
	wd2= ( *sgp == sg0 )? wd1 : - wd1 ;
	wd3 = (*++bhp * 255) >> 8 ;
	*++bphp = wd2 + wd3 ;
	*++sgp = *++dhp >> 15 ;
	wd2= ( *sgp == sg0 )? wd1 : - wd1 ;
	wd3 = (*++bhp * 255) >> 8 ;
	*++bphp = wd2 + wd3 ;
	*++sgp = *++dhp >> 15 ;
	wd2= ( *sgp == sg0 )? wd1 : - wd1 ;
	wd3 = (*++bhp * 255) >> 8 ;
	*++bphp = wd2 + wd3 ;
	*++sgp = *++dhp >> 15 ;
	wd2= ( *sgp == sg0 )? wd1 : - wd1 ;
	wd3 = (*++bhp * 255) >> 8 ;
	*++bphp = wd2 + wd3 ;
	*++sgp = *++dhp >> 15 ;
	wd2= ( *sgp == sg0 )? wd1 : - wd1 ;
	wd3 = (*++bhp * 255) >> 8 ;
	*++bphp = wd2 + wd3 ;
	*++sgp = *++dhp >> 15 ;
	wd2= ( *sgp == sg0 )? wd1 : - wd1 ;
	wd3 = (*++bhp * 255) >> 8 ;
	*++bphp = wd2 + wd3 ;
    /********************************* BLOCK 4H, DELAYA  ******************/
    dhp_1=dhp-1,bhp=bh, bphp=bph;
	*dhp--=*dhp_1--;
	*++bhp=*++bphp;
	*dhp--=*dhp_1--;
	*++bhp=*++bphp;
	*dhp--=*dhp_1--;
	*++bhp=*++bphp;
	*dhp--=*dhp_1--;
	*++bhp=*++bphp;
	*dhp--=*dhp_1--;
	*++bhp=*++bphp;
	*dhp--=*dhp_1--;
	*++bhp=*++bphp;
    rh2=rh1;
    rh1=rh0; 
    ph2=ph1;
    ph1=ph0; 
    /********************************* BLOCK 4H, FILTEP  ******************/
    wd1 = ( ah1 * rh1 ) >> 14 ;
    wd2 = ( ah2 * rh2 ) >> 14 ;
    sph = wd1 + wd2 ;
    /*
      if (sph > 32767) sph = 32767;
      if (sph < -32768) sph = -32768;
    */ 
    /*************************************** BLOCK 4H, FILTEZ  ***********/
    dhp = dh, bhp = bh;
	szh = ((*++bhp) * *++dhp )>>14;
	/*
	  if (szh > 32767) szh = 32767;
	  else if (szh < -32768) szh = -32768;
	*/
	szh += ((*++bhp) * *++dhp )>>14;
	/*
	  if (szh > 32767) szh = 32767;
	  else if (szh < -32768) szh = -32768;
	*/
	szh += ((*++bhp) * *++dhp )>>14;
	/*
	  if (szh > 32767) szh = 32767;
	  else if (szh < -32768) szh = -32768;
	*/
	szh += ((*++bhp) * *++dhp )>>14;
	/*
	  if (szh > 32767) szh = 32767;
	  else if (szh < -32768) szh = -32768;
	*/
	szh += ((*++bhp) * *++dhp )>>14;
	/*
	  if (szh > 32767) szh = 32767;
	  else if (szh < -32768) szh = -32768;
	*/
	szh += ((*++bhp) * *++dhp )>>14;
	/*
	  if (szh > 32767) szh = 32767;
	  else if (szh < -32768) szh = -32768;
	*/
    /*********************************BLOCK 4H, PREDIC  *******************/
    sh = sph + szh ;
    /*
      if (sh > 32767) sh = 32767;
      if (sh < -32768) sh = -32768;
    */ 
    return (sh) ;
}
int g722_encoder(pG722_Handle test, short *datap, UINT16 *outdata, int samplesRead)
{	int i,j=0,k=1 ;
//        UINT32 P=(&(*outdata));
	static int nbl=0,nbh=0;
	int  el, eh, wd, wd1,wd2,wd3,il4,ih2,ril,mil,mih, hdu;
	int *xp, *xp_2,*q6p;
	int sumeven, sumodd; 
	while( j< samplesRead)
	{ 
	/* PROCESS PCM THROUGH THE QMF FILTER                               */

nexttwodata:
	xp = x + 23; xp_2 = x +21;

	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;
	*xp-- = *xp_2--;

	*xp = *datap++;
	*--xp = *datap++;

/* DISCARD EVERY OTHER QMF OUTPUT                                   */

		sumeven = *xp++ * 3;	
		sumodd = *xp++ * (-11);	
		sumeven += *xp++ * (-11);	
		sumodd += *xp++ * 53;	
		sumeven += *xp++ * 12;	
		sumodd += *xp++ * (-156);	
		sumeven += *xp++ * 32;	
		sumodd += *xp++ * 362;	
		sumeven += *xp++ * (-210);	
		sumodd += *xp++ * (-805);	
		sumeven += *xp++ * 951;	
		sumodd += *xp++ * 3876;	
		sumeven += *xp++ * 3876;	
		sumodd += *xp++ * 951;	
		sumeven += *xp++ * (-805);	
		sumodd += *xp++ * (-210);	
		sumeven += *xp++ * 362;	
		sumodd += *xp++ * 32;	
		sumeven += *xp++ * (-156);	
		sumodd += *xp++ * 12;	
		sumeven += *xp++ * 53;	
		sumodd += *xp++ * (-11);	
		sumeven += *xp++ * (-11);	
		sumodd += *xp++ * 3;	

	test->rlow = (sumeven + sumodd) >>13;
	test->rhigh = (sumeven - sumodd) >>13;

/*************************************** BLOCK 1L, SUBTRA ************/

	el = test->rlow - test->slow ;

        if ( el > 32767 ) el = 32767;
        else if ( el < -32768 ) el = -32768;

/*************************************** BLOCK 1L, QUANTL ************/

	wd= (el>= 0 ) ?  el: -(el+1) ;
	
	q6p = q6;

	mil = 1;
                for (i=0;i<29;i++)
	     {        
	       wd1 = (*++q6p * test->detlow) >>12 ;		
		   if (wd >= wd1)  mil++;
		   else 
                        break;	
            }
 
	test->ilowr = ( el < 0 ) ? iln[mil] : ilp[mil];

/************************************** BLOCK 2L, INVQAL ************/

	ril = test->ilowr >> 2 ;
	wd2 = qm4[ril] ;
	test->dlowt = (test->detlow * wd2) >> 15 ;

/************************************** BLOCK 3L, LOGSCL *************/

	il4 = rl42[ril] ;

	wd = (nbl * 127) >> 7 ;
	
	nbl = wd + wl[il4] ;

	if (nbl <     0) nbl = 0 ;
	else if (nbl > 18432) nbl = 18432 ;

/************************************** BLOCK 3L, SCALEL *************/
	wd1 =  (nbl >> 6) & 31 ;
	wd2 = nbl >> 11 ;
	wd3=((8 - wd2) < 0) ? ilb[wd1]<<(wd2 - 8) : ilb[wd1]>>(8 - wd2); 
	test->detlow  = wd3 << 2 ;
/************************************** BLOCK 3L, DELAYA *************/

	test->slow = encoder_block4l (test->dlowt) ;


/*************************************** BLOCK 1H, SUBTRA ************/

	eh = test->rhigh - test->shigh ;
/*
        if ( eh > 32767 ) eh = 32767;
        else if ( eh < -32768 ) eh = -32768;
*/
/*************************************** BLOCK 1H, QUANTH ************/

	wd = (eh>= 0 )? eh : -( eh +1 ) ;
	
	hdu = 564 * test->dethigh; 
	wd1 = hdu >> 12 ;
	mih = (wd >= wd1)? 2: 1 ;
	test->ihigh = (eh<0) ? ihn[mih] : ihp[mih] ;

/************************************** BLOCK 2H, INVQAH ************/

	wd2 = qm2[test->ihigh] ;
	test->dhigh = (test->dethigh * wd2) >> 15 ;


/************************************** BLOCK 3H, LOGSCH *************/

	ih2 = rh2[test->ihigh] ;
	wd = (nbh * 127) >> 7 ;
	nbh = wd + wh[ih2] ;

	if (nbh <     0) nbh = 0 ;
	else if (nbh > 22528) nbh = 22528 ;

/************************************** BLOCK 3H, SCALEH *************/
	wd1 =  (nbh >> 6) & 31 ;
	wd2 = nbh >> 11 ;
	wd3=((10-wd2) < 0)? ilb[wd1] << (wd2-10): ilb[wd1] >> (10-wd2) ;
	test->dethigh = wd3 << 2 ;
/************************************** BLOCK 3L, DELAYA *************/

	test->shigh = encoder_block4h (test->dhigh) ;
	if(k>0){
                outdata[j] = test->ilowr;
                outdata[j] = (outdata[j]<<2) + test->ihigh;
                k = -k;
                
                goto nexttwodata;
        
	}
        else {
                outdata[j] = (outdata[j]<<6) + test->ilowr;
                outdata[j] = (outdata[j]<<2) + test->ihigh;
                k = -k;
		}
  	
	j++;
	}
     //   P=(&(*outdata))-P
        return 1;//P/sizeof(UINT16);
}
int g722_decoder(pG722_Handle test, UINT16 *pdecdata, short *pcmout, int samplesRead)
	{	int j,k=1;
           //     UINT32 P= (&(*pcmout));
		static int nbl = 0;
		static int nbh = 0;
		int wd1, wd2, wd3;
		int *xdp, *xsp, *xdp_1, *xsp_1, xout;
		for(j=0; j < samplesRead;)
	{	test->ilowr = *pdecdata >> 10;
		test->ihigh = (*pdecdata >> 8) & 3;
nexttwodata:
	/*********************** BLOCK 5L, LOW BAND INVQBL ****************/
	
	wd2 = qm6[test->ilowr] ;
	wd2 = (test->detlow * wd2 ) >> 15 ;
	/******************************** BLOCK 5L, RECONS ****************/
	test->rlow = test->slow + wd2 ;
	/*
	  if (test->rlow > 32767) test->rlow = 32767;
	  if (test->rlow < -32768) test->rlow = -32768;
	*/
	/******************************** BLOCK 6L, LIMIT ******************/
	/*
	  if (test->rlow > 16383 ) test->rlow = 16383;
	  if (test->rlow < -16383 ) test->rlow = -16383;
	*/
	/************************************** BLOCK 2L, INVQAL ************/
	
	wd1 = test->ilowr >> 2 ;
	wd2 = qm4[wd1] ;
	test->dlowt = (test->detlow * wd2) >> 15 ;
	/************************************** BLOCK 3L, LOGSCL *************/
	
        wd2 = rl42[wd1] ;
        wd1 = (nbl * 127) >> 7 ;
        nbl = wd1 + wl[wd2] ;
        if (nbl < 0)
	    nbl = 0 ;
        else if (nbl > 18432)
	    nbl = 18432 ;
	/************************************** BLOCK 3L, SCALEL *************/
	
        wd1 =  (nbl >> 6) & 31 ;
        wd2 = nbl >> 11 ;
        wd3= ((8 - wd2) < 0)? ilb[wd1] << (wd2 - 8) : ilb[wd1] >> (8 - wd2) ;
        test->detlow = wd3 << 2 ;
	
	test->slow = decoder_block4l (test->dlowt) ;
	/************************************** BLOCK 2H, INVQAH ************/
	
	wd2 = qm2[test->ihigh] ;
	test->dhigh  = (test->dethigh * wd2) >> 15 ;
	
        test->rhigh  = test->dhigh + test->shigh ;
	/*
	  if (test->rhigh >  16383 )  test->rhigh =  16383 ;
	  if (test->rhigh < -16384 )  test->rhigh = -16384 ;
	  */
	/************************************** BLOCK 2H, INVQAH ************/
	
        wd2 = rh2[test->ihigh] ;
        wd1 = (nbh * 127) >> 7 ;
        nbh = wd1 + wh[wd2] ;
	if (nbh <     0)
	    nbh = 0 ;
	else if (nbh > 22528)
	    nbh = 22528 ;
	/************************************** BLOCK 3H, SCALEH *************/
	
        wd1 =  (nbh >> 6) & 31 ;
        wd2 = nbh >> 11 ;
        wd3= ((10 - wd2) < 0)? ilb[wd1] << (wd2 - 10) : ilb[wd1] >> (10 - wd2) ;
        test->dethigh = wd3 << 2 ;
	
	test->shigh = decoder_block4h (test->dhigh) ;
	/******************************* RECIEVE QMF ************************/

		xdp=xd+11, xsp=xs+11, xdp_1=xd+10, xsp_1=xs+10;
		*xdp-- = *xdp_1--; *xsp-- = *xsp_1--; 
		*xdp-- = *xdp_1--; *xsp-- = *xsp_1--; 
		*xdp-- = *xdp_1--; *xsp-- = *xsp_1--; 
		*xdp-- = *xdp_1--; *xsp-- = *xsp_1--; 
		*xdp-- = *xdp_1--; *xsp-- = *xsp_1--; 
		*xdp-- = *xdp_1--; *xsp-- = *xsp_1--; 
		*xdp-- = *xdp_1--; *xsp-- = *xsp_1--; 
		*xdp-- = *xdp_1--; *xsp-- = *xsp_1--; 
		*xdp-- = *xdp_1--; *xsp-- = *xsp_1--; 
		*xdp-- = *xdp_1--; *xsp-- = *xsp_1--; 
		*xdp-- = *xdp_1--; *xsp-- = *xsp_1--; 
	/************************************* RECA ***************************/
	*xdp  = test->rlow - test->rhigh ;
	/*
	  if (*xdp > 16383) *xdp = 16383;
	  if (*xdp < -16384) *xdp = -16384;
	*/
/************************************* RECA ***************************/
	*xsp  = test->rlow + test->rhigh ;
	/*
	  if (*xsp > 16383) *xsp = 16383;
	  if (*xsp < -16384) *xsp = -16384;
	*/
	/************************************* ACCUM C&D *************************/
					/*	qmf tap coefficients	*/
/*      pmf=&tap[0];
		while(*++pmf)
		xout+=*xdp++ * *(pmf);		*/
		xout = *xdp++ * 3;
		xout += *xdp++ * -11;
		xout += *xdp++ * 12;
		xout += *xdp++ * 32;
		xout += *xdp++ * -210;
		xout += *xdp++ * 951;
		xout += *xdp++ * 3876;
		xout += *xdp++ * -805;
		xout += *xdp++ * 362;
		xout += *xdp++ * -156;
		xout += *xdp++ * 53;
		xout += *xdp++ * -11;	

                wd1=xout>>12;
                if(wd1>32767)   wd1=32767;
                else if(wd1<-32768)     wd1=-32768;

		*pcmout++ = wd1 ;
/*        fprintf (stdout, "%5d\n", xout);  output pcm */
//			xout=0;
//			while(*--pmf)
//			xout+=*xsp++ * *(pmf);
		xout = *xsp++ * -11;
		xout += *xsp++ * 53;
		xout += *xsp++ * -156;
		xout += *xsp++ * 362;
		xout += *xsp++ * -805;
		xout += *xsp++ * 3876;
		xout += *xsp++ * 951;
		xout += *xsp++ * -210;
		xout += *xsp++ * 32;
		xout += *xsp++ * 12;
		xout += *xsp++ * -11;
		xout += *xsp++ * 3;

		wd1=xout>>12;
                if(wd1>32767)   wd1=32767;
                else if(wd1<-32768)     wd1=-32768;

		*pcmout++ = wd1 ;
/*       fprintf (stdout, "%5d\n", xout1);  output pcm */

	j++, j++;

	k=-k;
	/*
	  if (xout1 >  16383)  xout1 =  16383 ;
	  if (xout1 < -16384)  xout1 = -16384 ;
	  if (xout1 >  16383)  xout1 =  16383 ;
	  if (xout1 < -16384)  xout1 = -16384 ;
	  */
	if(k<0)
	{	test->ilowr = (*pdecdata >> 2) & 63;
		test->ihigh = (*pdecdata++) & 3;
		goto nexttwodata;
	}
    }
//    P=(&(*pcmout))-P;
    return 1; //P/sizeof(short);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -