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

📄 sp_dec.c

📁 AMR-NB的解码程序,纯C, VC建立工程即可使用.标准测试序列通过测试,与编码配合使用.
💻 C
📖 第 1 页 / 共 5 页
字号:
static void dtx_dec( dtx_decState *st, Word32 *mem_syn, D_plsfState *lsfState,
      gc_predState *pred_state, Cb_gain_averageState *averState, enum
      DTXStateType new_state, enum Mode mode, Word16 parm[], Word32 synth[],
      Word32 A_t[] )
{
	Word32 ex[L_SUBFR], acoeff[11], acoeff_variab[M + 1], lsp_int[M];
	Word32 refl[M], lsf[M], lsf_int[M], lsf_int_variab[M], lsp_int_variab[M];
	Word32 i, j, int_fac, log_en_int, pred_err, log_pg_e, log_pg_m, log_pg;
	Word32 negative, lsf_mean, lsf_variab_index, lsf_variab_factor, ptr;
	Word16 log_en_index, log_en_int_e, log_en_int_m, level, ma_pred_init,
	 tmp_int_length;


	if ( ( st->dtxHangoverAdded != 0 ) & ( st->sid_frame != 0 ) ) 
	{
		/*
		* sidFirst after dtx hangover period
		* or sidUpd after dtxhangover
		*/
		/* set log_en_adjust to correct value */
		st->log_en_adjust = dtx_log_en_adjust[mode];
		ptr = st->lsf_hist_ptr + M;

		if ( ptr == 80 ) 
		{
			 ptr = 0;
		}
		memcpy( &st->lsf_hist[ptr], &st->lsf_hist[st->lsf_hist_ptr], M <<2 );
		ptr = st->log_en_hist_ptr + 1;

		if ( ptr == DTX_HIST_SIZE ) 
		{
			 ptr = 0;
		}
		st->log_en_hist[ptr] = st->log_en_hist[st->log_en_hist_ptr];   /* Q11 */

		/*
		* compute mean log energy and lsp
		* from decoded signal (SID_FIRST)
		*/
		st->log_en = 0;
		memset( lsf, 0, M <<2 );

		/* average energy and lsp */
		for ( i = 0; i < DTX_HIST_SIZE; i++ ) 
		{
			 st->log_en = st->log_en + ( st->log_en_hist[i] >> 3 );

			 for ( j = 0; j < M; j++ ) 
			{
				lsf[j] += st->lsf_hist[i * M + j];
			 }
		}

		for ( j = 0; j < M; j+=2 ) 
		{
			lsf[j] = lsf[j] >> 3;   /* divide by 8 */
			lsf[j+1] = lsf[j+1] >> 3; 
		}
		Lsf_lsp( lsf, st->lsp );

		/*
		* make log_en speech coder mode independent
		* added again later before synthesis
		*/
		st->log_en = st->log_en - st->log_en_adjust;

		/* compute lsf variability vector */
		memcpy( st->lsf_hist_mean, st->lsf_hist, 80 <<2 );

		for ( i = 0; i < M; i++ ) 
		{
			 lsf_mean = 0;

			 /* compute mean lsf */
			 for ( j = 0; j < 8; j+=4 ) 
			{
				lsf_mean += st->lsf_hist_mean[i + j * M];
				lsf_mean += st->lsf_hist_mean[i + (j+1) * M];
				lsf_mean += st->lsf_hist_mean[i + (j+2) * M];
				lsf_mean += st->lsf_hist_mean[i + (j+3) * M];
			}
			lsf_mean = lsf_mean >> 3;

			/*
			* subtract mean and limit to within reasonable limits
			* moreover the upper lsf's are attenuated
			*/
			for ( j = 0; j < 8; j++ ) 
			{
				/* subtract mean */
				st->lsf_hist_mean[i + j * M] -= lsf_mean;

				/* attenuate deviation from mean, especially for upper lsf's */
				st->lsf_hist_mean[i + j * M] = st->lsf_hist_mean[i + j * M] * lsf_hist_mean_scale[i] >> 15;

				/* limit the deviation */
				if ( st->lsf_hist_mean[i + j * M] < 0 ) 
				{
					negative = 1;
				}
				else 
				{
					negative = 0;
				}
				st->lsf_hist_mean[i + j * M] = labs( st->lsf_hist_mean[i + j * M] );

				/* apply soft limit */
				if ( st->lsf_hist_mean[i + j * M] > 655 ) 
				{
					st->lsf_hist_mean[i + j * M] = 655 + ( ( st->lsf_hist_mean[i + j
					* M] - 655 ) >> 2 );
				}

				/* apply hard limit */
				if ( st->lsf_hist_mean[i + j * M] > 1310 )
				{
					st->lsf_hist_mean[i + j * M] = 1310;
				}

				if ( negative != 0 ) 
				{
					st->lsf_hist_mean[i + j * M] = -st->lsf_hist_mean[i + j * M];
				}
			 }
		}
	}

	if ( st->sid_frame != 0 ) 
	{
	/*
	* Set old SID parameters, always shift
	* even if there is no new valid_data
	*/
		memcpy( st->lsp_old, st->lsp, M <<2 );
		st->old_log_en = st->log_en;

		if ( st->valid_data != 0 ) /* new data available (no CRC) */ 
		{
			/* Compute interpolation factor, since the division only works
			* for values of since_last_sid < 32 we have to limit the
			* interpolation to 32 frames
			*/
			tmp_int_length = st->since_last_sid;
			st->since_last_sid = 0;

			if ( tmp_int_length > 32 ) 
			{
				tmp_int_length = 32;
			}

			if ( tmp_int_length >= 2 ) 
			{
				st->true_sid_period_inv = 0x2000000 / ( tmp_int_length<< 10 );
			}
			else 
			{
				st->true_sid_period_inv = 16384;   /* 0.5 it Q15 */
			}
			memcpy( lsfState->past_r_q, &past_rq_init[parm[0] * M], M <<2 );
			D_plsf_3( lsfState, MRDTX, 0, &parm[1], st->lsp );

			/* reset for next speech frame */
			memset( lsfState->past_r_q, 0, M <<2 );
			log_en_index = parm[4];

			st->log_en = ( Word16 )( log_en_index << 9 );

			st->log_en = ( Word16 )( st->log_en - 5120 );

			/* Index 0 is reserved for silence */
			if ( log_en_index == 0 ) 
			{
				st->log_en = MIN_16;
			}

			/*
			* no interpolation at startup after coder reset
			* or when SID_UPD has been received right after SPEECH
			*/
			if ( ( st->data_updated == 0 ) || ( st->dtxGlobalState == SPEECH ) )
			{
				memcpy( st->lsp_old, st->lsp, M <<2 );
				st->old_log_en = st->log_en;
			}
		}   /* endif valid_data */

		/* initialize gain predictor memory of other modes */
		ma_pred_init = ( Word16 )( ( st->log_en >> 1 ) - 9000 );

		if ( ma_pred_init > 0 ) 
		{
			ma_pred_init = 0;
		}

		if ( ma_pred_init < - 14436 ) 
		{
			ma_pred_init = -14436;
		}
		pred_state->past_qua_en[0] = ma_pred_init;
		pred_state->past_qua_en[1] = ma_pred_init;
		pred_state->past_qua_en[2] = ma_pred_init;
		pred_state->past_qua_en[3] = ma_pred_init;

		/* past_qua_en for other modes than MR122 */
		ma_pred_init = ( Word16 )( ( 5443*ma_pred_init ) >> 15 );

		pred_state->past_qua_en_MR122[0] = ma_pred_init;
		pred_state->past_qua_en_MR122[1] = ma_pred_init;
		pred_state->past_qua_en_MR122[2] = ma_pred_init;
		pred_state->past_qua_en_MR122[3] = ma_pred_init;
	}   /* endif sid_frame */

	/*
	* CN generation
	* recompute level adjustment factor Q11
	* st->log_en_adjust = 0.9*st->log_en_adjust +
	*                     0.1*dtx_log_en_adjust[mode]);
	*/
	st->log_en_adjust = ( Word16 )( ( ( st->log_en_adjust * 29491 ) >> 15 ) + ( (
	 ( dtx_log_en_adjust[mode] << 5 ) * 3277 ) >> 20 ) );

	/* Interpolate SID info */
	if ( st->since_last_sid > 30 )
		int_fac = 32767;
	else
		int_fac = ( Word16 )( (st->since_last_sid + 1) << 10 );

	int_fac = ( int_fac * st->true_sid_period_inv ) >> 15;

	if ( int_fac > 1024 ) 
	{
		int_fac = 1024;
	}

	int_fac = ( Word16 )( int_fac << 4 );

	log_en_int = ( int_fac * st->log_en ) << 1;

	for ( i = 0; i < M; i+=2 ) 
	{
		lsp_int[i] = ( int_fac * st->lsp[i] ) >> 15;
		lsp_int[i+1] = ( int_fac * st->lsp[i+1] ) >> 15;
	}

	int_fac = 16384 - int_fac;

	log_en_int += ( int_fac * st->old_log_en ) << 1;

	for ( i = 0; i < M; i++ ) 
	{
		lsp_int[i] = lsp_int[i] + ( ( int_fac * st->lsp_old[i] ) >> 15 );

		lsp_int[i] = lsp_int[i] << 1;
	}

	/* compute the amount of lsf variability */
	lsf_variab_factor = st->log_pg_mean - 2457;

	lsf_variab_factor = 4096 - ( ( lsf_variab_factor * 9830 ) >> 15 );

	if ( lsf_variab_factor >= 4096 ) 
	{
		lsf_variab_factor = 32767;
	}
	else if ( lsf_variab_factor < 0 ) 
	{
		lsf_variab_factor = 0;
	}
	else
		lsf_variab_factor = lsf_variab_factor << 3;   

	/* get index of vector to do variability with */
	lsf_variab_index = pseudonoise( &st->pn_seed_rx, 3 );

	Lsp_lsf( lsp_int, lsf_int );

	memcpy( lsf_int_variab, lsf_int, M <<2 );

	for ( i = 0; i < M; i++ ) 
	{
		lsf_int_variab[i] = lsf_int_variab[i] + ( ( lsf_variab_factor * st->
		    lsf_hist_mean[i + lsf_variab_index * M] ) >> 15 );
	}

	/* make sure that LSP's are ordered */
	Reorder_lsf( lsf_int, LSF_GAP );
	Reorder_lsf( lsf_int_variab, LSF_GAP );

	/* copy lsf to speech decoders lsf state */
	memcpy( lsfState->past_lsf_q, lsf_int, M <<2 );

	/* convert to lsp */
	Lsf_lsp( lsf_int, lsp_int );
	Lsf_lsp( lsf_int_variab, lsp_int_variab );

	/* Compute acoeffs Q12 acoeff is used for level
	* normalization and Post_Filter, acoeff_variab is
	* used for synthesis filter
	* by doing this we make sure that the level
	* in high frequenncies does not jump up and down
	*/
	Lsp_Az( lsp_int, acoeff );
	Lsp_Az( lsp_int_variab, acoeff_variab );

	/* For use in Post_Filter */
	memcpy( &A_t[0], acoeff, MP1 <<2 );
	memcpy( &A_t[MP1], acoeff, MP1 <<2 );
	memcpy( &A_t[MP1 <<1], acoeff, MP1 <<2 );
	memcpy( &A_t[MP1 + MP1 + MP1], acoeff, MP1 <<2 );

	/* Compute reflection coefficients Q15 */
	A_Refl( &acoeff[1], refl );

	/* Compute prediction error in Q15 */
	/* 0.99997 in Q15 */
	pred_err = MAX_16;

	for ( i = 0; i < M; i++ ) 
	{
		pred_err = ( pred_err * ( MAX_16 - ( ( refl[i] * refl[i] ) >> 15 ) ) ) >>  15;
	}

	/* compute logarithm of prediction gain */
	Log2( pred_err, &log_pg_e, &log_pg_m );

	/* convert exponent and mantissa to Word16 Q12 */
	log_pg = ( log_pg_e - 15 ) << 12;
	if (log_pg < -32768) 
	{
		log_pg = -32768;
	}
	log_pg = ( -( log_pg + ( log_pg_m >> 3 ) ) ) >> 1;
	st->log_pg_mean = ( Word16 )( ( ( 29491*st->log_pg_mean ) >> 15 ) + ( ( 3277
	 * log_pg ) >> 15 ) );

	/* Compute interpolated log energy */
	log_en_int = log_en_int >> 10;

	log_en_int += 262144L;

	/* subtract prediction gain */
	log_en_int = log_en_int - ( log_pg << 4 );

	/* adjust level to speech coder mode */
	log_en_int += st->log_en_adjust << 5;
	log_en_int_e = ( Word16 )( log_en_int >> 16 );
	log_en_int_m = ( Word16 )( ( log_en_int - ( log_en_int_e << 16 ) ) >> 1 );

	level = ( Word16 )( Pow2( log_en_int_e, log_en_int_m ) );

	for ( i = 0; i < 4; i++ ) 
	{
	/* Compute innovation vector */
		Build_CN_code( &st->pn_seed_rx, ex );

		for ( j = 0; j < L_SUBFR; j+=4 ) 
		{
			ex[j] = ( level * ex[j] ) >> 15;
			ex[j+1] = ( level * ex[j+1] ) >> 15;
			ex[j+2] = ( level * ex[j+2] ) >> 15;
			ex[j+3] = ( level * ex[j+3] ) >> 15;
		}

		/* Synthesize */
		Syn_filt( acoeff_variab, ex, &synth[i * L_SUBFR], L_SUBFR, mem_syn, 1 );
	}   /* next i */

	/* reset codebook averaging variables */
	averState->hangVar = 20;
	averState->hangCount = 0;

	if ( new_state == DTX_MUTE ) 
	{
		/*
		* mute comfort noise as it has been quite a long time since
		* last SID update  was performed
		*/
		Word32 num, denom;


		tmp_int_length = st->since_last_sid;

		if ( tmp_int_length > 32 ) 
		{
			tmp_int_length = 32;
		}

		if ( tmp_int_length == 1 ) 
		{
			st->true_sid_period_inv = MAX_16;
		}
		else 
		{
			num = 1024;
			denom = ( tmp_int_length << 10 );
			st->true_sid_period_inv = 0;

			for ( i = 0; i < 15; i++ ) 
			{
				st->true_sid_period_inv <<= 1;
				num <<= 1;

				if ( num >= denom ) 
				{
					num = num - denom;
					st->true_sid_period_inv += 1;
				}
			}
		}
		st->since_last_sid = 0;
		memcpy( st->lsp_old, st->lsp, M <<2 );
		st->old_log_en = st->log_en;

		/* subtract 1/8 in Q11 i.e -6/8 dB */
		st->log_en = st->log_en - 256;
		if (st->log_en < -32768) st->log_en = -32768;
	}

	/*
	* reset interpolation length timer
	* if data has been updated.
	*/
	if ( ( st->sid_frame != 0 ) & ( ( st->valid_data != 0 ) || ( ( st->valid_data
	 == 0 ) & ( st->dtxHangoverAdded != 0 ) ) ) ) 
	{
	st->since_last_sid = 0;
	st->data_updated = 1;
	}
	return;
}


/************************************************************************************
 * lsp_avg
 *
 *
 * Parameters:
 *    st->lsp_meanSave  B: LSP averages
 *    lsp               I: LSPs
 *
 * Function:
 *    Calculate the LSP averages
 *
 * Returns:
 *    void
 ************************************************************************************/
static void lsp_avg( lsp_avgState *st, Word32 *lsp )
{
	Word32 i, tmp;


	for ( i = 0; i < M; i++ ) 
	{
		/* mean = 0.84*mean */
		tmp = ( st->lsp_meanSave[i] << 16 );
		tmp -= ( EXPCONST * st->lsp_meanSave[i] ) << 1;

		/* Add 0.16 of newest LSPs to mean */
		tmp += ( EXPCONST * lsp[i] ) << 1;

		/* Save means */
		tmp += 0x00008000L;
		st->lsp_meanSave[i] = tmp >> 16;
	}
	return;
}


/************************************************************************************
 * Int_lpc_1and3
 *
 *
 * Parameters:
 *    lsp_old        I: LSP vector at the 4th subfr. of past frame      [M]
 *    lsp_mid        I: LSP vector at the 2nd subframe of present frame [M]
 *    lsp_new        I: LSP vector at the 4th subframe of present frame [M]
 *    Az             O: interpolated LP parameters in subframes 1 and 3
 *                                                                   [AZ_SIZE]
 *
 * Function:
 *    Interpolates the LSPs and converts to LPC parameters
 *    to get a different LP filter in each subframe.
 *
 *    The 20 ms speech frame is divided into 4 subframes.
 *    The LSPs are quantized and transmitted at the 2nd and
 *    4th subframes (twice per frame) and interpolated at the
 *    1st and 3rd subframe.
 *
 * Returns:
 *    void
 ************************************************************************************/
static void Int_lpc_1and3( Word32 lsp_old[], Word32 lsp_mid[], Word32 lsp_new[],
      Word32 Az[] )
{
	Word32 lsp[M];
	Word32 i;


	for ( i = 0; i < 10; i+=2 ) 
	{
		lsp[i] = ( lsp_mid[i] >> 1 ) + ( lsp_old[i] >> 1 );
		lsp[i+1] = ( lsp_mid[i+1] >> 1 ) + ( lsp_old[i+1] >> 1 );
	}

	/* Subframe 1 */
	Lsp_Az( lsp, Az );
	Az += MP1;

	/* Subframe 2 */
	Lsp_Az( lsp_mid, Az );
	Az += MP1;

	for ( i = 0; i < 10; i++ ) 

⌨️ 快捷键说明

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