1 /* Spa
2  *
3  * Copyright © 2018 Wim Taymans
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <string.h>
26 #include <stdio.h>
27 
28 #include <spa/utils/defs.h>
29 #include <spa/param/audio/raw.h>
30 
31 #undef SPA_LOG_TOPIC_DEFAULT
32 #define SPA_LOG_TOPIC_DEFAULT log_topic
33 extern struct spa_log_topic *log_topic;
34 
35 #include "crossover.h"
36 
37 #define VOLUME_MIN 0.0f
38 #define VOLUME_NORM 1.0f
39 
40 #define _M(ch)		(1UL << SPA_AUDIO_CHANNEL_ ## ch)
41 #define MASK_MONO	_M(FC)|_M(MONO)|_M(UNKNOWN)
42 #define MASK_STEREO	_M(FL)|_M(FR)|_M(UNKNOWN)
43 #define MASK_QUAD	_M(FL)|_M(FR)|_M(RL)|_M(RR)|_M(UNKNOWN)
44 #define MASK_3_1	_M(FL)|_M(FR)|_M(FC)|_M(LFE)
45 #define MASK_5_1	_M(FL)|_M(FR)|_M(FC)|_M(LFE)|_M(SL)|_M(SR)|_M(RL)|_M(RR)
46 #define MASK_7_1	_M(FL)|_M(FR)|_M(FC)|_M(LFE)|_M(SL)|_M(SR)|_M(RL)|_M(RR)
47 
48 
49 struct channelmix {
50 	uint32_t src_chan;
51 	uint32_t dst_chan;
52 	uint64_t src_mask;
53 	uint64_t dst_mask;
54 	uint32_t cpu_flags;
55 #define CHANNELMIX_OPTION_MIX_LFE	(1<<0)		/**< mix LFE */
56 #define CHANNELMIX_OPTION_NORMALIZE	(1<<1)		/**< normalize volumes */
57 #define CHANNELMIX_OPTION_UPMIX		(1<<2)		/**< do simple upmixing */
58 	uint32_t options;
59 
60 	struct spa_log *log;
61 
62 #define CHANNELMIX_FLAG_ZERO		(1<<0)		/**< all zero components */
63 #define CHANNELMIX_FLAG_IDENTITY	(1<<1)		/**< identity matrix */
64 #define CHANNELMIX_FLAG_EQUAL		(1<<2)		/**< all values are equal */
65 #define CHANNELMIX_FLAG_COPY		(1<<3)		/**< 1 on diagonal, can be nxm */
66 	uint32_t flags;
67 	float matrix_orig[SPA_AUDIO_MAX_CHANNELS][SPA_AUDIO_MAX_CHANNELS];
68 	float matrix[SPA_AUDIO_MAX_CHANNELS][SPA_AUDIO_MAX_CHANNELS];
69 
70 	float freq;					/* sample frequency */
71 	float lfe_cutoff;				/* in Hz, 0 is disabled */
72 	uint32_t lr4_info[SPA_AUDIO_MAX_CHANNELS];
73 	struct lr4 lr4[SPA_AUDIO_MAX_CHANNELS];
74 
75 	void (*process) (struct channelmix *mix, uint32_t n_dst, void * SPA_RESTRICT dst[n_dst],
76 			uint32_t n_src, const void * SPA_RESTRICT src[n_src], uint32_t n_samples);
77 	void (*set_volume) (struct channelmix *mix, float volume, bool mute,
78 			uint32_t n_channel_volumes, float *channel_volumes);
79 	void (*free) (struct channelmix *mix);
80 
81 	void *data;
82 };
83 
84 int channelmix_init(struct channelmix *mix);
85 
86 #define channelmix_process(mix,...)	(mix)->process(mix, __VA_ARGS__)
87 #define channelmix_set_volume(mix,...)	(mix)->set_volume(mix, __VA_ARGS__)
88 #define channelmix_free(mix)		(mix)->free(mix)
89 
90 #define DEFINE_FUNCTION(name,arch)					\
91 void channelmix_##name##_##arch(struct channelmix *mix,			\
92 		uint32_t n_dst, void * SPA_RESTRICT dst[n_dst],		\
93 		uint32_t n_src, const void * SPA_RESTRICT src[n_src],	\
94 		uint32_t n_samples);
95 
96 DEFINE_FUNCTION(copy, c);
97 DEFINE_FUNCTION(f32_n_m, c);
98 DEFINE_FUNCTION(f32_1_2, c);
99 DEFINE_FUNCTION(f32_2_1, c);
100 DEFINE_FUNCTION(f32_4_1, c);
101 DEFINE_FUNCTION(f32_3p1_1, c);
102 DEFINE_FUNCTION(f32_2_4, c);
103 DEFINE_FUNCTION(f32_2_3p1, c);
104 DEFINE_FUNCTION(f32_2_5p1, c);
105 DEFINE_FUNCTION(f32_5p1_2, c);
106 DEFINE_FUNCTION(f32_5p1_3p1, c);
107 DEFINE_FUNCTION(f32_5p1_4, c);
108 DEFINE_FUNCTION(f32_7p1_2, c);
109 DEFINE_FUNCTION(f32_7p1_3p1, c);
110 DEFINE_FUNCTION(f32_7p1_4, c);
111 
112 #if defined (HAVE_SSE)
113 DEFINE_FUNCTION(copy, sse);
114 DEFINE_FUNCTION(f32_2_4, sse);
115 DEFINE_FUNCTION(f32_5p1_2, sse);
116 DEFINE_FUNCTION(f32_5p1_3p1, sse);
117 DEFINE_FUNCTION(f32_5p1_4, sse);
118 DEFINE_FUNCTION(f32_7p1_4, sse);
119 #endif
120