diff --git a/sys/dev/sound/pcm/feeder_eq.c b/sys/dev/sound/pcm/feeder_eq.c index df34ee44a2ab..c5f82a2f18fc 100644 --- a/sys/dev/sound/pcm/feeder_eq.c +++ b/sys/dev/sound/pcm/feeder_eq.c @@ -1,698 +1,694 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2008-2009 Ariff Abdullah * 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. */ /* * feeder_eq: Parametric (compile time) Software Equalizer. Though accidental, * it proves good enough for educational and general consumption. * * "Cookbook formulae for audio EQ biquad filter coefficients" * by Robert Bristow-Johnson * - http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt */ #ifdef _KERNEL #ifdef HAVE_KERNEL_OPTION_HEADERS #include "opt_snd.h" #endif #include #include #include "feeder_if.h" #define SND_USE_FXDIV #include "snd_fxdiv_gen.h" #endif #include "feeder_eq_gen.h" #define FEEDEQ_LEVELS \ (((FEEDEQ_GAIN_MAX - FEEDEQ_GAIN_MIN) * \ (FEEDEQ_GAIN_DIV / FEEDEQ_GAIN_STEP)) + 1) #define FEEDEQ_L2GAIN(v) \ ((int)min(((v) * FEEDEQ_LEVELS) / 100, FEEDEQ_LEVELS - 1)) #define FEEDEQ_PREAMP_IPART(x) (abs(x) >> FEEDEQ_GAIN_SHIFT) #define FEEDEQ_PREAMP_FPART(x) (abs(x) & FEEDEQ_GAIN_FMASK) #define FEEDEQ_PREAMP_SIGNVAL(x) ((x) < 0 ? -1 : 1) #define FEEDEQ_PREAMP_SIGNMARK(x) (((x) < 0) ? '-' : '+') #define FEEDEQ_PREAMP_IMIN -192 #define FEEDEQ_PREAMP_IMAX 192 #define FEEDEQ_PREAMP_FMIN 0 #define FEEDEQ_PREAMP_FMAX 9 #define FEEDEQ_PREAMP_INVALID INT_MAX #define FEEDEQ_IF2PREAMP(i, f) \ ((abs(i) << FEEDEQ_GAIN_SHIFT) | \ (((abs(f) / FEEDEQ_GAIN_STEP) * FEEDEQ_GAIN_STEP) & \ FEEDEQ_GAIN_FMASK)) #define FEEDEQ_PREAMP_MIN \ (FEEDEQ_PREAMP_SIGNVAL(FEEDEQ_GAIN_MIN) * \ FEEDEQ_IF2PREAMP(FEEDEQ_GAIN_MIN, 0)) #define FEEDEQ_PREAMP_MAX \ (FEEDEQ_PREAMP_SIGNVAL(FEEDEQ_GAIN_MAX) * \ FEEDEQ_IF2PREAMP(FEEDEQ_GAIN_MAX, 0)) #define FEEDEQ_PREAMP_DEFAULT FEEDEQ_IF2PREAMP(0, 0) #define FEEDEQ_PREAMP2IDX(v) \ ((int32_t)((FEEDEQ_GAIN_MAX * (FEEDEQ_GAIN_DIV / \ FEEDEQ_GAIN_STEP)) + (FEEDEQ_PREAMP_SIGNVAL(v) * \ FEEDEQ_PREAMP_IPART(v) * (FEEDEQ_GAIN_DIV / \ FEEDEQ_GAIN_STEP)) + (FEEDEQ_PREAMP_SIGNVAL(v) * \ (FEEDEQ_PREAMP_FPART(v) / FEEDEQ_GAIN_STEP)))) static int feeder_eq_exact_rate = 0; #ifdef _KERNEL static char feeder_eq_presets[] = FEEDER_EQ_PRESETS; SYSCTL_STRING(_hw_snd, OID_AUTO, feeder_eq_presets, CTLFLAG_RD, &feeder_eq_presets, 0, "compile-time eq presets"); SYSCTL_INT(_hw_snd, OID_AUTO, feeder_eq_exact_rate, CTLFLAG_RWTUN, &feeder_eq_exact_rate, 0, "force exact rate validation"); #endif struct feed_eq_info; typedef void (*feed_eq_t)(struct feed_eq_info *, uint8_t *, uint32_t); struct feed_eq_tone { intpcm_t o1[SND_CHN_MAX]; intpcm_t o2[SND_CHN_MAX]; intpcm_t i1[SND_CHN_MAX]; intpcm_t i2[SND_CHN_MAX]; int gain; }; struct feed_eq_info { struct feed_eq_tone treble; struct feed_eq_tone bass; struct feed_eq_coeff *coeff; feed_eq_t biquad; uint32_t channels; uint32_t rate; uint32_t align; int32_t preamp; int state; }; #if !defined(_KERNEL) && defined(FEEDEQ_ERR_CLIP) #define FEEDEQ_ERR_CLIP_CHECK(t, v) do { \ if ((v) < PCM_S32_MIN || (v) > PCM_S32_MAX) \ errx(1, "\n\n%s(): ["#t"] Sample clipping: %jd\n", \ __func__, (intmax_t)(v)); \ } while (0) #else #define FEEDEQ_ERR_CLIP_CHECK(...) #endif -#define FEEDEQ_CLAMP(v) (((v) > PCM_S32_MAX) ? PCM_S32_MAX : \ - (((v) < PCM_S32_MIN) ? PCM_S32_MIN : \ - (v))) - #define FEEDEQ_DECLARE(SIGN, BIT, ENDIAN) \ static void \ feed_eq_biquad_##SIGN##BIT##ENDIAN(struct feed_eq_info *info, \ uint8_t *dst, uint32_t count) \ { \ struct feed_eq_coeff_tone *treble, *bass; \ intpcm64_t w; \ intpcm_t v; \ uint32_t i, j; \ int32_t pmul, pshift; \ \ pmul = feed_eq_preamp[info->preamp].mul; \ pshift = feed_eq_preamp[info->preamp].shift; \ \ if (info->state == FEEDEQ_DISABLE) { \ j = count * info->channels; \ dst += j * PCM_##BIT##_BPS; \ do { \ dst -= PCM_##BIT##_BPS; \ v = pcm_sample_read(dst, AFMT_##SIGN##BIT##_##ENDIAN); \ v = ((intpcm64_t)pmul * v) >> pshift; \ pcm_sample_write(dst, v, AFMT_##SIGN##BIT##_##ENDIAN); \ } while (--j != 0); \ \ return; \ } \ \ treble = &(info->coeff[info->treble.gain].treble); \ bass = &(info->coeff[info->bass.gain].bass); \ \ do { \ i = 0; \ j = info->channels; \ do { \ v = pcm_sample_read_norm(dst, \ AFMT_##SIGN##BIT##_##ENDIAN); \ v = ((intpcm64_t)pmul * v) >> pshift; \ \ w = (intpcm64_t)v * treble->b0; \ w += (intpcm64_t)info->treble.i1[i] * treble->b1; \ w += (intpcm64_t)info->treble.i2[i] * treble->b2; \ w -= (intpcm64_t)info->treble.o1[i] * treble->a1; \ w -= (intpcm64_t)info->treble.o2[i] * treble->a2; \ info->treble.i2[i] = info->treble.i1[i]; \ info->treble.i1[i] = v; \ info->treble.o2[i] = info->treble.o1[i]; \ w >>= FEEDEQ_COEFF_SHIFT; \ FEEDEQ_ERR_CLIP_CHECK(treble, w); \ - v = FEEDEQ_CLAMP(w); \ + v = pcm_clamp(w, AFMT_S32_NE); \ info->treble.o1[i] = v; \ \ w = (intpcm64_t)v * bass->b0; \ w += (intpcm64_t)info->bass.i1[i] * bass->b1; \ w += (intpcm64_t)info->bass.i2[i] * bass->b2; \ w -= (intpcm64_t)info->bass.o1[i] * bass->a1; \ w -= (intpcm64_t)info->bass.o2[i] * bass->a2; \ info->bass.i2[i] = info->bass.i1[i]; \ info->bass.i1[i] = v; \ info->bass.o2[i] = info->bass.o1[i]; \ w >>= FEEDEQ_COEFF_SHIFT; \ FEEDEQ_ERR_CLIP_CHECK(bass, w); \ - v = FEEDEQ_CLAMP(w); \ + v = pcm_clamp(w, AFMT_S32_NE); \ info->bass.o1[i] = v; \ \ pcm_sample_write_norm(dst, v, \ AFMT_##SIGN##BIT##_##ENDIAN); \ dst += PCM_##BIT##_BPS; \ i++; \ } while (--j != 0); \ } while (--count != 0); \ } #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) FEEDEQ_DECLARE(S, 16, LE) FEEDEQ_DECLARE(S, 32, LE) #endif #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) FEEDEQ_DECLARE(S, 16, BE) FEEDEQ_DECLARE(S, 32, BE) #endif #ifdef SND_FEEDER_MULTIFORMAT FEEDEQ_DECLARE(S, 8, NE) FEEDEQ_DECLARE(S, 24, LE) FEEDEQ_DECLARE(S, 24, BE) FEEDEQ_DECLARE(U, 8, NE) FEEDEQ_DECLARE(U, 16, LE) FEEDEQ_DECLARE(U, 24, LE) FEEDEQ_DECLARE(U, 32, LE) FEEDEQ_DECLARE(U, 16, BE) FEEDEQ_DECLARE(U, 24, BE) FEEDEQ_DECLARE(U, 32, BE) #endif #define FEEDEQ_ENTRY(SIGN, BIT, ENDIAN) \ { \ AFMT_##SIGN##BIT##_##ENDIAN, \ feed_eq_biquad_##SIGN##BIT##ENDIAN \ } static const struct { uint32_t format; feed_eq_t biquad; } feed_eq_biquad_tab[] = { #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) FEEDEQ_ENTRY(S, 16, LE), FEEDEQ_ENTRY(S, 32, LE), #endif #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) FEEDEQ_ENTRY(S, 16, BE), FEEDEQ_ENTRY(S, 32, BE), #endif #ifdef SND_FEEDER_MULTIFORMAT FEEDEQ_ENTRY(S, 8, NE), FEEDEQ_ENTRY(S, 24, LE), FEEDEQ_ENTRY(S, 24, BE), FEEDEQ_ENTRY(U, 8, NE), FEEDEQ_ENTRY(U, 16, LE), FEEDEQ_ENTRY(U, 24, LE), FEEDEQ_ENTRY(U, 32, LE), FEEDEQ_ENTRY(U, 16, BE), FEEDEQ_ENTRY(U, 24, BE), FEEDEQ_ENTRY(U, 32, BE) #endif }; #define FEEDEQ_BIQUAD_TAB_SIZE \ ((int32_t)(sizeof(feed_eq_biquad_tab) / sizeof(feed_eq_biquad_tab[0]))) static struct feed_eq_coeff * feed_eq_coeff_rate(uint32_t rate) { uint32_t spd, threshold; int i; if (rate < FEEDEQ_RATE_MIN || rate > FEEDEQ_RATE_MAX) return (NULL); /* * Not all rates are supported. Choose the best rate that we can to * allow 'sloppy' conversion. Good enough for naive listeners. */ for (i = 0; i < FEEDEQ_TAB_SIZE; i++) { spd = feed_eq_tab[i].rate; threshold = spd + ((i < (FEEDEQ_TAB_SIZE - 1) && feed_eq_tab[i + 1].rate > spd) ? ((feed_eq_tab[i + 1].rate - spd) >> 1) : 0); if (rate == spd || (feeder_eq_exact_rate == 0 && rate <= threshold)) return (feed_eq_tab[i].coeff); } return (NULL); } int feeder_eq_validrate(uint32_t rate) { if (feed_eq_coeff_rate(rate) != NULL) return (1); return (0); } static void feed_eq_reset(struct feed_eq_info *info) { uint32_t i; for (i = 0; i < info->channels; i++) { info->treble.i1[i] = 0; info->treble.i2[i] = 0; info->treble.o1[i] = 0; info->treble.o2[i] = 0; info->bass.i1[i] = 0; info->bass.i2[i] = 0; info->bass.o1[i] = 0; info->bass.o2[i] = 0; } } static int feed_eq_setup(struct feed_eq_info *info) { info->coeff = feed_eq_coeff_rate(info->rate); if (info->coeff == NULL) return (EINVAL); feed_eq_reset(info); return (0); } static int feed_eq_init(struct pcm_feeder *f) { struct feed_eq_info *info; feed_eq_t biquad_op; int i; if (f->desc->in != f->desc->out) return (EINVAL); biquad_op = NULL; for (i = 0; i < FEEDEQ_BIQUAD_TAB_SIZE && biquad_op == NULL; i++) { if (AFMT_ENCODING(f->desc->in) == feed_eq_biquad_tab[i].format) biquad_op = feed_eq_biquad_tab[i].biquad; } if (biquad_op == NULL) return (EINVAL); info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT | M_ZERO); if (info == NULL) return (ENOMEM); info->channels = AFMT_CHANNEL(f->desc->in); info->align = info->channels * AFMT_BPS(f->desc->in); info->rate = FEEDEQ_RATE_MIN; info->treble.gain = FEEDEQ_L2GAIN(50); info->bass.gain = FEEDEQ_L2GAIN(50); info->preamp = FEEDEQ_PREAMP2IDX(FEEDEQ_PREAMP_DEFAULT); info->state = FEEDEQ_UNKNOWN; info->biquad = biquad_op; f->data = info; return (feed_eq_setup(info)); } static int feed_eq_set(struct pcm_feeder *f, int what, int value) { struct feed_eq_info *info; info = f->data; switch (what) { case FEEDEQ_CHANNELS: if (value < SND_CHN_MIN || value > SND_CHN_MAX) return (EINVAL); info->channels = (uint32_t)value; info->align = info->channels * AFMT_BPS(f->desc->in); feed_eq_reset(info); break; case FEEDEQ_RATE: if (feeder_eq_validrate(value) == 0) return (EINVAL); info->rate = (uint32_t)value; if (info->state == FEEDEQ_UNKNOWN) info->state = FEEDEQ_ENABLE; return (feed_eq_setup(info)); break; case FEEDEQ_TREBLE: case FEEDEQ_BASS: if (value < 0 || value > 100) return (EINVAL); if (what == FEEDEQ_TREBLE) info->treble.gain = FEEDEQ_L2GAIN(value); else info->bass.gain = FEEDEQ_L2GAIN(value); break; case FEEDEQ_PREAMP: if (value < FEEDEQ_PREAMP_MIN || value > FEEDEQ_PREAMP_MAX) return (EINVAL); info->preamp = FEEDEQ_PREAMP2IDX(value); break; case FEEDEQ_STATE: if (!(value == FEEDEQ_BYPASS || value == FEEDEQ_ENABLE || value == FEEDEQ_DISABLE)) return (EINVAL); info->state = value; feed_eq_reset(info); break; default: return (EINVAL); break; } return (0); } static int feed_eq_free(struct pcm_feeder *f) { struct feed_eq_info *info; info = f->data; if (info != NULL) free(info, M_DEVBUF); f->data = NULL; return (0); } static int feed_eq_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b, uint32_t count, void *source) { struct feed_eq_info *info; uint32_t j; uint8_t *dst; info = f->data; /* * 3 major states: * FEEDEQ_BYPASS - Bypass entirely, nothing happened. * FEEDEQ_ENABLE - Preamp+biquad filtering. * FEEDEQ_DISABLE - Preamp only. */ if (info->state == FEEDEQ_BYPASS) return (FEEDER_FEED(f->source, c, b, count, source)); dst = b; count = SND_FXROUND(count, info->align); do { if (count < info->align) break; j = SND_FXDIV(FEEDER_FEED(f->source, c, dst, count, source), info->align); if (j == 0) break; info->biquad(info, dst, j); j *= info->align; dst += j; count -= j; } while (count != 0); return (dst - b); } static struct pcm_feederdesc feeder_eq_desc[] = { { FEEDER_EQ, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } }; static kobj_method_t feeder_eq_methods[] = { KOBJMETHOD(feeder_init, feed_eq_init), KOBJMETHOD(feeder_free, feed_eq_free), KOBJMETHOD(feeder_set, feed_eq_set), KOBJMETHOD(feeder_feed, feed_eq_feed), KOBJMETHOD_END }; FEEDER_DECLARE(feeder_eq, NULL); static int32_t feed_eq_scan_preamp_arg(const char *s) { int r, i, f; size_t len; char buf[32]; bzero(buf, sizeof(buf)); /* XXX kind of ugly, but works for now.. */ r = sscanf(s, "%d.%d", &i, &f); if (r == 1 && !(i < FEEDEQ_PREAMP_IMIN || i > FEEDEQ_PREAMP_IMAX)) { snprintf(buf, sizeof(buf), "%c%d", FEEDEQ_PREAMP_SIGNMARK(i), abs(i)); f = 0; } else if (r == 2 && !(i < FEEDEQ_PREAMP_IMIN || i > FEEDEQ_PREAMP_IMAX || f < FEEDEQ_PREAMP_FMIN || f > FEEDEQ_PREAMP_FMAX)) snprintf(buf, sizeof(buf), "%c%d.%d", FEEDEQ_PREAMP_SIGNMARK(i), abs(i), f); else return (FEEDEQ_PREAMP_INVALID); len = strlen(s); if (len > 2 && strcasecmp(s + len - 2, "dB") == 0) strlcat(buf, "dB", sizeof(buf)); if (i == 0 && *s == '-') *buf = '-'; if (strcasecmp(buf + ((*s >= '0' && *s <= '9') ? 1 : 0), s) != 0) return (FEEDEQ_PREAMP_INVALID); while ((f / FEEDEQ_GAIN_DIV) > 0) f /= FEEDEQ_GAIN_DIV; return (((i < 0 || *buf == '-') ? -1 : 1) * FEEDEQ_IF2PREAMP(i, f)); } #ifdef _KERNEL static int sysctl_dev_pcm_eq(SYSCTL_HANDLER_ARGS) { struct snddev_info *d; struct pcm_channel *c; struct pcm_feeder *f; int err, val, oval; d = oidp->oid_arg1; if (!PCM_REGISTERED(d)) return (ENODEV); PCM_LOCK(d); PCM_WAIT(d); if (d->flags & SD_F_EQ_BYPASSED) val = 2; else if (d->flags & SD_F_EQ_ENABLED) val = 1; else val = 0; PCM_ACQUIRE(d); PCM_UNLOCK(d); oval = val; err = sysctl_handle_int(oidp, &val, 0, req); if (err == 0 && req->newptr != NULL && val != oval) { if (!(val == 0 || val == 1 || val == 2)) { PCM_RELEASE_QUICK(d); return (EINVAL); } PCM_LOCK(d); d->flags &= ~(SD_F_EQ_ENABLED | SD_F_EQ_BYPASSED); if (val == 2) { val = FEEDEQ_BYPASS; d->flags |= SD_F_EQ_BYPASSED; } else if (val == 1) { val = FEEDEQ_ENABLE; d->flags |= SD_F_EQ_ENABLED; } else val = FEEDEQ_DISABLE; CHN_FOREACH(c, d, channels.pcm.busy) { CHN_LOCK(c); f = feeder_find(c, FEEDER_EQ); if (f != NULL) (void)FEEDER_SET(f, FEEDEQ_STATE, val); CHN_UNLOCK(c); } PCM_RELEASE(d); PCM_UNLOCK(d); } else PCM_RELEASE_QUICK(d); return (err); } static int sysctl_dev_pcm_eq_preamp(SYSCTL_HANDLER_ARGS) { struct snddev_info *d; struct pcm_channel *c; struct pcm_feeder *f; int err, val, oval; char buf[32]; d = oidp->oid_arg1; if (!PCM_REGISTERED(d)) return (ENODEV); PCM_LOCK(d); PCM_WAIT(d); val = d->eqpreamp; bzero(buf, sizeof(buf)); (void)snprintf(buf, sizeof(buf), "%c%d.%ddB", FEEDEQ_PREAMP_SIGNMARK(val), FEEDEQ_PREAMP_IPART(val), FEEDEQ_PREAMP_FPART(val)); PCM_ACQUIRE(d); PCM_UNLOCK(d); oval = val; err = sysctl_handle_string(oidp, buf, sizeof(buf), req); if (err == 0 && req->newptr != NULL) { val = feed_eq_scan_preamp_arg(buf); if (val == FEEDEQ_PREAMP_INVALID) { PCM_RELEASE_QUICK(d); return (EINVAL); } PCM_LOCK(d); if (val != oval) { if (val < FEEDEQ_PREAMP_MIN) val = FEEDEQ_PREAMP_MIN; else if (val > FEEDEQ_PREAMP_MAX) val = FEEDEQ_PREAMP_MAX; d->eqpreamp = val; CHN_FOREACH(c, d, channels.pcm.busy) { CHN_LOCK(c); f = feeder_find(c, FEEDER_EQ); if (f != NULL) (void)FEEDER_SET(f, FEEDEQ_PREAMP, val); CHN_UNLOCK(c); } } PCM_RELEASE(d); PCM_UNLOCK(d); } else PCM_RELEASE_QUICK(d); return (err); } void feeder_eq_initsys(device_t dev) { struct snddev_info *d; const char *preamp; char buf[64]; d = device_get_softc(dev); if (!(resource_string_value(device_get_name(dev), device_get_unit(dev), "eq_preamp", &preamp) == 0 && (d->eqpreamp = feed_eq_scan_preamp_arg(preamp)) != FEEDEQ_PREAMP_INVALID)) d->eqpreamp = FEEDEQ_PREAMP_DEFAULT; if (d->eqpreamp < FEEDEQ_PREAMP_MIN) d->eqpreamp = FEEDEQ_PREAMP_MIN; else if (d->eqpreamp > FEEDEQ_PREAMP_MAX) d->eqpreamp = FEEDEQ_PREAMP_MAX; SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "eq", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, d, sizeof(d), sysctl_dev_pcm_eq, "I", "Bass/Treble Equalizer (0=disable, 1=enable, 2=bypass)"); (void)snprintf(buf, sizeof(buf), "Bass/Treble Equalizer Preamp " "(-/+ %d.0dB , %d.%ddB step)", FEEDEQ_GAIN_MAX, FEEDEQ_GAIN_STEP / FEEDEQ_GAIN_DIV, FEEDEQ_GAIN_STEP - ((FEEDEQ_GAIN_STEP / FEEDEQ_GAIN_DIV) * FEEDEQ_GAIN_DIV)); SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "eq_preamp", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, d, sizeof(d), sysctl_dev_pcm_eq_preamp, "A", buf); } #endif diff --git a/sys/dev/sound/pcm/feeder_mixer.c b/sys/dev/sound/pcm/feeder_mixer.c index 7640b09400ed..9a7d75198692 100644 --- a/sys/dev/sound/pcm/feeder_mixer.c +++ b/sys/dev/sound/pcm/feeder_mixer.c @@ -1,405 +1,405 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2008-2009 Ariff Abdullah * 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. */ #ifdef _KERNEL #ifdef HAVE_KERNEL_OPTION_HEADERS #include "opt_snd.h" #endif #include #include #include #include "feeder_if.h" #define SND_USE_FXDIV #include "snd_fxdiv_gen.h" #endif #undef SND_FEEDER_MULTIFORMAT #define SND_FEEDER_MULTIFORMAT 1 typedef void (*feed_mixer_t)(uint8_t *, uint8_t *, uint32_t); #define FEEDMIXER_DECLARE(SIGN, BIT, ENDIAN) \ static void \ feed_mixer_##SIGN##BIT##ENDIAN(uint8_t *src, uint8_t *dst, \ uint32_t count) \ { \ intpcm##BIT##_t z; \ intpcm_t x, y; \ \ src += count; \ dst += count; \ \ do { \ src -= PCM_##BIT##_BPS; \ dst -= PCM_##BIT##_BPS; \ count -= PCM_##BIT##_BPS; \ x = pcm_sample_read_calc(src, \ AFMT_##SIGN##BIT##_##ENDIAN); \ y = pcm_sample_read_calc(dst, \ AFMT_##SIGN##BIT##_##ENDIAN); \ z = INTPCM##BIT##_T(x) + y; \ - x = PCM_CLAMP_##SIGN##BIT(z); \ + x = pcm_clamp_calc(z, AFMT_##SIGN##BIT##_##ENDIAN); \ pcm_sample_write(dst, x, \ AFMT_##SIGN##BIT##_##ENDIAN); \ } while (count != 0); \ } #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) FEEDMIXER_DECLARE(S, 16, LE) FEEDMIXER_DECLARE(S, 32, LE) #endif #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) FEEDMIXER_DECLARE(S, 16, BE) FEEDMIXER_DECLARE(S, 32, BE) #endif #ifdef SND_FEEDER_MULTIFORMAT FEEDMIXER_DECLARE(S, 8, NE) FEEDMIXER_DECLARE(S, 24, LE) FEEDMIXER_DECLARE(S, 24, BE) FEEDMIXER_DECLARE(U, 8, NE) FEEDMIXER_DECLARE(U, 16, LE) FEEDMIXER_DECLARE(U, 24, LE) FEEDMIXER_DECLARE(U, 32, LE) FEEDMIXER_DECLARE(U, 16, BE) FEEDMIXER_DECLARE(U, 24, BE) FEEDMIXER_DECLARE(U, 32, BE) #endif struct feed_mixer_info { uint32_t format; int bps; feed_mixer_t mix; }; #define FEEDMIXER_ENTRY(SIGN, BIT, ENDIAN) \ { \ AFMT_##SIGN##BIT##_##ENDIAN, PCM_##BIT##_BPS, \ feed_mixer_##SIGN##BIT##ENDIAN \ } static struct feed_mixer_info feed_mixer_info_tab[] = { FEEDMIXER_ENTRY(S, 8, NE), #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) FEEDMIXER_ENTRY(S, 16, LE), FEEDMIXER_ENTRY(S, 32, LE), #endif #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) FEEDMIXER_ENTRY(S, 16, BE), FEEDMIXER_ENTRY(S, 32, BE), #endif #ifdef SND_FEEDER_MULTIFORMAT FEEDMIXER_ENTRY(S, 24, LE), FEEDMIXER_ENTRY(S, 24, BE), FEEDMIXER_ENTRY(U, 8, NE), FEEDMIXER_ENTRY(U, 16, LE), FEEDMIXER_ENTRY(U, 24, LE), FEEDMIXER_ENTRY(U, 32, LE), FEEDMIXER_ENTRY(U, 16, BE), FEEDMIXER_ENTRY(U, 24, BE), FEEDMIXER_ENTRY(U, 32, BE), #endif { AFMT_AC3, PCM_16_BPS, NULL }, { AFMT_MU_LAW, PCM_8_BPS, feed_mixer_U8NE }, /* dummy */ { AFMT_A_LAW, PCM_8_BPS, feed_mixer_U8NE } /* dummy */ }; #define FEEDMIXER_TAB_SIZE ((int32_t) \ (sizeof(feed_mixer_info_tab) / \ sizeof(feed_mixer_info_tab[0]))) #define FEEDMIXER_DATA(i, c) ((void *) \ ((uintptr_t)((((i) & 0x1f) << 7) | \ ((c) & 0x7f)))) #define FEEDMIXER_INFOIDX(d) ((uint32_t)((uintptr_t)(d) >> 7) & 0x1f) #define FEEDMIXER_CHANNELS(d) ((uint32_t)((uintptr_t)(d)) & 0x7f) static int feed_mixer_init(struct pcm_feeder *f) { int i; if (f->desc->in != f->desc->out) return (EINVAL); for (i = 0; i < FEEDMIXER_TAB_SIZE; i++) { if (AFMT_ENCODING(f->desc->in) == feed_mixer_info_tab[i].format) { f->data = FEEDMIXER_DATA(i, AFMT_CHANNEL(f->desc->in)); return (0); } } return (EINVAL); } static int feed_mixer_set(struct pcm_feeder *f, int what, int value) { switch (what) { case FEEDMIXER_CHANNELS: if (value < SND_CHN_MIN || value > SND_CHN_MAX) return (EINVAL); f->data = FEEDMIXER_DATA(FEEDMIXER_INFOIDX(f->data), value); break; default: return (EINVAL); break; } return (0); } static __inline int feed_mixer_rec(struct pcm_channel *c) { struct pcm_channel *ch; struct snd_dbuf *b, *bs; uint32_t cnt, maxfeed; int rdy; /* * Reset ready and moving pointer. We're not using bufsoft * anywhere since its sole purpose is to become the primary * distributor for the recorded buffer and also as an interrupt * threshold progress indicator. */ b = c->bufsoft; b->rp = 0; b->rl = 0; cnt = sndbuf_getsize(b); maxfeed = SND_FXROUND(SND_FXDIV_MAX, sndbuf_getalign(b)); do { cnt = FEEDER_FEED(c->feeder->source, c, b->tmpbuf, min(cnt, maxfeed), c->bufhard); if (cnt != 0) { sndbuf_acquire(b, b->tmpbuf, cnt); cnt = sndbuf_getfree(b); } } while (cnt != 0); /* Not enough data */ if (b->rl < sndbuf_getalign(b)) { b->rl = 0; return (0); } /* * Keep track of ready and moving pointer since we will use * bufsoft over and over again, pretending nothing has happened. */ rdy = b->rl; CHN_FOREACH(ch, c, children.busy) { CHN_LOCK(ch); if (CHN_STOPPED(ch) || (ch->flags & CHN_F_DIRTY)) { CHN_UNLOCK(ch); continue; } #ifdef SND_DEBUG if ((c->flags & CHN_F_DIRTY) && VCHAN_SYNC_REQUIRED(ch)) { if (vchan_sync(ch) != 0) { CHN_UNLOCK(ch); continue; } } #endif bs = ch->bufsoft; if (ch->flags & CHN_F_MMAP) sndbuf_dispose(bs, NULL, sndbuf_getready(bs)); cnt = sndbuf_getfree(bs); if (cnt < sndbuf_getalign(bs)) { CHN_UNLOCK(ch); continue; } maxfeed = SND_FXROUND(SND_FXDIV_MAX, sndbuf_getalign(bs)); do { cnt = FEEDER_FEED(ch->feeder, ch, bs->tmpbuf, min(cnt, maxfeed), b); if (cnt != 0) { sndbuf_acquire(bs, bs->tmpbuf, cnt); cnt = sndbuf_getfree(bs); } } while (cnt != 0); /* * Not entirely flushed out... */ if (b->rl != 0) ch->xruns++; CHN_UNLOCK(ch); /* * Rewind buffer position for next virtual channel. */ b->rp = 0; b->rl = rdy; } /* * Set ready pointer to indicate that our children are ready * to be woken up, also as an interrupt threshold progress * indicator. */ b->rl = 1; c->flags &= ~CHN_F_DIRTY; /* * Return 0 to bail out early from sndbuf_feed() loop. * No need to increase feedcount counter since part of this * feeder chains already include feed_root(). */ return (0); } static int feed_mixer_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b, uint32_t count, void *source) { struct feed_mixer_info *info; struct snd_dbuf *src = source; struct pcm_channel *ch; uint32_t cnt, mcnt, rcnt, sz; int passthrough; uint8_t *tmp; if (c->direction == PCMDIR_REC) return (feed_mixer_rec(c)); sz = sndbuf_getsize(src); if (sz < count) count = sz; info = &feed_mixer_info_tab[FEEDMIXER_INFOIDX(f->data)]; sz = info->bps * FEEDMIXER_CHANNELS(f->data); count = SND_FXROUND(count, sz); if (count < sz) return (0); /* * We are going to use our source as a temporary buffer since it's * got no other purpose. We obtain our data by traversing the channel * list of children and calling mixer function to mix count bytes from * each into our destination buffer, b. */ tmp = sndbuf_getbuf(src); rcnt = 0; mcnt = 0; passthrough = 0; /* 'passthrough' / 'exclusive' marker */ CHN_FOREACH(ch, c, children.busy) { CHN_LOCK(ch); if (CHN_STOPPED(ch) || (ch->flags & CHN_F_DIRTY)) { CHN_UNLOCK(ch); continue; } #ifdef SND_DEBUG if ((c->flags & CHN_F_DIRTY) && VCHAN_SYNC_REQUIRED(ch)) { if (vchan_sync(ch) != 0) { CHN_UNLOCK(ch); continue; } } #endif if ((ch->flags & CHN_F_MMAP) && !(ch->flags & CHN_F_CLOSING)) sndbuf_acquire(ch->bufsoft, NULL, sndbuf_getfree(ch->bufsoft)); if (info->mix == NULL) { /* * Passthrough. Dump the first digital/passthrough * channel into destination buffer, and the rest into * nothingness (mute effect). */ if (passthrough == 0 && (ch->format & AFMT_PASSTHROUGH)) { rcnt = SND_FXROUND(FEEDER_FEED(ch->feeder, ch, b, count, ch->bufsoft), sz); passthrough = 1; } else FEEDER_FEED(ch->feeder, ch, tmp, count, ch->bufsoft); } else if (c->flags & CHN_F_EXCLUSIVE) { /* * Exclusive. Dump the first 'exclusive' channel into * destination buffer, and the rest into nothingness * (mute effect). */ if (passthrough == 0 && (ch->flags & CHN_F_EXCLUSIVE)) { rcnt = SND_FXROUND(FEEDER_FEED(ch->feeder, ch, b, count, ch->bufsoft), sz); passthrough = 1; } else FEEDER_FEED(ch->feeder, ch, tmp, count, ch->bufsoft); } else { if (rcnt == 0) { rcnt = SND_FXROUND(FEEDER_FEED(ch->feeder, ch, b, count, ch->bufsoft), sz); mcnt = count - rcnt; } else { cnt = SND_FXROUND(FEEDER_FEED(ch->feeder, ch, tmp, count, ch->bufsoft), sz); if (cnt != 0) { if (mcnt != 0) { memset(b + rcnt, sndbuf_zerodata( f->desc->out), mcnt); mcnt = 0; } info->mix(tmp, b, cnt); if (cnt > rcnt) rcnt = cnt; } } } CHN_UNLOCK(ch); } if (++c->feedcount == 0) c->feedcount = 2; c->flags &= ~CHN_F_DIRTY; return (rcnt); } static struct pcm_feederdesc feeder_mixer_desc[] = { { FEEDER_MIXER, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } }; static kobj_method_t feeder_mixer_methods[] = { KOBJMETHOD(feeder_init, feed_mixer_init), KOBJMETHOD(feeder_set, feed_mixer_set), KOBJMETHOD(feeder_feed, feed_mixer_feed), KOBJMETHOD_END }; FEEDER_DECLARE(feeder_mixer, NULL); diff --git a/sys/dev/sound/pcm/feeder_rate.c b/sys/dev/sound/pcm/feeder_rate.c index e1b4076e248e..1610211ff5f5 100644 --- a/sys/dev/sound/pcm/feeder_rate.c +++ b/sys/dev/sound/pcm/feeder_rate.c @@ -1,1720 +1,1716 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2005-2009 Ariff Abdullah * 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. */ /* * feeder_rate: (Codename: Z Resampler), which means any effort to create * future replacement for this resampler are simply absurd unless * the world decide to add new alphabet after Z. * * FreeBSD bandlimited sinc interpolator, technically based on * "Digital Audio Resampling" by Julius O. Smith III * - http://ccrma.stanford.edu/~jos/resample/ * * The Good: * + all out fixed point integer operations, no soft-float or anything like * that. * + classic polyphase converters with high quality coefficient's polynomial * interpolators. * + fast, faster, or the fastest of its kind. * + compile time configurable. * + etc etc.. * * The Bad: * - The z, z_, and Z_ . Due to mental block (or maybe just 0x7a69), I * couldn't think of anything simpler than that (feeder_rate_xxx is just * too long). Expect possible clashes with other zitizens (any?). */ #ifdef _KERNEL #ifdef HAVE_KERNEL_OPTION_HEADERS #include "opt_snd.h" #endif #include #include #include "feeder_if.h" #define SND_USE_FXDIV #include "snd_fxdiv_gen.h" #endif #include "feeder_rate_gen.h" #if !defined(_KERNEL) && defined(SND_DIAGNOSTIC) #undef Z_DIAGNOSTIC #define Z_DIAGNOSTIC 1 #elif defined(_KERNEL) #undef Z_DIAGNOSTIC #endif #ifndef Z_QUALITY_DEFAULT #define Z_QUALITY_DEFAULT Z_QUALITY_LINEAR #endif #define Z_RESERVOIR 2048 #define Z_RESERVOIR_MAX 131072 #define Z_SINC_MAX 0x3fffff #define Z_SINC_DOWNMAX 48 /* 384000 / 8000 */ #ifdef _KERNEL #define Z_POLYPHASE_MAX 183040 /* 286 taps, 640 phases */ #else #define Z_POLYPHASE_MAX 1464320 /* 286 taps, 5120 phases */ #endif #define Z_RATE_DEFAULT 48000 #define Z_RATE_MIN FEEDRATE_RATEMIN #define Z_RATE_MAX FEEDRATE_RATEMAX #define Z_ROUNDHZ FEEDRATE_ROUNDHZ #define Z_ROUNDHZ_MIN FEEDRATE_ROUNDHZ_MIN #define Z_ROUNDHZ_MAX FEEDRATE_ROUNDHZ_MAX #define Z_RATE_SRC FEEDRATE_SRC #define Z_RATE_DST FEEDRATE_DST #define Z_RATE_QUALITY FEEDRATE_QUALITY #define Z_RATE_CHANNELS FEEDRATE_CHANNELS #define Z_PARANOID 1 #define Z_MULTIFORMAT 1 #ifdef _KERNEL #undef Z_USE_ALPHADRIFT #define Z_USE_ALPHADRIFT 1 #endif #define Z_FACTOR_MIN 1 #define Z_FACTOR_MAX Z_MASK #define Z_FACTOR_SAFE(v) (!((v) < Z_FACTOR_MIN || (v) > Z_FACTOR_MAX)) struct z_info; typedef void (*z_resampler_t)(struct z_info *, uint8_t *); struct z_info { int32_t rsrc, rdst; /* original source / destination rates */ int32_t src, dst; /* rounded source / destination rates */ int32_t channels; /* total channels */ int32_t bps; /* bytes-per-sample */ int32_t quality; /* resampling quality */ int32_t z_gx, z_gy; /* interpolation / decimation ratio */ int32_t z_alpha; /* output sample time phase / drift */ uint8_t *z_delay; /* FIR delay line / linear buffer */ int32_t *z_coeff; /* FIR coefficients */ int32_t *z_dcoeff; /* FIR coefficients differences */ int32_t *z_pcoeff; /* FIR polyphase coefficients */ int32_t z_scale; /* output scaling */ int32_t z_dx; /* input sample drift increment */ int32_t z_dy; /* output sample drift increment */ #ifdef Z_USE_ALPHADRIFT int32_t z_alphadrift; /* alpha drift rate */ int32_t z_startdrift; /* buffer start position drift rate */ #endif int32_t z_mask; /* delay line full length mask */ int32_t z_size; /* half width of FIR taps */ int32_t z_full; /* full size of delay line */ int32_t z_alloc; /* largest allocated full size of delay line */ int32_t z_start; /* buffer processing start position */ int32_t z_pos; /* current position for the next feed */ #ifdef Z_DIAGNOSTIC uint32_t z_cycle; /* output cycle, purely for statistical */ #endif int32_t z_maxfeed; /* maximum feed to avoid 32bit overflow */ z_resampler_t z_resample; }; int feeder_rate_min = Z_RATE_MIN; int feeder_rate_max = Z_RATE_MAX; int feeder_rate_round = Z_ROUNDHZ; int feeder_rate_quality = Z_QUALITY_DEFAULT; static int feeder_rate_polyphase_max = Z_POLYPHASE_MAX; #ifdef _KERNEL static char feeder_rate_presets[] = FEEDER_RATE_PRESETS; SYSCTL_STRING(_hw_snd, OID_AUTO, feeder_rate_presets, CTLFLAG_RD, &feeder_rate_presets, 0, "compile-time rate presets"); SYSCTL_INT(_hw_snd, OID_AUTO, feeder_rate_polyphase_max, CTLFLAG_RWTUN, &feeder_rate_polyphase_max, 0, "maximum allowable polyphase entries"); static int sysctl_hw_snd_feeder_rate_min(SYSCTL_HANDLER_ARGS) { int err, val; val = feeder_rate_min; err = sysctl_handle_int(oidp, &val, 0, req); if (err != 0 || req->newptr == NULL || val == feeder_rate_min) return (err); if (!(Z_FACTOR_SAFE(val) && val < feeder_rate_max)) return (EINVAL); feeder_rate_min = val; return (0); } SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_min, CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 0, sizeof(int), sysctl_hw_snd_feeder_rate_min, "I", "minimum allowable rate"); static int sysctl_hw_snd_feeder_rate_max(SYSCTL_HANDLER_ARGS) { int err, val; val = feeder_rate_max; err = sysctl_handle_int(oidp, &val, 0, req); if (err != 0 || req->newptr == NULL || val == feeder_rate_max) return (err); if (!(Z_FACTOR_SAFE(val) && val > feeder_rate_min)) return (EINVAL); feeder_rate_max = val; return (0); } SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_max, CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 0, sizeof(int), sysctl_hw_snd_feeder_rate_max, "I", "maximum allowable rate"); static int sysctl_hw_snd_feeder_rate_round(SYSCTL_HANDLER_ARGS) { int err, val; val = feeder_rate_round; err = sysctl_handle_int(oidp, &val, 0, req); if (err != 0 || req->newptr == NULL || val == feeder_rate_round) return (err); if (val < Z_ROUNDHZ_MIN || val > Z_ROUNDHZ_MAX) return (EINVAL); feeder_rate_round = val - (val % Z_ROUNDHZ); return (0); } SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_round, CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 0, sizeof(int), sysctl_hw_snd_feeder_rate_round, "I", "sample rate converter rounding threshold"); static int sysctl_hw_snd_feeder_rate_quality(SYSCTL_HANDLER_ARGS) { struct snddev_info *d; struct pcm_channel *c; struct pcm_feeder *f; int i, err, val; val = feeder_rate_quality; err = sysctl_handle_int(oidp, &val, 0, req); if (err != 0 || req->newptr == NULL || val == feeder_rate_quality) return (err); if (val < Z_QUALITY_MIN || val > Z_QUALITY_MAX) return (EINVAL); feeder_rate_quality = val; /* * Traverse all available channels on each device and try to * set resampler quality if and only if it is exist as * part of feeder chains and the channel is idle. */ for (i = 0; pcm_devclass != NULL && i < devclass_get_maxunit(pcm_devclass); i++) { d = devclass_get_softc(pcm_devclass, i); if (!PCM_REGISTERED(d)) continue; PCM_LOCK(d); PCM_WAIT(d); PCM_ACQUIRE(d); CHN_FOREACH(c, d, channels.pcm) { CHN_LOCK(c); f = feeder_find(c, FEEDER_RATE); if (f == NULL || f->data == NULL || CHN_STARTED(c)) { CHN_UNLOCK(c); continue; } (void)FEEDER_SET(f, FEEDRATE_QUALITY, val); CHN_UNLOCK(c); } PCM_RELEASE(d); PCM_UNLOCK(d); } return (0); } SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_quality, CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int), sysctl_hw_snd_feeder_rate_quality, "I", "sample rate converter quality ("__XSTRING(Z_QUALITY_MIN)"=low .. " __XSTRING(Z_QUALITY_MAX)"=high)"); #endif /* _KERNEL */ /* * Resampler type. */ #define Z_IS_ZOH(i) ((i)->quality == Z_QUALITY_ZOH) #define Z_IS_LINEAR(i) ((i)->quality == Z_QUALITY_LINEAR) #define Z_IS_SINC(i) ((i)->quality > Z_QUALITY_LINEAR) /* * Macroses for accurate sample time drift calculations. * * gy2gx : given the amount of output, return the _exact_ required amount of * input. * gx2gy : given the amount of input, return the _maximum_ amount of output * that will be generated. * drift : given the amount of input and output, return the elapsed * sample-time. */ #define _Z_GCAST(x) ((uint64_t)(x)) #if defined(__i386__) /* * This is where i386 being beaten to a pulp. Fortunately this function is * rarely being called and if it is, it will decide the best (hopefully) * fastest way to do the division. If we can ensure that everything is dword * aligned, letting the compiler to call udivdi3 to do the division can be * faster compared to this. * * amd64 is the clear winner here, no question about it. */ static __inline uint32_t Z_DIV(uint64_t v, uint32_t d) { uint32_t hi, lo, quo, rem; hi = v >> 32; lo = v & 0xffffffff; /* * As much as we can, try to avoid long division like a plague. */ if (hi == 0) quo = lo / d; else __asm("divl %2" : "=a" (quo), "=d" (rem) : "r" (d), "0" (lo), "1" (hi)); return (quo); } #else #define Z_DIV(x, y) ((x) / (y)) #endif #define _Z_GY2GX(i, a, v) \ Z_DIV(((_Z_GCAST((i)->z_gx) * (v)) + ((i)->z_gy - (a) - 1)), \ (i)->z_gy) #define _Z_GX2GY(i, a, v) \ Z_DIV(((_Z_GCAST((i)->z_gy) * (v)) + (a)), (i)->z_gx) #define _Z_DRIFT(i, x, y) \ ((_Z_GCAST((i)->z_gy) * (x)) - (_Z_GCAST((i)->z_gx) * (y))) #define z_gy2gx(i, v) _Z_GY2GX(i, (i)->z_alpha, v) #define z_gx2gy(i, v) _Z_GX2GY(i, (i)->z_alpha, v) #define z_drift(i, x, y) _Z_DRIFT(i, x, y) /* * Macroses for SINC coefficients table manipulations.. whatever. */ #define Z_SINC_COEFF_IDX(i) ((i)->quality - Z_QUALITY_LINEAR - 1) #define Z_SINC_LEN(i) \ ((int32_t)(((uint64_t)z_coeff_tab[Z_SINC_COEFF_IDX(i)].len << \ Z_SHIFT) / (i)->z_dy)) #define Z_SINC_BASE_LEN(i) \ ((z_coeff_tab[Z_SINC_COEFF_IDX(i)].len - 1) >> (Z_DRIFT_SHIFT - 1)) /* * Macroses for linear delay buffer operations. Alignment is not * really necessary since we're not using true circular buffer, but it * will help us guard against possible trespasser. To be honest, * the linear block operations does not need guarding at all due to * accurate drifting! */ #define z_align(i, v) ((v) & (i)->z_mask) #define z_next(i, o, v) z_align(i, (o) + (v)) #define z_prev(i, o, v) z_align(i, (o) - (v)) #define z_fetched(i) (z_align(i, (i)->z_pos - (i)->z_start) - 1) #define z_free(i) ((i)->z_full - (i)->z_pos) /* * Macroses for Bla Bla .. :) */ #define z_copy(src, dst, sz) (void)memcpy(dst, src, sz) #define z_feed(...) FEEDER_FEED(__VA_ARGS__) static __inline uint32_t z_min(uint32_t x, uint32_t y) { return ((x < y) ? x : y); } static int32_t z_gcd(int32_t x, int32_t y) { int32_t w; while (y != 0) { w = x % y; x = y; y = w; } return (x); } static int32_t z_roundpow2(int32_t v) { int32_t i; i = 1; /* * Let it overflow at will.. */ while (i > 0 && i < v) i <<= 1; return (i); } /* * Zero Order Hold, the worst of the worst, an insult against quality, * but super fast. */ static void z_feed_zoh(struct z_info *info, uint8_t *dst) { uint32_t cnt; uint8_t *src; cnt = info->channels * info->bps; src = info->z_delay + (info->z_start * cnt); /* * This is a bit faster than doing bcopy() since we're dealing * with possible unaligned samples. */ do { *dst++ = *src++; } while (--cnt != 0); } /* * Linear Interpolation. This at least sounds better (perceptually) and fast, * but without any proper filtering which means aliasing still exist and * could become worst with a right sample. Interpolation centered within * Z_LINEAR_ONE between the present and previous sample and everything is * done with simple 32bit scaling arithmetic. */ #define Z_DECLARE_LINEAR(SIGN, BIT, ENDIAN) \ static void \ z_feed_linear_##SIGN##BIT##ENDIAN(struct z_info *info, uint8_t *dst) \ { \ int32_t z; \ intpcm_t x, y; \ uint32_t ch; \ uint8_t *sx, *sy; \ \ z = ((uint32_t)info->z_alpha * info->z_dx) >> Z_LINEAR_UNSHIFT; \ \ sx = info->z_delay + (info->z_start * info->channels * \ PCM_##BIT##_BPS); \ sy = sx - (info->channels * PCM_##BIT##_BPS); \ \ ch = info->channels; \ \ do { \ x = pcm_sample_read(sx, AFMT_##SIGN##BIT##_##ENDIAN); \ y = pcm_sample_read(sy, AFMT_##SIGN##BIT##_##ENDIAN); \ x = Z_LINEAR_INTERPOLATE_##BIT(z, x, y); \ pcm_sample_write(dst, x, AFMT_##SIGN##BIT##_##ENDIAN); \ sx += PCM_##BIT##_BPS; \ sy += PCM_##BIT##_BPS; \ dst += PCM_##BIT##_BPS; \ } while (--ch != 0); \ } /* * Userland clipping diagnostic check, not enabled in kernel compilation. * While doing sinc interpolation, unrealistic samples like full scale sine * wav will clip, but for other things this will not make any noise at all. * Everybody should learn how to normalized perceived loudness of their own * music/sounds/samples (hint: ReplayGain). */ #ifdef Z_DIAGNOSTIC #define Z_CLIP_CHECK(v, BIT) do { \ if ((v) > PCM_S##BIT##_MAX) { \ fprintf(stderr, "Overflow: v=%jd, max=%jd\n", \ (intmax_t)(v), (intmax_t)PCM_S##BIT##_MAX); \ } else if ((v) < PCM_S##BIT##_MIN) { \ fprintf(stderr, "Underflow: v=%jd, min=%jd\n", \ (intmax_t)(v), (intmax_t)PCM_S##BIT##_MIN); \ } \ } while (0) #else #define Z_CLIP_CHECK(...) #endif -#define Z_CLAMP(v, BIT) \ - (((v) > PCM_S##BIT##_MAX) ? PCM_S##BIT##_MAX : \ - (((v) < PCM_S##BIT##_MIN) ? PCM_S##BIT##_MIN : (v))) - /* * Sine Cardinal (SINC) Interpolation. Scaling is done in 64 bit, so * there's no point to hold the plate any longer. All samples will be * shifted to a full 32 bit, scaled and restored during write for * maximum dynamic range (only for downsampling). */ #define _Z_SINC_ACCUMULATE(SIGN, BIT, ENDIAN, adv) \ c += z >> Z_SHIFT; \ z &= Z_MASK; \ coeff = Z_COEFF_INTERPOLATE(z, z_coeff[c], z_dcoeff[c]); \ x = pcm_sample_read(p, AFMT_##SIGN##BIT##_##ENDIAN); \ v += Z_NORM_##BIT((intpcm64_t)x * coeff); \ z += info->z_dy; \ p adv##= info->channels * PCM_##BIT##_BPS /* * XXX GCC4 optimization is such a !@#$%, need manual unrolling. */ #if defined(__GNUC__) && __GNUC__ >= 4 #define Z_SINC_ACCUMULATE(...) do { \ _Z_SINC_ACCUMULATE(__VA_ARGS__); \ _Z_SINC_ACCUMULATE(__VA_ARGS__); \ } while (0) #define Z_SINC_ACCUMULATE_DECR 2 #else #define Z_SINC_ACCUMULATE(...) do { \ _Z_SINC_ACCUMULATE(__VA_ARGS__); \ } while (0) #define Z_SINC_ACCUMULATE_DECR 1 #endif #define Z_DECLARE_SINC(SIGN, BIT, ENDIAN) \ static void \ z_feed_sinc_##SIGN##BIT##ENDIAN(struct z_info *info, uint8_t *dst) \ { \ intpcm64_t v; \ intpcm_t x; \ uint8_t *p; \ int32_t coeff, z, *z_coeff, *z_dcoeff; \ uint32_t c, center, ch, i; \ \ z_coeff = info->z_coeff; \ z_dcoeff = info->z_dcoeff; \ center = z_prev(info, info->z_start, info->z_size); \ ch = info->channels * PCM_##BIT##_BPS; \ dst += ch; \ \ do { \ dst -= PCM_##BIT##_BPS; \ ch -= PCM_##BIT##_BPS; \ v = 0; \ z = info->z_alpha * info->z_dx; \ c = 0; \ p = info->z_delay + (z_next(info, center, 1) * \ info->channels * PCM_##BIT##_BPS) + ch; \ for (i = info->z_size; i != 0; i -= Z_SINC_ACCUMULATE_DECR) \ Z_SINC_ACCUMULATE(SIGN, BIT, ENDIAN, +); \ z = info->z_dy - (info->z_alpha * info->z_dx); \ c = 0; \ p = info->z_delay + (center * info->channels * \ PCM_##BIT##_BPS) + ch; \ for (i = info->z_size; i != 0; i -= Z_SINC_ACCUMULATE_DECR) \ Z_SINC_ACCUMULATE(SIGN, BIT, ENDIAN, -); \ if (info->z_scale != Z_ONE) \ v = Z_SCALE_##BIT(v, info->z_scale); \ else \ v >>= Z_COEFF_SHIFT - Z_GUARD_BIT_##BIT; \ Z_CLIP_CHECK(v, BIT); \ - pcm_sample_write(dst, Z_CLAMP(v, BIT), \ + pcm_sample_write(dst, pcm_clamp(v, AFMT_##SIGN##BIT##_##ENDIAN),\ AFMT_##SIGN##BIT##_##ENDIAN); \ } while (ch != 0); \ } #define Z_DECLARE_SINC_POLYPHASE(SIGN, BIT, ENDIAN) \ static void \ z_feed_sinc_polyphase_##SIGN##BIT##ENDIAN(struct z_info *info, uint8_t *dst) \ { \ intpcm64_t v; \ intpcm_t x; \ uint8_t *p; \ int32_t ch, i, start, *z_pcoeff; \ \ ch = info->channels * PCM_##BIT##_BPS; \ dst += ch; \ start = z_prev(info, info->z_start, (info->z_size << 1) - 1) * ch; \ \ do { \ dst -= PCM_##BIT##_BPS; \ ch -= PCM_##BIT##_BPS; \ v = 0; \ p = info->z_delay + start + ch; \ z_pcoeff = info->z_pcoeff + \ ((info->z_alpha * info->z_size) << 1); \ for (i = info->z_size; i != 0; i--) { \ x = pcm_sample_read(p, AFMT_##SIGN##BIT##_##ENDIAN); \ v += Z_NORM_##BIT((intpcm64_t)x * *z_pcoeff); \ z_pcoeff++; \ p += info->channels * PCM_##BIT##_BPS; \ x = pcm_sample_read(p, AFMT_##SIGN##BIT##_##ENDIAN); \ v += Z_NORM_##BIT((intpcm64_t)x * *z_pcoeff); \ z_pcoeff++; \ p += info->channels * PCM_##BIT##_BPS; \ } \ if (info->z_scale != Z_ONE) \ v = Z_SCALE_##BIT(v, info->z_scale); \ else \ v >>= Z_COEFF_SHIFT - Z_GUARD_BIT_##BIT; \ Z_CLIP_CHECK(v, BIT); \ - pcm_sample_write(dst, Z_CLAMP(v, BIT), \ + pcm_sample_write(dst, pcm_clamp(v, AFMT_##SIGN##BIT##_##ENDIAN),\ AFMT_##SIGN##BIT##_##ENDIAN); \ } while (ch != 0); \ } #define Z_DECLARE(SIGN, BIT, ENDIAN) \ Z_DECLARE_LINEAR(SIGN, BIT, ENDIAN) \ Z_DECLARE_SINC(SIGN, BIT, ENDIAN) \ Z_DECLARE_SINC_POLYPHASE(SIGN, BIT, ENDIAN) #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) Z_DECLARE(S, 16, LE) Z_DECLARE(S, 32, LE) #endif #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) Z_DECLARE(S, 16, BE) Z_DECLARE(S, 32, BE) #endif #ifdef SND_FEEDER_MULTIFORMAT Z_DECLARE(S, 8, NE) Z_DECLARE(S, 24, LE) Z_DECLARE(S, 24, BE) Z_DECLARE(U, 8, NE) Z_DECLARE(U, 16, LE) Z_DECLARE(U, 24, LE) Z_DECLARE(U, 32, LE) Z_DECLARE(U, 16, BE) Z_DECLARE(U, 24, BE) Z_DECLARE(U, 32, BE) #endif enum { Z_RESAMPLER_ZOH, Z_RESAMPLER_LINEAR, Z_RESAMPLER_SINC, Z_RESAMPLER_SINC_POLYPHASE, Z_RESAMPLER_LAST }; #define Z_RESAMPLER_IDX(i) \ (Z_IS_SINC(i) ? Z_RESAMPLER_SINC : (i)->quality) #define Z_RESAMPLER_ENTRY(SIGN, BIT, ENDIAN) \ { \ AFMT_##SIGN##BIT##_##ENDIAN, \ { \ [Z_RESAMPLER_ZOH] = z_feed_zoh, \ [Z_RESAMPLER_LINEAR] = z_feed_linear_##SIGN##BIT##ENDIAN, \ [Z_RESAMPLER_SINC] = z_feed_sinc_##SIGN##BIT##ENDIAN, \ [Z_RESAMPLER_SINC_POLYPHASE] = \ z_feed_sinc_polyphase_##SIGN##BIT##ENDIAN \ } \ } static const struct { uint32_t format; z_resampler_t resampler[Z_RESAMPLER_LAST]; } z_resampler_tab[] = { #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) Z_RESAMPLER_ENTRY(S, 16, LE), Z_RESAMPLER_ENTRY(S, 32, LE), #endif #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) Z_RESAMPLER_ENTRY(S, 16, BE), Z_RESAMPLER_ENTRY(S, 32, BE), #endif #ifdef SND_FEEDER_MULTIFORMAT Z_RESAMPLER_ENTRY(S, 8, NE), Z_RESAMPLER_ENTRY(S, 24, LE), Z_RESAMPLER_ENTRY(S, 24, BE), Z_RESAMPLER_ENTRY(U, 8, NE), Z_RESAMPLER_ENTRY(U, 16, LE), Z_RESAMPLER_ENTRY(U, 24, LE), Z_RESAMPLER_ENTRY(U, 32, LE), Z_RESAMPLER_ENTRY(U, 16, BE), Z_RESAMPLER_ENTRY(U, 24, BE), Z_RESAMPLER_ENTRY(U, 32, BE), #endif }; #define Z_RESAMPLER_TAB_SIZE \ ((int32_t)(sizeof(z_resampler_tab) / sizeof(z_resampler_tab[0]))) static void z_resampler_reset(struct z_info *info) { info->src = info->rsrc - (info->rsrc % ((feeder_rate_round > 0 && info->rsrc > feeder_rate_round) ? feeder_rate_round : 1)); info->dst = info->rdst - (info->rdst % ((feeder_rate_round > 0 && info->rdst > feeder_rate_round) ? feeder_rate_round : 1)); info->z_gx = 1; info->z_gy = 1; info->z_alpha = 0; info->z_resample = NULL; info->z_size = 1; info->z_coeff = NULL; info->z_dcoeff = NULL; if (info->z_pcoeff != NULL) { free(info->z_pcoeff, M_DEVBUF); info->z_pcoeff = NULL; } info->z_scale = Z_ONE; info->z_dx = Z_FULL_ONE; info->z_dy = Z_FULL_ONE; #ifdef Z_DIAGNOSTIC info->z_cycle = 0; #endif if (info->quality < Z_QUALITY_MIN) info->quality = Z_QUALITY_MIN; else if (info->quality > Z_QUALITY_MAX) info->quality = Z_QUALITY_MAX; } #ifdef Z_PARANOID static int32_t z_resampler_sinc_len(struct z_info *info) { int32_t c, z, len, lmax; if (!Z_IS_SINC(info)) return (1); /* * A rather careful (or useless) way to calculate filter length. * Z_SINC_LEN() itself is accurate enough to do its job. Extra * sanity checking is not going to hurt though.. */ c = 0; z = info->z_dy; len = 0; lmax = z_coeff_tab[Z_SINC_COEFF_IDX(info)].len; do { c += z >> Z_SHIFT; z &= Z_MASK; z += info->z_dy; } while (c < lmax && ++len > 0); if (len != Z_SINC_LEN(info)) { #ifdef _KERNEL printf("%s(): sinc l=%d != Z_SINC_LEN=%d\n", __func__, len, Z_SINC_LEN(info)); #else fprintf(stderr, "%s(): sinc l=%d != Z_SINC_LEN=%d\n", __func__, len, Z_SINC_LEN(info)); return (-1); #endif } return (len); } #else #define z_resampler_sinc_len(i) (Z_IS_SINC(i) ? Z_SINC_LEN(i) : 1) #endif #define Z_POLYPHASE_COEFF_SHIFT 0 /* * Pick suitable polynomial interpolators based on filter oversampled ratio * (2 ^ Z_DRIFT_SHIFT). */ #if !(defined(Z_COEFF_INTERP_ZOH) || defined(Z_COEFF_INTERP_LINEAR) || \ defined(Z_COEFF_INTERP_QUADRATIC) || defined(Z_COEFF_INTERP_HERMITE) || \ defined(Z_COEFF_INTER_BSPLINE) || defined(Z_COEFF_INTERP_OPT32X) || \ defined(Z_COEFF_INTERP_OPT16X) || defined(Z_COEFF_INTERP_OPT8X) || \ defined(Z_COEFF_INTERP_OPT4X) || defined(Z_COEFF_INTERP_OPT2X)) #if Z_DRIFT_SHIFT >= 6 #define Z_COEFF_INTERP_BSPLINE 1 #elif Z_DRIFT_SHIFT >= 5 #define Z_COEFF_INTERP_OPT32X 1 #elif Z_DRIFT_SHIFT == 4 #define Z_COEFF_INTERP_OPT16X 1 #elif Z_DRIFT_SHIFT == 3 #define Z_COEFF_INTERP_OPT8X 1 #elif Z_DRIFT_SHIFT == 2 #define Z_COEFF_INTERP_OPT4X 1 #elif Z_DRIFT_SHIFT == 1 #define Z_COEFF_INTERP_OPT2X 1 #else #error "Z_DRIFT_SHIFT screwed!" #endif #endif /* * In classic polyphase mode, the actual coefficients for each phases need to * be calculated based on default prototype filters. For highly oversampled * filter, linear or quadradatic interpolator should be enough. Anything less * than that require 'special' interpolators to reduce interpolation errors. * * "Polynomial Interpolators for High-Quality Resampling of Oversampled Audio" * by Olli Niemitalo * - http://www.student.oulu.fi/~oniemita/dsp/deip.pdf * */ static int32_t z_coeff_interpolate(int32_t z, int32_t *z_coeff) { int32_t coeff; #if defined(Z_COEFF_INTERP_ZOH) /* 1-point, 0th-order (Zero Order Hold) */ z = z; coeff = z_coeff[0]; #elif defined(Z_COEFF_INTERP_LINEAR) int32_t zl0, zl1; /* 2-point, 1st-order Linear */ zl0 = z_coeff[0]; zl1 = z_coeff[1] - z_coeff[0]; coeff = Z_RSHIFT((int64_t)zl1 * z, Z_SHIFT) + zl0; #elif defined(Z_COEFF_INTERP_QUADRATIC) int32_t zq0, zq1, zq2; /* 3-point, 2nd-order Quadratic */ zq0 = z_coeff[0]; zq1 = z_coeff[1] - z_coeff[-1]; zq2 = z_coeff[1] + z_coeff[-1] - (z_coeff[0] << 1); coeff = Z_RSHIFT((Z_RSHIFT((int64_t)zq2 * z, Z_SHIFT) + zq1) * z, Z_SHIFT + 1) + zq0; #elif defined(Z_COEFF_INTERP_HERMITE) int32_t zh0, zh1, zh2, zh3; /* 4-point, 3rd-order Hermite */ zh0 = z_coeff[0]; zh1 = z_coeff[1] - z_coeff[-1]; zh2 = (z_coeff[-1] << 1) - (z_coeff[0] * 5) + (z_coeff[1] << 2) - z_coeff[2]; zh3 = z_coeff[2] - z_coeff[-1] + ((z_coeff[0] - z_coeff[1]) * 3); coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((int64_t)zh3 * z, Z_SHIFT) + zh2) * z, Z_SHIFT) + zh1) * z, Z_SHIFT + 1) + zh0; #elif defined(Z_COEFF_INTERP_BSPLINE) int32_t zb0, zb1, zb2, zb3; /* 4-point, 3rd-order B-Spline */ zb0 = Z_RSHIFT(0x15555555LL * (((int64_t)z_coeff[0] << 2) + z_coeff[-1] + z_coeff[1]), 30); zb1 = z_coeff[1] - z_coeff[-1]; zb2 = z_coeff[-1] + z_coeff[1] - (z_coeff[0] << 1); zb3 = Z_RSHIFT(0x15555555LL * (((z_coeff[0] - z_coeff[1]) * 3) + z_coeff[2] - z_coeff[-1]), 30); coeff = (Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((int64_t)zb3 * z, Z_SHIFT) + zb2) * z, Z_SHIFT) + zb1) * z, Z_SHIFT) + zb0 + 1) >> 1; #elif defined(Z_COEFF_INTERP_OPT32X) int32_t zoz, zoe1, zoe2, zoe3, zoo1, zoo2, zoo3; int32_t zoc0, zoc1, zoc2, zoc3, zoc4, zoc5; /* 6-point, 5th-order Optimal 32x */ zoz = z - (Z_ONE >> 1); zoe1 = z_coeff[1] + z_coeff[0]; zoe2 = z_coeff[2] + z_coeff[-1]; zoe3 = z_coeff[3] + z_coeff[-2]; zoo1 = z_coeff[1] - z_coeff[0]; zoo2 = z_coeff[2] - z_coeff[-1]; zoo3 = z_coeff[3] - z_coeff[-2]; zoc0 = Z_RSHIFT((0x1ac2260dLL * zoe1) + (0x0526cdcaLL * zoe2) + (0x00170c29LL * zoe3), 30); zoc1 = Z_RSHIFT((0x14f8a49aLL * zoo1) + (0x0d6d1109LL * zoo2) + (0x008cd4dcLL * zoo3), 30); zoc2 = Z_RSHIFT((-0x0d3e94a4LL * zoe1) + (0x0bddded4LL * zoe2) + (0x0160b5d0LL * zoe3), 30); zoc3 = Z_RSHIFT((-0x0de10cc4LL * zoo1) + (0x019b2a7dLL * zoo2) + (0x01cfe914LL * zoo3), 30); zoc4 = Z_RSHIFT((0x02aa12d7LL * zoe1) + (-0x03ff1bb3LL * zoe2) + (0x015508ddLL * zoe3), 30); zoc5 = Z_RSHIFT((0x051d29e5LL * zoo1) + (-0x028e7647LL * zoo2) + (0x0082d81aLL * zoo3), 30); coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT( (int64_t)zoc5 * zoz, Z_SHIFT) + zoc4) * zoz, Z_SHIFT) + zoc3) * zoz, Z_SHIFT) + zoc2) * zoz, Z_SHIFT) + zoc1) * zoz, Z_SHIFT) + zoc0; #elif defined(Z_COEFF_INTERP_OPT16X) int32_t zoz, zoe1, zoe2, zoe3, zoo1, zoo2, zoo3; int32_t zoc0, zoc1, zoc2, zoc3, zoc4, zoc5; /* 6-point, 5th-order Optimal 16x */ zoz = z - (Z_ONE >> 1); zoe1 = z_coeff[1] + z_coeff[0]; zoe2 = z_coeff[2] + z_coeff[-1]; zoe3 = z_coeff[3] + z_coeff[-2]; zoo1 = z_coeff[1] - z_coeff[0]; zoo2 = z_coeff[2] - z_coeff[-1]; zoo3 = z_coeff[3] - z_coeff[-2]; zoc0 = Z_RSHIFT((0x1ac2260dLL * zoe1) + (0x0526cdcaLL * zoe2) + (0x00170c29LL * zoe3), 30); zoc1 = Z_RSHIFT((0x14f8a49aLL * zoo1) + (0x0d6d1109LL * zoo2) + (0x008cd4dcLL * zoo3), 30); zoc2 = Z_RSHIFT((-0x0d3e94a4LL * zoe1) + (0x0bddded4LL * zoe2) + (0x0160b5d0LL * zoe3), 30); zoc3 = Z_RSHIFT((-0x0de10cc4LL * zoo1) + (0x019b2a7dLL * zoo2) + (0x01cfe914LL * zoo3), 30); zoc4 = Z_RSHIFT((0x02aa12d7LL * zoe1) + (-0x03ff1bb3LL * zoe2) + (0x015508ddLL * zoe3), 30); zoc5 = Z_RSHIFT((0x051d29e5LL * zoo1) + (-0x028e7647LL * zoo2) + (0x0082d81aLL * zoo3), 30); coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT( (int64_t)zoc5 * zoz, Z_SHIFT) + zoc4) * zoz, Z_SHIFT) + zoc3) * zoz, Z_SHIFT) + zoc2) * zoz, Z_SHIFT) + zoc1) * zoz, Z_SHIFT) + zoc0; #elif defined(Z_COEFF_INTERP_OPT8X) int32_t zoz, zoe1, zoe2, zoe3, zoo1, zoo2, zoo3; int32_t zoc0, zoc1, zoc2, zoc3, zoc4, zoc5; /* 6-point, 5th-order Optimal 8x */ zoz = z - (Z_ONE >> 1); zoe1 = z_coeff[1] + z_coeff[0]; zoe2 = z_coeff[2] + z_coeff[-1]; zoe3 = z_coeff[3] + z_coeff[-2]; zoo1 = z_coeff[1] - z_coeff[0]; zoo2 = z_coeff[2] - z_coeff[-1]; zoo3 = z_coeff[3] - z_coeff[-2]; zoc0 = Z_RSHIFT((0x1aa9b47dLL * zoe1) + (0x053d9944LL * zoe2) + (0x0018b23fLL * zoe3), 30); zoc1 = Z_RSHIFT((0x14a104d1LL * zoo1) + (0x0d7d2504LL * zoo2) + (0x0094b599LL * zoo3), 30); zoc2 = Z_RSHIFT((-0x0d22530bLL * zoe1) + (0x0bb37a2cLL * zoe2) + (0x016ed8e0LL * zoe3), 30); zoc3 = Z_RSHIFT((-0x0d744b1cLL * zoo1) + (0x01649591LL * zoo2) + (0x01dae93aLL * zoo3), 30); zoc4 = Z_RSHIFT((0x02a7ee1bLL * zoe1) + (-0x03fbdb24LL * zoe2) + (0x0153ed07LL * zoe3), 30); zoc5 = Z_RSHIFT((0x04cf9b6cLL * zoo1) + (-0x0266b378LL * zoo2) + (0x007a7c26LL * zoo3), 30); coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT( (int64_t)zoc5 * zoz, Z_SHIFT) + zoc4) * zoz, Z_SHIFT) + zoc3) * zoz, Z_SHIFT) + zoc2) * zoz, Z_SHIFT) + zoc1) * zoz, Z_SHIFT) + zoc0; #elif defined(Z_COEFF_INTERP_OPT4X) int32_t zoz, zoe1, zoe2, zoe3, zoo1, zoo2, zoo3; int32_t zoc0, zoc1, zoc2, zoc3, zoc4, zoc5; /* 6-point, 5th-order Optimal 4x */ zoz = z - (Z_ONE >> 1); zoe1 = z_coeff[1] + z_coeff[0]; zoe2 = z_coeff[2] + z_coeff[-1]; zoe3 = z_coeff[3] + z_coeff[-2]; zoo1 = z_coeff[1] - z_coeff[0]; zoo2 = z_coeff[2] - z_coeff[-1]; zoo3 = z_coeff[3] - z_coeff[-2]; zoc0 = Z_RSHIFT((0x1a8eda43LL * zoe1) + (0x0556ee38LL * zoe2) + (0x001a3784LL * zoe3), 30); zoc1 = Z_RSHIFT((0x143d863eLL * zoo1) + (0x0d910e36LL * zoo2) + (0x009ca889LL * zoo3), 30); zoc2 = Z_RSHIFT((-0x0d026821LL * zoe1) + (0x0b837773LL * zoe2) + (0x017ef0c6LL * zoe3), 30); zoc3 = Z_RSHIFT((-0x0cef1502LL * zoo1) + (0x01207a8eLL * zoo2) + (0x01e936dbLL * zoo3), 30); zoc4 = Z_RSHIFT((0x029fe643LL * zoe1) + (-0x03ef3fc8LL * zoe2) + (0x014f5923LL * zoe3), 30); zoc5 = Z_RSHIFT((0x043a9d08LL * zoo1) + (-0x02154febLL * zoo2) + (0x00670dbdLL * zoo3), 30); coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT( (int64_t)zoc5 * zoz, Z_SHIFT) + zoc4) * zoz, Z_SHIFT) + zoc3) * zoz, Z_SHIFT) + zoc2) * zoz, Z_SHIFT) + zoc1) * zoz, Z_SHIFT) + zoc0; #elif defined(Z_COEFF_INTERP_OPT2X) int32_t zoz, zoe1, zoe2, zoe3, zoo1, zoo2, zoo3; int32_t zoc0, zoc1, zoc2, zoc3, zoc4, zoc5; /* 6-point, 5th-order Optimal 2x */ zoz = z - (Z_ONE >> 1); zoe1 = z_coeff[1] + z_coeff[0]; zoe2 = z_coeff[2] + z_coeff[-1]; zoe3 = z_coeff[3] + z_coeff[-2]; zoo1 = z_coeff[1] - z_coeff[0]; zoo2 = z_coeff[2] - z_coeff[-1]; zoo3 = z_coeff[3] - z_coeff[-2]; zoc0 = Z_RSHIFT((0x19edb6fdLL * zoe1) + (0x05ebd062LL * zoe2) + (0x00267881LL * zoe3), 30); zoc1 = Z_RSHIFT((0x1223af76LL * zoo1) + (0x0de3dd6bLL * zoo2) + (0x00d683cdLL * zoo3), 30); zoc2 = Z_RSHIFT((-0x0c3ee068LL * zoe1) + (0x0a5c3769LL * zoe2) + (0x01e2aceaLL * zoe3), 30); zoc3 = Z_RSHIFT((-0x0a8ab614LL * zoo1) + (-0x0019522eLL * zoo2) + (0x022cefc7LL * zoo3), 30); zoc4 = Z_RSHIFT((0x0276187dLL * zoe1) + (-0x03a801e8LL * zoe2) + (0x0131d935LL * zoe3), 30); zoc5 = Z_RSHIFT((0x02c373f5LL * zoo1) + (-0x01275f83LL * zoo2) + (0x0018ee79LL * zoo3), 30); coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT( (int64_t)zoc5 * zoz, Z_SHIFT) + zoc4) * zoz, Z_SHIFT) + zoc3) * zoz, Z_SHIFT) + zoc2) * zoz, Z_SHIFT) + zoc1) * zoz, Z_SHIFT) + zoc0; #else #error "Interpolation type screwed!" #endif #if Z_POLYPHASE_COEFF_SHIFT > 0 coeff = Z_RSHIFT(coeff, Z_POLYPHASE_COEFF_SHIFT); #endif return (coeff); } static int z_resampler_build_polyphase(struct z_info *info) { int32_t alpha, c, i, z, idx; /* Let this be here first. */ if (info->z_pcoeff != NULL) { free(info->z_pcoeff, M_DEVBUF); info->z_pcoeff = NULL; } if (feeder_rate_polyphase_max < 1) return (ENOTSUP); if (((int64_t)info->z_size * info->z_gy * 2) > feeder_rate_polyphase_max) { #ifndef _KERNEL fprintf(stderr, "Polyphase entries exceed: [%d/%d] %jd > %d\n", info->z_gx, info->z_gy, (intmax_t)info->z_size * info->z_gy * 2, feeder_rate_polyphase_max); #endif return (E2BIG); } info->z_pcoeff = malloc(sizeof(int32_t) * info->z_size * info->z_gy * 2, M_DEVBUF, M_NOWAIT | M_ZERO); if (info->z_pcoeff == NULL) return (ENOMEM); for (alpha = 0; alpha < info->z_gy; alpha++) { z = alpha * info->z_dx; c = 0; for (i = info->z_size; i != 0; i--) { c += z >> Z_SHIFT; z &= Z_MASK; idx = (alpha * info->z_size * 2) + (info->z_size * 2) - i; info->z_pcoeff[idx] = z_coeff_interpolate(z, info->z_coeff + c); z += info->z_dy; } z = info->z_dy - (alpha * info->z_dx); c = 0; for (i = info->z_size; i != 0; i--) { c += z >> Z_SHIFT; z &= Z_MASK; idx = (alpha * info->z_size * 2) + i - 1; info->z_pcoeff[idx] = z_coeff_interpolate(z, info->z_coeff + c); z += info->z_dy; } } #ifndef _KERNEL fprintf(stderr, "Polyphase: [%d/%d] %d entries\n", info->z_gx, info->z_gy, info->z_size * info->z_gy * 2); #endif return (0); } static int z_resampler_setup(struct pcm_feeder *f) { struct z_info *info; int64_t gy2gx_max, gx2gy_max; uint32_t format; int32_t align, i, z_scale; int adaptive; info = f->data; z_resampler_reset(info); if (info->src == info->dst) return (0); /* Shrink by greatest common divisor. */ i = z_gcd(info->src, info->dst); info->z_gx = info->src / i; info->z_gy = info->dst / i; /* Too big, or too small. Bail out. */ if (!(Z_FACTOR_SAFE(info->z_gx) && Z_FACTOR_SAFE(info->z_gy))) return (EINVAL); format = f->desc->in; adaptive = 0; z_scale = 0; /* * Setup everything: filter length, conversion factor, etc. */ if (Z_IS_SINC(info)) { /* * Downsampling, or upsampling scaling factor. As long as the * factor can be represented by a fraction of 1 << Z_SHIFT, * we're pretty much in business. Scaling is not needed for * upsampling, so we just slap Z_ONE there. */ if (info->z_gx > info->z_gy) /* * If the downsampling ratio is beyond sanity, * enable semi-adaptive mode. Although handling * extreme ratio is possible, the result of the * conversion is just pointless, unworthy, * nonsensical noises, etc. */ if ((info->z_gx / info->z_gy) > Z_SINC_DOWNMAX) z_scale = Z_ONE / Z_SINC_DOWNMAX; else z_scale = ((uint64_t)info->z_gy << Z_SHIFT) / info->z_gx; else z_scale = Z_ONE; /* * This is actually impossible, unless anything above * overflow. */ if (z_scale < 1) return (E2BIG); /* * Calculate sample time/coefficients index drift. It is * a constant for upsampling, but downsampling require * heavy duty filtering with possible too long filters. * If anything goes wrong, revisit again and enable * adaptive mode. */ z_setup_adaptive_sinc: if (info->z_pcoeff != NULL) { free(info->z_pcoeff, M_DEVBUF); info->z_pcoeff = NULL; } if (adaptive == 0) { info->z_dy = z_scale << Z_DRIFT_SHIFT; if (info->z_dy < 1) return (E2BIG); info->z_scale = z_scale; } else { info->z_dy = Z_FULL_ONE; info->z_scale = Z_ONE; } /* Smallest drift increment. */ info->z_dx = info->z_dy / info->z_gy; /* * Overflow or underflow. Try adaptive, let it continue and * retry. */ if (info->z_dx < 1) { if (adaptive == 0) { adaptive = 1; goto z_setup_adaptive_sinc; } return (E2BIG); } /* * Round back output drift. */ info->z_dy = info->z_dx * info->z_gy; for (i = 0; i < Z_COEFF_TAB_SIZE; i++) { if (Z_SINC_COEFF_IDX(info) != i) continue; /* * Calculate required filter length and guard * against possible abusive result. Note that * this represents only 1/2 of the entire filter * length. */ info->z_size = z_resampler_sinc_len(info); /* * Multiple of 2 rounding, for better accumulator * performance. */ info->z_size &= ~1; if (info->z_size < 2 || info->z_size > Z_SINC_MAX) { if (adaptive == 0) { adaptive = 1; goto z_setup_adaptive_sinc; } return (E2BIG); } info->z_coeff = z_coeff_tab[i].coeff + Z_COEFF_OFFSET; info->z_dcoeff = z_coeff_tab[i].dcoeff; break; } if (info->z_coeff == NULL || info->z_dcoeff == NULL) return (EINVAL); } else if (Z_IS_LINEAR(info)) { /* * Don't put much effort if we're doing linear interpolation. * Just center the interpolation distance within Z_LINEAR_ONE, * and be happy about it. */ info->z_dx = Z_LINEAR_FULL_ONE / info->z_gy; } /* * We're safe for now, lets continue.. Look for our resampler * depending on configured format and quality. */ for (i = 0; i < Z_RESAMPLER_TAB_SIZE; i++) { int ridx; if (AFMT_ENCODING(format) != z_resampler_tab[i].format) continue; if (Z_IS_SINC(info) && adaptive == 0 && z_resampler_build_polyphase(info) == 0) ridx = Z_RESAMPLER_SINC_POLYPHASE; else ridx = Z_RESAMPLER_IDX(info); info->z_resample = z_resampler_tab[i].resampler[ridx]; break; } if (info->z_resample == NULL) return (EINVAL); info->bps = AFMT_BPS(format); align = info->channels * info->bps; /* * Calculate largest value that can be fed into z_gy2gx() and * z_gx2gy() without causing (signed) 32bit overflow. z_gy2gx() will * be called early during feeding process to determine how much input * samples that is required to generate requested output, while * z_gx2gy() will be called just before samples filtering / * accumulation process based on available samples that has been * calculated using z_gx2gy(). * * Now that is damn confusing, I guess ;-) . */ gy2gx_max = (((uint64_t)info->z_gy * INT32_MAX) - info->z_gy + 1) / info->z_gx; if ((gy2gx_max * align) > SND_FXDIV_MAX) gy2gx_max = SND_FXDIV_MAX / align; if (gy2gx_max < 1) return (E2BIG); gx2gy_max = (((uint64_t)info->z_gx * INT32_MAX) - info->z_gy) / info->z_gy; if (gx2gy_max > INT32_MAX) gx2gy_max = INT32_MAX; if (gx2gy_max < 1) return (E2BIG); /* * Ensure that z_gy2gx() at its largest possible calculated value * (alpha = 0) will not cause overflow further late during z_gx2gy() * stage. */ if (z_gy2gx(info, gy2gx_max) > _Z_GCAST(gx2gy_max)) return (E2BIG); info->z_maxfeed = gy2gx_max * align; #ifdef Z_USE_ALPHADRIFT info->z_startdrift = z_gy2gx(info, 1); info->z_alphadrift = z_drift(info, info->z_startdrift, 1); #endif i = z_gy2gx(info, 1); info->z_full = z_roundpow2((info->z_size << 1) + i); /* * Too big to be true, and overflowing left and right like mad .. */ if ((info->z_full * align) < 1) { if (adaptive == 0 && Z_IS_SINC(info)) { adaptive = 1; goto z_setup_adaptive_sinc; } return (E2BIG); } /* * Increase full buffer size if its too small to reduce cyclic * buffer shifting in main conversion/feeder loop. */ while (info->z_full < Z_RESERVOIR_MAX && (info->z_full - (info->z_size << 1)) < Z_RESERVOIR) info->z_full <<= 1; /* Initialize buffer position. */ info->z_mask = info->z_full - 1; info->z_start = z_prev(info, info->z_size << 1, 1); info->z_pos = z_next(info, info->z_start, 1); /* * Allocate or reuse delay line buffer, whichever makes sense. */ i = info->z_full * align; if (i < 1) return (E2BIG); if (info->z_delay == NULL || info->z_alloc < i || i <= (info->z_alloc >> 1)) { if (info->z_delay != NULL) free(info->z_delay, M_DEVBUF); info->z_delay = malloc(i, M_DEVBUF, M_NOWAIT | M_ZERO); if (info->z_delay == NULL) return (ENOMEM); info->z_alloc = i; } /* * Zero out head of buffer to avoid pops and clicks. */ memset(info->z_delay, sndbuf_zerodata(f->desc->out), info->z_pos * align); #ifdef Z_DIAGNOSTIC /* * XXX Debuging mess !@#$%^ */ #define dumpz(x) fprintf(stderr, "\t%12s = %10u : %-11d\n", \ "z_"__STRING(x), (uint32_t)info->z_##x, \ (int32_t)info->z_##x) fprintf(stderr, "\n%s():\n", __func__); fprintf(stderr, "\tchannels=%d, bps=%d, format=0x%08x, quality=%d\n", info->channels, info->bps, format, info->quality); fprintf(stderr, "\t%d (%d) -> %d (%d), ", info->src, info->rsrc, info->dst, info->rdst); fprintf(stderr, "[%d/%d]\n", info->z_gx, info->z_gy); fprintf(stderr, "\tminreq=%d, ", z_gy2gx(info, 1)); if (adaptive != 0) z_scale = Z_ONE; fprintf(stderr, "factor=0x%08x/0x%08x (%f)\n", z_scale, Z_ONE, (double)z_scale / Z_ONE); fprintf(stderr, "\tbase_length=%d, ", Z_SINC_BASE_LEN(info)); fprintf(stderr, "adaptive=%s\n", (adaptive != 0) ? "YES" : "NO"); dumpz(size); dumpz(alloc); if (info->z_alloc < 1024) fprintf(stderr, "\t%15s%10d Bytes\n", "", info->z_alloc); else if (info->z_alloc < (1024 << 10)) fprintf(stderr, "\t%15s%10d KBytes\n", "", info->z_alloc >> 10); else if (info->z_alloc < (1024 << 20)) fprintf(stderr, "\t%15s%10d MBytes\n", "", info->z_alloc >> 20); else fprintf(stderr, "\t%15s%10d GBytes\n", "", info->z_alloc >> 30); fprintf(stderr, "\t%12s %10d (min output samples)\n", "", (int32_t)z_gx2gy(info, info->z_full - (info->z_size << 1))); fprintf(stderr, "\t%12s %10d (min allocated output samples)\n", "", (int32_t)z_gx2gy(info, (info->z_alloc / align) - (info->z_size << 1))); fprintf(stderr, "\t%12s = %10d\n", "z_gy2gx()", (int32_t)z_gy2gx(info, 1)); fprintf(stderr, "\t%12s = %10d -> z_gy2gx() -> %d\n", "Max", (int32_t)gy2gx_max, (int32_t)z_gy2gx(info, gy2gx_max)); fprintf(stderr, "\t%12s = %10d\n", "z_gx2gy()", (int32_t)z_gx2gy(info, 1)); fprintf(stderr, "\t%12s = %10d -> z_gx2gy() -> %d\n", "Max", (int32_t)gx2gy_max, (int32_t)z_gx2gy(info, gx2gy_max)); dumpz(maxfeed); dumpz(full); dumpz(start); dumpz(pos); dumpz(scale); fprintf(stderr, "\t%12s %10f\n", "", (double)info->z_scale / Z_ONE); dumpz(dx); fprintf(stderr, "\t%12s %10f\n", "", (double)info->z_dx / info->z_dy); dumpz(dy); fprintf(stderr, "\t%12s %10d (drift step)\n", "", info->z_dy >> Z_SHIFT); fprintf(stderr, "\t%12s %10d (scaling differences)\n", "", (z_scale << Z_DRIFT_SHIFT) - info->z_dy); fprintf(stderr, "\t%12s = %u bytes\n", "intpcm32_t", sizeof(intpcm32_t)); fprintf(stderr, "\t%12s = 0x%08x, smallest=%.16lf\n", "Z_ONE", Z_ONE, (double)1.0 / (double)Z_ONE); #endif return (0); } static int z_resampler_set(struct pcm_feeder *f, int what, int32_t value) { struct z_info *info; int32_t oquality; info = f->data; switch (what) { case Z_RATE_SRC: if (value < feeder_rate_min || value > feeder_rate_max) return (E2BIG); if (value == info->rsrc) return (0); info->rsrc = value; break; case Z_RATE_DST: if (value < feeder_rate_min || value > feeder_rate_max) return (E2BIG); if (value == info->rdst) return (0); info->rdst = value; break; case Z_RATE_QUALITY: if (value < Z_QUALITY_MIN || value > Z_QUALITY_MAX) return (EINVAL); if (value == info->quality) return (0); /* * If we failed to set the requested quality, restore * the old one. We cannot afford leaving it broken since * passive feeder chains like vchans never reinitialize * itself. */ oquality = info->quality; info->quality = value; if (z_resampler_setup(f) == 0) return (0); info->quality = oquality; break; case Z_RATE_CHANNELS: if (value < SND_CHN_MIN || value > SND_CHN_MAX) return (EINVAL); if (value == info->channels) return (0); info->channels = value; break; default: return (EINVAL); break; } return (z_resampler_setup(f)); } static int z_resampler_get(struct pcm_feeder *f, int what) { struct z_info *info; info = f->data; switch (what) { case Z_RATE_SRC: return (info->rsrc); break; case Z_RATE_DST: return (info->rdst); break; case Z_RATE_QUALITY: return (info->quality); break; case Z_RATE_CHANNELS: return (info->channels); break; default: break; } return (-1); } static int z_resampler_init(struct pcm_feeder *f) { struct z_info *info; int ret; if (f->desc->in != f->desc->out) return (EINVAL); info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT | M_ZERO); if (info == NULL) return (ENOMEM); info->rsrc = Z_RATE_DEFAULT; info->rdst = Z_RATE_DEFAULT; info->quality = feeder_rate_quality; info->channels = AFMT_CHANNEL(f->desc->in); f->data = info; ret = z_resampler_setup(f); if (ret != 0) { if (info->z_pcoeff != NULL) free(info->z_pcoeff, M_DEVBUF); if (info->z_delay != NULL) free(info->z_delay, M_DEVBUF); free(info, M_DEVBUF); f->data = NULL; } return (ret); } static int z_resampler_free(struct pcm_feeder *f) { struct z_info *info; info = f->data; if (info != NULL) { if (info->z_pcoeff != NULL) free(info->z_pcoeff, M_DEVBUF); if (info->z_delay != NULL) free(info->z_delay, M_DEVBUF); free(info, M_DEVBUF); } f->data = NULL; return (0); } static uint32_t z_resampler_feed_internal(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b, uint32_t count, void *source) { struct z_info *info; int32_t alphadrift, startdrift, reqout, ocount, reqin, align; int32_t fetch, fetched, start, cp; uint8_t *dst; info = f->data; if (info->z_resample == NULL) return (z_feed(f->source, c, b, count, source)); /* * Calculate sample size alignment and amount of sample output. * We will do everything in sample domain, but at the end we * will jump back to byte domain. */ align = info->channels * info->bps; ocount = SND_FXDIV(count, align); if (ocount == 0) return (0); /* * Calculate amount of input samples that is needed to generate * exact amount of output. */ reqin = z_gy2gx(info, ocount) - z_fetched(info); #ifdef Z_USE_ALPHADRIFT startdrift = info->z_startdrift; alphadrift = info->z_alphadrift; #else startdrift = _Z_GY2GX(info, 0, 1); alphadrift = z_drift(info, startdrift, 1); #endif dst = b; do { if (reqin != 0) { fetch = z_min(z_free(info), reqin); if (fetch == 0) { /* * No more free spaces, so wind enough * samples back to the head of delay line * in byte domain. */ fetched = z_fetched(info); start = z_prev(info, info->z_start, (info->z_size << 1) - 1); cp = (info->z_size << 1) + fetched; z_copy(info->z_delay + (start * align), info->z_delay, cp * align); info->z_start = z_prev(info, info->z_size << 1, 1); info->z_pos = z_next(info, info->z_start, fetched + 1); fetch = z_min(z_free(info), reqin); #ifdef Z_DIAGNOSTIC if (1) { static uint32_t kk = 0; fprintf(stderr, "Buffer Move: " "start=%d fetched=%d cp=%d " "cycle=%u [%u]\r", start, fetched, cp, info->z_cycle, ++kk); } info->z_cycle = 0; #endif } if (fetch != 0) { /* * Fetch in byte domain and jump back * to sample domain. */ fetched = SND_FXDIV(z_feed(f->source, c, info->z_delay + (info->z_pos * align), fetch * align, source), align); /* * Prepare to convert fetched buffer, * or mark us done if we cannot fulfill * the request. */ reqin -= fetched; info->z_pos += fetched; if (fetched != fetch) reqin = 0; } } reqout = z_min(z_gx2gy(info, z_fetched(info)), ocount); if (reqout != 0) { ocount -= reqout; /* * Drift.. drift.. drift.. * * Notice that there are 2 methods of doing the drift * operations: The former is much cleaner (in a sense * of mathematical readings of my eyes), but slower * due to integer division in z_gy2gx(). Nevertheless, * both should give the same exact accurate drifting * results, so the later is favourable. */ do { info->z_resample(info, dst); info->z_alpha += alphadrift; if (info->z_alpha < info->z_gy) info->z_start += startdrift; else { info->z_start += startdrift - 1; info->z_alpha -= info->z_gy; } dst += align; #ifdef Z_DIAGNOSTIC info->z_cycle++; #endif } while (--reqout != 0); } } while (reqin != 0 && ocount != 0); /* * Back to byte domain.. */ return (dst - b); } static int z_resampler_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b, uint32_t count, void *source) { uint32_t feed, maxfeed, left; /* * Split count to smaller chunks to avoid possible 32bit overflow. */ maxfeed = ((struct z_info *)(f->data))->z_maxfeed; left = count; do { feed = z_resampler_feed_internal(f, c, b, z_min(maxfeed, left), source); b += feed; left -= feed; } while (left != 0 && feed != 0); return (count - left); } static struct pcm_feederdesc feeder_rate_desc[] = { { FEEDER_RATE, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, }; static kobj_method_t feeder_rate_methods[] = { KOBJMETHOD(feeder_init, z_resampler_init), KOBJMETHOD(feeder_free, z_resampler_free), KOBJMETHOD(feeder_set, z_resampler_set), KOBJMETHOD(feeder_get, z_resampler_get), KOBJMETHOD(feeder_feed, z_resampler_feed), KOBJMETHOD_END }; FEEDER_DECLARE(feeder_rate, NULL); diff --git a/sys/dev/sound/pcm/feeder_volume.c b/sys/dev/sound/pcm/feeder_volume.c index 572bc980ffe3..f72c6aa7ef4f 100644 --- a/sys/dev/sound/pcm/feeder_volume.c +++ b/sys/dev/sound/pcm/feeder_volume.c @@ -1,352 +1,353 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2005-2009 Ariff Abdullah * 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. */ /* feeder_volume, a long 'Lost Technology' rather than a new feature. */ #ifdef _KERNEL #ifdef HAVE_KERNEL_OPTION_HEADERS #include "opt_snd.h" #endif #include #include #include "feeder_if.h" #define SND_USE_FXDIV #include "snd_fxdiv_gen.h" #endif typedef void (*feed_volume_t)(int *, int *, uint32_t, uint8_t *, uint32_t); #define FEEDVOLUME_CALC8(s, v) (SND_VOL_CALC_SAMPLE((intpcm_t) \ (s) << 8, v) >> 8) #define FEEDVOLUME_CALC16(s, v) SND_VOL_CALC_SAMPLE((intpcm_t)(s), v) #define FEEDVOLUME_CALC24(s, v) SND_VOL_CALC_SAMPLE((intpcm64_t)(s), v) #define FEEDVOLUME_CALC32(s, v) SND_VOL_CALC_SAMPLE((intpcm64_t)(s), v) #define FEEDVOLUME_DECLARE(SIGN, BIT, ENDIAN) \ static void \ feed_volume_##SIGN##BIT##ENDIAN(int *vol, int *matrix, \ uint32_t channels, uint8_t *dst, uint32_t count) \ { \ intpcm##BIT##_t v; \ intpcm_t x; \ uint32_t i; \ \ dst += count * PCM_##BIT##_BPS * channels; \ do { \ i = channels; \ do { \ dst -= PCM_##BIT##_BPS; \ i--; \ x = pcm_sample_read_calc(dst, \ AFMT_##SIGN##BIT##_##ENDIAN); \ v = FEEDVOLUME_CALC##BIT(x, vol[matrix[i]]); \ - x = PCM_CLAMP_##SIGN##BIT(v); \ + x = pcm_clamp_calc(v, \ + AFMT_##SIGN##BIT##_##ENDIAN); \ pcm_sample_write(dst, x, \ AFMT_##SIGN##BIT##_##ENDIAN); \ } while (i != 0); \ } while (--count != 0); \ } #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) FEEDVOLUME_DECLARE(S, 16, LE) FEEDVOLUME_DECLARE(S, 32, LE) #endif #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) FEEDVOLUME_DECLARE(S, 16, BE) FEEDVOLUME_DECLARE(S, 32, BE) #endif #ifdef SND_FEEDER_MULTIFORMAT FEEDVOLUME_DECLARE(S, 8, NE) FEEDVOLUME_DECLARE(S, 24, LE) FEEDVOLUME_DECLARE(S, 24, BE) FEEDVOLUME_DECLARE(U, 8, NE) FEEDVOLUME_DECLARE(U, 16, LE) FEEDVOLUME_DECLARE(U, 24, LE) FEEDVOLUME_DECLARE(U, 32, LE) FEEDVOLUME_DECLARE(U, 16, BE) FEEDVOLUME_DECLARE(U, 24, BE) FEEDVOLUME_DECLARE(U, 32, BE) #endif struct feed_volume_info { uint32_t bps, channels; feed_volume_t apply; int volume_class; int state; int matrix[SND_CHN_MAX]; }; #define FEEDVOLUME_ENTRY(SIGN, BIT, ENDIAN) \ { \ AFMT_##SIGN##BIT##_##ENDIAN, \ feed_volume_##SIGN##BIT##ENDIAN \ } static const struct { uint32_t format; feed_volume_t apply; } feed_volume_info_tab[] = { #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) FEEDVOLUME_ENTRY(S, 16, LE), FEEDVOLUME_ENTRY(S, 32, LE), #endif #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) FEEDVOLUME_ENTRY(S, 16, BE), FEEDVOLUME_ENTRY(S, 32, BE), #endif #ifdef SND_FEEDER_MULTIFORMAT FEEDVOLUME_ENTRY(S, 8, NE), FEEDVOLUME_ENTRY(S, 24, LE), FEEDVOLUME_ENTRY(S, 24, BE), FEEDVOLUME_ENTRY(U, 8, NE), FEEDVOLUME_ENTRY(U, 16, LE), FEEDVOLUME_ENTRY(U, 24, LE), FEEDVOLUME_ENTRY(U, 32, LE), FEEDVOLUME_ENTRY(U, 16, BE), FEEDVOLUME_ENTRY(U, 24, BE), FEEDVOLUME_ENTRY(U, 32, BE) #endif }; #define FEEDVOLUME_TAB_SIZE ((int32_t) \ (sizeof(feed_volume_info_tab) / \ sizeof(feed_volume_info_tab[0]))) static int feed_volume_init(struct pcm_feeder *f) { struct feed_volume_info *info; struct pcmchan_matrix *m; uint32_t i; int ret; if (f->desc->in != f->desc->out || AFMT_CHANNEL(f->desc->in) > SND_CHN_MAX) return (EINVAL); for (i = 0; i < FEEDVOLUME_TAB_SIZE; i++) { if (AFMT_ENCODING(f->desc->in) == feed_volume_info_tab[i].format) { info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT | M_ZERO); if (info == NULL) return (ENOMEM); info->bps = AFMT_BPS(f->desc->in); info->channels = AFMT_CHANNEL(f->desc->in); info->apply = feed_volume_info_tab[i].apply; info->volume_class = SND_VOL_C_PCM; info->state = FEEDVOLUME_ENABLE; f->data = info; m = feeder_matrix_default_channel_map(info->channels); if (m == NULL) { free(info, M_DEVBUF); return (EINVAL); } ret = feeder_volume_apply_matrix(f, m); if (ret != 0) free(info, M_DEVBUF); return (ret); } } return (EINVAL); } static int feed_volume_free(struct pcm_feeder *f) { struct feed_volume_info *info; info = f->data; if (info != NULL) free(info, M_DEVBUF); f->data = NULL; return (0); } static int feed_volume_set(struct pcm_feeder *f, int what, int value) { struct feed_volume_info *info; struct pcmchan_matrix *m; int ret; info = f->data; ret = 0; switch (what) { case FEEDVOLUME_CLASS: if (value < SND_VOL_C_BEGIN || value > SND_VOL_C_END) return (EINVAL); info->volume_class = value; break; case FEEDVOLUME_CHANNELS: if (value < SND_CHN_MIN || value > SND_CHN_MAX) return (EINVAL); m = feeder_matrix_default_channel_map(value); if (m == NULL) return (EINVAL); ret = feeder_volume_apply_matrix(f, m); break; case FEEDVOLUME_STATE: if (!(value == FEEDVOLUME_ENABLE || value == FEEDVOLUME_BYPASS)) return (EINVAL); info->state = value; break; default: return (EINVAL); break; } return (ret); } static int feed_volume_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b, uint32_t count, void *source) { int temp_vol[SND_CHN_T_VOL_MAX]; struct feed_volume_info *info; uint32_t j, align; int i, *matrix; uint8_t *dst; const int16_t *vol; const int8_t *muted; /* * Fetch filter data operation. */ info = f->data; if (info->state == FEEDVOLUME_BYPASS) return (FEEDER_FEED(f->source, c, b, count, source)); vol = c->volume[SND_VOL_C_VAL(info->volume_class)]; muted = c->muted[SND_VOL_C_VAL(info->volume_class)]; matrix = info->matrix; /* * First, let see if we really need to apply gain at all. */ j = 0; i = info->channels; while (i--) { if (vol[matrix[i]] != SND_VOL_FLAT || muted[matrix[i]] != 0) { j = 1; break; } } /* Nope, just bypass entirely. */ if (j == 0) return (FEEDER_FEED(f->source, c, b, count, source)); /* Check if any controls are muted. */ for (j = 0; j != SND_CHN_T_VOL_MAX; j++) temp_vol[j] = muted[j] ? 0 : vol[j]; dst = b; align = info->bps * info->channels; do { if (count < align) break; j = SND_FXDIV(FEEDER_FEED(f->source, c, dst, count, source), align); if (j == 0) break; info->apply(temp_vol, matrix, info->channels, dst, j); j *= align; dst += j; count -= j; } while (count != 0); return (dst - b); } static struct pcm_feederdesc feeder_volume_desc[] = { { FEEDER_VOLUME, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } }; static kobj_method_t feeder_volume_methods[] = { KOBJMETHOD(feeder_init, feed_volume_init), KOBJMETHOD(feeder_free, feed_volume_free), KOBJMETHOD(feeder_set, feed_volume_set), KOBJMETHOD(feeder_feed, feed_volume_feed), KOBJMETHOD_END }; FEEDER_DECLARE(feeder_volume, NULL); /* Extern */ /* * feeder_volume_apply_matrix(): For given matrix map, apply its configuration * to feeder_volume matrix structure. There are * possibilites that feeder_volume be inserted * before or after feeder_matrix, which in this * case feeder_volume must be in a good terms * with _current_ matrix. */ int feeder_volume_apply_matrix(struct pcm_feeder *f, struct pcmchan_matrix *m) { struct feed_volume_info *info; uint32_t i; if (f == NULL || f->desc == NULL || f->desc->type != FEEDER_VOLUME || f->data == NULL || m == NULL || m->channels < SND_CHN_MIN || m->channels > SND_CHN_MAX) return (EINVAL); info = f->data; for (i = 0; i < nitems(info->matrix); i++) { if (i < m->channels) info->matrix[i] = m->map[i].type; else info->matrix[i] = SND_CHN_T_FL; } info->channels = m->channels; return (0); } diff --git a/sys/dev/sound/pcm/pcm.h b/sys/dev/sound/pcm/pcm.h index f18d28b3b196..3b3b083457ee 100644 --- a/sys/dev/sound/pcm/pcm.h +++ b/sys/dev/sound/pcm/pcm.h @@ -1,373 +1,385 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2006-2009 Ariff Abdullah * 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. */ #ifndef _SND_PCM_H_ #define _SND_PCM_H_ #include #include #ifndef _KERNEL #include /* for __assert_unreachable() */ #endif /* * Automatically turn on 64bit arithmetic on suitable archs * (amd64 64bit, etc..) for wider 32bit samples / integer processing. */ #if LONG_BIT >= 64 #undef SND_PCM_64 #define SND_PCM_64 1 #endif typedef int32_t intpcm_t; typedef int32_t intpcm8_t; typedef int32_t intpcm16_t; typedef int32_t intpcm24_t; typedef uint32_t uintpcm_t; typedef uint32_t uintpcm8_t; typedef uint32_t uintpcm16_t; typedef uint32_t uintpcm24_t; #ifdef SND_PCM_64 typedef int64_t intpcm32_t; typedef uint64_t uintpcm32_t; #else typedef int32_t intpcm32_t; typedef uint32_t uintpcm32_t; #endif typedef int64_t intpcm64_t; typedef uint64_t uintpcm64_t; /* 32bit fixed point shift */ #define PCM_FXSHIFT 8 #define PCM_S8_MAX 0x7f #define PCM_S8_MIN -0x80 #define PCM_S16_MAX 0x7fff #define PCM_S16_MIN -0x8000 #define PCM_S24_MAX 0x7fffff #define PCM_S24_MIN -0x800000 #ifdef SND_PCM_64 #if LONG_BIT >= 64 #define PCM_S32_MAX 0x7fffffffL #define PCM_S32_MIN -0x80000000L #else #define PCM_S32_MAX 0x7fffffffLL #define PCM_S32_MIN -0x80000000LL #endif #else #define PCM_S32_MAX 0x7fffffff #define PCM_S32_MIN (-0x7fffffff - 1) #endif /* Bytes-per-sample definition */ #define PCM_8_BPS 1 #define PCM_16_BPS 2 #define PCM_24_BPS 3 #define PCM_32_BPS 4 #define INTPCM_T(v) ((intpcm_t)(v)) #define INTPCM8_T(v) ((intpcm8_t)(v)) #define INTPCM16_T(v) ((intpcm16_t)(v)) #define INTPCM24_T(v) ((intpcm24_t)(v)) #define INTPCM32_T(v) ((intpcm32_t)(v)) -#define PCM_CLAMP_S8(val) \ - (((val) > PCM_S8_MAX) ? PCM_S8_MAX : \ - (((val) < PCM_S8_MIN) ? PCM_S8_MIN : (val))) -#define PCM_CLAMP_S16(val) \ - (((val) > PCM_S16_MAX) ? PCM_S16_MAX : \ - (((val) < PCM_S16_MIN) ? PCM_S16_MIN : (val))) -#define PCM_CLAMP_S24(val) \ - (((val) > PCM_S24_MAX) ? PCM_S24_MAX : \ - (((val) < PCM_S24_MIN) ? PCM_S24_MIN : (val))) - -#ifdef SND_PCM_64 -#define PCM_CLAMP_S32(val) \ - (((val) > PCM_S32_MAX) ? PCM_S32_MAX : \ - (((val) < PCM_S32_MIN) ? PCM_S32_MIN : (val))) -#else /* !SND_PCM_64 */ -#define PCM_CLAMP_S32(val) \ - (((val) > PCM_S24_MAX) ? PCM_S32_MAX : \ - (((val) < PCM_S24_MIN) ? PCM_S32_MIN : \ - ((val) << PCM_FXSHIFT))) -#endif /* SND_PCM_64 */ - -#define PCM_CLAMP_U8(val) PCM_CLAMP_S8(val) -#define PCM_CLAMP_U16(val) PCM_CLAMP_S16(val) -#define PCM_CLAMP_U24(val) PCM_CLAMP_S24(val) -#define PCM_CLAMP_U32(val) PCM_CLAMP_S32(val) - static const struct { const uint8_t ulaw_to_u8[G711_TABLE_SIZE]; const uint8_t alaw_to_u8[G711_TABLE_SIZE]; const uint8_t u8_to_ulaw[G711_TABLE_SIZE]; const uint8_t u8_to_alaw[G711_TABLE_SIZE]; } xlaw_conv_tables = { ULAW_TO_U8, ALAW_TO_U8, U8_TO_ULAW, U8_TO_ALAW }; /* * Functions for reading/writing PCM integer sample values from bytes array. * Since every process is done using signed integer (and to make our life less * miserable), unsigned sample will be converted to its signed counterpart and * restored during writing back. */ static __always_inline __unused intpcm_t pcm_sample_read(const uint8_t *src, uint32_t fmt) { intpcm_t v; fmt = AFMT_ENCODING(fmt); switch (fmt) { case AFMT_AC3: v = 0; break; case AFMT_MU_LAW: v = _G711_TO_INTPCM(xlaw_conv_tables.ulaw_to_u8, *src); break; case AFMT_A_LAW: v = _G711_TO_INTPCM(xlaw_conv_tables.alaw_to_u8, *src); break; case AFMT_S8: v = INTPCM_T((int8_t)*src); break; case AFMT_U8: v = INTPCM_T((int8_t)(*src ^ 0x80)); break; case AFMT_S16_LE: v = INTPCM_T(src[0] | (int8_t)src[1] << 8); break; case AFMT_S16_BE: v = INTPCM_T(src[1] | (int8_t)src[0] << 8); break; case AFMT_U16_LE: v = INTPCM_T(src[0] | (int8_t)(src[1] ^ 0x80) << 8); break; case AFMT_U16_BE: v = INTPCM_T(src[1] | (int8_t)(src[0] ^ 0x80) << 8); break; case AFMT_S24_LE: v = INTPCM_T(src[0] | src[1] << 8 | (int8_t)src[2] << 16); break; case AFMT_S24_BE: v = INTPCM_T(src[2] | src[1] << 8 | (int8_t)src[0] << 16); break; case AFMT_U24_LE: v = INTPCM_T(src[0] | src[1] << 8 | (int8_t)(src[2] ^ 0x80) << 16); break; case AFMT_U24_BE: v = INTPCM_T(src[2] | src[1] << 8 | (int8_t)(src[0] ^ 0x80) << 16); break; case AFMT_S32_LE: v = INTPCM_T(src[0] | src[1] << 8 | src[2] << 16 | (int8_t)src[3] << 24); break; case AFMT_S32_BE: v = INTPCM_T(src[3] | src[2] << 8 | src[1] << 16 | (int8_t)src[0] << 24); break; case AFMT_U32_LE: v = INTPCM_T(src[0] | src[1] << 8 | src[2] << 16 | (int8_t)(src[3] ^ 0x80) << 24); break; case AFMT_U32_BE: v = INTPCM_T(src[3] | src[2] << 8 | src[1] << 16 | (int8_t)(src[0] ^ 0x80) << 24); break; default: v = 0; printf("%s(): unknown format: 0x%08x\n", __func__, fmt); __assert_unreachable(); } return (v); } /* * Read sample and normalize to 32-bit magnitude. */ static __always_inline __unused intpcm_t pcm_sample_read_norm(const uint8_t *src, uint32_t fmt) { return (pcm_sample_read(src, fmt) << (32 - AFMT_BIT(fmt))); } /* * Read sample and restrict magnitude to 24 bits. */ static __always_inline __unused intpcm_t pcm_sample_read_calc(const uint8_t *src, uint32_t fmt) { intpcm_t v; v = pcm_sample_read(src, fmt); #ifndef SND_PCM_64 /* * Dynamic range for humans: ~140db. * * 16bit = 96db (close enough) * 24bit = 144db (perfect) * 32bit = 196db (way too much) * * 24bit is pretty much sufficient for our signed integer processing. * Also, to avoid overflow, we truncate 32bit (and only 32bit) samples * down to 24bit (see below for the reason), unless SND_PCM_64 is * defined. */ if (fmt & AFMT_32BIT) v >>= PCM_FXSHIFT; #endif return (v); } static __always_inline __unused void pcm_sample_write(uint8_t *dst, intpcm_t v, uint32_t fmt) { fmt = AFMT_ENCODING(fmt); switch (fmt) { case AFMT_AC3: *(int16_t *)dst = 0; break; case AFMT_MU_LAW: *dst = _INTPCM_TO_G711(xlaw_conv_tables.u8_to_ulaw, v); break; case AFMT_A_LAW: *dst = _INTPCM_TO_G711(xlaw_conv_tables.u8_to_alaw, v); break; case AFMT_S8: *(int8_t *)dst = v; break; case AFMT_U8: *(int8_t *)dst = v ^ 0x80; break; case AFMT_S16_LE: dst[0] = v; dst[1] = v >> 8; break; case AFMT_S16_BE: dst[1] = v; dst[0] = v >> 8; break; case AFMT_U16_LE: dst[0] = v; dst[1] = (v >> 8) ^ 0x80; break; case AFMT_U16_BE: dst[1] = v; dst[0] = (v >> 8) ^ 0x80; break; case AFMT_S24_LE: dst[0] = v; dst[1] = v >> 8; dst[2] = v >> 16; break; case AFMT_S24_BE: dst[2] = v; dst[1] = v >> 8; dst[0] = v >> 16; break; case AFMT_U24_LE: dst[0] = v; dst[1] = v >> 8; dst[2] = (v >> 16) ^ 0x80; break; case AFMT_U24_BE: dst[2] = v; dst[1] = v >> 8; dst[0] = (v >> 16) ^ 0x80; break; case AFMT_S32_LE: dst[0] = v; dst[1] = v >> 8; dst[2] = v >> 16; dst[3] = v >> 24; break; case AFMT_S32_BE: dst[3] = v; dst[2] = v >> 8; dst[1] = v >> 16; dst[0] = v >> 24; break; case AFMT_U32_LE: dst[0] = v; dst[1] = v >> 8; dst[2] = v >> 16; dst[3] = (v >> 24) ^ 0x80; break; case AFMT_U32_BE: dst[3] = v; dst[2] = v >> 8; dst[1] = v >> 16; dst[0] = (v >> 24) ^ 0x80; break; default: printf("%s(): unknown format: 0x%08x\n", __func__, fmt); __assert_unreachable(); } } /* * Write sample and normalize to original magnitude. */ static __always_inline __unused void pcm_sample_write_norm(uint8_t *dst, intpcm_t v, uint32_t fmt) { pcm_sample_write(dst, v >> (32 - AFMT_BIT(fmt)), fmt); } /* * To be used with pcm_sample_read_calc(). */ static __always_inline __unused void pcm_sample_write_calc(uint8_t *dst, intpcm_t v, uint32_t fmt) { #ifndef SND_PCM_64 /* Shift back to 32-bit magnitude. */ if (fmt & AFMT_32BIT) v <<= PCM_FXSHIFT; #endif pcm_sample_write(dst, v, fmt); } +static __always_inline __unused intpcm_t +pcm_clamp(intpcm32_t sample, uint32_t fmt) +{ + fmt = AFMT_ENCODING(fmt); + + switch (AFMT_BIT(fmt)) { + case 8: + return ((sample > PCM_S8_MAX) ? PCM_S8_MAX : + ((sample < PCM_S8_MIN) ? PCM_S8_MIN : sample)); + case 16: + return ((sample > PCM_S16_MAX) ? PCM_S16_MAX : + ((sample < PCM_S16_MIN) ? PCM_S16_MIN : sample)); + case 24: + return ((sample > PCM_S24_MAX) ? PCM_S24_MAX : + ((sample < PCM_S24_MIN) ? PCM_S24_MIN : sample)); + case 32: + return ((sample > PCM_S32_MAX) ? PCM_S32_MAX : + ((sample < PCM_S32_MIN) ? PCM_S32_MIN : sample)); + default: + printf("%s(): unknown format: 0x%08x\n", __func__, fmt); + __assert_unreachable(); + } +} + +static __always_inline __unused intpcm_t +pcm_clamp_calc(intpcm32_t sample, uint32_t fmt) +{ +#ifndef SND_PCM_64 + if (fmt & AFMT_32BIT) { + return ((sample > PCM_S24_MAX) ? PCM_S32_MAX : + ((sample < PCM_S24_MIN) ? PCM_S32_MIN : + sample << PCM_FXSHIFT)); + } +#endif + + return (pcm_clamp(sample, fmt)); +} + #endif /* !_SND_PCM_H_ */