xref: /freebsd/sys/dev/sound/pcm/feeder_rate.c (revision 640235e2c2ba32947f7c59d168437ffa1280f1e6)
1 /*-
2  * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 /*
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.
31  *
32  * FreeBSD bandlimited sinc interpolator, technically based on
33  * "Digital Audio Resampling" by Julius O. Smith III
34  *  - http://ccrma.stanford.edu/~jos/resample/
35  *
36  * The Good:
37  * + all out fixed point integer operations, no soft-float or anything like
38  *   that.
39  * + classic polyphase converters with high quality coefficient's polynomial
40  *   interpolators.
41  * + fast, faster, or the fastest of its kind.
42  * + compile time configurable.
43  * + etc etc..
44  *
45  * The Bad:
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?).
49  */
50 
51 #ifdef _KERNEL
52 #ifdef HAVE_KERNEL_OPTION_HEADERS
53 #include "opt_snd.h"
54 #endif
55 #include <dev/sound/pcm/sound.h>
56 #include <dev/sound/pcm/pcm.h>
57 #include "feeder_if.h"
58 
59 #define SND_USE_FXDIV
60 #include "snd_fxdiv_gen.h"
61 
62 SND_DECLARE_FILE("$FreeBSD$");
63 #endif
64 
65 #include "feeder_rate_gen.h"
66 
67 #if !defined(_KERNEL) && defined(SND_DIAGNOSTIC)
68 #undef Z_DIAGNOSTIC
69 #define Z_DIAGNOSTIC		1
70 #elif defined(_KERNEL)
71 #undef Z_DIAGNOSTIC
72 #endif
73 
74 #ifndef Z_QUALITY_DEFAULT
75 #define Z_QUALITY_DEFAULT	Z_QUALITY_LINEAR
76 #endif
77 
78 #define Z_RESERVOIR		2048
79 #define Z_RESERVOIR_MAX		131072
80 
81 #define Z_SINC_MAX		0x3fffff
82 #define Z_SINC_DOWNMAX		48		/* 384000 / 8000 */
83 
84 #ifdef _KERNEL
85 #define Z_POLYPHASE_MAX		183040		/* 286 taps, 640 phases */
86 #else
87 #define Z_POLYPHASE_MAX		1464320		/* 286 taps, 5120 phases */
88 #endif
89 
90 #define Z_RATE_DEFAULT		48000
91 
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
97 
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
102 
103 #define Z_PARANOID		1
104 
105 #define Z_MULTIFORMAT		1
106 
107 #ifdef _KERNEL
108 #undef Z_USE_ALPHADRIFT
109 #define Z_USE_ALPHADRIFT	1
110 #endif
111 
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))
115 
116 struct z_info;
117 
118 typedef void (*z_resampler_t)(struct z_info *, uint8_t *);
119 
120 struct z_info {
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 */
126 
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 */
139 #endif
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 */
146 #ifdef Z_DIAGNOSTIC
147 	uint32_t z_cycle;	/* output cycle, purely for statistical */
148 #endif
149 	int32_t z_maxfeed;	/* maximum feed to avoid 32bit overflow */
150 
151 	z_resampler_t z_resample;
152 };
153 
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;
158 
159 static int feeder_rate_polyphase_max = Z_POLYPHASE_MAX;
160 
161 #ifdef _KERNEL
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");
165 SYSCTL_INT(_hw_snd, OID_AUTO, feeder_rate_polyphase_max, CTLFLAG_RWTUN,
166     &feeder_rate_polyphase_max, 0, "maximum allowable polyphase entries");
167 
168 static int
169 sysctl_hw_snd_feeder_rate_min(SYSCTL_HANDLER_ARGS)
170 {
171 	int err, val;
172 
173 	val = feeder_rate_min;
174 	err = sysctl_handle_int(oidp, &val, 0, req);
175 
176 	if (err != 0 || req->newptr == NULL || val == feeder_rate_min)
177 		return (err);
178 
179 	if (!(Z_FACTOR_SAFE(val) && val < feeder_rate_max))
180 		return (EINVAL);
181 
182 	feeder_rate_min = val;
183 
184 	return (0);
185 }
186 SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_min, CTLTYPE_INT | CTLFLAG_RWTUN,
187     0, sizeof(int), sysctl_hw_snd_feeder_rate_min, "I",
188     "minimum allowable rate");
189 
190 static int
191 sysctl_hw_snd_feeder_rate_max(SYSCTL_HANDLER_ARGS)
192 {
193 	int err, val;
194 
195 	val = feeder_rate_max;
196 	err = sysctl_handle_int(oidp, &val, 0, req);
197 
198 	if (err != 0 || req->newptr == NULL || val == feeder_rate_max)
199 		return (err);
200 
201 	if (!(Z_FACTOR_SAFE(val) && val > feeder_rate_min))
202 		return (EINVAL);
203 
204 	feeder_rate_max = val;
205 
206 	return (0);
207 }
208 SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_max, CTLTYPE_INT | CTLFLAG_RWTUN,
209     0, sizeof(int), sysctl_hw_snd_feeder_rate_max, "I",
210     "maximum allowable rate");
211 
212 static int
213 sysctl_hw_snd_feeder_rate_round(SYSCTL_HANDLER_ARGS)
214 {
215 	int err, val;
216 
217 	val = feeder_rate_round;
218 	err = sysctl_handle_int(oidp, &val, 0, req);
219 
220 	if (err != 0 || req->newptr == NULL || val == feeder_rate_round)
221 		return (err);
222 
223 	if (val < Z_ROUNDHZ_MIN || val > Z_ROUNDHZ_MAX)
224 		return (EINVAL);
225 
226 	feeder_rate_round = val - (val % Z_ROUNDHZ);
227 
228 	return (0);
229 }
230 SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_round, CTLTYPE_INT | CTLFLAG_RWTUN,
231     0, sizeof(int), sysctl_hw_snd_feeder_rate_round, "I",
232     "sample rate converter rounding threshold");
233 
234 static int
235 sysctl_hw_snd_feeder_rate_quality(SYSCTL_HANDLER_ARGS)
236 {
237 	struct snddev_info *d;
238 	struct pcm_channel *c;
239 	struct pcm_feeder *f;
240 	int i, err, val;
241 
242 	val = feeder_rate_quality;
243 	err = sysctl_handle_int(oidp, &val, 0, req);
244 
245 	if (err != 0 || req->newptr == NULL || val == feeder_rate_quality)
246 		return (err);
247 
248 	if (val < Z_QUALITY_MIN || val > Z_QUALITY_MAX)
249 		return (EINVAL);
250 
251 	feeder_rate_quality = val;
252 
253 	/*
254 	 * Traverse all available channels on each device and try to
255 	 * set resampler quality if and only if it is exist as
256 	 * part of feeder chains and the channel is idle.
257 	 */
258 	for (i = 0; pcm_devclass != NULL &&
259 	    i < devclass_get_maxunit(pcm_devclass); i++) {
260 		d = devclass_get_softc(pcm_devclass, i);
261 		if (!PCM_REGISTERED(d))
262 			continue;
263 		PCM_LOCK(d);
264 		PCM_WAIT(d);
265 		PCM_ACQUIRE(d);
266 		CHN_FOREACH(c, d, channels.pcm) {
267 			CHN_LOCK(c);
268 			f = chn_findfeeder(c, FEEDER_RATE);
269 			if (f == NULL || f->data == NULL || CHN_STARTED(c)) {
270 				CHN_UNLOCK(c);
271 				continue;
272 			}
273 			(void)FEEDER_SET(f, FEEDRATE_QUALITY, val);
274 			CHN_UNLOCK(c);
275 		}
276 		PCM_RELEASE(d);
277 		PCM_UNLOCK(d);
278 	}
279 
280 	return (0);
281 }
282 SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_quality, CTLTYPE_INT | CTLFLAG_RWTUN,
283     0, sizeof(int), sysctl_hw_snd_feeder_rate_quality, "I",
284     "sample rate converter quality ("__XSTRING(Z_QUALITY_MIN)"=low .. "
285     __XSTRING(Z_QUALITY_MAX)"=high)");
286 #endif	/* _KERNEL */
287 
288 
289 /*
290  * Resampler type.
291  */
292 #define Z_IS_ZOH(i)		((i)->quality == Z_QUALITY_ZOH)
293 #define Z_IS_LINEAR(i)		((i)->quality == Z_QUALITY_LINEAR)
294 #define Z_IS_SINC(i)		((i)->quality > Z_QUALITY_LINEAR)
295 
296 /*
297  * Macroses for accurate sample time drift calculations.
298  *
299  * gy2gx : given the amount of output, return the _exact_ required amount of
300  *         input.
301  * gx2gy : given the amount of input, return the _maximum_ amount of output
302  *         that will be generated.
303  * drift : given the amount of input and output, return the elapsed
304  *         sample-time.
305  */
306 #define _Z_GCAST(x)		((uint64_t)(x))
307 
308 #if defined(__GNUCLIKE_ASM) && defined(__i386__)
309 /*
310  * This is where i386 being beaten to a pulp. Fortunately this function is
311  * rarely being called and if it is, it will decide the best (hopefully)
312  * fastest way to do the division. If we can ensure that everything is dword
313  * aligned, letting the compiler to call udivdi3 to do the division can be
314  * faster compared to this.
315  *
316  * amd64 is the clear winner here, no question about it.
317  */
318 static __inline uint32_t
319 Z_DIV(uint64_t v, uint32_t d)
320 {
321 	uint32_t hi, lo, quo, rem;
322 
323 	hi = v >> 32;
324 	lo = v & 0xffffffff;
325 
326 	/*
327 	 * As much as we can, try to avoid long division like a plague.
328 	 */
329 	if (hi == 0)
330 		quo = lo / d;
331 	else
332 		__asm("divl %2"
333 		    : "=a" (quo), "=d" (rem)
334 		    : "r" (d), "0" (lo), "1" (hi));
335 
336 	return (quo);
337 }
338 #else
339 #define Z_DIV(x, y)		((x) / (y))
340 #endif
341 
342 #define _Z_GY2GX(i, a, v)						\
343 	Z_DIV(((_Z_GCAST((i)->z_gx) * (v)) + ((i)->z_gy - (a) - 1)),	\
344 	(i)->z_gy)
345 
346 #define _Z_GX2GY(i, a, v)						\
347 	Z_DIV(((_Z_GCAST((i)->z_gy) * (v)) + (a)), (i)->z_gx)
348 
349 #define _Z_DRIFT(i, x, y)						\
350 	((_Z_GCAST((i)->z_gy) * (x)) - (_Z_GCAST((i)->z_gx) * (y)))
351 
352 #define z_gy2gx(i, v)		_Z_GY2GX(i, (i)->z_alpha, v)
353 #define z_gx2gy(i, v)		_Z_GX2GY(i, (i)->z_alpha, v)
354 #define z_drift(i, x, y)	_Z_DRIFT(i, x, y)
355 
356 /*
357  * Macroses for SINC coefficients table manipulations.. whatever.
358  */
359 #define Z_SINC_COEFF_IDX(i)	((i)->quality - Z_QUALITY_LINEAR - 1)
360 
361 #define Z_SINC_LEN(i)							\
362 	((int32_t)(((uint64_t)z_coeff_tab[Z_SINC_COEFF_IDX(i)].len <<	\
363 	    Z_SHIFT) / (i)->z_dy))
364 
365 #define Z_SINC_BASE_LEN(i)						\
366 	((z_coeff_tab[Z_SINC_COEFF_IDX(i)].len - 1) >> (Z_DRIFT_SHIFT - 1))
367 
368 /*
369  * Macroses for linear delay buffer operations. Alignment is not
370  * really necessary since we're not using true circular buffer, but it
371  * will help us guard against possible trespasser. To be honest,
372  * the linear block operations does not need guarding at all due to
373  * accurate drifting!
374  */
375 #define z_align(i, v)		((v) & (i)->z_mask)
376 #define z_next(i, o, v)		z_align(i, (o) + (v))
377 #define z_prev(i, o, v)		z_align(i, (o) - (v))
378 #define z_fetched(i)		(z_align(i, (i)->z_pos - (i)->z_start) - 1)
379 #define z_free(i)		((i)->z_full - (i)->z_pos)
380 
381 /*
382  * Macroses for Bla Bla .. :)
383  */
384 #define z_copy(src, dst, sz)	(void)memcpy(dst, src, sz)
385 #define z_feed(...)		FEEDER_FEED(__VA_ARGS__)
386 
387 static __inline uint32_t
388 z_min(uint32_t x, uint32_t y)
389 {
390 
391 	return ((x < y) ? x : y);
392 }
393 
394 static int32_t
395 z_gcd(int32_t x, int32_t y)
396 {
397 	int32_t w;
398 
399 	while (y != 0) {
400 		w = x % y;
401 		x = y;
402 		y = w;
403 	}
404 
405 	return (x);
406 }
407 
408 static int32_t
409 z_roundpow2(int32_t v)
410 {
411 	int32_t i;
412 
413 	i = 1;
414 
415 	/*
416 	 * Let it overflow at will..
417 	 */
418 	while (i > 0 && i < v)
419 		i <<= 1;
420 
421 	return (i);
422 }
423 
424 /*
425  * Zero Order Hold, the worst of the worst, an insult against quality,
426  * but super fast.
427  */
428 static void
429 z_feed_zoh(struct z_info *info, uint8_t *dst)
430 {
431 #if 0
432 	z_copy(info->z_delay +
433 	    (info->z_start * info->channels * info->bps), dst,
434 	    info->channels * info->bps);
435 #else
436 	uint32_t cnt;
437 	uint8_t *src;
438 
439 	cnt = info->channels * info->bps;
440 	src = info->z_delay + (info->z_start * cnt);
441 
442 	/*
443 	 * This is a bit faster than doing bcopy() since we're dealing
444 	 * with possible unaligned samples.
445 	 */
446 	do {
447 		*dst++ = *src++;
448 	} while (--cnt != 0);
449 #endif
450 }
451 
452 /*
453  * Linear Interpolation. This at least sounds better (perceptually) and fast,
454  * but without any proper filtering which means aliasing still exist and
455  * could become worst with a right sample. Interpolation centered within
456  * Z_LINEAR_ONE between the present and previous sample and everything is
457  * done with simple 32bit scaling arithmetic.
458  */
459 #define Z_DECLARE_LINEAR(SIGN, BIT, ENDIAN)					\
460 static void									\
461 z_feed_linear_##SIGN##BIT##ENDIAN(struct z_info *info, uint8_t *dst)		\
462 {										\
463 	int32_t z;								\
464 	intpcm_t x, y;								\
465 	uint32_t ch;								\
466 	uint8_t *sx, *sy;							\
467 										\
468 	z = ((uint32_t)info->z_alpha * info->z_dx) >> Z_LINEAR_UNSHIFT;		\
469 										\
470 	sx = info->z_delay + (info->z_start * info->channels *			\
471 	    PCM_##BIT##_BPS);							\
472 	sy = sx - (info->channels * PCM_##BIT##_BPS);				\
473 										\
474 	ch = info->channels;							\
475 										\
476 	do {									\
477 		x = _PCM_READ_##SIGN##BIT##_##ENDIAN(sx);			\
478 		y = _PCM_READ_##SIGN##BIT##_##ENDIAN(sy);			\
479 		x = Z_LINEAR_INTERPOLATE_##BIT(z, x, y);			\
480 		_PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, x);			\
481 		sx += PCM_##BIT##_BPS;						\
482 		sy += PCM_##BIT##_BPS;						\
483 		dst += PCM_##BIT##_BPS;						\
484 	} while (--ch != 0);							\
485 }
486 
487 /*
488  * Userland clipping diagnostic check, not enabled in kernel compilation.
489  * While doing sinc interpolation, unrealistic samples like full scale sine
490  * wav will clip, but for other things this will not make any noise at all.
491  * Everybody should learn how to normalized perceived loudness of their own
492  * music/sounds/samples (hint: ReplayGain).
493  */
494 #ifdef Z_DIAGNOSTIC
495 #define Z_CLIP_CHECK(v, BIT)	do {					\
496 	if ((v) > PCM_S##BIT##_MAX) {					\
497 		fprintf(stderr, "Overflow: v=%jd, max=%jd\n",		\
498 		    (intmax_t)(v), (intmax_t)PCM_S##BIT##_MAX);		\
499 	} else if ((v) < PCM_S##BIT##_MIN) {				\
500 		fprintf(stderr, "Underflow: v=%jd, min=%jd\n",		\
501 		    (intmax_t)(v), (intmax_t)PCM_S##BIT##_MIN);		\
502 	}								\
503 } while (0)
504 #else
505 #define Z_CLIP_CHECK(...)
506 #endif
507 
508 #define Z_CLAMP(v, BIT)							\
509 	(((v) > PCM_S##BIT##_MAX) ? PCM_S##BIT##_MAX :			\
510 	(((v) < PCM_S##BIT##_MIN) ? PCM_S##BIT##_MIN : (v)))
511 
512 /*
513  * Sine Cardinal (SINC) Interpolation. Scaling is done in 64 bit, so
514  * there's no point to hold the plate any longer. All samples will be
515  * shifted to a full 32 bit, scaled and restored during write for
516  * maximum dynamic range (only for downsampling).
517  */
518 #define _Z_SINC_ACCUMULATE(SIGN, BIT, ENDIAN, adv)			\
519 	c += z >> Z_SHIFT;						\
520 	z &= Z_MASK;							\
521 	coeff = Z_COEFF_INTERPOLATE(z, z_coeff[c], z_dcoeff[c]);	\
522 	x = _PCM_READ_##SIGN##BIT##_##ENDIAN(p);			\
523 	v += Z_NORM_##BIT((intpcm64_t)x * coeff);			\
524 	z += info->z_dy;						\
525 	p adv##= info->channels * PCM_##BIT##_BPS
526 
527 /*
528  * XXX GCC4 optimization is such a !@#$%, need manual unrolling.
529  */
530 #if defined(__GNUC__) && __GNUC__ >= 4
531 #define Z_SINC_ACCUMULATE(...)	do {					\
532 	_Z_SINC_ACCUMULATE(__VA_ARGS__);				\
533 	_Z_SINC_ACCUMULATE(__VA_ARGS__);				\
534 } while (0)
535 #define Z_SINC_ACCUMULATE_DECR		2
536 #else
537 #define Z_SINC_ACCUMULATE(...)	do {					\
538 	_Z_SINC_ACCUMULATE(__VA_ARGS__);				\
539 } while (0)
540 #define Z_SINC_ACCUMULATE_DECR		1
541 #endif
542 
543 #define Z_DECLARE_SINC(SIGN, BIT, ENDIAN)					\
544 static void									\
545 z_feed_sinc_##SIGN##BIT##ENDIAN(struct z_info *info, uint8_t *dst)		\
546 {										\
547 	intpcm64_t v;								\
548 	intpcm_t x;								\
549 	uint8_t *p;								\
550 	int32_t coeff, z, *z_coeff, *z_dcoeff;					\
551 	uint32_t c, center, ch, i;						\
552 										\
553 	z_coeff = info->z_coeff;						\
554 	z_dcoeff = info->z_dcoeff;						\
555 	center = z_prev(info, info->z_start, info->z_size);			\
556 	ch = info->channels * PCM_##BIT##_BPS;					\
557 	dst += ch;								\
558 										\
559 	do {									\
560 		dst -= PCM_##BIT##_BPS;						\
561 		ch -= PCM_##BIT##_BPS;						\
562 		v = 0;								\
563 		z = info->z_alpha * info->z_dx;					\
564 		c = 0;								\
565 		p = info->z_delay + (z_next(info, center, 1) *			\
566 		    info->channels * PCM_##BIT##_BPS) + ch;			\
567 		for (i = info->z_size; i != 0; i -= Z_SINC_ACCUMULATE_DECR) 	\
568 			Z_SINC_ACCUMULATE(SIGN, BIT, ENDIAN, +);		\
569 		z = info->z_dy - (info->z_alpha * info->z_dx);			\
570 		c = 0;								\
571 		p = info->z_delay + (center * info->channels *			\
572 		    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 		if (info->z_scale != Z_ONE)					\
576 			v = Z_SCALE_##BIT(v, info->z_scale);			\
577 		else								\
578 			v >>= Z_COEFF_SHIFT - Z_GUARD_BIT_##BIT;		\
579 		Z_CLIP_CHECK(v, BIT);						\
580 		_PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, Z_CLAMP(v, BIT));	\
581 	} while (ch != 0);							\
582 }
583 
584 #define Z_DECLARE_SINC_POLYPHASE(SIGN, BIT, ENDIAN)				\
585 static void									\
586 z_feed_sinc_polyphase_##SIGN##BIT##ENDIAN(struct z_info *info, uint8_t *dst)	\
587 {										\
588 	intpcm64_t v;								\
589 	intpcm_t x;								\
590 	uint8_t *p;								\
591 	int32_t ch, i, start, *z_pcoeff;					\
592 										\
593 	ch = info->channels * PCM_##BIT##_BPS;					\
594 	dst += ch;								\
595 	start = z_prev(info, info->z_start, (info->z_size << 1) - 1) * ch;	\
596 										\
597 	do {									\
598 		dst -= PCM_##BIT##_BPS;						\
599 		ch -= PCM_##BIT##_BPS;						\
600 		v = 0;								\
601 		p = info->z_delay + start + ch;					\
602 		z_pcoeff = info->z_pcoeff +					\
603 		    ((info->z_alpha * info->z_size) << 1);			\
604 		for (i = info->z_size; i != 0; i--) {				\
605 			x = _PCM_READ_##SIGN##BIT##_##ENDIAN(p);		\
606 			v += Z_NORM_##BIT((intpcm64_t)x * *z_pcoeff);		\
607 			z_pcoeff++;						\
608 			p += info->channels * PCM_##BIT##_BPS;			\
609 			x = _PCM_READ_##SIGN##BIT##_##ENDIAN(p);		\
610 			v += Z_NORM_##BIT((intpcm64_t)x * *z_pcoeff);		\
611 			z_pcoeff++;						\
612 			p += info->channels * PCM_##BIT##_BPS;			\
613 		}								\
614 		if (info->z_scale != Z_ONE)					\
615 			v = Z_SCALE_##BIT(v, info->z_scale);			\
616 		else								\
617 			v >>= Z_COEFF_SHIFT - Z_GUARD_BIT_##BIT;		\
618 		Z_CLIP_CHECK(v, BIT);						\
619 		_PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, Z_CLAMP(v, BIT));	\
620 	} while (ch != 0);							\
621 }
622 
623 #define Z_DECLARE(SIGN, BIT, ENDIAN)					\
624 	Z_DECLARE_LINEAR(SIGN, BIT, ENDIAN)				\
625 	Z_DECLARE_SINC(SIGN, BIT, ENDIAN)				\
626 	Z_DECLARE_SINC_POLYPHASE(SIGN, BIT, ENDIAN)
627 
628 #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
629 Z_DECLARE(S, 16, LE)
630 Z_DECLARE(S, 32, LE)
631 #endif
632 #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
633 Z_DECLARE(S, 16, BE)
634 Z_DECLARE(S, 32, BE)
635 #endif
636 #ifdef SND_FEEDER_MULTIFORMAT
637 Z_DECLARE(S,  8, NE)
638 Z_DECLARE(S, 24, LE)
639 Z_DECLARE(S, 24, BE)
640 Z_DECLARE(U,  8, NE)
641 Z_DECLARE(U, 16, LE)
642 Z_DECLARE(U, 24, LE)
643 Z_DECLARE(U, 32, LE)
644 Z_DECLARE(U, 16, BE)
645 Z_DECLARE(U, 24, BE)
646 Z_DECLARE(U, 32, BE)
647 #endif
648 
649 enum {
650 	Z_RESAMPLER_ZOH,
651 	Z_RESAMPLER_LINEAR,
652 	Z_RESAMPLER_SINC,
653 	Z_RESAMPLER_SINC_POLYPHASE,
654 	Z_RESAMPLER_LAST
655 };
656 
657 #define Z_RESAMPLER_IDX(i)						\
658 	(Z_IS_SINC(i) ? Z_RESAMPLER_SINC : (i)->quality)
659 
660 #define Z_RESAMPLER_ENTRY(SIGN, BIT, ENDIAN)					\
661 	{									\
662 	    AFMT_##SIGN##BIT##_##ENDIAN,					\
663 	    {									\
664 		[Z_RESAMPLER_ZOH]    = z_feed_zoh,				\
665 		[Z_RESAMPLER_LINEAR] = z_feed_linear_##SIGN##BIT##ENDIAN,	\
666 		[Z_RESAMPLER_SINC]   = z_feed_sinc_##SIGN##BIT##ENDIAN,		\
667 		[Z_RESAMPLER_SINC_POLYPHASE]   =				\
668 		    z_feed_sinc_polyphase_##SIGN##BIT##ENDIAN			\
669 	    }									\
670 	}
671 
672 static const struct {
673 	uint32_t format;
674 	z_resampler_t resampler[Z_RESAMPLER_LAST];
675 } z_resampler_tab[] = {
676 #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
677 	Z_RESAMPLER_ENTRY(S, 16, LE),
678 	Z_RESAMPLER_ENTRY(S, 32, LE),
679 #endif
680 #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
681 	Z_RESAMPLER_ENTRY(S, 16, BE),
682 	Z_RESAMPLER_ENTRY(S, 32, BE),
683 #endif
684 #ifdef SND_FEEDER_MULTIFORMAT
685 	Z_RESAMPLER_ENTRY(S,  8, NE),
686 	Z_RESAMPLER_ENTRY(S, 24, LE),
687 	Z_RESAMPLER_ENTRY(S, 24, BE),
688 	Z_RESAMPLER_ENTRY(U,  8, NE),
689 	Z_RESAMPLER_ENTRY(U, 16, LE),
690 	Z_RESAMPLER_ENTRY(U, 24, LE),
691 	Z_RESAMPLER_ENTRY(U, 32, LE),
692 	Z_RESAMPLER_ENTRY(U, 16, BE),
693 	Z_RESAMPLER_ENTRY(U, 24, BE),
694 	Z_RESAMPLER_ENTRY(U, 32, BE),
695 #endif
696 };
697 
698 #define Z_RESAMPLER_TAB_SIZE						\
699 	((int32_t)(sizeof(z_resampler_tab) / sizeof(z_resampler_tab[0])))
700 
701 static void
702 z_resampler_reset(struct z_info *info)
703 {
704 
705 	info->src = info->rsrc - (info->rsrc % ((feeder_rate_round > 0 &&
706 	    info->rsrc > feeder_rate_round) ? feeder_rate_round : 1));
707 	info->dst = info->rdst - (info->rdst % ((feeder_rate_round > 0 &&
708 	    info->rdst > feeder_rate_round) ? feeder_rate_round : 1));
709 	info->z_gx = 1;
710 	info->z_gy = 1;
711 	info->z_alpha = 0;
712 	info->z_resample = NULL;
713 	info->z_size = 1;
714 	info->z_coeff = NULL;
715 	info->z_dcoeff = NULL;
716 	if (info->z_pcoeff != NULL) {
717 		free(info->z_pcoeff, M_DEVBUF);
718 		info->z_pcoeff = NULL;
719 	}
720 	info->z_scale = Z_ONE;
721 	info->z_dx = Z_FULL_ONE;
722 	info->z_dy = Z_FULL_ONE;
723 #ifdef Z_DIAGNOSTIC
724 	info->z_cycle = 0;
725 #endif
726 	if (info->quality < Z_QUALITY_MIN)
727 		info->quality = Z_QUALITY_MIN;
728 	else if (info->quality > Z_QUALITY_MAX)
729 		info->quality = Z_QUALITY_MAX;
730 }
731 
732 #ifdef Z_PARANOID
733 static int32_t
734 z_resampler_sinc_len(struct z_info *info)
735 {
736 	int32_t c, z, len, lmax;
737 
738 	if (!Z_IS_SINC(info))
739 		return (1);
740 
741 	/*
742 	 * A rather careful (or useless) way to calculate filter length.
743 	 * Z_SINC_LEN() itself is accurate enough to do its job. Extra
744 	 * sanity checking is not going to hurt though..
745 	 */
746 	c = 0;
747 	z = info->z_dy;
748 	len = 0;
749 	lmax = z_coeff_tab[Z_SINC_COEFF_IDX(info)].len;
750 
751 	do {
752 		c += z >> Z_SHIFT;
753 		z &= Z_MASK;
754 		z += info->z_dy;
755 	} while (c < lmax && ++len > 0);
756 
757 	if (len != Z_SINC_LEN(info)) {
758 #ifdef _KERNEL
759 		printf("%s(): sinc l=%d != Z_SINC_LEN=%d\n",
760 		    __func__, len, Z_SINC_LEN(info));
761 #else
762 		fprintf(stderr, "%s(): sinc l=%d != Z_SINC_LEN=%d\n",
763 		    __func__, len, Z_SINC_LEN(info));
764 		return (-1);
765 #endif
766 	}
767 
768 	return (len);
769 }
770 #else
771 #define z_resampler_sinc_len(i)		(Z_IS_SINC(i) ? Z_SINC_LEN(i) : 1)
772 #endif
773 
774 #define Z_POLYPHASE_COEFF_SHIFT		0
775 
776 /*
777  * Pick suitable polynomial interpolators based on filter oversampled ratio
778  * (2 ^ Z_DRIFT_SHIFT).
779  */
780 #if !(defined(Z_COEFF_INTERP_ZOH) || defined(Z_COEFF_INTERP_LINEAR) ||		\
781     defined(Z_COEFF_INTERP_QUADRATIC) || defined(Z_COEFF_INTERP_HERMITE) ||	\
782     defined(Z_COEFF_INTER_BSPLINE) || defined(Z_COEFF_INTERP_OPT32X) ||		\
783     defined(Z_COEFF_INTERP_OPT16X) || defined(Z_COEFF_INTERP_OPT8X) ||		\
784     defined(Z_COEFF_INTERP_OPT4X) || defined(Z_COEFF_INTERP_OPT2X))
785 #if Z_DRIFT_SHIFT >= 6
786 #define Z_COEFF_INTERP_BSPLINE		1
787 #elif Z_DRIFT_SHIFT >= 5
788 #define Z_COEFF_INTERP_OPT32X		1
789 #elif Z_DRIFT_SHIFT == 4
790 #define Z_COEFF_INTERP_OPT16X		1
791 #elif Z_DRIFT_SHIFT == 3
792 #define Z_COEFF_INTERP_OPT8X		1
793 #elif Z_DRIFT_SHIFT == 2
794 #define Z_COEFF_INTERP_OPT4X		1
795 #elif Z_DRIFT_SHIFT == 1
796 #define Z_COEFF_INTERP_OPT2X		1
797 #else
798 #error "Z_DRIFT_SHIFT screwed!"
799 #endif
800 #endif
801 
802 /*
803  * In classic polyphase mode, the actual coefficients for each phases need to
804  * be calculated based on default prototype filters. For highly oversampled
805  * filter, linear or quadradatic interpolator should be enough. Anything less
806  * than that require 'special' interpolators to reduce interpolation errors.
807  *
808  * "Polynomial Interpolators for High-Quality Resampling of Oversampled Audio"
809  *    by Olli Niemitalo
810  *    - http://www.student.oulu.fi/~oniemita/dsp/deip.pdf
811  *
812  */
813 static int32_t
814 z_coeff_interpolate(int32_t z, int32_t *z_coeff)
815 {
816 	int32_t coeff;
817 #if defined(Z_COEFF_INTERP_ZOH)
818 
819 	/* 1-point, 0th-order (Zero Order Hold) */
820 	z = z;
821 	coeff = z_coeff[0];
822 #elif defined(Z_COEFF_INTERP_LINEAR)
823 	int32_t zl0, zl1;
824 
825 	/* 2-point, 1st-order Linear */
826 	zl0 = z_coeff[0];
827 	zl1 = z_coeff[1] - z_coeff[0];
828 
829 	coeff = Z_RSHIFT((int64_t)zl1 * z, Z_SHIFT) + zl0;
830 #elif defined(Z_COEFF_INTERP_QUADRATIC)
831 	int32_t zq0, zq1, zq2;
832 
833 	/* 3-point, 2nd-order Quadratic */
834 	zq0 = z_coeff[0];
835 	zq1 = z_coeff[1] - z_coeff[-1];
836 	zq2 = z_coeff[1] + z_coeff[-1] - (z_coeff[0] << 1);
837 
838 	coeff = Z_RSHIFT((Z_RSHIFT((int64_t)zq2 * z, Z_SHIFT) +
839 	    zq1) * z, Z_SHIFT + 1) + zq0;
840 #elif defined(Z_COEFF_INTERP_HERMITE)
841 	int32_t zh0, zh1, zh2, zh3;
842 
843 	/* 4-point, 3rd-order Hermite */
844 	zh0 = z_coeff[0];
845 	zh1 = z_coeff[1] - z_coeff[-1];
846 	zh2 = (z_coeff[-1] << 1) - (z_coeff[0] * 5) + (z_coeff[1] << 2) -
847 	    z_coeff[2];
848 	zh3 = z_coeff[2] - z_coeff[-1] + ((z_coeff[0] - z_coeff[1]) * 3);
849 
850 	coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((int64_t)zh3 * z, Z_SHIFT) +
851 	    zh2) * z, Z_SHIFT) + zh1) * z, Z_SHIFT + 1) + zh0;
852 #elif defined(Z_COEFF_INTERP_BSPLINE)
853 	int32_t zb0, zb1, zb2, zb3;
854 
855 	/* 4-point, 3rd-order B-Spline */
856 	zb0 = Z_RSHIFT(0x15555555LL * (((int64_t)z_coeff[0] << 2) +
857 	    z_coeff[-1] + z_coeff[1]), 30);
858 	zb1 = z_coeff[1] - z_coeff[-1];
859 	zb2 = z_coeff[-1] + z_coeff[1] - (z_coeff[0] << 1);
860 	zb3 = Z_RSHIFT(0x15555555LL * (((z_coeff[0] - z_coeff[1]) * 3) +
861 	    z_coeff[2] - z_coeff[-1]), 30);
862 
863 	coeff = (Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((int64_t)zb3 * z, Z_SHIFT) +
864 	    zb2) * z, Z_SHIFT) + zb1) * z, Z_SHIFT) + zb0 + 1) >> 1;
865 #elif defined(Z_COEFF_INTERP_OPT32X)
866 	int32_t zoz, zoe1, zoe2, zoe3, zoo1, zoo2, zoo3;
867 	int32_t zoc0, zoc1, zoc2, zoc3, zoc4, zoc5;
868 
869 	/* 6-point, 5th-order Optimal 32x */
870 	zoz = z - (Z_ONE >> 1);
871 	zoe1 = z_coeff[1] + z_coeff[0];
872 	zoe2 = z_coeff[2] + z_coeff[-1];
873 	zoe3 = z_coeff[3] + z_coeff[-2];
874 	zoo1 = z_coeff[1] - z_coeff[0];
875 	zoo2 = z_coeff[2] - z_coeff[-1];
876 	zoo3 = z_coeff[3] - z_coeff[-2];
877 
878 	zoc0 = Z_RSHIFT((0x1ac2260dLL * zoe1) + (0x0526cdcaLL * zoe2) +
879 	    (0x00170c29LL * zoe3), 30);
880 	zoc1 = Z_RSHIFT((0x14f8a49aLL * zoo1) + (0x0d6d1109LL * zoo2) +
881 	    (0x008cd4dcLL * zoo3), 30);
882 	zoc2 = Z_RSHIFT((-0x0d3e94a4LL * zoe1) + (0x0bddded4LL * zoe2) +
883 	    (0x0160b5d0LL * zoe3), 30);
884 	zoc3 = Z_RSHIFT((-0x0de10cc4LL * zoo1) + (0x019b2a7dLL * zoo2) +
885 	    (0x01cfe914LL * zoo3), 30);
886 	zoc4 = Z_RSHIFT((0x02aa12d7LL * zoe1) + (-0x03ff1bb3LL * zoe2) +
887 	    (0x015508ddLL * zoe3), 30);
888 	zoc5 = Z_RSHIFT((0x051d29e5LL * zoo1) + (-0x028e7647LL * zoo2) +
889 	    (0x0082d81aLL * zoo3), 30);
890 
891 	coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT(
892 	    (int64_t)zoc5 * zoz, Z_SHIFT) +
893 	    zoc4) * zoz, Z_SHIFT) + zoc3) * zoz, Z_SHIFT) +
894 	    zoc2) * zoz, Z_SHIFT) + zoc1) * zoz, Z_SHIFT) + zoc0;
895 #elif defined(Z_COEFF_INTERP_OPT16X)
896 	int32_t zoz, zoe1, zoe2, zoe3, zoo1, zoo2, zoo3;
897 	int32_t zoc0, zoc1, zoc2, zoc3, zoc4, zoc5;
898 
899 	/* 6-point, 5th-order Optimal 16x */
900 	zoz = z - (Z_ONE >> 1);
901 	zoe1 = z_coeff[1] + z_coeff[0];
902 	zoe2 = z_coeff[2] + z_coeff[-1];
903 	zoe3 = z_coeff[3] + z_coeff[-2];
904 	zoo1 = z_coeff[1] - z_coeff[0];
905 	zoo2 = z_coeff[2] - z_coeff[-1];
906 	zoo3 = z_coeff[3] - z_coeff[-2];
907 
908 	zoc0 = Z_RSHIFT((0x1ac2260dLL * zoe1) + (0x0526cdcaLL * zoe2) +
909 	    (0x00170c29LL * zoe3), 30);
910 	zoc1 = Z_RSHIFT((0x14f8a49aLL * zoo1) + (0x0d6d1109LL * zoo2) +
911 	    (0x008cd4dcLL * zoo3), 30);
912 	zoc2 = Z_RSHIFT((-0x0d3e94a4LL * zoe1) + (0x0bddded4LL * zoe2) +
913 	    (0x0160b5d0LL * zoe3), 30);
914 	zoc3 = Z_RSHIFT((-0x0de10cc4LL * zoo1) + (0x019b2a7dLL * zoo2) +
915 	    (0x01cfe914LL * zoo3), 30);
916 	zoc4 = Z_RSHIFT((0x02aa12d7LL * zoe1) + (-0x03ff1bb3LL * zoe2) +
917 	    (0x015508ddLL * zoe3), 30);
918 	zoc5 = Z_RSHIFT((0x051d29e5LL * zoo1) + (-0x028e7647LL * zoo2) +
919 	    (0x0082d81aLL * zoo3), 30);
920 
921 	coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT(
922 	    (int64_t)zoc5 * zoz, Z_SHIFT) +
923 	    zoc4) * zoz, Z_SHIFT) + zoc3) * zoz, Z_SHIFT) +
924 	    zoc2) * zoz, Z_SHIFT) + zoc1) * zoz, Z_SHIFT) + zoc0;
925 #elif defined(Z_COEFF_INTERP_OPT8X)
926 	int32_t zoz, zoe1, zoe2, zoe3, zoo1, zoo2, zoo3;
927 	int32_t zoc0, zoc1, zoc2, zoc3, zoc4, zoc5;
928 
929 	/* 6-point, 5th-order Optimal 8x */
930 	zoz = z - (Z_ONE >> 1);
931 	zoe1 = z_coeff[1] + z_coeff[0];
932 	zoe2 = z_coeff[2] + z_coeff[-1];
933 	zoe3 = z_coeff[3] + z_coeff[-2];
934 	zoo1 = z_coeff[1] - z_coeff[0];
935 	zoo2 = z_coeff[2] - z_coeff[-1];
936 	zoo3 = z_coeff[3] - z_coeff[-2];
937 
938 	zoc0 = Z_RSHIFT((0x1aa9b47dLL * zoe1) + (0x053d9944LL * zoe2) +
939 	    (0x0018b23fLL * zoe3), 30);
940 	zoc1 = Z_RSHIFT((0x14a104d1LL * zoo1) + (0x0d7d2504LL * zoo2) +
941 	    (0x0094b599LL * zoo3), 30);
942 	zoc2 = Z_RSHIFT((-0x0d22530bLL * zoe1) + (0x0bb37a2cLL * zoe2) +
943 	    (0x016ed8e0LL * zoe3), 30);
944 	zoc3 = Z_RSHIFT((-0x0d744b1cLL * zoo1) + (0x01649591LL * zoo2) +
945 	    (0x01dae93aLL * zoo3), 30);
946 	zoc4 = Z_RSHIFT((0x02a7ee1bLL * zoe1) + (-0x03fbdb24LL * zoe2) +
947 	    (0x0153ed07LL * zoe3), 30);
948 	zoc5 = Z_RSHIFT((0x04cf9b6cLL * zoo1) + (-0x0266b378LL * zoo2) +
949 	    (0x007a7c26LL * zoo3), 30);
950 
951 	coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT(
952 	    (int64_t)zoc5 * zoz, Z_SHIFT) +
953 	    zoc4) * zoz, Z_SHIFT) + zoc3) * zoz, Z_SHIFT) +
954 	    zoc2) * zoz, Z_SHIFT) + zoc1) * zoz, Z_SHIFT) + zoc0;
955 #elif defined(Z_COEFF_INTERP_OPT4X)
956 	int32_t zoz, zoe1, zoe2, zoe3, zoo1, zoo2, zoo3;
957 	int32_t zoc0, zoc1, zoc2, zoc3, zoc4, zoc5;
958 
959 	/* 6-point, 5th-order Optimal 4x */
960 	zoz = z - (Z_ONE >> 1);
961 	zoe1 = z_coeff[1] + z_coeff[0];
962 	zoe2 = z_coeff[2] + z_coeff[-1];
963 	zoe3 = z_coeff[3] + z_coeff[-2];
964 	zoo1 = z_coeff[1] - z_coeff[0];
965 	zoo2 = z_coeff[2] - z_coeff[-1];
966 	zoo3 = z_coeff[3] - z_coeff[-2];
967 
968 	zoc0 = Z_RSHIFT((0x1a8eda43LL * zoe1) + (0x0556ee38LL * zoe2) +
969 	    (0x001a3784LL * zoe3), 30);
970 	zoc1 = Z_RSHIFT((0x143d863eLL * zoo1) + (0x0d910e36LL * zoo2) +
971 	    (0x009ca889LL * zoo3), 30);
972 	zoc2 = Z_RSHIFT((-0x0d026821LL * zoe1) + (0x0b837773LL * zoe2) +
973 	    (0x017ef0c6LL * zoe3), 30);
974 	zoc3 = Z_RSHIFT((-0x0cef1502LL * zoo1) + (0x01207a8eLL * zoo2) +
975 	    (0x01e936dbLL * zoo3), 30);
976 	zoc4 = Z_RSHIFT((0x029fe643LL * zoe1) + (-0x03ef3fc8LL * zoe2) +
977 	    (0x014f5923LL * zoe3), 30);
978 	zoc5 = Z_RSHIFT((0x043a9d08LL * zoo1) + (-0x02154febLL * zoo2) +
979 	    (0x00670dbdLL * zoo3), 30);
980 
981 	coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT(
982 	    (int64_t)zoc5 * zoz, Z_SHIFT) +
983 	    zoc4) * zoz, Z_SHIFT) + zoc3) * zoz, Z_SHIFT) +
984 	    zoc2) * zoz, Z_SHIFT) + zoc1) * zoz, Z_SHIFT) + zoc0;
985 #elif defined(Z_COEFF_INTERP_OPT2X)
986 	int32_t zoz, zoe1, zoe2, zoe3, zoo1, zoo2, zoo3;
987 	int32_t zoc0, zoc1, zoc2, zoc3, zoc4, zoc5;
988 
989 	/* 6-point, 5th-order Optimal 2x */
990 	zoz = z - (Z_ONE >> 1);
991 	zoe1 = z_coeff[1] + z_coeff[0];
992 	zoe2 = z_coeff[2] + z_coeff[-1];
993 	zoe3 = z_coeff[3] + z_coeff[-2];
994 	zoo1 = z_coeff[1] - z_coeff[0];
995 	zoo2 = z_coeff[2] - z_coeff[-1];
996 	zoo3 = z_coeff[3] - z_coeff[-2];
997 
998 	zoc0 = Z_RSHIFT((0x19edb6fdLL * zoe1) + (0x05ebd062LL * zoe2) +
999 	    (0x00267881LL * zoe3), 30);
1000 	zoc1 = Z_RSHIFT((0x1223af76LL * zoo1) + (0x0de3dd6bLL * zoo2) +
1001 	    (0x00d683cdLL * zoo3), 30);
1002 	zoc2 = Z_RSHIFT((-0x0c3ee068LL * zoe1) + (0x0a5c3769LL * zoe2) +
1003 	    (0x01e2aceaLL * zoe3), 30);
1004 	zoc3 = Z_RSHIFT((-0x0a8ab614LL * zoo1) + (-0x0019522eLL * zoo2) +
1005 	    (0x022cefc7LL * zoo3), 30);
1006 	zoc4 = Z_RSHIFT((0x0276187dLL * zoe1) + (-0x03a801e8LL * zoe2) +
1007 	    (0x0131d935LL * zoe3), 30);
1008 	zoc5 = Z_RSHIFT((0x02c373f5LL * zoo1) + (-0x01275f83LL * zoo2) +
1009 	    (0x0018ee79LL * zoo3), 30);
1010 
1011 	coeff = Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT((Z_RSHIFT(
1012 	    (int64_t)zoc5 * zoz, Z_SHIFT) +
1013 	    zoc4) * zoz, Z_SHIFT) + zoc3) * zoz, Z_SHIFT) +
1014 	    zoc2) * zoz, Z_SHIFT) + zoc1) * zoz, Z_SHIFT) + zoc0;
1015 #else
1016 #error "Interpolation type screwed!"
1017 #endif
1018 
1019 #if Z_POLYPHASE_COEFF_SHIFT > 0
1020 	coeff = Z_RSHIFT(coeff, Z_POLYPHASE_COEFF_SHIFT);
1021 #endif
1022 	return (coeff);
1023 }
1024 
1025 static int
1026 z_resampler_build_polyphase(struct z_info *info)
1027 {
1028 	int32_t alpha, c, i, z, idx;
1029 
1030 	/* Let this be here first. */
1031 	if (info->z_pcoeff != NULL) {
1032 		free(info->z_pcoeff, M_DEVBUF);
1033 		info->z_pcoeff = NULL;
1034 	}
1035 
1036 	if (feeder_rate_polyphase_max < 1)
1037 		return (ENOTSUP);
1038 
1039 	if (((int64_t)info->z_size * info->z_gy * 2) >
1040 	    feeder_rate_polyphase_max) {
1041 #ifndef _KERNEL
1042 		fprintf(stderr, "Polyphase entries exceed: [%d/%d] %jd > %d\n",
1043 		    info->z_gx, info->z_gy,
1044 		    (intmax_t)info->z_size * info->z_gy * 2,
1045 		    feeder_rate_polyphase_max);
1046 #endif
1047 		return (E2BIG);
1048 	}
1049 
1050 	info->z_pcoeff = malloc(sizeof(int32_t) *
1051 	    info->z_size * info->z_gy * 2, M_DEVBUF, M_NOWAIT | M_ZERO);
1052 	if (info->z_pcoeff == NULL)
1053 		return (ENOMEM);
1054 
1055 	for (alpha = 0; alpha < info->z_gy; alpha++) {
1056 		z = alpha * info->z_dx;
1057 		c = 0;
1058 		for (i = info->z_size; i != 0; i--) {
1059 			c += z >> Z_SHIFT;
1060 			z &= Z_MASK;
1061 			idx = (alpha * info->z_size * 2) +
1062 			    (info->z_size * 2) - i;
1063 			info->z_pcoeff[idx] =
1064 			    z_coeff_interpolate(z, info->z_coeff + c);
1065 			z += info->z_dy;
1066 		}
1067 		z = info->z_dy - (alpha * info->z_dx);
1068 		c = 0;
1069 		for (i = info->z_size; i != 0; i--) {
1070 			c += z >> Z_SHIFT;
1071 			z &= Z_MASK;
1072 			idx = (alpha * info->z_size * 2) + i - 1;
1073 			info->z_pcoeff[idx] =
1074 			    z_coeff_interpolate(z, info->z_coeff + c);
1075 			z += info->z_dy;
1076 		}
1077 	}
1078 
1079 #ifndef _KERNEL
1080 	fprintf(stderr, "Polyphase: [%d/%d] %d entries\n",
1081 	    info->z_gx, info->z_gy, info->z_size * info->z_gy * 2);
1082 #endif
1083 
1084 	return (0);
1085 }
1086 
1087 static int
1088 z_resampler_setup(struct pcm_feeder *f)
1089 {
1090 	struct z_info *info;
1091 	int64_t gy2gx_max, gx2gy_max;
1092 	uint32_t format;
1093 	int32_t align, i, z_scale;
1094 	int adaptive;
1095 
1096 	info = f->data;
1097 	z_resampler_reset(info);
1098 
1099 	if (info->src == info->dst)
1100 		return (0);
1101 
1102 	/* Shrink by greatest common divisor. */
1103 	i = z_gcd(info->src, info->dst);
1104 	info->z_gx = info->src / i;
1105 	info->z_gy = info->dst / i;
1106 
1107 	/* Too big, or too small. Bail out. */
1108 	if (!(Z_FACTOR_SAFE(info->z_gx) && Z_FACTOR_SAFE(info->z_gy)))
1109 		return (EINVAL);
1110 
1111 	format = f->desc->in;
1112 	adaptive = 0;
1113 	z_scale = 0;
1114 
1115 	/*
1116 	 * Setup everything: filter length, conversion factor, etc.
1117 	 */
1118 	if (Z_IS_SINC(info)) {
1119 		/*
1120 		 * Downsampling, or upsampling scaling factor. As long as the
1121 		 * factor can be represented by a fraction of 1 << Z_SHIFT,
1122 		 * we're pretty much in business. Scaling is not needed for
1123 		 * upsampling, so we just slap Z_ONE there.
1124 		 */
1125 		if (info->z_gx > info->z_gy)
1126 			/*
1127 			 * If the downsampling ratio is beyond sanity,
1128 			 * enable semi-adaptive mode. Although handling
1129 			 * extreme ratio is possible, the result of the
1130 			 * conversion is just pointless, unworthy,
1131 			 * nonsensical noises, etc.
1132 			 */
1133 			if ((info->z_gx / info->z_gy) > Z_SINC_DOWNMAX)
1134 				z_scale = Z_ONE / Z_SINC_DOWNMAX;
1135 			else
1136 				z_scale = ((uint64_t)info->z_gy << Z_SHIFT) /
1137 				    info->z_gx;
1138 		else
1139 			z_scale = Z_ONE;
1140 
1141 		/*
1142 		 * This is actually impossible, unless anything above
1143 		 * overflow.
1144 		 */
1145 		if (z_scale < 1)
1146 			return (E2BIG);
1147 
1148 		/*
1149 		 * Calculate sample time/coefficients index drift. It is
1150 		 * a constant for upsampling, but downsampling require
1151 		 * heavy duty filtering with possible too long filters.
1152 		 * If anything goes wrong, revisit again and enable
1153 		 * adaptive mode.
1154 		 */
1155 z_setup_adaptive_sinc:
1156 		if (info->z_pcoeff != NULL) {
1157 			free(info->z_pcoeff, M_DEVBUF);
1158 			info->z_pcoeff = NULL;
1159 		}
1160 
1161 		if (adaptive == 0) {
1162 			info->z_dy = z_scale << Z_DRIFT_SHIFT;
1163 			if (info->z_dy < 1)
1164 				return (E2BIG);
1165 			info->z_scale = z_scale;
1166 		} else {
1167 			info->z_dy = Z_FULL_ONE;
1168 			info->z_scale = Z_ONE;
1169 		}
1170 
1171 #if 0
1172 #define Z_SCALE_DIV	10000
1173 #define Z_SCALE_LIMIT(s, v)						\
1174 	((((uint64_t)(s) * (v)) + (Z_SCALE_DIV >> 1)) / Z_SCALE_DIV)
1175 
1176 		info->z_scale = Z_SCALE_LIMIT(info->z_scale, 9780);
1177 #endif
1178 
1179 		/* Smallest drift increment. */
1180 		info->z_dx = info->z_dy / info->z_gy;
1181 
1182 		/*
1183 		 * Overflow or underflow. Try adaptive, let it continue and
1184 		 * retry.
1185 		 */
1186 		if (info->z_dx < 1) {
1187 			if (adaptive == 0) {
1188 				adaptive = 1;
1189 				goto z_setup_adaptive_sinc;
1190 			}
1191 			return (E2BIG);
1192 		}
1193 
1194 		/*
1195 		 * Round back output drift.
1196 		 */
1197 		info->z_dy = info->z_dx * info->z_gy;
1198 
1199 		for (i = 0; i < Z_COEFF_TAB_SIZE; i++) {
1200 			if (Z_SINC_COEFF_IDX(info) != i)
1201 				continue;
1202 			/*
1203 			 * Calculate required filter length and guard
1204 			 * against possible abusive result. Note that
1205 			 * this represents only 1/2 of the entire filter
1206 			 * length.
1207 			 */
1208 			info->z_size = z_resampler_sinc_len(info);
1209 
1210 			/*
1211 			 * Multiple of 2 rounding, for better accumulator
1212 			 * performance.
1213 			 */
1214 			info->z_size &= ~1;
1215 
1216 			if (info->z_size < 2 || info->z_size > Z_SINC_MAX) {
1217 				if (adaptive == 0) {
1218 					adaptive = 1;
1219 					goto z_setup_adaptive_sinc;
1220 				}
1221 				return (E2BIG);
1222 			}
1223 			info->z_coeff = z_coeff_tab[i].coeff + Z_COEFF_OFFSET;
1224 			info->z_dcoeff = z_coeff_tab[i].dcoeff;
1225 			break;
1226 		}
1227 
1228 		if (info->z_coeff == NULL || info->z_dcoeff == NULL)
1229 			return (EINVAL);
1230 	} else if (Z_IS_LINEAR(info)) {
1231 		/*
1232 		 * Don't put much effort if we're doing linear interpolation.
1233 		 * Just center the interpolation distance within Z_LINEAR_ONE,
1234 		 * and be happy about it.
1235 		 */
1236 		info->z_dx = Z_LINEAR_FULL_ONE / info->z_gy;
1237 	}
1238 
1239 	/*
1240 	 * We're safe for now, lets continue.. Look for our resampler
1241 	 * depending on configured format and quality.
1242 	 */
1243 	for (i = 0; i < Z_RESAMPLER_TAB_SIZE; i++) {
1244 		int ridx;
1245 
1246 		if (AFMT_ENCODING(format) != z_resampler_tab[i].format)
1247 			continue;
1248 		if (Z_IS_SINC(info) && adaptive == 0 &&
1249 		    z_resampler_build_polyphase(info) == 0)
1250 			ridx = Z_RESAMPLER_SINC_POLYPHASE;
1251 		else
1252 			ridx = Z_RESAMPLER_IDX(info);
1253 		info->z_resample = z_resampler_tab[i].resampler[ridx];
1254 		break;
1255 	}
1256 
1257 	if (info->z_resample == NULL)
1258 		return (EINVAL);
1259 
1260 	info->bps = AFMT_BPS(format);
1261 	align = info->channels * info->bps;
1262 
1263 	/*
1264 	 * Calculate largest value that can be fed into z_gy2gx() and
1265 	 * z_gx2gy() without causing (signed) 32bit overflow. z_gy2gx() will
1266 	 * be called early during feeding process to determine how much input
1267 	 * samples that is required to generate requested output, while
1268 	 * z_gx2gy() will be called just before samples filtering /
1269 	 * accumulation process based on available samples that has been
1270 	 * calculated using z_gx2gy().
1271 	 *
1272 	 * Now that is damn confusing, I guess ;-) .
1273 	 */
1274 	gy2gx_max = (((uint64_t)info->z_gy * INT32_MAX) - info->z_gy + 1) /
1275 	    info->z_gx;
1276 
1277 	if ((gy2gx_max * align) > SND_FXDIV_MAX)
1278 		gy2gx_max = SND_FXDIV_MAX / align;
1279 
1280 	if (gy2gx_max < 1)
1281 		return (E2BIG);
1282 
1283 	gx2gy_max = (((uint64_t)info->z_gx * INT32_MAX) - info->z_gy) /
1284 	    info->z_gy;
1285 
1286 	if (gx2gy_max > INT32_MAX)
1287 		gx2gy_max = INT32_MAX;
1288 
1289 	if (gx2gy_max < 1)
1290 		return (E2BIG);
1291 
1292 	/*
1293 	 * Ensure that z_gy2gx() at its largest possible calculated value
1294 	 * (alpha = 0) will not cause overflow further late during z_gx2gy()
1295 	 * stage.
1296 	 */
1297 	if (z_gy2gx(info, gy2gx_max) > _Z_GCAST(gx2gy_max))
1298 		return (E2BIG);
1299 
1300 	info->z_maxfeed = gy2gx_max * align;
1301 
1302 #ifdef Z_USE_ALPHADRIFT
1303 	info->z_startdrift = z_gy2gx(info, 1);
1304 	info->z_alphadrift = z_drift(info, info->z_startdrift, 1);
1305 #endif
1306 
1307 	i = z_gy2gx(info, 1);
1308 	info->z_full = z_roundpow2((info->z_size << 1) + i);
1309 
1310 	/*
1311 	 * Too big to be true, and overflowing left and right like mad ..
1312 	 */
1313 	if ((info->z_full * align) < 1) {
1314 		if (adaptive == 0 && Z_IS_SINC(info)) {
1315 			adaptive = 1;
1316 			goto z_setup_adaptive_sinc;
1317 		}
1318 		return (E2BIG);
1319 	}
1320 
1321 	/*
1322 	 * Increase full buffer size if its too small to reduce cyclic
1323 	 * buffer shifting in main conversion/feeder loop.
1324 	 */
1325 	while (info->z_full < Z_RESERVOIR_MAX &&
1326 	    (info->z_full - (info->z_size << 1)) < Z_RESERVOIR)
1327 		info->z_full <<= 1;
1328 
1329 	/* Initialize buffer position. */
1330 	info->z_mask = info->z_full - 1;
1331 	info->z_start = z_prev(info, info->z_size << 1, 1);
1332 	info->z_pos = z_next(info, info->z_start, 1);
1333 
1334 	/*
1335 	 * Allocate or reuse delay line buffer, whichever makes sense.
1336 	 */
1337 	i = info->z_full * align;
1338 	if (i < 1)
1339 		return (E2BIG);
1340 
1341 	if (info->z_delay == NULL || info->z_alloc < i ||
1342 	    i <= (info->z_alloc >> 1)) {
1343 		if (info->z_delay != NULL)
1344 			free(info->z_delay, M_DEVBUF);
1345 		info->z_delay = malloc(i, M_DEVBUF, M_NOWAIT | M_ZERO);
1346 		if (info->z_delay == NULL)
1347 			return (ENOMEM);
1348 		info->z_alloc = i;
1349 	}
1350 
1351 	/*
1352 	 * Zero out head of buffer to avoid pops and clicks.
1353 	 */
1354 	memset(info->z_delay, sndbuf_zerodata(f->desc->out),
1355 	    info->z_pos * align);
1356 
1357 #ifdef Z_DIAGNOSTIC
1358 	/*
1359 	 * XXX Debuging mess !@#$%^
1360 	 */
1361 #define dumpz(x)	fprintf(stderr, "\t%12s = %10u : %-11d\n",	\
1362 			    "z_"__STRING(x), (uint32_t)info->z_##x,	\
1363 			    (int32_t)info->z_##x)
1364 	fprintf(stderr, "\n%s():\n", __func__);
1365 	fprintf(stderr, "\tchannels=%d, bps=%d, format=0x%08x, quality=%d\n",
1366 	    info->channels, info->bps, format, info->quality);
1367 	fprintf(stderr, "\t%d (%d) -> %d (%d), ",
1368 	    info->src, info->rsrc, info->dst, info->rdst);
1369 	fprintf(stderr, "[%d/%d]\n", info->z_gx, info->z_gy);
1370 	fprintf(stderr, "\tminreq=%d, ", z_gy2gx(info, 1));
1371 	if (adaptive != 0)
1372 		z_scale = Z_ONE;
1373 	fprintf(stderr, "factor=0x%08x/0x%08x (%f)\n",
1374 	    z_scale, Z_ONE, (double)z_scale / Z_ONE);
1375 	fprintf(stderr, "\tbase_length=%d, ", Z_SINC_BASE_LEN(info));
1376 	fprintf(stderr, "adaptive=%s\n", (adaptive != 0) ? "YES" : "NO");
1377 	dumpz(size);
1378 	dumpz(alloc);
1379 	if (info->z_alloc < 1024)
1380 		fprintf(stderr, "\t%15s%10d Bytes\n",
1381 		    "", info->z_alloc);
1382 	else if (info->z_alloc < (1024 << 10))
1383 		fprintf(stderr, "\t%15s%10d KBytes\n",
1384 		    "", info->z_alloc >> 10);
1385 	else if (info->z_alloc < (1024 << 20))
1386 		fprintf(stderr, "\t%15s%10d MBytes\n",
1387 		    "", info->z_alloc >> 20);
1388 	else
1389 		fprintf(stderr, "\t%15s%10d GBytes\n",
1390 		    "", info->z_alloc >> 30);
1391 	fprintf(stderr, "\t%12s   %10d (min output samples)\n",
1392 	    "",
1393 	    (int32_t)z_gx2gy(info, info->z_full - (info->z_size << 1)));
1394 	fprintf(stderr, "\t%12s   %10d (min allocated output samples)\n",
1395 	    "",
1396 	    (int32_t)z_gx2gy(info, (info->z_alloc / align) -
1397 	    (info->z_size << 1)));
1398 	fprintf(stderr, "\t%12s = %10d\n",
1399 	    "z_gy2gx()", (int32_t)z_gy2gx(info, 1));
1400 	fprintf(stderr, "\t%12s = %10d -> z_gy2gx() -> %d\n",
1401 	    "Max", (int32_t)gy2gx_max, (int32_t)z_gy2gx(info, gy2gx_max));
1402 	fprintf(stderr, "\t%12s = %10d\n",
1403 	    "z_gx2gy()", (int32_t)z_gx2gy(info, 1));
1404 	fprintf(stderr, "\t%12s = %10d -> z_gx2gy() -> %d\n",
1405 	    "Max", (int32_t)gx2gy_max, (int32_t)z_gx2gy(info, gx2gy_max));
1406 	dumpz(maxfeed);
1407 	dumpz(full);
1408 	dumpz(start);
1409 	dumpz(pos);
1410 	dumpz(scale);
1411 	fprintf(stderr, "\t%12s   %10f\n", "",
1412 	    (double)info->z_scale / Z_ONE);
1413 	dumpz(dx);
1414 	fprintf(stderr, "\t%12s   %10f\n", "",
1415 	    (double)info->z_dx / info->z_dy);
1416 	dumpz(dy);
1417 	fprintf(stderr, "\t%12s   %10d (drift step)\n", "",
1418 	    info->z_dy >> Z_SHIFT);
1419 	fprintf(stderr, "\t%12s   %10d (scaling differences)\n", "",
1420 	    (z_scale << Z_DRIFT_SHIFT) - info->z_dy);
1421 	fprintf(stderr, "\t%12s = %u bytes\n",
1422 	    "intpcm32_t", sizeof(intpcm32_t));
1423 	fprintf(stderr, "\t%12s = 0x%08x, smallest=%.16lf\n",
1424 	    "Z_ONE", Z_ONE, (double)1.0 / (double)Z_ONE);
1425 #endif
1426 
1427 	return (0);
1428 }
1429 
1430 static int
1431 z_resampler_set(struct pcm_feeder *f, int what, int32_t value)
1432 {
1433 	struct z_info *info;
1434 	int32_t oquality;
1435 
1436 	info = f->data;
1437 
1438 	switch (what) {
1439 	case Z_RATE_SRC:
1440 		if (value < feeder_rate_min || value > feeder_rate_max)
1441 			return (E2BIG);
1442 		if (value == info->rsrc)
1443 			return (0);
1444 		info->rsrc = value;
1445 		break;
1446 	case Z_RATE_DST:
1447 		if (value < feeder_rate_min || value > feeder_rate_max)
1448 			return (E2BIG);
1449 		if (value == info->rdst)
1450 			return (0);
1451 		info->rdst = value;
1452 		break;
1453 	case Z_RATE_QUALITY:
1454 		if (value < Z_QUALITY_MIN || value > Z_QUALITY_MAX)
1455 			return (EINVAL);
1456 		if (value == info->quality)
1457 			return (0);
1458 		/*
1459 		 * If we failed to set the requested quality, restore
1460 		 * the old one. We cannot afford leaving it broken since
1461 		 * passive feeder chains like vchans never reinitialize
1462 		 * itself.
1463 		 */
1464 		oquality = info->quality;
1465 		info->quality = value;
1466 		if (z_resampler_setup(f) == 0)
1467 			return (0);
1468 		info->quality = oquality;
1469 		break;
1470 	case Z_RATE_CHANNELS:
1471 		if (value < SND_CHN_MIN || value > SND_CHN_MAX)
1472 			return (EINVAL);
1473 		if (value == info->channels)
1474 			return (0);
1475 		info->channels = value;
1476 		break;
1477 	default:
1478 		return (EINVAL);
1479 		break;
1480 	}
1481 
1482 	return (z_resampler_setup(f));
1483 }
1484 
1485 static int
1486 z_resampler_get(struct pcm_feeder *f, int what)
1487 {
1488 	struct z_info *info;
1489 
1490 	info = f->data;
1491 
1492 	switch (what) {
1493 	case Z_RATE_SRC:
1494 		return (info->rsrc);
1495 		break;
1496 	case Z_RATE_DST:
1497 		return (info->rdst);
1498 		break;
1499 	case Z_RATE_QUALITY:
1500 		return (info->quality);
1501 		break;
1502 	case Z_RATE_CHANNELS:
1503 		return (info->channels);
1504 		break;
1505 	default:
1506 		break;
1507 	}
1508 
1509 	return (-1);
1510 }
1511 
1512 static int
1513 z_resampler_init(struct pcm_feeder *f)
1514 {
1515 	struct z_info *info;
1516 	int ret;
1517 
1518 	if (f->desc->in != f->desc->out)
1519 		return (EINVAL);
1520 
1521 	info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT | M_ZERO);
1522 	if (info == NULL)
1523 		return (ENOMEM);
1524 
1525 	info->rsrc = Z_RATE_DEFAULT;
1526 	info->rdst = Z_RATE_DEFAULT;
1527 	info->quality = feeder_rate_quality;
1528 	info->channels = AFMT_CHANNEL(f->desc->in);
1529 
1530 	f->data = info;
1531 
1532 	ret = z_resampler_setup(f);
1533 	if (ret != 0) {
1534 		if (info->z_pcoeff != NULL)
1535 			free(info->z_pcoeff, M_DEVBUF);
1536 		if (info->z_delay != NULL)
1537 			free(info->z_delay, M_DEVBUF);
1538 		free(info, M_DEVBUF);
1539 		f->data = NULL;
1540 	}
1541 
1542 	return (ret);
1543 }
1544 
1545 static int
1546 z_resampler_free(struct pcm_feeder *f)
1547 {
1548 	struct z_info *info;
1549 
1550 	info = f->data;
1551 	if (info != NULL) {
1552 		if (info->z_pcoeff != NULL)
1553 			free(info->z_pcoeff, M_DEVBUF);
1554 		if (info->z_delay != NULL)
1555 			free(info->z_delay, M_DEVBUF);
1556 		free(info, M_DEVBUF);
1557 	}
1558 
1559 	f->data = NULL;
1560 
1561 	return (0);
1562 }
1563 
1564 static uint32_t
1565 z_resampler_feed_internal(struct pcm_feeder *f, struct pcm_channel *c,
1566     uint8_t *b, uint32_t count, void *source)
1567 {
1568 	struct z_info *info;
1569 	int32_t alphadrift, startdrift, reqout, ocount, reqin, align;
1570 	int32_t fetch, fetched, start, cp;
1571 	uint8_t *dst;
1572 
1573 	info = f->data;
1574 	if (info->z_resample == NULL)
1575 		return (z_feed(f->source, c, b, count, source));
1576 
1577 	/*
1578 	 * Calculate sample size alignment and amount of sample output.
1579 	 * We will do everything in sample domain, but at the end we
1580 	 * will jump back to byte domain.
1581 	 */
1582 	align = info->channels * info->bps;
1583 	ocount = SND_FXDIV(count, align);
1584 	if (ocount == 0)
1585 		return (0);
1586 
1587 	/*
1588 	 * Calculate amount of input samples that is needed to generate
1589 	 * exact amount of output.
1590 	 */
1591 	reqin = z_gy2gx(info, ocount) - z_fetched(info);
1592 
1593 #ifdef Z_USE_ALPHADRIFT
1594 	startdrift = info->z_startdrift;
1595 	alphadrift = info->z_alphadrift;
1596 #else
1597 	startdrift = _Z_GY2GX(info, 0, 1);
1598 	alphadrift = z_drift(info, startdrift, 1);
1599 #endif
1600 
1601 	dst = b;
1602 
1603 	do {
1604 		if (reqin != 0) {
1605 			fetch = z_min(z_free(info), reqin);
1606 			if (fetch == 0) {
1607 				/*
1608 				 * No more free spaces, so wind enough
1609 				 * samples back to the head of delay line
1610 				 * in byte domain.
1611 				 */
1612 				fetched = z_fetched(info);
1613 				start = z_prev(info, info->z_start,
1614 				    (info->z_size << 1) - 1);
1615 				cp = (info->z_size << 1) + fetched;
1616 				z_copy(info->z_delay + (start * align),
1617 				    info->z_delay, cp * align);
1618 				info->z_start =
1619 				    z_prev(info, info->z_size << 1, 1);
1620 				info->z_pos =
1621 				    z_next(info, info->z_start, fetched + 1);
1622 				fetch = z_min(z_free(info), reqin);
1623 #ifdef Z_DIAGNOSTIC
1624 				if (1) {
1625 					static uint32_t kk = 0;
1626 					fprintf(stderr,
1627 					    "Buffer Move: "
1628 					    "start=%d fetched=%d cp=%d "
1629 					    "cycle=%u [%u]\r",
1630 					    start, fetched, cp, info->z_cycle,
1631 					    ++kk);
1632 				}
1633 				info->z_cycle = 0;
1634 #endif
1635 			}
1636 			if (fetch != 0) {
1637 				/*
1638 				 * Fetch in byte domain and jump back
1639 				 * to sample domain.
1640 				 */
1641 				fetched = SND_FXDIV(z_feed(f->source, c,
1642 				    info->z_delay + (info->z_pos * align),
1643 				    fetch * align, source), align);
1644 				/*
1645 				 * Prepare to convert fetched buffer,
1646 				 * or mark us done if we cannot fulfill
1647 				 * the request.
1648 				 */
1649 				reqin -= fetched;
1650 				info->z_pos += fetched;
1651 				if (fetched != fetch)
1652 					reqin = 0;
1653 			}
1654 		}
1655 
1656 		reqout = z_min(z_gx2gy(info, z_fetched(info)), ocount);
1657 		if (reqout != 0) {
1658 			ocount -= reqout;
1659 
1660 			/*
1661 			 * Drift.. drift.. drift..
1662 			 *
1663 			 * Notice that there are 2 methods of doing the drift
1664 			 * operations: The former is much cleaner (in a sense
1665 			 * of mathematical readings of my eyes), but slower
1666 			 * due to integer division in z_gy2gx(). Nevertheless,
1667 			 * both should give the same exact accurate drifting
1668 			 * results, so the later is favourable.
1669 			 */
1670 			do {
1671 				info->z_resample(info, dst);
1672 #if 0
1673 				startdrift = z_gy2gx(info, 1);
1674 				alphadrift = z_drift(info, startdrift, 1);
1675 				info->z_start += startdrift;
1676 				info->z_alpha += alphadrift;
1677 #else
1678 				info->z_alpha += alphadrift;
1679 				if (info->z_alpha < info->z_gy)
1680 					info->z_start += startdrift;
1681 				else {
1682 					info->z_start += startdrift - 1;
1683 					info->z_alpha -= info->z_gy;
1684 				}
1685 #endif
1686 				dst += align;
1687 #ifdef Z_DIAGNOSTIC
1688 				info->z_cycle++;
1689 #endif
1690 			} while (--reqout != 0);
1691 		}
1692 	} while (reqin != 0 && ocount != 0);
1693 
1694 	/*
1695 	 * Back to byte domain..
1696 	 */
1697 	return (dst - b);
1698 }
1699 
1700 static int
1701 z_resampler_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
1702     uint32_t count, void *source)
1703 {
1704 	uint32_t feed, maxfeed, left;
1705 
1706 	/*
1707 	 * Split count to smaller chunks to avoid possible 32bit overflow.
1708 	 */
1709 	maxfeed = ((struct z_info *)(f->data))->z_maxfeed;
1710 	left = count;
1711 
1712 	do {
1713 		feed = z_resampler_feed_internal(f, c, b,
1714 		    z_min(maxfeed, left), source);
1715 		b += feed;
1716 		left -= feed;
1717 	} while (left != 0 && feed != 0);
1718 
1719 	return (count - left);
1720 }
1721 
1722 static struct pcm_feederdesc feeder_rate_desc[] = {
1723 	{ FEEDER_RATE, 0, 0, 0, 0 },
1724 	{ 0, 0, 0, 0, 0 },
1725 };
1726 
1727 static kobj_method_t feeder_rate_methods[] = {
1728 	KOBJMETHOD(feeder_init,		z_resampler_init),
1729 	KOBJMETHOD(feeder_free,		z_resampler_free),
1730 	KOBJMETHOD(feeder_set,		z_resampler_set),
1731 	KOBJMETHOD(feeder_get,		z_resampler_get),
1732 	KOBJMETHOD(feeder_feed,		z_resampler_feed),
1733 	KOBJMETHOD_END
1734 };
1735 
1736 FEEDER_DECLARE(feeder_rate, NULL);
1737