summaryrefslogtreecommitdiff
path: root/audio_codec/libraac/dequant.c (plain)
blob: e60517af4c3fa038fb98a39494838e498d5c2f22
1/* ***** BEGIN LICENSE BLOCK *****
2 * Source last modified: $Id: dequant.c,v 1.2 2005/05/20 18:05:41 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), Ken Cooke (kenc@real.com)
41 * February 2005
42 *
43 * dequant.c - transform coefficient dequantization and short-block deinterleaving
44 **************************************************************************************/
45
46#include "coder.h"
47
48#include "assembly.h"
49
50//#include <core/dsp.h>
51#include <stdio.h>
52
53
54#define SF_OFFSET 100
55
56/* pow(2, i/4.0) for i = [0,1,2,3], format = Q30 */
57static const int pow14[4] = {
58 0x40000000, 0x4c1bf829, 0x5a82799a, 0x6ba27e65
59};
60
61/* pow(2, i/4.0) * pow(j, 4.0/3.0) for i = [0,1,2,3], j = [0,1,2,...,15]
62 * format = Q28 for j = [0-3], Q25 for j = [4-15]
63 */
64static const int pow43_14[4][16] = {
65 {
66 0x00000000, 0x10000000, 0x285145f3, 0x453a5cdb, /* Q28 */
67 0x0cb2ff53, 0x111989d6, 0x15ce31c8, 0x1ac7f203, /* Q25 */
68 0x20000000, 0x257106b9, 0x2b16b4a3, 0x30ed74b4, /* Q25 */
69 0x36f23fa5, 0x3d227bd3, 0x437be656, 0x49fc823c, /* Q25 */
70 },
71 {
72 0x00000000, 0x1306fe0a, 0x2ff221af, 0x52538f52,
73 0x0f1a1bf4, 0x1455ccc2, 0x19ee62a8, 0x1fd92396,
74 0x260dfc14, 0x2c8694d8, 0x333dcb29, 0x3a2f5c7a,
75 0x4157aed5, 0x48b3aaa3, 0x50409f76, 0x57fc3010,
76 },
77 {
78 0x00000000, 0x16a09e66, 0x39047c0f, 0x61e734aa,
79 0x11f59ac4, 0x182ec633, 0x1ed66a45, 0x25dfc55a,
80 0x2d413ccd, 0x34f3462d, 0x3cefc603, 0x4531ab69,
81 0x4db4adf8, 0x56752054, 0x5f6fcfcd, 0x68a1eca1,
82 },
83 {
84 0x00000000, 0x1ae89f99, 0x43ce3e4b, 0x746d57b2,
85 0x155b8109, 0x1cc21cdc, 0x24ac1839, 0x2d0a479e,
86 0x35d13f33, 0x3ef80748, 0x48775c93, 0x524938cd,
87 0x5c68841d, 0x66d0df0a, 0x717e7bfe, 0x7c6e0305,
88 },
89};
90
91/* pow(j, 4.0 / 3.0) for j = [16,17,18,...,63], format = Q23 */
92static const int pow43[48] = {
93 0x1428a2fa, 0x15db1bd6, 0x1796302c, 0x19598d85,
94 0x1b24e8bb, 0x1cf7fcfa, 0x1ed28af2, 0x20b4582a,
95 0x229d2e6e, 0x248cdb55, 0x26832fda, 0x28800000,
96 0x2a832287, 0x2c8c70a8, 0x2e9bc5d8, 0x30b0ff99,
97 0x32cbfd4a, 0x34eca001, 0x3712ca62, 0x393e6088,
98 0x3b6f47e0, 0x3da56717, 0x3fe0a5fc, 0x4220ed72,
99 0x44662758, 0x46b03e7c, 0x48ff1e87, 0x4b52b3f3,
100 0x4daaebfd, 0x5007b497, 0x5268fc62, 0x54ceb29c,
101 0x5738c721, 0x59a72a59, 0x5c19cd35, 0x5e90a129,
102 0x610b9821, 0x638aa47f, 0x660db90f, 0x6894c90b,
103 0x6b1fc80c, 0x6daeaa0d, 0x70416360, 0x72d7e8b0,
104 0x75722ef9, 0x78102b85, 0x7ab1d3ec, 0x7d571e09,
105};
106
107/* sqrt(0.5), format = Q31 */
108#define SQRTHALF 0x5a82799a
109
110/* Minimax polynomial approximation to pow(x, 4/3), over the range
111 * poly43lo: x = [0.5, 0.7071]
112 * poly43hi: x = [0.7071, 1.0]
113 *
114 * Relative error < 1E-7
115 * Coefs are scaled by 4, 2, 1, 0.5, 0.25
116 */
117static const int poly43lo[5] = { 0x29a0bda9, 0xb02e4828, 0x5957aa1b, 0x236c498d, 0xff581859 };
118static const int poly43hi[5] = { 0x10852163, 0xd333f6a4, 0x46e9408b, 0x27c2cef0, 0xfef577b4 };
119
120/* pow2exp[i] = pow(2, i*4/3) exponent */
121static const int pow2exp[8] = { 14, 13, 11, 10, 9, 7, 6, 5 };
122
123/* pow2exp[i] = pow(2, i*4/3) fraction */
124static const int pow2frac[8] = {
125 0x6597fa94, 0x50a28be6, 0x7fffffff, 0x6597fa94,
126 0x50a28be6, 0x7fffffff, 0x6597fa94, 0x50a28be6
127};
128
129/**************************************************************************************
130 * Function: DequantBlock
131 *
132 * Description: dequantize one block of transform coefficients (in-place)
133 *
134 * Inputs: quantized transform coefficients, range = [0, 8191]
135 * number of samples to dequantize
136 * scalefactor for this block of data, range = [0, 256]
137 *
138 * Outputs: dequantized transform coefficients in Q(FBITS_OUT_DQ_OFF)
139 *
140 * Return: guard bit mask (OR of abs value of all dequantized coefs)
141 *
142 * Notes: applies dequant formula y = pow(x, 4.0/3.0) * pow(2, (scale - 100)/4.0)
143 * * pow(2, FBITS_OUT_DQ_OFF)
144 * clips outputs to Q(FBITS_OUT_DQ_OFF)
145 * output has no minimum number of guard bits
146 **************************************************************************************/
147static int DequantBlock(int *inbuf, int nSamps, int scale)
148{
149 int iSamp, scalef, scalei, x, y, gbMask, shift, tab4[4];
150 const int *tab16, *coef;
151
152 if (nSamps <= 0) {
153 return 0;
154 }
155
156 scale -= SF_OFFSET; /* new range = [-100, 156] */
157
158 /* with two's complement numbers, scalei/scalef factorization works for pos and neg values of scale:
159 * [+4...+7] >> 2 = +1, [ 0...+3] >> 2 = 0, [-4...-1] >> 2 = -1, [-8...-5] >> 2 = -2 ...
160 * (-1 & 0x3) = 3, (-2 & 0x3) = 2, (-3 & 0x3) = 1, (0 & 0x3) = 0
161 *
162 * Example: 2^(-5/4) = 2^(-1) * 2^(-1/4) = 2^-2 * 2^(3/4)
163 */
164 tab16 = pow43_14[scale & 0x3];
165 scalef = pow14[scale & 0x3];
166 scalei = (scale >> 2) + FBITS_OUT_DQ_OFF;
167
168 /* cache first 4 values:
169 * tab16[j] = Q28 for j = [0,3]
170 * tab4[x] = x^(4.0/3.0) * 2^(0.25*scale), Q(FBITS_OUT_DQ_OFF)
171 */
172 shift = 28 - scalei;
173 if (shift > 31) {
174 tab4[0] = tab4[1] = tab4[2] = tab4[3] = 0;
175 } else if (shift <= 0) {
176 shift = -shift;
177 if (shift > 31) {
178 shift = 31;
179 }
180 for (x = 0; x < 4; x++) {
181 y = tab16[x];
182 if (y > (0x7fffffff >> shift)) {
183 y = 0x7fffffff; /* clip (rare) */
184 } else {
185 y <<= shift;
186 }
187 tab4[x] = y;
188 }
189 } else {
190 tab4[0] = 0;
191 tab4[1] = tab16[1] >> shift;
192 tab4[2] = tab16[2] >> shift;
193 tab4[3] = tab16[3] >> shift;
194 }
195
196 gbMask = 0;
197 do {
198 iSamp = *inbuf;
199 x = FASTABS(iSamp);
200
201 if (x < 4) {
202 y = tab4[x];
203 } else {
204
205 if (x < 16) {
206 /* result: y = Q25 (tab16 = Q25) */
207 y = tab16[x];
208 shift = 25 - scalei;
209 } else if (x < 64) {
210 /* result: y = Q21 (pow43tab[j] = Q23, scalef = Q30) */
211 y = pow43[x - 16];
212 shift = 21 - scalei;
213 y = MULSHIFT32(y, scalef);
214 } else {
215 /* normalize to [0x40000000, 0x7fffffff]
216 * input x = [64, 8191] = [64, 2^13-1]
217 * ranges:
218 * shift = 7: 64 - 127
219 * shift = 6: 128 - 255
220 * shift = 5: 256 - 511
221 * shift = 4: 512 - 1023
222 * shift = 3: 1024 - 2047
223 * shift = 2: 2048 - 4095
224 * shift = 1: 4096 - 8191
225 */
226 x <<= 17;
227 shift = 0;
228 if (x < 0x08000000) {
229 x <<= 4, shift += 4;
230 }
231 if (x < 0x20000000) {
232 x <<= 2, shift += 2;
233 }
234 if (x < 0x40000000) {
235 x <<= 1, shift += 1;
236 }
237
238 coef = (x < SQRTHALF) ? poly43lo : poly43hi;
239
240 /* polynomial */
241 y = coef[0];
242 y = MULSHIFT32(y, x) + coef[1];
243 y = MULSHIFT32(y, x) + coef[2];
244 y = MULSHIFT32(y, x) + coef[3];
245 y = MULSHIFT32(y, x) + coef[4];
246 y = MULSHIFT32(y, pow2frac[shift]) << 3;
247
248 /* fractional scale
249 * result: y = Q21 (pow43tab[j] = Q23, scalef = Q30)
250 */
251 y = MULSHIFT32(y, scalef); /* now y is Q24 */
252 shift = 24 - scalei - pow2exp[shift];
253 }
254
255 /* integer scale */
256 if (shift <= 0) {
257 shift = -shift;
258 if (shift > 31) {
259 shift = 31;
260 }
261
262 if (y > (0x7fffffff >> shift)) {
263 y = 0x7fffffff; /* clip (rare) */
264 } else {
265 y <<= shift;
266 }
267 } else {
268 if (shift > 31) {
269 shift = 31;
270 }
271 y >>= shift;
272 }
273 }
274
275 /* sign and store (gbMask used to count GB's) */
276 gbMask |= y;
277
278 /* apply sign */
279 iSamp >>= 31;
280 y ^= iSamp;
281 y -= iSamp;
282
283 *inbuf++ = y;
284 } while (--nSamps);
285
286 return gbMask;
287}
288
289/**************************************************************************************
290 * Function: Dequantize
291 *
292 * Description: dequantize all transform coefficients for one channel
293 *
294 * Inputs: valid AACDecInfo struct (including unpacked, quantized coefficients)
295 * index of current channel
296 *
297 * Outputs: dequantized coefficients, including short-block deinterleaving
298 * flags indicating if intensity and/or PNS is active
299 * minimum guard bit count for dequantized coefficients
300 *
301 * Return: 0 if successful, error code (< 0) if error
302 **************************************************************************************/
303int Dequantize(AACDecInfo *aacDecInfo, int ch)
304{
305 int gp, cb, sfb, win, width, nSamps, gbMask;
306 int *coef;
307 const short *sfbTab;
308 unsigned char *sfbCodeBook;
309 short *scaleFactors;
310 PSInfoBase *psi;
311 ICSInfo *icsInfo;
312
313 /* validate pointers */
314 if (!aacDecInfo || !aacDecInfo->psInfoBase) {
315 return ERR_AAC_NULL_POINTER;
316 }
317 psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
318 icsInfo = (ch == 1 && psi->commonWin == 1) ? &(psi->icsInfo[0]) : &(psi->icsInfo[ch]);
319
320 if (icsInfo->winSequence == 2) {
321 sfbTab = sfBandTabShort + sfBandTabShortOffset[psi->sampRateIdx];
322 nSamps = NSAMPS_SHORT;
323 } else {
324 sfbTab = sfBandTabLong + sfBandTabLongOffset[psi->sampRateIdx];
325 nSamps = NSAMPS_LONG;
326 }
327 coef = psi->coef[ch];
328 sfbCodeBook = psi->sfbCodeBook[ch];
329 scaleFactors = psi->scaleFactors[ch];
330
331 psi->intensityUsed[ch] = 0;
332 psi->pnsUsed[ch] = 0;
333 gbMask = 0;
334 for (gp = 0; gp < icsInfo->numWinGroup; gp++) {
335 for (win = 0; win < icsInfo->winGroupLen[gp]; win++) {
336 for (sfb = 0; sfb < icsInfo->maxSFB; sfb++) {
337 /* dequantize one scalefactor band (not necessary if codebook is intensity or PNS)
338 * for zero codebook, still run dequantizer in case non-zero pulse data was added
339 */
340 cb = (int)(sfbCodeBook[sfb]);
341 width = sfbTab[sfb + 1] - sfbTab[sfb];
342 if (cb >= 0 && cb <= 11) {
343 gbMask |= DequantBlock(coef, width, scaleFactors[sfb]);
344 } else if (cb == 13) {
345 psi->pnsUsed[ch] = 1;
346 } else if (cb == 14 || cb == 15) {
347 psi->intensityUsed[ch] = 1; /* should only happen if ch == 1 */
348 }
349 coef += width;
350 }
351 coef += (nSamps - sfbTab[icsInfo->maxSFB]);
352 }
353 sfbCodeBook += icsInfo->maxSFB;
354 scaleFactors += icsInfo->maxSFB;
355 }
356 aacDecInfo->pnsUsed |= psi->pnsUsed[ch]; /* set flag if PNS used for any channel */
357
358 /* calculate number of guard bits in dequantized data */
359 psi->gbCurrent[ch] = CLZ(gbMask) - 1;
360
361 return ERR_AAC_NONE;
362}
363
364/**************************************************************************************
365 * Function: DeinterleaveShortBlocks
366 *
367 * Description: deinterleave transform coefficients in short blocks for one channel
368 *
369 * Inputs: valid AACDecInfo struct (including unpacked, quantized coefficients)
370 * index of current channel
371 *
372 * Outputs: deinterleaved coefficients (window groups into 8 separate windows)
373 *
374 * Return: 0 if successful, error code (< 0) if error
375 *
376 * Notes: only necessary if deinterleaving not part of Huffman decoding
377 **************************************************************************************/
378int DeinterleaveShortBlocks(AACDecInfo *aacDecInfo, int ch)
379{
380 /* not used for this implementation - short block deinterleaving performed during Huffman decoding */
381 return ERR_AAC_NONE;
382}
383