Annotation of src/usr.bin/aucat/aproc.c, Revision 1.43
1.43 ! ratchov 1: /* $OpenBSD: aproc.c,v 1.42 2010/01/10 21:47:41 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: #ifdef DEBUG
! 908: if (debug_level >= 3) {
! 909: aproc_dbg(p);
! 910: dbg_puts(": prime1: lat/maxlat=");
! 911: dbg_puti(p->u.mix.lat);
! 912: dbg_puts("/");
! 913: dbg_puti(p->u.mix.maxlat);
! 914: dbg_puts("\n");
! 915: }
! 916: #endif
! 917: for (;;) {
! 918: if (!ABUF_WOK(obuf))
! 919: break;
! 920: todo = (p->u.mix.maxlat - p->u.mix.lat) * obuf->bpf;
! 921: if (todo == 0)
! 922: break;
! 923: mix_bzero(obuf, obuf->len);
! 924: count = obuf->w.mix.todo;
! 925: if (count > todo)
! 926: count = todo;
! 927: obuf->w.mix.todo -= count;
! 928: p->u.mix.lat += count / obuf->bpf;
! 929: abuf_wcommit(obuf, count);
! 930: abuf_flush(obuf);
! 931: }
! 932: #ifdef DEBUG
! 933: if (debug_level >= 3) {
! 934: aproc_dbg(p);
! 935: dbg_puts(": prime: lat/maxlat=");
! 936: dbg_puti(p->u.mix.lat);
! 937: dbg_puts("/");
! 938: dbg_puti(p->u.mix.maxlat);
! 939: dbg_puts("\n");
! 940: }
! 941: #endif
1.22 ratchov 942: }
943:
1.1 ratchov 944: /*
945: * Copy data from ibuf to obuf.
946: */
947: void
948: sub_bcopy(struct abuf *ibuf, struct abuf *obuf)
949: {
1.13 ratchov 950: short *idata, *odata;
951: unsigned i, j, ocnt, inext, istart;
1.1 ratchov 952: unsigned icount, ocount, scount;
953:
1.36 ratchov 954: idata = (short *)abuf_rgetblk(ibuf, &icount, obuf->w.sub.done);
1.13 ratchov 955: icount /= ibuf->bpf;
1.1 ratchov 956: if (icount == 0)
957: return;
1.13 ratchov 958: odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
959: ocount /= obuf->bpf;
1.1 ratchov 960: if (ocount == 0)
961: return;
1.13 ratchov 962: istart = obuf->cmin - ibuf->cmin;
963: inext = ibuf->cmax - obuf->cmax + istart;
964: ocnt = obuf->cmax - obuf->cmin + 1;
1.1 ratchov 965: scount = (icount < ocount) ? icount : ocount;
1.13 ratchov 966: idata += istart;
967: for (i = scount; i > 0; i--) {
968: for (j = ocnt; j > 0; j--) {
969: *odata = *idata;
970: odata++;
971: idata++;
972: }
973: idata += inext;
974: }
1.31 ratchov 975: abuf_wcommit(obuf, scount * obuf->bpf);
1.36 ratchov 976: obuf->w.sub.done += scount * ibuf->bpf;
1.42 ratchov 977: #ifdef DEBUG
978: if (debug_level >= 4) {
979: abuf_dbg(obuf);
980: dbg_puts(": bcopy ");
981: dbg_putu(scount);
982: dbg_puts(" fr\n");
983: }
984: #endif
1.1 ratchov 985: }
986:
1.28 ratchov 987: /*
1.33 ratchov 988: * Handle buffer overruns. Return 0 if the stream died.
1.28 ratchov 989: */
990: int
991: sub_xrun(struct abuf *ibuf, struct abuf *i)
992: {
993: unsigned fdrop;
994:
1.36 ratchov 995: if (i->w.sub.done > 0)
1.28 ratchov 996: return 1;
1.36 ratchov 997: if (i->w.sub.xrun == XRUN_ERROR) {
1.28 ratchov 998: abuf_eof(i);
999: return 0;
1000: }
1001: fdrop = ibuf->used / ibuf->bpf;
1.42 ratchov 1002: #ifdef DEBUG
1003: if (debug_level >= 3) {
1004: abuf_dbg(i);
1005: dbg_puts(": overrun, silence ");
1006: dbg_putu(fdrop);
1007: dbg_puts(" + ");
1008: dbg_putu(i->silence / i->bpf);
1009: dbg_puts("\n");
1010: }
1011: #endif
1.36 ratchov 1012: if (i->w.sub.xrun == XRUN_SYNC)
1.28 ratchov 1013: i->silence += fdrop * i->bpf;
1014: else {
1015: abuf_ipos(i, -(int)fdrop);
1016: if (i->duplex) {
1.42 ratchov 1017: #ifdef DEBUG
1018: if (debug_level >= 3) {
1019: abuf_dbg(i->duplex);
1020: dbg_puts(": full-duplex resync\n");
1021: }
1022: #endif
1.28 ratchov 1023: i->duplex->silence += fdrop * i->duplex->bpf;
1024: abuf_opos(i->duplex, -(int)fdrop);
1025: }
1026: }
1.36 ratchov 1027: i->w.sub.done += fdrop * ibuf->bpf;
1.28 ratchov 1028: return 1;
1029: }
1030:
1.1 ratchov 1031: int
1032: sub_in(struct aproc *p, struct abuf *ibuf)
1033: {
1034: struct abuf *i, *inext;
1.28 ratchov 1035: unsigned idone;
1.31 ratchov 1036:
1.12 ratchov 1037: if (!ABUF_ROK(ibuf))
1038: return 0;
1.28 ratchov 1039: idone = ibuf->len;
1.12 ratchov 1040: for (i = LIST_FIRST(&p->obuflist); i != NULL; i = inext) {
1.1 ratchov 1041: inext = LIST_NEXT(i, oent);
1.5 ratchov 1042: if (!ABUF_WOK(i)) {
1.38 ratchov 1043: if (p->flags & APROC_DROP) {
1.28 ratchov 1044: if (!sub_xrun(ibuf, i))
1.5 ratchov 1045: continue;
1046: }
1.12 ratchov 1047: } else
1.1 ratchov 1048: sub_bcopy(ibuf, i);
1.36 ratchov 1049: if (idone > i->w.sub.done)
1050: idone = i->w.sub.done;
1.12 ratchov 1051: if (!abuf_flush(i))
1052: continue;
1.1 ratchov 1053: }
1.28 ratchov 1054: if (LIST_EMPTY(&p->obuflist)) {
1.38 ratchov 1055: if (p->flags & APROC_QUIT) {
1.28 ratchov 1056: aproc_del(p);
1057: return 0;
1058: }
1.38 ratchov 1059: if (!(p->flags & APROC_DROP))
1.28 ratchov 1060: return 0;
1061: idone = ibuf->used;
1062: p->u.sub.idle += idone / ibuf->bpf;
1063: }
1064: if (idone == 0)
1065: return 0;
1.1 ratchov 1066: LIST_FOREACH(i, &p->obuflist, oent) {
1.36 ratchov 1067: i->w.sub.done -= idone;
1.1 ratchov 1068: }
1.28 ratchov 1069: abuf_rdiscard(ibuf, idone);
1070: p->u.sub.lat -= idone / ibuf->bpf;
1.12 ratchov 1071: return 1;
1.1 ratchov 1072: }
1073:
1074: int
1075: sub_out(struct aproc *p, struct abuf *obuf)
1076: {
1077: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1078: struct abuf *i, *inext;
1.28 ratchov 1079: unsigned idone;
1.1 ratchov 1080:
1.12 ratchov 1081: if (!ABUF_WOK(obuf))
1082: return 0;
1.28 ratchov 1083: if (!abuf_fill(ibuf))
1084: return 0; /* eof */
1085: idone = ibuf->len;
1.12 ratchov 1086: for (i = LIST_FIRST(&p->obuflist); i != NULL; i = inext) {
1087: inext = LIST_NEXT(i, oent);
1.28 ratchov 1088: sub_bcopy(ibuf, i);
1.36 ratchov 1089: if (idone > i->w.sub.done)
1090: idone = i->w.sub.done;
1.12 ratchov 1091: if (!abuf_flush(i))
1092: continue;
1.1 ratchov 1093: }
1.28 ratchov 1094: if (LIST_EMPTY(&p->obuflist) || idone == 0)
1095: return 0;
1.1 ratchov 1096: LIST_FOREACH(i, &p->obuflist, oent) {
1.36 ratchov 1097: i->w.sub.done -= idone;
1.1 ratchov 1098: }
1.28 ratchov 1099: abuf_rdiscard(ibuf, idone);
1100: p->u.sub.lat -= idone / ibuf->bpf;
1.1 ratchov 1101: return 1;
1102: }
1103:
1104: void
1105: sub_eof(struct aproc *p, struct abuf *ibuf)
1106: {
1.8 ratchov 1107: aproc_del(p);
1.1 ratchov 1108: }
1109:
1110: void
1111: sub_hup(struct aproc *p, struct abuf *obuf)
1112: {
1.28 ratchov 1113: struct abuf *i, *ibuf = LIST_FIRST(&p->ibuflist);
1114: unsigned idone;
1.1 ratchov 1115:
1.28 ratchov 1116: if (!aproc_inuse(p)) {
1.42 ratchov 1117: #ifdef DEBUG
1118: if (debug_level >= 3) {
1119: aproc_dbg(p);
1120: dbg_puts(": running other streams\n");
1121: }
1122: #endif
1.28 ratchov 1123: /*
1.33 ratchov 1124: * Find a blocked output.
1.28 ratchov 1125: */
1126: idone = ibuf->len;
1127: LIST_FOREACH(i, &p->obuflist, oent) {
1.36 ratchov 1128: if (ABUF_WOK(i) && i->w.sub.done < ibuf->used) {
1.28 ratchov 1129: abuf_run(i);
1130: return;
1131: }
1.36 ratchov 1132: if (idone > i->w.sub.done)
1133: idone = i->w.sub.done;
1.28 ratchov 1134: }
1135: /*
1.33 ratchov 1136: * No blocked outputs. Check if input is blocked.
1.28 ratchov 1137: */
1138: if (LIST_EMPTY(&p->obuflist) || idone == ibuf->used)
1139: abuf_run(ibuf);
1140: }
1.1 ratchov 1141: }
1142:
1143: void
1144: sub_newout(struct aproc *p, struct abuf *obuf)
1145: {
1.42 ratchov 1146: #ifdef DEBUG
1147: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1148:
1149: if (!ibuf || obuf->cmin < ibuf->cmin || obuf->cmax > ibuf->cmax) {
1150: dbg_puts("newout: channel ranges mismatch\n");
1151: dbg_panic();
1152: }
1153: #endif
1.22 ratchov 1154: p->u.sub.idle = 0;
1.36 ratchov 1155: obuf->w.sub.done = 0;
1156: obuf->w.sub.xrun = XRUN_IGNORE;
1.1 ratchov 1157: }
1158:
1.12 ratchov 1159: void
1160: sub_ipos(struct aproc *p, struct abuf *ibuf, int delta)
1161: {
1162: p->u.sub.lat += delta;
1.42 ratchov 1163: #ifdef DEBUG
1164: if (debug_level >= 4) {
1165: aproc_dbg(p);
1166: dbg_puts(": ipos: lat = ");
1167: dbg_puti(p->u.sub.lat);
1168: dbg_puts("/");
1169: dbg_puti(p->u.sub.maxlat);
1170: dbg_puts(" fr\n");
1171: }
1172: #endif
1.38 ratchov 1173: if (p->u.sub.ctl)
1174: ctl_ontick(p->u.sub.ctl, delta);
1.12 ratchov 1175: aproc_ipos(p, ibuf, delta);
1176: }
1177:
1.1 ratchov 1178: struct aproc_ops sub_ops = {
1.12 ratchov 1179: "sub",
1180: sub_in,
1181: sub_out,
1182: sub_eof,
1183: sub_hup,
1184: NULL,
1185: sub_newout,
1186: sub_ipos,
1187: aproc_opos,
1188: NULL
1.1 ratchov 1189: };
1190:
1191: struct aproc *
1.38 ratchov 1192: sub_new(char *name, int maxlat, struct aproc *ctl)
1.1 ratchov 1193: {
1194: struct aproc *p;
1195:
1.12 ratchov 1196: p = aproc_new(&sub_ops, name);
1.22 ratchov 1197: p->u.sub.idle = 0;
1.12 ratchov 1198: p->u.sub.lat = 0;
1199: p->u.sub.maxlat = maxlat;
1.38 ratchov 1200: p->u.sub.ctl = ctl;
1.1 ratchov 1201: return p;
1.22 ratchov 1202: }
1203:
1204: void
1205: sub_clear(struct aproc *p)
1206: {
1207: p->u.mix.lat = 0;
1.1 ratchov 1208: }
1209:
1210: /*
1211: * Convert one block.
1212: */
1213: void
1.15 ratchov 1214: resamp_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
1215: {
1216: unsigned inch;
1217: short *idata;
1.27 ratchov 1218: unsigned oblksz;
1.15 ratchov 1219: unsigned ifr;
1220: unsigned onch;
1.27 ratchov 1221: int s1, s2, diff;
1.15 ratchov 1222: short *odata;
1.27 ratchov 1223: unsigned iblksz;
1.15 ratchov 1224: unsigned ofr;
1225: unsigned c;
1.31 ratchov 1226: short *ctxbuf, *ctx;
1.27 ratchov 1227: unsigned ctx_start;
1.15 ratchov 1228: unsigned icount, ocount;
1229:
1230: /*
1231: * Calculate max frames readable at once from the input buffer.
1232: */
1233: idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
1234: ifr = icount / ibuf->bpf;
1235: icount = ifr * ibuf->bpf;
1236:
1237: odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
1238: ofr = ocount / obuf->bpf;
1239: ocount = ofr * obuf->bpf;
1240:
1241: /*
1242: * Partially copy structures into local variables, to avoid
1243: * unnecessary indirections; this also allows the compiler to
1244: * order local variables more "cache-friendly".
1245: */
1.27 ratchov 1246: diff = p->u.resamp.diff;
1.15 ratchov 1247: inch = ibuf->cmax - ibuf->cmin + 1;
1.26 ratchov 1248: iblksz = p->u.resamp.iblksz;
1.15 ratchov 1249: onch = obuf->cmax - obuf->cmin + 1;
1.26 ratchov 1250: oblksz = p->u.resamp.oblksz;
1.15 ratchov 1251: ctxbuf = p->u.resamp.ctx;
1.27 ratchov 1252: ctx_start = p->u.resamp.ctx_start;
1.15 ratchov 1253:
1254: /*
1255: * Start conversion.
1256: */
1.42 ratchov 1257: #ifdef DEBUG
1258: if (debug_level >= 4) {
1259: aproc_dbg(p);
1260: dbg_puts(": resamp starting ifr = ");
1261: dbg_putu(ifr);
1262: dbg_puts(", ofr = ");
1263: dbg_puti(ofr);
1264: dbg_puts(" fr\n");
1265: }
1266: #endif
1.15 ratchov 1267: for (;;) {
1.27 ratchov 1268: if (diff < 0) {
1.15 ratchov 1269: if (ifr == 0)
1270: break;
1.27 ratchov 1271: ctx_start ^= 1;
1272: ctx = ctxbuf + ctx_start;
1.15 ratchov 1273: for (c = inch; c > 0; c--) {
1.29 ratchov 1274: *ctx = *idata++;
1.27 ratchov 1275: ctx += RESAMP_NCTX;
1.15 ratchov 1276: }
1.27 ratchov 1277: diff += oblksz;
1.15 ratchov 1278: ifr--;
1.27 ratchov 1279: } else {
1280: if (ofr == 0)
1281: break;
1282: ctx = ctxbuf;
1283: for (c = onch; c > 0; c--) {
1.29 ratchov 1284: s1 = ctx[ctx_start];
1285: s2 = ctx[ctx_start ^ 1];
1.27 ratchov 1286: ctx += RESAMP_NCTX;
1287: *odata++ = s1 + (s2 - s1) * diff / (int)oblksz;
1288: }
1289: diff -= iblksz;
1290: ofr--;
1.15 ratchov 1291: }
1292: }
1.27 ratchov 1293: p->u.resamp.diff = diff;
1294: p->u.resamp.ctx_start = ctx_start;
1.42 ratchov 1295: #ifdef DEBUG
1296: if (debug_level >= 4) {
1297: aproc_dbg(p);
1298: dbg_puts(": resamp done ifr = ");
1299: dbg_putu(ifr);
1300: dbg_puts(", ofr = ");
1301: dbg_puti(ofr);
1302: dbg_puts(" fr\n");
1303: }
1304: #endif
1.15 ratchov 1305: /*
1306: * Update FIFO pointers.
1307: */
1308: icount -= ifr * ibuf->bpf;
1309: ocount -= ofr * obuf->bpf;
1310: abuf_rdiscard(ibuf, icount);
1311: abuf_wcommit(obuf, ocount);
1312: }
1313:
1314: int
1315: resamp_in(struct aproc *p, struct abuf *ibuf)
1316: {
1317: struct abuf *obuf = LIST_FIRST(&p->obuflist);
1318:
1319: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1320: return 0;
1321: resamp_bcopy(p, ibuf, obuf);
1322: if (!abuf_flush(obuf))
1323: return 0;
1324: return 1;
1325: }
1326:
1327: int
1328: resamp_out(struct aproc *p, struct abuf *obuf)
1329: {
1330: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1331:
1332: if (!abuf_fill(ibuf))
1333: return 0;
1334: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1335: return 0;
1336: resamp_bcopy(p, ibuf, obuf);
1337: return 1;
1338: }
1339:
1340: void
1341: resamp_eof(struct aproc *p, struct abuf *ibuf)
1342: {
1343: aproc_del(p);
1344: }
1345:
1346: void
1347: resamp_hup(struct aproc *p, struct abuf *obuf)
1348: {
1349: aproc_del(p);
1350: }
1351:
1352: void
1353: resamp_ipos(struct aproc *p, struct abuf *ibuf, int delta)
1354: {
1.31 ratchov 1355: struct abuf *obuf = LIST_FIRST(&p->obuflist);
1.15 ratchov 1356: long long ipos;
1357: int ifac, ofac;
1358:
1.26 ratchov 1359: ifac = p->u.resamp.iblksz;
1360: ofac = p->u.resamp.oblksz;
1.15 ratchov 1361: ipos = p->u.resamp.idelta + (long long)delta * ofac;
1362: delta = (ipos + ifac - 1) / ifac;
1363: p->u.resamp.idelta = ipos - (long long)delta * ifac;
1364: abuf_ipos(obuf, delta);
1365: }
1366:
1367: void
1368: resamp_opos(struct aproc *p, struct abuf *obuf, int delta)
1369: {
1370: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1371: long long opos;
1372: int ifac, ofac;
1373:
1.26 ratchov 1374: ifac = p->u.resamp.iblksz;
1375: ofac = p->u.resamp.oblksz;
1.15 ratchov 1376: opos = p->u.resamp.odelta + (long long)delta * ifac;
1377: delta = (opos + ofac - 1) / ofac;
1378: p->u.resamp.odelta = opos - (long long)delta * ofac;
1379: abuf_opos(ibuf, delta);
1380: }
1381:
1382: struct aproc_ops resamp_ops = {
1383: "resamp",
1384: resamp_in,
1385: resamp_out,
1386: resamp_eof,
1387: resamp_hup,
1388: NULL,
1389: NULL,
1390: resamp_ipos,
1391: resamp_opos,
1392: NULL
1393: };
1394:
1395: struct aproc *
1.26 ratchov 1396: resamp_new(char *name, unsigned iblksz, unsigned oblksz)
1.15 ratchov 1397: {
1398: struct aproc *p;
1.16 ratchov 1399: unsigned i;
1.15 ratchov 1400:
1401: p = aproc_new(&resamp_ops, name);
1.26 ratchov 1402: p->u.resamp.iblksz = iblksz;
1403: p->u.resamp.oblksz = oblksz;
1.27 ratchov 1404: p->u.resamp.diff = 0;
1.15 ratchov 1405: p->u.resamp.idelta = 0;
1406: p->u.resamp.odelta = 0;
1.27 ratchov 1407: p->u.resamp.ctx_start = 0;
1408: for (i = 0; i < NCHAN_MAX * RESAMP_NCTX; i++)
1.16 ratchov 1409: p->u.resamp.ctx[i] = 0;
1.42 ratchov 1410: #ifdef DEBUG
1411: if (debug_level >= 3) {
1412: aproc_dbg(p);
1413: dbg_puts(": new ");
1414: dbg_putu(iblksz);
1415: dbg_puts("/");
1416: dbg_putu(oblksz);
1417: dbg_puts("\n");
1418: }
1419: #endif
1.17 ratchov 1420: return p;
1421: }
1422:
1423: /*
1424: * Convert one block.
1425: */
1426: void
1427: cmap_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
1428: {
1429: unsigned inch;
1430: short *idata;
1431: unsigned onch;
1432: short *odata;
1433: short *ctx, *ictx, *octx;
1434: unsigned c, f, scount, icount, ocount;
1435:
1436: /*
1437: * Calculate max frames readable at once from the input buffer.
1438: */
1439: idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
1440: icount /= ibuf->bpf;
1441: if (icount == 0)
1442: return;
1443: odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
1444: ocount /= obuf->bpf;
1445: if (ocount == 0)
1446: return;
1447: scount = icount < ocount ? icount : ocount;
1448: inch = ibuf->cmax - ibuf->cmin + 1;
1449: onch = obuf->cmax - obuf->cmin + 1;
1450: ictx = p->u.cmap.ctx + ibuf->cmin;
1451: octx = p->u.cmap.ctx + obuf->cmin;
1452:
1453: for (f = scount; f > 0; f--) {
1454: ctx = ictx;
1455: for (c = inch; c > 0; c--) {
1456: *ctx = *idata;
1457: idata++;
1458: ctx++;
1459: }
1460: ctx = octx;
1461: for (c = onch; c > 0; c--) {
1462: *odata = *ctx;
1463: odata++;
1464: ctx++;
1465: }
1466: }
1.42 ratchov 1467: #ifdef DEBUG
1468: if (debug_level >= 4) {
1469: aproc_dbg(p);
1470: dbg_puts(": bcopy ");
1471: dbg_putu(scount);
1472: dbg_puts(" fr\n");
1473: }
1474: #endif
1.17 ratchov 1475: abuf_rdiscard(ibuf, scount * ibuf->bpf);
1476: abuf_wcommit(obuf, scount * obuf->bpf);
1477: }
1478:
1479: int
1480: cmap_in(struct aproc *p, struct abuf *ibuf)
1481: {
1482: struct abuf *obuf = LIST_FIRST(&p->obuflist);
1483:
1484: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1485: return 0;
1486: cmap_bcopy(p, ibuf, obuf);
1487: if (!abuf_flush(obuf))
1488: return 0;
1489: return 1;
1490: }
1491:
1492: int
1493: cmap_out(struct aproc *p, struct abuf *obuf)
1494: {
1495: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1496:
1497: if (!abuf_fill(ibuf))
1498: return 0;
1499: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1500: return 0;
1501: cmap_bcopy(p, ibuf, obuf);
1502: return 1;
1503: }
1504:
1505: void
1506: cmap_eof(struct aproc *p, struct abuf *ibuf)
1507: {
1508: aproc_del(p);
1509: }
1510:
1511: void
1512: cmap_hup(struct aproc *p, struct abuf *obuf)
1513: {
1514: aproc_del(p);
1515: }
1516:
1517: struct aproc_ops cmap_ops = {
1518: "cmap",
1519: cmap_in,
1520: cmap_out,
1521: cmap_eof,
1522: cmap_hup,
1523: NULL,
1524: NULL,
1.19 ratchov 1525: aproc_ipos,
1526: aproc_opos,
1.17 ratchov 1527: NULL
1528: };
1529:
1530: struct aproc *
1531: cmap_new(char *name, struct aparams *ipar, struct aparams *opar)
1532: {
1533: struct aproc *p;
1534: unsigned i;
1535:
1536: p = aproc_new(&cmap_ops, name);
1537: for (i = 0; i < NCHAN_MAX; i++)
1538: p->u.cmap.ctx[i] = 0;
1.42 ratchov 1539: #ifdef DEBUG
1540: if (debug_level >= 3) {
1541: aproc_dbg(p);
1542: dbg_puts(": new ");
1543: aparams_dbg(ipar);
1544: dbg_puts(" -> ");
1545: aparams_dbg(opar);
1546: dbg_puts("\n");
1547: }
1548: #endif
1.19 ratchov 1549: return p;
1550: }
1551:
1552: /*
1553: * Convert one block.
1554: */
1555: void
1556: enc_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
1557: {
1558: unsigned nch, scount, icount, ocount;
1559: unsigned f;
1560: short *idata;
1561: int s;
1562: unsigned oshift;
1563: int osigbit;
1564: unsigned obps;
1565: unsigned i;
1566: unsigned char *odata;
1567: int obnext;
1568: int osnext;
1569:
1570: /*
1571: * Calculate max frames readable at once from the input buffer.
1572: */
1573: idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
1574: icount /= ibuf->bpf;
1575: if (icount == 0)
1576: return;
1577: odata = abuf_wgetblk(obuf, &ocount, 0);
1578: ocount /= obuf->bpf;
1579: if (ocount == 0)
1580: return;
1581: scount = (icount < ocount) ? icount : ocount;
1582: nch = ibuf->cmax - ibuf->cmin + 1;
1.42 ratchov 1583: #ifdef DEBUG
1584: if (debug_level >= 4) {
1585: aproc_dbg(p);
1586: dbg_puts(": bcopy ");
1587: dbg_putu(scount);
1588: dbg_puts(" fr * ");
1589: dbg_putu(nch);
1590: dbg_puts(" ch\n");
1591: }
1592: #endif
1.19 ratchov 1593: /*
1594: * Partially copy structures into local variables, to avoid
1595: * unnecessary indirections; this also allows the compiler to
1596: * order local variables more "cache-friendly".
1597: */
1598: oshift = p->u.conv.shift;
1599: osigbit = p->u.conv.sigbit;
1600: obps = p->u.conv.bps;
1601: obnext = p->u.conv.bnext;
1602: osnext = p->u.conv.snext;
1603:
1604: /*
1605: * Start conversion.
1606: */
1607: odata += p->u.conv.bfirst;
1608: for (f = scount * nch; f > 0; f--) {
1609: s = *idata++;
1610: s <<= 16;
1611: s >>= oshift;
1612: s ^= osigbit;
1613: for (i = obps; i > 0; i--) {
1614: *odata = (unsigned char)s;
1615: s >>= 8;
1616: odata += obnext;
1617: }
1618: odata += osnext;
1619: }
1620:
1621: /*
1622: * Update FIFO pointers.
1623: */
1624: abuf_rdiscard(ibuf, scount * ibuf->bpf);
1625: abuf_wcommit(obuf, scount * obuf->bpf);
1626: }
1627:
1628: int
1629: enc_in(struct aproc *p, struct abuf *ibuf)
1630: {
1631: struct abuf *obuf = LIST_FIRST(&p->obuflist);
1632:
1633: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1634: return 0;
1635: enc_bcopy(p, ibuf, obuf);
1636: if (!abuf_flush(obuf))
1637: return 0;
1638: return 1;
1639: }
1640:
1641: int
1642: enc_out(struct aproc *p, struct abuf *obuf)
1643: {
1644: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1645:
1646: if (!abuf_fill(ibuf))
1647: return 0;
1648: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1649: return 0;
1650: enc_bcopy(p, ibuf, obuf);
1651: return 1;
1652: }
1653:
1654: void
1655: enc_eof(struct aproc *p, struct abuf *ibuf)
1656: {
1657: aproc_del(p);
1658: }
1659:
1660: void
1661: enc_hup(struct aproc *p, struct abuf *obuf)
1662: {
1663: aproc_del(p);
1664: }
1665:
1666: struct aproc_ops enc_ops = {
1667: "enc",
1668: enc_in,
1669: enc_out,
1670: enc_eof,
1671: enc_hup,
1672: NULL,
1673: NULL,
1674: aproc_ipos,
1675: aproc_opos,
1676: NULL
1677: };
1678:
1679: struct aproc *
1680: enc_new(char *name, struct aparams *par)
1681: {
1682: struct aproc *p;
1683:
1684: p = aproc_new(&enc_ops, name);
1685: p->u.conv.bps = par->bps;
1686: p->u.conv.sigbit = par->sig ? 0 : 1 << (par->bits - 1);
1687: if (par->msb) {
1688: p->u.conv.shift = 32 - par->bps * 8;
1689: } else {
1690: p->u.conv.shift = 32 - par->bits;
1691: }
1692: if (!par->le) {
1693: p->u.conv.bfirst = par->bps - 1;
1694: p->u.conv.bnext = -1;
1695: p->u.conv.snext = 2 * par->bps;
1696: } else {
1697: p->u.conv.bfirst = 0;
1698: p->u.conv.bnext = 1;
1699: p->u.conv.snext = 0;
1700: }
1.42 ratchov 1701: #ifdef DEBUG
1702: if (debug_level >= 3) {
1703: aproc_dbg(p);
1704: dbg_puts(": new ");
1705: aparams_dbg(par);
1706: dbg_puts("\n");
1707: }
1708: #endif
1.19 ratchov 1709: return p;
1710: }
1711:
1712: /*
1713: * Convert one block.
1714: */
1715: void
1716: dec_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
1717: {
1718: unsigned nch, scount, icount, ocount;
1719: unsigned f;
1720: unsigned ibps;
1721: unsigned i;
1722: int s = 0xdeadbeef;
1723: unsigned char *idata;
1724: int ibnext;
1725: int isnext;
1726: int isigbit;
1727: unsigned ishift;
1728: short *odata;
1729:
1730: /*
1731: * Calculate max frames readable at once from the input buffer.
1732: */
1733: idata = abuf_rgetblk(ibuf, &icount, 0);
1734: icount /= ibuf->bpf;
1735: if (icount == 0)
1736: return;
1737: odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
1738: ocount /= obuf->bpf;
1739: if (ocount == 0)
1740: return;
1741: scount = (icount < ocount) ? icount : ocount;
1742: nch = obuf->cmax - obuf->cmin + 1;
1.42 ratchov 1743: #ifdef DEBUG
1744: if (debug_level >= 4) {
1745: aproc_dbg(p);
1746: dbg_puts(": bcopy ");
1747: dbg_putu(scount);
1748: dbg_puts(" fr * ");
1749: dbg_putu(nch);
1750: dbg_puts(" ch\n");
1751: }
1752: #endif
1.19 ratchov 1753: /*
1754: * Partially copy structures into local variables, to avoid
1755: * unnecessary indirections; this also allows the compiler to
1756: * order local variables more "cache-friendly".
1757: */
1758: ibps = p->u.conv.bps;
1759: ibnext = p->u.conv.bnext;
1760: isigbit = p->u.conv.sigbit;
1761: ishift = p->u.conv.shift;
1762: isnext = p->u.conv.snext;
1763:
1764: /*
1765: * Start conversion.
1766: */
1767: idata += p->u.conv.bfirst;
1768: for (f = scount * nch; f > 0; f--) {
1769: for (i = ibps; i > 0; i--) {
1770: s <<= 8;
1771: s |= *idata;
1772: idata += ibnext;
1773: }
1774: idata += isnext;
1775: s ^= isigbit;
1776: s <<= ishift;
1777: s >>= 16;
1778: *odata++ = s;
1779: }
1780:
1781: /*
1782: * Update FIFO pointers.
1783: */
1784: abuf_rdiscard(ibuf, scount * ibuf->bpf);
1785: abuf_wcommit(obuf, scount * obuf->bpf);
1786: }
1787:
1788: int
1789: dec_in(struct aproc *p, struct abuf *ibuf)
1790: {
1791: struct abuf *obuf = LIST_FIRST(&p->obuflist);
1792:
1793: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1794: return 0;
1795: dec_bcopy(p, ibuf, obuf);
1796: if (!abuf_flush(obuf))
1797: return 0;
1798: return 1;
1799: }
1800:
1801: int
1802: dec_out(struct aproc *p, struct abuf *obuf)
1803: {
1804: struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
1805:
1806: if (!abuf_fill(ibuf))
1807: return 0;
1808: if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1809: return 0;
1810: dec_bcopy(p, ibuf, obuf);
1811: return 1;
1812: }
1813:
1814: void
1815: dec_eof(struct aproc *p, struct abuf *ibuf)
1816: {
1817: aproc_del(p);
1818: }
1819:
1820: void
1821: dec_hup(struct aproc *p, struct abuf *obuf)
1822: {
1823: aproc_del(p);
1824: }
1825:
1826: struct aproc_ops dec_ops = {
1827: "dec",
1828: dec_in,
1829: dec_out,
1830: dec_eof,
1831: dec_hup,
1832: NULL,
1833: NULL,
1834: aproc_ipos,
1835: aproc_opos,
1836: NULL
1837: };
1838:
1839: struct aproc *
1840: dec_new(char *name, struct aparams *par)
1841: {
1842: struct aproc *p;
1843:
1844: p = aproc_new(&dec_ops, name);
1845: p->u.conv.bps = par->bps;
1846: p->u.conv.sigbit = par->sig ? 0 : 1 << (par->bits - 1);
1847: if (par->msb) {
1848: p->u.conv.shift = 32 - par->bps * 8;
1849: } else {
1850: p->u.conv.shift = 32 - par->bits;
1851: }
1852: if (par->le) {
1853: p->u.conv.bfirst = par->bps - 1;
1854: p->u.conv.bnext = -1;
1855: p->u.conv.snext = 2 * par->bps;
1856: } else {
1857: p->u.conv.bfirst = 0;
1858: p->u.conv.bnext = 1;
1859: p->u.conv.snext = 0;
1860: }
1.42 ratchov 1861: #ifdef DEBUG
1862: if (debug_level >= 3) {
1863: aproc_dbg(p);
1864: dbg_puts(": new ");
1865: aparams_dbg(par);
1866: dbg_puts("\n");
1867: }
1868: #endif
1.1 ratchov 1869: return p;
1870: }