1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
|
/* Spa
*
* Copyright © 2018 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <spa/param/audio/format-utils.h>
#include <spa/support/cpu.h>
#include <spa/support/log.h>
#include <spa/utils/defs.h>
#include <spa/debug/types.h>
#include "channelmix-ops.h"
#include "hilbert.h"
#define ANY ((uint32_t)-1)
#define EQ ((uint32_t)-2)
typedef void (*channelmix_func_t) (struct channelmix *mix, void * SPA_RESTRICT dst[],
const void * SPA_RESTRICT src[], uint32_t n_samples);
#define MAKE(sc,sm,dc,dm,func,...) \
{ sc, sm, dc, dm, func, #func, __VA_ARGS__ }
static const struct channelmix_info {
uint32_t src_chan;
uint64_t src_mask;
uint32_t dst_chan;
uint64_t dst_mask;
channelmix_func_t process;
const char *name;
uint32_t cpu_flags;
} channelmix_table[] =
{
#if defined (HAVE_SSE)
MAKE(2, MASK_MONO, 2, MASK_MONO, channelmix_copy_sse, SPA_CPU_FLAG_SSE),
MAKE(2, MASK_STEREO, 2, MASK_STEREO, channelmix_copy_sse, SPA_CPU_FLAG_SSE),
MAKE(EQ, 0, EQ, 0, channelmix_copy_sse, SPA_CPU_FLAG_SSE),
#endif
MAKE(2, MASK_MONO, 2, MASK_MONO, channelmix_copy_c),
MAKE(2, MASK_STEREO, 2, MASK_STEREO, channelmix_copy_c),
MAKE(EQ, 0, EQ, 0, channelmix_copy_c),
MAKE(1, MASK_MONO, 2, MASK_STEREO, channelmix_f32_1_2_c),
MAKE(2, MASK_STEREO, 1, MASK_MONO, channelmix_f32_2_1_c),
MAKE(4, MASK_QUAD, 1, MASK_MONO, channelmix_f32_4_1_c),
MAKE(4, MASK_3_1, 1, MASK_MONO, channelmix_f32_4_1_c),
MAKE(2, MASK_STEREO, 4, MASK_QUAD, channelmix_f32_2_4_c),
#if defined (HAVE_SSE)
MAKE(2, MASK_STEREO, 4, MASK_3_1, channelmix_f32_2_3p1_sse, SPA_CPU_FLAG_SSE),
#endif
MAKE(2, MASK_STEREO, 4, MASK_3_1, channelmix_f32_2_3p1_c),
#if defined (HAVE_SSE)
MAKE(2, MASK_STEREO, 6, MASK_5_1, channelmix_f32_2_5p1_sse, SPA_CPU_FLAG_SSE),
#endif
MAKE(2, MASK_STEREO, 6, MASK_5_1, channelmix_f32_2_5p1_c),
#if defined (HAVE_SSE)
MAKE(2, MASK_STEREO, 8, MASK_7_1, channelmix_f32_2_7p1_sse, SPA_CPU_FLAG_SSE),
#endif
MAKE(2, MASK_STEREO, 8, MASK_7_1, channelmix_f32_2_7p1_c),
#if defined (HAVE_SSE)
MAKE(4, MASK_3_1, 2, MASK_STEREO, channelmix_f32_3p1_2_sse, SPA_CPU_FLAG_SSE),
#endif
MAKE(4, MASK_3_1, 2, MASK_STEREO, channelmix_f32_3p1_2_c),
#if defined (HAVE_SSE)
MAKE(6, MASK_5_1, 2, MASK_STEREO, channelmix_f32_5p1_2_sse, SPA_CPU_FLAG_SSE),
#endif
MAKE(6, MASK_5_1, 2, MASK_STEREO, channelmix_f32_5p1_2_c),
#if defined (HAVE_SSE)
MAKE(6, MASK_5_1, 4, MASK_QUAD, channelmix_f32_5p1_4_sse, SPA_CPU_FLAG_SSE),
#endif
MAKE(6, MASK_5_1, 4, MASK_QUAD, channelmix_f32_5p1_4_c),
#if defined (HAVE_SSE)
MAKE(6, MASK_5_1, 4, MASK_3_1, channelmix_f32_5p1_3p1_sse, SPA_CPU_FLAG_SSE),
#endif
MAKE(6, MASK_5_1, 4, MASK_3_1, channelmix_f32_5p1_3p1_c),
MAKE(8, MASK_7_1, 2, MASK_STEREO, channelmix_f32_7p1_2_c),
MAKE(8, MASK_7_1, 4, MASK_QUAD, channelmix_f32_7p1_4_c),
MAKE(8, MASK_7_1, 4, MASK_3_1, channelmix_f32_7p1_3p1_c),
#if defined (HAVE_SSE)
MAKE(ANY, 0, ANY, 0, channelmix_f32_n_m_sse, SPA_CPU_FLAG_SSE),
#endif
MAKE(ANY, 0, ANY, 0, channelmix_f32_n_m_c),
};
#undef MAKE
#define MATCH_CHAN(a,b) ((a) == ANY || (a) == (b))
#define MATCH_CPU_FLAGS(a,b) ((a) == 0 || ((a) & (b)) == a)
#define MATCH_MASK(a,b) ((a) == 0 || ((a) & (b)) == (b))
static const struct channelmix_info *find_channelmix_info(uint32_t src_chan, uint64_t src_mask,
uint32_t dst_chan, uint64_t dst_mask, uint32_t cpu_flags)
{
SPA_FOR_EACH_ELEMENT_VAR(channelmix_table, info) {
if (!MATCH_CPU_FLAGS(info->cpu_flags, cpu_flags))
continue;
if (src_chan == dst_chan && src_mask == dst_mask)
return info;
if (MATCH_CHAN(info->src_chan, src_chan) &&
MATCH_CHAN(info->dst_chan, dst_chan) &&
MATCH_MASK(info->src_mask, src_mask) &&
MATCH_MASK(info->dst_mask, dst_mask))
return info;
}
return NULL;
}
#define SQRT3_2 1.224744871f /* sqrt(3/2) */
#define SQRT1_2 0.707106781f
#define SQRT2 1.414213562f
#define MATRIX_NORMAL 0
#define MATRIX_DOLBY 1
#define MATRIX_DPLII 2
#define _CH(ch) ((SPA_AUDIO_CHANNEL_ ## ch)-3)
#define _MASK(ch) (1ULL << _CH(ch))
#define FRONT (_MASK(FC))
#define STEREO (_MASK(FL)|_MASK(FR))
#define REAR (_MASK(RL)|_MASK(RR))
#define SIDE (_MASK(SL)|_MASK(SR))
static int make_matrix(struct channelmix *mix)
{
float matrix[SPA_AUDIO_MAX_CHANNELS][SPA_AUDIO_MAX_CHANNELS] = {{ 0.0f }};
uint64_t src_mask = mix->src_mask;
uint64_t dst_mask = mix->dst_mask;
uint32_t src_chan = mix->src_chan;
uint32_t dst_chan = mix->dst_chan;
uint64_t unassigned, keep;
uint32_t i, j, ic, jc, matrix_encoding = MATRIX_NORMAL;
float clev = SQRT1_2;
float slev = SQRT1_2;
float llev = 0.5f;
float maxsum = 0.0f;
bool filter_fc = false, filter_lfe = false;
#define _MATRIX(s,d) matrix[_CH(s)][_CH(d)]
spa_log_debug(mix->log, "src-mask:%08"PRIx64" dst-mask:%08"PRIx64
" options:%08x", src_mask, dst_mask, mix->options);
/* move the MONO mask to FRONT so that the lower bits can be shifted
* away. */
if ((src_mask & (1Ull << SPA_AUDIO_CHANNEL_MONO)) != 0) {
if (src_chan == 1)
src_mask = 0;
else
src_mask |= (1ULL << SPA_AUDIO_CHANNEL_FC);
}
if ((dst_mask & (1Ull << SPA_AUDIO_CHANNEL_MONO)) != 0)
dst_mask |= (1ULL << SPA_AUDIO_CHANNEL_FC);
/* shift so that bit 0 is FL */
src_mask >>= 3;
dst_mask >>= 3;
/* unknown channels or just 1 channel */
if (src_mask == 0 || dst_mask == 0) {
if (src_chan == 1) {
/* one FC/MONO src goes everywhere */
spa_log_debug(mix->log, "distribute FC/MONO (%f)", 1.0f);
for (i = 0; i < SPA_AUDIO_MAX_CHANNELS; i++)
matrix[i][0]= 1.0f;
} else if (dst_chan == 1) {
/* one FC/MONO dst get average of everything */
spa_log_debug(mix->log, "average FC/MONO (%f)", 1.0f / src_chan);
for (i = 0; i < SPA_AUDIO_MAX_CHANNELS; i++)
matrix[0][i]= 1.0f / src_chan;
} else {
/* just pair channels */
spa_log_debug(mix->log, "pairing channels (%f)", 1.0f);
for (i = 0; i < SPA_AUDIO_MAX_CHANNELS; i++)
matrix[i][i]= 1.0f;
}
if (dst_mask & FRONT)
filter_fc = true;
if (dst_mask & _MASK(LFE))
filter_lfe = true;
src_mask = dst_mask = ~0LU;
goto done;
} else {
spa_log_debug(mix->log, "matching channels");
for (i = 0; i < SPA_AUDIO_MAX_CHANNELS; i++) {
if ((src_mask & dst_mask & (1ULL << i))) {
spa_log_debug(mix->log, "matched channel %u (%f)", i, 1.0f);
matrix[i][i]= 1.0f;
}
}
}
unassigned = src_mask & ~dst_mask;
keep = dst_mask & ~src_mask;
if (!SPA_FLAG_IS_SET(mix->options, CHANNELMIX_OPTION_UPMIX)) {
keep = 0;
} else {
if (mix->upmix == CHANNELMIX_UPMIX_NONE)
keep = 0;
keep |= FRONT;
if (mix->lfe_cutoff > 0.0f)
keep |= _MASK(LFE);
else
keep &= ~_MASK(LFE);
}
spa_log_debug(mix->log, "unassigned downmix %08" PRIx64 " %08" PRIx64, unassigned, keep);
if (unassigned & FRONT){
if ((dst_mask & STEREO) == STEREO){
if(src_mask & STEREO) {
spa_log_debug(mix->log, "assign FC to STEREO (%f)", clev);
_MATRIX(FL,FC) += clev;
_MATRIX(FR,FC) += clev;
} else {
spa_log_debug(mix->log, "assign FC to STEREO (%f)", SQRT1_2);
_MATRIX(FL,FC) += SQRT1_2;
_MATRIX(FR,FC) += SQRT1_2;
}
} else {
spa_log_warn(mix->log, "can't assign FC");
}
}
if (unassigned & STEREO){
if (dst_mask & FRONT) {
spa_log_debug(mix->log, "assign STEREO to FC (%f)", SQRT1_2);
_MATRIX(FC,FL) += SQRT1_2;
_MATRIX(FC,FR) += SQRT1_2;
if (src_mask & FRONT) {
spa_log_debug(mix->log, "assign FC to FC (%f)", clev * SQRT2);
_MATRIX(FC,FC) = clev * SQRT2;
}
keep &= ~FRONT;
} else {
spa_log_warn(mix->log, "can't assign STEREO");
}
}
if (unassigned & _MASK(RC)) {
if (dst_mask & REAR){
spa_log_debug(mix->log, "assign RC to RL+RR (%f)", SQRT1_2);
_MATRIX(RL,RC) += SQRT1_2;
_MATRIX(RR,RC) += SQRT1_2;
} else if (dst_mask & SIDE) {
spa_log_debug(mix->log, "assign RC to SL+SR (%f)", SQRT1_2);
_MATRIX(SL,RC) += SQRT1_2;
_MATRIX(SR,RC) += SQRT1_2;
} else if(dst_mask & STEREO) {
spa_log_debug(mix->log, "assign RC to FL+FR");
if (matrix_encoding == MATRIX_DOLBY ||
matrix_encoding == MATRIX_DPLII) {
if (unassigned & (_MASK(RL)|_MASK(RR))) {
_MATRIX(FL,RC) -= slev * SQRT1_2;
_MATRIX(FR,RC) += slev * SQRT1_2;
} else {
_MATRIX(FL,RC) -= slev;
_MATRIX(FR,RC) += slev;
}
} else {
_MATRIX(FL,RC) += slev * SQRT1_2;
_MATRIX(FR,RC) += slev * SQRT1_2;
}
} else if (dst_mask & FRONT) {
spa_log_debug(mix->log, "assign RC to FC (%f)", slev * SQRT1_2);
_MATRIX(FC,RC) += slev * SQRT1_2;
} else {
spa_log_warn(mix->log, "can't assign RC");
}
}
if (unassigned & REAR) {
if (dst_mask & _MASK(RC)) {
spa_log_debug(mix->log, "assign RL+RR to RC");
_MATRIX(RC,RL) += SQRT1_2;
_MATRIX(RC,RR) += SQRT1_2;
} else if (dst_mask & SIDE) {
spa_log_debug(mix->log, "assign RL+RR to SL+SR");
if (src_mask & SIDE) {
_MATRIX(SL,RL) += SQRT1_2;
_MATRIX(SR,RR) += SQRT1_2;
} else {
_MATRIX(SL,RL) += 1.0f;
_MATRIX(SR,RR) += 1.0f;
}
keep &= ~SIDE;
} else if (dst_mask & STEREO) {
spa_log_debug(mix->log, "assign RL+RR to FL+FR (%f)", slev);
if (matrix_encoding == MATRIX_DOLBY) {
_MATRIX(FL,RL) -= slev * SQRT1_2;
_MATRIX(FL,RR) -= slev * SQRT1_2;
_MATRIX(FR,RL) += slev * SQRT1_2;
_MATRIX(FR,RR) += slev * SQRT1_2;
} else if (matrix_encoding == MATRIX_DPLII) {
_MATRIX(FL,RL) -= slev * SQRT3_2;
_MATRIX(FL,RR) -= slev * SQRT1_2;
_MATRIX(FR,RL) += slev * SQRT1_2;
_MATRIX(FR,RR) += slev * SQRT3_2;
} else {
_MATRIX(FL,RL) += slev;
_MATRIX(FR,RR) += slev;
}
} else if (dst_mask & FRONT) {
spa_log_debug(mix->log, "assign RL+RR to FC (%f)",
slev * SQRT1_2);
_MATRIX(FC,RL)+= slev * SQRT1_2;
_MATRIX(FC,RR)+= slev * SQRT1_2;
} else {
spa_log_warn(mix->log, "can't assign RL");
}
}
if (unassigned & SIDE) {
if (dst_mask & REAR) {
if (src_mask & _MASK(RL)) {
spa_log_debug(mix->log, "assign SL+SR to RL+RR (%f)", SQRT1_2);
_MATRIX(RL,SL) += SQRT1_2;
_MATRIX(RR,SR) += SQRT1_2;
} else {
spa_log_debug(mix->log, "assign SL+SR to RL+RR (%f)", 1.0f);
_MATRIX(RL,SL) += 1.0f;
_MATRIX(RR,SR) += 1.0f;
}
keep &= ~REAR;
} else if (dst_mask & _MASK(RC)) {
spa_log_debug(mix->log, "assign SL+SR to RC (%f)", SQRT1_2);
_MATRIX(RC,SL)+= SQRT1_2;
_MATRIX(RC,SR)+= SQRT1_2;
} else if (dst_mask & STEREO) {
if (matrix_encoding == MATRIX_DOLBY) {
spa_log_debug(mix->log, "assign SL+SR to FL+FR (%f)",
slev * SQRT1_2);
_MATRIX(FL,SL) -= slev * SQRT1_2;
_MATRIX(FL,SR) -= slev * SQRT1_2;
_MATRIX(FR,SL) += slev * SQRT1_2;
_MATRIX(FR,SR) += slev * SQRT1_2;
} else if (matrix_encoding == MATRIX_DPLII) {
spa_log_debug(mix->log, "assign SL+SR to FL+FR (%f / %f)",
slev * SQRT3_2, slev * SQRT1_2);
_MATRIX(FL,SL) -= slev * SQRT3_2;
_MATRIX(FL,SR) -= slev * SQRT1_2;
_MATRIX(FR,SL) += slev * SQRT1_2;
_MATRIX(FR,SR) += slev * SQRT3_2;
} else {
spa_log_debug(mix->log, "assign SL+SR to FL+FR (%f)", slev);
_MATRIX(FL,SL) += slev;
_MATRIX(FR,SR) += slev;
}
} else if (dst_mask & FRONT) {
spa_log_debug(mix->log, "assign SL+SR to FC (%f)", slev * SQRT1_2);
_MATRIX(FC,SL) += slev * SQRT1_2;
_MATRIX(FC,SR) += slev * SQRT1_2;
} else {
spa_log_warn(mix->log, "can't assign SL");
}
}
if (unassigned & _MASK(FLC)) {
if (dst_mask & STEREO) {
spa_log_debug(mix->log, "assign FLC+FRC to FL+FR (%f)", 1.0f);
_MATRIX(FL,FLC)+= 1.0f;
_MATRIX(FR,FRC)+= 1.0f;
} else if(dst_mask & FRONT) {
spa_log_debug(mix->log, "assign FLC+FRC to FC (%f)", SQRT1_2);
_MATRIX(FC,FLC)+= SQRT1_2;
_MATRIX(FC,FRC)+= SQRT1_2;
} else {
spa_log_warn(mix->log, "can't assign FLC");
}
}
if (unassigned & _MASK(LFE) &&
SPA_FLAG_IS_SET(mix->options, CHANNELMIX_OPTION_MIX_LFE)) {
if (dst_mask & FRONT) {
spa_log_debug(mix->log, "assign LFE to FC (%f)", llev);
_MATRIX(FC,LFE) += llev;
} else if (dst_mask & STEREO) {
spa_log_debug(mix->log, "assign LFE to FL+FR (%f)",
llev * SQRT1_2);
_MATRIX(FL,LFE) += llev * SQRT1_2;
_MATRIX(FR,LFE) += llev * SQRT1_2;
} else {
spa_log_warn(mix->log, "can't assign LFE");
}
}
unassigned = dst_mask & ~src_mask & keep;
spa_log_debug(mix->log, "unassigned upmix %08"PRIx64" lfe:%f",
unassigned, mix->lfe_cutoff);
if (unassigned & STEREO) {
if ((src_mask & FRONT) == FRONT) {
spa_log_debug(mix->log, "produce STEREO from FC (%f)", clev);
_MATRIX(FL,FC) += clev;
_MATRIX(FR,FC) += clev;
} else {
spa_log_warn(mix->log, "can't produce STEREO");
}
}
if (unassigned & FRONT) {
if ((src_mask & STEREO) == STEREO) {
spa_log_debug(mix->log, "produce FC from STEREO (%f)", clev);
_MATRIX(FC,FL) += clev;
_MATRIX(FC,FR) += clev;
filter_fc = true;
} else {
spa_log_warn(mix->log, "can't produce FC");
}
}
if (unassigned & _MASK(LFE)) {
if ((src_mask & STEREO) == STEREO) {
spa_log_debug(mix->log, "produce LFE from STEREO (%f)", llev);
_MATRIX(LFE,FL) += llev;
_MATRIX(LFE,FR) += llev;
filter_lfe = true;
} else if ((src_mask & FRONT) == FRONT) {
spa_log_debug(mix->log, "produce LFE from FC (%f)", llev);
_MATRIX(LFE,FC) += llev;
filter_lfe = true;
} else {
spa_log_warn(mix->log, "can't produce LFE");
}
}
if (unassigned & SIDE) {
if ((src_mask & REAR) == REAR) {
spa_log_debug(mix->log, "produce SIDE from REAR (%f)", 1.0f);
_MATRIX(SL,RL) += 1.0f;
_MATRIX(SR,RR) += 1.0f;
} else if ((src_mask & STEREO) == STEREO) {
spa_log_debug(mix->log, "produce SIDE from STEREO (%f)", slev);
_MATRIX(SL,FL) += slev;
_MATRIX(SR,FR) += slev;
} else if ((src_mask & FRONT) == FRONT &&
mix->upmix == CHANNELMIX_UPMIX_SIMPLE) {
spa_log_debug(mix->log, "produce SIDE from FC (%f)", clev);
_MATRIX(SL,FC) += clev;
_MATRIX(SR,FC) += clev;
} else {
spa_log_debug(mix->log, "won't produce SIDE");
}
}
if (unassigned & REAR) {
if ((src_mask & SIDE) == SIDE) {
spa_log_debug(mix->log, "produce REAR from SIDE (%f)", 1.0f);
_MATRIX(RL,SL) += 1.0f;
_MATRIX(RR,SR) += 1.0f;
} else if ((src_mask & STEREO) == STEREO) {
spa_log_debug(mix->log, "produce REAR from STEREO (%f)", slev);
_MATRIX(RL,FL) += slev;
_MATRIX(RR,FR) += slev;
} else if ((src_mask & FRONT) == FRONT &&
mix->upmix == CHANNELMIX_UPMIX_SIMPLE) {
spa_log_debug(mix->log, "produce REAR from FC (%f)", clev);
_MATRIX(RL,FC) += clev;
_MATRIX(RR,FC) += clev;
} else {
spa_log_debug(mix->log, "won't produce SIDE");
}
}
if (unassigned & _MASK(RC)) {
if ((src_mask & REAR) == REAR) {
spa_log_debug(mix->log, "produce RC from REAR (%f)", 0.5f);
_MATRIX(RC,RL) += 0.5f;
_MATRIX(RC,RR) += 0.5f;
} else if ((src_mask & SIDE) == SIDE) {
spa_log_debug(mix->log, "produce RC from SIDE (%f)", 0.5f);
_MATRIX(RC,SL) += 0.5f;
_MATRIX(RC,SR) += 0.5f;
} else if ((src_mask & STEREO) == STEREO) {
spa_log_debug(mix->log, "produce RC from STEREO (%f)", 0.5f);
_MATRIX(RC,FL) += 0.5f;
_MATRIX(RC,FR) += 0.5f;
} else if ((src_mask & FRONT) == FRONT &&
mix->upmix == CHANNELMIX_UPMIX_SIMPLE) {
spa_log_debug(mix->log, "produce RC from FC (%f)", slev);
_MATRIX(RC,FC) += slev;
} else {
spa_log_debug(mix->log, "won't produce RC");
}
}
done:
for (jc = 0, ic = 0, i = 0; i < SPA_AUDIO_MAX_CHANNELS; i++) {
float sum = 0.0f;
char str[1024], str2[1024];
int idx = 0, idx2 = 0;
if ((dst_mask & (1UL << i)) == 0)
continue;
for (jc = 0, j = 0; j < SPA_AUDIO_MAX_CHANNELS; j++) {
if ((src_mask & (1UL << j)) == 0)
continue;
if (ic >= dst_chan || jc >= src_chan)
continue;
if (ic == 0)
idx2 += snprintf(str2 + idx2, sizeof(str2) - idx2, "%-4.4s ",
src_mask == ~0LU ? "MONO" :
spa_debug_type_find_short_name(spa_type_audio_channel, j + 3));
mix->matrix_orig[ic][jc++] = matrix[i][j];
sum += fabs(matrix[i][j]);
if (matrix[i][j] == 0.0f)
idx += snprintf(str + idx, sizeof(str) - idx, " ");
else
idx += snprintf(str + idx, sizeof(str) - idx, "%1.3f ", matrix[i][j]);
}
if (idx2 > 0)
spa_log_info(mix->log, " %s", str2);
if (idx > 0) {
spa_log_info(mix->log, "%-4.4s %s %f",
dst_mask == ~0LU ? "MONO" :
spa_debug_type_find_short_name(spa_type_audio_channel, i + 3),
str, sum);
}
maxsum = SPA_MAX(maxsum, sum);
if (i == _CH(LFE) && mix->lfe_cutoff > 0.0f && filter_lfe) {
spa_log_info(mix->log, "channel %d is LFE cutoff:%f", ic, mix->lfe_cutoff);
lr4_set(&mix->lr4[ic], BQ_LOWPASS, mix->lfe_cutoff / mix->freq);
} else if (i == _CH(FC) && mix->fc_cutoff > 0.0f && filter_fc) {
spa_log_info(mix->log, "channel %d is FC cutoff:%f", ic, mix->fc_cutoff);
lr4_set(&mix->lr4[ic], BQ_LOWPASS, mix->fc_cutoff / mix->freq);
} else {
mix->lr4[ic].active = false;
}
ic++;
}
if (SPA_FLAG_IS_SET(mix->options, CHANNELMIX_OPTION_NORMALIZE) &&
maxsum > 1.0f) {
spa_log_debug(mix->log, "normalize %f", maxsum);
for (i = 0; i < dst_chan; i++)
for (j = 0; j < src_chan; j++)
mix->matrix_orig[i][j] /= maxsum;
}
return 0;
}
static void impl_channelmix_set_volume(struct channelmix *mix, float volume, bool mute,
uint32_t n_channel_volumes, float *channel_volumes)
{
float volumes[SPA_AUDIO_MAX_CHANNELS];
float vol = mute ? 0.0f : volume, t;
uint32_t i, j;
uint32_t src_chan = mix->src_chan;
uint32_t dst_chan = mix->dst_chan;
spa_log_debug(mix->log, "volume:%f mute:%d n_volumes:%d", volume, mute, n_channel_volumes);
/** apply global volume to channels */
for (i = 0; i < n_channel_volumes; i++) {
volumes[i] = channel_volumes[i] * vol;
spa_log_debug(mix->log, "%d: %f * %f = %f", i, channel_volumes[i], vol, volumes[i]);
}
/** apply volumes per channel */
if (n_channel_volumes == src_chan) {
for (i = 0; i < dst_chan; i++) {
for (j = 0; j < src_chan; j++) {
mix->matrix[i][j] = mix->matrix_orig[i][j] * volumes[j];
}
}
} else if (n_channel_volumes == dst_chan) {
for (i = 0; i < dst_chan; i++) {
for (j = 0; j < src_chan; j++) {
mix->matrix[i][j] = mix->matrix_orig[i][j] * volumes[i];
}
}
} else if (n_channel_volumes == 0) {
for (i = 0; i < dst_chan; i++) {
for (j = 0; j < src_chan; j++) {
mix->matrix[i][j] = mix->matrix_orig[i][j] * vol;
}
}
}
SPA_FLAG_SET(mix->flags, CHANNELMIX_FLAG_ZERO);
SPA_FLAG_SET(mix->flags, CHANNELMIX_FLAG_EQUAL);
SPA_FLAG_SET(mix->flags, CHANNELMIX_FLAG_COPY);
t = 0.0;
for (i = 0; i < dst_chan; i++) {
for (j = 0; j < src_chan; j++) {
float v = mix->matrix[i][j];
spa_log_debug(mix->log, "%d %d: %f", i, j, v);
if (i == 0 && j == 0)
t = v;
else if (t != v)
SPA_FLAG_CLEAR(mix->flags, CHANNELMIX_FLAG_EQUAL);
if (v != 0.0)
SPA_FLAG_CLEAR(mix->flags, CHANNELMIX_FLAG_ZERO);
if ((i == j && v != 1.0f) ||
(i != j && v != 0.0f))
SPA_FLAG_CLEAR(mix->flags, CHANNELMIX_FLAG_COPY);
}
}
SPA_FLAG_UPDATE(mix->flags, CHANNELMIX_FLAG_IDENTITY,
dst_chan == src_chan && SPA_FLAG_IS_SET(mix->flags, CHANNELMIX_FLAG_COPY));
spa_log_debug(mix->log, "flags:%08x", mix->flags);
}
static void impl_channelmix_free(struct channelmix *mix)
{
mix->process = NULL;
}
int channelmix_init(struct channelmix *mix)
{
const struct channelmix_info *info;
if (mix->src_chan > SPA_AUDIO_MAX_CHANNELS ||
mix->dst_chan > SPA_AUDIO_MAX_CHANNELS)
return -EINVAL;
info = find_channelmix_info(mix->src_chan, mix->src_mask, mix->dst_chan, mix->dst_mask,
mix->cpu_flags);
if (info == NULL)
return -ENOTSUP;
mix->free = impl_channelmix_free;
mix->process = info->process;
mix->set_volume = impl_channelmix_set_volume;
mix->cpu_flags = info->cpu_flags;
mix->delay = mix->rear_delay * mix->freq / 1000.0f;
mix->func_name = info->name;
spa_log_debug(mix->log, "selected %s delay:%d options:%08x", info->name, mix->delay,
mix->options);
if (mix->hilbert_taps > 0) {
mix->n_taps = SPA_CLAMP(mix->hilbert_taps, 15u, 255u) | 1;
blackman_window(mix->taps, mix->n_taps);
hilbert_generate(mix->taps, mix->n_taps);
} else {
mix->n_taps = 1;
mix->taps[0] = 1.0f;
}
return make_matrix(mix);
}
|