xref: /freebsd/sys/dev/sound/pcm/feeder_volume.c (revision e1bbaa71d62c8681a576f9f5bedf475c7541bd35)
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 #endif
138 };
139 
140 #define FEEDVOLUME_TAB_SIZE	((int32_t)				\
141 				 (sizeof(feed_volume_info_tab) /	\
142 				  sizeof(feed_volume_info_tab[0])))
143 
144 static int
feed_volume_init(struct pcm_feeder * f)145 feed_volume_init(struct pcm_feeder *f)
146 {
147 	struct feed_volume_info *info;
148 	struct pcmchan_matrix *m;
149 	uint32_t i;
150 	int ret;
151 
152 	if (f->desc->in != f->desc->out ||
153 	    AFMT_CHANNEL(f->desc->in) > SND_CHN_MAX)
154 		return (EINVAL);
155 
156 	for (i = 0; i < FEEDVOLUME_TAB_SIZE; i++) {
157 		if (AFMT_ENCODING(f->desc->in) ==
158 		    feed_volume_info_tab[i].format) {
159 			info = malloc(sizeof(*info), M_DEVBUF,
160 			    M_NOWAIT | M_ZERO);
161 			if (info == NULL)
162 				return (ENOMEM);
163 
164 			info->bps = AFMT_BPS(f->desc->in);
165 			info->channels = AFMT_CHANNEL(f->desc->in);
166 			info->apply = feed_volume_info_tab[i].apply;
167 			info->volume_class = SND_VOL_C_PCM;
168 			info->state = FEEDVOLUME_ENABLE;
169 
170 			f->data = info;
171 			m = feeder_matrix_default_channel_map(info->channels);
172 			if (m == NULL) {
173 				free(info, M_DEVBUF);
174 				return (EINVAL);
175 			}
176 
177 			ret = feeder_volume_apply_matrix(f, m);
178 			if (ret != 0)
179 				free(info, M_DEVBUF);
180 
181 			return (ret);
182 		}
183 	}
184 
185 	return (EINVAL);
186 }
187 
188 static int
feed_volume_free(struct pcm_feeder * f)189 feed_volume_free(struct pcm_feeder *f)
190 {
191 	struct feed_volume_info *info;
192 
193 	info = f->data;
194 	if (info != NULL)
195 		free(info, M_DEVBUF);
196 
197 	f->data = NULL;
198 
199 	return (0);
200 }
201 
202 static int
feed_volume_set(struct pcm_feeder * f,int what,int value)203 feed_volume_set(struct pcm_feeder *f, int what, int value)
204 {
205 	struct feed_volume_info *info;
206 	struct pcmchan_matrix *m;
207 	int ret;
208 
209 	info = f->data;
210 	ret = 0;
211 
212 	switch (what) {
213 	case FEEDVOLUME_CLASS:
214 		if (value < SND_VOL_C_BEGIN || value > SND_VOL_C_END)
215 			return (EINVAL);
216 		info->volume_class = value;
217 		break;
218 	case FEEDVOLUME_CHANNELS:
219 		if (value < SND_CHN_MIN || value > SND_CHN_MAX)
220 			return (EINVAL);
221 		m = feeder_matrix_default_channel_map(value);
222 		if (m == NULL)
223 			return (EINVAL);
224 		ret = feeder_volume_apply_matrix(f, m);
225 		break;
226 	case FEEDVOLUME_STATE:
227 		if (!(value == FEEDVOLUME_ENABLE || value == FEEDVOLUME_BYPASS))
228 			return (EINVAL);
229 		info->state = value;
230 		break;
231 	default:
232 		return (EINVAL);
233 		break;
234 	}
235 
236 	return (ret);
237 }
238 
239 static int
feed_volume_feed(struct pcm_feeder * f,struct pcm_channel * c,uint8_t * b,uint32_t count,void * source)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 struct pcm_feederdesc feeder_volume_desc[] = {
308 	{ FEEDER_VOLUME, 0, 0, 0, 0 },
309 	{ 0, 0, 0, 0, 0 }
310 };
311 
312 static kobj_method_t feeder_volume_methods[] = {
313 	KOBJMETHOD(feeder_init,		feed_volume_init),
314 	KOBJMETHOD(feeder_free,		feed_volume_free),
315 	KOBJMETHOD(feeder_set,		feed_volume_set),
316 	KOBJMETHOD(feeder_feed,		feed_volume_feed),
317 	KOBJMETHOD_END
318 };
319 
320 FEEDER_DECLARE(feeder_volume, NULL);
321 
322 /* Extern */
323 
324 /*
325  * feeder_volume_apply_matrix(): For given matrix map, apply its configuration
326  *                               to feeder_volume matrix structure. There are
327  *                               possibilites that feeder_volume be inserted
328  *                               before or after feeder_matrix, which in this
329  *                               case feeder_volume must be in a good terms
330  *                               with _current_ matrix.
331  */
332 int
feeder_volume_apply_matrix(struct pcm_feeder * f,struct pcmchan_matrix * m)333 feeder_volume_apply_matrix(struct pcm_feeder *f, struct pcmchan_matrix *m)
334 {
335 	struct feed_volume_info *info;
336 	uint32_t i;
337 
338 	if (f == NULL || f->desc == NULL || f->desc->type != FEEDER_VOLUME ||
339 	    f->data == NULL || m == NULL || m->channels < SND_CHN_MIN ||
340 	    m->channels > SND_CHN_MAX)
341 		return (EINVAL);
342 
343 	info = f->data;
344 
345 	for (i = 0; i < nitems(info->matrix); i++) {
346 		if (i < m->channels)
347 			info->matrix[i] = m->map[i].type;
348 		else
349 			info->matrix[i] = SND_CHN_T_FL;
350 	}
351 
352 	info->channels = m->channels;
353 
354 	return (0);
355 }
356