/*-
 * Copyright (c) 2005-2007 Ariff Abdullah <ariff@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * XXX Rewritten for the great lulz and justice.
 *
 *     Z Resampler, which means any effort to create future replacement
 *     for this resampler are simply absurd unless the world decide
 *     to add new alphabet after Z.
 */

/*
 * This is an entirely new bandlimited sinc interpolator, technically
 * based on excellent research paper by Julius O. Smith III
 * - http://ccrma.stanford.edu/~jos/resample/ .
 */

#ifdef _KERNEL
#include <dev/sound/pcm/sound.h>
#include "feeder_if.h"

SND_DECLARE_FILE("$FreeBSD: src/sys/dev/sound/pcm/feeder_rate.c,v 1.23 2007/06/16 03:37:28 ariff Exp $");
#endif  /* _KERNEL */

#ifdef _KERNEL
#include <dev/sound/pcm/pcm.h>
#include <dev/sound/pcm/zcoeff.h>
#else
#include "pcm.h"
#ifdef USE_ZCOEFF_GEN
#include "zcoeff_gen.h"
#else
#include "zcoeff.h"
#endif
#define FEEDRATE_SRC            1
#define FEEDRATE_DST            2
#define FEEDRATE_RATEMIN        1
#define FEEDRATE_RATEMAX        2016000
#define FEEDRATE_ROUNDHZ        0
#endif

#ifndef AFMT_S8_NE
#define AFMT_S8_NE      AFMT_S8
#endif
#ifndef AFMT_U8_NE
#define AFMT_U8_NE      AFMT_U8
#endif

#ifndef FEEDRATE_QUALITY
#define FEEDRATE_QUALITY        (FEEDRATE_DST + 1)
#endif

#ifndef Z_QUALITY_DEFAULT
#define Z_QUALITY_DEFAULT       Z_QUALITY_LINEAR
#endif

#define Z_RESERVOIR             8192
#define Z_RESERVOIR_MAX         131072

#define Z_SINC_MAX              0x3fffff

#define Z_RATE_DEFAULT          48000

#define Z_RATE_MIN              FEEDRATE_RATEMIN
#define Z_RATE_MAX              FEEDRATE_RATEMAX
#define Z_ROUNDHZ               FEEDRATE_ROUNDHZ
#define Z_ROUNDHZ_MIN           FEEDRATE_ROUNDHZ_MIN
#define Z_ROUNDHZ_MAX           FEEDRATE_ROUNDHZ_MAX

#define Z_RATE_SRC              FEEDRATE_SRC
#define Z_RATE_DST              FEEDRATE_DST
#define Z_RATE_QUALITY          FEEDRATE_QUALITY

#define Z_PARANOID              1

/*
 * Don't overflow 32bit integer, since everything is done
 * within 32bit arithmetic.
 */
#define Z_FACTOR_MIN            1
#define Z_FACTOR_MAX            Z_MASK
#define Z_FACTOR_SAFE(v)        (!((v) < Z_FACTOR_MIN || (v) > Z_FACTOR_MAX))

struct z_info;

typedef void (*z_resampler_t)(struct z_info *, uint8_t *);

struct z_info {
        int32_t rsrc, rdst;    /* original source / destination rates */
        int32_t src, dst;      /* rounded source / destination rates */
        int32_t channels;      /* total channels */
        int32_t bps;           /* bytes-per-sample */
        int32_t quality;       /* resampling quality */

        int32_t z_gx, z_gy;    /* interpolation / decimation ratio */
        int32_t z_alpha;       /* input / output sample time drift */
        uint8_t *z_delay;      /* FIR delay line / linear buffer */
        int32_t *z_coeff;      /* FIR coefficients */
        int32_t *z_dcoeff;     /* FIR coefficients differences */
        int32_t z_scale;       /* output scaling */
        int32_t z_dx;          /* input sample drift increment */
        int32_t z_dy;          /* output sample drift increment */
        int32_t z_mask;                /* delay line full length mask */
        int32_t z_size;                /* half width of FIR taps */
        int32_t z_full;                /* full size of delay line */
        int32_t z_full_alloc;  /* largest allocated full size of delay line */
        int32_t z_start;       /* buffer processing start position */
        int32_t z_pos;         /* current position for the next feed */
#ifndef _KERNEL
        uint32_t z_cycle;      /* output cycle, purely for statistical */
#endif

        z_resampler_t z_resample;
};

int feeder_rate_min = Z_RATE_MIN;
int feeder_rate_max = Z_RATE_MAX;
int feeder_rate_round = Z_ROUNDHZ;
static int feeder_rate_quality = Z_QUALITY_DEFAULT;

#ifdef _KERNEL
TUNABLE_INT("hw.snd.feeder_rate_min", &feeder_rate_min);
TUNABLE_INT("hw.snd.feeder_rate_max", &feeder_rate_max);
TUNABLE_INT("hw.snd.feeder_rate_round", &feeder_rate_round);
TUNABLE_INT("hw.snd.feeder_rate_quality", &feeder_rate_quality);

static int
sysctl_hw_snd_feeder_rate_min(SYSCTL_HANDLER_ARGS)
{
        int err, val;

        val = feeder_rate_min;
        err = sysctl_handle_int(oidp, &val, 0, req);

        if (err != 0 || req->newptr == NULL || val == feeder_rate_min)
                return (err);

        if (!(Z_FACTOR_SAFE(val) && val < feeder_rate_max))
                return (EINVAL);

        feeder_rate_min = val;

        return (0);
}
SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_min, CTLTYPE_INT | CTLFLAG_RW,
    0, sizeof(int), sysctl_hw_snd_feeder_rate_min, "I",
    "minimum allowable rate");

static int
sysctl_hw_snd_feeder_rate_max(SYSCTL_HANDLER_ARGS)
{
        int err, val;

        val = feeder_rate_max;
        err = sysctl_handle_int(oidp, &val, 0, req);

        if (err != 0 || req->newptr == NULL || val == feeder_rate_max)
                return (err);

        if (!(Z_FACTOR_SAFE(val) && val > feeder_rate_min))
                return (EINVAL);

        feeder_rate_max = val;

        return (0);
}
SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_max, CTLTYPE_INT | CTLFLAG_RW,
    0, sizeof(int), sysctl_hw_snd_feeder_rate_max, "I",
    "maximum allowable rate");

static int
sysctl_hw_snd_feeder_rate_round(SYSCTL_HANDLER_ARGS)
{
        int err, val;

        val = feeder_rate_round;
        err = sysctl_handle_int(oidp, &val, 0, req);

        if (err != 0 || req->newptr == NULL || val == feeder_rate_round)
                return (err);

        if (val < Z_ROUNDHZ_MIN || val > Z_ROUNDHZ_MAX)
                return (EINVAL);

        feeder_rate_round = val - (val % Z_ROUNDHZ);

        return (0);
}
SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_round, CTLTYPE_INT | CTLFLAG_RW,
    0, sizeof(int), sysctl_hw_snd_feeder_rate_round, "I",
    "sample rate converter rounding threshold");

static int
sysctl_hw_snd_feeder_rate_quality(SYSCTL_HANDLER_ARGS)
{
        struct snddev_info *d;
        struct pcm_channel *c;
        struct pcm_feeder *f;
        int i, err, val;

        val = feeder_rate_quality;
        err = sysctl_handle_int(oidp, &val, 0, req);

        if (err != 0 || req->newptr == NULL || val == feeder_rate_quality)
                return (err);

        if (val < Z_QUALITY_MIN || val > Z_QUALITY_MAX)
                return (EINVAL);

        feeder_rate_quality = val;

        /*
         * Traverse all available channels on each device and try to
         * set resampler quality if and only if it is exist as
         * part of feeder chains and the channel is idle.
         */
        for (i = 0; pcm_devclass != NULL &&
            i < devclass_get_maxunit(pcm_devclass); i++) {
                d = devclass_get_softc(pcm_devclass, i);
                if (!PCM_REGISTERED(d))
                        continue;
                pcm_lock(d);
                PCM_WAIT(d);
                PCM_ACQUIRE(d);
                CHN_FOREACH(c, d, channels.pcm) {
                        CHN_LOCK(c);
                        f = chn_findfeeder(c, FEEDER_RATE);
                        if (f == NULL || f->data == NULL || CHN_STARTED(c)) {
                                CHN_UNLOCK(c);
                                continue;
                        }
                        FEEDER_SET(f, FEEDRATE_QUALITY, val);
                        CHN_UNLOCK(c);
                }
                PCM_RELEASE(d);
                pcm_unlock(d);
        }

        return (0);
}
SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_quality, CTLTYPE_INT | CTLFLAG_RW,
    0, sizeof(int), sysctl_hw_snd_feeder_rate_quality, "I",
    "sample rate converter quality ("__XSTRING(Z_QUALITY_MIN)"=low .. "
    __XSTRING(Z_QUALITY_MAX)"=high)");
#endif /* _KERNEL */


/*
 * Macroses for accurate sample time drift calculations.
 */
#define _Z_GY2GX(i, a, v)                                               \
        ((((i)->z_gx * (v)) + (i)->z_gy - (a) - 1) / (i)->z_gy)

#define _Z_GX2GY(i, a, v)                                               \
        (((a) + ((v) * (i)->z_gy)) / (i)->z_gx)

#define z_gy2gx(i, v)           _Z_GY2GX(i, (i)->z_alpha, v)
#define z_gx2gy(i, v)           _Z_GX2GY(i, (i)->z_alpha, v)
#define z_drift(i, x, y)        (((x) * (i)->z_gy) - ((y) * (i)->z_gx))

/*
 * Macroses for SINC coefficients table manipulations.. whatever.
 */
#define Z_SINC_COEFF_IDX(i)                                                     \
        ((i)->quality - Z_QUALITY_LINEAR - 1)

#define Z_SINC_LEN(i)                                                   \
        ((int32_t)(((uint64_t)z_coeff_tab[Z_SINC_COEFF_IDX(i)].len <<  \
            Z_SHIFT) / (i)->z_dy))

#define Z_SINC_BASE_LEN(i)      \
        ((z_coeff_tab[Z_SINC_COEFF_IDX(i)].len - 1) >> (Z_DRIFT_SHIFT - 1))

#define Z_IS_SINC(i)            (Z_SINC_COEFF_IDX(i) >= 0)

/*
 * Macroses for linear delay buffer operations. Alignment is not
 * really necessary since we're not using true circular buffer, but it
 * will help us guard against possible trespasser. To be honest,
 * the linear block operations does not need guarding at all due to
 * accurate drifting!
 */
#define z_align(i, v)           ((v) & (i)->z_mask)
#define z_next(i, o, v)         z_align(i, (o) + (v))
#define z_prev(i, o, v)         z_align(i, (o) - (v))
#define z_fetched(i)            (z_align(i, (i)->z_pos - (i)->z_start) - 1)
#define z_free(i)               ((i)->z_full - (i)->z_pos)

/*
 * Macroses for Bla Bla .. :)
 */
#define z_copy(src, dst, sz)    memcpy(dst, src, sz)
#define z_feed                  FEEDER_FEED


static int32_t
z_gcd(int32_t x, int32_t y)
{
        while (x != y) {
                if (x > y)
                        x -= y;
                else
                        y -= x;
        }

        return (x);
}

static int32_t
z_roundpow2(int32_t v)
{
        int32_t i;

        i = 1;

        while (i > 0 && i < v)
                i <<= 1;

        return (i);
}

/*
 * Zero Order Hold, the worst of the worst, an insult against quality,
 * but super fast.
 */
static void
z_feed_zoh(struct z_info *info, uint8_t *dst)
{
        z_copy(info->z_delay +
            (info->z_start * info->channels * info->bps), dst,
            info->channels * info->bps);
}

/*
 * (Classic) Linear Interpolation. This at least sounds better (perceptually)
 * and fast, but no further advance filtering which means aliasing still exist
 * and could become worst with a right sample.  Interpolation centered around
 * 8bit distance between the present and previous sample, so there are chances
 * to shift 8 and 16 bit samples to 24 bit to gain extra dynamic range during
 * interpolation while retaining 32bit arithmetic.
 */
#define Z_DECLARE_LINEAR(SIGN, BIT, ENDIAN, SHIFT)                              \
static void                                                                     \
z_feed_linear_##SIGN##BIT##ENDIAN(struct z_info *info, uint8_t *dst)            \
{                                                                               \
        uint32_t dx, dy;                                                       \
        intpcm_t x, y;                                                          \
        int32_t ch;                                                            \
        uint8_t *sx, *sy;                                                      \
                                                                                \
        dy = ((uint32_t)info->z_alpha << Z_LINEAR_SHIFT) / info->z_gy;         \
        dx = Z_LINEAR_ONE - dy;                                                 \
                                                                                \
        sx = info->z_delay + (info->z_start * info->channels *                  \
            PCM_##BIT##_BPS);                                                   \
        sy = info->z_delay +                                                    \
            (z_prev(info, info->z_start, 1) * info->channels *                 \
            PCM_##BIT##_BPS);                                                   \
                                                                                \
        ch = info->channels;                                                    \
                                                                                \
        do {                                                                   \
                x = PCM_READ_##SIGN##BIT##_##ENDIAN(sx) << SHIFT;               \
                y = PCM_READ_##SIGN##BIT##_##ENDIAN(sy) << SHIFT;               \
                x = ((INTPCM##BIT##_T(x) * dx) +                                \
                    (INTPCM##BIT##_T(y) * dy)) >> Z_LINEAR_SHIFT;               \
                PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, x >> SHIFT);              \
                sx += PCM_##BIT##_BPS;                                          \
                sy += PCM_##BIT##_BPS;                                          \
                dst += PCM_##BIT##_BPS;                                         \
        } while (--ch != 0);                                                   \
}

#define Z_PCM_NORMALIZE(v, i, SHIFT)                                    \
        v <<= SHIFT;                                                    \
        v >>= Z_COEFF_SHIFT;                                            \
        if ((i)->z_scale != Z_ONE)                                     \
                v = ((v) * (i)->z_scale) >> Z_SHIFT;                    \
        v >>= SHIFT
#if 0
#define Z_PCM_NORMALIZE(v, i, SHIFT)                                    \
        v = ((i)->z_scale == Z_ONE) ?                                   \
            ((v) >> Z_COEFF_SHIFT) :                                    \
            (((((v) << (SHIFT)) >> Z_COEFF_SHIFT) *                     \
            (i)->z_scale) >> (Z_SHIFT + (SHIFT)))
#define Z_PCM_NORMALIZE(v, i, SHIFT)                                    \
        if ((i)->z_scale == Z_ONE)                                      \
                v >>= Z_COEFF_SHIFT;                                    \
        else                                                            \
                v = ((((v) << (SHIFT)) >> Z_COEFF_SHIFT) *              \
                    (i)->z_scale) >> (Z_SHIFT + (SHIFT))
#endif

/*
 * Sine Cardinal (SINC) Interpolation. Scaling is done in 64 bit, so
 * there's no point to hold the plate any longer. All samples will be
 * shifted to a full 32 bit and restore during write for maximum dynamic
 * range during accumulation.
 */
#define Z_DECLARE_SINC(SIGN, BIT, ENDIAN, SHIFT)                                \
static void                                                                     \
z_feed_sinc_##SIGN##BIT##ENDIAN(struct z_info *info, uint8_t *dst)              \
{                                                                               \
        int64_t v;                                                             \
        intpcm_t x;                                                             \
        int32_t c, z, z1, z_dy, coeff, *z_coeff, *z_dcoeff;                    \
        int32_t i, center, ch;                                                 \
        uint8_t *p;                                                            \
                                                                                \
        center = z_prev(info, info->z_start, info->z_size);                     \
        z_coeff = info->z_coeff;                                                \
        z_dcoeff = info->z_dcoeff;                                              \
        z_dy = info->z_dy;                                                      \
        z1 = info->z_alpha * info->z_dx;                                        \
        ch = info->channels - 1;                                               \
        dst += ch * PCM_##BIT##_BPS;                                            \
                                                                                \
        do {                                                                   \
                v = 0;                                                                \
                z = z1;                                                         \
                c = 0;                                                                \
                p = info->z_delay + (z_next(info, center, 1) *                        \
                    info->channels * PCM_##BIT##_BPS) + (ch * PCM_##BIT##_BPS); \
                i = info->z_size;                                               \
                do {                                                          \
                        c += z >> Z_SHIFT;                                      \
                        z &= Z_MASK;                                            \
                        coeff = z_coeff[c] + (((z >> Z_COEFF_UNSHIFT) *         \
                            z_dcoeff[c]) >> Z_COEFF_SHIFT);                     \
                        x = _PCM_READ_##SIGN##BIT##_##ENDIAN(p);                \
                        v += (int64_t)x * coeff;                             \
                        z += z_dy;                                              \
                        p += info->channels * PCM_##BIT##_BPS;                  \
                } while (--i != 0);                                           \
                z = z_dy - z1;                                                  \
                c = 0;                                                                \
                p = info->z_delay + (center * info->channels *                  \
                    PCM_##BIT##_BPS) + (ch * PCM_##BIT##_BPS);                  \
                i = info->z_size;                                               \
                do {                                                          \
                        c += z >> Z_SHIFT;                                      \
                        z &= Z_MASK;                                            \
                        coeff = z_coeff[c] + (((z >> Z_COEFF_UNSHIFT) *         \
                            z_dcoeff[c]) >> Z_COEFF_SHIFT);                     \
                        x = _PCM_READ_##SIGN##BIT##_##ENDIAN(p);                \
                        v += (int64_t)x * coeff;                             \
                        z += z_dy;                                              \
                        p -= info->channels * PCM_##BIT##_BPS;                  \
                } while (--i != 0);                                           \
                Z_PCM_NORMALIZE(v, info, SHIFT);                                \
                if (v > PCM_S##BIT##_MAX)                                     \
                        v = PCM_S##BIT##_MAX;                                   \
                else if (v < PCM_S##BIT##_MIN)                                        \
                        v = PCM_S##BIT##_MIN;                                   \
                _PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, v);                      \
                dst -= PCM_##BIT##_BPS;                                         \
        } while (ch-- != 0);                                                   \
}

#define Z_DECLARE(SIGN, BIT, ENDIAN, L_SHIFT, S_SHIFT)                  \
        Z_DECLARE_LINEAR(SIGN, BIT, ENDIAN, L_SHIFT)                    \
        Z_DECLARE_SINC(SIGN, BIT, ENDIAN, S_SHIFT)

Z_DECLARE(S,  8, NE, 16, 24)
Z_DECLARE(S, 16, LE,  8, 16)
Z_DECLARE(S, 24, LE,  0,  8)
Z_DECLARE(S, 32, LE,  0,  0)
Z_DECLARE(S, 16, BE,  8, 16)
Z_DECLARE(S, 24, BE,  0,  8)
Z_DECLARE(S, 32, BE,  0,  0)
Z_DECLARE(U,  8, NE, 16, 24)
Z_DECLARE(U, 16, LE,  8, 16)
Z_DECLARE(U, 24, LE,  0,  8)
Z_DECLARE(U, 32, LE,  0,  0)
Z_DECLARE(U, 16, BE,  8, 16)
Z_DECLARE(U, 24, BE,  0,  8)
Z_DECLARE(U, 32, BE,  0,  0)

enum {
        Z_RESAMPLER_ZOH,
        Z_RESAMPLER_LINEAR,
        Z_RESAMPLER_SINC,
        Z_RESAMPLER_LAST
};

#define Z_RESAMPLER_IDX(i)                                              \
        (Z_IS_SINC(i) ? Z_RESAMPLER_SINC : (i)->quality)

#define Z_RESAMPLER_ENTRY(SIGN, BIT, ENDIAN)                                    \
        {                                                                       \
            AFMT_##SIGN##BIT##_##ENDIAN, PCM_##BIT##_BPS,                       \
            {                                                                   \
                [Z_RESAMPLER_ZOH]    = z_feed_zoh,                              \
                [Z_RESAMPLER_LINEAR] = z_feed_linear_##SIGN##BIT##ENDIAN,       \
                [Z_RESAMPLER_SINC]   = z_feed_sinc_##SIGN##BIT##ENDIAN          \
            }                                                                   \
        }

static const struct {
        uint32_t format;
        int32_t bps;
        z_resampler_t resampler[Z_RESAMPLER_LAST];
} z_resampler_tab[] = {
        Z_RESAMPLER_ENTRY(S,  8, NE),
        Z_RESAMPLER_ENTRY(S, 16, LE),
        Z_RESAMPLER_ENTRY(S, 24, LE),
        Z_RESAMPLER_ENTRY(S, 32, LE),
        Z_RESAMPLER_ENTRY(S, 16, BE),
        Z_RESAMPLER_ENTRY(S, 24, BE),
        Z_RESAMPLER_ENTRY(S, 32, BE),
        Z_RESAMPLER_ENTRY(U,  8, NE),
        Z_RESAMPLER_ENTRY(U, 16, LE),
        Z_RESAMPLER_ENTRY(U, 24, LE),
        Z_RESAMPLER_ENTRY(U, 32, LE),
        Z_RESAMPLER_ENTRY(U, 16, BE),
        Z_RESAMPLER_ENTRY(U, 24, BE),
        Z_RESAMPLER_ENTRY(U, 32, BE),
};

#define Z_RESAMPLER_TAB_SIZE                                            \
        ((int32_t)(sizeof(z_resampler_tab) / sizeof(z_resampler_tab[0])))

static void
z_resampler_reset(struct z_info *info)
{
        info->src = info->rsrc - (info->rsrc % ((feeder_rate_round > 0 &&
            info->rsrc > feeder_rate_round) ? feeder_rate_round : 1));
        info->dst = info->rdst - (info->rdst % ((feeder_rate_round > 0 &&
            info->rdst > feeder_rate_round) ? feeder_rate_round : 1));
        info->z_gx = 1;
        info->z_gy = 1;
        info->z_alpha = 0;
        info->z_resample = NULL;
        info->z_size = 1;
        info->z_coeff = NULL;
        info->z_dcoeff = NULL;
        info->z_scale = -1;
        info->z_dx = -1;
        info->z_dy = -1;
#ifndef _KERNEL
        info->z_cycle = 0;
#endif
        if (info->quality < Z_QUALITY_MIN)
                info->quality = Z_QUALITY_MIN;
        else if (info->quality > Z_QUALITY_MAX)
                info->quality = Z_QUALITY_MAX;
}

#ifdef Z_PARANOID
static int32_t
z_resampler_sinc_len(struct z_info *info)
{
        int32_t c, z, len, lmax;

        if (!Z_IS_SINC(info))
                return (1);

        c = 0;
        z = info->z_dy;
        len = 0;
        lmax = z_coeff_tab[Z_SINC_COEFF_IDX(info)].len;
        do {
                c += z >> Z_SHIFT;
                z &= Z_MASK;
                z += info->z_dy;
        } while (c < lmax && ++len > 0);

        if (len != Z_SINC_LEN(info)) {
#ifdef _KERNEL
                printf("%s(): sinc l=%d != Z_SINC_LEN=%d\n",
                    __func__, len, Z_SINC_LEN(info));
#else
                fprintf(stderr, "sinc l=%d != Z_SINC_LEN=%d\n",
                    len, Z_SINC_LEN(info));
                return (-1);
#endif
        }

        return (len);
}
#else
#define z_resampler_sinc_len(i)         (Z_IS_SINC(i) ? Z_SINC_LEN(i) : 1)
#endif

static int
z_resampler_setup(struct pcm_feeder *f)
{
        struct z_info *info;
        uint64_t z_fact;
        uint32_t format;
        int32_t i;
        int adaptive;

        info = f->data;
        z_resampler_reset(info);

        if (info->src == info->dst)
                return (0);

        /* Shrink by greatest common divisor. */
        i = z_gcd(info->src, info->dst);
        info->z_gx = info->src / i;
        info->z_gy = info->dst / i;

        /* Too big, or too small. Bail out. */
        if (!(Z_FACTOR_SAFE(info->z_gx) && Z_FACTOR_SAFE(info->z_gy)))
                return (EINVAL);

        format = f->desc->out;

        z_fact = 0;
        adaptive = 0;

        if (Z_IS_SINC(info)) {
                z_fact = ((uint64_t)info->z_gy << Z_SHIFT) / info->z_gx;
                /*
                 * This is actually impossible, unless Z_SHIFT is too
                 * big.
                 */
                if (z_fact == 0)
                        return (E2BIG);

                /*
                 * Calculate sample time/coefficients index drift. It is
                 * a constant for upsampling, but downsampling require
                 * heavy duty filtering with possible too long filters.
                 * If anything goes wrong, revisit again and enable
                 * adaptive mode.
                 */
z_setup_adaptive_sinc:
                if (adaptive == 0 && z_fact < Z_ONE) {
                        info->z_dy = ((z_fact << Z_FULL_SHIFT) +
                            (Z_ONE >> 1)) >> Z_SHIFT;
                        if (info->z_dy < 1)
                                return (E2BIG);
                        info->z_scale = z_fact;
                } else {
                        info->z_dy = Z_FULL_ONE;
                        info->z_scale = Z_ONE;
                }

                /* Smallest drift increment */
                info->z_dx = info->z_dy / info->z_gy;

                /*
                 * Another ridiculous attempt that bork on us, but since
                 * we're being made adaptive, let it continue and retry.
                 */
                if (info->z_dx < 1) {
                        if (adaptive == 0) {
                                adaptive = 1;
                                goto z_setup_adaptive_sinc;
                        }
                        return (E2BIG);
                }

                for (i = 0; i < Z_COEFF_TAB_SIZE; i++) {
                        if (Z_SINC_COEFF_IDX(info) != i)
                                continue;
                        /*
                         * Calculate required filter length and guard
                         * against possible abusive result. Note that
                         * this represents only 1/2 of the entire filter
                         * length.
                         */
                        info->z_size = z_resampler_sinc_len(info);
                        if (info->z_size < 1 || info->z_size > Z_SINC_MAX) {
                                if (adaptive == 0) {
                                        adaptive = 1;
                                        goto z_setup_adaptive_sinc;
                                }
                                return (E2BIG);
                        }
                        info->z_coeff = z_coeff_tab[i].coeff;
                        info->z_dcoeff = z_coeff_tab[i].dcoeff;
                        break;
                }
        }

        /*
         * We're safe for now, lets continue.. Look for our resampler
         * depending on configured format and quality.
         */
        for (i = 0; i < Z_RESAMPLER_TAB_SIZE; i++) {
                if ((format & ~AFMT_STEREO) != z_resampler_tab[i].format)
                        continue;
                info->bps = z_resampler_tab[i].bps;
                info->z_resample =
                    z_resampler_tab[i].resampler[Z_RESAMPLER_IDX(info)];
                break;
        }

        if (info->z_resample == NULL)
                return (EINVAL);

        info->channels = (format & AFMT_STEREO) ? 2 : 1;

        i = z_gy2gx(info, 1);
        info->z_full = z_roundpow2((info->z_size << 1) + i);

        /*
         * Too big to be true, and overflowing left and right like mad ..
         */
        if ((info->z_full * info->channels * info->bps) < 1) {
                if (adaptive == 0 && Z_IS_SINC(info)) {
                        adaptive = 1;
                        goto z_setup_adaptive_sinc;
                }
                return (E2BIG);
        }

        /*
         * Increase full buffer size if its too small to reduce cyclic
         * shifting in main conversion/feeder loop.
         */
        while (info->z_full < Z_RESERVOIR_MAX &&
            (info->z_full - (info->z_size << 1)) < Z_RESERVOIR)
                info->z_full <<= 1;

        /* Initialize buffer position. */
        info->z_mask = info->z_full - 1;
        info->z_start = z_prev(info, info->z_size << 1, 1);
        info->z_pos = z_next(info, info->z_start, 1);

#ifndef _KERNEL
#define dumpz(x)        fprintf(stderr, "\t%12s = %10u : %-11d\n",     \
                            "z_"__STRING(x), (uint32_t)info->z_##x,  \
                            (int32_t)info->z_##x)
        fprintf(stderr, "\n%s():\n", __func__);
        fprintf(stderr, "\tchannels=%d, bps=%d, format=0x%08x, quality=%d\n",
            info->channels, info->bps, format, info->quality);
        fprintf(stderr, "\t%d (%d) -> %d (%d), ",
            info->src, info->rsrc, info->dst, info->rdst);
        fprintf(stderr, "[%d/%d]\n", info->z_gx, info->z_gy);
        fprintf(stderr, "\tminreq=%d, ", z_gy2gx(info, 1));
        fprintf(stderr, "factor=%llu, ", z_fact);
        fprintf(stderr, "base_length=%d, ", Z_SINC_BASE_LEN(info));
        fprintf(stderr, "adaptive=%s\n", (adaptive != 0) ? "YES" : "NO");
        dumpz(size);
        dumpz(full_alloc);
        dumpz(full);
        dumpz(start);
        dumpz(pos);
        dumpz(scale);
        fprintf(stderr, "\t%12s   %10f\n", "",
            (double)info->z_scale / Z_ONE);
        dumpz(dx);
        fprintf(stderr, "\t%12s   %10f\n", "",
            (double)info->z_dx / Z_FULL_ONE);
        dumpz(dy);
        fprintf(stderr, "\t%12s   %10d\n", "", info->z_dy >> Z_SHIFT);
        fprintf(stderr, "\t%12s = %u bytes\n",
            "intpcm32_t", sizeof(intpcm32_t));
        fprintf(stderr, "\t%12s = 0x%08x, smallest=%.16lf\n",
            "Z_ONE", Z_ONE, (double)1.0 / (double)Z_ONE);
#endif

        /*
         * Allocate or reuse delay line buffer, whichever makes sense.
         */
        if (info->z_delay == NULL || info->z_full_alloc < info->z_full) {
                if (info->z_delay != NULL)
                        free(info->z_delay, M_DEVBUF);
                info->z_delay =
                    malloc(info->z_full * info->channels * info->bps,
                    M_DEVBUF, M_NOWAIT | M_ZERO);
                if (info->z_delay == NULL)
                        return (ENOMEM);
                info->z_full_alloc = info->z_full;
        }

        /*
         * "Zero" out head of buffer to avoid pops and clicks.
         */
        memset(info->z_delay, sndbuf_zerodata(f->desc->out),
            info->z_pos * info->channels * info->bps);

        return (0);
}

static int
z_resampler_set(struct pcm_feeder *f, int what, int32_t value)
{
        struct z_info *info;
        int32_t oquality;

        info = f->data;

        switch (what) {
        case Z_RATE_SRC:
                if (value < feeder_rate_min || value > feeder_rate_max)
                        return (E2BIG);
                info->rsrc = value;
                break;
        case Z_RATE_DST:
                if (value < feeder_rate_min || value > feeder_rate_max)
                        return (E2BIG);
                info->rdst = value;
                break;
        case Z_RATE_QUALITY:
                if (value < Z_QUALITY_MIN || value > Z_QUALITY_MAX)
                        return (EINVAL);
                /*
                 * If we failed to set the requested quality, restore
                 * the old one. We cannot afford leaving it broken since
                 * passive feeder chains like vchans never reinitialize
                 * itself.
                 */
                oquality = info->quality;
                info->quality = value;
                if (z_resampler_setup(f) == 0)
                        return (0);
                info->quality = oquality;
                break;
        default:
                return (EINVAL);
                break;
        }

        return (z_resampler_setup(f));
}

static int
z_resampler_get(struct pcm_feeder *f, int what)
{
        struct z_info *info;

        info = f->data;

        switch (what) {
        case Z_RATE_SRC:
                return (info->rsrc);
                break;
        case Z_RATE_DST:
                return (info->rdst);
                break;
        case Z_RATE_QUALITY:
                return (info->quality);
                break;
        default:
                break;
        }

        return (-1);
}

static int
z_resampler_init(struct pcm_feeder *f)
{
        struct z_info *info;

        if (f->desc->out != f->desc->in)
                return (EINVAL);

        info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT | M_ZERO);
        if (info == NULL)
                return (ENOMEM);

        info->rsrc = Z_RATE_DEFAULT;
        info->rdst = Z_RATE_DEFAULT;
        info->quality = feeder_rate_quality;
        f->data = info;

        return (z_resampler_setup(f));
}

static int
z_resampler_free(struct pcm_feeder *f)
{
        struct z_info *info;

        info = f->data;
        if (info != NULL) {
                if (info->z_delay != NULL)
                        free(info->z_delay, M_DEVBUF);
                free(info, M_DEVBUF);
        }
        f->data = NULL;

        return (0);
}

static int
z_resampler_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
    uint32_t count, void *source)
{
        struct z_info *info;
        int32_t alphadrift, startdrift, reqout, ocount, reqin, smpsz;
        int32_t fetch, fetched, start, cp;
        uint8_t *dst;

        info = f->data;
        if (info->z_resample == NULL)
                return (z_feed(f->source, c, b, count, source));

        /*
         * Calculate sample size and amount of sample output. We will do
         * everything in sample domain, but at the end we will jump back
         * to byte domain.
         */
        smpsz = info->channels * info->bps;
        ocount = count / smpsz;
        if (ocount == 0)
                return (0);

        /*
         * Calculate amount of input samples that is needed to generate
         * exact amount of output.
         */
        reqin = z_gy2gx(info, ocount) - z_fetched(info);

        startdrift = _Z_GY2GX(info, 0, 1);
        alphadrift = z_drift(info, startdrift, 1);

        dst = b;

        do {
                if (reqin != 0) {
                        fetch = min(z_free(info), reqin);
                        if (fetch == 0) {
                                /*
                                 * No more free spaces, so wind enough
                                 * samples back to the head of delay line
                                 * in byte domain.
                                 */
                                fetched = z_fetched(info);
                                start = z_prev(info, info->z_start,
                                    (info->z_size << 1) - 1);
                                cp = (info->z_size << 1) + fetched;
                                z_copy(info->z_delay + (start * smpsz),
                                    info->z_delay, cp * smpsz);
                                info->z_start =
                                    z_prev(info, info->z_size << 1, 1);
                                info->z_pos =
                                    z_next(info, info->z_start, fetched + 1);
                                fetch = min(z_free(info), reqin);
#ifndef _KERNEL
                                if (1) {
                                        static uint32_t kk = 0;
                                        fprintf(stderr,
                                            "Buffer Move: "
                                            "start=%d fetched=%d cp=%d "
                                            "cycle=%u [%u]\r",
                                            start, fetched, cp, info->z_cycle,
                                            ++kk);
                                }
                                info->z_cycle = 0;
#endif
                        }
                        if (fetch != 0) {
                                /*
                                 * Fetch in byte domain and jump back
                                 * to sample domain.
                                 */
                                fetched = z_feed(f->source, c,
                                    info->z_delay + (info->z_pos * smpsz),
                                    fetch * smpsz, source) / smpsz;
                                /*
                                 * Prepare to convert fetched buffer,
                                 * or mark us done if we cannot fulfill
                                 * the request.
                                 */
                                reqin -= fetched;
                                info->z_pos += fetched;
                                if (fetched != fetch)
                                        reqin = 0;
                        }
                }

                reqout = min(z_gx2gy(info, z_fetched(info)), ocount);
                if (reqout != 0) {
                        ocount -= reqout;

                        /*
                         * Drift.. drift.. drift..
                         *
                         * Notice that there are 2 methods of doing the drift
                         * operations: The former is much cleaner (in a sense
                         * sense of mathematical readings), but slower due to
                         * integer division in z_gy2gx(). Nevertheless, both
                         * should give the same exact accurate drifting
                         * results, so the later is favourable.
                         */
                        do {
                                info->z_resample(info, dst);
#if 0
                                startdrift = z_gy2gx(info, 1);
                                alphadrift = z_drift(info, startdrift, 1);
                                info->z_start += startdrift;
                                info->z_alpha += alphadrift;
#else
                                info->z_alpha += alphadrift;
                                if (info->z_alpha < info->z_gy)
                                        info->z_start += startdrift;
                                else {
                                        info->z_start += startdrift - 1;
                                        info->z_alpha -= info->z_gy;
                                }
#endif
                                dst += smpsz;
#ifndef _KERNEL
                                info->z_cycle++;
#endif
                        } while (--reqout != 0);
                }
        } while (reqin != 0 && ocount != 0);

        /*
         * Back to byte domain..
         */
        return (dst - b);
}

static struct pcm_feederdesc feeder_rate_desc[] = {
        {FEEDER_RATE,     AFMT_S8, AFMT_S8, 0},
        {FEEDER_RATE, AFMT_S16_LE, AFMT_S16_LE, 0},
        {FEEDER_RATE, AFMT_S24_LE, AFMT_S24_LE, 0},
        {FEEDER_RATE, AFMT_S32_LE, AFMT_S32_LE, 0},
        {FEEDER_RATE, AFMT_S16_BE, AFMT_S16_BE, 0},
        {FEEDER_RATE, AFMT_S24_BE, AFMT_S24_BE, 0},
        {FEEDER_RATE, AFMT_S32_BE, AFMT_S32_BE, 0},
        {FEEDER_RATE,     AFMT_S8 | AFMT_STEREO,     AFMT_S8 | AFMT_STEREO, 0},
        {FEEDER_RATE, AFMT_S16_LE | AFMT_STEREO, AFMT_S16_LE | AFMT_STEREO, 0},
        {FEEDER_RATE, AFMT_S24_LE | AFMT_STEREO, AFMT_S24_LE | AFMT_STEREO, 0},
        {FEEDER_RATE, AFMT_S32_LE | AFMT_STEREO, AFMT_S32_LE | AFMT_STEREO, 0},
        {FEEDER_RATE, AFMT_S16_BE | AFMT_STEREO, AFMT_S16_BE | AFMT_STEREO, 0},
        {FEEDER_RATE, AFMT_S24_BE | AFMT_STEREO, AFMT_S24_BE | AFMT_STEREO, 0},
        {FEEDER_RATE, AFMT_S32_BE | AFMT_STEREO, AFMT_S32_BE | AFMT_STEREO, 0},
        {FEEDER_RATE,     AFMT_U8, AFMT_U8, 0},
        {FEEDER_RATE, AFMT_U16_LE, AFMT_U16_LE, 0},
        {FEEDER_RATE, AFMT_U24_LE, AFMT_U24_LE, 0},
        {FEEDER_RATE, AFMT_U32_LE, AFMT_U32_LE, 0},
        {FEEDER_RATE, AFMT_U16_BE, AFMT_U16_BE, 0},
        {FEEDER_RATE, AFMT_U24_BE, AFMT_U24_BE, 0},
        {FEEDER_RATE, AFMT_U32_BE, AFMT_U32_BE, 0},
        {FEEDER_RATE,     AFMT_U8 | AFMT_STEREO,     AFMT_U8 | AFMT_STEREO, 0},
        {FEEDER_RATE, AFMT_U16_LE | AFMT_STEREO, AFMT_U16_LE | AFMT_STEREO, 0},
        {FEEDER_RATE, AFMT_U24_LE | AFMT_STEREO, AFMT_U24_LE | AFMT_STEREO, 0},
        {FEEDER_RATE, AFMT_U32_LE | AFMT_STEREO, AFMT_U32_LE | AFMT_STEREO, 0},
        {FEEDER_RATE, AFMT_U16_BE | AFMT_STEREO, AFMT_U16_BE | AFMT_STEREO, 0},
        {FEEDER_RATE, AFMT_U24_BE | AFMT_STEREO, AFMT_U24_BE | AFMT_STEREO, 0},
        {FEEDER_RATE, AFMT_U32_BE | AFMT_STEREO, AFMT_U32_BE | AFMT_STEREO, 0},
        {0, 0, 0, 0},
};

static kobj_method_t feeder_rate_methods[] = {
        KOBJMETHOD(feeder_init,         z_resampler_init),
        KOBJMETHOD(feeder_free,         z_resampler_free),
        KOBJMETHOD(feeder_set,          z_resampler_set),
        KOBJMETHOD(feeder_get,          z_resampler_get),
        KOBJMETHOD(feeder_feed,         z_resampler_feed),
        {0, 0}
};

FEEDER_DECLARE(feeder_rate, 2, NULL);