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

📄 codld8cp.c

📁 语音压缩编码中的g729p编码程序
💻 C
📖 第 1 页 / 共 3 页
字号:
    avg_lag = AVG(lag_buf[0],lag_buf[1],lag_buf[2],lag_buf[3]);
    if( abs( (int) (T_op/2.0) - avg_lag)<=2)
        lag_buf[4] = (int) (T_op/2.0);
    else if( abs((int) (T_op/3.0) - avg_lag)<=2)
        lag_buf[4] = (int) (T_op/3.0);
    else
        lag_buf[4] = T_op;

    /* 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 2 times.                             *
    *     - find the weighted LPC coefficients                               *
    *     - find the LPC residual signal res[]                               *
    *     - 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                                           *
    *     - update the impulse response h1[] by including fixed-gain pitch   *
    *     - find target vector for codebook search                           *
    *     - codebook search                                                  *
    *     - encode codebook address                                          *
    *     - VQ of pitch and codebook gains                                   *
    *     - find synthesis speech                                            *
    *     - update states of weighting filter                                *
    *------------------------------------------------------------------------*/
    pAp  = Ap;     /* pointer to interpolated "unquantized"LPC parameters           */
    pAq = Aq;      /* pointer to interpolated "quantized" LPC parameters */
        
    i_gamma = 0;
    
    for (i_subfr = 0;  i_subfr < L_FRAME; i_subfr += L_SUBFR) {
        
    /*---------------------------------------------------------------*
    * Find the weighted LPC coefficients for the weighting filter.  *
        *---------------------------------------------------------------*/
        weight_az(pAp, gamma1[i_gamma], m_ap, Ap1);
        weight_az(pAp, gamma2[i_gamma], m_ap, Ap2);
        i_gamma++;
        
        /*---------------------------------------------------------------*
        * Compute impulse response, h1[], of weighted synthesis filter  *
        *---------------------------------------------------------------*/
        for (i = 0; i <=m_ap; i++) ai_zero[i] = Ap1[i];
        syn_filte(m_aq,  pAq, ai_zero, h1, L_SUBFR, zero, 0);
        syn_filte(m_ap,  Ap2, h1, h1, L_SUBFR, zero, 0);
        
        /*------------------------------------------------------------------------*
        *                                                                        *
        *          Find the target vector for pitch search:                      *
        *          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                       *
        *                                                                        *
        *              |------|  res[n]                                          *
        *  speech[n]---| A(z) |--------                                          *
        *              |------|       |   |--------| error[n]  |------|          *
        *                    zero -- (-)--| 1/A(z) |-----------| W(z) |-- target *
        *                    exc          |--------|           |------|          *
        *                                                                        *
        * Instead of subtracting the zero-input response of filters from         *
        * the weighted input speech, the above configuration is used to          *
        * compute the target vector. This configuration gives better performance *
        * with fixed-point implementation. The memory of 1/A(z) is updated by    *
        * filtering (res[n]-exc[n]) through 1/A(z), or simply by subtracting     *
        * the synthesis speech from the input speech:                            *
        *    error[n] = speech[n] - syn[n].                                      *
        * The memory of W(z) is updated by filtering error[n] through W(z),      *
        * or more simply by subtracting the filtered adaptive and fixed          *
        * codebook excitations from the target:                                  *
        *     target[n] - gain_pit*y1[n] - gain_code*y2[n]                       *
        * as these signals are already available.                                *
        *                                                                        *
        *------------------------------------------------------------------------*/
        residue(m_aq, pAq, &speech[i_subfr], &exc[i_subfr], L_SUBFR);   /* LPC residual */
        for (i=0; i<L_SUBFR; i++) res2[i] = exc[i_subfr+i];
        syn_filte(m_aq,  pAq, &exc[i_subfr], error, L_SUBFR,
            &mem_err[M_BWD-m_aq], 0);
        residue(m_ap, Ap1, error, xn, L_SUBFR);
        syn_filte(m_ap,  Ap2, xn, xn, L_SUBFR, &mem_w0[M_BWD-m_ap], 0);    /* target signal xn[]*/
        
        t0 = pitch_fr3cp(&exc[i_subfr], xn, h1, L_SUBFR, t0_min, t0_max,
            i_subfr, &t0_frac, rate);
        
        index = enc_lag3cp(t0, t0_frac, &t0_min, &t0_max,PIT_MIN,PIT_MAX,i_subfr, rate);
        
        *ana++ = index;
        
        if ( (i_subfr == 0) && (rate != G729D) ) {
            *ana = parity_pitch(index);
            if( rate == G729E) {       
                *ana ^= ((index >> 1) & 0x0001);
            }
            ana++;
        }
        /*-----------------------------------------------------------------*
        *   - find unity gain pitch excitation (adaptive codebook entry)  *
        *     with fractional interpolation.                              *
        *   - find filtered pitch exc. y1[]=exc[] convolve with h1[])     *
        *   - compute pitch gain and limit between 0 and 1.2              *
        *   - update target vector for codebook search                    *
        *   - find LTP residual.                                          *
        *-----------------------------------------------------------------*/
        pred_lt_3(&exc[i_subfr], t0, t0_frac, L_SUBFR);
                
        convolve(&exc[i_subfr], h1, y1, L_SUBFR);
        
        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.                       *
        *-----------------------------------------------------*/
        
        switch (rate) {
                        
            case G729:    /* 8 kbit/s */
            { 
                index = ACELP_codebook(xn2, h1, t0, pit_sharp, i_subfr, code,
                    y2, &i);
                *ana++ = index;        /* Positions index */
                *ana++ = i;            /* Signs index     */
                break;
            }

            case G729D:    /* 6.4 kbit/s */
            {
                index = ACELP_codebook64(xn2, h1, t0, pit_sharp, code, y2, &i);
                *ana++ = index;        /* Positions index */
                *ana++ = i;            /* Signs index     */
                break;
            }
                
            case G729E:    /* 11.8 kbit/s */
            {
            /*-----------------------------------------------------------------*
            * Include fixed-gain pitch contribution into impulse resp. h[]    *
                *-----------------------------------------------------------------*/
                if(t0 < L_SUBFR) {
                    for (i = t0; i < L_SUBFR; i++) {
                        h1[i] += pit_sharp * h1[i-t0];
                    }
                }
                    
                /* calculate residual after long term prediction */
                for (i = 0; i < L_SUBFR;  i++)
                    res2[i] -= gain_pit*exc[i+i_subfr];
                if (lp_mode == 0)
                    ACELP_10i40_35bits(xn2, res2, h1, code, y2, ana); /* Forward */
                else
                    ACELP_12i40_44bits(xn2, res2, h1, code, y2, ana); /* Backward */
                ana += 5;
                
                /*-----------------------------------------------------------------*
                * Include fixed-gain pitch contribution into code[].              *
                *-----------------------------------------------------------------*/
                if(t0 < L_SUBFR)
                    for (i = t0; i < L_SUBFR; i++)
                        code[i] += pit_sharp * code[i-t0];
                    
                break;
            }
            
            default : {
                printf("Unrecognized bit rate\n");
                exit(-1);
            }
                

        }  /* end of switch */
                
        
        /*-----------------------------------------------------*
        * - Quantization of gains.                            *
        *-----------------------------------------------------*/
        corr_xy2( xn, y1, y2, g_coeff);
        
        if (rate == G729D) 
            index = qua_gain_6k(code, g_coeff, L_SUBFR, &gain_pit, &gain_code, taming );
        else 
            index = qua_gain(code, g_coeff, L_SUBFR, &gain_pit, &gain_code, taming);
        
        *ana++ = index;
        
        /*------------------------------------------------------------*
        * - Update pitch sharpening  with quantized gain_pit          *
        *------------------------------------------------------------*/
        for (i= 0; i< 4; i++)
            pgain_buf[i] = pgain_buf[i+1];
        pgain_buf[4] = gain_pit;

        pit_sharp = gain_pit;
        if (pit_sharp > SHARPMAX) pit_sharp = SHARPMAX;
        if (pit_sharp < SHARPMIN) pit_sharp = SHARPMIN;
        
        /*------------------------------------------------------*
        * - Find the total excitation                          *
        * - find synthesis speech corresponding to exc[]       *
        * - update filters memories for finding the target     *
        *   vector in the next subframe                        *
        *   (update error[-m..-1] and mem_w_err[])             *
        *   update error function for taming process           *
        *------------------------------------------------------*/
        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);
                
        syn_filte(m_aq,  pAq, &exc[i_subfr], &synth_ptr[i_subfr], L_SUBFR,
            &mem_syn[M_BWD-m_aq], 0);
        for(j=0; j<M_BWD; j++) mem_syn[j] = synth_ptr[i_subfr+L_SUBFR-M_BWD+j];
        
        for (i = L_SUBFR-M_BWD, j = 0; i < L_SUBFR; i++, j++) {
            mem_err[j] = speech[i_subfr+i] - synth_ptr[i_subfr+i];
            mem_w0[j]  = xn[i] - gain_pit*y1[i] - gain_code*y2[i];
        }
        pAp   += m_ap+1;
        pAq   += m_aq+1;
    }
    
    /*--------------------------------------------------*
    * 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);
    prev_lp_mode = lp_mode;
    return;
}

⌨️ 快捷键说明

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