feeder.c revision 167644
11541Srgrimes/*- 21541Srgrimes * Copyright (c) 1999 Cameron Grant <cg@freebsd.org> 31541Srgrimes * All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 141541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 151541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 161541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 171541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 181541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 191541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 201541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 211541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 221541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 231541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 241541Srgrimes * SUCH DAMAGE. 251541Srgrimes */ 261541Srgrimes 271541Srgrimes#include <dev/sound/pcm/sound.h> 281541Srgrimes 291541Srgrimes#include "feeder_if.h" 301541Srgrimes 311541SrgrimesSND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/feeder.c 167644 2007-03-16 17:15:33Z ariff $"); 321541Srgrimes 331541SrgrimesMALLOC_DEFINE(M_FEEDER, "feeder", "pcm feeder"); 3421673Sjkh 351541Srgrimes#define MAXFEEDERS 256 361541Srgrimes#undef FEEDER_DEBUG 372165Spaul 382811Sbdeint feeder_buffersize = FEEDBUFSZ; 392165SpaulTUNABLE_INT("hw.snd.feeder_buffersize", &feeder_buffersize); 4022521Sdyson 411541Srgrimes#ifdef SND_DEBUG 421541Srgrimesstatic int 431541Srgrimessysctl_hw_snd_feeder_buffersize(SYSCTL_HANDLER_ARGS) 441541Srgrimes{ 451541Srgrimes int i, err, val; 461541Srgrimes 471541Srgrimes val = feeder_buffersize; 481541Srgrimes err = sysctl_handle_int(oidp, &val, sizeof(val), req); 491541Srgrimes 501541Srgrimes if (err != 0 || req->newptr == NULL) 511541Srgrimes return err; 521541Srgrimes 531541Srgrimes if (val < FEEDBUFSZ_MIN || val > FEEDBUFSZ_MAX) 541541Srgrimes return EINVAL; 551541Srgrimes 561541Srgrimes i = 0; 571541Srgrimes while (val >> i) 581541Srgrimes i++; 591541Srgrimes i = 1 << i; 601541Srgrimes if (i > val && (i >> 1) > 0 && (i >> 1) >= ((val * 3) >> 2)) 611541Srgrimes i >>= 1; 6218990Sjkh 631541Srgrimes feeder_buffersize = i; 641541Srgrimes 651541Srgrimes return err; 661541Srgrimes} 671541SrgrimesSYSCTL_PROC(_hw_snd, OID_AUTO, feeder_buffersize, CTLTYPE_INT | CTLFLAG_RW, 681541Srgrimes 0, sizeof(int), sysctl_hw_snd_feeder_buffersize, "I", 691541Srgrimes "feeder buffer size"); 701541Srgrimes#else 7112158SbdeSYSCTL_INT(_hw_snd, OID_AUTO, feeder_buffersize, CTLFLAG_RD, 7217761Sdyson &feeder_buffersize, FEEDBUFSZ, "feeder buffer size"); 7312158Sbde#endif 7422521Sdyson 7522521Sdysonstruct feedertab_entry { 7622521Sdyson SLIST_ENTRY(feedertab_entry) link; 7722521Sdyson struct feeder_class *feederclass; 7822521Sdyson struct pcm_feederdesc *desc; 7922521Sdyson 8022521Sdyson int idx; 811541Srgrimes}; 821541Srgrimesstatic SLIST_HEAD(, feedertab_entry) feedertab; 8314902Sdg 8414902Sdg/*****************************************************************************/ 8514902Sdg 861541Srgrimesvoid 871541Srgrimesfeeder_register(void *p) 881541Srgrimes{ 8912158Sbde static int feedercnt = 0; 901541Srgrimes 911541Srgrimes struct feeder_class *fc = p; 921541Srgrimes struct feedertab_entry *fte; 931541Srgrimes int i; 941541Srgrimes 951541Srgrimes if (feedercnt == 0) { 961541Srgrimes KASSERT(fc->desc == NULL, ("first feeder not root: %s", fc->name)); 971541Srgrimes 981541Srgrimes SLIST_INIT(&feedertab); 991541Srgrimes fte = malloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO); 1001541Srgrimes if (fte == NULL) { 1011541Srgrimes printf("can't allocate memory for root feeder: %s\n", 1021541Srgrimes fc->name); 1031541Srgrimes 1041541Srgrimes return; 1051541Srgrimes } 1061541Srgrimes fte->feederclass = fc; 10713490Sdyson fte->desc = NULL; 10817761Sdyson fte->idx = feedercnt; 10922521Sdyson SLIST_INSERT_HEAD(&feedertab, fte, link); 11022521Sdyson feedercnt++; 1111541Srgrimes 1121541Srgrimes /* initialize global variables */ 1131541Srgrimes 1141541Srgrimes if (snd_verbose < 0 || snd_verbose > 3) 1151541Srgrimes snd_verbose = 1; 1161541Srgrimes 1171541Srgrimes if (snd_unit < 0 || snd_unit > PCMMAXDEV) 1181541Srgrimes snd_unit = 0; 1191541Srgrimes 1201541Srgrimes if (snd_maxautovchans < 0 || 1211541Srgrimes snd_maxautovchans > SND_MAXVCHANS) 12222521Sdyson snd_maxautovchans = 0; 12322521Sdyson 12422521Sdyson if (chn_latency < CHN_LATENCY_MIN || 12522521Sdyson chn_latency > CHN_LATENCY_MAX) 12622521Sdyson chn_latency = CHN_LATENCY_DEFAULT; 12722521Sdyson 12822521Sdyson if (chn_latency_profile < CHN_LATENCY_PROFILE_MIN || 12922521Sdyson chn_latency_profile > CHN_LATENCY_PROFILE_MAX) 13022521Sdyson chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT; 13122521Sdyson 13222521Sdyson if (feeder_buffersize < FEEDBUFSZ_MIN || 13322521Sdyson feeder_buffersize > FEEDBUFSZ_MAX) 13422521Sdyson feeder_buffersize = FEEDBUFSZ; 13522521Sdyson 1361541Srgrimes if (feeder_rate_min < FEEDRATE_MIN || 1371541Srgrimes feeder_rate_max < FEEDRATE_MIN || 1381541Srgrimes feeder_rate_min > FEEDRATE_MAX || 1391541Srgrimes feeder_rate_max > FEEDRATE_MAX || 1401541Srgrimes !(feeder_rate_min < feeder_rate_max)) { 1411541Srgrimes feeder_rate_min = FEEDRATE_RATEMIN; 1421541Srgrimes feeder_rate_max = FEEDRATE_RATEMAX; 1431541Srgrimes } 1441541Srgrimes 1451541Srgrimes if (feeder_rate_round < FEEDRATE_ROUNDHZ_MIN || 1461541Srgrimes feeder_rate_round > FEEDRATE_ROUNDHZ_MAX) 1471541Srgrimes feeder_rate_round = FEEDRATE_ROUNDHZ; 1481541Srgrimes 1491541Srgrimes if (bootverbose) 1501541Srgrimes printf("%s: snd_unit=%d snd_maxautovchans=%d " 1511541Srgrimes "latency=%d feeder_buffersize=%d " 1521541Srgrimes "feeder_rate_min=%d feeder_rate_max=%d " 1531541Srgrimes "feeder_rate_round=%d\n", 1541541Srgrimes __func__, snd_unit, snd_maxautovchans, 1551541Srgrimes chn_latency, feeder_buffersize, 1561541Srgrimes feeder_rate_min, feeder_rate_max, 1571541Srgrimes feeder_rate_round); 1581541Srgrimes 1591541Srgrimes /* we've got our root feeder so don't veto pcm loading anymore */ 1601541Srgrimes pcm_veto_load = 0; 1611541Srgrimes 1621541Srgrimes return; 1631541Srgrimes } 16422521Sdyson 1651541Srgrimes KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name)); 1661541Srgrimes 16722521Sdyson /* beyond this point failure is non-fatal but may result in some translations being unavailable */ 1681541Srgrimes i = 0; 1691541Srgrimes while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) { 1701541Srgrimes /* printf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */ 1711541Srgrimes fte = malloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO); 1721541Srgrimes if (fte == NULL) { 1731541Srgrimes printf("can't allocate memory for feeder '%s', %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); 1741541Srgrimes 1751541Srgrimes return; 1761541Srgrimes } 17712873Sbde fte->feederclass = fc; 1781541Srgrimes fte->desc = &fc->desc[i]; 1791541Srgrimes fte->idx = feedercnt; 1801541Srgrimes fte->desc->idx = feedercnt; 1811541Srgrimes SLIST_INSERT_HEAD(&feedertab, fte, link); 1821541Srgrimes i++; 1831541Srgrimes } 1841541Srgrimes feedercnt++; 1851541Srgrimes if (feedercnt >= MAXFEEDERS) 1861541Srgrimes printf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS); 1871541Srgrimes} 1881541Srgrimes 1891541Srgrimesstatic void 1901541Srgrimesfeeder_unregisterall(void *p) 1911541Srgrimes{ 1921541Srgrimes struct feedertab_entry *fte, *next; 1931541Srgrimes 1941541Srgrimes next = SLIST_FIRST(&feedertab); 1951541Srgrimes while (next != NULL) { 1961541Srgrimes fte = next; 1971541Srgrimes next = SLIST_NEXT(fte, link); 1981541Srgrimes free(fte, M_FEEDER); 1991541Srgrimes } 2001541Srgrimes} 2011541Srgrimes 2021541Srgrimesstatic int 2031541Srgrimescmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m) 2041541Srgrimes{ 2051541Srgrimes return ((n->type == m->type) && 2061541Srgrimes ((n->in == 0) || (n->in == m->in)) && 2071541Srgrimes ((n->out == 0) || (n->out == m->out)) && 2081541Srgrimes (n->flags == m->flags)); 20913765Smpp} 21013765Smpp 2111541Srgrimesstatic void 2121541Srgrimesfeeder_destroy(struct pcm_feeder *f) 2131541Srgrimes{ 21422521Sdyson FEEDER_FREE(f); 2151541Srgrimes kobj_delete((kobj_t)f, M_FEEDER); 2161541Srgrimes} 2171541Srgrimes 2181541Srgrimesstatic struct pcm_feeder * 2191541Srgrimesfeeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc) 2201541Srgrimes{ 2211541Srgrimes struct pcm_feeder *f; 2221541Srgrimes int err; 2231541Srgrimes 22422521Sdyson f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_NOWAIT | M_ZERO); 2251541Srgrimes if (f == NULL) 2261541Srgrimes return NULL; 22722521Sdyson 22822521Sdyson f->align = fc->align; 22922521Sdyson f->data = fc->data; 23022521Sdyson f->source = NULL; 23122521Sdyson f->parent = NULL; 23222521Sdyson f->class = fc; 23322521Sdyson f->desc = &(f->desc_static); 23422521Sdyson 23522521Sdyson if (desc) { 23622521Sdyson *(f->desc) = *desc; 23722521Sdyson } else { 23822521Sdyson f->desc->type = FEEDER_ROOT; 23922521Sdyson f->desc->in = 0; 24022521Sdyson f->desc->out = 0; 24122521Sdyson f->desc->flags = 0; 24222521Sdyson f->desc->idx = 0; 24317761Sdyson } 24422521Sdyson 24522521Sdyson err = FEEDER_INIT(f); 2461541Srgrimes if (err) { 2471541Srgrimes printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err); 2481541Srgrimes feeder_destroy(f); 2492946Swollman 25012873Sbde return NULL; 2512946Swollman } 25212873Sbde 2532946Swollman return f; 2542946Swollman} 2551541Srgrimes 2561541Srgrimesstruct feeder_class * 2571541Srgrimesfeeder_getclass(struct pcm_feederdesc *desc) 2581541Srgrimes{ 2591541Srgrimes struct feedertab_entry *fte; 2607090Sbde 2611541Srgrimes SLIST_FOREACH(fte, &feedertab, link) { 2621541Srgrimes if ((desc == NULL) && (fte->desc == NULL)) 2631541Srgrimes return fte->feederclass; 2641541Srgrimes if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc)) 2651541Srgrimes return fte->feederclass; 2661541Srgrimes } 2671541Srgrimes return NULL; 2681541Srgrimes} 26922521Sdyson 2702997Swollmanint 2712997Swollmanchn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc) 2721541Srgrimes{ 2737461Sdg struct pcm_feeder *nf; 2741541Srgrimes 2751541Srgrimes nf = feeder_create(fc, desc); 27612873Sbde if (nf == NULL) 2777461Sdg return ENOSPC; 2787461Sdg 27912873Sbde nf->source = c->feeder; 2802997Swollman 2811541Srgrimes /* XXX we should use the lowest common denominator for align */ 28222521Sdyson if (nf->align > 0) 2831541Srgrimes c->align += nf->align; 2841541Srgrimes else if (nf->align < 0 && c->align < -nf->align) 2851541Srgrimes c->align = -nf->align; 2861541Srgrimes if (c->feeder != NULL) 28713765Smpp c->feeder->parent = nf; 2881541Srgrimes c->feeder = nf; 2891541Srgrimes 2901541Srgrimes return 0; 2911541Srgrimes} 2921541Srgrimes 2931541Srgrimesint 2941541Srgrimeschn_removefeeder(struct pcm_channel *c) 2951541Srgrimes{ 2961541Srgrimes struct pcm_feeder *f; 2971541Srgrimes 2981541Srgrimes if (c->feeder == NULL) 2991541Srgrimes return -1; 3001541Srgrimes f = c->feeder; 3011541Srgrimes c->feeder = c->feeder->source; 3021541Srgrimes feeder_destroy(f); 3031541Srgrimes 3041541Srgrimes return 0; 3051541Srgrimes} 3061541Srgrimes 3071541Srgrimesstruct pcm_feeder * 3081541Srgrimeschn_findfeeder(struct pcm_channel *c, u_int32_t type) 3091541Srgrimes{ 3101541Srgrimes struct pcm_feeder *f; 3111541Srgrimes 3121541Srgrimes f = c->feeder; 3131541Srgrimes while (f != NULL) { 3141541Srgrimes if (f->desc->type == type) 3151541Srgrimes return f; 3161541Srgrimes f = f->source; 3171541Srgrimes } 3181541Srgrimes 3191541Srgrimes return NULL; 3201541Srgrimes} 3211541Srgrimes 3221541Srgrimesstatic int 3231541Srgrimeschainok(struct pcm_feeder *test, struct pcm_feeder *stop) 3241541Srgrimes{ 3251541Srgrimes u_int32_t visited[MAXFEEDERS / 32]; 3261541Srgrimes u_int32_t idx, mask; 3271541Srgrimes 3281541Srgrimes bzero(visited, sizeof(visited)); 3291541Srgrimes while (test && (test != stop)) { 3301541Srgrimes idx = test->desc->idx; 3311541Srgrimes if (idx < 0) 3321541Srgrimes panic("bad idx %d", idx); 3331541Srgrimes if (idx >= MAXFEEDERS) 3341541Srgrimes panic("bad idx %d", idx); 3351541Srgrimes mask = 1 << (idx & 31); 3361541Srgrimes idx >>= 5; 3371541Srgrimes if (visited[idx] & mask) 3381541Srgrimes return 0; 3391541Srgrimes visited[idx] |= mask; 3401541Srgrimes test = test->source; 34122521Sdyson } 34222521Sdyson 34322521Sdyson return 1; 34422521Sdyson} 3451541Srgrimes 3461541Srgrimes/* 3471541Srgrimes * See feeder_fmtchain() for the mumbo-jumbo ridiculous explaination 3481541Srgrimes * of what the heck is this FMT_Q_* 34913765Smpp */ 3501541Srgrimes#define FMT_Q_UP 1 35113765Smpp#define FMT_Q_DOWN 2 3521541Srgrimes#define FMT_Q_EQ 3 3531541Srgrimes#define FMT_Q_MULTI 4 3541541Srgrimes 3551541Srgrimes/* 3561541Srgrimes * 14bit format scoring 3571541Srgrimes * -------------------- 3581541Srgrimes * 3591541Srgrimes * 13 12 11 10 9 8 2 1 0 offset 3601541Srgrimes * +---+---+---+---+---+---+-------------+---+---+ 3611541Srgrimes * | X | X | X | X | X | X | X X X X X X | X | X | 3621541Srgrimes * +---+---+---+---+---+---+-------------+---+---+ 3631541Srgrimes * | | | | | | | | | 3641541Srgrimes * | | | | | | | | +--> signed? 3651541Srgrimes * | | | | | | | | 36612158Sbde * | | | | | | | +------> bigendian? 3671541Srgrimes * | | | | | | | 3681541Srgrimes * | | | | | | +---------------> total channels 3691541Srgrimes * | | | | | | 37012158Sbde * | | | | | +------------------------> AFMT_A_LAW 3711541Srgrimes * | | | | | 3721541Srgrimes * | | | | +----------------------------> AFMT_MU_LAW 3731541Srgrimes * | | | | 3741541Srgrimes * | | | +--------------------------------> AFMT_8BIT 3751541Srgrimes * | | | 3761541Srgrimes * | | +------------------------------------> AFMT_16BIT 3771541Srgrimes * | | 3781541Srgrimes * | +----------------------------------------> AFMT_24BIT 3791541Srgrimes * | 3801541Srgrimes * +--------------------------------------------> AFMT_32BIT 3811541Srgrimes */ 3821541Srgrimes#define score_signeq(s1, s2) (((s1) & 0x1) == ((s2) & 0x1)) 3831541Srgrimes#define score_endianeq(s1, s2) (((s1) & 0x2) == ((s2) & 0x2)) 3841541Srgrimes#define score_cheq(s1, s2) (((s1) & 0xfc) == ((s2) & 0xfc)) 3851541Srgrimes#define score_val(s1) ((s1) & 0x3f00) 3861541Srgrimes#define score_cse(s1) ((s1) & 0x7f) 3871541Srgrimes 3881541Srgrimesu_int32_t 3891541Srgrimeschn_fmtscore(u_int32_t fmt) 3901541Srgrimes{ 3911541Srgrimes u_int32_t ret; 3921541Srgrimes 3931541Srgrimes ret = 0; 3941541Srgrimes if (fmt & AFMT_SIGNED) 3951541Srgrimes ret |= 1 << 0; 3961541Srgrimes if (fmt & AFMT_BIGENDIAN) 3971541Srgrimes ret |= 1 << 1; 3981541Srgrimes if (fmt & AFMT_STEREO) 3991541Srgrimes ret |= (2 & 0x3f) << 2; 4001541Srgrimes else 4011541Srgrimes ret |= (1 & 0x3f) << 2; 4021541Srgrimes if (fmt & AFMT_A_LAW) 4031541Srgrimes ret |= 1 << 8; 4041541Srgrimes else if (fmt & AFMT_MU_LAW) 40518946Sbde ret |= 1 << 9; 4061541Srgrimes else if (fmt & AFMT_8BIT) 4071541Srgrimes ret |= 1 << 10; 4081541Srgrimes else if (fmt & AFMT_16BIT) 4091541Srgrimes ret |= 1 << 11; 4102811Sbde else if (fmt & AFMT_24BIT) 4111541Srgrimes ret |= 1 << 12; 4121541Srgrimes else if (fmt & AFMT_32BIT) 4131541Srgrimes ret |= 1 << 13; 41422521Sdyson 4151541Srgrimes return ret; 4161541Srgrimes} 4171541Srgrimes 4181541Srgrimesstatic u_int32_t 4191541Srgrimeschn_fmtbestfunc(u_int32_t fmt, u_int32_t *fmts, int cheq) 4201541Srgrimes{ 4211541Srgrimes u_int32_t best, score, score2, oldscore; 4221541Srgrimes int i; 42322521Sdyson 42422521Sdyson if (fmt == 0 || fmts == NULL || fmts[0] == 0) 4251541Srgrimes return 0; 4262811Sbde 4272811Sbde if (fmtvalid(fmt, fmts)) 4282811Sbde return fmt; 4292811Sbde 4302811Sbde best = 0; 4312811Sbde score = chn_fmtscore(fmt); 4323098Sphk oldscore = 0; 43322521Sdyson for (i = 0; fmts[i] != 0; i++) { 4341541Srgrimes score2 = chn_fmtscore(fmts[i]); 43512873Sbde if (cheq && !score_cheq(score, score2)) 43612873Sbde continue; 43722521Sdyson if (oldscore == 0 || 4381541Srgrimes (score_val(score2) == score_val(score)) || 4391541Srgrimes (score_val(score2) == score_val(oldscore)) || 4404465Sbde (score_val(score2) > score_val(oldscore) && 44112873Sbde score_val(score2) < score_val(score)) || 44222521Sdyson (score_val(score2) < score_val(oldscore) && 44322521Sdyson score_val(score2) > score_val(score)) || 4441541Srgrimes (score_val(oldscore) < score_val(score) && 4451541Srgrimes score_val(score2) > score_val(oldscore))) { 44612873Sbde if (score_val(oldscore) != score_val(score2) || 44712873Sbde score_cse(score) == score_cse(score2) || 44822521Sdyson ((score_cse(oldscore) != score_cse(score) && 44922521Sdyson !score_endianeq(score, oldscore) && 45022521Sdyson (score_endianeq(score, score2) || 4511541Srgrimes (!score_signeq(score, oldscore) && 4521541Srgrimes score_signeq(score, score2)))))) { 4531541Srgrimes best = fmts[i]; 45422521Sdyson oldscore = score2; 4551541Srgrimes } 4561541Srgrimes } 4571541Srgrimes } 4581541Srgrimes return best; 4591541Srgrimes} 46017761Sdyson 46117761Sdysonu_int32_t 46212873Sbdechn_fmtbestbit(u_int32_t fmt, u_int32_t *fmts) 46322521Sdyson{ 46422521Sdyson return chn_fmtbestfunc(fmt, fmts, 0); 46522521Sdyson} 46622521Sdyson 46722521Sdysonu_int32_t 46822521Sdysonchn_fmtbeststereo(u_int32_t fmt, u_int32_t *fmts) 4691541Srgrimes{ 4701541Srgrimes return chn_fmtbestfunc(fmt, fmts, 1); 4711541Srgrimes} 4722165Spaul 4732811Sbdeu_int32_t 474chn_fmtbest(u_int32_t fmt, u_int32_t *fmts) 475{ 476 u_int32_t best1, best2; 477 u_int32_t score, score1, score2; 478 479 if (fmtvalid(fmt, fmts)) 480 return fmt; 481 482 best1 = chn_fmtbeststereo(fmt, fmts); 483 best2 = chn_fmtbestbit(fmt, fmts); 484 485 if (best1 != 0 && best2 != 0 && best1 != best2) { 486 if (fmt & AFMT_STEREO) 487 return best1; 488 else { 489 score = score_val(chn_fmtscore(fmt)); 490 score1 = score_val(chn_fmtscore(best1)); 491 score2 = score_val(chn_fmtscore(best2)); 492 if (score1 == score2 || score1 == score) 493 return best1; 494 else if (score2 == score) 495 return best2; 496 else if (score1 > score2) 497 return best1; 498 return best2; 499 } 500 } else if (best2 == 0) 501 return best1; 502 else 503 return best2; 504} 505 506static struct pcm_feeder * 507feeder_fmtchain(u_int32_t *to, struct pcm_feeder *source, struct pcm_feeder *stop, int maxdepth) 508{ 509 struct feedertab_entry *fte, *ftebest; 510 struct pcm_feeder *try, *ret; 511 uint32_t fl, qout, qsrc, qdst; 512 int qtype; 513 514 if (to == NULL || to[0] == 0) 515 return NULL; 516 517 DEB(printf("trying %s (0x%08x -> 0x%08x)...\n", source->class->name, source->desc->in, source->desc->out)); 518 if (fmtvalid(source->desc->out, to)) { 519 DEB(printf("got it\n")); 520 return source; 521 } 522 523 if (maxdepth < 0) 524 return NULL; 525 526 /* 527 * WARNING: THIS IS _NOT_ FOR THE FAINT HEART 528 * Disclaimer: I don't expect anybody could understand this 529 * without deep logical and mathematical analysis 530 * involving various unnamed probability theorem. 531 * 532 * This "Best Fit Random Chain Selection" (BLEHBLEHWHATEVER) algorithm 533 * is **extremely** difficult to digest especially when applied to 534 * large sets / numbers of random chains (feeders), each with 535 * unique characteristic providing different sets of in/out format. 536 * 537 * Basically, our FEEDER_FMT (see feeder_fmt.c) chains characteristic: 538 * 1) Format chains 539 * 1.1 "8bit to any, not to 8bit" 540 * 1.1.1 sign can remain consistent, e.g: u8 -> u16[le|be] 541 * 1.1.2 sign can be changed, e.g: u8 -> s16[le|be] 542 * 1.1.3 endian can be changed, e.g: u8 -> u16[le|be] 543 * 1.1.4 both can be changed, e.g: u8 -> [u|s]16[le|be] 544 * 1.2 "Any to 8bit, not from 8bit" 545 * 1.2.1 sign can remain consistent, e.g: s16le -> s8 546 * 1.2.2 sign can be changed, e.g: s16le -> u8 547 * 1.2.3 source endian can be anything e.g: s16[le|be] -> s8 548 * 1.2.4 source endian / sign can be anything e.g: [u|s]16[le|be] -> u8 549 * 1.3 "Any to any where BOTH input and output either 8bit or non-8bit" 550 * 1.3.1 endian MUST remain consistent 551 * 1.3.2 sign CAN be changed 552 * 1.4 "Long jump" is allowed, e.g: from 16bit to 32bit, excluding 553 * 16bit to 24bit . 554 * 2) Channel chains (mono <-> stereo) 555 * 2.1 Both endian and sign MUST remain consistent 556 * 3) Endian chains (big endian <-> little endian) 557 * 3.1 Channels and sign MUST remain consistent 558 * 4) Sign chains (signed <-> unsigned) 559 * 4.1 Channels and endian MUST remain consistent 560 * 561 * .. and the mother of all chaining rules: 562 * 563 * Rules 0: Source and destination MUST not contain multiple selections. 564 * (qtype != FMT_Q_MULTI) 565 * 566 * First of all, our caller ( chn_fmtchain() ) will reduce the possible 567 * multiple from/to formats to a single best format using chn_fmtbest(). 568 * Then, using chn_fmtscore(), we determine the chaining characteristic. 569 * Our main goal is to narrow it down until it reach FMT_Q_EQ chaining 570 * type while still adhering above chaining rules. 571 * 572 * The need for this complicated chaining procedures is inevitable, 573 * since currently we have more than 200 different types of FEEDER_FMT 574 * doing various unique format conversion. Without this (the old way), 575 * it is possible to generate broken chain since it doesn't do any 576 * sanity checking to ensure that the output format is "properly aligned" 577 * with the direction of conversion (quality up/down/equal). 578 * 579 * Conversion: s24le to s32le 580 * Possible chain: 1) s24le -> s32le (correct, optimized) 581 * 2) s24le -> s16le -> s32le 582 * (since we have feeder_24to16 and feeder_16to32) 583 * +-- obviously broken! 584 * 585 * Using scoring mechanisme, this will ensure that the chaining 586 * process do the right thing, or at least, give the best chain 587 * possible without causing quality (the 'Q') degradation. 588 */ 589 590 qdst = chn_fmtscore(to[0]); 591 qsrc = chn_fmtscore(source->desc->out); 592 593#define score_q(s1) score_val(s1) 594#define score_8bit(s1) ((s1) & 0x700) 595#define score_non8bit(s1) (!score_8bit(s1)) 596#define score_across8bit(s1, s2) ((score_8bit(s1) && score_non8bit(s2)) || \ 597 (score_8bit(s2) && score_non8bit(s1))) 598 599#define FMT_CHAIN_Q_UP(s1, s2) (score_q(s1) < score_q(s2)) 600#define FMT_CHAIN_Q_DOWN(s1, s2) (score_q(s1) > score_q(s2)) 601#define FMT_CHAIN_Q_EQ(s1, s2) (score_q(s1) == score_q(s2)) 602#define FMT_Q_DOWN_FLAGS(s1, s2) (0x1 | (score_across8bit(s1, s2) ? \ 603 0x2 : 0x0)) 604#define FMT_Q_UP_FLAGS(s1, s2) FMT_Q_DOWN_FLAGS(s1, s2) 605#define FMT_Q_EQ_FLAGS(s1, s2) (0x3ffc | \ 606 ((score_cheq(s1, s2) && \ 607 score_endianeq(s1, s2)) ? \ 608 0x1 : 0x0) | \ 609 ((score_cheq(s1, s2) && \ 610 score_signeq(s1, s2)) ? \ 611 0x2 : 0x0)) 612 613 /* Determine chaining direction and set matching flag */ 614 fl = 0x3fff; 615 if (to[1] != 0) { 616 qtype = FMT_Q_MULTI; 617 printf("%s: WARNING: FMT_Q_MULTI chaining. Expect the unexpected.\n", __func__); 618 } else if (FMT_CHAIN_Q_DOWN(qsrc, qdst)) { 619 qtype = FMT_Q_DOWN; 620 fl = FMT_Q_DOWN_FLAGS(qsrc, qdst); 621 } else if (FMT_CHAIN_Q_UP(qsrc, qdst)) { 622 qtype = FMT_Q_UP; 623 fl = FMT_Q_UP_FLAGS(qsrc, qdst); 624 } else { 625 qtype = FMT_Q_EQ; 626 fl = FMT_Q_EQ_FLAGS(qsrc, qdst); 627 } 628 629 ftebest = NULL; 630 631 SLIST_FOREACH(fte, &feedertab, link) { 632 if (fte->desc == NULL) 633 continue; 634 if (fte->desc->type != FEEDER_FMT) 635 continue; 636 qout = chn_fmtscore(fte->desc->out); 637#define FMT_Q_MULTI_VALIDATE(qt) ((qt) == FMT_Q_MULTI) 638#define FMT_Q_FL_MATCH(qfl, s1, s2) (((s1) & (qfl)) == ((s2) & (qfl))) 639#define FMT_Q_UP_VALIDATE(qt, s1, s2, s3) ((qt) == FMT_Q_UP && \ 640 score_q(s3) >= score_q(s1) && \ 641 score_q(s3) <= score_q(s2)) 642#define FMT_Q_DOWN_VALIDATE(qt, s1, s2, s3) ((qt) == FMT_Q_DOWN && \ 643 score_q(s3) <= score_q(s1) && \ 644 score_q(s3) >= score_q(s2)) 645#define FMT_Q_EQ_VALIDATE(qt, s1, s2) ((qt) == FMT_Q_EQ && \ 646 score_q(s1) == score_q(s2)) 647 if (fte->desc->in == source->desc->out && 648 (FMT_Q_MULTI_VALIDATE(qtype) || 649 (FMT_Q_FL_MATCH(fl, qout, qdst) && 650 (FMT_Q_UP_VALIDATE(qtype, qsrc, qdst, qout) || 651 FMT_Q_DOWN_VALIDATE(qtype, qsrc, qdst, qout) || 652 FMT_Q_EQ_VALIDATE(qtype, qdst, qout))))) { 653 try = feeder_create(fte->feederclass, fte->desc); 654 if (try) { 655 try->source = source; 656 ret = chainok(try, stop) ? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL; 657 if (ret != NULL) 658 return ret; 659 feeder_destroy(try); 660 } 661 } else if (fte->desc->in == source->desc->out) { 662 /* XXX quality must be considered! */ 663 if (ftebest == NULL) 664 ftebest = fte; 665 } 666 } 667 668 if (ftebest != NULL) { 669 try = feeder_create(ftebest->feederclass, ftebest->desc); 670 if (try) { 671 try->source = source; 672 ret = chainok(try, stop) ? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL; 673 if (ret != NULL) 674 return ret; 675 feeder_destroy(try); 676 } 677 } 678 679 /* printf("giving up %s...\n", source->class->name); */ 680 681 return NULL; 682} 683 684u_int32_t 685chn_fmtchain(struct pcm_channel *c, u_int32_t *to) 686{ 687 struct pcm_feeder *try, *del, *stop; 688 u_int32_t tmpfrom[2], tmpto[2], best, *from; 689 int i, max, bestmax; 690 691 KASSERT(c != NULL, ("c == NULL")); 692 KASSERT(c->feeder != NULL, ("c->feeder == NULL")); 693 KASSERT(to != NULL, ("to == NULL")); 694 KASSERT(to[0] != 0, ("to[0] == 0")); 695 696 if (c == NULL || c->feeder == NULL || to == NULL || to[0] == 0) 697 return 0; 698 699 stop = c->feeder; 700 best = 0; 701 702 if (c->direction == PCMDIR_REC && c->feeder->desc->type == FEEDER_ROOT) { 703 from = chn_getcaps(c)->fmtlist; 704 if (from[1] != 0) { 705 best = chn_fmtbest(to[0], from); 706 if (best != 0) { 707 tmpfrom[0] = best; 708 tmpfrom[1] = 0; 709 from = tmpfrom; 710 } 711 } 712 } else { 713 tmpfrom[0] = c->feeder->desc->out; 714 tmpfrom[1] = 0; 715 from = tmpfrom; 716 if (to[1] != 0) { 717 best = chn_fmtbest(from[0], to); 718 if (best != 0) { 719 tmpto[0] = best; 720 tmpto[1] = 0; 721 to = tmpto; 722 } 723 } 724 } 725 726#define FEEDER_FMTCHAIN_MAXDEPTH 8 727 728 try = NULL; 729 730 if (to[0] != 0 && from[0] != 0 && 731 to[1] == 0 && from[1] == 0) { 732 max = 0; 733 best = from[0]; 734 c->feeder->desc->out = best; 735 do { 736 try = feeder_fmtchain(to, c->feeder, stop, max); 737 DEB(if (try != NULL) { 738 printf("%s: 0x%08x -> 0x%08x (maxdepth: %d)\n", 739 __func__, from[0], to[0], max); 740 }); 741 } while (try == NULL && max++ < FEEDER_FMTCHAIN_MAXDEPTH); 742 } else { 743 printf("%s: Using the old-way format chaining!\n", __func__); 744 i = 0; 745 best = 0; 746 bestmax = 100; 747 while (from[i] != 0) { 748 c->feeder->desc->out = from[i]; 749 try = NULL; 750 max = 0; 751 do { 752 try = feeder_fmtchain(to, c->feeder, stop, max); 753 } while (try == NULL && max++ < FEEDER_FMTCHAIN_MAXDEPTH); 754 if (try != NULL && max < bestmax) { 755 bestmax = max; 756 best = from[i]; 757 } 758 while (try != NULL && try != stop) { 759 del = try; 760 try = try->source; 761 feeder_destroy(del); 762 } 763 i++; 764 } 765 if (best == 0) 766 return 0; 767 768 c->feeder->desc->out = best; 769 try = feeder_fmtchain(to, c->feeder, stop, bestmax); 770 } 771 if (try == NULL) 772 return 0; 773 774 c->feeder = try; 775 c->align = 0; 776#ifdef FEEDER_DEBUG 777 printf("\n\nchain: "); 778#endif 779 while (try && (try != stop)) { 780#ifdef FEEDER_DEBUG 781 printf("%s [%d]", try->class->name, try->desc->idx); 782 if (try->source) 783 printf(" -> "); 784#endif 785 if (try->source) 786 try->source->parent = try; 787 if (try->align > 0) 788 c->align += try->align; 789 else if (try->align < 0 && c->align < -try->align) 790 c->align = -try->align; 791 try = try->source; 792 } 793#ifdef FEEDER_DEBUG 794 printf("%s [%d]\n", try->class->name, try->desc->idx); 795#endif 796 797 if (c->direction == PCMDIR_REC) { 798 try = c->feeder; 799 while (try != NULL) { 800 if (try->desc->type == FEEDER_ROOT) 801 return try->desc->out; 802 try = try->source; 803 } 804 return best; 805 } else 806 return c->feeder->desc->out; 807} 808 809void 810feeder_printchain(struct pcm_feeder *head) 811{ 812 struct pcm_feeder *f; 813 814 printf("feeder chain (head @%p)\n", head); 815 f = head; 816 while (f != NULL) { 817 printf("%s/%d @ %p\n", f->class->name, f->desc->idx, f); 818 f = f->source; 819 } 820 printf("[end]\n\n"); 821} 822 823/*****************************************************************************/ 824 825static int 826feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source) 827{ 828 struct snd_dbuf *src = source; 829 int l, offset; 830 831 KASSERT(count > 0, ("feed_root: count == 0")); 832 /* count &= ~((1 << ch->align) - 1); */ 833 KASSERT(count > 0, ("feed_root: aligned count == 0 (align = %d)", ch->align)); 834 835 if (++ch->feedcount == 0) 836 ch->feedcount = 2; 837 838 l = min(count, sndbuf_getready(src)); 839 840 /* When recording only return as much data as available */ 841 if (ch->direction == PCMDIR_REC) { 842 sndbuf_dispose(src, buffer, l); 843 return l; 844 } 845 846 847 offset = count - l; 848 849 if (offset > 0) { 850 if (snd_verbose > 3) 851 printf("%s: (%s) %spending %d bytes " 852 "(count=%d l=%d feed=%d)\n", 853 __func__, 854 (ch->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware", 855 (ch->feedcount == 1) ? "pre" : "ap", 856 offset, count, l, ch->feedcount); 857 858 if (ch->feedcount == 1) { 859 memset(buffer, 860 sndbuf_zerodata(sndbuf_getfmt(src)), 861 offset); 862 if (l > 0) 863 sndbuf_dispose(src, buffer + offset, l); 864 else 865 ch->feedcount--; 866 } else { 867 if (l > 0) 868 sndbuf_dispose(src, buffer, l); 869#if 1 870 memset(buffer + l, 871 sndbuf_zerodata(sndbuf_getfmt(src)), 872 offset); 873 if (!(ch->flags & CHN_F_CLOSING)) 874 ch->xruns++; 875#else 876 if (l < 1 || (ch->flags & CHN_F_CLOSING)) { 877 memset(buffer + l, 878 sndbuf_zerodata(sndbuf_getfmt(src)), 879 offset); 880 if (!(ch->flags & CHN_F_CLOSING)) 881 ch->xruns++; 882 } else { 883 int cp, tgt; 884 885 tgt = l; 886 while (offset > 0) { 887 cp = min(l, offset); 888 memcpy(buffer + tgt, buffer, cp); 889 offset -= cp; 890 tgt += cp; 891 } 892 ch->xruns++; 893 } 894#endif 895 } 896 } else if (l > 0) 897 sndbuf_dispose(src, buffer, l); 898 899 return count; 900} 901 902static kobj_method_t feeder_root_methods[] = { 903 KOBJMETHOD(feeder_feed, feed_root), 904 { 0, 0 } 905}; 906static struct feeder_class feeder_root_class = { 907 .name = "feeder_root", 908 .methods = feeder_root_methods, 909 .size = sizeof(struct pcm_feeder), 910 .align = 0, 911 .desc = NULL, 912 .data = NULL, 913}; 914SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class); 915SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL); 916