make mpa_decode_header() 10 times faster
[FFMpeg-mirror/ordered_chapters.git] / libavcodec / liba52 / downmix.c
blob7999b7db0910f17f6bf32bc943c00fe0582d8153
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "a52.h"
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}
55 int output;
57 output = flags & A52_CHANNEL_MASK;
58 if (output > A52_DOLBY)
59 return -1;
61 output = table[output][input & 7];
63 if (output == A52_STEREO &&
64 (input == A52_DOLBY || (input == A52_3F && clev == LEVEL (LEVEL_3DB))))
65 output = A52_DOLBY;
67 if (flags & A52_ADJUST_LEVEL) {
68 level_t adjust;
70 switch (CONVERT (input & 7, output)) {
72 case CONVERT (A52_3F, A52_MONO):
73 adjust = DIV (LEVEL_3DB, LEVEL (1) + clev);
74 break;
76 case CONVERT (A52_STEREO, A52_MONO):
77 case CONVERT (A52_2F2R, A52_2F1R):
78 case CONVERT (A52_3F2R, A52_3F1R):
79 level_3db:
80 adjust = LEVEL (LEVEL_3DB);
81 break;
83 case CONVERT (A52_3F2R, A52_2F1R):
84 if (clev < LEVEL (LEVEL_PLUS3DB - 1))
85 goto level_3db;
86 /* break thru */
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);
92 break;
94 case CONVERT (A52_2F1R, A52_MONO):
95 adjust = DIV (LEVEL_PLUS3DB, LEVEL (2) + slev);
96 break;
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));
101 break;
103 case CONVERT (A52_3F1R, A52_MONO):
104 adjust = DIV (LEVEL_3DB, LEVEL (1) + clev + MUL_C (slev, 0.5));
105 break;
107 case CONVERT (A52_3F1R, A52_STEREO):
108 adjust = DIV (1, LEVEL (1) + clev + MUL_C (slev, LEVEL_3DB));
109 break;
111 case CONVERT (A52_2F2R, A52_MONO):
112 adjust = DIV (LEVEL_3DB, LEVEL (1) + slev);
113 break;
115 case CONVERT (A52_2F2R, A52_STEREO):
116 case CONVERT (A52_3F2R, A52_3F):
117 adjust = DIV (1, LEVEL (1) + slev);
118 break;
120 case CONVERT (A52_3F2R, A52_MONO):
121 adjust = DIV (LEVEL_3DB, LEVEL (1) + clev + slev);
122 break;
124 case CONVERT (A52_3F2R, A52_STEREO):
125 adjust = DIV (1, LEVEL (1) + clev + slev);
126 break;
128 case CONVERT (A52_MONO, A52_DOLBY):
129 adjust = LEVEL (LEVEL_PLUS3DB);
130 break;
132 case CONVERT (A52_3F, A52_DOLBY):
133 case CONVERT (A52_2F1R, A52_DOLBY):
134 adjust = LEVEL (1 / (1 + LEVEL_3DB));
135 break;
137 case CONVERT (A52_3F1R, A52_DOLBY):
138 case CONVERT (A52_2F2R, A52_DOLBY):
139 adjust = LEVEL (1 / (1 + 2 * LEVEL_3DB));
140 break;
142 case CONVERT (A52_3F2R, A52_DOLBY):
143 adjust = LEVEL (1 / (1 + 3 * LEVEL_3DB));
144 break;
146 default:
147 return output;
150 *level = MUL_L (*level, adjust);
153 return output;
156 int a52_downmix_coeff (level_t * coeff, int acmod, int output, level_t level,
157 level_t clev, level_t slev)
159 level_t level_3db;
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;
175 return 0;
177 case CONVERT (A52_CHANNEL, A52_MONO):
178 coeff[0] = coeff[1] = MUL_C (level, LEVEL_6DB);
179 return 3;
181 case CONVERT (A52_STEREO, A52_MONO):
182 coeff[0] = coeff[1] = level_3db;
183 return 3;
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);
188 return 7;
190 case CONVERT (A52_2F1R, A52_MONO):
191 coeff[0] = coeff[1] = level_3db;
192 coeff[2] = MUL_L (level_3db, slev);
193 return 7;
195 case CONVERT (A52_2F2R, A52_MONO):
196 coeff[0] = coeff[1] = level_3db;
197 coeff[2] = coeff[3] = MUL_L (level_3db, slev);
198 return 15;
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);
204 return 15;
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);
210 return 31;
212 case CONVERT (A52_MONO, A52_DOLBY):
213 coeff[0] = level_3db;
214 return 0;
216 case CONVERT (A52_3F, A52_DOLBY):
217 coeff[0] = coeff[2] = coeff[3] = coeff[4] = level;
218 coeff[1] = level_3db;
219 return 7;
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);
226 return 7;
228 case CONVERT (A52_2F1R, A52_DOLBY):
229 coeff[0] = coeff[1] = level;
230 coeff[2] = level_3db;
231 return 7;
233 case CONVERT (A52_2F1R, A52_STEREO):
234 coeff[0] = coeff[1] = level;
235 coeff[2] = MUL_L (level_3db, slev);
236 return 7;
238 case CONVERT (A52_3F1R, A52_DOLBY):
239 coeff[0] = coeff[2] = level;
240 coeff[1] = coeff[3] = level_3db;
241 return 15;
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);
247 return 15;
249 case CONVERT (A52_2F2R, A52_DOLBY):
250 coeff[0] = coeff[1] = level;
251 coeff[2] = coeff[3] = level_3db;
252 return 15;
254 case CONVERT (A52_2F2R, A52_STEREO):
255 coeff[0] = coeff[1] = level;
256 coeff[2] = coeff[3] = MUL_L (level, slev);
257 return 15;
259 case CONVERT (A52_3F2R, A52_DOLBY):
260 coeff[0] = coeff[2] = level;
261 coeff[1] = coeff[3] = coeff[4] = level_3db;
262 return 31;
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;
268 return 31;
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);
274 return 31;
276 case CONVERT (A52_3F1R, A52_3F):
277 coeff[0] = coeff[1] = coeff[2] = level;
278 coeff[3] = MUL_L (level_3db, slev);
279 return 13;
281 case CONVERT (A52_3F2R, A52_3F):
282 coeff[0] = coeff[1] = coeff[2] = level;
283 coeff[3] = coeff[4] = MUL_L (level, slev);
284 return 29;
286 case CONVERT (A52_2F2R, A52_2F1R):
287 coeff[0] = coeff[1] = level;
288 coeff[2] = coeff[3] = level_3db;
289 return 12;
291 case CONVERT (A52_3F2R, A52_3F1R):
292 coeff[0] = coeff[1] = coeff[2] = level;
293 coeff[3] = coeff[4] = level_3db;
294 return 24;
296 case CONVERT (A52_2F1R, A52_2F2R):
297 coeff[0] = coeff[1] = level;
298 coeff[2] = level_3db;
299 return 0;
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;
305 return 7;
307 case CONVERT (A52_3F1R, A52_3F2R):
308 coeff[0] = coeff[1] = coeff[2] = level;
309 coeff[3] = level_3db;
310 return 0;
312 case CONVERT (A52_CHANNEL, A52_CHANNEL1):
313 coeff[0] = level;
314 coeff[1] = 0;
315 return 0;
317 case CONVERT (A52_CHANNEL, A52_CHANNEL2):
318 coeff[0] = 0;
319 coeff[1] = level;
320 return 0;
323 return -1; /* NOTREACHED */
326 static void mix2to1 (sample_t * dest, sample_t * src, sample_t bias)
328 int i;
330 for (i = 0; i < 256; i++)
331 dest[i] += BIAS (src[i]);
334 static void mix3to1 (sample_t * samples, sample_t bias)
336 int i;
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)
344 int i;
346 for (i = 0; i < 256; i++)
347 samples[i] += BIAS (samples[i + 256] + samples[i + 512] +
348 samples[i + 768]);
351 static void mix5to1 (sample_t * samples, sample_t bias)
353 int i;
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)
362 int i;
363 sample_t common;
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)
374 int i;
375 sample_t common;
377 for (i = 0; i < 256; i++) {
378 common = BIAS (right[i + 256]);
379 left[i] += common;
380 right[i] += common;
384 static void mix21toS (sample_t * samples, sample_t bias)
386 int i;
387 sample_t surround;
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)
398 int i;
399 sample_t common;
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)
410 int i;
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)
423 int i;
424 sample_t surround;
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)
435 int i;
436 sample_t common;
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)
447 int i;
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)
460 int i;
462 for (i = 0; i < 256; i++)
463 dest[i] = BIAS (src[i] + src[i + 256]);
466 static void zero (sample_t * samples)
468 int i;
470 for (i = 0; i < 256; i++)
471 samples[i] = 0;
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));
481 break;
483 case CONVERT (A52_CHANNEL, A52_MONO):
484 case CONVERT (A52_STEREO, A52_MONO):
485 mix_2to1:
486 mix2to1 (samples, samples + 256, bias);
487 break;
489 case CONVERT (A52_2F1R, A52_MONO):
490 if (slev == 0)
491 goto mix_2to1;
492 case CONVERT (A52_3F, A52_MONO):
493 mix_3to1:
494 mix3to1 (samples, bias);
495 break;
497 case CONVERT (A52_3F1R, A52_MONO):
498 if (slev == 0)
499 goto mix_3to1;
500 case CONVERT (A52_2F2R, A52_MONO):
501 if (slev == 0)
502 goto mix_2to1;
503 mix4to1 (samples, bias);
504 break;
506 case CONVERT (A52_3F2R, A52_MONO):
507 if (slev == 0)
508 goto mix_3to1;
509 mix5to1 (samples, bias);
510 break;
512 case CONVERT (A52_MONO, A52_DOLBY):
513 memcpy (samples + 256, samples, 256 * sizeof (sample_t));
514 break;
516 case CONVERT (A52_3F, A52_STEREO):
517 case CONVERT (A52_3F, A52_DOLBY):
518 mix_3to2:
519 mix3to2 (samples, bias);
520 break;
522 case CONVERT (A52_2F1R, A52_STEREO):
523 if (slev == 0)
524 break;
525 mix21to2 (samples, samples + 256, bias);
526 break;
528 case CONVERT (A52_2F1R, A52_DOLBY):
529 mix21toS (samples, bias);
530 break;
532 case CONVERT (A52_3F1R, A52_STEREO):
533 if (slev == 0)
534 goto mix_3to2;
535 mix31to2 (samples, bias);
536 break;
538 case CONVERT (A52_3F1R, A52_DOLBY):
539 mix31toS (samples, bias);
540 break;
542 case CONVERT (A52_2F2R, A52_STEREO):
543 if (slev == 0)
544 break;
545 mix2to1 (samples, samples + 512, bias);
546 mix2to1 (samples + 256, samples + 768, bias);
547 break;
549 case CONVERT (A52_2F2R, A52_DOLBY):
550 mix22toS (samples, bias);
551 break;
553 case CONVERT (A52_3F2R, A52_STEREO):
554 if (slev == 0)
555 goto mix_3to2;
556 mix32to2 (samples, bias);
557 break;
559 case CONVERT (A52_3F2R, A52_DOLBY):
560 mix32toS (samples, bias);
561 break;
563 case CONVERT (A52_3F1R, A52_3F):
564 if (slev == 0)
565 break;
566 mix21to2 (samples, samples + 512, bias);
567 break;
569 case CONVERT (A52_3F2R, A52_3F):
570 if (slev == 0)
571 break;
572 mix2to1 (samples, samples + 768, bias);
573 mix2to1 (samples + 512, samples + 1024, bias);
574 break;
576 case CONVERT (A52_3F1R, A52_2F1R):
577 mix3to2 (samples, bias);
578 memcpy (samples + 512, samples + 768, 256 * sizeof (sample_t));
579 break;
581 case CONVERT (A52_2F2R, A52_2F1R):
582 mix2to1 (samples + 512, samples + 768, bias);
583 break;
585 case CONVERT (A52_3F2R, A52_2F1R):
586 mix3to2 (samples, bias);
587 move2to1 (samples + 768, samples + 512, bias);
588 break;
590 case CONVERT (A52_3F2R, A52_3F1R):
591 mix2to1 (samples + 768, samples + 1024, bias);
592 break;
594 case CONVERT (A52_2F1R, A52_2F2R):
595 memcpy (samples + 768, samples + 512, 256 * sizeof (sample_t));
596 break;
598 case CONVERT (A52_3F1R, A52_2F2R):
599 mix3to2 (samples, bias);
600 memcpy (samples + 512, samples + 768, 256 * sizeof (sample_t));
601 break;
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));
607 break;
609 case CONVERT (A52_3F1R, A52_3F2R):
610 memcpy (samples + 1024, samples + 768, 256 * sizeof (sample_t));
611 break;
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));
621 break;
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);
634 break;
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):
644 mix_3to2:
645 memcpy (samples + 512, samples + 256, 256 * sizeof (sample_t));
646 zero (samples + 256);
647 break;
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);
655 break;
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);
662 break;
664 case CONVERT (A52_3F2R, A52_3F1R):
665 zero (samples + 1024);
666 break;
668 case CONVERT (A52_3F2R, A52_2F1R):
669 zero (samples + 1024);
670 case CONVERT (A52_3F1R, A52_2F1R):
671 mix_31to21:
672 memcpy (samples + 768, samples + 512, 256 * sizeof (sample_t));
673 goto mix_3to2;
675 case CONVERT (A52_3F2R, A52_2F2R):
676 memcpy (samples + 1024, samples + 768, 256 * sizeof (sample_t));
677 goto mix_31to21;