📄 aac_enc_quantization_int.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) 2004-2005 Intel Corporation. All Rights Reserved.//// Intel Integrated Performance Primitives AAC Encode Sample for Windows*//// By downloading and installing this sample, you hereby agree that the// accompanying Materials are being provided to you under the terms and// conditions of the End User License Agreement for the Intel Integrated// Performance Primitives product previously accepted by you. Please refer// to the file ipplic.htm located in the root directory of your Intel IPP// product installation for more information.//// MPEG-4 and AAC are international standards promoted by ISO, IEC, ITU, ETSI// and other organizations. Implementations of these standards, or the standard// enabled platforms may require licenses from various entities, including// Intel Corporation.//*/#include <math.h>#include "ippac.h"#include "ipps.h"#include "aac_enc_quantization_int.h"#include "aac_enc_search.h"/****************************************************************************/void main_loop(sQuantizationBlock* pBlock, sEnc_individual_channel_stream* pStream, Ipp16s* mdct_line, Ipp16s* mdct_scaled, Ipp16s* mdct_line_pred, Ipp16s* mdct_scaled_pred, int pow34_scaleFact);/****************************************************************************/Ipp16s scalefac_pow[] = { 32767, 18658, 21247, 24196, 27554, 31379, 17867, 20347, 23170, 26386, 30048, 17109, 19484, 22188, 25268, 28774};Ipp16s scalefac_pow_shift[] = { 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3};/****************************************************************************/void Quantization(sQuantizationBlock* pBlock, sEnc_individual_channel_stream* pStream, Ipp16s* mdct_line, Ipp16s* mdct_line_pred, int mdct_scaleFactor){ Ipp16s mdct_line_abs[N_LONG/2]; Ipp16s mdct_scaled[N_LONG/2]; Ipp16s mdct_scaled_pred[N_LONG/2]; int pow34_scaleFact; Ipp16s max_mdct_line, max_mdct_line_pred; pBlock->start_common_scalefac = -SF_OFFSET; pBlock->finish_common_scalefac = -SF_OFFSET; max_mdct_line = 0; ippsAbs_16s(mdct_line, mdct_line_abs, N_LONG/2); ippsMax_16s(mdct_line_abs, N_LONG/2, &max_mdct_line); if (mdct_line_pred) { ippsAbs_16s(mdct_line_pred, mdct_line_abs, N_LONG/2); ippsMax_16s(mdct_line_abs, N_LONG/2, &max_mdct_line_pred); if (max_mdct_line_pred > max_mdct_line) { max_mdct_line = max_mdct_line_pred; } } pow34_scaleFact = 3 * (mdct_scaleFactor >> 2) - 1; if (max_mdct_line != 0) { Ipp16s tmp_mdct_scaled; int i_tmp_mdct_scaled; ippsPow34_16s_Sfs(&max_mdct_line, mdct_scaleFactor, &tmp_mdct_scaled, pow34_scaleFact, 1); i_tmp_mdct_scaled = tmp_mdct_scaled; while (i_tmp_mdct_scaled <= 16384) { i_tmp_mdct_scaled = i_tmp_mdct_scaled << 1; pow34_scaleFact--; } } ippsPow34_16s_Sfs(mdct_line, mdct_scaleFactor, mdct_scaled, pow34_scaleFact, N_LONG/2); ippsMax_16s(mdct_scaled, N_LONG/2, &max_mdct_line); if (mdct_line_pred) { ippsPow34_16s_Sfs(mdct_line_pred, mdct_scaleFactor, mdct_scaled_pred, pow34_scaleFact, N_LONG/2); ippsMax_16s(mdct_scaled_pred, N_LONG/2, &max_mdct_line_pred); if (max_mdct_line_pred > max_mdct_line) { max_mdct_line = max_mdct_line_pred; } } if (max_mdct_line > 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, 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; } } pBlock->start_common_scalefac = tmp_quot * 16 - shift; 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++; } pBlock->finish_common_scalefac = (quot + 6) * 16 - shift + 1; if (pBlock->start_common_scalefac > 255 - SF_OFFSET) { pBlock->start_common_scalefac = 255 - SF_OFFSET; } else if (pBlock->start_common_scalefac < -SF_OFFSET) { pBlock->start_common_scalefac = -SF_OFFSET; } if (pBlock->finish_common_scalefac > 255 - SF_OFFSET) { pBlock->finish_common_scalefac = 255 - SF_OFFSET; } else if (pBlock->finish_common_scalefac < -SF_OFFSET) { pBlock->finish_common_scalefac = -SF_OFFSET; } } main_loop(pBlock, pStream, mdct_line, mdct_scaled, mdct_line_pred, mdct_scaled_pred, pow34_scaleFact);}/****************************************************************************/void main_loop(sQuantizationBlock* pBlock, sEnc_individual_channel_stream* pStream, Ipp16s* mdct_line, Ipp16s* mdct_scaled, Ipp16s* mdct_line_pred, Ipp16s* mdct_scaled_pred, int pow34_scaleFact){ Ipp16s tmp_x_quant[N_LONG/2]; Ipp16s mdct_sign[N_LONG/2]; Ipp16s mdct_sign_pred[N_LONG/2]; Ipp16s x_quant_unsigned[N_LONG/2]; Ipp16s quant_unsigned_pred[N_LONG/2]; Ipp16s x_quant_signed_pred[N_LONG/2]; Ipp16s *x_quant_unsigned_pred; int start_common_scalefac = pBlock->start_common_scalefac; int finish_common_scalefac = pBlock->finish_common_scalefac; int common_scalefactor; int common_scalefactor_update; int needed_bits, bits_for_scale_factor_data; int num_scale_factor; int i, pred; int index, scale, shift; Ipp32s magic_number; num_scale_factor = pStream->num_window_groups * pStream->max_sfb; pred = 0; if (mdct_line_pred) { pred = 1; } for (i = 0; i < N_LONG/2; i++) { mdct_sign[i] = SIGN(mdct_line[i]); } x_quant_unsigned_pred = 0; if (pred) { for (i = 0; i < N_LONG/2; i++) { mdct_sign_pred[i] = SIGN(mdct_line_pred[i]); } x_quant_unsigned_pred = quant_unsigned_pred; } common_scalefactor = pBlock->last_frame_common_scalefactor[0]; common_scalefactor_update = pBlock->common_scalefactor_update[0]; 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) & 0xF; scale = pow34_scaleFact + (3 * ((-common_scalefactor) >> 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, scalefac_pow[index], tmp_x_quant, N_LONG/2, 15 + shift); ippsAddC_16s_Sfs(tmp_x_quant, (Ipp16s)magic_number, x_quant_unsigned, N_LONG/2, -scale); ippsMul_16s(mdct_sign, x_quant_unsigned, pStream->x_quant, N_LONG/2); if (pred) { ippsMulC_16s_Sfs(mdct_scaled_pred, scalefac_pow[index], tmp_x_quant, N_LONG/2, 15 + shift); ippsAddC_16s_Sfs(tmp_x_quant, (Ipp16s)magic_number, x_quant_unsigned_pred, N_LONG/2, -scale); ippsMul_16s(mdct_sign_pred, x_quant_unsigned_pred, x_quant_signed_pred, N_LONG/2); } needed_bits = best_codebooks_search(pStream, x_quant_unsigned, pStream->x_quant, x_quant_unsigned_pred, x_quant_signed_pred); bits_for_scale_factor_data = num_scale_factor; for (i = 0; i < num_scale_factor; i++) { if (pStream->sfb_cb[i] == 0) bits_for_scale_factor_data--; } needed_bits += bits_for_scale_factor_data; if (needed_bits == pBlock->available_bits) break; if (needed_bits > pBlock->available_bits) { if (common_scalefactor == finish_common_scalefac) break; if (common_scalefactor_update < 0) { common_scalefactor_update = -common_scalefactor_update; } common_scalefactor_update = (common_scalefactor_update + 1) >> 1; } else { if (common_scalefactor == start_common_scalefac) break; if (common_scalefactor_update == 1) break; if (common_scalefactor_update > 0) { common_scalefactor_update = -common_scalefactor_update; } common_scalefactor_update >>= 1; } common_scalefactor += common_scalefactor_update; if (common_scalefactor < start_common_scalefac) common_scalefactor = start_common_scalefac; if (common_scalefactor > finish_common_scalefac) common_scalefactor = finish_common_scalefac; } pBlock->common_scalefactor_update[0] = common_scalefactor - pBlock->last_frame_common_scalefactor[0]; pBlock->last_frame_common_scalefactor[0] = common_scalefactor; if (pBlock->common_scalefactor_update[0] >= 0) { if (pBlock->common_scalefactor_update[0] <= 2) pBlock->common_scalefactor_update[0] = 2; } else { if (pBlock->common_scalefactor_update[0] >= -2) pBlock->common_scalefactor_update[0] = -2; } pBlock->used_bits = needed_bits; common_scalefactor += SF_OFFSET; for (i = 0; i < num_scale_factor; i++) { pStream->scale_factors[i] = (short)common_scalefactor; } if (pred) { int *pred_used, pred_present; int max_sfb_pred = pStream->max_sfb; pred_present = 0; pred_used = 0; if (pStream->audioObjectType == AOT_AAC_LTP) { pred_present = pStream->ltp_data_present; pred_used = pStream->ltp_long_used; if (max_sfb_pred > MAX_LTP_SFB_LONG) max_sfb_pred = MAX_LTP_SFB_LONG; } if (pred_present) { int sfb; for (sfb = 0; sfb < max_sfb_pred; sfb++) { if (pred_used[sfb]) { int begin = pStream->sfb_offset[sfb]; int end = pStream->sfb_offset[sfb+1]; for (i = begin; i < end; i++) { pStream->x_quant[i] = x_quant_signed_pred[i]; } } } } }}/****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -