📄 aac_qc.c
字号:
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[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 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[j]) > max_sb_coeff)
max_sb_coeff = ABS(quant[j]);
}
j = 0;
offset = sfb_offset[i];
if ((i+hop) > nr_of_sfb){
end = sfb_offset[nr_of_sfb];
}
else
end = sfb_offset[q];
length = end - offset;
/* all spectral coefficients in this section are zero */
if (max_sb_coeff == 0) {
book_choice[j][0] = output_bits(quantInfo,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,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,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,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,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,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,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,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,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,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,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,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[NUM_COEFF],
int offset,
int length,
int write_flag)
{
/*
This function inputs
- all the huffman codebooks, 'huff[]'
- a specific codebook number, 'book'
- the quantized spectral data, 'quant[][]'
- the offset into the spectral data to begin scanning, 'offset'
- the 'length' of the segment to huffman code
-> therefore, the segment quant[CHANNEL][offset] to quant[CHANNEL][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 'quantInfo->spectralCount''. 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;
/* 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[i] + 9*quant[i+1] + 3*quant[i+2] + quant[i+3] + 40;
if (book == 1) {
codebook = huff1[index][LASTINTAB];
tmp = huff1[index][FIRSTINTAB];
} else {
codebook = huff2[index][LASTINTAB];
tmp = huff2[index][FIRSTINTAB];
}
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[i]) + 9*ABS(quant[i+1]) + 3*ABS(quant[i+2]) + ABS(quant[i+3]);
if (book == 3) {
codebook = huff3[index][LASTINTAB];
tmp = huff3[index][FIRSTINTAB];
} else {
codebook = huff4[index][LASTINTAB];
tmp = huff4[index][FIRSTINTAB];
}
bits = bits + tmp;
for(j=0;j<4;j++){
if(ABS(quant[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[i+j] > 0) { /* send out '0' if a positive value */
data[counter] = 0;
len[counter++] = 1;
}
if(quant[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[i]) + (quant[i+1]) + 40;
if (book == 5) {
codebook = huff5[index][LASTINTAB];
tmp = huff5[index][FIRSTINTAB];
} else {
codebook = huff6[index][LASTINTAB];
tmp = huff6[index][FIRSTINTAB];
}
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[i]) + ABS(quant[i+1]);
if (book == 7) {
codebook = huff7[index][LASTINTAB];
tmp = huff7[index][FIRSTINTAB];
} else {
codebook = huff8[index][LASTINTAB];
tmp = huff8[index][FIRSTINTAB];
}
bits = bits + tmp;
for(j=0;j<2;j++){
if(ABS(quant[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[i+j] > 0) { /* send out '0' if a positive value */
data[counter] = 0;
len[counter++] = 1;
}
if(quant[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[i]) + ABS(quant[i+1]);
if (book == 9) {
codebook = huff9[index][LASTINTAB];
tmp = huff9[index][FIRSTINTAB];
} else {
codebook = huff10[index][LASTINTAB];
tmp = huff10[index][FIRSTINTAB];
}
bits = bits + tmp;
for(j=0;j<2;j++){
if(ABS(quant[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[i+j] > 0) { /* send out '0' if a positive value */
data[counter] = 0;
len[counter++] = 1;
}
if(quant[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[i]) >= 16) && (ABS(quant[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[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[i+1]);
}
else if (ABS(quant[i+1]) >= 16) { /* the second codeword was above 16, not the first one */
index = 17*ABS(quant[i]) + 16;
}
else { /* there were no values above 16, so no escape sequences */
index = 17*ABS(quant[i]) + ABS(quant[i+1]);
}
/* write out the codewords */
tmp = huff11[index][FIRSTINTAB];
codebook = huff11[index][LASTINTAB];
bits += tmp;
if (write_flag) {
/* printf("[book %d] {%d %d} \n",book,quant[i],quant[i+1]);*/
data[counter] = codebook;
len[counter++] = tmp;
}
/* Take care of the sign bits */
for(j=0;j<2;j++){
if(ABS(quant[i+j]) > 0) bits += 1; /* only for non-zero spectral coefficients */
}
if (write_flag) {
for(j=0;j<2;j++){
if(quant[i+j] > 0) { /* send out '0' if a positive value */
data[counter] = 0;
len[counter++] = 1;
}
if(quant[i+j] < 0) { /* send out '1' if a negative value */
data[counter] = 1;
len[counter++] = 1;
}
}
}
/* write out the escape sequences */
if ((ABS(quant[i]) >= 16) && (ABS(quant[i+1]) >= 16)) { /* both codewords were above 16 */
/* code and transmit the first escape_sequence */
esc_sequence = calculate_esc_sequence(quant[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[i+1],&len_esc);
bits += len_esc;
if (write_flag) {
data[counter] = esc_sequence;
len[counter++] = len_esc;
}
}
else if (ABS(quant[i]) >= 16) { /* the first codeword was above 16, not the second one */
/* code and transmit the escape_sequence */
esc_sequence = calculate_esc_sequence(quant[i],&len_esc);
bits += len_esc;
if (write_flag) {
data[counter] = esc_sequence;
len[counter++] = len_esc;
}
}
else if (ABS(quant[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[i+1],&len_esc);
bits += len_esc;
if (write_flag) {
data[counter] = esc_sequence;
len[counter++] = len_esc;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -