feeder.c revision 170289
17015SN/A/*- 217644Sjjg * Copyright (c) 1999 Cameron Grant <cg@freebsd.org> 37015SN/A * All rights reserved. 47015SN/A * 57015SN/A * Redistribution and use in source and binary forms, with or without 67015SN/A * modification, are permitted provided that the following conditions 77015SN/A * are met: 87015SN/A * 1. Redistributions of source code must retain the above copyright 97015SN/A * notice, this list of conditions and the following disclaimer. 107015SN/A * 2. Redistributions in binary form must reproduce the above copyright 117015SN/A * notice, this list of conditions and the following disclaimer in the 127015SN/A * documentation and/or other materials provided with the distribution. 137015SN/A * 147015SN/A * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 157015SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 167015SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 177015SN/A * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 187015SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 197015SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 207015SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 217015SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 227015SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 237015SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 247015SN/A * SUCH DAMAGE. 257015SN/A */ 267015SN/A 278051SN/A#include <dev/sound/pcm/sound.h> 288051SN/A 297015SN/A#include "feeder_if.h" 307015SN/A 318051SN/ASND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/feeder.c 170289 2007-06-04 18:25:08Z dwmalone $"); 328051SN/A 338051SN/AMALLOC_DEFINE(M_FEEDER, "feeder", "pcm feeder"); 348051SN/A 357015SN/A#define MAXFEEDERS 256 367015SN/A#undef FEEDER_DEBUG 378051SN/A 388051SN/Aint feeder_buffersize = FEEDBUFSZ; 398051SN/ATUNABLE_INT("hw.snd.feeder_buffersize", &feeder_buffersize); 408051SN/A 418051SN/A#ifdef SND_DEBUG 427015SN/Astatic int 438051SN/Asysctl_hw_snd_feeder_buffersize(SYSCTL_HANDLER_ARGS) 448051SN/A{ 458051SN/A int i, err, val; 468051SN/A 478051SN/A val = feeder_buffersize; 487015SN/A err = sysctl_handle_int(oidp, &val, 0, req); 497015SN/A 508051SN/A if (err != 0 || req->newptr == NULL) 518051SN/A return err; 528051SN/A 538051SN/A if (val < FEEDBUFSZ_MIN || val > FEEDBUFSZ_MAX) 548051SN/A return EINVAL; 558051SN/A 568051SN/A i = 0; 578051SN/A while (val >> i) 588051SN/A i++; 598051SN/A i = 1 << i; 608051SN/A if (i > val && (i >> 1) > 0 && (i >> 1) >= ((val * 3) >> 2)) 618051SN/A i >>= 1; 628051SN/A 638051SN/A feeder_buffersize = i; 648051SN/A 658051SN/A return err; 668051SN/A} 678060SN/ASYSCTL_PROC(_hw_snd, OID_AUTO, feeder_buffersize, CTLTYPE_INT | CTLFLAG_RW, 688051SN/A 0, sizeof(int), sysctl_hw_snd_feeder_buffersize, "I", 698051SN/A "feeder buffer size"); 708051SN/A#else 718051SN/ASYSCTL_INT(_hw_snd, OID_AUTO, feeder_buffersize, CTLFLAG_RD, 728051SN/A &feeder_buffersize, FEEDBUFSZ, "feeder buffer size"); 738051SN/A#endif 748051SN/A 758051SN/Astruct feedertab_entry { 768051SN/A SLIST_ENTRY(feedertab_entry) link; 778051SN/A struct feeder_class *feederclass; 788051SN/A struct pcm_feederdesc *desc; 798051SN/A 808051SN/A int idx; 818051SN/A}; 828051SN/Astatic SLIST_HEAD(, feedertab_entry) feedertab; 838051SN/A 848051SN/A/*****************************************************************************/ 858051SN/A 868051SN/Avoid 878051SN/Afeeder_register(void *p) 888051SN/A{ 898051SN/A static int feedercnt = 0; 908051SN/A 918051SN/A struct feeder_class *fc = p; 927015SN/A struct feedertab_entry *fte; 937015SN/A int i; 948051SN/A 958051SN/A if (feedercnt == 0) { 968051SN/A KASSERT(fc->desc == NULL, ("first feeder not root: %s", fc->name)); 9717127Sjjg 987015SN/A SLIST_INIT(&feedertab); 998060SN/A fte = malloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO); 1008060SN/A if (fte == NULL) { 1018060SN/A printf("can't allocate memory for root feeder: %s\n", 1028060SN/A fc->name); 1038060SN/A 1048060SN/A return; 1058060SN/A } 1067015SN/A fte->feederclass = fc; 1078051SN/A fte->desc = NULL; 1088051SN/A fte->idx = feedercnt; 1098051SN/A SLIST_INSERT_HEAD(&feedertab, fte, link); 1108051SN/A feedercnt++; 1118051SN/A 1128051SN/A /* initialize global variables */ 1138051SN/A 1147015SN/A if (snd_verbose < 0 || snd_verbose > 4) 1158051SN/A snd_verbose = 1; 1168051SN/A 1178051SN/A /* initialize unit numbering */ 1188051SN/A snd_unit_init(); 1198051SN/A if (snd_unit < 0 || snd_unit > PCMMAXUNIT) 1208051SN/A snd_unit = 0; 1218051SN/A 1228051SN/A if (snd_maxautovchans < 0 || 1238051SN/A snd_maxautovchans > SND_MAXVCHANS) 1248051SN/A snd_maxautovchans = 0; 1258051SN/A 1267015SN/A if (chn_latency < CHN_LATENCY_MIN || 1278051SN/A chn_latency > CHN_LATENCY_MAX) 1288051SN/A chn_latency = CHN_LATENCY_DEFAULT; 1298051SN/A 1308051SN/A if (chn_latency_profile < CHN_LATENCY_PROFILE_MIN || 1318051SN/A chn_latency_profile > CHN_LATENCY_PROFILE_MAX) 1328051SN/A chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT; 1338051SN/A 1348051SN/A if (feeder_buffersize < FEEDBUFSZ_MIN || 1358051SN/A feeder_buffersize > FEEDBUFSZ_MAX) 1367015SN/A feeder_buffersize = FEEDBUFSZ; 1378051SN/A 1388051SN/A if (feeder_rate_min < FEEDRATE_MIN || 1398051SN/A feeder_rate_max < FEEDRATE_MIN || 1408051SN/A feeder_rate_min > FEEDRATE_MAX || 1418051SN/A feeder_rate_max > FEEDRATE_MAX || 1428051SN/A !(feeder_rate_min < feeder_rate_max)) { 1438051SN/A feeder_rate_min = FEEDRATE_RATEMIN; 1447015SN/A feeder_rate_max = FEEDRATE_RATEMAX; 1458051SN/A } 1468051SN/A 1478051SN/A if (feeder_rate_round < FEEDRATE_ROUNDHZ_MIN || 1488051SN/A feeder_rate_round > FEEDRATE_ROUNDHZ_MAX) 1498051SN/A feeder_rate_round = FEEDRATE_ROUNDHZ; 1508051SN/A 1518051SN/A if (bootverbose) 1528051SN/A printf("%s: snd_unit=%d snd_maxautovchans=%d " 1537015SN/A "latency=%d feeder_buffersize=%d " 1548051SN/A "feeder_rate_min=%d feeder_rate_max=%d " 1558051SN/A "feeder_rate_round=%d\n", 1568051SN/A __func__, snd_unit, snd_maxautovchans, 1578051SN/A chn_latency, feeder_buffersize, 1588051SN/A feeder_rate_min, feeder_rate_max, 1598051SN/A feeder_rate_round); 1608051SN/A 1617015SN/A /* we've got our root feeder so don't veto pcm loading anymore */ 16217127Sjjg pcm_veto_load = 0; 1637015SN/A 1648051SN/A return; 1658051SN/A } 1668051SN/A 1678051SN/A KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name)); 1688051SN/A 1698051SN/A /* beyond this point failure is non-fatal but may result in some translations being unavailable */ 1707015SN/A i = 0; 1718051SN/A while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) { 1728051SN/A /* printf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */ 1738051SN/A fte = malloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO); 1748051SN/A if (fte == NULL) { 1758051SN/A printf("can't allocate memory for feeder '%s', %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); 1768051SN/A 1777015SN/A return; 1787015SN/A } 1798060SN/A fte->feederclass = fc; 1808060SN/A fte->desc = &fc->desc[i]; 1818060SN/A fte->idx = feedercnt; 1828060SN/A fte->desc->idx = feedercnt; 1837015SN/A SLIST_INSERT_HEAD(&feedertab, fte, link); 1847015SN/A i++; 1858051SN/A } 1868051SN/A feedercnt++; 18712503Ssmarks if (feedercnt >= MAXFEEDERS) 18812503Ssmarks printf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS); 18912503Ssmarks} 19012503Ssmarks 19112503Ssmarksstatic void 1928051SN/Afeeder_unregisterall(void *p) 19312503Ssmarks{ 19412503Ssmarks struct feedertab_entry *fte, *next; 19512503Ssmarks 1967015SN/A next = SLIST_FIRST(&feedertab); 1978051SN/A while (next != NULL) { 1988051SN/A fte = next; 1998051SN/A next = SLIST_NEXT(fte, link); 2007015SN/A free(fte, M_FEEDER); 2018051SN/A } 2028051SN/A} 2038051SN/A 2048051SN/Astatic int 2058051SN/Acmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m) 2068051SN/A{ 2078051SN/A return ((n->type == m->type) && 2087015SN/A ((n->in == 0) || (n->in == m->in)) && 20917127Sjjg ((n->out == 0) || (n->out == m->out)) && 2107015SN/A (n->flags == m->flags)); 2118051SN/A} 2128051SN/A 2138051SN/Astatic void 2148051SN/Afeeder_destroy(struct pcm_feeder *f) 2158051SN/A{ 2168051SN/A FEEDER_FREE(f); 2178051SN/A kobj_delete((kobj_t)f, M_FEEDER); 2188051SN/A} 2198051SN/A 2208051SN/Astatic struct pcm_feeder * 2218051SN/Afeeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc) 2228626SN/A{ 2238626SN/A struct pcm_feeder *f; 2247015SN/A int err; 2258626SN/A 2268626SN/A f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_NOWAIT | M_ZERO); 2278626SN/A if (f == NULL) 2288626SN/A return NULL; 2298051SN/A 2308051SN/A f->align = fc->align; 2318051SN/A f->data = fc->data; 2328051SN/A f->source = NULL; 2337015SN/A f->parent = NULL; 2348051SN/A f->class = fc; 2358051SN/A f->desc = &(f->desc_static); 2368051SN/A 2378051SN/A if (desc) { 2388051SN/A *(f->desc) = *desc; 2398051SN/A } else { 2408051SN/A f->desc->type = FEEDER_ROOT; 2418051SN/A f->desc->in = 0; 2428051SN/A f->desc->out = 0; 2438051SN/A f->desc->flags = 0; 2448051SN/A f->desc->idx = 0; 2458051SN/A } 2468051SN/A 2478051SN/A err = FEEDER_INIT(f); 2488051SN/A if (err) { 2498051SN/A printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err); 2508051SN/A feeder_destroy(f); 2518051SN/A 2528051SN/A return NULL; 2538051SN/A } 2548051SN/A 25517127Sjjg return f; 2568626SN/A} 2578626SN/A 2588626SN/Astruct feeder_class * 2598626SN/Afeeder_getclass(struct pcm_feederdesc *desc) 2608626SN/A{ 2618626SN/A struct feedertab_entry *fte; 2628626SN/A 2638626SN/A SLIST_FOREACH(fte, &feedertab, link) { 2648051SN/A if ((desc == NULL) && (fte->desc == NULL)) 2658626SN/A return fte->feederclass; 2668626SN/A if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc)) 2678051SN/A return fte->feederclass; 2687015SN/A } 2698626SN/A return NULL; 2708626SN/A} 2718626SN/A 2728626SN/Aint 2738626SN/Achn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc) 2748626SN/A{ 2758626SN/A struct pcm_feeder *nf; 2768626SN/A 2778626SN/A nf = feeder_create(fc, desc); 2788626SN/A if (nf == NULL) 2798626SN/A return ENOSPC; 2808626SN/A 2818626SN/A nf->source = c->feeder; 2828626SN/A 28317127Sjjg /* XXX we should use the lowest common denominator for align */ 2847015SN/A if (nf->align > 0) 2858051SN/A c->align += nf->align; 2868051SN/A else if (nf->align < 0 && c->align < -nf->align) 2878626SN/A c->align = -nf->align; 2888626SN/A if (c->feeder != NULL) 2898626SN/A c->feeder->parent = nf; 29014562Spsandoz c->feeder = nf; 29114562Spsandoz 29214562Spsandoz return 0; 29314562Spsandoz} 29414562Spsandoz 29514562Spsandozint 29614562Spsandozchn_removefeeder(struct pcm_channel *c) 29714562Spsandoz{ 29814562Spsandoz struct pcm_feeder *f; 29914562Spsandoz 30014562Spsandoz if (c->feeder == NULL) 30114562Spsandoz return -1; 30214562Spsandoz f = c->feeder; 30314562Spsandoz c->feeder = c->feeder->source; 30414562Spsandoz feeder_destroy(f); 30514562Spsandoz 3068626SN/A return 0; 3078626SN/A} 3088626SN/A 3098626SN/Astruct pcm_feeder * 31014562Spsandozchn_findfeeder(struct pcm_channel *c, u_int32_t type) 31114562Spsandoz{ 31214562Spsandoz struct pcm_feeder *f; 31314562Spsandoz 31414562Spsandoz f = c->feeder; 31514562Spsandoz while (f != NULL) { 31614562Spsandoz if (f->desc->type == type) 31714562Spsandoz return f; 31814562Spsandoz f = f->source; 3198626SN/A } 3208051SN/A 3218051SN/A return NULL; 3228051SN/A} 3238051SN/A 3248051SN/Astatic int 3258051SN/Achainok(struct pcm_feeder *test, struct pcm_feeder *stop) 3268051SN/A{ 3278051SN/A u_int32_t visited[MAXFEEDERS / 32]; 3288051SN/A u_int32_t idx, mask; 3298051SN/A 3308051SN/A bzero(visited, sizeof(visited)); 3318051SN/A while (test && (test != stop)) { 3328051SN/A idx = test->desc->idx; 3338051SN/A if (idx < 0) 3348051SN/A panic("bad idx %d", idx); 3358051SN/A if (idx >= MAXFEEDERS) 3368051SN/A panic("bad idx %d", idx); 3378051SN/A mask = 1 << (idx & 31); 3388051SN/A idx >>= 5; 3398051SN/A if (visited[idx] & mask) 3408051SN/A return 0; 3418051SN/A visited[idx] |= mask; 3428051SN/A test = test->source; 3438051SN/A } 3448051SN/A 3458051SN/A return 1; 3468051SN/A} 3478051SN/A 3488051SN/A/* 3498051SN/A * See feeder_fmtchain() for the mumbo-jumbo ridiculous explanation 3508051SN/A * of what the heck is this FMT_Q_* 3518051SN/A */ 35217127Sjjg#define FMT_Q_UP 1 3538051SN/A#define FMT_Q_DOWN 2 3548051SN/A#define FMT_Q_EQ 3 3558051SN/A#define FMT_Q_MULTI 4 3568051SN/A 3578051SN/A/* 3588051SN/A * 14bit format scoring 3598051SN/A * -------------------- 3608051SN/A * 3618051SN/A * 13 12 11 10 9 8 2 1 0 offset 3628051SN/A * +---+---+---+---+---+---+-------------+---+---+ 3638051SN/A * | X | X | X | X | X | X | X X X X X X | X | X | 3648051SN/A * +---+---+---+---+---+---+-------------+---+---+ 3658051SN/A * | | | | | | | | | 3668051SN/A * | | | | | | | | +--> signed? 3678051SN/A * | | | | | | | | 3688051SN/A * | | | | | | | +------> bigendian? 3698051SN/A * | | | | | | | 3708051SN/A * | | | | | | +---------------> total channels 3718051SN/A * | | | | | | 3728051SN/A * | | | | | +------------------------> AFMT_A_LAW 3738051SN/A * | | | | | 3748051SN/A * | | | | +----------------------------> AFMT_MU_LAW 3758051SN/A * | | | | 3768051SN/A * | | | +--------------------------------> AFMT_8BIT 3778051SN/A * | | | 3788051SN/A * | | +------------------------------------> AFMT_16BIT 3798051SN/A * | | 3808051SN/A * | +----------------------------------------> AFMT_24BIT 3818051SN/A * | 3828051SN/A * +--------------------------------------------> AFMT_32BIT 3838051SN/A */ 3848051SN/A#define score_signeq(s1, s2) (((s1) & 0x1) == ((s2) & 0x1)) 3858051SN/A#define score_endianeq(s1, s2) (((s1) & 0x2) == ((s2) & 0x2)) 3868051SN/A#define score_cheq(s1, s2) (((s1) & 0xfc) == ((s2) & 0xfc)) 3878051SN/A#define score_val(s1) ((s1) & 0x3f00) 3888051SN/A#define score_cse(s1) ((s1) & 0x7f) 3898051SN/A 3908051SN/Au_int32_t 39117127Sjjgchn_fmtscore(u_int32_t fmt) 3927015SN/A{ 3938051SN/A u_int32_t ret; 3948051SN/A 3958051SN/A ret = 0; 3968051SN/A if (fmt & AFMT_SIGNED) 3978051SN/A ret |= 1 << 0; 3988051SN/A if (fmt & AFMT_BIGENDIAN) 3998051SN/A ret |= 1 << 1; 4008051SN/A if (fmt & AFMT_STEREO) 4018051SN/A ret |= (2 & 0x3f) << 2; 4028051SN/A else 4037015SN/A ret |= (1 & 0x3f) << 2; 4048051SN/A if (fmt & AFMT_A_LAW) 4058051SN/A ret |= 1 << 8; 4067015SN/A else if (fmt & AFMT_MU_LAW) 4077015SN/A ret |= 1 << 9; 4087015SN/A else if (fmt & AFMT_8BIT) 4097015SN/A ret |= 1 << 10; 4107015SN/A else if (fmt & AFMT_16BIT) 4117015SN/A ret |= 1 << 11; 4128051SN/A else if (fmt & AFMT_24BIT) 4138051SN/A ret |= 1 << 12; 4148051SN/A else if (fmt & AFMT_32BIT) 4158051SN/A ret |= 1 << 13; 4168051SN/A 4178051SN/A return ret; 41817644Sjjg} 4198051SN/A 4208051SN/Astatic u_int32_t 4217015SN/Achn_fmtbestfunc(u_int32_t fmt, u_int32_t *fmts, int cheq) 4228051SN/A{ 4237015SN/A u_int32_t best, score, score2, oldscore; 4248051SN/A int i; 4257015SN/A 4268051SN/A if (fmt == 0 || fmts == NULL || fmts[0] == 0) 4277015SN/A return 0; 4287015SN/A 4298051SN/A if (fmtvalid(fmt, fmts)) 4308051SN/A return fmt; 4318051SN/A 4328051SN/A best = 0; 4338051SN/A score = chn_fmtscore(fmt); 4347015SN/A oldscore = 0; 4358051SN/A for (i = 0; fmts[i] != 0; i++) { 4368051SN/A score2 = chn_fmtscore(fmts[i]); 4378051SN/A if (cheq && !score_cheq(score, score2)) 4388051SN/A continue; 4398051SN/A if (oldscore == 0 || 4408051SN/A (score_val(score2) == score_val(score)) || 4418051SN/A (score_val(score2) == score_val(oldscore)) || 4428051SN/A (score_val(score2) > score_val(oldscore) && 4438051SN/A score_val(score2) < score_val(score)) || 4448051SN/A (score_val(score2) < score_val(oldscore) && 4458051SN/A score_val(score2) > score_val(score)) || 4467015SN/A (score_val(oldscore) < score_val(score) && 4478051SN/A score_val(score2) > score_val(oldscore))) { 4488051SN/A if (score_val(oldscore) != score_val(score2) || 4498051SN/A score_cse(score) == score_cse(score2) || 4508051SN/A ((score_cse(oldscore) != score_cse(score) && 4517015SN/A !score_endianeq(score, oldscore) && 4528051SN/A (score_endianeq(score, score2) || 4538051SN/A (!score_signeq(score, oldscore) && 4548051SN/A score_signeq(score, score2)))))) { 4557015SN/A best = fmts[i]; 4567015SN/A oldscore = score2; 4578051SN/A } 4588051SN/A } 4597015SN/A } 4607015SN/A return best; 4618051SN/A} 4627015SN/A 4637015SN/Au_int32_t 4648051SN/Achn_fmtbestbit(u_int32_t fmt, u_int32_t *fmts) 4658051SN/A{ 4668051SN/A return chn_fmtbestfunc(fmt, fmts, 0); 4678051SN/A} 4688051SN/A 4698051SN/Au_int32_t 4708051SN/Achn_fmtbeststereo(u_int32_t fmt, u_int32_t *fmts) 4718051SN/A{ 4727015SN/A return chn_fmtbestfunc(fmt, fmts, 1); 4738051SN/A} 4748051SN/A 4758051SN/Au_int32_t 4768051SN/Achn_fmtbest(u_int32_t fmt, u_int32_t *fmts) 4778051SN/A{ 4788051SN/A u_int32_t best1, best2; 4798051SN/A u_int32_t score, score1, score2; 4808051SN/A 4818051SN/A if (fmtvalid(fmt, fmts)) 4828051SN/A return fmt; 4838051SN/A 4848051SN/A best1 = chn_fmtbeststereo(fmt, fmts); 4857015SN/A best2 = chn_fmtbestbit(fmt, fmts); 4868051SN/A 4878051SN/A if (best1 != 0 && best2 != 0 && best1 != best2) { 48810249SN/A if (fmt & AFMT_STEREO) 4898051SN/A return best1; 4907015SN/A else { 4918051SN/A score = score_val(chn_fmtscore(fmt)); 4928051SN/A score1 = score_val(chn_fmtscore(best1)); 4938051SN/A score2 = score_val(chn_fmtscore(best2)); 4948051SN/A if (score1 == score2 || score1 == score) 4957015SN/A return best1; 49617127Sjjg else if (score2 == score) 4977015SN/A return best2; 4988051SN/A else if (score1 > score2) 4998051SN/A return best1; 5007015SN/A return best2; 5017015SN/A } 5028051SN/A } else if (best2 == 0) 5038051SN/A return best1; 5047015SN/A else 5057015SN/A return best2; 5067015SN/A} 5077015SN/A 5088051SN/Astatic struct pcm_feeder * 5098051SN/Afeeder_fmtchain(u_int32_t *to, struct pcm_feeder *source, struct pcm_feeder *stop, int maxdepth) 5108051SN/A{ 5118051SN/A struct feedertab_entry *fte, *ftebest; 5128051SN/A struct pcm_feeder *try, *ret; 5138051SN/A uint32_t fl, qout, qsrc, qdst; 5147015SN/A int qtype; 5157015SN/A 5168051SN/A if (to == NULL || to[0] == 0) 5178051SN/A return NULL; 5188051SN/A 5198051SN/A DEB(printf("trying %s (0x%08x -> 0x%08x)...\n", source->class->name, source->desc->in, source->desc->out)); 5208051SN/A if (fmtvalid(source->desc->out, to)) { 5218060SN/A DEB(printf("got it\n")); 5228051SN/A return source; 5238051SN/A } 5248051SN/A 5257015SN/A if (maxdepth < 0) 5267015SN/A return NULL; 5278060SN/A 5287015SN/A /* 5297015SN/A * WARNING: THIS IS _NOT_ FOR THE FAINT HEART 5307015SN/A * Disclaimer: I don't expect anybody could understand this 5318051SN/A * without deep logical and mathematical analysis 5328051SN/A * involving various unnamed probability theorem. 5338051SN/A * 5348051SN/A * This "Best Fit Random Chain Selection" (BLEHBLEHWHATEVER) algorithm 5358051SN/A * is **extremely** difficult to digest especially when applied to 5368051SN/A * large sets / numbers of random chains (feeders), each with 5378051SN/A * unique characteristic providing different sets of in/out format. 5387015SN/A * 5397015SN/A * Basically, our FEEDER_FMT (see feeder_fmt.c) chains characteristic: 5407015SN/A * 1) Format chains 5417015SN/A * 1.1 "8bit to any, not to 8bit" 5427015SN/A * 1.1.1 sign can remain consistent, e.g: u8 -> u16[le|be] 5437015SN/A * 1.1.2 sign can be changed, e.g: u8 -> s16[le|be] 5447015SN/A * 1.1.3 endian can be changed, e.g: u8 -> u16[le|be] 5457015SN/A * 1.1.4 both can be changed, e.g: u8 -> [u|s]16[le|be] 5467015SN/A * 1.2 "Any to 8bit, not from 8bit" 5477015SN/A * 1.2.1 sign can remain consistent, e.g: s16le -> s8 5487015SN/A * 1.2.2 sign can be changed, e.g: s16le -> u8 5497015SN/A * 1.2.3 source endian can be anything e.g: s16[le|be] -> s8 5508051SN/A * 1.2.4 source endian / sign can be anything e.g: [u|s]16[le|be] -> u8 5518051SN/A * 1.3 "Any to any where BOTH input and output either 8bit or non-8bit" 5527015SN/A * 1.3.1 endian MUST remain consistent 5538051SN/A * 1.3.2 sign CAN be changed 5548051SN/A * 1.4 "Long jump" is allowed, e.g: from 16bit to 32bit, excluding 5557015SN/A * 16bit to 24bit . 5568051SN/A * 2) Channel chains (mono <-> stereo) 5578051SN/A * 2.1 Both endian and sign MUST remain consistent 5588051SN/A * 3) Endian chains (big endian <-> little endian) 5598051SN/A * 3.1 Channels and sign MUST remain consistent 5608051SN/A * 4) Sign chains (signed <-> unsigned) 5618060SN/A * 4.1 Channels and endian MUST remain consistent 5628060SN/A * 5638060SN/A * .. and the mother of all chaining rules: 5648060SN/A * 5658051SN/A * Rules 0: Source and destination MUST not contain multiple selections. 5667015SN/A * (qtype != FMT_Q_MULTI) 5678051SN/A * 5688051SN/A * First of all, our caller ( chn_fmtchain() ) will reduce the possible 5697015SN/A * multiple from/to formats to a single best format using chn_fmtbest(). 5707015SN/A * Then, using chn_fmtscore(), we determine the chaining characteristic. 5718051SN/A * Our main goal is to narrow it down until it reach FMT_Q_EQ chaining 5728051SN/A * type while still adhering above chaining rules. 5738051SN/A * 5748051SN/A * The need for this complicated chaining procedures is inevitable, 5758051SN/A * since currently we have more than 200 different types of FEEDER_FMT 5768051SN/A * doing various unique format conversion. Without this (the old way), 5778051SN/A * it is possible to generate broken chain since it doesn't do any 5788051SN/A * sanity checking to ensure that the output format is "properly aligned" 5798051SN/A * with the direction of conversion (quality up/down/equal). 5808051SN/A * 5818060SN/A * Conversion: s24le to s32le 5828051SN/A * Possible chain: 1) s24le -> s32le (correct, optimized) 5838051SN/A * 2) s24le -> s16le -> s32le 5848051SN/A * (since we have feeder_24to16 and feeder_16to32) 5858051SN/A * +-- obviously broken! 5868051SN/A * 5878051SN/A * Using scoring mechanisme, this will ensure that the chaining 5888051SN/A * process do the right thing, or at least, give the best chain 5898051SN/A * possible without causing quality (the 'Q') degradation. 5908051SN/A */ 5918051SN/A 5928051SN/A qdst = chn_fmtscore(to[0]); 5938060SN/A qsrc = chn_fmtscore(source->desc->out); 5947015SN/A 5958051SN/A#define score_q(s1) score_val(s1) 5968060SN/A#define score_8bit(s1) ((s1) & 0x700) 5978060SN/A#define score_non8bit(s1) (!score_8bit(s1)) 5988060SN/A#define score_across8bit(s1, s2) ((score_8bit(s1) && score_non8bit(s2)) || \ 5998060SN/A (score_8bit(s2) && score_non8bit(s1))) 6008060SN/A 6018060SN/A#define FMT_CHAIN_Q_UP(s1, s2) (score_q(s1) < score_q(s2)) 6028051SN/A#define FMT_CHAIN_Q_DOWN(s1, s2) (score_q(s1) > score_q(s2)) 6038051SN/A#define FMT_CHAIN_Q_EQ(s1, s2) (score_q(s1) == score_q(s2)) 6048051SN/A#define FMT_Q_DOWN_FLAGS(s1, s2) (0x1 | (score_across8bit(s1, s2) ? \ 6058051SN/A 0x2 : 0x0)) 6068051SN/A#define FMT_Q_UP_FLAGS(s1, s2) FMT_Q_DOWN_FLAGS(s1, s2) 6078051SN/A#define FMT_Q_EQ_FLAGS(s1, s2) (0x3ffc | \ 6088051SN/A ((score_cheq(s1, s2) && \ 6098051SN/A score_endianeq(s1, s2)) ? \ 6108051SN/A 0x1 : 0x0) | \ 6118051SN/A ((score_cheq(s1, s2) && \ 6128051SN/A score_signeq(s1, s2)) ? \ 6138051SN/A 0x2 : 0x0)) 6148051SN/A 6158051SN/A /* Determine chaining direction and set matching flag */ 6168051SN/A fl = 0x3fff; 6178060SN/A if (to[1] != 0) { 6188051SN/A qtype = FMT_Q_MULTI; 6198051SN/A printf("%s: WARNING: FMT_Q_MULTI chaining. Expect the unexpected.\n", __func__); 6208051SN/A } else if (FMT_CHAIN_Q_DOWN(qsrc, qdst)) { 6218051SN/A qtype = FMT_Q_DOWN; 6228051SN/A fl = FMT_Q_DOWN_FLAGS(qsrc, qdst); 62317127Sjjg } else if (FMT_CHAIN_Q_UP(qsrc, qdst)) { 6248051SN/A qtype = FMT_Q_UP; 6258051SN/A fl = FMT_Q_UP_FLAGS(qsrc, qdst); 6268051SN/A } else { 6277015SN/A qtype = FMT_Q_EQ; 6287015SN/A fl = FMT_Q_EQ_FLAGS(qsrc, qdst); 6297015SN/A } 6307015SN/A 6317015SN/A ftebest = NULL; 6327015SN/A 6338060SN/A SLIST_FOREACH(fte, &feedertab, link) { 6348060SN/A if (fte->desc == NULL) 6357015SN/A continue; 6367015SN/A if (fte->desc->type != FEEDER_FMT) 6377015SN/A continue; 6388051SN/A qout = chn_fmtscore(fte->desc->out); 6398051SN/A#define FMT_Q_MULTI_VALIDATE(qt) ((qt) == FMT_Q_MULTI) 6408051SN/A#define FMT_Q_FL_MATCH(qfl, s1, s2) (((s1) & (qfl)) == ((s2) & (qfl))) 6418051SN/A#define FMT_Q_UP_VALIDATE(qt, s1, s2, s3) ((qt) == FMT_Q_UP && \ 6428060SN/A score_q(s3) >= score_q(s1) && \ 6438060SN/A score_q(s3) <= score_q(s2)) 6447015SN/A#define FMT_Q_DOWN_VALIDATE(qt, s1, s2, s3) ((qt) == FMT_Q_DOWN && \ 6458051SN/A score_q(s3) <= score_q(s1) && \ 6468051SN/A score_q(s3) >= score_q(s2)) 6478051SN/A#define FMT_Q_EQ_VALIDATE(qt, s1, s2) ((qt) == FMT_Q_EQ && \ 6488051SN/A score_q(s1) == score_q(s2)) 6498051SN/A if (fte->desc->in == source->desc->out && 6508051SN/A (FMT_Q_MULTI_VALIDATE(qtype) || 6518051SN/A (FMT_Q_FL_MATCH(fl, qout, qdst) && 6527015SN/A (FMT_Q_UP_VALIDATE(qtype, qsrc, qdst, qout) || 6537015SN/A FMT_Q_DOWN_VALIDATE(qtype, qsrc, qdst, qout) || 6547015SN/A FMT_Q_EQ_VALIDATE(qtype, qdst, qout))))) { 6557015SN/A try = feeder_create(fte->feederclass, fte->desc); 6567015SN/A if (try) { 6577015SN/A try->source = source; 6587015SN/A ret = chainok(try, stop) ? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL; 6597015SN/A if (ret != NULL) 6607015SN/A return ret; 6618051SN/A feeder_destroy(try); 6628051SN/A } 6637015SN/A } else if (fte->desc->in == source->desc->out) { 6647015SN/A /* XXX quality must be considered! */ 6657015SN/A if (ftebest == NULL) 6667015SN/A ftebest = fte; 6677015SN/A } 6687015SN/A } 6698051SN/A 6708051SN/A if (ftebest != NULL) { 6717015SN/A try = feeder_create(ftebest->feederclass, ftebest->desc); 6728060SN/A if (try) { 6738060SN/A try->source = source; 6748060SN/A ret = chainok(try, stop) ? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL; 6758060SN/A if (ret != NULL) 6768060SN/A return ret; 6777015SN/A feeder_destroy(try); 67817127Sjjg } 6797015SN/A } 6808051SN/A 6818051SN/A /* printf("giving up %s...\n", source->class->name); */ 6827015SN/A 6837015SN/A return NULL; 6847015SN/A} 6858051SN/A 6868051SN/Au_int32_t 6877015SN/Achn_fmtchain(struct pcm_channel *c, u_int32_t *to) 6887015SN/A{ 6897015SN/A struct pcm_feeder *try, *del, *stop; 6908051SN/A u_int32_t tmpfrom[2], tmpto[2], best, *from; 6918051SN/A int i, max, bestmax; 6927015SN/A 6938060SN/A KASSERT(c != NULL, ("c == NULL")); 6948060SN/A KASSERT(c->feeder != NULL, ("c->feeder == NULL")); 6958051SN/A KASSERT(to != NULL, ("to == NULL")); 69617127Sjjg KASSERT(to[0] != 0, ("to[0] == 0")); 6977015SN/A 6988051SN/A if (c == NULL || c->feeder == NULL || to == NULL || to[0] == 0) 6998051SN/A return 0; 7008051SN/A 7018051SN/A stop = c->feeder; 7028060SN/A best = 0; 7038060SN/A 7048060SN/A if (c->direction == PCMDIR_REC && c->feeder->desc->type == FEEDER_ROOT) { 7058060SN/A from = chn_getcaps(c)->fmtlist; 7068060SN/A if (from[1] != 0) { 7078060SN/A best = chn_fmtbest(to[0], from); 7088060SN/A if (best != 0) { 7098060SN/A tmpfrom[0] = best; 7107015SN/A tmpfrom[1] = 0; 7118060SN/A from = tmpfrom; 7128060SN/A } 7138060SN/A } 7148060SN/A } else { 7158060SN/A tmpfrom[0] = c->feeder->desc->out; 7168060SN/A tmpfrom[1] = 0; 7178051SN/A from = tmpfrom; 7188060SN/A if (to[1] != 0) { 7198060SN/A best = chn_fmtbest(from[0], to); 7208060SN/A if (best != 0) { 7217015SN/A tmpto[0] = best; 7228060SN/A tmpto[1] = 0; 7238060SN/A to = tmpto; 7248051SN/A } 7258051SN/A } 7268051SN/A } 7277015SN/A 7288060SN/A#define FEEDER_FMTCHAIN_MAXDEPTH 8 7298060SN/A 7308060SN/A try = NULL; 7318060SN/A 7328060SN/A if (to[0] != 0 && from[0] != 0 && 7338060SN/A to[1] == 0 && from[1] == 0) { 7348060SN/A max = 0; 7358060SN/A best = from[0]; 7368060SN/A c->feeder->desc->out = best; 7378060SN/A do { 7388051SN/A try = feeder_fmtchain(to, c->feeder, stop, max); 7398051SN/A DEB(if (try != NULL) { 7407015SN/A printf("%s: 0x%08x -> 0x%08x (maxdepth: %d)\n", 7417015SN/A __func__, from[0], to[0], max); 7428060SN/A }); 7438060SN/A } while (try == NULL && max++ < FEEDER_FMTCHAIN_MAXDEPTH); 7448060SN/A } else { 7458060SN/A printf("%s: Using the old-way format chaining!\n", __func__); 7468060SN/A i = 0; 7478060SN/A best = 0; 7488060SN/A bestmax = 100; 7499085SN/A while (from[i] != 0) { 7508051SN/A c->feeder->desc->out = from[i]; 7517015SN/A try = NULL; 7528051SN/A max = 0; 7537015SN/A do { 7548051SN/A try = feeder_fmtchain(to, c->feeder, stop, max); 7557015SN/A } while (try == NULL && max++ < FEEDER_FMTCHAIN_MAXDEPTH); 7568051SN/A if (try != NULL && max < bestmax) { 7578051SN/A bestmax = max; 758 best = from[i]; 759 } 760 while (try != NULL && try != stop) { 761 del = try; 762 try = try->source; 763 feeder_destroy(del); 764 } 765 i++; 766 } 767 if (best == 0) 768 return 0; 769 770 c->feeder->desc->out = best; 771 try = feeder_fmtchain(to, c->feeder, stop, bestmax); 772 } 773 if (try == NULL) 774 return 0; 775 776 c->feeder = try; 777 c->align = 0; 778#ifdef FEEDER_DEBUG 779 printf("\n\nchain: "); 780#endif 781 while (try && (try != stop)) { 782#ifdef FEEDER_DEBUG 783 printf("%s [%d]", try->class->name, try->desc->idx); 784 if (try->source) 785 printf(" -> "); 786#endif 787 if (try->source) 788 try->source->parent = try; 789 if (try->align > 0) 790 c->align += try->align; 791 else if (try->align < 0 && c->align < -try->align) 792 c->align = -try->align; 793 try = try->source; 794 } 795#ifdef FEEDER_DEBUG 796 printf("%s [%d]\n", try->class->name, try->desc->idx); 797#endif 798 799 if (c->direction == PCMDIR_REC) { 800 try = c->feeder; 801 while (try != NULL) { 802 if (try->desc->type == FEEDER_ROOT) 803 return try->desc->out; 804 try = try->source; 805 } 806 return best; 807 } else 808 return c->feeder->desc->out; 809} 810 811void 812feeder_printchain(struct pcm_feeder *head) 813{ 814 struct pcm_feeder *f; 815 816 printf("feeder chain (head @%p)\n", head); 817 f = head; 818 while (f != NULL) { 819 printf("%s/%d @ %p\n", f->class->name, f->desc->idx, f); 820 f = f->source; 821 } 822 printf("[end]\n\n"); 823} 824 825/*****************************************************************************/ 826 827static int 828feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source) 829{ 830 struct snd_dbuf *src = source; 831 int l, offset; 832 833 KASSERT(count > 0, ("feed_root: count == 0")); 834 /* count &= ~((1 << ch->align) - 1); */ 835 KASSERT(count > 0, ("feed_root: aligned count == 0 (align = %d)", ch->align)); 836 837 if (++ch->feedcount == 0) 838 ch->feedcount = 2; 839 840 l = min(count, sndbuf_getready(src)); 841 842 /* When recording only return as much data as available */ 843 if (ch->direction == PCMDIR_REC) { 844 sndbuf_dispose(src, buffer, l); 845 return l; 846 } 847 848 849 offset = count - l; 850 851 if (offset > 0) { 852 if (snd_verbose > 3) 853 printf("%s: (%s) %spending %d bytes " 854 "(count=%d l=%d feed=%d)\n", 855 __func__, 856 (ch->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware", 857 (ch->feedcount == 1) ? "pre" : "ap", 858 offset, count, l, ch->feedcount); 859 860 if (ch->feedcount == 1) { 861 memset(buffer, 862 sndbuf_zerodata(sndbuf_getfmt(src)), 863 offset); 864 if (l > 0) 865 sndbuf_dispose(src, buffer + offset, l); 866 else 867 ch->feedcount--; 868 } else { 869 if (l > 0) 870 sndbuf_dispose(src, buffer, l); 871#if 1 872 memset(buffer + l, 873 sndbuf_zerodata(sndbuf_getfmt(src)), 874 offset); 875 if (!(ch->flags & CHN_F_CLOSING)) 876 ch->xruns++; 877#else 878 if (l < 1 || (ch->flags & CHN_F_CLOSING)) { 879 memset(buffer + l, 880 sndbuf_zerodata(sndbuf_getfmt(src)), 881 offset); 882 if (!(ch->flags & CHN_F_CLOSING)) 883 ch->xruns++; 884 } else { 885 int cp, tgt; 886 887 tgt = l; 888 while (offset > 0) { 889 cp = min(l, offset); 890 memcpy(buffer + tgt, buffer, cp); 891 offset -= cp; 892 tgt += cp; 893 } 894 ch->xruns++; 895 } 896#endif 897 } 898 } else if (l > 0) 899 sndbuf_dispose(src, buffer, l); 900 901 return count; 902} 903 904static kobj_method_t feeder_root_methods[] = { 905 KOBJMETHOD(feeder_feed, feed_root), 906 { 0, 0 } 907}; 908static struct feeder_class feeder_root_class = { 909 .name = "feeder_root", 910 .methods = feeder_root_methods, 911 .size = sizeof(struct pcm_feeder), 912 .align = 0, 913 .desc = NULL, 914 .data = NULL, 915}; 916SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class); 917SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL); 918