FS#8961 - Anti-Aliased Fonts.
[kugel-rb/myfork.git] / apps / codecs / liba52 / downmix.c
blob7bbf3793f065d9df8cd3ccfa9f7794c79f527934
1 /*
2 * downmix.c
3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
6 * This file is part of a52dec, a free ATSC A-52 stream decoder.
7 * See http://liba52.sourceforge.net/ for updates.
9 * a52dec is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * a52dec is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "config-a52.h"
26 #include <string.h>
27 #include <inttypes.h>
29 #include "a52.h"
30 #include "a52_internal.h"
32 #define CONVERT(acmod,output) (((output) << 3) + (acmod))
34 int a52_downmix_init (int input, int flags, level_t * level,
35 level_t clev, level_t slev)
37 static uint8_t table[11][8] = {
38 {A52_CHANNEL, A52_DOLBY, A52_STEREO, A52_STEREO,
39 A52_STEREO, A52_STEREO, A52_STEREO, A52_STEREO},
40 {A52_MONO, A52_MONO, A52_MONO, A52_MONO,
41 A52_MONO, A52_MONO, A52_MONO, A52_MONO},
42 {A52_CHANNEL, A52_DOLBY, A52_STEREO, A52_STEREO,
43 A52_STEREO, A52_STEREO, A52_STEREO, A52_STEREO},
44 {A52_CHANNEL, A52_DOLBY, A52_STEREO, A52_3F,
45 A52_STEREO, A52_3F, A52_STEREO, A52_3F},
46 {A52_CHANNEL, A52_DOLBY, A52_STEREO, A52_STEREO,
47 A52_2F1R, A52_2F1R, A52_2F1R, A52_2F1R},
48 {A52_CHANNEL, A52_DOLBY, A52_STEREO, A52_STEREO,
49 A52_2F1R, A52_3F1R, A52_2F1R, A52_3F1R},
50 {A52_CHANNEL, A52_DOLBY, A52_STEREO, A52_3F,
51 A52_2F2R, A52_2F2R, A52_2F2R, A52_2F2R},
52 {A52_CHANNEL, A52_DOLBY, A52_STEREO, A52_3F,
53 A52_2F2R, A52_3F2R, A52_2F2R, A52_3F2R},
54 {A52_CHANNEL1, A52_MONO, A52_MONO, A52_MONO,
55 A52_MONO, A52_MONO, A52_MONO, A52_MONO},
56 {A52_CHANNEL2, A52_MONO, A52_MONO, A52_MONO,
57 A52_MONO, A52_MONO, A52_MONO, A52_MONO},
58 {A52_CHANNEL, A52_DOLBY, A52_STEREO, A52_DOLBY,
59 A52_DOLBY, A52_DOLBY, A52_DOLBY, A52_DOLBY}
61 int output;
63 output = flags & A52_CHANNEL_MASK;
64 if (output > A52_DOLBY)
65 return -1;
67 output = table[output][input & 7];
69 if (output == A52_STEREO &&
70 (input == A52_DOLBY || (input == A52_3F && clev == LEVEL (LEVEL_3DB))))
71 output = A52_DOLBY;
73 if (flags & A52_ADJUST_LEVEL) {
74 level_t adjust;
76 switch (CONVERT (input & 7, output)) {
78 case CONVERT (A52_3F, A52_MONO):
79 adjust = DIV (LEVEL_3DB, LEVEL (1) + clev);
80 break;
82 case CONVERT (A52_STEREO, A52_MONO):
83 case CONVERT (A52_2F2R, A52_2F1R):
84 case CONVERT (A52_3F2R, A52_3F1R):
85 level_3db:
86 adjust = LEVEL (LEVEL_3DB);
87 break;
89 case CONVERT (A52_3F2R, A52_2F1R):
90 if (clev < LEVEL (LEVEL_PLUS3DB - 1))
91 goto level_3db;
92 /* break thru */
93 case CONVERT (A52_3F, A52_STEREO):
94 case CONVERT (A52_3F1R, A52_2F1R):
95 case CONVERT (A52_3F1R, A52_2F2R):
96 case CONVERT (A52_3F2R, A52_2F2R):
97 adjust = DIV (1, LEVEL (1) + clev);
98 break;
100 case CONVERT (A52_2F1R, A52_MONO):
101 adjust = DIV (LEVEL_PLUS3DB, LEVEL (2) + slev);
102 break;
104 case CONVERT (A52_2F1R, A52_STEREO):
105 case CONVERT (A52_3F1R, A52_3F):
106 adjust = DIV (1, LEVEL (1) + MUL_C (slev, LEVEL_3DB));
107 break;
109 case CONVERT (A52_3F1R, A52_MONO):
110 adjust = DIV (LEVEL_3DB, LEVEL (1) + clev + MUL_C (slev, 0.5));
111 break;
113 case CONVERT (A52_3F1R, A52_STEREO):
114 adjust = DIV (1, LEVEL (1) + clev + MUL_C (slev, LEVEL_3DB));
115 break;
117 case CONVERT (A52_2F2R, A52_MONO):
118 adjust = DIV (LEVEL_3DB, LEVEL (1) + slev);
119 break;
121 case CONVERT (A52_2F2R, A52_STEREO):
122 case CONVERT (A52_3F2R, A52_3F):
123 adjust = DIV (1, LEVEL (1) + slev);
124 break;
126 case CONVERT (A52_3F2R, A52_MONO):
127 adjust = DIV (LEVEL_3DB, LEVEL (1) + clev + slev);
128 break;
130 case CONVERT (A52_3F2R, A52_STEREO):
131 adjust = DIV (1, LEVEL (1) + clev + slev);
132 break;
134 case CONVERT (A52_MONO, A52_DOLBY):
135 adjust = LEVEL (LEVEL_PLUS3DB);
136 break;
138 case CONVERT (A52_3F, A52_DOLBY):
139 case CONVERT (A52_2F1R, A52_DOLBY):
140 adjust = LEVEL (1 / (1 + LEVEL_3DB));
141 break;
143 case CONVERT (A52_3F1R, A52_DOLBY):
144 case CONVERT (A52_2F2R, A52_DOLBY):
145 adjust = LEVEL (1 / (1 + 2 * LEVEL_3DB));
146 break;
148 case CONVERT (A52_3F2R, A52_DOLBY):
149 adjust = LEVEL (1 / (1 + 3 * LEVEL_3DB));
150 break;
152 default:
153 return output;
156 *level = MUL_L (*level, adjust);
159 return output;
162 int a52_downmix_coeff (level_t * coeff, int acmod, int output, level_t level,
163 level_t clev, level_t slev)
165 level_t level_3db;
167 level_3db = MUL_C (level, LEVEL_3DB);
169 switch (CONVERT (acmod, output & A52_CHANNEL_MASK)) {
171 case CONVERT (A52_CHANNEL, A52_CHANNEL):
172 case CONVERT (A52_MONO, A52_MONO):
173 case CONVERT (A52_STEREO, A52_STEREO):
174 case CONVERT (A52_3F, A52_3F):
175 case CONVERT (A52_2F1R, A52_2F1R):
176 case CONVERT (A52_3F1R, A52_3F1R):
177 case CONVERT (A52_2F2R, A52_2F2R):
178 case CONVERT (A52_3F2R, A52_3F2R):
179 case CONVERT (A52_STEREO, A52_DOLBY):
180 coeff[0] = coeff[1] = coeff[2] = coeff[3] = coeff[4] = level;
181 return 0;
183 case CONVERT (A52_CHANNEL, A52_MONO):
184 coeff[0] = coeff[1] = MUL_C (level, LEVEL_6DB);
185 return 3;
187 case CONVERT (A52_STEREO, A52_MONO):
188 coeff[0] = coeff[1] = level_3db;
189 return 3;
191 case CONVERT (A52_3F, A52_MONO):
192 coeff[0] = coeff[2] = level_3db;
193 coeff[1] = MUL_C (MUL_L (level_3db, clev), LEVEL_PLUS6DB);
194 return 7;
196 case CONVERT (A52_2F1R, A52_MONO):
197 coeff[0] = coeff[1] = level_3db;
198 coeff[2] = MUL_L (level_3db, slev);
199 return 7;
201 case CONVERT (A52_2F2R, A52_MONO):
202 coeff[0] = coeff[1] = level_3db;
203 coeff[2] = coeff[3] = MUL_L (level_3db, slev);
204 return 15;
206 case CONVERT (A52_3F1R, A52_MONO):
207 coeff[0] = coeff[2] = level_3db;
208 coeff[1] = MUL_C (MUL_L (level_3db, clev), LEVEL_PLUS6DB);
209 coeff[3] = MUL_L (level_3db, slev);
210 return 15;
212 case CONVERT (A52_3F2R, A52_MONO):
213 coeff[0] = coeff[2] = level_3db;
214 coeff[1] = MUL_C (MUL_L (level_3db, clev), LEVEL_PLUS6DB);
215 coeff[3] = coeff[4] = MUL_L (level_3db, slev);
216 return 31;
218 case CONVERT (A52_MONO, A52_DOLBY):
219 coeff[0] = level_3db;
220 return 0;
222 case CONVERT (A52_3F, A52_DOLBY):
223 coeff[0] = coeff[2] = coeff[3] = coeff[4] = level;
224 coeff[1] = level_3db;
225 return 7;
227 case CONVERT (A52_3F, A52_STEREO):
228 case CONVERT (A52_3F1R, A52_2F1R):
229 case CONVERT (A52_3F2R, A52_2F2R):
230 coeff[0] = coeff[2] = coeff[3] = coeff[4] = level;
231 coeff[1] = MUL_L (level, clev);
232 return 7;
234 case CONVERT (A52_2F1R, A52_DOLBY):
235 coeff[0] = coeff[1] = level;
236 coeff[2] = level_3db;
237 return 7;
239 case CONVERT (A52_2F1R, A52_STEREO):
240 coeff[0] = coeff[1] = level;
241 coeff[2] = MUL_L (level_3db, slev);
242 return 7;
244 case CONVERT (A52_3F1R, A52_DOLBY):
245 coeff[0] = coeff[2] = level;
246 coeff[1] = coeff[3] = level_3db;
247 return 15;
249 case CONVERT (A52_3F1R, A52_STEREO):
250 coeff[0] = coeff[2] = level;
251 coeff[1] = MUL_L (level, clev);
252 coeff[3] = MUL_L (level_3db, slev);
253 return 15;
255 case CONVERT (A52_2F2R, A52_DOLBY):
256 coeff[0] = coeff[1] = level;
257 coeff[2] = coeff[3] = level_3db;
258 return 15;
260 case CONVERT (A52_2F2R, A52_STEREO):
261 coeff[0] = coeff[1] = level;
262 coeff[2] = coeff[3] = MUL_L (level, slev);
263 return 15;
265 case CONVERT (A52_3F2R, A52_DOLBY):
266 coeff[0] = coeff[2] = level;
267 coeff[1] = coeff[3] = coeff[4] = level_3db;
268 return 31;
270 case CONVERT (A52_3F2R, A52_2F1R):
271 coeff[0] = coeff[2] = level;
272 coeff[1] = MUL_L (level, clev);
273 coeff[3] = coeff[4] = level_3db;
274 return 31;
276 case CONVERT (A52_3F2R, A52_STEREO):
277 coeff[0] = coeff[2] = level;
278 coeff[1] = MUL_L (level, clev);
279 coeff[3] = coeff[4] = MUL_L (level, slev);
280 return 31;
282 case CONVERT (A52_3F1R, A52_3F):
283 coeff[0] = coeff[1] = coeff[2] = level;
284 coeff[3] = MUL_L (level_3db, slev);
285 return 13;
287 case CONVERT (A52_3F2R, A52_3F):
288 coeff[0] = coeff[1] = coeff[2] = level;
289 coeff[3] = coeff[4] = MUL_L (level, slev);
290 return 29;
292 case CONVERT (A52_2F2R, A52_2F1R):
293 coeff[0] = coeff[1] = level;
294 coeff[2] = coeff[3] = level_3db;
295 return 12;
297 case CONVERT (A52_3F2R, A52_3F1R):
298 coeff[0] = coeff[1] = coeff[2] = level;
299 coeff[3] = coeff[4] = level_3db;
300 return 24;
302 case CONVERT (A52_2F1R, A52_2F2R):
303 coeff[0] = coeff[1] = level;
304 coeff[2] = level_3db;
305 return 0;
307 case CONVERT (A52_3F1R, A52_2F2R):
308 coeff[0] = coeff[2] = level;
309 coeff[1] = MUL_L (level, clev);
310 coeff[3] = level_3db;
311 return 7;
313 case CONVERT (A52_3F1R, A52_3F2R):
314 coeff[0] = coeff[1] = coeff[2] = level;
315 coeff[3] = level_3db;
316 return 0;
318 case CONVERT (A52_CHANNEL, A52_CHANNEL1):
319 coeff[0] = level;
320 coeff[1] = 0;
321 return 0;
323 case CONVERT (A52_CHANNEL, A52_CHANNEL2):
324 coeff[0] = 0;
325 coeff[1] = level;
326 return 0;
329 return -1; /* NOTREACHED */
332 static void mix2to1 (sample_t * dest, sample_t * src, sample_t bias)
334 int i;
336 for (i = 0; i < 256; i++)
337 dest[i] += BIAS (src[i]);
340 static void mix3to1 (sample_t * samples, sample_t bias)
342 int i;
344 for (i = 0; i < 256; i++)
345 samples[i] += BIAS (samples[i + 256] + samples[i + 512]);
348 static void mix4to1 (sample_t * samples, sample_t bias)
350 int i;
352 for (i = 0; i < 256; i++)
353 samples[i] += BIAS (samples[i + 256] + samples[i + 512] +
354 samples[i + 768]);
357 static void mix5to1 (sample_t * samples, sample_t bias)
359 int i;
361 for (i = 0; i < 256; i++)
362 samples[i] += BIAS (samples[i + 256] + samples[i + 512] +
363 samples[i + 768] + samples[i + 1024]);
366 static void mix3to2 (sample_t * samples, sample_t bias)
368 int i;
369 sample_t common;
371 for (i = 0; i < 256; i++) {
372 common = BIAS (samples[i + 256]);
373 samples[i] += common;
374 samples[i + 256] = samples[i + 512] + common;
378 static void mix21to2 (sample_t * left, sample_t * right, sample_t bias)
380 int i;
381 sample_t common;
383 for (i = 0; i < 256; i++) {
384 common = BIAS (right[i + 256]);
385 left[i] += common;
386 right[i] += common;
390 static void mix21toS (sample_t * samples, sample_t bias)
392 int i;
393 sample_t surround;
395 for (i = 0; i < 256; i++) {
396 surround = samples[i + 512];
397 samples[i] += BIAS (-surround);
398 samples[i + 256] += BIAS (surround);
402 static void mix31to2 (sample_t * samples, sample_t bias)
404 int i;
405 sample_t common;
407 for (i = 0; i < 256; i++) {
408 common = BIAS (samples[i + 256] + samples[i + 768]);
409 samples[i] += common;
410 samples[i + 256] = samples[i + 512] + common;
414 static void mix31toS (sample_t * samples, sample_t bias)
416 int i;
417 sample_t common, surround;
419 for (i = 0; i < 256; i++) {
420 common = BIAS (samples[i + 256]);
421 surround = samples[i + 768];
422 samples[i] += common - surround;
423 samples[i + 256] = samples[i + 512] + common + surround;
427 static void mix22toS (sample_t * samples, sample_t bias)
429 int i;
430 sample_t surround;
432 for (i = 0; i < 256; i++) {
433 surround = samples[i + 512] + samples[i + 768];
434 samples[i] += BIAS (-surround);
435 samples[i + 256] += BIAS (surround);
439 static void mix32to2 (sample_t * samples, sample_t bias)
441 int i;
442 sample_t common;
444 for (i = 0; i < 256; i++) {
445 common = BIAS (samples[i + 256]);
446 samples[i] += common + samples[i + 768];
447 samples[i + 256] = common + samples[i + 512] + samples[i + 1024];
451 static void mix32toS (sample_t * samples, sample_t bias)
453 int i;
454 sample_t common, surround;
456 for (i = 0; i < 256; i++) {
457 common = BIAS (samples[i + 256]);
458 surround = samples[i + 768] + samples[i + 1024];
459 samples[i] += common - surround;
460 samples[i + 256] = samples[i + 512] + common + surround;
464 static void move2to1 (sample_t * src, sample_t * dest, sample_t bias)
466 int i;
468 for (i = 0; i < 256; i++)
469 dest[i] = BIAS (src[i] + src[i + 256]);
472 static void zero (sample_t * samples)
474 int i;
476 for (i = 0; i < 256; i++)
477 samples[i] = 0;
480 void a52_downmix (sample_t * samples, int acmod, int output, sample_t bias,
481 level_t clev, level_t slev)
483 /* avoid compiler warning */
484 (void)clev;
486 switch (CONVERT (acmod, output & A52_CHANNEL_MASK)) {
488 case CONVERT (A52_CHANNEL, A52_CHANNEL2):
489 memcpy (samples, samples + 256, 256 * sizeof (sample_t));
490 break;
492 case CONVERT (A52_CHANNEL, A52_MONO):
493 case CONVERT (A52_STEREO, A52_MONO):
494 mix_2to1:
495 mix2to1 (samples, samples + 256, bias);
496 break;
498 case CONVERT (A52_2F1R, A52_MONO):
499 if (slev == 0)
500 goto mix_2to1;
501 case CONVERT (A52_3F, A52_MONO):
502 mix_3to1:
503 mix3to1 (samples, bias);
504 break;
506 case CONVERT (A52_3F1R, A52_MONO):
507 if (slev == 0)
508 goto mix_3to1;
509 case CONVERT (A52_2F2R, A52_MONO):
510 if (slev == 0)
511 goto mix_2to1;
512 mix4to1 (samples, bias);
513 break;
515 case CONVERT (A52_3F2R, A52_MONO):
516 if (slev == 0)
517 goto mix_3to1;
518 mix5to1 (samples, bias);
519 break;
521 case CONVERT (A52_MONO, A52_DOLBY):
522 memcpy (samples + 256, samples, 256 * sizeof (sample_t));
523 break;
525 case CONVERT (A52_3F, A52_STEREO):
526 case CONVERT (A52_3F, A52_DOLBY):
527 mix_3to2:
528 mix3to2 (samples, bias);
529 break;
531 case CONVERT (A52_2F1R, A52_STEREO):
532 if (slev == 0)
533 break;
534 mix21to2 (samples, samples + 256, bias);
535 break;
537 case CONVERT (A52_2F1R, A52_DOLBY):
538 mix21toS (samples, bias);
539 break;
541 case CONVERT (A52_3F1R, A52_STEREO):
542 if (slev == 0)
543 goto mix_3to2;
544 mix31to2 (samples, bias);
545 break;
547 case CONVERT (A52_3F1R, A52_DOLBY):
548 mix31toS (samples, bias);
549 break;
551 case CONVERT (A52_2F2R, A52_STEREO):
552 if (slev == 0)
553 break;
554 mix2to1 (samples, samples + 512, bias);
555 mix2to1 (samples + 256, samples + 768, bias);
556 break;
558 case CONVERT (A52_2F2R, A52_DOLBY):
559 mix22toS (samples, bias);
560 break;
562 case CONVERT (A52_3F2R, A52_STEREO):
563 if (slev == 0)
564 goto mix_3to2;
565 mix32to2 (samples, bias);
566 break;
568 case CONVERT (A52_3F2R, A52_DOLBY):
569 mix32toS (samples, bias);
570 break;
572 case CONVERT (A52_3F1R, A52_3F):
573 if (slev == 0)
574 break;
575 mix21to2 (samples, samples + 512, bias);
576 break;
578 case CONVERT (A52_3F2R, A52_3F):
579 if (slev == 0)
580 break;
581 mix2to1 (samples, samples + 768, bias);
582 mix2to1 (samples + 512, samples + 1024, bias);
583 break;
585 case CONVERT (A52_3F1R, A52_2F1R):
586 mix3to2 (samples, bias);
587 memcpy (samples + 512, samples + 768, 256 * sizeof (sample_t));
588 break;
590 case CONVERT (A52_2F2R, A52_2F1R):
591 mix2to1 (samples + 512, samples + 768, bias);
592 break;
594 case CONVERT (A52_3F2R, A52_2F1R):
595 mix3to2 (samples, bias);
596 move2to1 (samples + 768, samples + 512, bias);
597 break;
599 case CONVERT (A52_3F2R, A52_3F1R):
600 mix2to1 (samples + 768, samples + 1024, bias);
601 break;
603 case CONVERT (A52_2F1R, A52_2F2R):
604 memcpy (samples + 768, samples + 512, 256 * sizeof (sample_t));
605 break;
607 case CONVERT (A52_3F1R, A52_2F2R):
608 mix3to2 (samples, bias);
609 memcpy (samples + 512, samples + 768, 256 * sizeof (sample_t));
610 break;
612 case CONVERT (A52_3F2R, A52_2F2R):
613 mix3to2 (samples, bias);
614 memcpy (samples + 512, samples + 768, 256 * sizeof (sample_t));
615 memcpy (samples + 768, samples + 1024, 256 * sizeof (sample_t));
616 break;
618 case CONVERT (A52_3F1R, A52_3F2R):
619 memcpy (samples + 1024, samples + 768, 256 * sizeof (sample_t));
620 break;
624 void a52_upmix (sample_t * samples, int acmod, int output)
626 switch (CONVERT (acmod, output & A52_CHANNEL_MASK)) {
628 case CONVERT (A52_CHANNEL, A52_CHANNEL2):
629 memcpy (samples + 256, samples, 256 * sizeof (sample_t));
630 break;
632 case CONVERT (A52_3F2R, A52_MONO):
633 zero (samples + 1024);
634 case CONVERT (A52_3F1R, A52_MONO):
635 case CONVERT (A52_2F2R, A52_MONO):
636 zero (samples + 768);
637 case CONVERT (A52_3F, A52_MONO):
638 case CONVERT (A52_2F1R, A52_MONO):
639 zero (samples + 512);
640 case CONVERT (A52_CHANNEL, A52_MONO):
641 case CONVERT (A52_STEREO, A52_MONO):
642 zero (samples + 256);
643 break;
645 case CONVERT (A52_3F2R, A52_STEREO):
646 case CONVERT (A52_3F2R, A52_DOLBY):
647 zero (samples + 1024);
648 case CONVERT (A52_3F1R, A52_STEREO):
649 case CONVERT (A52_3F1R, A52_DOLBY):
650 zero (samples + 768);
651 case CONVERT (A52_3F, A52_STEREO):
652 case CONVERT (A52_3F, A52_DOLBY):
653 mix_3to2:
654 memcpy (samples + 512, samples + 256, 256 * sizeof (sample_t));
655 zero (samples + 256);
656 break;
658 case CONVERT (A52_2F2R, A52_STEREO):
659 case CONVERT (A52_2F2R, A52_DOLBY):
660 zero (samples + 768);
661 case CONVERT (A52_2F1R, A52_STEREO):
662 case CONVERT (A52_2F1R, A52_DOLBY):
663 zero (samples + 512);
664 break;
666 case CONVERT (A52_3F2R, A52_3F):
667 zero (samples + 1024);
668 case CONVERT (A52_3F1R, A52_3F):
669 case CONVERT (A52_2F2R, A52_2F1R):
670 zero (samples + 768);
671 break;
673 case CONVERT (A52_3F2R, A52_3F1R):
674 zero (samples + 1024);
675 break;
677 case CONVERT (A52_3F2R, A52_2F1R):
678 zero (samples + 1024);
679 case CONVERT (A52_3F1R, A52_2F1R):
680 mix_31to21:
681 memcpy (samples + 768, samples + 512, 256 * sizeof (sample_t));
682 goto mix_3to2;
684 case CONVERT (A52_3F2R, A52_2F2R):
685 memcpy (samples + 1024, samples + 768, 256 * sizeof (sample_t));
686 goto mix_31to21;