📄 aac_qc.c
字号:
/* (max_sb_coeff >= 13), choose table 11 */ counter = 0; /* just for debugging : using data and len vectors */ book_choice[j][0] = output_bits(windowSequence,huffman_code_table,11,quant_data,offset,length,data,len,&counter,write_flag); book_choice[j][1] = 11; j++; } /* find the minimum bit cost and table number for huffman coding this scalefactor section */ min_book_choice[startIdx][0] = 100000; for(k=0;k<j;k++){ if (book_choice[k][0] < min_book_choice[startIdx][0]){ min_book_choice[startIdx][1] = book_choice[k][1]; min_book_choice[startIdx][0] = book_choice[k][0]; } } total_bits_cost += min_book_choice[startIdx][0]; startIdx=endIdx; /* startIdx+hop; */ } 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( WINDOW_SEQUENCE windowSequence, int huffman_code_table[13][1090][4], int book, int quant_data[NUM_COEFF], int offset, int length, int data[], int len[], int *outside_counter, int write_flag ){ /* This function inputs - all the huffman codebooks, 'huff[]' - a specific codebook number, 'book' - the quantized spectral data, 'quant_data[]' - the offset into the spectral data to begin scanning, 'offset' - the 'length' of the segment to huffman code -> therefore, the segment quant_data[offset] to quant_data[offset+length-1] is huffman coded. - a flag, 'write_flag' to determine whether the codebooks and lengths need to be written to file. If write_flag=0, then this function is being used only in the quantization rate loop, and does not need to spend time writing the codebooks and lengths to file. If write_flag=1, then it is being called by the function output_bits(), which is sending the bitsteam out of the encoder. This function outputs - the number of bits required, 'bits' using the prescribed codebook, book applied to the given segment of spectral data. There are three parameters that are passed back and forth into this function. data[] and len[] are one-dimensional arrays that store the codebook values and their respective bit lengths. These are used when packing the data for the bitstream in output_bits(). The index into these arrays is 'outside_counter'. It gets incremented internally in this function as counter, then passed to the outside through outside_counter. The next time output_bits() is called, counter starts at the value it left off from the previous call. */ int esc_sequence; int len_esc; int index; int bits=0; int tmp = 0; int codebook,i,j; int counter; counter = *outside_counter; if (book == 0) { /* if using the zero codebook, data of zero length is sent */ if (( qdebug>3) && write_flag) fprintf(stderr,"\n [book %d] start%d ",book,offset); if (write_flag) { data[counter] = 0; len[counter++] = 0; } } if ((book == 1) || (book == 2)) { if (( qdebug>3) && write_flag) fprintf(stderr,"\n [book %d] start%d ",book,offset); for(i=offset;i<offset+length;i=i+4){ index = 27*quant_data[i] + 9*quant_data[i+1] + 3*quant_data[i+2] + quant_data[i+3] + 40; codebook = huffman_code_table[book][index][3]; tmp = huffman_code_table[book][index][2]; bits = bits + tmp; if (write_flag) { if (qdebug>3) fprintf(stderr," %d %d %d %d ",quant_data[i],quant_data[i+1],quant_data[i+2],quant_data[i+3]); data[counter] = codebook; len[counter++] = tmp; } } } if ((book == 3) || (book == 4)) { if (( qdebug>3) && write_flag) fprintf(stderr,"\n [book %d] start%d ",book,offset); for(i=offset;i<offset+length;i=i+4){ index = 27*abs(quant_data[i]) + 9*abs(quant_data[i+1]) + 3*abs(quant_data[i+2]) + abs(quant_data[i+3]); codebook = huffman_code_table[book][index][3]; tmp = huffman_code_table[book][index][2]; bits = bits + tmp; for(j=0;j<4;j++){ if(abs(quant_data[i+j]) > 0) bits += 1; /* only for non-zero spectral coefficients */ } if (write_flag) { if (qdebug>3) fprintf(stderr," %d %d %d %d ",quant_data[i],quant_data[i+1],quant_data[i+2],quant_data[i+3]); data[counter] = codebook; len[counter++] = tmp; for(j=0;j<4;j++){ if(quant_data[i+j] > 0) { /* send out '0' if a positive value */ data[counter] = 0; len[counter++] = 1; } if(quant_data[i+j] < 0) { /* send out '1' if a negative value */ data[counter] = 1; len[counter++] = 1; } } } } } if ((book == 5) || (book == 6)) { if (( qdebug>3) && write_flag) fprintf(stderr,"\n [book %d] start%d ",book,offset); for(i=offset;i<offset+length;i=i+2){ index = 9*(quant_data[i]) + (quant_data[i+1]) + 40; codebook = huffman_code_table[book][index][3]; tmp = huffman_code_table[book][index][2]; bits = bits + tmp; if (write_flag) { if (qdebug>3) fprintf(stderr," %d %d ",quant_data[i],quant_data[i+1]); data[counter] = codebook; len[counter++] = tmp; } } } if ((book == 7) || (book == 8)) { if (( qdebug>3) && write_flag) fprintf(stderr,"\n [book %d] start%d ",book,offset); for(i=offset;i<offset+length;i=i+2){ index = 8*abs(quant_data[i]) + abs(quant_data[i+1]); codebook = huffman_code_table[book][index][3]; tmp = huffman_code_table[book][index][2]; bits = bits + tmp; for(j=0;j<2;j++){ if(abs(quant_data[i+j]) > 0) bits += 1; /* only for non-zero spectral coefficients */ } if (write_flag) { if (qdebug>3) fprintf(stderr," %d %d ",quant_data[i],quant_data[i+1]); data[counter] = codebook; len[counter++] = tmp; for(j=0;j<2;j++){ if(quant_data[i+j] > 0) { /* send out '0' if a positive value */ data[counter] = 0; len[counter++] = 1; } if(quant_data[i+j] < 0) { /* send out '1' if a negative value */ data[counter] = 1; len[counter++] = 1; } } } } } if ((book == 9) || (book == 10)) { if (( qdebug>3) && write_flag) fprintf(stderr,"\n [book %d] start%d ",book,offset); for(i=offset;i<offset+length;i=i+2){ index = 13*abs(quant_data[i]) + abs(quant_data[i+1]); codebook = huffman_code_table[book][index][3]; tmp = huffman_code_table[book][index][2]; bits = bits + tmp; for(j=0;j<2;j++){ if(abs(quant_data[i+j]) > 0) bits += 1; /* only for non-zero spectral coefficients */ } if (write_flag) { if (qdebug>3) fprintf(stderr," %d %d ",quant_data[i],quant_data[i+1]); data[counter] = codebook; len[counter++] = tmp; for(j=0;j<2;j++){ if(quant_data[i+j] > 0) { /* send out '0' if a positive value */ data[counter] = 0; len[counter++] = 1; } if(quant_data[i+j] < 0) { /* send out '1' if a negative value */ data[counter] = 1; len[counter++] = 1; } } } } } if ((book == 11)){ if (( qdebug>3) && write_flag) fprintf(stderr,"\n [book %d] start%d ",book,offset); /* First, calculate the indecies into the huffman tables */ for(i=offset;i<offset+length;i=i+2){ if ((abs(quant_data[i]) >= 16) && (abs(quant_data[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_data[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_data[i+1]); } else if (abs(quant_data[i+1]) >= 16) { /* the second codeword was above 16, not the first one */ index = 17*abs(quant_data[i]) + 16; } else { /* there were no values above 16, so no escape sequences */ index = 17*abs(quant_data[i]) + abs(quant_data[i+1]); } /* write out the codewords */ tmp = huffman_code_table[book][index][2]; codebook = huffman_code_table[book][index][3]; bits += tmp; if (write_flag) { if (qdebug>3) fprintf(stderr," %d %d ",quant_data[i],quant_data[i+1]); /* printf("[book %d] {%d %d} \n",book,quant_data[i],quant_data[i+1]);*/ data[counter] = codebook; len[counter++] = tmp; } /* Take care of the sign bits */ for(j=0;j<2;j++){ if(abs(quant_data[i+j]) > 0) bits += 1; /* only for non-zero spectral coefficients */ } if (write_flag) { for(j=0;j<2;j++){ if(quant_data[i+j] > 0) { /* send out '0' if a positive value */ data[counter] = 0; len[counter++] = 1; } if(quant_data[i+j] < 0) { /* send out '1' if a negative value */ data[counter] = 1; len[counter++] = 1; } } } /* write out the escape sequences */ if ((abs(quant_data[i]) >= 16) && (abs(quant_data[i+1]) >= 16)) { /* both codewords were above 16 */ /* code and transmit the first escape_sequence */ esc_sequence = calculate_esc_sequence(quant_data[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_data[i+1],&len_esc); bits += len_esc; if (write_flag) { data[counter] = esc_sequence; len[counter++] = len_esc; } } else if (abs(quant_data[i]) >= 16) { /* the first codeword was above 16, not the second one */ /* code and transmit the escape_sequence */ esc_sequence = calculate_esc_sequence(quant_data[i],&len_esc); bits += len_esc; if (write_flag) { data[counter] = esc_sequence; len[counter++] = len_esc; } } else if (abs(quant_data[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_data[i+1],&len_esc); bits += len_esc; if (write_flag) { data[counter] = esc_sequence; len[counter++] = len_esc; } } } } if (book == PNS_HCB) { /* if using the PNS codebook, data of zero length is sent */ if (( qdebug>3) && write_flag) fprintf(stderr,"\n [book %d] start%d ",book,offset); if (write_flag) { data[counter] = 0; len[counter++] = 0; } } *outside_counter = counter; /* send the current count back to the outside world */ return(bits);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -