📄 noiseless.c
字号:
/* ***** BEGIN LICENSE BLOCK *****
*
* Portions Copyright (c) 1995-2005 RealNetworks, Inc. All Rights Reserved.
*
* The contents of this file, and the files included with this file,
* are subject to the current version of the RealNetworks Public
* Source License (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the current version of the RealNetworks Community
* Source License (the "RCSL") available at
* http://www.helixcommunity.org/content/rcsl, in which case the RCSL
* will apply. You may also obtain the license terms directly from
* RealNetworks. You may not use this file except in compliance with
* the RPSL or, if you have a valid RCSL with RealNetworks applicable
* to this file, the RCSL. Please see the applicable RPSL or RCSL for
* the rights, obligations and limitations governing use of the
* contents of the file.
*
* This file is part of the Helix DNA Technology. RealNetworks is the
* developer of the Original Code and owns the copyrights in the
* portions it created.
*
* This file, and the files included with this file, is distributed
* and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
* ENJOYMENT OR NON-INFRINGEMENT.
*
* Technology Compatibility Kit Test Suite(s) Location:
* http://www.helixcommunity.org/content/tck
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
/**************************************************************************************
* Fixed-point HE-AAC decoder
* Jon Recker (jrecker@real.com)
* February 2005
*
* noiseless.c - decode channel info, scalefactors, quantized coefficients,
* scalefactor band codebook, and TNS coefficients from bitstream
**************************************************************************************/
#include "coder.h"
/**************************************************************************************
* Function: DecodeICSInfo
*
* Description: decode individual channel stream info
*
* Inputs: BitStreamInfo struct pointing to start of ICS info
* (14496-3, table 4.4.6)
* sample rate index
*
* Outputs: updated icsInfo struct
*
* Return: none
**************************************************************************************/
void DecodeICSInfo(BitStreamInfo *bsi, ICSInfo *icsInfo, int sampRateIdx)
{
int sfb, g, mask;
icsInfo->icsResBit = GetBits(bsi, 1);
icsInfo->winSequence = GetBits(bsi, 2);
icsInfo->winShape = GetBits(bsi, 1);
if (icsInfo->winSequence == 2) {
/* short block */
icsInfo->maxSFB = GetBits(bsi, 4);
icsInfo->sfGroup = GetBits(bsi, 7);
icsInfo->numWinGroup = 1;
icsInfo->winGroupLen[0] = 1;
mask = 0x40; /* start with bit 6 */
for (g = 0; g < 7; g++) {
if (icsInfo->sfGroup & mask) {
icsInfo->winGroupLen[icsInfo->numWinGroup - 1]++;
} else {
icsInfo->numWinGroup++;
icsInfo->winGroupLen[icsInfo->numWinGroup - 1] = 1;
}
mask >>= 1;
}
} else {
/* long block */
icsInfo->maxSFB = GetBits(bsi, 6);
icsInfo->predictorDataPresent = GetBits(bsi, 1);
if (icsInfo->predictorDataPresent) {
icsInfo->predictorReset = GetBits(bsi, 1);
if (icsInfo->predictorReset)
icsInfo->predictorResetGroupNum = GetBits(bsi, 5);
for (sfb = 0; sfb < MIN(icsInfo->maxSFB, predSFBMax[sampRateIdx]); sfb++)
icsInfo->predictionUsed[sfb] = GetBits(bsi, 1);
}
icsInfo->numWinGroup = 1;
icsInfo->winGroupLen[0] = 1;
}
}
/**************************************************************************************
* Function: DecodeSectionData
*
* Description: decode section data (scale factor band groupings and
* associated Huffman codebooks)
*
* Inputs: BitStreamInfo struct pointing to start of ICS info
* (14496-3, table 4.4.25)
* window sequence (short or long blocks)
* number of window groups (1 for long blocks, 1-8 for short blocks)
* max coded scalefactor band
*
* Outputs: index of Huffman codebook for each scalefactor band in each section
*
* Return: none
*
* Notes: sectCB, sectEnd, sfbCodeBook, ordered by window groups for short blocks
**************************************************************************************/
static void DecodeSectionData(BitStreamInfo *bsi, int winSequence, int numWinGrp, int maxSFB, unsigned char *sfbCodeBook)
{
int g, cb, sfb;
int sectLen, sectLenBits, sectLenIncr, sectEscapeVal;
sectLenBits = (winSequence == 2 ? 3 : 5);
sectEscapeVal = (1 << sectLenBits) - 1;
for (g = 0; g < numWinGrp; g++) {
sfb = 0;
while (sfb < maxSFB) {
cb = GetBits(bsi, 4); /* next section codebook */
sectLen = 0;
do {
sectLenIncr = GetBits(bsi, sectLenBits);
sectLen += sectLenIncr;
} while (sectLenIncr == sectEscapeVal);
sfb += sectLen;
while (sectLen--)
*sfbCodeBook++ = (unsigned char)cb;
}
ASSERT(sfb == maxSFB);
}
}
/**************************************************************************************
* Function: DecodeOneScaleFactor
*
* Description: decode one scalefactor using scalefactor Huffman codebook
*
* Inputs: BitStreamInfo struct pointing to start of next coded scalefactor
*
* Outputs: updated BitstreamInfo struct
*
* Return: one decoded scalefactor, including index_offset of -60
**************************************************************************************/
static int DecodeOneScaleFactor(BitStreamInfo *bsi)
{
int nBits, val;
unsigned int bitBuf;
/* decode next scalefactor from bitstream */
bitBuf = GetBitsNoAdvance(bsi, huffTabScaleFactInfo.maxBits) << (32 - huffTabScaleFactInfo.maxBits);
nBits = DecodeHuffmanScalar(huffTabScaleFact, &huffTabScaleFactInfo, bitBuf, &val);
AdvanceBitstream(bsi, nBits);
return val;
}
/**************************************************************************************
* Function: DecodeScaleFactors
*
* Description: decode scalefactors, PNS energy, and intensity stereo weights
*
* Inputs: BitStreamInfo struct pointing to start of ICS info
* (14496-3, table 4.4.26)
* number of window groups (1 for long blocks, 1-8 for short blocks)
* max coded scalefactor band
* global gain (starting value for differential scalefactor coding)
* index of Huffman codebook for each scalefactor band in each section
*
* Outputs: decoded scalefactor for each section
*
* Return: none
*
* Notes: sfbCodeBook, scaleFactors ordered by window groups for short blocks
* for section with codebook 13, scaleFactors buffer has decoded PNS
* energy instead of regular scalefactor
* for section with codebook 14 or 15, scaleFactors buffer has intensity
* stereo weight instead of regular scalefactor
**************************************************************************************/
static void DecodeScaleFactors(BitStreamInfo *bsi, int numWinGrp, int maxSFB, int globalGain,
unsigned char *sfbCodeBook, short *scaleFactors)
{
int g, sfbCB, nrg, npf, val, sf, is;
/* starting values for differential coding */
sf = globalGain;
is = 0;
nrg = globalGain - 90 - 256;
npf = 1;
for (g = 0; g < numWinGrp * maxSFB; g++) {
sfbCB = *sfbCodeBook++;
if (sfbCB == 14 || sfbCB == 15) {
/* intensity stereo - differential coding */
val = DecodeOneScaleFactor(bsi);
is += val;
*scaleFactors++ = (short)is;
} else if (sfbCB == 13) {
/* PNS - first energy is directly coded, rest are Huffman coded (npf = noise_pcm_flag) */
if (npf) {
val = GetBits(bsi, 9);
npf = 0;
} else {
val = DecodeOneScaleFactor(bsi);
}
nrg += val;
*scaleFactors++ = (short)nrg;
} else if (sfbCB >= 1 && sfbCB <= 11) {
/* regular (non-zero) region - differential coding */
val = DecodeOneScaleFactor(bsi);
sf += val;
*scaleFactors++ = (short)sf;
} else {
/* inactive scalefactor band if codebook 0 */
*scaleFactors++ = 0;
}
}
}
/**************************************************************************************
* Function: DecodePulseInfo
*
* Description: decode pulse information
*
* Inputs: BitStreamInfo struct pointing to start of pulse info
* (14496-3, table 4.4.7)
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -