📄 aac_enc_api_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) 2004-2005 Intel Corporation. All Rights Reserved.////////*//********************************************************************/#include <stdlib.h>#include <stdio.h>#include "aac_enc_own_int.h"#include "aac_enc_own.h"#include "aac_enc.h"#include "aac_sfb_tables.h"#include "aac_enc_huff_tables.h"#include "aaccmn_const.h"#include "aac_enc_psychoacoustic_int.h"#include "aac_enc_filterbank_int.h"#include "aac_enc_quantization_int.h"#include "aac_enc_ltp_int.h"#include "ipps.h"/****************************************************************************/AACStatus aacencInit(AACEnc **state_ptr, int sampling_frequency, int chNum, int bit_rate, enum AudioObjectType audioObjectType){ AACEnc *state; AACEnc_com *state_com; Ipp8u *ptr; Ipp32s **ltp_overlap; Ipp16s **m_buff_pointers; Ipp16s **ltp_buff; Ipp16s **buff; int sf_index; int ch; int i, k, w; int sizeOfAACenc, sizeOfOneChannelInfo; int sizeOfPsychoacousticBlock, sizeOfPointers; int size; int num_sfb_for_long; int num_sfb_for_short; int* sfb_offset_for_long; int* sfb_offset_for_short; int cutoff_frequency; int bits_per_frame; int bits_for_lfe = 0; int lfe_channel_present; int max_line, max_sfb; int samp_rate_per_ch, inScaleFactor; short short_tmp, short_cutoff_frequency; if (!state_ptr) return AAC_NULL_PTR; if (chNum < 1) { return AAC_BAD_PARAMETER; } sf_index = 12; for (i = 0; i < 12; i ++) { if (sfb_tables[i].samp_rate == sampling_frequency) { sf_index = i; break; } } if (sf_index == 12) return AAC_BAD_PARAMETER; sizeOfAACenc = (sizeof(AACEnc) + 31) & (~31); sizeOfOneChannelInfo = (sizeof(sOneChannelInfo) + 31) & (~31); sizeOfPsychoacousticBlock = (sizeof(sPsychoacousticBlock) + 31) & (~31); sizeOfPointers = (chNum * (5 * sizeof(Ipp16s*) + sizeof(Ipp32s*)) + 31) & (~31); size = sizeOfAACenc + sizeOfPointers + chNum * (sizeOfOneChannelInfo + sizeOfPsychoacousticBlock + 3 * 1024 * sizeof(Ipp16s)) + 32; if (audioObjectType == AOT_AAC_LTP) { size += chNum * (3072 * sizeof(Ipp16s) + 1024 * sizeof(Ipp32s)); } ptr = (Ipp8u *)ippsMalloc_8u(size); if (ptr == NULL) return AAC_ALLOC; ippsZero_8u(ptr, size); state = (AACEnc *)(ptr + (32 - ((int)ptr & 31))); state_com = &(state->com); state_com->real_state = ptr; ptr = (Ipp8u *)state + sizeOfAACenc; state_com->chInfo = (sOneChannelInfo *)ptr; ptr += chNum * sizeOfOneChannelInfo; state_com->m_channel_number = chNum; state_com->sampling_frequency_index = sf_index; state_com->m_sampling_frequency = sampling_frequency; state_com->m_bitrate = bit_rate; ltp_overlap = (Ipp32s**)ptr; m_buff_pointers = (Ipp16s**)(ltp_overlap + chNum); ltp_buff = m_buff_pointers + 3 * chNum; buff = ltp_buff + chNum; ptr += sizeOfPointers; state->psychoacoustic_block = (sPsychoacousticBlock *)ptr; ptr += chNum * sizeOfPsychoacousticBlock; if (audioObjectType == AOT_AAC_LTP) { for (i = 0; i < chNum; i++) { ltp_overlap[i] = (Ipp32s*)ptr; ptr += 1024 * sizeof(Ipp32s); } for (i = 0; i < chNum; i++) { ltp_buff[i] = (Ipp16s*)ptr; ptr += 3072 * sizeof(Ipp16s); } } for (i = 0; i < 3 * chNum; i++) { m_buff_pointers[i] = (Ipp16s*)ptr; ptr += 1024 * sizeof(Ipp16s); } state->m_buff_pointers = m_buff_pointers; state->ltp_buff = ltp_buff; state->ltp_overlap = ltp_overlap; state_com->buff = buff; state_com->m_buff_prev_index = 0; state_com->m_buff_curr_index = 1; state_com->m_buff_next_index = 2; num_sfb_for_long = state_com->real_num_sfb[0] = state_com->real_num_sfb[1] = state_com->real_num_sfb[3] = sfb_tables[sf_index].num_sfb_long_window; num_sfb_for_short = state_com->real_num_sfb[2] = sfb_tables[sf_index].num_sfb_short_window; sfb_offset_for_short = sfb_tables[sf_index].sfb_offset_short_window; state_com->sfb_offset_for_short_window[0] = 0; k = 1; for (w = 0; w < 8; w ++) { for ( i = 0; i < num_sfb_for_short; i++) { state_com->sfb_offset_for_short_window[k] = state_com->sfb_offset_for_short_window[k-1] + (sfb_offset_for_short[i + 1] - sfb_offset_for_short[i]); k++; } } state_com->sfb_offset[0] = state_com->sfb_offset[1] = state_com->sfb_offset[3] = sfb_tables[sf_index].sfb_offset_long_window; state_com->sfb_offset[2] = state_com->sfb_offset_for_short_window; BuildHuffmanTables((IppsVLCEncodeSpec_32s**)(&state_com->huffman_tables)); lfe_channel_present = 0; for (ch = 0; ch < chNum;) { if (ch == 0) { if (state_com->m_channel_number == 2) { state_com->chInfo[ch].element_id = ID_CPE; state_com->chInfo[ch + 1].element_id = ID_CPE; ch += 2; } else { state_com->chInfo[ch].element_id = ID_SCE; ch += 1; } } else { if (state_com->m_channel_number != state_com->m_channel_number - 1) { state_com->chInfo[ch].element_id = ID_CPE; state_com->chInfo[ch + 1].element_id = ID_CPE; ch += 2; } else { state_com->chInfo[ch].element_id = ID_LFE; ch += 1; lfe_channel_present++; } } } bits_per_frame = (bit_rate * 1024)/(sfb_tables[sf_index].samp_rate); bits_per_frame &= ~7; bits_per_frame -= 3; /* for ID_END */ samp_rate_per_ch = bit_rate / chNum; sfb_offset_for_long = sfb_tables[sf_index].sfb_offset_long_window; inScaleFactor = 0; while (samp_rate_per_ch >= 32768) { inScaleFactor += 1; samp_rate_per_ch >>= 1; } short_tmp = (short)samp_rate_per_ch; ippsPow34_16s_Sfs(&short_tmp, inScaleFactor, &short_cutoff_frequency, 0, 1); cutoff_frequency = short_cutoff_frequency * 4; max_line = (2048 * cutoff_frequency) / sampling_frequency; max_sfb = num_sfb_for_long; for (i = 0; i < num_sfb_for_long; i++) { if (sfb_offset_for_long[i] > max_line) { max_sfb = i; break; } } state_com->real_max_sfb[0] = state_com->real_max_sfb[1] = state_com->real_max_sfb[3] = max_sfb; max_line = (256 * cutoff_frequency) / sampling_frequency; max_sfb = num_sfb_for_short; for (i = 0; i < num_sfb_for_short; i++) { if (sfb_offset_for_short[i] > max_line) { max_sfb = i; break; } } state_com->real_max_sfb[2] = max_sfb; /* max_sfb for LFE channel calculating */ if (cutoff_frequency > MAX_LFE_FREQUENCY) { cutoff_frequency = MAX_LFE_FREQUENCY; max_line = (2048 * cutoff_frequency) / sampling_frequency; max_sfb = num_sfb_for_long; for (i = 0; i < num_sfb_for_long; i++) { if (sfb_offset_for_long[i] > max_line) { max_sfb = i; break; } } state_com->real_max_sfb_lfe[0] = state_com->real_max_sfb_lfe[1] = state_com->real_max_sfb_lfe[3] = max_sfb; max_line = (256 * cutoff_frequency) / sampling_frequency; max_sfb = num_sfb_for_short; for (i = 0; i < num_sfb_for_short; i++) { if (sfb_offset_for_short[i] > max_line) { max_sfb = i; break; } } state_com->real_max_sfb_lfe[2] = max_sfb; } else { state_com->real_max_sfb_lfe[0] = state_com->real_max_sfb_lfe[1] = state_com->real_max_sfb_lfe[3] = state_com->real_max_sfb[0]; state_com->real_max_sfb_lfe[2] = state_com->real_max_sfb[2]; } if (InitFilterbank_enc(&(state->filterbank_enc)) != AAC_OK) { aacencClose(state); return AAC_ALLOC; } if (InitFilterbankInt(&(state->filterbank), FB_DECODER | FB_ENCODER) != AAC_OK) { aacencClose(state); return AAC_ALLOC; } if (InitPsychoacousticCom(&state->psychoacoustic_block_com, sf_index) != AAC_OK) { aacencClose(state); return AAC_ALLOC; } state->psychoacoustic_block_com.prev_prev_f_r_index = state_com->m_buff_prev_index; state->psychoacoustic_block_com.prev_f_r_index = state_com->m_buff_curr_index; state->psychoacoustic_block_com.current_f_r_index = state_com->m_buff_next_index; for (ch = 0; ch < chNum; ch++) { InitPsychoacoustic(&state->psychoacoustic_block[ch]); } if (lfe_channel_present != 0) { bits_for_lfe = bits_per_frame/(5 * chNum - 4 * lfe_channel_present); if (bits_for_lfe < 300) bits_for_lfe = 300; else if (bits_for_lfe > 768 * 8) bits_for_lfe = 768 * 8; bits_per_frame -= bits_for_lfe * lfe_channel_present; } for (ch = 0; ch < chNum;) { int sce_tag = 0; int cpe_tag = 0; int lfe_tag = 0; int num_channel = (state_com->m_channel_number - lfe_channel_present); state_com->chInfo[ch].prev_window_shape = 1; state_com->chInfo[ch].common_scalefactor_update = 2; state_com->chInfo[ch].last_frame_common_scalefactor = 0; if (state_com->chInfo[ch].element_id == ID_CPE) { state_com->chInfo[ch+1].common_scalefactor_update = 2; state_com->chInfo[ch+1].last_frame_common_scalefactor = 0; state_com->chInfo[ch].element_instance_tag = cpe_tag; state_com->chInfo[ch].bits_in_buf = 0; state_com->chInfo[ch].mean_bits = (2 * bits_per_frame)/num_channel; state_com->chInfo[ch].max_bits_in_buf = 768 * 8 * 2; bits_per_frame -= (state_com->chInfo[ch].mean_bits); cpe_tag++; ch += 2; num_channel -= 2; } else if (state_com->chInfo[ch].element_id == ID_SCE) { state_com->chInfo[ch].element_instance_tag = sce_tag; state_com->chInfo[ch].bits_in_buf = 0; state_com->chInfo[ch].mean_bits = bits_per_frame/num_channel; state_com->chInfo[ch].max_bits_in_buf = 768 * 8; bits_per_frame -= (state_com->chInfo[ch].mean_bits); sce_tag++; ch += 1; num_channel -= 1; } else { state_com->chInfo[ch].element_instance_tag = lfe_tag; state_com->chInfo[ch].bits_in_buf = 0; state_com->chInfo[ch].mean_bits = bits_for_lfe; state_com->chInfo[ch].max_bits_in_buf = 768 * 8; lfe_tag++; ch += 1; } } state_com->audioObjectType = audioObjectType; if (state_com->audioObjectType == AOT_AAC_LTP) { if (ippsFFTInitAlloc_R_32s(&state->corrFft, 12, IPP_FFT_DIV_INV_BY_N, ippAlgHintNone) != ippStsOk) { aacencClose(state); return AAC_ALLOC; } if (ippsFFTGetBufSize_R_32s(state->corrFft, &size) != ippStsOk) { aacencClose(state); return AAC_ALLOC; } if (size != 0) { state->corrBuff = ippsMalloc_8u(size); if (state->corrBuff== NULL) { aacencClose(state); aacencClose(state); } } } *state_ptr = state; return AAC_OK;}/********************************************************************/AACStatus aacencGetFrame(Ipp16s *inPointer, int *encodedBytes, Ipp8u *outPointer, AACEnc *state){ __ALIGN Ipp16s mdct_line[1024]; __ALIGN Ipp16s mdct_line_i[1024]; __ALIGN Ipp16s mdct_line_pred[1024]; __ALIGN Ipp32s predictedBuf[2048]; __ALIGN Ipp32s predictedSpectrum[1024]; __ALIGN Ipp16s predictedSpectrum16s[1024]; __ALIGN Ipp32s inSignal[4 * 1024]; Ipp16s *p_mdct_line; Ipp16s *p_mdct_line_pred; AACEnc_com *state_com; int sfb_offset[MAX_SECTION_NUMBER]; sEnc_individual_channel_stream ics[2]; sQuantizationBlock quantization_block; sEnc_single_channel_element sce; sEnc_channel_pair_element cpe; sBitsreamBuffer BS; sBitsreamBuffer *pBS = &BS; int win_seq; int win_shape; int i, j, numCh, ch, procCh, sfb; int available_bits, bits_in_buf; int bits_from_buffer, com_additional_bits, used_bits, save_bits; int additional_bits[2], max_bits_in_buf, norm_bits, lim_bits;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -