COD_LD8A.C
上传用户:meifeng08
上传日期:2013-06-18
资源大小:5304k
文件大小:19k
源码类别:

语音压缩

开发平台:

C/C++

  1. /*
  2.    ITU-T G.729A Speech Coder    ANSI-C Source Code
  3.    Version 1.1    Last modified: September 1996
  4.    Copyright (c) 1996,
  5.    AT&T, France Telecom, NTT, Universite de Sherbrooke
  6.    All rights reserved.
  7. */
  8. /*-----------------------------------------------------------------*
  9.  *   Functions Coder_ld8a and Init_Coder_ld8a                      *
  10.  *             ~~~~~~~~~~     ~~~~~~~~~~~~~~~                      *
  11.  *                                                                 *      
  12.  *  Init_Coder_ld8a(void);                                         *
  13.  *                                                                 *
  14.  *   ->Initialization of variables for the coder section.          *
  15.  *                                                                 *
  16.  *                                                                 *
  17.  *  Coder_ld8a(Word16 ana[]);                                      *
  18.  *                                                                 *
  19.  *   ->Main coder function.                                        *
  20.  *                                                                 *
  21.  *                                                                 *
  22.  *  Input:                                                         *
  23.  *                                                                 *
  24.  *    80 speech data should have beee copy to vector new_speech[]. *
  25.  *    This vector is global and is declared in this function.      *
  26.  *                                                                 *
  27.  *  Ouputs:                                                        *
  28.  *                                                                 *
  29.  *    ana[]      ->analysis parameters.                            *
  30.  *                                                                 *
  31.  *-----------------------------------------------------------------*/
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include "typedef.h"
  35. #include "basic_op.h"
  36. #include "ld8a.h"
  37. /*-----------------------------------------------------------*
  38.  *    Coder constant parameters (defined in "ld8a.h")        *
  39.  *-----------------------------------------------------------*
  40.  *   L_WINDOW    : LPC analysis window size.                 *
  41.  *   L_NEXT      : Samples of next frame needed for autocor. *
  42.  *   L_FRAME     : Frame size.                               *
  43.  *   L_SUBFR     : Sub-frame size.                           *
  44.  *   M           : LPC order.                                *
  45.  *   MP1         : LPC order+1                               *
  46.  *   L_TOTAL     : Total size of speech buffer.              *
  47.  *   PIT_MIN     : Minimum pitch lag.                        *
  48.  *   PIT_MAX     : Maximum pitch lag.                        *
  49.  *   L_INTERPOL  : Length of filter for interpolation        *
  50.  *-----------------------------------------------------------*/
  51. /*--------------------------------------------------------*
  52.  *         Static memory allocation.                      *
  53.  *--------------------------------------------------------*/
  54.         /* Speech vector */
  55.  static Word16 old_speech[L_TOTAL];
  56.  static Word16 *speech, *p_window;
  57.  Word16 *new_speech;                    /* Global variable */
  58.         /* Weighted speech vector */
  59.  static Word16 old_wsp[L_FRAME+PIT_MAX];
  60.  static Word16 *wsp;
  61.         /* Excitation vector */
  62.  static Word16 old_exc[L_FRAME+PIT_MAX+L_INTERPOL];
  63.  static Word16 *exc;
  64.         /* Lsp (Line spectral pairs) */
  65.  static Word16 lsp_old[M]={
  66.               30000, 26000, 21000, 15000, 8000, 0, -8000,-15000,-21000,-26000};
  67.  static Word16 lsp_old_q[M];
  68.         /* Filter's memory */
  69.  static Word16  mem_w0[M], mem_w[M], mem_zero[M];
  70.  static Word16  sharp;
  71. /*-----------------------------------------------------------------*
  72.  *   Function  Init_Coder_ld8a                                     *
  73.  *            ~~~~~~~~~~~~~~~                                      *
  74.  *                                                                 *
  75.  *  Init_Coder_ld8a(void);                                         *
  76.  *                                                                 *
  77.  *   ->Initialization of variables for the coder section.          *
  78.  *       - initialize pointers to speech buffer                    *
  79.  *       - initialize static  pointers                             *
  80.  *       - set static vectors to zero                              *
  81.  *                                                                 *
  82.  *-----------------------------------------------------------------*/
  83. void Init_Coder_ld8a(void)
  84. {
  85.   /*----------------------------------------------------------------------*
  86.   *      Initialize pointers to speech vector.                            *
  87.   *                                                                       *
  88.   *                                                                       *
  89.   *   |--------------------|-------------|-------------|------------|     *
  90.   *     previous speech           sf1           sf2         L_NEXT        *
  91.   *                                                                       *
  92.   *   <----------------  Total speech vector (L_TOTAL)   ----------->     *
  93.   *   <----------------  LPC analysis window (L_WINDOW)  ----------->     *
  94.   *   |                   <-- present frame (L_FRAME) -->                 *
  95.   * old_speech            |              <-- new speech (L_FRAME) -->     *
  96.   * p_window              |              |                                *
  97.   *                     speech           |                                *
  98.   *                             new_speech                                *
  99.   *-----------------------------------------------------------------------*/
  100.   new_speech = old_speech + L_TOTAL - L_FRAME;         /* New speech     */
  101.   speech     = new_speech - L_NEXT;                    /* Present frame  */
  102.   p_window   = old_speech + L_TOTAL - L_WINDOW;        /* For LPC window */
  103.   /* Initialize static pointers */
  104.   wsp    = old_wsp + PIT_MAX;
  105.   exc    = old_exc + PIT_MAX + L_INTERPOL;
  106.   /* Static vectors to zero */
  107.   Set_zero(old_speech, L_TOTAL);
  108.   Set_zero(old_exc, PIT_MAX+L_INTERPOL);
  109.   Set_zero(old_wsp, PIT_MAX);
  110.   Set_zero(mem_w,   M);
  111.   Set_zero(mem_w0,  M);
  112.   Set_zero(mem_zero, M);
  113.   sharp = SHARPMIN;
  114.   /* Initialize lsp_old_q[] */
  115.   Copy(lsp_old, lsp_old_q, M);
  116.   Lsp_encw_reset();
  117.   Init_exc_err();
  118.  return;
  119. }
  120. /*-----------------------------------------------------------------*
  121.  *   Functions Coder_ld8a                                          *
  122.  *            ~~~~~~~~~~                                           *
  123.  *  Coder_ld8a(Word16 ana[]);                                      *
  124.  *                                                                 *
  125.  *   ->Main coder function.                                        *
  126.  *                                                                 *
  127.  *                                                                 *
  128.  *  Input:                                                         *
  129.  *                                                                 *
  130.  *    80 speech data should have beee copy to vector new_speech[]. *
  131.  *    This vector is global and is declared in this function.      *
  132.  *                                                                 *
  133.  *  Ouputs:                                                        *
  134.  *                                                                 *
  135.  *    ana[]      ->analysis parameters.                            *
  136.  *                                                                 *
  137.  *-----------------------------------------------------------------*/
  138. void Coder_ld8a(
  139.      Word16 ana[]       /* output  : Analysis parameters */
  140. )
  141. {
  142.   /* LPC analysis */
  143.   Word16 Aq_t[(MP1)*2];         /* A(z)   quantized for the 2 subframes */
  144.   Word16 Ap_t[(MP1)*2];         /* A(z/gamma)       for the 2 subframes */
  145.   Word16 *Aq, *Ap;              /* Pointer on Aq_t and Ap_t             */
  146.   /* Other vectors */
  147.   Word16 h1[L_SUBFR];            /* Impulse response h1[]              */
  148.   Word16 xn[L_SUBFR];            /* Target vector for pitch search     */
  149.   Word16 xn2[L_SUBFR];           /* Target vector for codebook search  */
  150.   Word16 code[L_SUBFR];          /* Fixed codebook excitation          */
  151.   Word16 y1[L_SUBFR];            /* Filtered adaptive excitation       */
  152.   Word16 y2[L_SUBFR];            /* Filtered fixed codebook excitation */
  153.   Word16 g_coeff[4];             /* Correlations between xn & y1       */
  154.   Word16 g_coeff_cs[5];
  155.   Word16 exp_g_coeff_cs[5];      /* Correlations between xn, y1, & y2
  156.                                      <y1,y1>, -2<xn,y1>,
  157.                                           <y2,y2>, -2<xn,y2>, 2<y1,y2> */
  158.   /* Scalars */
  159.   Word16 i, j, k, i_subfr;
  160.   Word16 T_op, T0, T0_min, T0_max, T0_frac;
  161.   Word16 gain_pit, gain_code, index;
  162.   Word16 temp, taming;
  163.   Word32 L_temp;
  164. /*------------------------------------------------------------------------*
  165.  *  - Perform LPC analysis:                                               *
  166.  *       * autocorrelation + lag windowing                                *
  167.  *       * Levinson-durbin algorithm to find a[]                          *
  168.  *       * convert a[] to lsp[]                                           *
  169.  *       * quantize and code the LSPs                                     *
  170.  *       * find the interpolated LSPs and convert to a[] for the 2        *
  171.  *         subframes (both quantized and unquantized)                     *
  172.  *------------------------------------------------------------------------*/
  173.   {
  174.      /* Temporary vectors */
  175.     Word16 r_l[MP1], r_h[MP1];       /* Autocorrelations low and hi          */
  176.     Word16 rc[M];                    /* Reflection coefficients.             */
  177.     Word16 lsp_new[M], lsp_new_q[M]; /* LSPs at 2th subframe                 */
  178.     /* LP analysis */
  179.     Autocorr(p_window, M, r_h, r_l);              /* Autocorrelations */
  180.     Lag_window(M, r_h, r_l);                      /* Lag windowing    */
  181.     Levinson(r_h, r_l, Ap_t, rc);                 /* Levinson Durbin  */
  182.     Az_lsp(Ap_t, lsp_new, lsp_old);               /* From A(z) to lsp */
  183.     /* LSP quantization */
  184.     Qua_lsp(lsp_new, lsp_new_q, ana);
  185.     ana += 2;                         /* Advance analysis parameters pointer */
  186.     /*--------------------------------------------------------------------*
  187.      * Find interpolated LPC parameters in all subframes                  *
  188.      * The interpolated parameters are in array Aq_t[].                   *
  189.      *--------------------------------------------------------------------*/
  190.     Int_qlpc(lsp_old_q, lsp_new_q, Aq_t);
  191.     /* Compute A(z/gamma) */
  192.     Weight_Az(&Aq_t[0],   GAMMA1, M, &Ap_t[0]);
  193.     Weight_Az(&Aq_t[MP1], GAMMA1, M, &Ap_t[MP1]);
  194.     /* update the LSPs for the next frame */
  195.     Copy(lsp_new,   lsp_old,   M);
  196.     Copy(lsp_new_q, lsp_old_q, M);
  197.   }
  198.  /*----------------------------------------------------------------------*
  199.   * - Find the weighted input speech w_sp[] for the whole speech frame   *
  200.   * - Find the open-loop pitch delay                                     *
  201.   *----------------------------------------------------------------------*/
  202.   Residu(&Aq_t[0], &speech[0], &exc[0], L_SUBFR);
  203.   Residu(&Aq_t[MP1], &speech[L_SUBFR], &exc[L_SUBFR], L_SUBFR);
  204.   {
  205.     Word16 Ap1[MP1];
  206.     Ap = Ap_t;
  207.     Ap1[0] = 4096;
  208.     for(i=1; i<=M; i++)    /* Ap1[i] = Ap[i] - 0.7 * Ap[i-1]; */
  209.        Ap1[i] = sub(Ap[i], mult(Ap[i-1], 22938));
  210.     Syn_filt(Ap1, &exc[0], &wsp[0], L_SUBFR, mem_w, 1);
  211.     Ap += MP1;
  212.     for(i=1; i<=M; i++)    /* Ap1[i] = Ap[i] - 0.7 * Ap[i-1]; */
  213.        Ap1[i] = sub(Ap[i], mult(Ap[i-1], 22938));
  214.     Syn_filt(Ap1, &exc[L_SUBFR], &wsp[L_SUBFR], L_SUBFR, mem_w, 1);
  215.   }
  216.   /* Find open loop pitch lag */
  217.   T_op = Pitch_ol_fast(wsp, PIT_MAX, L_FRAME);
  218.   /* Range for closed loop pitch search in 1st subframe */
  219.   T0_min = sub(T_op, 3);
  220.   if (sub(T0_min,PIT_MIN)<0) {
  221.     T0_min = PIT_MIN;
  222.   }
  223.   T0_max = add(T0_min, 6);
  224.   if (sub(T0_max ,PIT_MAX)>0)
  225.   {
  226.      T0_max = PIT_MAX;
  227.      T0_min = sub(T0_max, 6);
  228.   }
  229.  /*------------------------------------------------------------------------*
  230.   *          Loop for every subframe in the analysis frame                 *
  231.   *------------------------------------------------------------------------*
  232.   *  To find the pitch and innovation parameters. The subframe size is     *
  233.   *  L_SUBFR and the loop is repeated 2 times.                             *
  234.   *     - find the weighted LPC coefficients                               *
  235.   *     - find the LPC residual signal res[]                               *
  236.   *     - compute the target signal for pitch search                       *
  237.   *     - compute impulse response of weighted synthesis filter (h1[])     *
  238.   *     - find the closed-loop pitch parameters                            *
  239.   *     - encode the pitch delay                                           *
  240.   *     - find target vector for codebook search                           *
  241.   *     - codebook search                                                  *
  242.   *     - VQ of pitch and codebook gains                                   *
  243.   *     - update states of weighting filter                                *
  244.   *------------------------------------------------------------------------*/
  245.   Aq = Aq_t;    /* pointer to interpolated quantized LPC parameters */
  246.   Ap = Ap_t;    /* pointer to weighted LPC coefficients             */
  247.   for (i_subfr = 0;  i_subfr < L_FRAME; i_subfr += L_SUBFR)
  248.   {
  249.     /*---------------------------------------------------------------*
  250.      * Compute impulse response, h1[], of weighted synthesis filter  *
  251.      *---------------------------------------------------------------*/
  252.     h1[0] = 4096;
  253.     Set_zero(&h1[1], L_SUBFR-1);
  254.     Syn_filt(Ap, h1, h1, L_SUBFR, &h1[1], 0);
  255.    /*----------------------------------------------------------------------*
  256.     *  Find the target vector for pitch search:                            *
  257.     *----------------------------------------------------------------------*/
  258.     Syn_filt(Ap, &exc[i_subfr], xn, L_SUBFR, mem_w0, 0);
  259.     /*---------------------------------------------------------------------*
  260.      *                 Closed-loop fractional pitch search                 *
  261.      *---------------------------------------------------------------------*/
  262.     T0 = Pitch_fr3_fast(&exc[i_subfr], xn, h1, L_SUBFR, T0_min, T0_max,
  263.                     i_subfr, &T0_frac);
  264.     index = Enc_lag3(T0, T0_frac, &T0_min, &T0_max,PIT_MIN,PIT_MAX,i_subfr);
  265.     *ana++ = index;
  266.     if (i_subfr == 0) {
  267.       *ana++ = Parity_Pitch(index);
  268.     }
  269.    /*-----------------------------------------------------------------*
  270.     *   - find filtered pitch exc                                     *
  271.     *   - compute pitch gain and limit between 0 and 1.2              *
  272.     *   - update target vector for codebook search                    *
  273.     *-----------------------------------------------------------------*/
  274.     Syn_filt(Ap, &exc[i_subfr], y1, L_SUBFR, mem_zero, 0);
  275.     gain_pit = G_pitch(xn, y1, g_coeff, L_SUBFR);
  276.     /* clip pitch gain if taming is necessary */
  277.     taming = test_err(T0, T0_frac);
  278.     if( taming == 1){
  279.       if (sub(gain_pit, GPCLIP) > 0) {
  280.         gain_pit = GPCLIP;
  281.       }
  282.     }
  283.     /* xn2[i]   = xn[i] - y1[i] * gain_pit  */
  284.     for (i = 0; i < L_SUBFR; i++)
  285.     {
  286.       L_temp = L_mult(y1[i], gain_pit);
  287.       L_temp = L_shl(L_temp, 1);               /* gain_pit in Q14 */
  288.       xn2[i] = sub(xn[i], extract_h(L_temp));
  289.     }
  290.    /*-----------------------------------------------------*
  291.     * - Innovative codebook search.                       *
  292.     *-----------------------------------------------------*/
  293.     index = ACELP_Code_A(xn2, h1, T0, sharp, code, y2, &i);
  294.     *ana++ = index;        /* Positions index */
  295.     *ana++ = i;            /* Signs index     */
  296.    /*-----------------------------------------------------*
  297.     * - Quantization of gains.                            *
  298.     *-----------------------------------------------------*/
  299.     g_coeff_cs[0]     = g_coeff[0];            /* <y1,y1> */
  300.     exp_g_coeff_cs[0] = negate(g_coeff[1]);    /* Q-Format:XXX -> JPN */
  301.     g_coeff_cs[1]     = negate(g_coeff[2]);    /* (xn,y1) -> -2<xn,y1> */
  302.     exp_g_coeff_cs[1] = negate(add(g_coeff[3], 1)); /* Q-Format:XXX -> JPN */
  303.     Corr_xy2( xn, y1, y2, g_coeff_cs, exp_g_coeff_cs );  /* Q0 Q0 Q12 ^Qx ^Q0 */
  304.                          /* g_coeff_cs[3]:exp_g_coeff_cs[3] = <y2,y2>   */
  305.                          /* g_coeff_cs[4]:exp_g_coeff_cs[4] = -2<xn,y2> */
  306.                          /* g_coeff_cs[5]:exp_g_coeff_cs[5] = 2<y1,y2>  */
  307.     *ana++ = Qua_gain(code, g_coeff_cs, exp_g_coeff_cs,
  308.                          L_SUBFR, &gain_pit, &gain_code, taming);
  309.    /*------------------------------------------------------------*
  310.     * - Update pitch sharpening "sharp" with quantized gain_pit  *
  311.     *------------------------------------------------------------*/
  312.     sharp = gain_pit;
  313.     if (sub(sharp, SHARPMAX) > 0) { sharp = SHARPMAX;         }
  314.     if (sub(sharp, SHARPMIN) < 0) { sharp = SHARPMIN;         }
  315.    /*------------------------------------------------------*
  316.     * - Find the total excitation                          *
  317.     * - update filters memories for finding the target     *
  318.     *   vector in the next subframe                        *
  319.     *------------------------------------------------------*/
  320.     for (i = 0; i < L_SUBFR;  i++)
  321.     {
  322.       /* exc[i] = gain_pit*exc[i] + gain_code*code[i]; */
  323.       /* exc[i]  in Q0   gain_pit in Q14               */
  324.       /* code[i] in Q13  gain_cod in Q1                */
  325.       L_temp = L_mult(exc[i+i_subfr], gain_pit);
  326.       L_temp = L_mac(L_temp, code[i], gain_code);
  327.       L_temp = L_shl(L_temp, 1);
  328.       exc[i+i_subfr] = round(L_temp);
  329.     }
  330.     update_exc_err(gain_pit, T0);
  331.     for (i = L_SUBFR-M, j = 0; i < L_SUBFR; i++, j++)
  332.     {
  333.       temp       = extract_h(L_shl( L_mult(y1[i], gain_pit),  1) );
  334.       k          = extract_h(L_shl( L_mult(y2[i], gain_code), 2) );
  335.       mem_w0[j]  = sub(xn[i], add(temp, k));
  336.     }
  337.     Aq += MP1;           /* interpolated LPC parameters for next subframe */
  338.     Ap += MP1;
  339.   }
  340.  /*--------------------------------------------------*
  341.   * Update signal for next frame.                    *
  342.   * -> shift to the left by L_FRAME:                 *
  343.   *     speech[], wsp[] and  exc[]                   *
  344.   *--------------------------------------------------*/
  345.   Copy(&old_speech[L_FRAME], &old_speech[0], L_TOTAL-L_FRAME);
  346.   Copy(&old_wsp[L_FRAME], &old_wsp[0], PIT_MAX);
  347.   Copy(&old_exc[L_FRAME], &old_exc[0], PIT_MAX+L_INTERPOL);
  348.   return;
  349. }