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

📄 aac_qc.c

📁 JPEG-MPEG編解碼技術書集的代碼
💻 C
📖 第 1 页 / 共 3 页
字号:
		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;
			QUANT_TYPE worstISMR = PsySigMaskRatio[bandNum];
			int w;
			for (w=1;w<window_group_length[i];w++) {
				bandNum=(w+windowOffset)*NSFB_SHORT + k;
				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[CHANNEL][NUM_COEFF],  /* Quantized spectral values */
	       /* int huff[13][MAXINDEX][NUMINTAB],*/          /* Huffman codebooks */
               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;
	QUANT_TYPE 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)); 

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

		/* 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 */
						bit_stats[j][0] = bit_stats[2*j][0] + bit_stats[2*j+1][0];
					}
				} else {  /* during the first stage of the iteration, all sfb's are individual sections */
					if ( (book_vector[k]!=INTENSITY_HCB)&&(book_vector[k]!=INTENSITY_HCB2) ) {
						book_vector[k] = bit_stats[j][1];  /* initially, set all sfb's to their own optimal section table values */
					}
				}
				total_bit_count = total_bit_count +  bit_stats[j][0];
				k=k+hop;
				m++;
			}
#ifdef SLOW
	}
#endif
	/*   book_vector[k] = book_vector[k-1]; */
	return(total_bit_count);
}


int noiseless_bit_count(int quant[CHANNEL][NUM_COEFF],
			/*int huff[13][MAXINDEX][NUMINTAB],*/
			int hop,  // hop is now always 1
			int min_book_choice[112][3],
			AACQuantInfo* quantInfo)         /* Quantization information */
{
  int i,j,k;

  /* 
     This function inputs:
     - the quantized spectral data, 'quant[][]';
     - all of the huffman codebooks, 'huff[][]';
     - the size of the sections, in scalefactor bands (SFB's), 'hop';
     - an empty matrix, min_book_choice[][] passed to it; 

     This function outputs:
     - the matrix, min_book_choice.  It is a two dimensional matrix, with its
     rows corresponding to spectral sections.  The 0th column corresponds to 
     the bits needed to code a section with 'hop' scalefactors bands wide, all using 
     the same huffman codebook.  The 1st column contains the huffman codebook number 
     that allows the minimum number of bits to be used.   

     Other notes:
     - Initally, the dynamic range is calculated for each spectral section.  The section
     can only be entropy coded with books that have an equal or greater dynamic range
     than the section's spectral data.  The exception to this is for the 11th ESC codebook.
     If the dynamic range is larger than 16, then an escape code is appended after the
     table 11 codeword which encodes the larger value explicity in a pseudo-non-uniform
     quantization method.
     
     */

	int max_sb_coeff;
	int book_choice[12][2];
	int total_bits_cost = 0;
	int counter = 0;
	int offset, length, end;
	int q;
	int write_flag = 0;

	/* set local pointer to sfb_offset */
	int* sfb_offset = quantInfo->sfb_offset;
	int nr_of_sfb = quantInfo->nr_of_sfb;

	/* each section is 'hop' scalefactor bands wide */
	for (i=0; i < nr_of_sfb; i=i+hop){ 
		if ((i+hop) > nr_of_sfb)
			q = nr_of_sfb;
		else
			q = i+hop;

		{
			
			/* find the maximum absolute value in the current spectral section, to see what tables are available to use */
			max_sb_coeff = 0;
			for (j=sfb_offset[i]; j<sfb_offset[q]; j++){  /* snl */
				if (ABS(quant[MONO_CHAN][j]) > max_sb_coeff)
					max_sb_coeff = ABS(quant[MONO_CHAN][j]);
			}
			
			j = 0;
			offset = sfb_offset[i];
			if ((i+hop) > nr_of_sfb){
				end = sfb_offset[nr_of_sfb];
			}
			else
				end = sfb_offset[i+hop];
			length = end - offset;

			/* all spectral coefficients in this section are zero */
			if (max_sb_coeff == 0) { 
				book_choice[j][0] = output_bits(quantInfo,/*huff,*/0,quant,offset,length,write_flag);
				book_choice[j++][1] = 0; 

			}
			else {  /* if the section does have non-zero coefficients */
				/* Changed all the else's to else if's, big speed up. Hardly any loss in coding. */
				if(max_sb_coeff < 2){
					quantInfo->spectralCount = 0; /* just for debugging : using data and len vectors */
					book_choice[j][0] = output_bits(quantInfo,/*huff,*/1,quant,offset,length,write_flag);
					book_choice[j++][1] = 1;
					quantInfo->spectralCount = 0; /* just for debugging : using data and len vectors */
					book_choice[j][0] = output_bits(quantInfo,/*huff,*/2,quant,offset,length,write_flag);
					book_choice[j++][1] = 2;
				}
				else if (max_sb_coeff < 3){
					quantInfo->spectralCount = 0; /* just for debugging : using data and len vectors */
					book_choice[j][0] = output_bits(quantInfo,/*huff,*/3,quant,offset,length,write_flag);
					book_choice[j++][1] = 3;
					quantInfo->spectralCount = 0; /* just for debugging : using data and len vectors */
					book_choice[j][0] = output_bits(quantInfo,/*huff,*/4,quant,offset,length,write_flag);
					book_choice[j++][1] = 4;
				}
				else if (max_sb_coeff < 5){
					quantInfo->spectralCount = 0; /* just for debugging : using data and len vectors */
					book_choice[j][0] = output_bits(quantInfo,/*huff,*/5,quant,offset,length,write_flag);
					book_choice[j++][1] = 5;
					quantInfo->spectralCount = 0; /* just for debugging : using data and len vectors */
					book_choice[j][0] = output_bits(quantInfo,/*huff,*/6,quant,offset,length,write_flag);
					book_choice[j++][1] = 6;
				}
				else if (max_sb_coeff < 8){
					quantInfo->spectralCount = 0; /* just for debugging : using data and len vectors */
					book_choice[j][0] = output_bits(quantInfo,/*huff,*/7,quant,offset,length,write_flag);
					book_choice[j++][1] = 7;
					quantInfo->spectralCount = 0; /* just for debugging : using data and len vectors */
					book_choice[j][0] = output_bits(quantInfo,/*huff,*/8,quant,offset,length,write_flag);
					book_choice[j++][1] = 8;
				}
				else if (max_sb_coeff < 13){
					quantInfo->spectralCount = 0; /* just for debugging : using data and len vectors */
					book_choice[j][0] = output_bits(quantInfo,/*huff,*/9,quant,offset,length,write_flag);
					book_choice[j++][1] = 9;
					quantInfo->spectralCount = 0; /* just for debugging : using data and len vectors */
					book_choice[j][0] = output_bits(quantInfo,/*huff,*/10,quant,offset,length,write_flag);
					book_choice[j++][1] = 10;
				}
				/* (max_sb_coeff >= 13), choose table 11 */
				else {
					quantInfo->spectralCount = 0; /* just for debugging : using data and len vectors */
					book_choice[j][0] = output_bits(quantInfo,/*huff,*/11,quant,offset,length,write_flag);
					book_choice[j++][1] = 11;
				}
			}

			/* find the minimum bit cost and table number for huffman coding this scalefactor section */
			min_book_choice[i][0] = 100000;  
			for(k=0;k<j;k++){
				if (book_choice[k][0] < min_book_choice[i][0]){
					min_book_choice[i][1] = book_choice[k][1];
					min_book_choice[i][0] = book_choice[k][0];
				}
			}
			total_bits_cost += min_book_choice[i][0];
		}
	}
	return(total_bits_cost);
}



int calculate_esc_sequence(int input,
						   int *len_esc_sequence
						   )
/* 
   This function takes an element that is larger than 16 and generates the base10 value of the 
   equivalent escape sequence.  It returns the escape sequence in the variable, 'output'.  It
   also passed the length of the escape sequence through the parameter, 'len_esc_sequence'.
*/

{
	float x,y;
	int output;
	int N;

	N = -1;
	y = (float)ABS(input);
	x = y / 16;

	while (x >= 1) {
		N++;
		x = x/2;
	}

	*len_esc_sequence = 2*N + 5;  /* the length of the escape sequence in bits */

	output = (int)((pow(2,N) - 1)*pow(2,N+5) + y - pow(2,N+4));
	return(output);
}



int output_bits(AACQuantInfo* quantInfo,
				/*int huff[13][MAXINDEX][NUMINTAB],*/
                int book,
				int quant[CHANNEL][NUM_COEFF],
                int offset,
				int length,
				int write_flag)
{

	int esc_sequence;

⌨️ 快捷键说明

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