📄 mp3enc_quantization_fp.c
字号:
/*//////////////////////////////////////////////////////////////////////////////
//
// INTEL CORPORATION PROPRIETARY INFORMATION
// This software is supplied under the terms of a license agreement or
// nondisclosure agreement with Intel Corporation and may not be copied
// or disclosed except in accordance with the terms of that agreement.
// Copyright(c) 2002-2007 Intel Corporation. All Rights Reserved.
//
*/
#include "mp3enc_own_fp.h"
#define LEN_MDCT_LINE 576
#define SF_OFFSET 210
#define MAX_QUANT 8206
#define MAGIC_NUMBER (-0.0946 + 0.5)
#define QUANT_ITER_NUM 8
void mp3enc_quantCalcAvailableBits(MP3Enc *state) {
Ipp32s ch, gr, grnum;
Ipp32s len;
Ipp32s bitsPerFrame;
Ipp32s mean_bits;
Ipp32s bits_in_buf, max_bits_in_buf;
Ipp32s bits_from_buffer, com_additional_bits;
Ipp32s additional_bits[2][2];
// Ipp32s up, l;
IppMP3FrameHeader *header = &state->com.header;
Ipp32s stereo = state->com.stereo;
Ipp32s ms = state->com.stereo_mode == MPA_MS_STEREO ? 2 : 0;
Ipp32s max_br;
grnum = state->com.grnum;
len = 32; // header
if (state->com.header.id) {
if (stereo == 1)
len += 136;
else
len += 256;
max_bits_in_buf = 8*511;
max_br = 320;
} else {
if (stereo == 1)
len += 72;
else
len += 136;
max_bits_in_buf = 8*255;
max_br = 160;
}
if (header->protectionBit)
len += 16;
state->com.frameBits = len;
bitsPerFrame = state->com.slot_size << 3;
mean_bits = (bitsPerFrame - len);
bits_in_buf = state->com.si_main_data_begin * 8;
if (mp3_bitrate[header->id][header->layer-1][header->bitRate] != max_br) {
if (state->com.br_mode == MPAENC_ABR) {
Ipp32s br_ind;
bits_from_buffer = state->com.bytes_in_gbuf << 3;
if (bits_from_buffer < state->com.slot_size * MAX_GBUF_COEF)
mean_bits = (Ipp32s)(mean_bits * 0.85);
br_ind = mp3encLEBitrate(&state->com, (bitsPerFrame + bits_from_buffer) >> 3);
bits_from_buffer =
(state->com.slot_sizes[br_ind] - state->com.slot_size +
state->com.si_main_data_begin) << 3;
} else {
if (bits_in_buf > 0.95 * max_bits_in_buf) {
bits_from_buffer = (Ipp32s)(0.95 * max_bits_in_buf);
mean_bits += (Ipp32s)(bits_in_buf - 0.95 * max_bits_in_buf);
} else {
bits_from_buffer = bits_in_buf;
}
mean_bits = (Ipp32s)(mean_bits * 0.85);
}
mean_bits >>= (stereo + grnum - 2);
com_additional_bits = 0;
for (gr = 0; gr < grnum; gr++) {
for (ch = 0; ch < stereo; ch++) {
additional_bits[gr][ch] =
(Ipp32s)((state->pa_curr_frame_PE_st[gr][ch+ms] - 1000) * 0.6);
if (additional_bits[gr][ch] < 0) {
additional_bits[gr][ch] = 0;
}
if (state->com.si_blockType[gr][ch] != NORM_TYPE) {
if (additional_bits[gr][ch] < 0.5 * mean_bits)
additional_bits[gr][ch] = (Ipp32s)(0.5 * mean_bits);
else if (additional_bits[gr][ch] > 1.2 * mean_bits)
additional_bits[gr][ch] = (Ipp32s)(1.2 * mean_bits);
}
else {
Ipp32s max_add_bits;
if (state->com.br_mode == MPAENC_ABR)
max_add_bits = (Ipp32s)(0.5 * mean_bits);
else
max_add_bits = (Ipp32s)(0.2 * mean_bits);
if (additional_bits[gr][ch] > max_add_bits)
additional_bits[gr][ch] = max_add_bits;
}
com_additional_bits += additional_bits[gr][ch];
}
}
for (gr = 0; gr < grnum; gr++) {
for (ch = 0; ch < stereo; ch++) {
if (com_additional_bits > bits_from_buffer) {
additional_bits[gr][ch] = (Ipp32s)(additional_bits[gr][ch] *
((Ipp32f)bits_from_buffer / (Ipp32f)com_additional_bits));
}
state->com.max_bits[gr][ch] =
mean_bits + additional_bits[gr][ch];
}
}
for (gr = 0; gr < grnum; gr++) {
if (state->com.stereo_mode == MPA_MS_STEREO) {
Ipp32s add_bits = (Ipp32s)(state->com.max_bits[gr][1] * (state->ms_coef[gr] - 0.5) * 1.5);
if (add_bits < 0) add_bits = 0;
state->com.max_bits[gr][0] += add_bits;
state->com.max_bits[gr][1] -= add_bits;
}
}
} else {
mean_bits += bits_in_buf;
mean_bits >>= (stereo + grnum - 2);
for (gr = 0; gr < grnum; gr++)
for (ch = 0; ch < stereo; ch++)
state->com.max_bits[gr][ch] = mean_bits;
}
for (gr = 0; gr < grnum; gr++)
for (ch = 0; ch < stereo; ch++)
if (state->com.max_bits[gr][ch] > 4095)
state->com.max_bits[gr][ch] = 4095;
}
Ipp32s mp3enc_AdjustSF(MP3Enc *state, Ipp32f *xmin_,
Ipp32f *distortion,
Ipp32s *sfb_last, Ipp32s gr, Ipp32s ch)
{
Ipp32s i, sfb, width, pos, wnd;
Ipp32u *sfb_long, *sfb_short;
Ipp16s sf_tmp_16s;
Ipp32s sf_tmp, scl;
Ipp32f noise, xmin;
Ipp32f tmp22;
Ipp32f ifqstep;
Ipp32f dist_max, dist;
Ipp32s ret_flag = 0;
Ipp32s sf_glob;
Ipp32f *mdct_line_abs = state->mdct_line_abs;
Ipp32f *mdct_scaled = state->mdct_scaled;
Ipp32f *mdct_rqnt = state->mdct_rqnt;
Ipp16u scalefac_l_save[32];
Ipp16u scalefac_s_save[12][3];
sfb_long = mp3enc_sfBandIndex[state->com.header.id][state->com.header.samplingFreq].l;
sfb_short = mp3enc_sfBandIndex[state->com.header.id][state->com.header.samplingFreq].s;
if (state->com.si_sfScale[gr][ch] == 0) {
ifqstep = 1.2968395f;
} else {
ifqstep = 1.6817928f;
}
scl = -2 - 2 * (Ipp16s)state->com.si_sfScale[gr][ch];
sf_glob = state->com.si_globGain[gr][ch] - SF_OFFSET;
if (state->com.si_blockType[gr][ch] != 2) {
for (sfb = 0, pos = 0; sfb < state->com.sfb_l_max; sfb++, pos+=width) {
scalefac_l_save[sfb] = state->com.scalefac_l[gr][ch][sfb];
width = sfb_long[sfb+1] - sfb_long[sfb];
noise = 0;
if (state->com.si_preFlag[gr][ch] == 0)
sf_tmp = scl * state->com.scalefac_l[gr][ch][sfb];
else
sf_tmp = scl * (state->com.scalefac_l[gr][ch][sfb]/* + pretab[sfb]*/);
sf_tmp += sf_glob;
if (sf_tmp == sfb_last[sfb])
continue;
sfb_last[sfb] = sf_tmp;
ippsPow43_16s32f(state->com.quant_ix[gr][ch] + pos, mdct_rqnt + pos, width);
sf_tmp_16s = (Ipp16s)sf_tmp;
ippsCalcSF_16s32f(&sf_tmp_16s, 0, &tmp22, 1);
ippsMulC_32f_I(tmp22, mdct_rqnt + pos, width);
noise = 0.0f;
for (i = 0; i < width; i++) {
Ipp32f temp;
temp = mdct_line_abs[pos+i] - mdct_rqnt[pos+i];
noise += temp * temp;
}
xmin = xmin_[sfb];
dist = 0;
if (xmin > 0) dist = noise / xmin;
distortion[sfb] = dist;
}
ippsMax_32f(distortion, state->com.sfb_l_max, &dist_max);
ippsSqrt_32f_I(&dist_max, 1);
if (dist_max >= 1) {
pos = 0;
for (sfb = 0; sfb < state->com.sfb_l_max; sfb++) {
width = sfb_long[sfb+1] - sfb_long[sfb];
if (distortion[sfb] > dist_max)
{
Ipp32s s = state->com.scalefac_l[gr][ch][sfb];
if (s < 3) {
state->com.scalefac_l[gr][ch][sfb] = (Ipp16u)(s + 1);
ippsMulC_32f_I(ifqstep, mdct_scaled + pos, width);
ret_flag = 1;
}
}
pos += width;
}
}
} else {
for (sfb = 0, pos = 0; sfb < state->com.sfb_s_max; sfb++) {
width = sfb_short[sfb+1] - sfb_short[sfb];
for (wnd = 0; wnd < 3; wnd++, pos += width) {
scalefac_s_save[sfb][wnd] = state->com.scalefac_s[gr][ch][sfb][wnd];
noise = 0;
if (state->com.si_preFlag[gr][ch] == 0)
sf_tmp = scl * state->com.scalefac_s[gr][ch][sfb][wnd];
else
sf_tmp = scl * (state->com.scalefac_s[gr][ch][sfb][wnd]/* + pretab[sfb]*/);
sf_tmp += sf_glob;
if (sf_tmp == sfb_last[sfb*3+wnd])
continue;
sfb_last[sfb*3+wnd] = sf_tmp;
ippsPow43_16s32f(state->com.quant_ix[gr][ch] + pos, mdct_rqnt + pos, width);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -