xref: /freebsd/sys/dev/sound/pcm/feeder_volume.c (revision 4a7b8cd7057f0c37b4fc4b47b0ef3ec8402b0eb9)
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
feed_volume_init(struct pcm_feeder * f)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
feed_volume_free(struct pcm_feeder * f)191 feed_volume_free(struct pcm_feeder *f)
192 {
193 	struct feed_volume_info *info;
194 
195 	info = f->data;
196 	if (info != NULL)
197 		free(info, M_DEVBUF);
198 
199 	f->data = NULL;
200 
201 	return (0);
202 }
203 
204 static int
feed_volume_set(struct pcm_feeder * f,int what,int value)205 feed_volume_set(struct pcm_feeder *f, int what, int value)
206 {
207 	struct feed_volume_info *info;
208 	struct pcmchan_matrix *m;
209 	int ret;
210 
211 	info = f->data;
212 	ret = 0;
213 
214 	switch (what) {
215 	case FEEDVOLUME_CLASS:
216 		if (value < SND_VOL_C_BEGIN || value > SND_VOL_C_END)
217 			return (EINVAL);
218 		info->volume_class = value;
219 		break;
220 	case FEEDVOLUME_CHANNELS:
221 		if (value < SND_CHN_MIN || value > SND_CHN_MAX)
222 			return (EINVAL);
223 		m = feeder_matrix_default_channel_map(value);
224 		if (m == NULL)
225 			return (EINVAL);
226 		ret = feeder_volume_apply_matrix(f, m);
227 		break;
228 	case FEEDVOLUME_STATE:
229 		if (!(value == FEEDVOLUME_ENABLE || value == FEEDVOLUME_BYPASS))
230 			return (EINVAL);
231 		info->state = value;
232 		break;
233 	default:
234 		return (EINVAL);
235 		break;
236 	}
237 
238 	return (ret);
239 }
240 
241 static int
feed_volume_feed(struct pcm_feeder * f,struct pcm_channel * c,uint8_t * b,uint32_t count,void * source)242 feed_volume_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
243     uint32_t count, void *source)
244 {
245 	int temp_vol[SND_CHN_T_VOL_MAX];
246 	struct feed_volume_info *info;
247 	uint32_t j, align;
248 	int i, *matrix;
249 	uint8_t *dst;
250 	const int16_t *vol;
251 	const int8_t *muted;
252 
253 	/*
254 	 * Fetch filter data operation.
255 	 */
256 	info = f->data;
257 
258 	if (info->state == FEEDVOLUME_BYPASS)
259 		return (FEEDER_FEED(f->source, c, b, count, source));
260 
261 	vol = c->volume[SND_VOL_C_VAL(info->volume_class)];
262 	muted = c->muted[SND_VOL_C_VAL(info->volume_class)];
263 	matrix = info->matrix;
264 
265 	/*
266 	 * First, let see if we really need to apply gain at all.
267 	 */
268 	j = 0;
269 	i = info->channels;
270 	while (i--) {
271 		if (vol[matrix[i]] != SND_VOL_FLAT ||
272 		    muted[matrix[i]] != 0) {
273 			j = 1;
274 			break;
275 		}
276 	}
277 
278 	/* Nope, just bypass entirely. */
279 	if (j == 0)
280 		return (FEEDER_FEED(f->source, c, b, count, source));
281 
282 	/* Check if any controls are muted. */
283 	for (j = 0; j != SND_CHN_T_VOL_MAX; j++)
284 		temp_vol[j] = muted[j] ? 0 : vol[j];
285 
286 	dst = b;
287 	align = info->bps * info->channels;
288 
289 	do {
290 		if (count < align)
291 			break;
292 
293 		j = SND_FXDIV(FEEDER_FEED(f->source, c, dst, count, source),
294 		    align);
295 		if (j == 0)
296 			break;
297 
298 		info->apply(temp_vol, matrix, info->channels, dst, j);
299 
300 		j *= align;
301 		dst += j;
302 		count -= j;
303 
304 	} while (count != 0);
305 
306 	return (dst - b);
307 }
308 
309 static struct pcm_feederdesc feeder_volume_desc[] = {
310 	{ FEEDER_VOLUME, 0, 0, 0, 0 },
311 	{ 0, 0, 0, 0, 0 }
312 };
313 
314 static kobj_method_t feeder_volume_methods[] = {
315 	KOBJMETHOD(feeder_init,		feed_volume_init),
316 	KOBJMETHOD(feeder_free,		feed_volume_free),
317 	KOBJMETHOD(feeder_set,		feed_volume_set),
318 	KOBJMETHOD(feeder_feed,		feed_volume_feed),
319 	KOBJMETHOD_END
320 };
321 
322 FEEDER_DECLARE(feeder_volume, NULL);
323 
324 /* Extern */
325 
326 /*
327  * feeder_volume_apply_matrix(): For given matrix map, apply its configuration
328  *                               to feeder_volume matrix structure. There are
329  *                               possibilites that feeder_volume be inserted
330  *                               before or after feeder_matrix, which in this
331  *                               case feeder_volume must be in a good terms
332  *                               with _current_ matrix.
333  */
334 int
feeder_volume_apply_matrix(struct pcm_feeder * f,struct pcmchan_matrix * m)335 feeder_volume_apply_matrix(struct pcm_feeder *f, struct pcmchan_matrix *m)
336 {
337 	struct feed_volume_info *info;
338 	uint32_t i;
339 
340 	if (f == NULL || f->desc == NULL || f->desc->type != FEEDER_VOLUME ||
341 	    f->data == NULL || m == NULL || m->channels < SND_CHN_MIN ||
342 	    m->channels > SND_CHN_MAX)
343 		return (EINVAL);
344 
345 	info = f->data;
346 
347 	for (i = 0; i < nitems(info->matrix); i++) {
348 		if (i < m->channels)
349 			info->matrix[i] = m->map[i].type;
350 		else
351 			info->matrix[i] = SND_CHN_T_FL;
352 	}
353 
354 	info->channels = m->channels;
355 
356 	return (0);
357 }
358