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"
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 /* avoid compiler warning */
486 switch (CONVERT (acmod
, output
& A52_CHANNEL_MASK
)) {
488 case CONVERT (A52_CHANNEL
, A52_CHANNEL2
):
489 memcpy (samples
, samples
+ 256, 256 * sizeof (sample_t
));
492 case CONVERT (A52_CHANNEL
, A52_MONO
):
493 case CONVERT (A52_STEREO
, A52_MONO
):
495 mix2to1 (samples
, samples
+ 256, bias
);
498 case CONVERT (A52_2F1R
, A52_MONO
):
501 case CONVERT (A52_3F
, A52_MONO
):
503 mix3to1 (samples
, bias
);
506 case CONVERT (A52_3F1R
, A52_MONO
):
509 case CONVERT (A52_2F2R
, A52_MONO
):
512 mix4to1 (samples
, bias
);
515 case CONVERT (A52_3F2R
, A52_MONO
):
518 mix5to1 (samples
, bias
);
521 case CONVERT (A52_MONO
, A52_DOLBY
):
522 memcpy (samples
+ 256, samples
, 256 * sizeof (sample_t
));
525 case CONVERT (A52_3F
, A52_STEREO
):
526 case CONVERT (A52_3F
, A52_DOLBY
):
528 mix3to2 (samples
, bias
);
531 case CONVERT (A52_2F1R
, A52_STEREO
):
534 mix21to2 (samples
, samples
+ 256, bias
);
537 case CONVERT (A52_2F1R
, A52_DOLBY
):
538 mix21toS (samples
, bias
);
541 case CONVERT (A52_3F1R
, A52_STEREO
):
544 mix31to2 (samples
, bias
);
547 case CONVERT (A52_3F1R
, A52_DOLBY
):
548 mix31toS (samples
, bias
);
551 case CONVERT (A52_2F2R
, A52_STEREO
):
554 mix2to1 (samples
, samples
+ 512, bias
);
555 mix2to1 (samples
+ 256, samples
+ 768, bias
);
558 case CONVERT (A52_2F2R
, A52_DOLBY
):
559 mix22toS (samples
, bias
);
562 case CONVERT (A52_3F2R
, A52_STEREO
):
565 mix32to2 (samples
, bias
);
568 case CONVERT (A52_3F2R
, A52_DOLBY
):
569 mix32toS (samples
, bias
);
572 case CONVERT (A52_3F1R
, A52_3F
):
575 mix21to2 (samples
, samples
+ 512, bias
);
578 case CONVERT (A52_3F2R
, A52_3F
):
581 mix2to1 (samples
, samples
+ 768, bias
);
582 mix2to1 (samples
+ 512, samples
+ 1024, bias
);
585 case CONVERT (A52_3F1R
, A52_2F1R
):
586 mix3to2 (samples
, bias
);
587 memcpy (samples
+ 512, samples
+ 768, 256 * sizeof (sample_t
));
590 case CONVERT (A52_2F2R
, A52_2F1R
):
591 mix2to1 (samples
+ 512, samples
+ 768, bias
);
594 case CONVERT (A52_3F2R
, A52_2F1R
):
595 mix3to2 (samples
, bias
);
596 move2to1 (samples
+ 768, samples
+ 512, bias
);
599 case CONVERT (A52_3F2R
, A52_3F1R
):
600 mix2to1 (samples
+ 768, samples
+ 1024, bias
);
603 case CONVERT (A52_2F1R
, A52_2F2R
):
604 memcpy (samples
+ 768, samples
+ 512, 256 * sizeof (sample_t
));
607 case CONVERT (A52_3F1R
, A52_2F2R
):
608 mix3to2 (samples
, bias
);
609 memcpy (samples
+ 512, samples
+ 768, 256 * sizeof (sample_t
));
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
));
618 case CONVERT (A52_3F1R
, A52_3F2R
):
619 memcpy (samples
+ 1024, samples
+ 768, 256 * sizeof (sample_t
));
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
));
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);
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
):
654 memcpy (samples
+ 512, samples
+ 256, 256 * sizeof (sample_t
));
655 zero (samples
+ 256);
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);
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);
673 case CONVERT (A52_3F2R
, A52_3F1R
):
674 zero (samples
+ 1024);
677 case CONVERT (A52_3F2R
, A52_2F1R
):
678 zero (samples
+ 1024);
679 case CONVERT (A52_3F1R
, A52_2F1R
):
681 memcpy (samples
+ 768, samples
+ 512, 256 * sizeof (sample_t
));
684 case CONVERT (A52_3F2R
, A52_2F2R
):
685 memcpy (samples
+ 1024, samples
+ 768, 256 * sizeof (sample_t
));