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
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}
63 output = flags & A52_CHANNEL_MASK;
64 if (output > A52_DOLBY)
67 output = table[output][input & 7];
69 if (output == A52_STEREO &&
70 (input == A52_DOLBY || (input == A52_3F && clev == LEVEL (LEVEL_3DB))))
73 if (flags & A52_ADJUST_LEVEL) {
76 switch (CONVERT (input & 7, output)) {
78 case CONVERT (A52_3F, A52_MONO):
79 adjust = DIV (LEVEL_3DB, LEVEL (1) + clev);
82 case CONVERT (A52_STEREO, A52_MONO):
83 case CONVERT (A52_2F2R, A52_2F1R):
84 case CONVERT (A52_3F2R, A52_3F1R):
86 adjust = LEVEL (LEVEL_3DB);
89 case CONVERT (A52_3F2R, A52_2F1R):
90 if (clev < LEVEL (LEVEL_PLUS3DB - 1))
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);
100 case CONVERT (A52_2F1R, A52_MONO):
101 adjust = DIV (LEVEL_PLUS3DB, LEVEL (2) + slev);
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));
109 case CONVERT (A52_3F1R, A52_MONO):
110 adjust = DIV (LEVEL_3DB, LEVEL (1) + clev + MUL_C (slev, 0.5));
113 case CONVERT (A52_3F1R, A52_STEREO):
114 adjust = DIV (1, LEVEL (1) + clev + MUL_C (slev, LEVEL_3DB));
117 case CONVERT (A52_2F2R, A52_MONO):
118 adjust = DIV (LEVEL_3DB, LEVEL (1) + slev);
121 case CONVERT (A52_2F2R, A52_STEREO):
122 case CONVERT (A52_3F2R, A52_3F):
123 adjust = DIV (1, LEVEL (1) + slev);
126 case CONVERT (A52_3F2R, A52_MONO):
127 adjust = DIV (LEVEL_3DB, LEVEL (1) + clev + slev);
130 case CONVERT (A52_3F2R, A52_STEREO):
131 adjust = DIV (1, LEVEL (1) + clev + slev);
134 case CONVERT (A52_MONO, A52_DOLBY):
135 adjust = LEVEL (LEVEL_PLUS3DB);
138 case CONVERT (A52_3F, A52_DOLBY):
139 case CONVERT (A52_2F1R, A52_DOLBY):
140 adjust = LEVEL (1 / (1 + LEVEL_3DB));
143 case CONVERT (A52_3F1R, A52_DOLBY):
144 case CONVERT (A52_2F2R, A52_DOLBY):
145 adjust = LEVEL (1 / (1 + 2 * LEVEL_3DB));
148 case CONVERT (A52_3F2R, A52_DOLBY):
149 adjust = LEVEL (1 / (1 + 3 * LEVEL_3DB));
156 *level = MUL_L (*level, adjust);
162 int a52_downmix_coeff (level_t * coeff, int acmod, int output, level_t level,
163 level_t clev, level_t slev)
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;
183 case CONVERT (A52_CHANNEL, A52_MONO):
184 coeff[0] = coeff[1] = MUL_C (level, LEVEL_6DB);
187 case CONVERT (A52_STEREO, A52_MONO):
188 coeff[0] = coeff[1] = level_3db;
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);
196 case CONVERT (A52_2F1R, A52_MONO):
197 coeff[0] = coeff[1] = level_3db;
198 coeff[2] = MUL_L (level_3db, slev);
201 case CONVERT (A52_2F2R, A52_MONO):
202 coeff[0] = coeff[1] = level_3db;
203 coeff[2] = coeff[3] = MUL_L (level_3db, slev);
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);
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);
218 case CONVERT (A52_MONO, A52_DOLBY):
219 coeff[0] = level_3db;
222 case CONVERT (A52_3F, A52_DOLBY):
223 coeff[0] = coeff[2] = coeff[3] = coeff[4] = level;
224 coeff[1] = level_3db;
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);
234 case CONVERT (A52_2F1R, A52_DOLBY):
235 coeff[0] = coeff[1] = level;
236 coeff[2] = level_3db;
239 case CONVERT (A52_2F1R, A52_STEREO):
240 coeff[0] = coeff[1] = level;
241 coeff[2] = MUL_L (level_3db, slev);
244 case CONVERT (A52_3F1R, A52_DOLBY):
245 coeff[0] = coeff[2] = level;
246 coeff[1] = coeff[3] = level_3db;
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);
255 case CONVERT (A52_2F2R, A52_DOLBY):
256 coeff[0] = coeff[1] = level;
257 coeff[2] = coeff[3] = level_3db;
260 case CONVERT (A52_2F2R, A52_STEREO):
261 coeff[0] = coeff[1] = level;
262 coeff[2] = coeff[3] = MUL_L (level, slev);
265 case CONVERT (A52_3F2R, A52_DOLBY):
266 coeff[0] = coeff[2] = level;
267 coeff[1] = coeff[3] = coeff[4] = level_3db;
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;
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);
282 case CONVERT (A52_3F1R, A52_3F):
283 coeff[0] = coeff[1] = coeff[2] = level;
284 coeff[3] = MUL_L (level_3db, slev);
287 case CONVERT (A52_3F2R, A52_3F):
288 coeff[0] = coeff[1] = coeff[2] = level;
289 coeff[3] = coeff[4] = MUL_L (level, slev);
292 case CONVERT (A52_2F2R, A52_2F1R):
293 coeff[0] = coeff[1] = level;
294 coeff[2] = coeff[3] = level_3db;
297 case CONVERT (A52_3F2R, A52_3F1R):
298 coeff[0] = coeff[1] = coeff[2] = level;
299 coeff[3] = coeff[4] = level_3db;
302 case CONVERT (A52_2F1R, A52_2F2R):
303 coeff[0] = coeff[1] = level;
304 coeff[2] = level_3db;
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;
313 case CONVERT (A52_3F1R, A52_3F2R):
314 coeff[0] = coeff[1] = coeff[2] = level;
315 coeff[3] = level_3db;
318 case CONVERT (A52_CHANNEL, A52_CHANNEL1):
323 case CONVERT (A52_CHANNEL, A52_CHANNEL2):
329 return -1; /* NOTREACHED */
332 static void mix2to1 (sample_t * dest, sample_t * src, sample_t bias)
336 for (i = 0; i < 256; i++)
337 dest[i] += BIAS (src[i]);
340 static void mix3to1 (sample_t * samples, sample_t bias)
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)
352 for (i = 0; i < 256; i++)
353 samples[i] += BIAS (samples[i + 256] + samples[i + 512] +
357 static void mix5to1 (sample_t * samples, sample_t bias)
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)
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)
383 for (i = 0; i < 256; i++) {
384 common = BIAS (right[i + 256]);
390 static void mix21toS (sample_t * samples, sample_t bias)
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)
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)
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)
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)
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)
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)
468 for (i = 0; i < 256; i++)
469 dest[i] = BIAS (src[i] + src[i + 256]);
472 static void zero (sample_t * samples)
476 for (i = 0; i < 256; i++)
480 void a52_downmix (sample_t * samples, int acmod, int output, sample_t bias,
481 level_t clev, level_t slev)
483 switch (CONVERT (acmod, output & A52_CHANNEL_MASK)) {
485 case CONVERT (A52_CHANNEL, A52_CHANNEL2):
486 memcpy (samples, samples + 256, 256 * sizeof (sample_t));
489 case CONVERT (A52_CHANNEL, A52_MONO):
490 case CONVERT (A52_STEREO, A52_MONO):
492 mix2to1 (samples, samples + 256, bias);
495 case CONVERT (A52_2F1R, A52_MONO):
498 case CONVERT (A52_3F, A52_MONO):
500 mix3to1 (samples, bias);
503 case CONVERT (A52_3F1R, A52_MONO):
506 case CONVERT (A52_2F2R, A52_MONO):
509 mix4to1 (samples, bias);
512 case CONVERT (A52_3F2R, A52_MONO):
515 mix5to1 (samples, bias);
518 case CONVERT (A52_MONO, A52_DOLBY):
519 memcpy (samples + 256, samples, 256 * sizeof (sample_t));
522 case CONVERT (A52_3F, A52_STEREO):
523 case CONVERT (A52_3F, A52_DOLBY):
525 mix3to2 (samples, bias);
528 case CONVERT (A52_2F1R, A52_STEREO):
531 mix21to2 (samples, samples + 256, bias);
534 case CONVERT (A52_2F1R, A52_DOLBY):
535 mix21toS (samples, bias);
538 case CONVERT (A52_3F1R, A52_STEREO):
541 mix31to2 (samples, bias);
544 case CONVERT (A52_3F1R, A52_DOLBY):
545 mix31toS (samples, bias);
548 case CONVERT (A52_2F2R, A52_STEREO):
551 mix2to1 (samples, samples + 512, bias);
552 mix2to1 (samples + 256, samples + 768, bias);
555 case CONVERT (A52_2F2R, A52_DOLBY):
556 mix22toS (samples, bias);
559 case CONVERT (A52_3F2R, A52_STEREO):
562 mix32to2 (samples, bias);
565 case CONVERT (A52_3F2R, A52_DOLBY):
566 mix32toS (samples, bias);
569 case CONVERT (A52_3F1R, A52_3F):
572 mix21to2 (samples, samples + 512, bias);
575 case CONVERT (A52_3F2R, A52_3F):
578 mix2to1 (samples, samples + 768, bias);
579 mix2to1 (samples + 512, samples + 1024, bias);
582 case CONVERT (A52_3F1R, A52_2F1R):
583 mix3to2 (samples, bias);
584 memcpy (samples + 512, samples + 768, 256 * sizeof (sample_t));
587 case CONVERT (A52_2F2R, A52_2F1R):
588 mix2to1 (samples + 512, samples + 768, bias);
591 case CONVERT (A52_3F2R, A52_2F1R):
592 mix3to2 (samples, bias);
593 move2to1 (samples + 768, samples + 512, bias);
596 case CONVERT (A52_3F2R, A52_3F1R):
597 mix2to1 (samples + 768, samples + 1024, bias);
600 case CONVERT (A52_2F1R, A52_2F2R):
601 memcpy (samples + 768, samples + 512, 256 * sizeof (sample_t));
604 case CONVERT (A52_3F1R, A52_2F2R):
605 mix3to2 (samples, bias);
606 memcpy (samples + 512, samples + 768, 256 * sizeof (sample_t));
609 case CONVERT (A52_3F2R, A52_2F2R):
610 mix3to2 (samples, bias);
611 memcpy (samples + 512, samples + 768, 256 * sizeof (sample_t));
612 memcpy (samples + 768, samples + 1024, 256 * sizeof (sample_t));
615 case CONVERT (A52_3F1R, A52_3F2R):
616 memcpy (samples + 1024, samples + 768, 256 * sizeof (sample_t));
621 void a52_upmix (sample_t * samples, int acmod, int output)
623 switch (CONVERT (acmod, output & A52_CHANNEL_MASK)) {
625 case CONVERT (A52_CHANNEL, A52_CHANNEL2):
626 memcpy (samples + 256, samples, 256 * sizeof (sample_t));
629 case CONVERT (A52_3F2R, A52_MONO):
630 zero (samples + 1024);
631 case CONVERT (A52_3F1R, A52_MONO):
632 case CONVERT (A52_2F2R, A52_MONO):
633 zero (samples + 768);
634 case CONVERT (A52_3F, A52_MONO):
635 case CONVERT (A52_2F1R, A52_MONO):
636 zero (samples + 512);
637 case CONVERT (A52_CHANNEL, A52_MONO):
638 case CONVERT (A52_STEREO, A52_MONO):
639 zero (samples + 256);
642 case CONVERT (A52_3F2R, A52_STEREO):
643 case CONVERT (A52_3F2R, A52_DOLBY):
644 zero (samples + 1024);
645 case CONVERT (A52_3F1R, A52_STEREO):
646 case CONVERT (A52_3F1R, A52_DOLBY):
647 zero (samples + 768);
648 case CONVERT (A52_3F, A52_STEREO):
649 case CONVERT (A52_3F, A52_DOLBY):
651 memcpy (samples + 512, samples + 256, 256 * sizeof (sample_t));
652 zero (samples + 256);
655 case CONVERT (A52_2F2R, A52_STEREO):
656 case CONVERT (A52_2F2R, A52_DOLBY):
657 zero (samples + 768);
658 case CONVERT (A52_2F1R, A52_STEREO):
659 case CONVERT (A52_2F1R, A52_DOLBY):
660 zero (samples + 512);
663 case CONVERT (A52_3F2R, A52_3F):
664 zero (samples + 1024);
665 case CONVERT (A52_3F1R, A52_3F):
666 case CONVERT (A52_2F2R, A52_2F1R):
667 zero (samples + 768);
670 case CONVERT (A52_3F2R, A52_3F1R):
671 zero (samples + 1024);
674 case CONVERT (A52_3F2R, A52_2F1R):
675 zero (samples + 1024);
676 case CONVERT (A52_3F1R, A52_2F1R):
678 memcpy (samples + 768, samples + 512, 256 * sizeof (sample_t));
681 case CONVERT (A52_3F2R, A52_2F2R):
682 memcpy (samples + 1024, samples + 768, 256 * sizeof (sample_t));