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

📄 phi_xits.c

📁 MPEG-4编解码的实现(包括MPEG4视音频编解码)
💻 C
📖 第 1 页 / 共 3 页
字号:
long  nos,              /* In:  Number of samples to be processed       */
long  **cb,             /* In:  Local fixed codebook                    */
long  p,                /* In:  Phase of the fixed codebook, 0 to D-1   */
long  *pi,              /* In:  Preselected codebook indices, p[0..Pf-1]*/
float *h,               /* In:  Synthesis filter impulse response,      */
float *e,               /* In:  Target residual signal, e[0..nos-1]     */
float *gain,            /* Out: Selected Fixed Codebook gain (Uncoded)  */ 
long  *gid,             /* Out: Selected Fixed Codebook gain index      */
long  *amp,             /* Out: S rpe pulse amplitudes, amp[0..Np-1]    */
long  n                 /* In:  Subframe index, 0 to n_subframes-1      */
)
{
    static float gp ;                            
    float *y;                            
    float num, den;
    float g, r, rs;
    int ks;
    int i, j, k, m;
        

    rs = -FLT_MAX;

    /*==================================================================*/
    /* Allocate temp states                                             */
    /*==================================================================*/
    if ((y = (float *)malloc((unsigned int)nos * sizeof(float))) == NULL )
    {
       fprintf(stderr, "\n Malloc Failure in Block: Excitation Anlaysis \n");
       exit(1);
    }
    for (k = 0; k < (int)num_fcbk_cands; k ++)
    {
        for (i = 0; i < (int)nos; i ++)
        {
            register float temp_y = (float)0.0;
            for (j = (int)p; j <= i; j += (int)pulse_spacing)
            {
                temp_y += h[i - j] * (float) cb[pi[k]][j];
            }
            y[i] = temp_y;
        }

        num = (float)0.0;
        den = FLT_MIN;
        for (i = 0; i < (int)nos; i ++)
        {
            num += e[i] * y[i];
            den += y[i] * y[i];
        }
        g = num / den;
        
        if (!n)
        {
            for (m = 0; g > (float)tbl_cbf_dir[m].dec && m < QLf - 1; m ++);
            g = (float)tbl_cbf_dir[m].rep;
        }
        else
        {
            g /= gp;
            for (m = 0; g > (float)tbl_cbf_dif[m].dec && m < QLfd - 1; m ++);
            g = gp * (float)tbl_cbf_dif[m].rep;
        }

        r = (float)2.0 * g * num - g * g * den;
        if (r > rs)
        {
            rs = r;
            ks = k;
            *gid = (long)m;
            *gain = g;
        }
    }
    
    for (k = (int)p, i = 0; i < (int)num_of_pulses; k += (int)pulse_spacing, i ++)
    {
        amp[i] = cb[pi[ks]][k];
    }
    gp = *gain;

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

    return;
}

/*
----------------------------------------------------------------------------
  encodes rpe pulse amplitudes and phase into one index  
----------------------------------------------------------------------------
*/

void 
PHI_code_cbf_amplitude_phase
(
long num_of_pulses,      /* In:     Number of pulses in the sequence     */
long pulse_spacing,      /* In:     Regular Spacing Between Pulses       */
long *amp,               /* In:   Array of Pulse Amplitudes, amp[0..Np-1]*/
long phase,              /* In:   The Phase of the RPE sequence          */
long *index              /* Out:  Coded Index: Fixed Codebook index      */
)
{
    int i;
    long ac = 0;

    for (i = 0; i < (int)num_of_pulses; i ++)
    {
        ac = (ac * 3) + (amp[i] == -1 ? (long)0 : (amp[i] ==  1 ? (long)1 : (long)2));
    }
    ac *= pulse_spacing;
    
    *index = ac + phase;

    return;
}

/*
----------------------------------------------------------------------------
  Decodes the RPE amplitudes and phase from the cbk-index
----------------------------------------------------------------------------
*/
void
PHI_decode_cbf_amplitude_phase
(
const long    num_of_pulses,  /* In:     Number of pulses in the sequence     */
const long    pulse_spacing,  /* In:     Regular Spacing Between Pulses       */
long  * const amp,            /* Out:  The Array of pulse amplitudes          */
long  * const phase,          /* Out:  The phase of the RPE sequence          */ 
const long    index           /* In:   Coded Fixed codebook index             */
)
{
    int  i;
    long ac;

    /* =================================================================*/
    /* Extract the phase First                                          */
    /* =================================================================*/
    *phase = (index % pulse_spacing);
    ac = index - *phase;
    ac /= pulse_spacing;

    /* =================================================================*/
    /* The remainder is used to extract the amplitude values            */
    /* =================================================================*/
    for (i = (int)num_of_pulses - 1; i >= 0; i--)
    {
        amp[i]  =  (ac % 3);
        ac     /= 3;
        
        /*==============================================================*/
        /* Only 3 amplitude values are permitted                        */
        /*==============================================================*/
        if (amp[i] == 0)
            amp[i] = -1;
        else if (amp[i] == 2)
            amp[i] = 0;
        else if (amp[i] == 1)
            amp[i] = 1;
        else        
        {
            fprintf(stderr, "FATAL ERROR: Unpermitted Amplitude Value \n");
            exit(1);
        }
    }
    return;
}

/*
----------------------------------------------------------------------------
  Decodes the Adaptive-Codebook Gain
----------------------------------------------------------------------------
*/
void
PHI_DecodeAcbkGain
(
long  acbk_gain_index,
float *gain
)
{
    int i, j;
    if (acbk_gain_index > 31)
    {
        acbk_gain_index = -(64 - acbk_gain_index);
    }
    
    if (acbk_gain_index < 0)
    {
        i = -1; 
        j = (-1 * (int)acbk_gain_index) - 1;
    }
    else
    {
        i = 1;
        j = (int)acbk_gain_index;
    }
    *gain = (float)i * (float)tbl_cba_dir[j].rep;
}

/*
----------------------------------------------------------------------------
  Decodes the Fixed-Codebook Gain
----------------------------------------------------------------------------
*/
void
PHI_DecodeFcbkGain
(
long  fcbk_gain_index,
long  ctr,
float prev_gain, 
float *gain
)
{
    if (ctr == 0)
    {
        *gain = (float)tbl_cbf_dir[fcbk_gain_index].rep;
    }
    else
    {
        *gain = (float)tbl_cbf_dif[fcbk_gain_index].rep * prev_gain;
    }
}
/*
------------------------------------------------------------------------
  computes excitation of the adaptive codebook
------------------------------------------------------------------------
*/

void 
PHI_calc_cba_excitation
(
long   nos,             /* In:     Number of samples to be updated      */
long   max_lag,         /* In:     Maximum Permitted Adapt cbk Lag      */
long   min_lag,         /* In:     Minimum Permitted Adapt cbk Lag      */
float  *cb,             /* In:     The current Adaptive codebook content*/ 
long   idx,             /* In:     The chosen lag candidate             */
float  *v               /* Out:    The Adaptive Codebook contribution   */
)
{
    int i, j;

    i = (int)(max_lag - min_lag - idx);

    for (j = 0; j < (int)nos; j ++)
        v[j] = cb[i + j];

    return;
}


/*
----------------------------------------------------------------------------
  computes excitation of the fixed codebook
----------------------------------------------------------------------------
*/

void 
PHI_calc_cbf_excitation
(
long   nos,             /* In:     Number of samples to be updated      */
long   num_of_pulses,   /* In:     Number of pulses in the sequence     */
long   pulse_spacing,   /* In:     Regular Spacing Between Pulses       */
long   *amp,            /* In:     Aray of RPE pulse amplitudes         */
long   p,               /* In:     Phase of the RPE sequence            */
float  *v               /* Out:    The fixed codebook contribution      */
)
{
    int i,k;

    for (i = 0; i < (int)nos; i ++)
        v[i] = (float)0;

    for (k = 0, i = (int)p; k < (int)num_of_pulses; i += (int)pulse_spacing, k++)
        v[i] = (float)amp[k];

    return;
}

/*
----------------------------------------------------------------------------
  compute the sum of the excitations
----------------------------------------------------------------------------
*/

void 
PHI_sum_excitations 
( 
long  nos,             /* In:     Number of samples to be updated      */
float again,           /* In:     Adaptive Codebook gain               */ 
float *asig,           /* In:     Adaptive Codebook Contribution       */ 
float fgain,           /* In:     Fixed Codebook gain                  */  
float *fsig,           /* In:     Fixed Codebook Contribution          */ 
float *sum_sig         /* Out:    The Excitation sequence              */
)
{
  int k;

  for(k = 0; k < (int)nos; k++)
  {
     sum_sig[k] = again * asig[k] + fgain * fsig[k];
  }
  return;
}


/*
----------------------------------------------------------------------------
  update adaptive codebook with the total excitation computed
----------------------------------------------------------------------------
*/

void 
PHI_update_cba_memory
(
long   nos,             /* In:     Number of samples to be updated      */
long   max_lag,         /* In:     Maximum Adaptive Codebook Lag        */
float *cb,              /* In/Out: Adaptive Codebook                    */ 
float *vi               /* In:     Sum of adaptive and fixed excitaion  */
)
{
    int i;                        

    for (i = (int)nos; i < (int)max_lag; i ++)
        cb[i - (int)nos] = cb[i];

    for (i = 0; i < (int)nos; i ++)
        cb[(int)max_lag - 1 - i] = vi[(int)nos - 1 - i];

    return;
}

/*
---------------------------------------------------------------------------
  update synthesis filter states
----------------------------------------------------------------------------
*/

void 
PHI_update_filter_states
(
long   nos,             /* In:     Number of samples                    */
long   order,           /* In:     Order of the LPC                     */ 
float *vi,              /* In:     Total Codebook contribution          */
float *vp,              /* In/Out: Filter States, vp[0..order-1]        */ 
float *a                /* In:     Lpc Coefficients, a[0..order-1]      */
)
{
    float v;                       
    int i, j;

    for (i = 0; i < (int)nos; i ++)
    {
        v = vi[i];

        for (j = 0; j < (int)order; j ++)
            v += a[j] * vp[j];

        for (j = (int)order - 1; j > 0; j --)
            vp[j] = vp[j - 1];
        vp[0] = v;
    }

    return;
}
/*======================================================================*/
/*      H I S T O R Y                                                   */
/*======================================================================*/
/* 17-04-96 R. Taori  Initial Version                                   */
/* 13-08-96 R. Taori  Added 2 subroutines CompAmpArray CompPosArray     */
/*                    Modified generate_cbf to reflect the above        */


⌨️ 快捷键说明

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