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

📄 phi_xits.c

📁 MPEG-4编解码的实现(包括MPEG4视音频编解码)
💻 C
📖 第 1 页 / 共 3 页
字号:
            den += y[i] * y[i];
        }
        r = num * num / den;

        /* =============================================================*/
        /* Check if the curent lag is the best candidate                */
        /* =============================================================*/
        if (r > rs)
        {
            ks = k;
            rs = r;
            *g = num / den;
        }
    }
    *vid = pi[ks];

    /*==================================================================*/
    /* Gain Quantisation                                                */
    /*==================================================================*/

    i = *g < (float)0.0 ? -1 : 1;
    *g = (float)fabs((double)*g);

    for (j = 0; *g > (float)tbl_cba_dir[j].dec && j < QLa - 1; j ++);
    *g = (float) i * (float)tbl_cba_dir[j].rep;
    *gid = i == 1 ? (long)j : (((long)(-j - 1)) & 63);

    /*==================================================================*/
    /*FREE temp states                                                 */
    /*==================================================================*/
   FREE(y);
   FREE(yp);

    return;
}


/*
----------------------------------------------------------------------------
  computes residual signal after adaptive codebook
----------------------------------------------------------------------------
*/
    
void 
PHI_calc_cba_residual
(
long  nos,             /* In:     Number of samples to be processed    */
float *vi,             /* In:     Succesful excitation candidate       */ 
float gain,            /* In:     Gain of the adaptive codebook        */ 
float *h,              /* In:     Impulse response of synthesis filt   */
float *t,              /* In:     The real target signal               */
float *e               /* Out:    Adapt-cbk residual: Target for f-cbk */
)
{
    float v;                                
    int i, j;
    
    for (i = 0; i < (int)nos; i ++)
    {
        v = (float)0.0;
        for (j = 0; j <= i; j ++)
        {
            v += h[i - j] * vi[j];
        }
        e[i] = t[i] - gain * v;
    }

    return;
}

/*
----------------------------------------------------------------------------
  determines phase of the local rpe codebook vectors
----------------------------------------------------------------------------
*/

void 
PHI_calc_cbf_phase
(
long  pulse_spacing,    /* In:    Regular Spacing Between Pulses        */
long  nos,              /* In:    Number of samples to be processed     */
float *tf,              /* In:    Backward filtered target signal       */ 
long  *p                /* Out:   Phase of the local RPE codebook vector*/
)
{
    float vs, v;
    int i, j;

    vs = -FLT_MAX;
    *p = 0;
    
    for (i = 0; i < (int)pulse_spacing; i ++)
    {
        for (j = i, v = (float)0.0; j < (int)nos; j += (int)pulse_spacing)
        {
            v += (float)fabs((double)tf[j]);
        }

        if (v > vs)
        {
            vs = v;
            *p = (long)i;
        }
    }

    return;
}

/*
----------------------------------------------------------------------------
  computes rpe pulse amplitude (Version 2:only computes amplitude)
----------------------------------------------------------------------------
*/

void 
PHI_CompAmpArray
(
long  num_of_pulses,    /* In:    Number of pulses in the sequence      */
long  pulse_spacing,    /* In:    Regular Spacing Between Pulses        */
float *tf,              /* In:    Backward filtered target signal       */ 
long  p,                /* In:    Phase of the RPE codebook             */
long  *amp              /* Out:   Pulse amplitudes  +/- 1               */ 
)
{

    int i, k;
    float v;
    
    for (k = (int)p, i = 0; i < (int)num_of_pulses; k += (int)pulse_spacing, i ++)
    {
        v = tf[k];
        
        if (v == (float)0.0)
        {
            amp[i] = 0; /* THIS SHOULD BE ZERO!!! */
        }
        else
        {
            amp[i] = v > (float)0.0 ? (long)1 : (long)-1;
        }
    }
    return;
}

/*
----------------------------------------------------------------------------
  computes pos array {extension to Vienna code which only fixed 1 amp
----------------------------------------------------------------------------
*/
void 
PHI_CompPosArray
(
long  num_of_pulses,    /* In:    Number of pulses in the sequence      */
long  pulse_spacing,    /* In:    Regular Spacing Between Pulses        */
long  num_fxd_amps,     /* IN:    Number of fixed amplitudes            */
float *tf,              /* In:    Backward filtered target signal       */ 
long  p,                /* In:    Phase of the RPE codebook             */
long  *pos              /* Out:   Pulse amplitudes  +/- 1               */ 
)
{
    float *d_tmp;
    int i, nr_nonzero_samples;
    
    /*==================================================================*/
    /* Allocate d_tmp                                             */
    /*==================================================================*/
    if ((d_tmp = (float *)malloc((unsigned int)num_of_pulses * sizeof(float)))== NULL )
    {
       fprintf(stderr, "\n Malloc Failure in CompPosArray:Excitation Anlaysis \n");
       exit(1);
    }
    for (i = nr_nonzero_samples = 0; i < (int)num_of_pulses; i ++)
    {
        pos[i] = 0;
        d_tmp[i] = (float)fabs((double)tf[(int)p + i * (int)pulse_spacing]);
	if (d_tmp[i] > 0)
	    nr_nonzero_samples++;
    }
    if (nr_nonzero_samples >= (int)num_fxd_amps)
    {
        for (i = 0; i < (int)num_fxd_amps; i ++)
        {
           float tmp   = (float)0.0;
           int   p_tmp = 0;
           int   l;

           for(l = 0; l < (int)num_of_pulses; l++)
           {
                   if (d_tmp[l] > tmp)
                   {
                   tmp = d_tmp[l];
                   p_tmp = l;
                   }
           }
           pos[p_tmp] = 1;
           d_tmp[p_tmp] = (float)0;
        }
    }
    else
    {
        int   l;

	for(i = 0; i < (int)num_of_pulses; i++)
	{
	    if (d_tmp[i] > 0)
	    {
                pos[i] = 1;
	    }
	}
	for(i = 0, l = nr_nonzero_samples; l < num_fxd_amps; i++)
	{
	    if (d_tmp[i] == 0)
	    {
                pos[i] = 1;
		l++;
	    }
	}
    }
   FREE(d_tmp);
    return;
}

/*
----------------------------------------------------------------------------
  generates local fixed codebook
----------------------------------------------------------------------------
*/

void 
PHI_generate_cbf
(
long  num_of_pulses,    /* In:    Number of pulses in the sequence      */
long  pulse_spacing,    /* In:    Regular Spacing Between Pulses        */
long  num_fcbk_vecs,    /* In:    #Vectors in the fixed code book       */
long  nos,              /* In:    Number of samples to be processed     */
long  **cb,             /* Out:   Generated Local Fixed Codebook        */ 
long  p,                /* In:    Phase of the codebook vector          */
long  *amp,             /* In:    Pulse Amplitudes                      */ 
long  *pos              /* In:    Array saying which are fixed amps     */
)
{
    int i, j, k, m, n, index;
    for (i = 0; i < (int)num_fcbk_vecs; i ++)
        for (j = 0; j < (int)nos; j ++)
            cb[i][j] = 0;

    for (index = (int)p, i = 0; i < (int)num_of_pulses; index += (int)pulse_spacing, i ++)
    {
        cb[0][index] = amp[i];
    }
    
    for (index = (int)p,i = 0, m = 1; i < (int)num_of_pulses; index += (int)pulse_spacing, i ++)
    {
        if (!pos[i])
        {
            for (j = 0, k = m; j < k; j ++)
            {
                for (n = (int)p; n < (int)nos; n += (int)pulse_spacing)
                {
                    cb[m][n] = cb[j][n];
                }
                cb[m ++][index] = 0;
            }
        }
    }
    /* -----------------------------------------------------------------*/
    /* To test the codebook content                                     */
    /* -----------------------------------------------------------------*/
    /*
    for (i = 0; i < (int)num_fcbk_vecs; i ++)
    {
        for (j = (int)p; j < (int)nos; j += (int)pulse_spacing)
            printf("%3d  ", cb[i][j]);
            
        printf("\n");
    }
    printf("\n");
    */
    /* -----------------------------------------------------------------*/
    return;
}

/*
----------------------------------------------------------------------------
  preselection of fixed codebook indices (Reduction from 16 to 5 WDBxx)
----------------------------------------------------------------------------
*/

void 
PHI_cbf_preselection
(
long  pulse_spacing,    /* In:   Regular Spacing Between Pulses         */
long  num_fcbk_cands,   /* In:   #Preselected candidates for fixed cbk  */
long  num_fcbk_vecs,    /* In:   #Vectors in the fixed code book        */
long  nos,              /* In:    Number of samples to be processed     */
long  **cb,             /* In:    Local fixed codebook,(Nf-1) by (nos-1)*/  
long  p,                /* In:    Phase  of the RPE codebook            */
float *tf,              /* In:    Backward-filtered target signal       */ 
float a,                /* In:    LPC coeffs of the preselection filter */
long  *pi               /* Out:   Result: Preselected Codebook Indices  */
)
{
    float t1, t2, e;                           
    float *rfp;                           
    long is;
    int i, j;

    /*==================================================================*/
    /* Allocate memory for rfp                                          */
    /*==================================================================*/
    if ((rfp = (float *)malloc((unsigned int)num_fcbk_vecs * sizeof(float)))== NULL )
    {
       fprintf(stderr, "\n Malloc Failure in Block:Excitation Anlaysis \n");
       exit(1);
    }
  
    /*==================================================================*/
    /* computes rfp ratios                                              */
    /*==================================================================*/
    for (i = 0; i < (int)num_fcbk_vecs; i ++)
    {
        t1 = t2 = (float)0.0;
        e = FLT_MIN;

        for (j = 0; j < (int)nos; j ++)
        {
            t1  = t1 * a + (float) cb[i][j];
            e  += t1 * t1;
        }

        for (j = (int)p; j < (int)nos; j += (int)pulse_spacing)
        {
            t2 += (float) cb[i][j] * tf[j];
        }

        rfp[i] = (t2 * t2) / e;
    }

    /*==================================================================*/
    /* select Pf indices with largest rfp's                             */
    /*==================================================================*/
    for (j = 0; j < (int)num_fcbk_cands; j ++)
    {
        e = -FLT_MAX;

        for (i = 0; i < (int)num_fcbk_vecs; i ++)
            if (rfp[i] > e)
            {
                e = rfp[i];
                is = (long)i;
            }
        assert(is < num_fcbk_vecs);
        pi[j] = is;

        rfp[is] = -FLT_MAX;
    }
    
    /*==================================================================*/
    /*FREE rfp                                                         */
    /*==================================================================*/
   FREE(rfp);
    return;
}

/*
------------------------------------------------------------------------
  fixed codebook search
------------------------------------------------------------------------
*/

void 
PHI_cbf_search(
long  num_of_pulses,    /* In:  Number of pulses in the sequence        */
long  pulse_spacing,    /* In:  Regular Spacing Between Pulses          */
long  num_fcbk_cands,   /* In:  #Preselected candidates for fixed cbk   */

⌨️ 快捷键说明

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