cod_ld8a.c
上传用户:zhouyunkk
上传日期:2013-01-10
资源大小:59k
文件大小:18k
源码类别:

语音压缩

开发平台:

C/C++

  1. /*
  2.    ITU-T G.729 Annex C - Reference C code for floating point
  3.                          implementation of G.729 Annex A
  4.                          Version 1.01 of 15.September.98
  5. */
  6. /*
  7. ----------------------------------------------------------------------
  8.                     COPYRIGHT NOTICE
  9. ----------------------------------------------------------------------
  10.    ITU-T G.729 Annex C ANSI C source code
  11.    Copyright (C) 1998, AT&T, France Telecom, NTT, University of
  12.    Sherbrooke.  All rights reserved.
  13. ----------------------------------------------------------------------
  14. */
  15. /*
  16.  File : COD_LD8A.C
  17.  Used for the floating point version of G.729A only
  18.  (not for G.729 main body)
  19. */
  20. /*-----------------------------------------------------------------*
  21.  *   Functions coder_ld8a and init_coder_ld8a                      *
  22.  *             ~~~~~~~~~~     ~~~~~~~~~~~~~~~                      *
  23.  *                                                                 *
  24.  *  init_coder_ld8a(void);                                         *
  25.  *                                                                 *
  26.  *   ->Initialization of variables for the coder section.          *
  27.  *                                                                 *
  28.  *                                                                 *
  29.  *  coder_ld8a(Short ana[]);                                       *
  30.  *                                                                 *
  31.  *   ->Main coder function.                                        *
  32.  *                                                                 *
  33.  *                                                                 *
  34.  *  Input:                                                         *
  35.  *                                                                 *
  36.  *    80 speech data should have beee copy to vector new_speech[]. *
  37.  *    This vector is global and is declared in this function.      *
  38.  *                                                                 *
  39.  *  Ouputs:                                                        *
  40.  *                                                                 *
  41.  *    ana[]      ->analysis parameters.                            *
  42.  *                                                                 *
  43.  *-----------------------------------------------------------------*/
  44. #include <math.h>
  45. #include "typedef.h"
  46. #include "ld8a.h"
  47. /*-----------------------------------------------------------*
  48.  *    Coder constant parameters (defined in "ld8a.h")        *
  49.  *-----------------------------------------------------------*
  50.  *   L_WINDOW    : LPC analysis window size.                 *
  51.  *   L_NEXT      : Samples of next frame needed for autocor. *
  52.  *   L_FRAME     : Frame size.                               *
  53.  *   L_SUBFR     : Sub-frame size.                           *
  54.  *   M           : LPC order.                                *
  55.  *   MP1         : LPC order+1                               *
  56.  *   L_TOTAL     : Total size of speech buffer.              *
  57.  *   PIT_MIN     : Minimum pitch lag.                        *
  58.  *   PIT_MAX     : Maximum pitch lag.                        *
  59.  *   L_INTERPOL  : Length of filter for interpolation        *
  60.  *-----------------------------------------------------------*/
  61. /*--------------------------------------------------------*
  62.  *         Static memory allocation.                      *
  63.  *--------------------------------------------------------*/
  64.         /* Speech vector */
  65.  static FLOAT old_speech[L_TOTAL];
  66.  static FLOAT *speech, *p_window;
  67.  FLOAT *new_speech;                    /* Global variable */
  68.         /* Weighted speech vector */
  69.  static FLOAT old_wsp[L_FRAME+PIT_MAX];
  70.  static FLOAT *wsp;
  71.         /* Excitation vector */
  72.  static FLOAT old_exc[L_FRAME+PIT_MAX+L_INTERPOL];
  73.  static FLOAT *exc;
  74.         /* LSP Line spectral frequencies */
  75.  static FLOAT lsp_old[M] =
  76.      { (F)0.9595,  (F)0.8413,  (F)0.6549,  (F)0.4154,  (F)0.1423,
  77.       (F)-0.1423, (F)-0.4154, (F)-0.6549, (F)-0.8413, (F)-0.9595};
  78.  static FLOAT lsp_old_q[M];
  79.         /* Filter's memory */
  80.  static FLOAT  mem_w0[M], mem_w[M], mem_zero[M];
  81.  static FLOAT  sharp;
  82. /*-----------------------------------------------------------------*
  83.  *   Function  init_coder_ld8a                                     *
  84.  *            ~~~~~~~~~~~~~~~                                      *
  85.  *                                                                 *
  86.  *  init_coder_ld8a(void);                                         *
  87.  *                                                                 *
  88.  *   ->Initialization of variables for the coder section.          *
  89.  *       - initialize pointers to speech buffer                    *
  90.  *       - initialize static  pointers                             *
  91.  *       - set static vectors to zero                              *
  92.  *-----------------------------------------------------------------*/
  93. void init_coder_ld8a(void)
  94. {
  95.   /*----------------------------------------------------------------------*
  96.   *      Initialize pointers to speech vector.                            *
  97.   *                                                                       *
  98.   *                                                                       *
  99.   *   |--------------------|-------------|-------------|------------|     *
  100.   *     previous speech           sf1           sf2         L_NEXT        *
  101.   *                                                                       *
  102.   *   <----------------  Total speech vector (L_TOTAL)   ----------->     *
  103.   *   <----------------  LPC analysis window (L_WINDOW)  ----------->     *
  104.   *   |                   <-- present frame (L_FRAME) -->                 *
  105.   * old_speech            |              <-- new speech (L_FRAME) -->     *
  106.   * p_window              |              |                                *
  107.   *                     speech           |                                *
  108.   *                             new_speech                                *
  109.   *-----------------------------------------------------------------------*/
  110.    new_speech = old_speech + L_TOTAL - L_FRAME;         /* New speech     */
  111.    speech     = new_speech - L_NEXT;                    /* Present frame  */
  112.    p_window   = old_speech + L_TOTAL - L_WINDOW;        /* For LPC window */
  113.    /* Initialize static pointers */
  114.    wsp    = old_wsp + PIT_MAX;
  115.    exc    = old_exc + PIT_MAX + L_INTERPOL;
  116.    /* Static vectors to zero */
  117.    set_zero(old_speech, L_TOTAL);
  118.    set_zero(old_exc, PIT_MAX+L_INTERPOL);
  119.    set_zero(old_wsp, PIT_MAX);
  120.    set_zero(mem_w,   M);
  121.    set_zero(mem_w0,  M);
  122.    set_zero(mem_zero, M);
  123.    sharp = SHARPMIN;
  124.    copy(lsp_old, lsp_old_q, M);
  125.    lsp_encw_reset() ;
  126.    init_exc_err();
  127.    return;
  128. }
  129. /*-----------------------------------------------------------------*
  130.  *   Function coder_ld8a                                           *
  131.  *            ~~~~~~~~~~                                           *
  132.  *  coder_ld8a(int    ana[]);                                      *
  133.  *                                                                 *
  134.  *   ->Main coder function.                                        *
  135.  *                                                                 *
  136.  *                                                                 *
  137.  *  Input:                                                         *
  138.  *                                                                 *
  139.  *    80 speech data should have beee copy to vector new_speech[]. *
  140.  *    This vector is global and is declared in this function.      *
  141.  *                                                                 *
  142.  *  Ouputs:                                                        *
  143.  *                                                                 *
  144.  *    ana[]      ->analysis parameters.                            *
  145.  *                                                                 *
  146.  *-----------------------------------------------------------------*/
  147. void coder_ld8a(
  148.  int ana[]                   /* output: analysis parameters */
  149. )
  150. {
  151.    /* LPC coefficients */
  152.    FLOAT Aq_t[(MP1)*2];         /* A(z) quantized for the 2 subframes   */
  153.    FLOAT Ap_t[(MP1)*2];         /* A(z) with spectral expansion         */
  154.    FLOAT *Aq, *Ap;              /* Pointer on Aq_t  and Ap_t            */
  155.    /* Other vectors */
  156.    FLOAT h1[L_SUBFR];           /* Impulse response h1[]              */
  157.    FLOAT xn[L_SUBFR];           /* Target vector for pitch search     */
  158.    FLOAT xn2[L_SUBFR];          /* Target vector for codebook search  */
  159.    FLOAT code[L_SUBFR];         /* Fixed codebook excitation          */
  160.    FLOAT y1[L_SUBFR];           /* Filtered adaptive excitation       */
  161.    FLOAT y2[L_SUBFR];           /* Filtered fixed codebook excitation */
  162.    FLOAT g_coeff[5];            /* Correlations between xn, y1, & y2:
  163.                                    <y1,y1>, <xn,y1>, <y2,y2>, <xn,y2>,<y1,y2>*/
  164.    /* Scalars */
  165.    int i, j, i_subfr;
  166.    int T_op, T0, T0_min, T0_max, T0_frac;
  167.    int index;
  168.    FLOAT   gain_pit, gain_code;
  169.    int     taming;
  170. /*------------------------------------------------------------------------*
  171.  *  - Perform LPC analysis:                                               *
  172.  *       * autocorrelation + lag windowing                                *
  173.  *       * Levinson-durbin algorithm to find a[]                          *
  174.  *       * convert a[] to lsp[]                                           *
  175.  *       * quantize and code the LSPs                                     *
  176.  *       * find the interpolated LSPs and convert to a[] for the 2        *
  177.  *         subframes (both quantized and unquantized)                     *
  178.  *------------------------------------------------------------------------*/
  179.    {
  180.       /* Temporary vectors */
  181.      FLOAT r[MP1];                    /* Autocorrelations       */
  182.      FLOAT rc[M];                     /* Reflexion coefficients */
  183.      FLOAT lsp_new[M];                /* lsp coefficients       */
  184.      FLOAT lsp_new_q[M];              /* Quantized lsp coeff.   */
  185.      /* LP analysis */
  186.      autocorr(p_window, M, r);             /* Autocorrelations */
  187.      lag_window(M, r);                     /* Lag windowing    */
  188.      levinson(r, Ap_t, rc);                /* Levinson Durbin  */
  189.      az_lsp(Ap_t, lsp_new, lsp_old);       /* Convert A(z) to lsp */
  190.      /* LSP quantization */
  191.      qua_lsp(lsp_new, lsp_new_q, ana);
  192.      ana += 2;                        /* Advance analysis parameters pointer */
  193.     /*--------------------------------------------------------------------*
  194.      * Find interpolated LPC parameters in all subframes                  *
  195.      * The interpolated parameters are in array Aq_t[].                   *
  196.      *--------------------------------------------------------------------*/
  197.     int_qlpc(lsp_old_q, lsp_new_q, Aq_t);
  198.     /* Compute A(z/gamma) */
  199.     weight_az(&Aq_t[0],   GAMMA1, M, &Ap_t[0]);
  200.     weight_az(&Aq_t[MP1], GAMMA1, M, &Ap_t[MP1]);
  201.     /* update the LSPs for the next frame */
  202.     copy(lsp_new,   lsp_old,   M);
  203.     copy(lsp_new_q, lsp_old_q, M);
  204.   }
  205.    /*----------------------------------------------------------------------*
  206.     * - Find the weighted input speech w_sp[] for the whole speech frame   *
  207.     * - Find the open-loop pitch delay for the whole speech frame          *
  208.     * - Set the range for searching closed-loop pitch in 1st subframe      *
  209.     *----------------------------------------------------------------------*/
  210.    residu(&Aq_t[0],   &speech[0],       &exc[0],       L_SUBFR);
  211.    residu(&Aq_t[MP1], &speech[L_SUBFR], &exc[L_SUBFR], L_SUBFR);
  212.   {
  213.      FLOAT Ap1[MP1];
  214.      Ap = Ap_t;
  215.      Ap1[0] = (F)1.0;
  216.      for(i=1; i<=M; i++)
  217.        Ap1[i] = Ap[i] - (F)0.7 * Ap[i-1];
  218.      syn_filt(Ap1, &exc[0], &wsp[0], L_SUBFR, mem_w, 1);
  219.      Ap += MP1;
  220.      for(i=1; i<=M; i++)
  221.        Ap1[i] = Ap[i] - (F)0.7 * Ap[i-1];
  222.      syn_filt(Ap1, &exc[L_SUBFR], &wsp[L_SUBFR], L_SUBFR, mem_w, 1);
  223.    }
  224.    /* Find open loop pitch lag for whole speech frame */
  225.    T_op = pitch_ol_fast(wsp, L_FRAME);
  226.    /* Range for closed loop pitch search in 1st subframe */
  227.    T0_min = T_op - 3;
  228.    if (T0_min < PIT_MIN) T0_min = PIT_MIN;
  229.    T0_max = T0_min + 6;
  230.    if (T0_max > PIT_MAX)
  231.    {
  232.       T0_max = PIT_MAX;
  233.       T0_min = T0_max - 6;
  234.    }
  235.    /*------------------------------------------------------------------------*
  236.     *          Loop for every subframe in the analysis frame                 *
  237.     *------------------------------------------------------------------------*
  238.     *  To find the pitch and innovation parameters. The subframe size is     *
  239.     *  L_SUBFR and the loop is repeated L_FRAME/L_SUBFR times.               *
  240.     *     - find the weighted LPC coefficients                               *
  241.     *     - find the LPC residual signal                                     *
  242.     *     - compute the target signal for pitch search                       *
  243.     *     - compute impulse response of weighted synthesis filter (h1[])     *
  244.     *     - find the closed-loop pitch parameters                            *
  245.     *     - encode the pitch delay                                           *
  246.     *     - find target vector for codebook search                           *
  247.     *     - codebook search                                                  *
  248.     *     - VQ of pitch and codebook gains                                   *
  249.     *     - update states of weighting filter                                *
  250.     *------------------------------------------------------------------------*/
  251.    Aq = Aq_t;    /* pointer to interpolated quantized LPC parameters */
  252.    Ap = Ap_t;    /* pointer to weighted LPC coefficients             */
  253.    for (i_subfr = 0;  i_subfr < L_FRAME; i_subfr += L_SUBFR)
  254.    {
  255.       /*---------------------------------------------------------------*
  256.        * Compute impulse response, h1[], of weighted synthesis filter  *
  257.        *---------------------------------------------------------------*/
  258.       h1[0] = (F)1.0;
  259.       set_zero(&h1[1], L_SUBFR-1);
  260.       syn_filt(Ap, h1, h1, L_SUBFR, &h1[1], 0);
  261.       /*-----------------------------------------------*
  262.        * Find the target vector for pitch search:      *
  263.        *----------------------------------------------*/
  264.       syn_filt(Ap, &exc[i_subfr], xn, L_SUBFR, mem_w0, 0);
  265.       /*-----------------------------------------------------------------*
  266.        *    Closed-loop fractional pitch search                          *
  267.        *-----------------------------------------------------------------*/
  268.       T0 = pitch_fr3_fast(&exc[i_subfr], xn, h1, L_SUBFR, T0_min, T0_max,
  269.                     i_subfr, &T0_frac);
  270.       index = enc_lag3(T0, T0_frac, &T0_min, &T0_max, PIT_MIN, PIT_MAX,
  271.                             i_subfr);
  272.       *ana++ = index;
  273.       if (i_subfr == 0)
  274.         *ana++ = parity_pitch(index);
  275.       /*-----------------------------------------------------------------*
  276.        *   - find filtered pitch exc                                     *
  277.        *   - compute pitch gain and limit between 0 and 1.2              *
  278.        *   - update target vector for codebook search                    *
  279.        *   - find LTP residual.                                          *
  280.        *-----------------------------------------------------------------*/
  281.       syn_filt(Ap, &exc[i_subfr], y1, L_SUBFR, mem_zero, 0);
  282.       gain_pit = g_pitch(xn, y1, g_coeff, L_SUBFR);
  283.       /* clip pitch gain if taming is necessary */
  284.       taming = test_err(T0, T0_frac);
  285.       if( taming == 1){
  286.         if (gain_pit > GPCLIP) {
  287.           gain_pit = GPCLIP;
  288.       }
  289.     }
  290.     for (i = 0; i < L_SUBFR; i++)
  291.         xn2[i] = xn[i] - y1[i]*gain_pit;
  292.       /*-----------------------------------------------------*
  293.        * - Innovative codebook search.                       *
  294.        *-----------------------------------------------------*/
  295.       index = ACELP_code_A(xn2, h1, T0, sharp, code, y2, &i);
  296.       *ana++ = index;           /* Positions index */
  297.       *ana++ = i;               /* Signs index     */
  298.       /*------------------------------------------------------*
  299.        *  - Compute the correlations <y2,y2>, <xn,y2>, <y1,y2>*
  300.        *  - Vector quantize gains.                            *
  301.        *------------------------------------------------------*/
  302.       corr_xy2(xn, y1, y2, g_coeff);
  303.        *ana++ =qua_gain(code, g_coeff, L_SUBFR, &gain_pit, &gain_code,
  304.                                     taming);
  305.       /*------------------------------------------------------------*
  306.        * - Update pitch sharpening "sharp" with quantized gain_pit  *
  307.        *------------------------------------------------------------*/
  308.       sharp = gain_pit;
  309.       if (sharp > SHARPMAX) sharp = SHARPMAX;
  310.       if (sharp < SHARPMIN) sharp = SHARPMIN;
  311.       /*------------------------------------------------------*
  312.        * - Find the total excitation                          *
  313.        * - update filters' memories for finding the target    *
  314.        *   vector in the next subframe  (mem_w0[])            *
  315.        *------------------------------------------------------*/
  316.       for (i = 0; i < L_SUBFR;  i++)
  317.         exc[i+i_subfr] = gain_pit*exc[i+i_subfr] + gain_code*code[i];
  318.       update_exc_err(gain_pit, T0);
  319.       for (i = L_SUBFR-M, j = 0; i < L_SUBFR; i++, j++)
  320.         mem_w0[j]  = xn[i] - gain_pit*y1[i] - gain_code*y2[i];
  321.       Aq += MP1;           /* interpolated LPC parameters for next subframe */
  322.       Ap += MP1;
  323.    }
  324.    /*--------------------------------------------------*
  325.     * Update signal for next frame.                    *
  326.     * -> shift to the left by L_FRAME:                 *
  327.     *     speech[], wsp[] and  exc[]                   *
  328.     *--------------------------------------------------*/
  329.    copy(&old_speech[L_FRAME], &old_speech[0], L_TOTAL-L_FRAME);
  330.    copy(&old_wsp[L_FRAME], &old_wsp[0], PIT_MAX);
  331.    copy(&old_exc[L_FRAME], &old_exc[0], PIT_MAX+L_INTERPOL);
  332.    return;
  333. }