diff --git a/sys/dev/sound/pcm/feeder.c b/sys/dev/sound/pcm/feeder.c index 75c0f0405040..e2d05f307f4d 100644 --- a/sys/dev/sound/pcm/feeder.c +++ b/sys/dev/sound/pcm/feeder.c @@ -1,402 +1,386 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2005-2009 Ariff Abdullah * Copyright (c) 1999 Cameron Grant * All rights reserved. * Copyright (c) 2024-2025 The FreeBSD Foundation * * Portions of this software were developed by Christos Margiolis * under sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifdef HAVE_KERNEL_OPTION_HEADERS #include "opt_snd.h" #endif #include #include "feeder_if.h" static MALLOC_DEFINE(M_FEEDER, "feeder", "pcm feeder"); static SLIST_HEAD(, feeder_class) feedertab; static void feeder_register_root(void *p) { struct feeder_class *fc = p; KASSERT(fc->type == FEEDER_ROOT, ("first feeder not root: %s", fc->name)); SLIST_INIT(&feedertab); SLIST_INSERT_HEAD(&feedertab, fc, link); } void feeder_register(void *p) { struct feeder_class *fc = p; KASSERT(fc->type != 0, ("feeder '%s' has no descriptor", fc->name)); SLIST_INSERT_HEAD(&feedertab, fc, link); } static void feeder_unregisterall(void *p __unused) { SLIST_INIT(&feedertab); } static void feeder_destroy(struct pcm_feeder *f) { FEEDER_FREE(f); kobj_delete((kobj_t)f, M_FEEDER); } static struct pcm_feeder * feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc) { struct pcm_feeder *f; int err; f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_NOWAIT | M_ZERO); if (f == NULL) return NULL; f->class = fc; f->desc = &(f->desc_static); if (desc != NULL) *(f->desc) = *desc; err = FEEDER_INIT(f); if (err) { printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err); feeder_destroy(f); return NULL; } return f; } struct feeder_class * feeder_getclass(u_int32_t type) { struct feeder_class *fc; SLIST_FOREACH(fc, &feedertab, link) { if (fc->type == type) return (fc); } return (NULL); } int feeder_add(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc) { struct pcm_feeder *nf; nf = feeder_create(fc, desc); if (nf == NULL) return ENOSPC; nf->source = c->feeder; if (c->feeder != NULL) c->feeder->parent = nf; c->feeder = nf; return 0; } void feeder_remove(struct pcm_channel *c) { struct pcm_feeder *f; while (c->feeder != NULL) { f = c->feeder; c->feeder = c->feeder->source; feeder_destroy(f); } } struct pcm_feeder * feeder_find(struct pcm_channel *c, u_int32_t type) { struct pcm_feeder *f; f = c->feeder; while (f != NULL) { if (f->class->type == type) return f; f = f->source; } return NULL; } /* * 14bit format scoring * -------------------- * * 13 12 11 10 9 8 2 1 0 offset * +---+---+---+---+---+---+-------------+---+---+ * | X | X | X | X | X | X | X X X X X X | X | X | * +---+---+---+---+---+---+-------------+---+---+ * | | | | | | | | | * | | | | | | | | +--> signed? * | | | | | | | | * | | | | | | | +------> bigendian? * | | | | | | | * | | | | | | +---------------> total channels * | | | | | | * | | | | | +------------------------> AFMT_A_LAW * | | | | | * | | | | +----------------------------> AFMT_MU_LAW * | | | | * | | | +--------------------------------> AFMT_8BIT * | | | * | | +------------------------------------> AFMT_16BIT * | | * | +----------------------------------------> AFMT_24BIT * | * +--------------------------------------------> AFMT_32BIT */ #define score_signeq(s1, s2) (((s1) & 0x1) == ((s2) & 0x1)) #define score_endianeq(s1, s2) (((s1) & 0x2) == ((s2) & 0x2)) #define score_cheq(s1, s2) (((s1) & 0xfc) == ((s2) & 0xfc)) #define score_chgt(s1, s2) (((s1) & 0xfc) > ((s2) & 0xfc)) #define score_chlt(s1, s2) (((s1) & 0xfc) < ((s2) & 0xfc)) #define score_val(s1) ((s1) & 0x3f00) #define score_cse(s1) ((s1) & 0x7f) u_int32_t snd_fmtscore(u_int32_t fmt) { u_int32_t ret; ret = 0; if (fmt & AFMT_SIGNED) ret |= 1 << 0; if (fmt & AFMT_BIGENDIAN) ret |= 1 << 1; /*if (fmt & AFMT_STEREO) ret |= (2 & 0x3f) << 2; else ret |= (1 & 0x3f) << 2;*/ ret |= (AFMT_CHANNEL(fmt) & 0x3f) << 2; if (fmt & AFMT_A_LAW) ret |= 1 << 8; else if (fmt & AFMT_MU_LAW) ret |= 1 << 9; else if (fmt & AFMT_8BIT) ret |= 1 << 10; else if (fmt & AFMT_16BIT) ret |= 1 << 11; else if (fmt & AFMT_24BIT) ret |= 1 << 12; else if (fmt & AFMT_32BIT) ret |= 1 << 13; return ret; } static u_int32_t snd_fmtbestfunc(u_int32_t fmt, u_int32_t *fmts, int cheq) { u_int32_t best, score, score2, oldscore; int i; if (fmt == 0 || fmts == NULL || fmts[0] == 0) return 0; if (snd_fmtvalid(fmt, fmts)) return fmt; best = 0; score = snd_fmtscore(fmt); oldscore = 0; for (i = 0; fmts[i] != 0; i++) { score2 = snd_fmtscore(fmts[i]); if (cheq && !score_cheq(score, score2) && (score_chlt(score2, score) || (oldscore != 0 && score_chgt(score2, oldscore)))) continue; if (oldscore == 0 || (score_val(score2) == score_val(score)) || (score_val(score2) == score_val(oldscore)) || (score_val(score2) > score_val(oldscore) && score_val(score2) < score_val(score)) || (score_val(score2) < score_val(oldscore) && score_val(score2) > score_val(score)) || (score_val(oldscore) < score_val(score) && score_val(score2) > score_val(oldscore))) { if (score_val(oldscore) != score_val(score2) || score_cse(score) == score_cse(score2) || ((score_cse(oldscore) != score_cse(score) && !score_endianeq(score, oldscore) && (score_endianeq(score, score2) || (!score_signeq(score, oldscore) && score_signeq(score, score2)))))) { best = fmts[i]; oldscore = score2; } } } return best; } u_int32_t snd_fmtbestbit(u_int32_t fmt, u_int32_t *fmts) { return snd_fmtbestfunc(fmt, fmts, 0); } u_int32_t snd_fmtbestchannel(u_int32_t fmt, u_int32_t *fmts) { return snd_fmtbestfunc(fmt, fmts, 1); } u_int32_t snd_fmtbest(u_int32_t fmt, u_int32_t *fmts) { u_int32_t best1, best2; u_int32_t score, score1, score2; if (snd_fmtvalid(fmt, fmts)) return fmt; best1 = snd_fmtbestchannel(fmt, fmts); best2 = snd_fmtbestbit(fmt, fmts); if (best1 != 0 && best2 != 0 && best1 != best2) { /*if (fmt & AFMT_STEREO)*/ if (AFMT_CHANNEL(fmt) > 1) return best1; else { score = score_val(snd_fmtscore(fmt)); score1 = score_val(snd_fmtscore(best1)); score2 = score_val(snd_fmtscore(best2)); if (score1 == score2 || score1 == score) return best1; else if (score2 == score) return best2; else if (score1 > score2) return best1; return best2; } } else if (best2 == 0) return best1; else return best2; } -void -feeder_printchain(struct pcm_feeder *head) -{ - struct pcm_feeder *f; - - printf("feeder chain (head @%p)\n", head); - f = head; - while (f != NULL) { - printf("%s @ %p\n", f->class->name, f); - f = f->source; - } - printf("[end]\n\n"); -} - -/*****************************************************************************/ - static int feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source) { struct snd_dbuf *src = source; int l, offset; KASSERT(count > 0, ("feed_root: count == 0")); if (++ch->feedcount == 0) ch->feedcount = 2; l = min(count, sndbuf_getready(src)); /* When recording only return as much data as available */ if (ch->direction == PCMDIR_REC) { sndbuf_dispose(src, buffer, l); return l; } offset = count - l; if (offset > 0) { if (snd_verbose > 3) printf("%s: (%s) %spending %d bytes " "(count=%d l=%d feed=%d)\n", __func__, (ch->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware", (ch->feedcount == 1) ? "pre" : "ap", offset, count, l, ch->feedcount); if (ch->feedcount == 1) { memset(buffer, sndbuf_zerodata(src->fmt), offset); if (l > 0) sndbuf_dispose(src, buffer + offset, l); else ch->feedcount--; } else { if (l > 0) sndbuf_dispose(src, buffer, l); memset(buffer + l, sndbuf_zerodata(src->fmt), offset); if (!(ch->flags & CHN_F_CLOSING)) ch->xruns++; } } else if (l > 0) sndbuf_dispose(src, buffer, l); return count; } static kobj_method_t feeder_root_methods[] = { KOBJMETHOD(feeder_feed, feed_root), KOBJMETHOD_END }; static struct feeder_class feeder_root_class = { .name = "feeder_root", .methods = feeder_root_methods, .size = sizeof(struct pcm_feeder), .type = FEEDER_ROOT, }; /* * Register the root feeder first so that pcm_addchan() and subsequent * functions can use it. */ SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register_root, &feeder_root_class); SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL); diff --git a/sys/dev/sound/pcm/feeder.h b/sys/dev/sound/pcm/feeder.h index d191edd201e9..f1c96d86fda0 100644 --- a/sys/dev/sound/pcm/feeder.h +++ b/sys/dev/sound/pcm/feeder.h @@ -1,182 +1,181 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2005-2009 Ariff Abdullah * Copyright (c) 1999 Cameron Grant * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ enum feeder_type { FEEDER_ROOT, FEEDER_FORMAT, FEEDER_MIXER, FEEDER_RATE, FEEDER_EQ, FEEDER_VOLUME, FEEDER_MATRIX, FEEDER_LAST, }; struct pcm_feederdesc { u_int32_t in, out; }; struct feeder_class { KOBJ_CLASS_FIELDS; enum feeder_type type; SLIST_ENTRY(feeder_class) link; }; struct pcm_feeder { KOBJ_FIELDS; int align; struct pcm_feederdesc *desc, desc_static; void *data; struct feeder_class *class; struct pcm_feeder *source, *parent; }; void feeder_register(void *p); struct feeder_class *feeder_getclass(u_int32_t type); u_int32_t snd_fmtscore(u_int32_t fmt); u_int32_t snd_fmtbestbit(u_int32_t fmt, u_int32_t *fmts); u_int32_t snd_fmtbestchannel(u_int32_t fmt, u_int32_t *fmts); u_int32_t snd_fmtbest(u_int32_t fmt, u_int32_t *fmts); int feeder_add(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc); void feeder_remove(struct pcm_channel *c); struct pcm_feeder *feeder_find(struct pcm_channel *c, u_int32_t type); -void feeder_printchain(struct pcm_feeder *head); int feeder_chain(struct pcm_channel *); #define FEEDER_DECLARE(feeder, ctype) \ static struct feeder_class feeder ## _class = { \ .name = #feeder, \ .methods = feeder ## _methods, \ .size = sizeof(struct pcm_feeder), \ .type = ctype, \ }; \ SYSINIT(feeder, SI_SUB_DRIVERS, SI_ORDER_ANY, feeder_register, \ &feeder ## _class) /* feeder_format */ enum { FEEDFORMAT_CHANNELS }; /* feeder_mixer */ enum { FEEDMIXER_CHANNELS }; /* feeder_rate */ enum { FEEDRATE_SRC, FEEDRATE_DST, FEEDRATE_QUALITY, FEEDRATE_CHANNELS }; #define FEEDRATE_RATEMIN 1 #define FEEDRATE_RATEMAX 2016000 /* 48000 * 42 */ #define FEEDRATE_MIN 1 #define FEEDRATE_MAX 0x7fffff /* sign 24bit ~ 8ghz ! */ #define FEEDRATE_ROUNDHZ 25 #define FEEDRATE_ROUNDHZ_MIN 0 #define FEEDRATE_ROUNDHZ_MAX 500 extern int feeder_rate_min; extern int feeder_rate_max; extern int feeder_rate_round; extern int feeder_rate_quality; /* feeder_eq */ enum { FEEDEQ_CHANNELS, FEEDEQ_RATE, FEEDEQ_TREBLE, FEEDEQ_BASS, FEEDEQ_PREAMP, FEEDEQ_STATE, FEEDEQ_DISABLE, FEEDEQ_ENABLE, FEEDEQ_BYPASS, FEEDEQ_UNKNOWN }; int feeder_eq_validrate(uint32_t); void feeder_eq_initsys(device_t); /* feeder_volume */ enum { FEEDVOLUME_CLASS, FEEDVOLUME_CHANNELS, FEEDVOLUME_STATE, FEEDVOLUME_ENABLE, FEEDVOLUME_BYPASS }; int feeder_volume_apply_matrix(struct pcm_feeder *, struct pcmchan_matrix *); /* feeder_matrix */ int feeder_matrix_default_id(uint32_t); struct pcmchan_matrix *feeder_matrix_default_channel_map(uint32_t); uint32_t feeder_matrix_default_format(uint32_t); int feeder_matrix_format_id(uint32_t); struct pcmchan_matrix *feeder_matrix_format_map(uint32_t); struct pcmchan_matrix *feeder_matrix_id_map(int); int feeder_matrix_setup(struct pcm_feeder *, struct pcmchan_matrix *, struct pcmchan_matrix *); int feeder_matrix_compare(struct pcmchan_matrix *, struct pcmchan_matrix *); /* 4Front OSS stuffs */ int feeder_matrix_oss_get_channel_order(struct pcmchan_matrix *, unsigned long long *); int feeder_matrix_oss_set_channel_order(struct pcmchan_matrix *, unsigned long long *); /* * By default, various feeders only deal with sign 16/32 bit native-endian * since it should provide the fastest processing path. Processing 8bit samples * is too noisy due to limited dynamic range, while 24bit is quite slow due to * unnatural per-byte read/write. However, for debugging purposes, ensuring * implementation correctness and torture test, the following can be defined: * * SND_FEEDER_MULTIFORMAT - Compile all type of converters, but force * 8bit samples to be converted to 16bit * native-endian for better dynamic range. * Process 24bit samples natively. * SND_FEEDER_FULL_MULTIFORMAT - Ditto, but process 8bit samples natively. */ #ifdef SND_FEEDER_FULL_MULTIFORMAT #undef SND_FEEDER_MULTIFORMAT #define SND_FEEDER_MULTIFORMAT 1 #endif