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