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