synth.c

00001 /*
00002  * libmad - MPEG audio decoder library
00003  * Copyright (C) 2000-2003 Underbit Technologies, Inc.
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  *
00019  * $Id: synth.c,v 1.1 2003/08/31 19:00:15 gabest Exp $
00020  */
00021 
00022 # ifdef HAVE_CONFIG_H
00023 #  include "config.h"
00024 # endif
00025 
00026 # include "global.h"
00027 
00028 # include "fixed.h"
00029 # include "frame.h"
00030 # include "synth.h"
00031 
00032 /*
00033  * NAME:        synth->init()
00034  * DESCRIPTION: initialize synth struct
00035  */
00036 void mad_synth_init(struct mad_synth *synth)
00037 {
00038   mad_synth_mute(synth);
00039 
00040   synth->phase = 0;
00041 
00042   synth->pcm.samplerate = 0;
00043   synth->pcm.channels   = 0;
00044   synth->pcm.length     = 0;
00045 }
00046 
00047 /*
00048  * NAME:        synth->mute()
00049  * DESCRIPTION: zero all polyphase filterbank values, resetting synthesis
00050  */
00051 void mad_synth_mute(struct mad_synth *synth)
00052 {
00053   unsigned int ch, s, v;
00054 
00055   for (ch = 0; ch < 2; ++ch) {
00056     for (s = 0; s < 16; ++s) {
00057       for (v = 0; v < 8; ++v) {
00058         synth->filter[ch][0][0][s][v] = synth->filter[ch][0][1][s][v] =
00059         synth->filter[ch][1][0][s][v] = synth->filter[ch][1][1][s][v] = 0;
00060       }
00061     }
00062   }
00063 }
00064 
00065 /*
00066  * An optional optimization called here the Subband Synthesis Optimization
00067  * (SSO) improves the performance of subband synthesis at the expense of
00068  * accuracy.
00069  *
00070  * The idea is to simplify 32x32->64-bit multiplication to 32x32->32 such
00071  * that extra scaling and rounding are not necessary. This often allows the
00072  * compiler to use faster 32-bit multiply-accumulate instructions instead of
00073  * explicit 64-bit multiply, shift, and add instructions.
00074  *
00075  * SSO works like this: a full 32x32->64-bit multiply of two mad_fixed_t
00076  * values requires the result to be right-shifted 28 bits to be properly
00077  * scaled to the same fixed-point format. Right shifts can be applied at any
00078  * time to either operand or to the result, so the optimization involves
00079  * careful placement of these shifts to minimize the loss of accuracy.
00080  *
00081  * First, a 14-bit shift is applied with rounding at compile-time to the D[]
00082  * table of coefficients for the subband synthesis window. This only loses 2
00083  * bits of accuracy because the lower 12 bits are always zero. A second
00084  * 12-bit shift occurs after the DCT calculation. This loses 12 bits of
00085  * accuracy. Finally, a third 2-bit shift occurs just before the sample is
00086  * saved in the PCM buffer. 14 + 12 + 2 == 28 bits.
00087  */
00088 
00089 /* FPM_DEFAULT without OPT_SSO will actually lose accuracy and performance */
00090 
00091 # if defined(FPM_DEFAULT) && !defined(OPT_SSO)
00092 #  define OPT_SSO
00093 # endif
00094 
00095 /* second SSO shift, with rounding */
00096 
00097 # if defined(OPT_SSO)
00098 #  define SHIFT(x)  (((x) + (1L << 11)) >> 12)
00099 # else
00100 #  define SHIFT(x)  (x)
00101 # endif
00102 
00103 /* possible DCT speed optimization */
00104 
00105 # if defined(OPT_SPEED) && defined(MAD_F_MLX)
00106 #  define OPT_DCTO
00107 #  define MUL(x, y)  \
00108     ({ mad_fixed64hi_t hi;  \
00109        mad_fixed64lo_t lo;  \
00110        MAD_F_MLX(hi, lo, (x), (y));  \
00111        hi << (32 - MAD_F_SCALEBITS - 3);  \
00112     })
00113 # else
00114 #  undef OPT_DCTO
00115 #  define MUL(x, y)  mad_f_mul((x), (y))
00116 # endif
00117 
00118 /*
00119  * NAME:        dct32()
00120  * DESCRIPTION: perform fast in[32]->out[32] DCT
00121  */
00122 static
00123 void dct32(mad_fixed_t const in[32], unsigned int slot,
00124            mad_fixed_t lo[16][8], mad_fixed_t hi[16][8])
00125 {
00126   mad_fixed_t t0,   t1,   t2,   t3,   t4,   t5,   t6,   t7;
00127   mad_fixed_t t8,   t9,   t10,  t11,  t12,  t13,  t14,  t15;
00128   mad_fixed_t t16,  t17,  t18,  t19,  t20,  t21,  t22,  t23;
00129   mad_fixed_t t24,  t25,  t26,  t27,  t28,  t29,  t30,  t31;
00130   mad_fixed_t t32,  t33,  t34,  t35,  t36,  t37,  t38,  t39;
00131   mad_fixed_t t40,  t41,  t42,  t43,  t44,  t45,  t46,  t47;
00132   mad_fixed_t t48,  t49,  t50,  t51,  t52,  t53,  t54,  t55;
00133   mad_fixed_t t56,  t57,  t58,  t59,  t60,  t61,  t62,  t63;
00134   mad_fixed_t t64,  t65,  t66,  t67,  t68,  t69,  t70,  t71;
00135   mad_fixed_t t72,  t73,  t74,  t75,  t76,  t77,  t78,  t79;
00136   mad_fixed_t t80,  t81,  t82,  t83,  t84,  t85,  t86,  t87;
00137   mad_fixed_t t88,  t89,  t90,  t91,  t92,  t93,  t94,  t95;
00138   mad_fixed_t t96,  t97,  t98,  t99,  t100, t101, t102, t103;
00139   mad_fixed_t t104, t105, t106, t107, t108, t109, t110, t111;
00140   mad_fixed_t t112, t113, t114, t115, t116, t117, t118, t119;
00141   mad_fixed_t t120, t121, t122, t123, t124, t125, t126, t127;
00142   mad_fixed_t t128, t129, t130, t131, t132, t133, t134, t135;
00143   mad_fixed_t t136, t137, t138, t139, t140, t141, t142, t143;
00144   mad_fixed_t t144, t145, t146, t147, t148, t149, t150, t151;
00145   mad_fixed_t t152, t153, t154, t155, t156, t157, t158, t159;
00146   mad_fixed_t t160, t161, t162, t163, t164, t165, t166, t167;
00147   mad_fixed_t t168, t169, t170, t171, t172, t173, t174, t175;
00148   mad_fixed_t t176;
00149 
00150   /* costab[i] = cos(PI / (2 * 32) * i) */
00151 
00152 # if defined(OPT_DCTO)
00153 #  define costab1       MAD_F(0x7fd8878e)
00154 #  define costab2       MAD_F(0x7f62368f)
00155 #  define costab3       MAD_F(0x7e9d55fc)
00156 #  define costab4       MAD_F(0x7d8a5f40)
00157 #  define costab5       MAD_F(0x7c29fbee)
00158 #  define costab6       MAD_F(0x7a7d055b)
00159 #  define costab7       MAD_F(0x78848414)
00160 #  define costab8       MAD_F(0x7641af3d)
00161 #  define costab9       MAD_F(0x73b5ebd1)
00162 #  define costab10      MAD_F(0x70e2cbc6)
00163 #  define costab11      MAD_F(0x6dca0d14)
00164 #  define costab12      MAD_F(0x6a6d98a4)
00165 #  define costab13      MAD_F(0x66cf8120)
00166 #  define costab14      MAD_F(0x62f201ac)
00167 #  define costab15      MAD_F(0x5ed77c8a)
00168 #  define costab16      MAD_F(0x5a82799a)
00169 #  define costab17      MAD_F(0x55f5a4d2)
00170 #  define costab18      MAD_F(0x5133cc94)
00171 #  define costab19      MAD_F(0x4c3fdff4)
00172 #  define costab20      MAD_F(0x471cece7)
00173 #  define costab21      MAD_F(0x41ce1e65)
00174 #  define costab22      MAD_F(0x3c56ba70)
00175 #  define costab23      MAD_F(0x36ba2014)
00176 #  define costab24      MAD_F(0x30fbc54d)
00177 #  define costab25      MAD_F(0x2b1f34eb)
00178 #  define costab26      MAD_F(0x25280c5e)
00179 #  define costab27      MAD_F(0x1f19f97b)
00180 #  define costab28      MAD_F(0x18f8b83c)
00181 #  define costab29      MAD_F(0x12c8106f)
00182 #  define costab30      MAD_F(0x0c8bd35e)
00183 #  define costab31      MAD_F(0x0647d97c)
00184 # else
00185 #  define costab1       MAD_F(0x0ffb10f2)  /* 0.998795456 */
00186 #  define costab2       MAD_F(0x0fec46d2)  /* 0.995184727 */
00187 #  define costab3       MAD_F(0x0fd3aac0)  /* 0.989176510 */
00188 #  define costab4       MAD_F(0x0fb14be8)  /* 0.980785280 */
00189 #  define costab5       MAD_F(0x0f853f7e)  /* 0.970031253 */
00190 #  define costab6       MAD_F(0x0f4fa0ab)  /* 0.956940336 */
00191 #  define costab7       MAD_F(0x0f109082)  /* 0.941544065 */
00192 #  define costab8       MAD_F(0x0ec835e8)  /* 0.923879533 */
00193 #  define costab9       MAD_F(0x0e76bd7a)  /* 0.903989293 */
00194 #  define costab10      MAD_F(0x0e1c5979)  /* 0.881921264 */
00195 #  define costab11      MAD_F(0x0db941a3)  /* 0.857728610 */
00196 #  define costab12      MAD_F(0x0d4db315)  /* 0.831469612 */
00197 #  define costab13      MAD_F(0x0cd9f024)  /* 0.803207531 */
00198 #  define costab14      MAD_F(0x0c5e4036)  /* 0.773010453 */
00199 #  define costab15      MAD_F(0x0bdaef91)  /* 0.740951125 */
00200 #  define costab16      MAD_F(0x0b504f33)  /* 0.707106781 */
00201 #  define costab17      MAD_F(0x0abeb49a)  /* 0.671558955 */
00202 #  define costab18      MAD_F(0x0a267993)  /* 0.634393284 */
00203 #  define costab19      MAD_F(0x0987fbfe)  /* 0.595699304 */
00204 #  define costab20      MAD_F(0x08e39d9d)  /* 0.555570233 */
00205 #  define costab21      MAD_F(0x0839c3cd)  /* 0.514102744 */
00206 #  define costab22      MAD_F(0x078ad74e)  /* 0.471396737 */
00207 #  define costab23      MAD_F(0x06d74402)  /* 0.427555093 */
00208 #  define costab24      MAD_F(0x061f78aa)  /* 0.382683432 */
00209 #  define costab25      MAD_F(0x0563e69d)  /* 0.336889853 */
00210 #  define costab26      MAD_F(0x04a5018c)  /* 0.290284677 */
00211 #  define costab27      MAD_F(0x03e33f2f)  /* 0.242980180 */
00212 #  define costab28      MAD_F(0x031f1708)  /* 0.195090322 */
00213 #  define costab29      MAD_F(0x0259020e)  /* 0.146730474 */
00214 #  define costab30      MAD_F(0x01917a6c)  /* 0.098017140 */
00215 #  define costab31      MAD_F(0x00c8fb30)  /* 0.049067674 */
00216 # endif
00217 
00218   t0   = in[0]  + in[31];  t16  = MUL(in[0]  - in[31], costab1);
00219   t1   = in[15] + in[16];  t17  = MUL(in[15] - in[16], costab31);
00220 
00221   t41  = t16 + t17;
00222   t59  = MUL(t16 - t17, costab2);
00223   t33  = t0  + t1;
00224   t50  = MUL(t0  - t1,  costab2);
00225 
00226   t2   = in[7]  + in[24];  t18  = MUL(in[7]  - in[24], costab15);
00227   t3   = in[8]  + in[23];  t19  = MUL(in[8]  - in[23], costab17);
00228 
00229   t42  = t18 + t19;
00230   t60  = MUL(t18 - t19, costab30);
00231   t34  = t2  + t3;
00232   t51  = MUL(t2  - t3,  costab30);
00233 
00234   t4   = in[3]  + in[28];  t20  = MUL(in[3]  - in[28], costab7);
00235   t5   = in[12] + in[19];  t21  = MUL(in[12] - in[19], costab25);
00236 
00237   t43  = t20 + t21;
00238   t61  = MUL(t20 - t21, costab14);
00239   t35  = t4  + t5;
00240   t52  = MUL(t4  - t5,  costab14);
00241 
00242   t6   = in[4]  + in[27];  t22  = MUL(in[4]  - in[27], costab9);
00243   t7   = in[11] + in[20];  t23  = MUL(in[11] - in[20], costab23);
00244 
00245   t44  = t22 + t23;
00246   t62  = MUL(t22 - t23, costab18);
00247   t36  = t6  + t7;
00248   t53  = MUL(t6  - t7,  costab18);
00249 
00250   t8   = in[1]  + in[30];  t24  = MUL(in[1]  - in[30], costab3);
00251   t9   = in[14] + in[17];  t25  = MUL(in[14] - in[17], costab29);
00252 
00253   t45  = t24 + t25;
00254   t63  = MUL(t24 - t25, costab6);
00255   t37  = t8  + t9;
00256   t54  = MUL(t8  - t9,  costab6);
00257 
00258   t10  = in[6]  + in[25];  t26  = MUL(in[6]  - in[25], costab13);
00259   t11  = in[9]  + in[22];  t27  = MUL(in[9]  - in[22], costab19);
00260 
00261   t46  = t26 + t27;
00262   t64  = MUL(t26 - t27, costab26);
00263   t38  = t10 + t11;
00264   t55  = MUL(t10 - t11, costab26);
00265 
00266   t12  = in[2]  + in[29];  t28  = MUL(in[2]  - in[29], costab5);
00267   t13  = in[13] + in[18];  t29  = MUL(in[13] - in[18], costab27);
00268 
00269   t47  = t28 + t29;
00270   t65  = MUL(t28 - t29, costab10);
00271   t39  = t12 + t13;
00272   t56  = MUL(t12 - t13, costab10);
00273 
00274   t14  = in[5]  + in[26];  t30  = MUL(in[5]  - in[26], costab11);
00275   t15  = in[10] + in[21];  t31  = MUL(in[10] - in[21], costab21);
00276 
00277   t48  = t30 + t31;
00278   t66  = MUL(t30 - t31, costab22);
00279   t40  = t14 + t15;
00280   t57  = MUL(t14 - t15, costab22);
00281 
00282   t69  = t33 + t34;  t89  = MUL(t33 - t34, costab4);
00283   t70  = t35 + t36;  t90  = MUL(t35 - t36, costab28);
00284   t71  = t37 + t38;  t91  = MUL(t37 - t38, costab12);
00285   t72  = t39 + t40;  t92  = MUL(t39 - t40, costab20);
00286   t73  = t41 + t42;  t94  = MUL(t41 - t42, costab4);
00287   t74  = t43 + t44;  t95  = MUL(t43 - t44, costab28);
00288   t75  = t45 + t46;  t96  = MUL(t45 - t46, costab12);
00289   t76  = t47 + t48;  t97  = MUL(t47 - t48, costab20);
00290 
00291   t78  = t50 + t51;  t100 = MUL(t50 - t51, costab4);
00292   t79  = t52 + t53;  t101 = MUL(t52 - t53, costab28);
00293   t80  = t54 + t55;  t102 = MUL(t54 - t55, costab12);
00294   t81  = t56 + t57;  t103 = MUL(t56 - t57, costab20);
00295 
00296   t83  = t59 + t60;  t106 = MUL(t59 - t60, costab4);
00297   t84  = t61 + t62;  t107 = MUL(t61 - t62, costab28);
00298   t85  = t63 + t64;  t108 = MUL(t63 - t64, costab12);
00299   t86  = t65 + t66;  t109 = MUL(t65 - t66, costab20);
00300 
00301   t113 = t69  + t70;
00302   t114 = t71  + t72;
00303 
00304   /*  0 */ hi[15][slot] = SHIFT(t113 + t114);
00305   /* 16 */ lo[ 0][slot] = SHIFT(MUL(t113 - t114, costab16));
00306 
00307   t115 = t73  + t74;
00308   t116 = t75  + t76;
00309 
00310   t32  = t115 + t116;
00311 
00312   /*  1 */ hi[14][slot] = SHIFT(t32);
00313 
00314   t118 = t78  + t79;
00315   t119 = t80  + t81;
00316 
00317   t58  = t118 + t119;
00318 
00319   /*  2 */ hi[13][slot] = SHIFT(t58);
00320 
00321   t121 = t83  + t84;
00322   t122 = t85  + t86;
00323 
00324   t67  = t121 + t122;
00325 
00326   t49  = (t67 * 2) - t32;
00327 
00328   /*  3 */ hi[12][slot] = SHIFT(t49);
00329 
00330   t125 = t89  + t90;
00331   t126 = t91  + t92;
00332 
00333   t93  = t125 + t126;
00334 
00335   /*  4 */ hi[11][slot] = SHIFT(t93);
00336 
00337   t128 = t94  + t95;
00338   t129 = t96  + t97;
00339 
00340   t98  = t128 + t129;
00341 
00342   t68  = (t98 * 2) - t49;
00343 
00344   /*  5 */ hi[10][slot] = SHIFT(t68);
00345 
00346   t132 = t100 + t101;
00347   t133 = t102 + t103;
00348 
00349   t104 = t132 + t133;
00350 
00351   t82  = (t104 * 2) - t58;
00352 
00353   /*  6 */ hi[ 9][slot] = SHIFT(t82);
00354 
00355   t136 = t106 + t107;
00356   t137 = t108 + t109;
00357 
00358   t110 = t136 + t137;
00359 
00360   t87  = (t110 * 2) - t67;
00361 
00362   t77  = (t87 * 2) - t68;
00363 
00364   /*  7 */ hi[ 8][slot] = SHIFT(t77);
00365 
00366   t141 = MUL(t69 - t70, costab8);
00367   t142 = MUL(t71 - t72, costab24);
00368   t143 = t141 + t142;
00369 
00370   /*  8 */ hi[ 7][slot] = SHIFT(t143);
00371   /* 24 */ lo[ 8][slot] =
00372              SHIFT((MUL(t141 - t142, costab16) * 2) - t143);
00373 
00374   t144 = MUL(t73 - t74, costab8);
00375   t145 = MUL(t75 - t76, costab24);
00376   t146 = t144 + t145;
00377 
00378   t88  = (t146 * 2) - t77;
00379 
00380   /*  9 */ hi[ 6][slot] = SHIFT(t88);
00381 
00382   t148 = MUL(t78 - t79, costab8);
00383   t149 = MUL(t80 - t81, costab24);
00384   t150 = t148 + t149;
00385 
00386   t105 = (t150 * 2) - t82;
00387 
00388   /* 10 */ hi[ 5][slot] = SHIFT(t105);
00389 
00390   t152 = MUL(t83 - t84, costab8);
00391   t153 = MUL(t85 - t86, costab24);
00392   t154 = t152 + t153;
00393 
00394   t111 = (t154 * 2) - t87;
00395 
00396   t99  = (t111 * 2) - t88;
00397 
00398   /* 11 */ hi[ 4][slot] = SHIFT(t99);
00399 
00400   t157 = MUL(t89 - t90, costab8);
00401   t158 = MUL(t91 - t92, costab24);
00402   t159 = t157 + t158;
00403 
00404   t127 = (t159 * 2) - t93;
00405 
00406   /* 12 */ hi[ 3][slot] = SHIFT(t127);
00407 
00408   t160 = (MUL(t125 - t126, costab16) * 2) - t127;
00409 
00410   /* 20 */ lo[ 4][slot] = SHIFT(t160);
00411   /* 28 */ lo[12][slot] =
00412              SHIFT((((MUL(t157 - t158, costab16) * 2) - t159) * 2) - t160);
00413 
00414   t161 = MUL(t94 - t95, costab8);
00415   t162 = MUL(t96 - t97, costab24);
00416   t163 = t161 + t162;
00417 
00418   t130 = (t163 * 2) - t98;
00419 
00420   t112 = (t130 * 2) - t99;
00421 
00422   /* 13 */ hi[ 2][slot] = SHIFT(t112);
00423 
00424   t164 = (MUL(t128 - t129, costab16) * 2) - t130;
00425 
00426   t166 = MUL(t100 - t101, costab8);
00427   t167 = MUL(t102 - t103, costab24);
00428   t168 = t166 + t167;
00429 
00430   t134 = (t168 * 2) - t104;
00431 
00432   t120 = (t134 * 2) - t105;
00433 
00434   /* 14 */ hi[ 1][slot] = SHIFT(t120);
00435 
00436   t135 = (MUL(t118 - t119, costab16) * 2) - t120;
00437 
00438   /* 18 */ lo[ 2][slot] = SHIFT(t135);
00439 
00440   t169 = (MUL(t132 - t133, costab16) * 2) - t134;
00441 
00442   t151 = (t169 * 2) - t135;
00443 
00444   /* 22 */ lo[ 6][slot] = SHIFT(t151);
00445 
00446   t170 = (((MUL(t148 - t149, costab16) * 2) - t150) * 2) - t151;
00447 
00448   /* 26 */ lo[10][slot] = SHIFT(t170);
00449   /* 30 */ lo[14][slot] =
00450              SHIFT((((((MUL(t166 - t167, costab16) * 2) -
00451                        t168) * 2) - t169) * 2) - t170);
00452 
00453   t171 = MUL(t106 - t107, costab8);
00454   t172 = MUL(t108 - t109, costab24);
00455   t173 = t171 + t172;
00456 
00457   t138 = (t173 * 2) - t110;
00458 
00459   t123 = (t138 * 2) - t111;
00460 
00461   t139 = (MUL(t121 - t122, costab16) * 2) - t123;
00462 
00463   t117 = (t123 * 2) - t112;
00464 
00465   /* 15 */ hi[ 0][slot] = SHIFT(t117);
00466 
00467   t124 = (MUL(t115 - t116, costab16) * 2) - t117;
00468 
00469   /* 17 */ lo[ 1][slot] = SHIFT(t124);
00470 
00471   t131 = (t139 * 2) - t124;
00472 
00473   /* 19 */ lo[ 3][slot] = SHIFT(t131);
00474 
00475   t140 = (t164 * 2) - t131;
00476 
00477   /* 21 */ lo[ 5][slot] = SHIFT(t140);
00478 
00479   t174 = (MUL(t136 - t137, costab16) * 2) - t138;
00480 
00481   t155 = (t174 * 2) - t139;
00482 
00483   t147 = (t155 * 2) - t140;
00484 
00485   /* 23 */ lo[ 7][slot] = SHIFT(t147);
00486 
00487   t156 = (((MUL(t144 - t145, costab16) * 2) - t146) * 2) - t147;
00488 
00489   /* 25 */ lo[ 9][slot] = SHIFT(t156);
00490 
00491   t175 = (((MUL(t152 - t153, costab16) * 2) - t154) * 2) - t155;
00492 
00493   t165 = (t175 * 2) - t156;
00494 
00495   /* 27 */ lo[11][slot] = SHIFT(t165);
00496 
00497   t176 = (((((MUL(t161 - t162, costab16) * 2) -
00498              t163) * 2) - t164) * 2) - t165;
00499 
00500   /* 29 */ lo[13][slot] = SHIFT(t176);
00501   /* 31 */ lo[15][slot] =
00502              SHIFT((((((((MUL(t171 - t172, costab16) * 2) -
00503                          t173) * 2) - t174) * 2) - t175) * 2) - t176);
00504 
00505   /*
00506    * Totals:
00507    *  80 multiplies
00508    *  80 additions
00509    * 119 subtractions
00510    *  49 shifts (not counting SSO)
00511    */
00512 }
00513 
00514 # undef MUL
00515 # undef SHIFT
00516 
00517 /* third SSO shift and/or D[] optimization preshift */
00518 
00519 # if defined(OPT_SSO)
00520 #  if MAD_F_FRACBITS != 28
00521 #   error "MAD_F_FRACBITS must be 28 to use OPT_SSO"
00522 #  endif
00523 #  define ML0(hi, lo, x, y)     ((lo)  = (x) * (y))
00524 #  define MLA(hi, lo, x, y)     ((lo) += (x) * (y))
00525 #  define MLN(hi, lo)           ((lo)  = -(lo))
00526 #  define MLZ(hi, lo)           ((void) (hi), (mad_fixed_t) (lo))
00527 #  define SHIFT(x)              ((x) >> 2)
00528 #  define PRESHIFT(x)           ((MAD_F(x) + (1L << 13)) >> 14)
00529 # else
00530 #  define ML0(hi, lo, x, y)     MAD_F_ML0((hi), (lo), (x), (y))
00531 #  define MLA(hi, lo, x, y)     MAD_F_MLA((hi), (lo), (x), (y))
00532 #  define MLN(hi, lo)           MAD_F_MLN((hi), (lo))
00533 #  define MLZ(hi, lo)           MAD_F_MLZ((hi), (lo))
00534 #  define SHIFT(x)              (x)
00535 #  if defined(MAD_F_SCALEBITS)
00536 #   undef  MAD_F_SCALEBITS
00537 #   define MAD_F_SCALEBITS      (MAD_F_FRACBITS - 12)
00538 #   define PRESHIFT(x)          (MAD_F(x) >> 12)
00539 #  else
00540 #   define PRESHIFT(x)          MAD_F(x)
00541 #  endif
00542 # endif
00543 
00544 static
00545 mad_fixed_t const D[17][32] = {
00546 # include "D.dat"
00547 };
00548 
00549 # if defined(ASO_SYNTH)
00550 void synth_full(struct mad_synth *, struct mad_frame const *,
00551                 unsigned int, unsigned int);
00552 # else
00553 /*
00554  * NAME:        synth->full()
00555  * DESCRIPTION: perform full frequency PCM synthesis
00556  */
00557 static
00558 void synth_full(struct mad_synth *synth, struct mad_frame const *frame,
00559                 unsigned int nch, unsigned int ns)
00560 {
00561   unsigned int phase, ch, s, sb, pe, po;
00562   mad_fixed_t *pcm1, *pcm2, (*filter)[2][2][16][8];
00563   mad_fixed_t const (*sbsample)[36][32];
00564   register mad_fixed_t (*fe)[8], (*fx)[8], (*fo)[8];
00565   register mad_fixed_t const (*Dptr)[32], *ptr;
00566   register mad_fixed64hi_t hi;
00567   register mad_fixed64lo_t lo;
00568 
00569   for (ch = 0; ch < nch; ++ch) {
00570     sbsample = &frame->sbsample[ch];
00571     filter   = &synth->filter[ch];
00572     phase    = synth->phase;
00573     pcm1     = synth->pcm.samples[ch];
00574 
00575     for (s = 0; s < ns; ++s) {
00576       dct32((*sbsample)[s], phase >> 1,
00577             (*filter)[0][phase & 1], (*filter)[1][phase & 1]);
00578 
00579       pe = phase & ~1;
00580       po = ((phase - 1) & 0xf) | 1;
00581 
00582       /* calculate 32 samples */
00583 
00584       fe = &(*filter)[0][ phase & 1][0];
00585       fx = &(*filter)[0][~phase & 1][0];
00586       fo = &(*filter)[1][~phase & 1][0];
00587 
00588       Dptr = &D[0];
00589 
00590       ptr = *Dptr + po;
00591       ML0(hi, lo, (*fx)[0], ptr[ 0]);
00592       MLA(hi, lo, (*fx)[1], ptr[14]);
00593       MLA(hi, lo, (*fx)[2], ptr[12]);
00594       MLA(hi, lo, (*fx)[3], ptr[10]);
00595       MLA(hi, lo, (*fx)[4], ptr[ 8]);
00596       MLA(hi, lo, (*fx)[5], ptr[ 6]);
00597       MLA(hi, lo, (*fx)[6], ptr[ 4]);
00598       MLA(hi, lo, (*fx)[7], ptr[ 2]);
00599       MLN(hi, lo);
00600 
00601       ptr = *Dptr + pe;
00602       MLA(hi, lo, (*fe)[0], ptr[ 0]);
00603       MLA(hi, lo, (*fe)[1], ptr[14]);
00604       MLA(hi, lo, (*fe)[2], ptr[12]);
00605       MLA(hi, lo, (*fe)[3], ptr[10]);
00606       MLA(hi, lo, (*fe)[4], ptr[ 8]);
00607       MLA(hi, lo, (*fe)[5], ptr[ 6]);
00608       MLA(hi, lo, (*fe)[6], ptr[ 4]);
00609       MLA(hi, lo, (*fe)[7], ptr[ 2]);
00610 
00611       *pcm1++ = SHIFT(MLZ(hi, lo));
00612 
00613       pcm2 = pcm1 + 30;
00614 
00615       for (sb = 1; sb < 16; ++sb) {
00616         ++fe;
00617         ++Dptr;
00618 
00619         /* D[32 - sb][i] == -D[sb][31 - i] */
00620 
00621         ptr = *Dptr + po;
00622         ML0(hi, lo, (*fo)[0], ptr[ 0]);
00623         MLA(hi, lo, (*fo)[1], ptr[14]);
00624         MLA(hi, lo, (*fo)[2], ptr[12]);
00625         MLA(hi, lo, (*fo)[3], ptr[10]);
00626         MLA(hi, lo, (*fo)[4], ptr[ 8]);
00627         MLA(hi, lo, (*fo)[5], ptr[ 6]);
00628         MLA(hi, lo, (*fo)[6], ptr[ 4]);
00629         MLA(hi, lo, (*fo)[7], ptr[ 2]);
00630         MLN(hi, lo);
00631 
00632         ptr = *Dptr + pe;
00633         MLA(hi, lo, (*fe)[7], ptr[ 2]);
00634         MLA(hi, lo, (*fe)[6], ptr[ 4]);
00635         MLA(hi, lo, (*fe)[5], ptr[ 6]);
00636         MLA(hi, lo, (*fe)[4], ptr[ 8]);
00637         MLA(hi, lo, (*fe)[3], ptr[10]);
00638         MLA(hi, lo, (*fe)[2], ptr[12]);
00639         MLA(hi, lo, (*fe)[1], ptr[14]);
00640         MLA(hi, lo, (*fe)[0], ptr[ 0]);
00641 
00642         *pcm1++ = SHIFT(MLZ(hi, lo));
00643 
00644         ptr = *Dptr - pe;
00645         ML0(hi, lo, (*fe)[0], ptr[31 - 16]);
00646         MLA(hi, lo, (*fe)[1], ptr[31 - 14]);
00647         MLA(hi, lo, (*fe)[2], ptr[31 - 12]);
00648         MLA(hi, lo, (*fe)[3], ptr[31 - 10]);
00649         MLA(hi, lo, (*fe)[4], ptr[31 -  8]);
00650         MLA(hi, lo, (*fe)[5], ptr[31 -  6]);
00651         MLA(hi, lo, (*fe)[6], ptr[31 -  4]);
00652         MLA(hi, lo, (*fe)[7], ptr[31 -  2]);
00653 
00654         ptr = *Dptr - po;
00655         MLA(hi, lo, (*fo)[7], ptr[31 -  2]);
00656         MLA(hi, lo, (*fo)[6], ptr[31 -  4]);
00657         MLA(hi, lo, (*fo)[5], ptr[31 -  6]);
00658         MLA(hi, lo, (*fo)[4], ptr[31 -  8]);
00659         MLA(hi, lo, (*fo)[3], ptr[31 - 10]);
00660         MLA(hi, lo, (*fo)[2], ptr[31 - 12]);
00661         MLA(hi, lo, (*fo)[1], ptr[31 - 14]);
00662         MLA(hi, lo, (*fo)[0], ptr[31 - 16]);
00663 
00664         *pcm2-- = SHIFT(MLZ(hi, lo));
00665 
00666         ++fo;
00667       }
00668 
00669       ++Dptr;
00670 
00671       ptr = *Dptr + po;
00672       ML0(hi, lo, (*fo)[0], ptr[ 0]);
00673       MLA(hi, lo, (*fo)[1], ptr[14]);
00674       MLA(hi, lo, (*fo)[2], ptr[12]);
00675       MLA(hi, lo, (*fo)[3], ptr[10]);
00676       MLA(hi, lo, (*fo)[4], ptr[ 8]);
00677       MLA(hi, lo, (*fo)[5], ptr[ 6]);
00678       MLA(hi, lo, (*fo)[6], ptr[ 4]);
00679       MLA(hi, lo, (*fo)[7], ptr[ 2]);
00680 
00681       *pcm1 = SHIFT(-MLZ(hi, lo));
00682       pcm1 += 16;
00683 
00684       phase = (phase + 1) % 16;
00685     }
00686   }
00687 }
00688 # endif
00689 
00690 /*
00691  * NAME:        synth->half()
00692  * DESCRIPTION: perform half frequency PCM synthesis
00693  */
00694 static
00695 void synth_half(struct mad_synth *synth, struct mad_frame const *frame,
00696                 unsigned int nch, unsigned int ns)
00697 {
00698   unsigned int phase, ch, s, sb, pe, po;
00699   mad_fixed_t *pcm1, *pcm2, (*filter)[2][2][16][8];
00700   mad_fixed_t const (*sbsample)[36][32];
00701   register mad_fixed_t (*fe)[8], (*fx)[8], (*fo)[8];
00702   register mad_fixed_t const (*Dptr)[32], *ptr;
00703   register mad_fixed64hi_t hi;
00704   register mad_fixed64lo_t lo;
00705 
00706   for (ch = 0; ch < nch; ++ch) {
00707     sbsample = &frame->sbsample[ch];
00708     filter   = &synth->filter[ch];
00709     phase    = synth->phase;
00710     pcm1     = synth->pcm.samples[ch];
00711 
00712     for (s = 0; s < ns; ++s) {
00713       dct32((*sbsample)[s], phase >> 1,
00714             (*filter)[0][phase & 1], (*filter)[1][phase & 1]);
00715 
00716       pe = phase & ~1;
00717       po = ((phase - 1) & 0xf) | 1;
00718 
00719       /* calculate 16 samples */
00720 
00721       fe = &(*filter)[0][ phase & 1][0];
00722       fx = &(*filter)[0][~phase & 1][0];
00723       fo = &(*filter)[1][~phase & 1][0];
00724 
00725       Dptr = &D[0];
00726 
00727       ptr = *Dptr + po;
00728       ML0(hi, lo, (*fx)[0], ptr[ 0]);
00729       MLA(hi, lo, (*fx)[1], ptr[14]);
00730       MLA(hi, lo, (*fx)[2], ptr[12]);
00731       MLA(hi, lo, (*fx)[3], ptr[10]);
00732       MLA(hi, lo, (*fx)[4], ptr[ 8]);
00733       MLA(hi, lo, (*fx)[5], ptr[ 6]);
00734       MLA(hi, lo, (*fx)[6], ptr[ 4]);
00735       MLA(hi, lo, (*fx)[7], ptr[ 2]);
00736       MLN(hi, lo);
00737 
00738       ptr = *Dptr + pe;
00739       MLA(hi, lo, (*fe)[0], ptr[ 0]);
00740       MLA(hi, lo, (*fe)[1], ptr[14]);
00741       MLA(hi, lo, (*fe)[2], ptr[12]);
00742       MLA(hi, lo, (*fe)[3], ptr[10]);
00743       MLA(hi, lo, (*fe)[4], ptr[ 8]);
00744       MLA(hi, lo, (*fe)[5], ptr[ 6]);
00745       MLA(hi, lo, (*fe)[6], ptr[ 4]);
00746       MLA(hi, lo, (*fe)[7], ptr[ 2]);
00747 
00748       *pcm1++ = SHIFT(MLZ(hi, lo));
00749 
00750       pcm2 = pcm1 + 14;
00751 
00752       for (sb = 1; sb < 16; ++sb) {
00753         ++fe;
00754         ++Dptr;
00755 
00756         /* D[32 - sb][i] == -D[sb][31 - i] */
00757 
00758         if (!(sb & 1)) {
00759           ptr = *Dptr + po;
00760           ML0(hi, lo, (*fo)[0], ptr[ 0]);
00761           MLA(hi, lo, (*fo)[1], ptr[14]);
00762           MLA(hi, lo, (*fo)[2], ptr[12]);
00763           MLA(hi, lo, (*fo)[3], ptr[10]);
00764           MLA(hi, lo, (*fo)[4], ptr[ 8]);
00765           MLA(hi, lo, (*fo)[5], ptr[ 6]);
00766           MLA(hi, lo, (*fo)[6], ptr[ 4]);
00767           MLA(hi, lo, (*fo)[7], ptr[ 2]);
00768           MLN(hi, lo);
00769 
00770           ptr = *Dptr + pe;
00771           MLA(hi, lo, (*fe)[7], ptr[ 2]);
00772           MLA(hi, lo, (*fe)[6], ptr[ 4]);
00773           MLA(hi, lo, (*fe)[5], ptr[ 6]);
00774           MLA(hi, lo, (*fe)[4], ptr[ 8]);
00775           MLA(hi, lo, (*fe)[3], ptr[10]);
00776           MLA(hi, lo, (*fe)[2], ptr[12]);
00777           MLA(hi, lo, (*fe)[1], ptr[14]);
00778           MLA(hi, lo, (*fe)[0], ptr[ 0]);
00779 
00780           *pcm1++ = SHIFT(MLZ(hi, lo));
00781 
00782           ptr = *Dptr - po;
00783           ML0(hi, lo, (*fo)[7], ptr[31 -  2]);
00784           MLA(hi, lo, (*fo)[6], ptr[31 -  4]);
00785           MLA(hi, lo, (*fo)[5], ptr[31 -  6]);
00786           MLA(hi, lo, (*fo)[4], ptr[31 -  8]);
00787           MLA(hi, lo, (*fo)[3], ptr[31 - 10]);
00788           MLA(hi, lo, (*fo)[2], ptr[31 - 12]);
00789           MLA(hi, lo, (*fo)[1], ptr[31 - 14]);
00790           MLA(hi, lo, (*fo)[0], ptr[31 - 16]);
00791 
00792           ptr = *Dptr - pe;
00793           MLA(hi, lo, (*fe)[0], ptr[31 - 16]);
00794           MLA(hi, lo, (*fe)[1], ptr[31 - 14]);
00795           MLA(hi, lo, (*fe)[2], ptr[31 - 12]);
00796           MLA(hi, lo, (*fe)[3], ptr[31 - 10]);
00797           MLA(hi, lo, (*fe)[4], ptr[31 -  8]);
00798           MLA(hi, lo, (*fe)[5], ptr[31 -  6]);
00799           MLA(hi, lo, (*fe)[6], ptr[31 -  4]);
00800           MLA(hi, lo, (*fe)[7], ptr[31 -  2]);
00801 
00802           *pcm2-- = SHIFT(MLZ(hi, lo));
00803         }
00804 
00805         ++fo;
00806       }
00807 
00808       ++Dptr;
00809 
00810       ptr = *Dptr + po;
00811       ML0(hi, lo, (*fo)[0], ptr[ 0]);
00812       MLA(hi, lo, (*fo)[1], ptr[14]);
00813       MLA(hi, lo, (*fo)[2], ptr[12]);
00814       MLA(hi, lo, (*fo)[3], ptr[10]);
00815       MLA(hi, lo, (*fo)[4], ptr[ 8]);
00816       MLA(hi, lo, (*fo)[5], ptr[ 6]);
00817       MLA(hi, lo, (*fo)[6], ptr[ 4]);
00818       MLA(hi, lo, (*fo)[7], ptr[ 2]);
00819 
00820       *pcm1 = SHIFT(-MLZ(hi, lo));
00821       pcm1 += 8;
00822 
00823       phase = (phase + 1) % 16;
00824     }
00825   }
00826 }
00827 
00828 /*
00829  * NAME:        synth->frame()
00830  * DESCRIPTION: perform PCM synthesis of frame subband samples
00831  */
00832 void mad_synth_frame(struct mad_synth *synth, struct mad_frame const *frame)
00833 {
00834   unsigned int nch, ns;
00835   void (*synth_frame)(struct mad_synth *, struct mad_frame const *,
00836                       unsigned int, unsigned int);
00837 
00838   nch = MAD_NCHANNELS(&frame->header);
00839   ns  = MAD_NSBSAMPLES(&frame->header);
00840 
00841   synth->pcm.samplerate = frame->header.samplerate;
00842   synth->pcm.channels   = nch;
00843   synth->pcm.length     = 32 * ns;
00844 
00845   synth_frame = synth_full;
00846 
00847   if (frame->options & MAD_OPTION_HALFSAMPLERATE) {
00848     synth->pcm.samplerate /= 2;
00849     synth->pcm.length     /= 2;
00850 
00851     synth_frame = synth_half;
00852   }
00853 
00854   synth_frame(synth, frame, nch, ns);
00855 
00856   synth->phase = (synth->phase + ns) % 16;
00857 }

Generated on Tue Dec 13 14:47:49 2005 for guliverkli by  doxygen 1.4.5