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

📄 coder.c

📁 语音编解码算法G.723.1的C语言算法原代码
💻 C
字号:

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

#include "typedef.h"
#include "basop.h"
#include "cst_lbc.h"
#include "tab_lbc.h"
#include "coder.h"
#include "lpc.h"
#include "lsp.h"
#include "exc_lbc.h"
#include "util_lbc.h"

/*
   This file includes the coder main functions
*/

CODSTATDEF  CodStat  ;
/*
**
** Function:        Init_Coder()
**
** Description:     Initializes non-zero state variables
**          for the coder.
**
** Links to text:   Section 2.21
** 
** Arguments:       None
**
** Outputs:     None
**
** Return value:    None
**
*/
void  Init_Coder( void)
{
   int   i ;

   /* Initialize encoder data structure with zeros */
   memset(&CodStat, 0, sizeof(CODSTATDEF));

   /* Initialize the previously decoded LSP vector to the DC vector */
   for ( i = 0 ; i < LpcOrder ; i ++ )
      CodStat.PrevLsp[i] = LspDcTable[i] ;

    /* Initialize the taming procedure */
    for(i=0; i<SizErr; i++) CodStat.Err[i] = Err0;

    return;
}
/*
**
** Function:        Coder()
**
** Description:     Implements G.723 dual-rate coder for    a frame
**          of speech
**
** Links to text:   Section 2
**
** Arguments:
**
**  Word16 DataBuff[]   frame (480 bytes)
**

** Outputs:
**
**  Word16 Vout[]       Encoded frame (20/24 bytes)
**
** Return value:
**
**  Flag            Always True
**
*/
Flag  Coder( Word16 *DataBuff, char *Vout )
{
    int     i,j   ;

    /*
      Local variables
    */
   Word16   UnqLpc[SubFrames*LpcOrder] ;
   Word16   QntLpc[SubFrames*LpcOrder] ;
   Word16   PerLpc[2*SubFrames*LpcOrder] ;

   Word16   LspVect[LpcOrder] ;
   LINEDEF  Line  ;
   PWDEF    Pw[SubFrames]  ;

   Word16   ImpResp[SubFrLen] ;

   Word16  *Dpnt  ;
   Word16   VadBit = 1 ;

   /*
      Coder Start
   */
   Line.Crc = (Word16) 0 ;
   
   Rem_Dc( DataBuff ) ;

   /* Compute the Unquantized Lpc set for whole frame */
   Comp_Lpc( UnqLpc, CodStat.PrevDat, DataBuff) ;

   /* Convert to Lsp */
   AtoLsp( LspVect, &UnqLpc[LpcOrder*(SubFrames-1)], CodStat.PrevLsp ) ;

   /* VQ Lsp vector */
   Line.LspId = Lsp_Qnt( LspVect, CodStat.PrevLsp ) ;

   /* Inverse quantization of the LSP */
   Lsp_Inq( LspVect, CodStat.PrevLsp, Line.LspId, Line.Crc ) ;

   /* Interpolate the Lsp vectors */
   Lsp_Int( QntLpc, LspVect, CodStat.PrevLsp ) ;

   /* Copy the LSP vector for the next frame */
   for ( i = 0 ; i < LpcOrder ; i ++ )
      CodStat.PrevLsp[i] = LspVect[i] ;

   Mem_Shift( CodStat.PrevDat, DataBuff ) ;

   /* Compute Perceptual filter Lpc coefficients */
   Wght_Lpc( PerLpc, UnqLpc ) ;

   /* Apply the perceptual weighting filter */
   Error_Wght( DataBuff, PerLpc ) ;

   /*
   / Compute Open loop pitch estimates
   */
   Dpnt = (Word16 *) malloc( sizeof(Word16)*(PitchMax+Frame) ) ;

   /* Construct the buffer */
   for ( i = 0 ; i < PitchMax ; i ++ )
      Dpnt[i] = CodStat.PrevWgt[i] ;
   for ( i = 0 ; i < Frame ; i ++ )
      Dpnt[PitchMax+i] = DataBuff[i] ;

   Vec_Norm( Dpnt, (Word16) (PitchMax+Frame) ) ;

   j = PitchMax ;
   for ( i = 0 ; i < SubFrames/2 ; i ++ ) 
   {
      Line.Olp[i] = Estim_Pitch( Dpnt, (Word16) j ) ;
      j += 2*SubFrLen ;
   }

   /* Compute the Hmw */
   j = PitchMax ;
   for ( i = 0 ; i < SubFrames ; i ++ ) 
   {
      Pw[i] = Comp_Pw( Dpnt, (Word16) j, Line.Olp[i>>1] ) ;
      j += SubFrLen ;
   }

   /* Reload the buffer */
   for ( i = 0 ; i < PitchMax ; i ++ )
      Dpnt[i] = CodStat.PrevWgt[i] ;
   for ( i = 0 ; i < Frame ; i ++ )
      Dpnt[PitchMax+i] = DataBuff[i] ;

   /* Save PrevWgt */
   for ( i = 0 ; i < PitchMax ; i ++ )
      CodStat.PrevWgt[i] = Dpnt[Frame+i] ;

   /* Apply the Harmonic filter */
   j = 0 ;
   for ( i = 0 ; i < SubFrames ; i ++ ) {
      Filt_Pw( DataBuff, Dpnt, (Word16) j , Pw[i] ) ;
      j += SubFrLen ;
      }

   free ( (void *) Dpnt ) ;

   /*
   // Start the sub frame processing loop
   */
   Dpnt = DataBuff ;

   for ( i = 0 ; i < SubFrames ; i ++ )
   {

      /* Compute full impulse response */
      Comp_Ir( ImpResp, &QntLpc[i*LpcOrder], &PerLpc[i*2*LpcOrder], Pw[i] ) ;

      /* Subtract the ringing of previous sub-frame */
      Sub_Ring( Dpnt, &QntLpc[i*LpcOrder], &PerLpc[i*2*LpcOrder],CodStat.PrevErr, Pw[i] ) ;

      /* Compute adaptive code book contribution */
      Find_Acbk( Dpnt, ImpResp, CodStat.PrevExc, &Line, (Word16) i) ;

      /* Compute fixed code book contribution */
      Find_Fcbk( Dpnt, ImpResp, &Line, (Word16) i ) ;

      /* Reconstruct the excitation */
      Decod_Acbk( ImpResp, CodStat.PrevExc, Line.Olp[i>>1],Line.Sfs[i].AcLg, Line.Sfs[i].AcGn ) ;

      for ( j = SubFrLen ; j < PitchMax ; j ++ )
         CodStat.PrevExc[j-SubFrLen] = CodStat.PrevExc[j] ;

      for ( j = 0 ; j < SubFrLen ; j ++ )
	  {
         Dpnt[j] = shl( Dpnt[j], (Word16) 1 ) ;
         Dpnt[j] = add( Dpnt[j], ImpResp[j] ) ;
         CodStat.PrevExc[PitchMax-SubFrLen+j] = Dpnt[j] ;
	  }

      /* Update exc_err */
      Update_Err(Line.Olp[i>>1], Line.Sfs[i].AcLg, Line.Sfs[i].AcGn);


      /* Update the ringing delays */
      Upd_Ring( Dpnt, &QntLpc[i*LpcOrder], &PerLpc[i*2*LpcOrder],CodStat.PrevErr ) ;

      Dpnt += SubFrLen ;
   }

    /* Pack the Line structure */
    Line_Pack( &Line, Vout, VadBit ) ;

    return (Flag) True ;
}

⌨️ 快捷键说明

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