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

📄 exc_lbc.c

📁 一个非常好用的国际标准G。726算法
💻 C
📖 第 1 页 / 共 5 页
字号:
                                    Buff[PitchMax+(int)Sfc*SubFrLen+j] ) ;
    Lcr[0] = Acc0 ;

    if ( Bindx != (Word16) 0 ) {
        Acc0 = (Word32) 0 ;
        Acc1 = (Word32) 0 ;
        for ( j = 0 ; j < SubFrLen ; j ++ ) {
            Acc0 = L_mac( Acc0, Buff[PitchMax+(int)Sfc*SubFrLen+j],
                        Buff[PitchMax+(int)Sfc*SubFrLen+(int)Bindx+j] ) ;
            Acc1 = L_mac( Acc1, Buff[PitchMax+(int)Sfc*SubFrLen+(int)Bindx+j],
                        Buff[PitchMax+(int)Sfc*SubFrLen+(int)Bindx+j] ) ;
        }
        Lcr[1] = Acc0 ;
        Lcr[2] = Acc1 ;
    }
    else {
        Lcr[1] = (Word32) 0 ;
        Lcr[2] = (Word32) 0 ;
    }

    if ( Findx != (Word16) 0 ) {
        Acc0 = (Word32) 0 ;
        Acc1 = (Word32) 0 ;
        for ( j = 0 ; j < SubFrLen ; j ++ ) {
            Acc0 = L_mac( Acc0, Buff[PitchMax+(int)Sfc*SubFrLen+j],
                        Buff[PitchMax+(int)Sfc*SubFrLen+(int)Findx+j] ) ;
            Acc1 = L_mac( Acc1, Buff[PitchMax+(int)Sfc*SubFrLen+(int)Findx+j],
                        Buff[PitchMax+(int)Sfc*SubFrLen+(int)Findx+j] ) ;
        }
        Lcr[3] = Acc0 ;
        Lcr[4] = Acc1 ;
    }
    else {
        Lcr[3] = (Word32) 0 ;
        Lcr[4] = (Word32) 0 ;
    }

    /* Normalize and convert to shorts */
    Acc1 = 0L ;
    for ( i = 0 ; i < 5 ; i ++ ) {
        Acc0 = Lcr[i] ;
        if ( Acc0 > Acc1 )
            Acc1 = Acc0 ;
    }

    Exp = norm_l( Acc1 ) ;
    for ( i = 0 ; i < 5 ; i ++ ) {
        Acc0 = L_shl( Lcr[i], Exp ) ;
        Scr[i] = extract_h( Acc0 ) ;
    }

    /* Select the best pair */
    if ( (Bindx != (Word16) 0) && ( Findx == (Word16) 0) )
        Pf = Get_Ind( Bindx, Scr[0], Scr[1], Scr[2] ) ;

    if ( (Bindx == (Word16) 0) && ( Findx != (Word16) 0) )
        Pf = Get_Ind( Findx, Scr[0], Scr[3], Scr[4] ) ;

    if ( (Bindx != (Word16) 0) && ( Findx != (Word16) 0) ) {
        Exp = mult_r( Scr[1], Scr[1] ) ;
        Acc0 = L_mult( Exp, Scr[4] ) ;
        Exp = mult_r( Scr[3], Scr[3] ) ;
        Acc1 = L_mult( Exp, Scr[2] ) ;
        if ( Acc0 > Acc1 )
            Pf = Get_Ind( Bindx, Scr[0], Scr[1], Scr[2] ) ;
        else
            Pf = Get_Ind( Findx, Scr[0], Scr[3], Scr[4] ) ;
    }

    return Pf ;
}

/*
**
** Function:        Find_B()
**
** Description:     Computes best pitch postfilter backward lag by
**                  backward cross correlation maximization around the
**                  decoded pitch lag
**                  of the subframe 0 (for subframes 0 & 1)
**                  of the subframe 2 (for subframes 2 & 3)
**
** Links to text:   Section 3.6
**
** Arguments:
**
**  Word16 *Buff    decoded excitation
**  Word16 Olp      Decoded pitch lag
**  Word16 Sfc      Subframe index
**
** Outputs:     None
**
** Return value:
**
**  Word16   Pitch postfilter backward lag
*/
Word16   Find_B( Word16 *Buff, Word16 Olp, Word16 Sfc )
{
    int   i,j   ;

    Word16   Indx = 0 ;

    Word32   Acc0,Acc1 ;

    if ( Olp > (Word16) (PitchMax-3) )
        Olp = (Word16) (PitchMax-3) ;

    Acc1 = (Word32) 0 ;

    for ( i = (int)Olp-3 ; i <= (int)Olp+3 ; i ++ ) {

        Acc0 = (Word32) 0 ;
        for ( j = 0 ; j < SubFrLen ; j ++ )
            Acc0 = L_mac( Acc0, Buff[PitchMax+(int)Sfc*SubFrLen+j],
                                    Buff[PitchMax+(int)Sfc*SubFrLen-i+j] ) ;
        if ( Acc0 > Acc1 ) {
            Acc1 = Acc0 ;
            Indx = -(Word16) i ;
        }
    }
    return Indx ;
}

/*
**
** Function:        Find_F()
**
** Description:     Computes best pitch postfilter forward lag by
**                  forward cross correlation maximization around the
**                  decoded pitch lag
**                  of the subframe 0 (for subframes 0 & 1)
**                  of the subframe 2 (for subframes 2 & 3)
**
** Links to text:   Section 3.6
**
** Arguments:
**
**  Word16 *Buff    decoded excitation
**  Word16 Olp      Decoded pitch lag
**  Word16 Sfc      Subframe index
**
** Outputs:     None
**
** Return value:
**
**  Word16    Pitch postfilter forward lag
*/
Word16   Find_F( Word16 *Buff, Word16 Olp, Word16 Sfc )
{
    int   i,j   ;

    Word16   Indx = 0 ;

    Word32   Acc0,Acc1 ;

    if ( Olp > (Word16) (PitchMax-3) )
        Olp = (Word16) (PitchMax-3) ;

    Acc1 = (Word32) 0 ;

    for ( i = Olp-3 ; i <= Olp+3 ; i ++ ) {

        Acc0 = (Word32) 0 ;
        if ( ((int)Sfc*SubFrLen+SubFrLen+i) <= Frame ) {
            for ( j = 0 ; j < SubFrLen ; j ++ )
                Acc0 = L_mac( Acc0, Buff[PitchMax+(int)Sfc*SubFrLen+j],
                            Buff[PitchMax+(int)Sfc*SubFrLen+i+j] ) ;
        }


        if ( Acc0 > Acc1 ) {
            Acc1 = Acc0 ;
            Indx = (Word16) i ;
        }
    }

    return Indx ;
}

/*
**
** Function:        Get_Ind()
**
** Description:     Computes gains of the pitch postfilter.
**                  The gains are calculated using the cross correlation
**                  (forward or backward, the one with the greatest contribution)
**                  and the energy of the signal. Also, a test is performed on
**                  the prediction gain to see whether the pitch postfilter
**                  should be used or not.
**
**
**
** Links to text:   Section 3.6
**
** Arguments:
**
**  Word16 Ind      Pitch postfilter lag
**  Word16 Ten      energy of the current subframe excitation vector
**  Word16 Ccr      Crosscorrelation of the excitation
**  Word16 Enr      Energy of the (backward or forward) "delayed" excitation
**
** Outputs:     None
**
** Return value:
**
**  PFDEF
**         Word16   Indx    Pitch postfilter lag
**         Word16   Gain    Pitch postfilter gain
**         Word16   ScGn    Pitch postfilter scaling gain
**
*/
PFDEF Get_Ind( Word16 Ind, Word16 Ten, Word16 Ccr, Word16 Enr )
{
    Word32   Acc0,Acc1 ;
    Word16   Exp   ;

    PFDEF Pf ;


    Pf.Indx = Ind ;

    /* Check valid gain */
    Acc0 = L_mult( Ten, Enr ) ;
    Acc0 = L_shr( Acc0, (Word16) 2 ) ;
    Acc1 = L_mult( Ccr, Ccr ) ;

    if ( Acc1 > Acc0 ) {

        if ( Ccr >= Enr )
            Pf.Gain = LpfConstTable[(int)WrkRate] ;
        else {
            Pf.Gain = div_s( Ccr, Enr ) ;
            Pf.Gain = mult( Pf.Gain, LpfConstTable[(int)WrkRate] ) ;
        }
        /* Compute scaling gain */
        Acc0 = L_deposit_h( Ten ) ;
        Acc0 = L_shr( Acc0, (Word16) 1 ) ;
        Acc0 = L_mac( Acc0, Ccr, Pf.Gain ) ;
        Exp  = mult( Pf.Gain, Pf.Gain ) ;
        Acc1 = L_mult( Enr, Exp ) ;
        Acc1 = L_shr( Acc1, (Word16) 1 ) ;
        Acc0 = L_add( Acc0, Acc1 ) ;
        Exp = round( Acc0 ) ;

        Acc1 = L_deposit_h( Ten ) ;
        Acc0 = L_deposit_h( Exp ) ;
        Acc1 = L_shr( Acc1, (Word16) 1 ) ;

        if ( Acc1 >= Acc0 )
            Exp = (Word16) 0x7fff ;
        else
            Exp = div_l( Acc1, Exp ) ;

        Acc0 = L_deposit_h( Exp ) ;
        Pf.ScGn = Sqrt_lbc( Acc0 ) ;
    }
    else {
        Pf.Gain = (Word16) 0 ;
        Pf.ScGn = (Word16) 0x7fff ;
    }

    Pf.Gain = mult( Pf.Gain, Pf.ScGn ) ;

    return Pf ;
}

/*
**
** Function:        Filt_Lpf()
**
** Description:     Applies the pitch postfilter for each subframe.
**
** Links to text:   Section 3.6
**
** Arguments:
**
**  Word16 *Tv      Pitch postfiltered excitation
**  Word16 *Buff    decoded excitation
**  PFDEF Pf        Pitch postfilter parameters
**  Word16 Sfc      Subframe index
**
** Outputs:
**
**  Word16 *Tv      Pitch postfiltered excitation
**
** Return value: None
**
*/
void  Filt_Lpf( Word16 *Tv, Word16 *Buff, PFDEF Pf, Word16 Sfc )
{
    int   i  ;

    Word32   Acc0 ;

    for ( i = 0 ; i < SubFrLen ; i ++ ) {
        Acc0 = L_mult( Buff[PitchMax+(int)Sfc*SubFrLen+i], Pf.ScGn ) ;
        Acc0 = L_mac( Acc0, Buff[PitchMax+(int)Sfc*SubFrLen+(int)Pf.Indx+i],
                                                                Pf.Gain ) ;
        Tv[(int)Sfc*SubFrLen+i] = round( Acc0 ) ;
    }

    return;
}

/*
**
** Function:        ACELP_LBC_code()
**
** Description:     Find Algebraic codebook for low bit rate LBC encoder
**
** Links to text:   Section 2.16
**
** Arguments:
**
**   Word16 X[]              Target vector.     (in Q0)
**   Word16 h[]              Impulse response.  (in Q12)
**   Word16 T0               Pitch period.
**   Word16 code[]           Innovative vector.        (in Q12)
**   Word16 gain             Innovative vector gain.   (in Q0)
**   Word16 sign             Signs of the 4 pulses.
**   Word16 shift            Shift of the innovative vector
**   Word16 gain_T0          Gain for pitch synchronous fiter
**
** Inputs :
**
**   Word16 X[]              Target vector.     (in Q0)
**   Word16 h[]              Impulse response.  (in Q12)
**   Word16 T0               Pitch period.
**   Word16 gain_T0          Gain for pitch synchronous fiter
**
** Outputs:
**
**   Word16 code[]           Innovative vector.        (in Q12)
**   Word16 gain             Innovative vector gain.   (in Q0)
**   Word16 sign             Signs of the 4 pulses.
**   Word16 shift            Shift of the innovative vector.
**
** Return value:
**
**   Word16 index            Innovative codebook index
**
*/
Word16  ACELP_LBC_code(Word16 X[], Word16 h[], Word16 T0, Word16 code[],
        Word16 *ind_gain, Word16 *shift, Word16 *sign, Word16 gain_T0)
{
    Word16 i, index, gain_q;
    Word16 Dn[SubFrLen2], tmp_code[SubFrLen2];
    Word16 rr[DIM_RR];

 /*
  * Include fixed-gain pitch contribution into impulse resp. h[]
  * Find correlations of h[] needed for the codebook search.
 */
    for (i = 0; i < SubFrLen; i++)    /* Q13 -->  Q12*/
        h[i] = shr(h[i], 1);

    if (T0 < SubFrLen-2) {
        for (i = T0; i < SubFrLen; i++)    /* h[i] += gain_T0*h[i-T0] */
        h[i] = add(h[i], mult(h[i-T0], gain_T0));
    }

    Cor_h(h, rr);

 /*
  * Compute correlation of target vector with impulse response.
  */

    Cor_h_X(h, X, Dn);

 /*
  * Find innovative codebook.
  * rr input matrix autocorrelation
  *    output filtered codeword
  */

    index = D4i64_LBC(Dn, rr, h, tmp_code, rr, shift, sign);

 /*
  * Compute innovation vector gain.
  * Include fixed-gain pitch contribution into code[].
  */

    *ind_gain = G_code(X, rr, &gain_q);

    for (i = 0; i < SubFrLen; i++)   {
        code[i] = i_mult(tmp_code[i], gain_q);
    }

    if(T0 < SubFrLen-2)
        for (i = T0; i < SubFrLen; i++)    /* code[i] += gain_T0*code[i-T0] */
            code[i] = add(code[i], mult(code[i-T0], gain_T0));


    return index;
}

/*
**
** Function:        Cor_h()
**
** Description:     Compute correlations of h[] needed for the codebook search.
**
** Links to text:   Section 2.16
**
** Arguments:
**
**  Word16 h[]              Impulse response.
**  Word16 rr[]             Correlations.
**
**  Outputs:
**
**  Word16 rr[]             Correlations.
**
**  Return value :          None
*/
void Cor_h(Word16 *H, Word16 *rr)
{
    Word16 *rri0i0, *rri1i1, *rri2i2, *rri3i3;
    Word16 *rri0i1, *rri0i2, *rri0i3;
    Word16 *rri1i2, *rri1i3, *rri2i3;

    Word16 *p0, *p1, *p2, *p3;

    Word16 *ptr_hd, *ptr_hf, *ptr_h1, *ptr_h2;
    Word32 cor;
    Word16 i, k, ldec, l_fin_sup, l_fin_inf;
    Word16 h[SubFrLen2];

    /* Scaling for maximum precision */

    cor = 0;
    for(i=0; i<SubFrLen; i++)
        cor = L_mac(cor, H[i], H[i]);

    if(extract_h(cor) > 32000 ) {
        for(i=0; i<SubFrLen; i++) h[i+4] = shr(H[i], 1);
    }
    else {
        k = norm_l(cor);
        k = shr(k, 1);
        for(i=0; i<SubFrLen; i++) h[i+4] = shl(H[i], k);
    }

    for(i=0; i<4; i++) h[i] = 0;

    /* Init pointers */

    rri0i0 = rr;
    rri1i1 = rri0i0 + NB_POS;
    rri2i2 = rri1i1 + NB_POS;
    rri3i3 = rri2i2 + NB_POS;

    rri0i1 = rri3i3 + NB_POS;
    rri0i2 = rri0i1 + MSIZE;
    rri0i3 = rri0i2 + MSIZE;
    rri1i2 = rri0i3 + MSIZE;
    rri1i3 = rri1i2 + MSIZE;
    rri2i3 = rri1i3 + MSIZE;

 /*
  * Compute rri0i0[], rri1i1[], rri2i2[] and rri3i3[]
  */

    p0 = rri0i0 + NB_POS-1;   /* Init pointers to last position of rrixix[] */
    p1 = rri1i1 + NB_POS-1;
    p2 = rri2i2 + NB_POS-1;
    p3 = rri3i3 + NB_POS-1;

    ptr_h1 = h;
    cor    = 0;

⌨️ 快捷键说明

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