2
0

dequant.c 12 KB

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