Annotation of src/usr.bin/sndiod/dsp.c, Revision 1.13
1.13 ! ratchov 1: /* $OpenBSD: dsp.c,v 1.12 2016/10/27 04:37:47 ratchov Exp $ */
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:
1.10 ratchov 155: par->msb = msb;
1.1 ratchov 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: */
1.13 ! ratchov 203: void
1.1 ratchov 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: int s, ds, diff;
210: adata_t *odata;
211: unsigned int iblksz;
212: unsigned int c;
213: adata_t *ctxbuf, *ctx;
214: unsigned int ctx_start;
215:
1.13 ! ratchov 216: #ifdef DEBUG
! 217: if (todo % p->iblksz != 0) {
! 218: log_puts("resamp_do: partial blocks not supported\n");
! 219: panic();
! 220: }
! 221: #endif
! 222:
1.1 ratchov 223: /*
224: * Partially copy structures into local variables, to avoid
225: * unnecessary indirections; this also allows the compiler to
226: * order local variables more "cache-friendly".
227: */
228: idata = in;
229: odata = out;
1.13 ! ratchov 230: diff = p->oblksz;
1.1 ratchov 231: iblksz = p->iblksz;
232: oblksz = p->oblksz;
233: ctxbuf = p->ctx;
234: ctx_start = p->ctx_start;
235: nch = p->nch;
236:
237: for (;;) {
1.13 ! ratchov 238: if (diff >= oblksz) {
! 239: if (todo == 0)
1.1 ratchov 240: break;
241: ctx_start ^= 1;
242: ctx = ctxbuf + ctx_start;
243: for (c = nch; c > 0; c--) {
244: *ctx = *idata++;
245: ctx += RESAMP_NCTX;
246: }
1.13 ! ratchov 247: diff -= oblksz;
! 248: todo--;
! 249: } else {
1.1 ratchov 250: ctx = ctxbuf;
251: for (c = nch; c > 0; c--) {
1.13 ! ratchov 252: s = ctx[ctx_start ^ 1];
! 253: ds = ctx[ctx_start] - s;
1.1 ratchov 254: ctx += RESAMP_NCTX;
255: *odata++ = s + ADATA_MULDIV(ds, diff, oblksz);
256: }
1.13 ! ratchov 257: diff += iblksz;
1.1 ratchov 258: }
259: }
1.13 ! ratchov 260:
1.1 ratchov 261: p->ctx_start = ctx_start;
262: }
263:
264: /*
265: * initialize resampler with ibufsz/obufsz factor and "nch" channels
266: */
267: void
1.9 ratchov 268: resamp_init(struct resamp *p, unsigned int iblksz,
269: unsigned int oblksz, int nch)
1.1 ratchov 270: {
271: unsigned int i;
272:
273: p->iblksz = iblksz;
274: p->oblksz = oblksz;
275: p->nch = nch;
276: p->ctx_start = 0;
277: for (i = 0; i < NCHAN_MAX * RESAMP_NCTX; i++)
278: p->ctx[i] = 0;
279: #ifdef DEBUG
280: if (log_level >= 3) {
281: log_puts("resamp: ");
282: log_putu(iblksz);
283: log_puts("/");
284: log_putu(oblksz);
285: log_puts("\n");
286: }
287: #endif
288: }
289:
290: /*
291: * encode "todo" frames from native to foreign encoding
292: */
293: void
294: enc_do(struct conv *p, unsigned char *in, unsigned char *out, int todo)
295: {
296: unsigned int f;
297: adata_t *idata;
1.8 ratchov 298: unsigned int s;
1.1 ratchov 299: unsigned int oshift;
1.8 ratchov 300: unsigned int obias;
1.1 ratchov 301: unsigned int obps;
302: unsigned int i;
303: unsigned char *odata;
304: int obnext;
305: int osnext;
306:
307: #ifdef DEBUG
308: if (log_level >= 4) {
309: log_puts("enc: copying ");
310: log_putu(todo);
311: log_puts(" frames\n");
312: }
313: #endif
314: /*
315: * Partially copy structures into local variables, to avoid
316: * unnecessary indirections; this also allows the compiler to
317: * order local variables more "cache-friendly".
318: */
319: idata = (adata_t *)in;
320: odata = out;
321: oshift = p->shift;
1.8 ratchov 322: obias = p->bias;
1.1 ratchov 323: obps = p->bps;
324: obnext = p->bnext;
325: osnext = p->snext;
326:
327: /*
328: * Start conversion.
329: */
330: odata += p->bfirst;
331: for (f = todo * p->nch; f > 0; f--) {
1.8 ratchov 332: /* convert adata to u32 */
333: s = (int)*idata++ + ADATA_UNIT;
1.1 ratchov 334: s <<= 32 - ADATA_BITS;
1.8 ratchov 335: /* convert u32 to uN */
1.1 ratchov 336: s >>= oshift;
1.8 ratchov 337: /* convert uN to sN */
338: s -= obias;
339: /* packetize sN */
1.1 ratchov 340: for (i = obps; i > 0; i--) {
341: *odata = (unsigned char)s;
342: s >>= 8;
343: odata += obnext;
344: }
345: odata += osnext;
346: }
347: }
348:
349: /*
350: * store "todo" frames of silence in foreign encoding
351: */
352: void
353: enc_sil_do(struct conv *p, unsigned char *out, int todo)
354: {
355: unsigned int f;
1.8 ratchov 356: unsigned int s;
357: unsigned int oshift;
358: int obias;
1.1 ratchov 359: unsigned int obps;
360: unsigned int i;
361: unsigned char *odata;
362: int obnext;
363: int osnext;
364:
365: #ifdef DEBUG
366: if (log_level >= 4) {
367: log_puts("enc: silence ");
368: log_putu(todo);
369: log_puts(" frames\n");
370: }
371: #endif
372: /*
373: * Partially copy structures into local variables, to avoid
374: * unnecessary indirections; this also allows the compiler to
375: * order local variables more "cache-friendly".
376: */
377: odata = out;
1.8 ratchov 378: oshift = p->shift;
379: obias = p->bias;
1.1 ratchov 380: obps = p->bps;
381: obnext = p->bnext;
382: osnext = p->snext;
383:
384: /*
385: * Start conversion.
386: */
387: odata += p->bfirst;
388: for (f = todo * p->nch; f > 0; f--) {
1.8 ratchov 389: s = ((1U << 31) >> oshift) - obias;
1.1 ratchov 390: for (i = obps; i > 0; i--) {
391: *odata = (unsigned char)s;
392: s >>= 8;
393: odata += obnext;
394: }
395: odata += osnext;
396: }
397: }
398:
399: /*
400: * initialize encoder from native to foreign encoding
401: */
402: void
403: enc_init(struct conv *p, struct aparams *par, int nch)
404: {
405: p->nch = nch;
406: p->bps = par->bps;
407: if (par->msb) {
408: p->shift = 32 - par->bps * 8;
409: } else {
410: p->shift = 32 - par->bits;
411: }
1.8 ratchov 412: if (par->sig) {
413: p->bias = (1U << 31) >> p->shift;
414: } else {
415: p->bias = 0;
1.9 ratchov 416: }
1.1 ratchov 417: if (!par->le) {
418: p->bfirst = par->bps - 1;
419: p->bnext = -1;
420: p->snext = 2 * par->bps;
421: } else {
422: p->bfirst = 0;
423: p->bnext = 1;
424: p->snext = 0;
425: }
426: #ifdef DEBUG
427: if (log_level >= 3) {
428: log_puts("enc: ");
429: aparams_log(par);
430: log_puts(", ");
431: log_puti(p->nch);
432: log_puts(" channels\n");
433: }
434: #endif
435: }
436:
437: /*
1.12 ratchov 438: * decode "todo" frames from foreign to native encoding
1.1 ratchov 439: */
440: void
441: dec_do(struct conv *p, unsigned char *in, unsigned char *out, int todo)
442: {
443: unsigned int f;
444: unsigned int ibps;
445: unsigned int i;
1.8 ratchov 446: unsigned int s = 0xdeadbeef;
1.1 ratchov 447: unsigned char *idata;
448: int ibnext;
449: int isnext;
1.8 ratchov 450: unsigned int ibias;
1.1 ratchov 451: unsigned int ishift;
452: adata_t *odata;
453:
454: #ifdef DEBUG
455: if (log_level >= 4) {
456: log_puts("dec: copying ");
457: log_putu(todo);
458: log_puts(" frames\n");
459: }
460: #endif
461: /*
462: * Partially copy structures into local variables, to avoid
463: * unnecessary indirections; this also allows the compiler to
464: * order local variables more "cache-friendly".
465: */
466: idata = in;
467: odata = (adata_t *)out;
468: ibps = p->bps;
469: ibnext = p->bnext;
1.8 ratchov 470: ibias = p->bias;
1.1 ratchov 471: ishift = p->shift;
472: isnext = p->snext;
473:
474: /*
475: * Start conversion.
476: */
477: idata += p->bfirst;
478: for (f = todo * p->nch; f > 0; f--) {
479: for (i = ibps; i > 0; i--) {
480: s <<= 8;
481: s |= *idata;
482: idata += ibnext;
483: }
484: idata += isnext;
1.8 ratchov 485: s += ibias;
1.1 ratchov 486: s <<= ishift;
487: s >>= 32 - ADATA_BITS;
1.8 ratchov 488: *odata++ = s - ADATA_UNIT;
1.1 ratchov 489: }
490: }
491:
492: /*
493: * initialize decoder from foreign to native encoding
494: */
495: void
496: dec_init(struct conv *p, struct aparams *par, int nch)
497: {
498: p->bps = par->bps;
499: p->nch = nch;
500: if (par->msb) {
501: p->shift = 32 - par->bps * 8;
502: } else {
503: p->shift = 32 - par->bits;
504: }
1.8 ratchov 505: if (par->sig) {
506: p->bias = (1U << 31) >> p->shift;
507: } else {
508: p->bias = 0;
1.9 ratchov 509: }
1.1 ratchov 510: if (par->le) {
511: p->bfirst = par->bps - 1;
512: p->bnext = -1;
513: p->snext = 2 * par->bps;
514: } else {
515: p->bfirst = 0;
516: p->bnext = 1;
517: p->snext = 0;
518: }
519: #ifdef DEBUG
520: if (log_level >= 3) {
521: log_puts("dec: ");
522: aparams_log(par);
523: log_puts(", ");
524: log_puti(p->nch);
525: log_puts(" channels\n");
526: }
527: #endif
528: }
529:
530: /*
531: * mix "todo" input frames on the output with the given volume
532: */
533: void
534: cmap_add(struct cmap *p, void *in, void *out, int vol, int todo)
535: {
536: adata_t *idata, *odata;
537: int i, j, nch, istart, inext, onext, ostart, y, v;
538:
539: #ifdef DEBUG
540: if (log_level >= 4) {
541: log_puts("cmap: adding ");
542: log_puti(todo);
543: log_puts(" frames\n");
544: }
545: #endif
546: idata = in;
547: odata = out;
548: ostart = p->ostart;
549: onext = p->onext;
550: istart = p->istart;
551: inext = p->inext;
552: nch = p->nch;
1.2 ratchov 553: v = vol;
1.1 ratchov 554:
555: /*
556: * map/mix input on the output
557: */
558: for (i = todo; i > 0; i--) {
559: odata += ostart;
560: idata += istart;
561: for (j = nch; j > 0; j--) {
562: y = *odata + ADATA_MUL(*idata, v);
563: if (y >= ADATA_UNIT)
564: y = ADATA_UNIT - 1;
565: else if (y < -ADATA_UNIT)
566: y = -ADATA_UNIT;
567: *odata = y;
568: idata++;
569: odata++;
570: }
571: odata += onext;
572: idata += inext;
573: }
574: }
575:
576: /*
1.12 ratchov 577: * overwrite output with "todo" input frames with the given volume
1.1 ratchov 578: */
579: void
580: cmap_copy(struct cmap *p, void *in, void *out, int vol, int todo)
581: {
582: adata_t *idata, *odata;
583: int i, j, nch, istart, inext, onext, ostart, v;
584:
585: #ifdef DEBUG
586: if (log_level >= 4) {
587: log_puts("cmap: copying ");
588: log_puti(todo);
589: log_puts(" frames\n");
590: }
591: #endif
592: idata = in;
593: odata = out;
594: ostart = p->ostart;
595: onext = p->onext;
596: istart = p->istart;
597: inext = p->inext;
598: nch = p->nch;
599: v = vol;
600:
601: /*
602: * copy to the output buffer
603: */
604: for (i = todo; i > 0; i--) {
605: idata += istart;
1.4 ratchov 606: odata += ostart;
1.1 ratchov 607: for (j = nch; j > 0; j--) {
608: *odata = ADATA_MUL(*idata, v);
609: odata++;
610: idata++;
611: }
1.4 ratchov 612: odata += onext;
1.1 ratchov 613: idata += inext;
614: }
615: }
616:
617: /*
618: * initialize channel mapper, to map a subset of input channel range
619: * into a subset of the output channel range
620: */
621: void
622: cmap_init(struct cmap *p,
623: int imin, int imax, int isubmin, int isubmax,
624: int omin, int omax, int osubmin, int osubmax)
625: {
626: int cmin, cmax;
627:
628: cmin = -NCHAN_MAX;
629: if (osubmin > cmin)
630: cmin = osubmin;
631: if (omin > cmin)
632: cmin = omin;
633: if (isubmin > cmin)
634: cmin = isubmin;
635: if (imin > cmin)
636: cmin = imin;
637:
638: cmax = NCHAN_MAX;
639: if (osubmax < cmax)
640: cmax = osubmax;
641: if (omax < cmax)
642: cmax = omax;
643: if (isubmax < cmax)
644: cmax = isubmax;
645: if (imax < cmax)
646: cmax = imax;
647:
648: p->ostart = cmin - omin;
649: p->onext = omax - cmax;
650: p->istart = cmin - imin;
651: p->inext = imax - cmax;
652: p->nch = cmax - cmin + 1;
653: #ifdef DEBUG
654: if (log_level >= 3) {
655: log_puts("cmap: nch = ");
656: log_puti(p->nch);
657: log_puts(", ostart = ");
658: log_puti(p->ostart);
659: log_puts(", onext = ");
660: log_puti(p->onext);
661: log_puts(", istart = ");
662: log_puti(p->istart);
1.6 ratchov 663: log_puts(", inext = ");
1.1 ratchov 664: log_puti(p->inext);
665: log_puts("\n");
666: }
667: #endif
668: }