Annotation of src/usr.bin/aucat/aproc.c, Revision 1.45
1.45 ! ratchov 1: /* $OpenBSD: aproc.c,v 1.44 2010/01/12 21:42:59 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: */
37: #include <err.h>
38: #include <stdlib.h>
39: #include <string.h>
40:
1.33 ratchov 41: #include "abuf.h"
1.1 ratchov 42: #include "aparams.h"
43: #include "aproc.h"
1.33 ratchov 44: #include "conf.h"
1.1 ratchov 45: #include "file.h"
1.38 ratchov 46: #include "midi.h"
1.42 ratchov 47: #ifdef DEBUG
48: #include "dbg.h"
49: #endif
50:
51: #ifdef DEBUG
52: void
53: aproc_dbg(struct aproc *p)
54: {
55: dbg_puts(p->ops->name);
56: dbg_puts("(");
57: dbg_puts(p->name);
58: dbg_puts(")");
59: }
60:
61: int
62: zomb_in(struct aproc *p, struct abuf *ibuf)
63: {
64: aproc_dbg(p);
65: dbg_puts(": in: terminated\n");
66: dbg_panic();
67: return 0;
68: }
69:
1.1 ratchov 70:
1.42 ratchov 71: int
72: zomb_out(struct aproc *p, struct abuf *obuf)
73: {
74: aproc_dbg(p);
75: dbg_puts(": out: terminated\n");
76: dbg_panic();
77: return 0;
78: }
79:
80: void
81: zomb_eof(struct aproc *p, struct abuf *ibuf)
82: {
83: aproc_dbg(p);
84: dbg_puts(": eof: terminated\n");
85: dbg_panic();
86: }
87:
88: void
89: zomb_hup(struct aproc *p, struct abuf *obuf)
90: {
91: aproc_dbg(p);
92: dbg_puts(": hup: terminated\n");
93: dbg_panic();
94: }
95:
96: void
97: zomb_newin(struct aproc *p, struct abuf *ibuf)
98: {
99: aproc_dbg(p);
100: dbg_puts(": newin: terminated\n");
101: dbg_panic();
102: }
103:
104: void
105: zomb_newout(struct aproc *p, struct abuf *obuf)
106: {
107: aproc_dbg(p);
108: dbg_puts(": newout: terminated\n");
109: dbg_panic();
110: }
111:
112: void
113: zomb_ipos(struct aproc *p, struct abuf *ibuf, int delta)
114: {
115: aproc_dbg(p);
116: dbg_puts(": ipos: terminated\n");
117: dbg_panic();
118: }
119:
120: void
121: zomb_opos(struct aproc *p, struct abuf *obuf, int delta)
122: {
123: aproc_dbg(p);
124: dbg_puts(": opos: terminated\n");
125: dbg_panic();
126: }
127:
128: struct aproc_ops zomb_ops = {
129: "zomb",
130: zomb_in,
131: zomb_out,
132: zomb_eof,
133: zomb_hup,
134: zomb_newin,
135: zomb_newout,
136: zomb_ipos,
137: zomb_opos,
138: NULL
139: };
140: #endif
1.34 ratchov 141:
1.1 ratchov 142: struct aproc *
143: aproc_new(struct aproc_ops *ops, char *name)
144: {
145: struct aproc *p;
146:
147: p = malloc(sizeof(struct aproc));
148: if (p == NULL)
149: err(1, name);
150: LIST_INIT(&p->ibuflist);
151: LIST_INIT(&p->obuflist);
152: p->name = name;
153: p->ops = ops;
1.30 ratchov 154: p->refs = 0;
1.38 ratchov 155: p->flags = 0;
1.1 ratchov 156: return p;
157: }
158:
159: void
160: aproc_del(struct aproc *p)
161: {
1.12 ratchov 162: struct abuf *i;
163:
1.38 ratchov 164: if (!(p->flags & APROC_ZOMB)) {
1.42 ratchov 165: #ifdef DEBUG
166: if (debug_level >= 3) {
167: aproc_dbg(p);
168: dbg_puts(": terminating...\n");
169: }
170: #endif
1.35 ratchov 171: if (p->ops->done) {
1.42 ratchov 172: #ifdef DEBUG
173: if (debug_level >= 3) {
174: aproc_dbg(p);
175: dbg_puts(": done\n");
176: }
177: #endif
1.35 ratchov 178: p->ops->done(p);
179: }
180: while (!LIST_EMPTY(&p->ibuflist)) {
181: i = LIST_FIRST(&p->ibuflist);
182: abuf_hup(i);
183: }
184: while (!LIST_EMPTY(&p->obuflist)) {
185: i = LIST_FIRST(&p->obuflist);
186: abuf_eof(i);
187: }
1.38 ratchov 188: p->flags |= APROC_ZOMB;
1.34 ratchov 189: }
1.35 ratchov 190: if (p->refs > 0) {
1.42 ratchov 191: #ifdef DEBUG
192: if (debug_level >= 3) {
193: aproc_dbg(p);
194: dbg_puts(": free delayed\n");
195: p->ops = &zomb_ops;
196: }
197: #endif
1.35 ratchov 198: return;
1.12 ratchov 199: }
1.42 ratchov 200: #ifdef DEBUG
201: if (debug_level >= 3) {
202: aproc_dbg(p);
203: dbg_puts(": freed\n");
204: }
205: #endif
1.1 ratchov 206: free(p);
207: }
208:
209: void
210: aproc_setin(struct aproc *p, struct abuf *ibuf)
211: {
212: LIST_INSERT_HEAD(&p->ibuflist, ibuf, ient);
213: ibuf->rproc = p;
214: if (p->ops->newin)
215: p->ops->newin(p, ibuf);
216: }
217:
218: void
219: aproc_setout(struct aproc *p, struct abuf *obuf)
220: {
221: LIST_INSERT_HEAD(&p->obuflist, obuf, oent);
222: obuf->wproc = p;
223: if (p->ops->newout)
224: p->ops->newout(p, obuf);
225: }
226:
1.12 ratchov 227: void
228: aproc_ipos(struct aproc *p, struct abuf *ibuf, int delta)
229: {
230: struct abuf *obuf;
231:
232: LIST_FOREACH(obuf, &p->obuflist, oent) {
233: abuf_ipos(obuf, delta);
234: }
235: }
236:
237: void
238: aproc_opos(struct aproc *p, struct abuf *obuf, int delta)
239: {
240: struct abuf *ibuf;
241:
242: LIST_FOREACH(ibuf, &p->ibuflist, ient) {
243: abuf_opos(ibuf, delta);
244: }
245: }
246:
1.1 ratchov 247: int
1.28 ratchov 248: aproc_inuse(struct aproc *p)
249: {
250: struct abuf *i;
251:
252: LIST_FOREACH(i, &p->ibuflist, ient) {
253: if (i->inuse)
254: return 1;
255: }
256: LIST_FOREACH(i, &p->obuflist, oent) {
257: if (i->inuse)
258: return 1;
259: }
260: return 0;
261: }
262:
263: int
1.32 ratchov 264: aproc_depend(struct aproc *p, struct aproc *dep)
265: {
266: struct abuf *i;
267:
268: if (p == dep)
269: return 1;
270: LIST_FOREACH(i, &p->ibuflist, ient) {
271: if (i->wproc && aproc_depend(i->wproc, dep))
272: return 1;
273: }
274: return 0;
275: }
276:
277: int
1.41 ratchov 278: rfile_in(struct aproc *p, struct abuf *ibuf_dummy)
1.1 ratchov 279: {
280: struct abuf *obuf = LIST_FIRST(&p->obuflist);
281: struct file *f = p->u.io.file;
282: unsigned char *data;
283: unsigned count;
284:
1.12 ratchov 285: if (ABUF_FULL(obuf) || !(f->state & FILE_ROK))
1.1 ratchov 286: return 0;
287: data = abuf_wgetblk(obuf, &count, 0);
1.6 ratchov 288: count = file_read(f, data, count);
1.12 ratchov 289: if (count == 0)
290: return 0;
1.6 ratchov 291: abuf_wcommit(obuf, count);
1.12 ratchov 292: if (!abuf_flush(obuf))
293: return 0;
294: return 1;
1.1 ratchov 295: }
296:
297: int
1.41 ratchov 298: rfile_out(struct aproc *p, struct abuf *obuf)
1.1 ratchov 299: {
300: struct file *f = p->u.io.file;
301: unsigned char *data;
302: unsigned count;
303:
1.32 ratchov 304: if (f->state & FILE_RINUSE)
1.12 ratchov 305: return 0;
306: if (ABUF_FULL(obuf) || !(f->state & FILE_ROK))
1.1 ratchov 307: return 0;
308: data = abuf_wgetblk(obuf, &count, 0);
1.6 ratchov 309: count = file_read(f, data, count);
1.12 ratchov 310: if (count == 0)
311: return 0;
1.6 ratchov 312: abuf_wcommit(obuf, count);
1.12 ratchov 313: return 1;
1.1 ratchov 314: }
315:
316: void
1.41 ratchov 317: rfile_done(struct aproc *p)
1.1 ratchov 318: {
319: struct file *f = p->u.io.file;
1.39 ratchov 320: struct abuf *obuf;
1.1 ratchov 321:
1.30 ratchov 322: if (f == NULL)
323: return;
1.39 ratchov 324: /*
325: * all buffers must be detached before deleting f->wproc,
326: * because otherwise it could trigger this code again
327: */
328: obuf = LIST_FIRST(&p->obuflist);
329: if (obuf)
330: abuf_eof(obuf);
1.37 ratchov 331: if (f->wproc) {
332: f->rproc = NULL;
333: aproc_del(f->wproc);
334: } else
1.12 ratchov 335: file_del(f);
1.30 ratchov 336: p->u.io.file = NULL;
1.1 ratchov 337: }
338:
339: void
1.41 ratchov 340: rfile_eof(struct aproc *p, struct abuf *ibuf_dummy)
1.1 ratchov 341: {
1.8 ratchov 342: aproc_del(p);
1.1 ratchov 343: }
344:
345: void
1.41 ratchov 346: rfile_hup(struct aproc *p, struct abuf *obuf)
1.1 ratchov 347: {
1.8 ratchov 348: aproc_del(p);
1.1 ratchov 349: }
350:
1.41 ratchov 351: struct aproc_ops rfile_ops = {
1.43 ratchov 352: "rfile",
1.41 ratchov 353: rfile_in,
354: rfile_out,
355: rfile_eof,
356: rfile_hup,
1.12 ratchov 357: NULL, /* newin */
358: NULL, /* newout */
359: aproc_ipos,
360: aproc_opos,
1.41 ratchov 361: rfile_done
1.1 ratchov 362: };
363:
364: struct aproc *
1.41 ratchov 365: rfile_new(struct file *f)
1.1 ratchov 366: {
367: struct aproc *p;
368:
1.41 ratchov 369: p = aproc_new(&rfile_ops, f->name);
1.1 ratchov 370: p->u.io.file = f;
1.31 ratchov 371: f->rproc = p;
1.1 ratchov 372: return p;
373: }
374:
375: void
1.41 ratchov 376: wfile_done(struct aproc *p)
1.1 ratchov 377: {
378: struct file *f = p->u.io.file;
1.39 ratchov 379: struct abuf *ibuf;
1.1 ratchov 380:
1.30 ratchov 381: if (f == NULL)
382: return;
1.39 ratchov 383: /*
384: * all buffers must be detached before deleting f->rproc,
385: * because otherwise it could trigger this code again
386: */
387: ibuf = LIST_FIRST(&p->ibuflist);
388: if (ibuf)
389: abuf_hup(ibuf);
1.37 ratchov 390: if (f->rproc) {
391: f->wproc = NULL;
392: aproc_del(f->rproc);
393: } else
1.12 ratchov 394: file_del(f);
1.30 ratchov 395: p->u.io.file = NULL;
1.1 ratchov 396: }
397:
398: int
1.41 ratchov 399: wfile_in(struct aproc *p, struct abuf *ibuf)
1.1 ratchov 400: {
401: struct file *f = p->u.io.file;
402: unsigned char *data;
403: unsigned count;
404:
1.32 ratchov 405: if (f->state & FILE_WINUSE)
1.12 ratchov 406: return 0;
407: if (ABUF_EMPTY(ibuf) || !(f->state & FILE_WOK))
1.1 ratchov 408: return 0;
409: data = abuf_rgetblk(ibuf, &count, 0);
410: count = file_write(f, data, count);
1.12 ratchov 411: if (count == 0)
412: return 0;
1.6 ratchov 413: abuf_rdiscard(ibuf, count);
1.12 ratchov 414: return 1;
1.1 ratchov 415: }
416:
417: int
1.41 ratchov 418: wfile_out(struct aproc *p, struct abuf *obuf_dummy)
1.1 ratchov 419: {
420: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
421: struct file *f = p->u.io.file;
422: unsigned char *data;
423: unsigned count;
424:
1.34 ratchov 425: if (!abuf_fill(ibuf))
1.12 ratchov 426: return 0;
427: if (ABUF_EMPTY(ibuf) || !(f->state & FILE_WOK))
1.1 ratchov 428: return 0;
429: data = abuf_rgetblk(ibuf, &count, 0);
1.12 ratchov 430: if (count == 0) {
1.34 ratchov 431: /* XXX: this can't happen, right ? */
1.12 ratchov 432: return 0;
433: }
1.1 ratchov 434: count = file_write(f, data, count);
1.12 ratchov 435: if (count == 0)
436: return 0;
1.6 ratchov 437: abuf_rdiscard(ibuf, count);
1.1 ratchov 438: return 1;
439: }
440:
441: void
1.41 ratchov 442: wfile_eof(struct aproc *p, struct abuf *ibuf)
1.1 ratchov 443: {
1.8 ratchov 444: aproc_del(p);
1.1 ratchov 445: }
446:
447: void
1.41 ratchov 448: wfile_hup(struct aproc *p, struct abuf *obuf_dummy)
1.1 ratchov 449: {
1.8 ratchov 450: aproc_del(p);
1.1 ratchov 451: }
452:
1.41 ratchov 453: struct aproc_ops wfile_ops = {
1.43 ratchov 454: "wfile",
1.41 ratchov 455: wfile_in,
456: wfile_out,
457: wfile_eof,
458: wfile_hup,
1.12 ratchov 459: NULL, /* newin */
460: NULL, /* newout */
461: aproc_ipos,
462: aproc_opos,
1.41 ratchov 463: wfile_done
1.1 ratchov 464: };
465:
466: struct aproc *
1.41 ratchov 467: wfile_new(struct file *f)
1.1 ratchov 468: {
469: struct aproc *p;
470:
1.41 ratchov 471: p = aproc_new(&wfile_ops, f->name);
1.1 ratchov 472: p->u.io.file = f;
473: f->wproc = p;
474: return p;
475: }
476:
477: /*
1.28 ratchov 478: * Append the given amount of silence (or less if there's not enough
1.33 ratchov 479: * space), and crank mixitodo accordingly.
1.1 ratchov 480: */
481: void
1.28 ratchov 482: mix_bzero(struct abuf *obuf, unsigned zcount)
1.1 ratchov 483: {
484: short *odata;
485: unsigned ocount;
486:
1.42 ratchov 487: #ifdef DEBUG
488: if (debug_level >= 4) {
489: abuf_dbg(obuf);
490: dbg_puts(": bzero(");
491: dbg_putu(zcount);
492: dbg_puts(")\n");
493: }
494: #endif
1.36 ratchov 495: odata = (short *)abuf_wgetblk(obuf, &ocount, obuf->w.mix.todo);
1.12 ratchov 496: ocount -= ocount % obuf->bpf;
1.28 ratchov 497: if (ocount > zcount)
498: ocount = zcount;
1.1 ratchov 499: memset(odata, 0, ocount);
1.36 ratchov 500: obuf->w.mix.todo += ocount;
1.1 ratchov 501: }
502:
503: /*
504: * Mix an input block over an output block.
505: */
506: void
507: mix_badd(struct abuf *ibuf, struct abuf *obuf)
508: {
509: short *idata, *odata;
1.13 ratchov 510: unsigned i, j, icnt, onext, ostart;
1.28 ratchov 511: unsigned scount, icount, ocount, zcount;
1.23 ratchov 512: int vol;
1.1 ratchov 513:
1.42 ratchov 514: #ifdef DEBUG
515: if (debug_level >= 4) {
516: abuf_dbg(ibuf);
1.43 ratchov 517: dbg_puts(": badd: done = ");
518: dbg_putu(ibuf->r.mix.done);
519: dbg_puts("/");
1.42 ratchov 520: dbg_putu(obuf->w.mix.todo);
521: dbg_puts("\n");
522: }
523: #endif
1.28 ratchov 524: /*
1.33 ratchov 525: * Calculate the maximum we can read.
1.28 ratchov 526: */
1.1 ratchov 527: idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
1.13 ratchov 528: icount /= ibuf->bpf;
1.1 ratchov 529: if (icount == 0)
530: return;
531:
1.28 ratchov 532: /*
1.33 ratchov 533: * Zero-fill if necessary.
1.28 ratchov 534: */
1.36 ratchov 535: zcount = ibuf->r.mix.done + icount * obuf->bpf;
536: if (zcount > obuf->w.mix.todo)
537: mix_bzero(obuf, zcount - obuf->w.mix.todo);
1.28 ratchov 538:
539: /*
1.33 ratchov 540: * Calculate the maximum we can write.
1.28 ratchov 541: */
1.36 ratchov 542: odata = (short *)abuf_wgetblk(obuf, &ocount, ibuf->r.mix.done);
1.13 ratchov 543: ocount /= obuf->bpf;
1.1 ratchov 544: if (ocount == 0)
545: return;
546:
1.36 ratchov 547: vol = (ibuf->r.mix.weight * ibuf->r.mix.vol) >> ADATA_SHIFT;
1.31 ratchov 548: ostart = ibuf->cmin - obuf->cmin;
1.13 ratchov 549: onext = obuf->cmax - ibuf->cmax + ostart;
550: icnt = ibuf->cmax - ibuf->cmin + 1;
551: odata += ostart;
1.1 ratchov 552: scount = (icount < ocount) ? icount : ocount;
1.13 ratchov 553: for (i = scount; i > 0; i--) {
554: for (j = icnt; j > 0; j--) {
555: *odata += (*idata * vol) >> ADATA_SHIFT;
556: idata++;
557: odata++;
558: }
559: odata += onext;
560: }
561: abuf_rdiscard(ibuf, scount * ibuf->bpf);
1.36 ratchov 562: ibuf->r.mix.done += scount * obuf->bpf;
1.1 ratchov 563:
1.42 ratchov 564: #ifdef DEBUG
565: if (debug_level >= 4) {
566: abuf_dbg(ibuf);
567: dbg_puts(": badd: done = ");
568: dbg_putu(scount);
569: dbg_puts(", todo = ");
570: dbg_putu(ibuf->r.mix.done);
571: dbg_puts("/");
572: dbg_putu(obuf->w.mix.todo);
573: dbg_puts("\n");
574: }
575: #endif
1.1 ratchov 576: }
577:
1.28 ratchov 578: /*
579: * Handle buffer underrun, return 0 if stream died.
580: */
581: int
582: mix_xrun(struct abuf *i, struct abuf *obuf)
583: {
584: unsigned fdrop;
1.31 ratchov 585:
1.36 ratchov 586: if (i->r.mix.done > 0)
1.28 ratchov 587: return 1;
1.36 ratchov 588: if (i->r.mix.xrun == XRUN_ERROR) {
1.28 ratchov 589: abuf_hup(i);
590: return 0;
591: }
592: mix_bzero(obuf, obuf->len);
1.36 ratchov 593: fdrop = obuf->w.mix.todo / obuf->bpf;
1.42 ratchov 594: #ifdef DEBUG
595: if (debug_level >= 3) {
596: abuf_dbg(i);
597: dbg_puts(": underrun, dropping ");
598: dbg_putu(fdrop);
599: dbg_puts(" + ");
600: dbg_putu(i->drop / i->bpf);
601: dbg_puts("\n");
602: }
603: #endif
1.36 ratchov 604: i->r.mix.done += fdrop * obuf->bpf;
605: if (i->r.mix.xrun == XRUN_SYNC)
1.28 ratchov 606: i->drop += fdrop * i->bpf;
607: else {
608: abuf_opos(i, -(int)fdrop);
609: if (i->duplex) {
1.42 ratchov 610: #ifdef DEBUG
611: if (debug_level >= 3) {
612: abuf_dbg(i->duplex);
613: dbg_puts(": full-duplex resync\n");
614: }
615: #endif
1.28 ratchov 616: i->duplex->drop += fdrop * i->duplex->bpf;
617: abuf_ipos(i->duplex, -(int)fdrop);
618: }
619: }
620: return 1;
621: }
622:
1.1 ratchov 623: int
624: mix_in(struct aproc *p, struct abuf *ibuf)
625: {
626: struct abuf *i, *inext, *obuf = LIST_FIRST(&p->obuflist);
1.28 ratchov 627: unsigned odone;
1.1 ratchov 628:
1.42 ratchov 629: #ifdef DEBUG
630: if (debug_level >= 4) {
631: aproc_dbg(p);
632: dbg_puts(": used = ");
633: dbg_putu(ibuf->used);
634: dbg_puts("/");
635: dbg_putu(ibuf->len);
1.43 ratchov 636: dbg_puts(", done = ");
637: dbg_putu(ibuf->r.mix.done);
638: dbg_puts("/");
1.42 ratchov 639: dbg_putu(obuf->w.mix.todo);
640: dbg_puts("\n");
641: }
642: #endif
1.28 ratchov 643: if (!ABUF_ROK(ibuf))
1.1 ratchov 644: return 0;
1.28 ratchov 645: odone = obuf->len;
646: for (i = LIST_FIRST(&p->ibuflist); i != NULL; i = inext) {
647: inext = LIST_NEXT(i, ient);
648: if (!abuf_fill(i))
649: continue; /* eof */
650: mix_badd(i, obuf);
1.36 ratchov 651: if (odone > i->r.mix.done)
652: odone = i->r.mix.done;
1.28 ratchov 653: }
654: if (LIST_EMPTY(&p->ibuflist) || odone == 0)
655: return 0;
656: p->u.mix.lat += odone / obuf->bpf;
1.1 ratchov 657: LIST_FOREACH(i, &p->ibuflist, ient) {
1.36 ratchov 658: i->r.mix.done -= odone;
1.1 ratchov 659: }
1.28 ratchov 660: abuf_wcommit(obuf, odone);
1.36 ratchov 661: obuf->w.mix.todo -= odone;
1.12 ratchov 662: if (!abuf_flush(obuf))
663: return 0; /* hup */
1.1 ratchov 664: return 1;
665: }
666:
667: int
668: mix_out(struct aproc *p, struct abuf *obuf)
669: {
670: struct abuf *i, *inext;
1.28 ratchov 671: unsigned odone;
1.1 ratchov 672:
1.42 ratchov 673: #ifdef DEBUG
674: if (debug_level >= 4) {
675: aproc_dbg(p);
676: dbg_puts(": used = ");
677: dbg_putu(obuf->used);
678: dbg_puts("/");
679: dbg_putu(obuf->len);
680: dbg_puts(", todo = ");
681: dbg_putu(obuf->w.mix.todo);
682: dbg_puts("/");
683: dbg_putu(obuf->len);
684: dbg_puts("\n");
685: }
686: #endif
1.12 ratchov 687: if (!ABUF_WOK(obuf))
1.31 ratchov 688: return 0;
1.28 ratchov 689: odone = obuf->len;
1.12 ratchov 690: for (i = LIST_FIRST(&p->ibuflist); i != NULL; i = inext) {
1.1 ratchov 691: inext = LIST_NEXT(i, ient);
1.12 ratchov 692: if (!abuf_fill(i))
1.28 ratchov 693: continue; /* eof */
1.5 ratchov 694: if (!ABUF_ROK(i)) {
1.38 ratchov 695: if (p->flags & APROC_DROP) {
1.28 ratchov 696: if (!mix_xrun(i, obuf))
1.5 ratchov 697: continue;
698: }
1.3 ratchov 699: } else
700: mix_badd(i, obuf);
1.36 ratchov 701: if (odone > i->r.mix.done)
702: odone = i->r.mix.done;
1.28 ratchov 703: }
704: if (LIST_EMPTY(&p->ibuflist)) {
1.38 ratchov 705: if (p->flags & APROC_QUIT) {
1.28 ratchov 706: aproc_del(p);
707: return 0;
708: }
1.38 ratchov 709: if (!(p->flags & APROC_DROP))
1.28 ratchov 710: return 0;
711: mix_bzero(obuf, obuf->len);
1.36 ratchov 712: odone = obuf->w.mix.todo;
1.28 ratchov 713: p->u.mix.idle += odone / obuf->bpf;
1.1 ratchov 714: }
1.28 ratchov 715: if (odone == 0)
1.1 ratchov 716: return 0;
1.28 ratchov 717: p->u.mix.lat += odone / obuf->bpf;
1.1 ratchov 718: LIST_FOREACH(i, &p->ibuflist, ient) {
1.36 ratchov 719: i->r.mix.done -= odone;
1.1 ratchov 720: }
1.28 ratchov 721: abuf_wcommit(obuf, odone);
1.36 ratchov 722: obuf->w.mix.todo -= odone;
1.1 ratchov 723: return 1;
724: }
725:
726: void
727: mix_eof(struct aproc *p, struct abuf *ibuf)
728: {
1.28 ratchov 729: struct abuf *i, *obuf = LIST_FIRST(&p->obuflist);
730: unsigned odone;
1.1 ratchov 731:
1.12 ratchov 732: mix_setmaster(p);
733:
1.28 ratchov 734: if (!aproc_inuse(p)) {
1.42 ratchov 735: #ifdef DEBUG
736: if (debug_level >= 3) {
737: aproc_dbg(p);
738: dbg_puts(": running other streams\n");
739: }
740: #endif
1.28 ratchov 741: /*
1.33 ratchov 742: * Find a blocked input.
1.28 ratchov 743: */
744: odone = obuf->len;
745: LIST_FOREACH(i, &p->ibuflist, ient) {
1.36 ratchov 746: if (ABUF_ROK(i) && i->r.mix.done < obuf->w.mix.todo) {
1.28 ratchov 747: abuf_run(i);
748: return;
749: }
1.36 ratchov 750: if (odone > i->r.mix.done)
751: odone = i->r.mix.done;
1.28 ratchov 752: }
753: /*
1.33 ratchov 754: * No blocked inputs. Check if output is blocked.
1.28 ratchov 755: */
1.36 ratchov 756: if (LIST_EMPTY(&p->ibuflist) || odone == obuf->w.mix.todo)
1.28 ratchov 757: abuf_run(obuf);
758: }
1.1 ratchov 759: }
760:
761: void
762: mix_hup(struct aproc *p, struct abuf *obuf)
763: {
764: aproc_del(p);
765: }
766:
767: void
768: mix_newin(struct aproc *p, struct abuf *ibuf)
769: {
1.42 ratchov 770: #ifdef DEBUG
771: struct abuf *obuf = LIST_FIRST(&p->obuflist);
772:
773: if (!obuf || ibuf->cmin < obuf->cmin || ibuf->cmax > obuf->cmax) {
774: dbg_puts("newin: channel ranges mismatch\n");
775: dbg_panic();
776: }
777: #endif
1.22 ratchov 778: p->u.mix.idle = 0;
1.36 ratchov 779: ibuf->r.mix.done = 0;
780: ibuf->r.mix.vol = ADATA_UNIT;
781: ibuf->r.mix.weight = ADATA_UNIT;
782: ibuf->r.mix.maxweight = ADATA_UNIT;
783: ibuf->r.mix.xrun = XRUN_IGNORE;
1.1 ratchov 784: }
785:
786: void
787: mix_newout(struct aproc *p, struct abuf *obuf)
788: {
1.42 ratchov 789: #ifdef DEBUG
790: if (debug_level >= 3) {
791: aproc_dbg(p);
792: dbg_puts(": newin, will use ");
793: dbg_putu(obuf->len / obuf->bpf);
794: dbg_puts(" fr\n");
795: }
796: #endif
1.36 ratchov 797: obuf->w.mix.todo = 0;
1.1 ratchov 798: }
799:
1.12 ratchov 800: void
801: mix_opos(struct aproc *p, struct abuf *obuf, int delta)
802: {
1.42 ratchov 803: #ifdef DEBUG
804: if (debug_level >= 4) {
805: aproc_dbg(p);
806: dbg_puts(": opos: lat = ");
807: dbg_puti(p->u.mix.lat);
808: dbg_puts("/");
809: dbg_puti(p->u.mix.maxlat);
810: dbg_puts(" fr\n");
811: }
812: #endif
1.12 ratchov 813: p->u.mix.lat -= delta;
1.38 ratchov 814: if (p->u.mix.ctl)
815: ctl_ontick(p->u.mix.ctl, delta);
1.12 ratchov 816: aproc_opos(p, obuf, delta);
817: }
818:
1.1 ratchov 819: struct aproc_ops mix_ops = {
1.12 ratchov 820: "mix",
821: mix_in,
822: mix_out,
823: mix_eof,
824: mix_hup,
825: mix_newin,
826: mix_newout,
827: aproc_ipos,
828: mix_opos,
829: NULL
1.1 ratchov 830: };
831:
832: struct aproc *
1.38 ratchov 833: mix_new(char *name, int maxlat, struct aproc *ctl)
1.1 ratchov 834: {
835: struct aproc *p;
836:
1.12 ratchov 837: p = aproc_new(&mix_ops, name);
1.22 ratchov 838: p->u.mix.idle = 0;
1.12 ratchov 839: p->u.mix.lat = 0;
840: p->u.mix.maxlat = maxlat;
1.38 ratchov 841: p->u.mix.ctl = ctl;
1.1 ratchov 842: return p;
1.10 ratchov 843: }
844:
845: /*
1.33 ratchov 846: * Normalize input levels.
1.10 ratchov 847: */
848: void
849: mix_setmaster(struct aproc *p)
850: {
851: unsigned n;
1.40 ratchov 852: struct abuf *i, *j;
1.24 ratchov 853: int weight;
1.10 ratchov 854:
1.40 ratchov 855: /*
856: * count the number of inputs. If a set of inputs
857: * uses channels that have no intersection, they are
858: * counted only once because they don't need to
859: * share their volume
860: */
1.10 ratchov 861: n = 0;
1.40 ratchov 862: LIST_FOREACH(i, &p->ibuflist, ient) {
863: j = LIST_NEXT(i, ient);
864: for (;;) {
865: if (j == NULL) {
866: n++;
867: break;
868: }
869: if (i->cmin > j->cmax || i->cmax < j->cmin)
870: break;
871: j = LIST_NEXT(j, ient);
872: }
1.24 ratchov 873: }
1.40 ratchov 874: LIST_FOREACH(i, &p->ibuflist, ient) {
1.24 ratchov 875: weight = ADATA_UNIT / n;
1.40 ratchov 876: if (weight > i->r.mix.maxweight)
877: weight = i->r.mix.maxweight;
878: i->r.mix.weight = weight;
1.42 ratchov 879: #ifdef DEBUG
880: if (debug_level >= 3) {
881: abuf_dbg(i);
882: dbg_puts(": setmaster: ");
883: dbg_puti(i->r.mix.weight);
884: dbg_puts("/");
885: dbg_puti(i->r.mix.maxweight);
886: dbg_puts("\n");
887: }
888: #endif
1.24 ratchov 889: }
1.1 ratchov 890: }
891:
1.22 ratchov 892: void
893: mix_clear(struct aproc *p)
894: {
895: struct abuf *obuf = LIST_FIRST(&p->obuflist);
896:
897: p->u.mix.lat = 0;
1.36 ratchov 898: obuf->w.mix.todo = 0;
1.43 ratchov 899: }
900:
901: void
902: mix_prime(struct aproc *p)
903: {
904: struct abuf *obuf = LIST_FIRST(&p->obuflist);
905: unsigned todo, count;
906:
907: for (;;) {
908: if (!ABUF_WOK(obuf))
909: break;
910: todo = (p->u.mix.maxlat - p->u.mix.lat) * obuf->bpf;
911: if (todo == 0)
912: break;
913: mix_bzero(obuf, obuf->len);
914: count = obuf->w.mix.todo;
915: if (count > todo)
916: count = todo;
917: obuf->w.mix.todo -= count;
918: p->u.mix.lat += count / obuf->bpf;
919: abuf_wcommit(obuf, count);
920: abuf_flush(obuf);
921: }
922: #ifdef DEBUG
923: if (debug_level >= 3) {
924: aproc_dbg(p);
925: dbg_puts(": prime: lat/maxlat=");
926: dbg_puti(p->u.mix.lat);
927: dbg_puts("/");
928: dbg_puti(p->u.mix.maxlat);
929: dbg_puts("\n");
930: }
931: #endif
1.22 ratchov 932: }
933:
1.1 ratchov 934: /*
935: * Copy data from ibuf to obuf.
936: */
937: void
938: sub_bcopy(struct abuf *ibuf, struct abuf *obuf)
939: {
1.13 ratchov 940: short *idata, *odata;
941: unsigned i, j, ocnt, inext, istart;
1.1 ratchov 942: unsigned icount, ocount, scount;
943:
1.36 ratchov 944: idata = (short *)abuf_rgetblk(ibuf, &icount, obuf->w.sub.done);
1.13 ratchov 945: icount /= ibuf->bpf;
1.1 ratchov 946: if (icount == 0)
947: return;
1.13 ratchov 948: odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
949: ocount /= obuf->bpf;
1.1 ratchov 950: if (ocount == 0)
951: return;
1.13 ratchov 952: istart = obuf->cmin - ibuf->cmin;
953: inext = ibuf->cmax - obuf->cmax + istart;
954: ocnt = obuf->cmax - obuf->cmin + 1;
1.1 ratchov 955: scount = (icount < ocount) ? icount : ocount;
1.13 ratchov 956: idata += istart;
957: for (i = scount; i > 0; i--) {
958: for (j = ocnt; j > 0; j--) {
959: *odata = *idata;
960: odata++;
961: idata++;
962: }
963: idata += inext;
964: }
1.31 ratchov 965: abuf_wcommit(obuf, scount * obuf->bpf);
1.36 ratchov 966: obuf->w.sub.done += scount * ibuf->bpf;
1.42 ratchov 967: #ifdef DEBUG
968: if (debug_level >= 4) {
969: abuf_dbg(obuf);
970: dbg_puts(": bcopy ");
971: dbg_putu(scount);
972: dbg_puts(" fr\n");
973: }
974: #endif
1.1 ratchov 975: }
976:
1.28 ratchov 977: /*
1.33 ratchov 978: * Handle buffer overruns. Return 0 if the stream died.
1.28 ratchov 979: */
980: int
981: sub_xrun(struct abuf *ibuf, struct abuf *i)
982: {
983: unsigned fdrop;
984:
1.36 ratchov 985: if (i->w.sub.done > 0)
1.28 ratchov 986: return 1;
1.36 ratchov 987: if (i->w.sub.xrun == XRUN_ERROR) {
1.28 ratchov 988: abuf_eof(i);
989: return 0;
990: }
991: fdrop = ibuf->used / ibuf->bpf;
1.42 ratchov 992: #ifdef DEBUG
993: if (debug_level >= 3) {
994: abuf_dbg(i);
995: dbg_puts(": overrun, silence ");
996: dbg_putu(fdrop);
997: dbg_puts(" + ");
998: dbg_putu(i->silence / i->bpf);
999: dbg_puts("\n");
1000: }
1001: #endif
1.36 ratchov 1002: if (i->w.sub.xrun == XRUN_SYNC)
1.28 ratchov 1003: i->silence += fdrop * i->bpf;
1004: else {
1005: abuf_ipos(i, -(int)fdrop);
1006: if (i->duplex) {
1.42 ratchov 1007: #ifdef DEBUG
1008: if (debug_level >= 3) {
1009: abuf_dbg(i->duplex);
1010: dbg_puts(": full-duplex resync\n");
1011: }
1012: #endif
1.28 ratchov 1013: i->duplex->silence += fdrop * i->duplex->bpf;
1014: abuf_opos(i->duplex, -(int)fdrop);
1015: }
1016: }
1.36 ratchov 1017: i->w.sub.done += fdrop * ibuf->bpf;
1.28 ratchov 1018: return 1;
1019: }
1020:
1.1 ratchov 1021: int
1022: sub_in(struct aproc *p, struct abuf *ibuf)
1023: {
1024: struct abuf *i, *inext;
1.28 ratchov 1025: unsigned idone;
1.31 ratchov 1026:
1.12 ratchov 1027: if (!ABUF_ROK(ibuf))
1028: return 0;
1.28 ratchov 1029: idone = ibuf->len;
1.12 ratchov 1030: for (i = LIST_FIRST(&p->obuflist); i != NULL; i = inext) {
1.1 ratchov 1031: inext = LIST_NEXT(i, oent);
1.5 ratchov 1032: if (!ABUF_WOK(i)) {
1.38 ratchov 1033: if (p->flags & APROC_DROP) {
1.28 ratchov 1034: if (!sub_xrun(ibuf, i))
1.5 ratchov 1035: continue;
1036: }
1.12 ratchov 1037: } else
1.1 ratchov 1038: sub_bcopy(ibuf, i);
1.36 ratchov 1039: if (idone > i->w.sub.done)
1040: idone = i->w.sub.done;
1.12 ratchov 1041: if (!abuf_flush(i))
1042: continue;
1.1 ratchov 1043: }
1.28 ratchov 1044: if (LIST_EMPTY(&p->obuflist)) {
1.38 ratchov 1045: if (p->flags & APROC_QUIT) {
1.28 ratchov 1046: aproc_del(p);
1047: return 0;
1048: }
1.38 ratchov 1049: if (!(p->flags & APROC_DROP))
1.28 ratchov 1050: return 0;
1051: idone = ibuf->used;
1052: p->u.sub.idle += idone / ibuf->bpf;
1053: }
1054: if (idone == 0)
1055: return 0;
1.1 ratchov 1056: LIST_FOREACH(i, &p->obuflist, oent) {
1.36 ratchov 1057: i->w.sub.done -= idone;
1.1 ratchov 1058: }
1.28 ratchov 1059: abuf_rdiscard(ibuf, idone);
1060: p->u.sub.lat -= idone / ibuf->bpf;
1.12 ratchov 1061: return 1;
1.1 ratchov 1062: }
1063:
1064: int
1065: sub_out(struct aproc *p, struct abuf *obuf)
1066: {
1067: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1068: struct abuf *i, *inext;
1.28 ratchov 1069: unsigned idone;
1.1 ratchov 1070:
1.12 ratchov 1071: if (!ABUF_WOK(obuf))
1072: return 0;
1.28 ratchov 1073: if (!abuf_fill(ibuf))
1074: return 0; /* eof */
1075: idone = ibuf->len;
1.12 ratchov 1076: for (i = LIST_FIRST(&p->obuflist); i != NULL; i = inext) {
1077: inext = LIST_NEXT(i, oent);
1.28 ratchov 1078: sub_bcopy(ibuf, i);
1.36 ratchov 1079: if (idone > i->w.sub.done)
1080: idone = i->w.sub.done;
1.12 ratchov 1081: if (!abuf_flush(i))
1082: continue;
1.1 ratchov 1083: }
1.28 ratchov 1084: if (LIST_EMPTY(&p->obuflist) || idone == 0)
1085: return 0;
1.1 ratchov 1086: LIST_FOREACH(i, &p->obuflist, oent) {
1.36 ratchov 1087: i->w.sub.done -= idone;
1.1 ratchov 1088: }
1.28 ratchov 1089: abuf_rdiscard(ibuf, idone);
1090: p->u.sub.lat -= idone / ibuf->bpf;
1.1 ratchov 1091: return 1;
1092: }
1093:
1094: void
1095: sub_eof(struct aproc *p, struct abuf *ibuf)
1096: {
1.8 ratchov 1097: aproc_del(p);
1.1 ratchov 1098: }
1099:
1100: void
1101: sub_hup(struct aproc *p, struct abuf *obuf)
1102: {
1.28 ratchov 1103: struct abuf *i, *ibuf = LIST_FIRST(&p->ibuflist);
1104: unsigned idone;
1.1 ratchov 1105:
1.28 ratchov 1106: if (!aproc_inuse(p)) {
1.42 ratchov 1107: #ifdef DEBUG
1108: if (debug_level >= 3) {
1109: aproc_dbg(p);
1110: dbg_puts(": running other streams\n");
1111: }
1112: #endif
1.28 ratchov 1113: /*
1.33 ratchov 1114: * Find a blocked output.
1.28 ratchov 1115: */
1116: idone = ibuf->len;
1117: LIST_FOREACH(i, &p->obuflist, oent) {
1.36 ratchov 1118: if (ABUF_WOK(i) && i->w.sub.done < ibuf->used) {
1.28 ratchov 1119: abuf_run(i);
1120: return;
1121: }
1.36 ratchov 1122: if (idone > i->w.sub.done)
1123: idone = i->w.sub.done;
1.28 ratchov 1124: }
1125: /*
1.33 ratchov 1126: * No blocked outputs. Check if input is blocked.
1.28 ratchov 1127: */
1128: if (LIST_EMPTY(&p->obuflist) || idone == ibuf->used)
1129: abuf_run(ibuf);
1130: }
1.1 ratchov 1131: }
1132:
1133: void
1134: sub_newout(struct aproc *p, struct abuf *obuf)
1135: {
1.42 ratchov 1136: #ifdef DEBUG
1137: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1138:
1139: if (!ibuf || obuf->cmin < ibuf->cmin || obuf->cmax > ibuf->cmax) {
1140: dbg_puts("newout: channel ranges mismatch\n");
1141: dbg_panic();
1142: }
1143: #endif
1.22 ratchov 1144: p->u.sub.idle = 0;
1.36 ratchov 1145: obuf->w.sub.done = 0;
1146: obuf->w.sub.xrun = XRUN_IGNORE;
1.1 ratchov 1147: }
1148:
1.12 ratchov 1149: void
1150: sub_ipos(struct aproc *p, struct abuf *ibuf, int delta)
1151: {
1152: p->u.sub.lat += delta;
1.42 ratchov 1153: #ifdef DEBUG
1154: if (debug_level >= 4) {
1155: aproc_dbg(p);
1156: dbg_puts(": ipos: lat = ");
1157: dbg_puti(p->u.sub.lat);
1158: dbg_puts("/");
1159: dbg_puti(p->u.sub.maxlat);
1160: dbg_puts(" fr\n");
1161: }
1162: #endif
1.38 ratchov 1163: if (p->u.sub.ctl)
1164: ctl_ontick(p->u.sub.ctl, delta);
1.12 ratchov 1165: aproc_ipos(p, ibuf, delta);
1166: }
1167:
1.1 ratchov 1168: struct aproc_ops sub_ops = {
1.12 ratchov 1169: "sub",
1170: sub_in,
1171: sub_out,
1172: sub_eof,
1173: sub_hup,
1174: NULL,
1175: sub_newout,
1176: sub_ipos,
1177: aproc_opos,
1178: NULL
1.1 ratchov 1179: };
1180:
1181: struct aproc *
1.38 ratchov 1182: sub_new(char *name, int maxlat, struct aproc *ctl)
1.1 ratchov 1183: {
1184: struct aproc *p;
1185:
1.12 ratchov 1186: p = aproc_new(&sub_ops, name);
1.22 ratchov 1187: p->u.sub.idle = 0;
1.12 ratchov 1188: p->u.sub.lat = 0;
1189: p->u.sub.maxlat = maxlat;
1.38 ratchov 1190: p->u.sub.ctl = ctl;
1.1 ratchov 1191: return p;
1.22 ratchov 1192: }
1193:
1194: void
1195: sub_clear(struct aproc *p)
1196: {
1197: p->u.mix.lat = 0;
1.1 ratchov 1198: }
1199:
1200: /*
1201: * Convert one block.
1202: */
1203: void
1.15 ratchov 1204: resamp_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
1205: {
1206: unsigned inch;
1207: short *idata;
1.27 ratchov 1208: unsigned oblksz;
1.15 ratchov 1209: unsigned ifr;
1210: unsigned onch;
1.27 ratchov 1211: int s1, s2, diff;
1.15 ratchov 1212: short *odata;
1.27 ratchov 1213: unsigned iblksz;
1.15 ratchov 1214: unsigned ofr;
1215: unsigned c;
1.31 ratchov 1216: short *ctxbuf, *ctx;
1.27 ratchov 1217: unsigned ctx_start;
1.15 ratchov 1218: unsigned icount, ocount;
1219:
1220: /*
1221: * Calculate max frames readable at once from the input buffer.
1222: */
1223: idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
1224: ifr = icount / ibuf->bpf;
1225: icount = ifr * ibuf->bpf;
1226:
1227: odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
1228: ofr = ocount / obuf->bpf;
1229: ocount = ofr * obuf->bpf;
1230:
1231: /*
1232: * Partially copy structures into local variables, to avoid
1233: * unnecessary indirections; this also allows the compiler to
1234: * order local variables more "cache-friendly".
1235: */
1.27 ratchov 1236: diff = p->u.resamp.diff;
1.15 ratchov 1237: inch = ibuf->cmax - ibuf->cmin + 1;
1.26 ratchov 1238: iblksz = p->u.resamp.iblksz;
1.15 ratchov 1239: onch = obuf->cmax - obuf->cmin + 1;
1.26 ratchov 1240: oblksz = p->u.resamp.oblksz;
1.15 ratchov 1241: ctxbuf = p->u.resamp.ctx;
1.27 ratchov 1242: ctx_start = p->u.resamp.ctx_start;
1.15 ratchov 1243:
1244: /*
1245: * Start conversion.
1246: */
1.42 ratchov 1247: #ifdef DEBUG
1248: if (debug_level >= 4) {
1249: aproc_dbg(p);
1250: dbg_puts(": resamp starting ifr = ");
1251: dbg_putu(ifr);
1252: dbg_puts(", ofr = ");
1253: dbg_puti(ofr);
1254: dbg_puts(" fr\n");
1255: }
1256: #endif
1.15 ratchov 1257: for (;;) {
1.27 ratchov 1258: if (diff < 0) {
1.15 ratchov 1259: if (ifr == 0)
1260: break;
1.27 ratchov 1261: ctx_start ^= 1;
1262: ctx = ctxbuf + ctx_start;
1.15 ratchov 1263: for (c = inch; c > 0; c--) {
1.29 ratchov 1264: *ctx = *idata++;
1.27 ratchov 1265: ctx += RESAMP_NCTX;
1.15 ratchov 1266: }
1.27 ratchov 1267: diff += oblksz;
1.15 ratchov 1268: ifr--;
1.27 ratchov 1269: } else {
1270: if (ofr == 0)
1271: break;
1272: ctx = ctxbuf;
1273: for (c = onch; c > 0; c--) {
1.29 ratchov 1274: s1 = ctx[ctx_start];
1275: s2 = ctx[ctx_start ^ 1];
1.27 ratchov 1276: ctx += RESAMP_NCTX;
1277: *odata++ = s1 + (s2 - s1) * diff / (int)oblksz;
1278: }
1279: diff -= iblksz;
1280: ofr--;
1.15 ratchov 1281: }
1282: }
1.27 ratchov 1283: p->u.resamp.diff = diff;
1284: p->u.resamp.ctx_start = ctx_start;
1.42 ratchov 1285: #ifdef DEBUG
1286: if (debug_level >= 4) {
1287: aproc_dbg(p);
1288: dbg_puts(": resamp done ifr = ");
1289: dbg_putu(ifr);
1290: dbg_puts(", ofr = ");
1291: dbg_puti(ofr);
1292: dbg_puts(" fr\n");
1293: }
1294: #endif
1.15 ratchov 1295: /*
1296: * Update FIFO pointers.
1297: */
1298: icount -= ifr * ibuf->bpf;
1299: ocount -= ofr * obuf->bpf;
1300: abuf_rdiscard(ibuf, icount);
1301: abuf_wcommit(obuf, ocount);
1302: }
1303:
1304: int
1305: resamp_in(struct aproc *p, struct abuf *ibuf)
1306: {
1307: struct abuf *obuf = LIST_FIRST(&p->obuflist);
1308:
1309: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1310: return 0;
1311: resamp_bcopy(p, ibuf, obuf);
1312: if (!abuf_flush(obuf))
1313: return 0;
1314: return 1;
1315: }
1316:
1317: int
1318: resamp_out(struct aproc *p, struct abuf *obuf)
1319: {
1320: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1321:
1322: if (!abuf_fill(ibuf))
1323: return 0;
1324: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1325: return 0;
1326: resamp_bcopy(p, ibuf, obuf);
1327: return 1;
1328: }
1329:
1330: void
1331: resamp_eof(struct aproc *p, struct abuf *ibuf)
1332: {
1333: aproc_del(p);
1334: }
1335:
1336: void
1337: resamp_hup(struct aproc *p, struct abuf *obuf)
1338: {
1339: aproc_del(p);
1340: }
1341:
1342: void
1343: resamp_ipos(struct aproc *p, struct abuf *ibuf, int delta)
1344: {
1.31 ratchov 1345: struct abuf *obuf = LIST_FIRST(&p->obuflist);
1.15 ratchov 1346: long long ipos;
1.45 ! ratchov 1347:
! 1348: ipos = (long long)delta * p->u.resamp.oblksz + p->u.resamp.idelta;
! 1349: p->u.resamp.idelta = ipos % p->u.resamp.iblksz;
! 1350: abuf_ipos(obuf, ipos / (int)p->u.resamp.iblksz);
1.15 ratchov 1351: }
1352:
1353: void
1354: resamp_opos(struct aproc *p, struct abuf *obuf, int delta)
1355: {
1356: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1357: long long opos;
1358:
1.45 ! ratchov 1359: opos = (long long)delta * p->u.resamp.iblksz + p->u.resamp.odelta;
! 1360: p->u.resamp.odelta = opos % p->u.resamp.oblksz;
! 1361: abuf_opos(ibuf, opos / p->u.resamp.oblksz);
1.15 ratchov 1362: }
1363:
1364: struct aproc_ops resamp_ops = {
1365: "resamp",
1366: resamp_in,
1367: resamp_out,
1368: resamp_eof,
1369: resamp_hup,
1370: NULL,
1371: NULL,
1372: resamp_ipos,
1373: resamp_opos,
1374: NULL
1375: };
1376:
1377: struct aproc *
1.26 ratchov 1378: resamp_new(char *name, unsigned iblksz, unsigned oblksz)
1.15 ratchov 1379: {
1380: struct aproc *p;
1.16 ratchov 1381: unsigned i;
1.15 ratchov 1382:
1383: p = aproc_new(&resamp_ops, name);
1.26 ratchov 1384: p->u.resamp.iblksz = iblksz;
1385: p->u.resamp.oblksz = oblksz;
1.27 ratchov 1386: p->u.resamp.diff = 0;
1.15 ratchov 1387: p->u.resamp.idelta = 0;
1388: p->u.resamp.odelta = 0;
1.27 ratchov 1389: p->u.resamp.ctx_start = 0;
1390: for (i = 0; i < NCHAN_MAX * RESAMP_NCTX; i++)
1.16 ratchov 1391: p->u.resamp.ctx[i] = 0;
1.42 ratchov 1392: #ifdef DEBUG
1393: if (debug_level >= 3) {
1394: aproc_dbg(p);
1395: dbg_puts(": new ");
1396: dbg_putu(iblksz);
1397: dbg_puts("/");
1398: dbg_putu(oblksz);
1399: dbg_puts("\n");
1400: }
1401: #endif
1.17 ratchov 1402: return p;
1403: }
1404:
1405: /*
1406: * Convert one block.
1407: */
1408: void
1409: cmap_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
1410: {
1411: unsigned inch;
1412: short *idata;
1413: unsigned onch;
1414: short *odata;
1415: short *ctx, *ictx, *octx;
1416: unsigned c, f, scount, icount, ocount;
1417:
1418: /*
1419: * Calculate max frames readable at once from the input buffer.
1420: */
1421: idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
1422: icount /= ibuf->bpf;
1423: if (icount == 0)
1424: return;
1425: odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
1426: ocount /= obuf->bpf;
1427: if (ocount == 0)
1428: return;
1429: scount = icount < ocount ? icount : ocount;
1430: inch = ibuf->cmax - ibuf->cmin + 1;
1431: onch = obuf->cmax - obuf->cmin + 1;
1432: ictx = p->u.cmap.ctx + ibuf->cmin;
1433: octx = p->u.cmap.ctx + obuf->cmin;
1434:
1435: for (f = scount; f > 0; f--) {
1436: ctx = ictx;
1437: for (c = inch; c > 0; c--) {
1438: *ctx = *idata;
1439: idata++;
1440: ctx++;
1441: }
1442: ctx = octx;
1443: for (c = onch; c > 0; c--) {
1444: *odata = *ctx;
1445: odata++;
1446: ctx++;
1447: }
1448: }
1.42 ratchov 1449: #ifdef DEBUG
1450: if (debug_level >= 4) {
1451: aproc_dbg(p);
1452: dbg_puts(": bcopy ");
1453: dbg_putu(scount);
1454: dbg_puts(" fr\n");
1455: }
1456: #endif
1.17 ratchov 1457: abuf_rdiscard(ibuf, scount * ibuf->bpf);
1458: abuf_wcommit(obuf, scount * obuf->bpf);
1459: }
1460:
1461: int
1462: cmap_in(struct aproc *p, struct abuf *ibuf)
1463: {
1464: struct abuf *obuf = LIST_FIRST(&p->obuflist);
1465:
1466: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1467: return 0;
1468: cmap_bcopy(p, ibuf, obuf);
1469: if (!abuf_flush(obuf))
1470: return 0;
1471: return 1;
1472: }
1473:
1474: int
1475: cmap_out(struct aproc *p, struct abuf *obuf)
1476: {
1477: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1478:
1479: if (!abuf_fill(ibuf))
1480: return 0;
1481: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1482: return 0;
1483: cmap_bcopy(p, ibuf, obuf);
1484: return 1;
1485: }
1486:
1487: void
1488: cmap_eof(struct aproc *p, struct abuf *ibuf)
1489: {
1490: aproc_del(p);
1491: }
1492:
1493: void
1494: cmap_hup(struct aproc *p, struct abuf *obuf)
1495: {
1496: aproc_del(p);
1497: }
1498:
1499: struct aproc_ops cmap_ops = {
1500: "cmap",
1501: cmap_in,
1502: cmap_out,
1503: cmap_eof,
1504: cmap_hup,
1505: NULL,
1506: NULL,
1.19 ratchov 1507: aproc_ipos,
1508: aproc_opos,
1.17 ratchov 1509: NULL
1510: };
1511:
1512: struct aproc *
1513: cmap_new(char *name, struct aparams *ipar, struct aparams *opar)
1514: {
1515: struct aproc *p;
1516: unsigned i;
1517:
1518: p = aproc_new(&cmap_ops, name);
1519: for (i = 0; i < NCHAN_MAX; i++)
1520: p->u.cmap.ctx[i] = 0;
1.42 ratchov 1521: #ifdef DEBUG
1522: if (debug_level >= 3) {
1523: aproc_dbg(p);
1524: dbg_puts(": new ");
1525: aparams_dbg(ipar);
1526: dbg_puts(" -> ");
1527: aparams_dbg(opar);
1528: dbg_puts("\n");
1529: }
1530: #endif
1.19 ratchov 1531: return p;
1532: }
1533:
1534: /*
1535: * Convert one block.
1536: */
1537: void
1538: enc_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
1539: {
1540: unsigned nch, scount, icount, ocount;
1541: unsigned f;
1542: short *idata;
1543: int s;
1544: unsigned oshift;
1545: int osigbit;
1546: unsigned obps;
1547: unsigned i;
1548: unsigned char *odata;
1549: int obnext;
1550: int osnext;
1551:
1552: /*
1553: * Calculate max frames readable at once from the input buffer.
1554: */
1555: idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
1556: icount /= ibuf->bpf;
1557: if (icount == 0)
1558: return;
1559: odata = abuf_wgetblk(obuf, &ocount, 0);
1560: ocount /= obuf->bpf;
1561: if (ocount == 0)
1562: return;
1563: scount = (icount < ocount) ? icount : ocount;
1564: nch = ibuf->cmax - ibuf->cmin + 1;
1.42 ratchov 1565: #ifdef DEBUG
1566: if (debug_level >= 4) {
1567: aproc_dbg(p);
1568: dbg_puts(": bcopy ");
1569: dbg_putu(scount);
1570: dbg_puts(" fr * ");
1571: dbg_putu(nch);
1572: dbg_puts(" ch\n");
1573: }
1574: #endif
1.19 ratchov 1575: /*
1576: * Partially copy structures into local variables, to avoid
1577: * unnecessary indirections; this also allows the compiler to
1578: * order local variables more "cache-friendly".
1579: */
1580: oshift = p->u.conv.shift;
1581: osigbit = p->u.conv.sigbit;
1582: obps = p->u.conv.bps;
1583: obnext = p->u.conv.bnext;
1584: osnext = p->u.conv.snext;
1585:
1586: /*
1587: * Start conversion.
1588: */
1589: odata += p->u.conv.bfirst;
1590: for (f = scount * nch; f > 0; f--) {
1591: s = *idata++;
1592: s <<= 16;
1593: s >>= oshift;
1594: s ^= osigbit;
1595: for (i = obps; i > 0; i--) {
1596: *odata = (unsigned char)s;
1597: s >>= 8;
1598: odata += obnext;
1599: }
1600: odata += osnext;
1601: }
1602:
1603: /*
1604: * Update FIFO pointers.
1605: */
1606: abuf_rdiscard(ibuf, scount * ibuf->bpf);
1607: abuf_wcommit(obuf, scount * obuf->bpf);
1608: }
1609:
1610: int
1611: enc_in(struct aproc *p, struct abuf *ibuf)
1612: {
1613: struct abuf *obuf = LIST_FIRST(&p->obuflist);
1614:
1615: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1616: return 0;
1617: enc_bcopy(p, ibuf, obuf);
1618: if (!abuf_flush(obuf))
1619: return 0;
1620: return 1;
1621: }
1622:
1623: int
1624: enc_out(struct aproc *p, struct abuf *obuf)
1625: {
1626: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1627:
1628: if (!abuf_fill(ibuf))
1629: return 0;
1630: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1631: return 0;
1632: enc_bcopy(p, ibuf, obuf);
1633: return 1;
1634: }
1635:
1636: void
1637: enc_eof(struct aproc *p, struct abuf *ibuf)
1638: {
1639: aproc_del(p);
1640: }
1641:
1642: void
1643: enc_hup(struct aproc *p, struct abuf *obuf)
1644: {
1645: aproc_del(p);
1646: }
1647:
1648: struct aproc_ops enc_ops = {
1649: "enc",
1650: enc_in,
1651: enc_out,
1652: enc_eof,
1653: enc_hup,
1654: NULL,
1655: NULL,
1656: aproc_ipos,
1657: aproc_opos,
1658: NULL
1659: };
1660:
1661: struct aproc *
1662: enc_new(char *name, struct aparams *par)
1663: {
1664: struct aproc *p;
1665:
1666: p = aproc_new(&enc_ops, name);
1667: p->u.conv.bps = par->bps;
1668: p->u.conv.sigbit = par->sig ? 0 : 1 << (par->bits - 1);
1669: if (par->msb) {
1670: p->u.conv.shift = 32 - par->bps * 8;
1671: } else {
1672: p->u.conv.shift = 32 - par->bits;
1673: }
1674: if (!par->le) {
1675: p->u.conv.bfirst = par->bps - 1;
1676: p->u.conv.bnext = -1;
1677: p->u.conv.snext = 2 * par->bps;
1678: } else {
1679: p->u.conv.bfirst = 0;
1680: p->u.conv.bnext = 1;
1681: p->u.conv.snext = 0;
1682: }
1.42 ratchov 1683: #ifdef DEBUG
1684: if (debug_level >= 3) {
1685: aproc_dbg(p);
1686: dbg_puts(": new ");
1687: aparams_dbg(par);
1688: dbg_puts("\n");
1689: }
1690: #endif
1.19 ratchov 1691: return p;
1692: }
1693:
1694: /*
1695: * Convert one block.
1696: */
1697: void
1698: dec_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
1699: {
1700: unsigned nch, scount, icount, ocount;
1701: unsigned f;
1702: unsigned ibps;
1703: unsigned i;
1704: int s = 0xdeadbeef;
1705: unsigned char *idata;
1706: int ibnext;
1707: int isnext;
1708: int isigbit;
1709: unsigned ishift;
1710: short *odata;
1711:
1712: /*
1713: * Calculate max frames readable at once from the input buffer.
1714: */
1715: idata = abuf_rgetblk(ibuf, &icount, 0);
1716: icount /= ibuf->bpf;
1717: if (icount == 0)
1718: return;
1719: odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
1720: ocount /= obuf->bpf;
1721: if (ocount == 0)
1722: return;
1723: scount = (icount < ocount) ? icount : ocount;
1724: nch = obuf->cmax - obuf->cmin + 1;
1.42 ratchov 1725: #ifdef DEBUG
1726: if (debug_level >= 4) {
1727: aproc_dbg(p);
1728: dbg_puts(": bcopy ");
1729: dbg_putu(scount);
1730: dbg_puts(" fr * ");
1731: dbg_putu(nch);
1732: dbg_puts(" ch\n");
1733: }
1734: #endif
1.19 ratchov 1735: /*
1736: * Partially copy structures into local variables, to avoid
1737: * unnecessary indirections; this also allows the compiler to
1738: * order local variables more "cache-friendly".
1739: */
1740: ibps = p->u.conv.bps;
1741: ibnext = p->u.conv.bnext;
1742: isigbit = p->u.conv.sigbit;
1743: ishift = p->u.conv.shift;
1744: isnext = p->u.conv.snext;
1745:
1746: /*
1747: * Start conversion.
1748: */
1749: idata += p->u.conv.bfirst;
1750: for (f = scount * nch; f > 0; f--) {
1751: for (i = ibps; i > 0; i--) {
1752: s <<= 8;
1753: s |= *idata;
1754: idata += ibnext;
1755: }
1756: idata += isnext;
1757: s ^= isigbit;
1758: s <<= ishift;
1759: s >>= 16;
1760: *odata++ = s;
1761: }
1762:
1763: /*
1764: * Update FIFO pointers.
1765: */
1766: abuf_rdiscard(ibuf, scount * ibuf->bpf);
1767: abuf_wcommit(obuf, scount * obuf->bpf);
1768: }
1769:
1770: int
1771: dec_in(struct aproc *p, struct abuf *ibuf)
1772: {
1773: struct abuf *obuf = LIST_FIRST(&p->obuflist);
1774:
1775: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1776: return 0;
1777: dec_bcopy(p, ibuf, obuf);
1778: if (!abuf_flush(obuf))
1779: return 0;
1780: return 1;
1781: }
1782:
1783: int
1784: dec_out(struct aproc *p, struct abuf *obuf)
1785: {
1786: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1787:
1788: if (!abuf_fill(ibuf))
1789: return 0;
1790: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1791: return 0;
1792: dec_bcopy(p, ibuf, obuf);
1793: return 1;
1794: }
1795:
1796: void
1797: dec_eof(struct aproc *p, struct abuf *ibuf)
1798: {
1799: aproc_del(p);
1800: }
1801:
1802: void
1803: dec_hup(struct aproc *p, struct abuf *obuf)
1804: {
1805: aproc_del(p);
1806: }
1807:
1808: struct aproc_ops dec_ops = {
1809: "dec",
1810: dec_in,
1811: dec_out,
1812: dec_eof,
1813: dec_hup,
1814: NULL,
1815: NULL,
1816: aproc_ipos,
1817: aproc_opos,
1818: NULL
1819: };
1820:
1821: struct aproc *
1822: dec_new(char *name, struct aparams *par)
1823: {
1824: struct aproc *p;
1825:
1826: p = aproc_new(&dec_ops, name);
1827: p->u.conv.bps = par->bps;
1828: p->u.conv.sigbit = par->sig ? 0 : 1 << (par->bits - 1);
1829: if (par->msb) {
1830: p->u.conv.shift = 32 - par->bps * 8;
1831: } else {
1832: p->u.conv.shift = 32 - par->bits;
1833: }
1834: if (par->le) {
1835: p->u.conv.bfirst = par->bps - 1;
1836: p->u.conv.bnext = -1;
1837: p->u.conv.snext = 2 * par->bps;
1838: } else {
1839: p->u.conv.bfirst = 0;
1840: p->u.conv.bnext = 1;
1841: p->u.conv.snext = 0;
1842: }
1.42 ratchov 1843: #ifdef DEBUG
1844: if (debug_level >= 3) {
1845: aproc_dbg(p);
1846: dbg_puts(": new ");
1847: aparams_dbg(par);
1848: dbg_puts("\n");
1849: }
1850: #endif
1.1 ratchov 1851: return p;
1852: }