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