📄 my_quantize.c
字号:
#include "aacenc.h"
#include "bitstream.h"
#include "tf_main.h"
#include "pulse.h"
#include "aac_qc.h"
#include "aac_se_enc.h"
#include <math.h>
#include "hufftab5.h"
#include "all.h"
int my_tf_encode_spectrum_aac(
double *p_spectrum[MAX_TIME_CHANNELS], //输入1:MDCT的能量谱向量
double *PsySigMaskRatio[MAX_TIME_CHANNELS], //输入2:心理声学模型输出:信掩比
double allowed_dist[MAX_TIME_CHANNELS][MAX_SCFAC_BANDS], //输入2:允许的最大误差
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], //输入5:SCALEFACTOR BAND WIDTH
int nr_of_sfb[MAX_TIME_CHANNELS], //sfb的数目
int average_block_bits, //输入3:平均可用比特数
int available_bitreservoir_bits, //输入4:可用比特数
int padding_limit,
BsBitStream *fixed_stream, //输出:
BsBitStream *var_stream, //输出:
int nr_of_chan,
double *p_reconstructed_spectrum[MAX_TIME_CHANNELS],
int useShortWindows,
int aacAllowScalefacs,
AACQuantInfo* quantInfo, /* AAC quantization information */
Ch_Info* ch_info,
int varBitRate,
int bitRate,
int is_first_frame) //输入定义见标准P134
{
int quant[NUM_COEFF];
int i=0;
int j=0;
int k;
double max_dct_line = 0;
int global_gain;
int store_common_scalefac;
int best_scale_factor[SFB_NUM_MAX];
double pow_spectrum[NUM_COEFF];
double requant[NUM_COEFF];
int sb;
int extra_bits;
int max_bits;
int output_book_vector[SFB_NUM_MAX*2];
int start_com_sf;
double SigMaskRatio[SFB_NUM_MAX];
double MaskValue[SFB_NUM_MAX];
double D_temp;
double D[SFB_NUM_MAX];
double M_noise[SFB_NUM_MAX];
double max_gain;
int real_used_bits_old;
double Gain_old;
int real_used_bits_new;
double Gain_new;
double a_scale;
IS_Info *is_info;
int *ptr_book_vector;
int inner_loop_count;
int if_success;
char message[255];
/* 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 outer_loop_count;
int quantizer_change;
int over = 0, best_over = 100, better;
int sfb_overflow, amp_over;
int best_common_scalefac;
int prev_scfac, prev_is_scfac;
double noise_thresh;
double over_noise, tot_noise, max_noise;
double noise[SFB_NUM_MAX];
double best_max_noise = 0;
double best_over_noise = 0;
double best_tot_noise = 0;
static int init = -1;
/* Set block type in quantization info */
quantInfo -> block_type = block_type[MONO_CHAN];
#if 0
if (init != quantInfo->block_type) {
init = quantInfo->block_type;
// compute_ath(quantInfo, ATH);
}
#endif
/** 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_width_table[MONO_CHAN], /* Widths of single window */
p_spectrum, /* Spectral values, noninterleaved */
SigMaskRatio, /*output new interleaved sigmaskratio*/
PsySigMaskRatio[MONO_CHAN] /*input, from psycho calculate*/
);
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 ... *//*for what?*/
}
}
extra_bits += 1; /*why?*/
/* Take into account bits for TNS data */
extra_bits += WriteTNSData(quantInfo,fixed_stream,0); /* Count but don't write */
// if(quantInfo->block_type!=ONLY_SHORT_WINDOW)
/* Take into account bits for LTP data */
// extra_bits += WriteLTP_PredictorData(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<numWindowGroups;g++) {
int numWindowsThisGroup = quantInfo->window_group_length[g];
int b;
for (b=0;b<maxBand;b++) {
double sum=0.0;
int w;
for (w=0;w<numWindowsThisGroup;w++) {
int bandNum = (w+windowOffset)*maxBand + b;
sum += energy[MONO_CHAN][bandNum];
}
energy[MONO_CHAN][sfb_index] = sum;
sfb_index++;
}
windowOffset += numWindowsThisGroup;
}
}
/* 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++) {
scale_factor[k]=((is_info->is_present)&&(is_info->is_used[k])) ? scale_factor[k] : 0 /*min(15,(int)(1.0/SigMaskRatio[k]+0.5))*/;
}
/* Mark IS bands by setting book_vector to INTENSITY_HCB */
ptr_book_vector=quantInfo->book_vector;
for (k=0;k<quantInfo->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;
}
}
/* PNS prepare */
for(sb=0; sb < quantInfo->nr_of_sfb; sb++ )
quantInfo->pns_sfb_flag[sb] = 0;
if (block_type[MONO_CHAN] != ONLY_SHORT_WINDOW) { /* long blocks only */
for(sb = pns_sfb_start; sb < quantInfo->nr_of_sfb; sb++ ) {
/* Calc. pseudo scalefactor */
if (energy[0][sb] == 0.0) {
quantInfo->pns_sfb_flag[sb] = 0;
continue;
}
if ((10*log10(energy[MONO_CHAN][sb]*sfb_width_table[0][sb]+1e-60)<70)||(SigMaskRatio[sb] > 1.0)) {
quantInfo->pns_sfb_flag[sb] = 1;
quantInfo->pns_sfb_nrg[sb] = (int) (2.0 * log(energy[0][sb]*sfb_width_table[0][sb]+1e-60) / log(2.0) + 0.5) + PNS_SF_OFFSET;
/* Erase spectral lines */
for( i=sfb_offset[sb]; i<sfb_offset[sb+1]; i++ ) {
p_spectrum[0][i] = 0.0;
}
}
}
}
/* Compute allowed distortion */
for(sb = 0; sb < quantInfo->nr_of_sfb; sb++) {
allowed_dist[MONO_CHAN][sb] = energy[MONO_CHAN][sb] * SigMaskRatio[sb];
}
/** find the maximum spectral coefficient **/
/* Bug fix, 3/10/98 CL */
/* for(i=0; i<NUM_COEFF; i++){ */
for(i=0; i < sfb_offset[quantInfo->nr_of_sfb]; i++){
pow_spectrum[i] = (pow(ABS(p_spectrum[0][i]), 0.75));
sign[i] = sgn(p_spectrum[0][i]);
if ((ABS(pow_spectrum[i])) > max_dct_line){
max_dct_line = pow_spectrum[i];
}
} /*the max of 1024 spectrum lines */
//my new quantize program;
max_gain = 0.0;
for(sb=0; sb<quantInfo->nr_of_sfb; sb++)
{
double gain_temp=0.0;
MaskValue[sb] = allowed_dist[MONO_CHAN][sb];
D_temp = 0.0;
for(i=sfb_offset[sb]; i<sfb_offset[sb+1]; i++)
{
D_temp+=sqrt(p_spectrum[i]);
}
D[sb]=0.1640578*D_temp/(sfb_offset[sb+1]-sfb_offset[sb]);
M_noise[sb] = MaskValue[sb]*0.75;
gain_temp = M_noise[sb]/D[sb];
if(gain_temp>max_gain)
max_gain=gain_temp;
}
max_gain = 2/3*log(max_gain)/log(2.0);
for(sb=0; sb<quantInfo->nr_of_sfb; sb++)
{
scale_factor[sb]=pow(2, max_gain)*pow((D[sb]/MaskValue[sb]), 2/3);
}
if(is_first_frame)
{
quantInfo->last_b = 0.0;
}
start_com_sf = 30 + (int)(16/3 * (log(max_dct_line/MAX_QUANT)/log(2.0)));
if((start_com_sf<40)||(start_com_sf>200))
start_com_sf = 40;
*common_scalefac = start_com_sf;
if(quantize(quantInfo, p_spectrum[0], pow_spectrum, quant))
max_bits = 1000000;
else
{
real_used_bits_old = count_bits(quantInfo, quant, output_book_vector);
max_bits = real_used_bits_old + extra_bits;
Gain_old = *common_scalefac;
}
if(quantInfo->last_b == 0)
{
if(max_bits>average_block_bits)
*common_scalefac+=4;
else
*common_scalefac-=4;
if(quantize(quantInfo, p_spectrum[0], pow_spectrum, quant))
max_bits = 1000000;
else
{
real_used_bits_new = count_bits(quantInfo, quant, output_book_vector);
max_bits = real_used_bits_new + extra_bits;
Gain_new = *common_scalefac;
}
quantInfo->last_b = (Gain_new - Gain_old)/(real_used_bits_new - real_used_bits_old);
a_scale = Gain_new - (quantInfo->last_b)*real_used_bits_new;
*common_scalefac = a_scale+quantInfo->last_b*(average_block_bits-extra_bits);
}
else
{
a_scale = Gain_old - (quantInfo->last_b)*real_used_bits_old;
*common_scalefac = a_scale+quantInfo->last_b*(average_block_bits-extra_bits);
quantize(quantInfo, p_spectrum[0], pow_spectrum, quant);
real_used_bits_new = count_bits(quantInfo, quant, output_book_vector);
if((real_used_bits_new - average_block_bits + extra_bits)/(average_block_bits - extra_bits) > 0.1)
{
Gain_new = *common_scalefac;
quantInfo->last_b = (Gain_new - Gain_old)/(real_used_bits_new - real_used_bits_old);
a_scale = Gain_new - (quantInfo->last_b)*real_used_bits_new;
*common_scalefac = a_scale+quantInfo->last_b*(average_block_bits-extra_bits);
}
}
/* outer_loop_count = 0;
do { // outer iteration loop
outer_loop_count++;
over = 0;
sfb_overflow = 0;
if (max_dct_line == 0.0)
sfb_overflow = 1;
if (outer_loop_count == 1) {
quantizer_change = 8;
*common_scalefac = start_com_sf;
} else {
quantizer_change = 2;
}
inner_loop_count = 0;
do { // inner iteration loop
inner_loop_count++;
if(quantize(quantInfo, p_spectrum[0], pow_spectrum, quant))
max_bits = 1000000;
else
max_bits = count_bits(quantInfo, quant, output_book_vector) + extra_bits;
quantizer_change /= 2;
store_common_scalefac = *common_scalefac;
if (max_bits > average_block_bits)
*common_scalefac += quantizer_change;
else
*common_scalefac -= quantizer_change;
if (*common_scalefac > 200)
return FERROR;
if ((quantizer_change == 1)&&(max_bits > average_block_bits))
quantizer_change = 2;
} while (quantizer_change != 1);
over = calc_noise(quantInfo, p_spectrum[0], quant, requant, noise, allowed_dist[0],
&over_noise, &tot_noise, &max_noise);
better = quant_compare(best_over, best_tot_noise, best_over_noise,
best_max_noise, over, tot_noise, over_noise, max_noise);
prev_is_scfac = 0;
prev_scfac = store_common_scalefac;
for (sb = 0; sb < quantInfo->nr_of_sfb; sb++) {
if (ch_info->is_info.is_used[sb]) {
if ((scale_factor[sb] - prev_is_scfac) <= -59)
sfb_overflow = 1;
if ((scale_factor[sb] - prev_is_scfac) >= 59)
sfb_overflow = 1;
prev_is_scfac = scale_factor[sb];
} else {
if ((scale_factor[sb] - prev_scfac) <= -59)
sfb_overflow = 1;
if ((scale_factor[sb] - prev_scfac) >= 59)
sfb_overflow = 1;
prev_scfac = scale_factor[sb];
}
if (sfb_overflow == 1)
better = 0;
}
if (outer_loop_count == 1)
better = 1;
if (better) {
best_over = over;
best_max_noise = max_noise;
best_over_noise = over_noise;
best_tot_noise = tot_noise;
best_common_scalefac = store_common_scalefac;
for (sb = 0; sb < quantInfo->nr_of_sfb; sb++) {
best_scale_factor[sb] = scale_factor[sb];
}
}
amp_over = 0;
#if 1
noise_thresh = 0.0;
#else
noise_thresh = -900;
for ( sb = 0; sb < quantInfo->nr_of_sfb; sb++ )
noise_thresh = max(1.05*noise[sb], noise_thresh);
noise_thresh = min(noise_thresh, 0.0);
#endif
for (sb = 0; sb < quantInfo->nr_of_sfb; sb++) {
if ((noise[sb] > noise_thresh)&&(quantInfo->book_vector[sb]!=INTENSITY_HCB)&&(quantInfo->book_vector[sb]!=INTENSITY_HCB2)) {
amp_over++;
allowed_dist[0][sb] *= 2;
scale_factor[sb]++;
}
}
if (sfb_overflow == 0) {
sfb_overflow = 1;
for (sb = 0; sb < quantInfo->nr_of_sfb; sb++) {
if (scale_factor[sb] == 0)
sfb_overflow = 0;
}
}
if ((amp_over == 0)||(over == 0)||(amp_over==quantInfo->nr_of_sfb)||(sfb_overflow))
break;
} while (1);
*/
/* *common_scalefac = best_common_scalefac;
for (sb = 0; sb < quantInfo->nr_of_sfb; sb++) {
scale_factor[sb] = best_scale_factor[sb];
}
old_startsf = *common_scalefac;
*/
quantize(quantInfo, p_spectrum[0], pow_spectrum, quant);
calc_noise(quantInfo, p_spectrum[0], quant, requant, noise, allowed_dist[0],
&over_noise, &tot_noise, &max_noise);
if (quantInfo->block_type==ONLY_SHORT_WINDOW)
quantInfo->pulseInfo.pulse_data_present = 0;
else
PulseCoder(quantInfo, quant);
count_bits(quantInfo, quant, output_book_vector);
/* offset the differenec of common_scalefac and scalefactors by SF_OFFSET */
for (i=0; i<quantInfo->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++) {
output_bits(
quantInfo,
quantInfo->book_vector[k],
quant,
quantInfo->sfb_offset[k],
quantInfo->sfb_offset[k+1]-quantInfo->sfb_offset[k],
1);
// printf("%d\t%d\n",k,quantInfo->book_vector[k]);
}
/* write the reconstructed spectrum to the output for use with prediction */
{
int i;
for (sb=0; sb<quantInfo -> 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[sb+1]; i++){
p_reconstructed_spectrum[0][i]=673;
}
} else {
for (i=sfb_offset[sb]; i<sfb_offset[sb+1]; i++){
p_reconstructed_spectrum[0][i] = sgn(p_spectrum[0][i]) * requant[i];
}
}
}
}
return FNO_ERROR;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -