xref: /freebsd/sys/dev/sound/pcm/feeder_chain.c (revision 0e97acdf58fe27b09c4824a474b0344daf997c5f)
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 int
565 feeder_chain(struct pcm_channel *c)
566 {
567 	struct snddev_info *d;
568 	struct pcmchan_caps *caps;
569 	struct feeder_chain_desc cdesc;
570 	struct pcmchan_matrix *hwmatrix, *softmatrix;
571 	uint32_t hwfmt, softfmt;
572 	int ret;
573 
574 	CHN_LOCKASSERT(c);
575 
576 	/* Remove everything first. */
577 	while (chn_removefeeder(c) == 0)
578 		;
579 
580 	KASSERT(c->feeder == NULL, ("feeder chain not empty"));
581 
582 	/* clear and populate chain descriptor. */
583 	bzero(&cdesc, sizeof(cdesc));
584 
585 	switch (feeder_chain_mode) {
586 	case FEEDER_CHAIN_LEAN:
587 	case FEEDER_CHAIN_16:
588 	case FEEDER_CHAIN_32:
589 #if defined(SND_FEEDER_MULTIFORMAT) || defined(SND_FEEDER_FULL_MULTIFORMAT)
590 	case FEEDER_CHAIN_MULTI:
591 #endif
592 #if defined(SND_FEEDER_FULL_MULTIFORMAT)
593 	case FEEDER_CHAIN_FULLMULTI:
594 #endif
595 		break;
596 	default:
597 		feeder_chain_mode = FEEDER_CHAIN_DEFAULT;
598 		break;
599 	}
600 
601 	cdesc.mode = feeder_chain_mode;
602 	cdesc.expensive = 1;	/* XXX faster.. */
603 
604 #define VCHAN_PASSTHROUGH(c)	(((c)->flags & (CHN_F_VIRTUAL |		\
605 				 CHN_F_PASSTHROUGH)) ==			\
606 				 (CHN_F_VIRTUAL | CHN_F_PASSTHROUGH))
607 
608 	/* Get the best possible hardware format. */
609 	if (VCHAN_PASSTHROUGH(c))
610 		hwfmt = c->parentchannel->format;
611 	else {
612 		caps = chn_getcaps(c);
613 		if (caps == NULL || caps->fmtlist == NULL) {
614 			device_printf(c->dev,
615 			    "%s(): failed to get channel caps\n", __func__);
616 			return (ENODEV);
617 		}
618 
619 		if ((c->format & AFMT_PASSTHROUGH) &&
620 		    !snd_fmtvalid(c->format, caps->fmtlist))
621 			return (ENODEV);
622 
623 		hwfmt = snd_fmtbest(c->format, caps->fmtlist);
624 		if (hwfmt == 0 || !snd_fmtvalid(hwfmt, caps->fmtlist)) {
625 			device_printf(c->dev,
626 			    "%s(): invalid hardware format 0x%08x\n",
627 			    __func__, hwfmt);
628 			{
629 				int i;
630 				for (i = 0; caps->fmtlist[i] != 0; i++)
631 					printf("0x%08x\n", caps->fmtlist[i]);
632 				printf("Req: 0x%08x\n", c->format);
633 			}
634 			return (ENODEV);
635 		}
636 	}
637 
638 	/*
639 	 * The 'hardware' possibly have different intepretation of channel
640 	 * matrixing, so get it first .....
641 	 */
642 	hwmatrix = CHANNEL_GETMATRIX(c->methods, c->devinfo, hwfmt);
643 	if (hwmatrix == NULL) {
644 		device_printf(c->dev,
645 		    "%s(): failed to acquire hw matrix [0x%08x]\n",
646 		    __func__, hwfmt);
647 		return (ENODEV);
648 	}
649 	/* ..... and rebuild hwfmt. */
650 	hwfmt = SND_FORMAT(hwfmt, hwmatrix->channels, hwmatrix->ext);
651 
652 	/* Reset and rebuild default channel format/matrix map. */
653 	softfmt = c->format;
654 	softmatrix = &c->matrix;
655 	if (softmatrix->channels != AFMT_CHANNEL(softfmt) ||
656 	    softmatrix->ext != AFMT_EXTCHANNEL(softfmt)) {
657 		softmatrix = feeder_matrix_format_map(softfmt);
658 		if (softmatrix == NULL) {
659 			device_printf(c->dev,
660 			    "%s(): failed to acquire soft matrix [0x%08x]\n",
661 			    __func__, softfmt);
662 			return (ENODEV);
663 		}
664 		c->matrix = *softmatrix;
665 		c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
666 	}
667 	softfmt = SND_FORMAT(softfmt, softmatrix->channels, softmatrix->ext);
668 	if (softfmt != c->format)
669 		device_printf(c->dev,
670 		    "%s(): WARNING: %s Soft format 0x%08x -> 0x%08x\n",
671 		    __func__, CHN_DIRSTR(c), c->format, softfmt);
672 
673 	/*
674 	 * PLAY and REC are opposite.
675 	 */
676 	if (c->direction == PCMDIR_PLAY) {
677 		cdesc.origin.afmt    = softfmt;
678 		cdesc.origin.matrix  = softmatrix;
679 		cdesc.origin.rate    = c->speed;
680 		cdesc.target.afmt    = hwfmt;
681 		cdesc.target.matrix  = hwmatrix;
682 		cdesc.target.rate    = sndbuf_getspd(c->bufhard);
683 	} else {
684 		cdesc.origin.afmt    = hwfmt;
685 		cdesc.origin.matrix  = hwmatrix;
686 		cdesc.origin.rate    = sndbuf_getspd(c->bufhard);
687 		cdesc.target.afmt    = softfmt;
688 		cdesc.target.matrix  = softmatrix;
689 		cdesc.target.rate    = c->speed;
690 	}
691 
692 	d = c->parentsnddev;
693 
694 	/*
695 	 * If channel is in bitperfect or passthrough mode, make it appear
696 	 * that 'origin' and 'target' identical, skipping mostly chain
697 	 * procedures.
698 	 */
699 	if (CHN_BITPERFECT(c) || (c->format & AFMT_PASSTHROUGH)) {
700 		if (c->direction == PCMDIR_PLAY)
701 			cdesc.origin = cdesc.target;
702 		else
703 			cdesc.target = cdesc.origin;
704 		c->format = cdesc.target.afmt;
705 		c->speed  = cdesc.target.rate;
706 	} else {
707 		/* hwfmt is not convertible, so 'dummy' it. */
708 		if (hwfmt & AFMT_PASSTHROUGH)
709 			cdesc.dummy = 1;
710 
711 		if ((softfmt & AFMT_CONVERTIBLE) &&
712 		    (((d->flags & SD_F_VPC) && !(c->flags & CHN_F_HAS_VCHAN)) ||
713 		    (!(d->flags & SD_F_VPC) && (d->flags & SD_F_SOFTPCMVOL) &&
714 		    !(c->flags & CHN_F_VIRTUAL))))
715 			cdesc.use_volume = 1;
716 
717 		if (feeder_matrix_compare(cdesc.origin.matrix,
718 		    cdesc.target.matrix) != 0)
719 			cdesc.use_matrix = 1;
720 
721 		/* Soft EQ only applicable for PLAY. */
722 		if (cdesc.dummy == 0 &&
723 		    c->direction == PCMDIR_PLAY && (d->flags & SD_F_EQ) &&
724 		    (((d->flags & SD_F_EQ_PC) &&
725 		    !(c->flags & CHN_F_HAS_VCHAN)) ||
726 		    (!(d->flags & SD_F_EQ_PC) && !(c->flags & CHN_F_VIRTUAL))))
727 			cdesc.use_eq = 1;
728 
729 		if (FEEDFORMAT_NE_REQUIRED(&cdesc)) {
730 			cdesc.afmt_ne =
731 			    (cdesc.dummy != 0) ?
732 			    snd_fmtbest(AFMT_ENCODING(softfmt),
733 			    feeder_chain_formats[cdesc.mode]) :
734 			    snd_fmtbest(AFMT_ENCODING(cdesc.target.afmt),
735 			    feeder_chain_formats[cdesc.mode]);
736 			if (cdesc.afmt_ne == 0) {
737 				device_printf(c->dev,
738 				    "%s(): snd_fmtbest failed!\n", __func__);
739 				cdesc.afmt_ne =
740 				    (((cdesc.dummy != 0) ? softfmt :
741 				    cdesc.target.afmt) &
742 				    (AFMT_24BIT | AFMT_32BIT)) ?
743 				    AFMT_S32_NE : AFMT_S16_NE;
744 			}
745 		}
746 	}
747 
748 	cdesc.current = cdesc.origin;
749 
750 	/* Build everything. */
751 
752 	c->feederflags = 0;
753 
754 #define FEEDER_BUILD(t)	do {						\
755 	ret = feeder_build_##t(c, &cdesc);				\
756 	if (ret != 0)							\
757 		return (ret);						\
758 	} while (0)
759 
760 	if (!(c->flags & CHN_F_HAS_VCHAN) || c->direction == PCMDIR_REC)
761 		FEEDER_BUILD(root);
762 	else if (c->direction == PCMDIR_PLAY && (c->flags & CHN_F_HAS_VCHAN))
763 		FEEDER_BUILD(mixer);
764 	else
765 		return (ENOTSUP);
766 
767 	/*
768 	 * The basic idea is: The smaller the bandwidth, the cheaper the
769 	 * conversion process, with following constraints:-
770 	 *
771 	 * 1) Almost all feeders work best in 16/32 native endian.
772 	 * 2) Try to avoid 8bit feeders due to poor dynamic range.
773 	 * 3) Avoid volume, format, matrix and rate in BITPERFECT or
774 	 *    PASSTHROUGH mode.
775 	 * 4) Try putting volume before EQ or rate. Should help to
776 	 *    avoid/reduce possible clipping.
777 	 * 5) EQ require specific, valid rate, unless it allow sloppy
778 	 *    conversion.
779 	 */
780 	if (FEEDMATRIX_UP(&cdesc)) {
781 		if (FEEDEQ_REQUIRED(&cdesc) &&
782 		    (!FEEDEQ_VALIDRATE(&cdesc, target) ||
783 		    (cdesc.expensive == 0 && FEEDEQ_ECONOMY(&cdesc))))
784 			FEEDER_BUILD(eq);
785 		if (FEEDRATE_REQUIRED(&cdesc))
786 			FEEDER_BUILD(rate);
787 		FEEDER_BUILD(matrix);
788 		if (FEEDVOLUME_REQUIRED(&cdesc))
789 			FEEDER_BUILD(volume);
790 		if (FEEDEQ_REQUIRED(&cdesc))
791 			FEEDER_BUILD(eq);
792 	} else if (FEEDMATRIX_DOWN(&cdesc)) {
793 		FEEDER_BUILD(matrix);
794 		if (FEEDVOLUME_REQUIRED(&cdesc))
795 			FEEDER_BUILD(volume);
796 		if (FEEDEQ_REQUIRED(&cdesc) &&
797 		    (!FEEDEQ_VALIDRATE(&cdesc, target) ||
798 		    FEEDEQ_ECONOMY(&cdesc)))
799 			FEEDER_BUILD(eq);
800 		if (FEEDRATE_REQUIRED(&cdesc))
801 			FEEDER_BUILD(rate);
802 		if (FEEDEQ_REQUIRED(&cdesc))
803 			FEEDER_BUILD(eq);
804 	} else {
805 		if (FEEDRATE_DOWN(&cdesc)) {
806 			if (FEEDEQ_REQUIRED(&cdesc) &&
807 			    !FEEDEQ_VALIDRATE(&cdesc, target)) {
808 				if (FEEDVOLUME_REQUIRED(&cdesc))
809 					FEEDER_BUILD(volume);
810 				FEEDER_BUILD(eq);
811 			}
812 			FEEDER_BUILD(rate);
813 		}
814 		if (FEEDMATRIX_REQUIRED(&cdesc))
815 			FEEDER_BUILD(matrix);
816 		if (FEEDVOLUME_REQUIRED(&cdesc))
817 			FEEDER_BUILD(volume);
818 		if (FEEDRATE_UP(&cdesc)) {
819 			if (FEEDEQ_REQUIRED(&cdesc) &&
820 			    !FEEDEQ_VALIDRATE(&cdesc, target))
821 				FEEDER_BUILD(eq);
822 			FEEDER_BUILD(rate);
823 		}
824 		if (FEEDEQ_REQUIRED(&cdesc))
825 			FEEDER_BUILD(eq);
826 	}
827 
828 	if (FEEDFORMAT_REQUIRED(&cdesc))
829 		FEEDER_BUILD(format);
830 
831 	if (c->direction == PCMDIR_REC && (c->flags & CHN_F_HAS_VCHAN))
832 		FEEDER_BUILD(mixer);
833 
834 	sndbuf_setfmt(c->bufsoft, c->format);
835 	sndbuf_setspd(c->bufsoft, c->speed);
836 
837 	sndbuf_setfmt(c->bufhard, hwfmt);
838 
839 	chn_syncstate(c);
840 
841 	return (0);
842 }
843