downmix.c 17.6 KB
Newer Older
Gildas Bazin's avatar
Gildas Bazin committed
1 2 3 4 5 6
/*
 * downmix.c
 * Copyright (C) 2004 Gildas Bazin <gbazin@videolan.org>
 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
 *
7 8
 * This file is part of libdca, a free DTS Coherent Acoustics stream decoder.
 * See http://www.videolan.org/developers/libdca.html for updates.
Gildas Bazin's avatar
Gildas Bazin committed
9
 *
10
 * libdca is free software; you can redistribute it and/or modify
Gildas Bazin's avatar
Gildas Bazin committed
11 12 13 14
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
15
 * libdca is distributed in the hope that it will be useful,
Gildas Bazin's avatar
Gildas Bazin committed
16 17 18 19 20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
21 22 23
 * along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Gildas Bazin's avatar
Gildas Bazin committed
24 25 26 27 28 29 30
 */

#include "config.h"

#include <string.h>
#include <inttypes.h>

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
31
#include "dca.h"
32
#include "dca_internal.h"
Gildas Bazin's avatar
Gildas Bazin committed
33

34
#define CONVERT(acmod,output) (((output) << DCA_CHANNEL_BITS) + (acmod))
Gildas Bazin's avatar
Gildas Bazin committed
35

36
int dca_downmix_init (int input, int flags, level_t * level,
Gildas Bazin's avatar
Gildas Bazin committed
37 38
		      level_t clev, level_t slev)
{
39
    static const uint8_t table[11][10] = {
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        /* DCA_MONO */
        {DCA_MONO,      DCA_MONO,       DCA_MONO,       DCA_MONO,
         DCA_MONO,      DCA_MONO,       DCA_MONO,       DCA_MONO,
         DCA_MONO,      DCA_MONO},
        /* DCA_CHANNEL */
        {DCA_MONO,      DCA_CHANNEL,    DCA_STEREO,     DCA_STEREO,
         DCA_STEREO,    DCA_STEREO,     DCA_STEREO,     DCA_STEREO,
         DCA_STEREO,    DCA_STEREO},
        /* DCA_STEREO */
        {DCA_MONO,      DCA_CHANNEL,    DCA_STEREO,     DCA_STEREO,
         DCA_STEREO,    DCA_STEREO,     DCA_STEREO,     DCA_STEREO,
         DCA_STEREO,    DCA_STEREO},
        /* DCA_STEREO_SUMDIFF */
        {DCA_MONO,      DCA_CHANNEL,    DCA_STEREO,     DCA_STEREO,
         DCA_STEREO,    DCA_STEREO,     DCA_STEREO,     DCA_STEREO,
         DCA_STEREO,    DCA_STEREO},
        /* DCA_STEREO_TOTAL */
        {DCA_MONO,      DCA_CHANNEL,    DCA_STEREO,     DCA_STEREO,
         DCA_STEREO,    DCA_STEREO,     DCA_STEREO,     DCA_STEREO,
         DCA_STEREO,    DCA_STEREO},
        /* DCA_3F */
        {DCA_MONO,      DCA_CHANNEL,    DCA_STEREO,     DCA_STEREO,
         DCA_STEREO,    DCA_3F,         DCA_3F,         DCA_3F,
         DCA_3F,        DCA_3F},
        /* DCA_2F1R */
        {DCA_MONO,      DCA_CHANNEL,    DCA_STEREO,     DCA_STEREO,
         DCA_STEREO,    DCA_2F1R,       DCA_2F1R,       DCA_2F1R,
         DCA_2F1R,      DCA_2F1R},
        /* DCA_3F1R */
        {DCA_MONO,      DCA_CHANNEL,    DCA_STEREO,     DCA_STEREO,
         DCA_STEREO,    DCA_3F,         DCA_3F1R,       DCA_3F1R,
         DCA_3F1R,      DCA_3F1R},
        /* DCA_2F2R */
        {DCA_MONO,      DCA_CHANNEL,    DCA_STEREO,     DCA_STEREO,
         DCA_STEREO,    DCA_STEREO,     DCA_2F2R,       DCA_2F2R,
         DCA_2F2R,      DCA_2F2R},
        /* DCA_3F2R */
        {DCA_MONO,      DCA_CHANNEL,    DCA_STEREO,     DCA_STEREO,
         DCA_STEREO,    DCA_3F,         DCA_3F2R,       DCA_3F2R,
         DCA_3F2R,      DCA_3F2R},
        /* DCA_4F2R */
        {DCA_MONO,      DCA_CHANNEL,    DCA_STEREO,     DCA_STEREO,
         DCA_STEREO,    DCA_4F2R,       DCA_4F2R,       DCA_4F2R,
         DCA_4F2R,      DCA_4F2R},
Gildas Bazin's avatar
Gildas Bazin committed
84 85 86
    };
    int output;

87
    output = flags & DCA_CHANNEL_MASK;
Gildas Bazin's avatar
Gildas Bazin committed
88

89
    if (output > DCA_CHANNEL_MAX)
Gildas Bazin's avatar
Gildas Bazin committed
90 91 92 93
	return -1;

    output = table[output][input];

94 95 96
    if (output == DCA_STEREO &&
	(input == DCA_DOLBY || (input == DCA_3F && clev == LEVEL (LEVEL_3DB))))
	output = DCA_DOLBY;
Gildas Bazin's avatar
Gildas Bazin committed
97

98
    if (flags & DCA_ADJUST_LEVEL) {
Gildas Bazin's avatar
Gildas Bazin committed
99 100 101 102
	level_t adjust;

	switch (CONVERT (input & 7, output)) {

103
	case CONVERT (DCA_3F, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
104 105 106
	    adjust = DIV (LEVEL_3DB, LEVEL (1) + clev);
	    break;

107 108 109
	case CONVERT (DCA_STEREO, DCA_MONO):
	case CONVERT (DCA_2F2R, DCA_2F1R):
	case CONVERT (DCA_3F2R, DCA_3F1R):
Gildas Bazin's avatar
Gildas Bazin committed
110 111 112 113
	level_3db:
	    adjust = LEVEL (LEVEL_3DB);
	    break;

114
	case CONVERT (DCA_3F2R, DCA_2F1R):
Gildas Bazin's avatar
Gildas Bazin committed
115 116 117
	    if (clev < LEVEL (LEVEL_PLUS3DB - 1))
		goto level_3db;
	    /* break thru */
118 119 120 121
	case CONVERT (DCA_3F, DCA_STEREO):
	case CONVERT (DCA_3F1R, DCA_2F1R):
	case CONVERT (DCA_3F1R, DCA_2F2R):
	case CONVERT (DCA_3F2R, DCA_2F2R):
Gildas Bazin's avatar
Gildas Bazin committed
122 123 124
	    adjust = DIV (1, LEVEL (1) + clev);
	    break;

125
	case CONVERT (DCA_2F1R, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
126 127 128
	    adjust = DIV (LEVEL_PLUS3DB, LEVEL (2) + slev);
	    break;

129 130
	case CONVERT (DCA_2F1R, DCA_STEREO):
	case CONVERT (DCA_3F1R, DCA_3F):
Gildas Bazin's avatar
Gildas Bazin committed
131 132 133
	    adjust = DIV (1, LEVEL (1) + MUL_C (slev, LEVEL_3DB));
	    break;

134
	case CONVERT (DCA_3F1R, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
135 136 137
	    adjust = DIV (LEVEL_3DB, LEVEL (1) + clev + MUL_C (slev, 0.5));
	    break;

138
	case CONVERT (DCA_3F1R, DCA_STEREO):
Gildas Bazin's avatar
Gildas Bazin committed
139 140 141
	    adjust = DIV (1, LEVEL (1) + clev + MUL_C (slev, LEVEL_3DB));
	    break;

142
	case CONVERT (DCA_2F2R, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
143 144 145
	    adjust = DIV (LEVEL_3DB, LEVEL (1) + slev);
	    break;

146 147
	case CONVERT (DCA_2F2R, DCA_STEREO):
	case CONVERT (DCA_3F2R, DCA_3F):
Gildas Bazin's avatar
Gildas Bazin committed
148 149 150
	    adjust = DIV (1, LEVEL (1) + slev);
	    break;

151
	case CONVERT (DCA_3F2R, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
152 153 154
	    adjust = DIV (LEVEL_3DB, LEVEL (1) + clev + slev);
	    break;

155
	case CONVERT (DCA_3F2R, DCA_STEREO):
Gildas Bazin's avatar
Gildas Bazin committed
156 157 158
	    adjust = DIV (1, LEVEL (1) + clev + slev);
	    break;

159
	case CONVERT (DCA_MONO, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
160 161 162
	    adjust = LEVEL (LEVEL_PLUS3DB);
	    break;

163 164
	case CONVERT (DCA_3F, DCA_DOLBY):
	case CONVERT (DCA_2F1R, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
165 166 167
	    adjust = LEVEL (1 / (1 + LEVEL_3DB));
	    break;

168 169
	case CONVERT (DCA_3F1R, DCA_DOLBY):
	case CONVERT (DCA_2F2R, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
170 171 172
	    adjust = LEVEL (1 / (1 + 2 * LEVEL_3DB));
	    break;

173
	case CONVERT (DCA_3F2R, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
174 175 176 177 178 179 180 181 182 183 184 185 186
	    adjust = LEVEL (1 / (1 + 3 * LEVEL_3DB));
	    break;

	default:
	    return output;
	}

	*level = MUL_L (*level, adjust);
    }

    return output;
}

187
int dca_downmix_coeff (level_t * coeff, int acmod, int output, level_t level,
Gildas Bazin's avatar
Gildas Bazin committed
188 189 190 191 192 193
		       level_t clev, level_t slev)
{
    level_t level_3db;

    level_3db = MUL_C (level, LEVEL_3DB);

194 195 196 197 198 199 200 201 202 203 204
    switch (CONVERT (acmod, output & DCA_CHANNEL_MASK)) {

    case CONVERT (DCA_CHANNEL, DCA_CHANNEL):
    case CONVERT (DCA_MONO, DCA_MONO):
    case CONVERT (DCA_STEREO, DCA_STEREO):
    case CONVERT (DCA_3F, DCA_3F):
    case CONVERT (DCA_2F1R, DCA_2F1R):
    case CONVERT (DCA_3F1R, DCA_3F1R):
    case CONVERT (DCA_2F2R, DCA_2F2R):
    case CONVERT (DCA_3F2R, DCA_3F2R):
    case CONVERT (DCA_STEREO, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
205 206 207
	coeff[0] = coeff[1] = coeff[2] = coeff[3] = coeff[4] = level;
	return 0;

208
    case CONVERT (DCA_CHANNEL, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
209 210 211
	coeff[0] = coeff[1] = MUL_C (level, LEVEL_6DB);
	return 3;

212
    case CONVERT (DCA_STEREO, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
213 214 215
	coeff[0] = coeff[1] = level_3db;
	return 3;

216
    case CONVERT (DCA_3F, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
217 218 219 220
	coeff[0] = coeff[2] = level_3db;
	coeff[1] = MUL_C (MUL_L (level_3db, clev), LEVEL_PLUS6DB);
	return 7;

221
    case CONVERT (DCA_2F1R, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
222 223 224 225
	coeff[0] = coeff[1] = level_3db;
	coeff[2] = MUL_L (level_3db, slev);
	return 7;

226
    case CONVERT (DCA_2F2R, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
227 228 229 230
	coeff[0] = coeff[1] = level_3db;
	coeff[2] = coeff[3] = MUL_L (level_3db, slev);
	return 15;

231
    case CONVERT (DCA_3F1R, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
232 233 234 235 236
	coeff[0] = coeff[2] = level_3db;
	coeff[1] = MUL_C (MUL_L (level_3db, clev), LEVEL_PLUS6DB);
	coeff[3] = MUL_L (level_3db, slev);
	return 15;

237
    case CONVERT (DCA_3F2R, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
238 239 240 241 242
	coeff[0] = coeff[2] = level_3db;
	coeff[1] = MUL_C (MUL_L (level_3db, clev), LEVEL_PLUS6DB);
	coeff[3] = coeff[4] = MUL_L (level_3db, slev);
	return 31;

243
    case CONVERT (DCA_MONO, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
244 245 246
	coeff[0] = level_3db;
	return 0;

247
    case CONVERT (DCA_3F, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
248 249 250 251
	coeff[0] = coeff[2] = coeff[3] = coeff[4] = level;
	coeff[1] = level_3db;
	return 7;

252 253 254
    case CONVERT (DCA_3F, DCA_STEREO):
    case CONVERT (DCA_3F1R, DCA_2F1R):
    case CONVERT (DCA_3F2R, DCA_2F2R):
Gildas Bazin's avatar
Gildas Bazin committed
255 256 257 258
	coeff[0] = coeff[2] = coeff[3] = coeff[4] = level;
	coeff[1] = MUL_L (level, clev);
	return 7;

259
    case CONVERT (DCA_2F1R, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
260 261 262 263
	coeff[0] = coeff[1] = level;
	coeff[2] = level_3db;
	return 7;

264
    case CONVERT (DCA_2F1R, DCA_STEREO):
Gildas Bazin's avatar
Gildas Bazin committed
265 266 267 268
	coeff[0] = coeff[1] = level;
	coeff[2] = MUL_L (level_3db, slev);
	return 7;

269
    case CONVERT (DCA_3F1R, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
270 271 272 273
	coeff[0] = coeff[2] = level;
	coeff[1] = coeff[3] = level_3db;
	return 15;

274
    case CONVERT (DCA_3F1R, DCA_STEREO):
Gildas Bazin's avatar
Gildas Bazin committed
275 276 277 278 279
	coeff[0] = coeff[2] = level;
	coeff[1] = MUL_L (level, clev);
	coeff[3] = MUL_L (level_3db, slev);
	return 15;

280
    case CONVERT (DCA_2F2R, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
281 282 283 284
	coeff[0] = coeff[1] = level;
	coeff[2] = coeff[3] = level_3db;
	return 15;

285
    case CONVERT (DCA_2F2R, DCA_STEREO):
Gildas Bazin's avatar
Gildas Bazin committed
286 287 288 289
	coeff[0] = coeff[1] = level;
	coeff[2] = coeff[3] = MUL_L (level, slev);
	return 15;

290
    case CONVERT (DCA_3F2R, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
291 292 293 294
	coeff[0] = coeff[2] = level;
	coeff[1] = coeff[3] = coeff[4] = level_3db;
	return 31;

295
    case CONVERT (DCA_3F2R, DCA_2F1R):
Gildas Bazin's avatar
Gildas Bazin committed
296 297 298 299 300
	coeff[0] = coeff[2] = level;
	coeff[1] = MUL_L (level, clev);
	coeff[3] = coeff[4] = level_3db;
	return 31;

301
    case CONVERT (DCA_3F2R, DCA_STEREO):
Gildas Bazin's avatar
Gildas Bazin committed
302 303 304 305 306
	coeff[0] = coeff[2] = level;
	coeff[1] = MUL_L (level, clev);
	coeff[3] = coeff[4] = MUL_L (level, slev);
	return 31;

307
    case CONVERT (DCA_3F1R, DCA_3F):
Gildas Bazin's avatar
Gildas Bazin committed
308 309 310 311
	coeff[0] = coeff[1] = coeff[2] = level;
	coeff[3] = MUL_L (level_3db, slev);
	return 13;

312
    case CONVERT (DCA_3F2R, DCA_3F):
Gildas Bazin's avatar
Gildas Bazin committed
313 314 315 316
	coeff[0] = coeff[1] = coeff[2] = level;
	coeff[3] = coeff[4] = MUL_L (level, slev);
	return 29;

317
    case CONVERT (DCA_2F2R, DCA_2F1R):
Gildas Bazin's avatar
Gildas Bazin committed
318 319 320 321
	coeff[0] = coeff[1] = level;
	coeff[2] = coeff[3] = level_3db;
	return 12;

322
    case CONVERT (DCA_3F2R, DCA_3F1R):
Gildas Bazin's avatar
Gildas Bazin committed
323 324 325 326
	coeff[0] = coeff[1] = coeff[2] = level;
	coeff[3] = coeff[4] = level_3db;
	return 24;

327
    case CONVERT (DCA_2F1R, DCA_2F2R):
Gildas Bazin's avatar
Gildas Bazin committed
328 329 330 331
	coeff[0] = coeff[1] = level;
	coeff[2] = level_3db;
	return 0;

332
    case CONVERT (DCA_3F1R, DCA_2F2R):
Gildas Bazin's avatar
Gildas Bazin committed
333 334 335 336 337
	coeff[0] = coeff[2] = level;
	coeff[1] = MUL_L (level, clev);
	coeff[3] = level_3db;
	return 7;

338
    case CONVERT (DCA_3F1R, DCA_3F2R):
Gildas Bazin's avatar
Gildas Bazin committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
	coeff[0] = coeff[1] = coeff[2] = level;
	coeff[3] = level_3db;
	return 0;
    }

    return -1;	/* NOTREACHED */
}

static void mix2to1 (sample_t * dest, sample_t * src, sample_t bias)
{
    int i;

    for (i = 0; i < 256; i++)
	dest[i] += BIAS (src[i]);
}

static void mix3to1 (sample_t * samples, sample_t bias)
{
    int i;

    for (i = 0; i < 256; i++)
	samples[i] += BIAS (samples[i + 256] + samples[i + 512]);
}

static void mix4to1 (sample_t * samples, sample_t bias)
{
    int i;

    for (i = 0; i < 256; i++)
	samples[i] += BIAS (samples[i + 256] + samples[i + 512] +
			    samples[i + 768]);
}

static void mix5to1 (sample_t * samples, sample_t bias)
{
    int i;

    for (i = 0; i < 256; i++)
	samples[i] += BIAS (samples[i + 256] + samples[i + 512] +
			    samples[i + 768] + samples[i + 1024]);
}

static void mix3to2 (sample_t * samples, sample_t bias)
{
    int i;
    sample_t common;

    for (i = 0; i < 256; i++) {
	common = BIAS (samples[i]);
	samples[i] = samples[i + 256] + common;
	samples[i + 256] = samples[i + 512] + common;
    }
}

static void mix21to2 (sample_t * left, sample_t * right, sample_t bias)
{
    int i;
    sample_t common;

    for (i = 0; i < 256; i++) {
	common = BIAS (right[i + 256]);
	left[i] += common;
	right[i] += common;
    }
}

static void mix21toS (sample_t * samples, sample_t bias)
{
    int i;
    sample_t surround;

    for (i = 0; i < 256; i++) {
	surround = samples[i + 512];
	samples[i] += BIAS (-surround);
	samples[i + 256] += BIAS (surround);
    }
}

static void mix31to2 (sample_t * samples, sample_t bias)
{
    int i;
    sample_t common;

    for (i = 0; i < 256; i++) {
	common = BIAS (samples[i] + samples[i + 768]);
	samples[i] = samples[i + 256] + common;
	samples[i + 256] = samples[i + 512] + common;
    }
}

static void mix31toS (sample_t * samples, sample_t bias)
{
    int i;
    sample_t common, surround;

    for (i = 0; i < 256; i++) {
	common = BIAS (samples[i]);
	surround = samples[i + 768];
	samples[i] = samples[i + 256] + common - surround;
	samples[i + 256] = samples[i + 512] + common + surround;
    }
}

static void mix22toS (sample_t * samples, sample_t bias)
{
    int i;
    sample_t surround;

    for (i = 0; i < 256; i++) {
	surround = samples[i + 512] + samples[i + 768];
	samples[i] += BIAS (-surround);
	samples[i + 256] += BIAS (surround);
    }
}

static void mix32to2 (sample_t * samples, sample_t bias)
{
    int i;
    sample_t common;

    for (i = 0; i < 256; i++) {
	common = BIAS (samples[i]);
	samples[i] = common + samples[i + 256] + samples[i + 768];
	samples[i + 256] = common + samples[i + 512] + samples[i + 1024];
    }
}

static void mix32toS (sample_t * samples, sample_t bias)
{
    int i;
    sample_t common, surround;

    for (i = 0; i < 256; i++) {
	common = BIAS (samples[i]);
	surround = samples[i + 768] + samples[i + 1024];
	samples[i] = samples[i + 256] + common - surround;
	samples[i + 256] = samples[i + 512] + common + surround;
    }
}

static void move2to1 (sample_t * src, sample_t * dest, sample_t bias)
{
    int i;

    for (i = 0; i < 256; i++)
	dest[i] = BIAS (src[i] + src[i + 256]);
}

static void zero (sample_t * samples)
{
    int i;

    for (i = 0; i < 256; i++)
	samples[i] = 0;
}

495
void dca_downmix (sample_t * samples, int acmod, int output, sample_t bias,
Gildas Bazin's avatar
Gildas Bazin committed
496 497
		  level_t clev, level_t slev)
{
498 499
    (void)clev;

500
    switch (CONVERT (acmod, output & DCA_CHANNEL_MASK)) {
Gildas Bazin's avatar
Gildas Bazin committed
501

502 503
    case CONVERT (DCA_CHANNEL, DCA_MONO):
    case CONVERT (DCA_STEREO, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
504 505 506 507
    mix_2to1:
	mix2to1 (samples, samples + 256, bias);
	break;

508
    case CONVERT (DCA_2F1R, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
509 510
	if (slev == 0)
	    goto mix_2to1;
511
    case CONVERT (DCA_3F, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
512 513 514 515
    mix_3to1:
	mix3to1 (samples, bias);
	break;

516
    case CONVERT (DCA_3F1R, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
517 518
	if (slev == 0)
	    goto mix_3to1;
519
    case CONVERT (DCA_2F2R, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
520 521 522 523 524
	if (slev == 0)
	    goto mix_2to1;
	mix4to1 (samples, bias);
	break;

525
    case CONVERT (DCA_3F2R, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
526 527 528 529 530
	if (slev == 0)
	    goto mix_3to1;
	mix5to1 (samples, bias);
	break;

531
    case CONVERT (DCA_MONO, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
532 533 534
	memcpy (samples + 256, samples, 256 * sizeof (sample_t));
	break;

535 536
    case CONVERT (DCA_3F, DCA_STEREO):
    case CONVERT (DCA_3F, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
537 538 539 540
    mix_3to2:
	mix3to2 (samples, bias);
	break;

541
    case CONVERT (DCA_2F1R, DCA_STEREO):
Gildas Bazin's avatar
Gildas Bazin committed
542 543 544 545 546
	if (slev == 0)
	    break;
	mix21to2 (samples, samples + 256, bias);
	break;

547
    case CONVERT (DCA_2F1R, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
548 549 550
	mix21toS (samples, bias);
	break;

551
    case CONVERT (DCA_3F1R, DCA_STEREO):
Gildas Bazin's avatar
Gildas Bazin committed
552 553 554 555 556
	if (slev == 0)
	    goto mix_3to2;
	mix31to2 (samples, bias);
	break;

557
    case CONVERT (DCA_3F1R, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
558 559 560
	mix31toS (samples, bias);
	break;

561
    case CONVERT (DCA_2F2R, DCA_STEREO):
Gildas Bazin's avatar
Gildas Bazin committed
562 563 564 565 566 567
	if (slev == 0)
	    break;
	mix2to1 (samples, samples + 512, bias);
	mix2to1 (samples + 256, samples + 768, bias);
	break;

568
    case CONVERT (DCA_2F2R, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
569 570 571
	mix22toS (samples, bias);
	break;

572
    case CONVERT (DCA_3F2R, DCA_STEREO):
Gildas Bazin's avatar
Gildas Bazin committed
573 574 575 576 577
	if (slev == 0)
	    goto mix_3to2;
	mix32to2 (samples, bias);
	break;

578
    case CONVERT (DCA_3F2R, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
579 580 581
	mix32toS (samples, bias);
	break;

582
    case CONVERT (DCA_3F1R, DCA_3F):
Gildas Bazin's avatar
Gildas Bazin committed
583 584 585 586 587
	if (slev == 0)
	    break;
	mix21to2 (samples, samples + 512, bias);
	break;

588
    case CONVERT (DCA_3F2R, DCA_3F):
Gildas Bazin's avatar
Gildas Bazin committed
589 590 591 592 593 594
	if (slev == 0)
	    break;
	mix2to1 (samples, samples + 768, bias);
	mix2to1 (samples + 512, samples + 1024, bias);
	break;

595
    case CONVERT (DCA_3F1R, DCA_2F1R):
Gildas Bazin's avatar
Gildas Bazin committed
596 597 598 599
	mix3to2 (samples, bias);
	memcpy (samples + 512, samples + 768, 256 * sizeof (sample_t));
	break;

600
    case CONVERT (DCA_2F2R, DCA_2F1R):
Gildas Bazin's avatar
Gildas Bazin committed
601 602 603
	mix2to1 (samples + 512, samples + 768, bias);
	break;

604
    case CONVERT (DCA_3F2R, DCA_2F1R):
Gildas Bazin's avatar
Gildas Bazin committed
605 606 607 608
	mix3to2 (samples, bias);
	move2to1 (samples + 768, samples + 512, bias);
	break;

609
    case CONVERT (DCA_3F2R, DCA_3F1R):
Gildas Bazin's avatar
Gildas Bazin committed
610 611 612
	mix2to1 (samples + 768, samples + 1024, bias);
	break;

613
    case CONVERT (DCA_2F1R, DCA_2F2R):
Gildas Bazin's avatar
Gildas Bazin committed
614 615 616
	memcpy (samples + 768, samples + 512, 256 * sizeof (sample_t));
	break;

617
    case CONVERT (DCA_3F1R, DCA_2F2R):
Gildas Bazin's avatar
Gildas Bazin committed
618 619 620 621
	mix3to2 (samples, bias);
	memcpy (samples + 512, samples + 768, 256 * sizeof (sample_t));
	break;

622
    case CONVERT (DCA_3F2R, DCA_2F2R):
Gildas Bazin's avatar
Gildas Bazin committed
623 624 625 626 627
	mix3to2 (samples, bias);
	memcpy (samples + 512, samples + 768, 256 * sizeof (sample_t));
	memcpy (samples + 768, samples + 1024, 256 * sizeof (sample_t));
	break;

628
    case CONVERT (DCA_3F1R, DCA_3F2R):
Gildas Bazin's avatar
Gildas Bazin committed
629 630 631 632 633
	memcpy (samples + 1024, samples + 768, 256 * sizeof (sample_t));
	break;
    }
}

634
void dca_upmix (sample_t * samples, int acmod, int output)
Gildas Bazin's avatar
Gildas Bazin committed
635
{
636
    switch (CONVERT (acmod, output & DCA_CHANNEL_MASK)) {
Gildas Bazin's avatar
Gildas Bazin committed
637

638
    case CONVERT (DCA_3F2R, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
639
	zero (samples + 1024);
640 641
    case CONVERT (DCA_3F1R, DCA_MONO):
    case CONVERT (DCA_2F2R, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
642
	zero (samples + 768);
643 644
    case CONVERT (DCA_3F, DCA_MONO):
    case CONVERT (DCA_2F1R, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
645
	zero (samples + 512);
646 647
    case CONVERT (DCA_CHANNEL, DCA_MONO):
    case CONVERT (DCA_STEREO, DCA_MONO):
Gildas Bazin's avatar
Gildas Bazin committed
648 649 650
	zero (samples + 256);
	break;

651 652
    case CONVERT (DCA_3F2R, DCA_STEREO):
    case CONVERT (DCA_3F2R, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
653
	zero (samples + 1024);
654 655
    case CONVERT (DCA_3F1R, DCA_STEREO):
    case CONVERT (DCA_3F1R, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
656
	zero (samples + 768);
657 658
    case CONVERT (DCA_3F, DCA_STEREO):
    case CONVERT (DCA_3F, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
659 660 661 662 663
    mix_3to2:
	memcpy (samples + 512, samples + 256, 256 * sizeof (sample_t));
	zero (samples + 256);
	break;

664 665
    case CONVERT (DCA_2F2R, DCA_STEREO):
    case CONVERT (DCA_2F2R, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
666
	zero (samples + 768);
667 668
    case CONVERT (DCA_2F1R, DCA_STEREO):
    case CONVERT (DCA_2F1R, DCA_DOLBY):
Gildas Bazin's avatar
Gildas Bazin committed
669 670 671
	zero (samples + 512);
	break;

672
    case CONVERT (DCA_3F2R, DCA_3F):
Gildas Bazin's avatar
Gildas Bazin committed
673
	zero (samples + 1024);
674 675
    case CONVERT (DCA_3F1R, DCA_3F):
    case CONVERT (DCA_2F2R, DCA_2F1R):
Gildas Bazin's avatar
Gildas Bazin committed
676 677 678
	zero (samples + 768);
	break;

679
    case CONVERT (DCA_3F2R, DCA_3F1R):
Gildas Bazin's avatar
Gildas Bazin committed
680 681 682
	zero (samples + 1024);
	break;

683
    case CONVERT (DCA_3F2R, DCA_2F1R):
Gildas Bazin's avatar
Gildas Bazin committed
684
	zero (samples + 1024);
685
    case CONVERT (DCA_3F1R, DCA_2F1R):
Gildas Bazin's avatar
Gildas Bazin committed
686 687 688 689
    mix_31to21:
	memcpy (samples + 768, samples + 512, 256 * sizeof (sample_t));
	goto mix_3to2;

690
    case CONVERT (DCA_3F2R, DCA_2F2R):
Gildas Bazin's avatar
Gildas Bazin committed
691 692 693 694
	memcpy (samples + 1024, samples + 768, 256 * sizeof (sample_t));
	goto mix_31to21;
    }
}