synth.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * libmad - MPEG audio decoder library
  3. * Copyright (C) 2000-2004 Underbit Technologies, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * $Id: synth.c,v 1.25 2004/01/23 09:41:33 rob Exp $
  20. */
  21. # include "libmad_config.h"
  22. # include "libmad_global.h"
  23. # include "fixed.h"
  24. # include "frame.h"
  25. # include "synth.h"
  26. /*
  27. * NAME: synth->init()
  28. * DESCRIPTION: initialize synth struct
  29. */
  30. void mad_synth_init(struct mad_synth *synth)
  31. {
  32. mad_synth_mute(synth);
  33. synth->phase = 0;
  34. synth->pcm.samplerate = 0;
  35. synth->pcm.channels = 0;
  36. synth->pcm.length = 0;
  37. }
  38. /*
  39. * NAME: synth->mute()
  40. * DESCRIPTION: zero all polyphase filterbank values, resetting synthesis
  41. */
  42. void mad_synth_mute(struct mad_synth *synth)
  43. {
  44. unsigned int ch, s, v;
  45. for (ch = 0; ch < 2; ++ch) {
  46. for (s = 0; s < 16; ++s) {
  47. for (v = 0; v < 8; ++v) {
  48. synth->filter[ch][0][0][s][v] = synth->filter[ch][0][1][s][v] =
  49. synth->filter[ch][1][0][s][v] = synth->filter[ch][1][1][s][v] = 0;
  50. }
  51. }
  52. }
  53. }
  54. /*
  55. * An optional optimization called here the Subband Synthesis Optimization
  56. * (SSO) improves the performance of subband synthesis at the expense of
  57. * accuracy.
  58. *
  59. * The idea is to simplify 32x32->64-bit multiplication to 32x32->32 such
  60. * that extra scaling and rounding are not necessary. This often allows the
  61. * compiler to use faster 32-bit multiply-accumulate instructions instead of
  62. * explicit 64-bit multiply, shift, and add instructions.
  63. *
  64. * SSO works like this: a full 32x32->64-bit multiply of two mad_fixed_t
  65. * values requires the result to be right-shifted 28 bits to be properly
  66. * scaled to the same fixed-point format. Right shifts can be applied at any
  67. * time to either operand or to the result, so the optimization involves
  68. * careful placement of these shifts to minimize the loss of accuracy.
  69. *
  70. * First, a 14-bit shift is applied with rounding at compile-time to the D[]
  71. * table of coefficients for the subband synthesis window. This only loses 2
  72. * bits of accuracy because the lower 12 bits are always zero. A second
  73. * 12-bit shift occurs after the DCT calculation. This loses 12 bits of
  74. * accuracy. Finally, a third 2-bit shift occurs just before the sample is
  75. * saved in the PCM buffer. 14 + 12 + 2 == 28 bits.
  76. */
  77. /* FPM_DEFAULT without OPT_SSO will actually lose accuracy and performance */
  78. # if defined(FPM_DEFAULT) && !defined(OPT_SSO)
  79. # define OPT_SSO
  80. # endif
  81. /* second SSO shift, with rounding */
  82. # if defined(OPT_SSO)
  83. # define SHIFT(x) (((x) + (1L << 11)) >> 12)
  84. # else
  85. # define SHIFT(x) (x)
  86. # endif
  87. /* possible DCT speed optimization */
  88. # if defined(OPT_SPEED) && defined(MAD_F_MLX)
  89. # define OPT_DCTO
  90. # define MUL(x, y) \
  91. ({ mad_fixed64hi_t hi; \
  92. mad_fixed64lo_t lo; \
  93. MAD_F_MLX(hi, lo, (x), (y)); \
  94. hi << (32 - MAD_F_SCALEBITS - 3); \
  95. })
  96. # else
  97. # undef OPT_DCTO
  98. # define MUL(x, y) mad_f_mul((x), (y))
  99. # endif
  100. /*
  101. * NAME: dct32()
  102. * DESCRIPTION: perform fast in[32]->out[32] DCT
  103. */
  104. static
  105. void dct32(mad_fixed_t const in[32], unsigned int slot,
  106. mad_fixed_t lo[16][8], mad_fixed_t hi[16][8])
  107. {
  108. mad_fixed_t t0, t1, t2, t3, t4, t5, t6, t7;
  109. mad_fixed_t t8, t9, t10, t11, t12, t13, t14, t15;
  110. mad_fixed_t t16, t17, t18, t19, t20, t21, t22, t23;
  111. mad_fixed_t t24, t25, t26, t27, t28, t29, t30, t31;
  112. mad_fixed_t t32, t33, t34, t35, t36, t37, t38, t39;
  113. mad_fixed_t t40, t41, t42, t43, t44, t45, t46, t47;
  114. mad_fixed_t t48, t49, t50, t51, t52, t53, t54, t55;
  115. mad_fixed_t t56, t57, t58, t59, t60, t61, t62, t63;
  116. mad_fixed_t t64, t65, t66, t67, t68, t69, t70, t71;
  117. mad_fixed_t t72, t73, t74, t75, t76, t77, t78, t79;
  118. mad_fixed_t t80, t81, t82, t83, t84, t85, t86, t87;
  119. mad_fixed_t t88, t89, t90, t91, t92, t93, t94, t95;
  120. mad_fixed_t t96, t97, t98, t99, t100, t101, t102, t103;
  121. mad_fixed_t t104, t105, t106, t107, t108, t109, t110, t111;
  122. mad_fixed_t t112, t113, t114, t115, t116, t117, t118, t119;
  123. mad_fixed_t t120, t121, t122, t123, t124, t125, t126, t127;
  124. mad_fixed_t t128, t129, t130, t131, t132, t133, t134, t135;
  125. mad_fixed_t t136, t137, t138, t139, t140, t141, t142, t143;
  126. mad_fixed_t t144, t145, t146, t147, t148, t149, t150, t151;
  127. mad_fixed_t t152, t153, t154, t155, t156, t157, t158, t159;
  128. mad_fixed_t t160, t161, t162, t163, t164, t165, t166, t167;
  129. mad_fixed_t t168, t169, t170, t171, t172, t173, t174, t175;
  130. mad_fixed_t t176;
  131. /* costab[i] = cos(PI / (2 * 32) * i) */
  132. # if defined(OPT_DCTO)
  133. # define costab1 MAD_F(0x7fd8878e)
  134. # define costab2 MAD_F(0x7f62368f)
  135. # define costab3 MAD_F(0x7e9d55fc)
  136. # define costab4 MAD_F(0x7d8a5f40)
  137. # define costab5 MAD_F(0x7c29fbee)
  138. # define costab6 MAD_F(0x7a7d055b)
  139. # define costab7 MAD_F(0x78848414)
  140. # define costab8 MAD_F(0x7641af3d)
  141. # define costab9 MAD_F(0x73b5ebd1)
  142. # define costab10 MAD_F(0x70e2cbc6)
  143. # define costab11 MAD_F(0x6dca0d14)
  144. # define costab12 MAD_F(0x6a6d98a4)
  145. # define costab13 MAD_F(0x66cf8120)
  146. # define costab14 MAD_F(0x62f201ac)
  147. # define costab15 MAD_F(0x5ed77c8a)
  148. # define costab16 MAD_F(0x5a82799a)
  149. # define costab17 MAD_F(0x55f5a4d2)
  150. # define costab18 MAD_F(0x5133cc94)
  151. # define costab19 MAD_F(0x4c3fdff4)
  152. # define costab20 MAD_F(0x471cece7)
  153. # define costab21 MAD_F(0x41ce1e65)
  154. # define costab22 MAD_F(0x3c56ba70)
  155. # define costab23 MAD_F(0x36ba2014)
  156. # define costab24 MAD_F(0x30fbc54d)
  157. # define costab25 MAD_F(0x2b1f34eb)
  158. # define costab26 MAD_F(0x25280c5e)
  159. # define costab27 MAD_F(0x1f19f97b)
  160. # define costab28 MAD_F(0x18f8b83c)
  161. # define costab29 MAD_F(0x12c8106f)
  162. # define costab30 MAD_F(0x0c8bd35e)
  163. # define costab31 MAD_F(0x0647d97c)
  164. # else
  165. # define costab1 MAD_F(0x0ffb10f2) /* 0.998795456 */
  166. # define costab2 MAD_F(0x0fec46d2) /* 0.995184727 */
  167. # define costab3 MAD_F(0x0fd3aac0) /* 0.989176510 */
  168. # define costab4 MAD_F(0x0fb14be8) /* 0.980785280 */
  169. # define costab5 MAD_F(0x0f853f7e) /* 0.970031253 */
  170. # define costab6 MAD_F(0x0f4fa0ab) /* 0.956940336 */
  171. # define costab7 MAD_F(0x0f109082) /* 0.941544065 */
  172. # define costab8 MAD_F(0x0ec835e8) /* 0.923879533 */
  173. # define costab9 MAD_F(0x0e76bd7a) /* 0.903989293 */
  174. # define costab10 MAD_F(0x0e1c5979) /* 0.881921264 */
  175. # define costab11 MAD_F(0x0db941a3) /* 0.857728610 */
  176. # define costab12 MAD_F(0x0d4db315) /* 0.831469612 */
  177. # define costab13 MAD_F(0x0cd9f024) /* 0.803207531 */
  178. # define costab14 MAD_F(0x0c5e4036) /* 0.773010453 */
  179. # define costab15 MAD_F(0x0bdaef91) /* 0.740951125 */
  180. # define costab16 MAD_F(0x0b504f33) /* 0.707106781 */
  181. # define costab17 MAD_F(0x0abeb49a) /* 0.671558955 */
  182. # define costab18 MAD_F(0x0a267993) /* 0.634393284 */
  183. # define costab19 MAD_F(0x0987fbfe) /* 0.595699304 */
  184. # define costab20 MAD_F(0x08e39d9d) /* 0.555570233 */
  185. # define costab21 MAD_F(0x0839c3cd) /* 0.514102744 */
  186. # define costab22 MAD_F(0x078ad74e) /* 0.471396737 */
  187. # define costab23 MAD_F(0x06d74402) /* 0.427555093 */
  188. # define costab24 MAD_F(0x061f78aa) /* 0.382683432 */
  189. # define costab25 MAD_F(0x0563e69d) /* 0.336889853 */
  190. # define costab26 MAD_F(0x04a5018c) /* 0.290284677 */
  191. # define costab27 MAD_F(0x03e33f2f) /* 0.242980180 */
  192. # define costab28 MAD_F(0x031f1708) /* 0.195090322 */
  193. # define costab29 MAD_F(0x0259020e) /* 0.146730474 */
  194. # define costab30 MAD_F(0x01917a6c) /* 0.098017140 */
  195. # define costab31 MAD_F(0x00c8fb30) /* 0.049067674 */
  196. # endif
  197. t0 = in[0] + in[31]; t16 = MUL(in[0] - in[31], costab1);
  198. t1 = in[15] + in[16]; t17 = MUL(in[15] - in[16], costab31);
  199. t41 = t16 + t17;
  200. t59 = MUL(t16 - t17, costab2);
  201. t33 = t0 + t1;
  202. t50 = MUL(t0 - t1, costab2);
  203. t2 = in[7] + in[24]; t18 = MUL(in[7] - in[24], costab15);
  204. t3 = in[8] + in[23]; t19 = MUL(in[8] - in[23], costab17);
  205. t42 = t18 + t19;
  206. t60 = MUL(t18 - t19, costab30);
  207. t34 = t2 + t3;
  208. t51 = MUL(t2 - t3, costab30);
  209. t4 = in[3] + in[28]; t20 = MUL(in[3] - in[28], costab7);
  210. t5 = in[12] + in[19]; t21 = MUL(in[12] - in[19], costab25);
  211. t43 = t20 + t21;
  212. t61 = MUL(t20 - t21, costab14);
  213. t35 = t4 + t5;
  214. t52 = MUL(t4 - t5, costab14);
  215. t6 = in[4] + in[27]; t22 = MUL(in[4] - in[27], costab9);
  216. t7 = in[11] + in[20]; t23 = MUL(in[11] - in[20], costab23);
  217. t44 = t22 + t23;
  218. t62 = MUL(t22 - t23, costab18);
  219. t36 = t6 + t7;
  220. t53 = MUL(t6 - t7, costab18);
  221. t8 = in[1] + in[30]; t24 = MUL(in[1] - in[30], costab3);
  222. t9 = in[14] + in[17]; t25 = MUL(in[14] - in[17], costab29);
  223. t45 = t24 + t25;
  224. t63 = MUL(t24 - t25, costab6);
  225. t37 = t8 + t9;
  226. t54 = MUL(t8 - t9, costab6);
  227. t10 = in[6] + in[25]; t26 = MUL(in[6] - in[25], costab13);
  228. t11 = in[9] + in[22]; t27 = MUL(in[9] - in[22], costab19);
  229. t46 = t26 + t27;
  230. t64 = MUL(t26 - t27, costab26);
  231. t38 = t10 + t11;
  232. t55 = MUL(t10 - t11, costab26);
  233. t12 = in[2] + in[29]; t28 = MUL(in[2] - in[29], costab5);
  234. t13 = in[13] + in[18]; t29 = MUL(in[13] - in[18], costab27);
  235. t47 = t28 + t29;
  236. t65 = MUL(t28 - t29, costab10);
  237. t39 = t12 + t13;
  238. t56 = MUL(t12 - t13, costab10);
  239. t14 = in[5] + in[26]; t30 = MUL(in[5] - in[26], costab11);
  240. t15 = in[10] + in[21]; t31 = MUL(in[10] - in[21], costab21);
  241. t48 = t30 + t31;
  242. t66 = MUL(t30 - t31, costab22);
  243. t40 = t14 + t15;
  244. t57 = MUL(t14 - t15, costab22);
  245. t69 = t33 + t34; t89 = MUL(t33 - t34, costab4);
  246. t70 = t35 + t36; t90 = MUL(t35 - t36, costab28);
  247. t71 = t37 + t38; t91 = MUL(t37 - t38, costab12);
  248. t72 = t39 + t40; t92 = MUL(t39 - t40, costab20);
  249. t73 = t41 + t42; t94 = MUL(t41 - t42, costab4);
  250. t74 = t43 + t44; t95 = MUL(t43 - t44, costab28);
  251. t75 = t45 + t46; t96 = MUL(t45 - t46, costab12);
  252. t76 = t47 + t48; t97 = MUL(t47 - t48, costab20);
  253. t78 = t50 + t51; t100 = MUL(t50 - t51, costab4);
  254. t79 = t52 + t53; t101 = MUL(t52 - t53, costab28);
  255. t80 = t54 + t55; t102 = MUL(t54 - t55, costab12);
  256. t81 = t56 + t57; t103 = MUL(t56 - t57, costab20);
  257. t83 = t59 + t60; t106 = MUL(t59 - t60, costab4);
  258. t84 = t61 + t62; t107 = MUL(t61 - t62, costab28);
  259. t85 = t63 + t64; t108 = MUL(t63 - t64, costab12);
  260. t86 = t65 + t66; t109 = MUL(t65 - t66, costab20);
  261. t113 = t69 + t70;
  262. t114 = t71 + t72;
  263. /* 0 */ hi[15][slot] = SHIFT(t113 + t114);
  264. /* 16 */ lo[ 0][slot] = SHIFT(MUL(t113 - t114, costab16));
  265. t115 = t73 + t74;
  266. t116 = t75 + t76;
  267. t32 = t115 + t116;
  268. /* 1 */ hi[14][slot] = SHIFT(t32);
  269. t118 = t78 + t79;
  270. t119 = t80 + t81;
  271. t58 = t118 + t119;
  272. /* 2 */ hi[13][slot] = SHIFT(t58);
  273. t121 = t83 + t84;
  274. t122 = t85 + t86;
  275. t67 = t121 + t122;
  276. t49 = (t67 * 2) - t32;
  277. /* 3 */ hi[12][slot] = SHIFT(t49);
  278. t125 = t89 + t90;
  279. t126 = t91 + t92;
  280. t93 = t125 + t126;
  281. /* 4 */ hi[11][slot] = SHIFT(t93);
  282. t128 = t94 + t95;
  283. t129 = t96 + t97;
  284. t98 = t128 + t129;
  285. t68 = (t98 * 2) - t49;
  286. /* 5 */ hi[10][slot] = SHIFT(t68);
  287. t132 = t100 + t101;
  288. t133 = t102 + t103;
  289. t104 = t132 + t133;
  290. t82 = (t104 * 2) - t58;
  291. /* 6 */ hi[ 9][slot] = SHIFT(t82);
  292. t136 = t106 + t107;
  293. t137 = t108 + t109;
  294. t110 = t136 + t137;
  295. t87 = (t110 * 2) - t67;
  296. t77 = (t87 * 2) - t68;
  297. /* 7 */ hi[ 8][slot] = SHIFT(t77);
  298. t141 = MUL(t69 - t70, costab8);
  299. t142 = MUL(t71 - t72, costab24);
  300. t143 = t141 + t142;
  301. /* 8 */ hi[ 7][slot] = SHIFT(t143);
  302. /* 24 */ lo[ 8][slot] =
  303. SHIFT((MUL(t141 - t142, costab16) * 2) - t143);
  304. t144 = MUL(t73 - t74, costab8);
  305. t145 = MUL(t75 - t76, costab24);
  306. t146 = t144 + t145;
  307. t88 = (t146 * 2) - t77;
  308. /* 9 */ hi[ 6][slot] = SHIFT(t88);
  309. t148 = MUL(t78 - t79, costab8);
  310. t149 = MUL(t80 - t81, costab24);
  311. t150 = t148 + t149;
  312. t105 = (t150 * 2) - t82;
  313. /* 10 */ hi[ 5][slot] = SHIFT(t105);
  314. t152 = MUL(t83 - t84, costab8);
  315. t153 = MUL(t85 - t86, costab24);
  316. t154 = t152 + t153;
  317. t111 = (t154 * 2) - t87;
  318. t99 = (t111 * 2) - t88;
  319. /* 11 */ hi[ 4][slot] = SHIFT(t99);
  320. t157 = MUL(t89 - t90, costab8);
  321. t158 = MUL(t91 - t92, costab24);
  322. t159 = t157 + t158;
  323. t127 = (t159 * 2) - t93;
  324. /* 12 */ hi[ 3][slot] = SHIFT(t127);
  325. t160 = (MUL(t125 - t126, costab16) * 2) - t127;
  326. /* 20 */ lo[ 4][slot] = SHIFT(t160);
  327. /* 28 */ lo[12][slot] =
  328. SHIFT((((MUL(t157 - t158, costab16) * 2) - t159) * 2) - t160);
  329. t161 = MUL(t94 - t95, costab8);
  330. t162 = MUL(t96 - t97, costab24);
  331. t163 = t161 + t162;
  332. t130 = (t163 * 2) - t98;
  333. t112 = (t130 * 2) - t99;
  334. /* 13 */ hi[ 2][slot] = SHIFT(t112);
  335. t164 = (MUL(t128 - t129, costab16) * 2) - t130;
  336. t166 = MUL(t100 - t101, costab8);
  337. t167 = MUL(t102 - t103, costab24);
  338. t168 = t166 + t167;
  339. t134 = (t168 * 2) - t104;
  340. t120 = (t134 * 2) - t105;
  341. /* 14 */ hi[ 1][slot] = SHIFT(t120);
  342. t135 = (MUL(t118 - t119, costab16) * 2) - t120;
  343. /* 18 */ lo[ 2][slot] = SHIFT(t135);
  344. t169 = (MUL(t132 - t133, costab16) * 2) - t134;
  345. t151 = (t169 * 2) - t135;
  346. /* 22 */ lo[ 6][slot] = SHIFT(t151);
  347. t170 = (((MUL(t148 - t149, costab16) * 2) - t150) * 2) - t151;
  348. /* 26 */ lo[10][slot] = SHIFT(t170);
  349. /* 30 */ lo[14][slot] =
  350. SHIFT((((((MUL(t166 - t167, costab16) * 2) -
  351. t168) * 2) - t169) * 2) - t170);
  352. t171 = MUL(t106 - t107, costab8);
  353. t172 = MUL(t108 - t109, costab24);
  354. t173 = t171 + t172;
  355. t138 = (t173 * 2) - t110;
  356. t123 = (t138 * 2) - t111;
  357. t139 = (MUL(t121 - t122, costab16) * 2) - t123;
  358. t117 = (t123 * 2) - t112;
  359. /* 15 */ hi[ 0][slot] = SHIFT(t117);
  360. t124 = (MUL(t115 - t116, costab16) * 2) - t117;
  361. /* 17 */ lo[ 1][slot] = SHIFT(t124);
  362. t131 = (t139 * 2) - t124;
  363. /* 19 */ lo[ 3][slot] = SHIFT(t131);
  364. t140 = (t164 * 2) - t131;
  365. /* 21 */ lo[ 5][slot] = SHIFT(t140);
  366. t174 = (MUL(t136 - t137, costab16) * 2) - t138;
  367. t155 = (t174 * 2) - t139;
  368. t147 = (t155 * 2) - t140;
  369. /* 23 */ lo[ 7][slot] = SHIFT(t147);
  370. t156 = (((MUL(t144 - t145, costab16) * 2) - t146) * 2) - t147;
  371. /* 25 */ lo[ 9][slot] = SHIFT(t156);
  372. t175 = (((MUL(t152 - t153, costab16) * 2) - t154) * 2) - t155;
  373. t165 = (t175 * 2) - t156;
  374. /* 27 */ lo[11][slot] = SHIFT(t165);
  375. t176 = (((((MUL(t161 - t162, costab16) * 2) -
  376. t163) * 2) - t164) * 2) - t165;
  377. /* 29 */ lo[13][slot] = SHIFT(t176);
  378. /* 31 */ lo[15][slot] =
  379. SHIFT((((((((MUL(t171 - t172, costab16) * 2) -
  380. t173) * 2) - t174) * 2) - t175) * 2) - t176);
  381. /*
  382. * Totals:
  383. * 80 multiplies
  384. * 80 additions
  385. * 119 subtractions
  386. * 49 shifts (not counting SSO)
  387. */
  388. }
  389. # undef MUL
  390. # undef SHIFT
  391. /* third SSO shift and/or D[] optimization preshift */
  392. # if defined(OPT_SSO)
  393. # if MAD_F_FRACBITS != 28
  394. # error "MAD_F_FRACBITS must be 28 to use OPT_SSO"
  395. # endif
  396. # define ML0(hi, lo, x, y) ((lo) = (x) * (y))
  397. # define MLA(hi, lo, x, y) ((lo) += (x) * (y))
  398. # define MLN(hi, lo) ((lo) = -(lo))
  399. # define MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo))
  400. # define SHIFT(x) ((x) >> 2)
  401. # define PRESHIFT(x) ((MAD_F(x) + (1L << 13)) >> 14)
  402. # else
  403. # define ML0(hi, lo, x, y) MAD_F_ML0((hi), (lo), (x), (y))
  404. # define MLA(hi, lo, x, y) MAD_F_MLA((hi), (lo), (x), (y))
  405. # define MLN(hi, lo) MAD_F_MLN((hi), (lo))
  406. # define MLZ(hi, lo) MAD_F_MLZ((hi), (lo))
  407. # define SHIFT(x) (x)
  408. # if defined(MAD_F_SCALEBITS)
  409. # undef MAD_F_SCALEBITS
  410. # define MAD_F_SCALEBITS (MAD_F_FRACBITS - 12)
  411. # define PRESHIFT(x) (MAD_F(x) >> 12)
  412. # else
  413. # define PRESHIFT(x) MAD_F(x)
  414. # endif
  415. # endif
  416. static
  417. mad_fixed_t const D[17][32] = {
  418. # include "D.dat"
  419. };
  420. # if defined(ASO_SYNTH)
  421. void synth_full(struct mad_synth *, struct mad_frame const *,
  422. unsigned int, unsigned int);
  423. # else
  424. /*
  425. * NAME: synth->full()
  426. * DESCRIPTION: perform full frequency PCM synthesis
  427. */
  428. static
  429. void synth_full(struct mad_synth *synth, struct mad_frame const *frame,
  430. unsigned int nch, unsigned int ns)
  431. {
  432. unsigned int phase, ch, s, sb, pe, po;
  433. mad_fixed_t *pcm1, *pcm2, (*filter)[2][2][16][8];
  434. mad_fixed_t const (*sbsample)[36][32];
  435. register mad_fixed_t (*fe)[8], (*fx)[8], (*fo)[8];
  436. register mad_fixed_t const (*Dptr)[32], *ptr;
  437. register mad_fixed64hi_t hi;
  438. register mad_fixed64lo_t lo;
  439. for (ch = 0; ch < nch; ++ch) {
  440. sbsample = &frame->sbsample[ch];
  441. filter = &synth->filter[ch];
  442. phase = synth->phase;
  443. pcm1 = synth->pcm.samples[ch];
  444. for (s = 0; s < ns; ++s) {
  445. dct32((*sbsample)[s], phase >> 1,
  446. (*filter)[0][phase & 1], (*filter)[1][phase & 1]);
  447. pe = phase & ~1;
  448. po = ((phase - 1) & 0xf) | 1;
  449. /* calculate 32 samples */
  450. fe = &(*filter)[0][ phase & 1][0];
  451. fx = &(*filter)[0][~phase & 1][0];
  452. fo = &(*filter)[1][~phase & 1][0];
  453. Dptr = &D[0];
  454. ptr = *Dptr + po;
  455. ML0(hi, lo, (*fx)[0], ptr[ 0]);
  456. MLA(hi, lo, (*fx)[1], ptr[14]);
  457. MLA(hi, lo, (*fx)[2], ptr[12]);
  458. MLA(hi, lo, (*fx)[3], ptr[10]);
  459. MLA(hi, lo, (*fx)[4], ptr[ 8]);
  460. MLA(hi, lo, (*fx)[5], ptr[ 6]);
  461. MLA(hi, lo, (*fx)[6], ptr[ 4]);
  462. MLA(hi, lo, (*fx)[7], ptr[ 2]);
  463. MLN(hi, lo);
  464. ptr = *Dptr + pe;
  465. MLA(hi, lo, (*fe)[0], ptr[ 0]);
  466. MLA(hi, lo, (*fe)[1], ptr[14]);
  467. MLA(hi, lo, (*fe)[2], ptr[12]);
  468. MLA(hi, lo, (*fe)[3], ptr[10]);
  469. MLA(hi, lo, (*fe)[4], ptr[ 8]);
  470. MLA(hi, lo, (*fe)[5], ptr[ 6]);
  471. MLA(hi, lo, (*fe)[6], ptr[ 4]);
  472. MLA(hi, lo, (*fe)[7], ptr[ 2]);
  473. *pcm1++ = SHIFT(MLZ(hi, lo));
  474. pcm2 = pcm1 + 30;
  475. for (sb = 1; sb < 16; ++sb) {
  476. ++fe;
  477. ++Dptr;
  478. /* D[32 - sb][i] == -D[sb][31 - i] */
  479. ptr = *Dptr + po;
  480. ML0(hi, lo, (*fo)[0], ptr[ 0]);
  481. MLA(hi, lo, (*fo)[1], ptr[14]);
  482. MLA(hi, lo, (*fo)[2], ptr[12]);
  483. MLA(hi, lo, (*fo)[3], ptr[10]);
  484. MLA(hi, lo, (*fo)[4], ptr[ 8]);
  485. MLA(hi, lo, (*fo)[5], ptr[ 6]);
  486. MLA(hi, lo, (*fo)[6], ptr[ 4]);
  487. MLA(hi, lo, (*fo)[7], ptr[ 2]);
  488. MLN(hi, lo);
  489. ptr = *Dptr + pe;
  490. MLA(hi, lo, (*fe)[7], ptr[ 2]);
  491. MLA(hi, lo, (*fe)[6], ptr[ 4]);
  492. MLA(hi, lo, (*fe)[5], ptr[ 6]);
  493. MLA(hi, lo, (*fe)[4], ptr[ 8]);
  494. MLA(hi, lo, (*fe)[3], ptr[10]);
  495. MLA(hi, lo, (*fe)[2], ptr[12]);
  496. MLA(hi, lo, (*fe)[1], ptr[14]);
  497. MLA(hi, lo, (*fe)[0], ptr[ 0]);
  498. *pcm1++ = SHIFT(MLZ(hi, lo));
  499. ptr = *Dptr - pe;
  500. ML0(hi, lo, (*fe)[0], ptr[31 - 16]);
  501. MLA(hi, lo, (*fe)[1], ptr[31 - 14]);
  502. MLA(hi, lo, (*fe)[2], ptr[31 - 12]);
  503. MLA(hi, lo, (*fe)[3], ptr[31 - 10]);
  504. MLA(hi, lo, (*fe)[4], ptr[31 - 8]);
  505. MLA(hi, lo, (*fe)[5], ptr[31 - 6]);
  506. MLA(hi, lo, (*fe)[6], ptr[31 - 4]);
  507. MLA(hi, lo, (*fe)[7], ptr[31 - 2]);
  508. ptr = *Dptr - po;
  509. MLA(hi, lo, (*fo)[7], ptr[31 - 2]);
  510. MLA(hi, lo, (*fo)[6], ptr[31 - 4]);
  511. MLA(hi, lo, (*fo)[5], ptr[31 - 6]);
  512. MLA(hi, lo, (*fo)[4], ptr[31 - 8]);
  513. MLA(hi, lo, (*fo)[3], ptr[31 - 10]);
  514. MLA(hi, lo, (*fo)[2], ptr[31 - 12]);
  515. MLA(hi, lo, (*fo)[1], ptr[31 - 14]);
  516. MLA(hi, lo, (*fo)[0], ptr[31 - 16]);
  517. *pcm2-- = SHIFT(MLZ(hi, lo));
  518. ++fo;
  519. }
  520. ++Dptr;
  521. ptr = *Dptr + po;
  522. ML0(hi, lo, (*fo)[0], ptr[ 0]);
  523. MLA(hi, lo, (*fo)[1], ptr[14]);
  524. MLA(hi, lo, (*fo)[2], ptr[12]);
  525. MLA(hi, lo, (*fo)[3], ptr[10]);
  526. MLA(hi, lo, (*fo)[4], ptr[ 8]);
  527. MLA(hi, lo, (*fo)[5], ptr[ 6]);
  528. MLA(hi, lo, (*fo)[6], ptr[ 4]);
  529. MLA(hi, lo, (*fo)[7], ptr[ 2]);
  530. *pcm1 = SHIFT(-MLZ(hi, lo));
  531. pcm1 += 16;
  532. phase = (phase + 1) % 16;
  533. }
  534. }
  535. }
  536. # endif
  537. /*
  538. * NAME: synth->half()
  539. * DESCRIPTION: perform half frequency PCM synthesis
  540. */
  541. static
  542. void synth_half(struct mad_synth *synth, struct mad_frame const *frame,
  543. unsigned int nch, unsigned int ns)
  544. {
  545. unsigned int phase, ch, s, sb, pe, po;
  546. mad_fixed_t *pcm1, *pcm2, (*filter)[2][2][16][8];
  547. mad_fixed_t const (*sbsample)[36][32];
  548. register mad_fixed_t (*fe)[8], (*fx)[8], (*fo)[8];
  549. register mad_fixed_t const (*Dptr)[32], *ptr;
  550. register mad_fixed64hi_t hi;
  551. register mad_fixed64lo_t lo;
  552. for (ch = 0; ch < nch; ++ch) {
  553. sbsample = &frame->sbsample[ch];
  554. filter = &synth->filter[ch];
  555. phase = synth->phase;
  556. pcm1 = synth->pcm.samples[ch];
  557. for (s = 0; s < ns; ++s) {
  558. dct32((*sbsample)[s], phase >> 1,
  559. (*filter)[0][phase & 1], (*filter)[1][phase & 1]);
  560. pe = phase & ~1;
  561. po = ((phase - 1) & 0xf) | 1;
  562. /* calculate 16 samples */
  563. fe = &(*filter)[0][ phase & 1][0];
  564. fx = &(*filter)[0][~phase & 1][0];
  565. fo = &(*filter)[1][~phase & 1][0];
  566. Dptr = &D[0];
  567. ptr = *Dptr + po;
  568. ML0(hi, lo, (*fx)[0], ptr[ 0]);
  569. MLA(hi, lo, (*fx)[1], ptr[14]);
  570. MLA(hi, lo, (*fx)[2], ptr[12]);
  571. MLA(hi, lo, (*fx)[3], ptr[10]);
  572. MLA(hi, lo, (*fx)[4], ptr[ 8]);
  573. MLA(hi, lo, (*fx)[5], ptr[ 6]);
  574. MLA(hi, lo, (*fx)[6], ptr[ 4]);
  575. MLA(hi, lo, (*fx)[7], ptr[ 2]);
  576. MLN(hi, lo);
  577. ptr = *Dptr + pe;
  578. MLA(hi, lo, (*fe)[0], ptr[ 0]);
  579. MLA(hi, lo, (*fe)[1], ptr[14]);
  580. MLA(hi, lo, (*fe)[2], ptr[12]);
  581. MLA(hi, lo, (*fe)[3], ptr[10]);
  582. MLA(hi, lo, (*fe)[4], ptr[ 8]);
  583. MLA(hi, lo, (*fe)[5], ptr[ 6]);
  584. MLA(hi, lo, (*fe)[6], ptr[ 4]);
  585. MLA(hi, lo, (*fe)[7], ptr[ 2]);
  586. *pcm1++ = SHIFT(MLZ(hi, lo));
  587. pcm2 = pcm1 + 14;
  588. for (sb = 1; sb < 16; ++sb) {
  589. ++fe;
  590. ++Dptr;
  591. /* D[32 - sb][i] == -D[sb][31 - i] */
  592. if (!(sb & 1)) {
  593. ptr = *Dptr + po;
  594. ML0(hi, lo, (*fo)[0], ptr[ 0]);
  595. MLA(hi, lo, (*fo)[1], ptr[14]);
  596. MLA(hi, lo, (*fo)[2], ptr[12]);
  597. MLA(hi, lo, (*fo)[3], ptr[10]);
  598. MLA(hi, lo, (*fo)[4], ptr[ 8]);
  599. MLA(hi, lo, (*fo)[5], ptr[ 6]);
  600. MLA(hi, lo, (*fo)[6], ptr[ 4]);
  601. MLA(hi, lo, (*fo)[7], ptr[ 2]);
  602. MLN(hi, lo);
  603. ptr = *Dptr + pe;
  604. MLA(hi, lo, (*fe)[7], ptr[ 2]);
  605. MLA(hi, lo, (*fe)[6], ptr[ 4]);
  606. MLA(hi, lo, (*fe)[5], ptr[ 6]);
  607. MLA(hi, lo, (*fe)[4], ptr[ 8]);
  608. MLA(hi, lo, (*fe)[3], ptr[10]);
  609. MLA(hi, lo, (*fe)[2], ptr[12]);
  610. MLA(hi, lo, (*fe)[1], ptr[14]);
  611. MLA(hi, lo, (*fe)[0], ptr[ 0]);
  612. *pcm1++ = SHIFT(MLZ(hi, lo));
  613. ptr = *Dptr - po;
  614. ML0(hi, lo, (*fo)[7], ptr[31 - 2]);
  615. MLA(hi, lo, (*fo)[6], ptr[31 - 4]);
  616. MLA(hi, lo, (*fo)[5], ptr[31 - 6]);
  617. MLA(hi, lo, (*fo)[4], ptr[31 - 8]);
  618. MLA(hi, lo, (*fo)[3], ptr[31 - 10]);
  619. MLA(hi, lo, (*fo)[2], ptr[31 - 12]);
  620. MLA(hi, lo, (*fo)[1], ptr[31 - 14]);
  621. MLA(hi, lo, (*fo)[0], ptr[31 - 16]);
  622. ptr = *Dptr - pe;
  623. MLA(hi, lo, (*fe)[0], ptr[31 - 16]);
  624. MLA(hi, lo, (*fe)[1], ptr[31 - 14]);
  625. MLA(hi, lo, (*fe)[2], ptr[31 - 12]);
  626. MLA(hi, lo, (*fe)[3], ptr[31 - 10]);
  627. MLA(hi, lo, (*fe)[4], ptr[31 - 8]);
  628. MLA(hi, lo, (*fe)[5], ptr[31 - 6]);
  629. MLA(hi, lo, (*fe)[6], ptr[31 - 4]);
  630. MLA(hi, lo, (*fe)[7], ptr[31 - 2]);
  631. *pcm2-- = SHIFT(MLZ(hi, lo));
  632. }
  633. ++fo;
  634. }
  635. ++Dptr;
  636. ptr = *Dptr + po;
  637. ML0(hi, lo, (*fo)[0], ptr[ 0]);
  638. MLA(hi, lo, (*fo)[1], ptr[14]);
  639. MLA(hi, lo, (*fo)[2], ptr[12]);
  640. MLA(hi, lo, (*fo)[3], ptr[10]);
  641. MLA(hi, lo, (*fo)[4], ptr[ 8]);
  642. MLA(hi, lo, (*fo)[5], ptr[ 6]);
  643. MLA(hi, lo, (*fo)[6], ptr[ 4]);
  644. MLA(hi, lo, (*fo)[7], ptr[ 2]);
  645. *pcm1 = SHIFT(-MLZ(hi, lo));
  646. pcm1 += 8;
  647. phase = (phase + 1) % 16;
  648. }
  649. }
  650. }
  651. /*
  652. * NAME: synth->frame()
  653. * DESCRIPTION: perform PCM synthesis of frame subband samples
  654. */
  655. void mad_synth_frame(struct mad_synth *synth, struct mad_frame const *frame)
  656. {
  657. unsigned int nch, ns;
  658. void (*synth_frame)(struct mad_synth *, struct mad_frame const *,
  659. unsigned int, unsigned int);
  660. nch = MAD_NCHANNELS(&frame->header);
  661. ns = MAD_NSBSAMPLES(&frame->header);
  662. synth->pcm.samplerate = frame->header.samplerate;
  663. synth->pcm.channels = nch;
  664. synth->pcm.length = 32 * ns;
  665. synth_frame = synth_full;
  666. if (frame->options & MAD_OPTION_HALFSAMPLERATE) {
  667. synth->pcm.samplerate /= 2;
  668. synth->pcm.length /= 2;
  669. synth_frame = synth_half;
  670. }
  671. synth_frame(synth, frame, nch, ns);
  672. synth->phase = (synth->phase + ns) % 16;
  673. }