📄 mp3enc_quantization_int.c
字号:
state->com.scalefac_s[gr][ch][sfb][wnd] = (Ipp16u)(s + 1);
ippsMinMax_16s(mdct_scaled_short + pos, width, &sca_min16, &sca_max16);
sca_min = -((Ipp32s)sca_min16);
sca_max = sca_max16;
if (sca_min > sca_max) {
sca_max = sca_min - 1;
}
if (sca_max >= scaled_max) {
*pow34_scaleFact = *pow34_scaleFact + 1;
ippsRShiftC_16s_I(1, mdct_scaled_short, 576);
}
ippsMulC_16s_ISfs(ifqstep, mdct_scaled_short + pos, width, 14);
ret_flag = 1;
}
}
pos += width;
}
}
}
}
mp3enc_quantcalcPart2Len(&state->com, gr, ch);
if (state->com.si_part2Len[gr][ch] + state->com.si_part23Len[gr][ch] > (Ipp32u)state->com.max_bits[gr][ch]) {
if (state->com.si_blockType[gr][ch] != 2) {
for (sfb = 0; sfb < state->com.sfb_l_max; sfb++) {
state->com.scalefac_l[gr][ch][sfb] = scalefac_l_save[sfb];
}
} else {
for (sfb = 0; sfb < state->com.sfb_s_max; sfb++) {
for (wnd = 0; wnd < 3; wnd++) {
state->com.scalefac_s[gr][ch][sfb][wnd] = scalefac_s_save[sfb][wnd];
}
}
}
mp3enc_quantcalcPart2Len(&state->com, gr, ch);
return 0;
}
return ret_flag;
}
Ipp32s mp3ienc_quantization(MP3Enc *state)
{
Ipp32s gr, ch, pos, wnd, sfb;
Ipp32u *sfb_long, *sfb_short;
IppMP3FrameHeader *header = &state->com.header;
Ipp32s last_frame_common_scalefactor;
Ipp32s xmin[36], distortion[36], mdct_pwr;
Ipp32s sfb_last[36];
Ipp32s iter;
Ipp32s *mdct_line_abs_int = state->mdct_line_abs_int;
Ipp16s *mdct_line_abs_short = state->mdct_line_abs_short;
Ipp16s *mdct_scaled_short = state->mdct_scaled_short;
sfb_long = mp3enc_sfBandIndex[header->id][header->samplingFreq].l;
sfb_short = mp3enc_sfBandIndex[header->id][header->samplingFreq].s;
mp3ienc_quantCalcAvailableBits(state);
for (gr = 0; gr < 2; gr++) {
for (ch = 0; ch < state->com.stereo; ch++) {
Ipp32s i;
Ipp32s max_mdct_line_int, tmp;
Ipp16s max_mdct_line_short;
Ipp32s mdct_scaleFactor, mdct_scaleFactor1;
Ipp32s pow34_scaleFact;
mp3enc_quantIterReset(&state->com, gr, ch);
mdct_scaleFactor = 0;
max_mdct_line_int = 0;
max_mdct_line_short = 0;
ippsAbs_32s(state->mdct_out_int[gr][ch], mdct_line_abs_int, LEN_MDCT_LINE);
ippsMax_32s(mdct_line_abs_int, LEN_MDCT_LINE, &max_mdct_line_int);
while(max_mdct_line_int >= 32768) {
max_mdct_line_int >>= 1;
mdct_scaleFactor ++;
}
mdct_scaleFactor1 = mdct_scaleFactor;
tmp = max_mdct_line_int;
if (tmp)
while(tmp <= 16383) {
tmp <<= 1;
mdct_scaleFactor1 --;
}
for (i = 0; i < LEN_MDCT_LINE; i++) {
mdct_line_abs_short[i] = (Ipp16s)(mdct_line_abs_int[i] >> mdct_scaleFactor);
}
max_mdct_line_short = (Ipp16s)max_mdct_line_int;
pow34_scaleFact = 3 * (mdct_scaleFactor >> 2) - 1;
if (max_mdct_line_short != 0) {
Ipp16s tmp_mdct_scaled;
Ipp32s i_tmp_mdct_scaled;
ippsPow34_16s_Sfs(&max_mdct_line_short, mdct_scaleFactor,
&tmp_mdct_scaled, pow34_scaleFact, 1);
i_tmp_mdct_scaled = tmp_mdct_scaled;
while (i_tmp_mdct_scaled <= 16383) {
i_tmp_mdct_scaled = i_tmp_mdct_scaled << 1;
pow34_scaleFact--;
}
}
ippsPow34_16s_Sfs(mdct_line_abs_short, mdct_scaleFactor,
mdct_scaled_short, pow34_scaleFact, LEN_MDCT_LINE);
if (state->com.si_blockType[gr][ch] != 2) {
pos = 0;
for (sfb = 0; sfb < state->com.sfb_l_max; sfb++) {
Ipp32s width = sfb_long[sfb+1] - sfb_long[sfb];
ippsDotProd_32s_Sfs(&mdct_line_abs_int[pos], &mdct_line_abs_int[pos],
width, &mdct_pwr, 2 * mdct_scaleFactor1 + 8);
ippsMul_32s_Sfs(&mdct_pwr, &state->pa_ratio_l[gr][ch][sfb], &xmin[sfb], 1, 23);
sfb_last[sfb] = 10000;
pos += width;
}
} else {
pos = 0;
for (sfb = 0; sfb < state->com.sfb_s_max; sfb++) {
Ipp32s width = sfb_short[sfb+1] - sfb_short[sfb];
for (wnd = 0; wnd < 3; wnd++) {
ippsDotProd_32s_Sfs(&mdct_line_abs_int[pos], &mdct_line_abs_int[pos],
width, &mdct_pwr, 2 * mdct_scaleFactor1 + 8);
ippsMul_32s_Sfs(&mdct_pwr, &state->pa_ratio_s[gr][ch][wnd][sfb], &xmin[sfb*3+wnd], 1, 23);
sfb_last[sfb*3+wnd] = 10000;
pos += width;
}
}
}
iter = 0;
last_frame_common_scalefactor = state->last_frame_common_scalefactor[ch];
do {
ippsMax_16s(mdct_scaled_short, LEN_MDCT_LINE, &max_mdct_line_short);
state->start_common_scalefac = -SF_OFFSET;
state->finish_common_scalefac = -SF_OFFSET;
if (max_mdct_line_short > 0) {
Ipp32s quot = pow34_scaleFact / 3;
Ipp32s rem = pow34_scaleFact - quot * 3;
Ipp32s shift, max_quant, j, tmp_quot, tmp;
Ipp32s delta, add_shift;
Ipp16s tmp_buf[16];
if (rem > 0) {
rem -= 3;
quot += 1;
}
tmp_quot = quot;
ippsMulC_16s_Sfs(scalefac_pow, max_mdct_line_short, tmp_buf, 16, 15);
shift = 0;
max_quant = (MAX_QUANT << (-rem));
if (max_quant < (Ipp32s)tmp_buf[0]) {
max_quant <<= 3;
tmp_quot++;
}
for (j = 15; j >= 0; j--) {
tmp = (Ipp32s)tmp_buf[j];
if ((tmp << scalefac_pow_shift[j]) <= max_quant) {
shift = j;
break;
}
}
state->start_common_scalefac = tmp_quot * 16 - shift - 104;
shift = 16;
delta = (65536 - MAGIC_NUMBER_I) << (2-rem);
tmp = ((Ipp32s)tmp_buf[15] << scalefac_pow_shift[15]);
add_shift = 0;
while (delta > tmp) {
tmp <<= 3;
add_shift+=3;
quot--;
}
for (j = 15; j >= 0; j--) {
tmp = (Ipp32s)tmp_buf[j];
if ((tmp << (scalefac_pow_shift[j] + add_shift)) < delta) {
shift = j;
break;
}
}
if (shift == 16) {
shift = 15;
quot++;
}
state->finish_common_scalefac = (quot + 6) * 16 - shift + 1 - 104;
if (state->start_common_scalefac > 255 - SF_OFFSET) {
state->start_common_scalefac = 255 - SF_OFFSET;
} else if (state->start_common_scalefac < -SF_OFFSET) {
state->start_common_scalefac = -SF_OFFSET;
}
if (state->finish_common_scalefac > 255 - SF_OFFSET) {
state->finish_common_scalefac = 255 - SF_OFFSET;
} else if (state->finish_common_scalefac < -SF_OFFSET) {
state->finish_common_scalefac = -SF_OFFSET;
}
}
mp3ienc_quantMainLoop(state,
gr, ch, mdct_scaled_short, pow34_scaleFact);
state->common_scalefactor_update[ch] = 2;
if (state->com.ns_mode == 0)
break;
} while(iter++ < QUANT_ITER_NUM &&
mp3ienc_AdjustSF(state, xmin, distortion, sfb_last, gr, ch, mdct_scaleFactor1, &pow34_scaleFact));
state->common_scalefactor_update[ch] = state->com.si_globGain[gr][ch] - SF_OFFSET -
last_frame_common_scalefactor;
if (state->common_scalefactor_update[ch] >= 0) {
if (state->common_scalefactor_update[ch] <= 2)
state->common_scalefactor_update[ch] = 2;
} else {
if (state->common_scalefactor_update[ch] >= -2)
state->common_scalefactor_update[ch] = -2;
}
state->com.frameBits += state->com.si_part2Len[gr][ch];
state->com.frameBits += state->com.si_part23Len[gr][ch];
}
}
if (state->com.br_mode == MPAENC_ABR) {
state->com.header.bitRate =
mp3encGEBitrate(&state->com, ((state->com.frameBits + 7) >> 3) - state->com.si_main_data_begin);
}
return 1;
}
void mp3ienc_quantMainLoop(MP3Enc *state,
Ipp32s gr, Ipp32s ch,
Ipp16s *mdct_scaled_short,
Ipp32s pow34_scaleFact)
{
Ipp16s tmp_x_quant_short[LEN_MDCT_LINE];
Ipp32s index, scale, shift;
Ipp32s magic_number;
Ipp32s common_scalefactor;
Ipp32s needed_bits;
common_scalefactor = state->last_frame_common_scalefactor[ch];
// state->common_scalefactor_update[ch] = state->common_scalefactor_update[ch];
if (common_scalefactor < state->start_common_scalefac)
common_scalefactor = state->start_common_scalefac;
if (common_scalefactor > state->finish_common_scalefac)
common_scalefactor = state->finish_common_scalefac;
for(;;) {
index = (-(common_scalefactor + 104)) & 0xF;
scale = pow34_scaleFact +
(3 * ((-(common_scalefactor + 104)) >> 4) + scalefac_pow_shift[index]);
magic_number = (Ipp32s)MAGIC_NUMBER_I - 32768;
shift = scale + 16;
if (scale >= 0) magic_number = 0;
else if (scale > -16) {
magic_number = ((magic_number + (1 << (shift - 1))) >> shift);
shift = 0;
} else if (scale < -16) {
shift = -shift;
scale = -16;
}
ippsMulC_16s_Sfs(mdct_scaled_short, scalefac_pow[index], tmp_x_quant_short,
LEN_MDCT_LINE, 15 + shift);
ippsAddC_16s_Sfs(tmp_x_quant_short, (Ipp16s)magic_number, state->com.quant_ix[gr][ch],
LEN_MDCT_LINE, -scale);
needed_bits = mp3enc_quantCalcBits(&state->com, gr, ch);
if (needed_bits == state->com.max_bits[gr][ch])
break;
if (needed_bits > state->com.max_bits[gr][ch]) {
if (common_scalefactor == state->finish_common_scalefac)
break;
if (state->common_scalefactor_update[ch] < 0) {
state->common_scalefactor_update[ch] = -state->common_scalefactor_update[ch];
}
state->common_scalefactor_update[ch] = (state->common_scalefactor_update[ch] + 1) >> 1;
} else {
if (common_scalefactor == state->start_common_scalefac)
break;
if (state->common_scalefactor_update[ch] == 1)
break;
if (state->common_scalefactor_update[ch] > 0) {
state->common_scalefactor_update[ch] = -state->common_scalefactor_update[ch];
}
state->common_scalefactor_update[ch] >>= 1;
}
common_scalefactor += state->common_scalefactor_update[ch];
if (common_scalefactor < state->start_common_scalefac)
common_scalefactor = state->start_common_scalefac;
if (common_scalefactor > state->finish_common_scalefac)
common_scalefactor = state->finish_common_scalefac;
}
state->common_scalefactor_update[ch] =
common_scalefactor - state->last_frame_common_scalefactor[ch];
state->last_frame_common_scalefactor[ch] = common_scalefactor;
if (state->common_scalefactor_update[ch] >= 0) {
if (state->common_scalefactor_update[ch] <= 2)
state->common_scalefactor_update[ch] = 2;
} else {
if (state->common_scalefactor_update[ch] >= -2)
state->common_scalefactor_update[ch] = -2;
}
state->com.si_globGain[gr][ch] = (Ipp16s)(common_scalefactor + SF_OFFSET);
state->com.si_part23Len[gr][ch] = needed_bits;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -