118 snd_unit = 0; 119 120 if (snd_maxautovchans < 0 || 121 snd_maxautovchans > SND_MAXVCHANS) 122 snd_maxautovchans = 0; 123 124 if (chn_latency < CHN_LATENCY_MIN || 125 chn_latency > CHN_LATENCY_MAX) 126 chn_latency = CHN_LATENCY_DEFAULT; 127 128 if (chn_latency_profile < CHN_LATENCY_PROFILE_MIN || 129 chn_latency_profile > CHN_LATENCY_PROFILE_MAX) 130 chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT; 131 132 if (feeder_buffersize < FEEDBUFSZ_MIN || 133 feeder_buffersize > FEEDBUFSZ_MAX) 134 feeder_buffersize = FEEDBUFSZ; 135 136 if (feeder_rate_min < FEEDRATE_MIN || 137 feeder_rate_max < FEEDRATE_MIN || 138 feeder_rate_min > FEEDRATE_MAX || 139 feeder_rate_max > FEEDRATE_MAX || 140 !(feeder_rate_min < feeder_rate_max)) { 141 feeder_rate_min = FEEDRATE_RATEMIN; 142 feeder_rate_max = FEEDRATE_RATEMAX; 143 } 144 145 if (feeder_rate_round < FEEDRATE_ROUNDHZ_MIN || 146 feeder_rate_round > FEEDRATE_ROUNDHZ_MAX) 147 feeder_rate_round = FEEDRATE_ROUNDHZ; 148 149 if (bootverbose) 150 printf("%s: snd_unit=%d snd_maxautovchans=%d " 151 "latency=%d feeder_buffersize=%d " 152 "feeder_rate_min=%d feeder_rate_max=%d " 153 "feeder_rate_round=%d\n", 154 __func__, snd_unit, snd_maxautovchans, 155 chn_latency, feeder_buffersize, 156 feeder_rate_min, feeder_rate_max, 157 feeder_rate_round); 158 159 /* we've got our root feeder so don't veto pcm loading anymore */ 160 pcm_veto_load = 0; 161 162 return; 163 } 164 165 KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name)); 166 167 /* beyond this point failure is non-fatal but may result in some translations being unavailable */ 168 i = 0; 169 while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) { 170 /* printf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */ 171 fte = malloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO); 172 if (fte == NULL) { 173 printf("can't allocate memory for feeder '%s', %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); 174 175 return; 176 } 177 fte->feederclass = fc; 178 fte->desc = &fc->desc[i]; 179 fte->idx = feedercnt; 180 fte->desc->idx = feedercnt; 181 SLIST_INSERT_HEAD(&feedertab, fte, link); 182 i++; 183 } 184 feedercnt++; 185 if (feedercnt >= MAXFEEDERS) 186 printf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS); 187} 188 189static void 190feeder_unregisterall(void *p) 191{ 192 struct feedertab_entry *fte, *next; 193 194 next = SLIST_FIRST(&feedertab); 195 while (next != NULL) { 196 fte = next; 197 next = SLIST_NEXT(fte, link); 198 free(fte, M_FEEDER); 199 } 200} 201 202static int 203cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m) 204{ 205 return ((n->type == m->type) && 206 ((n->in == 0) || (n->in == m->in)) && 207 ((n->out == 0) || (n->out == m->out)) && 208 (n->flags == m->flags)); 209} 210 211static void 212feeder_destroy(struct pcm_feeder *f) 213{ 214 FEEDER_FREE(f); 215 kobj_delete((kobj_t)f, M_FEEDER); 216} 217 218static struct pcm_feeder * 219feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc) 220{ 221 struct pcm_feeder *f; 222 int err; 223 224 f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_NOWAIT | M_ZERO); 225 if (f == NULL) 226 return NULL; 227 228 f->align = fc->align; 229 f->data = fc->data; 230 f->source = NULL; 231 f->parent = NULL; 232 f->class = fc; 233 f->desc = &(f->desc_static); 234 235 if (desc) { 236 *(f->desc) = *desc; 237 } else { 238 f->desc->type = FEEDER_ROOT; 239 f->desc->in = 0; 240 f->desc->out = 0; 241 f->desc->flags = 0; 242 f->desc->idx = 0; 243 } 244 245 err = FEEDER_INIT(f); 246 if (err) { 247 printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err); 248 feeder_destroy(f); 249 250 return NULL; 251 } 252 253 return f; 254} 255 256struct feeder_class * 257feeder_getclass(struct pcm_feederdesc *desc) 258{ 259 struct feedertab_entry *fte; 260 261 SLIST_FOREACH(fte, &feedertab, link) { 262 if ((desc == NULL) && (fte->desc == NULL)) 263 return fte->feederclass; 264 if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc)) 265 return fte->feederclass; 266 } 267 return NULL; 268} 269 270int 271chn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc) 272{ 273 struct pcm_feeder *nf; 274 275 nf = feeder_create(fc, desc); 276 if (nf == NULL) 277 return ENOSPC; 278 279 nf->source = c->feeder; 280 281 /* XXX we should use the lowest common denominator for align */ 282 if (nf->align > 0) 283 c->align += nf->align; 284 else if (nf->align < 0 && c->align < -nf->align) 285 c->align = -nf->align; 286 if (c->feeder != NULL) 287 c->feeder->parent = nf; 288 c->feeder = nf; 289 290 return 0; 291} 292 293int 294chn_removefeeder(struct pcm_channel *c) 295{ 296 struct pcm_feeder *f; 297 298 if (c->feeder == NULL) 299 return -1; 300 f = c->feeder; 301 c->feeder = c->feeder->source; 302 feeder_destroy(f); 303 304 return 0; 305} 306 307struct pcm_feeder * 308chn_findfeeder(struct pcm_channel *c, u_int32_t type) 309{ 310 struct pcm_feeder *f; 311 312 f = c->feeder; 313 while (f != NULL) { 314 if (f->desc->type == type) 315 return f; 316 f = f->source; 317 } 318 319 return NULL; 320} 321 322static int 323chainok(struct pcm_feeder *test, struct pcm_feeder *stop) 324{ 325 u_int32_t visited[MAXFEEDERS / 32]; 326 u_int32_t idx, mask; 327 328 bzero(visited, sizeof(visited)); 329 while (test && (test != stop)) { 330 idx = test->desc->idx; 331 if (idx < 0) 332 panic("bad idx %d", idx); 333 if (idx >= MAXFEEDERS) 334 panic("bad idx %d", idx); 335 mask = 1 << (idx & 31); 336 idx >>= 5; 337 if (visited[idx] & mask) 338 return 0; 339 visited[idx] |= mask; 340 test = test->source; 341 } 342 343 return 1; 344} 345 346/*
| 120 snd_unit = 0; 121 122 if (snd_maxautovchans < 0 || 123 snd_maxautovchans > SND_MAXVCHANS) 124 snd_maxautovchans = 0; 125 126 if (chn_latency < CHN_LATENCY_MIN || 127 chn_latency > CHN_LATENCY_MAX) 128 chn_latency = CHN_LATENCY_DEFAULT; 129 130 if (chn_latency_profile < CHN_LATENCY_PROFILE_MIN || 131 chn_latency_profile > CHN_LATENCY_PROFILE_MAX) 132 chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT; 133 134 if (feeder_buffersize < FEEDBUFSZ_MIN || 135 feeder_buffersize > FEEDBUFSZ_MAX) 136 feeder_buffersize = FEEDBUFSZ; 137 138 if (feeder_rate_min < FEEDRATE_MIN || 139 feeder_rate_max < FEEDRATE_MIN || 140 feeder_rate_min > FEEDRATE_MAX || 141 feeder_rate_max > FEEDRATE_MAX || 142 !(feeder_rate_min < feeder_rate_max)) { 143 feeder_rate_min = FEEDRATE_RATEMIN; 144 feeder_rate_max = FEEDRATE_RATEMAX; 145 } 146 147 if (feeder_rate_round < FEEDRATE_ROUNDHZ_MIN || 148 feeder_rate_round > FEEDRATE_ROUNDHZ_MAX) 149 feeder_rate_round = FEEDRATE_ROUNDHZ; 150 151 if (bootverbose) 152 printf("%s: snd_unit=%d snd_maxautovchans=%d " 153 "latency=%d feeder_buffersize=%d " 154 "feeder_rate_min=%d feeder_rate_max=%d " 155 "feeder_rate_round=%d\n", 156 __func__, snd_unit, snd_maxautovchans, 157 chn_latency, feeder_buffersize, 158 feeder_rate_min, feeder_rate_max, 159 feeder_rate_round); 160 161 /* we've got our root feeder so don't veto pcm loading anymore */ 162 pcm_veto_load = 0; 163 164 return; 165 } 166 167 KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name)); 168 169 /* beyond this point failure is non-fatal but may result in some translations being unavailable */ 170 i = 0; 171 while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) { 172 /* printf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */ 173 fte = malloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO); 174 if (fte == NULL) { 175 printf("can't allocate memory for feeder '%s', %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); 176 177 return; 178 } 179 fte->feederclass = fc; 180 fte->desc = &fc->desc[i]; 181 fte->idx = feedercnt; 182 fte->desc->idx = feedercnt; 183 SLIST_INSERT_HEAD(&feedertab, fte, link); 184 i++; 185 } 186 feedercnt++; 187 if (feedercnt >= MAXFEEDERS) 188 printf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS); 189} 190 191static void 192feeder_unregisterall(void *p) 193{ 194 struct feedertab_entry *fte, *next; 195 196 next = SLIST_FIRST(&feedertab); 197 while (next != NULL) { 198 fte = next; 199 next = SLIST_NEXT(fte, link); 200 free(fte, M_FEEDER); 201 } 202} 203 204static int 205cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m) 206{ 207 return ((n->type == m->type) && 208 ((n->in == 0) || (n->in == m->in)) && 209 ((n->out == 0) || (n->out == m->out)) && 210 (n->flags == m->flags)); 211} 212 213static void 214feeder_destroy(struct pcm_feeder *f) 215{ 216 FEEDER_FREE(f); 217 kobj_delete((kobj_t)f, M_FEEDER); 218} 219 220static struct pcm_feeder * 221feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc) 222{ 223 struct pcm_feeder *f; 224 int err; 225 226 f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_NOWAIT | M_ZERO); 227 if (f == NULL) 228 return NULL; 229 230 f->align = fc->align; 231 f->data = fc->data; 232 f->source = NULL; 233 f->parent = NULL; 234 f->class = fc; 235 f->desc = &(f->desc_static); 236 237 if (desc) { 238 *(f->desc) = *desc; 239 } else { 240 f->desc->type = FEEDER_ROOT; 241 f->desc->in = 0; 242 f->desc->out = 0; 243 f->desc->flags = 0; 244 f->desc->idx = 0; 245 } 246 247 err = FEEDER_INIT(f); 248 if (err) { 249 printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err); 250 feeder_destroy(f); 251 252 return NULL; 253 } 254 255 return f; 256} 257 258struct feeder_class * 259feeder_getclass(struct pcm_feederdesc *desc) 260{ 261 struct feedertab_entry *fte; 262 263 SLIST_FOREACH(fte, &feedertab, link) { 264 if ((desc == NULL) && (fte->desc == NULL)) 265 return fte->feederclass; 266 if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc)) 267 return fte->feederclass; 268 } 269 return NULL; 270} 271 272int 273chn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc) 274{ 275 struct pcm_feeder *nf; 276 277 nf = feeder_create(fc, desc); 278 if (nf == NULL) 279 return ENOSPC; 280 281 nf->source = c->feeder; 282 283 /* XXX we should use the lowest common denominator for align */ 284 if (nf->align > 0) 285 c->align += nf->align; 286 else if (nf->align < 0 && c->align < -nf->align) 287 c->align = -nf->align; 288 if (c->feeder != NULL) 289 c->feeder->parent = nf; 290 c->feeder = nf; 291 292 return 0; 293} 294 295int 296chn_removefeeder(struct pcm_channel *c) 297{ 298 struct pcm_feeder *f; 299 300 if (c->feeder == NULL) 301 return -1; 302 f = c->feeder; 303 c->feeder = c->feeder->source; 304 feeder_destroy(f); 305 306 return 0; 307} 308 309struct pcm_feeder * 310chn_findfeeder(struct pcm_channel *c, u_int32_t type) 311{ 312 struct pcm_feeder *f; 313 314 f = c->feeder; 315 while (f != NULL) { 316 if (f->desc->type == type) 317 return f; 318 f = f->source; 319 } 320 321 return NULL; 322} 323 324static int 325chainok(struct pcm_feeder *test, struct pcm_feeder *stop) 326{ 327 u_int32_t visited[MAXFEEDERS / 32]; 328 u_int32_t idx, mask; 329 330 bzero(visited, sizeof(visited)); 331 while (test && (test != stop)) { 332 idx = test->desc->idx; 333 if (idx < 0) 334 panic("bad idx %d", idx); 335 if (idx >= MAXFEEDERS) 336 panic("bad idx %d", idx); 337 mask = 1 << (idx & 31); 338 idx >>= 5; 339 if (visited[idx] & mask) 340 return 0; 341 visited[idx] |= mask; 342 test = test->source; 343 } 344 345 return 1; 346} 347 348/*
|
348 * of what the heck is this FMT_Q_* 349 */ 350#define FMT_Q_UP 1 351#define FMT_Q_DOWN 2 352#define FMT_Q_EQ 3 353#define FMT_Q_MULTI 4 354 355/* 356 * 14bit format scoring 357 * -------------------- 358 * 359 * 13 12 11 10 9 8 2 1 0 offset 360 * +---+---+---+---+---+---+-------------+---+---+ 361 * | X | X | X | X | X | X | X X X X X X | X | X | 362 * +---+---+---+---+---+---+-------------+---+---+ 363 * | | | | | | | | | 364 * | | | | | | | | +--> signed? 365 * | | | | | | | | 366 * | | | | | | | +------> bigendian? 367 * | | | | | | | 368 * | | | | | | +---------------> total channels 369 * | | | | | | 370 * | | | | | +------------------------> AFMT_A_LAW 371 * | | | | | 372 * | | | | +----------------------------> AFMT_MU_LAW 373 * | | | | 374 * | | | +--------------------------------> AFMT_8BIT 375 * | | | 376 * | | +------------------------------------> AFMT_16BIT 377 * | | 378 * | +----------------------------------------> AFMT_24BIT 379 * | 380 * +--------------------------------------------> AFMT_32BIT 381 */ 382#define score_signeq(s1, s2) (((s1) & 0x1) == ((s2) & 0x1)) 383#define score_endianeq(s1, s2) (((s1) & 0x2) == ((s2) & 0x2)) 384#define score_cheq(s1, s2) (((s1) & 0xfc) == ((s2) & 0xfc)) 385#define score_val(s1) ((s1) & 0x3f00) 386#define score_cse(s1) ((s1) & 0x7f) 387 388u_int32_t 389chn_fmtscore(u_int32_t fmt) 390{ 391 u_int32_t ret; 392 393 ret = 0; 394 if (fmt & AFMT_SIGNED) 395 ret |= 1 << 0; 396 if (fmt & AFMT_BIGENDIAN) 397 ret |= 1 << 1; 398 if (fmt & AFMT_STEREO) 399 ret |= (2 & 0x3f) << 2; 400 else 401 ret |= (1 & 0x3f) << 2; 402 if (fmt & AFMT_A_LAW) 403 ret |= 1 << 8; 404 else if (fmt & AFMT_MU_LAW) 405 ret |= 1 << 9; 406 else if (fmt & AFMT_8BIT) 407 ret |= 1 << 10; 408 else if (fmt & AFMT_16BIT) 409 ret |= 1 << 11; 410 else if (fmt & AFMT_24BIT) 411 ret |= 1 << 12; 412 else if (fmt & AFMT_32BIT) 413 ret |= 1 << 13; 414 415 return ret; 416} 417 418static u_int32_t 419chn_fmtbestfunc(u_int32_t fmt, u_int32_t *fmts, int cheq) 420{ 421 u_int32_t best, score, score2, oldscore; 422 int i; 423 424 if (fmt == 0 || fmts == NULL || fmts[0] == 0) 425 return 0; 426 427 if (fmtvalid(fmt, fmts)) 428 return fmt; 429 430 best = 0; 431 score = chn_fmtscore(fmt); 432 oldscore = 0; 433 for (i = 0; fmts[i] != 0; i++) { 434 score2 = chn_fmtscore(fmts[i]); 435 if (cheq && !score_cheq(score, score2)) 436 continue; 437 if (oldscore == 0 || 438 (score_val(score2) == score_val(score)) || 439 (score_val(score2) == score_val(oldscore)) || 440 (score_val(score2) > score_val(oldscore) && 441 score_val(score2) < score_val(score)) || 442 (score_val(score2) < score_val(oldscore) && 443 score_val(score2) > score_val(score)) || 444 (score_val(oldscore) < score_val(score) && 445 score_val(score2) > score_val(oldscore))) { 446 if (score_val(oldscore) != score_val(score2) || 447 score_cse(score) == score_cse(score2) || 448 ((score_cse(oldscore) != score_cse(score) && 449 !score_endianeq(score, oldscore) && 450 (score_endianeq(score, score2) || 451 (!score_signeq(score, oldscore) && 452 score_signeq(score, score2)))))) { 453 best = fmts[i]; 454 oldscore = score2; 455 } 456 } 457 } 458 return best; 459} 460 461u_int32_t 462chn_fmtbestbit(u_int32_t fmt, u_int32_t *fmts) 463{ 464 return chn_fmtbestfunc(fmt, fmts, 0); 465} 466 467u_int32_t 468chn_fmtbeststereo(u_int32_t fmt, u_int32_t *fmts) 469{ 470 return chn_fmtbestfunc(fmt, fmts, 1); 471} 472 473u_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);
| 350 * of what the heck is this FMT_Q_* 351 */ 352#define FMT_Q_UP 1 353#define FMT_Q_DOWN 2 354#define FMT_Q_EQ 3 355#define FMT_Q_MULTI 4 356 357/* 358 * 14bit format scoring 359 * -------------------- 360 * 361 * 13 12 11 10 9 8 2 1 0 offset 362 * +---+---+---+---+---+---+-------------+---+---+ 363 * | X | X | X | X | X | X | X X X X X X | X | X | 364 * +---+---+---+---+---+---+-------------+---+---+ 365 * | | | | | | | | | 366 * | | | | | | | | +--> signed? 367 * | | | | | | | | 368 * | | | | | | | +------> bigendian? 369 * | | | | | | | 370 * | | | | | | +---------------> total channels 371 * | | | | | | 372 * | | | | | +------------------------> AFMT_A_LAW 373 * | | | | | 374 * | | | | +----------------------------> AFMT_MU_LAW 375 * | | | | 376 * | | | +--------------------------------> AFMT_8BIT 377 * | | | 378 * | | +------------------------------------> AFMT_16BIT 379 * | | 380 * | +----------------------------------------> AFMT_24BIT 381 * | 382 * +--------------------------------------------> AFMT_32BIT 383 */ 384#define score_signeq(s1, s2) (((s1) & 0x1) == ((s2) & 0x1)) 385#define score_endianeq(s1, s2) (((s1) & 0x2) == ((s2) & 0x2)) 386#define score_cheq(s1, s2) (((s1) & 0xfc) == ((s2) & 0xfc)) 387#define score_val(s1) ((s1) & 0x3f00) 388#define score_cse(s1) ((s1) & 0x7f) 389 390u_int32_t 391chn_fmtscore(u_int32_t fmt) 392{ 393 u_int32_t ret; 394 395 ret = 0; 396 if (fmt & AFMT_SIGNED) 397 ret |= 1 << 0; 398 if (fmt & AFMT_BIGENDIAN) 399 ret |= 1 << 1; 400 if (fmt & AFMT_STEREO) 401 ret |= (2 & 0x3f) << 2; 402 else 403 ret |= (1 & 0x3f) << 2; 404 if (fmt & AFMT_A_LAW) 405 ret |= 1 << 8; 406 else if (fmt & AFMT_MU_LAW) 407 ret |= 1 << 9; 408 else if (fmt & AFMT_8BIT) 409 ret |= 1 << 10; 410 else if (fmt & AFMT_16BIT) 411 ret |= 1 << 11; 412 else if (fmt & AFMT_24BIT) 413 ret |= 1 << 12; 414 else if (fmt & AFMT_32BIT) 415 ret |= 1 << 13; 416 417 return ret; 418} 419 420static u_int32_t 421chn_fmtbestfunc(u_int32_t fmt, u_int32_t *fmts, int cheq) 422{ 423 u_int32_t best, score, score2, oldscore; 424 int i; 425 426 if (fmt == 0 || fmts == NULL || fmts[0] == 0) 427 return 0; 428 429 if (fmtvalid(fmt, fmts)) 430 return fmt; 431 432 best = 0; 433 score = chn_fmtscore(fmt); 434 oldscore = 0; 435 for (i = 0; fmts[i] != 0; i++) { 436 score2 = chn_fmtscore(fmts[i]); 437 if (cheq && !score_cheq(score, score2)) 438 continue; 439 if (oldscore == 0 || 440 (score_val(score2) == score_val(score)) || 441 (score_val(score2) == score_val(oldscore)) || 442 (score_val(score2) > score_val(oldscore) && 443 score_val(score2) < score_val(score)) || 444 (score_val(score2) < score_val(oldscore) && 445 score_val(score2) > score_val(score)) || 446 (score_val(oldscore) < score_val(score) && 447 score_val(score2) > score_val(oldscore))) { 448 if (score_val(oldscore) != score_val(score2) || 449 score_cse(score) == score_cse(score2) || 450 ((score_cse(oldscore) != score_cse(score) && 451 !score_endianeq(score, oldscore) && 452 (score_endianeq(score, score2) || 453 (!score_signeq(score, oldscore) && 454 score_signeq(score, score2)))))) { 455 best = fmts[i]; 456 oldscore = score2; 457 } 458 } 459 } 460 return best; 461} 462 463u_int32_t 464chn_fmtbestbit(u_int32_t fmt, u_int32_t *fmts) 465{ 466 return chn_fmtbestfunc(fmt, fmts, 0); 467} 468 469u_int32_t 470chn_fmtbeststereo(u_int32_t fmt, u_int32_t *fmts) 471{ 472 return chn_fmtbestfunc(fmt, fmts, 1); 473} 474 475u_int32_t 476chn_fmtbest(u_int32_t fmt, u_int32_t *fmts) 477{ 478 u_int32_t best1, best2; 479 u_int32_t score, score1, score2; 480 481 if (fmtvalid(fmt, fmts)) 482 return fmt; 483 484 best1 = chn_fmtbeststereo(fmt, fmts); 485 best2 = chn_fmtbestbit(fmt, fmts); 486 487 if (best1 != 0 && best2 != 0 && best1 != best2) { 488 if (fmt & AFMT_STEREO) 489 return best1; 490 else { 491 score = score_val(chn_fmtscore(fmt)); 492 score1 = score_val(chn_fmtscore(best1)); 493 score2 = score_val(chn_fmtscore(best2)); 494 if (score1 == score2 || score1 == score) 495 return best1; 496 else if (score2 == score) 497 return best2; 498 else if (score1 > score2) 499 return best1; 500 return best2; 501 } 502 } else if (best2 == 0) 503 return best1; 504 else 505 return best2; 506} 507 508static struct pcm_feeder * 509feeder_fmtchain(u_int32_t *to, struct pcm_feeder *source, struct pcm_feeder *stop, int maxdepth) 510{ 511 struct feedertab_entry *fte, *ftebest; 512 struct pcm_feeder *try, *ret; 513 uint32_t fl, qout, qsrc, qdst; 514 int qtype; 515 516 if (to == NULL || to[0] == 0) 517 return NULL; 518 519 DEB(printf("trying %s (0x%08x -> 0x%08x)...\n", source->class->name, source->desc->in, source->desc->out)); 520 if (fmtvalid(source->desc->out, to)) { 521 DEB(printf("got it\n")); 522 return source; 523 } 524 525 if (maxdepth < 0) 526 return NULL; 527 528 /* 529 * WARNING: THIS IS _NOT_ FOR THE FAINT HEART 530 * Disclaimer: I don't expect anybody could understand this 531 * without deep logical and mathematical analysis 532 * involving various unnamed probability theorem. 533 * 534 * This "Best Fit Random Chain Selection" (BLEHBLEHWHATEVER) algorithm 535 * is **extremely** difficult to digest especially when applied to 536 * large sets / numbers of random chains (feeders), each with 537 * unique characteristic providing different sets of in/out format. 538 * 539 * Basically, our FEEDER_FMT (see feeder_fmt.c) chains characteristic: 540 * 1) Format chains 541 * 1.1 "8bit to any, not to 8bit" 542 * 1.1.1 sign can remain consistent, e.g: u8 -> u16[le|be] 543 * 1.1.2 sign can be changed, e.g: u8 -> s16[le|be] 544 * 1.1.3 endian can be changed, e.g: u8 -> u16[le|be] 545 * 1.1.4 both can be changed, e.g: u8 -> [u|s]16[le|be] 546 * 1.2 "Any to 8bit, not from 8bit" 547 * 1.2.1 sign can remain consistent, e.g: s16le -> s8 548 * 1.2.2 sign can be changed, e.g: s16le -> u8 549 * 1.2.3 source endian can be anything e.g: s16[le|be] -> s8 550 * 1.2.4 source endian / sign can be anything e.g: [u|s]16[le|be] -> u8 551 * 1.3 "Any to any where BOTH input and output either 8bit or non-8bit" 552 * 1.3.1 endian MUST remain consistent 553 * 1.3.2 sign CAN be changed 554 * 1.4 "Long jump" is allowed, e.g: from 16bit to 32bit, excluding 555 * 16bit to 24bit . 556 * 2) Channel chains (mono <-> stereo) 557 * 2.1 Both endian and sign MUST remain consistent 558 * 3) Endian chains (big endian <-> little endian) 559 * 3.1 Channels and sign MUST remain consistent 560 * 4) Sign chains (signed <-> unsigned) 561 * 4.1 Channels and endian MUST remain consistent 562 * 563 * .. and the mother of all chaining rules: 564 * 565 * Rules 0: Source and destination MUST not contain multiple selections. 566 * (qtype != FMT_Q_MULTI) 567 * 568 * First of all, our caller ( chn_fmtchain() ) will reduce the possible 569 * multiple from/to formats to a single best format using chn_fmtbest(). 570 * Then, using chn_fmtscore(), we determine the chaining characteristic. 571 * Our main goal is to narrow it down until it reach FMT_Q_EQ chaining 572 * type while still adhering above chaining rules. 573 * 574 * The need for this complicated chaining procedures is inevitable, 575 * since currently we have more than 200 different types of FEEDER_FMT 576 * doing various unique format conversion. Without this (the old way), 577 * it is possible to generate broken chain since it doesn't do any 578 * sanity checking to ensure that the output format is "properly aligned" 579 * with the direction of conversion (quality up/down/equal). 580 * 581 * Conversion: s24le to s32le 582 * Possible chain: 1) s24le -> s32le (correct, optimized) 583 * 2) s24le -> s16le -> s32le 584 * (since we have feeder_24to16 and feeder_16to32) 585 * +-- obviously broken! 586 * 587 * Using scoring mechanisme, this will ensure that the chaining 588 * process do the right thing, or at least, give the best chain 589 * possible without causing quality (the 'Q') degradation. 590 */ 591 592 qdst = chn_fmtscore(to[0]); 593 qsrc = chn_fmtscore(source->desc->out); 594 595#define score_q(s1) score_val(s1) 596#define score_8bit(s1) ((s1) & 0x700) 597#define score_non8bit(s1) (!score_8bit(s1)) 598#define score_across8bit(s1, s2) ((score_8bit(s1) && score_non8bit(s2)) || \ 599 (score_8bit(s2) && score_non8bit(s1))) 600 601#define FMT_CHAIN_Q_UP(s1, s2) (score_q(s1) < score_q(s2)) 602#define FMT_CHAIN_Q_DOWN(s1, s2) (score_q(s1) > score_q(s2)) 603#define FMT_CHAIN_Q_EQ(s1, s2) (score_q(s1) == score_q(s2)) 604#define FMT_Q_DOWN_FLAGS(s1, s2) (0x1 | (score_across8bit(s1, s2) ? \ 605 0x2 : 0x0)) 606#define FMT_Q_UP_FLAGS(s1, s2) FMT_Q_DOWN_FLAGS(s1, s2) 607#define FMT_Q_EQ_FLAGS(s1, s2) (0x3ffc | \ 608 ((score_cheq(s1, s2) && \ 609 score_endianeq(s1, s2)) ? \ 610 0x1 : 0x0) | \ 611 ((score_cheq(s1, s2) && \ 612 score_signeq(s1, s2)) ? \ 613 0x2 : 0x0)) 614 615 /* Determine chaining direction and set matching flag */ 616 fl = 0x3fff; 617 if (to[1] != 0) { 618 qtype = FMT_Q_MULTI; 619 printf("%s: WARNING: FMT_Q_MULTI chaining. Expect the unexpected.\n", __func__); 620 } else if (FMT_CHAIN_Q_DOWN(qsrc, qdst)) { 621 qtype = FMT_Q_DOWN; 622 fl = FMT_Q_DOWN_FLAGS(qsrc, qdst); 623 } else if (FMT_CHAIN_Q_UP(qsrc, qdst)) { 624 qtype = FMT_Q_UP; 625 fl = FMT_Q_UP_FLAGS(qsrc, qdst); 626 } else { 627 qtype = FMT_Q_EQ; 628 fl = FMT_Q_EQ_FLAGS(qsrc, qdst); 629 } 630 631 ftebest = NULL; 632 633 SLIST_FOREACH(fte, &feedertab, link) { 634 if (fte->desc == NULL) 635 continue; 636 if (fte->desc->type != FEEDER_FMT) 637 continue; 638 qout = chn_fmtscore(fte->desc->out); 639#define FMT_Q_MULTI_VALIDATE(qt) ((qt) == FMT_Q_MULTI) 640#define FMT_Q_FL_MATCH(qfl, s1, s2) (((s1) & (qfl)) == ((s2) & (qfl))) 641#define FMT_Q_UP_VALIDATE(qt, s1, s2, s3) ((qt) == FMT_Q_UP && \ 642 score_q(s3) >= score_q(s1) && \ 643 score_q(s3) <= score_q(s2)) 644#define FMT_Q_DOWN_VALIDATE(qt, s1, s2, s3) ((qt) == FMT_Q_DOWN && \ 645 score_q(s3) <= score_q(s1) && \ 646 score_q(s3) >= score_q(s2)) 647#define FMT_Q_EQ_VALIDATE(qt, s1, s2) ((qt) == FMT_Q_EQ && \ 648 score_q(s1) == score_q(s2)) 649 if (fte->desc->in == source->desc->out && 650 (FMT_Q_MULTI_VALIDATE(qtype) || 651 (FMT_Q_FL_MATCH(fl, qout, qdst) && 652 (FMT_Q_UP_VALIDATE(qtype, qsrc, qdst, qout) || 653 FMT_Q_DOWN_VALIDATE(qtype, qsrc, qdst, qout) || 654 FMT_Q_EQ_VALIDATE(qtype, qdst, qout))))) { 655 try = feeder_create(fte->feederclass, fte->desc); 656 if (try) { 657 try->source = source; 658 ret = chainok(try, stop) ? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL; 659 if (ret != NULL) 660 return ret; 661 feeder_destroy(try); 662 } 663 } else if (fte->desc->in == source->desc->out) { 664 /* XXX quality must be considered! */ 665 if (ftebest == NULL) 666 ftebest = fte; 667 } 668 } 669 670 if (ftebest != NULL) { 671 try = feeder_create(ftebest->feederclass, ftebest->desc); 672 if (try) { 673 try->source = source; 674 ret = chainok(try, stop) ? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL; 675 if (ret != NULL) 676 return ret; 677 feeder_destroy(try); 678 } 679 } 680 681 /* printf("giving up %s...\n", source->class->name); */ 682 683 return NULL; 684} 685 686u_int32_t 687chn_fmtchain(struct pcm_channel *c, u_int32_t *to) 688{ 689 struct pcm_feeder *try, *del, *stop; 690 u_int32_t tmpfrom[2], tmpto[2], best, *from; 691 int i, max, bestmax; 692 693 KASSERT(c != NULL, ("c == NULL")); 694 KASSERT(c->feeder != NULL, ("c->feeder == NULL")); 695 KASSERT(to != NULL, ("to == NULL")); 696 KASSERT(to[0] != 0, ("to[0] == 0")); 697 698 if (c == NULL || c->feeder == NULL || to == NULL || to[0] == 0) 699 return 0; 700 701 stop = c->feeder; 702 best = 0; 703 704 if (c->direction == PCMDIR_REC && c->feeder->desc->type == FEEDER_ROOT) { 705 from = chn_getcaps(c)->fmtlist; 706 if (from[1] != 0) { 707 best = chn_fmtbest(to[0], from); 708 if (best != 0) { 709 tmpfrom[0] = best; 710 tmpfrom[1] = 0; 711 from = tmpfrom; 712 } 713 } 714 } else { 715 tmpfrom[0] = c->feeder->desc->out; 716 tmpfrom[1] = 0; 717 from = tmpfrom; 718 if (to[1] != 0) { 719 best = chn_fmtbest(from[0], to); 720 if (best != 0) { 721 tmpto[0] = best; 722 tmpto[1] = 0; 723 to = tmpto; 724 } 725 } 726 } 727 728#define FEEDER_FMTCHAIN_MAXDEPTH 8 729 730 try = NULL; 731 732 if (to[0] != 0 && from[0] != 0 && 733 to[1] == 0 && from[1] == 0) { 734 max = 0; 735 best = from[0]; 736 c->feeder->desc->out = best; 737 do { 738 try = feeder_fmtchain(to, c->feeder, stop, max); 739 DEB(if (try != NULL) { 740 printf("%s: 0x%08x -> 0x%08x (maxdepth: %d)\n", 741 __func__, from[0], to[0], max); 742 }); 743 } while (try == NULL && max++ < FEEDER_FMTCHAIN_MAXDEPTH); 744 } else { 745 printf("%s: Using the old-way format chaining!\n", __func__); 746 i = 0; 747 best = 0; 748 bestmax = 100; 749 while (from[i] != 0) { 750 c->feeder->desc->out = from[i]; 751 try = NULL; 752 max = 0; 753 do { 754 try = feeder_fmtchain(to, c->feeder, stop, max); 755 } while (try == NULL && max++ < FEEDER_FMTCHAIN_MAXDEPTH); 756 if (try != NULL && max < bestmax) { 757 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);
|