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

📄 exc_lbc.c

📁 这是g.723语音标准的编码过程
💻 C
📖 第 1 页 / 共 4 页
字号:
**                     i2 (+-1) : 4, 12, 20, 28, 36, 44, 52, (60)
**                     i3 (+-1) : 6, 14, 22, 30, 38, 46, 54, (62)
**
**                     All the pulse can be shifted by one.
**                     The last position of the last 2 pulses falls outside the
**                     frame and signifies that the pulse is not present.
**                     The threshold controls if a section of the innovative
**                     codebook should be searched or not.
**
**  Links to the text: Section 2.16
**
**  Input arguments:
**
**      Word16 Dn[]       Correlation between target vector and impulse response h[]
**      Word16 rr[]       Correlations of impulse response h[]
**      Word16 h[]        Impulse response of filters
**
**  Output arguments:
**
**      Word16 cod[]      Selected algebraic codeword
**      Word16 y[]        Filtered codeword
**      Word16 code_shift Shift of the codeword
**      Word16 sign       Signs of the 4 pulses.
**
**  Return value:
**
**      Word16   Index of selected codevector
**
*/
Word16 D4i64_LBC(Word16 Dn[], Word16 rr[], Word16 h[], Word16 cod[],
                 Word16 y[], Word16 *code_shift, Word16 *sign)
{
    Word16  i0, i1, i2, i3, ip0, ip1, ip2, ip3;
    Word16  i, j, time;
    Word16  shif, shift;
    Word16  ps0, ps1, ps2, ps3, alp, alp0;
    Word32  alp1, alp2, alp3, L32;
    Word16  ps0a, ps1a, ps2a;
    Word16  ps3c, psc, alpha;
    Word16  means, max0, max1, max2, thres;

    Word16  *rri0i0, *rri1i1, *rri2i2, *rri3i3;
    Word16  *rri0i1, *rri0i2, *rri0i3;
    Word16  *rri1i2, *rri1i3, *rri2i3;

    Word16  *ptr_ri0i0, *ptr_ri1i1, *ptr_ri2i2, *ptr_ri3i3;
    Word16  *ptr_ri0i1, *ptr_ri0i2, *ptr_ri0i3;
    Word16  *ptr_ri1i2, *ptr_ri1i3, *ptr_ri2i3;

    Word16  *ptr1_ri0i1, *ptr1_ri0i2, *ptr1_ri0i3;
    Word16  *ptr1_ri1i2, *ptr1_ri1i3, *ptr1_ri2i3;

    Word16  p_sign[SubFrLen2/2];

    /* 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;

 /*
  * Extend the backward filtered target vector by zeros
  */

    for (i = SubFrLen; i < SubFrLen2; i++) Dn[i] = 0;

 /*
  * Chose the sign of the impulse.
  */

    for (i=0; i<SubFrLen; i+=2) {
        if( add(Dn[i],Dn[i+1]) >= 0) {
            p_sign[i/2] = 1;
        }
        else {
            p_sign[i/2] = -1;
            Dn[i] = -Dn[i];
            Dn[i+1] = -Dn[i+1];
        }
    }
    p_sign[30] = p_sign[31] = 1;

 /*
  *   Compute the search threshold after three pulses
  */

    /* odd positions */
    /* Find maximum of Dn[i0]+Dn[i1]+Dn[i2] */

    max0 = Dn[0];
    max1 = Dn[2];
    max2 = Dn[4];
    for (i = 8; i < SubFrLen; i+=STEP) {
        if (Dn[i]   > max0) max0 = Dn[i];
        if (Dn[i+2] > max1) max1 = Dn[i+2];
        if (Dn[i+4] > max2) max2 = Dn[i+4];
    }
    max0 = add(max0, max1);
    max0 = add(max0, max2);

    /* Find means of Dn[i0]+Dn[i1]+Dn[i2] */

    L32 = 0;
    for (i = 0; i < SubFrLen; i+=STEP) {
        L32 = L_mac(L32, Dn[i], 1);
        L32 = L_mac(L32, Dn[i+2], 1);
        L32 = L_mac(L32, Dn[i+4], 1);
    }
    means =extract_l( L_shr(L32, 4));

    /* thres = means + (max0-means)*threshold; */

    thres = sub(max0, means);
    thres = mult(thres, threshold);
    thres = add(thres, means);

    /* even positions */
    /* Find maximum of Dn[i0]+Dn[i1]+Dn[i2] */

    max0 = Dn[1];
    max1 = Dn[3];
    max2 = Dn[5];
    for (i = 9; i < SubFrLen; i+=STEP) {
        if (Dn[i]   > max0) max0 = Dn[i];
        if (Dn[i+2] > max1) max1 = Dn[i+2];
        if (Dn[i+4] > max2) max2 = Dn[i+4];
    }
    max0 = add(max0, max1);
    max0 = add(max0, max2);

    /* Find means of Dn[i0]+Dn[i1]+Dn[i2] */

    L32 = 0;
    for (i = 1; i < SubFrLen; i+=STEP) {
        L32 = L_mac(L32, Dn[i], 1);
        L32 = L_mac(L32, Dn[i+2], 1);
        L32 = L_mac(L32, Dn[i+4], 1);
    }
    means =extract_l( L_shr(L32, 4));


    /* max1 = means + (max0-means)*threshold */

    max1 = sub(max0, means);
    max1 = mult(max1, threshold);
    max1 = add(max1, means);

    /* Keep maximum threshold between odd and even position */

    if(max1 > thres) thres = max1;

 /*
  * Modification of rrixiy[] to take signs into account.
  */

    ptr_ri0i1 = rri0i1;
    ptr_ri0i2 = rri0i2;
    ptr_ri0i3 = rri0i3;
    ptr1_ri0i1 = rri0i1;
    ptr1_ri0i2 = rri0i2;
    ptr1_ri0i3 = rri0i3;

    for(i0=0; i0<SubFrLen/2; i0+=STEP/2) {
        for(i1=2/2; i1<SubFrLen/2; i1+=STEP/2) {
            *ptr_ri0i1++ = i_mult(*ptr1_ri0i1++,
                                    i_mult(p_sign[i0], p_sign[i1]));
            *ptr_ri0i2++ = i_mult(*ptr1_ri0i2++,
                                    i_mult(p_sign[i0], p_sign[i1+1]));
            *ptr_ri0i3++ = i_mult(*ptr1_ri0i3++,
                                    i_mult(p_sign[i0], p_sign[i1+2]));
        }
    }

    ptr_ri1i2 = rri1i2;
    ptr_ri1i3 = rri1i3;
    ptr1_ri1i2 = rri1i2;
    ptr1_ri1i3 = rri1i3;
    for(i1=2/2; i1<SubFrLen/2; i1+=STEP/2) {
        for(i2=4/2; i2<SubFrLen2/2; i2+=STEP/2) {
            *ptr_ri1i2++ = i_mult(*ptr1_ri1i2++,
                                    i_mult(p_sign[i1], p_sign[i2]));
            *ptr_ri1i3++ = i_mult(*ptr1_ri1i3++,
                                    i_mult(p_sign[i1], p_sign[i2+1]));

        }
    }

    ptr_ri2i3 = rri2i3;

    ptr1_ri2i3 = rri2i3;
    for(i2=4/2; i2<SubFrLen2/2; i2+=STEP/2) {
        for(i3=6/2; i3<SubFrLen2/2; i3+=STEP/2)
        *ptr_ri2i3++ = i_mult(*ptr1_ri2i3++, i_mult(p_sign[i2], p_sign[i3]));
    }

 /*
  * Search the optimum positions of the four  pulses which maximize
  *     square(correlation) / energy
  * The search is performed in four  nested loops. At each loop, one
  * pulse contribution is added to the correlation and energy.
  *
  * The fourth loop is entered only if the correlation due to the
  *  contribution of the first three pulses exceeds the preset
  *  threshold.
  */

    /* Default values */
    ip0    = 0;
    ip1    = 2;
    ip2    = 4;
    ip3    = 6;
    shif   = 0;
    psc    = 0;
    alpha  = 32767;
    time   = add(max_time, extra);



    /* Four loops to search innovation code. */

    /* Init. pointers that depend on first loop */
    ptr_ri0i0 = rri0i0;
    ptr_ri0i1 = rri0i1;
    ptr_ri0i2 = rri0i2;
    ptr_ri0i3 = rri0i3;

    /* first pulse loop  */
    for (i0 = 0; i0 < SubFrLen; i0 += STEP) {

        ps0  = Dn[i0];
        ps0a = Dn[i0+1];
        alp0 = *ptr_ri0i0++;

        /* Init. pointers that depend on second loop */
        ptr_ri1i1 = rri1i1;
        ptr_ri1i2 = rri1i2;
        ptr_ri1i3 = rri1i3;

        /* second pulse loop */
        for (i1 = 2; i1 < SubFrLen; i1 += STEP) {

            ps1  = add(ps0, Dn[i1]);
            ps1a = add(ps0a, Dn[i1+1]);

            /* alp1 = alp0 + *ptr_ri1i1++ + 2.0 * ( *ptr_ri0i1++); */

            alp1 = L_mult(alp0, 1);
            alp1 = L_mac(alp1, *ptr_ri1i1++, 1);
            alp1 = L_mac(alp1, *ptr_ri0i1++, 2);

            /* Init. pointers that depend on third loop */
            ptr_ri2i2 = rri2i2;
            ptr_ri2i3 = rri2i3;

            /* third pulse loop */
            for (i2 = 4; i2 < SubFrLen2; i2 += STEP) {

                ps2  = add(ps1, Dn[i2]);
                ps2a = add(ps1a, Dn[i2+1]);

                /* alp2 = alp1 + *ptr_ri2i2++
                               + 2.0 * (*ptr_ri0i2++ + *ptr_ri1i2++); */

                alp2 = L_mac(alp1, *ptr_ri2i2++, 1);
                alp2 = L_mac(alp2, *ptr_ri0i2++, 2);
                alp2 = L_mac(alp2, *ptr_ri1i2++, 2);

                /* Decide the shift */

                shift = 0;
                if(ps2a > ps2) {
                    shift = 1;
                    ps2   = ps2a;
                }

                /* Test threshold */

                if ( ps2 > thres) {

                    /* Init. pointers that depend on 4th loop */
                    ptr_ri3i3 = rri3i3;

                    /* 4th pulse loop */
                    for (i3 = 6; i3 < SubFrLen2; i3 += STEP) {

                        ps3 = add(ps2, Dn[i3+shift]);

                        /* alp3 = alp2 + (*ptr_ri3i3++) +
                                         2 x ( (*ptr_ri0i3++) +
                                               (*ptr_ri1i3++) +
                                               (*ptr_ri2i3++) ) */

                        alp3 = L_mac(alp2, *ptr_ri3i3++, 1);
                        alp3 = L_mac(alp3, *ptr_ri0i3++, 2);
                        alp3 = L_mac(alp3, *ptr_ri1i3++, 2);
                        alp3 = L_mac(alp3, *ptr_ri2i3++, 2);
                        alp  = extract_l(L_shr(alp3, 5));

                        ps3c = mult(ps3, ps3);
                        if( L_mult(ps3c, alpha) > L_mult(psc, alp) ) {
                            psc = ps3c;
                            alpha = alp;
                            ip0 = i0;
                            ip1 = i1;
                            ip2 = i2;
                            ip3 = i3;
                            shif = shift;
                        }
                    }  /*  end of for i3 = */

                    time --;
                    if(time <= 0 ) goto end_search;   /* Max time finish */
                    ptr_ri0i3 -= NB_POS;
                    ptr_ri1i3 -= NB_POS;

                }  /* end of if >thres */

                else {
                    ptr_ri2i3 += NB_POS;
                }

            } /* end of for i2 = */

            ptr_ri0i2 -= NB_POS;
            ptr_ri1i3 += NB_POS;

        } /* end of for i1 = */

        ptr_ri0i2 += NB_POS;
        ptr_ri0i3 += NB_POS;

    } /* end of for i0 = */

end_search:

    extra = time;

    /* Set the sign of impulses */

    i0 = p_sign[shr(ip0, 1)];
    i1 = p_sign[shr(ip1, 1)];
    i2 = p_sign[shr(ip2, 1)];
    i3 = p_sign[shr(ip3, 1)];

    /* Find the codeword corresponding to the selected positions */

    for(i=0; i<SubFrLen; i++) cod[i] = 0;

    if(shif > 0) {
        ip0 = add(ip0 ,1);
        ip1 = add(ip1 ,1);
        ip2 = add(ip2 ,1);
        ip3 = add(ip3 ,1);
    }

    cod[ip0] =  i0;
    cod[ip1] =  i1;
    if(ip2<SubFrLen) cod[ip2] = i2;
    if(ip3<SubFrLen) cod[ip3] = i3;

    /* find the filtered codeword */

    for (i = 0; i < SubFrLen; i++) y[i] = 0;

    if(i0 > 0)
        for(i=ip0, j=0; i<SubFrLen; i++, j++)
            y[i] = add(y[i], h[j]);
    else
        for(i=ip0, j=0; i<SubFrLen; i++, j++)
            y[i] = sub(y[i], h[j]);

    if(i1 > 0)
        for(i=ip1, j=0; i<SubFrLen; i++, j++)
            y[i] = add(y[i], h[j]);
    else
        for(i=ip1, j=0; i<SubFrLen; i++, j++)
            y[i] = sub(y[i], h[j]);

    if(ip2 < SubFrLen) {

        if(i2 > 0)
            for(i=ip2, j=0; i<SubFrLen; i++, j++)
                y[i] = add(y[i], h[j]);
        else
            for(i=ip2, j=0; i<SubFrLen; i++, j++)
                y[i] = sub(y[i], h[j]);
    }

    if(ip3 < SubFrLen) {

        if(i3 > 0)
            for(i=ip3, j=0; i<SubFrLen; i++, j++)
                y[i] = add(y[i], h[j]);
        else
            for(i=ip3, j=0; i<SubFrLen; i++, j++)
                y[i] = sub(y[i], h[j]);
    }

    /* find codebook index;  17-bit address */

    *code_shift = shif;

    *sign = 0;
    if(i0 > 0) *sign = add(*sign, 1);
    if(i1 > 0) *sign = add(*sign, 2);
    if(i2 > 0) *sign = add(*sign, 4);
    if(i3 > 0) *sign = add(*sign, 8);

    i = shr(ip0, 3);
    i = add(i, shl(shr(ip1, 3), 3));
    i = add(i, shl(shr(ip2, 3), 6));
    i = add(i, shl(shr(ip3, 3), 9));

    return i;
}

/*
**
**  Function:  G_code()
**
**  Description: Compute the gain of innovative code.
**
**
**  Links to the text: Section 2.16
**
** Input arguments:
**
**      Word16 X[]        Code target.  (in Q0)
**      Word16 Y[]        Filtered innovation code. (in Q12)
**
** Output:
**
**      Word16 *gain_q    Gain of innovation code.  (in Q0)
**
**  Return value:
**
**      Word16  index of innovation code gain
**
*/
Word16 G_code(Word16 X[], Word16 Y[], Word16 *gain_q)
{
    Word16 i;
    Word16 xy, yy, exp_xy, exp_yy, gain, gain_nq;
    Word32 L_xy, L_yy;
    Word16 dist, dist_min;


    /* Scale down Y[] by 8 to avoid overflow */
    for(i=0; i<SubFrLen; i++)
        Y[i] = shr(Y[i], 3);

    /* Compute scalar product <X[],Y[]> */
    L_xy = 0L;
    for(i=0; i<SubFrLen; i++)
        L_xy = L_mac(L_xy, X[i], Y[i]);

    exp_xy = norm_l(L_xy);
    xy = extract_h( L_shl(L_xy, exp_xy) );

    if(xy <= 0) {
        gain = 0;
        *gain_q =FcbkGainTable[gain];
        return(gain);
    }

    /* Compute scalar product <Y[],Y[]> */
    L_yy = 0L;
    for(i=0; i<SubFrLen; i++)
        L_yy = L_mac(L_yy, Y[i], Y[i]);

    exp_yy = norm_l(L_yy);
    yy     = extract_h( L_shl(L_yy, exp_yy) );

    /* compute gain = xy/yy */
    xy = shr(xy, 1);             /* Be sure xy < yy */
    gain_nq = div_s( xy, yy);

    i = add(exp_xy, 5);          /* Denormalization of division */
    i = sub(i, exp_yy);

    gain_nq = shr(gain_nq, i);

    gain = (Word16) 0;
    dist_min = sub(gain_nq, FcbkGainTable[0]);
    dist_min = abs_s(dist_min);
    for ( i =  1; i <NumOfGainLev ; i ++ ) {
        dist = sub(gain_nq, FcbkGainTable[i]);
        dist =abs_s(dist);
        if ( dist< dist_min) {
            dist_min = dist;
            gain = (Word16) i ;
        }
    }
    *gain_q = FcbkGainTable[gain];

    return(gain);
}

/*
**
**  Function:       search_T0()
**
**  Description:          Gets parameters of pitch synchronous filter
**
**  Links to the text:    Section 2.16
**
**  Arguments:
**
**      Word16 T0         Decoded pitch lag
**      Word16 Gid        Gain vector index in the adaptive gain vector codebook
**      Word16 *gain_T0   Pitch synchronous gain
**
**  Outputs:
**
**      Word16 *gain_T0   Pitch synchronous filter gain
**
**  Return Value:
**
**      Word16 T0_mod     Pitch synchronous filter lag
*/
Word16 search_T0 ( Word16 T0, Word16 Gid, Word16 *gain_T0)
{

    Word16 T0_mod;

    T0_mod = T0+epsi170[Gid];
    *gain_T0 = gain170[Gid];

    return(T0_mod);
}

⌨️ 快捷键说明

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