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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "a52_internal.h"
26 #define CONVERT(acmod,output) (((output) << 3) + (acmod))
28 int a52_downmix_init (int input
, int flags
, level_t
* level
,
29 level_t clev
, level_t slev
)
31 static uint8_t table
[11][8] = {
32 {A52_CHANNEL
, A52_DOLBY
, A52_STEREO
, A52_STEREO
,
33 A52_STEREO
, A52_STEREO
, A52_STEREO
, A52_STEREO
},
34 {A52_MONO
, A52_MONO
, A52_MONO
, A52_MONO
,
35 A52_MONO
, A52_MONO
, A52_MONO
, A52_MONO
},
36 {A52_CHANNEL
, A52_DOLBY
, A52_STEREO
, A52_STEREO
,
37 A52_STEREO
, A52_STEREO
, A52_STEREO
, A52_STEREO
},
38 {A52_CHANNEL
, A52_DOLBY
, A52_STEREO
, A52_3F
,
39 A52_STEREO
, A52_3F
, A52_STEREO
, A52_3F
},
40 {A52_CHANNEL
, A52_DOLBY
, A52_STEREO
, A52_STEREO
,
41 A52_2F1R
, A52_2F1R
, A52_2F1R
, A52_2F1R
},
42 {A52_CHANNEL
, A52_DOLBY
, A52_STEREO
, A52_STEREO
,
43 A52_2F1R
, A52_3F1R
, A52_2F1R
, A52_3F1R
},
44 {A52_CHANNEL
, A52_DOLBY
, A52_STEREO
, A52_3F
,
45 A52_2F2R
, A52_2F2R
, A52_2F2R
, A52_2F2R
},
46 {A52_CHANNEL
, A52_DOLBY
, A52_STEREO
, A52_3F
,
47 A52_2F2R
, A52_3F2R
, A52_2F2R
, A52_3F2R
},
48 {A52_CHANNEL1
, A52_MONO
, A52_MONO
, A52_MONO
,
49 A52_MONO
, A52_MONO
, A52_MONO
, A52_MONO
},
50 {A52_CHANNEL2
, A52_MONO
, A52_MONO
, A52_MONO
,
51 A52_MONO
, A52_MONO
, A52_MONO
, A52_MONO
},
52 {A52_CHANNEL
, A52_DOLBY
, A52_STEREO
, A52_DOLBY
,
53 A52_DOLBY
, A52_DOLBY
, A52_DOLBY
, A52_DOLBY
}
57 output
= flags
& A52_CHANNEL_MASK
;
58 if (output
> A52_DOLBY
)
61 output
= table
[output
][input
& 7];
63 if (output
== A52_STEREO
&&
64 (input
== A52_DOLBY
|| (input
== A52_3F
&& clev
== LEVEL (LEVEL_3DB
))))
67 if (flags
& A52_ADJUST_LEVEL
) {
70 switch (CONVERT (input
& 7, output
)) {
72 case CONVERT (A52_3F
, A52_MONO
):
73 adjust
= DIV (LEVEL_3DB
, LEVEL (1) + clev
);
76 case CONVERT (A52_STEREO
, A52_MONO
):
77 case CONVERT (A52_2F2R
, A52_2F1R
):
78 case CONVERT (A52_3F2R
, A52_3F1R
):
80 adjust
= LEVEL (LEVEL_3DB
);
83 case CONVERT (A52_3F2R
, A52_2F1R
):
84 if (clev
< LEVEL (LEVEL_PLUS3DB
- 1))
87 case CONVERT (A52_3F
, A52_STEREO
):
88 case CONVERT (A52_3F1R
, A52_2F1R
):
89 case CONVERT (A52_3F1R
, A52_2F2R
):
90 case CONVERT (A52_3F2R
, A52_2F2R
):
91 adjust
= DIV (1, LEVEL (1) + clev
);
94 case CONVERT (A52_2F1R
, A52_MONO
):
95 adjust
= DIV (LEVEL_PLUS3DB
, LEVEL (2) + slev
);
98 case CONVERT (A52_2F1R
, A52_STEREO
):
99 case CONVERT (A52_3F1R
, A52_3F
):
100 adjust
= DIV (1, LEVEL (1) + MUL_C (slev
, LEVEL_3DB
));
103 case CONVERT (A52_3F1R
, A52_MONO
):
104 adjust
= DIV (LEVEL_3DB
, LEVEL (1) + clev
+ MUL_C (slev
, 0.5));
107 case CONVERT (A52_3F1R
, A52_STEREO
):
108 adjust
= DIV (1, LEVEL (1) + clev
+ MUL_C (slev
, LEVEL_3DB
));
111 case CONVERT (A52_2F2R
, A52_MONO
):
112 adjust
= DIV (LEVEL_3DB
, LEVEL (1) + slev
);
115 case CONVERT (A52_2F2R
, A52_STEREO
):
116 case CONVERT (A52_3F2R
, A52_3F
):
117 adjust
= DIV (1, LEVEL (1) + slev
);
120 case CONVERT (A52_3F2R
, A52_MONO
):
121 adjust
= DIV (LEVEL_3DB
, LEVEL (1) + clev
+ slev
);
124 case CONVERT (A52_3F2R
, A52_STEREO
):
125 adjust
= DIV (1, LEVEL (1) + clev
+ slev
);
128 case CONVERT (A52_MONO
, A52_DOLBY
):
129 adjust
= LEVEL (LEVEL_PLUS3DB
);
132 case CONVERT (A52_3F
, A52_DOLBY
):
133 case CONVERT (A52_2F1R
, A52_DOLBY
):
134 adjust
= LEVEL (1 / (1 + LEVEL_3DB
));
137 case CONVERT (A52_3F1R
, A52_DOLBY
):
138 case CONVERT (A52_2F2R
, A52_DOLBY
):
139 adjust
= LEVEL (1 / (1 + 2 * LEVEL_3DB
));
142 case CONVERT (A52_3F2R
, A52_DOLBY
):
143 adjust
= LEVEL (1 / (1 + 3 * LEVEL_3DB
));
150 *level
= MUL_L (*level
, adjust
);
156 int a52_downmix_coeff (level_t
* coeff
, int acmod
, int output
, level_t level
,
157 level_t clev
, level_t slev
)
161 level_3db
= MUL_C (level
, LEVEL_3DB
);
163 switch (CONVERT (acmod
, output
& A52_CHANNEL_MASK
)) {
165 case CONVERT (A52_CHANNEL
, A52_CHANNEL
):
166 case CONVERT (A52_MONO
, A52_MONO
):
167 case CONVERT (A52_STEREO
, A52_STEREO
):
168 case CONVERT (A52_3F
, A52_3F
):
169 case CONVERT (A52_2F1R
, A52_2F1R
):
170 case CONVERT (A52_3F1R
, A52_3F1R
):
171 case CONVERT (A52_2F2R
, A52_2F2R
):
172 case CONVERT (A52_3F2R
, A52_3F2R
):
173 case CONVERT (A52_STEREO
, A52_DOLBY
):
174 coeff
[0] = coeff
[1] = coeff
[2] = coeff
[3] = coeff
[4] = level
;
177 case CONVERT (A52_CHANNEL
, A52_MONO
):
178 coeff
[0] = coeff
[1] = MUL_C (level
, LEVEL_6DB
);
181 case CONVERT (A52_STEREO
, A52_MONO
):
182 coeff
[0] = coeff
[1] = level_3db
;
185 case CONVERT (A52_3F
, A52_MONO
):
186 coeff
[0] = coeff
[2] = level_3db
;
187 coeff
[1] = MUL_C (MUL_L (level_3db
, clev
), LEVEL_PLUS6DB
);
190 case CONVERT (A52_2F1R
, A52_MONO
):
191 coeff
[0] = coeff
[1] = level_3db
;
192 coeff
[2] = MUL_L (level_3db
, slev
);
195 case CONVERT (A52_2F2R
, A52_MONO
):
196 coeff
[0] = coeff
[1] = level_3db
;
197 coeff
[2] = coeff
[3] = MUL_L (level_3db
, slev
);
200 case CONVERT (A52_3F1R
, A52_MONO
):
201 coeff
[0] = coeff
[2] = level_3db
;
202 coeff
[1] = MUL_C (MUL_L (level_3db
, clev
), LEVEL_PLUS6DB
);
203 coeff
[3] = MUL_L (level_3db
, slev
);
206 case CONVERT (A52_3F2R
, A52_MONO
):
207 coeff
[0] = coeff
[2] = level_3db
;
208 coeff
[1] = MUL_C (MUL_L (level_3db
, clev
), LEVEL_PLUS6DB
);
209 coeff
[3] = coeff
[4] = MUL_L (level_3db
, slev
);
212 case CONVERT (A52_MONO
, A52_DOLBY
):
213 coeff
[0] = level_3db
;
216 case CONVERT (A52_3F
, A52_DOLBY
):
217 coeff
[0] = coeff
[2] = coeff
[3] = coeff
[4] = level
;
218 coeff
[1] = level_3db
;
221 case CONVERT (A52_3F
, A52_STEREO
):
222 case CONVERT (A52_3F1R
, A52_2F1R
):
223 case CONVERT (A52_3F2R
, A52_2F2R
):
224 coeff
[0] = coeff
[2] = coeff
[3] = coeff
[4] = level
;
225 coeff
[1] = MUL_L (level
, clev
);
228 case CONVERT (A52_2F1R
, A52_DOLBY
):
229 coeff
[0] = coeff
[1] = level
;
230 coeff
[2] = level_3db
;
233 case CONVERT (A52_2F1R
, A52_STEREO
):
234 coeff
[0] = coeff
[1] = level
;
235 coeff
[2] = MUL_L (level_3db
, slev
);
238 case CONVERT (A52_3F1R
, A52_DOLBY
):
239 coeff
[0] = coeff
[2] = level
;
240 coeff
[1] = coeff
[3] = level_3db
;
243 case CONVERT (A52_3F1R
, A52_STEREO
):
244 coeff
[0] = coeff
[2] = level
;
245 coeff
[1] = MUL_L (level
, clev
);
246 coeff
[3] = MUL_L (level_3db
, slev
);
249 case CONVERT (A52_2F2R
, A52_DOLBY
):
250 coeff
[0] = coeff
[1] = level
;
251 coeff
[2] = coeff
[3] = level_3db
;
254 case CONVERT (A52_2F2R
, A52_STEREO
):
255 coeff
[0] = coeff
[1] = level
;
256 coeff
[2] = coeff
[3] = MUL_L (level
, slev
);
259 case CONVERT (A52_3F2R
, A52_DOLBY
):
260 coeff
[0] = coeff
[2] = level
;
261 coeff
[1] = coeff
[3] = coeff
[4] = level_3db
;
264 case CONVERT (A52_3F2R
, A52_2F1R
):
265 coeff
[0] = coeff
[2] = level
;
266 coeff
[1] = MUL_L (level
, clev
);
267 coeff
[3] = coeff
[4] = level_3db
;
270 case CONVERT (A52_3F2R
, A52_STEREO
):
271 coeff
[0] = coeff
[2] = level
;
272 coeff
[1] = MUL_L (level
, clev
);
273 coeff
[3] = coeff
[4] = MUL_L (level
, slev
);
276 case CONVERT (A52_3F1R
, A52_3F
):
277 coeff
[0] = coeff
[1] = coeff
[2] = level
;
278 coeff
[3] = MUL_L (level_3db
, slev
);
281 case CONVERT (A52_3F2R
, A52_3F
):
282 coeff
[0] = coeff
[1] = coeff
[2] = level
;
283 coeff
[3] = coeff
[4] = MUL_L (level
, slev
);
286 case CONVERT (A52_2F2R
, A52_2F1R
):
287 coeff
[0] = coeff
[1] = level
;
288 coeff
[2] = coeff
[3] = level_3db
;
291 case CONVERT (A52_3F2R
, A52_3F1R
):
292 coeff
[0] = coeff
[1] = coeff
[2] = level
;
293 coeff
[3] = coeff
[4] = level_3db
;
296 case CONVERT (A52_2F1R
, A52_2F2R
):
297 coeff
[0] = coeff
[1] = level
;
298 coeff
[2] = level_3db
;
301 case CONVERT (A52_3F1R
, A52_2F2R
):
302 coeff
[0] = coeff
[2] = level
;
303 coeff
[1] = MUL_L (level
, clev
);
304 coeff
[3] = level_3db
;
307 case CONVERT (A52_3F1R
, A52_3F2R
):
308 coeff
[0] = coeff
[1] = coeff
[2] = level
;
309 coeff
[3] = level_3db
;
312 case CONVERT (A52_CHANNEL
, A52_CHANNEL1
):
317 case CONVERT (A52_CHANNEL
, A52_CHANNEL2
):
323 return -1; /* NOTREACHED */
326 static void mix2to1 (sample_t
* dest
, sample_t
* src
, sample_t bias
)
330 for (i
= 0; i
< 256; i
++)
331 dest
[i
] += BIAS (src
[i
]);
334 static void mix3to1 (sample_t
* samples
, sample_t bias
)
338 for (i
= 0; i
< 256; i
++)
339 samples
[i
] += BIAS (samples
[i
+ 256] + samples
[i
+ 512]);
342 static void mix4to1 (sample_t
* samples
, sample_t bias
)
346 for (i
= 0; i
< 256; i
++)
347 samples
[i
] += BIAS (samples
[i
+ 256] + samples
[i
+ 512] +
351 static void mix5to1 (sample_t
* samples
, sample_t bias
)
355 for (i
= 0; i
< 256; i
++)
356 samples
[i
] += BIAS (samples
[i
+ 256] + samples
[i
+ 512] +
357 samples
[i
+ 768] + samples
[i
+ 1024]);
360 static void mix3to2 (sample_t
* samples
, sample_t bias
)
365 for (i
= 0; i
< 256; i
++) {
366 common
= BIAS (samples
[i
+ 256]);
367 samples
[i
] += common
;
368 samples
[i
+ 256] = samples
[i
+ 512] + common
;
372 static void mix21to2 (sample_t
* left
, sample_t
* right
, sample_t bias
)
377 for (i
= 0; i
< 256; i
++) {
378 common
= BIAS (right
[i
+ 256]);
384 static void mix21toS (sample_t
* samples
, sample_t bias
)
389 for (i
= 0; i
< 256; i
++) {
390 surround
= samples
[i
+ 512];
391 samples
[i
] += BIAS (-surround
);
392 samples
[i
+ 256] += BIAS (surround
);
396 static void mix31to2 (sample_t
* samples
, sample_t bias
)
401 for (i
= 0; i
< 256; i
++) {
402 common
= BIAS (samples
[i
+ 256] + samples
[i
+ 768]);
403 samples
[i
] += common
;
404 samples
[i
+ 256] = samples
[i
+ 512] + common
;
408 static void mix31toS (sample_t
* samples
, sample_t bias
)
411 sample_t common
, surround
;
413 for (i
= 0; i
< 256; i
++) {
414 common
= BIAS (samples
[i
+ 256]);
415 surround
= samples
[i
+ 768];
416 samples
[i
] += common
- surround
;
417 samples
[i
+ 256] = samples
[i
+ 512] + common
+ surround
;
421 static void mix22toS (sample_t
* samples
, sample_t bias
)
426 for (i
= 0; i
< 256; i
++) {
427 surround
= samples
[i
+ 512] + samples
[i
+ 768];
428 samples
[i
] += BIAS (-surround
);
429 samples
[i
+ 256] += BIAS (surround
);
433 static void mix32to2 (sample_t
* samples
, sample_t bias
)
438 for (i
= 0; i
< 256; i
++) {
439 common
= BIAS (samples
[i
+ 256]);
440 samples
[i
] += common
+ samples
[i
+ 768];
441 samples
[i
+ 256] = common
+ samples
[i
+ 512] + samples
[i
+ 1024];
445 static void mix32toS (sample_t
* samples
, sample_t bias
)
448 sample_t common
, surround
;
450 for (i
= 0; i
< 256; i
++) {
451 common
= BIAS (samples
[i
+ 256]);
452 surround
= samples
[i
+ 768] + samples
[i
+ 1024];
453 samples
[i
] += common
- surround
;
454 samples
[i
+ 256] = samples
[i
+ 512] + common
+ surround
;
458 static void move2to1 (sample_t
* src
, sample_t
* dest
, sample_t bias
)
462 for (i
= 0; i
< 256; i
++)
463 dest
[i
] = BIAS (src
[i
] + src
[i
+ 256]);
466 static void zero (sample_t
* samples
)
470 for (i
= 0; i
< 256; i
++)
474 void a52_downmix (sample_t
* samples
, int acmod
, int output
, sample_t bias
,
475 level_t clev
, level_t slev
)
477 switch (CONVERT (acmod
, output
& A52_CHANNEL_MASK
)) {
479 case CONVERT (A52_CHANNEL
, A52_CHANNEL2
):
480 memcpy (samples
, samples
+ 256, 256 * sizeof (sample_t
));
483 case CONVERT (A52_CHANNEL
, A52_MONO
):
484 case CONVERT (A52_STEREO
, A52_MONO
):
486 mix2to1 (samples
, samples
+ 256, bias
);
489 case CONVERT (A52_2F1R
, A52_MONO
):
492 case CONVERT (A52_3F
, A52_MONO
):
494 mix3to1 (samples
, bias
);
497 case CONVERT (A52_3F1R
, A52_MONO
):
500 case CONVERT (A52_2F2R
, A52_MONO
):
503 mix4to1 (samples
, bias
);
506 case CONVERT (A52_3F2R
, A52_MONO
):
509 mix5to1 (samples
, bias
);
512 case CONVERT (A52_MONO
, A52_DOLBY
):
513 memcpy (samples
+ 256, samples
, 256 * sizeof (sample_t
));
516 case CONVERT (A52_3F
, A52_STEREO
):
517 case CONVERT (A52_3F
, A52_DOLBY
):
519 mix3to2 (samples
, bias
);
522 case CONVERT (A52_2F1R
, A52_STEREO
):
525 mix21to2 (samples
, samples
+ 256, bias
);
528 case CONVERT (A52_2F1R
, A52_DOLBY
):
529 mix21toS (samples
, bias
);
532 case CONVERT (A52_3F1R
, A52_STEREO
):
535 mix31to2 (samples
, bias
);
538 case CONVERT (A52_3F1R
, A52_DOLBY
):
539 mix31toS (samples
, bias
);
542 case CONVERT (A52_2F2R
, A52_STEREO
):
545 mix2to1 (samples
, samples
+ 512, bias
);
546 mix2to1 (samples
+ 256, samples
+ 768, bias
);
549 case CONVERT (A52_2F2R
, A52_DOLBY
):
550 mix22toS (samples
, bias
);
553 case CONVERT (A52_3F2R
, A52_STEREO
):
556 mix32to2 (samples
, bias
);
559 case CONVERT (A52_3F2R
, A52_DOLBY
):
560 mix32toS (samples
, bias
);
563 case CONVERT (A52_3F1R
, A52_3F
):
566 mix21to2 (samples
, samples
+ 512, bias
);
569 case CONVERT (A52_3F2R
, A52_3F
):
572 mix2to1 (samples
, samples
+ 768, bias
);
573 mix2to1 (samples
+ 512, samples
+ 1024, bias
);
576 case CONVERT (A52_3F1R
, A52_2F1R
):
577 mix3to2 (samples
, bias
);
578 memcpy (samples
+ 512, samples
+ 768, 256 * sizeof (sample_t
));
581 case CONVERT (A52_2F2R
, A52_2F1R
):
582 mix2to1 (samples
+ 512, samples
+ 768, bias
);
585 case CONVERT (A52_3F2R
, A52_2F1R
):
586 mix3to2 (samples
, bias
);
587 move2to1 (samples
+ 768, samples
+ 512, bias
);
590 case CONVERT (A52_3F2R
, A52_3F1R
):
591 mix2to1 (samples
+ 768, samples
+ 1024, bias
);
594 case CONVERT (A52_2F1R
, A52_2F2R
):
595 memcpy (samples
+ 768, samples
+ 512, 256 * sizeof (sample_t
));
598 case CONVERT (A52_3F1R
, A52_2F2R
):
599 mix3to2 (samples
, bias
);
600 memcpy (samples
+ 512, samples
+ 768, 256 * sizeof (sample_t
));
603 case CONVERT (A52_3F2R
, A52_2F2R
):
604 mix3to2 (samples
, bias
);
605 memcpy (samples
+ 512, samples
+ 768, 256 * sizeof (sample_t
));
606 memcpy (samples
+ 768, samples
+ 1024, 256 * sizeof (sample_t
));
609 case CONVERT (A52_3F1R
, A52_3F2R
):
610 memcpy (samples
+ 1024, samples
+ 768, 256 * sizeof (sample_t
));
615 void a52_upmix (sample_t
* samples
, int acmod
, int output
)
617 switch (CONVERT (acmod
, output
& A52_CHANNEL_MASK
)) {
619 case CONVERT (A52_CHANNEL
, A52_CHANNEL2
):
620 memcpy (samples
+ 256, samples
, 256 * sizeof (sample_t
));
623 case CONVERT (A52_3F2R
, A52_MONO
):
624 zero (samples
+ 1024);
625 case CONVERT (A52_3F1R
, A52_MONO
):
626 case CONVERT (A52_2F2R
, A52_MONO
):
627 zero (samples
+ 768);
628 case CONVERT (A52_3F
, A52_MONO
):
629 case CONVERT (A52_2F1R
, A52_MONO
):
630 zero (samples
+ 512);
631 case CONVERT (A52_CHANNEL
, A52_MONO
):
632 case CONVERT (A52_STEREO
, A52_MONO
):
633 zero (samples
+ 256);
636 case CONVERT (A52_3F2R
, A52_STEREO
):
637 case CONVERT (A52_3F2R
, A52_DOLBY
):
638 zero (samples
+ 1024);
639 case CONVERT (A52_3F1R
, A52_STEREO
):
640 case CONVERT (A52_3F1R
, A52_DOLBY
):
641 zero (samples
+ 768);
642 case CONVERT (A52_3F
, A52_STEREO
):
643 case CONVERT (A52_3F
, A52_DOLBY
):
645 memcpy (samples
+ 512, samples
+ 256, 256 * sizeof (sample_t
));
646 zero (samples
+ 256);
649 case CONVERT (A52_2F2R
, A52_STEREO
):
650 case CONVERT (A52_2F2R
, A52_DOLBY
):
651 zero (samples
+ 768);
652 case CONVERT (A52_2F1R
, A52_STEREO
):
653 case CONVERT (A52_2F1R
, A52_DOLBY
):
654 zero (samples
+ 512);
657 case CONVERT (A52_3F2R
, A52_3F
):
658 zero (samples
+ 1024);
659 case CONVERT (A52_3F1R
, A52_3F
):
660 case CONVERT (A52_2F2R
, A52_2F1R
):
661 zero (samples
+ 768);
664 case CONVERT (A52_3F2R
, A52_3F1R
):
665 zero (samples
+ 1024);
668 case CONVERT (A52_3F2R
, A52_2F1R
):
669 zero (samples
+ 1024);
670 case CONVERT (A52_3F1R
, A52_2F1R
):
672 memcpy (samples
+ 768, samples
+ 512, 256 * sizeof (sample_t
));
675 case CONVERT (A52_3F2R
, A52_2F2R
):
676 memcpy (samples
+ 1024, samples
+ 768, 256 * sizeof (sample_t
));