Example #1
0
void decod_ld8a(struct dec_state_t * state,
  int      parm[],      /* (i)   : vector of synthesis parameters
                                  parm[0] = bad frame indicator (bfi)  */
  GFLOAT   synth[],     /* (o)   : synthesis speech                     */
  GFLOAT   A_t[],       /* (o)   : decoded LP filter in 2 subframes     */
  int     *T2,         /* (o)   : decoded pitch lag in 2 subframes     */
  int     *Vad         /* (o)   : decoded frame type     */
)
{
  GFLOAT   *Az;                  /* Pointer on A_t   */
  GFLOAT   lsp_new[M];           /* Decoded LSP's    */
  GFLOAT   code[L_SUBFR];        /* ACELP codevector */

   /* Scalars */

   int i, i_subfr;
   int t0, t0_frac, index;
   int  bfi, bad_pitch;

   /* for G.729B */
   int ftyp;
   GFLOAT lsfq_mem[MA_NP][M];

   /* Test bad frame indicator (bfi) */

   bfi = *parm++;
   
   /* for G.729B */
   ftyp = *parm;

   if (bfi)
   {
     if(state->past_ftyp == 1)
       ftyp = 1;
     else
       ftyp = 0;
     *parm = ftyp;  /* modification introduced in version V1.3 */
   }
  
   *Vad = ftyp;

  /* Processing non active frames (SID & not transmitted) */
  if(ftyp != 1)
  {
    get_freq_prev((const GFLOAT (*)[M]) state->lsp_s.freq_prev, lsfq_mem);
    dec_cng(&state->cng_s, state->past_ftyp, state->sid_sav, parm, state->exc, state->lsp_old,
            A_t, &state->seed, lsfq_mem);
    update_freq_prev(state->lsp_s.freq_prev, (const GFLOAT (*)[M]) lsfq_mem);

    Az = A_t;
    for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
	{
      syn_filt(Az, &state->exc[i_subfr], &synth[i_subfr], L_SUBFR, state->mem_syn, 0);
      copy(&synth[i_subfr+L_SUBFR-M], state->mem_syn, M);
      
      Az += MP1;

      *T2++ = state->old_t0;
    }
    state->sharp = SHARPMIN;
    
  }
  else /* Processing active frame */
  {
    
    state->seed = INIT_SEED;
    parm++;

    /* Decode the LSPs */

    d_lsp(&state->lsp_s, parm, lsp_new, bfi+state->bad_lsf );
    parm += 2;                        /* Advance synthesis parameters pointer */

    /*
    Note: "bad_lsf" is introduce in case the standard is used with
           channel protection.
    */

    /* Interpolation of LPC for the 2 subframes */

    int_qlpc(state->lsp_old, lsp_new, A_t);

    /* update the LSFs for the next frame */

    copy(lsp_new, state->lsp_old, M);

    /*------------------------------------------------------------------------*
     *          Loop for every subframe in the analysis frame                 *
     *------------------------------------------------------------------------*
     * The subframe size is L_SUBFR and the loop is repeated L_FRAME/L_SUBFR  *
     *  times                                                                 *
     *     - decode the pitch delay                                           *
     *     - decode algebraic code                                            *
     *     - decode pitch and codebook gains                                  *
     *     - find the excitation and compute synthesis speech                 *
     *------------------------------------------------------------------------*/

    Az = A_t;                     /* pointer to interpolated LPC parameters */

    for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
	{
      /*-------------------------------------------------*
       *  - Find the adaptive codebook vector.            *
       *--------------------------------------------------*/

      index = *parm++;            /* pitch index */

      if (i_subfr == 0)
      {

        i = *parm++; /* get parity check result */
        bad_pitch = bfi + i;
        if( bad_pitch == 0)
        {
            dec_lag3(index, PIT_MIN, PIT_MAX, i_subfr, &t0, &t0_frac);
            state->old_t0 = t0;
        }
        else        /* Bad frame, or parity error */
        {
          t0  =  state->old_t0;
          t0_frac = 0;
          state->old_t0++;
          if( (state->old_t0 - PIT_MAX) > 0)
            state->old_t0 = PIT_MAX;
        }

      }
      else                  /* second subframe */
      {
        if( bfi == 0)
        {
          dec_lag3(index, PIT_MIN, PIT_MAX, i_subfr, &t0, &t0_frac);
          state->old_t0 = t0;
        }
        else
        {
          t0  =  state->old_t0;
          t0_frac = 0;
          state->old_t0++;
          if( (state->old_t0 - PIT_MAX) > 0)
            state->old_t0 = PIT_MAX;
        }
      }
      *T2++ = t0;

     /*-------------------------------------------------*
      * - Find the adaptive codebook vector.            *
      *-------------------------------------------------*/

      pred_lt_3(&state->exc[i_subfr], t0, t0_frac, L_SUBFR);

      /*-------------------------------------------------------*
       * - Decode innovative codebook.                         *
       * - Add the fixed-gain pitch contribution to code[].    *
       *-------------------------------------------------------*/

      if(bfi != 0)        /* Bad frame */
      {
        parm[0] = random_g729(&state->seed_fer) & (INT16)0x1fff;     /* 13 bits random */
        parm[1] = random_g729(&state->seed_fer) & (INT16)0x000f;     /*  4 bits random */
      }

      decod_ACELP(parm[1], parm[0], code);
      parm +=2;

      for (i = t0; i < L_SUBFR; i++)   code[i] += state->sharp * code[i-t0];

      /*-------------------------------------------------*
       * - Decode pitch and codebook gains.              *
       *-------------------------------------------------*/

      index = *parm++;          /* index of energy VQ */

      dec_gain(&state->gain_s, index, code, L_SUBFR, bfi, &state->gain_pitch, &state->gain_code);

      /*-------------------------------------------------------------*
       * - Update pitch sharpening "sharp" with quantized gain_pitch *
       *-------------------------------------------------------------*/

      state->sharp = state->gain_pitch;
      if (state->sharp > SHARPMAX) state->sharp = SHARPMAX;
      if (state->sharp < SHARPMIN) state->sharp = SHARPMIN;

      /*-------------------------------------------------------*
       * - Find the total excitation.                          *
       * - Find synthesis speech corresponding to exc[].       *
       *-------------------------------------------------------*/

      for (i = 0; i < L_SUBFR;  i++)
         state->exc[i+i_subfr] = state->gain_pitch*state->exc[i+i_subfr] + state->gain_code*code[i];

      syn_filt(Az, &state->exc[i_subfr], &synth[i_subfr], L_SUBFR, state->mem_syn, 1);

      Az  += MP1;        /* interpolated LPC parameters for next subframe */
    }
  }

  /*------------*
   *  For G729b
   *-----------*/
  if (bfi == 0)
  {
     state->sid_sav = (F)0.0;
     for (i=0; i<L_FRAME; i++)
        state->sid_sav += state->exc[i] * state->exc[i];
  }
  state->past_ftyp = ftyp;

  /*--------------------------------------------------*
   * Update signal for next frame.                    *
   * -> shift to the left by L_FRAME  exc[]           *
   *--------------------------------------------------*/

   copy(&state->old_exc[L_FRAME], &state->old_exc[0], PIT_MAX+L_INTERPOL);
}
Example #2
0
/*--------------------------------------------------------------------------
 * decod_ld8k - decoder
 *--------------------------------------------------------------------------
 */
void decod_ld8k(
 int parm[],            /* input : synthesis parameters (parm[0] = bfi)       */
 int voicing,           /* input : voicing decision from previous frame       */
 FLOAT synth[],         /* output: synthesized speech                         */
 FLOAT A_t[],           /* output: two sets of A(z) coefficients length=2*MP1 */
 int *t0_first          /* output: integer delay of first subframe            */
)
{
   FLOAT *Az;                  /* Pointer to A_t (LPC coefficients)  */
   FLOAT lsp_new[M];           /* LSPs                               */
   FLOAT code[L_SUBFR];        /* algebraic codevector               */

  /* Scalars */
  int   i, i_subfr;
  int   t0, t0_frac, index;

  int bfi;
  int bad_pitch;

  /* Test bad frame indicator (bfi) */

  bfi = *parm++;

  /* Decode the LSPs */

  d_lsp(parm, lsp_new, bfi);
  parm += 2;             /* Advance synthesis parameters pointer */

  /* Interpolation of LPC for the 2 subframes */

  int_qlpc(lsp_old, lsp_new, A_t);

  /* update the LSFs for the next frame */

  copy(lsp_new, lsp_old, M);

/*------------------------------------------------------------------------*
 *          Loop for every subframe in the analysis frame                 *
 *------------------------------------------------------------------------*
 * The subframe size is L_SUBFR and the loop is repeated L_FRAME/L_SUBFR  *
 *  times                                                                 *
 *     - decode the pitch delay                                           *
 *     - decode algebraic code                                            *
 *     - decode pitch and codebook gains                                  *
 *     - find the excitation and compute synthesis speech                 *
 *------------------------------------------------------------------------*/

  Az = A_t;            /* pointer to interpolated LPC parameters */

  for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) {

   index = *parm++;          /* pitch index */

   if (i_subfr == 0) {      /* if first subframe */
     i = *parm++;             /* get parity check result */
     bad_pitch = bfi+ i;
     if( bad_pitch == 0)
     {
       dec_lag3(index, PIT_MIN, PIT_MAX, i_subfr, &t0, &t0_frac);
       old_t0 = t0;
     }
     else                     /* Bad frame, or parity error */
     {
       t0  =  old_t0;
       t0_frac = 0;
       old_t0++;
       if( old_t0> PIT_MAX) {
           old_t0 = PIT_MAX;
       }
     }
      *t0_first = t0;         /* If first frame */
   }
   else                       /* second subframe */
   {
     if( bfi == 0)
     {
       dec_lag3(index, PIT_MIN, PIT_MAX, i_subfr, &t0, &t0_frac);
       old_t0 = t0;
     }
     else
     {
       t0  =  old_t0;
       t0_frac = 0;
       old_t0++;
       if( old_t0 >PIT_MAX) {
           old_t0 = PIT_MAX;
       }
     }
   }


   /*-------------------------------------------------*
    *  - Find the adaptive codebook vector.            *
    *--------------------------------------------------*/

   pred_lt_3(&exc[i_subfr], t0, t0_frac, L_SUBFR);

   /*-------------------------------------------------------*
    * - Decode innovative codebook.                         *
    * - Add the fixed-gain pitch contribution to code[].    *
    *-------------------------------------------------------*/

   if(bfi != 0) {            /* Bad Frame Error Concealment */
     parm[0] = (int) (random_g729() & 0x1fff);      /* 13 bits random*/
     parm[1]= (int) (random_g729() & 0x000f);      /*  4 bits random */
   }

   decod_ACELP(parm[1], parm[0], code);
   parm +=2;
   for (i = t0; i < L_SUBFR; i++)   code[i] += sharp * code[i-t0];

   /*-------------------------------------------------*
    * - Decode pitch and codebook gains.              *
    *-------------------------------------------------*/

   index = *parm++;          /* index of energy VQ */
   dec_gain(index, code, L_SUBFR, bfi, &gain_pitch, &gain_code);

   /*-------------------------------------------------------------*
    * - Update pitch sharpening "sharp" with quantized gain_pitch *
    *-------------------------------------------------------------*/

   sharp = gain_pitch;
   if (sharp > SHARPMAX) sharp = SHARPMAX;
   if (sharp < SHARPMIN) sharp = SHARPMIN;

   /*-------------------------------------------------------*
    * - Find the total excitation.                          *
    *-------------------------------------------------------*/

   if(bfi != 0 ) {
     if(voicing  == 0) {     /* for unvoiced frame */
         for (i = 0; i < L_SUBFR;  i++) {
            exc[i+i_subfr] = gain_code*code[i];
         }
      } else {               /* for voiced frame */
         for (i = 0; i < L_SUBFR;  i++) {
            exc[i+i_subfr] = gain_pitch*exc[i+i_subfr];
         }
      }
    } else {                  /* No frame errors */
      for (i = 0; i < L_SUBFR;  i++) {
         exc[i+i_subfr] = gain_pitch*exc[i+i_subfr] + gain_code*code[i];
      }
    }

    /*-------------------------------------------------------*
     * - Find synthesis speech corresponding to exc[].       *
     *-------------------------------------------------------*/

    syn_filt(Az, &exc[i_subfr], &synth[i_subfr], L_SUBFR, mem_syn, 1);

    Az  += MP1;        /* interpolated LPC parameters for next subframe */
  }

   /*--------------------------------------------------*
    * Update signal for next frame.                    *
    * -> shift to the left by L_FRAME  exc[]           *
    *--------------------------------------------------*/
  copy(&old_exc[L_FRAME], &old_exc[0], PIT_MAX+L_INTERPOL);

   return;
}
Example #3
0
void coder_ld8a(

				int ana[]                   /* output: analysis parameters */

)

{

	/* LPC coefficients */



	FLOAT Aq_t[(MP1)*2];         /* A(z) quantized for the 2 subframes   */

	FLOAT Ap_t[(MP1)*2];         /* A(z) with spectral expansion         */

	FLOAT *Aq, *Ap;              /* Pointer on Aq_t  and Ap_t            */



	/* Other vectors */



	FLOAT h1[L_SUBFR];           /* Impulse response h1[]              */

	FLOAT xn[L_SUBFR];           /* Target vector for pitch search     */

	FLOAT xn2[L_SUBFR];          /* Target vector for codebook search  */

	FLOAT code[L_SUBFR];         /* Fixed codebook excitation          */

	FLOAT y1[L_SUBFR];           /* Filtered adaptive excitation       */

	FLOAT y2[L_SUBFR];           /* Filtered fixed codebook excitation */

	FLOAT g_coeff[5];            /* Correlations between xn, y1, & y2:

								 <y1,y1>, <xn,y1>, <y2,y2>, <xn,y2>,<y1,y2>*/



	/* Scalars */



	int i, j, i_subfr;

	int T_op, T0, T0_min, T0_max, T0_frac;

	int index;

	FLOAT   gain_pit, gain_code;

	int     taming;



	/*------------------------------------------------------------------------*

	*  - 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)                     *

	*------------------------------------------------------------------------*/

	{

		/* Temporary vectors */

		FLOAT r[MP1];                    /* Autocorrelations       */

		FLOAT rc[M];                     /* Reflexion coefficients */

		FLOAT lsp_new[M];                /* lsp coefficients       */

		FLOAT lsp_new_q[M];              /* Quantized lsp coeff.   */



		/* LP analysis */



		autocorr(p_window, M, r);             /* Autocorrelations */

		lag_window(M, r);                     /* Lag windowing    */

		levinson(r, Ap_t, rc);                /* Levinson Durbin  */

		az_lsp(Ap_t, lsp_new, lsp_old);       /* Convert A(z) to lsp */



		/* LSP quantization */



		qua_lsp(lsp_new, lsp_new_q, ana);

		ana += 2;                        /* Advance analysis parameters pointer */



		/*--------------------------------------------------------------------*

		* Find interpolated LPC parameters in all subframes                  *

		* The interpolated parameters are in array Aq_t[].                   *

		*--------------------------------------------------------------------*/



		int_qlpc(lsp_old_q, lsp_new_q, Aq_t);



		/* Compute A(z/gamma) */



		weight_az(&Aq_t[0],   GAMMA1, M, &Ap_t[0]);

		weight_az(&Aq_t[MP1], GAMMA1, M, &Ap_t[MP1]);



		/* update the LSPs for the next frame */



		copy(lsp_new,   lsp_old,   M);

		copy(lsp_new_q, lsp_old_q, M);

	}



	/*----------------------------------------------------------------------*

	* - Find the weighted input speech w_sp[] for the whole speech frame   *

	* - Find the open-loop pitch delay for the whole speech frame          *

	* - Set the range for searching closed-loop pitch in 1st subframe      *

	*----------------------------------------------------------------------*/



	residu(&Aq_t[0],   &speech[0],       &exc[0],       L_SUBFR);

	residu(&Aq_t[MP1], &speech[L_SUBFR], &exc[L_SUBFR], L_SUBFR);



	{

		FLOAT Ap1[MP1];



		Ap = Ap_t;

		Ap1[0] = (F)1.0;

		for(i=1; i<=M; i++)

			Ap1[i] = Ap[i] - (F)0.7 * Ap[i-1];

		syn_filt(Ap1, &exc[0], &wsp[0], L_SUBFR, mem_w, 1);



		Ap += MP1;

		for(i=1; i<=M; i++)

			Ap1[i] = Ap[i] - (F)0.7 * Ap[i-1];

		syn_filt(Ap1, &exc[L_SUBFR], &wsp[L_SUBFR], L_SUBFR, mem_w, 1);

	}





	/* Find open loop pitch lag for whole speech frame */



	T_op = pitch_ol_fast(wsp, L_FRAME);



	/* Range for closed loop pitch search in 1st subframe */



	T0_min = T_op - 3;

	if (T0_min < PIT_MIN) T0_min = PIT_MIN;

	T0_max = T0_min + 6;

	if (T0_max > PIT_MAX)

	{

		T0_max = PIT_MAX;

		T0_min = T0_max - 6;

	}



	/*------------------------------------------------------------------------*

	*          Loop for every subframe in the analysis frame                 *

	*------------------------------------------------------------------------*

	*  To find the pitch and innovation parameters. The subframe size is     *

	*  L_SUBFR and the loop is repeated L_FRAME/L_SUBFR times.               *

	*     - find the weighted LPC coefficients                               *

	*     - find the LPC residual signal                                     *

	*     - compute the target signal for pitch search                       *

	*     - compute impulse response of weighted synthesis filter (h1[])     *

	*     - find the closed-loop pitch parameters                            *

	*     - encode the pitch delay                                           *

	*     - find target vector for codebook search                           *

	*     - codebook search                                                  *

	*     - VQ of pitch and codebook gains                                   *

	*     - update states of weighting filter                                *

	*------------------------------------------------------------------------*/



	Aq = Aq_t;    /* pointer to interpolated quantized LPC parameters */

	Ap = Ap_t;    /* pointer to weighted LPC coefficients             */



	for (i_subfr = 0;  i_subfr < L_FRAME; i_subfr += L_SUBFR)

	{

		/*---------------------------------------------------------------*

		* Compute impulse response, h1[], of weighted synthesis filter  *

		*---------------------------------------------------------------*/



		h1[0] = (F)1.0;

		set_zero(&h1[1], L_SUBFR-1);

		syn_filt(Ap, h1, h1, L_SUBFR, &h1[1], 0);



		/*-----------------------------------------------*

		* Find the target vector for pitch search:      *

		*----------------------------------------------*/



		syn_filt(Ap, &exc[i_subfr], xn, L_SUBFR, mem_w0, 0);



		/*-----------------------------------------------------------------*

		*    Closed-loop fractional pitch search                          *

		*-----------------------------------------------------------------*/



		T0 = pitch_fr3_fast(&exc[i_subfr], xn, h1, L_SUBFR, T0_min, T0_max,

			i_subfr, &T0_frac);



		index = enc_lag3(T0, T0_frac, &T0_min, &T0_max, PIT_MIN, PIT_MAX,

			i_subfr);



		*ana++ = index;



		if (i_subfr == 0)

			*ana++ = parity_pitch(index);



		/*-----------------------------------------------------------------*

		*   - find filtered pitch exc                                     *

		*   - compute pitch gain and limit between 0 and 1.2              *

		*   - update target vector for codebook search                    *

		*   - find LTP residual.                                          *

		*-----------------------------------------------------------------*/



		syn_filt(Ap, &exc[i_subfr], y1, L_SUBFR, mem_zero, 0);



		gain_pit = g_pitch(xn, y1, g_coeff, L_SUBFR);



		/* clip pitch gain if taming is necessary */



		taming = test_err(T0, T0_frac);



		if( taming == 1){

			if (gain_pit > GPCLIP) {

				gain_pit = GPCLIP;

			}

		}



		for (i = 0; i < L_SUBFR; i++)

			xn2[i] = xn[i] - y1[i]*gain_pit;



		/*-----------------------------------------------------*

		* - Innovative codebook search.                       *

		*-----------------------------------------------------*/



		index = ACELP_code_A(xn2, h1, T0, sharp, code, y2, &i);



		*ana++ = index;           /* Positions index */

		*ana++ = i;               /* Signs index     */



		/*------------------------------------------------------*

		*  - Compute the correlations <y2,y2>, <xn,y2>, <y1,y2>*

		*  - Vector quantize gains.                            *

		*------------------------------------------------------*/



		corr_xy2(xn, y1, y2, g_coeff);

		*ana++ =qua_gain(code, g_coeff, L_SUBFR, &gain_pit, &gain_code,

			taming);



		/*------------------------------------------------------------*

		* - Update pitch sharpening "sharp" with quantized gain_pit  *

		*------------------------------------------------------------*/



		sharp = gain_pit;

		if (sharp > SHARPMAX) sharp = SHARPMAX;

		if (sharp < SHARPMIN) sharp = SHARPMIN;



		/*------------------------------------------------------*

		* - Find the total excitation                          *

		* - update filters' memories for finding the target    *

		*   vector in the next subframe  (mem_w0[])            *

		*------------------------------------------------------*/



		for (i = 0; i < L_SUBFR;  i++)

			exc[i+i_subfr] = gain_pit*exc[i+i_subfr] + gain_code*code[i];



		update_exc_err(gain_pit, T0);



		for (i = L_SUBFR-M, j = 0; i < L_SUBFR; i++, j++)

			mem_w0[j]  = xn[i] - gain_pit*y1[i] - gain_code*y2[i];



		Aq += MP1;           /* interpolated LPC parameters for next subframe */

		Ap += MP1;

	}



	/*--------------------------------------------------*

	* Update signal for next frame.                    *

	* -> shift to the left by L_FRAME:                 *

	*     speech[], wsp[] and  exc[]                   *

	*--------------------------------------------------*/



	copy(&old_speech[L_FRAME], &old_speech[0], L_TOTAL-L_FRAME);

	copy(&old_wsp[L_FRAME], &old_wsp[0], PIT_MAX);

	copy(&old_exc[L_FRAME], &old_exc[0], PIT_MAX+L_INTERPOL);



	return;

}