www.pudn.com > aacenc.rar > aac_qc.c
#include "common.h" #include "bitstream.h" #include "tf_main.h" #include "aac_qc.h" #include "aac_se_enc.h" #include#ifdef OLDHUFF #include "hufftab.h" #else #include "hufftab5.h" #endif #define QUANT_TYPE double QUANT_TYPE pow_quant[9000]; int g_Count; void tf_init_encode_spectrum_aac( int quality ) { unsigned int j; g_Count = quality; for (j=0;j<9000;j++){ pow_quant[j]=pow(j, ((double)4.0/(double)3.0)); } } int tf_encode_spectrum_aac( double *p_spectrum[MAX_TIME_CHANNELS], double *PsySigMaskRatio[MAX_TIME_CHANNELS], double allowed_dist[MAX_TIME_CHANNELS][MAX_SCFAC_BANDS], double energy[MAX_TIME_CHANNELS][MAX_SCFAC_BANDS], enum WINDOW_TYPE block_type[MAX_TIME_CHANNELS], int sfb_width_table[MAX_TIME_CHANNELS][MAX_SCFAC_BANDS], int nr_of_sfb[MAX_TIME_CHANNELS], int average_block_bits, int available_bitreservoir_bits, int padding_limit, BsBitStream *fixed_stream, BsBitStream *var_stream, int nr_of_chan, double *p_reconstructed_spectrum[MAX_TIME_CHANNELS], int useShortWindows, /* Window_shape window_shape,*/ /* offers the possibility to select different window functions */ int aacAllowScalefacs, AACQuantInfo* quantInfo, /* AAC quantization information */ Ch_Info* ch_info, int varBitRate, int bitRate) { int quant[CHANNEL][NUM_COEFF]; int i=0; int j=0; int k,sfb; int ch=0; QUANT_TYPE max_dct_line = 0; int global_gain; /* int common_scalefac; */ /* Now in AACQuantInfo */ /* int scale_factor[SFB_NUM_MAX]; */ /* Now in AACQuantInfo structure */ int store_common_scalefac; QUANT_TYPE error_energy/*[SFB_NUM_MAX]*/; QUANT_TYPE pow_spectrum[NUM_COEFF]; int sfb_amplify_check[SFB_NUM_MAX]; QUANT_TYPE requant[CHANNEL][NUM_COEFF]; QUANT_TYPE ftmp; int largest_sb, sb; QUANT_TYPE largest_ratio; QUANT_TYPE ratio/*[SFB_NUM_MAX]*/; /* int data[5*NUM_COEFF]; */ /* for each pair of spec values 5 bitstream elemets are requiered: 1*(esc)+2*(sign)+2*(esc value)=5 */ /* Now in AACQuantInfo structure */ /* int len[5*NUM_COEFF]; */ /* Now in AACQuantInfo structure */ int max_quant/*,tmp*/; /* int sfb_offset[250]; */ /* Now in AACQuantInfo structure */ int sf_bits; int extra_bits; int book_bits; int spectral_bits; int max_bits; /* int book_vector[SFB_NUM_MAX]; */ /* Now in AACQuantInfo structure */ /* int num_window_groups; */ /* Now in AACQuantInfo structure */ int bits_written=0; int output_book_vector[SFB_NUM_MAX*2]; int all_amplified; int maxRatio; /* change these values to test the different GROUPING possibilities */ /* int window_group_length[8] = {1,0,0,0,0}; */ /* Now in AACQuantInfo structure */ int start_com_sf; QUANT_TYPE SigMaskRatio[SFB_NUM_MAX]; int calc_sf[SFB_NUM_MAX]; /* num_window_groups = 1; */ /* Now in AACQuantInfo structure */ IS_Info *is_info; int *ptr_book_vector; /* Set up local pointers to quantInfo elements for convenience */ int* sfb_offset = quantInfo -> sfb_offset; int* scale_factor = quantInfo -> scale_factor; int* common_scalefac = &(quantInfo -> common_scalefac); int count = g_Count; int check; start_com_sf = 40; /* Set block type in quantization info */ quantInfo -> block_type = block_type[MONO_CHAN]; /** create the sfb_offset tables **/ if (quantInfo -> block_type == ONLY_SHORT_WINDOW) { /* Now compute interleaved sf bands and spectrum */ sort_for_grouping( quantInfo, /* ptr to quantization information */ /* sfb_offset, */ /* Now in AACQuantInfo structure */ sfb_width_table[MONO_CHAN], /* Widths of single window */ p_spectrum, /* Spectral values, noninterleaved */ /* num_window_groups, */ /* Now in AACQuantInfo structure */ /* window_group_length, */ /* Now in AACQuantInfo structure */ /* &nr_of_sfb[MONO_CHAN], */ /* Now in AACQuantInfo structure */ SigMaskRatio, PsySigMaskRatio[MONO_CHAN] ); extra_bits = 51; } else{ /* For long windows, band are not actually interleaved */ if ((quantInfo -> block_type == ONLY_LONG_WINDOW) || (quantInfo -> block_type == LONG_SHORT_WINDOW) || (quantInfo -> block_type == SHORT_LONG_WINDOW)) { quantInfo->nr_of_sfb = quantInfo->max_sfb; sfb_offset[0] = 0; k=0; for( i=0; i< quantInfo -> nr_of_sfb; i++ ){ sfb_offset[i] = k; k +=sfb_width_table[MONO_CHAN][i]; SigMaskRatio[i]=PsySigMaskRatio[MONO_CHAN][i]; } sfb_offset[i] = k; extra_bits = 100; /* header bits and more ... */ } } extra_bits += 1; /* Take into account bits for TNS data */ extra_bits += WriteTNSData(quantInfo,fixed_stream,0); /* Count but don't write */ /* for short windows, compute interleaved energy here */ if (quantInfo->block_type==ONLY_SHORT_WINDOW) { int numWindowGroups = quantInfo->num_window_groups; int maxBand = quantInfo->max_sfb; int windowOffset=0; int sfb_index=0; int g; for (g=0;g window_group_length[g]; int b; for (b=0;b nr_of_sfb; sfb++ ) { allowed_dist[MONO_CHAN][sfb] = 0.0; } #else for( sfb=0; sfb< quantInfo -> nr_of_sfb; sfb++ ) { if ((10*log10(energy[MONO_CHAN][sfb]+1e-15))>70 ) { /* ? */ allowed_dist[MONO_CHAN][sfb] = energy[MONO_CHAN][sfb] * SigMaskRatio[sfb]; } else { allowed_dist[MONO_CHAN][sfb] = energy[MONO_CHAN][sfb] * 1.1; } } #endif /** find the maximum spectral coefficient **/ /* Bug fix, 3/10/98 CL */ /* for(i=0; i nr_of_sfb]; i++){ pow_spectrum[i]=(pow(ABS(p_spectrum[ch][i]), 0.75)); if ((p_spectrum[ch][i]) > max_dct_line){ max_dct_line = p_spectrum[ch][i]; } } if (max_dct_line!=0.0) { *common_scalefac = start_com_sf + (int)(16/3 * (log(ABS(pow(max_dct_line,0.75)/MAX_QUANT)/log(2.0)))); } else { *common_scalefac = 20; } if ((*common_scalefac>200) || (*common_scalefac<0) ) *common_scalefac = 20; *common_scalefac -= 10; /* Very simple VBR implementation. Doesn't work really well, but extremely fast encoding. */ if (varBitRate) *common_scalefac += 2*count; /* initialize the scale_factors that aren't intensity stereo bands */ is_info=&(ch_info->is_info); for(k=0; k< quantInfo -> nr_of_sfb ;k++) { sfb_amplify_check[k] = 0; calc_sf[k]=1; scale_factor[k]=((is_info->is_present)&&(is_info->is_used[k])) ? scale_factor[k] : 0; } ratio = 0.0; /* Mark IS bands by setting book_vector to INTENSITY_HCB */ ptr_book_vector=quantInfo->book_vector; for (k=0;k nr_of_sfb;k++) { if ((is_info->is_present)&&(is_info->is_used[k])) { ptr_book_vector[k] = (is_info->sign[k]) ? INTENSITY_HCB2 : INTENSITY_HCB; } else { ptr_book_vector[k] = 0; } } maxRatio=1; largest_sb = 0; do { /* rate loop */ do { /* distortion loop */ max_quant = 0; largest_ratio = 0; store_common_scalefac=*common_scalefac; for (sb=0; sb nr_of_sfb;sb++) { if (calc_sf[sb]) { register QUANT_TYPE quantFac; register QUANT_TYPE invQuantFac; quantFac = pow(2.0, 0.1875*(scale_factor[sb] - *common_scalefac )); invQuantFac = pow(2.0,-0.25 * (scale_factor[sb] - *common_scalefac )); error_energy = 0.0; ftmp=0; for (i=sfb_offset[sb]; i MAX_QUANT) { //CommonExit(1,"Error in quantization module"); return MBERROR; } tmp_requant = pow_quant[tmp_quant] * invQuantFac; tmp_p_spectrum = p_spectrum[ch][i]; quant[ch][i] = sgn(tmp_p_spectrum) * tmp_quant; /* restore the original sign */ requant[ch][i] = tmp_requant; /* measure the distortion in each scalefactor band */ /* error_energy[sb] += pow((p_spectrum[ch][i] - requant), 2.0); */ tmp_linediff = (ABS(tmp_p_spectrum) - tmp_requant); error_energy += tmp_linediff*tmp_linediff; } /* --- for (i=sfb_offset[sb] --- */ if ( ( allowed_dist[ch][sb] != 0) && (energy[MONO_CHAN][sb] > allowed_dist[ch][sb]) ){ ratio = error_energy / allowed_dist[ch][sb]; } else { ratio = 1e-15; sfb_amplify_check[sb] = 1; } /* find the scalefactor band with the largest error ratio */ if ((ratio > maxRatio) && (scale_factor[sb]<60) && aacAllowScalefacs){ scale_factor[sb]++; sfb_amplify_check[sb] = 1; calc_sf[sb]=1; } else { calc_sf[sb]=0; } if ( (ratio > largest_ratio)&& (scale_factor[sb]<60) ){ largest_ratio = ratio; } } /* --- for (sb=0; --- */ } /* amplify the scalefactor of the worst scalefactor band */ /* check to see if all the sfb's have been amplified.*/ /* if they have been all amplified, then all_amplified remains at 1 and we're done iterating */ all_amplified = 1; for(j=0; j nr_of_sfb;j++){ if (sfb_amplify_check[j] == 0 ) all_amplified = 0; } } while ((largest_ratio > maxRatio) && (all_amplified == 0) ); /* find a good method to section the scalefactor bands into huffman codebook sections */ bit_search(quant, /* Quantized spectral values */ /* book_vector, */ /* Now in quantInfo. */ // huff, /* Huffman codebooks */ quantInfo); /* Quantization information */ /* calculate the amount of bits needed for encoding the huffman codebook numbers */ book_bits = sort_book_numbers(quantInfo, /* Quantization information */ /* book_vector,*/ /* Now in quantInfo. */ output_book_vector, /* Output codebook vector, formatted for bitstream */ fixed_stream, /* Bitstream */ 0); /* Write flag: 0 count, 1 write */ /* calculate the amount of bits needed for the spectral values */ quantInfo -> spectralCount = 0; spectral_bits = 0; for(k=0;k< quantInfo -> nr_of_sfb;k++) { calc_sf[k]=1; spectral_bits += output_bits(/* block_type,*/ /* Now in quantInfo */ quantInfo, // huff, quantInfo->book_vector[k], quant, quantInfo->sfb_offset[k], quantInfo->sfb_offset[k+1]-quantInfo->sfb_offset[k], 0); } /* the number of bits for the scalefactors */ sf_bits = write_scalefactor_bitstream(/* nr_of_sfb[MONO_CHAN],*//* Now in quantInfo */ fixed_stream, /* Bitstream */ 0, /* Write flag */ quantInfo ); max_bits = spectral_bits + sf_bits + book_bits + extra_bits; if (all_amplified){ maxRatio = maxRatio*2; *common_scalefac += 1; } *common_scalefac += 1; if ( *common_scalefac > 200 ) { //CommonExit(-1,"Error in loops: common_scalefac"); return MBERROR; } if (varBitRate) { check = 0; //(count-- > 0) ? 1 : 0; } else { check = (max_bits > average_block_bits) ? 1 : 0; } } while( check ); // } while( max_bits > average_block_bits); *common_scalefac = store_common_scalefac; /* offset the differenec of common_scalefac and scalefactors by SF_OFFSET */ for (i=0; i nr_of_sfb; i++){ if ((ptr_book_vector[i]!=INTENSITY_HCB)&&(ptr_book_vector[i]!=INTENSITY_HCB2)) { scale_factor[i] = *common_scalefac - scale_factor[i] + SF_OFFSET; } } *common_scalefac = global_gain = scale_factor[0]; /* place the codewords and their respective lengths in arrays data[] and len[] respectively */ /* there are 'counter' elements in each array, and these are variable length arrays depending on the input */ quantInfo -> spectralCount = 0; for(k=0;k< quantInfo -> nr_of_sfb; k++) { spectral_bits += output_bits( quantInfo, quantInfo->book_vector[k], quant, quantInfo->sfb_offset[k], quantInfo->sfb_offset[k+1]-quantInfo->sfb_offset[k], 1); } #if 0 /* No prediction anyway. */ /* write the reconstructed spectrum to the output for use with prediction */ { int ch, i; for( ch=0; ch nr_of_sfb; sb++){ if ((ptr_book_vector[sb]==INTENSITY_HCB)||(ptr_book_vector[sb]==INTENSITY_HCB2)){ for (i=sfb_offset[sb]; i sfb_offset; int* nr_of_sfb = &(quantInfo -> nr_of_sfb); int* window_group_length; int num_window_groups; *nr_of_sfb = quantInfo->max_sfb; /* Init to max_sfb */ window_group_length = quantInfo -> window_group_length; num_window_groups = quantInfo -> num_window_groups; /* 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 */ 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 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 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 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 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 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 = 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; 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 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 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 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 = 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 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 num_window_groups; j++){ for(i=0;i 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); }