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

📄 aac_qc.c

📁 JPEG-MPEG編解碼技術書集的代碼
💻 C
📖 第 1 页 / 共 3 页
字号:
	int len_esc;
	int index;
	int bits=0;
	int tmp = 0;
	int codebook,i,j;
	int counter;

	/* Set up local pointers to quantInfo elements data and len */
	int* data= quantInfo -> data;
	int* len=  quantInfo -> len;

	counter = quantInfo->spectralCount;

	/* This case also applies to intensity stereo encoding */
	/*if (book == 0) { */ /* if using the zero codebook, data of zero length is sent */
	if ((book == 0)||(book==INTENSITY_HCB2)||(book==INTENSITY_HCB)) {  /* if using the zero codebook, 
		data of zero length is sent */
		
		if (write_flag) {
			quantInfo->data[counter] = 0;
			quantInfo->len[counter++] = 0;
		}
	}

	if ((book == 1) || (book == 2)) {
		for(i=offset;i<offset+length;i=i+4){
			index = 27*quant[MONO_CHAN][i] + 9*quant[MONO_CHAN][i+1] + 3*quant[MONO_CHAN][i+2] + quant[MONO_CHAN][i+3] + 40;
			if (book == 1) {
#ifdef OLDHUFF
				codebook = huff[1][index][LASTINTAB];
				tmp = huff[1][index][FIRSTINTAB];
#else
				codebook = huff1[index][LASTINTAB];
				tmp = huff1[index][FIRSTINTAB];
#endif
			} else {
#ifdef OLDHUFF
				codebook = huff[2][index][LASTINTAB];
				tmp = huff[2][index][FIRSTINTAB];
#else
				codebook = huff2[index][LASTINTAB];
				tmp = huff2[index][FIRSTINTAB];
#endif
			}
			bits = bits + tmp;
			if (write_flag) {
				data[counter] = codebook;
				len[counter++] = tmp;
			}
		}
	}

	if ((book == 3) || (book == 4)) {
		for(i=offset;i<offset+length;i=i+4){
			index = 27*ABS(quant[MONO_CHAN][i]) + 9*ABS(quant[MONO_CHAN][i+1]) + 3*ABS(quant[MONO_CHAN][i+2]) + ABS(quant[MONO_CHAN][i+3]);
			if (book == 3) {
#ifdef OLDHUFF
				codebook = huff[3][index][LASTINTAB];
				tmp = huff[3][index][FIRSTINTAB];
#else
				codebook = huff3[index][LASTINTAB];
				tmp = huff3[index][FIRSTINTAB];
#endif
			} else {
#ifdef OLDHUFF
				codebook = huff[4][index][LASTINTAB];
				tmp = huff[4][index][FIRSTINTAB];
#else
				codebook = huff4[index][LASTINTAB];
				tmp = huff4[index][FIRSTINTAB];
#endif
			}
			bits = bits + tmp;
			for(j=0;j<4;j++){
				if(ABS(quant[MONO_CHAN][i+j]) > 0) bits += 1; /* only for non-zero spectral coefficients */
			}
			if (write_flag) {
				data[counter] = codebook;
				len[counter++] = tmp;
				for(j=0;j<4;j++){
					if(quant[MONO_CHAN][i+j] > 0) {  /* send out '0' if a positive value */
						data[counter] = 0;
						len[counter++] = 1;
					}
					if(quant[MONO_CHAN][i+j] < 0) {  /* send out '1' if a negative value */
						data[counter] = 1;
						len[counter++] = 1;
					}
				}
			}
		}
	}

	if ((book == 5) || (book == 6)) {
		for(i=offset;i<offset+length;i=i+2){
			index = 9*(quant[MONO_CHAN][i]) + (quant[MONO_CHAN][i+1]) + 40;
			if (book == 5) {
#ifdef OLDHUFF
				codebook = huff[5][index][LASTINTAB];
				tmp = huff[5][index][FIRSTINTAB];
#else
				codebook = huff5[index][LASTINTAB];
				tmp = huff5[index][FIRSTINTAB];
#endif
			} else {
#ifdef OLDHUFF
				codebook = huff[6][index][LASTINTAB];
				tmp = huff[6][index][FIRSTINTAB];
#else
				codebook = huff6[index][LASTINTAB];
				tmp = huff6[index][FIRSTINTAB];
#endif
			}
			bits = bits + tmp;
			if (write_flag) {
				data[counter] = codebook;
				len[counter++] = tmp;
			}
		}
	}

	if ((book == 7) || (book == 8)) {
		for(i=offset;i<offset+length;i=i+2){
			index = 8*ABS(quant[MONO_CHAN][i]) + ABS(quant[MONO_CHAN][i+1]);
			if (book == 7) {
#ifdef OLDHUFF
				codebook = huff[7][index][LASTINTAB];
				tmp = huff[7][index][FIRSTINTAB];
#else
				codebook = huff7[index][LASTINTAB];
				tmp = huff7[index][FIRSTINTAB];
#endif
			} else {
#ifdef OLDHUFF
				codebook = huff[8][index][LASTINTAB];
				tmp = huff[8][index][FIRSTINTAB];
#else
				codebook = huff8[index][LASTINTAB];
				tmp = huff8[index][FIRSTINTAB];
#endif
			}
			bits = bits + tmp;
			for(j=0;j<2;j++){
				if(ABS(quant[MONO_CHAN][i+j]) > 0) bits += 1; /* only for non-zero spectral coefficients */
			}
			if (write_flag) {
				data[counter] = codebook;
				len[counter++] = tmp;
				for(j=0;j<2;j++){
					if(quant[MONO_CHAN][i+j] > 0) {  /* send out '0' if a positive value */
						data[counter] = 0;
						len[counter++] = 1;
					}
					if(quant[MONO_CHAN][i+j] < 0) {  /* send out '1' if a negative value */
						data[counter] = 1;
						len[counter++] = 1;
					}
				}
			}
		}
	}

	if ((book == 9) || (book == 10)) {
		for(i=offset;i<offset+length;i=i+2){
			index = 13*ABS(quant[MONO_CHAN][i]) + ABS(quant[MONO_CHAN][i+1]);
			if (book == 9) {
#ifdef OLDHUFF
				codebook = huff[9][index][LASTINTAB];
				tmp = huff[9][index][FIRSTINTAB];
#else
				codebook = huff9[index][LASTINTAB];
				tmp = huff9[index][FIRSTINTAB];
#endif
			} else {
#ifdef OLDHUFF
				codebook = huff[10][index][LASTINTAB];
				tmp = huff[10][index][FIRSTINTAB];
#else
				codebook = huff10[index][LASTINTAB];
				tmp = huff10[index][FIRSTINTAB];
#endif
			}
			bits = bits + tmp;
			for(j=0;j<2;j++){
				if(ABS(quant[MONO_CHAN][i+j]) > 0) bits += 1; /* only for non-zero spectral coefficients */
			}
			if (write_flag) {
				
				data[counter] = codebook;
				len[counter++] = tmp;

				for(j=0;j<2;j++){
					if(quant[MONO_CHAN][i+j] > 0) {  /* send out '0' if a positive value */
						data[counter] = 0;
						len[counter++] = 1;
					}
					if(quant[MONO_CHAN][i+j] < 0) {  /* send out '1' if a negative value */
						data[counter] = 1;
						len[counter++] = 1;
					}
				}
			}
		}
	}

	if ((book == 11)){
		/* First, calculate the indecies into the huffman tables */

		for(i=offset;i<offset+length;i=i+2){
			if ((ABS(quant[MONO_CHAN][i]) >= 16) && (ABS(quant[MONO_CHAN][i+1]) >= 16)) {  /* both codewords were above 16 */
				/* first, code the orignal pair, with the larger value saturated to +/- 16 */
				index = 17*16 + 16;
			}
			else if (ABS(quant[MONO_CHAN][i]) >= 16) {  /* the first codeword was above 16, not the second one */
				/* first, code the orignal pair, with the larger value saturated to +/- 16 */
				index = 17*16 + ABS(quant[MONO_CHAN][i+1]);
			}
			else if (ABS(quant[MONO_CHAN][i+1]) >= 16) { /* the second codeword was above 16, not the first one */
				index = 17*ABS(quant[MONO_CHAN][i]) + 16;
			}
			else {  /* there were no values above 16, so no escape sequences */
				index = 17*ABS(quant[MONO_CHAN][i]) + ABS(quant[MONO_CHAN][i+1]);
			}

			/* write out the codewords */

#ifdef OLDHUFF
			tmp = huff[11][index][FIRSTINTAB];
			codebook = huff[11][index][LASTINTAB];
#else
			tmp = huff11[index][FIRSTINTAB];
			codebook = huff11[index][LASTINTAB];
#endif
			bits += tmp;
			if (write_flag) {
				/*	printf("[book %d] {%d %d} \n",book,quant[0][i],quant[0][i+1]);*/
				data[counter] = codebook;
				len[counter++] = tmp;
			}
			
			/* Take care of the sign bits */

			for(j=0;j<2;j++){
				if(ABS(quant[MONO_CHAN][i+j]) > 0) bits += 1; /* only for non-zero spectral coefficients */
			}
			if (write_flag) {
				for(j=0;j<2;j++){
					if(quant[MONO_CHAN][i+j] > 0) {  /* send out '0' if a positive value */
						data[counter] = 0;
						len[counter++] = 1;
					}
					if(quant[MONO_CHAN][i+j] < 0) {  /* send out '1' if a negative value */
						data[counter] = 1;
						len[counter++] = 1;
					}
				}
			}

			/* write out the escape sequences */

			if ((ABS(quant[MONO_CHAN][i]) >= 16) && (ABS(quant[MONO_CHAN][i+1]) >= 16)) {  /* both codewords were above 16 */
				/* code and transmit the first escape_sequence */
				esc_sequence = calculate_esc_sequence(quant[MONO_CHAN][i],&len_esc); 
				bits += len_esc;
				if (write_flag) {
					data[counter] = esc_sequence;
					len[counter++] = len_esc;
				}

				/* then code and transmit the second escape_sequence */
				esc_sequence = calculate_esc_sequence(quant[MONO_CHAN][i+1],&len_esc); 
				bits += len_esc;
				if (write_flag) {
					data[counter] = esc_sequence;
					len[counter++] = len_esc;
				}
			}

			else if (ABS(quant[MONO_CHAN][i]) >= 16) {  /* the first codeword was above 16, not the second one */
				/* code and transmit the escape_sequence */
				esc_sequence = calculate_esc_sequence(quant[MONO_CHAN][i],&len_esc); 
				bits += len_esc;
				if (write_flag) {
					data[counter] = esc_sequence;
					len[counter++] = len_esc;
				}
			}

			else if (ABS(quant[MONO_CHAN][i+1]) >= 16) { /* the second codeword was above 16, not the first one */
				/* code and transmit the escape_sequence */
				esc_sequence = calculate_esc_sequence(quant[MONO_CHAN][i+1],&len_esc); 
				bits += len_esc;
				if (write_flag) {
					data[counter] = esc_sequence;
					len[counter++] = len_esc;
				}
			} 
		}
	}

	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;
			/*      printf("tmp[%d] = %d\n",index-1,tmp[index-1]);*/
		}
	}

	for(i=1; i<8; i++){
		grouping_bits = grouping_bits << 1;
		if(tmp[i] == tmp[i-1]) {
			grouping_bits++;
		}
	}
	
	/*  printf("grouping_bits = %d  [i=%d]\n",grouping_bits,i);*/
	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 m;
	int diff,length,codeword;
	int previous_scale_factor;
	int previous_is_factor;       /* Intensity stereo */
//	int limit;
	int index = 0;
	int count = 0;
//	int zero_flag;
	int group_offset = 0;
	int sf_out = 0;
	int sf_not_out = 0;
	int nr_of_sfb_per_group=0;

	/* 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 = scale_factors[0];
	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;
#ifdef OLDHUFF
				length = huff[12][diff+60][FIRSTINTAB];
#else
				length = huff12[diff+60][FIRSTINTAB];
#endif
				k+=length;
				previous_is_factor = scale_factors[index];
				if (write_flag == 1 ) {   
#ifdef OLDHUFF
					codeword = huff[12][diff+60][LASTINTAB];
#else
					codeword = huff12[diff+60][LASTINTAB];
#endif
					BsPutBit(fixed_stream,codeword,length); 
					sf_out++;
				}
			} else if (quantInfo->book_vector[index]) {
				/* only send scalefactors if using non-zero codebooks */
				diff = scale_factors[index] - previous_scale_factor;
#ifdef OLDHUFF
				length = huff[12][diff+60][FIRSTINTAB];
#else
				length = huff12[diff+60][FIRSTINTAB];
#endif
				k+=length;
				previous_scale_factor = scale_factors[index];
				if (write_flag == 1 ) {   
#ifdef OLDHUFF
					codeword = huff[12][diff+60][LASTINTAB];
#else
					codeword = huff12[diff+60][LASTINTAB];
#endif
					BsPutBit(fixed_stream,codeword,length); 
					sf_out++;
				}
			}
			else {
				/*	if (write_flag) printf("  zero_flag = 1, [j=%d index=%d i=%d]\n",j,index,i);*/
				sf_not_out++;
			}
			index++;
		}
	}
	/*  if (write_flag) printf("sf_out = %d  sf_not_out = %d\n",sf_out,sf_not_out);*/
	return(k);
}

⌨️ 快捷键说明

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