xref: /src/sys/dev/sound/pcm/feeder_volume.c (revision fa3519d068d95f87e773d27f96e9f1e18f70075a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /* feeder_volume, a long 'Lost Technology' rather than a new feature. */
30 
31 #ifdef _KERNEL
32 #ifdef HAVE_KERNEL_OPTION_HEADERS
33 #include "opt_snd.h"
34 #endif
35 #include <dev/sound/pcm/sound.h>
36 #include <dev/sound/pcm/pcm.h>
37 #include "feeder_if.h"
38 
39 #define SND_USE_FXDIV
40 #include "snd_fxdiv_gen.h"
41 #endif
42 
43 typedef void (*feed_volume_t)(int *, int *, uint32_t, uint8_t *, uint32_t);
44 
45 #define FEEDVOLUME_CALC8(s, v)	(SND_VOL_CALC_SAMPLE((intpcm_t)		\
46 				 (s) << 8, v) >> 8)
47 #define FEEDVOLUME_CALC16(s, v)	SND_VOL_CALC_SAMPLE((intpcm_t)(s), v)
48 #define FEEDVOLUME_CALC24(s, v)	SND_VOL_CALC_SAMPLE((intpcm64_t)(s), v)
49 #define FEEDVOLUME_CALC32(s, v)	SND_VOL_CALC_SAMPLE((intpcm64_t)(s), v)
50 
51 #define FEEDVOLUME_DECLARE(SIGN, BIT, ENDIAN)				\
52 static void								\
53 feed_volume_##SIGN##BIT##ENDIAN(int *vol, int *matrix,			\
54     uint32_t channels, uint8_t *dst, uint32_t count)			\
55 {									\
56 	intpcm##BIT##_t v;						\
57 	intpcm_t x;							\
58 	uint32_t i;							\
59 									\
60 	dst += count * PCM_##BIT##_BPS * channels;			\
61 	do {								\
62 		i = channels;						\
63 		do {							\
64 			dst -= PCM_##BIT##_BPS;				\
65 			i--;						\
66 			x = pcm_sample_read_calc(dst,			\
67 			    AFMT_##SIGN##BIT##_##ENDIAN);		\
68 			v = FEEDVOLUME_CALC##BIT(x, vol[matrix[i]]);	\
69 			x = pcm_clamp_calc(v,				\
70 			    AFMT_##SIGN##BIT##_##ENDIAN);		\
71 			pcm_sample_write(dst, x,			\
72 			    AFMT_##SIGN##BIT##_##ENDIAN);		\
73 		} while (i != 0);					\
74 	} while (--count != 0);						\
75 }
76 
77 #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
78 FEEDVOLUME_DECLARE(S, 16, LE)
79 FEEDVOLUME_DECLARE(S, 32, LE)
80 #endif
81 #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
82 FEEDVOLUME_DECLARE(S, 16, BE)
83 FEEDVOLUME_DECLARE(S, 32, BE)
84 #endif
85 #ifdef SND_FEEDER_MULTIFORMAT
86 FEEDVOLUME_DECLARE(S,  8, NE)
87 FEEDVOLUME_DECLARE(S, 24, LE)
88 FEEDVOLUME_DECLARE(S, 24, BE)
89 FEEDVOLUME_DECLARE(U,  8, NE)
90 FEEDVOLUME_DECLARE(U, 16, LE)
91 FEEDVOLUME_DECLARE(U, 24, LE)
92 FEEDVOLUME_DECLARE(U, 32, LE)
93 FEEDVOLUME_DECLARE(U, 16, BE)
94 FEEDVOLUME_DECLARE(U, 24, BE)
95 FEEDVOLUME_DECLARE(U, 32, BE)
96 FEEDVOLUME_DECLARE(F, 32, LE)
97 FEEDVOLUME_DECLARE(F, 32, BE)
98 #endif
99 
100 struct feed_volume_info {
101 	uint32_t bps, channels;
102 	feed_volume_t apply;
103 	int volume_class;
104 	int state;
105 	int matrix[SND_CHN_MAX];
106 };
107 
108 #define FEEDVOLUME_ENTRY(SIGN, BIT, ENDIAN)				\
109 	{								\
110 		AFMT_##SIGN##BIT##_##ENDIAN,				\
111 		feed_volume_##SIGN##BIT##ENDIAN				\
112 	}
113 
114 static const struct {
115 	uint32_t format;
116 	feed_volume_t apply;
117 } feed_volume_info_tab[] = {
118 #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
119 	FEEDVOLUME_ENTRY(S, 16, LE),
120 	FEEDVOLUME_ENTRY(S, 32, LE),
121 #endif
122 #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
123 	FEEDVOLUME_ENTRY(S, 16, BE),
124 	FEEDVOLUME_ENTRY(S, 32, BE),
125 #endif
126 #ifdef SND_FEEDER_MULTIFORMAT
127 	FEEDVOLUME_ENTRY(S,  8, NE),
128 	FEEDVOLUME_ENTRY(S, 24, LE),
129 	FEEDVOLUME_ENTRY(S, 24, BE),
130 	FEEDVOLUME_ENTRY(U,  8, NE),
131 	FEEDVOLUME_ENTRY(U, 16, LE),
132 	FEEDVOLUME_ENTRY(U, 24, LE),
133 	FEEDVOLUME_ENTRY(U, 32, LE),
134 	FEEDVOLUME_ENTRY(U, 16, BE),
135 	FEEDVOLUME_ENTRY(U, 24, BE),
136 	FEEDVOLUME_ENTRY(U, 32, BE),
137 	FEEDVOLUME_ENTRY(F, 32, LE),
138 	FEEDVOLUME_ENTRY(F, 32, BE),
139 #endif
140 };
141 
142 #define FEEDVOLUME_TAB_SIZE	((int32_t)				\
143 				 (sizeof(feed_volume_info_tab) /	\
144 				  sizeof(feed_volume_info_tab[0])))
145 
146 static int
147 feed_volume_init(struct pcm_feeder *f)
148 {
149 	struct feed_volume_info *info;
150 	struct pcmchan_matrix *m;
151 	uint32_t i;
152 	int ret;
153 
154 	if (f->desc.in != f->desc.out ||
155 	    AFMT_CHANNEL(f->desc.in) > SND_CHN_MAX)
156 		return (EINVAL);
157 
158 	for (i = 0; i < FEEDVOLUME_TAB_SIZE; i++) {
159 		if (AFMT_ENCODING(f->desc.in) ==
160 		    feed_volume_info_tab[i].format) {
161 			info = malloc(sizeof(*info), M_DEVBUF,
162 			    M_NOWAIT | M_ZERO);
163 			if (info == NULL)
164 				return (ENOMEM);
165 
166 			info->bps = AFMT_BPS(f->desc.in);
167 			info->channels = AFMT_CHANNEL(f->desc.in);
168 			info->apply = feed_volume_info_tab[i].apply;
169 			info->volume_class = SND_VOL_C_PCM;
170 			info->state = FEEDVOLUME_ENABLE;
171 
172 			f->data = info;
173 			m = feeder_matrix_default_channel_map(info->channels);
174 			if (m == NULL) {
175 				free(info, M_DEVBUF);
176 				return (EINVAL);
177 			}
178 
179 			ret = feeder_volume_apply_matrix(f, m);
180 			if (ret != 0)
181 				free(info, M_DEVBUF);
182 
183 			return (ret);
184 		}
185 	}
186 
187 	return (EINVAL);
188 }
189 
190 static int
191 feed_volume_free(struct pcm_feeder *f)
192 {
193 	struct feed_volume_info *info;
194 
195 	info = f->data;
196 	free(info, M_DEVBUF);
197 
198 	f->data = NULL;
199 
200 	return (0);
201 }
202 
203 static int
204 feed_volume_set(struct pcm_feeder *f, int what, int value)
205 {
206 	struct feed_volume_info *info;
207 	struct pcmchan_matrix *m;
208 	int ret;
209 
210 	info = f->data;
211 	ret = 0;
212 
213 	switch (what) {
214 	case FEEDVOLUME_CLASS:
215 		if (value < SND_VOL_C_BEGIN || value > SND_VOL_C_END)
216 			return (EINVAL);
217 		info->volume_class = value;
218 		break;
219 	case FEEDVOLUME_CHANNELS:
220 		if (value < SND_CHN_MIN || value > SND_CHN_MAX)
221 			return (EINVAL);
222 		m = feeder_matrix_default_channel_map(value);
223 		if (m == NULL)
224 			return (EINVAL);
225 		ret = feeder_volume_apply_matrix(f, m);
226 		break;
227 	case FEEDVOLUME_STATE:
228 		if (!(value == FEEDVOLUME_ENABLE || value == FEEDVOLUME_BYPASS))
229 			return (EINVAL);
230 		info->state = value;
231 		break;
232 	default:
233 		return (EINVAL);
234 	}
235 
236 	return (ret);
237 }
238 
239 static int
240 feed_volume_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
241     uint32_t count, void *source)
242 {
243 	int temp_vol[SND_CHN_T_VOL_MAX];
244 	struct feed_volume_info *info;
245 	uint32_t j, align;
246 	int i, *matrix;
247 	uint8_t *dst;
248 	const int16_t *vol;
249 	const int8_t *muted;
250 
251 	/*
252 	 * Fetch filter data operation.
253 	 */
254 	info = f->data;
255 
256 	if (info->state == FEEDVOLUME_BYPASS)
257 		return (FEEDER_FEED(f->source, c, b, count, source));
258 
259 	vol = c->volume[SND_VOL_C_VAL(info->volume_class)];
260 	muted = c->muted[SND_VOL_C_VAL(info->volume_class)];
261 	matrix = info->matrix;
262 
263 	/*
264 	 * First, let see if we really need to apply gain at all.
265 	 */
266 	j = 0;
267 	i = info->channels;
268 	while (i--) {
269 		if (vol[matrix[i]] != SND_VOL_FLAT ||
270 		    muted[matrix[i]] != 0) {
271 			j = 1;
272 			break;
273 		}
274 	}
275 
276 	/* Nope, just bypass entirely. */
277 	if (j == 0)
278 		return (FEEDER_FEED(f->source, c, b, count, source));
279 
280 	/* Check if any controls are muted. */
281 	for (j = 0; j != SND_CHN_T_VOL_MAX; j++)
282 		temp_vol[j] = muted[j] ? 0 : vol[j];
283 
284 	dst = b;
285 	align = info->bps * info->channels;
286 
287 	do {
288 		if (count < align)
289 			break;
290 
291 		j = SND_FXDIV(FEEDER_FEED(f->source, c, dst, count, source),
292 		    align);
293 		if (j == 0)
294 			break;
295 
296 		info->apply(temp_vol, matrix, info->channels, dst, j);
297 
298 		j *= align;
299 		dst += j;
300 		count -= j;
301 
302 	} while (count != 0);
303 
304 	return (dst - b);
305 }
306 
307 static kobj_method_t feeder_volume_methods[] = {
308 	KOBJMETHOD(feeder_init,		feed_volume_init),
309 	KOBJMETHOD(feeder_free,		feed_volume_free),
310 	KOBJMETHOD(feeder_set,		feed_volume_set),
311 	KOBJMETHOD(feeder_feed,		feed_volume_feed),
312 	KOBJMETHOD_END
313 };
314 
315 FEEDER_DECLARE(feeder_volume, FEEDER_VOLUME);
316 
317 /* Extern */
318 
319 /*
320  * feeder_volume_apply_matrix(): For given matrix map, apply its configuration
321  *                               to feeder_volume matrix structure. There are
322  *                               possibilites that feeder_volume be inserted
323  *                               before or after feeder_matrix, which in this
324  *                               case feeder_volume must be in a good terms
325  *                               with _current_ matrix.
326  */
327 int
328 feeder_volume_apply_matrix(struct pcm_feeder *f, struct pcmchan_matrix *m)
329 {
330 	struct feed_volume_info *info;
331 	uint32_t i;
332 
333 	if (f == NULL || f->class->type != FEEDER_VOLUME || f->data == NULL ||
334 	    m == NULL || m->channels < SND_CHN_MIN ||
335 	    m->channels > SND_CHN_MAX)
336 		return (EINVAL);
337 
338 	info = f->data;
339 
340 	for (i = 0; i < nitems(info->matrix); i++) {
341 		if (i < m->channels)
342 			info->matrix[i] = m->map[i].type;
343 		else
344 			info->matrix[i] = SND_CHN_T_FL;
345 	}
346 
347 	info->channels = m->channels;
348 
349 	return (0);
350 }
351