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

📄 exc_lbc.c

📁 语音编解码算法G.723.1的C语言算法原代码
💻 C
📖 第 1 页 / 共 5 页
字号:
   Word16   MaxAmp   ;
   Word32   Acc0,Acc1,Acc2 ;

   Word16   Imr[SubFrLen]  ;
   Word16   OccPos[SubFrLen] ;
   Word16   ImrCorr[SubFrLen] ;
   Word32   ErrBlk[SubFrLen] ;
   Word32   WrkBlk[SubFrLen] ;


   /* Update Impulse response */
   if ( Olp < (Word16) (SubFrLen-2) ) {
      Temp.UseTrn = (Word16) 1 ;
      Gen_Trn( Imr, ImpResp, Olp ) ;
      }
   else {
      Temp.UseTrn = (Word16) 0 ;
      for ( i = 0 ; i < SubFrLen ; i ++ )
         Imr[i] = ImpResp[i] ;
      }

   /* Scale Imr to avoid overflow */
   for ( i = 0 ; i < SubFrLen ; i ++ )
      OccPos[i] = shr( Imr[i], (Word16) 1 ) ;

   /* Compute Imr AutoCorr function */
   Acc0 = (Word32) 0 ;
   for ( i = 0 ; i < SubFrLen ; i ++ )
      Acc0 = L_mac( Acc0, OccPos[i], OccPos[i] ) ;

   Exp = norm_l( Acc0 ) ;
   Acc0 = L_shl( Acc0, Exp ) ;
   ImrCorr[0] = round( Acc0 ) ;

   /* Compute all the other */
   for ( i = 1 ; i < SubFrLen ; i ++ ) {
      Acc0 = (Word32) 0 ;
      for ( j = i ; j < SubFrLen ; j ++ )
         Acc0 = L_mac( Acc0, OccPos[j], OccPos[j-i] ) ;
      Acc0 = L_shl( Acc0, Exp ) ;
      ImrCorr[i] = round( Acc0 ) ;
      }

   /* Cross correlation with the signal */
   Exp = sub( Exp, 4 ) ;
   for ( i = 0 ; i < SubFrLen ; i ++ ) 
   {
      Acc0 = (Word32) 0 ;
      for ( j = i ; j < SubFrLen ; j ++ )
         Acc0 = L_mac( Acc0, Tv[j], Imr[j-i] ) ;
      ErrBlk[i] = L_shl( Acc0, Exp ) ;
   }

   /* Search for the best sequence */
   for ( k = 0 ; k < Sgrid ; k ++ )
   {

      Temp.GridId = (Word16) k ;

      /* Find maximum amplitude */
      Acc1 = (Word32) 0 ;
      for ( i = k ; i < SubFrLen ; i += Sgrid )
	  {
         Acc0 = L_abs( ErrBlk[i] ) ;
         if ( Acc0 >= Acc1 ) 
		 {
            Acc1 = Acc0 ;
            Temp.Ploc[0] = (Word16) i ;
		 }
	  }

      /* Quantize the maximum amplitude */
      Acc2 = Acc1 ;
      Acc1 = (Word32) 0x40000000L ;
      MaxAmpId = (Word16) (NumOfGainLev - MlqSteps) ;

      for ( i = MaxAmpId ; i >= MlqSteps ; i -- ) 
	  {
         Acc0 = L_mult( FcbkGainTable[i], ImrCorr[0] ) ;
         Acc0 = L_sub( Acc0, Acc2 ) ;
         Acc0 = L_abs( Acc0 ) ;
         if ( Acc0 < Acc1 ) 
		 {
            Acc1 = Acc0 ;
            MaxAmpId = (Word16) i ;
		 }
	  }
      MaxAmpId -- ;

      for ( i = 1 ; i <=2*MlqSteps ; i ++ ) 
	  {

         for ( j = k ; j < SubFrLen ; j += Sgrid ) 
		 {
            WrkBlk[j] = ErrBlk[j] ;
            OccPos[j] = (Word16) 0 ;
		 }
         Temp.MampId = MaxAmpId - (Word16) MlqSteps + (Word16) i ;

         MaxAmp = FcbkGainTable[Temp.MampId] ;

         if ( WrkBlk[Temp.Ploc[0]] >= (Word32) 0 )
            Temp.Pamp[0] = MaxAmp ;
         else
            Temp.Pamp[0] = negate(MaxAmp) ;

         OccPos[Temp.Ploc[0]] = (Word16) 1 ;

         for ( j = 1 ; j < Np ; j ++ )
		 {

            Acc1 = (Word32) 0xc0000000L ;

            for ( l = k ; l < SubFrLen ; l += Sgrid ) 
			{

               if ( OccPos[l] != (Word16) 0 )
                  continue ;

               Acc0 = WrkBlk[l] ;
               Acc0 = L_msu( Acc0, Temp.Pamp[j-1],ImrCorr[abs_s((Word16)(l-Temp.Ploc[j-1]))] ) ;
               WrkBlk[l] = Acc0 ;
               Acc0 = L_abs( Acc0 ) ;
               if ( Acc0 > Acc1 )
			   {
                  Acc1 = Acc0 ;
                  Temp.Ploc[j] = (Word16) l ;
			   }
			}

            if ( WrkBlk[Temp.Ploc[j]] >= (Word32) 0 )
               Temp.Pamp[j] = MaxAmp ;
            else
               Temp.Pamp[j] = negate(MaxAmp) ;

            OccPos[Temp.Ploc[j]] = (Word16) 1 ;
		 }

         /* Compute error vector */
         for ( j = 0 ; j < SubFrLen ; j ++ )
            OccPos[j] = (Word16) 0 ;

         for ( j = 0 ; j < Np ; j ++ )
            OccPos[Temp.Ploc[j]] = Temp.Pamp[j] ;

         for ( l = SubFrLen-1 ; l >= 0 ; l -- ) 
		 {
            Acc0 = (Word32) 0 ;
            for ( j = 0 ; j <= l ; j ++ )
               Acc0 = L_mac( Acc0, OccPos[j], Imr[l-j] ) ;
            Acc0 = L_shl( Acc0, (Word16) 2 ) ;
            OccPos[l] = extract_h( Acc0 ) ;
		 }

         /* Evaluate error */
         Acc1 = (Word32) 0 ;
         for ( j = 0 ; j < SubFrLen ; j ++ ) 
		 {
            Acc1 = L_mac( Acc1, Tv[j], OccPos[j] ) ;
            Acc0 = L_mult( OccPos[j], OccPos[j] ) ;
            Acc1 = L_sub( Acc1, L_shr( Acc0, (Word16) 1 ) ) ;
         }

         if ( Acc1 > (*Best).MaxErr ) 
		 {
            (*Best).MaxErr = Acc1 ;
            (*Best).GridId = Temp.GridId ;
            (*Best).MampId = Temp.MampId ;
            (*Best).UseTrn = Temp.UseTrn ;
            for ( j = 0 ; j < Np ; j ++ ) 
			{
               (*Best).Pamp[j] = Temp.Pamp[j] ;
               (*Best).Ploc[j] = Temp.Ploc[j] ;
			}
		 }
	  }
   }
    return;
}
/*
**
** Function:        Fcbk_Pack()
**
** Description:     Encoding of the pulse positions and gains for the high
**                  rate case.
**                  Combinatorial encoding is used to transmit the optimal
**                  combination of pulse locations.
**
** Links to text:   Section 2.15
**
** Arguments:
**
**  Word16 *Dpnt    Excitation vector
**  SFSDEF *Sfs     Encoded parameters of the excitation model
**  BESTDEF *Best   Parameters of the best excitation model
**  Word16 Np       Number of pulses (6 for even subframes; 5 for odd subframes)
**
** Outputs:
**
**  SFSDEF *Sfs     Encoded parameters of the excitation model
**
** Return value:    None
**
*/
void  Fcbk_Pack( Word16 *Dpnt, SFSDEF *Sfs, BESTDEF *Best, Word16 Np )
{
   int   i,j   ;


   /* Code the amplitudes and positions */
   j = MaxPulseNum - (int) Np ;

   (*Sfs).Pamp = (Word16) 0 ;
   (*Sfs).Ppos = (Word32) 0 ;

   for ( i = 0 ; i < SubFrLen/Sgrid ; i ++ ) 
   {

      if ( Dpnt[(int)(*Best).GridId + Sgrid*i] == (Word16) 0 )
         (*Sfs).Ppos = L_add( (*Sfs).Ppos, CombinatorialTable[j][i] ) ;
      else 
	  {
         (*Sfs).Pamp = shl( (*Sfs).Pamp, (Word16) 1 ) ;
         if ( Dpnt[(int)(*Best).GridId + Sgrid*i] < (Word16) 0 )
            (*Sfs).Pamp = add( (*Sfs).Pamp, (Word16) 1 ) ;

         j ++ ;
         /* Check for end */
         if ( j == MaxPulseNum )
            break ;
	  }
   }

   (*Sfs).Mamp = (*Best).MampId ;
   (*Sfs).Grid = (*Best).GridId ;
   (*Sfs).Tran = (*Best).UseTrn ;

   return;
}

/*
**
** Function:        Fcbk_Unpk()
**
** Description:     Decoding of the fixed codebook excitation for both rates.
**                  Gains, pulse positions, grid position (odd or even), signs
**                  are decoded and used to reconstruct the excitation.
**
** Links to text:   Section 2.17 & 3.5
**
** Arguments:
**
**  Word16 *Tv      Decoded excitation vector
**  SFSDEF Sfs      Encoded parameters of the excitation (for one subframe)
**  Word16 Olp      Closed loop adaptive pitch lag
**  Word16 Sfc      Subframe index
**
** Outputs:
**
**  Word16 *Tv      Decoded excitation vector
**
** Return value:    None
**
*/
void  Fcbk_Unpk( Word16 *Tv, SFSDEF Sfs, Word16 Olp, Word16 Sfc )
{
   int   i,j   ;

   Word32   Acc0  ;
   Word16   Np ;
   Word16 Tv_tmp[SubFrLen+4];
   Word16 acelp_gain, acelp_sign, acelp_shift, acelp_pos;
   Word16 offset, ipos, T0_acelp, gain_T0;



    switch(WrkRate) 
	{
        case Rate63: 
		{
			Np = Nb_puls[(int)Sfc] ;

            for ( i = 0 ; i < SubFrLen ; i ++ )
                Tv[i] = (Word16) 0 ;

            if ( Sfs.Ppos >= MaxPosTable[Sfc] )
                 return ;

            /* Decode the amplitudes and positions */
            j = MaxPulseNum - (int) Np ;

            Acc0 = Sfs.Ppos ;

            for ( i = 0 ; i < SubFrLen/Sgrid ; i ++ ) 
			{
				Acc0 = L_sub( Acc0, CombinatorialTable[j][i] ) ;

                if ( Acc0 < (Word32) 0 ) 
				{
					Acc0 = L_add( Acc0, CombinatorialTable[j][i] ) ;

                    j ++ ;

                    if ( (Sfs.Pamp & (1 << (MaxPulseNum-j) )) != (Word16) 0 )
                        Tv[(int)Sfs.Grid + Sgrid*i] = -FcbkGainTable[Sfs.Mamp] ;
					else
						Tv[(int)Sfs.Grid + Sgrid*i] =  FcbkGainTable[Sfs.Mamp] ;

                    if ( j == MaxPulseNum )
						break ;
				}
			}

            if ( Sfs.Tran == (Word16) 1 )
				Gen_Trn( Tv, Tv, Olp ) ;
            break;
		}

        case Rate53:
		{
			for ( i = 0 ; i < SubFrLen+4 ; i ++ )
				Tv_tmp[i] = (Word16) 0 ;
            /* decoding gain */
            acelp_gain = FcbkGainTable[Sfs.Mamp];
            /* decoding grid */
            acelp_shift = Sfs.Grid;
            /* decoding Sign */
            acelp_sign = Sfs.Pamp;
            /* decoding Pos */
            acelp_pos = (short) Sfs.Ppos;
            offset  = 0;
            for(i=0; i<4; i++) 
			{
				ipos = (acelp_pos & (Word16)0x0007) ;
                ipos = shl(ipos,3) + acelp_shift + offset;
                if( (acelp_sign & 1 )== 1)
				{
					Tv_tmp[ipos] = acelp_gain;
				}
                else 
				{
					Tv_tmp[ipos] = -acelp_gain;
				}
                offset = add(offset,2);
                acelp_pos = shr(acelp_pos, 3);
                acelp_sign = shr(acelp_sign,1);
			}
            for (i = 0; i < SubFrLen; i++)
				Tv[i] = Tv_tmp[i];
            T0_acelp = search_T0( (Word16) (Olp-1+Sfs.AcLg), Sfs.AcGn, &gain_T0);
            if(T0_acelp <SubFrLen-2) 
			{
				for (i = T0_acelp ; i < SubFrLen; i++)    /* code[i] += 0.8*code[i-Olp] */
					Tv[i] = add(Tv[i], mult(Tv[i-T0_acelp ], gain_T0));
			}
            break;
		}
    }
    return;
}

/*
**
** Function:        Find_Acbk()
**
** Description:     Computation of adaptive codebook contribution in
**                  closed-loop around the open-loop pitch lag (subframes 0 & 2)
**                  around the previous subframe closed-loop pitch lag
**                  (subframes 1 & 3).  For subframes 0 & 2, the pitch lag is
**                  encoded whereas for subframes 1 & 3, only the difference
**                  with the previous value is encoded (-1, 0, +1 or +2).
**                  The pitch predictor gains are quantized using one of the two
**                  codebooks (85 entries or 170 entries) depending on the
**                  rate and on the pitch lag value.
**                  Finally, the contribution of the pitch predictor is decoded
**                  and subtracted to obtain the residual signal.
**
** Links to text:   Section 2.14
**
** Arguments:
**
**  Word16 *Tv      Target vector
**  Word16 *ImpResp Impulse response of the combined filter
**  Word16 *PrevExc Previous excitation vector
**  LINEDEF *Line   Contains pitch related parameters (open/closed loop lag, gain)
**  Word16 Sfc      Subframe index
**
** Outputs:
**
**  Word16 *Tv     Residual vector
**  LINEDEF *Line  Contains pitch related parameters (closed loop lag, gain)
**
** Return value:    None
**
*/
void  Find_Acbk( Word16 *Tv, Word16 *ImpResp, Word16 *PrevExc, LINEDEF *Line, Word16 Sfc)
{
   int   i,j,k,l  ;

   Word32   Acc0,Acc1 ;

   Word16   RezBuf[SubFrLen+ClPitchOrd-1] ;
   Word16   FltBuf[ClPitchOrd][SubFrLen] ;
   Word32   CorBuf[4*(2*ClPitchOrd + ClPitchOrd*(ClPitchOrd-1)/2)] ;
   Word32   *lPnt ;

   Word16   CorVct[4*(2*ClPitchOrd + ClPitchOrd*(ClPitchOrd-1)/2)] ;
   Word16   *sPnt ;

   Word16   Olp ;
   Word16   Lid ;
   Word16   Gid ;
   Word16   Hb  ;
   Word16   Exp ;
   Word16   Bound[2] ;

   Word16   Lag1, Lag2;
   Word16   off_filt;

   /* Init constants */
   Olp = (*Line).Olp[shr(Sfc, (Word16) 1)] ;
   Lid = (Word16) Pstep ;
   Gid = (Word16) 0 ;
   Hb  = (Word16) 3 + (Sfc & (Word16) 1 ) ;

   /* For even frames only */
   if ( (Sfc & (Word16)1) == (Word16) 0 )
   {
      if ( Olp == (Word16) PitchMin )
         Olp = add( Olp, (Word16) 1 ) ;
      if ( Olp > (Word16) (PitchMax-5) )
         Olp = (Word16)(PitchMax-5) ;
   }

   lPnt = CorBuf ;
   for ( k = 0 ; k < (int) Hb ; k ++ ) 
   {

      /* Get residual from the excitation buffer */
      Get_Rez( RezBuf, PrevExc, (Word16) (Olp-Pstep+k) ) ;

      /* Filter the last one using the impulse response */
      for ( i = 0 ; i < SubFrLen ; i ++ ) 
	  {
         Acc0 = (Word32) 0 ;
         for ( j = 0 ; j <= i ; j ++ )
            Acc0 = L_mac( Acc0, RezBuf[ClPitchOrd-1+j], ImpResp[i-j] ) ;
         FltBuf[ClPitchOrd-1][i] = round( Acc0 ) ;
	  }

      /* Update all the others */
      for ( i = ClPitchOrd-2 ; i >= 0 ; i -- ) 
	  {
         FltBuf[i][0] = mult_r( RezBuf[i], (Word16) 0x2000 ) ;
         for ( j = 1 ; j < SubFrLen ; j ++ ) 
		 {
            Acc0 = L_deposit_h( FltBuf[i+1][j-1] ) ;
            Acc0 = L_mac( Acc0, RezBuf[i], ImpResp[j] ) ;
            FltBuf[i][j] = round( Acc0 ) ;
		 }
	  }

      /* Compute the cross with the signal */
      for ( i = 0 ; i < ClPitchOrd ; i ++ ) 
	  {
         Acc1 = (Word32) 0 ;
         for ( j = 0 ; j < SubFrLen ; j ++ ) 
		 {
            Acc0 = L_mult( Tv[j], FltBuf[i][j] ) ;
            Acc1 = L_add( Acc1, L_shr( Acc0, (Word16) 1 ) ) ;
		 }
         *lPnt ++ = L_shl( Acc1, (Word16) 1 ) ;
	  }

      /* Compute the energies */
      for ( i = 0 ; i < ClPitchOrd ; i ++ )
	  {
         Acc1 = (Word32) 0 ;
         for ( j = 0 ; j < SubFrLen ; j ++ )
            Acc1 = L_mac( Acc1, FltBuf[i][j], FltBuf[i][j] ) ;
         *lPnt ++ = Acc1 ;
	  }

      /* Compute the between crosses */
      for ( i = 1 ; i < ClPitchOrd ; i ++ )
         for ( j = 0 ; j < i ; j ++ )
		 {
            Acc1 = (Word32) 0 ;
            for ( l = 0 ; l < SubFrLen ; l ++ ) 
			{
               Acc0 = L_mult( FltBuf[i][l], FltBuf[j][l] ) ;
               Acc1 = L_add( Acc1, L_shr( Acc0, (Word16) 1 ) ) ;
			}
            *lPnt ++ = L_shl( Acc1, (Word16) 2 ) ;
		 }
   }

⌨️ 快捷键说明

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