www.pudn.com > p_voice.rar > CODER2.C


/* 
** 
** File:        "coder2.c" 
** 
** Description:     Top-level source code for G.723.1 dual-rate coder 
** 
** Functions:       Init_Coder() 
**                  Coder() 
** 
** 
*/ 
 
/* 
    ITU-T G.723.1 Floating Point Speech Coder ANSI C Source Code.  Version 5.1F 
 
    Original fixed-point code copyright (c) 1995, 
    AudioCodes, DSP Group, France Telecom, Universite de Sherbrooke. 
    All rights reserved. 
 
    Floating-point code copyright (c) 1995, 
    Intel Corporation and France Telecom (CNET). 
    All rights reserved. 
*/ 
 
 
#include  
#include  
#include  
 
#include "typedef2.h" 
#include "cst2.h" 
#include "tab2.h" 
#include "coder2.h" 
#include "lpc2.h" 
#include "lsp2.h" 
#include "exc2.h" 
#include "util2.h" 
#include "vad2.h" 
#include "codcng2.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 Ftyp = 0 (untransmitted) or 2 (SID) */ 
        Cod_Cng(DataBuff, &Ftyp, &Line, QntLpc); 
 
        /* Update the ringing delays */ 
        Dpnt = DataBuff; 
        for ( i = 0 ; i < SubFrames; i++ ) { 
 
            /* Update exc_err */ 
            Update_Err(Line.Olp[i>>1], Line.Sfs[i].AcLg, Line.Sfs[i].AcGn); 
 
            Upd_Ring( Dpnt, &QntLpc[i*LpcOrder], &PerLpc[i*2*LpcOrder], 
                                                        CodStat.PrevErr); 
            Dpnt += SubFrLen; 
        } 
    } 
    else { 
 
        /* Compute the Hmw */ 
        j = PitchMax; 
        for (i=0; i < SubFrames; i++) { 
            Pw[i] = Comp_Pw(Dpnt, 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, j , Pw[i]); 
            j += SubFrLen; 
        } 
 
        free((char *) Dpnt); 
 
        /* 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]; 
 
        /*  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]); 
 
            /* Subtruct the ringing of previos 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, i); 
 
            /* Compute fixed code book contribution */ 
            Find_Fcbk(Dpnt, ImpResp, &Line, 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] = Dpnt[j] + ImpResp[j]; 
                CodStat.PrevExc[PitchMax-SubFrLen+j] = Dpnt[j]; 
 
                /* Clip the new samples */ 
                if (CodStat.PrevExc[PitchMax-SubFrLen+j] < (FLOAT)-32767.5) 
                    CodStat.PrevExc[PitchMax-SubFrLen+j] = (FLOAT)-32768.0; 
                else if (CodStat.PrevExc[PitchMax-SubFrLen+j] > (FLOAT)32766.5) 
                    CodStat.PrevExc[PitchMax-SubFrLen+j] = (FLOAT)32767.0; 
            } 
 
            /* 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; 
        }  /* end of subframes loop */ 
 
        /* 
        // Save Vad information and reset CNG random generator 
        */ 
        CodCng.PastFtyp = 1; 
        CodCng.RandSeed = 12345; 
 
    } /* End of active frame case */ 
 
    /* Pack the Line structure */ 
    Line_Pack(&Line, Vout, Ftyp); 
 
    return (Flag) True; 
}