📄 sbrhuff.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
*
* sbrhuff.c - functions for unpacking Huffman-coded envelope and noise data
**************************************************************************************/
#include "sbr.h"
#include "assembly.h"
/**************************************************************************************
* Function: DecodeHuffmanScalar
*
* Description: decode one Huffman symbol from bitstream
*
* Inputs: pointers to Huffman table and info struct
* left-aligned bit buffer with >= huffTabInfo->maxBits bits
*
* Outputs: decoded symbol in *val
*
* Return: number of bits in symbol
*
* Notes: assumes canonical Huffman codes:
* first CW always 0, we have "count" CW's of length "nBits" bits
* starting CW for codes of length nBits+1 =
* (startCW[nBits] + count[nBits]) << 1
* if there are no codes at nBits, then we just keep << 1 each time
* (since count[nBits] = 0)
**************************************************************************************/
static int DecodeHuffmanScalar(const signed short *huffTab, const HuffInfo *huffTabInfo, unsigned int bitBuf, signed int *val)
{
unsigned int count, start, shift, t;
const unsigned char *countPtr;
const signed short *map;
map = huffTab + huffTabInfo->offset;
countPtr = huffTabInfo->count;
start = 0;
count = 0;
shift = 32;
do {
start += count;
start <<= 1;
map += count;
count = *countPtr++;
shift--;
t = (bitBuf >> shift) - start;
} while (t >= count);
*val = (signed int)map[t];
return (countPtr - huffTabInfo->count);
}
/**************************************************************************************
* Function: DecodeOneSymbol
*
* Description: dequantize one Huffman symbol from bitstream,
* using table huffTabSBR[huffTabIndex]
*
* Inputs: BitStreamInfo struct pointing to start of next Huffman codeword
* index of Huffman table
*
* Outputs: bitstream advanced by number of bits in codeword
*
* Return: one decoded symbol
**************************************************************************************/
static int DecodeOneSymbol(BitStreamInfo *bsi, int huffTabIndex)
{
int nBits, val;
unsigned int bitBuf;
const HuffInfo *hi;
hi = &(huffTabSBRInfo[huffTabIndex]);
bitBuf = GetBitsNoAdvance(bsi, hi->maxBits) << (32 - hi->maxBits);
nBits = DecodeHuffmanScalar(huffTabSBR, hi, bitBuf, &val);
AdvanceBitstream(bsi, nBits);
return val;
}
/* [1.0, sqrt(2)], format = Q29 (one guard bit for decoupling) */
static const int envDQTab[2] = {0x20000000, 0x2d413ccc};
/**************************************************************************************
* Function: DequantizeEnvelope
*
* Description: dequantize envelope scalefactors
*
* Inputs: number of scalefactors to process
* amplitude resolution flag for this frame (0 or 1)
* quantized envelope scalefactors
*
* Outputs: dequantized envelope scalefactors
*
* Return: extra int bits in output (6 + expMax)
* in other words, output format = Q(FBITS_OUT_DQ_ENV - (6 + expMax))
*
* Notes: dequantized scalefactors have at least 2 GB
**************************************************************************************/
static int DequantizeEnvelope(int nBands, int ampRes, signed char *envQuant, int *envDequant)
{
int exp, expMax, i, scalei;
if (nBands <= 0)
return 0;
/* scan for largest dequant value (do separately from envelope decoding to keep code cleaner) */
expMax = 0;
for (i = 0; i < nBands; i++) {
if (envQuant[i] > expMax)
expMax = envQuant[i];
}
/* dequantized envelope gains
* envDequant = 64*2^(envQuant / alpha) = 2^(6 + envQuant / alpha)
* if ampRes == 0, alpha = 2 and range of envQuant = [0, 127]
* if ampRes == 1, alpha = 1 and range of envQuant = [0, 63]
* also if coupling is on, envDequant is scaled by something in range [0, 2]
* so range of envDequant = [2^6, 2^69] (no coupling), [2^6, 2^70] (with coupling)
*
* typical range (from observation) of envQuant/alpha = [0, 27] --> largest envQuant ~= 2^33
* output: Q(29 - (6 + expMax))
*
* reference: 14496-3:2001(E)/4.6.18.3.5 and 14496-4:200X/FPDAM8/5.6.5.1.2.1.5
*/
if (ampRes) {
do {
exp = *envQuant++;
scalei = MIN(expMax - exp, 31);
*envDequant++ = envDQTab[0] >> scalei;
} while (--nBands);
return (6 + expMax);
} else {
expMax >>= 1;
do {
exp = *envQuant++;
scalei = MIN(expMax - (exp >> 1), 31);
*envDequant++ = envDQTab[exp & 0x01] >> scalei;
} while (--nBands);
return (6 + expMax);
}
}
/**************************************************************************************
* Function: DequantizeNoise
*
* Description: dequantize noise scalefactors
*
* Inputs: number of scalefactors to process
* quantized noise scalefactors
*
* Outputs: dequantized noise scalefactors, format = Q(FBITS_OUT_DQ_NOISE)
*
* Return: none
*
* Notes: dequantized scalefactors have at least 2 GB
**************************************************************************************/
static void DequantizeNoise(int nBands, signed char *noiseQuant, int *noiseDequant)
{
int exp, scalei;
if (nBands <= 0)
return;
/* dequantize noise floor gains (4.6.18.3.5):
* noiseDequant = 2^(NOISE_FLOOR_OFFSET - noiseQuant)
*
* range of noiseQuant = [0, 30] (see 4.6.18.3.6), NOISE_FLOOR_OFFSET = 6
* so range of noiseDequant = [2^-24, 2^6]
*/
do {
exp = *noiseQuant++;
scalei = NOISE_FLOOR_OFFSET - exp + FBITS_OUT_DQ_NOISE; /* 6 + 24 - exp, exp = [0,30] */
if (scalei < 0)
*noiseDequant++ = 0;
else if (scalei < 30)
*noiseDequant++ = 1 << scalei;
else
*noiseDequant++ = 0x3fffffff; /* leave 2 GB */
} while (--nBands);
}
/**************************************************************************************
* Function: DecodeSBREnvelope
*
* Description: decode delta Huffman coded envelope scalefactors from bitstream
*
* Inputs: BitStreamInfo struct pointing to start of env data
* initialized PSInfoSBR struct
* initialized SBRGrid struct for this channel
* initialized SBRFreq struct for this SCE/CPE block
* initialized SBRChan struct for this channel
* index of current channel (0 for SCE, 0 or 1 for CPE)
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -