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