2 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * feeder_rate: (Codename: Z Resampler), which means any effort to create
29 * future replacement for this resampler are simply absurd unless
30 * the world decide to add new alphabet after Z.
32 * FreeBSD bandlimited sinc interpolator, technically based on
33 * "Digital Audio Resampling" by Julius O. Smith III
34 * - http://ccrma.stanford.edu/~jos/resample/
37 * + all out fixed point integer operations, no soft-float or anything like
39 * + classic polyphase converters with high quality coefficient's polynomial
41 * + fast, faster, or the fastest of its kind.
42 * + compile time configurable.
46 * - The z, z_, and Z_ . Due to mental block (or maybe just 0x7a69), I
47 * couldn't think of anything simpler than that (feeder_rate_xxx is just
48 * too long). Expect possible clashes with other zitizens (any?).
52 #ifdef HAVE_KERNEL_OPTION_HEADERS
55 #include <dev/sound/pcm/sound.h>
56 #include <dev/sound/pcm/pcm.h>
57 #include "feeder_if.h"
60 #include "snd_fxdiv_gen.h"
62 SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/feeder_rate.c 267992 2014-06-28 03:56:17Z hselasky $");
65 #include "feeder_rate_gen.h"
67 #if !defined(_KERNEL) && defined(SND_DIAGNOSTIC)
69 #define Z_DIAGNOSTIC 1
70 #elif defined(_KERNEL)
74 #ifndef Z_QUALITY_DEFAULT
75 #define Z_QUALITY_DEFAULT Z_QUALITY_LINEAR
78 #define Z_RESERVOIR 2048
79 #define Z_RESERVOIR_MAX 131072
81 #define Z_SINC_MAX 0x3fffff
82 #define Z_SINC_DOWNMAX 48 /* 384000 / 8000 */
85 #define Z_POLYPHASE_MAX 183040 /* 286 taps, 640 phases */
87 #define Z_POLYPHASE_MAX 1464320 /* 286 taps, 5120 phases */
90 #define Z_RATE_DEFAULT 48000
92 #define Z_RATE_MIN FEEDRATE_RATEMIN
93 #define Z_RATE_MAX FEEDRATE_RATEMAX
94 #define Z_ROUNDHZ FEEDRATE_ROUNDHZ
95 #define Z_ROUNDHZ_MIN FEEDRATE_ROUNDHZ_MIN
96 #define Z_ROUNDHZ_MAX FEEDRATE_ROUNDHZ_MAX
98 #define Z_RATE_SRC FEEDRATE_SRC
99 #define Z_RATE_DST FEEDRATE_DST
100 #define Z_RATE_QUALITY FEEDRATE_QUALITY
101 #define Z_RATE_CHANNELS FEEDRATE_CHANNELS
105 #define Z_MULTIFORMAT 1
108 #undef Z_USE_ALPHADRIFT
109 #define Z_USE_ALPHADRIFT 1
112 #define Z_FACTOR_MIN 1
113 #define Z_FACTOR_MAX Z_MASK
114 #define Z_FACTOR_SAFE(v) (!((v) < Z_FACTOR_MIN || (v) > Z_FACTOR_MAX))
118 typedef void (*z_resampler_t)(struct z_info *, uint8_t *);
121 int32_t rsrc, rdst; /* original source / destination rates */
122 int32_t src, dst; /* rounded source / destination rates */
123 int32_t channels; /* total channels */
124 int32_t bps; /* bytes-per-sample */
125 int32_t quality; /* resampling quality */
127 int32_t z_gx, z_gy; /* interpolation / decimation ratio */
128 int32_t z_alpha; /* output sample time phase / drift */
129 uint8_t *z_delay; /* FIR delay line / linear buffer */
130 int32_t *z_coeff; /* FIR coefficients */
131 int32_t *z_dcoeff; /* FIR coefficients differences */
132 int32_t *z_pcoeff; /* FIR polyphase coefficients */
133 int32_t z_scale; /* output scaling */
134 int32_t z_dx; /* input sample drift increment */
135 int32_t z_dy; /* output sample drift increment */
136 #ifdef Z_USE_ALPHADRIFT
137 int32_t z_alphadrift; /* alpha drift rate */
138 int32_t z_startdrift; /* buffer start position drift rate */
140 int32_t z_mask; /* delay line full length mask */
141 int32_t z_size; /* half width of FIR taps */
142 int32_t z_full; /* full size of delay line */
143 int32_t z_alloc; /* largest allocated full size of delay line */
144 int32_t z_start; /* buffer processing start position */
145 int32_t z_pos; /* current position for the next feed */
147 uint32_t z_cycle; /* output cycle, purely for statistical */
149 int32_t z_maxfeed; /* maximum feed to avoid 32bit overflow */
151 z_resampler_t z_resample;
154 int feeder_rate_min = Z_RATE_MIN;
155 int feeder_rate_max = Z_RATE_MAX;
156 int feeder_rate_round = Z_ROUNDHZ;
157 int feeder_rate_quality = Z_QUALITY_DEFAULT;
159 static int feeder_rate_polyphase_max = Z_POLYPHASE_MAX;
162 static char feeder_rate_presets[] = FEEDER_RATE_PRESETS;
163 SYSCTL_STRING(_hw_snd, OID_AUTO, feeder_rate_presets, CTLFLAG_RD,
164 &feeder_rate_presets, 0, "compile-time rate presets");
166 TUNABLE_INT("hw.snd.feeder_rate_min", &feeder_rate_min);
167 TUNABLE_INT("hw.snd.feeder_rate_max", &feeder_rate_max);
168 TUNABLE_INT("hw.snd.feeder_rate_round", &feeder_rate_round);
169 TUNABLE_INT("hw.snd.feeder_rate_quality", &feeder_rate_quality);
171 SYSCTL_INT(_hw_snd, OID_AUTO, feeder_rate_polyphase_max, CTLFLAG_RWTUN,
172 &feeder_rate_polyphase_max, 0, "maximum allowable polyphase entries");
175 sysctl_hw_snd_feeder_rate_min(SYSCTL_HANDLER_ARGS)
179 val = feeder_rate_min;
180 err = sysctl_handle_int(oidp, &val, 0, req);
182 if (err != 0 || req->newptr == NULL || val == feeder_rate_min)
185 if (!(Z_FACTOR_SAFE(val) && val < feeder_rate_max))
188 feeder_rate_min = val;
192 SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_min, CTLTYPE_INT | CTLFLAG_RW,
193 0, sizeof(int), sysctl_hw_snd_feeder_rate_min, "I",
194 "minimum allowable rate");
197 sysctl_hw_snd_feeder_rate_max(SYSCTL_HANDLER_ARGS)
201 val = feeder_rate_max;
202 err = sysctl_handle_int(oidp, &val, 0, req);
204 if (err != 0 || req->newptr == NULL || val == feeder_rate_max)
207 if (!(Z_FACTOR_SAFE(val) && val > feeder_rate_min))
210 feeder_rate_max = val;
214 SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_max, CTLTYPE_INT | CTLFLAG_RW,
215 0, sizeof(int), sysctl_hw_snd_feeder_rate_max, "I",
216 "maximum allowable rate");
219 sysctl_hw_snd_feeder_rate_round(SYSCTL_HANDLER_ARGS)
223 val = feeder_rate_round;
224 err = sysctl_handle_int(oidp, &val, 0, req);
226 if (err != 0 || req->newptr == NULL || val == feeder_rate_round)
229 if (val < Z_ROUNDHZ_MIN || val > Z_ROUNDHZ_MAX)
232 feeder_rate_round = val - (val % Z_ROUNDHZ);
236 SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_round, CTLTYPE_INT | CTLFLAG_RW,
237 0, sizeof(int), sysctl_hw_snd_feeder_rate_round, "I",
238 "sample rate converter rounding threshold");
241 sysctl_hw_snd_feeder_rate_quality(SYSCTL_HANDLER_ARGS)
243 struct snddev_info *d;
244 struct pcm_channel *c;
245 struct pcm_feeder *f;
248 val = feeder_rate_quality;
249 err = sysctl_handle_int(oidp, &val, 0, req);
251 if (err != 0 || req->newptr == NULL || val == feeder_rate_quality)
254 if (val < Z_QUALITY_MIN || val > Z_QUALITY_MAX)
257 feeder_rate_quality = val;
260 * Traverse all available channels on each device and try to
261 * set resampler quality if and only if it is exist as
262 * part of feeder chains and the channel is idle.
264 for (i = 0; pcm_devclass != NULL &&
265 i < devclass_get_maxunit(pcm_devclass); i++) {
266 d = devclass_get_softc(pcm_devclass, i);
267 if (!PCM_REGISTERED(d))
272 CHN_FOREACH(c, d, channels.pcm) {
274 f = chn_findfeeder(c, FEEDER_RATE);
275 if (f == NULL || f->data == NULL || CHN_STARTED(c)) {
279 (void)FEEDER_SET(f, FEEDRATE_QUALITY, val);
288 SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_quality, CTLTYPE_INT | CTLFLAG_RW,
289 0, sizeof(int), sysctl_hw_snd_feeder_rate_quality, "I",
290 "sample rate converter quality ("__XSTRING(Z_QUALITY_MIN)"=low .. "
291 __XSTRING(Z_QUALITY_MAX)"=high)");
298 #define Z_IS_ZOH(i) ((i)->quality == Z_QUALITY_ZOH)
299 #define Z_IS_LINEAR(i) ((i)->quality == Z_QUALITY_LINEAR)
300 #define Z_IS_SINC(i) ((i)->quality > Z_QUALITY_LINEAR)
303 * Macroses for accurate sample time drift calculations.
305 * gy2gx : given the amount of output, return the _exact_ required amount of
307 * gx2gy : given the amount of input, return the _maximum_ amount of output
308 * that will be generated.
309 * drift : given the amount of input and output, return the elapsed
312 #define _Z_GCAST(x) ((uint64_t)(x))
314 #if defined(__GNUCLIKE_ASM) && defined(__i386__)
316 * This is where i386 being beaten to a pulp. Fortunately this function is
317 * rarely being called and if it is, it will decide the best (hopefully)
318 * fastest way to do the division. If we can ensure that everything is dword
319 * aligned, letting the compiler to call udivdi3 to do the division can be
320 * faster compared to this.
322 * amd64 is the clear winner here, no question about it.
324 static __inline uint32_t
325 Z_DIV(uint64_t v, uint32_t d)
327 uint32_t hi, lo, quo, rem;
333 * As much as we can, try to avoid long division like a plague.
339 : "=a" (quo), "=d" (rem)
340 : "r" (d), "0" (lo), "1" (hi));
345 #define Z_DIV(x, y) ((x) / (y))
348 #define _Z_GY2GX(i, a, v) \
349 Z_DIV(((_Z_GCAST((i)->z_gx) * (v)) + ((i)->z_gy - (a) - 1)), \
352 #define _Z_GX2GY(i, a, v) \
353 Z_DIV(((_Z_GCAST((i)->z_gy) * (v)) + (a)), (i)->z_gx)
355 #define _Z_DRIFT(i, x, y) \
356 ((_Z_GCAST((i)->z_gy) * (x)) - (_Z_GCAST((i)->z_gx) * (y)))
358 #define z_gy2gx(i, v) _Z_GY2GX(i, (i)->z_alpha, v)
359 #define z_gx2gy(i, v) _Z_GX2GY(i, (i)->z_alpha, v)
360 #define z_drift(i, x, y) _Z_DRIFT(i, x, y)
363 * Macroses for SINC coefficients table manipulations.. whatever.
365 #define Z_SINC_COEFF_IDX(i) ((i)->quality - Z_QUALITY_LINEAR - 1)
367 #define Z_SINC_LEN(i) \
368 ((int32_t)(((uint64_t)z_coeff_tab[Z_SINC_COEFF_IDX(i)].len << \
369 Z_SHIFT) / (i)->z_dy))
371 #define Z_SINC_BASE_LEN(i) \
372 ((z_coeff_tab[Z_SINC_COEFF_IDX(i)].len - 1) >> (Z_DRIFT_SHIFT - 1))
375 * Macroses for linear delay buffer operations. Alignment is not
376 * really necessary since we're not using true circular buffer, but it
377 * will help us guard against possible trespasser. To be honest,
378 * the linear block operations does not need guarding at all due to
381 #define z_align(i, v) ((v) & (i)->z_mask)
382 #define z_next(i, o, v) z_align(i, (o) + (v))
383 #define z_prev(i, o, v) z_align(i, (o) - (v))
384 #define z_fetched(i) (z_align(i, (i)->z_pos - (i)->z_start) - 1)
385 #define z_free(i) ((i)->z_full - (i)->z_pos)
388 * Macroses for Bla Bla .. :)
390 #define z_copy(src, dst, sz) (void)memcpy(dst, src, sz)
391 #define z_feed(...) FEEDER_FEED(__VA_ARGS__)
393 static __inline uint32_t
394 z_min(uint32_t x, uint32_t y)
397 return ((x < y) ? x : y);
401 z_gcd(int32_t x, int32_t y)
415 z_roundpow2(int32_t v)
422 * Let it overflow at will..
424 while (i > 0 && i < v)
431 * Zero Order Hold, the worst of the worst, an insult against quality,
435 z_feed_zoh(struct z_info *info, uint8_t *dst)
438 z_copy(info->z_delay +
439 (info->z_start * info->channels * info->bps), dst,
440 info->channels * info->bps);
445 cnt = info->channels * info->bps;
446 src = info->z_delay + (info->z_start * cnt);
449 * This is a bit faster than doing bcopy() since we're dealing
450 * with possible unaligned samples.
454 } while (--cnt != 0);
459 * Linear Interpolation. This at least sounds better (perceptually) and fast,
460 * but without any proper filtering which means aliasing still exist and
461 * could become worst with a right sample. Interpolation centered within
462 * Z_LINEAR_ONE between the present and previous sample and everything is
463 * done with simple 32bit scaling arithmetic.
465 #define Z_DECLARE_LINEAR(SIGN, BIT, ENDIAN) \
467 z_feed_linear_##SIGN##BIT##ENDIAN(struct z_info *info, uint8_t *dst) \
474 z = ((uint32_t)info->z_alpha * info->z_dx) >> Z_LINEAR_UNSHIFT; \
476 sx = info->z_delay + (info->z_start * info->channels * \
478 sy = sx - (info->channels * PCM_##BIT##_BPS); \
480 ch = info->channels; \
483 x = _PCM_READ_##SIGN##BIT##_##ENDIAN(sx); \
484 y = _PCM_READ_##SIGN##BIT##_##ENDIAN(sy); \
485 x = Z_LINEAR_INTERPOLATE_##BIT(z, x, y); \
486 _PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, x); \
487 sx += PCM_##BIT##_BPS; \
488 sy += PCM_##BIT##_BPS; \
489 dst += PCM_##BIT##_BPS; \
490 } while (--ch != 0); \
494 * Userland clipping diagnostic check, not enabled in kernel compilation.
495 * While doing sinc interpolation, unrealistic samples like full scale sine
496 * wav will clip, but for other things this will not make any noise at all.
497 * Everybody should learn how to normalized perceived loudness of their own
498 * music/sounds/samples (hint: ReplayGain).
501 #define Z_CLIP_CHECK(v, BIT) do { \
502 if ((v) > PCM_S##BIT##_MAX) { \
503 fprintf(stderr, "Overflow: v=%jd, max=%jd\n", \
504 (intmax_t)(v), (intmax_t)PCM_S##BIT##_MAX); \
505 } else if ((v) < PCM_S##BIT##_MIN) { \
506 fprintf(stderr, "Underflow: v=%jd, min=%jd\n", \
507 (intmax_t)(v), (intmax_t)PCM_S##BIT##_MIN); \
511 #define Z_CLIP_CHECK(...)
514 #define Z_CLAMP(v, BIT) \
515 (((v) > PCM_S##BIT##_MAX) ? PCM_S##BIT##_MAX : \
516 (((v) < PCM_S##BIT##_MIN) ? PCM_S##BIT##_MIN : (v)))
519 * Sine Cardinal (SINC) Interpolation. Scaling is done in 64 bit, so
520 * there's no point to hold the plate any longer. All samples will be
521 * shifted to a full 32 bit, scaled and restored during write for
522 * maximum dynamic range (only for downsampling).
524 #define _Z_SINC_ACCUMULATE(SIGN, BIT, ENDIAN, adv) \
527 coeff = Z_COEFF_INTERPOLATE(z, z_coeff[c], z_dcoeff[c]); \
528 x = _PCM_READ_##SIGN##BIT##_##ENDIAN(p); \
529 v += Z_NORM_##BIT((intpcm64_t)x * coeff); \
531 p adv##= info->channels * PCM_##BIT##_BPS
534 * XXX GCC4 optimization is such a !@#$%, need manual unrolling.
536 #if defined(__GNUC__) && __GNUC__ >= 4
537 #define Z_SINC_ACCUMULATE(...) do { \
538 _Z_SINC_ACCUMULATE(__VA_ARGS__); \
539 _Z_SINC_ACCUMULATE(__VA_ARGS__); \
541 #define Z_SINC_ACCUMULATE_DECR 2
543 #define Z_SINC_ACCUMULATE(...) do { \
544 _Z_SINC_ACCUMULATE(__VA_ARGS__); \
546 #define Z_SINC_ACCUMULATE_DECR 1
549 #define Z_DECLARE_SINC(SIGN, BIT, ENDIAN) \
551 z_feed_sinc_##SIGN##BIT##ENDIAN(struct z_info *info, uint8_t *dst) \
556 int32_t coeff, z, *z_coeff, *z_dcoeff; \
557 uint32_t c, center, ch, i; \
559 z_coeff = info->z_coeff; \
560 z_dcoeff = info->z_dcoeff; \
561 center = z_prev(info, info->z_start, info->z_size); \
562 ch = info->channels * PCM_##BIT##_BPS; \
566 dst -= PCM_##BIT##_BPS; \
567 ch -= PCM_##BIT##_BPS; \
569 z = info->z_alpha * info->z_dx; \
571 p = info->z_delay + (z_next(info, center, 1) * \
572 info->channels * PCM_##BIT##_BPS) + ch; \
573 for (i = info->z_size; i != 0; i -= Z_SINC_ACCUMULATE_DECR) \
574 Z_SINC_ACCUMULATE(SIGN, BIT, ENDIAN, +); \
575 z = info->z_dy - (info->z_alpha * info->z_dx); \
577 p = info->z_delay + (center * info->channels * \
578 PCM_##BIT##_BPS) + ch; \
579 for (i = info->z_size; i != 0; i -= Z_SINC_ACCUMULATE_DECR) \
580 Z_SINC_ACCUMULATE(SIGN, BIT, ENDIAN, -); \
581 if (info->z_scale != Z_ONE) \
582 v = Z_SCALE_##BIT(v, info->z_scale); \
584 v >>= Z_COEFF_SHIFT - Z_GUARD_BIT_##BIT; \
585 Z_CLIP_CHECK(v, BIT); \
586 _PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, Z_CLAMP(v, BIT)); \
590 #define Z_DECLARE_SINC_POLYPHASE(SIGN, BIT, ENDIAN) \
592 z_feed_sinc_polyphase_##SIGN##BIT##ENDIAN(struct z_info *info, uint8_t *dst) \
597 int32_t ch, i, start, *z_pcoeff; \
599 ch = info->channels * PCM_##BIT##_BPS; \
601 start = z_prev(info, info->z_start, (info->z_size << 1) - 1) * ch; \
604 dst -= PCM_##BIT##_BPS; \
605 ch -= PCM_##BIT##_BPS; \
607 p = info->z_delay + start + ch; \
608 z_pcoeff = info->z_pcoeff + \
609 ((info->z_alpha * info->z_size) << 1); \
610 for (i = info->z_size; i != 0; i--) { \
611 x = _PCM_READ_##SIGN##BIT##_##ENDIAN(p); \
612 v += Z_NORM_##BIT((intpcm64_t)x * *z_pcoeff); \
614 p += info->channels * PCM_##BIT##_BPS; \
615 x = _PCM_READ_##SIGN##BIT##_##ENDIAN(p); \
616 v += Z_NORM_##BIT((intpcm64_t)x * *z_pcoeff); \
618 p += info->channels * PCM_##BIT##_BPS; \
620 if (info->z_scale != Z_ONE) \
621 v = Z_SCALE_##BIT(v, info->z_scale); \
623 v >>= Z_COEFF_SHIFT - Z_GUARD_BIT_##BIT; \
624 Z_CLIP_CHECK(v, BIT); \
625 _PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, Z_CLAMP(v, BIT)); \
629 #define Z_DECLARE(SIGN, BIT, ENDIAN) \
630 Z_DECLARE_LINEAR(SIGN, BIT, ENDIAN) \
631 Z_DECLARE_SINC(SIGN, BIT, ENDIAN) \
632 Z_DECLARE_SINC_POLYPHASE(SIGN, BIT, ENDIAN)
634 #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
638 #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
642 #ifdef SND_FEEDER_MULTIFORMAT
659 Z_RESAMPLER_SINC_POLYPHASE,
663 #define Z_RESAMPLER_IDX(i) \
664 (Z_IS_SINC(i) ? Z_RESAMPLER_SINC : (i)->quality)
666 #define Z_RESAMPLER_ENTRY(SIGN, BIT, ENDIAN) \
668 AFMT_##SIGN##BIT##_##ENDIAN, \
670 [Z_RESAMPLER_ZOH] = z_feed_zoh, \
671 [Z_RESAMPLER_LINEAR] = z_feed_linear_##SIGN##BIT##ENDIAN, \
672 [Z_RESAMPLER_SINC] = z_feed_sinc_##SIGN##BIT##ENDIAN, \
673 [Z_RESAMPLER_SINC_POLYPHASE] = \
674 z_feed_sinc_polyphase_##SIGN##BIT##ENDIAN \
678 static const struct {
680 z_resampler_t resampler[Z_RESAMPLER_LAST];
681 } z_resampler_tab[] = {
682 #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
683 Z_RESAMPLER_ENTRY(S, 16, LE),
684 Z_RESAMPLER_ENTRY(S, 32, LE),
686 #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
687 Z_RESAMPLER_ENTRY(S, 16, BE),
688 Z_RESAMPLER_ENTRY(S, 32, BE),
690 #ifdef SND_FEEDER_MULTIFORMAT
691 Z_RESAMPLER_ENTRY(S, 8, NE),
692 Z_RESAMPLER_ENTRY(S, 24, LE),
693 Z_RESAMPLER_ENTRY(S, 24, BE),
694 Z_RESAMPLER_ENTRY(U, 8, NE),
695 Z_RESAMPLER_ENTRY(U, 16, LE),
696 Z_RESAMPLER_ENTRY(U, 24, LE),
697 Z_RESAMPLER_ENTRY(U, 32, LE),
698 Z_RESAMPLER_ENTRY(U, 16, BE),
699 Z_RESAMPLER_ENTRY(U, 24, BE),
700 Z_RESAMPLER_ENTRY(U, 32, BE),
704 #define Z_RESAMPLER_TAB_SIZE \
705 ((int32_t)(sizeof(z_resampler_tab) / sizeof(z_resampler_tab[0])))
708 z_resampler_reset(struct z_info *info)
711 info->src = info->rsrc - (info->rsrc % ((feeder_rate_round > 0 &&
712 info->rsrc > feeder_rate_round) ? feeder_rate_round : 1));
713 info->dst = info->rdst - (info->rdst % ((feeder_rate_round > 0 &&
714 info->rdst > feeder_rate_round) ? feeder_rate_round : 1));
718 info->z_resample = NULL;
720 info->z_coeff = NULL;
721 info->z_dcoeff = NULL;
722 if (info->z_pcoeff != NULL) {
723 free(info->z_pcoeff, M_DEVBUF);
724 info->z_pcoeff = NULL;
726 info->z_scale = Z_ONE;
727 info->z_dx = Z_FULL_ONE;
728 info->z_dy = Z_FULL_ONE;
732 if (info->quality < Z_QUALITY_MIN)
733 info->quality = Z_QUALITY_MIN;
734 else if (info->quality > Z_QUALITY_MAX)
735 info->quality = Z_QUALITY_MAX;
740 z_resampler_sinc_len(struct z_info *info)
742 int32_t c, z, len, lmax;
744 if (!Z_IS_SINC(info))
748 * A rather careful (or useless) way to calculate filter length.
749 * Z_SINC_LEN() itself is accurate enough to do its job. Extra
750 * sanity checking is not going to hurt though..
755 lmax = z_coeff_tab[Z_SINC_COEFF_IDX(info)].len;
761 } while (c < lmax && ++len > 0);
763 if (len != Z_SINC_LEN(info)) {
765 printf("%s(): sinc l=%d != Z_SINC_LEN=%d\n",
766 __func__, len, Z_SINC_LEN(info));
768 fprintf(stderr, "%s(): sinc l=%d != Z_SINC_LEN=%d\n",
769 __func__, len, Z_SINC_LEN(info));
777 #define z_resampler_sinc_len(i) (Z_IS_SINC(i) ? Z_SINC_LEN(i) : 1)
780 #define Z_POLYPHASE_COEFF_SHIFT 0
783 * Pick suitable polynomial interpolators based on filter oversampled ratio
784 * (2 ^ Z_DRIFT_SHIFT).
786 #if !(defined(Z_COEFF_INTERP_ZOH) || defined(Z_COEFF_INTERP_LINEAR) || \
787 defined(Z_COEFF_INTERP_QUADRATIC) || defined(Z_COEFF_INTERP_HERMITE) || \
788 defined(Z_COEFF_INTER_BSPLINE) || defined(Z_COEFF_INTERP_OPT32X) || \
789 defined(Z_COEFF_INTERP_OPT16X) || defined(Z_COEFF_INTERP_OPT8X) || \
790 defined(Z_COEFF_INTERP_OPT4X) || defined(Z_COEFF_INTERP_OPT2X))
791 #if Z_DRIFT_SHIFT >= 6
792 #define Z_COEFF_INTERP_BSPLINE 1
793 #elif Z_DRIFT_SHIFT >= 5
794 #define Z_COEFF_INTERP_OPT32X 1
795 #elif Z_DRIFT_SHIFT == 4
796 #define Z_COEFF_INTERP_OPT16X 1
797 #elif Z_DRIFT_SHIFT == 3
798 #define Z_COEFF_INTERP_OPT8X 1
799 #elif Z_DRIFT_SHIFT == 2
800 #define Z_COEFF_INTERP_OPT4X 1
801 #elif Z_DRIFT_SHIFT == 1
802 #define Z_COEFF_INTERP_OPT2X 1
804 #error "Z_DRIFT_SHIFT screwed!"
809 * In classic polyphase mode, the actual coefficients for each phases need to
810 * be calculated based on default prototype filters. For highly oversampled
811 * filter, linear or quadradatic interpolator should be enough. Anything less
812 * than that require 'special' interpolators to reduce interpolation errors.
814 * "Polynomial Interpolators for High-Quality Resampling of Oversampled Audio"
816 * - http://www.student.oulu.fi/~oniemita/dsp/deip.pdf
820 z_coeff_interpolate(int32_t z, int32_t *z_coeff)
823 #if defined(Z_COEFF_INTERP_ZOH)
825 /* 1-point, 0th-order (Zero Order Hold) */
828 #elif defined(Z_COEFF_INTERP_LINEAR)
831 /* 2-point, 1st-order Linear */
833 zl1 = z_coeff[1] - z_coeff[0];
835 coeff = Z_RSHIFT((int64_t)zl1 * z, Z_SHIFT) + zl0;
836 #elif defined(Z_COEFF_INTERP_QUADRATIC)
837 int32_t zq0, zq1, zq2;
839 /* 3-point, 2nd-order Quadratic */
841 zq1 = z_coeff[1] - z_coeff[-1];
842 zq2 = z_coeff[1] + z_coeff[-1] - (z_coeff[0] << 1);
844 coeff = Z_RSHIFT((Z_RSHIFT((int64_t)zq2 * z, Z_SHIFT) +
845 zq1) * z, Z_SHIFT + 1) + zq0;
846 #elif defined(Z_COEFF_INTERP_HERMITE)
847 int32_t zh0, zh1, zh2, zh3;
849 /* 4-point, 3rd-order Hermite */
851 zh1 = z_coeff[1] - z_coeff[-1];
852 zh2 = (z_coeff[-1] << 1) - (z_coeff[0] * 5) + (z_coeff[1] << 2) -
854 zh3 = z_coeff[2] - z_coeff[-1] + ((z_coeff[0] - z_coeff[1]) * 3);
856 coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((int64_t)zh3 * z, Z_SHIFT) +
857 zh2) * z, Z_SHIFT) + zh1) * z, Z_SHIFT + 1) + zh0;
858 #elif defined(Z_COEFF_INTERP_BSPLINE)
859 int32_t zb0, zb1, zb2, zb3;
861 /* 4-point, 3rd-order B-Spline */
862 zb0 = Z_RSHIFT(0x15555555LL * (((int64_t)z_coeff[0] << 2) +
863 z_coeff[-1] + z_coeff[1]), 30);
864 zb1 = z_coeff[1] - z_coeff[-1];
865 zb2 = z_coeff[-1] + z_coeff[1] - (z_coeff[0] << 1);
866 zb3 = Z_RSHIFT(0x15555555LL * (((z_coeff[0] - z_coeff[1]) * 3) +
867 z_coeff[2] - z_coeff[-1]), 30);
869 coeff = (Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((int64_t)zb3 * z, Z_SHIFT) +
870 zb2) * z, Z_SHIFT) + zb1) * z, Z_SHIFT) + zb0 + 1) >> 1;
871 #elif defined(Z_COEFF_INTERP_OPT32X)
872 int32_t zoz, zoe1, zoe2, zoe3, zoo1, zoo2, zoo3;
873 int32_t zoc0, zoc1, zoc2, zoc3, zoc4, zoc5;
875 /* 6-point, 5th-order Optimal 32x */
876 zoz = z - (Z_ONE >> 1);
877 zoe1 = z_coeff[1] + z_coeff[0];
878 zoe2 = z_coeff[2] + z_coeff[-1];
879 zoe3 = z_coeff[3] + z_coeff[-2];
880 zoo1 = z_coeff[1] - z_coeff[0];
881 zoo2 = z_coeff[2] - z_coeff[-1];
882 zoo3 = z_coeff[3] - z_coeff[-2];
884 zoc0 = Z_RSHIFT((0x1ac2260dLL * zoe1) + (0x0526cdcaLL * zoe2) +
885 (0x00170c29LL * zoe3), 30);
886 zoc1 = Z_RSHIFT((0x14f8a49aLL * zoo1) + (0x0d6d1109LL * zoo2) +
887 (0x008cd4dcLL * zoo3), 30);
888 zoc2 = Z_RSHIFT((-0x0d3e94a4LL * zoe1) + (0x0bddded4LL * zoe2) +
889 (0x0160b5d0LL * zoe3), 30);
890 zoc3 = Z_RSHIFT((-0x0de10cc4LL * zoo1) + (0x019b2a7dLL * zoo2) +
891 (0x01cfe914LL * zoo3), 30);
892 zoc4 = Z_RSHIFT((0x02aa12d7LL * zoe1) + (-0x03ff1bb3LL * zoe2) +
893 (0x015508ddLL * zoe3), 30);
894 zoc5 = Z_RSHIFT((0x051d29e5LL * zoo1) + (-0x028e7647LL * zoo2) +
895 (0x0082d81aLL * zoo3), 30);
897 coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT(
898 (int64_t)zoc5 * zoz, Z_SHIFT) +
899 zoc4) * zoz, Z_SHIFT) + zoc3) * zoz, Z_SHIFT) +
900 zoc2) * zoz, Z_SHIFT) + zoc1) * zoz, Z_SHIFT) + zoc0;
901 #elif defined(Z_COEFF_INTERP_OPT16X)
902 int32_t zoz, zoe1, zoe2, zoe3, zoo1, zoo2, zoo3;
903 int32_t zoc0, zoc1, zoc2, zoc3, zoc4, zoc5;
905 /* 6-point, 5th-order Optimal 16x */
906 zoz = z - (Z_ONE >> 1);
907 zoe1 = z_coeff[1] + z_coeff[0];
908 zoe2 = z_coeff[2] + z_coeff[-1];
909 zoe3 = z_coeff[3] + z_coeff[-2];
910 zoo1 = z_coeff[1] - z_coeff[0];
911 zoo2 = z_coeff[2] - z_coeff[-1];
912 zoo3 = z_coeff[3] - z_coeff[-2];
914 zoc0 = Z_RSHIFT((0x1ac2260dLL * zoe1) + (0x0526cdcaLL * zoe2) +
915 (0x00170c29LL * zoe3), 30);
916 zoc1 = Z_RSHIFT((0x14f8a49aLL * zoo1) + (0x0d6d1109LL * zoo2) +
917 (0x008cd4dcLL * zoo3), 30);
918 zoc2 = Z_RSHIFT((-0x0d3e94a4LL * zoe1) + (0x0bddded4LL * zoe2) +
919 (0x0160b5d0LL * zoe3), 30);
920 zoc3 = Z_RSHIFT((-0x0de10cc4LL * zoo1) + (0x019b2a7dLL * zoo2) +
921 (0x01cfe914LL * zoo3), 30);
922 zoc4 = Z_RSHIFT((0x02aa12d7LL * zoe1) + (-0x03ff1bb3LL * zoe2) +
923 (0x015508ddLL * zoe3), 30);
924 zoc5 = Z_RSHIFT((0x051d29e5LL * zoo1) + (-0x028e7647LL * zoo2) +
925 (0x0082d81aLL * zoo3), 30);
927 coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT(
928 (int64_t)zoc5 * zoz, Z_SHIFT) +
929 zoc4) * zoz, Z_SHIFT) + zoc3) * zoz, Z_SHIFT) +
930 zoc2) * zoz, Z_SHIFT) + zoc1) * zoz, Z_SHIFT) + zoc0;
931 #elif defined(Z_COEFF_INTERP_OPT8X)
932 int32_t zoz, zoe1, zoe2, zoe3, zoo1, zoo2, zoo3;
933 int32_t zoc0, zoc1, zoc2, zoc3, zoc4, zoc5;
935 /* 6-point, 5th-order Optimal 8x */
936 zoz = z - (Z_ONE >> 1);
937 zoe1 = z_coeff[1] + z_coeff[0];
938 zoe2 = z_coeff[2] + z_coeff[-1];
939 zoe3 = z_coeff[3] + z_coeff[-2];
940 zoo1 = z_coeff[1] - z_coeff[0];
941 zoo2 = z_coeff[2] - z_coeff[-1];
942 zoo3 = z_coeff[3] - z_coeff[-2];
944 zoc0 = Z_RSHIFT((0x1aa9b47dLL * zoe1) + (0x053d9944LL * zoe2) +
945 (0x0018b23fLL * zoe3), 30);
946 zoc1 = Z_RSHIFT((0x14a104d1LL * zoo1) + (0x0d7d2504LL * zoo2) +
947 (0x0094b599LL * zoo3), 30);
948 zoc2 = Z_RSHIFT((-0x0d22530bLL * zoe1) + (0x0bb37a2cLL * zoe2) +
949 (0x016ed8e0LL * zoe3), 30);
950 zoc3 = Z_RSHIFT((-0x0d744b1cLL * zoo1) + (0x01649591LL * zoo2) +
951 (0x01dae93aLL * zoo3), 30);
952 zoc4 = Z_RSHIFT((0x02a7ee1bLL * zoe1) + (-0x03fbdb24LL * zoe2) +
953 (0x0153ed07LL * zoe3), 30);
954 zoc5 = Z_RSHIFT((0x04cf9b6cLL * zoo1) + (-0x0266b378LL * zoo2) +
955 (0x007a7c26LL * zoo3), 30);
957 coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT(
958 (int64_t)zoc5 * zoz, Z_SHIFT) +
959 zoc4) * zoz, Z_SHIFT) + zoc3) * zoz, Z_SHIFT) +
960 zoc2) * zoz, Z_SHIFT) + zoc1) * zoz, Z_SHIFT) + zoc0;
961 #elif defined(Z_COEFF_INTERP_OPT4X)
962 int32_t zoz, zoe1, zoe2, zoe3, zoo1, zoo2, zoo3;
963 int32_t zoc0, zoc1, zoc2, zoc3, zoc4, zoc5;
965 /* 6-point, 5th-order Optimal 4x */
966 zoz = z - (Z_ONE >> 1);
967 zoe1 = z_coeff[1] + z_coeff[0];
968 zoe2 = z_coeff[2] + z_coeff[-1];
969 zoe3 = z_coeff[3] + z_coeff[-2];
970 zoo1 = z_coeff[1] - z_coeff[0];
971 zoo2 = z_coeff[2] - z_coeff[-1];
972 zoo3 = z_coeff[3] - z_coeff[-2];
974 zoc0 = Z_RSHIFT((0x1a8eda43LL * zoe1) + (0x0556ee38LL * zoe2) +
975 (0x001a3784LL * zoe3), 30);
976 zoc1 = Z_RSHIFT((0x143d863eLL * zoo1) + (0x0d910e36LL * zoo2) +
977 (0x009ca889LL * zoo3), 30);
978 zoc2 = Z_RSHIFT((-0x0d026821LL * zoe1) + (0x0b837773LL * zoe2) +
979 (0x017ef0c6LL * zoe3), 30);
980 zoc3 = Z_RSHIFT((-0x0cef1502LL * zoo1) + (0x01207a8eLL * zoo2) +
981 (0x01e936dbLL * zoo3), 30);
982 zoc4 = Z_RSHIFT((0x029fe643LL * zoe1) + (-0x03ef3fc8LL * zoe2) +
983 (0x014f5923LL * zoe3), 30);
984 zoc5 = Z_RSHIFT((0x043a9d08LL * zoo1) + (-0x02154febLL * zoo2) +
985 (0x00670dbdLL * zoo3), 30);
987 coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT(
988 (int64_t)zoc5 * zoz, Z_SHIFT) +
989 zoc4) * zoz, Z_SHIFT) + zoc3) * zoz, Z_SHIFT) +
990 zoc2) * zoz, Z_SHIFT) + zoc1) * zoz, Z_SHIFT) + zoc0;
991 #elif defined(Z_COEFF_INTERP_OPT2X)
992 int32_t zoz, zoe1, zoe2, zoe3, zoo1, zoo2, zoo3;
993 int32_t zoc0, zoc1, zoc2, zoc3, zoc4, zoc5;
995 /* 6-point, 5th-order Optimal 2x */
996 zoz = z - (Z_ONE >> 1);
997 zoe1 = z_coeff[1] + z_coeff[0];
998 zoe2 = z_coeff[2] + z_coeff[-1];
999 zoe3 = z_coeff[3] + z_coeff[-2];
1000 zoo1 = z_coeff[1] - z_coeff[0];
1001 zoo2 = z_coeff[2] - z_coeff[-1];
1002 zoo3 = z_coeff[3] - z_coeff[-2];
1004 zoc0 = Z_RSHIFT((0x19edb6fdLL * zoe1) + (0x05ebd062LL * zoe2) +
1005 (0x00267881LL * zoe3), 30);
1006 zoc1 = Z_RSHIFT((0x1223af76LL * zoo1) + (0x0de3dd6bLL * zoo2) +
1007 (0x00d683cdLL * zoo3), 30);
1008 zoc2 = Z_RSHIFT((-0x0c3ee068LL * zoe1) + (0x0a5c3769LL * zoe2) +
1009 (0x01e2aceaLL * zoe3), 30);
1010 zoc3 = Z_RSHIFT((-0x0a8ab614LL * zoo1) + (-0x0019522eLL * zoo2) +
1011 (0x022cefc7LL * zoo3), 30);
1012 zoc4 = Z_RSHIFT((0x0276187dLL * zoe1) + (-0x03a801e8LL * zoe2) +
1013 (0x0131d935LL * zoe3), 30);
1014 zoc5 = Z_RSHIFT((0x02c373f5LL * zoo1) + (-0x01275f83LL * zoo2) +
1015 (0x0018ee79LL * zoo3), 30);
1017 coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT(
1018 (int64_t)zoc5 * zoz, Z_SHIFT) +
1019 zoc4) * zoz, Z_SHIFT) + zoc3) * zoz, Z_SHIFT) +
1020 zoc2) * zoz, Z_SHIFT) + zoc1) * zoz, Z_SHIFT) + zoc0;
1022 #error "Interpolation type screwed!"
1025 #if Z_POLYPHASE_COEFF_SHIFT > 0
1026 coeff = Z_RSHIFT(coeff, Z_POLYPHASE_COEFF_SHIFT);
1032 z_resampler_build_polyphase(struct z_info *info)
1034 int32_t alpha, c, i, z, idx;
1036 /* Let this be here first. */
1037 if (info->z_pcoeff != NULL) {
1038 free(info->z_pcoeff, M_DEVBUF);
1039 info->z_pcoeff = NULL;
1042 if (feeder_rate_polyphase_max < 1)
1045 if (((int64_t)info->z_size * info->z_gy * 2) >
1046 feeder_rate_polyphase_max) {
1048 fprintf(stderr, "Polyphase entries exceed: [%d/%d] %jd > %d\n",
1049 info->z_gx, info->z_gy,
1050 (intmax_t)info->z_size * info->z_gy * 2,
1051 feeder_rate_polyphase_max);
1056 info->z_pcoeff = malloc(sizeof(int32_t) *
1057 info->z_size * info->z_gy * 2, M_DEVBUF, M_NOWAIT | M_ZERO);
1058 if (info->z_pcoeff == NULL)
1061 for (alpha = 0; alpha < info->z_gy; alpha++) {
1062 z = alpha * info->z_dx;
1064 for (i = info->z_size; i != 0; i--) {
1067 idx = (alpha * info->z_size * 2) +
1068 (info->z_size * 2) - i;
1069 info->z_pcoeff[idx] =
1070 z_coeff_interpolate(z, info->z_coeff + c);
1073 z = info->z_dy - (alpha * info->z_dx);
1075 for (i = info->z_size; i != 0; i--) {
1078 idx = (alpha * info->z_size * 2) + i - 1;
1079 info->z_pcoeff[idx] =
1080 z_coeff_interpolate(z, info->z_coeff + c);
1086 fprintf(stderr, "Polyphase: [%d/%d] %d entries\n",
1087 info->z_gx, info->z_gy, info->z_size * info->z_gy * 2);
1094 z_resampler_setup(struct pcm_feeder *f)
1096 struct z_info *info;
1097 int64_t gy2gx_max, gx2gy_max;
1099 int32_t align, i, z_scale;
1103 z_resampler_reset(info);
1105 if (info->src == info->dst)
1108 /* Shrink by greatest common divisor. */
1109 i = z_gcd(info->src, info->dst);
1110 info->z_gx = info->src / i;
1111 info->z_gy = info->dst / i;
1113 /* Too big, or too small. Bail out. */
1114 if (!(Z_FACTOR_SAFE(info->z_gx) && Z_FACTOR_SAFE(info->z_gy)))
1117 format = f->desc->in;
1122 * Setup everything: filter length, conversion factor, etc.
1124 if (Z_IS_SINC(info)) {
1126 * Downsampling, or upsampling scaling factor. As long as the
1127 * factor can be represented by a fraction of 1 << Z_SHIFT,
1128 * we're pretty much in business. Scaling is not needed for
1129 * upsampling, so we just slap Z_ONE there.
1131 if (info->z_gx > info->z_gy)
1133 * If the downsampling ratio is beyond sanity,
1134 * enable semi-adaptive mode. Although handling
1135 * extreme ratio is possible, the result of the
1136 * conversion is just pointless, unworthy,
1137 * nonsensical noises, etc.
1139 if ((info->z_gx / info->z_gy) > Z_SINC_DOWNMAX)
1140 z_scale = Z_ONE / Z_SINC_DOWNMAX;
1142 z_scale = ((uint64_t)info->z_gy << Z_SHIFT) /
1148 * This is actually impossible, unless anything above
1155 * Calculate sample time/coefficients index drift. It is
1156 * a constant for upsampling, but downsampling require
1157 * heavy duty filtering with possible too long filters.
1158 * If anything goes wrong, revisit again and enable
1161 z_setup_adaptive_sinc:
1162 if (info->z_pcoeff != NULL) {
1163 free(info->z_pcoeff, M_DEVBUF);
1164 info->z_pcoeff = NULL;
1167 if (adaptive == 0) {
1168 info->z_dy = z_scale << Z_DRIFT_SHIFT;
1171 info->z_scale = z_scale;
1173 info->z_dy = Z_FULL_ONE;
1174 info->z_scale = Z_ONE;
1178 #define Z_SCALE_DIV 10000
1179 #define Z_SCALE_LIMIT(s, v) \
1180 ((((uint64_t)(s) * (v)) + (Z_SCALE_DIV >> 1)) / Z_SCALE_DIV)
1182 info->z_scale = Z_SCALE_LIMIT(info->z_scale, 9780);
1185 /* Smallest drift increment. */
1186 info->z_dx = info->z_dy / info->z_gy;
1189 * Overflow or underflow. Try adaptive, let it continue and
1192 if (info->z_dx < 1) {
1193 if (adaptive == 0) {
1195 goto z_setup_adaptive_sinc;
1201 * Round back output drift.
1203 info->z_dy = info->z_dx * info->z_gy;
1205 for (i = 0; i < Z_COEFF_TAB_SIZE; i++) {
1206 if (Z_SINC_COEFF_IDX(info) != i)
1209 * Calculate required filter length and guard
1210 * against possible abusive result. Note that
1211 * this represents only 1/2 of the entire filter
1214 info->z_size = z_resampler_sinc_len(info);
1217 * Multiple of 2 rounding, for better accumulator
1222 if (info->z_size < 2 || info->z_size > Z_SINC_MAX) {
1223 if (adaptive == 0) {
1225 goto z_setup_adaptive_sinc;
1229 info->z_coeff = z_coeff_tab[i].coeff + Z_COEFF_OFFSET;
1230 info->z_dcoeff = z_coeff_tab[i].dcoeff;
1234 if (info->z_coeff == NULL || info->z_dcoeff == NULL)
1236 } else if (Z_IS_LINEAR(info)) {
1238 * Don't put much effort if we're doing linear interpolation.
1239 * Just center the interpolation distance within Z_LINEAR_ONE,
1240 * and be happy about it.
1242 info->z_dx = Z_LINEAR_FULL_ONE / info->z_gy;
1246 * We're safe for now, lets continue.. Look for our resampler
1247 * depending on configured format and quality.
1249 for (i = 0; i < Z_RESAMPLER_TAB_SIZE; i++) {
1252 if (AFMT_ENCODING(format) != z_resampler_tab[i].format)
1254 if (Z_IS_SINC(info) && adaptive == 0 &&
1255 z_resampler_build_polyphase(info) == 0)
1256 ridx = Z_RESAMPLER_SINC_POLYPHASE;
1258 ridx = Z_RESAMPLER_IDX(info);
1259 info->z_resample = z_resampler_tab[i].resampler[ridx];
1263 if (info->z_resample == NULL)
1266 info->bps = AFMT_BPS(format);
1267 align = info->channels * info->bps;
1270 * Calculate largest value that can be fed into z_gy2gx() and
1271 * z_gx2gy() without causing (signed) 32bit overflow. z_gy2gx() will
1272 * be called early during feeding process to determine how much input
1273 * samples that is required to generate requested output, while
1274 * z_gx2gy() will be called just before samples filtering /
1275 * accumulation process based on available samples that has been
1276 * calculated using z_gx2gy().
1278 * Now that is damn confusing, I guess ;-) .
1280 gy2gx_max = (((uint64_t)info->z_gy * INT32_MAX) - info->z_gy + 1) /
1283 if ((gy2gx_max * align) > SND_FXDIV_MAX)
1284 gy2gx_max = SND_FXDIV_MAX / align;
1289 gx2gy_max = (((uint64_t)info->z_gx * INT32_MAX) - info->z_gy) /
1292 if (gx2gy_max > INT32_MAX)
1293 gx2gy_max = INT32_MAX;
1299 * Ensure that z_gy2gx() at its largest possible calculated value
1300 * (alpha = 0) will not cause overflow further late during z_gx2gy()
1303 if (z_gy2gx(info, gy2gx_max) > _Z_GCAST(gx2gy_max))
1306 info->z_maxfeed = gy2gx_max * align;
1308 #ifdef Z_USE_ALPHADRIFT
1309 info->z_startdrift = z_gy2gx(info, 1);
1310 info->z_alphadrift = z_drift(info, info->z_startdrift, 1);
1313 i = z_gy2gx(info, 1);
1314 info->z_full = z_roundpow2((info->z_size << 1) + i);
1317 * Too big to be true, and overflowing left and right like mad ..
1319 if ((info->z_full * align) < 1) {
1320 if (adaptive == 0 && Z_IS_SINC(info)) {
1322 goto z_setup_adaptive_sinc;
1328 * Increase full buffer size if its too small to reduce cyclic
1329 * buffer shifting in main conversion/feeder loop.
1331 while (info->z_full < Z_RESERVOIR_MAX &&
1332 (info->z_full - (info->z_size << 1)) < Z_RESERVOIR)
1335 /* Initialize buffer position. */
1336 info->z_mask = info->z_full - 1;
1337 info->z_start = z_prev(info, info->z_size << 1, 1);
1338 info->z_pos = z_next(info, info->z_start, 1);
1341 * Allocate or reuse delay line buffer, whichever makes sense.
1343 i = info->z_full * align;
1347 if (info->z_delay == NULL || info->z_alloc < i ||
1348 i <= (info->z_alloc >> 1)) {
1349 if (info->z_delay != NULL)
1350 free(info->z_delay, M_DEVBUF);
1351 info->z_delay = malloc(i, M_DEVBUF, M_NOWAIT | M_ZERO);
1352 if (info->z_delay == NULL)
1358 * Zero out head of buffer to avoid pops and clicks.
1360 memset(info->z_delay, sndbuf_zerodata(f->desc->out),
1361 info->z_pos * align);
1365 * XXX Debuging mess !@#$%^
1367 #define dumpz(x) fprintf(stderr, "\t%12s = %10u : %-11d\n", \
1368 "z_"__STRING(x), (uint32_t)info->z_##x, \
1369 (int32_t)info->z_##x)
1370 fprintf(stderr, "\n%s():\n", __func__);
1371 fprintf(stderr, "\tchannels=%d, bps=%d, format=0x%08x, quality=%d\n",
1372 info->channels, info->bps, format, info->quality);
1373 fprintf(stderr, "\t%d (%d) -> %d (%d), ",
1374 info->src, info->rsrc, info->dst, info->rdst);
1375 fprintf(stderr, "[%d/%d]\n", info->z_gx, info->z_gy);
1376 fprintf(stderr, "\tminreq=%d, ", z_gy2gx(info, 1));
1379 fprintf(stderr, "factor=0x%08x/0x%08x (%f)\n",
1380 z_scale, Z_ONE, (double)z_scale / Z_ONE);
1381 fprintf(stderr, "\tbase_length=%d, ", Z_SINC_BASE_LEN(info));
1382 fprintf(stderr, "adaptive=%s\n", (adaptive != 0) ? "YES" : "NO");
1385 if (info->z_alloc < 1024)
1386 fprintf(stderr, "\t%15s%10d Bytes\n",
1388 else if (info->z_alloc < (1024 << 10))
1389 fprintf(stderr, "\t%15s%10d KBytes\n",
1390 "", info->z_alloc >> 10);
1391 else if (info->z_alloc < (1024 << 20))
1392 fprintf(stderr, "\t%15s%10d MBytes\n",
1393 "", info->z_alloc >> 20);
1395 fprintf(stderr, "\t%15s%10d GBytes\n",
1396 "", info->z_alloc >> 30);
1397 fprintf(stderr, "\t%12s %10d (min output samples)\n",
1399 (int32_t)z_gx2gy(info, info->z_full - (info->z_size << 1)));
1400 fprintf(stderr, "\t%12s %10d (min allocated output samples)\n",
1402 (int32_t)z_gx2gy(info, (info->z_alloc / align) -
1403 (info->z_size << 1)));
1404 fprintf(stderr, "\t%12s = %10d\n",
1405 "z_gy2gx()", (int32_t)z_gy2gx(info, 1));
1406 fprintf(stderr, "\t%12s = %10d -> z_gy2gx() -> %d\n",
1407 "Max", (int32_t)gy2gx_max, (int32_t)z_gy2gx(info, gy2gx_max));
1408 fprintf(stderr, "\t%12s = %10d\n",
1409 "z_gx2gy()", (int32_t)z_gx2gy(info, 1));
1410 fprintf(stderr, "\t%12s = %10d -> z_gx2gy() -> %d\n",
1411 "Max", (int32_t)gx2gy_max, (int32_t)z_gx2gy(info, gx2gy_max));
1417 fprintf(stderr, "\t%12s %10f\n", "",
1418 (double)info->z_scale / Z_ONE);
1420 fprintf(stderr, "\t%12s %10f\n", "",
1421 (double)info->z_dx / info->z_dy);
1423 fprintf(stderr, "\t%12s %10d (drift step)\n", "",
1424 info->z_dy >> Z_SHIFT);
1425 fprintf(stderr, "\t%12s %10d (scaling differences)\n", "",
1426 (z_scale << Z_DRIFT_SHIFT) - info->z_dy);
1427 fprintf(stderr, "\t%12s = %u bytes\n",
1428 "intpcm32_t", sizeof(intpcm32_t));
1429 fprintf(stderr, "\t%12s = 0x%08x, smallest=%.16lf\n",
1430 "Z_ONE", Z_ONE, (double)1.0 / (double)Z_ONE);
1437 z_resampler_set(struct pcm_feeder *f, int what, int32_t value)
1439 struct z_info *info;
1446 if (value < feeder_rate_min || value > feeder_rate_max)
1448 if (value == info->rsrc)
1453 if (value < feeder_rate_min || value > feeder_rate_max)
1455 if (value == info->rdst)
1459 case Z_RATE_QUALITY:
1460 if (value < Z_QUALITY_MIN || value > Z_QUALITY_MAX)
1462 if (value == info->quality)
1465 * If we failed to set the requested quality, restore
1466 * the old one. We cannot afford leaving it broken since
1467 * passive feeder chains like vchans never reinitialize
1470 oquality = info->quality;
1471 info->quality = value;
1472 if (z_resampler_setup(f) == 0)
1474 info->quality = oquality;
1476 case Z_RATE_CHANNELS:
1477 if (value < SND_CHN_MIN || value > SND_CHN_MAX)
1479 if (value == info->channels)
1481 info->channels = value;
1488 return (z_resampler_setup(f));
1492 z_resampler_get(struct pcm_feeder *f, int what)
1494 struct z_info *info;
1500 return (info->rsrc);
1503 return (info->rdst);
1505 case Z_RATE_QUALITY:
1506 return (info->quality);
1508 case Z_RATE_CHANNELS:
1509 return (info->channels);
1519 z_resampler_init(struct pcm_feeder *f)
1521 struct z_info *info;
1524 if (f->desc->in != f->desc->out)
1527 info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT | M_ZERO);
1531 info->rsrc = Z_RATE_DEFAULT;
1532 info->rdst = Z_RATE_DEFAULT;
1533 info->quality = feeder_rate_quality;
1534 info->channels = AFMT_CHANNEL(f->desc->in);
1538 ret = z_resampler_setup(f);
1540 if (info->z_pcoeff != NULL)
1541 free(info->z_pcoeff, M_DEVBUF);
1542 if (info->z_delay != NULL)
1543 free(info->z_delay, M_DEVBUF);
1544 free(info, M_DEVBUF);
1552 z_resampler_free(struct pcm_feeder *f)
1554 struct z_info *info;
1558 if (info->z_pcoeff != NULL)
1559 free(info->z_pcoeff, M_DEVBUF);
1560 if (info->z_delay != NULL)
1561 free(info->z_delay, M_DEVBUF);
1562 free(info, M_DEVBUF);
1571 z_resampler_feed_internal(struct pcm_feeder *f, struct pcm_channel *c,
1572 uint8_t *b, uint32_t count, void *source)
1574 struct z_info *info;
1575 int32_t alphadrift, startdrift, reqout, ocount, reqin, align;
1576 int32_t fetch, fetched, start, cp;
1580 if (info->z_resample == NULL)
1581 return (z_feed(f->source, c, b, count, source));
1584 * Calculate sample size alignment and amount of sample output.
1585 * We will do everything in sample domain, but at the end we
1586 * will jump back to byte domain.
1588 align = info->channels * info->bps;
1589 ocount = SND_FXDIV(count, align);
1594 * Calculate amount of input samples that is needed to generate
1595 * exact amount of output.
1597 reqin = z_gy2gx(info, ocount) - z_fetched(info);
1599 #ifdef Z_USE_ALPHADRIFT
1600 startdrift = info->z_startdrift;
1601 alphadrift = info->z_alphadrift;
1603 startdrift = _Z_GY2GX(info, 0, 1);
1604 alphadrift = z_drift(info, startdrift, 1);
1611 fetch = z_min(z_free(info), reqin);
1614 * No more free spaces, so wind enough
1615 * samples back to the head of delay line
1618 fetched = z_fetched(info);
1619 start = z_prev(info, info->z_start,
1620 (info->z_size << 1) - 1);
1621 cp = (info->z_size << 1) + fetched;
1622 z_copy(info->z_delay + (start * align),
1623 info->z_delay, cp * align);
1625 z_prev(info, info->z_size << 1, 1);
1627 z_next(info, info->z_start, fetched + 1);
1628 fetch = z_min(z_free(info), reqin);
1631 static uint32_t kk = 0;
1634 "start=%d fetched=%d cp=%d "
1636 start, fetched, cp, info->z_cycle,
1644 * Fetch in byte domain and jump back
1647 fetched = SND_FXDIV(z_feed(f->source, c,
1648 info->z_delay + (info->z_pos * align),
1649 fetch * align, source), align);
1651 * Prepare to convert fetched buffer,
1652 * or mark us done if we cannot fulfill
1656 info->z_pos += fetched;
1657 if (fetched != fetch)
1662 reqout = z_min(z_gx2gy(info, z_fetched(info)), ocount);
1667 * Drift.. drift.. drift..
1669 * Notice that there are 2 methods of doing the drift
1670 * operations: The former is much cleaner (in a sense
1671 * of mathematical readings of my eyes), but slower
1672 * due to integer division in z_gy2gx(). Nevertheless,
1673 * both should give the same exact accurate drifting
1674 * results, so the later is favourable.
1677 info->z_resample(info, dst);
1679 startdrift = z_gy2gx(info, 1);
1680 alphadrift = z_drift(info, startdrift, 1);
1681 info->z_start += startdrift;
1682 info->z_alpha += alphadrift;
1684 info->z_alpha += alphadrift;
1685 if (info->z_alpha < info->z_gy)
1686 info->z_start += startdrift;
1688 info->z_start += startdrift - 1;
1689 info->z_alpha -= info->z_gy;
1696 } while (--reqout != 0);
1698 } while (reqin != 0 && ocount != 0);
1701 * Back to byte domain..
1707 z_resampler_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
1708 uint32_t count, void *source)
1710 uint32_t feed, maxfeed, left;
1713 * Split count to smaller chunks to avoid possible 32bit overflow.
1715 maxfeed = ((struct z_info *)(f->data))->z_maxfeed;
1719 feed = z_resampler_feed_internal(f, c, b,
1720 z_min(maxfeed, left), source);
1723 } while (left != 0 && feed != 0);
1725 return (count - left);
1728 static struct pcm_feederdesc feeder_rate_desc[] = {
1729 { FEEDER_RATE, 0, 0, 0, 0 },
1733 static kobj_method_t feeder_rate_methods[] = {
1734 KOBJMETHOD(feeder_init, z_resampler_init),
1735 KOBJMETHOD(feeder_free, z_resampler_free),
1736 KOBJMETHOD(feeder_set, z_resampler_set),
1737 KOBJMETHOD(feeder_get, z_resampler_get),
1738 KOBJMETHOD(feeder_feed, z_resampler_feed),
1742 FEEDER_DECLARE(feeder_rate, NULL);