comparison libac3/downmix.c @ 0:986e461dc072 libavcodec

Initial revision
author glantau
date Sun, 22 Jul 2001 14:18:56 +0000
parents
children 5aa6292a1660
comparison
equal deleted inserted replaced
-1:000000000000 0:986e461dc072
1 /*
2 *
3 * downmix.c
4 *
5 * Copyright (C) Aaron Holtzman - Sept 1999
6 *
7 * Originally based on code by Yuqing Deng.
8 *
9 * This file is part of ac3dec, a free Dolby AC-3 stream decoder.
10 *
11 * ac3dec is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * ac3dec is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with GNU Make; see the file COPYING. If not, write to
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 *
26 */
27
28 #include <inttypes.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <math.h>
32 #include <string.h>
33 #include "ac3.h"
34 #include "ac3_internal.h"
35
36
37 #define CONVERT(acmod,output) (((output) << 3) + (acmod))
38
39 int downmix_init (int input, int flags, float * level, float clev, float slev)
40 {
41 static uint8_t table[11][8] = {
42 {AC3_CHANNEL, AC3_DOLBY, AC3_STEREO, AC3_STEREO,
43 AC3_STEREO, AC3_STEREO, AC3_STEREO, AC3_STEREO},
44 {AC3_MONO, AC3_MONO, AC3_MONO, AC3_MONO,
45 AC3_MONO, AC3_MONO, AC3_MONO, AC3_MONO},
46 {AC3_CHANNEL, AC3_DOLBY, AC3_STEREO, AC3_STEREO,
47 AC3_STEREO, AC3_STEREO, AC3_STEREO, AC3_STEREO},
48 {AC3_CHANNEL, AC3_DOLBY, AC3_STEREO, AC3_3F,
49 AC3_STEREO, AC3_3F, AC3_STEREO, AC3_3F},
50 {AC3_CHANNEL, AC3_DOLBY, AC3_STEREO, AC3_STEREO,
51 AC3_2F1R, AC3_2F1R, AC3_2F1R, AC3_2F1R},
52 {AC3_CHANNEL, AC3_DOLBY, AC3_STEREO, AC3_STEREO,
53 AC3_2F1R, AC3_3F1R, AC3_2F1R, AC3_3F1R},
54 {AC3_CHANNEL, AC3_DOLBY, AC3_STEREO, AC3_3F,
55 AC3_2F2R, AC3_2F2R, AC3_2F2R, AC3_2F2R},
56 {AC3_CHANNEL, AC3_DOLBY, AC3_STEREO, AC3_3F,
57 AC3_2F2R, AC3_3F2R, AC3_2F2R, AC3_3F2R},
58 {AC3_CHANNEL1, AC3_MONO, AC3_MONO, AC3_MONO,
59 AC3_MONO, AC3_MONO, AC3_MONO, AC3_MONO},
60 {AC3_CHANNEL2, AC3_MONO, AC3_MONO, AC3_MONO,
61 AC3_MONO, AC3_MONO, AC3_MONO, AC3_MONO},
62 {AC3_CHANNEL, AC3_DOLBY, AC3_STEREO, AC3_DOLBY,
63 AC3_DOLBY, AC3_DOLBY, AC3_DOLBY, AC3_DOLBY}
64 };
65 int output;
66
67 output = flags & AC3_CHANNEL_MASK;
68 if (output > AC3_DOLBY)
69 return -1;
70
71 output = table[output][input & 7];
72
73 if ((output == AC3_STEREO) &&
74 ((input == AC3_DOLBY) || ((input == AC3_3F) && (clev == LEVEL_3DB))))
75 output = AC3_DOLBY;
76
77 if (flags & AC3_ADJUST_LEVEL)
78 switch (CONVERT (input & 7, output)) {
79
80 case CONVERT (AC3_3F, AC3_MONO):
81 *level *= LEVEL_3DB / (1 + clev);
82 break;
83
84 case CONVERT (AC3_STEREO, AC3_MONO):
85 case CONVERT (AC3_2F2R, AC3_2F1R):
86 case CONVERT (AC3_3F2R, AC3_3F1R):
87 level_3db:
88 *level *= LEVEL_3DB;
89 break;
90
91 case CONVERT (AC3_3F2R, AC3_2F1R):
92 if (clev < LEVEL_PLUS3DB - 1)
93 goto level_3db;
94 // break thru
95 case CONVERT (AC3_3F, AC3_STEREO):
96 case CONVERT (AC3_3F1R, AC3_2F1R):
97 case CONVERT (AC3_3F1R, AC3_2F2R):
98 case CONVERT (AC3_3F2R, AC3_2F2R):
99 *level /= 1 + clev;
100 break;
101
102 case CONVERT (AC3_2F1R, AC3_MONO):
103 *level *= LEVEL_PLUS3DB / (2 + slev);
104 break;
105
106 case CONVERT (AC3_2F1R, AC3_STEREO):
107 case CONVERT (AC3_3F1R, AC3_3F):
108 *level /= 1 + slev * LEVEL_3DB;
109 break;
110
111 case CONVERT (AC3_3F1R, AC3_MONO):
112 *level *= LEVEL_3DB / (1 + clev + 0.5 * slev);
113 break;
114
115 case CONVERT (AC3_3F1R, AC3_STEREO):
116 *level /= 1 + clev + slev * LEVEL_3DB;
117 break;
118
119 case CONVERT (AC3_2F2R, AC3_MONO):
120 *level *= LEVEL_3DB / (1 + slev);
121 break;
122
123 case CONVERT (AC3_2F2R, AC3_STEREO):
124 case CONVERT (AC3_3F2R, AC3_3F):
125 *level /= (1 + slev);
126 break;
127
128 case CONVERT (AC3_3F2R, AC3_MONO):
129 *level *= LEVEL_3DB / (1 + clev + slev);
130 break;
131
132 case CONVERT (AC3_3F2R, AC3_STEREO):
133 *level /= 1 + clev + slev;
134 break;
135
136 case CONVERT (AC3_MONO, AC3_DOLBY):
137 *level *= LEVEL_PLUS3DB;
138 break;
139
140 case CONVERT (AC3_3F, AC3_DOLBY):
141 case CONVERT (AC3_2F1R, AC3_DOLBY):
142 *level *= 1 / (1 + LEVEL_3DB);
143 break;
144
145 case CONVERT (AC3_3F1R, AC3_DOLBY):
146 case CONVERT (AC3_2F2R, AC3_DOLBY):
147 *level *= 1 / (1 + 2 * LEVEL_3DB);
148 break;
149
150 case CONVERT (AC3_3F2R, AC3_DOLBY):
151 *level *= 1 / (1 + 3 * LEVEL_3DB);
152 break;
153 }
154
155 return output;
156 }
157
158 static void mix1to1 (float * samples, float level, float bias)
159 {
160 int i;
161
162 for (i = 0; i < 256; i++)
163 samples[i] = samples[i] * level + bias;
164 }
165
166 static void move1to1 (float * src, float * dest, float level, float bias)
167 {
168 int i;
169
170 for (i = 0; i < 256; i++)
171 dest[i] = src[i] * level + bias;
172 }
173
174 static void mix2to1 (float * samples, float level, float bias)
175 {
176 int i;
177
178 for (i = 0; i < 256; i++)
179 samples[i] = (samples[i] + samples[i + 256]) * level + bias;
180 }
181
182 static void move2to1 (float * src, float * dest, float level, float bias)
183 {
184 int i;
185
186 for (i = 0; i < 256; i++)
187 dest[i] = (src[i] + src[i + 256]) * level + bias;
188 }
189
190 static void mix3to1 (float * samples, float level, float clev, float bias)
191 {
192 int i;
193
194 for (i = 0; i < 256; i++)
195 samples[i] = ((samples[i] + samples[i + 512]) * level +
196 samples[i + 256] * clev + bias);
197 }
198
199 static void mix21to1 (float * samples, float level, float slev, float bias)
200 {
201 int i;
202
203 for (i = 0; i < 256; i++)
204 samples[i] = ((samples[i] + samples[i + 256]) * level +
205 samples[i + 512] * slev + bias);
206 }
207
208 static void mix31to1 (float * samples, float level, float clev, float slev,
209 float bias)
210 {
211 int i;
212
213 for (i = 0; i < 256; i++)
214 samples[i] = ((samples[i] + samples[i + 512]) * level +
215 samples[i + 256] * clev + samples[i + 768] * slev +
216 bias);
217 }
218
219 static void mix22to1 (float * samples, float level, float slev, float bias)
220 {
221 int i;
222
223 for (i = 0; i < 256; i++)
224 samples[i] = ((samples[i] + samples[i + 256]) * level +
225 (samples[i + 512] + samples[i + 768]) * slev + bias);
226 }
227
228 static void mix32to1 (float * samples, float level, float clev, float slev,
229 float bias)
230 {
231 int i;
232
233 for (i = 0; i < 256; i++)
234 samples[i] = ((samples[i] + samples[i + 512]) * level +
235 samples[i + 256] * clev +
236 (samples[i + 768] + samples[i + 1024]) * slev + bias);
237 }
238
239 static void mix1to2 (float * src, float * dest, float level, float bias)
240 {
241 int i;
242
243 for (i = 0; i < 256; i++)
244 dest[i] = src[i] = src[i] * level + bias;
245 }
246
247 static void mix3to2 (float * samples, float level, float clev, float bias)
248 {
249 int i;
250 float common;
251
252 for (i = 0; i < 256; i++) {
253 common = samples[i + 256] * clev + bias;
254 samples[i] = samples[i] * level + common;
255 samples[i + 256] = samples[i + 512] * level + common;
256 }
257 }
258
259 static void mix21to2 (float * left, float * right, float level, float slev,
260 float bias)
261 {
262 int i;
263 float common;
264
265 for (i = 0; i < 256; i++) {
266 common = right[i + 256] * slev + bias;
267 left[i] = left[i] * level + common;
268 right[i] = right[i] * level + common;
269 }
270 }
271
272 static void mix11to1 (float * front, float * rear, float level, float slev,
273 float bias)
274 {
275 int i;
276
277 for (i = 0; i < 256; i++)
278 front[i] = front[i] * level + rear[i] * slev + bias;
279 }
280
281 static void mix31to2 (float * samples, float level, float clev, float slev,
282 float bias)
283 {
284 int i;
285 float common;
286
287 for (i = 0; i < 256; i++) {
288 common = samples[i + 256] * clev + samples[i + 768] * slev + bias;
289 samples[i] = samples[i] * level + common;
290 samples[i + 256] = samples[i + 512] * level + common;
291 }
292 }
293
294 static void mix32to2 (float * samples, float level, float clev, float slev,
295 float bias)
296 {
297 int i;
298 float common;
299
300 for (i = 0; i < 256; i++) {
301 common = samples[i + 256] * clev + bias;
302 samples[i] = samples[i] * level + common + samples[i + 768] * slev;
303 samples[i + 256] = (samples[i + 512] * level + common +
304 samples[i + 1024] * slev);
305 }
306 }
307
308 static void mix21toS (float * samples, float level, float level3db, float bias)
309 {
310 int i;
311 float surround;
312
313 for (i = 0; i < 256; i++) {
314 surround = samples[i + 512] * level3db;
315 samples[i] = samples[i] * level - surround + bias;
316 samples[i + 256] = samples[i + 256] * level + surround + bias;
317 }
318 }
319
320 static void mix22toS (float * samples, float level, float level3db, float bias)
321 {
322 int i;
323 float surround;
324
325 for (i = 0; i < 256; i++) {
326 surround = (samples[i + 512] + samples[i + 768]) * level3db;
327 samples[i] = samples[i] * level - surround + bias;
328 samples[i + 256] = samples[i + 256] * level + surround + bias;
329 }
330 }
331
332 static void mix31toS (float * samples, float level, float level3db, float bias)
333 {
334 int i;
335 float common, surround;
336
337 for (i = 0; i < 256; i++) {
338 common = samples[i + 256] * level3db + bias;
339 surround = samples[i + 768] * level3db;
340 samples[i] = samples[i] * level + common - surround;
341 samples[i + 256] = samples[i + 512] * level + common + surround;
342 }
343 }
344
345 static void mix32toS (float * samples, float level, float level3db, float bias)
346 {
347 int i;
348 float common, surround;
349
350 for (i = 0; i < 256; i++) {
351 common = samples[i + 256] * level3db + bias;
352 surround = (samples[i + 768] + samples[i + 1024]) * level3db;
353 samples[i] = samples[i] * level + common - surround;
354 samples[i + 256] = samples[i + 512] * level + common + surround;
355 }
356 }
357
358 void downmix (float * samples, int acmod, int output, float level, float bias,
359 float clev, float slev)
360 {
361 switch (CONVERT (acmod, output & AC3_CHANNEL_MASK)) {
362
363 case CONVERT (AC3_3F2R, AC3_3F2R):
364 mix1to1 (samples + 1024, level, bias);
365 case CONVERT (AC3_3F1R, AC3_3F1R):
366 case CONVERT (AC3_2F2R, AC3_2F2R):
367 mix1to1 (samples + 768, level, bias);
368 case CONVERT (AC3_3F, AC3_3F):
369 case CONVERT (AC3_2F1R, AC3_2F1R):
370 mix_3to3:
371 mix1to1 (samples + 512, level, bias);
372 case CONVERT (AC3_CHANNEL, AC3_CHANNEL):
373 case CONVERT (AC3_STEREO, AC3_STEREO):
374 case CONVERT (AC3_STEREO, AC3_DOLBY):
375 mix_2to2:
376 mix1to1 (samples + 256, level, bias);
377 case CONVERT (AC3_CHANNEL, AC3_CHANNEL1):
378 case CONVERT (AC3_MONO, AC3_MONO):
379 mix1to1 (samples, level, bias);
380 break;
381
382 case CONVERT (AC3_CHANNEL, AC3_CHANNEL2):
383 mix_1to1_b:
384 mix1to1 (samples + 256, level, bias);
385 break;
386
387 case CONVERT (AC3_STEREO, AC3_MONO):
388 mix_2to1:
389 mix2to1 (samples, level * LEVEL_3DB, bias);
390 break;
391
392 case CONVERT (AC3_2F1R, AC3_MONO):
393 if (slev == 0)
394 goto mix_2to1;
395 mix21to1 (samples, level * LEVEL_3DB, level * slev * LEVEL_3DB, bias);
396 break;
397
398 case CONVERT (AC3_2F2R, AC3_MONO):
399 if (slev == 0)
400 goto mix_2to1;
401 mix22to1 (samples, level * LEVEL_3DB, level * slev * LEVEL_3DB, bias);
402 break;
403
404 case CONVERT (AC3_3F, AC3_MONO):
405 mix_3to1:
406 mix3to1 (samples, level * LEVEL_3DB, level * clev * LEVEL_PLUS3DB,
407 bias);
408 break;
409
410 case CONVERT (AC3_3F1R, AC3_MONO):
411 if (slev == 0)
412 goto mix_3to1;
413 mix31to1 (samples, level * LEVEL_3DB, level * clev * LEVEL_PLUS3DB,
414 level * slev * LEVEL_3DB, bias);
415 break;
416
417 case CONVERT (AC3_3F2R, AC3_MONO):
418 if (slev == 0)
419 goto mix_3to1;
420 mix32to1 (samples, level * LEVEL_3DB, level * clev * LEVEL_PLUS3DB,
421 level * slev * LEVEL_3DB, bias);
422 break;
423
424 case CONVERT (AC3_CHANNEL, AC3_MONO):
425 mix2to1 (samples, level * LEVEL_6DB, bias);
426 break;
427
428 case CONVERT (AC3_MONO, AC3_DOLBY):
429 mix1to2 (samples, samples + 256, level * LEVEL_3DB, bias);
430 break;
431
432 case CONVERT (AC3_3F, AC3_DOLBY):
433 clev = LEVEL_3DB;
434 case CONVERT (AC3_3F, AC3_STEREO):
435 mix_3to2:
436 mix3to2 (samples, level, level * clev, bias);
437 break;
438
439 case CONVERT (AC3_2F1R, AC3_DOLBY):
440 mix21toS (samples, level, level * LEVEL_3DB, bias);
441 break;
442
443 case CONVERT (AC3_3F1R, AC3_DOLBY):
444 mix31toS (samples, level, level * LEVEL_3DB, bias);
445 break;
446
447 case CONVERT (AC3_2F2R, AC3_DOLBY):
448 mix22toS (samples, level, level * LEVEL_3DB, bias);
449 break;
450
451 case CONVERT (AC3_3F2R, AC3_DOLBY):
452 mix32toS (samples, level, level * LEVEL_3DB, bias);
453 break;
454
455 case CONVERT (AC3_2F1R, AC3_STEREO):
456 if (slev == 0)
457 goto mix_2to2;
458 mix21to2 (samples, samples + 256, level, level * slev * LEVEL_3DB,
459 bias);
460 break;
461
462 case CONVERT (AC3_3F1R, AC3_STEREO):
463 if (slev == 0)
464 goto mix_3to2;
465 mix31to2 (samples, level, level * clev, level * slev * LEVEL_3DB,
466 bias);
467 break;
468
469 case CONVERT (AC3_2F2R, AC3_STEREO):
470 if (slev == 0)
471 goto mix_2to2;
472 mix11to1 (samples, samples + 512, level, level * slev, bias);
473 mix11to1 (samples + 256, samples + 768, level, level * slev, bias);
474 break;
475
476 case CONVERT (AC3_3F2R, AC3_STEREO):
477 if (slev == 0)
478 goto mix_3to2;
479 mix32to2 (samples, level, level * clev, level * slev, bias);
480 break;
481
482 case CONVERT (AC3_3F1R, AC3_3F):
483 if (slev == 0)
484 goto mix_3to3;
485 mix21to2 (samples, samples + 512, level, level * slev * LEVEL_3DB,
486 bias);
487
488 case CONVERT (AC3_3F2R, AC3_3F):
489 if (slev == 0)
490 goto mix_3to3;
491 mix11to1 (samples, samples + 768, level, level * slev, bias);
492 mix11to1 (samples + 512, samples + 1024, level, level * slev, bias);
493 goto mix_1to1_b;
494
495 case CONVERT (AC3_2F1R, AC3_2F2R):
496 mix1to2 (samples + 512, samples + 768, level * LEVEL_3DB, bias);
497 goto mix_2to2;
498
499 case CONVERT (AC3_3F1R, AC3_3F2R):
500 mix1to2 (samples + 768, samples + 1024, level * LEVEL_3DB, bias);
501 goto mix_3to3;
502
503 case CONVERT (AC3_2F2R, AC3_2F1R):
504 mix2to1 (samples + 512, level * LEVEL_3DB, bias);
505 goto mix_2to2;
506
507 case CONVERT (AC3_3F2R, AC3_3F1R):
508 mix2to1 (samples + 768, level * LEVEL_3DB, bias);
509 goto mix_3to3;
510
511 case CONVERT (AC3_3F1R, AC3_2F2R):
512 mix3to2 (samples, level, level * clev, bias);
513 mix1to2 (samples + 768, samples + 512, level * LEVEL_3DB, bias);
514 break;
515
516 case CONVERT (AC3_3F1R, AC3_2F1R):
517 mix3to2 (samples, level, level * clev, bias);
518 move1to1 (samples + 768, samples + 512, level, bias);
519 break;
520
521 case CONVERT (AC3_3F2R, AC3_2F1R):
522 mix3to2 (samples, level, level * clev, bias);
523 move2to1 (samples + 768, samples + 512, level * LEVEL_3DB, bias);
524 break;
525
526 case CONVERT (AC3_3F2R, AC3_2F2R):
527 mix3to2 (samples, level, level * clev, bias);
528 move1to1 (samples + 768, samples + 512, level, bias);
529 move1to1 (samples + 1024, samples + 768, level, bias);
530 break;
531
532 }
533 }