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

📄 codld8cp.c

📁 ITU-T在1996年3月公布了G.729建议的8Kbit/s共轭结构代数码激励线性预测(CS-ACELP)语音编码方案
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
 ITU-T G.729 Annex I  - Reference C code for fixed point
                         implementation of G.729 Annex I
                         Version 1.1 of October 1999
*/

/*
 File : CODLD8CP.C
 */

/* from cod_ld8e.c G.729 Annex E Version 1.2  Last modified: May 1998 */
/* from codld8kd.c G.729 Annex D Version 1.2  Last modified: May 1998 */
/* from cod_ld8k.c G.729 Annex B Version 1.3  Last modified: August 1997 */
/* from cod_ld8k.c G.729 Version 3.3 */

/*-----------------------------------------------------------------*
 *   Functions Coder_ld8c and Init_Coder_ld8c                      *
 *             ~~~~~~~~~~     ~~~~~~~~~~~~~~~                      *
 *-----------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>

#include "typedef.h"
#include "basic_op.h"
#include "ld8k.h"
#include "ld8cp.h"
#include "tab_ld8k.h"
#include "oper_32b.h"
#include "vad.h"
#include "dtx.h"
#include "sid.h"

/*-----------------------------------------------------------*
 *    Coder constant parameters (defined in "ld8k.h")        *
 *-----------------------------------------------------------*
 *   L_WINDOW    : LPC analysis window size.                 *
 *   L_NEXT      : Samples of next frame needed for autocor. *
 *   L_FRAME     : Frame size.                               *
 *   L_SUBFR     : Sub-frame size.                           *
 *   M           : LPC order.                                *
 *   MP1         : LPC order+1                               *
 *   L_TOTAL     : Total size of speech buffer.              *
 *   PIT_MIN     : Minimum pitch lag.                        *
 *   PIT_MAX     : Maximum pitch lag.                        *
 *   L_INTERPOL  : Length of filter for interpolation        *
 *-----------------------------------------------------------*/

/*--------------------------------------------------------*
 *         Static memory allocation.                      *
 *--------------------------------------------------------*/


   /* Speech vector */

    static Word16 old_speech[L_TOTAL];
    static Word16 *speech, *p_window;
    Word16 *new_speech;                    /* Global variable */

   /* Weighted speech vector */

    static Word16 old_wsp[L_FRAME+PIT_MAX];
    static Word16 *wsp;

    /* Excitation vector */
    static Word16 old_exc[L_FRAME+PIT_MAX+L_INTERPOL];
    static Word16 *exc;

    /* Zero vector */
    static Word16 ai_zero[L_SUBFR+M_BWDP1];
    static Word16 *zero;

    /* Lsp (Line spectral pairs) */
    static Word16 lsp_old[M]={
          30000, 26000, 21000, 15000, 8000, 0, -8000,-15000,-21000,-26000};
    static Word16 lsp_old_q[M];

    /* Filter's memory */

    static Word16 mem_syn[M_BWD], mem_w0[M_BWD], mem_w[M_BWD];
    static Word16 mem_err[M_BWD+L_SUBFR], *error;
    static Word16 sharp;

    /* to tame the coder */
    static Word32 L_exc_err[4];

    /* For G.729B */
    /* DTX variables */
    static Word16 pastVad;
    static Word16 ppastVad;
    static Word16 seed;

    /* for the backward analysis */
    static Word16    prev_filter[M_BWDP1]; /* Previous selected filter */

    static Word32  rexp[M_BWDP1];
    static Word16 synth[L_ANA_BWD];
    static Word16 *synth_ptr;
    static Word16 prev_lp_mode ;
    static Word16 gamma1[2], gamma2[2];       /* Weighting factor for the 2 subframes */
    static Word16 A_t_bwd_mem[M_BWDP1];
    static Word16 bwd_dominant, C_int;              /* See file bwfw.c */
    static Word16   glob_stat;  /* Mesure of global stationnarity Q8 */
    static Word16   stat_bwd;       /* Nbre of consecutive backward frames */
    static Word16   val_stat_bwd;   /* Value associated with stat_bwd */

    /* Last backward A(z) for case of unstable filter */
    static Word16 old_A_bwd[M_BWDP1];
    static Word16 old_rc_bwd[2];
    /* Last forkward A(z) for case of unstable filter */
    static Word16 old_A_fwd[MP1];
    static Word16 old_rc_fwd[2];
    static Word16 freq_prev[MA_NP][M];    /* Q13:previous LSP vector       */

    static Word16 lag_buf[5]={20,20, 20, 20,20};
    static Word16 pgain_buf[5]={11469,11469,11469,11469,11469};
/*-----------------------------------------------------------------*
 *   Function  Init_Coder_ld8c                                     *
 *            ~~~~~~~~~~~~~~~                                      *
 *                                                                 *
 *  Init_Coder_ld8c(void);                                         *
 *                                                                 *
 *   ->Initialization of variables for the coder section.          *
 *       - initialize pointers to speech buffer                    *
 *       - initialize static  pointers                             *
 *       - set static vectors to zero                              *
 *                                                                 *
 *-----------------------------------------------------------------*/

void Init_Coder_ld8c(Word16 dtx_enable)
{

    Word16 i;
  /*----------------------------------------------------------------------*
  *      Initialize pointers to speech vector.                            *
  *                                                                       *
  *                                                                       *
  *   |--------------------|-------------|-------------|------------|     *
  *     previous speech           sf1           sf2         L_NEXT        *
  *                                                                       *
  *   <----------------  Total speech vector (L_TOTAL)   ----------->     *
  *   <----------------  LPC analysis window (L_WINDOW)  ----------->     *
  *   |                   <-- present frame (L_FRAME) -->                 *
  * old_speech            |              <-- new speech (L_FRAME) -->     *
  * p_window              |              |                                *
  *                     speech           |                                *
  *                             new_speech                                *
  *-----------------------------------------------------------------------*/

    new_speech = old_speech + L_TOTAL - L_FRAME;         /* New speech     */
    speech     = new_speech - L_NEXT;                    /* Present frame  */
    p_window   = old_speech + L_TOTAL - L_WINDOW;        /* For LPC window */

    /* Initialize static pointers */
    wsp    = old_wsp + PIT_MAX;
    exc    = old_exc + PIT_MAX + L_INTERPOL;
    zero   = ai_zero + M_BWDP1;
    error  = mem_err + M_BWD;

    /* Static vectors to zero */
    Set_zero(old_speech, L_TOTAL);
    Set_zero(old_exc, PIT_MAX+L_INTERPOL);
    Set_zero(old_wsp, PIT_MAX);
    Set_zero(mem_syn, M_BWD);
    Set_zero(mem_w,   M_BWD);
    Set_zero(mem_w0,  M_BWD);
    Set_zero(mem_err, M_BWD);
    Set_zero(zero, L_SUBFR);
    sharp = SHARPMIN;

    /* Initialize lsp_old_q[] */
    Copy(lsp_old, lsp_old_q, M);

    Lsp_encw_resete(freq_prev);

    for(i=0; i<4; i++) L_exc_err[i] = 0x00004000L;   /* Q14 */

    /* for the backward analysis */
    Set_zero(synth, L_ANA_BWD); /*Q14 */
    synth_ptr = synth + MEM_SYN_BWD;
    prev_lp_mode = 0;
    bwd_dominant = 0;              /* See file bwfw.c */
    C_int = 4506;       /* Filter interpolation parameter */
    glob_stat = 10000;  /* Mesure of global stationnarity Q8 */
    stat_bwd = 0;       /* Nbre of consecutive backward frames */
    val_stat_bwd = 0;   /* Value associated with stat_bwd */

    for(i=0; i<M_BWDP1; i++) rexp[i] = 0L;

    A_t_bwd_mem[0] = 4096;
    for (i=1; i<M_BWDP1; i++) A_t_bwd_mem[i] = 0;
    Set_zero(prev_filter, M_BWDP1);
    prev_filter[0] = 4096;

    Set_zero(old_A_bwd, M_BWDP1);
    old_A_bwd[0]=4096;
    Set_zero(old_rc_bwd, 2);

    Set_zero(old_A_fwd, MP1);
    old_A_fwd[0]=4096;
    Set_zero(old_rc_fwd, 2);

    /* For G.729B */
    /* Initialize VAD/DTX parameters */
    if(dtx_enable == 1) {
        pastVad = 1;
        ppastVad = 1;
        seed = INIT_SEED;
        vad_init();
        Init_lsfq_noise();
    }
    
    return;
}

/*-----------------------------------------------------------------*
 *   Functions Coder_ld8c                                          *
 *            ~~~~~~~~~~                                           *
 *  Coder_ld8c(Word16 ana[], Word16 rate);                         *
 *                                                                 *
 *   ->Main coder function.                                        *
 *                                                                 *
 *                                                                 *
 *  Input:                                                         *
 *                                                                 *
 *   80 speech data should have been copied to vector new_speech[].*
 *   This vector is global and is declared in this function.       *
  *   rate :       rate for the current frame                      *
 *                                                                 *
 *  Ouputs:                                                        *
 *                                                                 *
 *    ana[]      ->analysis parameters.                            *
 *                                                                 *
 *-----------------------------------------------------------------*/

void Coder_ld8c(
  Word16 ana[],     /* (o)     : analysis parameters                        */
 Word16 frame,                   /* input : frame counter */
  Word16 dtx_enable,               /* input : DTX enable flag */
  Word16 rate           /* input   : rate selector/frame  =0 6.4kbps , =1 8kbps,= 2 11.8 kbps*/
)
{

  /* LPC analysis */
    Word16 r_l_fwd[NP+1], r_h_fwd[NP+1];    /* Autocorrelations low and hi (forward) */
    Word32 r_bwd[M_BWDP1];      /* Autocorrelations (backward) */
    Word16 r_l_bwd[M_BWDP1];      /* Autocorrelations low (backward) */
    Word16 r_h_bwd[M_BWDP1];      /* Autocorrelations high (backward) */
    Word16 rc_fwd[M];                 /* Reflection coefficients : forward analysis */
    Word16 rc_bwd[M_BWD];         /* Reflection coefficients : backward analysis */
    Word16 A_t_fwd[MP1*2];          /* A(z) forward unquantized for the 2 subframes */
    Word16 A_t_fwd_q[MP1*2];      /* A(z) forward quantized for the 2 subframes */
    Word16 A_t_bwd[2*M_BWDP1];    /* A(z) backward for the 2 subframes */
    Word16 *Aq;           /* A(z) "quantized" for the 2 subframes */
    Word16 *Ap;           /* A(z) "unquantized" for the 2 subframes */
    Word16 *pAp, *pAq;
    Word16 Ap1[M_BWDP1];          /* A(z) with spectral expansion         */
    Word16 Ap2[M_BWDP1];          /* A(z) with spectral expansion         */
    Word16 lsp_new[M], lsp_new_q[M]; /* LSPs at 2th subframe                 */
    Word16 lsf_int[M];               /* Interpolated LSF 1st subframe.       */
    Word16 lsf_new[M];
    Word16 lp_mode;                  /* Backward / Forward Indication mode */
    Word16 m_ap, m_aq, i_gamma;
    Word16 code_lsp[2];

    /* Other vectors */

    Word16 h1[L_SUBFR];            /* Impulse response h1[]              */
    Word16 xn[L_SUBFR];            /* Target vector for pitch search     */
    Word16 xn2[L_SUBFR];           /* Target vector for codebook search  */
    Word16 code[L_SUBFR];          /* Fixed codebook excitation          */
    Word16 y1[L_SUBFR];            /* Filtered adaptive excitation       */
    Word16 y2[L_SUBFR];            /* Filtered fixed codebook excitation */
    Word16 g_coeff[4];             /* Correlations between xn & y1       */
    Word16 res2[L_SUBFR];          /* residual after long term prediction*/
    Word16 g_coeff_cs[5];
    Word16 exp_g_coeff_cs[5];      /* Correlations between xn, y1, & y2
                                     <y1,y1>, -2<xn,y1>,
                                          <y2,y2>, -2<xn,y2>, 2<y1,y2> */
    /* Scalars */
    Word16 i, j, k, i_subfr;
    Word16 T_op, T0, T0_min, T0_max, T0_frac;
    Word16 gain_pit, gain_code, index;
    Word16 taming, pit_sharp;
    Word16 sat_filter;
    Word32 L_temp;
    Word16 freq_cur[M];

    /* For G.729B */
    Word16 rh_nbe[MP1];             
    Word16 lsfq_mem[MA_NP][M];
    Word16 exp_R0, Vad;
    Word16 tmp1, tmp2,avg_lag;
    
    Word16 temp, Energy_db;
    
/*------------------------------------------------------------------------*
 *  - Perform LPC analysis:                                               *
 *       * autocorrelation + lag windowing                                *
 *       * Levinson-durbin algorithm to find a[]                          *
 *       * convert a[] to lsp[]                                           *
 *       * quantize and code the LSPs                                     *
 *       * find the interpolated LSPs and convert to a[] for the 2        *
 *         subframes (both quantized and unquantized)                     *
 *------------------------------------------------------------------------*/
    /* ------------------- */
    /* LP Forward analysis */
    /* ------------------- */

⌨️ 快捷键说明

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