📄 mp3enc_quantization_int.cpp
字号:
/*////////////////////////////////////////////////////////////////////////////////// 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-2005 Intel Corporation. All Rights Reserved.//*/#include "umc_mp3_enc_int.h"#define LEN_MDCT_LINE 576#define SF_OFFSET 210#define MAX_QUANT 8206#define MAGIC_NUMBER (-0.0946 + 0.5)#define MAGIC_NUMBER_I 26568#define C_0_95_Q15 31129#define C_0_85_Q15 27852#define C_1_2_Q14 19660#define C_0_2_Q15 6553#define C_0_6_Q15 19660namespace UMC { void MP3EncoderInt::calcAvailableBits() { int ch, gr; int len; int bitsPerFrame; int mean_bits; int bits_in_buf, max_bits_in_buf; int bits_from_buffer, com_additional_bits; int additional_bits[2][2]; int up, l; int div; up = frameSize * mp3enc_bitrate[header.bitRate] * 1000; slot_size = up / (mp3enc_frequency[header.samplingFreq] << 3); l = slot_size * mp3enc_frequency[header.samplingFreq] << 3; if (up - l < mp3enc_frequency[header.samplingFreq] << 2) header.paddingBit = 0; else header.paddingBit = 1; slot_size += header.paddingBit; len = 32; // header if (stereo == 1) len += 136; else len += 256; if (header.protectionBit) len += 16; bitsPerFrame = slot_size << 3; max_bits_in_buf = 8*511; mean_bits = (bitsPerFrame - len); bits_in_buf = si_main_data_begin * 8; if (mp3enc_bitrate[header.bitRate] != 320) { if (bits_in_buf > (C_0_95_Q15 * max_bits_in_buf >> 15)) { bits_from_buffer = (C_0_95_Q15 * max_bits_in_buf >> 15); mean_bits += bits_in_buf - (C_0_95_Q15 * max_bits_in_buf >> 15); } else { bits_from_buffer = bits_in_buf; } mean_bits = C_0_85_Q15 * mean_bits >> 15; mean_bits >>= stereo; com_additional_bits = 0; for (gr = 0; gr < 2; gr++) { for (ch = 0; ch < 2; ch++) { int pe = ipa_curr_frame_PE_st[gr][ch] - 1000; if (pe > 32767) pe = 32767; additional_bits[gr][ch] = pe * C_0_6_Q15 >> 15; if (additional_bits[gr][ch] < 0) { additional_bits[gr][ch] = 0; } if (si_blockType[gr][ch] != NORM_TYPE) { if (additional_bits[gr][ch] * 2 < mean_bits) additional_bits[gr][ch] = mean_bits >> 1; else if (additional_bits[gr][ch] > (C_1_2_Q14 * mean_bits >> 14)) additional_bits[gr][ch] = (C_1_2_Q14 * mean_bits >> 14); } else if (additional_bits[gr][ch] > (C_0_2_Q15 * mean_bits >> 15)) additional_bits[gr][ch] = (C_0_2_Q15 * mean_bits >> 15); com_additional_bits += additional_bits[gr][ch]; } } if (com_additional_bits > bits_from_buffer && com_additional_bits) div = bits_from_buffer * 32768 / com_additional_bits; else div = 0; for (gr = 0; gr < 2; gr++) { for (ch = 0; ch < 2; ch++) { if (com_additional_bits > bits_from_buffer) { additional_bits[gr][ch] = additional_bits[gr][ch] * div >> 15; } max_bits[gr][ch] = mean_bits + additional_bits[gr][ch]; } } } else { mean_bits += bits_in_buf; mean_bits >>= stereo; for (gr = 0; gr < 2; gr++) for (ch = 0; ch < 2; ch++) max_bits[gr][ch] = mean_bits; } }static Ipp16s scalefac_pow[] = { 32767, 18658, 21247, 24196, 27554, 31379, 17867, 20347, 23170, 26386, 30048, 17109, 19484, 22188, 25268, 28774};static Ipp16s scalefac_pow_shift[] = { 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3}; int MP3EncoderInt::quantization() { int gr, ch; unsigned int *sfb_long, *sfb_short; VM_ALIGN16_DECL(short) ipptmp0_576[576]; Ipp16s mdct_line_abs_short[LEN_MDCT_LINE]; Ipp16s mdct_scaled_short[LEN_MDCT_LINE]; Ipp32s mdct_line_abs_int[LEN_MDCT_LINE]; sfb_long = mp3enc_sfBandIndex[header.samplingFreq].l; sfb_short = mp3enc_sfBandIndex[header.samplingFreq].s; calcAvailableBits(); ippsZero_16s(ipptmp0_576, 576); for (gr = 0; gr < 2; gr++) { for (ch = 0; ch < stereo; ch++) { int i; int max_mdct_line_int; short max_mdct_line_short; int mdct_scaleFactor = 0; int pow34_scaleFact; start_common_scalefac = -SF_OFFSET; finish_common_scalefac = -SF_OFFSET; max_mdct_line_int = 0; max_mdct_line_short = 0; ippsAbs_32s(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 ++; } for (i = 0; i < LEN_MDCT_LINE; i++) { mdct_line_abs_short[i] = (short)(mdct_line_abs_int[i] >> mdct_scaleFactor); } max_mdct_line_short = (short)max_mdct_line_int; pow34_scaleFact = 3 * (mdct_scaleFactor >> 2) - 1; if (max_mdct_line_short != 0) { Ipp16s tmp_mdct_scaled; int 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); ippsMax_16s(mdct_scaled_short, LEN_MDCT_LINE, &max_mdct_line_short); if (max_mdct_line_short > 0) { int quot = pow34_scaleFact / 3; int rem = pow34_scaleFact - quot * 3; int shift, max_quant, j, tmp_quot, tmp; int 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 < (int)tmp_buf[0]) { max_quant <<= 3; tmp_quot++; } for (j = 15; j >= 0; j--) { tmp = (int)tmp_buf[j]; if ((tmp << scalefac_pow_shift[j]) <= max_quant) { shift = j; break; } } start_common_scalefac = tmp_quot * 16 - shift - 104; shift = 16; delta = (65536 - MAGIC_NUMBER_I) << (2-rem); tmp = ((int)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 = (int)tmp_buf[j]; if ((tmp << (scalefac_pow_shift[j] + add_shift)) < delta) { shift = j; break; } } if (shift == 16) { shift = 15; quot++; } finish_common_scalefac = (quot + 6) * 16 - shift + 1 - 104; if (start_common_scalefac > 255 - SF_OFFSET) { start_common_scalefac = 255 - SF_OFFSET; } else if (start_common_scalefac < -SF_OFFSET) { start_common_scalefac = -SF_OFFSET; } if (finish_common_scalefac > 255 - SF_OFFSET) { finish_common_scalefac = 255 - SF_OFFSET; } else if (finish_common_scalefac < -SF_OFFSET) { finish_common_scalefac = -SF_OFFSET; } } iterReset(gr, ch); main_loop(gr, ch, mdct_scaled_short, pow34_scaleFact, ipptmp0_576); } } return 1; } void MP3EncoderInt::main_loop(int gr, int ch, Ipp16s* mdct_scaled_short, int pow34_scaleFact, short *ipptmp0_576) { Ipp16s tmp_x_quant_short[LEN_MDCT_LINE]; int index, scale, shift; Ipp32s magic_number; int common_scalefactor; int needed_bits; common_scalefactor = last_frame_common_scalefactor[ch]; common_scalefactor_update[ch] = common_scalefactor_update[ch]; if (common_scalefactor < start_common_scalefac) common_scalefactor = start_common_scalefac; if (common_scalefactor > finish_common_scalefac) common_scalefactor = 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, quant_ix[gr][ch], LEN_MDCT_LINE, -scale); needed_bits = calcBits(gr, ch, ipptmp0_576); if (needed_bits == max_bits[gr][ch]) break; if (needed_bits > max_bits[gr][ch]) { if (common_scalefactor == finish_common_scalefac) break; if (common_scalefactor_update[ch] < 0) { common_scalefactor_update[ch] = -common_scalefactor_update[ch]; } common_scalefactor_update[ch] = (common_scalefactor_update[ch] + 1) >> 1; } else { if (common_scalefactor == start_common_scalefac) break; if (common_scalefactor_update[ch] == 1) break; if (common_scalefactor_update[ch] > 0) { common_scalefactor_update[ch] = -common_scalefactor_update[ch]; } common_scalefactor_update[ch] >>= 1; } common_scalefactor += common_scalefactor_update[ch]; if (common_scalefactor < start_common_scalefac) common_scalefactor = start_common_scalefac; if (common_scalefactor > finish_common_scalefac) common_scalefactor = finish_common_scalefac; } common_scalefactor_update[ch] = common_scalefactor - last_frame_common_scalefactor[ch]; last_frame_common_scalefactor[ch] = common_scalefactor; if (common_scalefactor_update[ch] >= 0) { if (common_scalefactor_update[ch] <= 2) common_scalefactor_update[ch] = 2; } else { if (common_scalefactor_update[ch] >= -2) common_scalefactor_update[ch] = -2; } si_globGain[gr][ch] = (short)(common_scalefactor + SF_OFFSET); si_part23Len[gr][ch] = needed_bits; }}; // namespace UMC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -