xref: /freebsd/sys/dev/sound/pcm/feeder_chain.c (revision 00172d20070a544e0514cce9f94475c5525fd15e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008-2009 Ariff Abdullah <ariff@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifdef HAVE_KERNEL_OPTION_HEADERS
30 #include "opt_snd.h"
31 #endif
32 
33 #include <dev/sound/pcm/sound.h>
34 
35 #include "feeder_if.h"
36 
37 /* chain state */
38 struct feeder_chain_state {
39 	uint32_t afmt;				/* audio format */
40 	uint32_t rate;				/* sampling rate */
41 	struct pcmchan_matrix *matrix;		/* matrix map */
42 };
43 
44 /*
45  * chain descriptor that will be passed around from the beginning until the
46  * end of chain process.
47  */
48 struct feeder_chain_desc {
49 	struct feeder_chain_state origin;	/* original state */
50 	struct feeder_chain_state current;	/* current state */
51 	struct feeder_chain_state target;	/* target state */
52 	struct pcm_feederdesc desc;		/* feeder descriptor */
53 	uint32_t afmt_ne;			/* preferred native endian */
54 	int mode;				/* chain mode */
55 	int use_eq;				/* need EQ? */
56 	int use_matrix;				/* need channel matrixing? */
57 	int use_volume;				/* need softpcmvol? */
58 	int dummy;				/* dummy passthrough */
59 	int expensive;				/* possibly expensive */
60 };
61 
62 #define FEEDER_CHAIN_LEAN		0
63 #define FEEDER_CHAIN_16			1
64 #define FEEDER_CHAIN_32			2
65 #define FEEDER_CHAIN_MULTI		3
66 #define FEEDER_CHAIN_FULLMULTI		4
67 #define FEEDER_CHAIN_LAST		5
68 
69 #if defined(SND_FEEDER_FULL_MULTIFORMAT)
70 #define FEEDER_CHAIN_DEFAULT		FEEDER_CHAIN_FULLMULTI
71 #elif defined(SND_FEEDER_MULTIFORMAT)
72 #define FEEDER_CHAIN_DEFAULT		FEEDER_CHAIN_MULTI
73 #else
74 #define FEEDER_CHAIN_DEFAULT		FEEDER_CHAIN_LEAN
75 #endif
76 
77 /*
78  * List of preferred formats that might be required during
79  * processing. It will be decided through snd_fmtbest().
80  */
81 
82 /* 'Lean' mode, signed 16 or 32 bit native endian. */
83 static uint32_t feeder_chain_formats_lean[] = {
84 	AFMT_S16_NE, AFMT_S32_NE,
85 	0
86 };
87 
88 /* Force everything to signed 16 bit native endian. */
89 static uint32_t feeder_chain_formats_16[] = {
90 	AFMT_S16_NE,
91 	0
92 };
93 
94 /* Force everything to signed 32 bit native endian. */
95 static uint32_t feeder_chain_formats_32[] = {
96 	AFMT_S32_NE,
97 	0
98 };
99 
100 /* Multiple choices, all except 8 bit. */
101 static uint32_t feeder_chain_formats_multi[] = {
102 	AFMT_S16_LE, AFMT_S16_BE, AFMT_U16_LE, AFMT_U16_BE,
103 	AFMT_S24_LE, AFMT_S24_BE, AFMT_U24_LE, AFMT_U24_BE,
104 	AFMT_S32_LE, AFMT_S32_BE, AFMT_U32_LE, AFMT_U32_BE,
105 	0
106 };
107 
108 /* Everything that is convertible. */
109 static uint32_t feeder_chain_formats_fullmulti[] = {
110 	AFMT_S8, AFMT_U8,
111 	AFMT_S16_LE, AFMT_S16_BE, AFMT_U16_LE, AFMT_U16_BE,
112 	AFMT_S24_LE, AFMT_S24_BE, AFMT_U24_LE, AFMT_U24_BE,
113 	AFMT_S32_LE, AFMT_S32_BE, AFMT_U32_LE, AFMT_U32_BE,
114 	0
115 };
116 
117 static uint32_t *feeder_chain_formats[FEEDER_CHAIN_LAST] = {
118 	[FEEDER_CHAIN_LEAN]      = feeder_chain_formats_lean,
119 	[FEEDER_CHAIN_16]        = feeder_chain_formats_16,
120 	[FEEDER_CHAIN_32]        = feeder_chain_formats_32,
121 	[FEEDER_CHAIN_MULTI]     = feeder_chain_formats_multi,
122 	[FEEDER_CHAIN_FULLMULTI] = feeder_chain_formats_fullmulti
123 };
124 
125 static int feeder_chain_mode = FEEDER_CHAIN_DEFAULT;
126 
127 #if defined(_KERNEL) && defined(SND_DEBUG) && defined(SND_FEEDER_FULL_MULTIFORMAT)
128 SYSCTL_INT(_hw_snd, OID_AUTO, feeder_chain_mode, CTLFLAG_RWTUN,
129     &feeder_chain_mode, 0,
130     "feeder chain mode "
131     "(0=lean, 1=16bit, 2=32bit, 3=multiformat, 4=fullmultiformat)");
132 #endif
133 
134 /*
135  * feeder_build_format(): Chain any format converter.
136  */
137 static int
138 feeder_build_format(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
139 {
140 	struct feeder_class *fc;
141 	struct pcm_feederdesc *desc;
142 	int ret;
143 
144 	desc = &(cdesc->desc);
145 	desc->type = FEEDER_FORMAT;
146 	desc->in = 0;
147 	desc->out = 0;
148 	desc->flags = 0;
149 
150 	fc = feeder_getclass(desc);
151 	if (fc == NULL) {
152 		device_printf(c->dev,
153 		    "%s(): can't find feeder_format\n", __func__);
154 		return (ENOTSUP);
155 	}
156 
157 	desc->in = cdesc->current.afmt;
158 	desc->out = cdesc->target.afmt;
159 
160 	ret = feeder_add(c, fc, desc);
161 	if (ret != 0) {
162 		device_printf(c->dev,
163 		    "%s(): can't add feeder_format\n", __func__);
164 		return (ret);
165 	}
166 
167 	c->feederflags |= 1 << FEEDER_FORMAT;
168 
169 	cdesc->current.afmt = cdesc->target.afmt;
170 
171 	return (0);
172 }
173 
174 /*
175  * feeder_build_formatne(): Chain format converter that suite best for native
176  *                          endian format.
177  */
178 static int
179 feeder_build_formatne(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
180 {
181 	struct feeder_chain_state otarget;
182 	int ret;
183 
184 	if (cdesc->afmt_ne == 0 ||
185 	    AFMT_ENCODING(cdesc->current.afmt) == cdesc->afmt_ne)
186 		return (0);
187 
188 	otarget = cdesc->target;
189 	cdesc->target = cdesc->current;
190 	cdesc->target.afmt = SND_FORMAT(cdesc->afmt_ne,
191 	    cdesc->current.matrix->channels, cdesc->current.matrix->ext);
192 
193 	ret = feeder_build_format(c, cdesc);
194 	if (ret != 0)
195 		return (ret);
196 
197 	cdesc->target = otarget;
198 
199 	return (0);
200 }
201 
202 /*
203  * feeder_build_rate(): Chain sample rate converter.
204  */
205 static int
206 feeder_build_rate(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
207 {
208 	struct feeder_class *fc;
209 	struct pcm_feeder *f;
210 	struct pcm_feederdesc *desc;
211 	int ret;
212 
213 	ret = feeder_build_formatne(c, cdesc);
214 	if (ret != 0)
215 		return (ret);
216 
217 	desc = &(cdesc->desc);
218 	desc->type = FEEDER_RATE;
219 	desc->in = 0;
220 	desc->out = 0;
221 	desc->flags = 0;
222 
223 	fc = feeder_getclass(desc);
224 	if (fc == NULL) {
225 		device_printf(c->dev,
226 		    "%s(): can't find feeder_rate\n", __func__);
227 		return (ENOTSUP);
228 	}
229 
230 	desc->in = cdesc->current.afmt;
231 	desc->out = desc->in;
232 
233 	ret = feeder_add(c, fc, desc);
234 	if (ret != 0) {
235 		device_printf(c->dev,
236 		    "%s(): can't add feeder_rate\n", __func__);
237 		return (ret);
238 	}
239 
240 	f = c->feeder;
241 
242 	/*
243 	 * If in 'dummy' mode (possibly due to passthrough mode), set the
244 	 * conversion quality to the lowest possible (should be fastest) since
245 	 * listener won't be hearing anything. Theoretically we can just
246 	 * disable it, but that will cause weird runtime behaviour:
247 	 * application appear to play something that is either too fast or too
248 	 * slow.
249 	 */
250 	if (cdesc->dummy != 0) {
251 		ret = FEEDER_SET(f, FEEDRATE_QUALITY, 0);
252 		if (ret != 0) {
253 			device_printf(c->dev,
254 			    "%s(): can't set resampling quality\n", __func__);
255 			return (ret);
256 		}
257 	}
258 
259 	ret = FEEDER_SET(f, FEEDRATE_SRC, cdesc->current.rate);
260 	if (ret != 0) {
261 		device_printf(c->dev,
262 		    "%s(): can't set source rate\n", __func__);
263 		return (ret);
264 	}
265 
266 	ret = FEEDER_SET(f, FEEDRATE_DST, cdesc->target.rate);
267 	if (ret != 0) {
268 		device_printf(c->dev,
269 		    "%s(): can't set destination rate\n", __func__);
270 		return (ret);
271 	}
272 
273 	c->feederflags |= 1 << FEEDER_RATE;
274 
275 	cdesc->current.rate = cdesc->target.rate;
276 
277 	return (0);
278 }
279 
280 /*
281  * feeder_build_matrix(): Chain channel matrixing converter.
282  */
283 static int
284 feeder_build_matrix(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
285 {
286 	struct feeder_class *fc;
287 	struct pcm_feeder *f;
288 	struct pcm_feederdesc *desc;
289 	int ret;
290 
291 	ret = feeder_build_formatne(c, cdesc);
292 	if (ret != 0)
293 		return (ret);
294 
295 	desc = &(cdesc->desc);
296 	desc->type = FEEDER_MATRIX;
297 	desc->in = 0;
298 	desc->out = 0;
299 	desc->flags = 0;
300 
301 	fc = feeder_getclass(desc);
302 	if (fc == NULL) {
303 		device_printf(c->dev,
304 		    "%s(): can't find feeder_matrix\n", __func__);
305 		return (ENOTSUP);
306 	}
307 
308 	desc->in = cdesc->current.afmt;
309 	desc->out = SND_FORMAT(cdesc->current.afmt,
310 	    cdesc->target.matrix->channels, cdesc->target.matrix->ext);
311 
312 	ret = feeder_add(c, fc, desc);
313 	if (ret != 0) {
314 		device_printf(c->dev,
315 		    "%s(): can't add feeder_matrix\n", __func__);
316 		return (ret);
317 	}
318 
319 	f = c->feeder;
320 	ret = feeder_matrix_setup(f, cdesc->current.matrix,
321 	    cdesc->target.matrix);
322 	if (ret != 0) {
323 		device_printf(c->dev,
324 		    "%s(): feeder_matrix_setup() failed\n", __func__);
325 		return (ret);
326 	}
327 
328 	c->feederflags |= 1 << FEEDER_MATRIX;
329 
330 	cdesc->current.afmt = desc->out;
331 	cdesc->current.matrix = cdesc->target.matrix;
332 	cdesc->use_matrix = 0;
333 
334 	return (0);
335 }
336 
337 /*
338  * feeder_build_volume(): Chain soft volume.
339  */
340 static int
341 feeder_build_volume(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
342 {
343 	struct feeder_class *fc;
344 	struct pcm_feeder *f;
345 	struct pcm_feederdesc *desc;
346 	int ret;
347 
348 	ret = feeder_build_formatne(c, cdesc);
349 	if (ret != 0)
350 		return (ret);
351 
352 	desc = &(cdesc->desc);
353 	desc->type = FEEDER_VOLUME;
354 	desc->in = 0;
355 	desc->out = 0;
356 	desc->flags = 0;
357 
358 	fc = feeder_getclass(desc);
359 	if (fc == NULL) {
360 		device_printf(c->dev,
361 		    "%s(): can't find feeder_volume\n", __func__);
362 		return (ENOTSUP);
363 	}
364 
365 	desc->in = cdesc->current.afmt;
366 	desc->out = desc->in;
367 
368 	ret = feeder_add(c, fc, desc);
369 	if (ret != 0) {
370 		device_printf(c->dev,
371 		    "%s(): can't add feeder_volume\n", __func__);
372 		return (ret);
373 	}
374 
375 	f = c->feeder;
376 
377 	/*
378 	 * If in 'dummy' mode (possibly due to passthrough mode), set BYPASS
379 	 * mode since listener won't be hearing anything. Theoretically we can
380 	 * just disable it, but that will confuse volume per channel mixer.
381 	 */
382 	if (cdesc->dummy != 0) {
383 		ret = FEEDER_SET(f, FEEDVOLUME_STATE, FEEDVOLUME_BYPASS);
384 		if (ret != 0) {
385 			device_printf(c->dev,
386 			    "%s(): can't set volume bypass\n", __func__);
387 			return (ret);
388 		}
389 	}
390 
391 	ret = feeder_volume_apply_matrix(f, cdesc->current.matrix);
392 	if (ret != 0) {
393 		device_printf(c->dev,
394 		    "%s(): feeder_volume_apply_matrix() failed\n", __func__);
395 		return (ret);
396 	}
397 
398 	c->feederflags |= 1 << FEEDER_VOLUME;
399 
400 	cdesc->use_volume = 0;
401 
402 	return (0);
403 }
404 
405 /*
406  * feeder_build_eq(): Chain parametric software equalizer.
407  */
408 static int
409 feeder_build_eq(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
410 {
411 	struct feeder_class *fc;
412 	struct pcm_feeder *f;
413 	struct pcm_feederdesc *desc;
414 	int ret;
415 
416 	ret = feeder_build_formatne(c, cdesc);
417 	if (ret != 0)
418 		return (ret);
419 
420 	desc = &(cdesc->desc);
421 	desc->type = FEEDER_EQ;
422 	desc->in = 0;
423 	desc->out = 0;
424 	desc->flags = 0;
425 
426 	fc = feeder_getclass(desc);
427 	if (fc == NULL) {
428 		device_printf(c->dev,
429 		    "%s(): can't find feeder_eq\n", __func__);
430 		return (ENOTSUP);
431 	}
432 
433 	desc->in = cdesc->current.afmt;
434 	desc->out = desc->in;
435 
436 	ret = feeder_add(c, fc, desc);
437 	if (ret != 0) {
438 		device_printf(c->dev,
439 		    "%s(): can't add feeder_eq\n", __func__);
440 		return (ret);
441 	}
442 
443 	f = c->feeder;
444 
445 	ret = FEEDER_SET(f, FEEDEQ_RATE, cdesc->current.rate);
446 	if (ret != 0) {
447 		device_printf(c->dev,
448 		    "%s(): can't set rate on feeder_eq\n", __func__);
449 		return (ret);
450 	}
451 
452 	c->feederflags |= 1 << FEEDER_EQ;
453 
454 	cdesc->use_eq = 0;
455 
456 	return (0);
457 }
458 
459 /*
460  * feeder_build_root(): Chain root feeder, the top, father of all.
461  */
462 static int
463 feeder_build_root(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
464 {
465 	struct feeder_class *fc;
466 	int ret;
467 
468 	fc = feeder_getclass(NULL);
469 	if (fc == NULL) {
470 		device_printf(c->dev,
471 		    "%s(): can't find feeder_root\n", __func__);
472 		return (ENOTSUP);
473 	}
474 
475 	ret = feeder_add(c, fc, NULL);
476 	if (ret != 0) {
477 		device_printf(c->dev,
478 		    "%s(): can't add feeder_root\n", __func__);
479 		return (ret);
480 	}
481 
482 	c->feederflags |= 1 << FEEDER_ROOT;
483 
484 	c->feeder->desc->in = cdesc->current.afmt;
485 	c->feeder->desc->out = cdesc->current.afmt;
486 
487 	return (0);
488 }
489 
490 /*
491  * feeder_build_mixer(): Chain software mixer for virtual channels.
492  */
493 static int
494 feeder_build_mixer(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
495 {
496 	struct feeder_class *fc;
497 	struct pcm_feederdesc *desc;
498 	int ret;
499 
500 	desc = &(cdesc->desc);
501 	desc->type = FEEDER_MIXER;
502 	desc->in = 0;
503 	desc->out = 0;
504 	desc->flags = 0;
505 
506 	fc = feeder_getclass(desc);
507 	if (fc == NULL) {
508 		device_printf(c->dev,
509 		    "%s(): can't find feeder_mixer\n", __func__);
510 		return (ENOTSUP);
511 	}
512 
513 	desc->in = cdesc->current.afmt;
514 	desc->out = desc->in;
515 
516 	ret = feeder_add(c, fc, desc);
517 	if (ret != 0) {
518 		device_printf(c->dev,
519 		    "%s(): can't add feeder_mixer\n", __func__);
520 		return (ret);
521 	}
522 
523 	c->feederflags |= 1 << FEEDER_MIXER;
524 
525 	return (0);
526 }
527 
528 /* Macrosses to ease our job doing stuffs later. */
529 #define FEEDER_BW(c, t)		((c)->t.matrix->channels * (c)->t.rate)
530 
531 #define FEEDRATE_UP(c)		((c)->target.rate > (c)->current.rate)
532 #define FEEDRATE_DOWN(c)	((c)->target.rate < (c)->current.rate)
533 #define FEEDRATE_REQUIRED(c)	(FEEDRATE_UP(c) || FEEDRATE_DOWN(c))
534 
535 #define FEEDMATRIX_UP(c)	((c)->target.matrix->channels >		\
536 				 (c)->current.matrix->channels)
537 #define FEEDMATRIX_DOWN(c)	((c)->target.matrix->channels <		\
538 				 (c)->current.matrix->channels)
539 #define FEEDMATRIX_REQUIRED(c)	(FEEDMATRIX_UP(c) ||			\
540 				 FEEDMATRIX_DOWN(c) || (c)->use_matrix != 0)
541 
542 #define FEEDFORMAT_REQUIRED(c)	(AFMT_ENCODING((c)->current.afmt) !=	\
543 				 AFMT_ENCODING((c)->target.afmt))
544 
545 #define FEEDVOLUME_REQUIRED(c)	((c)->use_volume != 0)
546 
547 #define FEEDEQ_VALIDRATE(c, t)	(feeder_eq_validrate((c)->t.rate) != 0)
548 #define FEEDEQ_ECONOMY(c)	(FEEDER_BW(c, current) < FEEDER_BW(c, target))
549 #define FEEDEQ_REQUIRED(c)	((c)->use_eq != 0 &&			\
550 				 FEEDEQ_VALIDRATE(c, current))
551 
552 #define FEEDFORMAT_NE_REQUIRED(c)					\
553 	((c)->afmt_ne != AFMT_S32_NE &&					\
554 	(((c)->mode == FEEDER_CHAIN_16 &&				\
555 	AFMT_ENCODING((c)->current.afmt) != AFMT_S16_NE) ||		\
556 	((c)->mode == FEEDER_CHAIN_32 &&				\
557 	AFMT_ENCODING((c)->current.afmt) != AFMT_S32_NE) ||		\
558 	(c)->mode == FEEDER_CHAIN_FULLMULTI ||				\
559 	((c)->mode == FEEDER_CHAIN_MULTI &&				\
560 	((c)->current.afmt & AFMT_8BIT)) ||				\
561 	((c)->mode == FEEDER_CHAIN_LEAN &&				\
562 	!((c)->current.afmt & (AFMT_S16_NE | AFMT_S32_NE)))))
563 
564 static void
565 feeder_default_matrix(struct pcmchan_matrix *m, uint32_t fmt, int id)
566 {
567 	int x;
568 
569 	memset(m, 0, sizeof(*m));
570 
571 	m->id = id;
572 	m->channels = AFMT_CHANNEL(fmt);
573 	m->ext = AFMT_EXTCHANNEL(fmt);
574 	for (x = 0; x != SND_CHN_T_MAX; x++)
575 		m->offset[x] = -1;
576 }
577 
578 int
579 feeder_chain(struct pcm_channel *c)
580 {
581 	struct snddev_info *d;
582 	struct pcmchan_caps *caps;
583 	struct feeder_chain_desc cdesc;
584 	struct pcmchan_matrix *hwmatrix, *softmatrix;
585 	uint32_t hwfmt, softfmt;
586 	int ret;
587 
588 	CHN_LOCKASSERT(c);
589 
590 	/* Remove everything first. */
591 	feeder_remove(c);
592 
593 	KASSERT(c->feeder == NULL, ("feeder chain not empty"));
594 
595 	/* clear and populate chain descriptor. */
596 	bzero(&cdesc, sizeof(cdesc));
597 
598 	switch (feeder_chain_mode) {
599 	case FEEDER_CHAIN_LEAN:
600 	case FEEDER_CHAIN_16:
601 	case FEEDER_CHAIN_32:
602 #if defined(SND_FEEDER_MULTIFORMAT) || defined(SND_FEEDER_FULL_MULTIFORMAT)
603 	case FEEDER_CHAIN_MULTI:
604 #endif
605 #if defined(SND_FEEDER_FULL_MULTIFORMAT)
606 	case FEEDER_CHAIN_FULLMULTI:
607 #endif
608 		break;
609 	default:
610 		feeder_chain_mode = FEEDER_CHAIN_DEFAULT;
611 		break;
612 	}
613 
614 	cdesc.mode = feeder_chain_mode;
615 	cdesc.expensive = 1;	/* XXX faster.. */
616 
617 #define VCHAN_PASSTHROUGH(c)	(((c)->flags & (CHN_F_VIRTUAL |		\
618 				 CHN_F_PASSTHROUGH)) ==			\
619 				 (CHN_F_VIRTUAL | CHN_F_PASSTHROUGH))
620 
621 	/* Get the best possible hardware format. */
622 	if (VCHAN_PASSTHROUGH(c))
623 		hwfmt = c->parentchannel->format;
624 	else {
625 		caps = chn_getcaps(c);
626 		if (caps == NULL || caps->fmtlist == NULL) {
627 			device_printf(c->dev,
628 			    "%s(): failed to get channel caps\n", __func__);
629 			return (ENODEV);
630 		}
631 
632 		if ((c->format & AFMT_PASSTHROUGH) &&
633 		    !snd_fmtvalid(c->format, caps->fmtlist))
634 			return (ENODEV);
635 
636 		hwfmt = snd_fmtbest(c->format, caps->fmtlist);
637 		if (hwfmt == 0 || !snd_fmtvalid(hwfmt, caps->fmtlist)) {
638 			device_printf(c->dev,
639 			    "%s(): invalid hardware format 0x%08x\n",
640 			    __func__, hwfmt);
641 			{
642 				int i;
643 				for (i = 0; caps->fmtlist[i] != 0; i++)
644 					printf("0x%08x\n", caps->fmtlist[i]);
645 				printf("Req: 0x%08x\n", c->format);
646 			}
647 			return (ENODEV);
648 		}
649 	}
650 
651 	/*
652 	 * The 'hardware' possibly have different interpretation of channel
653 	 * matrixing, so get it first .....
654 	 */
655 	hwmatrix = CHANNEL_GETMATRIX(c->methods, c->devinfo, hwfmt);
656 	if (hwmatrix == NULL) {
657 		/* setup a default matrix */
658 		hwmatrix = &c->matrix_scratch;
659 		feeder_default_matrix(hwmatrix, hwfmt,
660 		    SND_CHN_MATRIX_UNKNOWN);
661 	}
662 	/* ..... and rebuild hwfmt. */
663 	hwfmt = SND_FORMAT(hwfmt, hwmatrix->channels, hwmatrix->ext);
664 
665 	/* Reset and rebuild default channel format/matrix map. */
666 	softfmt = c->format;
667 	softmatrix = &c->matrix;
668 	if (softmatrix->channels != AFMT_CHANNEL(softfmt) ||
669 	    softmatrix->ext != AFMT_EXTCHANNEL(softfmt)) {
670 		softmatrix = feeder_matrix_format_map(softfmt);
671 		if (softmatrix == NULL) {
672 			/* setup a default matrix */
673 		  	softmatrix = &c->matrix;
674 			feeder_default_matrix(softmatrix, softfmt,
675 			    SND_CHN_MATRIX_PCMCHANNEL);
676 		} else {
677 			c->matrix = *softmatrix;
678 			c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
679 		}
680 	}
681 	softfmt = SND_FORMAT(softfmt, softmatrix->channels, softmatrix->ext);
682 	if (softfmt != c->format)
683 		device_printf(c->dev,
684 		    "%s(): WARNING: %s Soft format 0x%08x -> 0x%08x\n",
685 		    __func__, CHN_DIRSTR(c), c->format, softfmt);
686 
687 	/*
688 	 * PLAY and REC are opposite.
689 	 */
690 	if (c->direction == PCMDIR_PLAY) {
691 		cdesc.origin.afmt    = softfmt;
692 		cdesc.origin.matrix  = softmatrix;
693 		cdesc.origin.rate    = c->speed;
694 		cdesc.target.afmt    = hwfmt;
695 		cdesc.target.matrix  = hwmatrix;
696 		cdesc.target.rate    = sndbuf_getspd(c->bufhard);
697 	} else {
698 		cdesc.origin.afmt    = hwfmt;
699 		cdesc.origin.matrix  = hwmatrix;
700 		cdesc.origin.rate    = sndbuf_getspd(c->bufhard);
701 		cdesc.target.afmt    = softfmt;
702 		cdesc.target.matrix  = softmatrix;
703 		cdesc.target.rate    = c->speed;
704 	}
705 
706 	d = c->parentsnddev;
707 
708 	/*
709 	 * If channel is in bitperfect or passthrough mode, make it appear
710 	 * that 'origin' and 'target' identical, skipping mostly chain
711 	 * procedures.
712 	 */
713 	if (CHN_BITPERFECT(c) || (c->format & AFMT_PASSTHROUGH)) {
714 		if (c->direction == PCMDIR_PLAY)
715 			cdesc.origin = cdesc.target;
716 		else
717 			cdesc.target = cdesc.origin;
718 		c->format = cdesc.target.afmt;
719 		c->speed  = cdesc.target.rate;
720 	} else {
721 		/* hwfmt is not convertible, so 'dummy' it. */
722 		if (hwfmt & AFMT_PASSTHROUGH)
723 			cdesc.dummy = 1;
724 
725 		if ((softfmt & AFMT_CONVERTIBLE) &&
726 		    (((d->flags & SD_F_VPC) && !(c->flags & CHN_F_HAS_VCHAN)) ||
727 		    (!(d->flags & SD_F_VPC) && (d->flags & SD_F_SOFTPCMVOL) &&
728 		    !(c->flags & CHN_F_VIRTUAL))))
729 			cdesc.use_volume = 1;
730 
731 		if (feeder_matrix_compare(cdesc.origin.matrix,
732 		    cdesc.target.matrix) != 0)
733 			cdesc.use_matrix = 1;
734 
735 		/* Soft EQ only applicable for PLAY. */
736 		if (cdesc.dummy == 0 &&
737 		    c->direction == PCMDIR_PLAY && (d->flags & SD_F_EQ) &&
738 		    (((d->flags & SD_F_EQ_PC) &&
739 		    !(c->flags & CHN_F_HAS_VCHAN)) ||
740 		    (!(d->flags & SD_F_EQ_PC) && !(c->flags & CHN_F_VIRTUAL))))
741 			cdesc.use_eq = 1;
742 
743 		if (FEEDFORMAT_NE_REQUIRED(&cdesc)) {
744 			cdesc.afmt_ne =
745 			    (cdesc.dummy != 0) ?
746 			    snd_fmtbest(AFMT_ENCODING(softfmt),
747 			    feeder_chain_formats[cdesc.mode]) :
748 			    snd_fmtbest(AFMT_ENCODING(cdesc.target.afmt),
749 			    feeder_chain_formats[cdesc.mode]);
750 			if (cdesc.afmt_ne == 0) {
751 				device_printf(c->dev,
752 				    "%s(): snd_fmtbest failed!\n", __func__);
753 				cdesc.afmt_ne =
754 				    (((cdesc.dummy != 0) ? softfmt :
755 				    cdesc.target.afmt) &
756 				    (AFMT_24BIT | AFMT_32BIT)) ?
757 				    AFMT_S32_NE : AFMT_S16_NE;
758 			}
759 		}
760 	}
761 
762 	cdesc.current = cdesc.origin;
763 
764 	/* Build everything. */
765 
766 	c->feederflags = 0;
767 
768 #define FEEDER_BUILD(t)	do {						\
769 	ret = feeder_build_##t(c, &cdesc);				\
770 	if (ret != 0)							\
771 		return (ret);						\
772 	} while (0)
773 
774 	if (!(c->flags & CHN_F_HAS_VCHAN) || c->direction == PCMDIR_REC)
775 		FEEDER_BUILD(root);
776 	else if (c->direction == PCMDIR_PLAY && (c->flags & CHN_F_HAS_VCHAN))
777 		FEEDER_BUILD(mixer);
778 	else
779 		return (ENOTSUP);
780 
781 	/*
782 	 * The basic idea is: The smaller the bandwidth, the cheaper the
783 	 * conversion process, with following constraints:-
784 	 *
785 	 * 1) Almost all feeders work best in 16/32 native endian.
786 	 * 2) Try to avoid 8bit feeders due to poor dynamic range.
787 	 * 3) Avoid volume, format, matrix and rate in BITPERFECT or
788 	 *    PASSTHROUGH mode.
789 	 * 4) Try putting volume before EQ or rate. Should help to
790 	 *    avoid/reduce possible clipping.
791 	 * 5) EQ require specific, valid rate, unless it allow sloppy
792 	 *    conversion.
793 	 */
794 	if (FEEDMATRIX_UP(&cdesc)) {
795 		if (FEEDEQ_REQUIRED(&cdesc) &&
796 		    (!FEEDEQ_VALIDRATE(&cdesc, target) ||
797 		    (cdesc.expensive == 0 && FEEDEQ_ECONOMY(&cdesc))))
798 			FEEDER_BUILD(eq);
799 		if (FEEDRATE_REQUIRED(&cdesc))
800 			FEEDER_BUILD(rate);
801 		FEEDER_BUILD(matrix);
802 		if (FEEDVOLUME_REQUIRED(&cdesc))
803 			FEEDER_BUILD(volume);
804 		if (FEEDEQ_REQUIRED(&cdesc))
805 			FEEDER_BUILD(eq);
806 	} else if (FEEDMATRIX_DOWN(&cdesc)) {
807 		FEEDER_BUILD(matrix);
808 		if (FEEDVOLUME_REQUIRED(&cdesc))
809 			FEEDER_BUILD(volume);
810 		if (FEEDEQ_REQUIRED(&cdesc) &&
811 		    (!FEEDEQ_VALIDRATE(&cdesc, target) ||
812 		    FEEDEQ_ECONOMY(&cdesc)))
813 			FEEDER_BUILD(eq);
814 		if (FEEDRATE_REQUIRED(&cdesc))
815 			FEEDER_BUILD(rate);
816 		if (FEEDEQ_REQUIRED(&cdesc))
817 			FEEDER_BUILD(eq);
818 	} else {
819 		if (FEEDRATE_DOWN(&cdesc)) {
820 			if (FEEDEQ_REQUIRED(&cdesc) &&
821 			    !FEEDEQ_VALIDRATE(&cdesc, target)) {
822 				if (FEEDVOLUME_REQUIRED(&cdesc))
823 					FEEDER_BUILD(volume);
824 				FEEDER_BUILD(eq);
825 			}
826 			FEEDER_BUILD(rate);
827 		}
828 		if (FEEDMATRIX_REQUIRED(&cdesc))
829 			FEEDER_BUILD(matrix);
830 		if (FEEDVOLUME_REQUIRED(&cdesc))
831 			FEEDER_BUILD(volume);
832 		if (FEEDRATE_UP(&cdesc)) {
833 			if (FEEDEQ_REQUIRED(&cdesc) &&
834 			    !FEEDEQ_VALIDRATE(&cdesc, target))
835 				FEEDER_BUILD(eq);
836 			FEEDER_BUILD(rate);
837 		}
838 		if (FEEDEQ_REQUIRED(&cdesc))
839 			FEEDER_BUILD(eq);
840 	}
841 
842 	if (FEEDFORMAT_REQUIRED(&cdesc))
843 		FEEDER_BUILD(format);
844 
845 	if (c->direction == PCMDIR_REC && (c->flags & CHN_F_HAS_VCHAN))
846 		FEEDER_BUILD(mixer);
847 
848 	sndbuf_setfmt(c->bufsoft, c->format);
849 	sndbuf_setspd(c->bufsoft, c->speed);
850 
851 	sndbuf_setfmt(c->bufhard, hwfmt);
852 
853 	chn_syncstate(c);
854 
855 	return (0);
856 }
857