[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.23

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