Annotation of src/usr.bin/aucat/dsp.c, Revision 1.7
1.2 ratchov 1: /* $OpenBSD: dsp.c,v 1.1 2015/01/21 08:43:55 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: short dec_ulawmap[256] = {
42: -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
43: -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
44: -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
45: -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
46: -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
47: -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
48: -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
49: -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
50: -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
51: -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
52: -876, -844, -812, -780, -748, -716, -684, -652,
53: -620, -588, -556, -524, -492, -460, -428, -396,
54: -372, -356, -340, -324, -308, -292, -276, -260,
55: -244, -228, -212, -196, -180, -164, -148, -132,
56: -120, -112, -104, -96, -88, -80, -72, -64,
57: -56, -48, -40, -32, -24, -16, -8, 0,
58: 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
59: 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
60: 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
61: 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
62: 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
63: 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
64: 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
65: 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
66: 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
67: 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
68: 876, 844, 812, 780, 748, 716, 684, 652,
69: 620, 588, 556, 524, 492, 460, 428, 396,
70: 372, 356, 340, 324, 308, 292, 276, 260,
71: 244, 228, 212, 196, 180, 164, 148, 132,
72: 120, 112, 104, 96, 88, 80, 72, 64,
73: 56, 48, 40, 32, 24, 16, 8, 0
74: };
75:
76: short dec_alawmap[256] = {
77: -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
78: -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
79: -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
80: -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
81: -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
82: -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
83: -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
84: -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
85: -344, -328, -376, -360, -280, -264, -312, -296,
86: -472, -456, -504, -488, -408, -392, -440, -424,
87: -88, -72, -120, -104, -24, -8, -56, -40,
88: -216, -200, -248, -232, -152, -136, -184, -168,
89: -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
90: -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
91: -688, -656, -752, -720, -560, -528, -624, -592,
92: -944, -912, -1008, -976, -816, -784, -880, -848,
93: 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
94: 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
95: 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
96: 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
97: 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
98: 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
99: 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
100: 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
101: 344, 328, 376, 360, 280, 264, 312, 296,
102: 472, 456, 504, 488, 408, 392, 440, 424,
103: 88, 72, 120, 104, 24, 8, 56, 40,
104: 216, 200, 248, 232, 152, 136, 184, 168,
105: 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
106: 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
107: 688, 656, 752, 720, 560, 528, 624, 592,
108: 944, 912, 1008, 976, 816, 784, 880, 848
109: };
110:
111: /*
112: * Generate a string corresponding to the encoding in par,
113: * return the length of the resulting string.
114: */
115: int
116: aparams_enctostr(struct aparams *par, char *ostr)
117: {
118: char *p = ostr;
119:
120: *p++ = par->sig ? 's' : 'u';
121: if (par->bits > 9)
122: *p++ = '0' + par->bits / 10;
123: *p++ = '0' + par->bits % 10;
124: if (par->bps > 1) {
125: *p++ = par->le ? 'l' : 'b';
126: *p++ = 'e';
127: if (par->bps != APARAMS_BPS(par->bits) ||
128: par->bits < par->bps * 8) {
129: *p++ = par->bps + '0';
130: if (par->bits < par->bps * 8) {
131: *p++ = par->msb ? 'm' : 'l';
132: *p++ = 's';
133: *p++ = 'b';
134: }
135: }
136: }
137: *p++ = '\0';
138: return p - ostr - 1;
139: }
140:
141: /*
142: * Parse an encoding string, examples: s8, u8, s16, s16le, s24be ...
143: * set *istr to the char following the encoding. Return the number
144: * of bytes consumed.
145: */
146: int
147: aparams_strtoenc(struct aparams *par, char *istr)
148: {
149: char *p = istr;
150: int i, sig, bits, le, bps, msb;
151:
152: #define IS_SEP(c) \
153: (((c) < 'a' || (c) > 'z') && \
154: ((c) < 'A' || (c) > 'Z') && \
155: ((c) < '0' || (c) > '9'))
156:
157: /*
158: * get signedness
159: */
160: if (*p == 's') {
161: sig = 1;
162: } else if (*p == 'u') {
163: sig = 0;
164: } else
165: return 0;
166: p++;
167:
168: /*
169: * get number of bits per sample
170: */
171: bits = 0;
172: for (i = 0; i < 2; i++) {
173: if (*p < '0' || *p > '9')
174: break;
175: bits = (bits * 10) + *p - '0';
176: p++;
177: }
178: if (bits < BITS_MIN || bits > BITS_MAX)
179: return 0;
180: bps = APARAMS_BPS(bits);
181: msb = 1;
182: le = ADATA_LE;
183:
184: /*
185: * get (optional) endianness
186: */
187: if (p[0] == 'l' && p[1] == 'e') {
188: le = 1;
189: p += 2;
190: } else if (p[0] == 'b' && p[1] == 'e') {
191: le = 0;
192: p += 2;
193: } else if (IS_SEP(*p)) {
194: goto done;
195: } else
196: return 0;
197:
198: /*
199: * get (optional) number of bytes
200: */
201: if (*p >= '0' && *p <= '9') {
202: bps = *p - '0';
203: if (bps < (bits + 7) / 8 ||
204: bps > (BITS_MAX + 7) / 8)
205: return 0;
206: p++;
207:
208: /*
209: * get (optional) alignment
210: */
211: if (p[0] == 'm' && p[1] == 's' && p[2] == 'b') {
212: msb = 1;
213: p += 3;
214: } else if (p[0] == 'l' && p[1] == 's' && p[2] == 'b') {
215: msb = 0;
216: p += 3;
217: } else if (IS_SEP(*p)) {
218: goto done;
219: } else
220: return 0;
221: } else if (!IS_SEP(*p))
222: return 0;
223:
224: done:
1.3 ratchov 225: par->msb = msb;
1.1 ratchov 226: par->sig = sig;
227: par->bits = bits;
228: par->bps = bps;
229: par->le = le;
230: return p - istr;
231: }
232:
233: /*
234: * Initialise parameters structure with the defaults natively supported
235: * by the machine.
236: */
237: void
238: aparams_init(struct aparams *par)
239: {
240: par->bps = sizeof(adata_t);
241: par->bits = ADATA_BITS;
242: par->le = ADATA_LE;
243: par->sig = 1;
244: par->msb = 0;
245: }
246:
247: /*
248: * log the given format/channels/encoding
249: */
250: void
251: aparams_log(struct aparams *par)
252: {
253: char enc[ENCMAX];
254:
255: aparams_enctostr(par, enc);
256: log_puts(enc);
257: }
258:
259: /*
260: * return true if encoding corresponds to what we store in adata_t
261: */
262: int
263: aparams_native(struct aparams *par)
264: {
265: return par->bps == sizeof(adata_t) && par->bits == ADATA_BITS &&
266: (par->bps == 1 || par->le == ADATA_LE) &&
267: (par->bits == par->bps * 8 || !par->msb);
268: }
269:
270: /*
1.7 ! ratchov 271: * return the number of input and output frame that would
! 272: * be consumed
1.5 ratchov 273: */
1.7 ! ratchov 274: void
! 275: resamp_getcnt(struct resamp *p, int *icnt, int *ocnt)
1.5 ratchov 276: {
1.7 ! ratchov 277: int diff, ifr, ofr;
! 278:
! 279: diff = p->diff;
! 280: ifr = *icnt;
! 281: ofr = *ocnt;
1.5 ratchov 282:
1.7 ! ratchov 283: for (;;) {
! 284: if (diff < 0) {
! 285: if (ifr == 0)
! 286: break;
! 287: diff += p->oblksz;
! 288: ifr--;
! 289: } else if (diff > 0) {
! 290: if (ofr == 0)
! 291: break;
! 292: diff -= p->iblksz;
! 293: ofr--;
! 294: } else {
! 295: if (ifr == 0 || ofr == 0)
! 296: break;
! 297: diff -= p->iblksz;
! 298: diff += p->oblksz;
! 299: ifr--;
! 300: ofr--;
! 301: }
! 302: }
! 303: *icnt -= ifr;
! 304: *ocnt -= ofr;
1.5 ratchov 305: }
306:
307: /*
308: * Resample the given number of frames. The number of output frames
1.7 ! ratchov 309: * must match the coresponding number the input frames. Either always
! 310: * use icnt and ocnt such that:
1.5 ratchov 311: *
1.7 ! ratchov 312: * icnt * oblksz = ocnt * iblksz
1.5 ratchov 313: *
1.7 ! ratchov 314: * or use resamp_getcnt() to calculate the proper numbers.
1.1 ratchov 315: */
1.4 ratchov 316: void
1.5 ratchov 317: resamp_do(struct resamp *p, adata_t *in, adata_t *out, int icnt, int ocnt)
1.1 ratchov 318: {
319: unsigned int nch;
320: adata_t *idata;
321: unsigned int oblksz;
322: unsigned int ifr;
323: int s, ds, diff;
324: adata_t *odata;
325: unsigned int iblksz;
326: unsigned int ofr;
327: unsigned int c;
328: adata_t *ctxbuf, *ctx;
329: unsigned int ctx_start;
330:
331: /*
332: * Partially copy structures into local variables, to avoid
333: * unnecessary indirections; this also allows the compiler to
334: * order local variables more "cache-friendly".
335: */
336: idata = in;
337: odata = out;
338: diff = p->diff;
339: iblksz = p->iblksz;
340: oblksz = p->oblksz;
341: ctxbuf = p->ctx;
342: ctx_start = p->ctx_start;
343: nch = p->nch;
1.5 ratchov 344: ifr = icnt;
345: ofr = ocnt;
1.1 ratchov 346:
347: /*
348: * Start conversion.
349: */
350: #ifdef DEBUG
351: if (log_level >= 4) {
352: log_puts("resamp: copying ");
1.4 ratchov 353: log_puti(ifr);
1.5 ratchov 354: log_puts(" -> ");
355: log_putu(ofr);
1.1 ratchov 356: log_puts(" frames, diff = ");
1.5 ratchov 357: log_puti(diff);
1.1 ratchov 358: log_puts("\n");
359: }
360: #endif
361: for (;;) {
362: if (diff < 0) {
363: if (ifr == 0)
364: break;
365: ctx_start ^= 1;
366: ctx = ctxbuf + ctx_start;
367: for (c = nch; c > 0; c--) {
368: *ctx = *idata++;
369: ctx += RESAMP_NCTX;
370: }
371: diff += oblksz;
372: ifr--;
373: } else if (diff > 0) {
374: if (ofr == 0)
375: break;
376: ctx = ctxbuf;
377: for (c = nch; c > 0; c--) {
378: s = ctx[ctx_start];
379: ds = ctx[ctx_start ^ 1] - s;
380: ctx += RESAMP_NCTX;
381: *odata++ = s + ADATA_MULDIV(ds, diff, oblksz);
382: }
383: diff -= iblksz;
384: ofr--;
385: } else {
386: if (ifr == 0 || ofr == 0)
387: break;
388: ctx = ctxbuf + ctx_start;
389: for (c = nch; c > 0; c--) {
390: *odata++ = *ctx;
391: ctx += RESAMP_NCTX;
392: }
393: ctx_start ^= 1;
394: ctx = ctxbuf + ctx_start;
395: for (c = nch; c > 0; c--) {
396: *ctx = *idata++;
397: ctx += RESAMP_NCTX;
398: }
399: diff -= iblksz;
400: diff += oblksz;
401: ifr--;
402: ofr--;
403: }
404: }
405: p->diff = diff;
406: p->ctx_start = ctx_start;
1.5 ratchov 407: #ifdef DEBUG
408: if (ifr != 0) {
409: log_puts("resamp_do: ");
410: log_puti(ifr);
411: log_puts(": too many input frames\n");
412: panic();
413: }
414: if (ofr != 0) {
415: log_puts("resamp_do: ");
416: log_puti(ofr);
417: log_puts(": too many output frames\n");
418: panic();
419: }
420: #endif
1.1 ratchov 421: }
422:
1.6 ratchov 423: static unsigned int
424: uint_gcd(unsigned int a, unsigned int b)
425: {
426: unsigned int r;
427:
428: while (b > 0) {
429: r = a % b;
430: a = b;
431: b = r;
432: }
433: return a;
434: }
435:
1.1 ratchov 436: /*
437: * initialize resampler with ibufsz/obufsz factor and "nch" channels
438: */
439: void
1.3 ratchov 440: resamp_init(struct resamp *p, unsigned int iblksz,
441: unsigned int oblksz, int nch)
1.1 ratchov 442: {
1.6 ratchov 443: unsigned int i, g;
444:
445: /*
446: * reduice iblksz/oblksz fraction
447: */
448: g = uint_gcd(iblksz, oblksz);
449: iblksz /= g;
450: oblksz /= g;
451:
452: /*
453: * ensure weired rates dont cause integer overflows
454: */
455: while (iblksz > ADATA_UNIT || oblksz > ADATA_UNIT) {
456: iblksz >>= 1;
457: oblksz >>= 1;
458: }
1.1 ratchov 459:
460: p->iblksz = iblksz;
461: p->oblksz = oblksz;
462: p->diff = 0;
463: p->idelta = 0;
464: p->odelta = 0;
465: p->nch = nch;
466: p->ctx_start = 0;
467: for (i = 0; i < NCHAN_MAX * RESAMP_NCTX; i++)
468: p->ctx[i] = 0;
469: #ifdef DEBUG
470: if (log_level >= 3) {
471: log_puts("resamp: ");
472: log_putu(iblksz);
473: log_puts("/");
474: log_putu(oblksz);
475: log_puts("\n");
476: }
477: #endif
478: }
479:
480: /*
481: * encode "todo" frames from native to foreign encoding
482: */
483: void
484: enc_do(struct conv *p, unsigned char *in, unsigned char *out, int todo)
485: {
486: unsigned int f;
487: adata_t *idata;
488: unsigned int s;
489: unsigned int oshift;
490: unsigned int obias;
491: unsigned int obps;
492: unsigned int i;
493: unsigned char *odata;
494: int obnext;
495: int osnext;
496:
497: #ifdef DEBUG
498: if (log_level >= 4) {
499: log_puts("enc: copying ");
500: log_putu(todo);
501: log_puts(" frames\n");
502: }
503: #endif
504: /*
505: * Partially copy structures into local variables, to avoid
506: * unnecessary indirections; this also allows the compiler to
507: * order local variables more "cache-friendly".
508: */
509: idata = (adata_t *)in;
510: odata = out;
511: oshift = p->shift;
512: obias = p->bias;
513: obps = p->bps;
514: obnext = p->bnext;
515: osnext = p->snext;
516:
517: /*
518: * Start conversion.
519: */
520: odata += p->bfirst;
521: for (f = todo * p->nch; f > 0; f--) {
522: /* convert adata to u32 */
523: s = (int)*idata++ + ADATA_UNIT;
524: s <<= 32 - ADATA_BITS;
525: /* convert u32 to uN */
526: s >>= oshift;
527: /* convert uN to sN */
528: s -= obias;
529: /* packetize sN */
530: for (i = obps; i > 0; i--) {
531: *odata = (unsigned char)s;
532: s >>= 8;
533: odata += obnext;
534: }
535: odata += osnext;
536: }
537: }
538:
539: /*
540: * store "todo" frames of silence in foreign encoding
541: */
542: void
543: enc_sil_do(struct conv *p, unsigned char *out, int todo)
544: {
545: unsigned int f;
546: unsigned int s;
547: unsigned int oshift;
548: int obias;
549: unsigned int obps;
550: unsigned int i;
551: unsigned char *odata;
552: int obnext;
553: int osnext;
554:
555: #ifdef DEBUG
556: if (log_level >= 4) {
557: log_puts("enc: silence ");
558: log_putu(todo);
559: log_puts(" frames\n");
560: }
561: #endif
562: /*
563: * Partially copy structures into local variables, to avoid
564: * unnecessary indirections; this also allows the compiler to
565: * order local variables more "cache-friendly".
566: */
567: odata = out;
568: oshift = p->shift;
569: obias = p->bias;
570: obps = p->bps;
571: obnext = p->bnext;
572: osnext = p->snext;
573:
574: /*
575: * Start conversion.
576: */
577: odata += p->bfirst;
578: for (f = todo * p->nch; f > 0; f--) {
579: s = ((1U << 31) >> oshift) - obias;
580: for (i = obps; i > 0; i--) {
581: *odata = (unsigned char)s;
582: s >>= 8;
583: odata += obnext;
584: }
585: odata += osnext;
586: }
587: }
588:
589: /*
590: * initialize encoder from native to foreign encoding
591: */
592: void
593: enc_init(struct conv *p, struct aparams *par, int nch)
594: {
595: p->nch = nch;
596: p->bps = par->bps;
597: if (par->msb) {
598: p->shift = 32 - par->bps * 8;
599: } else {
600: p->shift = 32 - par->bits;
601: }
602: if (par->sig) {
603: p->bias = (1U << 31) >> p->shift;
604: } else {
605: p->bias = 0;
1.3 ratchov 606: }
1.1 ratchov 607: if (!par->le) {
608: p->bfirst = par->bps - 1;
609: p->bnext = -1;
610: p->snext = 2 * par->bps;
611: } else {
612: p->bfirst = 0;
613: p->bnext = 1;
614: p->snext = 0;
615: }
616: #ifdef DEBUG
617: if (log_level >= 3) {
618: log_puts("enc: ");
619: aparams_log(par);
620: log_puts(", ");
621: log_puti(p->nch);
622: log_puts(" channels\n");
623: }
624: #endif
625: }
626:
627: /*
628: * decode "todo" frames from from foreign to native encoding
629: */
630: void
631: dec_do(struct conv *p, unsigned char *in, unsigned char *out, int todo)
632: {
633: unsigned int f;
634: unsigned int ibps;
635: unsigned int i;
636: unsigned int s = 0xdeadbeef;
637: unsigned char *idata;
638: int ibnext;
639: int isnext;
640: unsigned int ibias;
641: unsigned int ishift;
642: adata_t *odata;
643:
644: #ifdef DEBUG
645: if (log_level >= 4) {
646: log_puts("dec: copying ");
647: log_putu(todo);
648: log_puts(" frames\n");
649: }
650: #endif
651: /*
652: * Partially copy structures into local variables, to avoid
653: * unnecessary indirections; this also allows the compiler to
654: * order local variables more "cache-friendly".
655: */
656: idata = in;
657: odata = (adata_t *)out;
658: ibps = p->bps;
659: ibnext = p->bnext;
660: ibias = p->bias;
661: ishift = p->shift;
662: isnext = p->snext;
663:
664: /*
665: * Start conversion.
666: */
667: idata += p->bfirst;
668: for (f = todo * p->nch; f > 0; f--) {
669: for (i = ibps; i > 0; i--) {
670: s <<= 8;
671: s |= *idata;
672: idata += ibnext;
673: }
674: idata += isnext;
675: s += ibias;
676: s <<= ishift;
677: s >>= 32 - ADATA_BITS;
678: *odata++ = s - ADATA_UNIT;
679: }
680: }
681:
682: /*
683: * convert a 32-bit float to adata_t, clipping to -1:1, boundaries
684: * excluded
685: */
686: static inline int
687: f32_to_adata(unsigned int x)
688: {
689: unsigned int s, e, m, y;
690:
691: s = (x >> 31);
692: e = (x >> 23) & 0xff;
693: m = (x << 8) | 0x80000000;
1.2 ratchov 694:
695: /*
696: * f32 exponent is (e - 127) and the point is after the 31-th
697: * bit, thus the shift is:
698: *
699: * 31 - (BITS - 1) - (e - 127)
700: *
701: * to ensure output is in the 0..(2^BITS)-1 range, the minimum
702: * shift is 31 - (BITS - 1), and maximum shift is 31
703: */
704: if (e < 127 - (ADATA_BITS - 1))
1.1 ratchov 705: y = 0;
1.2 ratchov 706: else if (e > 127)
1.1 ratchov 707: y = ADATA_UNIT - 1;
708: else
709: y = m >> (127 + (32 - ADATA_BITS) - e);
710: return (y ^ -s) + s;
711: }
712:
713: /*
714: * convert samples from little endian ieee 754 floats to adata_t
715: */
716: void
717: dec_do_float(struct conv *p, unsigned char *in, unsigned char *out, int todo)
718: {
719: unsigned int f;
720: unsigned int i;
721: unsigned int s = 0xdeadbeef;
722: unsigned char *idata;
723: int ibnext;
724: int isnext;
725: adata_t *odata;
726:
727: #ifdef DEBUG
728: if (log_level >= 4) {
729: log_puts("dec_float: copying ");
730: log_putu(todo);
731: log_puts(" frames\n");
732: }
733: #endif
734: /*
735: * Partially copy structures into local variables, to avoid
736: * unnecessary indirections; this also allows the compiler to
737: * order local variables more "cache-friendly".
738: */
739: idata = in;
740: odata = (adata_t *)out;
741: ibnext = p->bnext;
742: isnext = p->snext;
743:
744: /*
745: * Start conversion.
746: */
747: idata += p->bfirst;
748: for (f = todo * p->nch; f > 0; f--) {
749: for (i = 4; i > 0; i--) {
750: s <<= 8;
751: s |= *idata;
752: idata += ibnext;
753: }
754: idata += isnext;
755: *odata++ = f32_to_adata(s);
756: }
757: }
758:
759: /*
760: * convert samples from ulaw/alaw to adata_t
761: */
762: void
1.3 ratchov 763: dec_do_ulaw(struct conv *p, unsigned char *in,
764: unsigned char *out, int todo, int is_alaw)
1.1 ratchov 765: {
766: unsigned int f;
767: unsigned char *idata;
768: adata_t *odata;
769: short *map;
770:
771: #ifdef DEBUG
772: if (log_level >= 4) {
773: log_puts("dec_ulaw: copying ");
774: log_putu(todo);
775: log_puts(" frames\n");
776: }
777: #endif
778: map = is_alaw ? dec_alawmap : dec_ulawmap;
779: idata = in;
780: odata = (adata_t *)out;
781: for (f = todo * p->nch; f > 0; f--)
782: *odata++ = map[*idata++] << (ADATA_BITS - 16);
783: }
784:
785: /*
786: * initialize decoder from foreign to native encoding
787: */
788: void
789: dec_init(struct conv *p, struct aparams *par, int nch)
790: {
791: p->bps = par->bps;
792: p->nch = nch;
793: if (par->msb) {
794: p->shift = 32 - par->bps * 8;
795: } else {
796: p->shift = 32 - par->bits;
797: }
798: if (par->sig) {
799: p->bias = (1U << 31) >> p->shift;
800: } else {
801: p->bias = 0;
1.3 ratchov 802: }
1.1 ratchov 803: if (par->le) {
804: p->bfirst = par->bps - 1;
805: p->bnext = -1;
806: p->snext = 2 * par->bps;
807: } else {
808: p->bfirst = 0;
809: p->bnext = 1;
810: p->snext = 0;
811: }
812: #ifdef DEBUG
813: if (log_level >= 3) {
814: log_puts("dec: ");
815: aparams_log(par);
816: log_puts(", ");
817: log_puti(p->nch);
818: log_puts(" channels\n");
819: }
820: #endif
821: }
822:
823: /*
824: * mix "todo" input frames on the output with the given volume
825: */
826: void
827: cmap_add(struct cmap *p, void *in, void *out, int vol, int todo)
828: {
829: adata_t *idata, *odata;
830: int i, j, nch, istart, inext, onext, ostart, y, v;
831:
832: #ifdef DEBUG
833: if (log_level >= 4) {
834: log_puts("cmap: adding ");
835: log_puti(todo);
836: log_puts(" frames\n");
837: }
838: #endif
839: idata = in;
840: odata = out;
841: ostart = p->ostart;
842: onext = p->onext;
843: istart = p->istart;
844: inext = p->inext;
845: nch = p->nch;
846: v = vol;
847:
848: /*
849: * map/mix input on the output
850: */
851: for (i = todo; i > 0; i--) {
852: odata += ostart;
853: idata += istart;
854: for (j = nch; j > 0; j--) {
855: y = *odata + ADATA_MUL(*idata, v);
856: if (y >= ADATA_UNIT)
857: y = ADATA_UNIT - 1;
858: else if (y < -ADATA_UNIT)
859: y = -ADATA_UNIT;
860: *odata = y;
861: idata++;
862: odata++;
863: }
864: odata += onext;
865: idata += inext;
866: }
867: }
868:
869: /*
870: * overwrite output with "todo" input frames with with the given volume
871: */
872: void
873: cmap_copy(struct cmap *p, void *in, void *out, int vol, int todo)
874: {
875: adata_t *idata, *odata;
876: int i, j, nch, istart, inext, onext, ostart, v;
877:
878: #ifdef DEBUG
879: if (log_level >= 4) {
880: log_puts("cmap: copying ");
881: log_puti(todo);
882: log_puts(" frames\n");
883: }
884: #endif
885: idata = in;
886: odata = out;
887: ostart = p->ostart;
888: onext = p->onext;
889: istart = p->istart;
890: inext = p->inext;
891: nch = p->nch;
892: v = vol;
893:
894: /*
895: * copy to the output buffer
896: */
897: for (i = todo; i > 0; i--) {
898: idata += istart;
899: odata += ostart;
900: for (j = nch; j > 0; j--) {
901: *odata = ADATA_MUL(*idata, v);
902: odata++;
903: idata++;
904: }
905: odata += onext;
906: idata += inext;
907: }
908: }
909:
910: /*
911: * initialize channel mapper, to map a subset of input channel range
912: * into a subset of the output channel range
913: */
914: void
915: cmap_init(struct cmap *p,
916: int imin, int imax, int isubmin, int isubmax,
917: int omin, int omax, int osubmin, int osubmax)
918: {
919: int cmin, cmax;
920:
921: cmin = -NCHAN_MAX;
922: if (osubmin > cmin)
923: cmin = osubmin;
924: if (omin > cmin)
925: cmin = omin;
926: if (isubmin > cmin)
927: cmin = isubmin;
928: if (imin > cmin)
929: cmin = imin;
930:
931: cmax = NCHAN_MAX;
932: if (osubmax < cmax)
933: cmax = osubmax;
934: if (omax < cmax)
935: cmax = omax;
936: if (isubmax < cmax)
937: cmax = isubmax;
938: if (imax < cmax)
939: cmax = imax;
940:
941: p->ostart = cmin - omin;
942: p->onext = omax - cmax;
943: p->istart = cmin - imin;
944: p->inext = imax - cmax;
945: p->nch = cmax - cmin + 1;
946: #ifdef DEBUG
947: if (log_level >= 3) {
948: log_puts("cmap: nch = ");
949: log_puti(p->nch);
950: log_puts(", ostart = ");
951: log_puti(p->ostart);
952: log_puts(", onext = ");
953: log_puti(p->onext);
954: log_puts(", istart = ");
955: log_puti(p->istart);
956: log_puts(", inext = ");
957: log_puti(p->inext);
958: log_puts("\n");
959: }
960: #endif
961: }