streo mp3 player see: http://mbed.org/users/okini3939/notebook/I2S_AUDIO

Dependencies:   FatFileSystemCpp I2SSlave TLV320 mbed

Fork of madplayer by Andreas Grün

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers synth.cpp Source File

synth.cpp

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