Annotation of src/usr.bin/aucat/aproc.c, Revision 1.24
1.24 ! ratchov 1: /* $OpenBSD: aproc.c,v 1.23 2008/11/10 23:25:37 ratchov Exp $ */
1.1 ratchov 2: /*
3: * Copyright (c) 2008 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: /*
18: * aproc structures are simple audio processing units. They are
19: * interconnected by abuf structures and form a kind of circuit. aproc
20: * structure have call-backs that do the actual processing.
21: *
22: * This module implements the following processing units:
23: *
24: * - rpipe: read end of an unix file (pipe, socket, device...)
25: *
26: * - wpipe: write end of an unix file (pipe, socket, device...)
27: *
28: * - mix: mix N inputs -> 1 output
29: *
30: * - sub: from 1 input -> extract/copy N outputs
31: *
32: * - conv: converts/resamples/remaps a single stream
33: *
1.15 ratchov 34: * - resamp: resample streams in native format
35: *
1.1 ratchov 36: * TODO
37: *
38: * (easy) split the "conv" into 2 converters: one for input (that
39: * convers anything to 16bit signed) and one for the output (that
40: * converts 16bit signed to anything)
41: *
42: * (hard) add a lowpass filter for the resampler. Quality is
43: * not acceptable as is.
1.12 ratchov 44: *
1.1 ratchov 45: */
46: #include <err.h>
47: #include <limits.h>
48: #include <stdio.h>
49: #include <stdlib.h>
50: #include <string.h>
51:
52: #include "conf.h"
53: #include "aparams.h"
54: #include "abuf.h"
55: #include "aproc.h"
56: #include "file.h"
57:
58: struct aproc *
59: aproc_new(struct aproc_ops *ops, char *name)
60: {
61: struct aproc *p;
62:
63: p = malloc(sizeof(struct aproc));
64: if (p == NULL)
65: err(1, name);
66: LIST_INIT(&p->ibuflist);
67: LIST_INIT(&p->obuflist);
68: p->name = name;
69: p->ops = ops;
70: return p;
71: }
72:
73: void
74: aproc_del(struct aproc *p)
75: {
1.12 ratchov 76: struct abuf *i;
77:
78: DPRINTF("aproc_del: %s(%s): terminating...\n", p->ops->name, p->name);
79:
1.8 ratchov 80: if (p->ops->done)
81: p->ops->done(p);
1.12 ratchov 82:
83: while (!LIST_EMPTY(&p->ibuflist)) {
84: i = LIST_FIRST(&p->ibuflist);
85: abuf_hup(i);
86: }
87: while (!LIST_EMPTY(&p->obuflist)) {
88: i = LIST_FIRST(&p->obuflist);
89: abuf_eof(i);
90: }
91: DPRINTF("aproc_del: %s(%s): freed\n", p->ops->name, p->name);
1.1 ratchov 92: free(p);
93: }
94:
95: void
96: aproc_setin(struct aproc *p, struct abuf *ibuf)
97: {
98: LIST_INSERT_HEAD(&p->ibuflist, ibuf, ient);
99: ibuf->rproc = p;
100: if (p->ops->newin)
101: p->ops->newin(p, ibuf);
102: }
103:
104: void
105: aproc_setout(struct aproc *p, struct abuf *obuf)
106: {
107: LIST_INSERT_HEAD(&p->obuflist, obuf, oent);
108: obuf->wproc = p;
109: if (p->ops->newout)
110: p->ops->newout(p, obuf);
111: }
112:
1.12 ratchov 113: void
114: aproc_ipos(struct aproc *p, struct abuf *ibuf, int delta)
115: {
116: struct abuf *obuf;
117:
118: DPRINTFN(3, "aproc_ipos: %s: delta = %d\n", p->name, delta);
119:
120: LIST_FOREACH(obuf, &p->obuflist, oent) {
121: abuf_ipos(obuf, delta);
122: }
123: }
124:
125: void
126: aproc_opos(struct aproc *p, struct abuf *obuf, int delta)
127: {
128: struct abuf *ibuf;
129:
130: DPRINTFN(3, "aproc_opos: %s: delta = %d\n", p->name, delta);
131:
132: LIST_FOREACH(ibuf, &p->ibuflist, ient) {
133: abuf_opos(ibuf, delta);
134: }
135: }
136:
1.1 ratchov 137: int
138: rpipe_in(struct aproc *p, struct abuf *ibuf_dummy)
139: {
140: struct abuf *obuf = LIST_FIRST(&p->obuflist);
141: struct file *f = p->u.io.file;
142: unsigned char *data;
143: unsigned count;
144:
1.6 ratchov 145: DPRINTFN(3, "rpipe_in: %s\n", p->name);
146:
1.12 ratchov 147: if (ABUF_FULL(obuf) || !(f->state & FILE_ROK))
1.1 ratchov 148: return 0;
149: data = abuf_wgetblk(obuf, &count, 0);
1.6 ratchov 150: count = file_read(f, data, count);
1.12 ratchov 151: if (count == 0)
152: return 0;
1.6 ratchov 153: abuf_wcommit(obuf, count);
1.12 ratchov 154: if (!abuf_flush(obuf))
155: return 0;
156: return 1;
1.1 ratchov 157: }
158:
159: int
160: rpipe_out(struct aproc *p, struct abuf *obuf)
161: {
162: struct file *f = p->u.io.file;
163: unsigned char *data;
164: unsigned count;
165:
1.12 ratchov 166: if (f->refs > 0)
167: return 0;
1.6 ratchov 168: DPRINTFN(3, "rpipe_out: %s\n", p->name);
1.12 ratchov 169:
170: if (ABUF_FULL(obuf) || !(f->state & FILE_ROK))
1.1 ratchov 171: return 0;
172: data = abuf_wgetblk(obuf, &count, 0);
1.6 ratchov 173: count = file_read(f, data, count);
1.12 ratchov 174: if (count == 0)
175: return 0;
1.6 ratchov 176: abuf_wcommit(obuf, count);
1.12 ratchov 177: return 1;
1.1 ratchov 178: }
179:
180: void
1.8 ratchov 181: rpipe_done(struct aproc *p)
1.1 ratchov 182: {
183: struct file *f = p->u.io.file;
184:
185: f->rproc = NULL;
1.12 ratchov 186: if (f->wproc == NULL)
187: file_del(f);
1.1 ratchov 188: }
189:
190: void
191: rpipe_eof(struct aproc *p, struct abuf *ibuf_dummy)
192: {
193: DPRINTFN(3, "rpipe_eof: %s\n", p->name);
1.8 ratchov 194: aproc_del(p);
1.1 ratchov 195: }
196:
197: void
198: rpipe_hup(struct aproc *p, struct abuf *obuf)
199: {
200: DPRINTFN(3, "rpipe_hup: %s\n", p->name);
1.8 ratchov 201: aproc_del(p);
1.1 ratchov 202: }
203:
204: struct aproc_ops rpipe_ops = {
1.12 ratchov 205: "rpipe",
206: rpipe_in,
207: rpipe_out,
208: rpipe_eof,
209: rpipe_hup,
210: NULL, /* newin */
211: NULL, /* newout */
212: aproc_ipos,
213: aproc_opos,
214: rpipe_done
1.1 ratchov 215: };
216:
217: struct aproc *
218: rpipe_new(struct file *f)
219: {
220: struct aproc *p;
221:
222: p = aproc_new(&rpipe_ops, f->name);
223: p->u.io.file = f;
1.12 ratchov 224: f->rproc = p;
1.1 ratchov 225: return p;
226: }
227:
228: void
1.8 ratchov 229: wpipe_done(struct aproc *p)
1.1 ratchov 230: {
231: struct file *f = p->u.io.file;
232:
233: f->wproc = NULL;
1.12 ratchov 234: if (f->rproc == NULL)
235: file_del(f);
1.1 ratchov 236: }
237:
238: int
239: wpipe_in(struct aproc *p, struct abuf *ibuf)
240: {
241: struct file *f = p->u.io.file;
242: unsigned char *data;
243: unsigned count;
244:
1.12 ratchov 245: if (f->refs > 0)
246: return 0;
1.6 ratchov 247: DPRINTFN(3, "wpipe_in: %s\n", p->name);
248:
1.12 ratchov 249: if (ABUF_EMPTY(ibuf) || !(f->state & FILE_WOK))
1.1 ratchov 250: return 0;
251: data = abuf_rgetblk(ibuf, &count, 0);
252: count = file_write(f, data, count);
1.12 ratchov 253: if (count == 0)
254: return 0;
1.6 ratchov 255: abuf_rdiscard(ibuf, count);
1.12 ratchov 256: return 1;
1.1 ratchov 257: }
258:
259: int
260: wpipe_out(struct aproc *p, struct abuf *obuf_dummy)
261: {
262: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
263: struct file *f = p->u.io.file;
264: unsigned char *data;
265: unsigned count;
266:
1.6 ratchov 267: DPRINTFN(3, "wpipe_out: %s\n", p->name);
268:
1.12 ratchov 269: if (!abuf_fill(ibuf)) {
270: DPRINTFN(3, "wpipe_out: fill failed\n");
271: return 0;
272: }
273: if (ABUF_EMPTY(ibuf) || !(f->state & FILE_WOK))
1.1 ratchov 274: return 0;
275: data = abuf_rgetblk(ibuf, &count, 0);
1.12 ratchov 276: if (count == 0) {
277: DPRINTF("wpipe_out: %s: underrun\n", p->name);
278: return 0;
279: }
1.1 ratchov 280: count = file_write(f, data, count);
1.12 ratchov 281: if (count == 0)
282: return 0;
1.6 ratchov 283: abuf_rdiscard(ibuf, count);
1.1 ratchov 284: return 1;
285: }
286:
287: void
288: wpipe_eof(struct aproc *p, struct abuf *ibuf)
289: {
290: DPRINTFN(3, "wpipe_eof: %s\n", p->name);
1.8 ratchov 291: aproc_del(p);
1.1 ratchov 292: }
293:
294: void
295: wpipe_hup(struct aproc *p, struct abuf *obuf_dummy)
296: {
297: DPRINTFN(3, "wpipe_hup: %s\n", p->name);
1.8 ratchov 298: aproc_del(p);
1.1 ratchov 299: }
300:
301: struct aproc_ops wpipe_ops = {
1.12 ratchov 302: "wpipe",
303: wpipe_in,
304: wpipe_out,
305: wpipe_eof,
306: wpipe_hup,
307: NULL, /* newin */
308: NULL, /* newout */
309: aproc_ipos,
310: aproc_opos,
311: wpipe_done
1.1 ratchov 312: };
313:
314: struct aproc *
315: wpipe_new(struct file *f)
316: {
317: struct aproc *p;
318:
319: p = aproc_new(&wpipe_ops, f->name);
320: p->u.io.file = f;
321: f->wproc = p;
322: return p;
323: }
324:
325: /*
326: * Fill an output block with silence.
327: */
328: void
329: mix_bzero(struct aproc *p)
330: {
331: struct abuf *obuf = LIST_FIRST(&p->obuflist);
332: short *odata;
333: unsigned ocount;
334:
1.6 ratchov 335: DPRINTFN(4, "mix_bzero: used = %u, todo = %u\n",
1.14 ratchov 336: obuf->used, obuf->mixitodo);
337: odata = (short *)abuf_wgetblk(obuf, &ocount, obuf->mixitodo);
1.12 ratchov 338: ocount -= ocount % obuf->bpf;
1.1 ratchov 339: if (ocount == 0)
340: return;
341: memset(odata, 0, ocount);
1.14 ratchov 342: obuf->mixitodo += ocount;
343: DPRINTFN(4, "mix_bzero: ocount %u, todo %u\n", ocount, obuf->mixitodo);
1.1 ratchov 344: }
345:
346: /*
347: * Mix an input block over an output block.
348: */
349: void
350: mix_badd(struct abuf *ibuf, struct abuf *obuf)
351: {
352: short *idata, *odata;
1.13 ratchov 353: unsigned i, j, icnt, onext, ostart;
354: unsigned scount, icount, ocount;
1.23 ratchov 355: int vol;
1.1 ratchov 356:
1.6 ratchov 357: DPRINTFN(4, "mix_badd: todo = %u, done = %u\n",
1.14 ratchov 358: obuf->mixitodo, ibuf->mixodone);
1.1 ratchov 359:
360: idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
1.13 ratchov 361: icount /= ibuf->bpf;
1.1 ratchov 362: if (icount == 0)
363: return;
364:
1.14 ratchov 365: odata = (short *)abuf_wgetblk(obuf, &ocount, ibuf->mixodone);
1.13 ratchov 366: ocount /= obuf->bpf;
1.1 ratchov 367: if (ocount == 0)
368: return;
369:
1.23 ratchov 370: vol = (ibuf->mixweight * ibuf->mixvol) >> ADATA_SHIFT;
1.13 ratchov 371: ostart = ibuf->cmin - obuf->cmin;
372: onext = obuf->cmax - ibuf->cmax + ostart;
373: icnt = ibuf->cmax - ibuf->cmin + 1;
374: odata += ostart;
1.1 ratchov 375: scount = (icount < ocount) ? icount : ocount;
1.13 ratchov 376: for (i = scount; i > 0; i--) {
377: for (j = icnt; j > 0; j--) {
378: *odata += (*idata * vol) >> ADATA_SHIFT;
379: idata++;
380: odata++;
381: }
382: odata += onext;
383: }
384: abuf_rdiscard(ibuf, scount * ibuf->bpf);
1.14 ratchov 385: ibuf->mixodone += scount * obuf->bpf;
1.1 ratchov 386:
1.6 ratchov 387: DPRINTFN(4, "mix_badd: added %u, done = %u, todo = %u\n",
1.14 ratchov 388: scount, ibuf->mixodone, obuf->mixitodo);
1.1 ratchov 389: }
390:
391: int
392: mix_in(struct aproc *p, struct abuf *ibuf)
393: {
394: struct abuf *i, *inext, *obuf = LIST_FIRST(&p->obuflist);
1.7 ratchov 395: unsigned ocount;
1.1 ratchov 396:
1.7 ratchov 397: DPRINTFN(4, "mix_in: used = %u, done = %u, todo = %u\n",
1.14 ratchov 398: ibuf->used, ibuf->mixodone, obuf->mixitodo);
1.3 ratchov 399:
1.14 ratchov 400: if (!ABUF_ROK(ibuf) || ibuf->mixodone == obuf->mixitodo)
1.1 ratchov 401: return 0;
1.12 ratchov 402:
1.1 ratchov 403: mix_badd(ibuf, obuf);
1.14 ratchov 404: ocount = obuf->mixitodo;
1.1 ratchov 405: LIST_FOREACH(i, &p->ibuflist, ient) {
1.14 ratchov 406: if (ocount > i->mixodone)
407: ocount = i->mixodone;
1.1 ratchov 408: }
409: if (ocount == 0)
410: return 0;
411:
1.6 ratchov 412: abuf_wcommit(obuf, ocount);
1.12 ratchov 413: p->u.mix.lat += ocount / obuf->bpf;
1.14 ratchov 414: obuf->mixitodo -= ocount;
1.12 ratchov 415: if (!abuf_flush(obuf))
416: return 0; /* hup */
1.1 ratchov 417: mix_bzero(p);
1.12 ratchov 418: for (i = LIST_FIRST(&p->ibuflist); i != NULL; i = inext) {
1.1 ratchov 419: inext = LIST_NEXT(i, ient);
1.14 ratchov 420: i->mixodone -= ocount;
421: if (i->mixodone < obuf->mixitodo)
1.1 ratchov 422: mix_badd(i, obuf);
1.12 ratchov 423: if (!abuf_fill(i))
424: continue;
1.1 ratchov 425: }
1.22 ratchov 426: if (LIST_EMPTY(&p->ibuflist))
427: p->u.mix.idle += ocount / obuf->bpf;
1.1 ratchov 428: return 1;
429: }
430:
431: int
432: mix_out(struct aproc *p, struct abuf *obuf)
433: {
434: struct abuf *i, *inext;
1.20 ratchov 435: unsigned ocount, fdrop;
1.1 ratchov 436:
1.6 ratchov 437: DPRINTFN(4, "mix_out: used = %u, todo = %u\n",
1.14 ratchov 438: obuf->used, obuf->mixitodo);
1.1 ratchov 439:
1.12 ratchov 440: if (!ABUF_WOK(obuf))
441: return 0;
442:
1.1 ratchov 443: mix_bzero(p);
1.14 ratchov 444: ocount = obuf->mixitodo;
1.12 ratchov 445: for (i = LIST_FIRST(&p->ibuflist); i != NULL; i = inext) {
1.1 ratchov 446: inext = LIST_NEXT(i, ient);
1.12 ratchov 447: if (!abuf_fill(i))
448: continue;
1.5 ratchov 449: if (!ABUF_ROK(i)) {
1.14 ratchov 450: if ((p->u.mix.flags & MIX_DROP) && i->mixodone == 0) {
1.5 ratchov 451: if (i->xrun == XRUN_ERROR) {
452: abuf_hup(i);
453: continue;
454: }
1.20 ratchov 455: fdrop = obuf->mixitodo / obuf->bpf;
456: i->mixodone += fdrop * obuf->bpf;
1.5 ratchov 457: if (i->xrun == XRUN_SYNC)
1.20 ratchov 458: i->drop += fdrop * i->bpf;
1.12 ratchov 459: else {
1.20 ratchov 460: abuf_opos(i, -(int)fdrop);
1.12 ratchov 461: if (i->duplex) {
462: DPRINTF("mix_out: duplex %u\n",
1.20 ratchov 463: fdrop);
464: i->duplex->drop += fdrop *
465: i->duplex->bpf;
1.12 ratchov 466: abuf_ipos(i->duplex,
1.20 ratchov 467: -(int)fdrop);
1.12 ratchov 468: }
469: }
1.7 ratchov 470: DPRINTF("mix_out: drop = %u\n", i->drop);
1.5 ratchov 471: }
1.3 ratchov 472: } else
473: mix_badd(i, obuf);
1.14 ratchov 474: if (ocount > i->mixodone)
475: ocount = i->mixodone;
1.1 ratchov 476: }
477: if (ocount == 0)
478: return 0;
1.9 ratchov 479: if (LIST_EMPTY(&p->ibuflist) && (p->u.mix.flags & MIX_AUTOQUIT)) {
1.1 ratchov 480: DPRINTF("mix_out: nothing more to do...\n");
481: aproc_del(p);
482: return 0;
483: }
1.6 ratchov 484: abuf_wcommit(obuf, ocount);
1.12 ratchov 485: p->u.mix.lat += ocount / obuf->bpf;
1.14 ratchov 486: obuf->mixitodo -= ocount;
1.1 ratchov 487: LIST_FOREACH(i, &p->ibuflist, ient) {
1.14 ratchov 488: i->mixodone -= ocount;
1.1 ratchov 489: }
1.22 ratchov 490: if (LIST_EMPTY(&p->ibuflist))
491: p->u.mix.idle += ocount / obuf->bpf;
1.1 ratchov 492: return 1;
493: }
494:
495: void
496: mix_eof(struct aproc *p, struct abuf *ibuf)
497: {
498: struct abuf *obuf = LIST_FIRST(&p->obuflist);
499:
500: DPRINTF("mix_eof: %s: detached\n", p->name);
1.12 ratchov 501: mix_setmaster(p);
502:
1.1 ratchov 503: /*
504: * If there's no more inputs, abuf_run() will trigger the eof
505: * condition and propagate it, so no need to handle it here.
506: */
507: abuf_run(obuf);
508: DPRINTF("mix_eof: done\n");
509: }
510:
511: void
512: mix_hup(struct aproc *p, struct abuf *obuf)
513: {
514: struct abuf *ibuf;
515:
516: while (!LIST_EMPTY(&p->ibuflist)) {
517: ibuf = LIST_FIRST(&p->ibuflist);
518: abuf_hup(ibuf);
519: }
520: DPRINTF("mix_hup: %s: done\n", p->name);
521: aproc_del(p);
522: }
523:
524: void
525: mix_newin(struct aproc *p, struct abuf *ibuf)
526: {
1.13 ratchov 527: struct abuf *obuf = LIST_FIRST(&p->obuflist);
528:
529: if (!obuf || ibuf->cmin < obuf->cmin || ibuf->cmax > obuf->cmax) {
530: fprintf(stderr, "mix_newin: channel ranges mismatch\n");
531: abort();
532: }
1.22 ratchov 533: p->u.mix.idle = 0;
1.14 ratchov 534: ibuf->mixodone = 0;
1.23 ratchov 535: ibuf->mixvol = ADATA_UNIT;
536: ibuf->mixweight = ADATA_UNIT;
1.24 ! ratchov 537: ibuf->mixmaxweight = ADATA_UNIT;
1.5 ratchov 538: ibuf->xrun = XRUN_IGNORE;
1.1 ratchov 539: }
540:
541: void
542: mix_newout(struct aproc *p, struct abuf *obuf)
543: {
1.21 ratchov 544: DPRINTF("mix_newout: using %u fpb\n", obuf->len / obuf->bpf);
1.14 ratchov 545: obuf->mixitodo = 0;
1.1 ratchov 546: mix_bzero(p);
547: }
548:
1.12 ratchov 549: void
550: mix_opos(struct aproc *p, struct abuf *obuf, int delta)
551: {
552: DPRINTFN(3, "mix_opos: lat = %d/%d\n", p->u.mix.lat, p->u.mix.maxlat);
553: p->u.mix.lat -= delta;
554: aproc_opos(p, obuf, delta);
555: }
556:
1.1 ratchov 557: struct aproc_ops mix_ops = {
1.12 ratchov 558: "mix",
559: mix_in,
560: mix_out,
561: mix_eof,
562: mix_hup,
563: mix_newin,
564: mix_newout,
565: aproc_ipos,
566: mix_opos,
567: NULL
1.1 ratchov 568: };
569:
570: struct aproc *
1.12 ratchov 571: mix_new(char *name, int maxlat)
1.1 ratchov 572: {
573: struct aproc *p;
574:
1.12 ratchov 575: p = aproc_new(&mix_ops, name);
1.5 ratchov 576: p->u.mix.flags = 0;
1.22 ratchov 577: p->u.mix.idle = 0;
1.12 ratchov 578: p->u.mix.lat = 0;
579: p->u.mix.maxlat = maxlat;
1.1 ratchov 580: return p;
1.10 ratchov 581: }
582:
583: void
584: mix_pushzero(struct aproc *p)
585: {
586: struct abuf *obuf = LIST_FIRST(&p->obuflist);
587:
1.14 ratchov 588: abuf_wcommit(obuf, obuf->mixitodo);
589: p->u.mix.lat += obuf->mixitodo / obuf->bpf;
590: obuf->mixitodo = 0;
1.11 ratchov 591: abuf_run(obuf);
1.10 ratchov 592: mix_bzero(p);
593: }
594:
595: /*
596: * Normalize input levels
597: */
598: void
599: mix_setmaster(struct aproc *p)
600: {
601: unsigned n;
602: struct abuf *buf;
1.24 ! ratchov 603: int weight;
1.10 ratchov 604:
605: n = 0;
1.24 ! ratchov 606: LIST_FOREACH(buf, &p->ibuflist, ient) {
! 607: n++;
! 608: }
! 609: LIST_FOREACH(buf, &p->ibuflist, ient) {
! 610: weight = ADATA_UNIT / n;
! 611: if (weight > buf->mixmaxweight)
! 612: weight = buf->mixmaxweight;
! 613: buf->mixweight = weight;
! 614: DPRINTF("mix_setmaster: %p: %d/%d -> %d\n", buf,
! 615: buf->mixweight, buf->mixmaxweight, weight);
! 616: }
1.1 ratchov 617: }
618:
1.22 ratchov 619: void
620: mix_clear(struct aproc *p)
621: {
622: struct abuf *obuf = LIST_FIRST(&p->obuflist);
623:
624: p->u.mix.lat = 0;
625: obuf->mixitodo = 0;
626: mix_bzero(p);
627: }
628:
1.1 ratchov 629: /*
630: * Copy data from ibuf to obuf.
631: */
632: void
633: sub_bcopy(struct abuf *ibuf, struct abuf *obuf)
634: {
1.13 ratchov 635: short *idata, *odata;
636: unsigned i, j, ocnt, inext, istart;
1.1 ratchov 637: unsigned icount, ocount, scount;
638:
1.14 ratchov 639: idata = (short *)abuf_rgetblk(ibuf, &icount, obuf->subidone);
1.13 ratchov 640: icount /= ibuf->bpf;
1.1 ratchov 641: if (icount == 0)
642: return;
1.13 ratchov 643: odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
644: ocount /= obuf->bpf;
1.1 ratchov 645: if (ocount == 0)
646: return;
1.13 ratchov 647: istart = obuf->cmin - ibuf->cmin;
648: inext = ibuf->cmax - obuf->cmax + istart;
649: ocnt = obuf->cmax - obuf->cmin + 1;
1.1 ratchov 650: scount = (icount < ocount) ? icount : ocount;
1.13 ratchov 651: idata += istart;
652: for (i = scount; i > 0; i--) {
653: for (j = ocnt; j > 0; j--) {
654: *odata = *idata;
655: odata++;
656: idata++;
657: }
658: idata += inext;
659: }
660: abuf_wcommit(obuf, scount * obuf->bpf);
1.14 ratchov 661: obuf->subidone += scount * ibuf->bpf;
1.13 ratchov 662: DPRINTFN(4, "sub_bcopy: %u frames\n", scount);
1.1 ratchov 663: }
664:
665: int
666: sub_in(struct aproc *p, struct abuf *ibuf)
667: {
668: struct abuf *i, *inext;
1.20 ratchov 669: unsigned done, fdrop;
1.12 ratchov 670:
671: if (!ABUF_ROK(ibuf))
672: return 0;
1.1 ratchov 673: done = ibuf->used;
1.12 ratchov 674: for (i = LIST_FIRST(&p->obuflist); i != NULL; i = inext) {
1.1 ratchov 675: inext = LIST_NEXT(i, oent);
1.5 ratchov 676: if (!ABUF_WOK(i)) {
1.14 ratchov 677: if ((p->u.sub.flags & SUB_DROP) && i->subidone == 0) {
1.5 ratchov 678: if (i->xrun == XRUN_ERROR) {
679: abuf_eof(i);
680: continue;
681: }
1.20 ratchov 682: fdrop = ibuf->used / ibuf->bpf;
1.5 ratchov 683: if (i->xrun == XRUN_SYNC)
1.20 ratchov 684: i->silence += fdrop * i->bpf;
1.12 ratchov 685: else {
1.20 ratchov 686: abuf_ipos(i, -(int)fdrop);
1.12 ratchov 687: if (i->duplex) {
688: DPRINTF("sub_in: duplex %u\n",
1.20 ratchov 689: fdrop);
690: i->duplex->silence += fdrop *
691: i->duplex->bpf;
1.12 ratchov 692: abuf_opos(i->duplex,
1.20 ratchov 693: -(int)fdrop);
1.12 ratchov 694: }
695: }
1.20 ratchov 696: i->subidone += fdrop * ibuf->bpf;
1.12 ratchov 697: DPRINTF("sub_in: silence = %u\n", i->silence);
1.5 ratchov 698: }
1.12 ratchov 699: } else
1.1 ratchov 700: sub_bcopy(ibuf, i);
1.14 ratchov 701: if (done > i->subidone)
702: done = i->subidone;
1.12 ratchov 703: if (!abuf_flush(i))
704: continue;
1.1 ratchov 705: }
706: LIST_FOREACH(i, &p->obuflist, oent) {
1.14 ratchov 707: i->subidone -= done;
1.1 ratchov 708: }
1.6 ratchov 709: abuf_rdiscard(ibuf, done);
1.12 ratchov 710: p->u.sub.lat -= done / ibuf->bpf;
1.22 ratchov 711: if (LIST_EMPTY(&p->obuflist))
712: p->u.sub.idle += done / ibuf->bpf;
1.12 ratchov 713: return 1;
1.1 ratchov 714: }
715:
716: int
717: sub_out(struct aproc *p, struct abuf *obuf)
718: {
719: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
720: struct abuf *i, *inext;
721: unsigned done;
722:
1.12 ratchov 723: if (!ABUF_WOK(obuf))
724: return 0;
725: if (!abuf_fill(ibuf)) {
726: return 0;
727: }
1.14 ratchov 728: if (obuf->subidone == ibuf->used)
1.1 ratchov 729: return 0;
730:
731: done = ibuf->used;
1.12 ratchov 732: for (i = LIST_FIRST(&p->obuflist); i != NULL; i = inext) {
733: inext = LIST_NEXT(i, oent);
734: if (!abuf_flush(i))
735: continue;
736: sub_bcopy(ibuf, i);
1.14 ratchov 737: if (done > i->subidone)
738: done = i->subidone;
1.1 ratchov 739: }
740: LIST_FOREACH(i, &p->obuflist, oent) {
1.14 ratchov 741: i->subidone -= done;
1.1 ratchov 742: }
1.6 ratchov 743: abuf_rdiscard(ibuf, done);
1.12 ratchov 744: p->u.sub.lat -= done / ibuf->bpf;
1.22 ratchov 745: if (LIST_EMPTY(&p->obuflist))
746: p->u.sub.idle += done / ibuf->bpf;
1.1 ratchov 747: return 1;
748: }
749:
750: void
751: sub_eof(struct aproc *p, struct abuf *ibuf)
752: {
753: struct abuf *obuf;
754:
755: while (!LIST_EMPTY(&p->obuflist)) {
756: obuf = LIST_FIRST(&p->obuflist);
757: abuf_eof(obuf);
758: }
1.8 ratchov 759: aproc_del(p);
1.1 ratchov 760: }
761:
762: void
763: sub_hup(struct aproc *p, struct abuf *obuf)
764: {
765: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
766:
767: DPRINTF("sub_hup: %s: detached\n", p->name);
1.12 ratchov 768: abuf_run(ibuf);
1.1 ratchov 769: DPRINTF("sub_hup: done\n");
770: }
771:
772: void
773: sub_newout(struct aproc *p, struct abuf *obuf)
774: {
1.13 ratchov 775: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
776:
777: if (!ibuf || obuf->cmin < ibuf->cmin || obuf->cmax > ibuf->cmax) {
778: fprintf(stderr, "sub_newout: channel ranges mismatch\n");
779: abort();
780: }
1.22 ratchov 781: p->u.sub.idle = 0;
1.14 ratchov 782: obuf->subidone = 0;
1.5 ratchov 783: obuf->xrun = XRUN_IGNORE;
1.1 ratchov 784: }
785:
1.12 ratchov 786: void
787: sub_ipos(struct aproc *p, struct abuf *ibuf, int delta)
788: {
789: p->u.sub.lat += delta;
790: DPRINTFN(3, "sub_ipos: lat = %d/%d\n", p->u.sub.lat, p->u.sub.maxlat);
791: aproc_ipos(p, ibuf, delta);
792: }
793:
1.1 ratchov 794: struct aproc_ops sub_ops = {
1.12 ratchov 795: "sub",
796: sub_in,
797: sub_out,
798: sub_eof,
799: sub_hup,
800: NULL,
801: sub_newout,
802: sub_ipos,
803: aproc_opos,
804: NULL
1.1 ratchov 805: };
806:
807: struct aproc *
1.12 ratchov 808: sub_new(char *name, int maxlat)
1.1 ratchov 809: {
810: struct aproc *p;
811:
1.12 ratchov 812: p = aproc_new(&sub_ops, name);
1.5 ratchov 813: p->u.sub.flags = 0;
1.22 ratchov 814: p->u.sub.idle = 0;
1.12 ratchov 815: p->u.sub.lat = 0;
816: p->u.sub.maxlat = maxlat;
1.1 ratchov 817: return p;
1.22 ratchov 818: }
819:
820: void
821: sub_clear(struct aproc *p)
822: {
823: p->u.mix.lat = 0;
1.1 ratchov 824: }
825:
826: /*
827: * Convert one block.
828: */
829: void
1.15 ratchov 830: resamp_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
831: {
832: unsigned inch;
833: short *idata;
834: unsigned ipos, orate;
835: unsigned ifr;
836: unsigned onch;
837: short *odata;
838: unsigned opos, irate;
839: unsigned ofr;
840: unsigned c;
841: short *ctxbuf, *ctx;
842: unsigned icount, ocount;
843:
844: /*
845: * Calculate max frames readable at once from the input buffer.
846: */
847: idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
848: ifr = icount / ibuf->bpf;
849: icount = ifr * ibuf->bpf;
850:
851: odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
852: ofr = ocount / obuf->bpf;
853: ocount = ofr * obuf->bpf;
854:
855: /*
856: * Partially copy structures into local variables, to avoid
857: * unnecessary indirections; this also allows the compiler to
858: * order local variables more "cache-friendly".
859: */
860: inch = ibuf->cmax - ibuf->cmin + 1;
861: ipos = p->u.resamp.ipos;
862: irate = p->u.resamp.irate;
863: onch = obuf->cmax - obuf->cmin + 1;
864: opos = p->u.resamp.opos;
865: orate = p->u.resamp.orate;
866: ctxbuf = p->u.resamp.ctx;
867:
868: /*
869: * Start conversion.
870: */
871: DPRINTFN(4, "resamp_bcopy: ifr=%d ofr=%d\n", ifr, ofr);
872: for (;;) {
873: if ((int)(ipos - opos) > 0) {
874: if (ofr == 0)
875: break;
876: ctx = ctxbuf;
877: for (c = onch; c > 0; c--) {
878: *odata = *ctx;
879: odata++;
880: ctx++;
881: }
882: opos += irate;
883: ofr--;
884: } else {
885: if (ifr == 0)
886: break;
887: ctx = ctxbuf;
888: for (c = inch; c > 0; c--) {
889: *ctx = *idata;
890: idata++;
891: ctx++;
892: }
893: ipos += orate;
894: ifr--;
895: }
896: }
897: p->u.resamp.ipos = ipos;
898: p->u.resamp.opos = opos;
899: DPRINTFN(4, "resamp_bcopy: done, ifr=%d ofr=%d\n", ifr, ofr);
900:
901: /*
902: * Update FIFO pointers.
903: */
904: icount -= ifr * ibuf->bpf;
905: ocount -= ofr * obuf->bpf;
906: abuf_rdiscard(ibuf, icount);
907: abuf_wcommit(obuf, ocount);
908: }
909:
910: int
911: resamp_in(struct aproc *p, struct abuf *ibuf)
912: {
913: struct abuf *obuf = LIST_FIRST(&p->obuflist);
914:
915: DPRINTFN(4, "resamp_in: %s\n", p->name);
916:
917: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
918: return 0;
919: resamp_bcopy(p, ibuf, obuf);
920: if (!abuf_flush(obuf))
921: return 0;
922: return 1;
923: }
924:
925: int
926: resamp_out(struct aproc *p, struct abuf *obuf)
927: {
928: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
929:
930: DPRINTFN(4, "resamp_out: %s\n", p->name);
931:
932: if (!abuf_fill(ibuf))
933: return 0;
934: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
935: return 0;
936: resamp_bcopy(p, ibuf, obuf);
937: return 1;
938: }
939:
940: void
941: resamp_eof(struct aproc *p, struct abuf *ibuf)
942: {
943: DPRINTFN(4, "resamp_eof: %s\n", p->name);
944:
945: aproc_del(p);
946: }
947:
948: void
949: resamp_hup(struct aproc *p, struct abuf *obuf)
950: {
951: DPRINTFN(4, "resamp_hup: %s\n", p->name);
952:
953: aproc_del(p);
954: }
955:
956: void
957: resamp_ipos(struct aproc *p, struct abuf *ibuf, int delta)
958: {
959: struct abuf *obuf = LIST_FIRST(&p->obuflist);
960: long long ipos;
961: int ifac, ofac;
962:
963: DPRINTFN(3, "resamp_ipos: %d\n", delta);
964:
965: ifac = p->u.resamp.irate;
966: ofac = p->u.resamp.orate;
967: ipos = p->u.resamp.idelta + (long long)delta * ofac;
968: delta = (ipos + ifac - 1) / ifac;
969: p->u.resamp.idelta = ipos - (long long)delta * ifac;
970: abuf_ipos(obuf, delta);
971: }
972:
973: void
974: resamp_opos(struct aproc *p, struct abuf *obuf, int delta)
975: {
976: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
977: long long opos;
978: int ifac, ofac;
979:
980: DPRINTFN(3, "resamp_opos: %d\n", delta);
981:
982: ifac = p->u.resamp.irate;
983: ofac = p->u.resamp.orate;
984: opos = p->u.resamp.odelta + (long long)delta * ifac;
985: delta = (opos + ofac - 1) / ofac;
986: p->u.resamp.odelta = opos - (long long)delta * ofac;
987: abuf_opos(ibuf, delta);
988: }
989:
990: struct aproc_ops resamp_ops = {
991: "resamp",
992: resamp_in,
993: resamp_out,
994: resamp_eof,
995: resamp_hup,
996: NULL,
997: NULL,
998: resamp_ipos,
999: resamp_opos,
1000: NULL
1001: };
1002:
1003: struct aproc *
1004: resamp_new(char *name, struct aparams *ipar, struct aparams *opar)
1005: {
1006: struct aproc *p;
1.16 ratchov 1007: unsigned i;
1.15 ratchov 1008:
1009: p = aproc_new(&resamp_ops, name);
1010: p->u.resamp.irate = ipar->rate;
1011: p->u.resamp.orate = opar->rate;
1012: p->u.resamp.ipos = 0;
1013: p->u.resamp.opos = 0;
1014: p->u.resamp.idelta = 0;
1015: p->u.resamp.odelta = 0;
1.16 ratchov 1016: for (i = 0; i < NCHAN_MAX; i++)
1017: p->u.resamp.ctx[i] = 0;
1.15 ratchov 1018: if (debug_level > 0) {
1019: DPRINTF("resamp_new: %s: ", p->name);
1.17 ratchov 1020: aparams_print2(ipar, opar);
1021: DPRINTF("\n");
1022: }
1023: return p;
1024: }
1025:
1026: /*
1027: * Convert one block.
1028: */
1029: void
1030: cmap_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
1031: {
1032: unsigned inch;
1033: short *idata;
1034: unsigned onch;
1035: short *odata;
1036: short *ctx, *ictx, *octx;
1037: unsigned c, f, scount, icount, ocount;
1038:
1039: /*
1040: * Calculate max frames readable at once from the input buffer.
1041: */
1042: idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
1043: icount /= ibuf->bpf;
1044: if (icount == 0)
1045: return;
1046: odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
1047: ocount /= obuf->bpf;
1048: if (ocount == 0)
1049: return;
1050: scount = icount < ocount ? icount : ocount;
1051: inch = ibuf->cmax - ibuf->cmin + 1;
1052: onch = obuf->cmax - obuf->cmin + 1;
1053: ictx = p->u.cmap.ctx + ibuf->cmin;
1054: octx = p->u.cmap.ctx + obuf->cmin;
1055:
1056: for (f = scount; f > 0; f--) {
1057: ctx = ictx;
1058: for (c = inch; c > 0; c--) {
1059: *ctx = *idata;
1060: idata++;
1061: ctx++;
1062: }
1063: ctx = octx;
1064: for (c = onch; c > 0; c--) {
1065: *odata = *ctx;
1066: odata++;
1067: ctx++;
1068: }
1069: }
1070: DPRINTFN(4, "cmap_bcopy: scount = %u\n", scount);
1071: abuf_rdiscard(ibuf, scount * ibuf->bpf);
1072: abuf_wcommit(obuf, scount * obuf->bpf);
1073: }
1074:
1075: int
1076: cmap_in(struct aproc *p, struct abuf *ibuf)
1077: {
1078: struct abuf *obuf = LIST_FIRST(&p->obuflist);
1079:
1080: DPRINTFN(4, "cmap_in: %s\n", p->name);
1081:
1082: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1083: return 0;
1084: cmap_bcopy(p, ibuf, obuf);
1085: if (!abuf_flush(obuf))
1086: return 0;
1087: return 1;
1088: }
1089:
1090: int
1091: cmap_out(struct aproc *p, struct abuf *obuf)
1092: {
1093: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1094:
1095: DPRINTFN(4, "cmap_out: %s\n", p->name);
1096:
1097: if (!abuf_fill(ibuf))
1098: return 0;
1099: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1100: return 0;
1101: cmap_bcopy(p, ibuf, obuf);
1102: return 1;
1103: }
1104:
1105: void
1106: cmap_eof(struct aproc *p, struct abuf *ibuf)
1107: {
1108: DPRINTFN(4, "cmap_eof: %s\n", p->name);
1109:
1110: aproc_del(p);
1111: }
1112:
1113: void
1114: cmap_hup(struct aproc *p, struct abuf *obuf)
1115: {
1116: DPRINTFN(4, "cmap_hup: %s\n", p->name);
1117:
1118: aproc_del(p);
1119: }
1120:
1121: struct aproc_ops cmap_ops = {
1122: "cmap",
1123: cmap_in,
1124: cmap_out,
1125: cmap_eof,
1126: cmap_hup,
1127: NULL,
1128: NULL,
1.19 ratchov 1129: aproc_ipos,
1130: aproc_opos,
1.17 ratchov 1131: NULL
1132: };
1133:
1134: struct aproc *
1135: cmap_new(char *name, struct aparams *ipar, struct aparams *opar)
1136: {
1137: struct aproc *p;
1138: unsigned i;
1139:
1140: p = aproc_new(&cmap_ops, name);
1141: for (i = 0; i < NCHAN_MAX; i++)
1142: p->u.cmap.ctx[i] = 0;
1143: if (debug_level > 0) {
1144: DPRINTF("cmap_new: %s: ", p->name);
1.12 ratchov 1145: aparams_print2(ipar, opar);
1146: DPRINTF("\n");
1.19 ratchov 1147: }
1148: return p;
1149: }
1150:
1151: /*
1152: * Convert one block.
1153: */
1154: void
1155: enc_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
1156: {
1157: unsigned nch, scount, icount, ocount;
1158: unsigned f;
1159: short *idata;
1160: int s;
1161: unsigned oshift;
1162: int osigbit;
1163: unsigned obps;
1164: unsigned i;
1165: unsigned char *odata;
1166: int obnext;
1167: int osnext;
1168:
1169: /*
1170: * Calculate max frames readable at once from the input buffer.
1171: */
1172: idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
1173: icount /= ibuf->bpf;
1174: if (icount == 0)
1175: return;
1176: odata = abuf_wgetblk(obuf, &ocount, 0);
1177: ocount /= obuf->bpf;
1178: if (ocount == 0)
1179: return;
1180: scount = (icount < ocount) ? icount : ocount;
1181: nch = ibuf->cmax - ibuf->cmin + 1;
1182: DPRINTFN(4, "enc_bcopy: scount = %u, nch = %u\n", scount, nch);
1183:
1184: /*
1185: * Partially copy structures into local variables, to avoid
1186: * unnecessary indirections; this also allows the compiler to
1187: * order local variables more "cache-friendly".
1188: */
1189: oshift = p->u.conv.shift;
1190: osigbit = p->u.conv.sigbit;
1191: obps = p->u.conv.bps;
1192: obnext = p->u.conv.bnext;
1193: osnext = p->u.conv.snext;
1194:
1195: /*
1196: * Start conversion.
1197: */
1198: odata += p->u.conv.bfirst;
1199: for (f = scount * nch; f > 0; f--) {
1200: s = *idata++;
1201: s <<= 16;
1202: s >>= oshift;
1203: s ^= osigbit;
1204: for (i = obps; i > 0; i--) {
1205: *odata = (unsigned char)s;
1206: s >>= 8;
1207: odata += obnext;
1208: }
1209: odata += osnext;
1210: }
1211:
1212: /*
1213: * Update FIFO pointers.
1214: */
1215: abuf_rdiscard(ibuf, scount * ibuf->bpf);
1216: abuf_wcommit(obuf, scount * obuf->bpf);
1217: }
1218:
1219: int
1220: enc_in(struct aproc *p, struct abuf *ibuf)
1221: {
1222: struct abuf *obuf = LIST_FIRST(&p->obuflist);
1223:
1224: DPRINTFN(4, "enc_in: %s\n", p->name);
1225:
1226: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1227: return 0;
1228: enc_bcopy(p, ibuf, obuf);
1229: if (!abuf_flush(obuf))
1230: return 0;
1231: return 1;
1232: }
1233:
1234: int
1235: enc_out(struct aproc *p, struct abuf *obuf)
1236: {
1237: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1238:
1239: DPRINTFN(4, "enc_out: %s\n", p->name);
1240:
1241: if (!abuf_fill(ibuf))
1242: return 0;
1243: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1244: return 0;
1245: enc_bcopy(p, ibuf, obuf);
1246: return 1;
1247: }
1248:
1249: void
1250: enc_eof(struct aproc *p, struct abuf *ibuf)
1251: {
1252: DPRINTFN(4, "enc_eof: %s\n", p->name);
1253:
1254: aproc_del(p);
1255: }
1256:
1257: void
1258: enc_hup(struct aproc *p, struct abuf *obuf)
1259: {
1260: DPRINTFN(4, "enc_hup: %s\n", p->name);
1261:
1262: aproc_del(p);
1263: }
1264:
1265: struct aproc_ops enc_ops = {
1266: "enc",
1267: enc_in,
1268: enc_out,
1269: enc_eof,
1270: enc_hup,
1271: NULL,
1272: NULL,
1273: aproc_ipos,
1274: aproc_opos,
1275: NULL
1276: };
1277:
1278: struct aproc *
1279: enc_new(char *name, struct aparams *par)
1280: {
1281: struct aproc *p;
1282:
1283: p = aproc_new(&enc_ops, name);
1284: p->u.conv.bps = par->bps;
1285: p->u.conv.sigbit = par->sig ? 0 : 1 << (par->bits - 1);
1286: if (par->msb) {
1287: p->u.conv.shift = 32 - par->bps * 8;
1288: } else {
1289: p->u.conv.shift = 32 - par->bits;
1290: }
1291: if (!par->le) {
1292: p->u.conv.bfirst = par->bps - 1;
1293: p->u.conv.bnext = -1;
1294: p->u.conv.snext = 2 * par->bps;
1295: } else {
1296: p->u.conv.bfirst = 0;
1297: p->u.conv.bnext = 1;
1298: p->u.conv.snext = 0;
1299: }
1300: if (debug_level > 0) {
1301: fprintf(stderr, "enc_new: %s: ", p->name);
1302: aparams_print(par);
1303: fprintf(stderr, "\n");
1304: }
1305: return p;
1306: }
1307:
1308: /*
1309: * Convert one block.
1310: */
1311: void
1312: dec_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
1313: {
1314: unsigned nch, scount, icount, ocount;
1315: unsigned f;
1316: unsigned ibps;
1317: unsigned i;
1318: int s = 0xdeadbeef;
1319: unsigned char *idata;
1320: int ibnext;
1321: int isnext;
1322: int isigbit;
1323: unsigned ishift;
1324: short *odata;
1325:
1326: /*
1327: * Calculate max frames readable at once from the input buffer.
1328: */
1329: idata = abuf_rgetblk(ibuf, &icount, 0);
1330: icount /= ibuf->bpf;
1331: if (icount == 0)
1332: return;
1333: odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
1334: ocount /= obuf->bpf;
1335: if (ocount == 0)
1336: return;
1337: scount = (icount < ocount) ? icount : ocount;
1338: nch = obuf->cmax - obuf->cmin + 1;
1339: DPRINTFN(4, "dec_bcopy: scount = %u, nch = %u\n", scount, nch);
1340:
1341: /*
1342: * Partially copy structures into local variables, to avoid
1343: * unnecessary indirections; this also allows the compiler to
1344: * order local variables more "cache-friendly".
1345: */
1346: ibps = p->u.conv.bps;
1347: ibnext = p->u.conv.bnext;
1348: isigbit = p->u.conv.sigbit;
1349: ishift = p->u.conv.shift;
1350: isnext = p->u.conv.snext;
1351:
1352: /*
1353: * Start conversion.
1354: */
1355: idata += p->u.conv.bfirst;
1356: for (f = scount * nch; f > 0; f--) {
1357: for (i = ibps; i > 0; i--) {
1358: s <<= 8;
1359: s |= *idata;
1360: idata += ibnext;
1361: }
1362: idata += isnext;
1363: s ^= isigbit;
1364: s <<= ishift;
1365: s >>= 16;
1366: *odata++ = s;
1367: }
1368:
1369: /*
1370: * Update FIFO pointers.
1371: */
1372: abuf_rdiscard(ibuf, scount * ibuf->bpf);
1373: abuf_wcommit(obuf, scount * obuf->bpf);
1374: }
1375:
1376: int
1377: dec_in(struct aproc *p, struct abuf *ibuf)
1378: {
1379: struct abuf *obuf = LIST_FIRST(&p->obuflist);
1380:
1381: DPRINTFN(4, "dec_in: %s\n", p->name);
1382:
1383: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1384: return 0;
1385: dec_bcopy(p, ibuf, obuf);
1386: if (!abuf_flush(obuf))
1387: return 0;
1388: return 1;
1389: }
1390:
1391: int
1392: dec_out(struct aproc *p, struct abuf *obuf)
1393: {
1394: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1395:
1396: DPRINTFN(4, "dec_out: %s\n", p->name);
1397:
1398: if (!abuf_fill(ibuf))
1399: return 0;
1400: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1401: return 0;
1402: dec_bcopy(p, ibuf, obuf);
1403: return 1;
1404: }
1405:
1406: void
1407: dec_eof(struct aproc *p, struct abuf *ibuf)
1408: {
1409: DPRINTFN(4, "dec_eof: %s\n", p->name);
1410:
1411: aproc_del(p);
1412: }
1413:
1414: void
1415: dec_hup(struct aproc *p, struct abuf *obuf)
1416: {
1417: DPRINTFN(4, "dec_hup: %s\n", p->name);
1418:
1419: aproc_del(p);
1420: }
1421:
1422: struct aproc_ops dec_ops = {
1423: "dec",
1424: dec_in,
1425: dec_out,
1426: dec_eof,
1427: dec_hup,
1428: NULL,
1429: NULL,
1430: aproc_ipos,
1431: aproc_opos,
1432: NULL
1433: };
1434:
1435: struct aproc *
1436: dec_new(char *name, struct aparams *par)
1437: {
1438: struct aproc *p;
1439:
1440: p = aproc_new(&dec_ops, name);
1441: p->u.conv.bps = par->bps;
1442: p->u.conv.sigbit = par->sig ? 0 : 1 << (par->bits - 1);
1443: if (par->msb) {
1444: p->u.conv.shift = 32 - par->bps * 8;
1445: } else {
1446: p->u.conv.shift = 32 - par->bits;
1447: }
1448: if (par->le) {
1449: p->u.conv.bfirst = par->bps - 1;
1450: p->u.conv.bnext = -1;
1451: p->u.conv.snext = 2 * par->bps;
1452: } else {
1453: p->u.conv.bfirst = 0;
1454: p->u.conv.bnext = 1;
1455: p->u.conv.snext = 0;
1456: }
1457: if (debug_level > 0) {
1458: fprintf(stderr, "dec_new: %s: ", p->name);
1459: aparams_print(par);
1460: fprintf(stderr, "\n");
1.12 ratchov 1461: }
1.1 ratchov 1462: return p;
1463: }