diff --git a/sys/dev/sound/pcm/feeder_chain.c b/sys/dev/sound/pcm/feeder_chain.c index b2d48abd71d1..fe114b727ffd 100644 --- a/sys/dev/sound/pcm/feeder_chain.c +++ b/sys/dev/sound/pcm/feeder_chain.c @@ -1,859 +1,859 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2008-2009 Ariff Abdullah * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifdef HAVE_KERNEL_OPTION_HEADERS #include "opt_snd.h" #endif #include #include "feeder_if.h" SND_DECLARE_FILE("$FreeBSD$"); /* chain state */ struct feeder_chain_state { uint32_t afmt; /* audio format */ uint32_t rate; /* sampling rate */ struct pcmchan_matrix *matrix; /* matrix map */ }; /* * chain descriptor that will be passed around from the beginning until the * end of chain process. */ struct feeder_chain_desc { struct feeder_chain_state origin; /* original state */ struct feeder_chain_state current; /* current state */ struct feeder_chain_state target; /* target state */ struct pcm_feederdesc desc; /* feeder descriptor */ - uint32_t afmt_ne; /* prefered native endian */ + uint32_t afmt_ne; /* preferred native endian */ int mode; /* chain mode */ int use_eq; /* need EQ? */ int use_matrix; /* need channel matrixing? */ int use_volume; /* need softpcmvol? */ int dummy; /* dummy passthrough */ int expensive; /* possibly expensive */ }; #define FEEDER_CHAIN_LEAN 0 #define FEEDER_CHAIN_16 1 #define FEEDER_CHAIN_32 2 #define FEEDER_CHAIN_MULTI 3 #define FEEDER_CHAIN_FULLMULTI 4 #define FEEDER_CHAIN_LAST 5 #if defined(SND_FEEDER_FULL_MULTIFORMAT) #define FEEDER_CHAIN_DEFAULT FEEDER_CHAIN_FULLMULTI #elif defined(SND_FEEDER_MULTIFORMAT) #define FEEDER_CHAIN_DEFAULT FEEDER_CHAIN_MULTI #else #define FEEDER_CHAIN_DEFAULT FEEDER_CHAIN_LEAN #endif /* - * List of prefered formats that might be required during + * List of preferred formats that might be required during * processing. It will be decided through snd_fmtbest(). */ /* 'Lean' mode, signed 16 or 32 bit native endian. */ static uint32_t feeder_chain_formats_lean[] = { AFMT_S16_NE, AFMT_S32_NE, 0 }; /* Force everything to signed 16 bit native endian. */ static uint32_t feeder_chain_formats_16[] = { AFMT_S16_NE, 0 }; /* Force everything to signed 32 bit native endian. */ static uint32_t feeder_chain_formats_32[] = { AFMT_S32_NE, 0 }; /* Multiple choices, all except 8 bit. */ static uint32_t feeder_chain_formats_multi[] = { AFMT_S16_LE, AFMT_S16_BE, AFMT_U16_LE, AFMT_U16_BE, AFMT_S24_LE, AFMT_S24_BE, AFMT_U24_LE, AFMT_U24_BE, AFMT_S32_LE, AFMT_S32_BE, AFMT_U32_LE, AFMT_U32_BE, 0 }; /* Everything that is convertible. */ static uint32_t feeder_chain_formats_fullmulti[] = { AFMT_S8, AFMT_U8, AFMT_S16_LE, AFMT_S16_BE, AFMT_U16_LE, AFMT_U16_BE, AFMT_S24_LE, AFMT_S24_BE, AFMT_U24_LE, AFMT_U24_BE, AFMT_S32_LE, AFMT_S32_BE, AFMT_U32_LE, AFMT_U32_BE, 0 }; static uint32_t *feeder_chain_formats[FEEDER_CHAIN_LAST] = { [FEEDER_CHAIN_LEAN] = feeder_chain_formats_lean, [FEEDER_CHAIN_16] = feeder_chain_formats_16, [FEEDER_CHAIN_32] = feeder_chain_formats_32, [FEEDER_CHAIN_MULTI] = feeder_chain_formats_multi, [FEEDER_CHAIN_FULLMULTI] = feeder_chain_formats_fullmulti }; static int feeder_chain_mode = FEEDER_CHAIN_DEFAULT; #if defined(_KERNEL) && defined(SND_DEBUG) && defined(SND_FEEDER_FULL_MULTIFORMAT) SYSCTL_INT(_hw_snd, OID_AUTO, feeder_chain_mode, CTLFLAG_RWTUN, &feeder_chain_mode, 0, "feeder chain mode " "(0=lean, 1=16bit, 2=32bit, 3=multiformat, 4=fullmultiformat)"); #endif /* * feeder_build_format(): Chain any format converter. */ static int feeder_build_format(struct pcm_channel *c, struct feeder_chain_desc *cdesc) { struct feeder_class *fc; struct pcm_feederdesc *desc; int ret; desc = &(cdesc->desc); desc->type = FEEDER_FORMAT; desc->in = 0; desc->out = 0; desc->flags = 0; fc = feeder_getclass(desc); if (fc == NULL) { device_printf(c->dev, "%s(): can't find feeder_format\n", __func__); return (ENOTSUP); } desc->in = cdesc->current.afmt; desc->out = cdesc->target.afmt; ret = chn_addfeeder(c, fc, desc); if (ret != 0) { device_printf(c->dev, "%s(): can't add feeder_format\n", __func__); return (ret); } c->feederflags |= 1 << FEEDER_FORMAT; cdesc->current.afmt = cdesc->target.afmt; return (0); } /* * feeder_build_formatne(): Chain format converter that suite best for native * endian format. */ static int feeder_build_formatne(struct pcm_channel *c, struct feeder_chain_desc *cdesc) { struct feeder_chain_state otarget; int ret; if (cdesc->afmt_ne == 0 || AFMT_ENCODING(cdesc->current.afmt) == cdesc->afmt_ne) return (0); otarget = cdesc->target; cdesc->target = cdesc->current; cdesc->target.afmt = SND_FORMAT(cdesc->afmt_ne, cdesc->current.matrix->channels, cdesc->current.matrix->ext); ret = feeder_build_format(c, cdesc); if (ret != 0) return (ret); cdesc->target = otarget; return (0); } /* * feeder_build_rate(): Chain sample rate converter. */ static int feeder_build_rate(struct pcm_channel *c, struct feeder_chain_desc *cdesc) { struct feeder_class *fc; struct pcm_feeder *f; struct pcm_feederdesc *desc; int ret; ret = feeder_build_formatne(c, cdesc); if (ret != 0) return (ret); desc = &(cdesc->desc); desc->type = FEEDER_RATE; desc->in = 0; desc->out = 0; desc->flags = 0; fc = feeder_getclass(desc); if (fc == NULL) { device_printf(c->dev, "%s(): can't find feeder_rate\n", __func__); return (ENOTSUP); } desc->in = cdesc->current.afmt; desc->out = desc->in; ret = chn_addfeeder(c, fc, desc); if (ret != 0) { device_printf(c->dev, "%s(): can't add feeder_rate\n", __func__); return (ret); } f = c->feeder; /* * If in 'dummy' mode (possibly due to passthrough mode), set the * conversion quality to the lowest possible (should be fastest) since * listener won't be hearing anything. Theoretically we can just * disable it, but that will cause weird runtime behaviour: * application appear to play something that is either too fast or too * slow. */ if (cdesc->dummy != 0) { ret = FEEDER_SET(f, FEEDRATE_QUALITY, 0); if (ret != 0) { device_printf(c->dev, "%s(): can't set resampling quality\n", __func__); return (ret); } } ret = FEEDER_SET(f, FEEDRATE_SRC, cdesc->current.rate); if (ret != 0) { device_printf(c->dev, "%s(): can't set source rate\n", __func__); return (ret); } ret = FEEDER_SET(f, FEEDRATE_DST, cdesc->target.rate); if (ret != 0) { device_printf(c->dev, "%s(): can't set destination rate\n", __func__); return (ret); } c->feederflags |= 1 << FEEDER_RATE; cdesc->current.rate = cdesc->target.rate; return (0); } /* * feeder_build_matrix(): Chain channel matrixing converter. */ static int feeder_build_matrix(struct pcm_channel *c, struct feeder_chain_desc *cdesc) { struct feeder_class *fc; struct pcm_feeder *f; struct pcm_feederdesc *desc; int ret; ret = feeder_build_formatne(c, cdesc); if (ret != 0) return (ret); desc = &(cdesc->desc); desc->type = FEEDER_MATRIX; desc->in = 0; desc->out = 0; desc->flags = 0; fc = feeder_getclass(desc); if (fc == NULL) { device_printf(c->dev, "%s(): can't find feeder_matrix\n", __func__); return (ENOTSUP); } desc->in = cdesc->current.afmt; desc->out = SND_FORMAT(cdesc->current.afmt, cdesc->target.matrix->channels, cdesc->target.matrix->ext); ret = chn_addfeeder(c, fc, desc); if (ret != 0) { device_printf(c->dev, "%s(): can't add feeder_matrix\n", __func__); return (ret); } f = c->feeder; ret = feeder_matrix_setup(f, cdesc->current.matrix, cdesc->target.matrix); if (ret != 0) { device_printf(c->dev, "%s(): feeder_matrix_setup() failed\n", __func__); return (ret); } c->feederflags |= 1 << FEEDER_MATRIX; cdesc->current.afmt = desc->out; cdesc->current.matrix = cdesc->target.matrix; cdesc->use_matrix = 0; return (0); } /* * feeder_build_volume(): Chain soft volume. */ static int feeder_build_volume(struct pcm_channel *c, struct feeder_chain_desc *cdesc) { struct feeder_class *fc; struct pcm_feeder *f; struct pcm_feederdesc *desc; int ret; ret = feeder_build_formatne(c, cdesc); if (ret != 0) return (ret); desc = &(cdesc->desc); desc->type = FEEDER_VOLUME; desc->in = 0; desc->out = 0; desc->flags = 0; fc = feeder_getclass(desc); if (fc == NULL) { device_printf(c->dev, "%s(): can't find feeder_volume\n", __func__); return (ENOTSUP); } desc->in = cdesc->current.afmt; desc->out = desc->in; ret = chn_addfeeder(c, fc, desc); if (ret != 0) { device_printf(c->dev, "%s(): can't add feeder_volume\n", __func__); return (ret); } f = c->feeder; /* * If in 'dummy' mode (possibly due to passthrough mode), set BYPASS * mode since listener won't be hearing anything. Theoretically we can * just disable it, but that will confuse volume per channel mixer. */ if (cdesc->dummy != 0) { ret = FEEDER_SET(f, FEEDVOLUME_STATE, FEEDVOLUME_BYPASS); if (ret != 0) { device_printf(c->dev, "%s(): can't set volume bypass\n", __func__); return (ret); } } ret = feeder_volume_apply_matrix(f, cdesc->current.matrix); if (ret != 0) { device_printf(c->dev, "%s(): feeder_volume_apply_matrix() failed\n", __func__); return (ret); } c->feederflags |= 1 << FEEDER_VOLUME; cdesc->use_volume = 0; return (0); } /* * feeder_build_eq(): Chain parametric software equalizer. */ static int feeder_build_eq(struct pcm_channel *c, struct feeder_chain_desc *cdesc) { struct feeder_class *fc; struct pcm_feeder *f; struct pcm_feederdesc *desc; int ret; ret = feeder_build_formatne(c, cdesc); if (ret != 0) return (ret); desc = &(cdesc->desc); desc->type = FEEDER_EQ; desc->in = 0; desc->out = 0; desc->flags = 0; fc = feeder_getclass(desc); if (fc == NULL) { device_printf(c->dev, "%s(): can't find feeder_eq\n", __func__); return (ENOTSUP); } desc->in = cdesc->current.afmt; desc->out = desc->in; ret = chn_addfeeder(c, fc, desc); if (ret != 0) { device_printf(c->dev, "%s(): can't add feeder_eq\n", __func__); return (ret); } f = c->feeder; ret = FEEDER_SET(f, FEEDEQ_RATE, cdesc->current.rate); if (ret != 0) { device_printf(c->dev, "%s(): can't set rate on feeder_eq\n", __func__); return (ret); } c->feederflags |= 1 << FEEDER_EQ; cdesc->use_eq = 0; return (0); } /* * feeder_build_root(): Chain root feeder, the top, father of all. */ static int feeder_build_root(struct pcm_channel *c, struct feeder_chain_desc *cdesc) { struct feeder_class *fc; int ret; fc = feeder_getclass(NULL); if (fc == NULL) { device_printf(c->dev, "%s(): can't find feeder_root\n", __func__); return (ENOTSUP); } ret = chn_addfeeder(c, fc, NULL); if (ret != 0) { device_printf(c->dev, "%s(): can't add feeder_root\n", __func__); return (ret); } c->feederflags |= 1 << FEEDER_ROOT; c->feeder->desc->in = cdesc->current.afmt; c->feeder->desc->out = cdesc->current.afmt; return (0); } /* * feeder_build_mixer(): Chain software mixer for virtual channels. */ static int feeder_build_mixer(struct pcm_channel *c, struct feeder_chain_desc *cdesc) { struct feeder_class *fc; struct pcm_feederdesc *desc; int ret; desc = &(cdesc->desc); desc->type = FEEDER_MIXER; desc->in = 0; desc->out = 0; desc->flags = 0; fc = feeder_getclass(desc); if (fc == NULL) { device_printf(c->dev, "%s(): can't find feeder_mixer\n", __func__); return (ENOTSUP); } desc->in = cdesc->current.afmt; desc->out = desc->in; ret = chn_addfeeder(c, fc, desc); if (ret != 0) { device_printf(c->dev, "%s(): can't add feeder_mixer\n", __func__); return (ret); } c->feederflags |= 1 << FEEDER_MIXER; return (0); } /* Macrosses to ease our job doing stuffs later. */ #define FEEDER_BW(c, t) ((c)->t.matrix->channels * (c)->t.rate) #define FEEDRATE_UP(c) ((c)->target.rate > (c)->current.rate) #define FEEDRATE_DOWN(c) ((c)->target.rate < (c)->current.rate) #define FEEDRATE_REQUIRED(c) (FEEDRATE_UP(c) || FEEDRATE_DOWN(c)) #define FEEDMATRIX_UP(c) ((c)->target.matrix->channels > \ (c)->current.matrix->channels) #define FEEDMATRIX_DOWN(c) ((c)->target.matrix->channels < \ (c)->current.matrix->channels) #define FEEDMATRIX_REQUIRED(c) (FEEDMATRIX_UP(c) || \ FEEDMATRIX_DOWN(c) || (c)->use_matrix != 0) #define FEEDFORMAT_REQUIRED(c) (AFMT_ENCODING((c)->current.afmt) != \ AFMT_ENCODING((c)->target.afmt)) #define FEEDVOLUME_REQUIRED(c) ((c)->use_volume != 0) #define FEEDEQ_VALIDRATE(c, t) (feeder_eq_validrate((c)->t.rate) != 0) #define FEEDEQ_ECONOMY(c) (FEEDER_BW(c, current) < FEEDER_BW(c, target)) #define FEEDEQ_REQUIRED(c) ((c)->use_eq != 0 && \ FEEDEQ_VALIDRATE(c, current)) #define FEEDFORMAT_NE_REQUIRED(c) \ ((c)->afmt_ne != AFMT_S32_NE && \ (((c)->mode == FEEDER_CHAIN_16 && \ AFMT_ENCODING((c)->current.afmt) != AFMT_S16_NE) || \ ((c)->mode == FEEDER_CHAIN_32 && \ AFMT_ENCODING((c)->current.afmt) != AFMT_S32_NE) || \ (c)->mode == FEEDER_CHAIN_FULLMULTI || \ ((c)->mode == FEEDER_CHAIN_MULTI && \ ((c)->current.afmt & AFMT_8BIT)) || \ ((c)->mode == FEEDER_CHAIN_LEAN && \ !((c)->current.afmt & (AFMT_S16_NE | AFMT_S32_NE))))) static void feeder_default_matrix(struct pcmchan_matrix *m, uint32_t fmt, int id) { int x; memset(m, 0, sizeof(*m)); m->id = id; m->channels = AFMT_CHANNEL(fmt); m->ext = AFMT_EXTCHANNEL(fmt); for (x = 0; x != SND_CHN_T_MAX; x++) m->offset[x] = -1; } int feeder_chain(struct pcm_channel *c) { struct snddev_info *d; struct pcmchan_caps *caps; struct feeder_chain_desc cdesc; struct pcmchan_matrix *hwmatrix, *softmatrix; uint32_t hwfmt, softfmt; int ret; CHN_LOCKASSERT(c); /* Remove everything first. */ while (chn_removefeeder(c) == 0) ; KASSERT(c->feeder == NULL, ("feeder chain not empty")); /* clear and populate chain descriptor. */ bzero(&cdesc, sizeof(cdesc)); switch (feeder_chain_mode) { case FEEDER_CHAIN_LEAN: case FEEDER_CHAIN_16: case FEEDER_CHAIN_32: #if defined(SND_FEEDER_MULTIFORMAT) || defined(SND_FEEDER_FULL_MULTIFORMAT) case FEEDER_CHAIN_MULTI: #endif #if defined(SND_FEEDER_FULL_MULTIFORMAT) case FEEDER_CHAIN_FULLMULTI: #endif break; default: feeder_chain_mode = FEEDER_CHAIN_DEFAULT; break; } cdesc.mode = feeder_chain_mode; cdesc.expensive = 1; /* XXX faster.. */ #define VCHAN_PASSTHROUGH(c) (((c)->flags & (CHN_F_VIRTUAL | \ CHN_F_PASSTHROUGH)) == \ (CHN_F_VIRTUAL | CHN_F_PASSTHROUGH)) /* Get the best possible hardware format. */ if (VCHAN_PASSTHROUGH(c)) hwfmt = c->parentchannel->format; else { caps = chn_getcaps(c); if (caps == NULL || caps->fmtlist == NULL) { device_printf(c->dev, "%s(): failed to get channel caps\n", __func__); return (ENODEV); } if ((c->format & AFMT_PASSTHROUGH) && !snd_fmtvalid(c->format, caps->fmtlist)) return (ENODEV); hwfmt = snd_fmtbest(c->format, caps->fmtlist); if (hwfmt == 0 || !snd_fmtvalid(hwfmt, caps->fmtlist)) { device_printf(c->dev, "%s(): invalid hardware format 0x%08x\n", __func__, hwfmt); { int i; for (i = 0; caps->fmtlist[i] != 0; i++) printf("0x%08x\n", caps->fmtlist[i]); printf("Req: 0x%08x\n", c->format); } return (ENODEV); } } /* * The 'hardware' possibly have different interpretation of channel * matrixing, so get it first ..... */ hwmatrix = CHANNEL_GETMATRIX(c->methods, c->devinfo, hwfmt); if (hwmatrix == NULL) { /* setup a default matrix */ hwmatrix = &c->matrix_scratch; feeder_default_matrix(hwmatrix, hwfmt, SND_CHN_MATRIX_UNKNOWN); } /* ..... and rebuild hwfmt. */ hwfmt = SND_FORMAT(hwfmt, hwmatrix->channels, hwmatrix->ext); /* Reset and rebuild default channel format/matrix map. */ softfmt = c->format; softmatrix = &c->matrix; if (softmatrix->channels != AFMT_CHANNEL(softfmt) || softmatrix->ext != AFMT_EXTCHANNEL(softfmt)) { softmatrix = feeder_matrix_format_map(softfmt); if (softmatrix == NULL) { /* setup a default matrix */ softmatrix = &c->matrix; feeder_default_matrix(softmatrix, softfmt, SND_CHN_MATRIX_PCMCHANNEL); } else { c->matrix = *softmatrix; c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL; } } softfmt = SND_FORMAT(softfmt, softmatrix->channels, softmatrix->ext); if (softfmt != c->format) device_printf(c->dev, "%s(): WARNING: %s Soft format 0x%08x -> 0x%08x\n", __func__, CHN_DIRSTR(c), c->format, softfmt); /* * PLAY and REC are opposite. */ if (c->direction == PCMDIR_PLAY) { cdesc.origin.afmt = softfmt; cdesc.origin.matrix = softmatrix; cdesc.origin.rate = c->speed; cdesc.target.afmt = hwfmt; cdesc.target.matrix = hwmatrix; cdesc.target.rate = sndbuf_getspd(c->bufhard); } else { cdesc.origin.afmt = hwfmt; cdesc.origin.matrix = hwmatrix; cdesc.origin.rate = sndbuf_getspd(c->bufhard); cdesc.target.afmt = softfmt; cdesc.target.matrix = softmatrix; cdesc.target.rate = c->speed; } d = c->parentsnddev; /* * If channel is in bitperfect or passthrough mode, make it appear * that 'origin' and 'target' identical, skipping mostly chain * procedures. */ if (CHN_BITPERFECT(c) || (c->format & AFMT_PASSTHROUGH)) { if (c->direction == PCMDIR_PLAY) cdesc.origin = cdesc.target; else cdesc.target = cdesc.origin; c->format = cdesc.target.afmt; c->speed = cdesc.target.rate; } else { /* hwfmt is not convertible, so 'dummy' it. */ if (hwfmt & AFMT_PASSTHROUGH) cdesc.dummy = 1; if ((softfmt & AFMT_CONVERTIBLE) && (((d->flags & SD_F_VPC) && !(c->flags & CHN_F_HAS_VCHAN)) || (!(d->flags & SD_F_VPC) && (d->flags & SD_F_SOFTPCMVOL) && !(c->flags & CHN_F_VIRTUAL)))) cdesc.use_volume = 1; if (feeder_matrix_compare(cdesc.origin.matrix, cdesc.target.matrix) != 0) cdesc.use_matrix = 1; /* Soft EQ only applicable for PLAY. */ if (cdesc.dummy == 0 && c->direction == PCMDIR_PLAY && (d->flags & SD_F_EQ) && (((d->flags & SD_F_EQ_PC) && !(c->flags & CHN_F_HAS_VCHAN)) || (!(d->flags & SD_F_EQ_PC) && !(c->flags & CHN_F_VIRTUAL)))) cdesc.use_eq = 1; if (FEEDFORMAT_NE_REQUIRED(&cdesc)) { cdesc.afmt_ne = (cdesc.dummy != 0) ? snd_fmtbest(AFMT_ENCODING(softfmt), feeder_chain_formats[cdesc.mode]) : snd_fmtbest(AFMT_ENCODING(cdesc.target.afmt), feeder_chain_formats[cdesc.mode]); if (cdesc.afmt_ne == 0) { device_printf(c->dev, "%s(): snd_fmtbest failed!\n", __func__); cdesc.afmt_ne = (((cdesc.dummy != 0) ? softfmt : cdesc.target.afmt) & (AFMT_24BIT | AFMT_32BIT)) ? AFMT_S32_NE : AFMT_S16_NE; } } } cdesc.current = cdesc.origin; /* Build everything. */ c->feederflags = 0; #define FEEDER_BUILD(t) do { \ ret = feeder_build_##t(c, &cdesc); \ if (ret != 0) \ return (ret); \ } while (0) if (!(c->flags & CHN_F_HAS_VCHAN) || c->direction == PCMDIR_REC) FEEDER_BUILD(root); else if (c->direction == PCMDIR_PLAY && (c->flags & CHN_F_HAS_VCHAN)) FEEDER_BUILD(mixer); else return (ENOTSUP); /* * The basic idea is: The smaller the bandwidth, the cheaper the * conversion process, with following constraints:- * * 1) Almost all feeders work best in 16/32 native endian. * 2) Try to avoid 8bit feeders due to poor dynamic range. * 3) Avoid volume, format, matrix and rate in BITPERFECT or * PASSTHROUGH mode. * 4) Try putting volume before EQ or rate. Should help to * avoid/reduce possible clipping. * 5) EQ require specific, valid rate, unless it allow sloppy * conversion. */ if (FEEDMATRIX_UP(&cdesc)) { if (FEEDEQ_REQUIRED(&cdesc) && (!FEEDEQ_VALIDRATE(&cdesc, target) || (cdesc.expensive == 0 && FEEDEQ_ECONOMY(&cdesc)))) FEEDER_BUILD(eq); if (FEEDRATE_REQUIRED(&cdesc)) FEEDER_BUILD(rate); FEEDER_BUILD(matrix); if (FEEDVOLUME_REQUIRED(&cdesc)) FEEDER_BUILD(volume); if (FEEDEQ_REQUIRED(&cdesc)) FEEDER_BUILD(eq); } else if (FEEDMATRIX_DOWN(&cdesc)) { FEEDER_BUILD(matrix); if (FEEDVOLUME_REQUIRED(&cdesc)) FEEDER_BUILD(volume); if (FEEDEQ_REQUIRED(&cdesc) && (!FEEDEQ_VALIDRATE(&cdesc, target) || FEEDEQ_ECONOMY(&cdesc))) FEEDER_BUILD(eq); if (FEEDRATE_REQUIRED(&cdesc)) FEEDER_BUILD(rate); if (FEEDEQ_REQUIRED(&cdesc)) FEEDER_BUILD(eq); } else { if (FEEDRATE_DOWN(&cdesc)) { if (FEEDEQ_REQUIRED(&cdesc) && !FEEDEQ_VALIDRATE(&cdesc, target)) { if (FEEDVOLUME_REQUIRED(&cdesc)) FEEDER_BUILD(volume); FEEDER_BUILD(eq); } FEEDER_BUILD(rate); } if (FEEDMATRIX_REQUIRED(&cdesc)) FEEDER_BUILD(matrix); if (FEEDVOLUME_REQUIRED(&cdesc)) FEEDER_BUILD(volume); if (FEEDRATE_UP(&cdesc)) { if (FEEDEQ_REQUIRED(&cdesc) && !FEEDEQ_VALIDRATE(&cdesc, target)) FEEDER_BUILD(eq); FEEDER_BUILD(rate); } if (FEEDEQ_REQUIRED(&cdesc)) FEEDER_BUILD(eq); } if (FEEDFORMAT_REQUIRED(&cdesc)) FEEDER_BUILD(format); if (c->direction == PCMDIR_REC && (c->flags & CHN_F_HAS_VCHAN)) FEEDER_BUILD(mixer); sndbuf_setfmt(c->bufsoft, c->format); sndbuf_setspd(c->bufsoft, c->speed); sndbuf_setfmt(c->bufhard, hwfmt); chn_syncstate(c); return (0); } diff --git a/sys/dev/sound/pcm/feeder_matrix.c b/sys/dev/sound/pcm/feeder_matrix.c index c6a65113574a..d4925fe0d7e6 100644 --- a/sys/dev/sound/pcm/feeder_matrix.c +++ b/sys/dev/sound/pcm/feeder_matrix.c @@ -1,828 +1,828 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2008-2009 Ariff Abdullah * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * feeder_matrix: Generic any-to-any channel matrixing. Probably not the * accurate way of doing things, but it should be fast and * transparent enough, not to mention capable of handling * possible non-standard way of multichannel interleaving * order. In other words, it is tough to break. * * The Good: * + very generic and compact, provided that the supplied matrix map is in a * sane form. * + should be fast enough. * * The Bad: * + somebody might disagree with it. * + 'matrix' is kind of 0x7a69, due to prolong mental block. */ #ifdef _KERNEL #ifdef HAVE_KERNEL_OPTION_HEADERS #include "opt_snd.h" #endif #include #include #include "feeder_if.h" #define SND_USE_FXDIV #include "snd_fxdiv_gen.h" SND_DECLARE_FILE("$FreeBSD$"); #endif #define FEEDMATRIX_RESERVOIR (SND_CHN_MAX * PCM_32_BPS) #define SND_CHN_T_EOF 0x00e0fe0f #define SND_CHN_T_NULL 0x0e0e0e0e struct feed_matrix_info; typedef void (*feed_matrix_t)(struct feed_matrix_info *, uint8_t *, uint8_t *, uint32_t); struct feed_matrix_info { uint32_t bps; uint32_t ialign, oalign; uint32_t in, out; feed_matrix_t apply; #ifdef FEEDMATRIX_GENERIC intpcm_read_t *rd; intpcm_write_t *wr; #endif struct { int chn[SND_CHN_T_MAX + 1]; int mul, shift; } matrix[SND_CHN_T_MAX + 1]; uint8_t reservoir[FEEDMATRIX_RESERVOIR]; }; static struct pcmchan_matrix feeder_matrix_maps[SND_CHN_MATRIX_MAX] = { [SND_CHN_MATRIX_1_0] = SND_CHN_MATRIX_MAP_1_0, [SND_CHN_MATRIX_2_0] = SND_CHN_MATRIX_MAP_2_0, [SND_CHN_MATRIX_2_1] = SND_CHN_MATRIX_MAP_2_1, [SND_CHN_MATRIX_3_0] = SND_CHN_MATRIX_MAP_3_0, [SND_CHN_MATRIX_3_1] = SND_CHN_MATRIX_MAP_3_1, [SND_CHN_MATRIX_4_0] = SND_CHN_MATRIX_MAP_4_0, [SND_CHN_MATRIX_4_1] = SND_CHN_MATRIX_MAP_4_1, [SND_CHN_MATRIX_5_0] = SND_CHN_MATRIX_MAP_5_0, [SND_CHN_MATRIX_5_1] = SND_CHN_MATRIX_MAP_5_1, [SND_CHN_MATRIX_6_0] = SND_CHN_MATRIX_MAP_6_0, [SND_CHN_MATRIX_6_1] = SND_CHN_MATRIX_MAP_6_1, [SND_CHN_MATRIX_7_0] = SND_CHN_MATRIX_MAP_7_0, [SND_CHN_MATRIX_7_1] = SND_CHN_MATRIX_MAP_7_1 }; static int feeder_matrix_default_ids[9] = { [0] = SND_CHN_MATRIX_UNKNOWN, [1] = SND_CHN_MATRIX_1, [2] = SND_CHN_MATRIX_2, [3] = SND_CHN_MATRIX_3, [4] = SND_CHN_MATRIX_4, [5] = SND_CHN_MATRIX_5, [6] = SND_CHN_MATRIX_6, [7] = SND_CHN_MATRIX_7, [8] = SND_CHN_MATRIX_8 }; #ifdef _KERNEL #define FEEDMATRIX_CLIP_CHECK(...) #else #define FEEDMATRIX_CLIP_CHECK(v, BIT) do { \ if ((v) < PCM_S##BIT##_MIN || (v) > PCM_S##BIT##_MAX) \ errx(1, "\n\n%s(): Sample clipping: %jd\n", \ __func__, (intmax_t)(v)); \ } while (0) #endif #define FEEDMATRIX_DECLARE(SIGN, BIT, ENDIAN) \ static void \ feed_matrix_##SIGN##BIT##ENDIAN(struct feed_matrix_info *info, \ uint8_t *src, uint8_t *dst, uint32_t count) \ { \ intpcm64_t accum; \ intpcm_t v; \ int i, j; \ \ do { \ for (i = 0; info->matrix[i].chn[0] != SND_CHN_T_EOF; \ i++) { \ if (info->matrix[i].chn[0] == SND_CHN_T_NULL) { \ _PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, \ 0); \ dst += PCM_##BIT##_BPS; \ continue; \ } else if (info->matrix[i].chn[1] == \ SND_CHN_T_EOF) { \ v = _PCM_READ_##SIGN##BIT##_##ENDIAN( \ src + info->matrix[i].chn[0]); \ _PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, \ v); \ dst += PCM_##BIT##_BPS; \ continue; \ } \ \ accum = 0; \ for (j = 0; \ info->matrix[i].chn[j] != SND_CHN_T_EOF; \ j++) { \ v = _PCM_READ_##SIGN##BIT##_##ENDIAN( \ src + info->matrix[i].chn[j]); \ accum += v; \ } \ \ accum = (accum * info->matrix[i].mul) >> \ info->matrix[i].shift; \ \ FEEDMATRIX_CLIP_CHECK(accum, BIT); \ \ v = (accum > PCM_S##BIT##_MAX) ? \ PCM_S##BIT##_MAX : \ ((accum < PCM_S##BIT##_MIN) ? \ PCM_S##BIT##_MIN : \ accum); \ _PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, v); \ dst += PCM_##BIT##_BPS; \ } \ src += info->ialign; \ } while (--count != 0); \ } #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) FEEDMATRIX_DECLARE(S, 16, LE) FEEDMATRIX_DECLARE(S, 32, LE) #endif #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) FEEDMATRIX_DECLARE(S, 16, BE) FEEDMATRIX_DECLARE(S, 32, BE) #endif #ifdef SND_FEEDER_MULTIFORMAT FEEDMATRIX_DECLARE(S, 8, NE) FEEDMATRIX_DECLARE(S, 24, LE) FEEDMATRIX_DECLARE(S, 24, BE) FEEDMATRIX_DECLARE(U, 8, NE) FEEDMATRIX_DECLARE(U, 16, LE) FEEDMATRIX_DECLARE(U, 24, LE) FEEDMATRIX_DECLARE(U, 32, LE) FEEDMATRIX_DECLARE(U, 16, BE) FEEDMATRIX_DECLARE(U, 24, BE) FEEDMATRIX_DECLARE(U, 32, BE) #endif #define FEEDMATRIX_ENTRY(SIGN, BIT, ENDIAN) \ { \ AFMT_##SIGN##BIT##_##ENDIAN, \ feed_matrix_##SIGN##BIT##ENDIAN \ } static const struct { uint32_t format; feed_matrix_t apply; } feed_matrix_tab[] = { #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) FEEDMATRIX_ENTRY(S, 16, LE), FEEDMATRIX_ENTRY(S, 32, LE), #endif #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) FEEDMATRIX_ENTRY(S, 16, BE), FEEDMATRIX_ENTRY(S, 32, BE), #endif #ifdef SND_FEEDER_MULTIFORMAT FEEDMATRIX_ENTRY(S, 8, NE), FEEDMATRIX_ENTRY(S, 24, LE), FEEDMATRIX_ENTRY(S, 24, BE), FEEDMATRIX_ENTRY(U, 8, NE), FEEDMATRIX_ENTRY(U, 16, LE), FEEDMATRIX_ENTRY(U, 24, LE), FEEDMATRIX_ENTRY(U, 32, LE), FEEDMATRIX_ENTRY(U, 16, BE), FEEDMATRIX_ENTRY(U, 24, BE), FEEDMATRIX_ENTRY(U, 32, BE) #endif }; static void feed_matrix_reset(struct feed_matrix_info *info) { uint32_t i, j; for (i = 0; i < (sizeof(info->matrix) / sizeof(info->matrix[0])); i++) { for (j = 0; j < (sizeof(info->matrix[i].chn) / sizeof(info->matrix[i].chn[0])); j++) { info->matrix[i].chn[j] = SND_CHN_T_EOF; } info->matrix[i].mul = 1; info->matrix[i].shift = 0; } } #ifdef FEEDMATRIX_GENERIC static void feed_matrix_apply_generic(struct feed_matrix_info *info, uint8_t *src, uint8_t *dst, uint32_t count) { intpcm64_t accum; intpcm_t v; int i, j; do { for (i = 0; info->matrix[i].chn[0] != SND_CHN_T_EOF; i++) { if (info->matrix[i].chn[0] == SND_CHN_T_NULL) { info->wr(dst, 0); dst += info->bps; continue; } else if (info->matrix[i].chn[1] == SND_CHN_T_EOF) { v = info->rd(src + info->matrix[i].chn[0]); info->wr(dst, v); dst += info->bps; continue; } accum = 0; for (j = 0; info->matrix[i].chn[j] != SND_CHN_T_EOF; j++) { v = info->rd(src + info->matrix[i].chn[j]); accum += v; } accum = (accum * info->matrix[i].mul) >> info->matrix[i].shift; FEEDMATRIX_CLIP_CHECK(accum, 32); v = (accum > PCM_S32_MAX) ? PCM_S32_MAX : ((accum < PCM_S32_MIN) ? PCM_S32_MIN : accum); info->wr(dst, v); dst += info->bps; } src += info->ialign; } while (--count != 0); } #endif static int feed_matrix_setup(struct feed_matrix_info *info, struct pcmchan_matrix *m_in, struct pcmchan_matrix *m_out) { uint32_t i, j, ch, in_mask, merge_mask; int mul, shift; if (info == NULL || m_in == NULL || m_out == NULL || AFMT_CHANNEL(info->in) != m_in->channels || AFMT_CHANNEL(info->out) != m_out->channels || m_in->channels < SND_CHN_MIN || m_in->channels > SND_CHN_MAX || m_out->channels < SND_CHN_MIN || m_out->channels > SND_CHN_MAX) return (EINVAL); feed_matrix_reset(info); /* * If both in and out are part of standard matrix and identical, skip * everything alltogether. */ if (m_in->id == m_out->id && !(m_in->id < SND_CHN_MATRIX_BEGIN || m_in->id > SND_CHN_MATRIX_END)) return (0); /* * Special case for mono input matrix. If the output supports * possible 'center' channel, route it there. Otherwise, let it be * matrixed to left/right. */ if (m_in->id == SND_CHN_MATRIX_1_0) { if (m_out->id == SND_CHN_MATRIX_1_0) in_mask = SND_CHN_T_MASK_FL; else if (m_out->mask & SND_CHN_T_MASK_FC) in_mask = SND_CHN_T_MASK_FC; else in_mask = SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR; } else in_mask = m_in->mask; /* Merge, reduce, expand all possibilites. */ for (ch = SND_CHN_T_BEGIN; ch <= SND_CHN_T_END && m_out->map[ch].type != SND_CHN_T_MAX; ch += SND_CHN_T_STEP) { merge_mask = m_out->map[ch].members & in_mask; if (merge_mask == 0) { info->matrix[ch].chn[0] = SND_CHN_T_NULL; continue; } j = 0; for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END; i += SND_CHN_T_STEP) { if (merge_mask & (1 << i)) { if (m_in->offset[i] >= 0 && m_in->offset[i] < (int)m_in->channels) info->matrix[ch].chn[j++] = m_in->offset[i] * info->bps; else { info->matrix[ch].chn[j++] = SND_CHN_T_EOF; break; } } } #define FEEDMATRIX_ATTN_SHIFT 16 if (j > 1) { /* * XXX For channel that require accumulation from * multiple channels, apply a slight attenuation to * avoid clipping. */ mul = (1 << (FEEDMATRIX_ATTN_SHIFT - 1)) + 143 - j; shift = FEEDMATRIX_ATTN_SHIFT; while ((mul & 1) == 0 && shift > 0) { mul >>= 1; shift--; } info->matrix[ch].mul = mul; info->matrix[ch].shift = shift; } } #ifndef _KERNEL fprintf(stderr, "Total: %d\n", ch); for (i = 0; info->matrix[i].chn[0] != SND_CHN_T_EOF; i++) { fprintf(stderr, "%d: [", i); for (j = 0; info->matrix[i].chn[j] != SND_CHN_T_EOF; j++) { if (j != 0) fprintf(stderr, ", "); fprintf(stderr, "%d", (info->matrix[i].chn[j] == SND_CHN_T_NULL) ? 0xffffffff : info->matrix[i].chn[j] / info->bps); } fprintf(stderr, "] attn: (x * %d) >> %d\n", info->matrix[i].mul, info->matrix[i].shift); } #endif return (0); } static int feed_matrix_init(struct pcm_feeder *f) { struct feed_matrix_info *info; struct pcmchan_matrix *m_in, *m_out; uint32_t i; int ret; if (AFMT_ENCODING(f->desc->in) != AFMT_ENCODING(f->desc->out)) return (EINVAL); info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT | M_ZERO); if (info == NULL) return (ENOMEM); info->in = f->desc->in; info->out = f->desc->out; info->bps = AFMT_BPS(info->in); info->ialign = AFMT_ALIGN(info->in); info->oalign = AFMT_ALIGN(info->out); info->apply = NULL; for (i = 0; info->apply == NULL && i < (sizeof(feed_matrix_tab) / sizeof(feed_matrix_tab[0])); i++) { if (AFMT_ENCODING(info->in) == feed_matrix_tab[i].format) info->apply = feed_matrix_tab[i].apply; } if (info->apply == NULL) { #ifdef FEEDMATRIX_GENERIC info->rd = feeder_format_read_op(info->in); info->wr = feeder_format_write_op(info->out); if (info->rd == NULL || info->wr == NULL) { free(info, M_DEVBUF); return (EINVAL); } info->apply = feed_matrix_apply_generic; #else free(info, M_DEVBUF); return (EINVAL); #endif } m_in = feeder_matrix_format_map(info->in); m_out = feeder_matrix_format_map(info->out); ret = feed_matrix_setup(info, m_in, m_out); if (ret != 0) { free(info, M_DEVBUF); return (ret); } f->data = info; return (0); } static int feed_matrix_free(struct pcm_feeder *f) { struct feed_matrix_info *info; info = f->data; if (info != NULL) free(info, M_DEVBUF); f->data = NULL; return (0); } static int feed_matrix_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b, uint32_t count, void *source) { struct feed_matrix_info *info; uint32_t j, inmax; uint8_t *src, *dst; info = f->data; if (info->matrix[0].chn[0] == SND_CHN_T_EOF) return (FEEDER_FEED(f->source, c, b, count, source)); dst = b; count = SND_FXROUND(count, info->oalign); inmax = info->ialign + info->oalign; /* * This loop might look simmilar to other feeder_* loops, but be * advised: matrixing might involve overlapping (think about * swapping end to front or something like that). In this regard it * might be simmilar to feeder_format, but feeder_format works on * 'sample' domain where it can be fitted into single 32bit integer * while matrixing works on 'sample frame' domain. */ do { if (count < info->oalign) break; if (count < inmax) { src = info->reservoir; j = info->ialign; } else { if (info->ialign == info->oalign) j = count - info->oalign; else if (info->ialign > info->oalign) j = SND_FXROUND(count - info->oalign, info->ialign); else j = (SND_FXDIV(count, info->oalign) - 1) * info->ialign; src = dst + count - j; } j = SND_FXDIV(FEEDER_FEED(f->source, c, src, j, source), info->ialign); if (j == 0) break; info->apply(info, src, dst, j); j *= info->oalign; dst += j; count -= j; } while (count != 0); return (dst - b); } static struct pcm_feederdesc feeder_matrix_desc[] = { { FEEDER_MATRIX, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } }; static kobj_method_t feeder_matrix_methods[] = { KOBJMETHOD(feeder_init, feed_matrix_init), KOBJMETHOD(feeder_free, feed_matrix_free), KOBJMETHOD(feeder_feed, feed_matrix_feed), KOBJMETHOD_END }; FEEDER_DECLARE(feeder_matrix, NULL); /* External */ int feeder_matrix_setup(struct pcm_feeder *f, struct pcmchan_matrix *m_in, struct pcmchan_matrix *m_out) { if (f == NULL || f->desc == NULL || f->desc->type != FEEDER_MATRIX || f->data == NULL) return (EINVAL); return (feed_matrix_setup(f->data, m_in, m_out)); } /* * feeder_matrix_default_id(): For a given number of channels, return - * default prefered id (example: both 5.1 and + * default preferred id (example: both 5.1 and * 6.0 are simply 6 channels, but 5.1 is more * preferable). */ int feeder_matrix_default_id(uint32_t ch) { if (ch < feeder_matrix_maps[SND_CHN_MATRIX_BEGIN].channels || ch > feeder_matrix_maps[SND_CHN_MATRIX_END].channels) return (SND_CHN_MATRIX_UNKNOWN); return (feeder_matrix_maps[feeder_matrix_default_ids[ch]].id); } /* * feeder_matrix_default_channel_map(): Ditto, but return matrix map * instead. */ struct pcmchan_matrix * feeder_matrix_default_channel_map(uint32_t ch) { if (ch < feeder_matrix_maps[SND_CHN_MATRIX_BEGIN].channels || ch > feeder_matrix_maps[SND_CHN_MATRIX_END].channels) return (NULL); return (&feeder_matrix_maps[feeder_matrix_default_ids[ch]]); } /* * feeder_matrix_default_format(): For a given audio format, return the * proper audio format based on preferable * matrix. */ uint32_t feeder_matrix_default_format(uint32_t format) { struct pcmchan_matrix *m; uint32_t i, ch, ext; ch = AFMT_CHANNEL(format); ext = AFMT_EXTCHANNEL(format); if (ext != 0) { for (i = SND_CHN_MATRIX_BEGIN; i <= SND_CHN_MATRIX_END; i++) { if (feeder_matrix_maps[i].channels == ch && feeder_matrix_maps[i].ext == ext) return (SND_FORMAT(format, ch, ext)); } } m = feeder_matrix_default_channel_map(ch); if (m == NULL) return (0x00000000); return (SND_FORMAT(format, ch, m->ext)); } /* * feeder_matrix_format_id(): For a given audio format, return its matrix * id. */ int feeder_matrix_format_id(uint32_t format) { uint32_t i, ch, ext; ch = AFMT_CHANNEL(format); ext = AFMT_EXTCHANNEL(format); for (i = SND_CHN_MATRIX_BEGIN; i <= SND_CHN_MATRIX_END; i++) { if (feeder_matrix_maps[i].channels == ch && feeder_matrix_maps[i].ext == ext) return (feeder_matrix_maps[i].id); } return (SND_CHN_MATRIX_UNKNOWN); } /* * feeder_matrix_format_map(): For a given audio format, return its matrix * map. */ struct pcmchan_matrix * feeder_matrix_format_map(uint32_t format) { uint32_t i, ch, ext; ch = AFMT_CHANNEL(format); ext = AFMT_EXTCHANNEL(format); for (i = SND_CHN_MATRIX_BEGIN; i <= SND_CHN_MATRIX_END; i++) { if (feeder_matrix_maps[i].channels == ch && feeder_matrix_maps[i].ext == ext) return (&feeder_matrix_maps[i]); } return (NULL); } /* * feeder_matrix_id_map(): For a given matrix id, return its matrix map. */ struct pcmchan_matrix * feeder_matrix_id_map(int id) { if (id < SND_CHN_MATRIX_BEGIN || id > SND_CHN_MATRIX_END) return (NULL); return (&feeder_matrix_maps[id]); } /* * feeder_matrix_compare(): Compare the simmilarities of matrices. */ int feeder_matrix_compare(struct pcmchan_matrix *m_in, struct pcmchan_matrix *m_out) { uint32_t i; if (m_in == m_out) return (0); if (m_in->channels != m_out->channels || m_in->ext != m_out->ext || m_in->mask != m_out->mask) return (1); for (i = 0; i < (sizeof(m_in->map) / sizeof(m_in->map[0])); i++) { if (m_in->map[i].type != m_out->map[i].type) return (1); if (m_in->map[i].type == SND_CHN_T_MAX) break; if (m_in->map[i].members != m_out->map[i].members) return (1); if (i <= SND_CHN_T_END) { if (m_in->offset[m_in->map[i].type] != m_out->offset[m_out->map[i].type]) return (1); } } return (0); } /* * XXX 4front interpretation of "surround" is ambigous and sort of * conflicting with "rear"/"back". Map it to "side". Well.. * who cares? */ static int snd_chn_to_oss[SND_CHN_T_MAX] = { [SND_CHN_T_FL] = CHID_L, [SND_CHN_T_FR] = CHID_R, [SND_CHN_T_FC] = CHID_C, [SND_CHN_T_LF] = CHID_LFE, [SND_CHN_T_SL] = CHID_LS, [SND_CHN_T_SR] = CHID_RS, [SND_CHN_T_BL] = CHID_LR, [SND_CHN_T_BR] = CHID_RR }; #define SND_CHN_OSS_VALIDMASK \ (SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR | \ SND_CHN_T_MASK_FC | SND_CHN_T_MASK_LF | \ SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR | \ SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR) #define SND_CHN_OSS_MAX 8 #define SND_CHN_OSS_BEGIN CHID_L #define SND_CHN_OSS_END CHID_RR static int oss_to_snd_chn[SND_CHN_OSS_END + 1] = { [CHID_L] = SND_CHN_T_FL, [CHID_R] = SND_CHN_T_FR, [CHID_C] = SND_CHN_T_FC, [CHID_LFE] = SND_CHN_T_LF, [CHID_LS] = SND_CHN_T_SL, [CHID_RS] = SND_CHN_T_SR, [CHID_LR] = SND_CHN_T_BL, [CHID_RR] = SND_CHN_T_BR }; /* * Used by SNDCTL_DSP_GET_CHNORDER. */ int feeder_matrix_oss_get_channel_order(struct pcmchan_matrix *m, unsigned long long *map) { unsigned long long tmpmap; uint32_t i; if (m == NULL || map == NULL || (m->mask & ~SND_CHN_OSS_VALIDMASK) || m->channels > SND_CHN_OSS_MAX) return (EINVAL); tmpmap = 0x0000000000000000ULL; for (i = 0; i < SND_CHN_OSS_MAX && m->map[i].type != SND_CHN_T_MAX; i++) { if ((1 << m->map[i].type) & ~SND_CHN_OSS_VALIDMASK) return (EINVAL); tmpmap |= (unsigned long long)snd_chn_to_oss[m->map[i].type] << (i * 4); } *map = tmpmap; return (0); } /* * Used by SNDCTL_DSP_SET_CHNORDER. */ int feeder_matrix_oss_set_channel_order(struct pcmchan_matrix *m, unsigned long long *map) { struct pcmchan_matrix tmp; uint32_t chmask, i; int ch, cheof; if (m == NULL || map == NULL || (m->mask & ~SND_CHN_OSS_VALIDMASK) || m->channels > SND_CHN_OSS_MAX || (*map & 0xffffffff00000000ULL)) return (EINVAL); tmp = *m; tmp.channels = 0; tmp.ext = 0; tmp.mask = 0; memset(tmp.offset, -1, sizeof(tmp.offset)); cheof = 0; for (i = 0; i < SND_CHN_OSS_MAX; i++) { ch = (*map >> (i * 4)) & 0xf; if (ch < SND_CHN_OSS_BEGIN) { if (cheof == 0 && m->map[i].type != SND_CHN_T_MAX) return (EINVAL); cheof++; tmp.map[i] = m->map[i]; continue; } else if (ch > SND_CHN_OSS_END) return (EINVAL); else if (cheof != 0) return (EINVAL); ch = oss_to_snd_chn[ch]; chmask = 1 << ch; /* channel not exist in matrix */ if (!(chmask & m->mask)) return (EINVAL); /* duplicated channel */ if (chmask & tmp.mask) return (EINVAL); tmp.map[i] = m->map[m->offset[ch]]; if (tmp.map[i].type != ch) return (EINVAL); tmp.offset[ch] = i; tmp.mask |= chmask; tmp.channels++; if (chmask & SND_CHN_T_MASK_LF) tmp.ext++; } if (tmp.channels != m->channels || tmp.ext != m->ext || tmp.mask != m->mask || tmp.map[m->channels].type != SND_CHN_T_MAX) return (EINVAL); *m = tmp; return (0); }