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

📄 phi_lpc.c

📁 MPEG-4编解码的实现(包括MPEG4视音频编解码)
💻 C
📖 第 1 页 / 共 5 页
字号:
    
	    if((tmp_lpc_coefficients=(float *)calloc(lpc_order+1, sizeof(float)))==NULL) {
			    printf("\n Memory allocation error in abs_lpc_quantizer\n");
			    exit(4);
		    }
    
	    /*------------------------------------------------------------------*/
	    /* Calculate Narrowband LSPs                                        */
	    /*------------------------------------------------------------------*/
	    tmp_lpc_coefficients[0] = 1.;
	    for(i=0;i<lpc_order;i++) 
		    tmp_lpc_coefficients[i+1] = 
				    -lpc_coefficients[i];
    
	    pc2lsf(PHI_Priv->next_uq_lsf_8, tmp_lpc_coefficients, lpc_order);


	    /*------------------------------------------------------------------*/
	    /* Narrowband Quantization Procedure                                */
	    /*------------------------------------------------------------------*/
	    mod_nb_abs_lsp_quantizer(PHI_Priv->next_uq_lsf_8, PHI_Priv->previous_q_lsf_8, PHI_Priv->next_q_lsf_8,
				     next_codes, lpc_order, 5,
				     n_lpc_analysis);

	    /*------------------------------------------------------------------*/
	    /* Narrowband Quantization Procedure                                */
	    /*------------------------------------------------------------------*/
	    mod_nb_abs_lsp_quantizer(PHI_Priv->current_uq_lsf_8, PHI_Priv->previous_q_lsf_8, PHI_Priv->current_q_lsf_8,
				     current_codes, lpc_order, 5,
				     n_lpc_analysis);
    
	    /*------------------------------------------------------------------*/
	    /* Determine dynamic threshold                                      */
	    /*------------------------------------------------------------------*/
	    if (PHI_Priv->PHI_frames_sent == PHI_Priv->PHI_FRAMES)
	    {
		    long actual_bit_rate = (long)(((float)PHI_Priv->PHI_actual_bits)/(((float)PHI_Priv->PHI_frames_sent)*PHI_Priv->PHI_FR));
    
		    float diff = (float)fabs((double)(PHI_Priv->PHI_desired_bit_rate - actual_bit_rate));   
		    float per_diff = diff/(float)PHI_Priv->PHI_desired_bit_rate;
    
		    if (per_diff > (float)0.005)
		    {
    
		    long coeff = (long)(per_diff * (float)10.0+(float)0.5) + 1;
    
		    if (actual_bit_rate > PHI_Priv->PHI_desired_bit_rate)
			    PHI_Priv->PHI_dyn_lpc_thresh += (float)0.01 * (float)coeff;
		    if (actual_bit_rate < PHI_Priv->PHI_desired_bit_rate)
			    PHI_Priv->PHI_dyn_lpc_thresh -= (float)0.01 * (float)coeff;
    
		    if (PHI_Priv->PHI_dyn_lpc_thresh < (float)0.0)
			    PHI_Priv->PHI_dyn_lpc_thresh = (float)0.0;
    
		    if (PHI_Priv->PHI_dyn_lpc_thresh > PHI_Priv->PHI_stop_threshold)
			    PHI_Priv->PHI_dyn_lpc_thresh = PHI_Priv->PHI_stop_threshold;
		    }
    
		    PHI_Priv->PHI_frames_sent = 0;
		    PHI_Priv->PHI_actual_bits = 0;                
	    }
    
    
	    /*------------------------------------------------------------------*/
	    /* Check if you really need to transmit an LPC set                  */
	    /*------------------------------------------------------------------*/
	    if (PHI_Priv->PHI_prev_int_flag)
	    {
		    *interpolation_flag = 0;
		    if (!PHI_Priv->PHI_prev_lpc_flag)
		    {
			*send_lpc_flag = 1;
		    }
		    else
		    {
			*send_lpc_flag = 0;
		    }
	    }
	    else
	    {
		    float d;
		    float lpc_thresh = PHI_Priv->PHI_dyn_lpc_thresh;
    
    
		    for(d = (float)0.0, k = 0; k < (int)lpc_order; k++)
		    {
			    double temp;
    
			    temp  = (double)(((float)0.5 * (PHI_Priv->previous_q_lsf_8[k] + PHI_Priv->next_q_lsf_8[k])) - PHI_Priv->current_q_lsf_8[k]);
			    d    += (float)fabs(temp);
		    }
		    d /= 2.0F;
    
		    if (d  < lpc_thresh) 
		    {
			    *interpolation_flag = 1;
			    *send_lpc_flag  = 1;
    
			    /*--------------------------------------------------------------*/
			    /* Perform Interpolation                                        */
			    /*--------------------------------------------------------------*/
			    for(k = 0; k < (int)lpc_order; k++)
			    {
				    PHI_Priv->current_q_lsf_8[k] = (float)0.5 * (PHI_Priv->previous_q_lsf_8[k] + PHI_Priv->next_q_lsf_8[k]);
			    } 
		    }
		    else
		    {
		    *interpolation_flag = 0;
		    *send_lpc_flag  = 1;
		    }
    
	    }
    
	    /*------------------------------------------------------------------*/
	    /* Make sure to copy the right indices for the bit stream           */
	    /*------------------------------------------------------------------*/
	    if (*send_lpc_flag)
	    {
		    if (*interpolation_flag)
		    {
		    /* INTERPOLATION: use next indices (= indices of next frame) */
			    for (k=0; k < (int)num_lpc_indices; k++)    
			    {
			    lpc_indices[k] = next_codes[k];
			    }
		    }
		    else
		    {
		    /* NO INTERPOLATION: use current indices (= indices of current frame) */
			    for (k=0; k < (int)num_lpc_indices; k++)    
			    {
			    lpc_indices[k] = current_codes[k];
			    }
		    }
	    }
	    else
	    {
		    int k;
    
		    for (k=0; k < (int)num_lpc_indices; k++)    
		    {
		    lpc_indices[k] = 0;
		    }
    
	    }
	    PHI_Priv->PHI_prev_lpc_flag = *send_lpc_flag;
	    PHI_Priv->PHI_prev_int_flag = *interpolation_flag;
    
	    /*------------------------------------------------------------------*/
	    /* Update the number of frames and bits that are output             */
	    /*------------------------------------------------------------------*/
	    PHI_Priv->PHI_frames_sent += 1;
	    if (*send_lpc_flag)
	    {
		PHI_Priv->PHI_actual_bits += PHI_Priv->PHI_MAX_BITS;
	    }
	    else
	    {
		PHI_Priv->PHI_actual_bits += PHI_Priv->PHI_MIN_BITS;
	    }
       
	    /*------------------------------------------------------------------*/
	    /* Interpolation Procedure on LSPs                                  */
	    /*------------------------------------------------------------------*/
    
	    for(s = 0; s < (int)n_subframes; s++)
	    {
		    for(k = 0; k < (int)lpc_order; k++)
		    {
		    register float tmp;
    
		    tmp = (((float)((int)n_subframes - s - 1) * PHI_Priv->previous_q_lsf_int_8[k]) 
		       + ((float)(1 + s) * PHI_Priv->current_q_lsf_8[k]))/(float)n_subframes;
		    int_lsf[k] = tmp;
		    }
    
		    /*--------------------------------------------------------------*/
		    /*Compute corresponding a-parameters                            */
		    /*--------------------------------------------------------------*/
		    lsf2pc(tmp_lpc_coefficients, int_lsf, lpc_order);
		    for( k = 0; k < lpc_order; k++)
		    {
		      int_Qlpc_coefficients[s*(int)lpc_order + k] = -tmp_lpc_coefficients[k+1];
		    }
	    }
    
	    /* -----------------------------------------------------------------*/
	    /* Save memory                                                      */
	    /* -----------------------------------------------------------------*/
	    for(k=0; k < (int)lpc_order; k++)
	    {
	       PHI_Priv->previous_uq_lsf_8[k] = PHI_Priv->current_uq_lsf_8[k];
	       PHI_Priv->current_uq_lsf_8[k] = PHI_Priv->next_uq_lsf_8[k];
	       PHI_Priv->previous_q_lsf_int_8[k] = PHI_Priv->current_q_lsf_8[k];
	    }
    
	    if (*interpolation_flag)
	    {
		    ;    
	    }
	    else
	    {
		    for(k=0; k < (int)lpc_order; k++)
		    {
			    PHI_Priv->previous_q_lsf_8[k] = PHI_Priv->current_q_lsf_8[k];
			    PHI_Priv->current_q_lsf_8[k] = PHI_Priv->next_q_lsf_8[k];
		    }
	    }


	    /* -----------------------------------------------------------------*/
	    /*FREE the malloc'd arrays                                         */
	    /* -----------------------------------------------------------------*/
	  FREE(int_lsf);
	  FREE(next_codes);
	  FREE(current_codes);
	  FREE(tmp_lpc_coefficients);		
    }
    else
    {
	    if (Wideband_VQ==Scalable_VQ)
	    {    /*   16 khz Scalable VQ  */
		float *int_lsf;
		long  *next_codes;
		long  *current_codes;
		float *tmp_lpc_coefficients;
		int   i, j, s,k;
		float blsp_next[NEC_LSPPRDCT_ORDER][NEC_MAX_LSPVQ_ORDER];
		float blsp_current[NEC_LSPPRDCT_ORDER][NEC_MAX_LSPVQ_ORDER];
	
		/*------------------------------------------------------------------*/
		/* Allocate space for current_rfc and current lar                   */
		/*------------------------------------------------------------------*/
		if 
		(
		(( int_lsf = (float *)malloc((unsigned int)lpc_order * sizeof(float))) == NULL ) ||
		(( next_codes = (long *)malloc((unsigned int)num_lpc_indices * sizeof(long))) == NULL ) ||
		(( current_codes = (long *)malloc((unsigned int)num_lpc_indices * sizeof(long))) == NULL )
		)
		{
				printf("MALLOC FAILURE in Routine abs_lpc_quantizer \n");
				exit(1);
		}
	
		if((tmp_lpc_coefficients=(float *)calloc(lpc_order+1, sizeof(float)))==NULL) {
				printf("\n Memory allocation error in abs_lpc_quantizer\n");
				exit(4);
			}
	
		/*------------------------------------------------------------------*/
		/* Copy memory for Wideband Quantizer                               */
		/*------------------------------------------------------------------*/
			for (i=0; i < NEC_LSPPRDCT_ORDER; i++)
			{
			for(j=0; j < NEC_MAX_LSPVQ_ORDER; j++)
			{
				blsp_next[i][j] = PHI_Priv->blsp_previous[i][j];
				blsp_current[i][j] = PHI_Priv->blsp_previous[i][j];
			}
		}
	
		/*------------------------------------------------------------------*/
		/* Calculate Narrowband LSPs                                        */
		/*------------------------------------------------------------------*/
		tmp_lpc_coefficients[0] = 1.;
		for(i=0;i<lpc_order/2;i++) 
			tmp_lpc_coefficients[i+1] = 
					-lpc_coefficients_8[i];
	
		pc2lsf(PHI_Priv->next_uq_lsf_8, tmp_lpc_coefficients, lpc_order/2);
	
		/*------------------------------------------------------------------*/
		/* Narrowband Quantization Procedure                                */
		/*------------------------------------------------------------------*/
		mod_nb_abs_lsp_quantizer(PHI_Priv->next_uq_lsf_8, PHI_Priv->previous_q_lsf_8, PHI_Priv->next_q_lsf_8,
					 next_codes, lpc_order/2, 5,
					 n_lpc_analysis);
	
		/*------------------------------------------------------------------*/
		/* Calculate Wideband LSPs                                          */
		/*------------------------------------------------------------------*/
		tmp_lpc_coefficients[0] = 1.;
		for(i=0;i<lpc_order;i++) 
			tmp_lpc_coefficients[i+1] = 
					-lpc_coefficients[i];
	
		pc2lsf(PHI_Priv->next_uq_lsf_16, tmp_lpc_coefficients, lpc_order);
	
		/*------------------------------------------------------------------*/
		/* Wideband Quantization Procedure                                  */
		/*------------------------------------------------------------------*/
			mod_nec_bws_lsp_quantizer(PHI_Priv->next_uq_lsf_16, PHI_Priv->next_q_lsf_8,
					  PHI_Priv->next_q_lsf_16, next_codes + 5,
					  lpc_order, lpc_order/2, num_lpc_indices, blsp_next, PHI_Priv);
	
		/*------------------------------------------------------------------*/
		/* Narrowband Quantization Procedure                                */
		/*------------------------------------------------------------------*/
		mod_nb_abs_lsp_quantizer(PHI_Priv->current_uq_lsf_8, PHI_Priv->previous_q_lsf_8, PHI_Priv->current_q_lsf_8,
					 current_codes, lpc_order/2, 5,
					 n_lpc_analysis);
	
	 
		/*------------------------------------------------------------------*/
		/* Wideband Quantization Procedure                                  */
		/*------------------------------------------------------------------*/
		mod_nec_bws_lsp_quantizer(PHI_Priv->current_uq_lsf_16, PHI_Pr

⌨️ 快捷键说明

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