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 FEEDVOLUME_DECLARE(S, 16, LE)
78 FEEDVOLUME_DECLARE(S, 32, LE)
79 FEEDVOLUME_DECLARE(S, 16, BE)
80 FEEDVOLUME_DECLARE(S, 32, BE)
81 FEEDVOLUME_DECLARE(S, 8, NE)
82 FEEDVOLUME_DECLARE(S, 24, LE)
83 FEEDVOLUME_DECLARE(S, 24, BE)
84 FEEDVOLUME_DECLARE(U, 8, NE)
85 FEEDVOLUME_DECLARE(U, 16, LE)
86 FEEDVOLUME_DECLARE(U, 24, LE)
87 FEEDVOLUME_DECLARE(U, 32, LE)
88 FEEDVOLUME_DECLARE(U, 16, BE)
89 FEEDVOLUME_DECLARE(U, 24, BE)
90 FEEDVOLUME_DECLARE(U, 32, BE)
91 FEEDVOLUME_DECLARE(F, 32, LE)
92 FEEDVOLUME_DECLARE(F, 32, BE)
93
94 struct feed_volume_info {
95 uint32_t bps, channels;
96 feed_volume_t apply;
97 int volume_class;
98 int state;
99 int matrix[SND_CHN_MAX];
100 };
101
102 #define FEEDVOLUME_ENTRY(SIGN, BIT, ENDIAN) \
103 { \
104 AFMT_##SIGN##BIT##_##ENDIAN, \
105 feed_volume_##SIGN##BIT##ENDIAN \
106 }
107
108 static const struct {
109 uint32_t format;
110 feed_volume_t apply;
111 } feed_volume_info_tab[] = {
112 FEEDVOLUME_ENTRY(S, 16, LE),
113 FEEDVOLUME_ENTRY(S, 32, LE),
114 FEEDVOLUME_ENTRY(S, 16, BE),
115 FEEDVOLUME_ENTRY(S, 32, BE),
116 FEEDVOLUME_ENTRY(S, 8, NE),
117 FEEDVOLUME_ENTRY(S, 24, LE),
118 FEEDVOLUME_ENTRY(S, 24, BE),
119 FEEDVOLUME_ENTRY(U, 8, NE),
120 FEEDVOLUME_ENTRY(U, 16, LE),
121 FEEDVOLUME_ENTRY(U, 24, LE),
122 FEEDVOLUME_ENTRY(U, 32, LE),
123 FEEDVOLUME_ENTRY(U, 16, BE),
124 FEEDVOLUME_ENTRY(U, 24, BE),
125 FEEDVOLUME_ENTRY(U, 32, BE),
126 FEEDVOLUME_ENTRY(F, 32, LE),
127 FEEDVOLUME_ENTRY(F, 32, BE),
128 };
129
130 #define FEEDVOLUME_TAB_SIZE ((int32_t) \
131 (sizeof(feed_volume_info_tab) / \
132 sizeof(feed_volume_info_tab[0])))
133
134 static int
feed_volume_init(struct pcm_feeder * f)135 feed_volume_init(struct pcm_feeder *f)
136 {
137 struct feed_volume_info *info;
138 struct pcmchan_matrix *m;
139 uint32_t i;
140 int ret;
141
142 if (f->desc.in != f->desc.out ||
143 AFMT_CHANNEL(f->desc.in) > SND_CHN_MAX)
144 return (EINVAL);
145
146 for (i = 0; i < FEEDVOLUME_TAB_SIZE; i++) {
147 if (AFMT_ENCODING(f->desc.in) ==
148 feed_volume_info_tab[i].format) {
149 info = malloc(sizeof(*info), M_DEVBUF,
150 M_NOWAIT | M_ZERO);
151 if (info == NULL)
152 return (ENOMEM);
153
154 info->bps = AFMT_BPS(f->desc.in);
155 info->channels = AFMT_CHANNEL(f->desc.in);
156 info->apply = feed_volume_info_tab[i].apply;
157 info->volume_class = SND_VOL_C_PCM;
158 info->state = FEEDVOLUME_ENABLE;
159
160 f->data = info;
161 m = feeder_matrix_default_channel_map(info->channels);
162 if (m == NULL) {
163 free(info, M_DEVBUF);
164 return (EINVAL);
165 }
166
167 ret = feeder_volume_apply_matrix(f, m);
168 if (ret != 0)
169 free(info, M_DEVBUF);
170
171 return (ret);
172 }
173 }
174
175 return (EINVAL);
176 }
177
178 static int
feed_volume_free(struct pcm_feeder * f)179 feed_volume_free(struct pcm_feeder *f)
180 {
181 struct feed_volume_info *info;
182
183 info = f->data;
184 free(info, M_DEVBUF);
185
186 f->data = NULL;
187
188 return (0);
189 }
190
191 static int
feed_volume_set(struct pcm_feeder * f,int what,int value)192 feed_volume_set(struct pcm_feeder *f, int what, int value)
193 {
194 struct feed_volume_info *info;
195 struct pcmchan_matrix *m;
196 int ret;
197
198 info = f->data;
199 ret = 0;
200
201 switch (what) {
202 case FEEDVOLUME_CLASS:
203 if (value < SND_VOL_C_BEGIN || value > SND_VOL_C_END)
204 return (EINVAL);
205 info->volume_class = value;
206 break;
207 case FEEDVOLUME_CHANNELS:
208 if (value < SND_CHN_MIN || value > SND_CHN_MAX)
209 return (EINVAL);
210 m = feeder_matrix_default_channel_map(value);
211 if (m == NULL)
212 return (EINVAL);
213 ret = feeder_volume_apply_matrix(f, m);
214 break;
215 case FEEDVOLUME_STATE:
216 if (!(value == FEEDVOLUME_ENABLE || value == FEEDVOLUME_BYPASS))
217 return (EINVAL);
218 info->state = value;
219 break;
220 default:
221 return (EINVAL);
222 }
223
224 return (ret);
225 }
226
227 static int
feed_volume_feed(struct pcm_feeder * f,struct pcm_channel * c,uint8_t * b,uint32_t count,void * source)228 feed_volume_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
229 uint32_t count, void *source)
230 {
231 int temp_vol[SND_CHN_T_VOL_MAX];
232 struct feed_volume_info *info;
233 struct snd_mixer *m;
234 struct snddev_info *d;
235 uint32_t j, align;
236 int i, *matrix;
237 uint8_t *dst;
238 const int16_t *vol;
239 const int8_t *muted;
240 bool master_muted = false;
241
242 /*
243 * Fetch filter data operation.
244 */
245 info = f->data;
246
247 if (info->state == FEEDVOLUME_BYPASS)
248 return (FEEDER_FEED(f->source, c, b, count, source));
249
250 vol = c->volume[SND_VOL_C_VAL(info->volume_class)];
251 muted = c->muted[SND_VOL_C_VAL(info->volume_class)];
252 matrix = info->matrix;
253
254 /*
255 * First, let see if we really need to apply gain at all.
256 */
257 j = 0;
258 i = info->channels;
259 while (i--) {
260 if (vol[matrix[i]] != SND_VOL_FLAT ||
261 muted[matrix[i]] != 0) {
262 j = 1;
263 break;
264 }
265 }
266
267 /* Nope, just bypass entirely. */
268 if (j == 0)
269 return (FEEDER_FEED(f->source, c, b, count, source));
270
271 /* Check if any controls are muted. */
272 d = (c != NULL) ? c->parentsnddev : NULL;
273 m = (d != NULL && d->mixer_dev != NULL) ? d->mixer_dev->si_drv1 : NULL;
274
275 if (m != NULL)
276 master_muted = (mix_getmutedevs(m) & (1 << SND_VOL_C_MASTER));
277
278 for (j = 0; j != SND_CHN_T_VOL_MAX; j++)
279 temp_vol[j] = (muted[j] || master_muted) ? 0 : vol[j];
280
281 dst = b;
282 align = info->bps * info->channels;
283
284 do {
285 if (count < align)
286 break;
287
288 j = SND_FXDIV(FEEDER_FEED(f->source, c, dst, count, source),
289 align);
290 if (j == 0)
291 break;
292
293 info->apply(temp_vol, matrix, info->channels, dst, j);
294
295 j *= align;
296 dst += j;
297 count -= j;
298
299 } while (count != 0);
300
301 return (dst - b);
302 }
303
304 static kobj_method_t feeder_volume_methods[] = {
305 KOBJMETHOD(feeder_init, feed_volume_init),
306 KOBJMETHOD(feeder_free, feed_volume_free),
307 KOBJMETHOD(feeder_set, feed_volume_set),
308 KOBJMETHOD(feeder_feed, feed_volume_feed),
309 KOBJMETHOD_END
310 };
311
312 FEEDER_DECLARE(feeder_volume, FEEDER_VOLUME);
313
314 /* Extern */
315
316 /*
317 * feeder_volume_apply_matrix(): For given matrix map, apply its configuration
318 * to feeder_volume matrix structure. There are
319 * possibilites that feeder_volume be inserted
320 * before or after feeder_matrix, which in this
321 * case feeder_volume must be in a good terms
322 * with _current_ matrix.
323 */
324 int
feeder_volume_apply_matrix(struct pcm_feeder * f,struct pcmchan_matrix * m)325 feeder_volume_apply_matrix(struct pcm_feeder *f, struct pcmchan_matrix *m)
326 {
327 struct feed_volume_info *info;
328 uint32_t i;
329
330 if (f == NULL || f->class->type != FEEDER_VOLUME || f->data == NULL ||
331 m == NULL || m->channels < SND_CHN_MIN ||
332 m->channels > SND_CHN_MAX)
333 return (EINVAL);
334
335 info = f->data;
336
337 for (i = 0; i < nitems(info->matrix); i++) {
338 if (i < m->channels)
339 info->matrix[i] = m->map[i].type;
340 else
341 info->matrix[i] = SND_CHN_T_FL;
342 }
343
344 info->channels = m->channels;
345
346 return (0);
347 }
348