2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008-2009 Ariff Abdullah <ariff@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 #ifdef HAVE_KERNEL_OPTION_HEADERS
33 #include <dev/sound/pcm/sound.h>
34 #include <dev/sound/pcm/pcm.h>
35 #include <dev/sound/pcm/vchan.h>
36 #include "feeder_if.h"
39 #include "snd_fxdiv_gen.h"
41 SND_DECLARE_FILE("$FreeBSD$");
44 #undef SND_FEEDER_MULTIFORMAT
45 #define SND_FEEDER_MULTIFORMAT 1
47 typedef void (*feed_mixer_t)(uint8_t *, uint8_t *, uint32_t);
49 #define FEEDMIXER_DECLARE(SIGN, BIT, ENDIAN) \
51 feed_mixer_##SIGN##BIT##ENDIAN(uint8_t *src, uint8_t *dst, \
61 src -= PCM_##BIT##_BPS; \
62 dst -= PCM_##BIT##_BPS; \
63 count -= PCM_##BIT##_BPS; \
64 x = PCM_READ_##SIGN##BIT##_##ENDIAN(src); \
65 y = PCM_READ_##SIGN##BIT##_##ENDIAN(dst); \
66 z = INTPCM##BIT##_T(x) + y; \
67 x = PCM_CLAMP_##SIGN##BIT(z); \
68 _PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, x); \
69 } while (count != 0); \
72 #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
73 FEEDMIXER_DECLARE(S, 16, LE)
74 FEEDMIXER_DECLARE(S, 32, LE)
76 #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
77 FEEDMIXER_DECLARE(S, 16, BE)
78 FEEDMIXER_DECLARE(S, 32, BE)
80 #ifdef SND_FEEDER_MULTIFORMAT
81 FEEDMIXER_DECLARE(S, 8, NE)
82 FEEDMIXER_DECLARE(S, 24, LE)
83 FEEDMIXER_DECLARE(S, 24, BE)
84 FEEDMIXER_DECLARE(U, 8, NE)
85 FEEDMIXER_DECLARE(U, 16, LE)
86 FEEDMIXER_DECLARE(U, 24, LE)
87 FEEDMIXER_DECLARE(U, 32, LE)
88 FEEDMIXER_DECLARE(U, 16, BE)
89 FEEDMIXER_DECLARE(U, 24, BE)
90 FEEDMIXER_DECLARE(U, 32, BE)
93 struct feed_mixer_info {
99 #define FEEDMIXER_ENTRY(SIGN, BIT, ENDIAN) \
101 AFMT_##SIGN##BIT##_##ENDIAN, PCM_##BIT##_BPS, \
102 feed_mixer_##SIGN##BIT##ENDIAN \
105 static struct feed_mixer_info feed_mixer_info_tab[] = {
106 FEEDMIXER_ENTRY(S, 8, NE),
107 #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
108 FEEDMIXER_ENTRY(S, 16, LE),
109 FEEDMIXER_ENTRY(S, 32, LE),
111 #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
112 FEEDMIXER_ENTRY(S, 16, BE),
113 FEEDMIXER_ENTRY(S, 32, BE),
115 #ifdef SND_FEEDER_MULTIFORMAT
116 FEEDMIXER_ENTRY(S, 24, LE),
117 FEEDMIXER_ENTRY(S, 24, BE),
118 FEEDMIXER_ENTRY(U, 8, NE),
119 FEEDMIXER_ENTRY(U, 16, LE),
120 FEEDMIXER_ENTRY(U, 24, LE),
121 FEEDMIXER_ENTRY(U, 32, LE),
122 FEEDMIXER_ENTRY(U, 16, BE),
123 FEEDMIXER_ENTRY(U, 24, BE),
124 FEEDMIXER_ENTRY(U, 32, BE),
126 { AFMT_AC3, PCM_16_BPS, NULL },
127 { AFMT_MU_LAW, PCM_8_BPS, feed_mixer_U8NE }, /* dummy */
128 { AFMT_A_LAW, PCM_8_BPS, feed_mixer_U8NE } /* dummy */
131 #define FEEDMIXER_TAB_SIZE ((int32_t) \
132 (sizeof(feed_mixer_info_tab) / \
133 sizeof(feed_mixer_info_tab[0])))
135 #define FEEDMIXER_DATA(i, c) ((void *) \
136 ((uintptr_t)((((i) & 0x1f) << 7) | \
138 #define FEEDMIXER_INFOIDX(d) ((uint32_t)((uintptr_t)(d) >> 7) & 0x1f)
139 #define FEEDMIXER_CHANNELS(d) ((uint32_t)((uintptr_t)(d)) & 0x7f)
142 feed_mixer_init(struct pcm_feeder *f)
146 if (f->desc->in != f->desc->out)
149 for (i = 0; i < FEEDMIXER_TAB_SIZE; i++) {
150 if (AFMT_ENCODING(f->desc->in) ==
151 feed_mixer_info_tab[i].format) {
153 FEEDMIXER_DATA(i, AFMT_CHANNEL(f->desc->in));
162 feed_mixer_set(struct pcm_feeder *f, int what, int value)
166 case FEEDMIXER_CHANNELS:
167 if (value < SND_CHN_MIN || value > SND_CHN_MAX)
169 f->data = FEEDMIXER_DATA(FEEDMIXER_INFOIDX(f->data), value);
180 feed_mixer_rec(struct pcm_channel *c)
182 struct pcm_channel *ch;
183 struct snd_dbuf *b, *bs;
184 uint32_t cnt, maxfeed;
188 * Reset ready and moving pointer. We're not using bufsoft
189 * anywhere since its sole purpose is to become the primary
190 * distributor for the recorded buffer and also as an interrupt
191 * threshold progress indicator.
196 cnt = sndbuf_getsize(b);
197 maxfeed = SND_FXROUND(SND_FXDIV_MAX, sndbuf_getalign(b));
200 cnt = FEEDER_FEED(c->feeder->source, c, b->tmpbuf,
201 min(cnt, maxfeed), c->bufhard);
203 sndbuf_acquire(b, b->tmpbuf, cnt);
204 cnt = sndbuf_getfree(b);
208 /* Not enough data */
209 if (b->rl < sndbuf_getalign(b)) {
215 * Keep track of ready and moving pointer since we will use
216 * bufsoft over and over again, pretending nothing has happened.
220 CHN_FOREACH(ch, c, children.busy) {
222 if (CHN_STOPPED(ch) || (ch->flags & CHN_F_DIRTY)) {
227 if ((c->flags & CHN_F_DIRTY) && VCHAN_SYNC_REQUIRED(ch)) {
228 if (vchan_sync(ch) != 0) {
235 if (ch->flags & CHN_F_MMAP)
236 sndbuf_dispose(bs, NULL, sndbuf_getready(bs));
237 cnt = sndbuf_getfree(bs);
238 if (cnt < sndbuf_getalign(bs)) {
242 maxfeed = SND_FXROUND(SND_FXDIV_MAX, sndbuf_getalign(bs));
244 cnt = FEEDER_FEED(ch->feeder, ch, bs->tmpbuf,
245 min(cnt, maxfeed), b);
247 sndbuf_acquire(bs, bs->tmpbuf, cnt);
248 cnt = sndbuf_getfree(bs);
252 * Not entirely flushed out...
258 * Rewind buffer position for next virtual channel.
265 * Set ready pointer to indicate that our children are ready
266 * to be woken up, also as an interrupt threshold progress
271 c->flags &= ~CHN_F_DIRTY;
274 * Return 0 to bail out early from sndbuf_feed() loop.
275 * No need to increase feedcount counter since part of this
276 * feeder chains already include feed_root().
282 feed_mixer_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
283 uint32_t count, void *source)
285 struct feed_mixer_info *info;
286 struct snd_dbuf *src = source;
287 struct pcm_channel *ch;
288 uint32_t cnt, mcnt, rcnt, sz;
292 if (c->direction == PCMDIR_REC)
293 return (feed_mixer_rec(c));
295 sz = sndbuf_getsize(src);
299 info = &feed_mixer_info_tab[FEEDMIXER_INFOIDX(f->data)];
300 sz = info->bps * FEEDMIXER_CHANNELS(f->data);
301 count = SND_FXROUND(count, sz);
306 * We are going to use our source as a temporary buffer since it's
307 * got no other purpose. We obtain our data by traversing the channel
308 * list of children and calling mixer function to mix count bytes from
309 * each into our destination buffer, b.
311 tmp = sndbuf_getbuf(src);
314 passthrough = 0; /* 'passthrough' / 'exclusive' marker */
316 CHN_FOREACH(ch, c, children.busy) {
318 if (CHN_STOPPED(ch) || (ch->flags & CHN_F_DIRTY)) {
323 if ((c->flags & CHN_F_DIRTY) && VCHAN_SYNC_REQUIRED(ch)) {
324 if (vchan_sync(ch) != 0) {
330 if ((ch->flags & CHN_F_MMAP) && !(ch->flags & CHN_F_CLOSING))
331 sndbuf_acquire(ch->bufsoft, NULL,
332 sndbuf_getfree(ch->bufsoft));
333 if (info->mix == NULL) {
335 * Passthrough. Dump the first digital/passthrough
336 * channel into destination buffer, and the rest into
337 * nothingness (mute effect).
339 if (passthrough == 0 &&
340 (ch->format & AFMT_PASSTHROUGH)) {
341 rcnt = SND_FXROUND(FEEDER_FEED(ch->feeder, ch,
342 b, count, ch->bufsoft), sz);
345 FEEDER_FEED(ch->feeder, ch, tmp, count,
347 } else if (c->flags & CHN_F_EXCLUSIVE) {
349 * Exclusive. Dump the first 'exclusive' channel into
350 * destination buffer, and the rest into nothingness
353 if (passthrough == 0 && (ch->flags & CHN_F_EXCLUSIVE)) {
354 rcnt = SND_FXROUND(FEEDER_FEED(ch->feeder, ch,
355 b, count, ch->bufsoft), sz);
358 FEEDER_FEED(ch->feeder, ch, tmp, count,
362 rcnt = SND_FXROUND(FEEDER_FEED(ch->feeder, ch,
363 b, count, ch->bufsoft), sz);
366 cnt = SND_FXROUND(FEEDER_FEED(ch->feeder, ch,
367 tmp, count, ch->bufsoft), sz);
372 f->desc->out), mcnt);
375 info->mix(tmp, b, cnt);
384 if (++c->feedcount == 0)
387 c->flags &= ~CHN_F_DIRTY;
392 static struct pcm_feederdesc feeder_mixer_desc[] = {
393 { FEEDER_MIXER, 0, 0, 0, 0 },
397 static kobj_method_t feeder_mixer_methods[] = {
398 KOBJMETHOD(feeder_init, feed_mixer_init),
399 KOBJMETHOD(feeder_set, feed_mixer_set),
400 KOBJMETHOD(feeder_feed, feed_mixer_feed),
404 FEEDER_DECLARE(feeder_mixer, NULL);