1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
|
/*-
* Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
* Copyright (c) 2003 Orion Hodson <orion@freebsd.org>
* Copyright (c) 2005 Ariff Abdullah <skywizard@MyBSD.org.my>
* 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.
*
* 2005-06-11:
* ==========
*
* *New* and rewritten soft sample rate converter supporting arbitary sample
* rate, fine grained scalling/coefficients and unified up/down stereo
* converter. Most of disclaimers from orion's previous version also applied
* here, regarding with linear interpolation deficiencies, pre/post
* anti-aliasing filtering issues. This version comes with much simpler and
* tighter interface, although it works almost exactly like the older one.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* This new implementation is fully dedicated in memory of Cameron Grant, *
* the creator of magnificent, highly addictive feeder infrastructure. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Orion's notes:
* =============
*
* This rate conversion code uses linear interpolation without any
* pre- or post- interpolation filtering to combat aliasing. This
* greatly limits the sound quality and should be addressed at some
* stage in the future.
*
* Since this accuracy of interpolation is sensitive and examination
* of the algorithm output is harder from the kernel, th code is
* designed to be compiled in the kernel and in a userland test
* harness. This is done by selectively including and excluding code
* with several portions based on whether _KERNEL is defined. It's a
* little ugly, but exceedingly useful. The testsuite and its
* revisions can be found at:
* http://people.freebsd.org/~orion/files/feedrate/
*
* Special thanks to Ken Marx for exposing flaws in the code and for
* testing revisions.
*/
#include <dev/sound/pcm/sound.h>
#include "feeder_if.h"
SND_DECLARE_FILE("$FreeBSD$");
#define RATE_ASSERT(x, y) /* KASSERT(x,y) */
#define RATE_TRACE(x...) /* printf(x) */
MALLOC_DEFINE(M_RATEFEEDER, "ratefeed", "pcm rate feeder");
#define FEEDBUFSZ 8192
#define ROUNDHZ 25
#define RATEMIN 4000
/* 8000 * 138 or 11025 * 100 . This is insane, indeed! */
#define RATEMAX 1102500
#define MINGAIN 92
#define MAXGAIN 96
#define FEEDRATE_CONVERT_64 0
#define FEEDRATE_CONVERT_SCALE64 1
#define FEEDRATE_CONVERT_SCALE32 2
#define FEEDRATE_CONVERT_PLAIN 3
#define FEEDRATE_CONVERT_FIXED 4
#define FEEDRATE_CONVERT_OPTIMAL 5
#define FEEDRATE_CONVERT_WORST 6
#define FEEDRATE_64_MAXROLL 32
#define FEEDRATE_32_MAXROLL 16
struct feed_rate_info {
uint32_t src, dst; /* rounded source / destination rates */
uint32_t rsrc, rdst; /* original source / destination rates */
uint32_t gx, gy; /* interpolation / decimation ratio */
uint32_t alpha; /* interpolation distance */
uint32_t pos, bpos; /* current sample / buffer positions */
uint32_t bufsz; /* total buffer size */
int32_t scale, roll; /* scale / roll factor */
int16_t *buffer;
uint32_t (*convert)(struct feed_rate_info *, int16_t *, uint32_t);
};
static uint32_t
feed_convert_64(struct feed_rate_info *, int16_t *, uint32_t);
static uint32_t
feed_convert_scale64(struct feed_rate_info *, int16_t *, uint32_t);
static uint32_t
feed_convert_scale32(struct feed_rate_info *, int16_t *, uint32_t);
static uint32_t
feed_convert_plain(struct feed_rate_info *, int16_t *, uint32_t);
int feeder_rate_ratemin = RATEMIN;
int feeder_rate_ratemax = RATEMAX;
/*
* See 'Feeder Scaling Type' below..
*/
static int feeder_rate_scaling = FEEDRATE_CONVERT_OPTIMAL;
static int feeder_rate_buffersize = FEEDBUFSZ & ~1;
/*
* sysctls.. I love sysctls..
*/
TUNABLE_INT("hw.snd.feeder_rate_ratemin", &feeder_rate_ratemin);
TUNABLE_INT("hw.snd.feeder_rate_ratemax", &feeder_rate_ratemin);
TUNABLE_INT("hw.snd.feeder_rate_scaling", &feeder_rate_scaling);
TUNABLE_INT("hw.snd.feeder_rate_buffersize", &feeder_rate_buffersize);
static int
sysctl_hw_snd_feeder_rate_ratemin(SYSCTL_HANDLER_ARGS)
{
int err, val;
val = feeder_rate_ratemin;
err = sysctl_handle_int(oidp, &val, sizeof(val), req);
if (val < 1 || val >= feeder_rate_ratemax)
err = EINVAL;
else
feeder_rate_ratemin = val;
return err;
}
SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_ratemin, CTLTYPE_INT | CTLFLAG_RW,
0, sizeof(int), sysctl_hw_snd_feeder_rate_ratemin, "I", "");
static int
sysctl_hw_snd_feeder_rate_ratemax(SYSCTL_HANDLER_ARGS)
{
int err, val;
val = feeder_rate_ratemax;
err = sysctl_handle_int(oidp, &val, sizeof(val), req);
if (val <= feeder_rate_ratemin || val > 0x7fffff)
err = EINVAL;
else
feeder_rate_ratemax = val;
return err;
}
SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_ratemax, CTLTYPE_INT | CTLFLAG_RW,
0, sizeof(int), sysctl_hw_snd_feeder_rate_ratemax, "I", "");
static int
sysctl_hw_snd_feeder_rate_scaling(SYSCTL_HANDLER_ARGS)
{
int err, val;
val = feeder_rate_scaling;
err = sysctl_handle_int(oidp, &val, sizeof(val), req);
/*
* Feeder Scaling Type
* ===================
*
* 1. Plain 64bit (high precision)
* 2. 64bit scaling (high precision, CPU friendly, but can
* cause gain up/down).
* 3. 32bit scaling (somehow can cause hz roundup, gain
* up/down).
* 4. Plain copy (default if src == dst. Except if src == dst,
* this is the worst / silly conversion method!).
*
* Sysctl options:-
*
* 0 - Plain 64bit - no fallback.
* 1 - 64bit scaling - no fallback.
* 2 - 32bit scaling - no fallback.
* 3 - Plain copy - no fallback.
* 4 - Fixed rate. Means that, choose optimal conversion method
* without causing hz roundup.
* 32bit scaling (as long as hz roundup does not occur),
* 64bit scaling, Plain 64bit.
* 5 - Optimal / CPU friendly (DEFAULT).
* 32bit scaling, 64bit scaling, Plain 64bit
* 6 - Optimal to worst, no 64bit arithmetic involved.
* 32bit scaling, Plain copy.
*/
if (val < FEEDRATE_CONVERT_64 || val > FEEDRATE_CONVERT_WORST)
err = EINVAL;
else
feeder_rate_scaling = val;
return err;
}
SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_scaling, CTLTYPE_INT | CTLFLAG_RW,
0, sizeof(int), sysctl_hw_snd_feeder_rate_scaling, "I", "");
static int
sysctl_hw_snd_feeder_rate_buffersize(SYSCTL_HANDLER_ARGS)
{
int err, val;
val = feeder_rate_buffersize;
err = sysctl_handle_int(oidp, &val, sizeof(val), req);
/*
* Don't waste too much kernel space
*/
if (val < 2 || val > 65536)
err = EINVAL;
else
feeder_rate_buffersize = val & ~1;
return err;
}
SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_rate_buffersize, CTLTYPE_INT | CTLFLAG_RW,
0, sizeof(int), sysctl_hw_snd_feeder_rate_buffersize, "I", "");
static void
feed_speed_ratio(uint32_t x, uint32_t y, uint32_t *gx, uint32_t *gy)
{
uint32_t w, src = x, dst = y;
while (y != 0) {
w = x % y;
x = y;
y = w;
}
*gx = src / x;
*gy = dst / x;
}
static void
feed_scale_roll(uint32_t dst, int32_t *scale, int32_t *roll, int32_t max)
{
int64_t k, tscale;
int32_t j, troll;
*scale = *roll = -1;
for (j = MAXGAIN; j >= MINGAIN; j -= 3) {
for (troll = 0; troll < max; troll++) {
tscale = (1 << troll) / dst;
k = (tscale * dst * 100) >> troll;
if (k > j && k <= 100) {
*scale = tscale;
*roll = troll;
return;
}
}
}
}
static int
feed_get_best_coef(uint32_t *src, uint32_t *dst, uint32_t *gx, uint32_t *gy,
int32_t *scale, int32_t *roll)
{
uint32_t tsrc, tdst, sscale, dscale;
int32_t tscale, troll;
int i, j, hzmin, hzmax;
*scale = *roll = -1;
for (i = 0; i < 2; i++) {
hzmin = (ROUNDHZ * i) + 1;
hzmax = hzmin + ROUNDHZ;
for (j = hzmin; j < hzmax; j++) {
tsrc = *src - (*src % j);
tdst = *dst;
if (tsrc < 1 || tdst < 1)
goto coef_failed;
feed_speed_ratio(tsrc, tdst, &sscale, &dscale);
feed_scale_roll(dscale, &tscale, &troll,
FEEDRATE_32_MAXROLL);
if (tscale != -1 && troll != -1) {
*src = tsrc;
*gx = sscale;
*gy = dscale;
*scale = tscale;
*roll = troll;
return j;
}
}
for (j = hzmin; j < hzmax; j++) {
tsrc = *src - (*src % j);
tdst = *dst - (*dst % j);
if (tsrc < 1 || tdst < 1)
goto coef_failed;
feed_speed_ratio(tsrc, tdst, &sscale, &dscale);
feed_scale_roll(dscale, &tscale, &troll,
FEEDRATE_32_MAXROLL);
if (tscale != -1 && troll != -1) {
*src = tsrc;
*dst = tdst;
*gx = sscale;
*gy = dscale;
*scale = tscale;
*roll = troll;
return j;
}
}
for (j = hzmin; j < hzmax; j++) {
tsrc = *src;
tdst = *dst - (*dst % j);
if (tsrc < 1 || tdst < 1)
goto coef_failed;
feed_speed_ratio(tsrc, tdst, &sscale, &dscale);
feed_scale_roll(dscale, &tscale, &troll,
FEEDRATE_32_MAXROLL);
if (tscale != -1 && troll != -1) {
*src = tsrc;
*dst = tdst;
*gx = sscale;
*gy = dscale;
*scale = tscale;
*roll = troll;
return j;
}
}
}
coef_failed:
feed_speed_ratio(*src, *dst, gx, gy);
feed_scale_roll(*gy, scale, roll, FEEDRATE_32_MAXROLL);
return 0;
}
static void
feed_rate_reset(struct feed_rate_info *info)
{
info->scale = -1;
info->roll = -1;
info->src = info->rsrc;
info->dst = info->rdst;
info->gx = 0;
info->gy = 0;
}
static int
feed_rate_setup(struct pcm_feeder *f)
{
struct feed_rate_info *info = f->data;
int r = 0;
info->pos = 2;
info->bpos = 4;
info->alpha = 0;
feed_rate_reset(info);
if (info->src == info->dst) {
/*
* No conversion ever needed. Just do plain copy.
*/
info->convert = feed_convert_plain;
info->gx = 1;
info->gy = 1;
} else {
switch (feeder_rate_scaling) {
case FEEDRATE_CONVERT_64:
feed_speed_ratio(info->src, info->dst,
&info->gx, &info->gy);
info->convert = feed_convert_64;
break;
case FEEDRATE_CONVERT_SCALE64:
feed_speed_ratio(info->src, info->dst,
&info->gx, &info->gy);
feed_scale_roll(info->gy, &info->scale,
&info->roll, FEEDRATE_64_MAXROLL);
if (info->scale == -1 || info->roll == -1)
return -1;
info->convert = feed_convert_scale64;
break;
case FEEDRATE_CONVERT_SCALE32:
r = feed_get_best_coef(&info->src, &info->dst,
&info->gx, &info->gy, &info->scale,
&info->roll);
if (r == 0)
return -1;
info->convert = feed_convert_scale32;
break;
case FEEDRATE_CONVERT_PLAIN:
feed_speed_ratio(info->src, info->dst,
&info->gx, &info->gy);
info->convert = feed_convert_plain;
break;
case FEEDRATE_CONVERT_FIXED:
r = feed_get_best_coef(&info->src, &info->dst,
&info->gx, &info->gy, &info->scale,
&info->roll);
if (r != 0 && info->src == info->rsrc &&
info->dst == info->rdst)
info->convert = feed_convert_scale32;
else {
/* Fallback */
feed_rate_reset(info);
feed_speed_ratio(info->src, info->dst,
&info->gx, &info->gy);
feed_scale_roll(info->gy, &info->scale,
&info->roll, FEEDRATE_64_MAXROLL);
if (info->scale != -1 && info->roll != -1)
info->convert = feed_convert_scale64;
else
info->convert = feed_convert_64;
}
break;
case FEEDRATE_CONVERT_OPTIMAL:
r = feed_get_best_coef(&info->src, &info->dst,
&info->gx, &info->gy, &info->scale,
&info->roll);
if (r != 0)
info->convert = feed_convert_scale32;
else {
/* Fallback */
feed_rate_reset(info);
feed_speed_ratio(info->src, info->dst,
&info->gx, &info->gy);
feed_scale_roll(info->gy, &info->scale,
&info->roll, FEEDRATE_64_MAXROLL);
if (info->scale != -1 && info->roll != -1)
info->convert = feed_convert_scale64;
else
info->convert = feed_convert_64;
}
break;
case FEEDRATE_CONVERT_WORST:
r = feed_get_best_coef(&info->src, &info->dst,
&info->gx, &info->gy, &info->scale,
&info->roll);
if (r != 0)
info->convert = feed_convert_scale32;
else {
/* Fallback */
feed_rate_reset(info);
feed_speed_ratio(info->src, info->dst,
&info->gx, &info->gy);
info->convert = feed_convert_plain;
}
break;
default:
return -1;
break;
}
/* No way! */
if (info->gx == 0 || info->gy == 0)
return -1;
/*
* No need to interpolate/decimate, just do plain copy.
* This probably caused by Hz roundup.
*/
if (info->gx == info->gy)
info->convert = feed_convert_plain;
}
return 0;
}
static int
feed_rate_set(struct pcm_feeder *f, int what, int value)
{
struct feed_rate_info *info = f->data;
if (value < feeder_rate_ratemin || value > feeder_rate_ratemax)
return -1;
switch (what) {
case FEEDRATE_SRC:
info->rsrc = value;
break;
case FEEDRATE_DST:
info->rdst = value;
break;
default:
return -1;
}
return feed_rate_setup(f);
}
static int
feed_rate_get(struct pcm_feeder *f, int what)
{
struct feed_rate_info *info = f->data;
/*
* Return *real* src/dst rate.
*/
switch (what) {
case FEEDRATE_SRC:
return info->rsrc;
case FEEDRATE_DST:
return info->rdst;
default:
return -1;
}
return -1;
}
static int
feed_rate_init(struct pcm_feeder *f)
{
struct feed_rate_info *info;
info = malloc(sizeof(*info), M_RATEFEEDER, M_NOWAIT | M_ZERO);
if (info == NULL)
return ENOMEM;
/*
* bufsz = sample from last cycle + conversion space
*/
info->bufsz = 2 + feeder_rate_buffersize;
info->buffer = malloc(sizeof(*info->buffer) * info->bufsz,
M_RATEFEEDER, M_NOWAIT | M_ZERO);
if (info->buffer == NULL) {
free(info, M_RATEFEEDER);
return ENOMEM;
}
info->rsrc = DSP_DEFAULT_SPEED;
info->rdst = DSP_DEFAULT_SPEED;
f->data = info;
return feed_rate_setup(f);
}
static int
feed_rate_free(struct pcm_feeder *f)
{
struct feed_rate_info *info = f->data;
if (info) {
if (info->buffer)
free(info->buffer, M_RATEFEEDER);
free(info, M_RATEFEEDER);
}
f->data = NULL;
return 0;
}
static uint32_t
feed_convert_64(struct feed_rate_info *info, int16_t *dst, uint32_t max)
{
int64_t x, alpha, distance;
uint32_t ret;
int32_t pos, bpos, gx, gy;
int16_t *src;
/*
* Plain, straight forward 64bit arith. No bit-magic applied here.
*/
ret = 0;
alpha = info->alpha;
gx = info->gx;
gy = info->gy;
pos = info->pos;
bpos = info->bpos;
src = info->buffer;
for (;;) {
if (alpha < gx) {
alpha += gy;
pos += 2;
if (pos == bpos)
break;
} else {
alpha -= gx;
distance = gy - alpha;
x = (alpha * src[pos - 2]) + (distance * src[pos]);
dst[ret++] = x / gy;
x = (alpha * src[pos - 1]) + (distance * src[pos + 1]);
dst[ret++] = x / gy;
if (ret == max)
break;
}
}
info->alpha = alpha;
info->pos = pos;
return ret;
}
static uint32_t
feed_convert_scale64(struct feed_rate_info *info, int16_t *dst, uint32_t max)
{
int64_t x, alpha, distance;
uint32_t ret;
int32_t pos, bpos, gx, gy, roll;
int16_t *src;
/*
* 64bit scaling.
*/
ret = 0;
roll = info->roll;
alpha = info->alpha * info->scale;
gx = info->gx * info->scale;
gy = info->gy * info->scale;
pos = info->pos;
bpos = info->bpos;
src = info->buffer;
for (;;) {
if (alpha < gx) {
alpha += gy;
pos += 2;
if (pos == bpos)
break;
} else {
alpha -= gx;
distance = gy - alpha;
x = (alpha * src[pos - 2]) + (distance * src[pos]);
dst[ret++] = x >> roll;
x = (alpha * src[pos - 1]) + (distance * src[pos + 1]);
dst[ret++] = x >> roll;
if (ret == max)
break;
}
}
info->alpha = alpha / info->scale;
info->pos = pos;
return ret;
}
static uint32_t
feed_convert_scale32(struct feed_rate_info *info, int16_t *dst, uint32_t max)
{
uint32_t ret;
int32_t x, pos, bpos, gx, gy, alpha, roll, distance;
int16_t *src;
/*
* 32bit scaling.
*/
ret = 0;
roll = info->roll;
alpha = info->alpha * info->scale;
gx = info->gx * info->scale;
gy = info->gy * info->scale;
pos = info->pos;
bpos = info->bpos;
src = info->buffer;
for (;;) {
if (alpha < gx) {
alpha += gy;
pos += 2;
if (pos == bpos)
break;
} else {
alpha -= gx;
distance = gy - alpha;
x = (alpha * src[pos - 2]) + (distance * src[pos]);
dst[ret++] = x >> roll;
x = (alpha * src[pos - 1]) + (distance * src[pos + 1]);
dst[ret++] = x >> roll;
if (ret == max)
break;
}
}
info->alpha = alpha / info->scale;
info->pos = pos;
return ret;
}
static uint32_t
feed_convert_plain(struct feed_rate_info *info, int16_t *dst, uint32_t max)
{
uint32_t ret;
int32_t pos, bpos, gx, gy, alpha;
int16_t *src;
/*
* Plain copy.
*/
ret = 0;
gx = info->gx;
gy = info->gy;
alpha = info->alpha;
pos = info->pos;
bpos = info->bpos;
src = info->buffer;
for (;;) {
if (alpha < gx) {
alpha += gy;
pos += 2;
if (pos == bpos)
break;
} else {
alpha -= gx;
dst[ret++] = src[pos];
dst[ret++] = src[pos + 1];
if (ret == max)
break;
}
}
info->pos = pos;
info->alpha = alpha;
return ret;
}
static int32_t
feed_rate(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
uint32_t count, void *source)
{
struct feed_rate_info *info = f->data;
uint32_t i;
int32_t fetch, slot;
int16_t *dst = (int16_t *)b;
/*
* This loop has been optimized to generalize both up / down
* sampling without causing missing samples or excessive buffer
* feeding.
*/
RATE_ASSERT(count >= 4 && count % 4 == 0,
("%s: Count size not byte integral\n", __func__));
count >>= 1;
slot = (((info->gx * (count >> 1)) + info->gy - info->alpha - 1) / info->gy) << 1;
/*
* Optimize buffer feeding aggresively to ensure calculated slot
* can be fitted nicely into available buffer free space, hence
* avoiding multiple feeding.
*/
if (info->pos != 2 && info->bpos - info->pos == 2 &&
info->bpos + slot > info->bufsz) {
/*
* Copy last unit sample and its previous to
* beginning of buffer.
*/
info->buffer[0] = info->buffer[info->pos - 2];
info->buffer[1] = info->buffer[info->pos - 1];
info->buffer[2] = info->buffer[info->pos];
info->buffer[3] = info->buffer[info->pos + 1];
info->pos = 2;
info->bpos = 4;
}
RATE_ASSERT(slot >= 0, ("%s: Negative Slot: %d\n",
__func__, slot));
i = 0;
for (;;) {
for (;;) {
fetch = info->bufsz - info->bpos;
RATE_ASSERT(fetch >= 0,
("%s: Buffer overrun: %d > %d\n",
__func__, info->bpos, info->bufsz));
if (slot < fetch)
fetch = slot;
if (fetch > 0) {
RATE_ASSERT(fetch % 2 == 0,
("%s: Fetch size not sample integral\n",
__func__));
fetch = FEEDER_FEED(f->source, c,
(uint8_t *)(info->buffer + info->bpos),
fetch << 1, source);
if (fetch == 0)
break;
RATE_ASSERT(fetch % 4 == 0,
("%s: Fetch size not byte integral\n",
__func__));
fetch >>= 1;
info->bpos += fetch;
slot -= fetch;
RATE_ASSERT(slot >= 0,
("%s: Negative Slot: %d\n", __func__,
slot));
if (slot == 0)
break;
if (info->bpos == info->bufsz)
break;
} else
break;
}
if (info->pos == info->bpos) {
RATE_ASSERT(info->pos == 2,
("%s: EOF while in progress\n", __func__));
break;
}
RATE_ASSERT(info->pos <= info->bpos,
("%s: Buffer overrun: %d > %d\n", __func__,
info->pos, info->bpos));
RATE_ASSERT(info->pos < info->bpos,
("%s: Zero buffer!\n", __func__));
RATE_ASSERT((info->bpos - info->pos) % 2 == 0,
("%s: Buffer not sample integral\n", __func__));
i += info->convert(info, dst + i, count - i);
RATE_ASSERT(info->pos <= info->bpos,
("%s: Buffer overrun: %d > %d\n",
__func__, info->pos, info->bpos));
if (info->pos == info->bpos) {
/*
* End of buffer cycle. Copy last unit sample
* to beginning of buffer so next cycle can
* interpolate using it.
*/
info->buffer[0] = info->buffer[info->pos - 2];
info->buffer[1] = info->buffer[info->pos - 1];
info->bpos = 2;
info->pos = 2;
}
if (i == count)
break;
}
return i << 1;
}
static struct pcm_feederdesc feeder_rate_desc[] = {
{FEEDER_RATE, AFMT_S16_LE | AFMT_STEREO, AFMT_S16_LE | AFMT_STEREO, 0},
{0, 0, 0, 0},
};
static kobj_method_t feeder_rate_methods[] = {
KOBJMETHOD(feeder_init, feed_rate_init),
KOBJMETHOD(feeder_free, feed_rate_free),
KOBJMETHOD(feeder_set, feed_rate_set),
KOBJMETHOD(feeder_get, feed_rate_get),
KOBJMETHOD(feeder_feed, feed_rate),
{0, 0}
};
FEEDER_DECLARE(feeder_rate, 2, NULL);
|