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