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

📄 acelp_cp.c

📁 语音压缩编码中的g729p编码程序
💻 C
📖 第 1 页 / 共 5 页
字号:
                    ptr_ri0i3 -= NB_POS;
                    ptr_ri1i3 -= NB_POS;
                    
                    ptr_ri4i4 = rri4i4;    /* Init. pointers that depend on 4th loop */
                    
                    for (i3 = 4; i3 < L_SUBFR; i3 += STEP)
                    {
                        ps3 = ps2 + dn[i3];
                        alp3 = alp2 + *ptr_ri4i4++ + (F)2.0*(*ptr_ri1i4++ + *ptr_ri0i4++ + *ptr_ri2i4++);
                        
                        ps3c = ps3*ps3;
                        if( (ps3c*alpha) > (psc * alp3) )
                        {
                            psc = ps3c;
                            alpha = alp3;
                            ip0 = i0;
                            ip1 = i1;
                            ip2 = i2;
                            ip3 = i3;
                        }
                    }       /*  end of for i3 = */
                    ptr_ri0i4 -= NB_POS;
                    ptr_ri1i4 -= NB_POS;
                    
                    time --;
                    if(time <= 0 ) goto end_search;     /* Maximum time finish */
                    
                }  /* end of if >thres */
                else {
                    ptr_ri2i3 += NB_POS;
                    ptr_ri2i4 += NB_POS;
                }
                
            } /* end of for i2 = */
            
            ptr_ri0i2 -= NB_POS;
            ptr_ri1i3 += NB_POS;
            ptr_ri1i4 += NB_POS;
            
        } /* end of for i1 = */
        
        ptr_ri0i2 += NB_POS;
        ptr_ri0i3 += NB_POS;
        ptr_ri0i4 += NB_POS;
        
    } /* end of for i0 = */
    
end_search:
    
    extra = time;
    
    /* Find the codeword corresponding to the selected positions */
    
    for(i=0; i<L_SUBFR; i++) cod[i] = (F)0.0;
    cod[ip0] = p_sign[ip0];
    cod[ip1] = p_sign[ip1];
    cod[ip2] = p_sign[ip2];
    cod[ip3] = p_sign[ip3];
    
    /* find the filtered codeword */
    
    for (i = 0; i < L_SUBFR; i++) y[i] = (F)0.0;
    
    if(p_sign[ip0] > (F)0.0) {
        for(i=ip0, j=0; i<L_SUBFR; i++, j++) y[i] = h[j];
    }
    else {
        for(i=ip0, j=0; i<L_SUBFR; i++, j++) y[i] = -h[j];
    }   
    if(p_sign[ip1] > (F)0.0) {
        for(i=ip1, j=0; i<L_SUBFR; i++, j++) y[i] = y[i] + h[j];
    }
    else {
        for(i=ip1, j=0; i<L_SUBFR; i++, j++) y[i] = y[i] - h[j];
    }
    if(p_sign[ip2] > (F)0.0) {
        for(i=ip2, j=0; i<L_SUBFR; i++, j++) y[i] = y[i] + h[j];
    }
    else {
        for(i=ip2, j=0; i<L_SUBFR; i++, j++) y[i] = y[i] - h[j];
    }                       
    if(p_sign[ip3] > (F)0.0) {
        for(i=ip3, j=0; i<L_SUBFR; i++, j++) y[i] = y[i] + h[j];
    }
    else {
        for(i=ip3, j=0; i<L_SUBFR; i++, j++) y[i] = y[i] - h[j];
    }                           
    /* find codebook index;  4 bit signs + 13 bit positions */
    
    i = 0;
    if(p_sign[ip0] > (F)0.0) i+=1;
    if(p_sign[ip1] > (F)0.0) i+=2;
    if(p_sign[ip2] > (F)0.0) i+=4;
    if(p_sign[ip3] > (F)0.0) i+=8;
    *signs = i;
    
    ip0 = ip0 / 5;
    ip1 = ip1 / 5;
    ip2 = ip2 / 5;
    j   = (ip3 % 5) - 3;
    ip3 = ( (ip3 / 5) << 1 ) + j;
    
    i = (ip0) + (ip1<<3) + (ip2<<6) + (ip3<<9);
    
    return i;
}
/*-------------------------------------------------------------------*
* Function  ACELP_12i40_44bits()                                    *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                    *
* Algebraic codebook; 44 bits: 12 pulses in a frame of 40 samples.  *
*-------------------------------------------------------------------*
* The code length is 40, containing 12 nonzero pulses: i0...i11.    *
* 12 pulses can have two (2) possible amplitudes: +1 or -1.         *
* 10 pulses can have eight (8) possible positions:                  *
* i2,i7  :  0, 5, 10, 15, 20, 25, 30, 35.  --> t0                   *
* i3,i8  :  1, 6, 11, 16, 21, 26, 31, 36.  --> t1                   *
* i4,i9  :  2, 7, 12, 17, 22, 27, 32, 37.  --> t2                   *
* i5,i10 :  3, 8, 13, 18, 23, 28, 33, 38.  --> t3                   *
* i6,i11 :  4, 9, 14, 19, 24, 29, 34, 39.  --> t4                   *
*                                                                   *
* The 2 other pulses can be on the following track:                 *
*   t0-t1,t1-t2,t2-t3,t3-t4,t4-t0.                                  *
*-------------------------------------------------------------------*/
void ACELP_12i40_44bits(
    FLOAT x[],    /* (i) : target vector                                 */
    FLOAT cn[],   /* (i) : residual after long term prediction           */
    FLOAT h1[],   /* (i) : impulse response of weighted synthesis filter */
    FLOAT code[], /* (o) : algebraic (fixed) codebook excitation         */
    FLOAT y[],    /* (o) : filtered fixed codebook excitation            */
    int indx[]    /* (o) : index 5 words: 13,10,7,7,7 = 44 bits          */
)
{

    int i, j, k, ix, iy, itrk[3], track, pos, index;
    int idx[NB_TRACK];
    FLOAT psk, ps, alpk, alp;
    FLOAT s, corr[NB_TRACK];
    FLOAT *p0, *p1, *h_inv, *h;
    
    FLOAT dn[L_SUBFR], sign[L_SUBFR], vec[L_SUBFR];
    int ip[12], codvec[12], pos_max[NB_TRACK];
    FLOAT cor_x[NB_POS], cor_y[NB_POS];
    FLOAT h_buf[4*L_SUBFR];
    FLOAT rrixix[NB_TRACK][NB_POS], rrixiy[NB_TRACK][MSIZE];
    FLOAT L_tmp;
    
    h = h_buf;
    h_inv = h_buf + (2*L_SUBFR);
    for (i=0; i<L_SUBFR; i++) {
        *h++ = (FLOAT)0.;
        *h_inv++ = (FLOAT)0.;
    }
    for (i=0; i<L_SUBFR; i++) {
        h[i] = h1[i];
    }
    
    /* Compute correlation between target x[] and H[] */
    cor_h_x(h, x, dn);
    
    /* find the sign of each pulse position */
    set_sign(cn, dn, sign, vec, pos_max, corr);
    
    /* Compute correlations of h[] needed for the codebook search. */
    cor_h_e(sign, vec, h, h_inv, rrixix, rrixiy);
    
    /*-------------------------------------------------------------------*
    * Search position for pulse i0 and i1.                              *
    *-------------------------------------------------------------------*/
    s = corr[4] + corr[0];
    for (k=0; k<NB_TRACK-1; k++) corr[k] += corr[k+1];
    corr[4] = s;
    
    for (k=0; k<3; k++) {
        s = corr[0];
        track = 0;
        for (i=1; i<NB_TRACK; i++) {
            L_tmp = corr[i]-s;
            if (L_tmp > (FLOAT)0.) {
                s = corr[i];
                track = i;
            }
        }
        corr[track] = (FLOAT)-1.;
        itrk[k] = track;
    }
    
    /*-------------------------------------------------------------------*
    * Deep first search: 3 iterations of 320 tests = 960 tests.         *
    *                                                                   *
    * Stages of deep first search:                                      *
    *   stage 1 : fix i0  and i1  --> 2 positions is fixed previously.  *
    *   stage 2 : fix i2  and i3  --> try 8x8 = 64 positions.           *
    *   stage 3 : fix i4  and i5  --> try 8x8 = 64 positions.           *
    *   stage 4 : fix i6  and i7  --> try 8x8 = 64 positions.           *
    *   stage 5 : fix i8  and i9  --> try 8x8 = 64 positions.           *
    *   stage 6 : fix i10 and i11 --> try 8x8 = 64 positions.           *
    *-------------------------------------------------------------------*/
    
    /* stage 0: fix pulse i0 and i1 according to max of correlation */
    psk = (FLOAT)-1.;
    alpk = (FLOAT)1.;
    for (pos=0; pos<3; pos++)  {
        k = itrk[pos];       /* starting position index */
        
        /* stage 1: fix pulse i0 and i1 according to max of correlation */
        ix = pos_max[ipos[k]];
        iy = pos_max[ipos[k+1]];
        ps = dn[ix] + dn[iy];
        i = ix/5;
        j = iy/5;
        alp = rrixix[ipos[k]][i] + rrixix[ipos[k+1]][j];
        i = (i<<3) + j;
        alp += rrixiy[ipos[k]][i];
        ip[0] = ix;
        ip[1] = iy;
        
        for (i=0; i<L_SUBFR; i++) vec[i] = (FLOAT)0.;
        
        /* stage 2..5: fix pulse i2,i3,i4,i5,i6,i7,i8 and i9 */
        for (j=2; j<12; j+=2) {
        /*--------------------------------------------------*
        * Store all impulse response of all fixed pulses   *
        * in vector vec[] for the "cor_h_vec()" function.  *
            *--------------------------------------------------*/
            if (sign[ix] < (FLOAT)0.) p0 = h_inv - ix;
            else p0 = h - ix;
            
            if (sign[iy] < (FLOAT)0.) p1 = h_inv - iy;
            else p1 = h - iy;
            
            for (i=0; i<L_SUBFR; i++) {
                vec[i] += *p0 + *p1;
                p0++; p1++;
            }

            /*--------------------------------------------------*
            * Calculate correlation of all possible positions  *
            * of the next 2 pulses with previous fixed pulses. *
            * Each pulse can have 8 possible positions         *
            *--------------------------------------------------*/
            cor_h_vec(h, vec, ipos[k+j], sign, rrixix, cor_x);
            cor_h_vec(h, vec, ipos[k+j+1], sign, rrixix, cor_y);
            
            /*--------------------------------------------------*
            * Fix 2 pulses, try 8x8 = 64 positions.            *
            *--------------------------------------------------*/
            search_ixiy((int)ipos[k+j], (int)ipos[k+j+1], &ps, &alp, &ix, &iy,
                dn, cor_x, cor_y, rrixiy);
            
            ip[j] = ix;
            ip[j+1] = iy;
            
        }
        
        /* memorise new codevector if it's better than the last one. */
        ps *= ps;
        s = alpk*ps-psk*alp;
        if (s > (FLOAT)0.) {
            psk = ps;
            alpk = alp;
            for (i=0; i<12; i++) codvec[i] = ip[i];
        }
    } /* end of for (pos=0; pos<3; pos++) */
    
      /*-------------------------------------------------------------------*
      * index of 12 pulses = 44 bits on 5 words                           *
      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                           *
      * indx[0] =13 bits --> 3(track) +                                   *
      *                      3(pos#11) + 3(pos#6) + 1(sign#1) + 3(pos#1)  *
      * indx[1] =10 bits --> 3(pos#12) + 3(pos#7) + 1(sign#2) + 3(pos#2)  *
      * indx[2] = 7 bits -->             3(pos#8) + 1(sign#3) + 3(pos#3)  *
      * indx[3] = 7 bits -->             3(pos#9) + 1(sign#4) + 3(pos#4)  *
      * indx[4] = 7 bits -->             3(pos#10)+ 1(sign#5) + 3(pos#5)  *
    *-------------------------------------------------------------------*/
    build_code(codvec+2, sign, 10, h, code, y, idx);
    track = 0; /* to avoid visual warning */
    for (k=0; k<2; k++) {
        
        pos = codvec[k];
        index = pos/5;    /* index = pos/5       */
        track = pos%5;
        if (sign[pos] > (FLOAT)0.) {
            code[pos] += (FLOAT)1.;
            for (i=pos, j=0; i<L_SUBFR; i++, j++) y[i] += h[j];
        }
        else {
            code[pos] -= (FLOAT)1.;
            for (i=pos, j=0; i<L_SUBFR; i++, j++) y[i] -= h[j];
            index +=  8;
        }
        
        ix = (idx[track]>>4) & (int)15;
        iy = idx[track] & (int)15;
        
        index = pack3(ix, iy, index);
        if (k == 0) index += track<<10;
        indx[k] = index;
        
    }
    
    for (k=2; k<NB_TRACK; k++) {
        track++;
        if (track >= NB_TRACK) track = 0;
        indx[k] = (idx[track] & (int)127);
    }

    return;
}

/*-------------------------------------------------------------------*
* Function  ACELP_10i40_35bits()                                    *
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                    *
* Algebraic codebook; 35 bits: 10 pulses in a frame of 40 samples.  *
*-------------------------------------------------------------------*
* The code length is 40, containing 10 nonzero pulses: i0...i9.     *
* All pulses can have two (2) possible amplitudes: +1 or -1.        *
* Each pulse can have eight (8) possible positions:                 *
*                                                                   *
* i0,i5 :  0, 5, 10, 15, 20, 25, 30, 35.                            *
* i1,i6 :  1, 6, 11, 16, 21, 26, 31, 36.                            *
* i2,i7 :  2, 7, 12, 17, 22, 27, 32, 37.                            *
* i3,i8 :  3, 8, 13, 18, 23, 28, 33, 38.                            *
* i4,i9 :  4, 9, 14, 19, 24, 29, 34, 39.                            *
*-------------------------------------------------------------------*/
void ACELP_10i40_35bits(
    FLOAT x[],    /* (i) : target vector                                 */
    FLOAT cn[],   /* (i) : residual after long term prediction           */
    FLOAT h1[],   /* (i) : impulse response of weighted synthesis filter */
    FLOAT code[], /* (o) : algebraic (fixed) codebook excitation         */
    FLOAT y[],    /* (o) : filtered fixed codebook excitation            */
    int indx[]    /* (o) : index 5 words: 7,7,7,7,7 = 35 bits            */
)
{
    int i, j, k, ix, iy, pos, track;
    FLOAT psk, ps, alpk, alp;
    int itrk[3];
    FLOAT s, corr[NB_TRACK], L_tmp;
    FLOAT *p0, *p1, *h_inv, *h;
    
    /* these vectors are not static */
    FLOAT dn[L_SUBFR], sign[L_SUBFR], vec[L_SUBFR];
    int ip[10], codvec[10], pos_max[NB_TRACK];
    FLOAT cor_x[NB_POS], cor_y[NB_POS];
    FLOAT h_buf[4*L_SUBFR];
    FLOAT rrixix[NB_TRACK][NB_POS], rrixiy[NB_TRACK][MSIZE];
    
    h = h_buf;
    h_inv = h_buf + (2*L_SUBFR);
    for (i=0; i<L_SUBFR; i++) {
        *h++ = (FLOAT)0.;
        *h_inv++ = (FLOAT)0.;
    }
    for (i=0; i<L_SUBFR; i++) {
        h[i] = h1[i];
    }
    
    /* Compute correlation between target x[] and h[] */
    cor_h_x(h, x, dn);
    
    /* find the sign of each pulse position */
    set_sign(cn, dn, sign, vec, pos_max, corr);
    
    /* Compute correlations of h[] needed for the codebook search. */
    cor_h_e(sign, vec, h, h_inv, rrixix, rrixiy);
    
    /*-------------------------------------------------------------------*
    * Search starting position for pulse i0 and i1.                     *

⌨️ 快捷键说明

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