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

📄 util_lbc.c

📁 语音编解码算法G.723.1的C语言算法原代码
💻 C
📖 第 1 页 / 共 2 页
字号:

#include <stdlib.h>
#include <stdio.h>

#include "typedef.h"
#include "basop.h"
#include "cst_lbc.h"
#include "tab_lbc.h"
#include "lbccodec.h"
#include "coder.h"
#include "decod.h"
#include "util_lbc.h"




/*
**
** Function:        Rem_Dc()
**
** Description:     High-pass filtering
**
** Links to text:   Section 2.3
**
** Arguments:
**
**  Word16 *Dpnt
**
** Inputs:
**
**  CodStat.HpfZdl  FIR filter memory from previous frame (1 word)
**  CodStat.HpfPdl  IIR filter memory from previous frame (1 word)
**
** Outputs:
**
**  Word16 *Dpnt
**
** Return value:    None
**
*/
void  Rem_Dc( Word16 *Dpnt )
{
   int   i  ;


   Word32   Acc0,Acc1 ;

   if ( UseHp )
      for ( i = 0 ; i < Frame ; i ++ )
	  {

         /* Do the Fir and scale by 2 */
         Acc0 = L_mult( Dpnt[i], (Word16) 0x4000 ) ;
         Acc0 = L_mac ( Acc0, CodStat.HpfZdl, (Word16) 0xc000 ) ;
         CodStat.HpfZdl = Dpnt[i] ;

         /* Do the Iir part */
         Acc1 = L_mls( CodStat.HpfPdl, (Word16) 0x7f00 ) ;		 
         Acc0 = L_add( Acc0, Acc1 ) ;
         CodStat.HpfPdl = Acc0 ;
		 //printf("i=%d:Acc0=%ld,Acc1=%ld ",i,Acc0,Acc1);
         Dpnt[i] = round(Acc0) ;
		 
	  }
   else
      for ( i = 0 ; i < Frame ; i ++ )
         Dpnt[i] = shr( Dpnt[i], (Word16) 1 ) ;

   return;
}

/*
**
** Function:        Vec_Norm()
**
** Description:     Vector normalization
**
** Links to text:
**
** Arguments:
**
**  Word16 *Vect
**  Word16 Len
**
** Outputs:
**
**  Word16 *Vect
**
** Return value:  The power of 2 by which the data vector multiplyed.
**
*/
Word16  Vec_Norm( Word16 *Vect, Word16 Len )
{
    int   i  ;

    Word16  Acc0,Acc1   ;
    Word16  Exp   ;
    Word16  Rez ;
    Word32  Temp  ;

   static   short ShiftTable[16] = {
      0x0001 ,
      0x0002 ,
      0x0004 ,
      0x0008 ,
      0x0010 ,
      0x0020 ,
      0x0040 ,
      0x0080 ,
      0x0100 ,
      0x0200 ,
      0x0400 ,
      0x0800 ,
      0x1000 ,
      0x2000 ,
      0x4000 ,
      0x7fff
   } ;

   /* Find absolute maximum */
   Acc1 = (Word16) 0 ;
   for ( i = 0 ; i < Len ; i ++ ) {
      Acc0 = abs_s( Vect[i] ) ;
      if ( Acc0 > Acc1 )
         Acc1 = Acc0 ;
      }

   /* Get the shift count */
   Rez = norm_s( Acc1 ) ;
   Exp = ShiftTable[Rez] ;

   /* Normalize all the vector */
   for ( i = 0 ; i < Len ; i ++ ) {
      Temp = L_mult( Exp, Vect[i] ) ;
      Temp = L_shr( Temp, 4 ) ;
      Vect[i] = extract_l( Temp ) ;
      }

    Rez = sub( Rez, (Word16) 3) ;
    return Rez ;
}
/*
**
** Function:        Mem_Shift()
**
** Description:     Memory shift, update of the high-passed input speech signal
**
** Links to text:
**
** Arguments:
**
**  Word16 *PrevDat
**  Word16 *DataBuff
**
** Outputs:
**
**  Word16 *PrevDat
**  Word16 *DataBuff
**
** Return value:    None
**
*/
void  Mem_Shift( Word16 *PrevDat, Word16 *DataBuff )
{
   int   i  ;

   Word16   Dpnt[Frame+LpcFrame-SubFrLen] ;

   /*
      Form Buffer
   */
   for ( i = 0 ; i < LpcFrame-SubFrLen ; i ++ )
      Dpnt[i] = PrevDat[i] ;
   for ( i = 0 ; i < Frame ; i ++ )
      Dpnt[i+LpcFrame-SubFrLen] = DataBuff[i] ;

   /* Update PrevDat */
   for ( i = 0 ; i < LpcFrame-SubFrLen ; i ++ )
      PrevDat[i] = Dpnt[Frame+i] ;

   /* Update DataBuff */
   for ( i = 0 ; i < Frame ; i ++ )
      DataBuff[i] = Dpnt[(LpcFrame-SubFrLen)/2+i] ;

   return;
}

/*
**
** Function:        Line_Pack()
**
** Description:     Packing coded parameters in bitstream of 16-bit words
**
** Links to text:   Section 4
**
** Arguments:
**
**  LINEDEF *Line     Coded parameters for a frame
**  char    *Vout     bitstream chars
**  Word16   VadBit   Voice Activity Indicator
**
** Outputs:
**
**  Word16 *Vout
**
** Return value:    None
**
*/
void    Line_Pack( LINEDEF *Line, char *Vout, Word16 VadBit )
{
    int     i ;
    int     BitCount ;

    Word16  BitStream[192] ;
    Word16 *Bsp = BitStream ;
    Word32  Temp ;

    /* Clear the output vector */
    for ( i = 0 ; i < 24 ; i ++ )
        Vout[i] = 0 ;

    /* 
    Add the coder rate info and the VAD status to the 2 msb
        of the first word of the frame.

    The signaling is as follows:
        00  :   High Rate
        10  :   Low Rate
        01  :   Non Speech
        11  :   Reserved for future use
    */

    if ( VadBit == 1 ) {
        if ( WrkRate == Rate63 )
            Temp = 0x00000000L ;
        else
            Temp = 0x00000001L ;
    }
    else {
        /*printf("Version 4.1 : Incorrect value of VadBit\n");*/
        exit(-1);
    }

    /* Serialize Control info */
    Bsp = Par2Ser( Temp, Bsp, 2 ) ;

    /* 24 bit LspId */
    Temp = (*Line).LspId ;
    Bsp = Par2Ser( Temp, Bsp, 24 ) ;

    /* Check for Speech/NonSpeech case */
    if ( VadBit == 1 )
	{

        /*
            Do the part common to both rates
        */

        /* Adaptive code book lags */
        Temp = (Word32) (*Line).Olp[0] - (Word32) PitchMin ;
        Bsp = Par2Ser( Temp, Bsp, 7 ) ;

        Temp = (Word32) (*Line).Sfs[1].AcLg ;
        Bsp = Par2Ser( Temp, Bsp, 2 ) ;

        Temp = (Word32) (*Line).Olp[1] - (Word32) PitchMin ;
        Bsp = Par2Ser( Temp, Bsp, 7 ) ;

        Temp = (Word32) (*Line).Sfs[3].AcLg ;
        Bsp = Par2Ser( Temp, Bsp, 2 ) ;

        /* Write combined 12 bit index of all the gains */
        for ( i = 0 ; i < SubFrames ; i ++ )
		{
            Temp = (*Line).Sfs[i].AcGn*NumOfGainLev + (*Line).Sfs[i].Mamp ;
            /*printf("TEMP=%12d\n",(*Line).Sfs[i].AcGn);*/
			if ( WrkRate == Rate63 )
                Temp += (Word32) (*Line).Sfs[i].Tran << 11 ;
            Bsp = Par2Ser( Temp, Bsp, 12 ) ;
        }
		/*getchar();*/

        /* Write all the Grid indices */
        for ( i = 0 ; i < SubFrames ; i ++ )
            *Bsp ++ = (*Line).Sfs[i].Grid ;

        /* High rate only part */
        if ( WrkRate == Rate63 ) 
		{

            /* Write the reserved bit as 0 */
            *Bsp ++ = (Word16) 0 ;

            /* Write 13 bit combined position index */
            Temp = (*Line).Sfs[0].Ppos >> 16 ;
            Temp = Temp * 9 + ( (*Line).Sfs[1].Ppos >> 14) ;
            Temp *= 90 ;
            Temp += ((*Line).Sfs[2].Ppos >> 16) * 9 + ( (*Line).Sfs[3].Ppos >> 14 ) ;
            Bsp = Par2Ser( Temp, Bsp, 13 ) ;

            /* Write all the pulse positions */
            Temp = (*Line).Sfs[0].Ppos & 0x0000ffffL ;
            Bsp = Par2Ser( Temp, Bsp, 16 ) ;

            Temp = (*Line).Sfs[1].Ppos & 0x00003fffL ;
            Bsp = Par2Ser( Temp, Bsp, 14 ) ;

            Temp = (*Line).Sfs[2].Ppos & 0x0000ffffL ;
            Bsp = Par2Ser( Temp, Bsp, 16 ) ;

            Temp = (*Line).Sfs[3].Ppos & 0x00003fffL ;
            Bsp = Par2Ser( Temp, Bsp, 14 ) ;

            /* Write pulse amplitudes */
            Temp = (Word32) (*Line).Sfs[0].Pamp ;
            Bsp = Par2Ser( Temp, Bsp, 6 ) ;

            Temp = (Word32) (*Line).Sfs[1].Pamp ;
            Bsp = Par2Ser( Temp, Bsp, 5 ) ;

            Temp = (Word32) (*Line).Sfs[2].Pamp ;
            Bsp = Par2Ser( Temp, Bsp, 6 ) ;

            Temp = (Word32) (*Line).Sfs[3].Pamp ;
            Bsp = Par2Ser( Temp, Bsp, 5 ) ;
        }
        /* Low rate only part */
        else 
		{

            /* Write 12 bits of positions */
            for ( i = 0 ; i < SubFrames ; i ++ ) 
			{
                Temp = (*Line).Sfs[i].Ppos ;
                Bsp = Par2Ser( Temp, Bsp, 12 ) ;
            }

            /* Write 4 bit Pamps */
            for ( i = 0 ; i < SubFrames ; i ++ ) 
			{
                Temp = (*Line).Sfs[i].Pamp ;
                Bsp = Par2Ser( Temp, Bsp, 4 ) ;
            }
        }

    }
    else 
	{
        /* Do Sid frame gain */

    }

    /* Write out voiced frames */
    if ( WrkRate == Rate63 )
        BitCount = 192 ;
    else
        BitCount = 160 ;

    if ( VadBit != 1 )
        BitCount = 32 ;

    for ( i = 0 ; i < BitCount ; i ++ )
        Vout[i>>3] ^= BitStream[i] << (i & 0x0007) ;
}

Word16* Par2Ser( Word32 Inp, Word16 *Pnt, int BitNum )
{
    int i   ;
    Word16  Temp ;

    for ( i = 0 ; i < BitNum ; i ++ )
	{
        Temp = (Word16) Inp & (Word16)0x0001 ;
        Inp >>= 1 ;
        *Pnt ++ = Temp ;
    }

    return Pnt ;
}   

/*

⌨️ 快捷键说明

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