Annotation of src/usr.bin/sndiod/dsp.c, Revision 1.8
1.8 ! ratchov 1: /* $OpenBSD$ */
1.1 ratchov 2: /*
3: * Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #include <string.h>
18: #include "dsp.h"
19: #include "utils.h"
20:
21: int aparams_ctltovol[128] = {
22: 0,
23: 256, 266, 276, 287, 299, 310, 323, 335,
24: 348, 362, 376, 391, 406, 422, 439, 456,
25: 474, 493, 512, 532, 553, 575, 597, 621,
26: 645, 670, 697, 724, 753, 782, 813, 845,
27: 878, 912, 948, 985, 1024, 1064, 1106, 1149,
28: 1195, 1241, 1290, 1341, 1393, 1448, 1505, 1564,
29: 1625, 1689, 1756, 1825, 1896, 1971, 2048, 2128,
30: 2212, 2299, 2389, 2483, 2580, 2682, 2787, 2896,
31: 3010, 3128, 3251, 3379, 3511, 3649, 3792, 3941,
32: 4096, 4257, 4424, 4598, 4778, 4966, 5161, 5363,
33: 5574, 5793, 6020, 6256, 6502, 6757, 7023, 7298,
34: 7585, 7883, 8192, 8514, 8848, 9195, 9556, 9931,
35: 10321, 10726, 11148, 11585, 12040, 12513, 13004, 13515,
36: 14045, 14596, 15170, 15765, 16384, 17027, 17696, 18390,
37: 19112, 19863, 20643, 21453, 22295, 23170, 24080, 25025,
38: 26008, 27029, 28090, 29193, 30339, 31530, 32768
39: };
40:
41: /*
42: * Generate a string corresponding to the encoding in par,
43: * return the length of the resulting string.
44: */
45: int
46: aparams_enctostr(struct aparams *par, char *ostr)
47: {
48: char *p = ostr;
49:
50: *p++ = par->sig ? 's' : 'u';
51: if (par->bits > 9)
52: *p++ = '0' + par->bits / 10;
53: *p++ = '0' + par->bits % 10;
54: if (par->bps > 1) {
55: *p++ = par->le ? 'l' : 'b';
56: *p++ = 'e';
57: if (par->bps != APARAMS_BPS(par->bits) ||
58: par->bits < par->bps * 8) {
59: *p++ = par->bps + '0';
60: if (par->bits < par->bps * 8) {
61: *p++ = par->msb ? 'm' : 'l';
62: *p++ = 's';
63: *p++ = 'b';
64: }
65: }
66: }
67: *p++ = '\0';
68: return p - ostr - 1;
69: }
70:
71: /*
72: * Parse an encoding string, examples: s8, u8, s16, s16le, s24be ...
73: * set *istr to the char following the encoding. Return the number
74: * of bytes consumed.
75: */
76: int
77: aparams_strtoenc(struct aparams *par, char *istr)
78: {
79: char *p = istr;
80: int i, sig, bits, le, bps, msb;
81:
82: #define IS_SEP(c) \
83: (((c) < 'a' || (c) > 'z') && \
84: ((c) < 'A' || (c) > 'Z') && \
85: ((c) < '0' || (c) > '9'))
86:
87: /*
88: * get signedness
89: */
90: if (*p == 's') {
91: sig = 1;
92: } else if (*p == 'u') {
93: sig = 0;
94: } else
95: return 0;
96: p++;
97:
98: /*
99: * get number of bits per sample
100: */
101: bits = 0;
102: for (i = 0; i < 2; i++) {
103: if (*p < '0' || *p > '9')
104: break;
105: bits = (bits * 10) + *p - '0';
106: p++;
107: }
108: if (bits < BITS_MIN || bits > BITS_MAX)
109: return 0;
110: bps = APARAMS_BPS(bits);
111: msb = 1;
112: le = ADATA_LE;
113:
114: /*
115: * get (optional) endianness
116: */
117: if (p[0] == 'l' && p[1] == 'e') {
118: le = 1;
119: p += 2;
120: } else if (p[0] == 'b' && p[1] == 'e') {
121: le = 0;
122: p += 2;
123: } else if (IS_SEP(*p)) {
124: goto done;
125: } else
126: return 0;
127:
128: /*
129: * get (optional) number of bytes
130: */
131: if (*p >= '0' && *p <= '9') {
132: bps = *p - '0';
133: if (bps < (bits + 7) / 8 ||
134: bps > (BITS_MAX + 7) / 8)
135: return 0;
136: p++;
137:
138: /*
1.7 nicm 139: * get (optional) alignment
1.1 ratchov 140: */
141: if (p[0] == 'm' && p[1] == 's' && p[2] == 'b') {
142: msb = 1;
143: p += 3;
144: } else if (p[0] == 'l' && p[1] == 's' && p[2] == 'b') {
145: msb = 0;
146: p += 3;
147: } else if (IS_SEP(*p)) {
148: goto done;
149: } else
150: return 0;
151: } else if (!IS_SEP(*p))
152: return 0;
153:
154: done:
155: par->msb = msb;
156: par->sig = sig;
157: par->bits = bits;
158: par->bps = bps;
159: par->le = le;
160: return p - istr;
161: }
162:
163: /*
164: * Initialise parameters structure with the defaults natively supported
165: * by the machine.
166: */
167: void
168: aparams_init(struct aparams *par)
169: {
170: par->bps = sizeof(adata_t);
171: par->bits = ADATA_BITS;
172: par->le = ADATA_LE;
173: par->sig = 1;
174: par->msb = 0;
175: }
176:
177: /*
178: * log the given format/channels/encoding
179: */
180: void
181: aparams_log(struct aparams *par)
182: {
183: char enc[ENCMAX];
184:
185: aparams_enctostr(par, enc);
186: log_puts(enc);
187: }
188:
189: /*
190: * return true if encoding corresponds to what we store in adata_t
191: */
192: int
193: aparams_native(struct aparams *par)
194: {
195: return par->bps == sizeof(adata_t) && par->bits == ADATA_BITS &&
196: (par->bps == 1 || par->le == ADATA_LE) &&
197: (par->bits == par->bps * 8 || !par->msb);
198: }
199:
200: /*
201: * resample the given number of frames
202: */
203: int
204: resamp_do(struct resamp *p, adata_t *in, adata_t *out, int todo)
205: {
206: unsigned int nch;
207: adata_t *idata;
208: unsigned int oblksz;
209: unsigned int ifr;
210: int s, ds, diff;
211: adata_t *odata;
212: unsigned int iblksz;
213: unsigned int ofr;
214: unsigned int c;
215: adata_t *ctxbuf, *ctx;
216: unsigned int ctx_start;
217:
218: /*
219: * Partially copy structures into local variables, to avoid
220: * unnecessary indirections; this also allows the compiler to
221: * order local variables more "cache-friendly".
222: */
223: idata = in;
224: odata = out;
225: diff = p->diff;
226: iblksz = p->iblksz;
227: oblksz = p->oblksz;
228: ctxbuf = p->ctx;
229: ctx_start = p->ctx_start;
230: nch = p->nch;
231: ifr = todo;
232: ofr = oblksz;
233:
234: /*
235: * Start conversion.
236: */
237: #ifdef DEBUG
238: if (log_level >= 4) {
239: log_puts("resamp: copying ");
240: log_puti(todo);
241: log_puts(" frames, diff = ");
242: log_putu(diff);
243: log_puts("\n");
244: }
245: #endif
246: for (;;) {
247: if (diff < 0) {
248: if (ifr == 0)
249: break;
250: ctx_start ^= 1;
251: ctx = ctxbuf + ctx_start;
252: for (c = nch; c > 0; c--) {
253: *ctx = *idata++;
254: ctx += RESAMP_NCTX;
255: }
256: diff += oblksz;
257: ifr--;
258: } else if (diff > 0) {
259: if (ofr == 0)
260: break;
261: ctx = ctxbuf;
262: for (c = nch; c > 0; c--) {
263: s = ctx[ctx_start];
264: ds = ctx[ctx_start ^ 1] - s;
265: ctx += RESAMP_NCTX;
266: *odata++ = s + ADATA_MULDIV(ds, diff, oblksz);
267: }
268: diff -= iblksz;
269: ofr--;
270: } else {
271: if (ifr == 0 || ofr == 0)
272: break;
273: ctx = ctxbuf + ctx_start;
274: for (c = nch; c > 0; c--) {
275: *odata++ = *ctx;
276: ctx += RESAMP_NCTX;
277: }
278: ctx_start ^= 1;
279: ctx = ctxbuf + ctx_start;
280: for (c = nch; c > 0; c--) {
281: *ctx = *idata++;
282: ctx += RESAMP_NCTX;
283: }
284: diff -= iblksz;
285: diff += oblksz;
286: ifr--;
287: ofr--;
288: }
289: }
290: p->diff = diff;
291: p->ctx_start = ctx_start;
292: return oblksz - ofr;
293: }
294:
295: /*
296: * initialize resampler with ibufsz/obufsz factor and "nch" channels
297: */
298: void
299: resamp_init(struct resamp *p, unsigned int iblksz, unsigned int oblksz, int nch)
300: {
301: unsigned int i;
302:
303: p->iblksz = iblksz;
304: p->oblksz = oblksz;
305: p->diff = 0;
306: p->idelta = 0;
307: p->odelta = 0;
308: p->nch = nch;
309: p->ctx_start = 0;
310: for (i = 0; i < NCHAN_MAX * RESAMP_NCTX; i++)
311: p->ctx[i] = 0;
312: #ifdef DEBUG
313: if (log_level >= 3) {
314: log_puts("resamp: ");
315: log_putu(iblksz);
316: log_puts("/");
317: log_putu(oblksz);
318: log_puts("\n");
319: }
320: #endif
321: }
322:
323: /*
324: * encode "todo" frames from native to foreign encoding
325: */
326: void
327: enc_do(struct conv *p, unsigned char *in, unsigned char *out, int todo)
328: {
329: unsigned int f;
330: adata_t *idata;
1.8 ! ratchov 331: unsigned int s;
1.1 ratchov 332: unsigned int oshift;
1.8 ! ratchov 333: unsigned int obias;
1.1 ratchov 334: unsigned int obps;
335: unsigned int i;
336: unsigned char *odata;
337: int obnext;
338: int osnext;
339:
340: #ifdef DEBUG
341: if (log_level >= 4) {
342: log_puts("enc: copying ");
343: log_putu(todo);
344: log_puts(" frames\n");
345: }
346: #endif
347: /*
348: * Partially copy structures into local variables, to avoid
349: * unnecessary indirections; this also allows the compiler to
350: * order local variables more "cache-friendly".
351: */
352: idata = (adata_t *)in;
353: odata = out;
354: oshift = p->shift;
1.8 ! ratchov 355: obias = p->bias;
1.1 ratchov 356: obps = p->bps;
357: obnext = p->bnext;
358: osnext = p->snext;
359:
360: /*
361: * Start conversion.
362: */
363: odata += p->bfirst;
364: for (f = todo * p->nch; f > 0; f--) {
1.8 ! ratchov 365: /* convert adata to u32 */
! 366: s = (int)*idata++ + ADATA_UNIT;
1.1 ratchov 367: s <<= 32 - ADATA_BITS;
1.8 ! ratchov 368: /* convert u32 to uN */
1.1 ratchov 369: s >>= oshift;
1.8 ! ratchov 370: /* convert uN to sN */
! 371: s -= obias;
! 372: /* packetize sN */
1.1 ratchov 373: for (i = obps; i > 0; i--) {
374: *odata = (unsigned char)s;
375: s >>= 8;
376: odata += obnext;
377: }
378: odata += osnext;
379: }
380: }
381:
382: /*
383: * store "todo" frames of silence in foreign encoding
384: */
385: void
386: enc_sil_do(struct conv *p, unsigned char *out, int todo)
387: {
388: unsigned int f;
1.8 ! ratchov 389: unsigned int s;
! 390: unsigned int oshift;
! 391: int obias;
1.1 ratchov 392: unsigned int obps;
393: unsigned int i;
394: unsigned char *odata;
395: int obnext;
396: int osnext;
397:
398: #ifdef DEBUG
399: if (log_level >= 4) {
400: log_puts("enc: silence ");
401: log_putu(todo);
402: log_puts(" frames\n");
403: }
404: #endif
405: /*
406: * Partially copy structures into local variables, to avoid
407: * unnecessary indirections; this also allows the compiler to
408: * order local variables more "cache-friendly".
409: */
410: odata = out;
1.8 ! ratchov 411: oshift = p->shift;
! 412: obias = p->bias;
1.1 ratchov 413: obps = p->bps;
414: obnext = p->bnext;
415: osnext = p->snext;
416:
417: /*
418: * Start conversion.
419: */
420: odata += p->bfirst;
421: for (f = todo * p->nch; f > 0; f--) {
1.8 ! ratchov 422: s = ((1U << 31) >> oshift) - obias;
1.1 ratchov 423: for (i = obps; i > 0; i--) {
424: *odata = (unsigned char)s;
425: s >>= 8;
426: odata += obnext;
427: }
428: odata += osnext;
429: }
430: }
431:
432: /*
433: * initialize encoder from native to foreign encoding
434: */
435: void
436: enc_init(struct conv *p, struct aparams *par, int nch)
437: {
438: p->nch = nch;
439: p->bps = par->bps;
440: if (par->msb) {
441: p->shift = 32 - par->bps * 8;
442: } else {
443: p->shift = 32 - par->bits;
444: }
1.8 ! ratchov 445: if (par->sig) {
! 446: p->bias = (1U << 31) >> p->shift;
! 447: } else {
! 448: p->bias = 0;
! 449: }
1.1 ratchov 450: if (!par->le) {
451: p->bfirst = par->bps - 1;
452: p->bnext = -1;
453: p->snext = 2 * par->bps;
454: } else {
455: p->bfirst = 0;
456: p->bnext = 1;
457: p->snext = 0;
458: }
459: #ifdef DEBUG
460: if (log_level >= 3) {
461: log_puts("enc: ");
462: aparams_log(par);
463: log_puts(", ");
464: log_puti(p->nch);
465: log_puts(" channels\n");
466: }
467: #endif
468: }
469:
470: /*
471: * decode "todo" frames from from foreign to native encoding
472: */
473: void
474: dec_do(struct conv *p, unsigned char *in, unsigned char *out, int todo)
475: {
476: unsigned int f;
477: unsigned int ibps;
478: unsigned int i;
1.8 ! ratchov 479: unsigned int s = 0xdeadbeef;
1.1 ratchov 480: unsigned char *idata;
481: int ibnext;
482: int isnext;
1.8 ! ratchov 483: unsigned int ibias;
1.1 ratchov 484: unsigned int ishift;
485: adata_t *odata;
486:
487: #ifdef DEBUG
488: if (log_level >= 4) {
489: log_puts("dec: copying ");
490: log_putu(todo);
491: log_puts(" frames\n");
492: }
493: #endif
494: /*
495: * Partially copy structures into local variables, to avoid
496: * unnecessary indirections; this also allows the compiler to
497: * order local variables more "cache-friendly".
498: */
499: idata = in;
500: odata = (adata_t *)out;
501: ibps = p->bps;
502: ibnext = p->bnext;
1.8 ! ratchov 503: ibias = p->bias;
1.1 ratchov 504: ishift = p->shift;
505: isnext = p->snext;
506:
507: /*
508: * Start conversion.
509: */
510: idata += p->bfirst;
511: for (f = todo * p->nch; f > 0; f--) {
512: for (i = ibps; i > 0; i--) {
513: s <<= 8;
514: s |= *idata;
515: idata += ibnext;
516: }
517: idata += isnext;
1.8 ! ratchov 518: s += ibias;
1.1 ratchov 519: s <<= ishift;
520: s >>= 32 - ADATA_BITS;
1.8 ! ratchov 521: *odata++ = s - ADATA_UNIT;
1.1 ratchov 522: }
523: }
524:
525: /*
526: * initialize decoder from foreign to native encoding
527: */
528: void
529: dec_init(struct conv *p, struct aparams *par, int nch)
530: {
531: p->bps = par->bps;
532: p->nch = nch;
533: if (par->msb) {
534: p->shift = 32 - par->bps * 8;
535: } else {
536: p->shift = 32 - par->bits;
537: }
1.8 ! ratchov 538: if (par->sig) {
! 539: p->bias = (1U << 31) >> p->shift;
! 540: } else {
! 541: p->bias = 0;
! 542: }
1.1 ratchov 543: if (par->le) {
544: p->bfirst = par->bps - 1;
545: p->bnext = -1;
546: p->snext = 2 * par->bps;
547: } else {
548: p->bfirst = 0;
549: p->bnext = 1;
550: p->snext = 0;
551: }
552: #ifdef DEBUG
553: if (log_level >= 3) {
554: log_puts("dec: ");
555: aparams_log(par);
556: log_puts(", ");
557: log_puti(p->nch);
558: log_puts(" channels\n");
559: }
560: #endif
561: }
562:
563: /*
564: * mix "todo" input frames on the output with the given volume
565: */
566: void
567: cmap_add(struct cmap *p, void *in, void *out, int vol, int todo)
568: {
569: adata_t *idata, *odata;
570: int i, j, nch, istart, inext, onext, ostart, y, v;
571:
572: #ifdef DEBUG
573: if (log_level >= 4) {
574: log_puts("cmap: adding ");
575: log_puti(todo);
576: log_puts(" frames\n");
577: }
578: #endif
579: idata = in;
580: odata = out;
581: ostart = p->ostart;
582: onext = p->onext;
583: istart = p->istart;
584: inext = p->inext;
585: nch = p->nch;
1.2 ratchov 586: v = vol;
1.1 ratchov 587:
588: /*
589: * map/mix input on the output
590: */
591: for (i = todo; i > 0; i--) {
592: odata += ostart;
593: idata += istart;
594: for (j = nch; j > 0; j--) {
595: y = *odata + ADATA_MUL(*idata, v);
596: if (y >= ADATA_UNIT)
597: y = ADATA_UNIT - 1;
598: else if (y < -ADATA_UNIT)
599: y = -ADATA_UNIT;
600: *odata = y;
601: idata++;
602: odata++;
603: }
604: odata += onext;
605: idata += inext;
606: }
607: }
608:
609: /*
610: * overwrite output with "todo" input frames with with the given volume
611: */
612: void
613: cmap_copy(struct cmap *p, void *in, void *out, int vol, int todo)
614: {
615: adata_t *idata, *odata;
616: int i, j, nch, istart, inext, onext, ostart, v;
617:
618: #ifdef DEBUG
619: if (log_level >= 4) {
620: log_puts("cmap: copying ");
621: log_puti(todo);
622: log_puts(" frames\n");
623: }
624: #endif
625: idata = in;
626: odata = out;
627: ostart = p->ostart;
628: onext = p->onext;
629: istart = p->istart;
630: inext = p->inext;
631: nch = p->nch;
632: v = vol;
633:
634: /*
635: * copy to the output buffer
636: */
637: for (i = todo; i > 0; i--) {
638: idata += istart;
1.4 ratchov 639: odata += ostart;
1.1 ratchov 640: for (j = nch; j > 0; j--) {
641: *odata = ADATA_MUL(*idata, v);
642: odata++;
643: idata++;
644: }
1.4 ratchov 645: odata += onext;
1.1 ratchov 646: idata += inext;
647: }
648: }
649:
650: /*
651: * initialize channel mapper, to map a subset of input channel range
652: * into a subset of the output channel range
653: */
654: void
655: cmap_init(struct cmap *p,
656: int imin, int imax, int isubmin, int isubmax,
657: int omin, int omax, int osubmin, int osubmax)
658: {
659: int cmin, cmax;
660:
661: cmin = -NCHAN_MAX;
662: if (osubmin > cmin)
663: cmin = osubmin;
664: if (omin > cmin)
665: cmin = omin;
666: if (isubmin > cmin)
667: cmin = isubmin;
668: if (imin > cmin)
669: cmin = imin;
670:
671: cmax = NCHAN_MAX;
672: if (osubmax < cmax)
673: cmax = osubmax;
674: if (omax < cmax)
675: cmax = omax;
676: if (isubmax < cmax)
677: cmax = isubmax;
678: if (imax < cmax)
679: cmax = imax;
680:
681: p->ostart = cmin - omin;
682: p->onext = omax - cmax;
683: p->istart = cmin - imin;
684: p->inext = imax - cmax;
685: p->nch = cmax - cmin + 1;
686: #ifdef DEBUG
687: if (log_level >= 3) {
688: log_puts("cmap: nch = ");
689: log_puti(p->nch);
690: log_puts(", ostart = ");
691: log_puti(p->ostart);
692: log_puts(", onext = ");
693: log_puti(p->onext);
694: log_puts(", istart = ");
695: log_puti(p->istart);
1.6 ratchov 696: log_puts(", inext = ");
1.1 ratchov 697: log_puti(p->inext);
698: log_puts("\n");
699: }
700: #endif
701: }