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

📄 gain_q.c

📁 AMR-NB 的编码实现,纯C, VC下建立工程即可用.
💻 C
📖 第 1 页 / 共 4 页
字号:
 Description   :  MA prediction of the innovation energy  (in dB/(20*log10(2))) with mean  removed).
 Input            :
            st       : State struct                  
           mode  : AMR mode    . MR122: Q12, other modes: Q13     
Output           :
	exp_gcode0   : exponent of predicted gain factor, Q0  
	frac_gcode0   : fraction of predicted gain factor  Q15 
	exp_en           : exponent of innovation energy,     Q0  (only calculated for MR795)        
	frac_en           : fraction of innovation energy,   
Return:

 *************************************************************************/voidgc_pred( gc_predState *st, Mode mode, Word16 *code,   
    Word16 *exp_gcode0,Word16 *frac_gcode0,Word16 *exp_en, Word16 *frac_en  )
{    Word16 i;
    Word32 ener_code;    Word16 exp, frac;    Word32 ener;

   gc_predState *pst = st;
   Word16 *pcode = code, *pexp_gcode0 = exp_gcode0, *pfrac_gcode0 = frac_gcode0, *pexp_en = exp_en, *pfrac_en = frac_en;
   
	    /*-------------------------------------------------------------------*     *  energy of code:                                                  *     *  ~~~~~~~~~~~~~~~                                                  *     *  ener_code = sum(code[i]^2)                                       *     *-------------------------------------------------------------------*/   // ener_code = L_mac((Word32) 0, code[0], code[0]);  
    ener_code = 0;                                         
    for (i = 0; i < L_SUBFR; i++)
    {
        ener_code += pcode[i]*pcode[i]<<1 ;
	 if(ener_code < 0) 	 {  
	 	 ener_code = MAX_32; break;	 }
    }  
        if ( mode == MR122)    {
        /* ener_code = ener_code / lcode; lcode = 40; 1/40 = 26214 Q20       */       // ener_code = L_mult (round (ener_code), 26214);   /* Q9  * Q20 -> Q30 */               ener_code = ( ener_code >0 )&&(ener_code+0x00008000)<0 ? 0x7fff: (ener_code+0x00008000)>>16;	 ener_code = ener_code*26214 != 0x40000000 ? ener_code*52428: MAX_32;	         /*-------------------------------------------------------------------*         *  energy of code:                                                  *         *  ~~~~~~~~~~~~~~~                                                  *         *  ener_code(Q17) = 10 * Log10(energy) / constant                   *         *                 = 1/2 * Log2(energy)                              *         *                                           constant = 20*Log10(2)  *         *-------------------------------------------------------------------*/        /* ener_code = 1/2 * Log2(ener_code); Note: Log2=log2+30 */        Log2(ener_code, &exp, &frac);     
        ener_code = ((exp-30)<<16) + (frac<<1);	                                                   
        /*-------------------------------------------------------------------*         *  predicted energy:                                                *         *  ~~~~~~~~~~~~~~~~~                                                *         *  ener(Q24) = (Emean + sum{pred[i]*past_en[i]})/constant           *         *            = MEAN_ENER + sum(pred[i]*past_qua_en[i])              *         *                                           constant = 20*Log10(2)  *         *-------------------------------------------------------------------*/        ener = MEAN_ENER_MR122; 
	 ener += ( pst->past_qua_en_MR122[0]* 44 +  pst->past_qua_en_MR122[1]* 37 + pst->past_qua_en_MR122[2]* 22 + pst->past_qua_en_MR122[3]*12)<<1;

        /*-------------------------------------------------------------------*         *  predicted codebook gain                                          *         *  ~~~~~~~~~~~~~~~~~~~~~~~                                          *         *  gc0     = Pow10( (ener*constant - ener_code*constant) / 20 )     *         *          = Pow2(ener-ener_code)                                   *         *          = Pow2(int(d)+frac(d))                                   *         *                                                                   *         *  (store exp and frac for pow2())                                  *         *-------------------------------------------------------------------*/
		ener =(ener -ener_code)>>1;                /* Q16 */         
	      *pexp_gcode0 = (Word16)(ener>>16);
	      *pfrac_gcode0 = (ener - (*pexp_gcode0<<16))>>1;
		  	        }    else /* all modes except 12.2 */    {        Word32 L_tmp;        Word16 exp_code, gcode0;                /*-----------------------------------------------------------------*         *  Compute: means_ener - 10log10(ener_code/ L_sufr)               *         *-----------------------------------------------------------------*/        exp_code  = norm_l (ener_code);
	 ener_code <<= exp_code;	 
        Log2_norm (ener_code, exp_code, &exp, &frac);
        L_tmp = (-24660*exp<<1) + ( (-24660*frac>>15) <<1 );	        /*   L_tmp = means_ener - 10log10(ener_code/L_SUBFR)         *         = means_ener - 10log10(ener_code) + 10log10(L_SUBFR)         *         = K - fact * Log2(ener_code)         *         = K - fact * log2(ener_code) - fact*27         *         *   ==> K = means_ener + fact*27 + 10log10(L_SUBFR)         *         *   means_ener =       33    =  540672    Q14  (MR475, MR515, MR59)         *   means_ener =       28.75 =  471040    Q14  (MR67)         *   means_ener =       30    =  491520    Q14  (MR74)         *   means_ener =       36    =  589824    Q14  (MR795)         *   means_ener =       33    =  540672    Q14  (MR102)                  *   10log10(L_SUBFR) = 16.02 =  262481.51 Q14         *   fact * 27                = 1331640    Q14         *   -----------------------------------------         *   (MR475, MR515, MR59)   K = 2134793.51 Q14 ~= 16678 * 64 * 2         *   (MR67)                 K = 2065161.51 Q14 ~= 32268 * 32 * 2         *   (MR74)                 K = 2085641.51 Q14 ~= 32588 * 32 * 2         *   (MR795)                K = 2183945.51 Q14 ~= 17062 * 64 * 2         *   (MR102)                K = 2134793.51 Q14 ~= 16678 * 64 * 2                  */        if (mode == MR102)        {
	       L_tmp +=  16678<<7;        }        else if ( mode ==  MR795)        {
            *pfrac_en = (Word16)(ener_code>>16); 
            *pexp_en = -11- exp_code;   
            /* mean = 36 dB */
            L_tmp +=  17062<<7;			        }        else if (mode == MR74 )        {            /* mean = 30 dB */
	     L_tmp +=  32588<<6 ;        }        else if (mode == MR67  )        {            /* mean = 28.75 dB */
            L_tmp +=  32268<<6 ;	}        else /* MR59, MR515, MR475 */        {            /* mean = 33 dB */
	     L_tmp += 16678<<7;        }                /*-----------------------------------------------------------------*         * Compute gcode0.                                                 *         *  = Sum(i=0,3) pred[i]*past_qua_en[i] - ener_code + mean_ener    *         *-----------------------------------------------------------------*/
	 L_tmp <<= 10 ;
        L_tmp += (5571*pst->past_qua_en[0] + 4751*pst->past_qua_en[1] + 2785*pst->past_qua_en[2] + 1556*pst->past_qua_en[3])<< 1;
		                                                              /* Q13 * Q10 -> Q24 */
        gcode0 = (Word16)(L_tmp>>16 );                  /* Q8  */
        /*-----------------------------------------------------------------*         * gcode0 = pow(10.0, gcode0/20)                                   *         *        = pow(2, 3.3219*gcode0/20)                               *         *        = pow(2, 0.166*gcode0)                                   *         *-----------------------------------------------------------------*/        /* 5439 Q15 = 0.165985                                        */        /* (correct: 1/(20*log10(2)) 0.166096 = 5443 Q15)             */
        L_tmp =( mode ==  MR74) ? (gcode0*5439 <<1 ) :(gcode0*5443<<1);
		
        L_tmp >>= 8 ;                   /*          -> Q16 */     
	 *pexp_gcode0  = (Word16)(L_tmp>>16);
	 *pfrac_gcode0 = (L_tmp - (*pexp_gcode0<<16))>>1;
	     }
}

/************************************************************************* * *  Function:   G_code
 * *  Purpose:  Compute the innovative codebook gain.
 * *  Description:
 *      The innovative codebook gain is given by * *              g = <x[], y[]> / <y[], y[]> * *      where x[] is the target vector, y[] is the filtered innovative *      codevector, and <> denotes dot product. *
     Input:
     Output:
     Return:
     
 *************************************************************************/Word16 G_code ( Word16 xn2[], Word16 y2[] )
{
    Word16 i,t1,t2,t3,t4;
    Word16 xy, yy, exp_xy, exp_yy, gain;
    Word32 s, s1;

    Word16 *pxn2 = xn2, *py2 = y2;
 
    s = 1L;   s1 = 0L;                             
    for (i = 0; i < L_SUBFR; i+=4 )
    {         t1 = py2[i]>>1; t2 = py2[i+1]>>1; t3 = py2[i+2]>>1; t4 = py2[i+3]>>1;  
         s   +=    (pxn2[i]*t1 +pxn2[i+1]*t2+pxn2[i+2]*t3+pxn2[i+3]*t4)<<1;
    }
	
    exp_xy = norm_l (s);    xy =(Word16)((s<<exp_xy)>>16);   
    if (xy <= 0)        return ((Word16) 0);
	
    for (i = 0; i < L_SUBFR; i+=4 )
    {         t1 = py2[i]>>1; t2 = py2[i+1]>>1; t3 = py2[i+2]>>1; t4 = py2[i+3]>>1;  
         s1 +=  ( t1*t1 + t2*t2 + t3*t3 +  t4*t4 )<<1;
		
    }
    
    exp_yy = norm_l (s1);
    yy =(Word16)((s1<<exp_yy)>>16);
    xy >>= 1 ;              
    gain = div_s (xy, yy);
    i =  exp_xy + 5- exp_yy;
    gain = ( (gain>> i)<<1);    
    return (gain);
}


/*************************************************************************
  Function      :  Qua_gain()
 
  Description : Quantization of pitch and codebook gains.(using predicted codebook gain)
 
  Input:
		mode               : AMR mode                        
		exp_gcode0    : predicted CB gain (exponent),      Q0 
		frac_gcode0   : predicted CB gain (fraction),      Q15 
		frac_coeff[]    : energy coeff. (5), fraction part,  Q15 
		exp_coeff[]    : energy coeff. (5), exponent part,  Q0  
		gp_limit           : pitch gain limit                
		
  Output:
		gain_pit          : Pitch gain,                        Q14 
		gain_cod         : Code gain,                         Q1  
		qua_ener_MR122 : quantized energy error,            Q10 
		qua_ener               : quantized energy error,            Q10 
		
  Return:
              index of quantization

    
 *************************************************************************/Word16 Qua_gain( Mode mode,  Word16 exp_gcode0,Word16 frac_gcode0, Word16 frac_coeff[],Word16 exp_coeff[],  
    Word16 gp_limit,  Word16 *gain_pit,   Word16 *gain_cod,  Word16 *qua_ener_MR122, Word16 *qua_ener    )
{
    const Word16 *p;
    Word16 i, j, index = 0;
    Word16 gcode0, e_max, exp_code;
    Word16 g_pitch, g2_pitch, g_code, g2_code, g_pit_cod;
    Word16 coeff[5], coeff_lo[5];
    Word16 exp_max[5];
    Word32 L_tmp, dist_min;
    const Word16 *table_gain;
    Word16 table_len,t1,t2;

    Word16 *pexp_coeff = exp_coeff, *pfrac_coeff = frac_coeff;
	

  L_tmp = ((mode== MR102) ||(mode== MR74) || (mode==MR67) ) ;
  
  table_len   = L_tmp ? VQ_SIZE_HIGHRATES : VQ_SIZE_LOWRATES;
  table_gain = L_tmp ? table_gain_highrates : table_gain_lowrates;

    /*-------------------------------------------------------------------*
     *  predicted codebook gain                                          *
     *  ~~~~~~~~~~~~~~~~~~~~~~~                                          *
     *  gc0     = 2^exp_gcode0 + 2^frac_gcode0                           *
     *                                                                   *
     *  gcode0 (Q14) = 2^14*2^frac_gcode0 = gc0 * 2^(14-exp_gcode0)      *
     *-------------------------------------------------------------------*/

    gcode0 =(Word16)Pow2(14, frac_gcode0);

    /*-------------------------------------------------------------------*
     *  Scaling considerations:                                          *
     *  ~~~~~~~~~~~~~~~~~~~~~~~                                          *
     *-------------------------------------------------------------------*/

    /*
     * The error energy (sum) to be minimized consists of five terms, t[0..4].
     *
     *                      t[0] =    gp^2  * <y1 y1>
     *                      t[1] = -2*gp    * <xn y1>
     *                      t[2] =    gc^2  * <y2 y2>
     *                      t[3] = -2*gc    * <xn y2>
     *                      t[4] =  2*gp*gc * <y1 y2>
     *
     */

⌨️ 快捷键说明

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