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

📄 sp_dec.c

📁 FLOAT PINT
💻 C
📖 第 1 页 / 共 4 页
字号:

   /* (Q14 * Q11 -> Q26) + Q26 -> Q26 */
   log_en_int += ( int_fac * st->old_log_en ) << 1;

   for ( i = 0; i < M; i++ ) {
      /* Q14 + (Q14 * Q15 -> Q14) -> Q14 */
      lsp_int[i] = lsp_int[i] + ( ( int_fac * st->lsp_old[i] ) >> 15 );

      /* Q14 -> Q15 */
      lsp_int[i] = lsp_int[i] << 1;
   }

   /* compute the amount of lsf variability */
   /* -0.6 in Q12 */
   lsf_variab_factor = st->log_pg_mean - 2457;

   /* *0.3 Q12*Q15 -> Q12 */
   lsf_variab_factor = 4096 - ( ( lsf_variab_factor * 9830 ) >> 15 );

   /* limit to values between 0..1 in Q12 */
   if ( lsf_variab_factor >= 4096 ) {
      lsf_variab_factor = 32767;
   }
   else if ( lsf_variab_factor < 0 ) {
      lsf_variab_factor = 0;
   }
   else
      lsf_variab_factor = lsf_variab_factor << 3;   /* -> Q15 */

   /* get index of vector to do variability with */
   lsf_variab_index = pseudonoise( &st->pn_seed_rx, 3 );

   /* convert to lsf */
   Lsp_lsf( lsp_int, lsf_int );

   /* apply lsf variability */
   memcpy( lsf_int_variab, lsf_int, M <<2 );

   for ( i = 0; i < M; i++ ) {
      lsf_int_variab[i] = lsf_int_variab[i] + ( ( lsf_variab_factor * st->
            lsf_hist_mean[i + lsf_variab_index * M] ) >> 15 );
   }

   /* make sure that LSP's are ordered */
   Reorder_lsf( lsf_int, LSF_GAP );
   Reorder_lsf( lsf_int_variab, LSF_GAP );

   /* copy lsf to speech decoders lsf state */
   memcpy( lsfState->past_lsf_q, lsf_int, M <<2 );

   /* convert to lsp */
   Lsf_lsp( lsf_int, lsp_int );
   Lsf_lsp( lsf_int_variab, lsp_int_variab );

     /* Compute acoeffs Q12 acoeff is used for level
      * normalization and Post_Filter, acoeff_variab is
      * used for synthesis filter
      * by doing this we make sure that the level
      * in high frequenncies does not jump up and down
      */
   Lsp_Az( lsp_int, acoeff );
   Lsp_Az( lsp_int_variab, acoeff_variab );

   /* For use in Post_Filter */
   memcpy( &A_t[0], acoeff, MP1 <<2 );
   memcpy( &A_t[MP1], acoeff, MP1 <<2 );
   memcpy( &A_t[MP1 <<1], acoeff, MP1 <<2 );
   memcpy( &A_t[MP1 + MP1 + MP1], acoeff, MP1 <<2 );

   /* Compute reflection coefficients Q15 */
   A_Refl( &acoeff[1], refl );

   /* Compute prediction error in Q15 */
   /* 0.99997 in Q15 */
   pred_err = MAX_16;

   for ( i = 0; i < M; i++ ) {
      pred_err = ( pred_err * ( MAX_16 - ( ( refl[i] * refl[i] ) >> 15 ) ) ) >>
            15;
   }

   /* compute logarithm of prediction gain */
   Log2( pred_err, &log_pg_e, &log_pg_m );

   /* convert exponent and mantissa to Word16 Q12 */
   /* Q12 */
   log_pg = ( log_pg_e - 15 ) << 12;
   /* saturate */
   if (log_pg < -32768) {
      log_pg = -32768;
   }
   log_pg = ( -( log_pg + ( log_pg_m >> 3 ) ) ) >> 1;
   st->log_pg_mean = ( Word16 )( ( ( 29491*st->log_pg_mean ) >> 15 ) + ( ( 3277
         * log_pg ) >> 15 ) );

   /* Compute interpolated log energy */
   /* Q26 -> Q16 */
   log_en_int = log_en_int >> 10;

   /* Add 4 in Q16 */
   log_en_int += 262144L;

   /* subtract prediction gain */
   log_en_int = log_en_int - ( log_pg << 4 );

   /* adjust level to speech coder mode */
   log_en_int += st->log_en_adjust << 5;
   log_en_int_e = ( Word16 )( log_en_int >> 16 );
   log_en_int_m = ( Word16 )( ( log_en_int - ( log_en_int_e << 16 ) ) >> 1 );

   /* Q4 */
   level = ( Word16 )( Pow2( log_en_int_e, log_en_int_m ) );

   for ( i = 0; i < 4; i++ ) {
      /* Compute innovation vector */
      Build_CN_code( &st->pn_seed_rx, ex );

      for ( j = 0; j < L_SUBFR; j++ ) {
         ex[j] = ( level * ex[j] ) >> 15;
      }

      /* Synthesize */
      Syn_filt( acoeff_variab, ex, &synth[i * L_SUBFR], L_SUBFR, mem_syn, 1 );
   }   /* next i */

   /* reset codebook averaging variables */
   averState->hangVar = 20;
   averState->hangCount = 0;

   if ( new_state == DTX_MUTE ) {
        /*
         * mute comfort noise as it has been quite a long time since
         * last SID update  was performed
         */
      Word32 num, denom;


      tmp_int_length = st->since_last_sid;

      if ( tmp_int_length > 32 ) {
         tmp_int_length = 32;
      }

      if ( tmp_int_length == 1 ) {
         st->true_sid_period_inv = MAX_16;
      }
      else {
         num = 1024;
         denom = ( tmp_int_length << 10 );
         st->true_sid_period_inv = 0;

         for ( i = 0; i < 15; i++ ) {
            st->true_sid_period_inv <<= 1;
            num <<= 1;

            if ( num >= denom ) {
               num = num - denom;
               st->true_sid_period_inv += 1;
            }
         }
      }
      st->since_last_sid = 0;
      memcpy( st->lsp_old, st->lsp, M << 2 );
      st->old_log_en = st->log_en;

      /* subtract 1/8 in Q11 i.e -6/8 dB */
      st->log_en = st->log_en - 256;
      if (st->log_en < -32768) st->log_en = -32768;
   }

     /*
      * reset interpolation length timer
      * if data has been updated.
      */
   if ( ( st->sid_frame != 0 ) & ( ( st->valid_data != 0 ) || ( ( st->valid_data
         == 0 ) & ( st->dtxHangoverAdded != 0 ) ) ) ) {
      st->since_last_sid = 0;
      st->data_updated = 1;
   }
   return;
}


/*
 * lsp_avg
 *
 *
 * Parameters:
 *    st->lsp_meanSave  B: LSP averages
 *    lsp               I: LSPs
 *
 * Function:
 *    Calculate the LSP averages
 *
 * Returns:
 *    void
 */
static void lsp_avg( lsp_avgState *st, Word32 *lsp )
{
   Word32 i, tmp;


   for ( i = 0; i < M; i++ ) {
      /* mean = 0.84*mean */
      tmp = ( st->lsp_meanSave[i] << 16 );
      tmp -= ( EXPCONST * st->lsp_meanSave[i] ) << 1;

      /* Add 0.16 of newest LSPs to mean */
      tmp += ( EXPCONST * lsp[i] ) << 1;

      /* Save means */
      tmp += 0x00008000L;
      st->lsp_meanSave[i] = tmp >> 16;
   }
   return;
}


/*
 * Int_lpc_1and3
 *
 *
 * Parameters:
 *    lsp_old        I: LSP vector at the 4th subfr. of past frame      [M]
 *    lsp_mid        I: LSP vector at the 2nd subframe of present frame [M]
 *    lsp_new        I: LSP vector at the 4th subframe of present frame [M]
 *    Az             O: interpolated LP parameters in subframes 1 and 3
 *                                                                   [AZ_SIZE]
 *
 * Function:
 *    Interpolates the LSPs and converts to LPC parameters
 *    to get a different LP filter in each subframe.
 *
 *    The 20 ms speech frame is divided into 4 subframes.
 *    The LSPs are quantized and transmitted at the 2nd and
 *    4th subframes (twice per frame) and interpolated at the
 *    1st and 3rd subframe.
 *
 * Returns:
 *    void
 */
static void Int_lpc_1and3( Word32 lsp_old[], Word32 lsp_mid[], Word32 lsp_new[],
      Word32 Az[] )
{
   Word32 lsp[M];
   Word32 i;


   /* lsp[i] = lsp_mid[i] * 0.5 + lsp_old[i] * 0.5 */
   for ( i = 0; i < 10; i++ ) {
      lsp[i] = ( lsp_mid[i] >> 1 ) + ( lsp_old[i] >> 1 );
   }

   /* Subframe 1 */
   Lsp_Az( lsp, Az );
   Az += MP1;

   /* Subframe 2 */
   Lsp_Az( lsp_mid, Az );
   Az += MP1;

   for ( i = 0; i < 10; i++ ) {
      lsp[i] = ( lsp_mid[i] >> 1 ) + ( lsp_new[i] >> 1 );
   }

   /* Subframe 3 */
   Lsp_Az( lsp, Az );
   Az += MP1;

   /* Subframe 4 */
   Lsp_Az( lsp_new, Az );
   return;
}


/*
 * Int_lpc_1to3
 *
 *
 * Parameters:
 *    lsp_old           I: LSP vector at the 4th subframe of past frame    [M]
 *    lsp_new           I: LSP vector at the 4th subframe of present frame [M]
 *    Az                O: interpolated LP parameters in all subframes
 *                                                                   [AZ_SIZE]
 *
 * Function:
 *    Interpolates the LSPs and converts to LPC parameters to get a different
 *    LP filter in each subframe.
 *
 *    The 20 ms speech frame is divided into 4 subframes.
 *    The LSPs are quantized and transmitted at the 4th
 *    subframes (once per frame) and interpolated at the
 *    1st, 2nd and 3rd subframe.
 *
 * Returns:
 *    void
 */
static void Int_lpc_1to3( Word32 lsp_old[], Word32 lsp_new[], Word32 Az[] )
{
   Word32 lsp[M];
   Word32 i;


   for ( i = 0; i < 10; i++ ) {
      lsp[i] = ( lsp_new[i] >> 2 ) + ( lsp_old[i] - ( lsp_old[i] >> 2 ) );
   }

   /* Subframe 1 */
   Lsp_Az( lsp, Az );
   Az += MP1;

   for ( i = 0; i < 10; i++ ) {
      lsp[i] = ( lsp_old[i] >> 1 ) + ( lsp_new[i] >> 1 );
   }

   /* Subframe 2 */
   Lsp_Az( lsp, Az );
   Az += MP1;

   for ( i = 0; i < 10; i++ ) {
      lsp[i] = ( lsp_old[i] >> 2 ) + ( lsp_new[i] - ( lsp_new[i] >> 2 ) );
   }

   /* Subframe 3 */
   Lsp_Az( lsp, Az );
   Az += MP1;

   /* Subframe 4 */
   Lsp_Az( lsp_new, Az );
   return;
}


/*
 * D_plsf_5
 *
 *
 * Parameters:
 *    st->past_lsf_q I: Past dequantized LFSs
 *    st->past_r_q      B: past quantized residual
 *    bfi               B: bad frame indicator
 *    indice            I: quantization indices of 3 submatrices, Q0
 *    lsp1_q            O: quantized 1st LSP vector
 *    lsp2_q            O: quantized 2nd LSP vector
 *
 * Function:
 *    Decodes the 2 sets of LSP parameters in a frame
 *    using the received quantization indices.
 *
 * Returns:
 *    void
 */
static void D_plsf_5( D_plsfState *st, Word16 bfi, Word16 *indice, Word32 *lsp1_q
      , Word32 *lsp2_q )
{
   Word32 lsf1_r[M], lsf2_r[M], lsf1_q[M], lsf2_q[M];
   Word32 i, temp1, temp2, sign;
   const Word32 *p_dico;


   /* if bad frame */
   if ( bfi != 0 ) {
      /* use the past LSFs slightly shifted towards their mean */
      for ( i = 0; i < M; i += 2 ) {
         /* lsfi_q[i] = ALPHA*st->past_lsf_q[i] + ONE_ALPHA*meanLsf[i]; */
         lsf1_q[i] = ( ( st->past_lsf_q[i] * ALPHA_122 ) >> 15 ) + ( ( mean_lsf_5[i]
               * ONE_ALPHA_122 ) >> 15 );
         lsf1_q[i + 1] = ( ( st->past_lsf_q[i + 1] * ALPHA_122 ) >> 15 ) + ( (
               mean_lsf_5[i + 1] * ONE_ALPHA_122 ) >> 15 );
      }
      memcpy( lsf2_q, lsf1_q, M <<2 );

      /* estimate past quantized residual to be used in next frame */
      for ( i = 0; i < M; i += 2 ) {
         /* temp  = meanLsf[i] +  st->past_r_q[i] * LSPPpred_facMR122; */
         temp1 = mean_lsf_5[i] + ( ( st->past_r_q[i] * LSP_PRED_FAC_MR122 ) >>
               15 );
         temp2 = mean_lsf_5[i + 1] +( ( st->past_r_q[i + 1] *LSP_PRED_FAC_MR122
               ) >> 15 );
         st->past_r_q[i] = lsf2_q[i] - temp1;
         st->past_r_q[i + 1] = lsf2_q[i + 1] -temp2;
      }
   }

   /* if good LSFs received */
   else {
      /* decode prediction residuals from 5 received indices */
      p_dico = &dico1_lsf_5[indice[0] << 2];
      lsf1_r[0] = *p_dico++;
      lsf1_r[1] = *p_dico++;
      lsf2_r[0] = *p_dico++;
      lsf2_r[1] = *p_dico++;
      p_dico = &dico2_lsf_5[indice[1] << 2];
      lsf1_r[2] = *p_dico++;
      lsf1_r[3] = *p_dico++;
      lsf2_r[2] = *p_dico++;
      lsf2_r[3] = *p_dico++;
      sign = ( Word16 )( indice[2] & 1 );
      i = indice[2] >> 1;
      p_dico = &dico3_lsf_5[i << 2];

      if ( sign == 0 ) {
         lsf1_r[4] = *p_dico++;
         lsf1_r[5] = *p_dico++;
         lsf2_r[4] = *p_dico++;
         lsf2_r[5] = *p_dico++;
      }
      else {
         lsf1_r[4] = ( Word16 )( -( *p_dico++ ) );
         lsf1_r[5] = ( Word16 )( -( *p_dico++ ) );
         lsf2_r[4] = ( Word16 )( -( *p_dico++ ) );
         lsf2_r[5] = ( Word16 )( -( *p_dico++ ) );
      }
      p_dico = &dico4_lsf_5[( indice[3]<<2 )];
      lsf1_r[6] = *p_dico++;
      lsf1_r[7] = *p_dico++;
      lsf2_r[6] = *p_dico++;
      lsf2_r[7] = *p_dico++;
      p_dico = &dico5_lsf_5[( indice[4]<<2 )];
      lsf1_r[8] = *p_dico++;
      lsf1_r[9] = *p_dico++;
      lsf2_r[8] = *p_dico++;
      lsf2_r[9] = *p_dico++;

      /* Compute quantized LSFs and update the past quantized residual */
      for ( i = 0; i < M; i++ ) {
         temp1 = mean_lsf_5[i] + ( ( st->past_r_q[i] * LSP_PRED_FAC_MR122 ) >>
               15 );
         lsf1_q[i] = lsf1_r[i] + temp1;
         lsf2_q[i] = lsf2_r[i] + temp1;
         st->past_r_q[i] = lsf2_r[i];
      }
   }

   /* verification that LSFs have minimum distance of LSF_GAP Hz */
   Reorder_lsf( lsf1_q, LSF_GAP );
   Reorder_lsf( lsf2_q, LSF_GAP );
   memcpy( st->past_lsf_q, lsf2_q, M <<2 );

   /*  convert LSFs to the cosine domain */
   Lsf_lsp( lsf1_q, lsp1_q );
   Lsf_lsp( lsf2_q, lsp2_q );
   return;
}


/*
 * Dec_lag3
 *
 *
 * Parameters:
 *    index             I: received pitch index
 *    t0_min            I: minimum of search range
 *    t0_max            I: maximum of search range
 *    i_subfr           I: subframe flag
 *    T0_prev           I: integer pitch delay of last subframe used
 *                         in 2nd and 4th subframes
 *    T0                O: integer part of pitch lag
 *    T0_frac           O : fractional part of pitch lag
 *    flag4             I : flag for encoding with 4 bits
 * Function:
 *    Decoding of fractional pitch lag with 1/3 resolution.
 *    Extract the integer and fraction parts of the pitch lag from
 *    the received adaptive codebook index.
 *
 *    The fractional lag in 1st and 3rd subframes is encoded with 8 bits
 *    while that in 2nd and 4th subframes is relatively encoded with 4, 5
 *    and 6 bits depending on the mode.
 *
 * Returns:
 *    void
 */
static void Dec_lag3( Word32 index, Word32 t0_min, Word32 t0_max, Word32 i_subfr
      , Word32 T0_prev, Word32 *T0, Word32 *T0_frac, Word32 flag4 )
{
   Word32 i, tmp_lag;


   /* if 1st or 3rd subframe */
   if ( i_subfr == 0 ) {
      if ( index < 197 ) {
         *T0 = ( ( ( index + 2 ) * 10923 ) >> 15 ) + 19;
         i = *T0 + *T0 + *T0;
         *T0_frac = ( index - i ) + 58;
      }
      else {
         *T0 = index - 112;
         *T0_frac = 0;
      }
   }

   /* 2nd or 4th subframe */
   else {
      if ( flag4 == 0 ) {
         /* 'normal' decoding: either with 5 or 6 bit resolution */
         i = ( ( ( index + 2 ) * 10923 ) >> 15 ) - 1;
         *T0 = i + t0_min;
         i = i + i + i;
         *T0_frac = ( index - 2 ) - i;
      }
      else {
         /* decoding with 4 bit resolution */
         tmp_lag = T0_prev;

         if ( ( tmp_lag - t0_min ) > 5 )
            tmp_lag = t0_min + 5;

         if ( ( t0_max - tmp_lag ) > 4 )
            tmp_lag = t0_max - 4;

         if ( index < 4 ) {
            i = ( tmp_lag - 5 );
            *T0 = i + index;
            *T0_frac = 0;
         }
         else {
            if ( index < 12 ) {
               i = ( ( ( index - 5 ) * 10923 ) >> 15 ) - 1;
               *T0 = i + tmp_lag;
               i = i + i + i;
               *T0_frac = ( index - 9 ) - i;
            }
            else {
               i = ( index - 12 ) + tmp_lag;
               *T0 = i + 1;
               *T0_frac = 0;
            }
         }
      }   /* end if (decoding with 4 bit resolution) */
   }
   return;
}


/*
 * Pred_lt_3or6_40
 *
 *
 * Parameters:
 *    exc               B: excitation buffer
 *    T0                I: integer pitch lag
 *    frac              I: fraction of lag
 *    flag3             I: if set, upsampling rate = 3 (6 otherwise)
 *
 * Function:
 *    Compute the result of long term prediction with fractional
 *    interpolation of resolution 1/3 or 1/6. (Interpolated past excitation).
 *
 *    Once the fractional pitch lag is determined,
 *    the adaptive codebook vector v(n) is computed by interpolating
 *    the past excitation signal u(n) at the given integer delay k
 *    and phase (fraction)  :
 *
 *          9                       9
 *    v(n) = SUM[ u(n-k-i) * b60(t+i*6) ] + SUM[ u(n-k+1+i) * b60(6-t+i*6) ],
 *          i=0                       i=0
 *    n = 0, ...,39, t = 0, ...,5.
 *
 *    The interpolation filter b60 is based on a Hamming windowed sin(x)/x
 *    function truncated at 

⌨️ 快捷键说明

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