📄 aac_se_enc.c
字号:
/*********************************************************************
*
* Module for writing/counting AAC syntactic elements
*
* Authors:
* CL Chuck Lueck, TI <lueck@ti.com>
* ADD Alberto Duenas, TI <alberto@ndsuk.com>
* RG Ralf Geiger, FhG/IIS
* Changes:
* 07-jun-97 CL Initial revision.
* 14-sep-97 CL Modified WritePredictorData to actually write
* predictor data. Still need to add resets.
* 20-oct-97 CL Updated WriteTNSData to support TNS.
* 03-Dec-97 ADD Addding the prediction reset
* 22-Jan-98 CL Added support for CPE's and common windows.
* 07-Apr-98 RG Added WriteLFE to write LFE channel element.
*
**********************************************************************/
#include <stdlib.h>
#include "aac_se_enc.h"
#include "aac_qc.h"
int max_pred_sfb;
/*****************************************************************************/
/* WriteAACFillBits(...) */
/* Write fill_elements to the bitstream. */
/* Number of bits written is LEN_SE_ID + FIL_COUNT_LEN + a multiple of 8. */
/* Return number of bits left over (less than 7 ). */
/*****************************************************************************/
int WriteAACFillBits(BsBitStream* ptrBs, /* Pointer to bitstream */
int numBits) /* Number of bits needed to fill */
{
int numberOfBitsLeft=numBits;
/* Need at least (LEN_SE_ID + LEN_F_CNT) bits for a fill_element */
int minNumberOfBits = LEN_SE_ID + LEN_F_CNT;
while (numberOfBitsLeft>=minNumberOfBits) {
int numberOfBytes;
int maxCount;
BsPutBit(ptrBs,ID_FIL,LEN_SE_ID); /* Write fill_element ID */
numberOfBitsLeft-=minNumberOfBits; /* Subtract for ID,count */
numberOfBytes=(int)(numberOfBitsLeft/LEN_BYTE);
maxCount = (1<<LEN_F_CNT) - 1; /* Max count without escaping */
/* if we have less than maxCount bytes, write them now */
if (numberOfBytes<maxCount) {
int i;
BsPutBit(ptrBs,numberOfBytes,LEN_F_CNT);
for (i=0;i<numberOfBytes;i++) {
BsPutBit(ptrBs,0,LEN_BYTE);
}
/* otherwise, we need to write an escape count */
} else {
int maxEscapeCount,maxNumberOfBytes,escCount;
int i;
BsPutBit(ptrBs,maxCount,LEN_F_CNT);
maxEscapeCount = (1<<LEN_BYTE) - 1; /* Max escape count */
maxNumberOfBytes = maxCount + maxEscapeCount;
numberOfBytes = (numberOfBytes > maxNumberOfBytes ) ?
(maxNumberOfBytes) : (numberOfBytes);
escCount = numberOfBytes - maxCount;
BsPutBit(ptrBs,escCount,LEN_BYTE);
for (i=0;i<numberOfBytes-1;i++) {
BsPutBit(ptrBs,0,LEN_BYTE);
}
}
numberOfBitsLeft -= LEN_BYTE*numberOfBytes;
}
return numberOfBitsLeft;
}
/*****************************************************************************/
/* WriteSCE(...), write a single-channel element to the bitstream. */
/*****************************************************************************/
int WriteSCE(AACQuantInfo* quantInfo, /* AACQuantInfo structure */
int tag,
BsBitStream* fixedStream, /* Pointer to bitstream */
int writeFlag) /* 1 means write, 0 means count only */
{
int bit_count=0;
if (writeFlag) {
/* write ID_SCE, single_element_channel() identifier */
BsPutBit(fixedStream,ID_SCE,LEN_SE_ID);
/* write the element_identifier_tag */
BsPutBit(fixedStream,tag,LEN_TAG);
}
bit_count += LEN_SE_ID;
bit_count += LEN_TAG;
/* Write an individual_channel_stream element */
bit_count += WriteICS(quantInfo,0,fixedStream,writeFlag);
return bit_count;
}
/*****************************************************************************/
/* WriteLFE(...), write a lfe-channel element to the bitstream. */
/*****************************************************************************/
int WriteLFE(AACQuantInfo* quantInfo, /* AACQuantInfo structure */
int tag,
BsBitStream* fixedStream, /* Pointer to bitstream */
int writeFlag) /* 1 means write, 0 means count only */
{
int bit_count=0;
if (writeFlag) {
/* write ID_LFE, lfe_element_channel() identifier */
BsPutBit(fixedStream,ID_LFE,LEN_SE_ID);
/* write the element_identifier_tag */
BsPutBit(fixedStream,tag,LEN_TAG);
}
bit_count += LEN_SE_ID;
bit_count += LEN_TAG;
/* Write an individual_channel_stream element */
bit_count += WriteICS(quantInfo,0,fixedStream,writeFlag);
return bit_count;
}
/*****************************************************************************/
/* WriteCPE(...), write a channel_pair_element to the bitstream. */
/*****************************************************************************/
int WriteCPE(AACQuantInfo* quantInfoL, /* AACQuantInfo structure, left */
AACQuantInfo* quantInfoR, /* AACQuantInfo structure, right */
int tag,
int commonWindow, /* common_window flag */
MS_Info* ms_info, /* MS stereo information */
BsBitStream* fixedStream, /* Pointer to bitstream */
int writeFlag) /* 1 means write, 0 means count only */
{
int bit_count=0;
if (writeFlag) {
/* write ID_CPE, single_element_channel() identifier */
BsPutBit(fixedStream,ID_CPE,LEN_SE_ID);
/* write the element_identifier_tag */
BsPutBit(fixedStream,tag,LEN_TAG); /* Currently, this is zero */
/* common_window? */
BsPutBit(fixedStream,commonWindow,LEN_COM_WIN);
}
bit_count += LEN_SE_ID;
bit_count += LEN_TAG;
bit_count += LEN_COM_WIN;
/* if common_window, write ics_info */
if (commonWindow) {
int numWindows,maxSfb;
bit_count = WriteICSInfo(quantInfoL,fixedStream,writeFlag);
numWindows=quantInfoL->num_window_groups;
maxSfb = quantInfoL->max_sfb;
if (writeFlag) {
BsPutBit(fixedStream,ms_info->is_present,LEN_MASK_PRES);
if (ms_info->is_present==1) {
int g;
int b;
for (g=0;g<numWindows;g++) {
for (b=0;b<maxSfb;b++) {
BsPutBit(fixedStream,ms_info->ms_used[g*maxSfb+b],LEN_MASK);
}
}
}
}
bit_count += LEN_MASK_PRES;
bit_count += (ms_info->is_present==1)*numWindows*maxSfb*LEN_MASK;
}
/* Write individual_channel_stream elements */
bit_count += WriteICS(quantInfoL,commonWindow,fixedStream,writeFlag);
bit_count += WriteICS(quantInfoR,commonWindow,fixedStream,writeFlag);
return bit_count;
}
/*****************************************************************************/
/* WriteICS(...), write an individual_channel_stream element to the bitstream.*/
/*****************************************************************************/
int WriteICS(AACQuantInfo* quantInfo, /* AACQuantInfo structure */
int commonWindow, /* Common window flag */
BsBitStream* fixed_stream, /* Pointer to bitstream */
int writeFlag) /* 1 means write, 0 means count only */
{
/* this function writes out an individual_channel_stream to the bitstream and */
/* returns the number of bits written to the bitstream */
int bit_count = 0;
int output_book_vector[SFB_NUM_MAX*2];
writeFlag = ( writeFlag != 0 );
/* Write the 8-bit global_gain */
BsPutBit(fixed_stream,quantInfo->common_scalefac,writeFlag*LEN_GLOB_GAIN);
bit_count += LEN_GLOB_GAIN;
/* Write ics information */
if (!commonWindow) {
bit_count += WriteICSInfo(quantInfo,fixed_stream,writeFlag);
}
/* Write section_data() information to the bitstream */
bit_count += sort_book_numbers(quantInfo,output_book_vector,fixed_stream,writeFlag);
/* Write scale_factor_data() information */
bit_count += write_scalefactor_bitstream(fixed_stream,writeFlag,quantInfo);
/* Write pulse_data() */
bit_count += WritePulseData(quantInfo,fixed_stream,writeFlag);
/* Write TNS data */
bit_count += WriteTNSData(quantInfo,fixed_stream,writeFlag);
/* Write gain control data */
bit_count += WriteGainControlData(quantInfo,fixed_stream,writeFlag);
/* Write out spectral_data() */
bit_count += WriteSpectralData(quantInfo,fixed_stream,writeFlag);
/* Return number of bits */
return(bit_count);
}
/*****************************************************************************/
/* WriteICSInfo(...), write individual_channel_stream information */
/* to the bitstream. */
/*****************************************************************************/
int WriteICSInfo(AACQuantInfo* quantInfo, /* AACQuantInfo structure */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -