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

📄 cod_amr.c

📁 AMR-NB 的编码实现,纯C, VC下建立工程即可用.
💻 C
📖 第 1 页 / 共 4 页
字号:
      {         subframePreProc(*usedMode, gamma1, gamma1_12k2, gamma2, A, Aq, &pst->speech[i_subfr],
                                       pst->mem_err, pst->mem_w0, pst->zero, pst->ai_zero, &pst->exc[i_subfr], pst->h1, xn, res, pst->error);
      }      else      { 
         /* MR475 */
         subframePreProc(*usedMode, gamma1, gamma1_12k2, gamma2, A, Aq, &pst->speech[i_subfr],
                                       pst->mem_err, mem_w0_save, pst->zero,  pst->ai_zero, &pst->exc[i_subfr], pst->h1, xn, res, pst->error);
         /* save impulse response (modified in cbsearch) */               if (evenSubfr != 0)         {             memcpy ( h1_sf0,  pst->h1, 80);
         }      }            /* copy the LP residual (res2 is modified in the CL LTP search)    */      memcpy ( res2, res,  80);       /*-----------------------------------------------------------------*       * - Closed-loop LTP search                                        *       *-----------------------------------------------------------------*/      cl_ltp(  pst->clLtpSt, pst->tonStabSt, *usedMode, i_subfr, T_op, pst->h1, 
                  &pst->exc[i_subfr], res2, xn, lsp_flag, xn2, y1,  &T0, &T0_frac, &gain_pit, gCoeff, &ana,  &gp_limit);
      /* update LTP lag history */           if ((subfrNr == 0) && (pst->ol_gain_flg[0] > 0))
      {         pst->old_lags[1] = T0;    
      }                 if ( (subfrNr == 3 ) && (pst->ol_gain_flg[1] > 0))
      {         pst->old_lags[0] = T0;    
      }                  /*-----------------------------------------------------------------*       * - Inovative codebook search (find index and gain)               *       *-----------------------------------------------------------------*/      cbsearch(xn2, pst->h1, T0, pst->sharp, gain_pit, res2,  code, y2, &ana, *usedMode, subfrNr);
                     /*------------------------------------------------------*       * - Quantization of gains.                             *       *------------------------------------------------------*/      gainQuant(pst->gainQuantSt, *usedMode, res, &pst->exc[i_subfr], code,
                          xn, xn2,  y1, y2, gCoeff, evenSubfr, gp_limit,                          &gain_pit_sf0, &gain_code_sf0, &gain_pit, &gain_code, &ana);                /* update gain history */    //  update_gp_clipping(st->tonStabSt, gain_pit);
      memcpy(&pst->tonStabSt->gp[0], &pst->tonStabSt->gp[1],  12  );  
      pst->tonStabSt->gp[N_FRAME-1] = gain_pit>> 3;
               if (*usedMode != MR475 )      {         /* Subframe Post Porcessing */         subframePostProc(pst->speech, *usedMode, i_subfr, gain_pit,
                          gain_code, Aq, psynth, xn, code, y1, y2, pst->mem_syn,
                          pst->mem_err, pst->mem_w0, pst->exc, &pst->sharp);
      }      else      {                if (evenSubfr != 0)         {            i_subfr_sf0 = i_subfr;                     memcpy(xn_sf0,xn,  80);            memcpy(y2_sf0,y2,  80);                      memcpy(code_sf0,code,  80);            T0_sf0 = T0;                                 T0_frac_sf0 = T0_frac;                                     /* Subframe Post Porcessing */            subframePostProc(pst->speech, *usedMode, i_subfr, gain_pit,
                             gain_code, Aq, psynth, xn, code, y1, y2,
                             mem_syn_save, pst->mem_err, mem_w0_save,
                             pst->exc, &pst->sharp);
            pst->sharp = sharp_save;                      
         }         else         {            /* update both subframes for the MR475 */                        /* Restore states for the MR475 mode */            memcpy(pst->mem_err, mem_err_save, M*sizeof(Word16));         
                        /* re-build excitation for sf 0 */            Pred_lt_3or6(&pst->exc[i_subfr_sf0], T0_sf0, T0_frac_sf0,L_SUBFR, 1);
            Convolve(&pst->exc[i_subfr_sf0], h1_sf0, y1, L_SUBFR);
                        Aq -= MP1;            subframePostProc(st->speech, *usedMode, i_subfr_sf0,                             gain_pit_sf0, gain_code_sf0, Aq,                             psynth, xn_sf0, code_sf0, y1, y2_sf0,
                             pst->mem_syn, pst->mem_err, pst->mem_w0, pst->exc,
                             &sharp_save); /* overwrites sharp_save */            Aq += MP1;                        /* re-run pre-processing to get xn right (needed by postproc) */            /* (this also reconstructs the unsharpened h1 for sf 1)       */            subframePreProc(*usedMode, gamma1, gamma1_12k2,                            gamma2, A, Aq, &pst->speech[i_subfr],
                            pst->mem_err, pst->mem_w0, pst->zero,
                            pst->ai_zero, &pst->exc[i_subfr],
                            pst->h1, xn, res, pst->error);
                        /* re-build excitation sf 1 (changed if lag < L_SUBFR) */            Pred_lt_3or6(&pst->exc[i_subfr], T0, T0_frac, L_SUBFR, 1);
            Convolve(&pst->exc[i_subfr], pst->h1, y1, L_SUBFR);
                        subframePostProc(pst->speech, *usedMode, i_subfr, gain_pit,
                             gain_code, Aq, psynth, xn, code, y1, y2,
                             pst->mem_syn, pst->mem_err, pst->mem_w0,
                             pst->exc, &pst->sharp);
         }      }                                          A += MP1;    /* interpolated LPC parameters for next subframe */      Aq += MP1;   }   memcpy(&st->old_exc[0], &st->old_exc[L_FRAME],  (PIT_MAX + L_INTERPOL)*sizeof(Word16));
   the_end:      /*--------------------------------------------------*    * Update signal for next frame.                    *    *--------------------------------------------------*/   memcpy( &pst->old_wsp[0], &pst->old_wsp[L_FRAME], PIT_MAX*sizeof(Word16));  
   memcpy( &pst->old_speech[0], &pst->old_speech[L_FRAME], (L_TOTAL - L_FRAME)*sizeof(Word16));
     return 0;
}

/****************************************************************
 Function     :  subframePreProc
 Description : pre-processing subframe
 Input           :
			mode:                     coder mode       
			gamma1[]:             spectral exp. factor 1         
			gamma1_12k2[]:  spectral exp. factor 1 for EFR  
			gamma2[]:   spectral exp. factor 2            
			A:                   A(z) unquantized for the 4 subframes 
			Aq:                 A(z)   quantized for the 4 subframes  
			speech:          speech segment                     
			mem_err :      pointer to error signal         
			mem_w0:       memory of weighting filter     
			zero :              pointer to zero vector   
Output:
			ai_zero[] :   history of weighted synth. filter
			exc[]        :   long term prediction residual      
			h1[]         :   impulse response                     
			xn[]         :   target vector for pitch search      
			res2[]      :   long term prediction residual        
			error[]     :   error of LPC synthesis filter        
 Return:
*****************************************************************/
int subframePreProc(Mode mode,    const Word16 gamma1[],const Word16 gamma1_12k2[],const Word16 gamma2[], 
    Word16 *A, Word16 *Aq,Word16 *speech,Word16 *mem_err, Word16 *mem_w0,  Word16 *zero,    
    Word16 ai_zero[],  Word16 exc[],   Word16 h1[], Word16 xn[],   Word16 res2[],  Word16 error[])
{

	Word16 Ap1[MP1];              /* A(z) with spectral expansion         */
	Word16 Ap2[MP1];              /* A(z) with spectral expansion         */
	const Word16 *g1,*pgamma2 = gamma2;             /* Pointer to correct gammma1 vector    */

	Word16* pA = A,*pAq = Aq, *pspeech = speech,*pmem_err = mem_err,*pmem_w0 = mem_w0;
	Word16*pzero = zero, *pai_zero =ai_zero, *pexc = exc, *ph1 = h1, *pxn =xn,*pres2 = res2, *perror = error;

	/*---------------------------------------------------------------*
	* mode specific pointer to gamma1 values                        *
	*---------------------------------------------------------------*/
	g1 = (mode==MR122) ||(mode== MR102) ? gamma1_12k2 : gamma1 ; 

	/*---------------------------------------------------------------*
	* Find the weighted LPC coefficients for the weighting filter.  *
	*---------------------------------------------------------------*/
	//   Weight_Ai(pA, g1, Ap1);

	Ap1[0] = pA[0];    
	Ap1[1] = (Word16)(( (pA[1]*g1[0] << 1) + 0x00008000L) >> 16) ;
	Ap1[2] = (Word16)(( (pA[2]*g1[1] << 1) + 0x00008000L) >> 16) ;  
	Ap1[3] = (Word16)(( (pA[3]*g1[2] << 1) + 0x00008000L) >> 16) ;  
	Ap1[4] = (Word16)(( (pA[4]*g1[3] << 1) + 0x00008000L) >> 16) ;  
	Ap1[5] = (Word16)(( (pA[5]*g1[4] << 1) + 0x00008000L) >> 16) ;  
	Ap1[6] = (Word16)(( (pA[6]*g1[5] << 1) + 0x00008000L) >> 16) ;  
	Ap1[7] = (Word16)(( (pA[7]*g1[6] << 1) + 0x00008000L) >> 16) ;  
	Ap1[8] = (Word16)(( (pA[8]*g1[7] << 1) + 0x00008000L) >> 16) ;  
	Ap1[9] = (Word16)(( (pA[9]*g1[8] << 1) + 0x00008000L) >> 16) ;  
	Ap1[10] = (Word16)(( (pA[10]*g1[9] << 1) + 0x00008000L) >> 16) ;  
		
	//Weight_Ai(pA, pgamma2, Ap2);
	Ap2[0] = pA[0];    
	Ap2[1] = (Word16)(( (pA[1]*pgamma2[0] << 1) + 0x00008000L) >> 16) ;
	Ap2[2] = (Word16)(( (pA[2]*pgamma2[1] << 1) + 0x00008000L) >> 16) ;  
	Ap2[3] = (Word16)(( (pA[3]*pgamma2[2] << 1) + 0x00008000L) >> 16) ;  
	Ap2[4] = (Word16)(( (pA[4]*pgamma2[3] << 1) + 0x00008000L) >> 16) ;  
	Ap2[5] = (Word16)(( (pA[5]*pgamma2[4] << 1) + 0x00008000L) >> 16) ;  
	Ap2[6] = (Word16)(( (pA[6]*pgamma2[5] << 1) + 0x00008000L) >> 16) ;  
	Ap2[7] = (Word16)(( (pA[7]*pgamma2[6] << 1) + 0x00008000L) >> 16) ;  
	Ap2[8] = (Word16)(( (pA[8]*pgamma2[7] << 1) + 0x00008000L) >> 16) ;  
	Ap2[9] = (Word16)(( (pA[9]*pgamma2[8] << 1) + 0x00008000L) >> 16) ;  
	Ap2[10] = (Word16)(( (pA[10]*pgamma2[9] << 1) + 0x00008000L) >> 16) ;  

	/*---------------------------------------------------------------*
	* Compute impulse response, h1[], of weighted synthesis filter  *
	*---------------------------------------------------------------*/

	memcpy(&pai_zero[0], &Ap1[0], 22);

	Syn_filt(pAq, pai_zero, ph1, L_SUBFR, pzero, 0);
	Syn_filt(Ap2, ph1, ph1, L_SUBFR, pzero, 0);

	/*------------------------------------------------------------------------*
	*                                                                        *
	*          Find the target vector for pitch search:                      *
	*                                                                        *
	*------------------------------------------------------------------------*/

	/* LPC residual */
	Residu(pAq, pspeech, pres2, L_SUBFR); 
	memcpy(pexc, pres2, 80);

	Syn_filt(pAq, pexc, perror, L_SUBFR, pmem_err, 0);
	Residu(Ap1, perror, pxn, L_SUBFR);

	/* target signal xn[]*/
	Syn_filt(Ap2, pxn, pxn, L_SUBFR, pmem_w0, 0);    

	return 0;
}

/****************************************************************
 Function     :  subframePostProc
 Description : pre-processing subframe
 Input           :
		speech  : speech segment        
		mode     : coder mode                  
		i_subfr  : Subframe nr         
		gain_pit: Pitch gain                       Q14  
		gain_code : Decoded innovation gain           
		Aq              : A(z) quantized for the 4 subframes 
		synth[]      : Local snthesis  
		xn[]           : Target vector for pitch search 
		code[]       : Fixed codebook exitation 
		y1[]           : Filtered adaptive exitation 
		y2[]           : Filtered fixed codebook excitation 
		mem_syn   : memory of synthesis filter 
Output:
		mem_syn   : memory of synthesis filter        
		mem_err    : pointer to error signal        
		mem_w0   : memory of weighting filter       
		exc             : long term prediction residual   
		sharp         : pitch sharpening value 
 Return:
*****************************************************************/

int subframePostProc(    Word16 *speech, Mode mode,Word16 i_subfr,Word16 gain_pit,Word16 gain_code, Word16 *Aq,
    Word16 synth[], Word16 xn[],  Word16 code[], Word16 y1[],  Word16 y2[],  
    Word16 *mem_syn, Word16 *mem_err,  Word16 *mem_w0, Word16 *exc, Word16 *sharp )
{

	Word16 i;
	Word16 temp,t1,t2,t3,t4,t5;
	Word32 L_temp,L_temp1,L_temp2,L_temp3;
	Word16 tempShift;
	Word16 kShift;
	Word16 pitch_fac;

	Word16  *pspeech = speech, *pAq = Aq, *psynth = synth, *pxn = xn,*pcode = code,*py1 = y1,*py2 = y2;
	Word16  *pmem_syn = mem_syn, *pmem_err = mem_err, *pmem_w0 = mem_w0, *pexc = exc, *psharp = sharp;


	temp = (mode !=  MR122);
	tempShift = temp ? 1 : 2;
	kShift      =  temp ? 2 : 4;
	pitch_fac  =  temp ? gain_pit : gain_pit>>1;

	/*------------------------------------------------------------*
	* - Update pitch sharpening "sharp" with quantized gain_pit  *
	*------------------------------------------------------------*/

	*psharp = (gain_pit > SHARPMAX) ? SHARPMAX : gain_pit;
	/*------------------------------------------------------*
	* - 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[])             *
	*------------------------------------------------------*/

	temp = 1+tempShift;  
	for (i = 0; i < L_SUBFR; i+=4 ) 
	{
		t1 = i + i_subfr;   t2 = i+1 + i_subfr;    t3 = i+2 + i_subfr;  t4 = i+3 + i_subfr;

		L_temp =    (pexc[t1]*pitch_fac  + pcode[i]* gain_code)<< temp;
		L_temp1 =  (pexc[t2]*pitch_fac  + pcode[i+1]* gain_code)<< temp;
		L_temp2 =  (pexc[t3]*pitch_fac  + pcode[i+2]* gain_code)<< temp;
		L_temp3 =  (pexc[t4]*pitch_fac  + pcode[i+3]* gain_code)<< temp;

		pexc[t1] = (L_temp + 0x00008000)>>16;      
		pexc[t2] = (L_temp1 + 0x00008000)>>16;      
		pexc[t3] = (L_temp2 + 0x00008000)>>16;      
		pexc[t4] = (L_temp3 + 0x00008000)>>16;      

	}
	Syn_filt(Aq, &exc[i_subfr], &synth[i_subfr], L_SUBFR, mem_syn, 1);
	temp = kShift+1;

	t1 = 30+ i_subfr;   t2 = 31 + i_subfr;    t3 = 32 + i_subfr;  t4 = 33 + i_subfr; t5 = 34 + i_subfr;	  
	pmem_err[0] = pspeech[t1] - psynth[t1];   
	pmem_err[1] = pspeech[t2] - psynth[t2];    
	pmem_err[2] = pspeech[t3] - psynth[t3];    
	pmem_err[3] = pspeech[t4] - psynth[t4];
	pmem_err[4] = pspeech[t5] - psynth[t5];   

	t1 = 35+ i_subfr;   t2 = 36 + i_subfr;    t3 = 37 + i_subfr;  t4 = 38 + i_subfr; t5 = 39 + i_subfr;	  
	pmem_err[5] = pspeech[t1] - psynth[t1];   
	pmem_err[6] = pspeech[t2] - psynth[t2];    
	pmem_err[7] = pspeech[t3] - psynth[t3];    
	pmem_err[8] = pspeech[t4] - psynth[t4];
	pmem_err[9] = pspeech[t5] - psynth[t5];   

	pmem_w0[0] =  pxn[30]- (py1[30]*gain_pit >> 14) - ((py2[30] *gain_code) <<temp >>16);      
	pmem_w0[1] =  pxn[31]- (py1[31]*gain_pit >> 14) - ((py2[31] *gain_code) <<temp >>16);     
	pmem_w0[2] =  pxn[32]- (py1[32]*gain_pit >> 14) - ((py2[32] *gain_code) <<temp >>16);     
	pmem_w0[3] =  pxn[33]- (py1[33]*gain_pit >> 14) - ((py2[33] *gain_code) <<temp >>16);
	pmem_w0[4] =  pxn[34]- (py1[34]*gain_pit >> 14) - ((py2[34] *gain_code) <<temp >>16);  
	pmem_w0[5]  = pxn[35]- (py1[35]*gain_pit >> 14) - ((py2[35] *gain_code) <<temp >>16);      
	pmem_w0[6] =  pxn[36]- (py1[36]*gain_pit >> 14) - ((py2[36] *gain_code) <<temp >>16);     
	pmem_w0[7] =  pxn[37]- (py1[37]*gain_pit >> 14) - ((py2[37] *gain_code) <<temp >>16);     
	pmem_w0[8] =  pxn[38]- (py1[38]*gain_pit >> 14) - ((py2[38] *gain_code) <<temp>>16);
	pmem_w0[9] =  pxn[39]- (py1[39]*gain_pit >> 14) - ((py2[39] *gain_code) <<temp >>16); 

	return 0;

}



⌨️ 快捷键说明

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