📄 aac_enc_elements.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) 2003-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 "aac_enc_huff_tables.h"#include "bstream.h"#include "aaccmn_adts.h"#include "aac_enc_own.h"unsigned int sf_huff_codebook[];int enc_ltp_data(sEnc_individual_channel_stream* pStream, sBitsreamBuffer* pBS, int writing);//void tns_data(sEnc_individual_channel_stream* pStream,// sBitsreamBuffer* pBS);/****************************************************************************/void enc_adts_header(sAdts_fixed_header *pFixedHeader, sAdts_variable_header *pVarHeader, sBitsreamBuffer *pBS){ Byte_alignment(pBS); PUT_BITS(pBS,0x0FFF,12); /// The following puts in bitsream ADTS fixed header PUT_BITS(pBS,pFixedHeader->ID,1); PUT_BITS(pBS,pFixedHeader->Layer,2); PUT_BITS(pBS,pFixedHeader->protection_absent,1); PUT_BITS(pBS,pFixedHeader->Profile,2); PUT_BITS(pBS,pFixedHeader->sampling_frequency_index,4); PUT_BITS(pBS,pFixedHeader->private_bit,1); PUT_BITS(pBS,pFixedHeader->channel_configuration,3); PUT_BITS(pBS,pFixedHeader->original_copy,1); PUT_BITS(pBS,pFixedHeader->Home,1); /// The following puts in bitsream ADTS variable header PUT_BITS(pBS,pVarHeader->copyright_identification_bit,1); PUT_BITS(pBS,pVarHeader->copyright_identification_start,1); PUT_BITS(pBS,pVarHeader->aac_frame_length,13); PUT_BITS(pBS,pVarHeader->adts_buffer_fullness,11); PUT_BITS(pBS,pVarHeader->no_raw_data_blocks_in_frame,2);}/****************************************************************************/int enc_ics_info(sEnc_individual_channel_stream* pStream, sBitsreamBuffer* pBS, int writing){ int i; int count_bits; if (writing) { PUT_BITS(pBS,pStream->ics_reserved_bit,1); PUT_BITS(pBS,pStream->windows_sequence,2); PUT_BITS(pBS,pStream->window_shape,1); } count_bits = 4; if (pStream->windows_sequence == EIGHT_SHORT_SEQUENCE) { if (writing) { PUT_BITS(pBS,pStream->max_sfb,4); for (i = 0; i < 7; i ++) { PUT_BITS(pBS,pStream->scale_factor_grouping[i],1); } } count_bits += 11; } else { if (writing) { PUT_BITS(pBS,pStream->max_sfb,6); PUT_BITS(pBS, pStream->predictor_data_present, 1) } count_bits += 7; if (pStream->predictor_data_present) { if (pStream->audioObjectType == AOT_AAC_LTP) { if (writing) { PUT_BITS(pBS, pStream->ltp_data_present, 1) } count_bits++; if (pStream->ltp_data_present) { count_bits += enc_ltp_data(pStream, pBS, writing); } } } } return count_bits;}/****************************************************************************/void enc_section_data(sEnc_individual_channel_stream* pStream, sBitsreamBuffer* pBS){ short* sfb_cb; int len_esc_value; int sect_esc_value; int sect_len; int i; int win; int sect_counter; int last_sect_counter; int sect_cb; if (pStream->windows_sequence == EIGHT_SHORT_SEQUENCE) { len_esc_value = 3; sect_esc_value = (1<<3) - 1; } else { len_esc_value = 5; sect_esc_value = (1<<5) - 1; } sfb_cb = pStream->sfb_cb; sect_counter = -1; last_sect_counter = -1; for (win = 0; win < pStream->num_window_groups; win++) { sect_cb = sfb_cb[0]; sect_counter++; pStream->sect_cb[sect_counter] = sect_cb; pStream->sect_len[sect_counter] = 0; for (i = 0; i < pStream->max_sfb; i++) { if (sfb_cb[i] == sect_cb) { pStream->sect_len[sect_counter]++; } else { sect_cb = sfb_cb[i]; sect_counter++; pStream->sect_cb[sect_counter] = sect_cb; pStream->sect_len[sect_counter] = 1; } } sfb_cb += pStream->max_sfb; pStream->sect_num[win] = sect_counter - last_sect_counter; last_sect_counter = sect_counter; } sect_counter ++; for (i = 0; i < sect_counter; i++) { PUT_BITS(pBS,pStream->sect_cb[i],4); sect_len = pStream->sect_len[i]; while (sect_len >= sect_esc_value) { PUT_BITS(pBS,sect_esc_value,len_esc_value); sect_len -= sect_esc_value; } PUT_BITS(pBS,sect_len,len_esc_value); }}/****************************************************************************/void enc_scale_factor_data(sEnc_individual_channel_stream* pStream, sBitsreamBuffer* pBS){ int i; int last_sf; int index; int len; unsigned int code; int num_scale_factor; num_scale_factor = pStream->num_window_groups * pStream->max_sfb; last_sf = pStream->global_gain; for (i = 0; i < num_scale_factor; i++) { switch(pStream->sfb_cb[i]) { case ZERO_HCB: break; case INTENSITY_HCB: break; case INTENSITY_HCB2: break; default: index = last_sf - pStream->scale_factors[i] + SF_MID; len = sf_huff_codebook[2*index+0]; code = sf_huff_codebook[2*index+1]; PUT_BITS(pBS,code,len); last_sf = pStream->scale_factors[i]; break; } }}/****************************************************************************/void enc_spectral_data(sEnc_individual_channel_stream* pStream, sBitsreamBuffer* pBS){ IppsVLCEncodeSpec_32s* pVLCEncSpec; Ipp8u *pDst; Ipp16s tmp_src[512], *q; int* sfb_offset; int i, j; int win; int sect_cb; int sect_counter; int sect_end; int data_begin; int data_end; int shift; int offset; int bitoffset; sfb_offset = pStream->sfb_offset; pDst = (Ipp8u*)pBS->pCurrent_dword + ((32 - pBS->nBit_offset) >> 3); bitoffset = (32 - pBS->nBit_offset) & 0x7; SAVE_BITSTREAM(pBS) sect_counter = 0; for (win = 0; win < pStream->num_window_groups; win++) { data_begin = sfb_offset[0]; sect_end = 0; for (i = 0; i < pStream->sect_num[win]; i++) { sect_end += pStream->sect_len[sect_counter]; data_end = sfb_offset[sect_end]; sect_cb = pStream->sect_cb[sect_counter]; shift = vlcEncShifts[sect_cb]; offset = vlcEncOffsets[sect_cb]; pVLCEncSpec = (IppsVLCEncodeSpec_32s*)pStream->pHuffTables[sect_cb]; switch (vlcEncTypes[sect_cb]) { case 0: /* 4 tuples */ q = &pStream->x_quant[data_begin]; for (j = 0; j < (data_end-data_begin) >> 2; j++) { tmp_src[j] = (Ipp16s)((q[0] << (3*shift)) + ((q[1] + offset) << (2*shift)) + ((q[2] + offset) << shift) + (q[3] + offset)); q += 4; } ippsVLCEncodeBlock_16s1u(tmp_src, (data_end-data_begin) >> 2, &pDst, &bitoffset, pVLCEncSpec); break; case 1: /* 2 tuples */ q = &pStream->x_quant[data_begin]; for (j = 0; j < (data_end-data_begin) >> 1; j++) { tmp_src[j] = (Ipp16s)((q[0] << shift) + (q[1] + offset)); q += 2; } ippsVLCEncodeBlock_16s1u(tmp_src, (data_end-data_begin) >> 1, &pDst, &bitoffset, pVLCEncSpec); break; case 2: /* esc */ ippsVLCEncodeEscBlock_AAC_16s1u(&pStream->x_quant[data_begin], (data_end-data_begin), &pDst, &bitoffset, pVLCEncSpec); } data_begin = data_end; sect_counter++; } sfb_offset += pStream->num_sfb; } pBS->pCurrent_dword = (Ipp32u*)(pDst - ((Ipp32s)(pDst) & 3)); pBS->nBit_offset = 32 - ((pDst - (Ipp8u*)pBS->pCurrent_dword) << 3)- bitoffset; LOAD_DWORD(pBS)}/****************************************************************************/int enc_individual_channel_stream(sEnc_individual_channel_stream* pStream, int common_window, int scale_flag, sBitsreamBuffer* pBS, int writing){ int sb; int count_bits; if (writing) { int num_scale_factor; num_scale_factor = pStream->num_window_groups * pStream->max_sfb; for (sb = 0; sb < num_scale_factor; sb++) { if (pStream->sfb_cb != 0) { pStream->global_gain = pStream->scale_factors[sb]; break; } } PUT_BITS(pBS,pStream->global_gain,8); } count_bits = 8; if (!common_window && !scale_flag) { count_bits += enc_ics_info(pStream, pBS, writing); } if (writing) { enc_section_data(pStream, pBS); enc_scale_factor_data(pStream, pBS); } if (!scale_flag) { if (writing) { PUT_BITS(pBS,0,1); /// Pulse data isn't supported. PUT_BITS(pBS,0,1); /// TNS data isn't supported. PUT_BITS(pBS,0,1); /// Gain control data isn't supported. } count_bits += 3; } if (writing) { enc_spectral_data(pStream, pBS); } return count_bits;}/****************************************************************************/int enc_single_channel_element(sEnc_single_channel_element* pElement, int element_instance_tag, sBitsreamBuffer* pBS, int writing){ int count_bits; if (writing) { PUT_BITS(pBS, element_instance_tag,4); } count_bits = 4; count_bits += enc_individual_channel_stream(pElement->p_individual_channel_stream, 0, 0, pBS, writing); return count_bits;}extern int ubit[];/****************************************************************************/int enc_channel_pair_element(sEnc_channel_pair_element* pElement, int element_instance_tag, sBitsreamBuffer* pBS, int writing){ int common_window = pElement->common_window; int count_bits; if (writing) { PUT_BITS(pBS,element_instance_tag,4); PUT_BITS(pBS,common_window,1); } count_bits = 5; if (common_window) { count_bits += enc_ics_info(pElement->p_individual_channel_stream_0, pBS, writing); if ((pElement->p_individual_channel_stream_0->audioObjectType == AOT_AAC_LTP) && (EIGHT_SHORT_SEQUENCE != pElement->p_individual_channel_stream_0->windows_sequence)) { if (pElement->p_individual_channel_stream_0->predictor_data_present) { if (writing) { PUT_BITS(pBS, pElement->p_individual_channel_stream_1->ltp_data_present, 1) } count_bits++; if (pElement->p_individual_channel_stream_1->ltp_data_present) { count_bits += enc_ltp_data(pElement->p_individual_channel_stream_1, pBS, writing); } } } if (writing) { PUT_BITS(pBS,0,2); /// MS data aren't supported. } count_bits += 2; } count_bits += enc_individual_channel_stream(pElement->p_individual_channel_stream_0, common_window, 0, pBS, writing); count_bits += enc_individual_channel_stream(pElement->p_individual_channel_stream_1, common_window, 0, pBS, writing); return count_bits;}/****************************************************************************/int enc_ltp_data(sEnc_individual_channel_stream* pStream, sBitsreamBuffer* pBS, int writing){ int i; int pred_max_sfb; pred_max_sfb = MAX_LTP_SFB_LONG < pStream->max_sfb ? MAX_LTP_SFB_LONG : pStream->max_sfb; if (writing) { PUT_BITS(pBS, pStream->ltp_lag, 11) PUT_BITS(pBS, pStream->ltp_coef, 3) for (i = 0; i < pred_max_sfb; i++) { PUT_BITS(pBS, pStream->ltp_long_used[i], 1) } } return (14 + pred_max_sfb);}/****************************************************************************/unsigned int sf_huff_codebook[] = { /* len, code*/ 18, 0x3FFE8, 18, 0x3FFE6, 18, 0x3FFE7, 18, 0x3FFE5, 19, 0x7FFF5, 19, 0x7FFF1, 19, 0x7FFED, 19, 0x7FFF6, 19, 0x7FFEE, 19, 0x7FFEF, 19, 0x7FFF0, 19, 0x7FFFC, 19, 0x7FFFD, 19, 0x7FFFF, 19, 0x7FFFE, 19, 0x7FFF7, 19, 0x7FFF8, 19, 0x7FFFB, 19, 0x7FFF9, 18, 0x3FFE4, 19, 0x7FFFA, 18, 0x3FFE3, 17, 0x1FFEF, 17, 0x1FFF0, 16, 0x0FFF5, 17, 0x1FFEE, 16, 0x0FFF2, 16, 0x0FFF3, 16, 0x0FFF4, 16, 0x0FFF1, 15, 0x07FF6, 15, 0x07FF7, 14, 0x03FF9, 14, 0x03FF5, 14, 0x03FF7, 14, 0x03FF3, 14, 0x03FF6, 14, 0x03FF2, 13, 0x01FF7, 13, 0x01FF5, 12, 0x00FF9, 12, 0x00FF7, 12, 0x00FF6, 11, 0x007F9, 12, 0x00FF4, 11, 0x007F8, 10, 0x003F9, 10, 0x003F7, 10, 0x003F5, 9, 0x001F8, 9, 0x001F7, 8, 0x000FA, 8, 0x000F8, 8, 0x000F6, 7, 0x00079, 6, 0x0003A, 6, 0x00038, 5, 0x0001A, 4, 0x0000B, 3, 0x00004, 1, 0x00000, 4, 0x0000A, 4, 0x0000C, 5, 0x0001B, 6, 0x00039, 6, 0x0003B, 7, 0x00078, 7, 0x0007A, 8, 0x000F7, 8, 0x000F9, 9, 0x001F6, 9, 0x001F9, 10, 0x003F4, 10, 0x003F6, 10, 0x003F8, 11, 0x007F5, 11, 0x007F4, 11, 0x007F6, 11, 0x007F7, 12, 0x00FF5, 12, 0x00FF8, 13, 0x01FF4, 13, 0x01FF6, 13, 0x01FF8, 14, 0x03FF8, 14, 0x03FF4, 16, 0x0FFF0, 15, 0x07FF4, 16, 0x0FFF6, 15, 0x07FF5, 18, 0x3FFE2, 19, 0x7FFD9, 19, 0x7FFDA, 19, 0x7FFDB, 19, 0x7FFDC, 19, 0x7FFDD, 19, 0x7FFDE, 19, 0x7FFD8, 19, 0x7FFD2, 19, 0x7FFD3, 19, 0x7FFD4, 19, 0x7FFD5, 19, 0x7FFD6, 19, 0x7FFF2, 19, 0x7FFDF, 19, 0x7FFE7, 19, 0x7FFE8, 19, 0x7FFE9, 19, 0x7FFEA, 19, 0x7FFEB, 19, 0x7FFE6, 19, 0x7FFE0, 19, 0x7FFE1, 19, 0x7FFE2, 19, 0x7FFE3, 19, 0x7FFE4, 19, 0x7FFE5, 19, 0x7FFD7, 19, 0x7FFEC, 19, 0x7FFF4, 19, 0x7FFF3};/****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -