machete-technique at its finest: rather than think about merge functions, just remove...
[jack.git] / drivers / alsa / memops.c
blobb6543454d236afd158802130717d3ae41d070fdc
1 /*
2 Copyright (C) 2000 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #define _ISOC9X_SOURCE 1
21 #define _ISOC99_SOURCE 1
23 #define __USE_ISOC9X 1
24 #define __USE_ISOC99 1
26 #include <stdio.h>
27 #include <string.h>
28 #include <math.h>
29 #include <memory.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <limits.h>
33 #include <endian.h>
35 #include <jack/memops.h>
37 /* Notes about these *_SCALING values.
39 the MAX_<N>BIT values are floating point. when multiplied by
40 a full-scale normalized floating point sample value (-1.0..+1.0)
41 they should give the maxium value representable with an integer
42 sample type of N bits. Note that this is asymmetric. Sample ranges
43 for signed integer, 2's complement values are -(2^(N-1) to +(2^(N-1)-1)
45 Complications
46 -------------
47 If we use +2^(N-1) for the scaling factors, we run into a problem:
49 if we start with a normalized float value of -1.0, scaling
50 to 24 bits would give -8388608 (-2^23), which is ideal.
51 But with +1.0, we get +8388608, which is technically out of range.
53 We never multiply a full range normalized value by this constant,
54 but we could multiply it by a positive value that is close enough to +1.0
55 to produce a value > +(2^(N-1)-1.
57 There is no way around this paradox without wasting CPU cycles to determine
58 which scaling factor to use (i.e. determine if its negative or not,
59 use the right factor).
61 So, for now (October 2008) we use 2^(N-1)-1 as the scaling factor.
64 #define SAMPLE_24BIT_SCALING 8388607.0f
65 #define SAMPLE_16BIT_SCALING 32767.0f
67 /* these are just values to use if the floating point value was out of range
69 advice from Fons Adriaensen: make the limits symmetrical
72 #define SAMPLE_24BIT_MAX 8388607
73 #define SAMPLE_24BIT_MIN -8388607
75 #define SAMPLE_16BIT_MAX 32767
76 #define SAMPLE_16BIT_MIN -32767
78 /* these mark the outer edges of the range considered "within" range
79 for a floating point sample value. values outside (and on the boundaries)
80 of this range will be clipped before conversion; values within this
81 range will be scaled to appropriate values for the target sample
82 type.
85 #define NORMALIZED_FLOAT_MIN -1.0f
86 #define NORMALIZED_FLOAT_MAX 1.0f
88 /* define this in case we end up on a platform that is missing
89 the real lrintf functions
92 #define f_round(f) lrintf(f)
94 /* Linear Congruential noise generator. From the music-dsp list
95 * less random than rand(), but good enough and 10x faster
98 inline unsigned int fast_rand() {
99 static unsigned int seed = 22222;
100 seed = (seed * 96314165) + 907633515;
102 return seed;
106 /* functions for native float sample data */
108 void sample_move_floatLE_sSs (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip) {
109 while (nsamples--) {
110 *dst = *((float *) src);
111 dst++;
112 src += src_skip;
116 void sample_move_dS_floatLE (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state) {
117 while (nsamples--) {
118 *((float *) dst) = *src;
119 dst += dst_skip;
120 src++;
124 /* NOTES on function naming:
126 foo_bar_d<TYPE>_s<TYPE>
128 the "d<TYPE>" component defines the destination type for the operation
129 the "s<TYPE>" component defines the source type for the operation
131 TYPE can be one of:
133 S - sample is a jack_default_audio_sample_t, currently (October 2008) a 32 bit floating point value
134 Ss - like S but reverse endian from the host CPU
135 32u24 - sample is an signed 32 bit integer value, but data is in upper 24 bits only
136 32u24s - like 32u24 but reverse endian from the host CPU
137 24 - sample is an signed 24 bit integer value
138 24s - like 24 but reverse endian from the host CPU
139 16 - sample is an signed 16 bit integer value
140 16s - like 16 but reverse endian from the host CPU
142 For obvious reasons, the reverse endian versions only show as source types.
144 This covers all known sample formats at 16 bits or larger.
147 /* functions for native integer sample data */
149 void sample_move_d32u24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
151 int32_t z;
153 while (nsamples--) {
154 if (*src <= NORMALIZED_FLOAT_MIN) {
155 z = SAMPLE_24BIT_MIN;
156 } else if (*src >= NORMALIZED_FLOAT_MAX) {
157 z = SAMPLE_24BIT_MAX;
158 } else {
159 z = f_round (*src * SAMPLE_24BIT_SCALING) << 8;
161 #if __BYTE_ORDER == __LITTLE_ENDIAN
162 dst[0]=(char)(z>>24);
163 dst[1]=(char)(z>>16);
164 dst[2]=(char)(z>>8);
165 dst[3]=(char)(z);
166 #elif __BYTE_ORDER == __BIG_ENDIAN
167 dst[0]=(char)(z);
168 dst[1]=(char)(z>>8);
169 dst[2]=(char)(z>>16);
170 dst[3]=(char)(z>>24);
171 #endif
172 dst += dst_skip;
173 src++;
177 void sample_move_d32u24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
179 while (nsamples--) {
180 if (*src <= NORMALIZED_FLOAT_MIN) {
181 *((int32_t*) dst) = SAMPLE_24BIT_MIN;
182 } else if (*src >= NORMALIZED_FLOAT_MAX) {
183 *((int32_t*) dst) = SAMPLE_24BIT_MAX;
184 } else {
185 *((int32_t*) dst) = f_round (*src * SAMPLE_24BIT_SCALING) << 8;
187 dst += dst_skip;
188 src++;
192 void sample_move_dS_s32u24s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
194 /* ALERT: signed sign-extension portability !!! */
196 while (nsamples--) {
197 int x;
198 #if __BYTE_ORDER == __LITTLE_ENDIAN
199 x = (unsigned char)(src[0]);
200 x <<= 8;
201 x |= (unsigned char)(src[1]);
202 x <<= 8;
203 x |= (unsigned char)(src[2]);
204 x <<= 8;
205 x |= (unsigned char)(src[3]);
206 #elif __BYTE_ORDER == __BIG_ENDIAN
207 x = (unsigned char)(src[3]);
208 x <<= 8;
209 x |= (unsigned char)(src[2]);
210 x <<= 8;
211 x |= (unsigned char)(src[1]);
212 x <<= 8;
213 x |= (unsigned char)(src[0]);
214 #endif
215 *dst = (x >> 8) / SAMPLE_24BIT_SCALING;
216 dst++;
217 src += src_skip;
221 void sample_move_dS_s32u24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
223 /* ALERT: signed sign-extension portability !!! */
225 while (nsamples--) {
226 *dst = (*((int *) src) >> 8) / SAMPLE_24BIT_SCALING;
227 dst++;
228 src += src_skip;
232 void sample_move_d24_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
234 int32_t z;
236 while (nsamples--) {
237 if (*src <= NORMALIZED_FLOAT_MIN) {
238 z = SAMPLE_24BIT_MIN;
239 } else if (*src >= NORMALIZED_FLOAT_MAX) {
240 z = SAMPLE_24BIT_MAX;
241 } else {
242 z = (int32_t)f_round (*src * SAMPLE_24BIT_SCALING);
245 #if __BYTE_ORDER == __LITTLE_ENDIAN
246 dst[0]=(char)(z>>16);
247 dst[1]=(char)(z>>8);
248 dst[2]=(char)(z);
249 #elif __BYTE_ORDER == __BIG_ENDIAN
250 dst[0]=(char)(z);
251 dst[1]=(char)(z>>8);
252 dst[2]=(char)(z>>16);
253 #endif
254 dst += dst_skip;
255 src++;
259 void sample_move_d24_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
261 int32_t z;
263 while (nsamples--) {
264 if (*src <= NORMALIZED_FLOAT_MIN) {
265 z = SAMPLE_24BIT_MIN;
266 } else if (*src >= NORMALIZED_FLOAT_MAX) {
267 z = SAMPLE_24BIT_MAX;
268 } else {
269 z = (int32_t) f_round (*src * SAMPLE_24BIT_SCALING);
272 #if __BYTE_ORDER == __LITTLE_ENDIAN
273 memcpy (dst, &z, 3);
274 #elif __BYTE_ORDER == __BIG_ENDIAN
275 memcpy (dst, (char *)&z + 1, 3);
276 #endif
277 dst += dst_skip;
278 src++;
282 void sample_move_dS_s24s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
284 /* ALERT: signed sign-extension portability !!! */
286 while (nsamples--) {
287 int x;
288 #if __BYTE_ORDER == __LITTLE_ENDIAN
289 x = (unsigned char)(src[0]);
290 x <<= 8;
291 x |= (unsigned char)(src[1]);
292 x <<= 8;
293 x |= (unsigned char)(src[2]);
294 /* correct sign bit and the rest of the top byte */
295 if (src[0] & 0x80) {
296 x |= 0xff << 24;
298 #elif __BYTE_ORDER == __BIG_ENDIAN
299 x = (unsigned char)(src[2]);
300 x <<= 8;
301 x |= (unsigned char)(src[1]);
302 x <<= 8;
303 x |= (unsigned char)(src[0]);
304 /* correct sign bit and the rest of the top byte */
305 if (src[0] & 0x80) {
306 x |= 0xff << 24;
308 #endif
309 *dst = x / SAMPLE_24BIT_SCALING;
310 dst++;
311 src += src_skip;
315 void sample_move_dS_s24 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
317 /* ALERT: signed sign-extension portability !!! */
319 while (nsamples--) {
320 int x;
321 #if __BYTE_ORDER == __LITTLE_ENDIAN
322 memcpy((char*)&x + 1, src, 3);
323 #elif __BYTE_ORDER == __BIG_ENDIAN
324 memcpy(&x, src, 3);
325 #endif
326 x >>= 8;
327 *dst = x / SAMPLE_24BIT_SCALING;
328 dst++;
329 src += src_skip;
334 void sample_move_d16_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
336 int16_t tmp;
338 /* ALERT: signed sign-extension portability !!! */
340 while (nsamples--) {
341 if (*src <= NORMALIZED_FLOAT_MIN) {
342 tmp = SAMPLE_16BIT_MIN;
343 } else if (*src >= NORMALIZED_FLOAT_MAX) {
344 tmp = SAMPLE_16BIT_MAX;
345 } else {
346 tmp = (int16_t) f_round (*src * SAMPLE_16BIT_SCALING);
348 #if __BYTE_ORDER == __LITTLE_ENDIAN
349 dst[0]=(char)(tmp>>8);
350 dst[1]=(char)(tmp);
351 #elif __BYTE_ORDER == __BIG_ENDIAN
352 dst[0]=(char)(tmp);
353 dst[1]=(char)(tmp>>8);
354 #endif
355 dst += dst_skip;
356 src++;
360 void sample_move_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
362 /* ALERT: signed sign-extension portability !!! */
364 while (nsamples--) {
365 if (*src <= NORMALIZED_FLOAT_MIN) {
366 *((int16_t*) dst) = SAMPLE_16BIT_MIN;
367 } else if (*src >= NORMALIZED_FLOAT_MAX) {
368 *((int16_t*) dst) = SAMPLE_16BIT_MAX;
369 } else {
370 *((int16_t*) dst) = (int16_t) f_round (*src * SAMPLE_16BIT_SCALING);
372 dst += dst_skip;
373 src++;
377 void sample_move_dither_rect_d16_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
379 jack_default_audio_sample_t val;
380 int16_t tmp;
382 while (nsamples--) {
383 val = *src - (fast_rand()/((float)INT_MAX-1.0f));
384 if (val <= NORMALIZED_FLOAT_MIN) {
385 tmp = SAMPLE_16BIT_MIN;
386 } else if (val >= NORMALIZED_FLOAT_MAX) {
387 tmp = SAMPLE_16BIT_MAX;
388 } else {
389 tmp = (int16_t) f_round(*src * SAMPLE_16BIT_SCALING);
391 #if __BYTE_ORDER == __LITTLE_ENDIAN
392 dst[0]=(char)(tmp>>8);
393 dst[1]=(char)(tmp);
394 #elif __BYTE_ORDER == __BIG_ENDIAN
395 dst[0]=(char)(tmp);
396 dst[1]=(char)(tmp>>8);
397 #endif
398 dst += dst_skip;
399 src++;
403 void sample_move_dither_rect_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
405 jack_default_audio_sample_t val;
407 while (nsamples--) {
408 val = *src - (fast_rand()/((float)INT_MAX - 1.0f));
409 if (val <= NORMALIZED_FLOAT_MIN) {
410 *((int16_t*) dst) = SAMPLE_16BIT_MIN;
411 } else if (val >= NORMALIZED_FLOAT_MAX) {
412 *((int16_t*) dst) = SAMPLE_16BIT_MAX;
413 } else {
414 *((int16_t*) dst) = (int16_t) f_round(*src * SAMPLE_16BIT_SCALING);
416 dst += dst_skip;
417 src++;
421 void sample_move_dither_tri_d16_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
423 jack_default_audio_sample_t x;
424 float r;
425 float rm1 = state->rm1;
426 int16_t y;
428 while (nsamples--) {
429 x = *src * SAMPLE_16BIT_SCALING;
430 r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
431 x += r - rm1;
432 rm1 = r;
434 if (x <= NORMALIZED_FLOAT_MIN) {
435 y = SAMPLE_16BIT_MIN;
436 } else if (x >= NORMALIZED_FLOAT_MAX) {
437 y = SAMPLE_16BIT_MAX;
438 } else {
439 y = f_round(x);
442 #if __BYTE_ORDER == __LITTLE_ENDIAN
443 dst[0]=(char)(y>>8);
444 dst[1]=(char)(y);
445 #elif __BYTE_ORDER == __BIG_ENDIAN
446 dst[0]=(char)(y);
447 dst[1]=(char)(y>>8);
448 #endif
449 dst += dst_skip;
450 src++;
452 state->rm1 = rm1;
455 void sample_move_dither_tri_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
457 jack_default_audio_sample_t x;
458 float r;
459 float rm1 = state->rm1;
461 while (nsamples--) {
462 x = *src * SAMPLE_16BIT_SCALING;
463 r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
464 x += r - rm1;
465 rm1 = r;
467 if (x <= NORMALIZED_FLOAT_MIN) {
468 *((int16_t*)dst) = SAMPLE_16BIT_MIN;
469 } else if (x >= NORMALIZED_FLOAT_MAX) {
470 *((int16_t*)dst) = SAMPLE_16BIT_MAX;
471 } else {
472 *((int16_t*)dst) = (int16_t) f_round (x * SAMPLE_16BIT_SCALING);
475 dst += dst_skip;
476 src++;
478 state->rm1 = rm1;
481 void sample_move_dither_shaped_d16_sSs (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
483 jack_default_audio_sample_t x;
484 jack_default_audio_sample_t xe; /* the innput sample - filtered error */
485 jack_default_audio_sample_t xp; /* x' */
486 float r;
487 float rm1 = state->rm1;
488 unsigned int idx = state->idx;
489 int16_t tmp;
491 while (nsamples--) {
492 x = *src * SAMPLE_16BIT_SCALING;
493 r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
494 /* Filter the error with Lipshitz's minimally audible FIR:
495 [2.033 -2.165 1.959 -1.590 0.6149] */
496 xe = x
497 - state->e[idx] * 2.033f
498 + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
499 - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
500 + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
501 - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
502 xp = xe + r - rm1;
503 rm1 = r;
505 if (xp <= NORMALIZED_FLOAT_MIN) {
506 tmp = SAMPLE_16BIT_MIN;
507 } else if (xp >= NORMALIZED_FLOAT_MAX) {
508 tmp = SAMPLE_16BIT_MAX;
509 } else {
510 tmp = (int16_t) f_round (xp * SAMPLE_16BIT_SCALING);
513 /* Intrinsic z^-1 delay */
514 idx = (idx + 1) & DITHER_BUF_MASK;
515 state->e[idx] = xp - xe;
517 #if __BYTE_ORDER == __LITTLE_ENDIAN
518 dst[0]=(char)(tmp>>8);
519 dst[1]=(char)(tmp);
520 #elif __BYTE_ORDER == __BIG_ENDIAN
521 dst[0]=(char)(tmp);
522 dst[1]=(char)(tmp>>8);
523 #endif
524 dst += dst_skip;
525 src++;
527 state->rm1 = rm1;
528 state->idx = idx;
531 void sample_move_dither_shaped_d16_sS (char *dst, jack_default_audio_sample_t *src, unsigned long nsamples, unsigned long dst_skip, dither_state_t *state)
533 jack_default_audio_sample_t x;
534 jack_default_audio_sample_t xe; /* the innput sample - filtered error */
535 jack_default_audio_sample_t xp; /* x' */
536 float r;
537 float rm1 = state->rm1;
538 unsigned int idx = state->idx;
540 while (nsamples--) {
541 x = *src * SAMPLE_16BIT_SCALING;
542 r = 2.0f * (float)fast_rand() / (float)INT_MAX - 1.0f;
543 /* Filter the error with Lipshitz's minimally audible FIR:
544 [2.033 -2.165 1.959 -1.590 0.6149] */
545 xe = x
546 - state->e[idx] * 2.033f
547 + state->e[(idx - 1) & DITHER_BUF_MASK] * 2.165f
548 - state->e[(idx - 2) & DITHER_BUF_MASK] * 1.959f
549 + state->e[(idx - 3) & DITHER_BUF_MASK] * 1.590f
550 - state->e[(idx - 4) & DITHER_BUF_MASK] * 0.6149f;
551 xp = xe + r - rm1;
552 rm1 = r;
554 if (xp <= NORMALIZED_FLOAT_MIN) {
555 *((int16_t*) dst) = SAMPLE_16BIT_MIN;
556 } else if (xp >= NORMALIZED_FLOAT_MIN) {
557 *((int16_t*) dst) = SAMPLE_16BIT_MAX;
558 } else {
559 *((int16_t*) dst) = (int16_t) f_round (*src * SAMPLE_16BIT_SCALING);
562 /* Intrinsic z^-1 delay */
563 idx = (idx + 1) & DITHER_BUF_MASK;
564 state->e[idx] = *((int16_t*) dst) - xe;
566 dst += dst_skip;
567 src++;
569 state->rm1 = rm1;
570 state->idx = idx;
573 void sample_move_dS_s16s (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
575 short z;
577 /* ALERT: signed sign-extension portability !!! */
578 while (nsamples--) {
579 #if __BYTE_ORDER == __LITTLE_ENDIAN
580 z = (unsigned char)(src[0]);
581 z <<= 8;
582 z |= (unsigned char)(src[1]);
583 #elif __BYTE_ORDER == __BIG_ENDIAN
584 z = (unsigned char)(src[1]);
585 z <<= 8;
586 z |= (unsigned char)(src[0]);
587 #endif
588 *dst = z / SAMPLE_16BIT_SCALING;
589 dst++;
590 src += src_skip;
594 void sample_move_dS_s16 (jack_default_audio_sample_t *dst, char *src, unsigned long nsamples, unsigned long src_skip)
597 /* ALERT: signed sign-extension portability !!! */
598 while (nsamples--) {
599 *dst = (*((short *) src)) / SAMPLE_16BIT_SCALING;
600 dst++;
601 src += src_skip;
605 void memset_interleave (char *dst, char val, unsigned long bytes,
606 unsigned long unit_bytes,
607 unsigned long skip_bytes)
609 switch (unit_bytes) {
610 case 1:
611 while (bytes--) {
612 *dst = val;
613 dst += skip_bytes;
615 break;
616 case 2:
617 while (bytes) {
618 *((short *) dst) = (short) val;
619 dst += skip_bytes;
620 bytes -= 2;
622 break;
623 case 4:
624 while (bytes) {
625 *((int *) dst) = (int) val;
626 dst += skip_bytes;
627 bytes -= 4;
629 break;
630 default:
631 while (bytes) {
632 memset(dst, val, unit_bytes);
633 dst += skip_bytes;
634 bytes -= unit_bytes;
636 break;
640 /* COPY FUNCTIONS: used to move data from an input channel to an
641 output channel. Note that we assume that the skip distance
642 is the same for both channels. This is completely fine
643 unless the input and output were on different audio interfaces that
644 were interleaved differently. We don't try to handle that.
647 void
648 memcpy_fake (char *dst, char *src, unsigned long src_bytes, unsigned long foo, unsigned long bar)
650 memcpy (dst, src, src_bytes);
653 void
654 memcpy_interleave_d16_s16 (char *dst, char *src, unsigned long src_bytes,
655 unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
657 while (src_bytes) {
658 *((short *) dst) = *((short *) src);
659 dst += dst_skip_bytes;
660 src += src_skip_bytes;
661 src_bytes -= 2;
665 void
666 memcpy_interleave_d24_s24 (char *dst, char *src, unsigned long src_bytes,
667 unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
670 while (src_bytes) {
671 memcpy(dst, src, 3);
672 dst += dst_skip_bytes;
673 src += src_skip_bytes;
674 src_bytes -= 3;
678 void
679 memcpy_interleave_d32_s32 (char *dst, char *src, unsigned long src_bytes,
680 unsigned long dst_skip_bytes, unsigned long src_skip_bytes)
683 while (src_bytes) {
684 *((int *) dst) = *((int *) src);
685 dst += dst_skip_bytes;
686 src += src_skip_bytes;
687 src_bytes -= 4;