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

📄 cod_amr.c

📁 AMR-NB 的编码实现,纯C, VC下建立工程即可用.
💻 C
📖 第 1 页 / 共 4 页
字号:
	((*state)->gainQuantSt)->adaptSt = NULL;	/* deallocate memory */	free((*state)->gainQuantSt);	(*state)->gainQuantSt = NULL;		//cl_ltp_exit(&(*state)->clLtpSt);	if (&(*state)->clLtpSt == NULL || (*state)->clLtpSt == NULL)		return;	/* dealloc members */	//Pitch_fr_exit(&(*state)->pitchSt);	if (&((*state)->clLtpSt)->pitchSt == NULL || ((*state)->clLtpSt)->pitchSt == NULL)		return;	/* deallocate memory */	free(((*state)->clLtpSt)->pitchSt);	((*state)->clLtpSt)->pitchSt = NULL;	/* deallocate memory */	free((*state)->clLtpSt);	(*state)->clLtpSt = NULL;	//  p_ol_wgh_exit(&(*state)->pitchOLWghtSt);	if (&(*state)->pitchOLWghtSt == NULL || (*state)->pitchOLWghtSt == NULL)		return;	/* deallocate memory */	free((*state)->pitchOLWghtSt);	(*state)->pitchOLWghtSt = NULL;	//  ton_stab_exit(&(*state)->tonStabSt);	if (&(*state)->tonStabSt == NULL || (*state)->tonStabSt == NULL)		return;	/* deallocate memory */	free((*state)->tonStabSt);	(*state)->tonStabSt = NULL;      #ifndef VAD2   vad1_exit(&(*state)->vadSt);#else   vad2_exit(&(*state)->vadSt);#endif    dtx_enc_exit(&(*state)->dtx_encSt);   /* deallocate memory */   free(*state);   *state = NULL;   
   return;}
/***************************************************************************    Function: cod_amr 
    Purpose:  Main encoder routine.
 
    Description: This function is called every 20 ms speech frame,
        operating on the newly read 160 speech samples. It performs the
        principle encoding functions to produce the set of encoded parameters
        which include the LSP, adaptive codebook, and fixed codebook
        quantization indices (addresses and gains).
 
    Input:
              st  : state struct pointer
              mode : AMR mode
              new_speech  : speech input
 
    Output:
 
                  ana[]         :     vector of analysis parameters.
                  usedMode :     used mode
                  synth[]      :     Local synthesis speech (for debugging purposes)
      Return:
      
  ***************************************************************************/
int cod_amr( cod_amrState *st,  Mode mode,  Word16 new_speech[], Word16 ana[],  Mode *usedMode, Word16 synth[] 
){
	/* LPC coefficients */
	Word16 A_t[(MP1) * 4];       /* A(z) unquantized for the 4 subframes */
	Word16 Aq_t[(MP1) * 4];     /* A(z)   quantized for the 4 subframes */
	Word16 *A, *Aq;                 /* Pointer on A_t and Aq_t              */
	Word16 lsp_new[M];

	/* Other vectors */
	Word16 xn[L_SUBFR];           /* Target vector for pitch search       */
	Word16 xn2[L_SUBFR];         /* Target vector for codebook search    */
	Word16 code[L_SUBFR];       /* Fixed codebook excitation            */
	Word16 y1[L_SUBFR];           /* Filtered adaptive excitation         */
	Word16 y2[L_SUBFR];           /* Filtered fixed codebook excitation   */
	Word16 gCoeff[6];               /* Correlations between xn, y1, & y2:   */
	Word16 res[L_SUBFR];          /* Short term (LPC) prediction residual */
	Word16 res2[L_SUBFR];        /* Long term (LTP) prediction residual  */

	/* Vector and scalars needed for the MR475 */
	Word16 xn_sf0[L_SUBFR];         /* Target vector for pitch search       */
	Word16 y2_sf0[L_SUBFR];         /* Filtered codebook innovation         */   
	Word16 code_sf0[L_SUBFR];     /* Fixed codebook excitation            */
	Word16 h1_sf0[L_SUBFR];         /* The impulse response of sf0          */
	Word16 mem_syn_save[M];     /* Filter memory                        */
	Word16 mem_w0_save[M];      /* Filter memory                        */
	Word16 mem_err_save[M];     /* Filter memory                        */
	Word16 sharp_save;                /* Sharpening                           */
	Word16 evenSubfr;                  /* Even subframe indicator              */ 
	Word16 T0_sf0 = 0;                  /* Integer pitch lag of sf0             */  
	Word16 T0_frac_sf0 = 0;         /* Fractional pitch lag of sf0          */  
	Word16 i_subfr_sf0 = 0;          /* Position in exc[] for sf0            */
	Word16 gain_pit_sf0;              /* Quantized pitch gain for sf0         */
	Word16 gain_code_sf0;           /* Quantized codebook gain for sf0      */

	/* Scalars */
	Word16 i_subfr, subfrNr;
	Word16 T_op[2];
	Word16 T0, T0_frac;
	Word16 gain_pit, gain_code;

	/* Flags */
	Word16 lsp_flag = 0;              /* indicates resonance in LPC filter */   
	Word16 gp_limit;                    /* pitch gain limit value            */
	Word16 vad_flag;                   /* VAD decision flag                 */
	Word16 compute_sid_flag;    /* SID analysis  flag                 */
#ifdef VAD2
       Word16 thresh,hi,lo;
       Word32 Ltmp;
#endif

	Word16 lagcount;
	cod_amrState *pst = st;  
	Word16 *pnew_speech = new_speech, *psynth = synth;

	memcpy(pst->new_speech, pnew_speech, L_FRAME*sizeof(Word16));

	*usedMode = mode;                
     /* DTX processing */
 if (pst->dtx)
 {  
      /* no test() call since this if is only in simulation env .  Find VAD decision */

#ifdef  VAD2
	vad_flag = vad2 (pst->new_speech,       pst->vadSt);
	vad_flag = vad2 (pst->new_speech+80, pst->vadSt) || vad_flag;    
#else
	vad_flag = vad1(pst->vadSt, pst->new_speech);     

#endif

      /* NB! usedMode may change here */      compute_sid_flag = tx_dtx_handler(pst->dtx_encSt, vad_flag,  usedMode);
   }   else    {      compute_sid_flag = 0;           }      /*------------------------------------------------------------------------*    *  - Perform LPC analysis:                                               *    *       * autocorrelation + lag windowing                                *    *       * Levinson-durbin algorithm to find a[]                          *    *       * convert a[] to lsp[]                                           *    *       * quantize and code the LSPs                                     *    *       * find the interpolated LSPs and convert to a[] for all          *    *         subframes (both quantized and unquantized)                     *    *------------------------------------------------------------------------*/      /* LP analysis */   lpc(pst->lpcSt, mode, pst->p_window, pst->p_window_12k2, A_t);
   /* From A(z) to lsp. LSP quantization and interpolation */
   lsp(pst->lspSt,  mode, *usedMode, A_t, Aq_t, lsp_new, &ana);
       /* Buffer lsp's and energy */
   dtx_buffer(pst->dtx_encSt, lsp_new,  pst->new_speech);
   /* Check if in DTX mode */
   if (*usedMode == MRDTX )
   {      dtx_enc(pst->dtx_encSt,compute_sid_flag, pst->lspSt->qSt,  pst->gainQuantSt->gc_predSt, &ana);
            memset(pst->old_exc, 0,  308);
      memset(pst->mem_w0, 0,    20);
      memset(pst->mem_err, 0,   20);
      memset(pst->zero, 0,     80);
      memset(pst->hvec, 0,   80);    /* set to zero "h1[-L_SUBFR..-1]" */
	/* Reset lsp states */	// lsp_reset(st->lspSt);	memcpy((void*)&pst->lspSt->lsp_old[0], (void*)lsp_init_data,  20);
	/* Initialize lsp_old_q[] */	memcpy(pst->lspSt->lsp_old_q,  pst->lspSt->lsp_old,20);
	/* Reset quantization state */	//  Q_plsf_reset(st->qSt);		memset( &pst->lspSt->qSt->past_rq[0] , 0, 20);
       memcpy( pst->lspSt->lsp_old,  lsp_new, 20);
       memcpy( pst->lspSt->lsp_old_q, lsp_new, 20);
            /* Reset clLtp states */      //  cl_ltp_reset(st->clLtpSt);      //  Pitch_fr_reset (state->pitchSt);      pst->clLtpSt->pitchSt->T0_prev_subframe = 0;
	        pst->sharp = SHARPMIN;     
   }   else   {       /* check resonance in the filter */      lsp_flag = check_lsp((tonStabState *)(pst->tonStabSt), (Word16*)(pst->lspSt->lsp_old));  
   }      /*----------------------------------------------------------------------*    * - Find the weighted input speech w_sp[] for the whole speech frame   *    * - Find the open-loop pitch delay for first 2 subframes               *    * - Set the range for searching closed-loop pitch in 1st subframe      *    * - Find the open-loop pitch delay for last 2 subframes                *    *----------------------------------------------------------------------*/#ifdef VAD2   if (pst->dtx)
   {  
      /* no test() call since this if is only in simulation env */
       pst->vadSt->L_Rmax = 0;		
       pst->vadSt->L_R0 = 0;			
   }#endif    for(subfrNr = 0, i_subfr = 0; subfrNr < 2;   subfrNr++, i_subfr += 80)   {       /* Pre-processing on 80 samples */
       pre_big(mode, gamma1, gamma1_12k2, gamma2, A_t, i_subfr, pst->speech,  pst->mem_w, pst->wsp);

      if ((mode!=MR475 ) && (mode!= MR515) )      {         /* Find open loop pitch lag for two subframes */         ol_ltp(pst->pitchOLWghtSt, pst->vadSt, mode, &pst->wsp[i_subfr],&T_op[subfrNr], pst->old_lags, pst->ol_gain_flg, subfrNr,  pst->dtx);
      }   }   if ( (mode == MR475) || (mode ==  MR515) )   {      /* Find open loop pitch lag for ONE FRAME ONLY . Search on 160 samples */
            ol_ltp(pst->pitchOLWghtSt, pst->vadSt, mode, &pst->wsp[0], &T_op[0],  pst->old_lags, pst->ol_gain_flg, 1, pst->dtx); 
      T_op[1] = T_op[0];       }            
#ifdef VAD2   if (pst->dtx)
   {  
       /* no test() call since this if is only in simulation env */
       //LTP_flag_update(pst->vadSt, mode);
     	if ( (mode == MR475) || (mode == MR515) )
	{		thresh = 18022;	
	}	else if (mode == MR102 )
	{		thresh = 19660;		
	}	else	{		thresh = 21299;	
	}
       hi     = (Word16)(pst->vadSt->L_R0 >>16);
	lo     = (pst->vadSt->L_R0 - (hi<<16))>>1;
       Ltmp = (hi*thresh<<1) + ((lo*thresh >> 15 )<<1);
       pst->vadSt->LTP_flag = ( pst->vadSt->L_Rmax > Ltmp) ? 1 : 0;
	  
   }#endif#ifndef VAD2

    /* run VAD pitch detection */
   if (pst->dtx)
   {
	/* no test() call since this if is only in simulation env */
	// vad_pitch_detection(pst->vadSt, T_op); 
	lagcount = 0;         
	lagcount  += ( abs(pst->vadSt->oldlag -T_op[0] ) < LTHRESH  );
	pst->vadSt->oldlag = T_op[0];  
	lagcount  += ( abs(pst->vadSt->oldlag -T_op[1] ) < LTHRESH  );
	pst->vadSt->oldlag = T_op[1]; 

	/* Make pitch decision.Save flag of the pitch detection to the variable pitch.   */
	pst->vadSt->pitch >>= 1; 

	if ((pst->vadSt->oldlag_count + lagcount) >=  NTHRESH)
	{
		pst->vadSt->pitch = pst->vadSt->pitch | 0x4000; 
	}

	/* Update oldlagcount */
	pst->vadSt->oldlag_count = lagcount;   

	  
   } #endif    if ( *usedMode == MRDTX)   {      goto the_end;   }      /*------------------------------------------------------------------------*    *          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 L_FRAME/L_SUBFR 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 dealy                                           *    *     - 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                                *    *------------------------------------------------------------------------*/   A    = A_t;        /* pointer to interpolated LPC parameters */
   Aq  = Aq_t;    /* pointer to interpolated quantized LPC parameters */
   evenSubfr    =  0;                                          
   subfrNr        = -1;                                              
   for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)   {      subfrNr ++;      evenSubfr = 1- evenSubfr ;      /* Save states for the MR475 mode */          if ((evenSubfr != 0) && (*usedMode == MR475) )      {         memcpy(mem_syn_save, pst->mem_syn, 20);
         memcpy(mem_w0_save, pst->mem_w0, 20);         
         memcpy(mem_err_save, pst->mem_err, 20);         
         sharp_save = pst->sharp;
      }            /*-----------------------------------------------------------------*       * - Preprocessing of subframe                                     *       *-----------------------------------------------------------------*/          if (*usedMode != MR475 )

⌨️ 快捷键说明

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