diff options
author | 2016-05-27 15:38:27 +0000 | |
---|---|---|
committer | 2016-05-27 15:38:27 +0000 | |
commit | bac751396dcf7de86c99636e60e2662764538807 (patch) | |
tree | 77df08da626339bc2d1249de3bd96d04fff930bc | |
parent | Per the libtls man page, tls_init() must be called prior to any other (diff) | |
download | wireguard-openbsd-bac751396dcf7de86c99636e60e2662764538807.tar.xz wireguard-openbsd-bac751396dcf7de86c99636e60e2662764538807.zip |
Make resamp_do() get the exact number input and output samples and
provide routines to calculate them. This way we don't rely on it
to calculate the bytes procuded/consumed anymore. No behaviour change.
-rw-r--r-- | usr.bin/aucat/aucat.c | 107 | ||||
-rw-r--r-- | usr.bin/aucat/dsp.c | 60 | ||||
-rw-r--r-- | usr.bin/aucat/dsp.h | 6 |
3 files changed, 119 insertions, 54 deletions
diff --git a/usr.bin/aucat/aucat.c b/usr.bin/aucat/aucat.c index df423b0673d..6405d8eef29 100644 --- a/usr.bin/aucat/aucat.c +++ b/usr.bin/aucat/aucat.c @@ -423,8 +423,20 @@ slot_del(struct slot *s) free(s); } +static int +slot_ocnt(struct slot *s, int icnt) +{ + return s->resampbuf ? resamp_ocnt(&s->resamp, icnt) : icnt; +} + +static int +slot_icnt(struct slot *s, int ocnt) +{ + return s->resampbuf ? resamp_icnt(&s->resamp, ocnt) : ocnt; +} + static void -play_filt_resamp(struct slot *s, void *res_in, void *out, int *icnt, int *ocnt) +play_filt_resamp(struct slot *s, void *res_in, void *out, int icnt, int ocnt) { int i, offs, vol, nch; void *in; @@ -437,22 +449,22 @@ play_filt_resamp(struct slot *s, void *res_in, void *out, int *icnt, int *ocnt) nch = s->cmap.nch; vol = s->vol / s->join; /* XXX */ - cmap_add(&s->cmap, in, out, vol, *ocnt); + cmap_add(&s->cmap, in, out, vol, ocnt); offs = 0; for (i = s->join - 1; i > 0; i--) { offs += nch; - cmap_add(&s->cmap, (adata_t *)in + offs, out, vol, *ocnt); + cmap_add(&s->cmap, (adata_t *)in + offs, out, vol, ocnt); } offs = 0; for (i = s->expand - 1; i > 0; i--) { offs += nch; - cmap_add(&s->cmap, in, (adata_t *)out + offs, vol, *ocnt); + cmap_add(&s->cmap, in, (adata_t *)out + offs, vol, ocnt); } } static void -play_filt_dec(struct slot *s, void *in, void *out, int *icnt, int *ocnt) +play_filt_dec(struct slot *s, void *in, void *out, int icnt, int ocnt) { void *tmp; @@ -460,16 +472,16 @@ play_filt_dec(struct slot *s, void *in, void *out, int *icnt, int *ocnt) if (tmp) { switch (s->afile.fmt) { case AFILE_FMT_PCM: - dec_do(&s->conv, in, tmp, *icnt); + dec_do(&s->conv, in, tmp, icnt); break; case AFILE_FMT_ULAW: - dec_do_ulaw(&s->conv, in, tmp, *icnt, 0); + dec_do_ulaw(&s->conv, in, tmp, icnt, 0); break; case AFILE_FMT_ALAW: - dec_do_ulaw(&s->conv, in, tmp, *icnt, 1); + dec_do_ulaw(&s->conv, in, tmp, icnt, 1); break; case AFILE_FMT_FLOAT: - dec_do_float(&s->conv, in, tmp, *icnt); + dec_do_float(&s->conv, in, tmp, icnt); break; } } else @@ -486,26 +498,32 @@ static int slot_mix_badd(struct slot *s, adata_t *odata) { adata_t *idata; - int len, icnt, ocnt; - - idata = (adata_t *)abuf_rgetblk(&s->buf, &len); - icnt = len / s->bpf; - if (icnt > s->round) - icnt = s->round; -#ifdef DEBUG - if (icnt == 0) { - log_puts("slot_mix_badd: not enough data\n"); - panic(); + int len, icnt, ocnt, otodo, odone; + + odone = 0; + otodo = dev_round; + while (otodo > 0) { + idata = (adata_t *)abuf_rgetblk(&s->buf, &len); + + icnt = len / s->bpf; + ocnt = slot_ocnt(s, icnt); + if (ocnt > otodo) { + ocnt = otodo; + icnt = slot_icnt(s, ocnt); + } + if (icnt == 0) + break; + play_filt_dec(s, idata, odata, icnt, ocnt); + abuf_rdiscard(&s->buf, icnt * s->bpf); + otodo -= ocnt; + odone += ocnt; + odata += ocnt * dev_pchan; } -#endif - ocnt = dev_round; - play_filt_dec(s, idata, odata, &icnt, &ocnt); - abuf_rdiscard(&s->buf, icnt * s->bpf); - return ocnt; + return odone; } static void -rec_filt_resamp(struct slot *s, void *in, void *res_out, int *icnt, int *ocnt) +rec_filt_resamp(struct slot *s, void *in, void *res_out, int icnt, int ocnt) { int i, vol, offs, nch; void *out = res_out; @@ -514,33 +532,33 @@ rec_filt_resamp(struct slot *s, void *in, void *res_out, int *icnt, int *ocnt) nch = s->cmap.nch; vol = ADATA_UNIT / s->join; - cmap_copy(&s->cmap, in, out, vol, *icnt); + cmap_copy(&s->cmap, in, out, vol, icnt); offs = 0; for (i = s->join - 1; i > 0; i--) { offs += nch; - cmap_add(&s->cmap, (adata_t *)in + offs, out, vol, *icnt); + cmap_add(&s->cmap, (adata_t *)in + offs, out, vol, icnt); } offs = 0; for (i = s->expand - 1; i > 0; i--) { offs += nch; - cmap_copy(&s->cmap, in, (adata_t *)out + offs, vol, *icnt); + cmap_copy(&s->cmap, in, (adata_t *)out + offs, vol, icnt); } if (s->resampbuf) resamp_do(&s->resamp, s->resampbuf, res_out, icnt, ocnt); else - *ocnt = *icnt; + ocnt = icnt; } static void -rec_filt_enc(struct slot *s, void *in, void *out, int *icnt, int *ocnt) +rec_filt_enc(struct slot *s, void *in, void *out, int icnt, int ocnt) { void *tmp; tmp = s->convbuf; rec_filt_resamp(s, in, tmp ? tmp : out, icnt, ocnt); if (tmp) - enc_do(&s->conv, tmp, out, *ocnt); + enc_do(&s->conv, tmp, out, ocnt); } /* @@ -548,22 +566,27 @@ rec_filt_enc(struct slot *s, void *in, void *out, int *icnt, int *ocnt) * but not more than a block. */ static void -slot_sub_bcopy(struct slot *s, adata_t *idata, int todo) +slot_sub_bcopy(struct slot *s, adata_t *idata, int itodo) { adata_t *odata; int len, icnt, ocnt; - odata = (adata_t *)abuf_wgetblk(&s->buf, &len); -#ifdef DEBUG - if (len < s->round * s->bpf) { - log_puts("slot_sub_bcopy: not enough space\n"); - panic(); + while (itodo > 0) { + odata = (adata_t *)abuf_wgetblk(&s->buf, &len); + + ocnt = len / s->bpf; + icnt = slot_icnt(s, ocnt); + if (icnt > itodo) { + icnt = itodo; + ocnt = slot_ocnt(s, icnt); + } + if (ocnt == 0) + break; + rec_filt_enc(s, idata, odata, icnt, ocnt); + abuf_wcommit(&s->buf, ocnt * s->bpf); + itodo -= icnt; + idata += icnt * dev_rchan; } -#endif - icnt = todo; - ocnt = len / s->bpf; - rec_filt_enc(s, idata, odata, &icnt, &ocnt); - abuf_wcommit(&s->buf, ocnt * s->bpf); } static int diff --git a/usr.bin/aucat/dsp.c b/usr.bin/aucat/dsp.c index 169cab1fb77..d1934f8b2cd 100644 --- a/usr.bin/aucat/dsp.c +++ b/usr.bin/aucat/dsp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dsp.c,v 1.4 2016/05/26 06:17:31 ratchov Exp $ */ +/* $OpenBSD: dsp.c,v 1.5 2016/05/27 15:38:27 ratchov Exp $ */ /* * Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org> * @@ -268,10 +268,38 @@ aparams_native(struct aparams *par) } /* - * resample the given number of frames + * return the number of output frames resamp_do() would produce with + * the given number of input frames + */ +int +resamp_ocnt(struct resamp *p, int icnt) +{ + return ((long long)p->oblksz * icnt + p->diff) / p->iblksz; +} + +/* + * return the number of input frames resamp_do() needs in order to + * produce the given number of output frames + */ +int +resamp_icnt(struct resamp *p, int ocnt) +{ + return ((long long)p->iblksz * ocnt - p->diff + + p->oblksz - 1) / p->oblksz; +} + +/* + * Resample the given number of frames. The number of output frames + * must match the coresponding number the input frames. Either + * use: + * + * icnt * orate = ocnt * irate + * + * or use resamp_icnt() or resamp_ocnt() to calculate the proper + * numbers. */ void -resamp_do(struct resamp *p, adata_t *in, adata_t *out, int *icnt, int *ocnt) +resamp_do(struct resamp *p, adata_t *in, adata_t *out, int icnt, int ocnt) { unsigned int nch; adata_t *idata; @@ -298,8 +326,8 @@ resamp_do(struct resamp *p, adata_t *in, adata_t *out, int *icnt, int *ocnt) ctxbuf = p->ctx; ctx_start = p->ctx_start; nch = p->nch; - ifr = *icnt; - ofr = *ocnt; + ifr = icnt; + ofr = ocnt; /* * Start conversion. @@ -308,10 +336,10 @@ resamp_do(struct resamp *p, adata_t *in, adata_t *out, int *icnt, int *ocnt) if (log_level >= 4) { log_puts("resamp: copying "); log_puti(ifr); - log_puts(" frames, diff = "); - log_putu(diff); - log_puts(", max = "); + log_puts(" -> "); log_putu(ofr); + log_puts(" frames, diff = "); + log_puti(diff); log_puts("\n"); } #endif @@ -361,8 +389,20 @@ resamp_do(struct resamp *p, adata_t *in, adata_t *out, int *icnt, int *ocnt) } p->diff = diff; p->ctx_start = ctx_start; - *icnt -= ifr; - *ocnt -= ofr; +#ifdef DEBUG + if (ifr != 0) { + log_puts("resamp_do: "); + log_puti(ifr); + log_puts(": too many input frames\n"); + panic(); + } + if (ofr != 0) { + log_puts("resamp_do: "); + log_puti(ofr); + log_puts(": too many output frames\n"); + panic(); + } +#endif } /* diff --git a/usr.bin/aucat/dsp.h b/usr.bin/aucat/dsp.h index 8ffc52da2f4..b71c77f0ebc 100644 --- a/usr.bin/aucat/dsp.h +++ b/usr.bin/aucat/dsp.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dsp.h,v 1.2 2016/05/26 06:17:31 ratchov Exp $ */ +/* $OpenBSD: dsp.h,v 1.3 2016/05/27 15:38:27 ratchov Exp $ */ /* * Copyright (c) 2012 Alexandre Ratchov <alex@caoua.org> * @@ -147,7 +147,9 @@ int aparams_strtoenc(struct aparams *, char *); int aparams_enctostr(struct aparams *, char *); int aparams_native(struct aparams *); -void resamp_do(struct resamp *, adata_t *, adata_t *, int *, int *); +int resamp_ocnt(struct resamp *, int); +int resamp_icnt(struct resamp *, int); +void resamp_do(struct resamp *, adata_t *, adata_t *, int, int); void resamp_init(struct resamp *, unsigned int, unsigned int, int); void enc_do(struct conv *, unsigned char *, unsigned char *, int); void enc_sil_do(struct conv *, unsigned char *, int); |