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