[BACK]Return to aproc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / aucat

Annotation of src/usr.bin/aucat/aproc.c, Revision 1.47

1.47    ! ratchov     1: /*     $OpenBSD: aproc.c,v 1.46 2010/01/15 22:17:10 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);
1.46      ratchov  1250:                dbg_puts(": resamp starting diff = ");
                   1251:                dbg_puti(diff);
                   1252:                dbg_puts(", ifr = ");
1.42      ratchov  1253:                dbg_putu(ifr);
                   1254:                dbg_puts(", ofr = ");
1.46      ratchov  1255:                dbg_putu(ofr);
1.42      ratchov  1256:                dbg_puts(" fr\n");
                   1257:        }
                   1258: #endif
1.15      ratchov  1259:        for (;;) {
1.27      ratchov  1260:                if (diff < 0) {
1.15      ratchov  1261:                        if (ifr == 0)
                   1262:                                break;
1.27      ratchov  1263:                        ctx_start ^= 1;
                   1264:                        ctx = ctxbuf + ctx_start;
1.15      ratchov  1265:                        for (c = inch; c > 0; c--) {
1.29      ratchov  1266:                                *ctx = *idata++;
1.27      ratchov  1267:                                ctx += RESAMP_NCTX;
1.15      ratchov  1268:                        }
1.27      ratchov  1269:                        diff += oblksz;
1.15      ratchov  1270:                        ifr--;
1.47    ! ratchov  1271:                } else {
1.27      ratchov  1272:                        if (ofr == 0)
                   1273:                                break;
                   1274:                        ctx = ctxbuf;
                   1275:                        for (c = onch; c > 0; c--) {
1.29      ratchov  1276:                                s1 = ctx[ctx_start];
                   1277:                                s2 = ctx[ctx_start ^ 1];
1.27      ratchov  1278:                                ctx += RESAMP_NCTX;
                   1279:                                *odata++ = s1 + (s2 - s1) * diff / (int)oblksz;
                   1280:                        }
                   1281:                        diff -= iblksz;
                   1282:                        ofr--;
1.15      ratchov  1283:                }
                   1284:        }
1.27      ratchov  1285:        p->u.resamp.diff = diff;
                   1286:        p->u.resamp.ctx_start = ctx_start;
1.42      ratchov  1287: #ifdef DEBUG
                   1288:        if (debug_level >= 4) {
                   1289:                aproc_dbg(p);
1.46      ratchov  1290:                dbg_puts(": resamp done delta = ");
                   1291:                dbg_puti(diff);
                   1292:                dbg_puts(", ifr = ");
1.42      ratchov  1293:                dbg_putu(ifr);
                   1294:                dbg_puts(", ofr = ");
1.46      ratchov  1295:                dbg_putu(ofr);
1.42      ratchov  1296:                dbg_puts(" fr\n");
                   1297:        }
                   1298: #endif
1.15      ratchov  1299:        /*
                   1300:         * Update FIFO pointers.
                   1301:         */
                   1302:        icount -= ifr * ibuf->bpf;
                   1303:        ocount -= ofr * obuf->bpf;
                   1304:        abuf_rdiscard(ibuf, icount);
                   1305:        abuf_wcommit(obuf, ocount);
                   1306: }
                   1307:
                   1308: int
                   1309: resamp_in(struct aproc *p, struct abuf *ibuf)
                   1310: {
                   1311:        struct abuf *obuf = LIST_FIRST(&p->obuflist);
                   1312:
                   1313:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
                   1314:                return 0;
                   1315:        resamp_bcopy(p, ibuf, obuf);
                   1316:        if (!abuf_flush(obuf))
                   1317:                return 0;
                   1318:        return 1;
                   1319: }
                   1320:
                   1321: int
                   1322: resamp_out(struct aproc *p, struct abuf *obuf)
                   1323: {
                   1324:        struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
                   1325:
                   1326:        if (!abuf_fill(ibuf))
                   1327:                return 0;
                   1328:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
                   1329:                return 0;
                   1330:        resamp_bcopy(p, ibuf, obuf);
                   1331:        return 1;
                   1332: }
                   1333:
                   1334: void
                   1335: resamp_eof(struct aproc *p, struct abuf *ibuf)
                   1336: {
                   1337:        aproc_del(p);
                   1338: }
                   1339:
                   1340: void
                   1341: resamp_hup(struct aproc *p, struct abuf *obuf)
                   1342: {
                   1343:        aproc_del(p);
                   1344: }
                   1345:
                   1346: void
                   1347: resamp_ipos(struct aproc *p, struct abuf *ibuf, int delta)
                   1348: {
1.31      ratchov  1349:        struct abuf *obuf = LIST_FIRST(&p->obuflist);
1.15      ratchov  1350:        long long ipos;
1.45      ratchov  1351:
                   1352:        ipos = (long long)delta * p->u.resamp.oblksz + p->u.resamp.idelta;
                   1353:        p->u.resamp.idelta = ipos % p->u.resamp.iblksz;
                   1354:        abuf_ipos(obuf, ipos / (int)p->u.resamp.iblksz);
1.15      ratchov  1355: }
                   1356:
                   1357: void
                   1358: resamp_opos(struct aproc *p, struct abuf *obuf, int delta)
                   1359: {
                   1360:        struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
                   1361:        long long opos;
                   1362:
1.45      ratchov  1363:        opos = (long long)delta * p->u.resamp.iblksz + p->u.resamp.odelta;
                   1364:        p->u.resamp.odelta = opos % p->u.resamp.oblksz;
                   1365:        abuf_opos(ibuf, opos / p->u.resamp.oblksz);
1.15      ratchov  1366: }
                   1367:
                   1368: struct aproc_ops resamp_ops = {
                   1369:        "resamp",
                   1370:        resamp_in,
                   1371:        resamp_out,
                   1372:        resamp_eof,
                   1373:        resamp_hup,
                   1374:        NULL,
                   1375:        NULL,
                   1376:        resamp_ipos,
                   1377:        resamp_opos,
                   1378:        NULL
                   1379: };
                   1380:
                   1381: struct aproc *
1.26      ratchov  1382: resamp_new(char *name, unsigned iblksz, unsigned oblksz)
1.15      ratchov  1383: {
                   1384:        struct aproc *p;
1.16      ratchov  1385:        unsigned i;
1.15      ratchov  1386:
                   1387:        p = aproc_new(&resamp_ops, name);
1.26      ratchov  1388:        p->u.resamp.iblksz = iblksz;
                   1389:        p->u.resamp.oblksz = oblksz;
1.47    ! ratchov  1390:        p->u.resamp.diff = (iblksz >= oblksz) ? -oblksz : 0;
1.15      ratchov  1391:        p->u.resamp.idelta = 0;
                   1392:        p->u.resamp.odelta = 0;
1.27      ratchov  1393:        p->u.resamp.ctx_start = 0;
                   1394:        for (i = 0; i < NCHAN_MAX * RESAMP_NCTX; i++)
1.16      ratchov  1395:                p->u.resamp.ctx[i] = 0;
1.42      ratchov  1396: #ifdef DEBUG
                   1397:        if (debug_level >= 3) {
                   1398:                aproc_dbg(p);
                   1399:                dbg_puts(": new ");
                   1400:                dbg_putu(iblksz);
                   1401:                dbg_puts("/");
                   1402:                dbg_putu(oblksz);
                   1403:                dbg_puts("\n");
                   1404:        }
                   1405: #endif
1.17      ratchov  1406:        return p;
                   1407: }
                   1408:
                   1409: /*
                   1410:  * Convert one block.
                   1411:  */
                   1412: void
                   1413: cmap_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
                   1414: {
                   1415:        unsigned inch;
                   1416:        short *idata;
                   1417:        unsigned onch;
                   1418:        short *odata;
                   1419:        short *ctx, *ictx, *octx;
                   1420:        unsigned c, f, scount, icount, ocount;
                   1421:
                   1422:        /*
                   1423:         * Calculate max frames readable at once from the input buffer.
                   1424:         */
                   1425:        idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
                   1426:        icount /= ibuf->bpf;
                   1427:        if (icount == 0)
                   1428:                return;
                   1429:        odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
                   1430:        ocount /= obuf->bpf;
                   1431:        if (ocount == 0)
                   1432:                return;
                   1433:        scount = icount < ocount ? icount : ocount;
                   1434:        inch = ibuf->cmax - ibuf->cmin + 1;
                   1435:        onch = obuf->cmax - obuf->cmin + 1;
                   1436:        ictx = p->u.cmap.ctx + ibuf->cmin;
                   1437:        octx = p->u.cmap.ctx + obuf->cmin;
                   1438:
                   1439:        for (f = scount; f > 0; f--) {
                   1440:                ctx = ictx;
                   1441:                for (c = inch; c > 0; c--) {
                   1442:                        *ctx = *idata;
                   1443:                        idata++;
                   1444:                        ctx++;
                   1445:                }
                   1446:                ctx = octx;
                   1447:                for (c = onch; c > 0; c--) {
                   1448:                        *odata = *ctx;
                   1449:                        odata++;
                   1450:                        ctx++;
                   1451:                }
                   1452:        }
1.42      ratchov  1453: #ifdef DEBUG
                   1454:        if (debug_level >= 4) {
                   1455:                aproc_dbg(p);
                   1456:                dbg_puts(": bcopy ");
                   1457:                dbg_putu(scount);
                   1458:                dbg_puts(" fr\n");
                   1459:        }
                   1460: #endif
1.17      ratchov  1461:        abuf_rdiscard(ibuf, scount * ibuf->bpf);
                   1462:        abuf_wcommit(obuf, scount * obuf->bpf);
                   1463: }
                   1464:
                   1465: int
                   1466: cmap_in(struct aproc *p, struct abuf *ibuf)
                   1467: {
                   1468:        struct abuf *obuf = LIST_FIRST(&p->obuflist);
                   1469:
                   1470:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
                   1471:                return 0;
                   1472:        cmap_bcopy(p, ibuf, obuf);
                   1473:        if (!abuf_flush(obuf))
                   1474:                return 0;
                   1475:        return 1;
                   1476: }
                   1477:
                   1478: int
                   1479: cmap_out(struct aproc *p, struct abuf *obuf)
                   1480: {
                   1481:        struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
                   1482:
                   1483:        if (!abuf_fill(ibuf))
                   1484:                return 0;
                   1485:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
                   1486:                return 0;
                   1487:        cmap_bcopy(p, ibuf, obuf);
                   1488:        return 1;
                   1489: }
                   1490:
                   1491: void
                   1492: cmap_eof(struct aproc *p, struct abuf *ibuf)
                   1493: {
                   1494:        aproc_del(p);
                   1495: }
                   1496:
                   1497: void
                   1498: cmap_hup(struct aproc *p, struct abuf *obuf)
                   1499: {
                   1500:        aproc_del(p);
                   1501: }
                   1502:
                   1503: struct aproc_ops cmap_ops = {
                   1504:        "cmap",
                   1505:        cmap_in,
                   1506:        cmap_out,
                   1507:        cmap_eof,
                   1508:        cmap_hup,
                   1509:        NULL,
                   1510:        NULL,
1.19      ratchov  1511:        aproc_ipos,
                   1512:        aproc_opos,
1.17      ratchov  1513:        NULL
                   1514: };
                   1515:
                   1516: struct aproc *
                   1517: cmap_new(char *name, struct aparams *ipar, struct aparams *opar)
                   1518: {
                   1519:        struct aproc *p;
                   1520:        unsigned i;
                   1521:
                   1522:        p = aproc_new(&cmap_ops, name);
                   1523:        for (i = 0; i < NCHAN_MAX; i++)
                   1524:                p->u.cmap.ctx[i] = 0;
1.42      ratchov  1525: #ifdef DEBUG
                   1526:        if (debug_level >= 3) {
                   1527:                aproc_dbg(p);
                   1528:                dbg_puts(": new ");
                   1529:                aparams_dbg(ipar);
                   1530:                dbg_puts(" -> ");
                   1531:                aparams_dbg(opar);
                   1532:                dbg_puts("\n");
                   1533:        }
                   1534: #endif
1.19      ratchov  1535:        return p;
                   1536: }
                   1537:
                   1538: /*
                   1539:  * Convert one block.
                   1540:  */
                   1541: void
                   1542: enc_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
                   1543: {
                   1544:        unsigned nch, scount, icount, ocount;
                   1545:        unsigned f;
                   1546:        short *idata;
                   1547:        int s;
                   1548:        unsigned oshift;
                   1549:        int osigbit;
                   1550:        unsigned obps;
                   1551:        unsigned i;
                   1552:        unsigned char *odata;
                   1553:        int obnext;
                   1554:        int osnext;
                   1555:
                   1556:        /*
                   1557:         * Calculate max frames readable at once from the input buffer.
                   1558:         */
                   1559:        idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
                   1560:        icount /= ibuf->bpf;
                   1561:        if (icount == 0)
                   1562:                return;
                   1563:        odata = abuf_wgetblk(obuf, &ocount, 0);
                   1564:        ocount /= obuf->bpf;
                   1565:        if (ocount == 0)
                   1566:                return;
                   1567:        scount = (icount < ocount) ? icount : ocount;
                   1568:        nch = ibuf->cmax - ibuf->cmin + 1;
1.42      ratchov  1569: #ifdef DEBUG
                   1570:        if (debug_level >= 4) {
                   1571:                aproc_dbg(p);
                   1572:                dbg_puts(": bcopy ");
                   1573:                dbg_putu(scount);
                   1574:                dbg_puts(" fr * ");
                   1575:                dbg_putu(nch);
                   1576:                dbg_puts(" ch\n");
                   1577:        }
                   1578: #endif
1.19      ratchov  1579:        /*
                   1580:         * Partially copy structures into local variables, to avoid
                   1581:         * unnecessary indirections; this also allows the compiler to
                   1582:         * order local variables more "cache-friendly".
                   1583:         */
                   1584:        oshift = p->u.conv.shift;
                   1585:        osigbit = p->u.conv.sigbit;
                   1586:        obps = p->u.conv.bps;
                   1587:        obnext = p->u.conv.bnext;
                   1588:        osnext = p->u.conv.snext;
                   1589:
                   1590:        /*
                   1591:         * Start conversion.
                   1592:         */
                   1593:        odata += p->u.conv.bfirst;
                   1594:        for (f = scount * nch; f > 0; f--) {
                   1595:                s = *idata++;
                   1596:                s <<= 16;
                   1597:                s >>= oshift;
                   1598:                s ^= osigbit;
                   1599:                for (i = obps; i > 0; i--) {
                   1600:                        *odata = (unsigned char)s;
                   1601:                        s >>= 8;
                   1602:                        odata += obnext;
                   1603:                }
                   1604:                odata += osnext;
                   1605:        }
                   1606:
                   1607:        /*
                   1608:         * Update FIFO pointers.
                   1609:         */
                   1610:        abuf_rdiscard(ibuf, scount * ibuf->bpf);
                   1611:        abuf_wcommit(obuf, scount * obuf->bpf);
                   1612: }
                   1613:
                   1614: int
                   1615: enc_in(struct aproc *p, struct abuf *ibuf)
                   1616: {
                   1617:        struct abuf *obuf = LIST_FIRST(&p->obuflist);
                   1618:
                   1619:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
                   1620:                return 0;
                   1621:        enc_bcopy(p, ibuf, obuf);
                   1622:        if (!abuf_flush(obuf))
                   1623:                return 0;
                   1624:        return 1;
                   1625: }
                   1626:
                   1627: int
                   1628: enc_out(struct aproc *p, struct abuf *obuf)
                   1629: {
                   1630:        struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
                   1631:
                   1632:        if (!abuf_fill(ibuf))
                   1633:                return 0;
                   1634:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
                   1635:                return 0;
                   1636:        enc_bcopy(p, ibuf, obuf);
                   1637:        return 1;
                   1638: }
                   1639:
                   1640: void
                   1641: enc_eof(struct aproc *p, struct abuf *ibuf)
                   1642: {
                   1643:        aproc_del(p);
                   1644: }
                   1645:
                   1646: void
                   1647: enc_hup(struct aproc *p, struct abuf *obuf)
                   1648: {
                   1649:        aproc_del(p);
                   1650: }
                   1651:
                   1652: struct aproc_ops enc_ops = {
                   1653:        "enc",
                   1654:        enc_in,
                   1655:        enc_out,
                   1656:        enc_eof,
                   1657:        enc_hup,
                   1658:        NULL,
                   1659:        NULL,
                   1660:        aproc_ipos,
                   1661:        aproc_opos,
                   1662:        NULL
                   1663: };
                   1664:
                   1665: struct aproc *
                   1666: enc_new(char *name, struct aparams *par)
                   1667: {
                   1668:        struct aproc *p;
                   1669:
                   1670:        p = aproc_new(&enc_ops, name);
                   1671:        p->u.conv.bps = par->bps;
                   1672:        p->u.conv.sigbit = par->sig ? 0 : 1 << (par->bits - 1);
                   1673:        if (par->msb) {
                   1674:                p->u.conv.shift = 32 - par->bps * 8;
                   1675:        } else {
                   1676:                p->u.conv.shift = 32 - par->bits;
                   1677:        }
                   1678:        if (!par->le) {
                   1679:                p->u.conv.bfirst = par->bps - 1;
                   1680:                p->u.conv.bnext = -1;
                   1681:                p->u.conv.snext = 2 * par->bps;
                   1682:        } else {
                   1683:                p->u.conv.bfirst = 0;
                   1684:                p->u.conv.bnext = 1;
                   1685:                p->u.conv.snext = 0;
                   1686:        }
1.42      ratchov  1687: #ifdef DEBUG
                   1688:        if (debug_level >= 3) {
                   1689:                aproc_dbg(p);
                   1690:                dbg_puts(": new ");
                   1691:                aparams_dbg(par);
                   1692:                dbg_puts("\n");
                   1693:        }
                   1694: #endif
1.19      ratchov  1695:        return p;
                   1696: }
                   1697:
                   1698: /*
                   1699:  * Convert one block.
                   1700:  */
                   1701: void
                   1702: dec_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
                   1703: {
                   1704:        unsigned nch, scount, icount, ocount;
                   1705:        unsigned f;
                   1706:        unsigned ibps;
                   1707:        unsigned i;
                   1708:        int s = 0xdeadbeef;
                   1709:        unsigned char *idata;
                   1710:        int ibnext;
                   1711:        int isnext;
                   1712:        int isigbit;
                   1713:        unsigned ishift;
                   1714:        short *odata;
                   1715:
                   1716:        /*
                   1717:         * Calculate max frames readable at once from the input buffer.
                   1718:         */
                   1719:        idata = abuf_rgetblk(ibuf, &icount, 0);
                   1720:        icount /= ibuf->bpf;
                   1721:        if (icount == 0)
                   1722:                return;
                   1723:        odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
                   1724:        ocount /= obuf->bpf;
                   1725:        if (ocount == 0)
                   1726:                return;
                   1727:        scount = (icount < ocount) ? icount : ocount;
                   1728:        nch = obuf->cmax - obuf->cmin + 1;
1.42      ratchov  1729: #ifdef DEBUG
                   1730:        if (debug_level >= 4) {
                   1731:                aproc_dbg(p);
                   1732:                dbg_puts(": bcopy ");
                   1733:                dbg_putu(scount);
                   1734:                dbg_puts(" fr * ");
                   1735:                dbg_putu(nch);
                   1736:                dbg_puts(" ch\n");
                   1737:        }
                   1738: #endif
1.19      ratchov  1739:        /*
                   1740:         * Partially copy structures into local variables, to avoid
                   1741:         * unnecessary indirections; this also allows the compiler to
                   1742:         * order local variables more "cache-friendly".
                   1743:         */
                   1744:        ibps = p->u.conv.bps;
                   1745:        ibnext = p->u.conv.bnext;
                   1746:        isigbit = p->u.conv.sigbit;
                   1747:        ishift = p->u.conv.shift;
                   1748:        isnext = p->u.conv.snext;
                   1749:
                   1750:        /*
                   1751:         * Start conversion.
                   1752:         */
                   1753:        idata += p->u.conv.bfirst;
                   1754:        for (f = scount * nch; f > 0; f--) {
                   1755:                for (i = ibps; i > 0; i--) {
                   1756:                        s <<= 8;
                   1757:                        s |= *idata;
                   1758:                        idata += ibnext;
                   1759:                }
                   1760:                idata += isnext;
                   1761:                s ^= isigbit;
                   1762:                s <<= ishift;
                   1763:                s >>= 16;
                   1764:                *odata++ = s;
                   1765:        }
                   1766:
                   1767:        /*
                   1768:         * Update FIFO pointers.
                   1769:         */
                   1770:        abuf_rdiscard(ibuf, scount * ibuf->bpf);
                   1771:        abuf_wcommit(obuf, scount * obuf->bpf);
                   1772: }
                   1773:
                   1774: int
                   1775: dec_in(struct aproc *p, struct abuf *ibuf)
                   1776: {
                   1777:        struct abuf *obuf = LIST_FIRST(&p->obuflist);
                   1778:
                   1779:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
                   1780:                return 0;
                   1781:        dec_bcopy(p, ibuf, obuf);
                   1782:        if (!abuf_flush(obuf))
                   1783:                return 0;
                   1784:        return 1;
                   1785: }
                   1786:
                   1787: int
                   1788: dec_out(struct aproc *p, struct abuf *obuf)
                   1789: {
                   1790:        struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
                   1791:
                   1792:        if (!abuf_fill(ibuf))
                   1793:                return 0;
                   1794:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
                   1795:                return 0;
                   1796:        dec_bcopy(p, ibuf, obuf);
                   1797:        return 1;
                   1798: }
                   1799:
                   1800: void
                   1801: dec_eof(struct aproc *p, struct abuf *ibuf)
                   1802: {
                   1803:        aproc_del(p);
                   1804: }
                   1805:
                   1806: void
                   1807: dec_hup(struct aproc *p, struct abuf *obuf)
                   1808: {
                   1809:        aproc_del(p);
                   1810: }
                   1811:
                   1812: struct aproc_ops dec_ops = {
                   1813:        "dec",
                   1814:        dec_in,
                   1815:        dec_out,
                   1816:        dec_eof,
                   1817:        dec_hup,
                   1818:        NULL,
                   1819:        NULL,
                   1820:        aproc_ipos,
                   1821:        aproc_opos,
                   1822:        NULL
                   1823: };
                   1824:
                   1825: struct aproc *
                   1826: dec_new(char *name, struct aparams *par)
                   1827: {
                   1828:        struct aproc *p;
                   1829:
                   1830:        p = aproc_new(&dec_ops, name);
                   1831:        p->u.conv.bps = par->bps;
                   1832:        p->u.conv.sigbit = par->sig ? 0 : 1 << (par->bits - 1);
                   1833:        if (par->msb) {
                   1834:                p->u.conv.shift = 32 - par->bps * 8;
                   1835:        } else {
                   1836:                p->u.conv.shift = 32 - par->bits;
                   1837:        }
                   1838:        if (par->le) {
                   1839:                p->u.conv.bfirst = par->bps - 1;
                   1840:                p->u.conv.bnext = -1;
                   1841:                p->u.conv.snext = 2 * par->bps;
                   1842:        } else {
                   1843:                p->u.conv.bfirst = 0;
                   1844:                p->u.conv.bnext = 1;
                   1845:                p->u.conv.snext = 0;
                   1846:        }
1.42      ratchov  1847: #ifdef DEBUG
                   1848:        if (debug_level >= 3) {
                   1849:                aproc_dbg(p);
                   1850:                dbg_puts(": new ");
                   1851:                aparams_dbg(par);
                   1852:                dbg_puts("\n");
                   1853:        }
                   1854: #endif
1.1       ratchov  1855:        return p;
                   1856: }