📄 aac_qc.c
字号:
if (qc_select == AAC_QC) bits_written += write_aac_end_id(fixed_stream); } return( bits_written );}int sort_for_grouping(int sfb_offset[], int sfb_width_table[], double *p_spectrum, int num_window_groups, int window_group_length[], int nr_of_sfb, double *allowed_dist, double *psy_allowed_dist, int blockSizeSamples ){ int i,j,ii; int index = 0; double tmp[1024]; int group_offset=0; int k=0; int shortBlockLines = blockSizeSamples/8; /* calc org sfb_offset just for shortblock */ sfb_offset[k]=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 */#ifdef DEBUG_xx for (k=0; k<blockSizeSamples; k++){ p_spectrum[k] = k; }#endif 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[ii+ sfb_offset[k] + shortBlockLines*j +group_offset]; } } group_offset += shortBlockLines*window_group_length[i]; }#ifdef DEBUG_xx ii=0; for (i=0;i<num_window_groups;i++){ fprintf(stderr,"\ngroup%d: " ,i); for (k=0; k< shortBlockLines *window_group_length[i]; k++){ fprintf(stderr," %4.0f" ,tmp[ii++]); } }#endif for (k=0; k<blockSizeSamples; k++){ p_spectrum[k] = tmp[k]; } /* now calc the new sfb_offset table for the whole p_spectrum vector*/ index = 0; sfb_offset[index++] = 0; for (i=0; i < num_window_groups; i++) { for (k=0 ; k <nr_of_sfb; k++) { allowed_dist[k+ i* nr_of_sfb]=psy_allowed_dist[k]; sfb_offset[index] = sfb_offset[index-1] + sfb_width_table[k]*window_group_length[i] ; index++; } } /* *nr_of_sfb = *nr_of_sfb * num_window_groups; */ return 0;}int bit_search(int quant[NUM_COEFF], int book_vector[], int huffman_code_table[13][1090][4], int sfb_offset[], int nr_of_sfb, WINDOW_SEQUENCE windowSequence, int nr_sfb_per_win ) /* 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 MAX_SCFAC_BANDS 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[MAX_SCFAC_BANDS][3]. There are MAX_SCFAC_BANDS 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,m,n; int hop; int min_book_choice[128][3]={{0x0}}; int bit_stats[300][3]={{0x0}}; int total_bits; int total_bit_count; int levels; double fraction; int startIdx,endIdx,startWin,endWin;#if 1 levels = (int) ((log((double)nr_of_sfb)/log((double)2.0))+1); fraction = (pow(2,levels)+nr_of_sfb)/(double)(pow(2,levels)); #else if (nr_of_sfb == 49){ /* one long window */ levels = 6; fraction = 1.75; } else if (nr_of_sfb == 8*14){ /* 8 short windows */ levels = 7; fraction = 1.875; } else { printf(" nr_of_sfb = %d INVALID\n",nr_of_sfb); exit(-1); }#endif for(i=0;i<5;i++){ hop = (int)(pow(2,i)); if (hop>nr_sfb_per_win){ hop=nr_sfb_per_win; } total_bits = noiseless_bit_count(quant,huffman_code_table,hop,min_book_choice,sfb_offset,nr_of_sfb,windowSequence,nr_sfb_per_win); /* load up the (not-full) binary search tree with the min_book_choice values */ startIdx=0; m=0; total_bit_count = 0; for (j=(int)(pow(2,levels-i)); j<(int)(fraction*pow(2,levels-i)); j++){ if ((j>=300) || (startIdx >= 128)) { fprintf(stderr,"\n j %d startIdx%d,levels %d, fraction %f ",j,startIdx,levels,(float)fraction); CommonWarning("\n error in bitcount"); } startWin=startIdx/nr_sfb_per_win; if ((startIdx+hop) > nr_of_sfb) endIdx = nr_of_sfb; else endIdx = startIdx+hop; endWin=endIdx/nr_sfb_per_win; if (startWin!=endWin){ endIdx=(startWin+1)*nr_sfb_per_win; } bit_stats[j][0] = min_book_choice[startIdx][0]; /* the minimum bit cost for this section */ bit_stats[j][1] = min_book_choice[startIdx][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]) && (hop<nr_sfb_per_win) ){ /* it is cheaper to combine surrounding sfb secionts into one larger huffman book section */ for(n=startIdx;n<endIdx;n++) { /* write the optimal huffman book value for the new larger section */ 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 */ book_vector[startIdx] = 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]; startIdx=endIdx; m++; } } /* book_vector[k] = book_vector[k-1]; */ return(total_bit_count);}int noiseless_bit_count( int quant_data[NUM_COEFF], int huffman_code_table[13][1090][4], int hop, int min_book_choice[128][3], int sfb_offset[], int nr_of_sfb, WINDOW_SEQUENCE windowSequence, int nr_sfb_per_win ){ int startIdx,j,k; /* This function inputs: - the quantized spectral data, 'quant_data[]'; - 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][2]; int total_bits_cost = 0; int data[NUM_COEFF*5]; int len[NUM_COEFF*5]; int counter = 0; int offset, length, end; int endIdx; int write_flag = 0; int startWin,endWin; /* each section is 'hop' scalefactor bands wide */ if (hop>nr_sfb_per_win){ CommonWarning("\nhop>nr_sfb_per_win"); } startIdx=0; while(startIdx<nr_of_sfb) { startWin=startIdx/nr_sfb_per_win; if ((startIdx+hop) > nr_of_sfb) endIdx = nr_of_sfb; else endIdx = startIdx+hop; endWin=endIdx/nr_sfb_per_win; if (startWin!=endWin){ endIdx=(startWin+1)*nr_sfb_per_win; } /* 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[startIdx]; j<sfb_offset[endIdx]; j++){ /* snl */ if (abs(quant_data[j]) > max_sb_coeff) max_sb_coeff = abs(quant_data[j]); } j = 0; offset = sfb_offset[startIdx]; if ((startIdx+hop) > nr_of_sfb){ end = sfb_offset[nr_of_sfb]; } else end = sfb_offset[startIdx+hop]; length = end - offset; /* all spectral coefficients in this section are zero */ if (max_sb_coeff == 0) { book_choice[j][0] = output_bits(windowSequence,huffman_code_table,0,quant_data,offset,length,data,len,&counter,write_flag); book_choice[j][1] = 0; j++; } else { /* if the section does have non-zero coefficients */ if(max_sb_coeff < 2){ counter = 0; /* just for debugging : using data and len vectors */ book_choice[j][0] = output_bits(windowSequence,huffman_code_table,1,quant_data,offset,length,data,len,&counter,write_flag); book_choice[j][1] = 1; j++; counter = 0; /* just for debugging : using data and len vectors */ book_choice[j][0] = output_bits(windowSequence,huffman_code_table,2,quant_data,offset,length,data,len,&counter,write_flag); book_choice[j][1] = 2; j++; } if (max_sb_coeff < 3){ counter = 0; /* just for debugging : using data and len vectors */ book_choice[j][0] = output_bits(windowSequence,huffman_code_table,3,quant_data,offset,length,data,len,&counter,write_flag); book_choice[j][1] = 3; j++; counter = 0; /* just for debugging : using data and len vectors */ book_choice[j][0] = output_bits(windowSequence,huffman_code_table,4,quant_data,offset,length,data,len,&counter,write_flag); book_choice[j][1] = 4; j++; } if (max_sb_coeff < 5){ counter = 0; /* just for debugging : using data and len vectors */ book_choice[j][0] = output_bits(windowSequence,huffman_code_table,5,quant_data,offset,length,data,len,&counter,write_flag); book_choice[j][1] = 5; j++; counter = 0; /* just for debugging : using data and len vectors */ book_choice[j][0] = output_bits(windowSequence,huffman_code_table,6,quant_data,offset,length,data,len,&counter,write_flag); book_choice[j][1] = 6; j++; } if (max_sb_coeff < 8){ counter = 0; /* just for debugging : using data and len vectors */ book_choice[j][0] = output_bits(windowSequence,huffman_code_table,7,quant_data,offset,length,data,len,&counter,write_flag); book_choice[j][1] = 7; j++; counter = 0; /* just for debugging : using data and len vectors */ book_choice[j][0] = output_bits(windowSequence,huffman_code_table,8,quant_data,offset,length,data,len,&counter,write_flag); book_choice[j][1] = 8; j++; } if (max_sb_coeff < 13){ counter = 0; /* just for debugging : using data and len vectors */ book_choice[j][0] = output_bits(windowSequence,huffman_code_table,9,quant_data,offset,length,data,len,&counter,write_flag); book_choice[j][1] = 9; j++; counter = 0; /* just for debugging : using data and len vectors */ book_choice[j][0] = output_bits(windowSequence,huffman_code_table,10,quant_data,offset,length,data,len,&counter,write_flag); book_choice[j][1] = 10; j++; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -