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

📄 aac_qc.c

📁 AAC音频解码算法程序
💻 C
📖 第 1 页 / 共 4 页
字号:
		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 */

	/*calculate the start_common_scalefac*/
	if (old_startsf < 40) {
		if (max_dct_line!=0.0) {
			start_com_sf = 30 + (int)(16/3 * (log(max_dct_line/MAX_QUANT)/log(2.0)));
		} else {
			start_com_sf = 40;
		}
	} else {
		start_com_sf = old_startsf;
	}
	if ((start_com_sf>200) || (start_com_sf<40) )
		start_com_sf = 40;

	outer_loop_count = 0;
	sprintf(message, "**************************************** max_dct_line %lf\n", max_dct_line);
	fwrite(message, 1, 70, log_file);
	do { // outer iteration loop
		
		outer_loop_count++;
		over = 0;
		sfb_overflow = 0;
		
		if (max_dct_line == 0.0)
			sfb_overflow = 1;

		if (outer_loop_count == 1) {
			quantizer_change = 8;
			*common_scalefac = start_com_sf;
		} else {
			quantizer_change = 2;
		}
		
		inner_loop_count = 0;
		do { // inner iteration loop
			inner_loop_count++;
			if(quantize(quantInfo,	p_spectrum[0], pow_spectrum, quant))
				max_bits = 1000000;
			else
				max_bits = count_bits(quantInfo, quant, output_book_vector) + extra_bits;

			quantizer_change /= 2;
			store_common_scalefac = *common_scalefac;

			if (max_bits > average_block_bits)
				*common_scalefac += quantizer_change;
			else
				*common_scalefac -= quantizer_change;
			if (*common_scalefac > 200)
				return FERROR;

			if ((quantizer_change == 1)&&(max_bits > average_block_bits))
				quantizer_change = 2;
			
		} while (quantizer_change != 1);
		
		sprintf(message, "out_loop_count %d th, inner_loop_count %d, start_com_sf %d\n", outer_loop_count, inner_loop_count, start_com_sf);
		fwrite(message, 1, 58, log_file);
		over = calc_noise(quantInfo, p_spectrum[0], quant, requant, noise, allowed_dist[0],
			&over_noise, &tot_noise, &max_noise);

		better = quant_compare(best_over, best_tot_noise, best_over_noise,
				  best_max_noise, over, tot_noise, over_noise, max_noise);

		prev_is_scfac = 0;
		prev_scfac = store_common_scalefac;
		for (sb = 0; sb < quantInfo->nr_of_sfb; sb++) {
			if (ch_info->is_info.is_used[sb]) {
				if ((scale_factor[sb] - prev_is_scfac) <= -59)
					sfb_overflow = 1;
				if ((scale_factor[sb] - prev_is_scfac) >= 59)
					sfb_overflow = 1;
				prev_is_scfac = scale_factor[sb];
			} else {
				if ((scale_factor[sb] - prev_scfac) <= -59)
					sfb_overflow = 1;
				if ((scale_factor[sb] - prev_scfac) >= 59)
					sfb_overflow = 1;
				prev_scfac = scale_factor[sb];
			}
			if (sfb_overflow == 1)
				better = 0;
		}

		if (outer_loop_count == 1)
			better = 1;

		if (better) {
			best_over = over;
			best_max_noise = max_noise;
			best_over_noise = over_noise;
			best_tot_noise = tot_noise;
			best_common_scalefac = store_common_scalefac;

			for (sb = 0; sb < quantInfo->nr_of_sfb; sb++) {
				best_scale_factor[sb] = scale_factor[sb];
			}
		}

		amp_over = 0;

#if 1
		noise_thresh = 0.0;
#else
		noise_thresh = -900;
		for ( sb = 0; sb < quantInfo->nr_of_sfb; sb++ )
			noise_thresh = max(1.05*noise[sb], noise_thresh);
		noise_thresh = min(noise_thresh, 0.0);
#endif

		for (sb = 0; sb < quantInfo->nr_of_sfb; sb++) {
			if ((noise[sb] > noise_thresh)&&(quantInfo->book_vector[sb]!=INTENSITY_HCB)&&(quantInfo->book_vector[sb]!=INTENSITY_HCB2)) {

				amp_over++;

				allowed_dist[0][sb] *= 2;
				scale_factor[sb]++;
			}
		}

		if (sfb_overflow == 0) {
			sfb_overflow = 1;
			for (sb = 0; sb < quantInfo->nr_of_sfb; sb++) {
				if (scale_factor[sb] == 0)
					sfb_overflow = 0;
			}
		}

		if ((amp_over == 0)||(over == 0)||(amp_over==quantInfo->nr_of_sfb)||(sfb_overflow))
			break;

	} while (1);

	*common_scalefac = best_common_scalefac;
	for (sb = 0; sb < quantInfo->nr_of_sfb; sb++) {
		scale_factor[sb] = best_scale_factor[sb];
	}

	old_startsf = *common_scalefac;

	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);

//	for( sb=0; sb< quantInfo -> nr_of_sfb; sb++ ) {
//		printf("%d error: %.4f all.dist.: %.4f energy: %.4f\n", sb,
//			noise[sb], allowed_dist[0][sb], energy[0][sb]);
//	}

	/* 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;
}

int sort_for_grouping(AACQuantInfo* quantInfo,        /* ptr to quantization information */
		      int sfb_width_table[],          /* Widths of single window */
		      double *p_spectrum[],           /* Spectral values, noninterleaved */
		      double *SigMaskRatio,
		      double *PsySigMaskRatio)
{
	int i,j,ii;
	int index = 0;
	double tmp[1024];
	int book=1;
	int group_offset=0;
	int k=0;
	int windowOffset = 0;

	/* set up local variables for used quantInfo elements */
	int* sfb_offset = quantInfo -> sfb_offset;
	int* nr_of_sfb = &(quantInfo -> nr_of_sfb);
	int* window_group_length;
	int num_window_groups;
	*nr_of_sfb = quantInfo->max_sfb;              /* Init to max_sfb */
	window_group_length = quantInfo -> window_group_length;
	num_window_groups = quantInfo -> num_window_groups;
	
	/* calc org sfb_offset just for shortblock */
	sfb_offset[k]=0;
	for (k=0; k < 1024; k++) {
		tmp[k] = 0.0;
	}
	for (k=1 ; k <*nr_of_sfb+1; k++) {
		sfb_offset[k] = sfb_offset[k-1] + sfb_width_table[k-1];
	}

	/* sort the input spectral coefficients */
	index = 0;
	group_offset=0;
	for (i=0; i< num_window_groups; i++) {
		for (k=0; k<*nr_of_sfb; k++) {
			for (j=0; j < window_group_length[i]; j++) {
				for (ii=0;ii< sfb_width_table[k];ii++)
					tmp[index++] = p_spectrum[MONO_CHAN][ii+ sfb_offset[k] + 128*j +group_offset];
			}
		}
		group_offset +=  128*window_group_length[i];     
	}

	for (k=0; k<1024; k++){
		p_spectrum[MONO_CHAN][k] = tmp[k];
	}

	/* now calc the new sfb_offset table for the whole p_spectrum vector*/
	index = 0;
	sfb_offset[index++] = 0;	  
	windowOffset = 0;
	for (i=0; i < num_window_groups; i++) {
		for (k=0 ; k <*nr_of_sfb; k++) {
			/* for this window group and this band, find worst case inverse sig-mask-ratio */
			int bandNum=windowOffset*NSFB_SHORT + k;
			double worstISMR = PsySigMaskRatio[bandNum];
			int w;
			for (w=1;w<window_group_length[i];w++) {
				bandNum=(w+windowOffset)*NSFB_SHORT + k;
				if (PsySigMaskRatio[bandNum]<worstISMR) {
				//if (PsySigMaskRatio[bandNum]>worstISMR) {
					worstISMR = PsySigMaskRatio[bandNum];
				}
			}
			SigMaskRatio[k+ i* *nr_of_sfb]=worstISMR;
			sfb_offset[index] = sfb_offset[index-1] + sfb_width_table[k]*window_group_length[i] ;
			index++;
		}
		windowOffset += window_group_length[i];
	}

	*nr_of_sfb = *nr_of_sfb * num_window_groups;  /* Number interleaved bands. */

	return 0;
}

sort_book_numbers(AACQuantInfo* quantInfo,     /* Quantization information */
		  int output_book_vector[],    /* Output codebook vector, formatted for bitstream */
		  BsBitStream* fixed_stream,   /* Bitstream */
		  int write_flag)              /* Write flag: 0 count, 1 write */
{
  /*
    This function inputs the vector, 'book_vector[]', which is of length SFB_NUM_MAX,
    and contains the optimal huffman tables of each sfb.  It returns the vector, 'output_book_vector[]', which
    has it's elements formatted for the encoded bit stream.  It's syntax is:
   
    {sect_cb[0], length_segment[0], ... ,sect_cb[num_of_sections], length_segment[num_of_sections]}

    The above syntax is true, unless there is an escape sequence.  An
    escape sequence occurs when a section is longer than 2 ^ (bit_len)
    long in units of scalefactor bands.  Also, the integer returned from
    this function is the number of bits written in the bitstream, 
    'bit_count'.  

    This function supports both long and short blocks.
    */

	int i;
	int repeat_counter = 1;
	int bit_count = 0;
	int previous;
	int max, bit_len/*,sfbs*/;
	int max_sfb,g,band;

	/* Set local pointers to quantInfo elements */
	int* book_vector = quantInfo -> book_vector;
	int nr_of_sfb = quantInfo -> nr_of_sfb;

	if (quantInfo->block_type == ONLY_SHORT_WINDOW){
		max = 7;
		bit_len = 3;
	} else {  /* the block_type is a long,start, or stop window */
		max = 31;
		bit_len = 5;
	}

	/* Compute number of scalefactor bands */
	max_sfb = quantInfo->nr_of_sfb/quantInfo->num_window_groups;


	for (g=0;g<quantInfo->num_window_groups;g++) {
		band=g*max_sfb;

		repeat_counter=1;

		previous = book_vector[band];
		if (write_flag) {   
			BsPutBit(fixed_stream,book_vector[band],4);  
		}
		bit_count += 4;

		for (i=band+1;i<band+max_sfb;i++) {
			if( (book_vector[i] != previous)) {
				if (write_flag) {
					BsPutBit(fixed_stream,repeat_counter,bit_len);  
				}
				bit_count += bit_len;

				if (repeat_counter == max){  /* in case you need to terminate an escape sequence */
					if (write_flag) BsPutBit(fixed_stream,0,bit_len);  
					bit_count += bit_len;
				}
				
				if (write_flag) BsPutBit(fixed_stream,book_vector[i],4);  
				bit_count += 4;
				previous = book_vector[i];
				repeat_counter=1;
				
			}
			/* if the length of the section is longer than the amount of bits available in */
			/* the bitsream, "max", then start up an escape sequence */
			else if ((book_vector[i] == previous) && (repeat_counter == max)) { 
				if (write_flag) {
					BsPutBit(fixed_stream,repeat_counter,bit_len);  
				}
				bit_count += bit_len;
				repeat_counter = 1;
			}
			else {
				repeat_counter++;
			}
		}
		
		if (write_flag) {
			BsPutBit(fixed_stream,repeat_counter,bit_len);  
		}
		bit_count += bit_len;
		if (repeat_counter == max) {  /* special case if the last section length is an */
			/* escape sequence */
			if (write_flag) BsPutBit(fixed_stream,0,bit_len);  
			bit_count += bit_len;
		}
		

	}  /* Bottom of group iteration */

	return(bit_count);
}

int bit_search(int quant[NUM_COEFF],  /* Quantized spectral values */
               AACQuantInfo* quantInfo)        /* Quantization information */
  /*
  This function inputs a vector of quantized spectral data, quant[][], and returns a vector,
  'book_vector[]' that describes how to group together the scalefactor bands into a smaller
  number of sections.  There are SFB_NUM_MAX elements in book_vector (equal to 49 in the 
  case of long blocks and 112 for short blocks), and each element has a huffman codebook 
  number assigned to it.

  For a quick and simple algorithm, this function performs a binary
  search across the sfb's (scale factor bands).  On the first approach, it calculates the 
  needed amount of bits if every sfb were its own section and transmitted its own huffman 
  codebook value side information (equal to 9 bits for a long block, 7 for a short).  The 
  next iteration combines adjacent sfb's, and calculates the bit rate for length two sfb 
  sections.  If any wider two-sfb section requires fewer bits than the sum of the two 
  single-sfb sections (below it in the binary tree), then the wider section will be chosen.
  This process occurs until the sections are split into three uniform parts, each with an
  equal amount of sfb's contained.  

  The binary tree is stored as a two-dimensional array.  Since this tree is not full, (there
  are only 49 nodes, not 2^6 = 64), the numbering is a little complicated.  If the tree were
  full, the top node would be 1.  It's children would be 2 and 3.  But, since this tree
  is not full, the top row of three nodes are numbered {4,5,6}.  The row below it is
  {8,9,10,11,12,13}, and so on.  

  The binary tree is called bit_stats[112][3].  There are 112 total nodes (some are not
  used since it's not full).  bit_stats[x][0] holds the bit totals needed for the sfb sectioning
  strategy represented by the node x in the tree.  bit_stats[x][1] holds the optimal huffman
  codebook table that minimizes the bit rate, given the sectioning boundaries dictated by node x.
*/

{
	int i,j,k,m,n;
	int hop;
	int min_book_choice[112][3];
	int bit_stats[240][3];
	int total_bits;
	int total_bit_count;
	int levels;
	double fraction;

	/* Set local pointer to quantInfo book_vector */
	int* book_vector = quantInfo -> book_vector;

	levels = (int) ((log((double)quantInfo->nr_of_sfb)/log((double)2.0))+1);
	fraction = (pow(2,levels)+quantInfo->nr_of_sfb)/(double)(pow(2,levels)); 

//#define SLOW
#ifdef SLOW
	for(i=0;i<5;i++){
		hop = 1 << i;
#else
		hop = 1;
		i = 0;
#endif
		total_bits = noiseless_bit_count(quant,
			hop,
			min_book_choice,
			quantInfo);         /* Quantization information */

		/* load up the (not-full) binary search tree with the min_book_choice values */
		k=0;
		m=0;
		total_bit_count = 0;

		for (j=(int)(pow(2,levels-i)); j<(int)(fraction*pow(2,levels-i)); j++)
		{
			bit_stats[j][0] = min_book_choice[k][0]; /* the minimum bit cost for this section */
			bit_stats[j][1] = min_book_choice[k][1]; /* used with this huffman book number */

			if (i>0){  /* not on the lowest level, grouping more than one signle scalefactor band per section*/
				if  (bit_stats[j][0] < bit_stats[2*j][0] + bit_stats[2*j+1][0]){

					/* it is cheaper to combine surrounding sfb secionts into one larger huffman book section */
					for(n=k;n<k+hop;n++) { /* write the optimal huffman book value for the new larger section */
						if ( (book_vector[n]!=INTENSITY_HCB)&&(book_vector[n]!=INTENSITY_HCB2) ) { /* Don't merge with IS bands */
							book_vector[n] = bit_stats[j][1];
						}
					}
				} else {  /* it was cheaper to transmit the smaller huffman table sections */

⌨️ 快捷键说明

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