imdct.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: RCSL 1.0/RPSL 1.0
  3. *
  4. * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
  5. *
  6. * The contents of this file, and the files included with this file, are
  7. * subject to the current version of the RealNetworks Public Source License
  8. * Version 1.0 (the "RPSL") available at
  9. * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10. * the file under the RealNetworks Community Source License Version 1.0
  11. * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
  12. * in which case the RCSL will apply. You may also obtain the license terms
  13. * directly from RealNetworks. You may not use this file except in
  14. * compliance with the RPSL or, if you have a valid RCSL with RealNetworks
  15. * applicable to this file, the RCSL. Please see the applicable RPSL or
  16. * RCSL for the rights, obligations and limitations governing use of the
  17. * contents of the file.
  18. *
  19. * This file is part of the Helix DNA Technology. RealNetworks is the
  20. * developer of the Original Code and owns the copyrights in the portions
  21. * it created.
  22. *
  23. * This file, and the files included with this file, is distributed and made
  24. * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  25. * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  26. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
  27. * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  28. *
  29. * Technology Compatibility Kit Test Suite(s) Location:
  30. * http://www.helixcommunity.org/content/tck
  31. *
  32. * Contributor(s):
  33. *
  34. * ***** END LICENSE BLOCK ***** */
  35. /**************************************************************************************
  36. * Fixed-point MP3 decoder
  37. * Jon Recker (jrecker@real.com), Ken Cooke (kenc@real.com)
  38. * June 2003
  39. *
  40. * imdct.c - antialias, inverse transform (short/long/mixed), windowing,
  41. * overlap-add, frequency inversion
  42. **************************************************************************************/
  43. #include "coder.h"
  44. #include "assembly.h"
  45. #include <pgmspace.h>
  46. /**************************************************************************************
  47. * Function: AntiAlias
  48. *
  49. * Description: smooth transition across DCT block boundaries (every 18 coefficients)
  50. *
  51. * Inputs: vector of dequantized coefficients, length = (nBfly+1) * 18
  52. * number of "butterflies" to perform (one butterfly means one
  53. * inter-block smoothing operation)
  54. *
  55. * Outputs: updated coefficient vector x
  56. *
  57. * Return: none
  58. *
  59. * Notes: weighted average of opposite bands (pairwise) from the 8 samples
  60. * before and after each block boundary
  61. * nBlocks = (nonZeroBound + 7) / 18, since nZB is the first ZERO sample
  62. * above which all other samples are also zero
  63. * max gain per sample = 1.372
  64. * MAX(i) (abs(csa[i][0]) + abs(csa[i][1]))
  65. * bits gained = 0
  66. * assume at least 1 guard bit in x[] to avoid overflow
  67. * (should be guaranteed from dequant, and max gain from stproc * max
  68. * gain from AntiAlias < 2.0)
  69. **************************************************************************************/
  70. // a little bit faster in RAM (< 1 ms per block)
  71. /* __attribute__ ((section (".data"))) */ static void AntiAlias(int *x, int nBfly)
  72. {
  73. int k, a0, b0, c0, c1;
  74. const int *c;
  75. /* csa = Q31 */
  76. for (k = nBfly; k > 0; k--) {
  77. c = csa[0];
  78. x += 18;
  79. a0 = x[-1]; c0 = *c; c++; b0 = x[0]; c1 = *c; c++;
  80. x[-1] = (MULSHIFT32(c0, a0) - MULSHIFT32(c1, b0)) << 1;
  81. x[0] = (MULSHIFT32(c0, b0) + MULSHIFT32(c1, a0)) << 1;
  82. a0 = x[-2]; c0 = *c; c++; b0 = x[1]; c1 = *c; c++;
  83. x[-2] = (MULSHIFT32(c0, a0) - MULSHIFT32(c1, b0)) << 1;
  84. x[1] = (MULSHIFT32(c0, b0) + MULSHIFT32(c1, a0)) << 1;
  85. a0 = x[-3]; c0 = *c; c++; b0 = x[2]; c1 = *c; c++;
  86. x[-3] = (MULSHIFT32(c0, a0) - MULSHIFT32(c1, b0)) << 1;
  87. x[2] = (MULSHIFT32(c0, b0) + MULSHIFT32(c1, a0)) << 1;
  88. a0 = x[-4]; c0 = *c; c++; b0 = x[3]; c1 = *c; c++;
  89. x[-4] = (MULSHIFT32(c0, a0) - MULSHIFT32(c1, b0)) << 1;
  90. x[3] = (MULSHIFT32(c0, b0) + MULSHIFT32(c1, a0)) << 1;
  91. a0 = x[-5]; c0 = *c; c++; b0 = x[4]; c1 = *c; c++;
  92. x[-5] = (MULSHIFT32(c0, a0) - MULSHIFT32(c1, b0)) << 1;
  93. x[4] = (MULSHIFT32(c0, b0) + MULSHIFT32(c1, a0)) << 1;
  94. a0 = x[-6]; c0 = *c; c++; b0 = x[5]; c1 = *c; c++;
  95. x[-6] = (MULSHIFT32(c0, a0) - MULSHIFT32(c1, b0)) << 1;
  96. x[5] = (MULSHIFT32(c0, b0) + MULSHIFT32(c1, a0)) << 1;
  97. a0 = x[-7]; c0 = *c; c++; b0 = x[6]; c1 = *c; c++;
  98. x[-7] = (MULSHIFT32(c0, a0) - MULSHIFT32(c1, b0)) << 1;
  99. x[6] = (MULSHIFT32(c0, b0) + MULSHIFT32(c1, a0)) << 1;
  100. a0 = x[-8]; c0 = *c; c++; b0 = x[7]; c1 = *c; c++;
  101. x[-8] = (MULSHIFT32(c0, a0) - MULSHIFT32(c1, b0)) << 1;
  102. x[7] = (MULSHIFT32(c0, b0) + MULSHIFT32(c1, a0)) << 1;
  103. }
  104. }
  105. /**************************************************************************************
  106. * Function: WinPrevious
  107. *
  108. * Description: apply specified window to second half of previous IMDCT (overlap part)
  109. *
  110. * Inputs: vector of 9 coefficients (xPrev)
  111. *
  112. * Outputs: 18 windowed output coefficients (gain 1 integer bit)
  113. * window type (0, 1, 2, 3)
  114. *
  115. * Return: none
  116. *
  117. * Notes: produces 9 output samples from 18 input samples via symmetry
  118. * all blocks gain at least 1 guard bit via window (long blocks get extra
  119. * sign bit, short blocks can have one addition but max gain < 1.0)
  120. **************************************************************************************/
  121. /*__attribute__ ((section (".data"))) */ static void WinPrevious(int *xPrev, int *xPrevWin, int btPrev)
  122. {
  123. int i, x, *xp, *xpwLo, *xpwHi, wLo, wHi;
  124. const int *wpLo, *wpHi;
  125. xp = xPrev;
  126. /* mapping (see IMDCT12x3): xPrev[0-2] = sum[6-8], xPrev[3-8] = sum[12-17] */
  127. if (btPrev == 2) {
  128. /* this could be reordered for minimum loads/stores */
  129. wpLo = imdctWin[btPrev];
  130. xPrevWin[ 0] = MULSHIFT32(wpLo[ 6], xPrev[2]) + MULSHIFT32(wpLo[0], xPrev[6]);
  131. xPrevWin[ 1] = MULSHIFT32(wpLo[ 7], xPrev[1]) + MULSHIFT32(wpLo[1], xPrev[7]);
  132. xPrevWin[ 2] = MULSHIFT32(wpLo[ 8], xPrev[0]) + MULSHIFT32(wpLo[2], xPrev[8]);
  133. xPrevWin[ 3] = MULSHIFT32(wpLo[ 9], xPrev[0]) + MULSHIFT32(wpLo[3], xPrev[8]);
  134. xPrevWin[ 4] = MULSHIFT32(wpLo[10], xPrev[1]) + MULSHIFT32(wpLo[4], xPrev[7]);
  135. xPrevWin[ 5] = MULSHIFT32(wpLo[11], xPrev[2]) + MULSHIFT32(wpLo[5], xPrev[6]);
  136. xPrevWin[ 6] = MULSHIFT32(wpLo[ 6], xPrev[5]);
  137. xPrevWin[ 7] = MULSHIFT32(wpLo[ 7], xPrev[4]);
  138. xPrevWin[ 8] = MULSHIFT32(wpLo[ 8], xPrev[3]);
  139. xPrevWin[ 9] = MULSHIFT32(wpLo[ 9], xPrev[3]);
  140. xPrevWin[10] = MULSHIFT32(wpLo[10], xPrev[4]);
  141. xPrevWin[11] = MULSHIFT32(wpLo[11], xPrev[5]);
  142. xPrevWin[12] = xPrevWin[13] = xPrevWin[14] = xPrevWin[15] = xPrevWin[16] = xPrevWin[17] = 0;
  143. } else {
  144. /* use ARM-style pointers (*ptr++) so that ADS compiles well */
  145. wpLo = imdctWin[btPrev] + 18;
  146. wpHi = wpLo + 17;
  147. xpwLo = xPrevWin;
  148. xpwHi = xPrevWin + 17;
  149. for (i = 9; i > 0; i--) {
  150. x = *xp++; wLo = *wpLo++; wHi = *wpHi--;
  151. *xpwLo++ = MULSHIFT32(wLo, x);
  152. *xpwHi-- = MULSHIFT32(wHi, x);
  153. }
  154. }
  155. }
  156. /**************************************************************************************
  157. * Function: FreqInvertRescale
  158. *
  159. * Description: do frequency inversion (odd samples of odd blocks) and rescale
  160. * if necessary (extra guard bits added before IMDCT)
  161. *
  162. * Inputs: output vector y (18 new samples, spaced NBANDS apart)
  163. * previous sample vector xPrev (9 samples)
  164. * index of current block
  165. * number of extra shifts added before IMDCT (usually 0)
  166. *
  167. * Outputs: inverted and rescaled (as necessary) outputs
  168. * rescaled (as necessary) previous samples
  169. *
  170. * Return: updated mOut (from new outputs y)
  171. **************************************************************************************/
  172. /*__attribute__ ((section (".data")))*/ static int FreqInvertRescale(int *y, int *xPrev, int blockIdx, int es)
  173. {
  174. int i, d, mOut;
  175. int y0, y1, y2, y3, y4, y5, y6, y7, y8;
  176. if (es == 0) {
  177. /* fast case - frequency invert only (no rescaling) - can fuse into overlap-add for speed, if desired */
  178. if (blockIdx & 0x01) {
  179. y += NBANDS;
  180. y0 = *y; y += 2*NBANDS;
  181. y1 = *y; y += 2*NBANDS;
  182. y2 = *y; y += 2*NBANDS;
  183. y3 = *y; y += 2*NBANDS;
  184. y4 = *y; y += 2*NBANDS;
  185. y5 = *y; y += 2*NBANDS;
  186. y6 = *y; y += 2*NBANDS;
  187. y7 = *y; y += 2*NBANDS;
  188. y8 = *y; y += 2*NBANDS;
  189. y -= 18*NBANDS;
  190. *y = -y0; y += 2*NBANDS;
  191. *y = -y1; y += 2*NBANDS;
  192. *y = -y2; y += 2*NBANDS;
  193. *y = -y3; y += 2*NBANDS;
  194. *y = -y4; y += 2*NBANDS;
  195. *y = -y5; y += 2*NBANDS;
  196. *y = -y6; y += 2*NBANDS;
  197. *y = -y7; y += 2*NBANDS;
  198. *y = -y8; y += 2*NBANDS;
  199. }
  200. return 0;
  201. } else {
  202. /* undo pre-IMDCT scaling, clipping if necessary */
  203. mOut = 0;
  204. if (blockIdx & 0x01) {
  205. /* frequency invert */
  206. for (i = 0; i < 18; i+=2) {
  207. d = *y; CLIP_2N(d, 31 - es); *y = d << es; mOut |= FASTABS(*y); y += NBANDS;
  208. d = -*y; CLIP_2N(d, 31 - es); *y = d << es; mOut |= FASTABS(*y); y += NBANDS;
  209. d = *xPrev; CLIP_2N(d, 31 - es); *xPrev++ = d << es;
  210. }
  211. } else {
  212. for (i = 0; i < 18; i+=2) {
  213. d = *y; CLIP_2N(d, 31 - es); *y = d << es; mOut |= FASTABS(*y); y += NBANDS;
  214. d = *y; CLIP_2N(d, 31 - es); *y = d << es; mOut |= FASTABS(*y); y += NBANDS;
  215. d = *xPrev; CLIP_2N(d, 31 - es); *xPrev++ = d << es;
  216. }
  217. }
  218. return mOut;
  219. }
  220. }
  221. /* format = Q31
  222. * #define M_PI 3.14159265358979323846
  223. * double u = 2.0 * M_PI / 9.0;
  224. * float c0 = sqrt(3.0) / 2.0;
  225. * float c1 = cos(u);
  226. * float c2 = cos(2*u);
  227. * float c3 = sin(u);
  228. * float c4 = sin(2*u);
  229. */
  230. static const int c9_0 = 0x6ed9eba1;
  231. static const int c9_1 = 0x620dbe8b;
  232. static const int c9_2 = 0x163a1a7e;
  233. static const int c9_3 = 0x5246dd49;
  234. static const int c9_4 = 0x7e0e2e32;
  235. /* format = Q31
  236. * cos(((0:8) + 0.5) * (pi/18))
  237. */
  238. static const int c18[9] PROGMEM = {
  239. 0x7f834ed0, 0x7ba3751d, 0x7401e4c1, 0x68d9f964, 0x5a82799a, 0x496af3e2, 0x36185aee, 0x2120fb83, 0x0b27eb5c,
  240. };
  241. /* require at least 3 guard bits in x[] to ensure no overflow */
  242. static __inline void idct9(int *x)
  243. {
  244. int a1, a2, a3, a4, a5, a6, a7, a8, a9;
  245. int a10, a11, a12, a13, a14, a15, a16, a17, a18;
  246. int a19, a20, a21, a22, a23, a24, a25, a26, a27;
  247. int m1, m3, m5, m6, m7, m8, m9, m10, m11, m12;
  248. int x0, x1, x2, x3, x4, x5, x6, x7, x8;
  249. x0 = x[0]; x1 = x[1]; x2 = x[2]; x3 = x[3]; x4 = x[4];
  250. x5 = x[5]; x6 = x[6]; x7 = x[7]; x8 = x[8];
  251. a1 = x0 - x6;
  252. a2 = x1 - x5;
  253. a3 = x1 + x5;
  254. a4 = x2 - x4;
  255. a5 = x2 + x4;
  256. a6 = x2 + x8;
  257. a7 = x1 + x7;
  258. a8 = a6 - a5; /* ie x[8] - x[4] */
  259. a9 = a3 - a7; /* ie x[5] - x[7] */
  260. a10 = a2 - x7; /* ie x[1] - x[5] - x[7] */
  261. a11 = a4 - x8; /* ie x[2] - x[4] - x[8] */
  262. /* do the << 1 as constant shifts where mX is actually used (free, no stall or extra inst.) */
  263. m1 = MULSHIFT32(c9_0, x3);
  264. m3 = MULSHIFT32(c9_0, a10);
  265. m5 = MULSHIFT32(c9_1, a5);
  266. m6 = MULSHIFT32(c9_2, a6);
  267. m7 = MULSHIFT32(c9_1, a8);
  268. m8 = MULSHIFT32(c9_2, a5);
  269. m9 = MULSHIFT32(c9_3, a9);
  270. m10 = MULSHIFT32(c9_4, a7);
  271. m11 = MULSHIFT32(c9_3, a3);
  272. m12 = MULSHIFT32(c9_4, a9);
  273. a12 = x[0] + (x[6] >> 1);
  274. a13 = a12 + ( m1 << 1);
  275. a14 = a12 - ( m1 << 1);
  276. a15 = a1 + ( a11 >> 1);
  277. a16 = ( m5 << 1) + (m6 << 1);
  278. a17 = ( m7 << 1) - (m8 << 1);
  279. a18 = a16 + a17;
  280. a19 = ( m9 << 1) + (m10 << 1);
  281. a20 = (m11 << 1) - (m12 << 1);
  282. a21 = a20 - a19;
  283. a22 = a13 + a16;
  284. a23 = a14 + a16;
  285. a24 = a14 + a17;
  286. a25 = a13 + a17;
  287. a26 = a14 - a18;
  288. a27 = a13 - a18;
  289. x0 = a22 + a19; x[0] = x0;
  290. x1 = a15 + (m3 << 1); x[1] = x1;
  291. x2 = a24 + a20; x[2] = x2;
  292. x3 = a26 - a21; x[3] = x3;
  293. x4 = a1 - a11; x[4] = x4;
  294. x5 = a27 + a21; x[5] = x5;
  295. x6 = a25 - a20; x[6] = x6;
  296. x7 = a15 - (m3 << 1); x[7] = x7;
  297. x8 = a23 - a19; x[8] = x8;
  298. }
  299. /* let c(j) = cos(M_PI/36 * ((j)+0.5)), s(j) = sin(M_PI/36 * ((j)+0.5))
  300. * then fastWin[2*j+0] = c(j)*(s(j) + c(j)), j = [0, 8]
  301. * fastWin[2*j+1] = c(j)*(s(j) - c(j))
  302. * format = Q30
  303. */
  304. #pragma GCC diagnostic push
  305. #pragma GCC diagnostic ignored "-Wnarrowing"
  306. const int fastWin36[18] PROGMEM = {
  307. 0x42aace8b, 0xc2e92724, 0x47311c28, 0xc95f619a, 0x4a868feb, 0xd0859d8c,
  308. 0x4c913b51, 0xd8243ea0, 0x4d413ccc, 0xe0000000, 0x4c913b51, 0xe7dbc161,
  309. 0x4a868feb, 0xef7a6275, 0x47311c28, 0xf6a09e67, 0x42aace8b, 0xfd16d8dd,
  310. };
  311. #pragma GCC diagnostic pop
  312. /**************************************************************************************
  313. * Function: IMDCT36
  314. *
  315. * Description: 36-point modified DCT, with windowing and overlap-add (50% overlap)
  316. *
  317. * Inputs: vector of 18 coefficients (N/2 inputs produces N outputs, by symmetry)
  318. * overlap part of last IMDCT (9 samples - see output comments)
  319. * window type (0,1,2,3) of current and previous block
  320. * current block index (for deciding whether to do frequency inversion)
  321. * number of guard bits in input vector
  322. *
  323. * Outputs: 18 output samples, after windowing and overlap-add with last frame
  324. * second half of (unwindowed) 36-point IMDCT - save for next time
  325. * only save 9 xPrev samples, using symmetry (see WinPrevious())
  326. *
  327. * Notes: this is Ken's hyper-fast algorithm, including symmetric sin window
  328. * optimization, if applicable
  329. * total number of multiplies, general case:
  330. * 2*10 (idct9) + 9 (last stage imdct) + 36 (for windowing) = 65
  331. * total number of multiplies, btCurr == 0 && btPrev == 0:
  332. * 2*10 (idct9) + 9 (last stage imdct) + 18 (for windowing) = 47
  333. *
  334. * blockType == 0 is by far the most common case, so it should be
  335. * possible to use the fast path most of the time
  336. * this is the fastest known algorithm for performing
  337. * long IMDCT + windowing + overlap-add in MP3
  338. *
  339. * Return: mOut (OR of abs(y) for all y calculated here)
  340. *
  341. * TODO: optimize for ARM (reorder window coefs, ARM-style pointers in C,
  342. * inline asm may or may not be helpful)
  343. **************************************************************************************/
  344. // barely faster in RAM
  345. /*__attribute__ ((section (".data")))*/ static int IMDCT36(int *xCurr, int *xPrev, int *y, int btCurr, int btPrev, int blockIdx, int gb)
  346. {
  347. int i, es, xBuf[18], xPrevWin[18];
  348. int acc1, acc2, s, d, t, mOut;
  349. int xo, xe, c, *xp, yLo, yHi;
  350. const int *cp, *wp;
  351. acc1 = acc2 = 0;
  352. xCurr += 17;
  353. /* 7 gb is always adequate for antialias + accumulator loop + idct9 */
  354. if (gb < 7) {
  355. /* rarely triggered - 5% to 10% of the time on normal clips (with Q25 input) */
  356. es = 7 - gb;
  357. for (i = 8; i >= 0; i--) {
  358. acc1 = ((*xCurr--) >> es) - acc1;
  359. acc2 = acc1 - acc2;
  360. acc1 = ((*xCurr--) >> es) - acc1;
  361. xBuf[i+9] = acc2; /* odd */
  362. xBuf[i+0] = acc1; /* even */
  363. xPrev[i] >>= es;
  364. }
  365. } else {
  366. es = 0;
  367. /* max gain = 18, assume adequate guard bits */
  368. for (i = 8; i >= 0; i--) {
  369. acc1 = (*xCurr--) - acc1;
  370. acc2 = acc1 - acc2;
  371. acc1 = (*xCurr--) - acc1;
  372. xBuf[i+9] = acc2; /* odd */
  373. xBuf[i+0] = acc1; /* even */
  374. }
  375. }
  376. /* xEven[0] and xOdd[0] scaled by 0.5 */
  377. xBuf[9] >>= 1;
  378. xBuf[0] >>= 1;
  379. /* do 9-point IDCT on even and odd */
  380. idct9(xBuf+0); /* even */
  381. idct9(xBuf+9); /* odd */
  382. xp = xBuf + 8;
  383. cp = c18 + 8;
  384. mOut = 0;
  385. if (btPrev == 0 && btCurr == 0) {
  386. /* fast path - use symmetry of sin window to reduce windowing multiplies to 18 (N/2) */
  387. wp = fastWin36;
  388. for (i = 0; i < 9; i++) {
  389. /* do ARM-style pointer arithmetic (i still needed for y[] indexing - compiler spills if 2 y pointers) */
  390. c = *cp--; xo = *(xp + 9); xe = *xp--;
  391. /* gain 2 int bits here */
  392. xo = MULSHIFT32(c, xo); /* 2*c18*xOdd (mul by 2 implicit in scaling) */
  393. xe >>= 2;
  394. s = -(*xPrev); /* sum from last block (always at least 2 guard bits) */
  395. d = -(xe - xo); /* gain 2 int bits, don't shift xo (effective << 1 to eat sign bit, << 1 for mul by 2) */
  396. (*xPrev++) = xe + xo; /* symmetry - xPrev[i] = xPrev[17-i] for long blocks */
  397. t = s - d;
  398. yLo = (d + (MULSHIFT32(t, *wp++) << 2));
  399. yHi = (s + (MULSHIFT32(t, *wp++) << 2));
  400. y[(i)*NBANDS] = yLo;
  401. y[(17-i)*NBANDS] = yHi;
  402. mOut |= FASTABS(yLo);
  403. mOut |= FASTABS(yHi);
  404. }
  405. } else {
  406. /* slower method - either prev or curr is using window type != 0 so do full 36-point window
  407. * output xPrevWin has at least 3 guard bits (xPrev has 2, gain 1 in WinPrevious)
  408. */
  409. WinPrevious(xPrev, xPrevWin, btPrev);
  410. wp = imdctWin[btCurr];
  411. for (i = 0; i < 9; i++) {
  412. c = *cp--; xo = *(xp + 9); xe = *xp--;
  413. /* gain 2 int bits here */
  414. xo = MULSHIFT32(c, xo); /* 2*c18*xOdd (mul by 2 implicit in scaling) */
  415. xe >>= 2;
  416. d = xe - xo;
  417. (*xPrev++) = xe + xo; /* symmetry - xPrev[i] = xPrev[17-i] for long blocks */
  418. yLo = (xPrevWin[i] + MULSHIFT32(d, wp[i])) << 2;
  419. yHi = (xPrevWin[17-i] + MULSHIFT32(d, wp[17-i])) << 2;
  420. y[(i)*NBANDS] = yLo;
  421. y[(17-i)*NBANDS] = yHi;
  422. mOut |= FASTABS(yLo);
  423. mOut |= FASTABS(yHi);
  424. }
  425. }
  426. xPrev -= 9;
  427. mOut |= FreqInvertRescale(y, xPrev, blockIdx, es);
  428. return mOut;
  429. }
  430. static int c3_0 = 0x6ed9eba1; /* format = Q31, cos(pi/6) */
  431. static int c6[3] = { 0x7ba3751d, 0x5a82799a, 0x2120fb83 }; /* format = Q31, cos(((0:2) + 0.5) * (pi/6)) */
  432. /* 12-point inverse DCT, used in IMDCT12x3()
  433. * 4 input guard bits will ensure no overflow
  434. */
  435. static __inline void imdct12 (int *x, int *out)
  436. {
  437. int a0, a1, a2;
  438. int x0, x1, x2, x3, x4, x5;
  439. x0 = *x; x+=3; x1 = *x; x+=3;
  440. x2 = *x; x+=3; x3 = *x; x+=3;
  441. x4 = *x; x+=3; x5 = *x; x+=3;
  442. x4 -= x5;
  443. x3 -= x4;
  444. x2 -= x3;
  445. x3 -= x5;
  446. x1 -= x2;
  447. x0 -= x1;
  448. x1 -= x3;
  449. x0 >>= 1;
  450. x1 >>= 1;
  451. a0 = MULSHIFT32(c3_0, x2) << 1;
  452. a1 = x0 + (x4 >> 1);
  453. a2 = x0 - x4;
  454. x0 = a1 + a0;
  455. x2 = a2;
  456. x4 = a1 - a0;
  457. a0 = MULSHIFT32(c3_0, x3) << 1;
  458. a1 = x1 + (x5 >> 1);
  459. a2 = x1 - x5;
  460. /* cos window odd samples, mul by 2, eat sign bit */
  461. x1 = MULSHIFT32(c6[0], a1 + a0) << 2;
  462. x3 = MULSHIFT32(c6[1], a2) << 2;
  463. x5 = MULSHIFT32(c6[2], a1 - a0) << 2;
  464. *out = x0 + x1; out++;
  465. *out = x2 + x3; out++;
  466. *out = x4 + x5; out++;
  467. *out = x4 - x5; out++;
  468. *out = x2 - x3; out++;
  469. *out = x0 - x1;
  470. }
  471. /**************************************************************************************
  472. * Function: IMDCT12x3
  473. *
  474. * Description: three 12-point modified DCT's for short blocks, with windowing,
  475. * short block concatenation, and overlap-add
  476. *
  477. * Inputs: 3 interleaved vectors of 6 samples each
  478. * (block0[0], block1[0], block2[0], block0[1], block1[1]....)
  479. * overlap part of last IMDCT (9 samples - see output comments)
  480. * window type (0,1,2,3) of previous block
  481. * current block index (for deciding whether to do frequency inversion)
  482. * number of guard bits in input vector
  483. *
  484. * Outputs: updated sample vector x, net gain of 1 integer bit
  485. * second half of (unwindowed) IMDCT's - save for next time
  486. * only save 9 xPrev samples, using symmetry (see WinPrevious())
  487. *
  488. * Return: mOut (OR of abs(y) for all y calculated here)
  489. *
  490. * TODO: optimize for ARM
  491. **************************************************************************************/
  492. // barely faster in RAM
  493. /*__attribute__ ((section (".data")))*/ static int IMDCT12x3(int *xCurr, int *xPrev, int *y, int btPrev, int blockIdx, int gb)
  494. {
  495. int i, es, mOut, yLo, xBuf[18], xPrevWin[18]; /* need temp buffer for reordering short blocks */
  496. const int *wp;
  497. es = 0;
  498. /* 7 gb is always adequate for accumulator loop + idct12 + window + overlap */
  499. if (gb < 7) {
  500. es = 7 - gb;
  501. for (i = 0; i < 18; i+=2) {
  502. xCurr[i+0] >>= es;
  503. xCurr[i+1] >>= es;
  504. *xPrev++ >>= es;
  505. }
  506. xPrev -= 9;
  507. }
  508. /* requires 4 input guard bits for each imdct12 */
  509. imdct12(xCurr + 0, xBuf + 0);
  510. imdct12(xCurr + 1, xBuf + 6);
  511. imdct12(xCurr + 2, xBuf + 12);
  512. /* window previous from last time */
  513. WinPrevious(xPrev, xPrevWin, btPrev);
  514. /* could unroll this for speed, minimum loads (short blocks usually rare, so doesn't make much overall difference)
  515. * xPrevWin[i] << 2 still has 1 gb always, max gain of windowed xBuf stuff also < 1.0 and gain the sign bit
  516. * so y calculations won't overflow
  517. */
  518. wp = imdctWin[2];
  519. mOut = 0;
  520. for (i = 0; i < 3; i++) {
  521. yLo = (xPrevWin[ 0+i] << 2);
  522. mOut |= FASTABS(yLo); y[( 0+i)*NBANDS] = yLo;
  523. yLo = (xPrevWin[ 3+i] << 2);
  524. mOut |= FASTABS(yLo); y[( 3+i)*NBANDS] = yLo;
  525. yLo = (xPrevWin[ 6+i] << 2) + (MULSHIFT32(wp[0+i], xBuf[3+i]));
  526. mOut |= FASTABS(yLo); y[( 6+i)*NBANDS] = yLo;
  527. yLo = (xPrevWin[ 9+i] << 2) + (MULSHIFT32(wp[3+i], xBuf[5-i]));
  528. mOut |= FASTABS(yLo); y[( 9+i)*NBANDS] = yLo;
  529. yLo = (xPrevWin[12+i] << 2) + (MULSHIFT32(wp[6+i], xBuf[2-i]) + MULSHIFT32(wp[0+i], xBuf[(6+3)+i]));
  530. mOut |= FASTABS(yLo); y[(12+i)*NBANDS] = yLo;
  531. yLo = (xPrevWin[15+i] << 2) + (MULSHIFT32(wp[9+i], xBuf[0+i]) + MULSHIFT32(wp[3+i], xBuf[(6+5)-i]));
  532. mOut |= FASTABS(yLo); y[(15+i)*NBANDS] = yLo;
  533. }
  534. /* save previous (unwindowed) for overlap - only need samples 6-8, 12-17 */
  535. for (i = 6; i < 9; i++)
  536. *xPrev++ = xBuf[i] >> 2;
  537. for (i = 12; i < 18; i++)
  538. *xPrev++ = xBuf[i] >> 2;
  539. xPrev -= 9;
  540. mOut |= FreqInvertRescale(y, xPrev, blockIdx, es);
  541. return mOut;
  542. }
  543. /**************************************************************************************
  544. * Function: HybridTransform
  545. *
  546. * Description: IMDCT's, windowing, and overlap-add on long/short/mixed blocks
  547. *
  548. * Inputs: vector of input coefficients, length = nBlocksTotal * 18)
  549. * vector of overlap samples from last time, length = nBlocksPrev * 9)
  550. * buffer for output samples, length = MAXNSAMP
  551. * SideInfoSub struct for this granule/channel
  552. * BlockCount struct with necessary info
  553. * number of non-zero input and overlap blocks
  554. * number of long blocks in input vector (rest assumed to be short blocks)
  555. * number of blocks which use long window (type) 0 in case of mixed block
  556. * (bc->currWinSwitch, 0 for non-mixed blocks)
  557. *
  558. * Outputs: transformed, windowed, and overlapped sample buffer
  559. * does frequency inversion on odd blocks
  560. * updated buffer of samples for overlap
  561. *
  562. * Return: number of non-zero IMDCT blocks calculated in this call
  563. * (including overlap-add)
  564. *
  565. * TODO: examine mixedBlock/winSwitch logic carefully (test he_mode.bit)
  566. **************************************************************************************/
  567. /* __attribute__ ((section (".data"))) */ static int HybridTransform(int *xCurr, int *xPrev, int y[BLOCK_SIZE][NBANDS], SideInfoSub *sis, BlockCount *bc)
  568. {
  569. int xPrevWin[18], currWinIdx, prevWinIdx;
  570. int i, j, nBlocksOut, nonZero, mOut;
  571. int fiBit, xp;
  572. ASSERT(bc->nBlocksLong <= NBANDS);
  573. ASSERT(bc->nBlocksTotal <= NBANDS);
  574. ASSERT(bc->nBlocksPrev <= NBANDS);
  575. mOut = 0;
  576. /* do long blocks, if any */
  577. for(i = 0; i < bc->nBlocksLong; i++) {
  578. /* currWinIdx picks the right window for long blocks (if mixed, long blocks use window type 0) */
  579. currWinIdx = sis->blockType;
  580. if (sis->mixedBlock && i < bc->currWinSwitch)
  581. currWinIdx = 0;
  582. prevWinIdx = bc->prevType;
  583. if (i < bc->prevWinSwitch)
  584. prevWinIdx = 0;
  585. /* do 36-point IMDCT, including windowing and overlap-add */
  586. mOut |= IMDCT36(xCurr, xPrev, &(y[0][i]), currWinIdx, prevWinIdx, i, bc->gbIn);
  587. xCurr += 18;
  588. xPrev += 9;
  589. }
  590. /* do short blocks (if any) */
  591. for ( ; i < bc->nBlocksTotal; i++) {
  592. ASSERT(sis->blockType == 2);
  593. prevWinIdx = bc->prevType;
  594. if (i < bc->prevWinSwitch)
  595. prevWinIdx = 0;
  596. mOut |= IMDCT12x3(xCurr, xPrev, &(y[0][i]), prevWinIdx, i, bc->gbIn);
  597. xCurr += 18;
  598. xPrev += 9;
  599. }
  600. nBlocksOut = i;
  601. /* window and overlap prev if prev longer that current */
  602. for ( ; i < bc->nBlocksPrev; i++) {
  603. prevWinIdx = bc->prevType;
  604. if (i < bc->prevWinSwitch)
  605. prevWinIdx = 0;
  606. WinPrevious(xPrev, xPrevWin, prevWinIdx);
  607. nonZero = 0;
  608. fiBit = i << 31;
  609. for (j = 0; j < 9; j++) {
  610. xp = xPrevWin[2*j+0] << 2; /* << 2 temp for scaling */
  611. nonZero |= xp;
  612. y[2*j+0][i] = xp;
  613. mOut |= FASTABS(xp);
  614. /* frequency inversion on odd blocks/odd samples (flip sign if i odd, j odd) */
  615. xp = xPrevWin[2*j+1] << 2;
  616. xp = (xp ^ (fiBit >> 31)) + (i & 0x01);
  617. nonZero |= xp;
  618. y[2*j+1][i] = xp;
  619. mOut |= FASTABS(xp);
  620. xPrev[j] = 0;
  621. }
  622. xPrev += 9;
  623. if (nonZero)
  624. nBlocksOut = i;
  625. }
  626. /* clear rest of blocks */
  627. for ( ; i < 32; i++) {
  628. for (j = 0; j < 18; j++)
  629. y[j][i] = 0;
  630. }
  631. bc->gbOut = CLZ(mOut) - 1;
  632. return nBlocksOut;
  633. }
  634. /**************************************************************************************
  635. * Function: IMDCT
  636. *
  637. * Description: do alias reduction, inverse MDCT, overlap-add, and frequency inversion
  638. *
  639. * Inputs: MP3DecInfo structure filled by UnpackFrameHeader(), UnpackSideInfo(),
  640. * UnpackScaleFactors(), and DecodeHuffman() (for this granule, channel)
  641. * includes PCM samples in overBuf (from last call to IMDCT) for OLA
  642. * index of current granule and channel
  643. *
  644. * Outputs: PCM samples in outBuf, for input to subband transform
  645. * PCM samples in overBuf, for OLA next time
  646. * updated hi->nonZeroBound index for this channel
  647. *
  648. * Return: 0 on success, -1 if null input pointers
  649. **************************************************************************************/
  650. // a bit faster in RAM
  651. /*__attribute__ ((section (".data")))*/ int IMDCT(MP3DecInfo *mp3DecInfo, int gr, int ch)
  652. {
  653. int nBfly, blockCutoff;
  654. FrameHeader *fh;
  655. SideInfo *si;
  656. HuffmanInfo *hi;
  657. IMDCTInfo *mi;
  658. BlockCount bc;
  659. /* validate pointers */
  660. if (!mp3DecInfo || !mp3DecInfo->FrameHeaderPS || !mp3DecInfo->SideInfoPS ||
  661. !mp3DecInfo->HuffmanInfoPS || !mp3DecInfo->IMDCTInfoPS)
  662. return -1;
  663. /* si is an array of up to 4 structs, stored as gr0ch0, gr0ch1, gr1ch0, gr1ch1 */
  664. fh = (FrameHeader *)(mp3DecInfo->FrameHeaderPS);
  665. si = (SideInfo *)(mp3DecInfo->SideInfoPS);
  666. hi = (HuffmanInfo*)(mp3DecInfo->HuffmanInfoPS);
  667. mi = (IMDCTInfo *)(mp3DecInfo->IMDCTInfoPS);
  668. /* anti-aliasing done on whole long blocks only
  669. * for mixed blocks, nBfly always 1, except 3 for 8 kHz MPEG 2.5 (see sfBandTab)
  670. * nLongBlocks = number of blocks with (possibly) non-zero power
  671. * nBfly = number of butterflies to do (nLongBlocks - 1, unless no long blocks)
  672. */
  673. blockCutoff = fh->sfBand->l[(fh->ver == MPEG1 ? 8 : 6)] / 18; /* same as 3* num short sfb's in spec */
  674. if (si->sis[gr][ch].blockType != 2) {
  675. /* all long transforms */
  676. bc.nBlocksLong = MIN((hi->nonZeroBound[ch] + 7) / 18 + 1, 32);
  677. nBfly = bc.nBlocksLong - 1;
  678. } else if (si->sis[gr][ch].blockType == 2 && si->sis[gr][ch].mixedBlock) {
  679. /* mixed block - long transforms until cutoff, then short transforms */
  680. bc.nBlocksLong = blockCutoff;
  681. nBfly = bc.nBlocksLong - 1;
  682. } else {
  683. /* all short transforms */
  684. bc.nBlocksLong = 0;
  685. nBfly = 0;
  686. }
  687. AntiAlias(hi->huffDecBuf[ch], nBfly);
  688. hi->nonZeroBound[ch] = MAX(hi->nonZeroBound[ch], (nBfly * 18) + 8);
  689. ASSERT(hi->nonZeroBound[ch] <= MAX_NSAMP);
  690. /* for readability, use a struct instead of passing a million parameters to HybridTransform() */
  691. bc.nBlocksTotal = (hi->nonZeroBound[ch] + 17) / 18;
  692. bc.nBlocksPrev = mi->numPrevIMDCT[ch];
  693. bc.prevType = mi->prevType[ch];
  694. bc.prevWinSwitch = mi->prevWinSwitch[ch];
  695. bc.currWinSwitch = (si->sis[gr][ch].mixedBlock ? blockCutoff : 0); /* where WINDOW switches (not nec. transform) */
  696. bc.gbIn = hi->gb[ch];
  697. mi->numPrevIMDCT[ch] = HybridTransform(hi->huffDecBuf[ch], mi->overBuf[ch], mi->outBuf[ch], &si->sis[gr][ch], &bc);
  698. mi->prevType[ch] = si->sis[gr][ch].blockType;
  699. mi->prevWinSwitch[ch] = bc.currWinSwitch; /* 0 means not a mixed block (either all short or all long) */
  700. mi->gb[ch] = bc.gbOut;
  701. ASSERT(mi->numPrevIMDCT[ch] <= NBANDS);
  702. /* output has gained 2 int bits */
  703. return 0;
  704. }