📄 l3loop.c
字号:
xr[i] *= pow(ifqstep, (double) pretab[sfb]);
}
}
}
}
int amp_scalefac_bands(double xr[576],
double xfsf[4][CBLIMIT],
L3_psy_xmin_t *l3_xmin,
L3_side_info_t *l3_side,
L3_scalefac_t *scalefac,
int gr, int ch, int iteration)
/*************************************************************************/
/* Amplify the scalefactor bands that violate the masking threshold. */
/* See ISO 11172-3 Section C.1.5.4.3.5 */
/*************************************************************************/
{
int start, end, l, sfb, i, scfsi_band, over = 0;
double ifqstep, ifqstep2;
double (*xr_s)[192][3];
gr_info *cod_info, *gr0;
int copySF, preventSF;
cod_info = &l3_side->gr[gr].ch[ch].tt;
gr0 = &l3_side->gr[0].ch[ch].tt;
xr_s = (double (*)[192][3]) xr;
copySF = 0;
preventSF = 0;
if (cod_info->scalefac_scale == 0)
ifqstep = SQRT2;
else
ifqstep = pow(2.0, 0.5 * (1.0 + (double) cod_info->scalefac_scale));
if (gr == 1)
{
/*
If the second granule is being coded and scfsi is active in at
least one scfsi_band...
*/
for (scfsi_band = 0; scfsi_band < 4; scfsi_band++)
if (l3_side->scfsi[ch][scfsi_band])
{
/*
a) ifqstep has to be set similar to the
first granule...
*/
if (gr0->scalefac_scale == 0)
ifqstep = SQRT2;
else
ifqstep = pow(2.0, 0.5 * (1.0 + (double) gr0->scalefac_scale));
if (iteration == 1)
{
/*
b) If it is the first iteration, the scalefactors
of scalefactor bands in which scfsi is enabled
must be taken from the first granule
*/
copySF = 1;
}
else
{
/*
c) If it is not the first iteration, the amplification
must be prevented for scalefactor bands in which
scfsi is enabled
*/
preventSF = 1;
}
break;
}
}
ifqstep2 = ifqstep * ifqstep;
scfsi_band = 0;
for (sfb = 0; sfb < cod_info->sfb_lmax; sfb++)
{
if (copySF || preventSF)
{
if (sfb == scfsi_band_long[scfsi_band + 1])
scfsi_band += 1;
if (l3_side->scfsi[ch][scfsi_band])
{
if (copySF)
scalefac->l[gr][ch][sfb] = scalefac->l[0][ch][sfb];
continue;
}
}
if (xfsf[0][sfb] > l3_xmin->l[gr][ch][sfb])
{
over++;
l3_xmin->l[gr][ch][sfb] *= ifqstep2;
scalefac->l[gr][ch][sfb]++;
start = scalefac_band_long[sfb];
end = scalefac_band_long[sfb+1];
for (l = start; l < end; l++)
xr[l] *= ifqstep;
}
}
/* Note that scfsi is not enabled for frames containing short blocks */
for (i = 0; i < 3; i++)
for (sfb = cod_info->sfb_smax; sfb < 12; sfb++)
if (xfsf[i+1][sfb] > l3_xmin->s[gr][ch][sfb][i])
{
over++;
l3_xmin->s[gr][ch][sfb][i] *= ifqstep2;
scalefac->s[gr][ch][sfb][i]++;
start = scalefac_band_short[sfb];
end = scalefac_band_short[sfb+1];
for (l = start; l < end; l++)
(*xr_s)[l][i] *= ifqstep;
}
return over;
}
void quantize(double xr[576], int ix[576], gr_info *cod_info)
/*************************************************************************/
/* Function: Quantization of the vector xr (-> ix) */
/*************************************************************************/
{
register int i,b;
int idx,l_end, s_start;
double step,quantizerStepSize;
float dbl;
long ln;
double (*xr_s)[192][3];
int (*ix_s)[192][3];
xr_s = (double (*)[192][3]) xr;
ix_s = (int (*)[192][3]) ix;
quantizerStepSize = (double) cod_info->quantizerStepSize;
if (cod_info->quantizerStepSize==0.0) step = 1.0;
else step = pow(2.0,quantizerStepSize*0.25);
if (cod_info->window_switching_flag != 0 && cod_info->block_type == 2)
if (cod_info->mixed_block_flag == 0)
{
s_start = 0;
l_end = 0;
}
else
{
s_start = 6 * 2;
l_end = 18 * 2;
}
else
{
s_start = 192;
l_end = 576;
}
for(i=0;i<l_end;i++)
{
dbl = fabs(xr[i])/step;
ln = (long)dbl;
if (ln<10000)
{
idx=int2idx[ln];
if (dbl<idx2dbl[idx+1]) ix[i] = idx;
else ix[i] = idx+1;
}
else
{
dbl = sqrt(sqrt(dbl)*dbl);
if (dbl<0.0946) ix[i] = (int)(dbl - 0.5946);
else ix[i] = (int)(dbl + 0.4154);
}
}
for(;i<576;i++) ix[i] = 0;
if (s_start < 192)
for (b = 0; b < 3; b++)
{
step = pow(2.0, (quantizerStepSize + 8.0 * (double) cod_info->subblock_gain[b]) * 0.25);
for (i=s_start;i<192;i++)
{
dbl = fabs((*xr_s)[i][b])/step;
ln = (long)dbl;
if (ln<10000)
{
idx=int2idx[ln];
if (dbl<idx2dbl[idx+1]) (*ix_s)[i][b] = idx;
else (*ix_s)[i][b] = idx+1;
}
else
{
dbl = sqrt(sqrt(dbl)*dbl);
if (dbl<0.0946) (*ix_s)[i][b] = (int)(dbl - 0.5946);
else (*ix_s)[i][b] = (int)(dbl + 0.4154);
}
}
}
}
int ix_max(int ix[576], unsigned int begin, unsigned int end)
/*************************************************************************/
/* Function: Calculate the maximum of ix from 0 to 575 */
/*************************************************************************/
{
register int i;
register int x;
register int max = 0;
for(i=begin;i<end;i++)
{
x = abs(ix[i]);
if (x>max) max=x;
}
return max;
}
/* THIS FUNCTION HAS BEEN CHANGED ..... */
double xr_max(double xr[576], unsigned int begin, unsigned int end)
/*************************************************************************/
/* Function: Calculate the maximum of xr[576] from 0 to 575 */
/*************************************************************************/
{
register int i;
double max = 0.0, temp;
for(i=begin;i<end;i++)
{
temp = fabs(xr[i]);
if (temp>max) max=temp;
}
return max;
}
/*************************************************************************/
/* Noiseless coding -- Huffman coding */
/*************************************************************************/
void calc_runlen(int ix[576], gr_info *cod_info)
/*************************************************************************/
/* Function: Calculation of rzero, count1, big_values */
/* (Partitions ix into big values, quadruples and zeros). */
/*************************************************************************/
{
int i;
int rzero = 0;
if (cod_info->window_switching_flag && (cod_info->block_type == 2))
{ /* short blocks */
cod_info->count1 = 0;
cod_info->big_values = 288;
}
else
{
for (i = 576; i > 1; i -= 2)
if (ix[i-1] == 0 && ix[i-2] == 0)
rzero++;
else
break;
cod_info->count1 = 0 ;
for (; i > 3; i -= 4)
if (abs(ix[i-1]) <= 1
&& abs(ix[i-2]) <= 1
&& abs(ix[i-3]) <= 1
&& abs(ix[i-4]) <= 1)
cod_info->count1++;
else
break;
cod_info->big_values = i/2;
}
if ((2*rzero+4*cod_info->count1+2*cod_info->big_values)!=576) ERROR("bladibla!=576");
}
/* THIS FUNCTION HAS BEEN CHANGED ..... */
int count1_bitcount(int ix[576], gr_info *cod_info)
/*************************************************************************/
/* Determines the number of bits to encode the quadruples. */
/*************************************************************************/
{
int p, i, k;
int v, w, x, y, signbits;
int sum0 = 0,
sum1 = 0;
for(i=cod_info->big_values*2, k=0; k<cod_info->count1; i+=4, k++)
{
v = abs(ix[i]);
w = abs(ix[i+1]);
x = abs(ix[i+2]);
y = abs(ix[i+3]);
p = v + (w<<1) + (x<<2) + (y<<3);
signbits = 0;
if (v!=0) signbits++;
if (w!=0) signbits++;
if (x!=0) signbits++;
if (y!=0) signbits++;
sum0 += signbits;
sum1 += signbits;
sum0 += ht[32].hlen[p];
sum1 += ht[33].hlen[p];
}
if (sum0<sum1)
{
cod_info->count1table_select = 0;
return sum0;
}
else
{
cod_info->count1table_select = 1;
return sum1;
}
}
void subdivide(gr_info *cod_info)
/*************************************************************************/
/* presumable subdivides the bigvalue region which will use separate Huffman tables. */
/*************************************************************************/
{
int scfb_anz = 0;
int bigvalues_region;
if (cod_info->big_values == 0)
{ /* no big_values region */
cod_info->region0_count = 0;
cod_info->region1_count = 0;
}
else
{
bigvalues_region = 2 * cod_info->big_values;
if ((cod_info->window_switching_flag == 0))
{ /* long blocks */
int thiscount, index;
/* Calculate scfb_anz */
while (scalefac_band_long[scfb_anz] < bigvalues_region)
scfb_anz++;
if (scfb_anz>=23) ERROR("scbf >= 23 !!");
cod_info->region0_count = subdv_table[scfb_anz].region0_count;
thiscount = cod_info->region0_count;
index = thiscount + 1;
while (thiscount && (scalefac_band_long[index] > bigvalues_region))
{
thiscount -= 1;
index -= 1;
}
cod_info->region0_count = thiscount;
cod_info->region1_count = subdv_table[scfb_anz].region1_count;
index = cod_info->region0_count + cod_info->region1_count + 2;
thiscount = cod_info->region1_count;
while (thiscount && (scalefac_band_long[index] > bigvalues_region))
{
thiscount -= 1;
index -= 1;
}
cod_info->region1_count = thiscount;
cod_info->address1 = scalefac_band_long[cod_info->region0_count+1];
cod_info->address2 = scalefac_band_long[cod_info->region0_count
+ cod_info->region1_count + 2 ];
cod_info->address3 = bigvalues_region;
}
else
{
if ((cod_info->block_type == 2) && (cod_info->mixed_block_flag == 0))
{
cod_info->region0_count = 8;
cod_info->region1_count = 36;
cod_info->address1 = 36;
cod_info->address2 = bigvalues_region;
cod_info->address3 = 0;
}
else
{
cod_info->region0_count = 7;
cod_info->region1_count = 13;
cod_info->address1 = scalefac_band_long[cod_info->region0_count+1];
cod_info->address2 = bigvalues_region;
cod_info->address3 = 0;
}
}
}
}
void bigv_tab_select(int ix[576], gr_info *cod_info)
/*************************************************************************/
/* Function: Select huffman code tables for bigvalues regions */
/*************************************************************************/
{
cod_info->table_select[0] = 0;
cod_info->table_select[1] = 0;
cod_info->table_select[2] = 0;
if (cod_info->window_switching_flag && (cod_info->block_type==2))
{
/*
Within each scalefactor band, data is given for successive
time windows, beginning with window 0 and ending with window 2.
Within each window, the quantized values are then arranged in
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -