summaryrefslogtreecommitdiff
path: root/audio_codec/libfaad/helixaac/sbrhuff.c (plain)
blob: 35e759f61d2926fda7087979b693819204c4afcb
1/* ***** BEGIN LICENSE BLOCK *****
2 * Source last modified: $Id: sbrhuff.c,v 1.1 2005/02/26 01:47:35 jrecker Exp $
3 *
4 * Portions Copyright (c) 1995-2005 RealNetworks, Inc. All Rights Reserved.
5 *
6 * The contents of this file, and the files included with this file,
7 * are subject to the current version of the RealNetworks Public
8 * Source License (the "RPSL") available at
9 * http://www.helixcommunity.org/content/rpsl unless you have licensed
10 * the file under the current version of the RealNetworks Community
11 * Source License (the "RCSL") available at
12 * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
13 * will apply. You may also obtain the license terms directly from
14 * RealNetworks. You may not use this file except in compliance with
15 * the RPSL or, if you have a valid RCSL with RealNetworks applicable
16 * to this file, the RCSL. Please see the applicable RPSL or RCSL for
17 * the rights, obligations and limitations governing use of the
18 * contents of the file.
19 *
20 * This file is part of the Helix DNA Technology. RealNetworks is the
21 * developer of the Original Code and owns the copyrights in the
22 * portions it created.
23 *
24 * This file, and the files included with this file, is distributed
25 * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
26 * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
27 * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
28 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
29 * ENJOYMENT OR NON-INFRINGEMENT.
30 *
31 * Technology Compatibility Kit Test Suite(s) Location:
32 * http://www.helixcommunity.org/content/tck
33 *
34 * Contributor(s):
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/**************************************************************************************
39 * Fixed-point HE-AAC decoder
40 * Jon Recker (jrecker@real.com)
41 * February 2005
42 *
43 * sbrhuff.c - functions for unpacking Huffman-coded envelope and noise data
44 **************************************************************************************/
45
46#include "sbr.h"
47#include "assembly.h"
48
49/**************************************************************************************
50 * Function: DecodeHuffmanScalar
51 *
52 * Description: decode one Huffman symbol from bitstream
53 *
54 * Inputs: pointers to Huffman table and info struct
55 * left-aligned bit buffer with >= huffTabInfo->maxBits bits
56 *
57 * Outputs: decoded symbol in *val
58 *
59 * Return: number of bits in symbol
60 *
61 * Notes: assumes canonical Huffman codes:
62 * first CW always 0, we have "count" CW's of length "nBits" bits
63 * starting CW for codes of length nBits+1 =
64 * (startCW[nBits] + count[nBits]) << 1
65 * if there are no codes at nBits, then we just keep << 1 each time
66 * (since count[nBits] = 0)
67 **************************************************************************************/
68static int DecodeHuffmanScalar(const signed short *huffTab, const HuffInfo *huffTabInfo, unsigned int bitBuf, signed int *val)
69{
70 unsigned int count, start, shift, t;
71 const unsigned char *countPtr;
72 const signed short *map;
73
74 map = huffTab + huffTabInfo->offset;
75 countPtr = huffTabInfo->count;
76
77 start = 0;
78 count = 0;
79 shift = 32;
80 do {
81 start += count;
82 start <<= 1;
83 map += count;
84 count = *countPtr++;
85 shift--;
86 t = (bitBuf >> shift) - start;
87 } while (t >= count);
88
89 *val = (signed int)map[t];
90 return (countPtr - huffTabInfo->count);
91}
92
93/**************************************************************************************
94 * Function: DecodeOneSymbol
95 *
96 * Description: dequantize one Huffman symbol from bitstream,
97 * using table huffTabSBR[huffTabIndex]
98 *
99 * Inputs: BitStreamInfo struct pointing to start of next Huffman codeword
100 * index of Huffman table
101 *
102 * Outputs: bitstream advanced by number of bits in codeword
103 *
104 * Return: one decoded symbol
105 **************************************************************************************/
106static int DecodeOneSymbol(BitStreamInfo *bsi, int huffTabIndex)
107{
108 int nBits, val;
109 unsigned int bitBuf;
110 const HuffInfo *hi;
111
112 hi = &(huffTabSBRInfo[huffTabIndex]);
113
114 bitBuf = GetBitsNoAdvance(bsi, hi->maxBits) << (32 - hi->maxBits);
115 nBits = DecodeHuffmanScalar(huffTabSBR, hi, bitBuf, &val);
116 AdvanceBitstream(bsi, nBits);
117
118 return val;
119}
120
121/* [1.0, sqrt(2)], format = Q29 (one guard bit for decoupling) */
122static const int envDQTab[2] = {0x20000000, 0x2d413ccc};
123
124/**************************************************************************************
125 * Function: DequantizeEnvelope
126 *
127 * Description: dequantize envelope scalefactors
128 *
129 * Inputs: number of scalefactors to process
130 * amplitude resolution flag for this frame (0 or 1)
131 * quantized envelope scalefactors
132 *
133 * Outputs: dequantized envelope scalefactors
134 *
135 * Return: extra int bits in output (6 + expMax)
136 * in other words, output format = Q(FBITS_OUT_DQ_ENV - (6 + expMax))
137 *
138 * Notes: dequantized scalefactors have at least 2 GB
139 **************************************************************************************/
140static int DequantizeEnvelope(int nBands, int ampRes, signed char *envQuant, int *envDequant)
141{
142 int exp, expMax, i, scalei;
143
144 if (nBands <= 0) {
145 return 0;
146 }
147
148 /* scan for largest dequant value (do separately from envelope decoding to keep code cleaner) */
149 expMax = 0;
150 for (i = 0; i < nBands; i++) {
151 if (envQuant[i] > expMax) {
152 expMax = envQuant[i];
153 }
154 }
155
156 /* dequantized envelope gains
157 * envDequant = 64*2^(envQuant / alpha) = 2^(6 + envQuant / alpha)
158 * if ampRes == 0, alpha = 2 and range of envQuant = [0, 127]
159 * if ampRes == 1, alpha = 1 and range of envQuant = [0, 63]
160 * also if coupling is on, envDequant is scaled by something in range [0, 2]
161 * so range of envDequant = [2^6, 2^69] (no coupling), [2^6, 2^70] (with coupling)
162 *
163 * typical range (from observation) of envQuant/alpha = [0, 27] --> largest envQuant ~= 2^33
164 * output: Q(29 - (6 + expMax))
165 *
166 * reference: 14496-3:2001(E)/4.6.18.3.5 and 14496-4:200X/FPDAM8/5.6.5.1.2.1.5
167 */
168 if (ampRes) {
169 do {
170 exp = *envQuant++;
171 scalei = MIN(expMax - exp, 31);
172 *envDequant++ = envDQTab[0] >> scalei;
173 } while (--nBands);
174
175 return (6 + expMax);
176 } else {
177 expMax >>= 1;
178 do {
179 exp = *envQuant++;
180 scalei = MIN(expMax - (exp >> 1), 31);
181 *envDequant++ = envDQTab[exp & 0x01] >> scalei;
182 } while (--nBands);
183
184 return (6 + expMax);
185 }
186
187}
188
189/**************************************************************************************
190 * Function: DequantizeNoise
191 *
192 * Description: dequantize noise scalefactors
193 *
194 * Inputs: number of scalefactors to process
195 * quantized noise scalefactors
196 *
197 * Outputs: dequantized noise scalefactors, format = Q(FBITS_OUT_DQ_NOISE)
198 *
199 * Return: none
200 *
201 * Notes: dequantized scalefactors have at least 2 GB
202 **************************************************************************************/
203static void DequantizeNoise(int nBands, signed char *noiseQuant, int *noiseDequant)
204{
205 int exp, scalei;
206
207 if (nBands <= 0) {
208 return;
209 }
210
211 /* dequantize noise floor gains (4.6.18.3.5):
212 * noiseDequant = 2^(NOISE_FLOOR_OFFSET - noiseQuant)
213 *
214 * range of noiseQuant = [0, 30] (see 4.6.18.3.6), NOISE_FLOOR_OFFSET = 6
215 * so range of noiseDequant = [2^-24, 2^6]
216 */
217 do {
218 exp = *noiseQuant++;
219 scalei = NOISE_FLOOR_OFFSET - exp + FBITS_OUT_DQ_NOISE; /* 6 + 24 - exp, exp = [0,30] */
220
221 if (scalei < 0) {
222 *noiseDequant++ = 0;
223 } else if (scalei < 30) {
224 *noiseDequant++ = 1 << scalei;
225 } else {
226 *noiseDequant++ = 0x3fffffff; /* leave 2 GB */
227 }
228
229 } while (--nBands);
230}
231
232/**************************************************************************************
233 * Function: DecodeSBREnvelope
234 *
235 * Description: decode delta Huffman coded envelope scalefactors from bitstream
236 *
237 * Inputs: BitStreamInfo struct pointing to start of env data
238 * initialized PSInfoSBR struct
239 * initialized SBRGrid struct for this channel
240 * initialized SBRFreq struct for this SCE/CPE block
241 * initialized SBRChan struct for this channel
242 * index of current channel (0 for SCE, 0 or 1 for CPE)
243 *
244 * Outputs: dequantized env scalefactors for left channel (before decoupling)
245 * dequantized env scalefactors for right channel (if coupling off)
246 * or raw decoded env scalefactors for right channel (if coupling on)
247 *
248 * Return: none
249 **************************************************************************************/
250int DecodeSBREnvelope(BitStreamInfo *bsi, PSInfoSBR *psi, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChan, int ch)
251{
252 int huffIndexTime, huffIndexFreq, env, envStartBits, band, nBands, sf, lastEnv;
253 int freqRes, freqResPrev, dShift, i;
254
255 if (psi->couplingFlag && ch) {
256 dShift = 1;
257 if (sbrGrid->ampResFrame) {
258 huffIndexTime = HuffTabSBR_tEnv30b;
259 huffIndexFreq = HuffTabSBR_fEnv30b;
260 envStartBits = 5;
261 } else {
262 huffIndexTime = HuffTabSBR_tEnv15b;
263 huffIndexFreq = HuffTabSBR_fEnv15b;
264 envStartBits = 6;
265 }
266 } else {
267 dShift = 0;
268 if (sbrGrid->ampResFrame) {
269 huffIndexTime = HuffTabSBR_tEnv30;
270 huffIndexFreq = HuffTabSBR_fEnv30;
271 envStartBits = 6;
272 } else {
273 huffIndexTime = HuffTabSBR_tEnv15;
274 huffIndexFreq = HuffTabSBR_fEnv15;
275 envStartBits = 7;
276 }
277 }
278
279 /* range of envDataQuant[] = [0, 127] (see comments in DequantizeEnvelope() for reference) */
280 for (env = 0; env < sbrGrid->numEnv; env++) {
281 nBands = (sbrGrid->freqRes[env] ? sbrFreq->nHigh : sbrFreq->nLow);
282 freqRes = (sbrGrid->freqRes[env]);
283 freqResPrev = (env == 0 ? sbrGrid->freqResPrev : sbrGrid->freqRes[env - 1]);
284 lastEnv = (env == 0 ? sbrGrid->numEnvPrev - 1 : env - 1);
285 if (lastEnv < 0) {
286 lastEnv = 0; /* first frame */
287 }
288
289 ASSERT(nBands <= MAX_QMF_BANDS, ERR_AAC_SBR_BITSTREAM);
290
291 if (sbrChan->deltaFlagEnv[env] == 0) {
292 /* delta coding in freq */
293 sf = GetBits(bsi, envStartBits) << dShift;
294 sbrChan->envDataQuant[env][0] = sf;
295 for (band = 1; band < nBands; band++) {
296 sf = DecodeOneSymbol(bsi, huffIndexFreq) << dShift;
297 sbrChan->envDataQuant[env][band] = sf + sbrChan->envDataQuant[env][band - 1];
298 }
299 } else if (freqRes == freqResPrev) {
300 /* delta coding in time - same freq resolution for both frames */
301 for (band = 0; band < nBands; band++) {
302 sf = DecodeOneSymbol(bsi, huffIndexTime) << dShift;
303 sbrChan->envDataQuant[env][band] = sf + sbrChan->envDataQuant[lastEnv][band];
304 }
305 } else if (freqRes == 0 && freqResPrev == 1) {
306 /* delta coding in time - low freq resolution for new frame, high freq resolution for old frame */
307 for (band = 0; band < nBands; band++) {
308 sf = DecodeOneSymbol(bsi, huffIndexTime) << dShift;
309 sbrChan->envDataQuant[env][band] = sf;
310 for (i = 0; i < sbrFreq->nHigh; i++) {
311 if (sbrFreq->freqHigh[i] == sbrFreq->freqLow[band]) {
312 sbrChan->envDataQuant[env][band] += sbrChan->envDataQuant[lastEnv][i];
313 break;
314 }
315 }
316 }
317 } else if (freqRes == 1 && freqResPrev == 0) {
318 /* delta coding in time - high freq resolution for new frame, low freq resolution for old frame */
319 for (band = 0; band < nBands; band++) {
320 sf = DecodeOneSymbol(bsi, huffIndexTime) << dShift;
321 sbrChan->envDataQuant[env][band] = sf;
322 for (i = 0; i < sbrFreq->nLow; i++) {
323 if (sbrFreq->freqLow[i] <= sbrFreq->freqHigh[band] && sbrFreq->freqHigh[band] < sbrFreq->freqLow[i + 1]) {
324 sbrChan->envDataQuant[env][band] += sbrChan->envDataQuant[lastEnv][i];
325 break;
326 }
327 }
328 }
329 }
330
331 /* skip coupling channel */
332 if (ch != 1 || psi->couplingFlag != 1) {
333 psi->envDataDequantScale[ch][env] = DequantizeEnvelope(nBands, sbrGrid->ampResFrame, sbrChan->envDataQuant[env], psi->envDataDequant[ch][env]);
334 }
335 }
336 sbrGrid->numEnvPrev = sbrGrid->numEnv;
337 sbrGrid->freqResPrev = sbrGrid->freqRes[sbrGrid->numEnv - 1];
338 return ERR_AAC_NONE;
339}
340
341/**************************************************************************************
342 * Function: DecodeSBRNoise
343 *
344 * Description: decode delta Huffman coded noise scalefactors from bitstream
345 *
346 * Inputs: BitStreamInfo struct pointing to start of noise data
347 * initialized PSInfoSBR struct
348 * initialized SBRGrid struct for this channel
349 * initialized SBRFreq struct for this SCE/CPE block
350 * initialized SBRChan struct for this channel
351 * index of current channel (0 for SCE, 0 or 1 for CPE)
352 *
353 * Outputs: dequantized noise scalefactors for left channel (before decoupling)
354 * dequantized noise scalefactors for right channel (if coupling off)
355 * or raw decoded noise scalefactors for right channel (if coupling on)
356 *
357 * Return: none
358 **************************************************************************************/
359int DecodeSBRNoise(BitStreamInfo *bsi, PSInfoSBR *psi, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChan, int ch)
360{
361 int huffIndexTime, huffIndexFreq, noiseFloor, band, dShift, sf, lastNoiseFloor;
362
363 if (psi->couplingFlag && ch) {
364 dShift = 1;
365 huffIndexTime = HuffTabSBR_tNoise30b;
366 huffIndexFreq = HuffTabSBR_fNoise30b;
367 } else {
368 dShift = 0;
369 huffIndexTime = HuffTabSBR_tNoise30;
370 huffIndexFreq = HuffTabSBR_fNoise30;
371 }
372
373 for (noiseFloor = 0; noiseFloor < sbrGrid->numNoiseFloors; noiseFloor++) {
374 lastNoiseFloor = (noiseFloor == 0 ? sbrGrid->numNoiseFloorsPrev - 1 : noiseFloor - 1);
375 if (lastNoiseFloor < 0) {
376 lastNoiseFloor = 0; /* first frame */
377 }
378
379 ASSERT(sbrFreq->numNoiseFloorBands <= MAX_QMF_BANDS, ERR_AAC_SBR_BITSTREAM);
380
381 if (sbrChan->deltaFlagNoise[noiseFloor] == 0) {
382 /* delta coding in freq */
383 sbrChan->noiseDataQuant[noiseFloor][0] = GetBits(bsi, 5) << dShift;
384 for (band = 1; band < sbrFreq->numNoiseFloorBands; band++) {
385 sf = DecodeOneSymbol(bsi, huffIndexFreq) << dShift;
386 sbrChan->noiseDataQuant[noiseFloor][band] = sf + sbrChan->noiseDataQuant[noiseFloor][band - 1];
387 }
388 } else {
389 /* delta coding in time */
390 for (band = 0; band < sbrFreq->numNoiseFloorBands; band++) {
391 sf = DecodeOneSymbol(bsi, huffIndexTime) << dShift;
392 sbrChan->noiseDataQuant[noiseFloor][band] = sf + sbrChan->noiseDataQuant[lastNoiseFloor][band];
393 }
394 }
395
396 /* skip coupling channel */
397 if (ch != 1 || psi->couplingFlag != 1) {
398 DequantizeNoise(sbrFreq->numNoiseFloorBands, sbrChan->noiseDataQuant[noiseFloor], psi->noiseDataDequant[ch][noiseFloor]);
399 }
400 }
401 sbrGrid->numNoiseFloorsPrev = sbrGrid->numNoiseFloors;
402 return ERR_AAC_NONE;
403}
404
405/* dqTabCouple[i] = 2 / (1 + 2^(12 - i)), format = Q30 */
406static const int dqTabCouple[25] = {
407 0x0007ff80, 0x000ffe00, 0x001ff802, 0x003fe010, 0x007f8080, 0x00fe03f8, 0x01f81f82, 0x03e0f83e,
408 0x07878788, 0x0e38e38e, 0x1999999a, 0x2aaaaaab, 0x40000000, 0x55555555, 0x66666666, 0x71c71c72,
409 0x78787878, 0x7c1f07c2, 0x7e07e07e, 0x7f01fc08, 0x7f807f80, 0x7fc01ff0, 0x7fe007fe, 0x7ff00200,
410 0x7ff80080,
411};
412
413/**************************************************************************************
414 * Function: UncoupleSBREnvelope
415 *
416 * Description: scale dequantized envelope scalefactors according to channel
417 * coupling rules
418 *
419 * Inputs: initialized PSInfoSBR struct including
420 * dequantized envelope data for left channel
421 * initialized SBRGrid struct for this channel
422 * initialized SBRFreq struct for this SCE/CPE block
423 * initialized SBRChan struct for right channel including
424 * quantized envelope scalefactors
425 *
426 * Outputs: dequantized envelope data for left channel (after decoupling)
427 * dequantized envelope data for right channel (after decoupling)
428 *
429 * Return: none
430 **************************************************************************************/
431void UncoupleSBREnvelope(PSInfoSBR *psi, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChanR)
432{
433 int env, band, nBands, scalei, E_1;
434
435 scalei = (sbrGrid->ampResFrame ? 0 : 1);
436 for (env = 0; env < sbrGrid->numEnv; env++) {
437 nBands = (sbrGrid->freqRes[env] ? sbrFreq->nHigh : sbrFreq->nLow);
438 psi->envDataDequantScale[1][env] = psi->envDataDequantScale[0][env]; /* same scalefactor for L and R */
439 for (band = 0; band < nBands; band++) {
440 /* clip E_1 to [0, 24] (scalefactors approach 0 or 2) */
441 E_1 = sbrChanR->envDataQuant[env][band] >> scalei;
442 if (E_1 < 0) {
443 E_1 = 0;
444 }
445 if (E_1 > 24) {
446 E_1 = 24;
447 }
448
449 /* envDataDequant[0] has 1 GB, so << by 2 is okay */
450 psi->envDataDequant[1][env][band] = MULSHIFT32(psi->envDataDequant[0][env][band], dqTabCouple[24 - E_1]) << 2;
451 psi->envDataDequant[0][env][band] = MULSHIFT32(psi->envDataDequant[0][env][band], dqTabCouple[E_1]) << 2;
452 }
453 }
454}
455
456/**************************************************************************************
457 * Function: UncoupleSBRNoise
458 *
459 * Description: scale dequantized noise floor scalefactors according to channel
460 * coupling rules
461 *
462 * Inputs: initialized PSInfoSBR struct including
463 * dequantized noise data for left channel
464 * initialized SBRGrid struct for this channel
465 * initialized SBRFreq struct for this SCE/CPE block
466 * initialized SBRChan struct for this channel including
467 * quantized noise scalefactors
468 *
469 * Outputs: dequantized noise data for left channel (after decoupling)
470 * dequantized noise data for right channel (after decoupling)
471 *
472 * Return: none
473 **************************************************************************************/
474void UncoupleSBRNoise(PSInfoSBR *psi, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChanR)
475{
476 int noiseFloor, band, Q_1;
477
478 for (noiseFloor = 0; noiseFloor < sbrGrid->numNoiseFloors; noiseFloor++) {
479 for (band = 0; band < sbrFreq->numNoiseFloorBands; band++) {
480 /* Q_1 should be in range [0, 24] according to 4.6.18.3.6, but check to make sure */
481 Q_1 = sbrChanR->noiseDataQuant[noiseFloor][band];
482 if (Q_1 < 0) {
483 Q_1 = 0;
484 }
485 if (Q_1 > 24) {
486 Q_1 = 24;
487 }
488
489 /* noiseDataDequant[0] has 1 GB, so << by 2 is okay */
490 psi->noiseDataDequant[1][noiseFloor][band] = MULSHIFT32(psi->noiseDataDequant[0][noiseFloor][band], dqTabCouple[24 - Q_1]) << 2;
491 psi->noiseDataDequant[0][noiseFloor][band] = MULSHIFT32(psi->noiseDataDequant[0][noiseFloor][band], dqTabCouple[Q_1]) << 2;
492 }
493 }
494}
495