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