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

📄 aac_qc.c

📁 AAC音频解码算法程序
💻 C
📖 第 1 页 / 共 4 页
字号:
	}

	quantInfo -> spectralCount = counter;  /* send the current count back to the outside world */

	return(bits);
}


int find_grouping_bits(int window_group_length[],
					   int num_window_groups
					   )
{

  /* This function inputs the grouping information and outputs the seven bit 
	'grouping_bits' field that the NBC decoder expects.  */


	int grouping_bits = 0;
	int tmp[8];
	int i,j;
	int index=0;

	for(i=0; i<num_window_groups; i++){
		for (j=0; j<window_group_length[i];j++){
			tmp[index++] = i;
		}
	}

	for(i=1; i<8; i++){
		grouping_bits = grouping_bits << 1;
		if(tmp[i] == tmp[i-1]) {
			grouping_bits++;
		}
	}
	
	return(grouping_bits);
}



int write_scalefactor_bitstream(BsBitStream* fixed_stream,             /* Bitstream */  
				int write_flag,                        /* Write flag */
				AACQuantInfo* quantInfo)               /* Quantization information */
{
	/* this function takes care of counting the number of bits necessary */
	/* to encode the scalefactors.  In addition, if the write_flag == 1, */
	/* then the scalefactors are written out the fixed_stream output bit */
	/* stream.  it returns k, the number of bits written to the bitstream*/

	int i,j,k=0;
	int diff,length,codeword;
	int previous_scale_factor;
	int previous_is_factor;       /* Intensity stereo */
	int index = 0;
	int count = 0;
	int group_offset = 0;
	int nr_of_sfb_per_group=0;

	int pns_pcm_flag = 1;
	int previous_noise_nrg = quantInfo->common_scalefac ;

	/* set local pointer to quantInfo elements */
	int* scale_factors = quantInfo->scale_factor;
	
	if (quantInfo->block_type == ONLY_SHORT_WINDOW) { /* short windows */
		nr_of_sfb_per_group = quantInfo->nr_of_sfb/quantInfo->num_window_groups;
	}
	else {
		nr_of_sfb_per_group = quantInfo->nr_of_sfb;
		quantInfo->num_window_groups = 1;
		quantInfo->window_group_length[0] = 1;
	}

	previous_scale_factor = quantInfo->common_scalefac;
	previous_is_factor = 0;
    
	for(j=0; j<quantInfo->num_window_groups; j++){
		for(i=0;i<nr_of_sfb_per_group;i++) {  
			/* test to see if any codebooks in a group are zero */
			if ( (quantInfo->book_vector[index]==INTENSITY_HCB) ||
				(quantInfo->book_vector[index]==INTENSITY_HCB2) ) {
				/* only send scalefactors if using non-zero codebooks */
				diff = scale_factors[index] - previous_is_factor;
				if ((diff < 60)&&(diff >= -60))
					length = huff12[diff+60][FIRSTINTAB];
				else length = 0;
				k+=length;
				previous_is_factor = scale_factors[index];
				if (write_flag == 1 ) {   
					codeword = huff12[diff+60][LASTINTAB];
					BsPutBit(fixed_stream,codeword,length); 
				}
			} else if (quantInfo-> book_vector[index] == PNS_HCB){
				diff = quantInfo->pns_sfb_nrg[index] - previous_noise_nrg;
				if (pns_pcm_flag) {
					pns_pcm_flag = 0;
					length = PNS_PCM_BITS;
					codeword = diff + PNS_PCM_OFFSET;
				} else {
					if (diff<-60  ||  diff>60) {
						length = 0;
						codeword = 0;
					} else {
						length = huff12[diff+60][FIRSTINTAB];    
						codeword = huff12[diff+60][LASTINTAB];
					}
				}
				k += length;
				previous_noise_nrg = quantInfo->pns_sfb_nrg[index];
				if (write_flag == 1 ) {   
					BsPutBit(fixed_stream,codeword,length); 
				}
			} else if (quantInfo->book_vector[index]) {
				/* only send scalefactors if using non-zero codebooks */
				diff = scale_factors[index] - previous_scale_factor;
				if ((diff < 60)&&(diff >= -60))
					length = huff12[diff+60][FIRSTINTAB];
				else length = 0;
				k+=length;
				previous_scale_factor = scale_factors[index];
				if (write_flag == 1 ) {   
					codeword = huff12[diff+60][LASTINTAB];
					BsPutBit(fixed_stream,codeword,length); 
				}
			}
			index++;
		}
	}
	return(k);
}

int my_tf_encode_spectrum_aac(
			   double      *p_spectrum[MAX_TIME_CHANNELS],   //输入1:MDCT的能量谱向量
			   double      *PsySigMaskRatio[MAX_TIME_CHANNELS], //输入2:心理声学模型输出:信掩比
			   double      allowed_dist[MAX_TIME_CHANNELS][MAX_SCFAC_BANDS], //输入2:允许的最大误差
			   double      energy[MAX_TIME_CHANNELS][MAX_SCFAC_BANDS],  //?
			   enum WINDOW_TYPE block_type[MAX_TIME_CHANNELS],	//块类型
			   int         sfb_width_table[MAX_TIME_CHANNELS][MAX_SCFAC_BANDS],  //输入5:SCALEFACTOR BAND WIDTH
			   int         nr_of_sfb[MAX_TIME_CHANNELS],	//sfb的数目
			   int         average_block_bits,           //输入3:平均可用比特数
			   int         available_bitreservoir_bits,  //输入4:可用比特数
			   int         padding_limit,
			   BsBitStream *fixed_stream,			//输出:
			   BsBitStream *var_stream,				//输出:
			   int         nr_of_chan,
			   double      *p_reconstructed_spectrum[MAX_TIME_CHANNELS],
			   int         useShortWindows,
			   int aacAllowScalefacs,
			   AACQuantInfo* quantInfo,      /* AAC quantization information */ 
			   Ch_Info* ch_info,
			   int varBitRate,
			   int bitRate,
			   int is_first_frame)              //输入定义见标准P134
{
	int quant[NUM_COEFF];
	int i=0;
	int j=0;
	int k;
	double max_dct_line = 0;
	int global_gain;
	int store_common_scalefac;
	int best_scale_factor[SFB_NUM_MAX];
	double pow_spectrum[NUM_COEFF];
	double requant[NUM_COEFF];
	int sb;
	int extra_bits;
	int max_bits;
	int output_book_vector[SFB_NUM_MAX*2];
	int start_com_sf;
	double SigMaskRatio[SFB_NUM_MAX];
	
	double MaskValue[SFB_NUM_MAX];
	double D_temp;
	double D[SFB_NUM_MAX];
	double M_noise[SFB_NUM_MAX];
	double max_gain;
	int real_used_bits_old;
	double Gain_old;
	int real_used_bits_new;
	double Gain_new;
	double a_scale;

	IS_Info *is_info;
	int *ptr_book_vector;

	int inner_loop_count;
	int if_success;
	char message[255];

	/* Set up local pointers to quantInfo elements for convenience */
	int* sfb_offset = quantInfo -> sfb_offset;
	int* scale_factor = quantInfo -> scale_factor;
	int* common_scalefac = &(quantInfo -> common_scalefac);

	int outer_loop_count;
	int quantizer_change;
	int over = 0, best_over = 100, better;
	int sfb_overflow, amp_over;
	int best_common_scalefac;
	int prev_scfac, prev_is_scfac;
	double noise_thresh;
	double over_noise, tot_noise, max_noise;
	double noise[SFB_NUM_MAX];
	double best_max_noise = 0;
	double best_over_noise = 0;
	double best_tot_noise = 0;
	static int init = -1;

	/* Set block type in quantization info */
	quantInfo -> block_type = block_type[MONO_CHAN];

#if 0
	if (init != quantInfo->block_type) {
		init = quantInfo->block_type;
//		compute_ath(quantInfo, ATH);
	}
#endif

	/** create the sfb_offset tables **/
	if (quantInfo -> block_type == ONLY_SHORT_WINDOW) {

		/* Now compute interleaved sf bands and spectrum */
		sort_for_grouping(
			quantInfo,                       /* ptr to quantization information */
			sfb_width_table[MONO_CHAN],      /* Widths of single window */
			p_spectrum,                      /* Spectral values, noninterleaved */
			SigMaskRatio,				/*output new interleaved sigmaskratio*/
			PsySigMaskRatio[MONO_CHAN]   /*input, from psycho calculate*/
			);

		extra_bits = 51;   //??????????????????????????
	} else{
		/* For long windows, band are not actually interleaved */
		if ((quantInfo -> block_type == ONLY_LONG_WINDOW) ||  
			(quantInfo -> block_type == LONG_SHORT_WINDOW) || 
			(quantInfo -> block_type == SHORT_LONG_WINDOW)) {
			quantInfo->nr_of_sfb = quantInfo->max_sfb;

			sfb_offset[0] = 0;
			k=0;
			for( i=0; i< quantInfo -> nr_of_sfb; i++ ){
				sfb_offset[i] = k;
				k +=sfb_width_table[MONO_CHAN][i];
				SigMaskRatio[i]=PsySigMaskRatio[MONO_CHAN][i];
			}
			sfb_offset[i] = k;
			extra_bits = 100; /* header bits and more ... *//*for what?*/

		} 
	}

	extra_bits += 1;  /*why?*/

    /* Take into account bits for TNS data */
    extra_bits += WriteTNSData(quantInfo,fixed_stream,0);    /* Count but don't write */

 // if(quantInfo->block_type!=ONLY_SHORT_WINDOW)
		/* Take into account bits for LTP data */
//		extra_bits += WriteLTP_PredictorData(quantInfo, fixed_stream, 0); /* Count but don't write */

    /* for short windows, compute interleaved energy here */
    if (quantInfo->block_type==ONLY_SHORT_WINDOW) {
		int numWindowGroups = quantInfo->num_window_groups;
		int maxBand = quantInfo->max_sfb;
		int windowOffset=0;
		int sfb_index=0;
		int g;
		for (g=0;g<numWindowGroups;g++) {
			int numWindowsThisGroup = quantInfo->window_group_length[g];
			int b;
			for (b=0;b<maxBand;b++) {
				double sum=0.0;
				int w;
				for (w=0;w<numWindowsThisGroup;w++) {
					int bandNum = (w+windowOffset)*maxBand + b;
					sum += energy[MONO_CHAN][bandNum];
				}
				energy[MONO_CHAN][sfb_index] = sum;
				sfb_index++;
			}
			windowOffset += numWindowsThisGroup;
		}
    } 

	/* initialize the scale_factors that aren't intensity stereo bands */
	is_info=&(ch_info->is_info);
	for(k=0; k< quantInfo -> nr_of_sfb ;k++) {
		scale_factor[k]=((is_info->is_present)&&(is_info->is_used[k])) ? scale_factor[k] : 0 /*min(15,(int)(1.0/SigMaskRatio[k]+0.5))*/;
	}

	/* Mark IS bands by setting book_vector to INTENSITY_HCB */
	ptr_book_vector=quantInfo->book_vector;
	for (k=0;k<quantInfo->nr_of_sfb;k++) {
		if ((is_info->is_present)&&(is_info->is_used[k])) {
			ptr_book_vector[k] = (is_info->sign[k]) ? INTENSITY_HCB2 : INTENSITY_HCB;
		} else {
			ptr_book_vector[k] = 0;
		}
	}

	/* PNS prepare */
    for(sb=0; sb < quantInfo->nr_of_sfb; sb++ )
		quantInfo->pns_sfb_flag[sb] = 0;

    if (block_type[MONO_CHAN] != ONLY_SHORT_WINDOW) {     /* long blocks only */
		for(sb = pns_sfb_start; sb < quantInfo->nr_of_sfb; sb++ ) {
			/* Calc. pseudo scalefactor */
			if (energy[0][sb] == 0.0) {
				quantInfo->pns_sfb_flag[sb] = 0;
				continue;
			}

			if ((10*log10(energy[MONO_CHAN][sb]*sfb_width_table[0][sb]+1e-60)<70)||(SigMaskRatio[sb] > 1.0)) {
				quantInfo->pns_sfb_flag[sb] = 1;
				quantInfo->pns_sfb_nrg[sb] = (int) (2.0 * log(energy[0][sb]*sfb_width_table[0][sb]+1e-60) / log(2.0) + 0.5) + PNS_SF_OFFSET;
				
				/* Erase spectral lines */
				for( i=sfb_offset[sb]; i<sfb_offset[sb+1]; i++ ) {
					p_spectrum[0][i] = 0.0;
				}
			}
		}
    }

	/* Compute allowed distortion */
	for(sb = 0; sb < quantInfo->nr_of_sfb; sb++) {
		allowed_dist[MONO_CHAN][sb] = energy[MONO_CHAN][sb] * SigMaskRatio[sb];
	}

	/** find the maximum spectral coefficient **/
	/* Bug fix, 3/10/98 CL */
	/* for(i=0; i<NUM_COEFF; i++){ */
	for(i=0; i < sfb_offset[quantInfo->nr_of_sfb]; i++){ 
		pow_spectrum[i] = (pow(ABS(p_spectrum[0][i]), 0.75));
		sign[i] = sgn(p_spectrum[0][i]);
		if ((ABS(pow_spectrum[i])) > max_dct_line){
			max_dct_line = pow_spectrum[i];
		}
	}    /*the max of 1024 spectrum lines */

	//my new quantize program;
	start_com_sf = 30 + (int)(16/3 * (log(max_dct_line/MAX_QUANT)/log(2.0)));
	if(max_dct_line == 0.0){
		*common_scalefac = start_com_sf;
	}
	else
	{
	max_gain = 0.0;
	for(sb=0; sb<quantInfo->nr_of_sfb; sb++)
	{
		double gain_temp=0.0;
		MaskValue[sb] = allowed_dist[MONO_CHAN][sb];
		D_temp = 0.0;
		for(i=sfb_offset[sb]; i<sfb_offset[sb+1]; i++)
		{
			D_temp+=sqrt(ABS(p_spectrum[0][i]));
		}
		D[sb]=0.1640578*D_temp/(sfb_offset[sb+1]-sfb_offset[sb]);
		M_noise[sb] = MaskValue[sb]*0.75;
		gain_temp = M_noise[sb]/D[sb];
		if(gain_temp>max_gain)
			max_gain=gain_temp;
	}
	max_gain = 2/3*log(max_gain)/log(2.0);
	for(sb=0; sb<quantInfo->nr_of_sfb; sb++)
	{
		scale_factor[sb]=4*log(pow(2, max_gain)*pow((D[sb]/MaskValue[sb]), 2/3))/log(2.0);
	}
	if(is_first_frame)
	{
		quantInfo->last_b = 0.0;
	}
	
	if((start_com_sf<40)||(start_com_sf>200))
		start_com_sf = 40;
	*common_scalefac = start_com_sf;
	if(quantize(quantInfo,	p_spectrum[0], pow_spectrum, quant))
		max_bits = 1000000;
	else
	{
		real_used_bits_old = count_bits(quantInfo, quant, output_book_vector);
		max_bits = real_used_bits_old + extra_bits;
		Gain_old = 0.25*(*common_scalefac);
	}
	if(quantInfo->last_b == 0)
	{
		if(max_bits>average_block_bits)
			*common_scalefac+=4;
		else
			*common_scalefac-=4;
		if(quantize(quantInfo,	p_spectrum[0], pow_spectrum, quant))
			max_bits = 1000000;
		else
		{
			real_used_bits_new = count_bits(quantInfo, quant, output_book_vector);
			max_bits = real_used_bits_new + extra_bits;
			Gain_new = 0.25*(*common_scalefac);
		}
		quantInfo->last_b = (Gain_new - Gain_old)/(real_used_bits_new - real_used_bits_old);
		a_scale = Gain_new - (quantInfo->last_b)*real_used_bits_new;
		*common_scalefac = 4*(a_scale+quantInfo->last_b*(average_block_bits-extra_bits));
	}
	else
	{
		a_scale = Gain_old - (quantInfo->last_b)*real_used_bits_old;
		*common_scalefac = 4*(a_scale+quantInfo->last_b*(average_block_bits-extra_bits));
		quantize(quantInfo, p_spectrum[0], pow_spectrum, quant);
		real_used_bits_new = count_bits(quantInfo, quant, output_book_vector);
		if((real_used_bits_new - average_block_bits + extra_bits)/(average_block_bits - extra_bits) > 0.1)
		{
			Gain_new = 0.25*(*common_scalefac);
			quantInfo->last_b = (Gain_new - Gain_old)/(real_used_bits_new - real_used_bits_old);
			a_scale = Gain_new - (quantInfo->last_b)*real_used_bits_new;
			*common_scalefac = 4*(a_scale+quantInfo->last_b*(average_block_bits-extra_bits));
		}
	}
	}
	quantize(quantInfo,	p_spectrum[0], pow_spectrum, quant);
	calc_noise(quantInfo, p_spectrum[0], quant, requant, noise, allowed_dist[0],
			&over_noise, &tot_noise, &max_noise);

	if (quantInfo->block_type==ONLY_SHORT_WINDOW)
		quantInfo->pulseInfo.pulse_data_present = 0;
	else
		PulseCoder(quantInfo, quant);
	count_bits(quantInfo, quant, output_book_vector);

	/* offset the differenec of common_scalefac and scalefactors by SF_OFFSET  */
	for (i=0; i<quantInfo->nr_of_sfb; i++){
		if ((ptr_book_vector[i]!=INTENSITY_HCB)&&(ptr_book_vector[i]!=INTENSITY_HCB2)) {
			scale_factor[i] = *common_scalefac - scale_factor[i] + SF_OFFSET;
		}
	}
	*common_scalefac = global_gain = scale_factor[0];

	/* place the codewords and their respective lengths in arrays data[] and len[] respectively */
	/* there are 'counter' elements in each array, and these are variable length arrays depending on the input */

	quantInfo -> spectralCount = 0;
	for(k=0;k< quantInfo -> nr_of_sfb; k++) {  
		output_bits(
			quantInfo,
			quantInfo->book_vector[k],
			quant,
			quantInfo->sfb_offset[k],
			quantInfo->sfb_offset[k+1]-quantInfo->sfb_offset[k], 
			1);
//		printf("%d\t%d\n",k,quantInfo->book_vector[k]);
	}

	/* write the reconstructed spectrum to the output for use with prediction */
	{
		int i;
		for (sb=0; sb<quantInfo -> nr_of_sfb; sb++){
			if ((ptr_book_vector[sb]==INTENSITY_HCB)||(ptr_book_vector[sb]==INTENSITY_HCB2)){
				for (i=sfb_offset[sb]; i<sfb_offset[sb+1]; i++){
					p_reconstructed_spectrum[0][i]=673;
				}
			} else {
				for (i=sfb_offset[sb]; i<sfb_offset[sb+1]; i++){
					p_reconstructed_spectrum[0][i] = sgn(p_spectrum[0][i]) * requant[i]; 
				}
			}
		}
	}

	return FNO_ERROR;
}

⌨️ 快捷键说明

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