xref: /freebsd/sys/dev/sound/pcm/feeder_chain.c (revision aa24f48b361effe51163877d84f1b70d32b77e04)
1 /*-
2  * Copyright (c) 2008-2009 Ariff Abdullah <ariff@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifdef HAVE_KERNEL_OPTION_HEADERS
28 #include "opt_snd.h"
29 #endif
30 
31 #include <dev/sound/pcm/sound.h>
32 
33 #include "feeder_if.h"
34 
35 SND_DECLARE_FILE("$FreeBSD$");
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;			/* prefered 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 prefered 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 = chn_addfeeder(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 = chn_addfeeder(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 = chn_addfeeder(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 = chn_addfeeder(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 = chn_addfeeder(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 = chn_addfeeder(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 = chn_addfeeder(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 	while (chn_removefeeder(c) == 0)
592 		;
593 
594 	KASSERT(c->feeder == NULL, ("feeder chain not empty"));
595 
596 	/* clear and populate chain descriptor. */
597 	bzero(&cdesc, sizeof(cdesc));
598 
599 	switch (feeder_chain_mode) {
600 	case FEEDER_CHAIN_LEAN:
601 	case FEEDER_CHAIN_16:
602 	case FEEDER_CHAIN_32:
603 #if defined(SND_FEEDER_MULTIFORMAT) || defined(SND_FEEDER_FULL_MULTIFORMAT)
604 	case FEEDER_CHAIN_MULTI:
605 #endif
606 #if defined(SND_FEEDER_FULL_MULTIFORMAT)
607 	case FEEDER_CHAIN_FULLMULTI:
608 #endif
609 		break;
610 	default:
611 		feeder_chain_mode = FEEDER_CHAIN_DEFAULT;
612 		break;
613 	}
614 
615 	cdesc.mode = feeder_chain_mode;
616 	cdesc.expensive = 1;	/* XXX faster.. */
617 
618 #define VCHAN_PASSTHROUGH(c)	(((c)->flags & (CHN_F_VIRTUAL |		\
619 				 CHN_F_PASSTHROUGH)) ==			\
620 				 (CHN_F_VIRTUAL | CHN_F_PASSTHROUGH))
621 
622 	/* Get the best possible hardware format. */
623 	if (VCHAN_PASSTHROUGH(c))
624 		hwfmt = c->parentchannel->format;
625 	else {
626 		caps = chn_getcaps(c);
627 		if (caps == NULL || caps->fmtlist == NULL) {
628 			device_printf(c->dev,
629 			    "%s(): failed to get channel caps\n", __func__);
630 			return (ENODEV);
631 		}
632 
633 		if ((c->format & AFMT_PASSTHROUGH) &&
634 		    !snd_fmtvalid(c->format, caps->fmtlist))
635 			return (ENODEV);
636 
637 		hwfmt = snd_fmtbest(c->format, caps->fmtlist);
638 		if (hwfmt == 0 || !snd_fmtvalid(hwfmt, caps->fmtlist)) {
639 			device_printf(c->dev,
640 			    "%s(): invalid hardware format 0x%08x\n",
641 			    __func__, hwfmt);
642 			{
643 				int i;
644 				for (i = 0; caps->fmtlist[i] != 0; i++)
645 					printf("0x%08x\n", caps->fmtlist[i]);
646 				printf("Req: 0x%08x\n", c->format);
647 			}
648 			return (ENODEV);
649 		}
650 	}
651 
652 	/*
653 	 * The 'hardware' possibly have different intepretation of channel
654 	 * matrixing, so get it first .....
655 	 */
656 	hwmatrix = CHANNEL_GETMATRIX(c->methods, c->devinfo, hwfmt);
657 	if (hwmatrix == NULL) {
658 		/* setup a default matrix */
659 		hwmatrix = &c->matrix_scratch;
660 		feeder_default_matrix(hwmatrix, hwfmt,
661 		    SND_CHN_MATRIX_UNKNOWN);
662 	}
663 	/* ..... and rebuild hwfmt. */
664 	hwfmt = SND_FORMAT(hwfmt, hwmatrix->channels, hwmatrix->ext);
665 
666 	/* Reset and rebuild default channel format/matrix map. */
667 	softfmt = c->format;
668 	softmatrix = &c->matrix;
669 	if (softmatrix->channels != AFMT_CHANNEL(softfmt) ||
670 	    softmatrix->ext != AFMT_EXTCHANNEL(softfmt)) {
671 		softmatrix = feeder_matrix_format_map(softfmt);
672 		if (softmatrix == NULL) {
673 			/* setup a default matrix */
674 		  	softmatrix = &c->matrix;
675 			feeder_default_matrix(softmatrix, softfmt,
676 			    SND_CHN_MATRIX_PCMCHANNEL);
677 		} else {
678 			c->matrix = *softmatrix;
679 			c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
680 		}
681 	}
682 	softfmt = SND_FORMAT(softfmt, softmatrix->channels, softmatrix->ext);
683 	if (softfmt != c->format)
684 		device_printf(c->dev,
685 		    "%s(): WARNING: %s Soft format 0x%08x -> 0x%08x\n",
686 		    __func__, CHN_DIRSTR(c), c->format, softfmt);
687 
688 	/*
689 	 * PLAY and REC are opposite.
690 	 */
691 	if (c->direction == PCMDIR_PLAY) {
692 		cdesc.origin.afmt    = softfmt;
693 		cdesc.origin.matrix  = softmatrix;
694 		cdesc.origin.rate    = c->speed;
695 		cdesc.target.afmt    = hwfmt;
696 		cdesc.target.matrix  = hwmatrix;
697 		cdesc.target.rate    = sndbuf_getspd(c->bufhard);
698 	} else {
699 		cdesc.origin.afmt    = hwfmt;
700 		cdesc.origin.matrix  = hwmatrix;
701 		cdesc.origin.rate    = sndbuf_getspd(c->bufhard);
702 		cdesc.target.afmt    = softfmt;
703 		cdesc.target.matrix  = softmatrix;
704 		cdesc.target.rate    = c->speed;
705 	}
706 
707 	d = c->parentsnddev;
708 
709 	/*
710 	 * If channel is in bitperfect or passthrough mode, make it appear
711 	 * that 'origin' and 'target' identical, skipping mostly chain
712 	 * procedures.
713 	 */
714 	if (CHN_BITPERFECT(c) || (c->format & AFMT_PASSTHROUGH)) {
715 		if (c->direction == PCMDIR_PLAY)
716 			cdesc.origin = cdesc.target;
717 		else
718 			cdesc.target = cdesc.origin;
719 		c->format = cdesc.target.afmt;
720 		c->speed  = cdesc.target.rate;
721 	} else {
722 		/* hwfmt is not convertible, so 'dummy' it. */
723 		if (hwfmt & AFMT_PASSTHROUGH)
724 			cdesc.dummy = 1;
725 
726 		if ((softfmt & AFMT_CONVERTIBLE) &&
727 		    (((d->flags & SD_F_VPC) && !(c->flags & CHN_F_HAS_VCHAN)) ||
728 		    (!(d->flags & SD_F_VPC) && (d->flags & SD_F_SOFTPCMVOL) &&
729 		    !(c->flags & CHN_F_VIRTUAL))))
730 			cdesc.use_volume = 1;
731 
732 		if (feeder_matrix_compare(cdesc.origin.matrix,
733 		    cdesc.target.matrix) != 0)
734 			cdesc.use_matrix = 1;
735 
736 		/* Soft EQ only applicable for PLAY. */
737 		if (cdesc.dummy == 0 &&
738 		    c->direction == PCMDIR_PLAY && (d->flags & SD_F_EQ) &&
739 		    (((d->flags & SD_F_EQ_PC) &&
740 		    !(c->flags & CHN_F_HAS_VCHAN)) ||
741 		    (!(d->flags & SD_F_EQ_PC) && !(c->flags & CHN_F_VIRTUAL))))
742 			cdesc.use_eq = 1;
743 
744 		if (FEEDFORMAT_NE_REQUIRED(&cdesc)) {
745 			cdesc.afmt_ne =
746 			    (cdesc.dummy != 0) ?
747 			    snd_fmtbest(AFMT_ENCODING(softfmt),
748 			    feeder_chain_formats[cdesc.mode]) :
749 			    snd_fmtbest(AFMT_ENCODING(cdesc.target.afmt),
750 			    feeder_chain_formats[cdesc.mode]);
751 			if (cdesc.afmt_ne == 0) {
752 				device_printf(c->dev,
753 				    "%s(): snd_fmtbest failed!\n", __func__);
754 				cdesc.afmt_ne =
755 				    (((cdesc.dummy != 0) ? softfmt :
756 				    cdesc.target.afmt) &
757 				    (AFMT_24BIT | AFMT_32BIT)) ?
758 				    AFMT_S32_NE : AFMT_S16_NE;
759 			}
760 		}
761 	}
762 
763 	cdesc.current = cdesc.origin;
764 
765 	/* Build everything. */
766 
767 	c->feederflags = 0;
768 
769 #define FEEDER_BUILD(t)	do {						\
770 	ret = feeder_build_##t(c, &cdesc);				\
771 	if (ret != 0)							\
772 		return (ret);						\
773 	} while (0)
774 
775 	if (!(c->flags & CHN_F_HAS_VCHAN) || c->direction == PCMDIR_REC)
776 		FEEDER_BUILD(root);
777 	else if (c->direction == PCMDIR_PLAY && (c->flags & CHN_F_HAS_VCHAN))
778 		FEEDER_BUILD(mixer);
779 	else
780 		return (ENOTSUP);
781 
782 	/*
783 	 * The basic idea is: The smaller the bandwidth, the cheaper the
784 	 * conversion process, with following constraints:-
785 	 *
786 	 * 1) Almost all feeders work best in 16/32 native endian.
787 	 * 2) Try to avoid 8bit feeders due to poor dynamic range.
788 	 * 3) Avoid volume, format, matrix and rate in BITPERFECT or
789 	 *    PASSTHROUGH mode.
790 	 * 4) Try putting volume before EQ or rate. Should help to
791 	 *    avoid/reduce possible clipping.
792 	 * 5) EQ require specific, valid rate, unless it allow sloppy
793 	 *    conversion.
794 	 */
795 	if (FEEDMATRIX_UP(&cdesc)) {
796 		if (FEEDEQ_REQUIRED(&cdesc) &&
797 		    (!FEEDEQ_VALIDRATE(&cdesc, target) ||
798 		    (cdesc.expensive == 0 && FEEDEQ_ECONOMY(&cdesc))))
799 			FEEDER_BUILD(eq);
800 		if (FEEDRATE_REQUIRED(&cdesc))
801 			FEEDER_BUILD(rate);
802 		FEEDER_BUILD(matrix);
803 		if (FEEDVOLUME_REQUIRED(&cdesc))
804 			FEEDER_BUILD(volume);
805 		if (FEEDEQ_REQUIRED(&cdesc))
806 			FEEDER_BUILD(eq);
807 	} else if (FEEDMATRIX_DOWN(&cdesc)) {
808 		FEEDER_BUILD(matrix);
809 		if (FEEDVOLUME_REQUIRED(&cdesc))
810 			FEEDER_BUILD(volume);
811 		if (FEEDEQ_REQUIRED(&cdesc) &&
812 		    (!FEEDEQ_VALIDRATE(&cdesc, target) ||
813 		    FEEDEQ_ECONOMY(&cdesc)))
814 			FEEDER_BUILD(eq);
815 		if (FEEDRATE_REQUIRED(&cdesc))
816 			FEEDER_BUILD(rate);
817 		if (FEEDEQ_REQUIRED(&cdesc))
818 			FEEDER_BUILD(eq);
819 	} else {
820 		if (FEEDRATE_DOWN(&cdesc)) {
821 			if (FEEDEQ_REQUIRED(&cdesc) &&
822 			    !FEEDEQ_VALIDRATE(&cdesc, target)) {
823 				if (FEEDVOLUME_REQUIRED(&cdesc))
824 					FEEDER_BUILD(volume);
825 				FEEDER_BUILD(eq);
826 			}
827 			FEEDER_BUILD(rate);
828 		}
829 		if (FEEDMATRIX_REQUIRED(&cdesc))
830 			FEEDER_BUILD(matrix);
831 		if (FEEDVOLUME_REQUIRED(&cdesc))
832 			FEEDER_BUILD(volume);
833 		if (FEEDRATE_UP(&cdesc)) {
834 			if (FEEDEQ_REQUIRED(&cdesc) &&
835 			    !FEEDEQ_VALIDRATE(&cdesc, target))
836 				FEEDER_BUILD(eq);
837 			FEEDER_BUILD(rate);
838 		}
839 		if (FEEDEQ_REQUIRED(&cdesc))
840 			FEEDER_BUILD(eq);
841 	}
842 
843 	if (FEEDFORMAT_REQUIRED(&cdesc))
844 		FEEDER_BUILD(format);
845 
846 	if (c->direction == PCMDIR_REC && (c->flags & CHN_F_HAS_VCHAN))
847 		FEEDER_BUILD(mixer);
848 
849 	sndbuf_setfmt(c->bufsoft, c->format);
850 	sndbuf_setspd(c->bufsoft, c->speed);
851 
852 	sndbuf_setfmt(c->bufhard, hwfmt);
853 
854 	chn_syncstate(c);
855 
856 	return (0);
857 }
858