2 * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
3 * Copyright (c) 2003 Orion Hodson <orion@FreeBSD.org>
4 * Copyright (c) 2005 Ariff Abdullah <ariff@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 * *New* and rewritten soft sample rate converter supporting arbitrary sample
32 * rates, fine grained scaling/coefficients and a unified up/down stereo
33 * converter. Most of the disclaimers from orion's notes also applies
34 * here, regarding linear interpolation deficiencies and pre/post
35 * anti-aliasing filtering issues. This version comes with a much simpler and
36 * tighter interface, although it works almost exactly like the older one.
38 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
40 * This new implementation is fully dedicated in memory of Cameron Grant, *
41 * the creator of the magnificent, highly addictive feeder infrastructure. *
43 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
48 * This rate conversion code uses linear interpolation without any
49 * pre- or post- interpolation filtering to combat aliasing. This
50 * greatly limits the sound quality and should be addressed at some
51 * stage in the future.
53 * Since this accuracy of interpolation is sensitive and examination
54 * of the algorithm output is harder from the kernel, the code is
55 * designed to be compiled in the kernel and in a userland test
56 * harness. This is done by selectively including and excluding code
57 * with several portions based on whether _KERNEL is defined. It's a
58 * little ugly, but exceedingly useful. The testsuite and its
59 * revisions can be found at:
60 * http://people.freebsd.org/~orion/files/feedrate/
62 * Special thanks to Ken Marx for exposing flaws in the code and for
66 #include <dev/sound/pcm/sound.h>
67 #include "feeder_if.h"
69 SND_DECLARE_FILE("$FreeBSD$");
71 #define RATE_ASSERT(x, y) /* KASSERT(x,y) */
72 #define RATE_TEST(x, y) /* if (!(x)) printf y */
73 #define RATE_TRACE(x...) /* printf(x) */
75 MALLOC_DEFINE(M_RATEFEEDER, "ratefeed", "pcm rate feeder");
77 #define FEEDBUFSZ 8192
80 /* 8000 * 138 or 11025 * 100 . This is insane, indeed! */
81 #define RATEMAX 1102500
85 #define FEEDRATE_CONVERT_64 0
86 #define FEEDRATE_CONVERT_SCALE64 1
87 #define FEEDRATE_CONVERT_SCALE32 2
88 #define FEEDRATE_CONVERT_PLAIN 3
89 #define FEEDRATE_CONVERT_FIXED 4
90 #define FEEDRATE_CONVERT_OPTIMAL 5
91 #define FEEDRATE_CONVERT_WORST 6
93 #define FEEDRATE_64_MAXROLL 32
94 #define FEEDRATE_32_MAXROLL 16
96 struct feed_rate_info {
97 uint32_t src, dst; /* rounded source / destination rates */
98 uint32_t rsrc, rdst; /* original source / destination rates */
99 uint32_t gx, gy; /* interpolation / decimation ratio */
100 uint32_t alpha; /* interpolation distance */
101 uint32_t pos, bpos; /* current sample / buffer positions */
102 uint32_t bufsz; /* total buffer size */
103 uint32_t stray; /* stray bytes */
104 int32_t scale, roll; /* scale / roll factor */
106 uint32_t (*convert)(struct feed_rate_info *, int16_t *, uint32_t);
110 feed_convert_64(struct feed_rate_info *, int16_t *, uint32_t);
112 feed_convert_scale64(struct feed_rate_info *, int16_t *, uint32_t);
114 feed_convert_scale32(struct feed_rate_info *, int16_t *, uint32_t);
116 feed_convert_plain(struct feed_rate_info *, int16_t *, uint32_t);
118 int feeder_rate_ratemin = RATEMIN;
119 int feeder_rate_ratemax = RATEMAX;
121 * See 'Feeder Scaling Type' below..
123 static int feeder_rate_scaling = FEEDRATE_CONVERT_OPTIMAL;
124 static int feeder_rate_buffersize = FEEDBUFSZ & ~1;
127 * sysctls.. I love sysctls..
129 TUNABLE_INT("hw.snd.feeder_rate_ratemin", &feeder_rate_ratemin);
130 TUNABLE_INT("hw.snd.feeder_rate_ratemax", &feeder_rate_ratemin);
131 TUNABLE_INT("hw.snd.feeder_rate_scaling", &feeder_rate_scaling);
132 TUNABLE_INT("hw.snd.feeder_rate_buffersize", &feeder_rate_buffersize);
135 sysctl_hw_snd_feeder_rate_ratemin(SYSCTL_HANDLER_ARGS)
139 val = feeder_rate_ratemin;
140 err = sysctl_handle_int(oidp, &val, sizeof(val), req);
141 if (val < 1 || val >= feeder_rate_ratemax)
144 feeder_rate_ratemin = val;
147 SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_ratemin, CTLTYPE_INT | CTLFLAG_RW,
148 0, sizeof(int), sysctl_hw_snd_feeder_rate_ratemin, "I", "");
151 sysctl_hw_snd_feeder_rate_ratemax(SYSCTL_HANDLER_ARGS)
155 val = feeder_rate_ratemax;
156 err = sysctl_handle_int(oidp, &val, sizeof(val), req);
157 if (val <= feeder_rate_ratemin || val > 0x7fffff)
160 feeder_rate_ratemax = val;
163 SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_ratemax, CTLTYPE_INT | CTLFLAG_RW,
164 0, sizeof(int), sysctl_hw_snd_feeder_rate_ratemax, "I", "");
167 sysctl_hw_snd_feeder_rate_scaling(SYSCTL_HANDLER_ARGS)
171 val = feeder_rate_scaling;
172 err = sysctl_handle_int(oidp, &val, sizeof(val), req);
174 * Feeder Scaling Type
175 * ===================
177 * 1. Plain 64bit (high precision)
178 * 2. 64bit scaling (high precision, CPU friendly, but can
179 * cause gain up/down).
180 * 3. 32bit scaling (somehow can cause hz roundup, gain
182 * 4. Plain copy (default if src == dst. Except if src == dst,
183 * this is the worst / silly conversion method!).
187 * 0 - Plain 64bit - no fallback.
188 * 1 - 64bit scaling - no fallback.
189 * 2 - 32bit scaling - no fallback.
190 * 3 - Plain copy - no fallback.
191 * 4 - Fixed rate. Means that, choose optimal conversion method
192 * without causing hz roundup.
193 * 32bit scaling (as long as hz roundup does not occur),
194 * 64bit scaling, Plain 64bit.
195 * 5 - Optimal / CPU friendly (DEFAULT).
196 * 32bit scaling, 64bit scaling, Plain 64bit
197 * 6 - Optimal to worst, no 64bit arithmetic involved.
198 * 32bit scaling, Plain copy.
200 if (val < FEEDRATE_CONVERT_64 || val > FEEDRATE_CONVERT_WORST)
203 feeder_rate_scaling = val;
206 SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_scaling, CTLTYPE_INT | CTLFLAG_RW,
207 0, sizeof(int), sysctl_hw_snd_feeder_rate_scaling, "I", "");
210 sysctl_hw_snd_feeder_rate_buffersize(SYSCTL_HANDLER_ARGS)
214 val = feeder_rate_buffersize;
215 err = sysctl_handle_int(oidp, &val, sizeof(val), req);
217 * Don't waste too much kernel space
219 if (val < 2 || val > 65536)
222 feeder_rate_buffersize = val & ~1;
225 /* XXX: this should be settable by an user via a control tool, the sysadmin
226 needs a max and min sysctl to limit what an user can do */
227 SYSCTL_PROC(_hw_snd, OID_AUTO, _feeder_rate_buffersize, CTLTYPE_INT | CTLFLAG_RW,
228 0, sizeof(int), sysctl_hw_snd_feeder_rate_buffersize, "I", "");
231 feed_speed_ratio(uint32_t x, uint32_t y, uint32_t *gx, uint32_t *gy)
233 uint32_t w, src = x, dst = y;
245 feed_scale_roll(uint32_t dst, int32_t *scale, int32_t *roll, int32_t max)
251 for (j = MAXGAIN; j >= MINGAIN; j -= 3) {
252 for (troll = 0; troll < max; troll++) {
253 tscale = (1 << troll) / dst;
254 k = (tscale * dst * 100) >> troll;
255 if (k > j && k <= 100) {
265 feed_get_best_coef(uint32_t *src, uint32_t *dst, uint32_t *gx, uint32_t *gy,
266 int32_t *scale, int32_t *roll)
268 uint32_t tsrc, tdst, sscale, dscale;
269 int32_t tscale, troll;
270 int i, j, hzmin, hzmax;
273 for (i = 0; i < 2; i++) {
274 hzmin = (ROUNDHZ * i) + 1;
275 hzmax = hzmin + ROUNDHZ;
276 for (j = hzmin; j < hzmax; j++) {
277 tsrc = *src - (*src % j);
279 if (tsrc < 1 || tdst < 1)
281 feed_speed_ratio(tsrc, tdst, &sscale, &dscale);
282 feed_scale_roll(dscale, &tscale, &troll,
283 FEEDRATE_32_MAXROLL);
284 if (tscale != -1 && troll != -1) {
293 for (j = hzmin; j < hzmax; j++) {
294 tsrc = *src - (*src % j);
295 tdst = *dst - (*dst % j);
296 if (tsrc < 1 || tdst < 1)
298 feed_speed_ratio(tsrc, tdst, &sscale, &dscale);
299 feed_scale_roll(dscale, &tscale, &troll,
300 FEEDRATE_32_MAXROLL);
301 if (tscale != -1 && troll != -1) {
311 for (j = hzmin; j < hzmax; j++) {
313 tdst = *dst - (*dst % j);
314 if (tsrc < 1 || tdst < 1)
316 feed_speed_ratio(tsrc, tdst, &sscale, &dscale);
317 feed_scale_roll(dscale, &tscale, &troll,
318 FEEDRATE_32_MAXROLL);
319 if (tscale != -1 && troll != -1) {
331 feed_speed_ratio(*src, *dst, gx, gy);
332 feed_scale_roll(*gy, scale, roll, FEEDRATE_32_MAXROLL);
337 feed_rate_reset(struct feed_rate_info *info)
341 info->src = info->rsrc;
342 info->dst = info->rdst;
348 feed_rate_setup(struct pcm_feeder *f)
350 struct feed_rate_info *info = f->data;
357 feed_rate_reset(info);
358 if (info->src == info->dst) {
360 * No conversion ever needed. Just do plain copy.
362 info->convert = feed_convert_plain;
366 switch (feeder_rate_scaling) {
367 case FEEDRATE_CONVERT_64:
368 feed_speed_ratio(info->src, info->dst,
369 &info->gx, &info->gy);
370 info->convert = feed_convert_64;
372 case FEEDRATE_CONVERT_SCALE64:
373 feed_speed_ratio(info->src, info->dst,
374 &info->gx, &info->gy);
375 feed_scale_roll(info->gy, &info->scale,
376 &info->roll, FEEDRATE_64_MAXROLL);
377 if (info->scale == -1 || info->roll == -1)
379 info->convert = feed_convert_scale64;
381 case FEEDRATE_CONVERT_SCALE32:
382 r = feed_get_best_coef(&info->src, &info->dst,
383 &info->gx, &info->gy, &info->scale,
387 info->convert = feed_convert_scale32;
389 case FEEDRATE_CONVERT_PLAIN:
390 feed_speed_ratio(info->src, info->dst,
391 &info->gx, &info->gy);
392 info->convert = feed_convert_plain;
394 case FEEDRATE_CONVERT_FIXED:
395 r = feed_get_best_coef(&info->src, &info->dst,
396 &info->gx, &info->gy, &info->scale,
398 if (r != 0 && info->src == info->rsrc &&
399 info->dst == info->rdst)
400 info->convert = feed_convert_scale32;
403 feed_rate_reset(info);
404 feed_speed_ratio(info->src, info->dst,
405 &info->gx, &info->gy);
406 feed_scale_roll(info->gy, &info->scale,
407 &info->roll, FEEDRATE_64_MAXROLL);
408 if (info->scale != -1 && info->roll != -1)
409 info->convert = feed_convert_scale64;
411 info->convert = feed_convert_64;
414 case FEEDRATE_CONVERT_OPTIMAL:
415 r = feed_get_best_coef(&info->src, &info->dst,
416 &info->gx, &info->gy, &info->scale,
419 info->convert = feed_convert_scale32;
422 feed_rate_reset(info);
423 feed_speed_ratio(info->src, info->dst,
424 &info->gx, &info->gy);
425 feed_scale_roll(info->gy, &info->scale,
426 &info->roll, FEEDRATE_64_MAXROLL);
427 if (info->scale != -1 && info->roll != -1)
428 info->convert = feed_convert_scale64;
430 info->convert = feed_convert_64;
433 case FEEDRATE_CONVERT_WORST:
434 r = feed_get_best_coef(&info->src, &info->dst,
435 &info->gx, &info->gy, &info->scale,
438 info->convert = feed_convert_scale32;
441 feed_rate_reset(info);
442 feed_speed_ratio(info->src, info->dst,
443 &info->gx, &info->gy);
444 info->convert = feed_convert_plain;
452 if (info->gx == 0 || info->gy == 0)
455 * No need to interpolate/decimate, just do plain copy.
456 * This probably caused by Hz roundup.
458 if (info->gx == info->gy)
459 info->convert = feed_convert_plain;
465 feed_rate_set(struct pcm_feeder *f, int what, int value)
467 struct feed_rate_info *info = f->data;
469 if (value < feeder_rate_ratemin || value > feeder_rate_ratemax)
482 return feed_rate_setup(f);
486 feed_rate_get(struct pcm_feeder *f, int what)
488 struct feed_rate_info *info = f->data;
491 * Return *real* src/dst rate.
505 feed_rate_init(struct pcm_feeder *f)
507 struct feed_rate_info *info;
509 info = malloc(sizeof(*info), M_RATEFEEDER, M_NOWAIT | M_ZERO);
513 * bufsz = sample from last cycle + conversion space
515 info->bufsz = 2 + feeder_rate_buffersize;
516 info->buffer = malloc(sizeof(*info->buffer) * info->bufsz,
517 M_RATEFEEDER, M_NOWAIT | M_ZERO);
518 if (info->buffer == NULL) {
519 free(info, M_RATEFEEDER);
522 info->rsrc = DSP_DEFAULT_SPEED;
523 info->rdst = DSP_DEFAULT_SPEED;
525 return feed_rate_setup(f);
529 feed_rate_free(struct pcm_feeder *f)
531 struct feed_rate_info *info = f->data;
535 free(info->buffer, M_RATEFEEDER);
536 free(info, M_RATEFEEDER);
543 feed_convert_64(struct feed_rate_info *info, int16_t *dst, uint32_t max)
545 int64_t x, alpha, distance;
547 int32_t pos, bpos, gx, gy;
550 * Plain, straight forward 64bit arith. No bit-magic applied here.
567 distance = gy - alpha;
568 x = (alpha * src[pos - 2]) + (distance * src[pos]);
570 x = (alpha * src[pos - 1]) + (distance * src[pos + 1]);
582 feed_convert_scale64(struct feed_rate_info *info, int16_t *dst, uint32_t max)
584 int64_t x, alpha, distance;
586 int32_t pos, bpos, gx, gy, roll;
593 alpha = info->alpha * info->scale;
594 gx = info->gx * info->scale;
595 gy = info->gy * info->scale;
607 distance = gy - alpha;
608 x = (alpha * src[pos - 2]) + (distance * src[pos]);
609 dst[ret++] = x >> roll;
610 x = (alpha * src[pos - 1]) + (distance * src[pos + 1]);
611 dst[ret++] = x >> roll;
616 info->alpha = alpha / info->scale;
622 feed_convert_scale32(struct feed_rate_info *info, int16_t *dst, uint32_t max)
625 int32_t x, pos, bpos, gx, gy, alpha, roll, distance;
632 alpha = info->alpha * info->scale;
633 gx = info->gx * info->scale;
634 gy = info->gy * info->scale;
646 distance = gy - alpha;
647 x = (alpha * src[pos - 2]) + (distance * src[pos]);
648 dst[ret++] = x >> roll;
649 x = (alpha * src[pos - 1]) + (distance * src[pos + 1]);
650 dst[ret++] = x >> roll;
655 info->alpha = alpha / info->scale;
661 feed_convert_plain(struct feed_rate_info *info, int16_t *dst, uint32_t max)
664 int32_t pos, bpos, gx, gy, alpha;
684 dst[ret++] = src[pos];
685 dst[ret++] = src[pos + 1];
696 feed_rate(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
697 uint32_t count, void *source)
699 struct feed_rate_info *info = f->data;
702 int16_t *dst = (int16_t *)b;
704 * This loop has been optimized to generalize both up / down
705 * sampling without causing missing samples or excessive buffer
708 RATE_TEST(count >= 4 && (count & 3) == 0,
709 ("%s: Count size not byte integral (%d)\n", __func__, count));
714 slot = (((info->gx * (count >> 1)) + info->gy - info->alpha - 1) / info->gy) << 1;
715 RATE_TEST((slot & 1) == 0, ("%s: Slot count not sample integral (%d)\n",
718 * Optimize buffer feeding aggressively to ensure calculated slot
719 * can be fitted nicely into available buffer free space, hence
720 * avoiding multiple feeding.
722 RATE_TEST(info->stray == 0, ("%s: [1] Stray bytes: %u\n",
723 __func__,info->stray));
724 if (info->pos != 2 && info->bpos - info->pos == 2 &&
725 info->bpos + slot > info->bufsz) {
727 * Copy last unit sample and its previous to
728 * beginning of buffer.
730 info->buffer[0] = info->buffer[info->pos - 2];
731 info->buffer[1] = info->buffer[info->pos - 1];
732 info->buffer[2] = info->buffer[info->pos];
733 info->buffer[3] = info->buffer[info->pos + 1];
737 RATE_ASSERT(slot >= 0, ("%s: Negative Slot: %d\n",
742 fetch = (info->bufsz - info->bpos) << 1;
743 fetch -= info->stray;
744 RATE_ASSERT(fetch >= 0,
745 ("%s: [1] Buffer overrun: %d > %d\n",
746 __func__, info->bpos, info->bufsz));
747 if ((slot << 1) < fetch)
750 RATE_ASSERT(((info->bpos << 1) - info->stray) >= 0 &&
751 ((info->bpos << 1) - info->stray) < (info->bufsz << 1),
752 ("%s: DANGER - BUFFER OVERRUN! bufsz=%d, pos=%d\n", __func__,
753 info->bufsz << 1, (info->bpos << 1) - info->stray));
754 fetch = FEEDER_FEED(f->source, c,
755 (uint8_t *)(info->buffer) + (info->bpos << 1) - info->stray,
760 RATE_TEST((fetch & 3) == 0,
761 ("%s: Fetch size not byte integral (%d)\n",
763 info->stray += fetch & 3;
764 RATE_TEST(info->stray == 0,
765 ("%s: Stray bytes detected (%d)\n",
766 __func__, info->stray));
771 RATE_ASSERT(slot >= 0,
772 ("%s: Negative Slot: %d\n", __func__,
776 if (info->bpos == info->bufsz)
781 if (info->pos == info->bpos) {
782 RATE_TEST(info->pos == 2,
783 ("%s: EOF while in progress\n", __func__));
786 RATE_ASSERT(info->pos <= info->bpos,
787 ("%s: [2] Buffer overrun: %d > %d\n", __func__,
788 info->pos, info->bpos));
789 RATE_ASSERT(info->pos < info->bpos,
790 ("%s: Zero buffer!\n", __func__));
791 RATE_ASSERT(((info->bpos - info->pos) & 1) == 0,
792 ("%s: Buffer not sample integral (%d)\n",
793 __func__, info->bpos - info->pos));
794 i += info->convert(info, dst + i, count - i);
795 RATE_ASSERT(info->pos <= info->bpos,
796 ("%s: [3] Buffer overrun: %d > %d\n",
797 __func__, info->pos, info->bpos));
798 if (info->pos == info->bpos) {
800 * End of buffer cycle. Copy last unit sample
801 * to beginning of buffer so next cycle can
802 * interpolate using it.
804 RATE_TEST(info->stray == 0, ("%s: [2] Stray bytes: %u\n", __func__, info->stray));
805 info->buffer[0] = info->buffer[info->pos - 2];
806 info->buffer[1] = info->buffer[info->pos - 1];
814 RATE_TEST(count == i, ("Expect: %u , Got: %u\n", count << 1, i << 1));
816 RATE_TEST(info->stray == 0, ("%s: [3] Stray bytes: %u\n", __func__, info->stray));
820 static struct pcm_feederdesc feeder_rate_desc[] = {
821 {FEEDER_RATE, AFMT_S16_LE | AFMT_STEREO, AFMT_S16_LE | AFMT_STEREO, 0},
824 static kobj_method_t feeder_rate_methods[] = {
825 KOBJMETHOD(feeder_init, feed_rate_init),
826 KOBJMETHOD(feeder_free, feed_rate_free),
827 KOBJMETHOD(feeder_set, feed_rate_set),
828 KOBJMETHOD(feeder_get, feed_rate_get),
829 KOBJMETHOD(feeder_feed, feed_rate),
832 FEEDER_DECLARE(feeder_rate, 2, NULL);