Linux Audio

Check our new training course

Loading...
v5.4
  1/*
  2 *  PCM Plug-In shared (kernel/library) code
  3 *  Copyright (c) 1999 by Jaroslav Kysela <perex@perex.cz>
  4 *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
  5 *
  6 *
  7 *   This library is free software; you can redistribute it and/or modify
  8 *   it under the terms of the GNU Library General Public License as
  9 *   published by the Free Software Foundation; either version 2 of
 10 *   the License, or (at your option) any later version.
 11 *
 12 *   This program is distributed in the hope that it will be useful,
 13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 *   GNU Library General Public License for more details.
 16 *
 17 *   You should have received a copy of the GNU Library General Public
 18 *   License along with this library; if not, write to the Free Software
 19 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 20 *
 21 */
 22  
 23#if 0
 24#define PLUGIN_DEBUG
 25#endif
 26
 27#include <linux/slab.h>
 28#include <linux/time.h>
 29#include <linux/vmalloc.h>
 30#include <sound/core.h>
 31#include <sound/pcm.h>
 32#include <sound/pcm_params.h>
 33#include "pcm_plugin.h"
 34
 35#define snd_pcm_plug_first(plug) ((plug)->runtime->oss.plugin_first)
 36#define snd_pcm_plug_last(plug) ((plug)->runtime->oss.plugin_last)
 37
 38/*
 39 *  because some cards might have rates "very close", we ignore
 40 *  all "resampling" requests within +-5%
 41 */
 42static int rate_match(unsigned int src_rate, unsigned int dst_rate)
 43{
 44	unsigned int low = (src_rate * 95) / 100;
 45	unsigned int high = (src_rate * 105) / 100;
 46	return dst_rate >= low && dst_rate <= high;
 47}
 48
 49static int snd_pcm_plugin_alloc(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames)
 50{
 51	struct snd_pcm_plugin_format *format;
 52	ssize_t width;
 53	size_t size;
 54	unsigned int channel;
 55	struct snd_pcm_plugin_channel *c;
 56
 57	if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 58		format = &plugin->src_format;
 59	} else {
 60		format = &plugin->dst_format;
 61	}
 62	if ((width = snd_pcm_format_physical_width(format->format)) < 0)
 
 63		return width;
 64	size = frames * format->channels * width;
 65	if (snd_BUG_ON(size % 8))
 66		return -ENXIO;
 67	size /= 8;
 68	if (plugin->buf_frames < frames) {
 69		kvfree(plugin->buf);
 70		plugin->buf = kvzalloc(size, GFP_KERNEL);
 71		plugin->buf_frames = frames;
 72	}
 73	if (!plugin->buf) {
 74		plugin->buf_frames = 0;
 75		return -ENOMEM;
 76	}
 77	c = plugin->buf_channels;
 78	if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
 79		for (channel = 0; channel < format->channels; channel++, c++) {
 80			c->frames = frames;
 81			c->enabled = 1;
 82			c->wanted = 0;
 83			c->area.addr = plugin->buf;
 84			c->area.first = channel * width;
 85			c->area.step = format->channels * width;
 86		}
 87	} else if (plugin->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
 88		if (snd_BUG_ON(size % format->channels))
 89			return -EINVAL;
 90		size /= format->channels;
 91		for (channel = 0; channel < format->channels; channel++, c++) {
 92			c->frames = frames;
 93			c->enabled = 1;
 94			c->wanted = 0;
 95			c->area.addr = plugin->buf + (channel * size);
 96			c->area.first = 0;
 97			c->area.step = width;
 98		}
 99	} else
100		return -EINVAL;
101	return 0;
102}
103
104int snd_pcm_plug_alloc(struct snd_pcm_substream *plug, snd_pcm_uframes_t frames)
105{
106	int err;
107	if (snd_BUG_ON(!snd_pcm_plug_first(plug)))
108		return -ENXIO;
109	if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) {
110		struct snd_pcm_plugin *plugin = snd_pcm_plug_first(plug);
111		while (plugin->next) {
112			if (plugin->dst_frames)
113				frames = plugin->dst_frames(plugin, frames);
114			if (snd_BUG_ON((snd_pcm_sframes_t)frames <= 0))
115				return -ENXIO;
116			plugin = plugin->next;
117			err = snd_pcm_plugin_alloc(plugin, frames);
118			if (err < 0)
119				return err;
120		}
121	} else {
122		struct snd_pcm_plugin *plugin = snd_pcm_plug_last(plug);
123		while (plugin->prev) {
124			if (plugin->src_frames)
125				frames = plugin->src_frames(plugin, frames);
126			if (snd_BUG_ON((snd_pcm_sframes_t)frames <= 0))
127				return -ENXIO;
128			plugin = plugin->prev;
129			err = snd_pcm_plugin_alloc(plugin, frames);
130			if (err < 0)
131				return err;
132		}
133	}
134	return 0;
135}
136
137
138snd_pcm_sframes_t snd_pcm_plugin_client_channels(struct snd_pcm_plugin *plugin,
139				       snd_pcm_uframes_t frames,
140				       struct snd_pcm_plugin_channel **channels)
141{
142	*channels = plugin->buf_channels;
143	return frames;
144}
145
146int snd_pcm_plugin_build(struct snd_pcm_substream *plug,
147			 const char *name,
148			 struct snd_pcm_plugin_format *src_format,
149			 struct snd_pcm_plugin_format *dst_format,
150			 size_t extra,
151			 struct snd_pcm_plugin **ret)
152{
153	struct snd_pcm_plugin *plugin;
154	unsigned int channels;
155	
156	if (snd_BUG_ON(!plug))
157		return -ENXIO;
158	if (snd_BUG_ON(!src_format || !dst_format))
159		return -ENXIO;
160	plugin = kzalloc(sizeof(*plugin) + extra, GFP_KERNEL);
161	if (plugin == NULL)
162		return -ENOMEM;
163	plugin->name = name;
164	plugin->plug = plug;
165	plugin->stream = snd_pcm_plug_stream(plug);
166	plugin->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
167	plugin->src_format = *src_format;
168	plugin->src_width = snd_pcm_format_physical_width(src_format->format);
169	snd_BUG_ON(plugin->src_width <= 0);
170	plugin->dst_format = *dst_format;
171	plugin->dst_width = snd_pcm_format_physical_width(dst_format->format);
172	snd_BUG_ON(plugin->dst_width <= 0);
173	if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK)
174		channels = src_format->channels;
175	else
176		channels = dst_format->channels;
177	plugin->buf_channels = kcalloc(channels, sizeof(*plugin->buf_channels), GFP_KERNEL);
178	if (plugin->buf_channels == NULL) {
179		snd_pcm_plugin_free(plugin);
180		return -ENOMEM;
181	}
182	plugin->client_channels = snd_pcm_plugin_client_channels;
183	*ret = plugin;
184	return 0;
185}
186
187int snd_pcm_plugin_free(struct snd_pcm_plugin *plugin)
188{
189	if (! plugin)
190		return 0;
191	if (plugin->private_free)
192		plugin->private_free(plugin);
193	kfree(plugin->buf_channels);
194	kvfree(plugin->buf);
195	kfree(plugin);
196	return 0;
197}
198
199snd_pcm_sframes_t snd_pcm_plug_client_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t drv_frames)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200{
201	struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
202	int stream;
203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204	if (snd_BUG_ON(!plug))
205		return -ENXIO;
206	if (drv_frames == 0)
207		return 0;
208	stream = snd_pcm_plug_stream(plug);
209	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
210		plugin = snd_pcm_plug_last(plug);
211		while (plugin && drv_frames > 0) {
212			plugin_prev = plugin->prev;
213			if (plugin->src_frames)
214				drv_frames = plugin->src_frames(plugin, drv_frames);
215			plugin = plugin_prev;
216		}
217	} else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
218		plugin = snd_pcm_plug_first(plug);
219		while (plugin && drv_frames > 0) {
220			plugin_next = plugin->next;
221			if (plugin->dst_frames)
222				drv_frames = plugin->dst_frames(plugin, drv_frames);
223			plugin = plugin_next;
224		}
225	} else
226		snd_BUG();
227	return drv_frames;
 
228}
229
230snd_pcm_sframes_t snd_pcm_plug_slave_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t clt_frames)
231{
232	struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
233	snd_pcm_sframes_t frames;
234	int stream;
235	
236	if (snd_BUG_ON(!plug))
237		return -ENXIO;
238	if (clt_frames == 0)
239		return 0;
240	frames = clt_frames;
241	stream = snd_pcm_plug_stream(plug);
242	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
243		plugin = snd_pcm_plug_first(plug);
244		while (plugin && frames > 0) {
245			plugin_next = plugin->next;
246			if (plugin->dst_frames) {
247				frames = plugin->dst_frames(plugin, frames);
248				if (frames < 0)
249					return frames;
250			}
251			plugin = plugin_next;
252		}
253	} else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
254		plugin = snd_pcm_plug_last(plug);
255		while (plugin) {
256			plugin_prev = plugin->prev;
257			if (plugin->src_frames) {
258				frames = plugin->src_frames(plugin, frames);
259				if (frames < 0)
260					return frames;
261			}
262			plugin = plugin_prev;
263		}
264	} else
265		snd_BUG();
266	return frames;
 
267}
268
269static int snd_pcm_plug_formats(const struct snd_mask *mask,
270				snd_pcm_format_t format)
271{
272	struct snd_mask formats = *mask;
273	u64 linfmts = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
274		       SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
275		       SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
276		       SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_S24_LE |
277		       SNDRV_PCM_FMTBIT_U24_BE | SNDRV_PCM_FMTBIT_S24_BE |
278		       SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_S24_3LE |
279		       SNDRV_PCM_FMTBIT_U24_3BE | SNDRV_PCM_FMTBIT_S24_3BE |
280		       SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
281		       SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE);
282	snd_mask_set(&formats, (__force int)SNDRV_PCM_FORMAT_MU_LAW);
283	
284	if (formats.bits[0] & lower_32_bits(linfmts))
285		formats.bits[0] |= lower_32_bits(linfmts);
286	if (formats.bits[1] & upper_32_bits(linfmts))
287		formats.bits[1] |= upper_32_bits(linfmts);
288	return snd_mask_test(&formats, (__force int)format);
289}
290
291static snd_pcm_format_t preferred_formats[] = {
292	SNDRV_PCM_FORMAT_S16_LE,
293	SNDRV_PCM_FORMAT_S16_BE,
294	SNDRV_PCM_FORMAT_U16_LE,
295	SNDRV_PCM_FORMAT_U16_BE,
296	SNDRV_PCM_FORMAT_S24_3LE,
297	SNDRV_PCM_FORMAT_S24_3BE,
298	SNDRV_PCM_FORMAT_U24_3LE,
299	SNDRV_PCM_FORMAT_U24_3BE,
300	SNDRV_PCM_FORMAT_S24_LE,
301	SNDRV_PCM_FORMAT_S24_BE,
302	SNDRV_PCM_FORMAT_U24_LE,
303	SNDRV_PCM_FORMAT_U24_BE,
304	SNDRV_PCM_FORMAT_S32_LE,
305	SNDRV_PCM_FORMAT_S32_BE,
306	SNDRV_PCM_FORMAT_U32_LE,
307	SNDRV_PCM_FORMAT_U32_BE,
308	SNDRV_PCM_FORMAT_S8,
309	SNDRV_PCM_FORMAT_U8
310};
311
312snd_pcm_format_t snd_pcm_plug_slave_format(snd_pcm_format_t format,
313					   const struct snd_mask *format_mask)
314{
315	int i;
316
317	if (snd_mask_test(format_mask, (__force int)format))
318		return format;
319	if (!snd_pcm_plug_formats(format_mask, format))
320		return (__force snd_pcm_format_t)-EINVAL;
321	if (snd_pcm_format_linear(format)) {
322		unsigned int width = snd_pcm_format_width(format);
323		int unsignd = snd_pcm_format_unsigned(format) > 0;
324		int big = snd_pcm_format_big_endian(format) > 0;
325		unsigned int badness, best = -1;
326		snd_pcm_format_t best_format = (__force snd_pcm_format_t)-1;
327		for (i = 0; i < ARRAY_SIZE(preferred_formats); i++) {
328			snd_pcm_format_t f = preferred_formats[i];
329			unsigned int w;
330			if (!snd_mask_test(format_mask, (__force int)f))
331				continue;
332			w = snd_pcm_format_width(f);
333			if (w >= width)
334				badness = w - width;
335			else
336				badness = width - w + 32;
337			badness += snd_pcm_format_unsigned(f) != unsignd;
338			badness += snd_pcm_format_big_endian(f) != big;
339			if (badness < best) {
340				best_format = f;
341				best = badness;
342			}
343		}
344		if ((__force int)best_format >= 0)
345			return best_format;
346		else
347			return (__force snd_pcm_format_t)-EINVAL;
348	} else {
349		switch (format) {
350		case SNDRV_PCM_FORMAT_MU_LAW:
351			for (i = 0; i < ARRAY_SIZE(preferred_formats); ++i) {
352				snd_pcm_format_t format1 = preferred_formats[i];
353				if (snd_mask_test(format_mask, (__force int)format1))
354					return format1;
355			}
356			/* fall through */
357		default:
358			return (__force snd_pcm_format_t)-EINVAL;
359		}
360	}
361}
362
363int snd_pcm_plug_format_plugins(struct snd_pcm_substream *plug,
364				struct snd_pcm_hw_params *params,
365				struct snd_pcm_hw_params *slave_params)
366{
367	struct snd_pcm_plugin_format tmpformat;
368	struct snd_pcm_plugin_format dstformat;
369	struct snd_pcm_plugin_format srcformat;
370	snd_pcm_access_t src_access, dst_access;
371	struct snd_pcm_plugin *plugin = NULL;
372	int err;
373	int stream = snd_pcm_plug_stream(plug);
374	int slave_interleaved = (params_channels(slave_params) == 1 ||
375				 params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED);
376
377	switch (stream) {
378	case SNDRV_PCM_STREAM_PLAYBACK:
379		dstformat.format = params_format(slave_params);
380		dstformat.rate = params_rate(slave_params);
381		dstformat.channels = params_channels(slave_params);
382		srcformat.format = params_format(params);
383		srcformat.rate = params_rate(params);
384		srcformat.channels = params_channels(params);
385		src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
386		dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
387						  SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
388		break;
389	case SNDRV_PCM_STREAM_CAPTURE:
390		dstformat.format = params_format(params);
391		dstformat.rate = params_rate(params);
392		dstformat.channels = params_channels(params);
393		srcformat.format = params_format(slave_params);
394		srcformat.rate = params_rate(slave_params);
395		srcformat.channels = params_channels(slave_params);
396		src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
397						  SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
398		dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
399		break;
400	default:
401		snd_BUG();
402		return -EINVAL;
403	}
404	tmpformat = srcformat;
405		
406	pdprintf("srcformat: format=%i, rate=%i, channels=%i\n", 
407		 srcformat.format,
408		 srcformat.rate,
409		 srcformat.channels);
410	pdprintf("dstformat: format=%i, rate=%i, channels=%i\n", 
411		 dstformat.format,
412		 dstformat.rate,
413		 dstformat.channels);
414
415	/* Format change (linearization) */
416	if (! rate_match(srcformat.rate, dstformat.rate) &&
417	    ! snd_pcm_format_linear(srcformat.format)) {
418		if (srcformat.format != SNDRV_PCM_FORMAT_MU_LAW)
419			return -EINVAL;
420		tmpformat.format = SNDRV_PCM_FORMAT_S16;
421		err = snd_pcm_plugin_build_mulaw(plug,
422						 &srcformat, &tmpformat,
423						 &plugin);
424		if (err < 0)
425			return err;
426		err = snd_pcm_plugin_append(plugin);
427		if (err < 0) {
428			snd_pcm_plugin_free(plugin);
429			return err;
430		}
431		srcformat = tmpformat;
432		src_access = dst_access;
433	}
434
435	/* channels reduction */
436	if (srcformat.channels > dstformat.channels) {
437		tmpformat.channels = dstformat.channels;
438		err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
439		pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
440		if (err < 0)
441			return err;
442		err = snd_pcm_plugin_append(plugin);
443		if (err < 0) {
444			snd_pcm_plugin_free(plugin);
445			return err;
446		}
447		srcformat = tmpformat;
448		src_access = dst_access;
449	}
450
451	/* rate resampling */
452	if (!rate_match(srcformat.rate, dstformat.rate)) {
453		if (srcformat.format != SNDRV_PCM_FORMAT_S16) {
454			/* convert to S16 for resampling */
455			tmpformat.format = SNDRV_PCM_FORMAT_S16;
456			err = snd_pcm_plugin_build_linear(plug,
457							  &srcformat, &tmpformat,
458							  &plugin);
459			if (err < 0)
460				return err;
461			err = snd_pcm_plugin_append(plugin);
462			if (err < 0) {
463				snd_pcm_plugin_free(plugin);
464				return err;
465			}
466			srcformat = tmpformat;
467			src_access = dst_access;
468		}
469		tmpformat.rate = dstformat.rate;
470        	err = snd_pcm_plugin_build_rate(plug,
471        					&srcformat, &tmpformat,
472						&plugin);
473		pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
474		if (err < 0)
475			return err;
476		err = snd_pcm_plugin_append(plugin);
477		if (err < 0) {
478			snd_pcm_plugin_free(plugin);
479			return err;
480		}
481		srcformat = tmpformat;
482		src_access = dst_access;
483        }
484
485	/* format change */
486	if (srcformat.format != dstformat.format) {
487		tmpformat.format = dstformat.format;
488		if (srcformat.format == SNDRV_PCM_FORMAT_MU_LAW ||
489		    tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) {
490			err = snd_pcm_plugin_build_mulaw(plug,
491							 &srcformat, &tmpformat,
492							 &plugin);
493		}
494		else if (snd_pcm_format_linear(srcformat.format) &&
495			 snd_pcm_format_linear(tmpformat.format)) {
496			err = snd_pcm_plugin_build_linear(plug,
497							  &srcformat, &tmpformat,
498							  &plugin);
499		}
500		else
501			return -EINVAL;
502		pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
503		if (err < 0)
504			return err;
505		err = snd_pcm_plugin_append(plugin);
506		if (err < 0) {
507			snd_pcm_plugin_free(plugin);
508			return err;
509		}
510		srcformat = tmpformat;
511		src_access = dst_access;
512	}
513
514	/* channels extension */
515	if (srcformat.channels < dstformat.channels) {
516		tmpformat.channels = dstformat.channels;
517		err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
518		pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
519		if (err < 0)
520			return err;
521		err = snd_pcm_plugin_append(plugin);
522		if (err < 0) {
523			snd_pcm_plugin_free(plugin);
524			return err;
525		}
526		srcformat = tmpformat;
527		src_access = dst_access;
528	}
529
530	/* de-interleave */
531	if (src_access != dst_access) {
532		err = snd_pcm_plugin_build_copy(plug,
533						&srcformat,
534						&tmpformat,
535						&plugin);
536		pdprintf("interleave change (copy: returns %i)\n", err);
537		if (err < 0)
538			return err;
539		err = snd_pcm_plugin_append(plugin);
540		if (err < 0) {
541			snd_pcm_plugin_free(plugin);
542			return err;
543		}
544	}
545
546	return 0;
547}
548
549snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(struct snd_pcm_substream *plug,
550					 char *buf,
551					 snd_pcm_uframes_t count,
552					 struct snd_pcm_plugin_channel **channels)
553{
554	struct snd_pcm_plugin *plugin;
555	struct snd_pcm_plugin_channel *v;
556	struct snd_pcm_plugin_format *format;
557	int width, nchannels, channel;
558	int stream = snd_pcm_plug_stream(plug);
559
560	if (snd_BUG_ON(!buf))
561		return -ENXIO;
562	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
563		plugin = snd_pcm_plug_first(plug);
564		format = &plugin->src_format;
565	} else {
566		plugin = snd_pcm_plug_last(plug);
567		format = &plugin->dst_format;
568	}
569	v = plugin->buf_channels;
570	*channels = v;
571	if ((width = snd_pcm_format_physical_width(format->format)) < 0)
 
572		return width;
573	nchannels = format->channels;
574	if (snd_BUG_ON(plugin->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
575		       format->channels > 1))
576		return -ENXIO;
577	for (channel = 0; channel < nchannels; channel++, v++) {
578		v->frames = count;
579		v->enabled = 1;
580		v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE);
581		v->area.addr = buf;
582		v->area.first = channel * width;
583		v->area.step = nchannels * width;
584	}
585	return count;
586}
587
588snd_pcm_sframes_t snd_pcm_plug_write_transfer(struct snd_pcm_substream *plug, struct snd_pcm_plugin_channel *src_channels, snd_pcm_uframes_t size)
589{
590	struct snd_pcm_plugin *plugin, *next;
591	struct snd_pcm_plugin_channel *dst_channels;
592	int err;
593	snd_pcm_sframes_t frames = size;
594
595	plugin = snd_pcm_plug_first(plug);
596	while (plugin) {
597		if (frames <= 0)
598			return frames;
599		if ((next = plugin->next) != NULL) {
 
600			snd_pcm_sframes_t frames1 = frames;
601			if (plugin->dst_frames) {
602				frames1 = plugin->dst_frames(plugin, frames);
603				if (frames1 <= 0)
604					return frames1;
605			}
606			if ((err = next->client_channels(next, frames1, &dst_channels)) < 0) {
 
607				return err;
608			}
609			if (err != frames1) {
610				frames = err;
611				if (plugin->src_frames) {
612					frames = plugin->src_frames(plugin, frames1);
613					if (frames <= 0)
614						return frames;
615				}
616			}
617		} else
618			dst_channels = NULL;
619		pdprintf("write plugin: %s, %li\n", plugin->name, frames);
620		if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
 
621			return frames;
622		src_channels = dst_channels;
623		plugin = next;
624	}
625	return snd_pcm_plug_client_size(plug, frames);
626}
627
628snd_pcm_sframes_t snd_pcm_plug_read_transfer(struct snd_pcm_substream *plug, struct snd_pcm_plugin_channel *dst_channels_final, snd_pcm_uframes_t size)
629{
630	struct snd_pcm_plugin *plugin, *next;
631	struct snd_pcm_plugin_channel *src_channels, *dst_channels;
632	snd_pcm_sframes_t frames = size;
633	int err;
634
635	frames = snd_pcm_plug_slave_size(plug, frames);
636	if (frames < 0)
637		return frames;
638
639	src_channels = NULL;
640	plugin = snd_pcm_plug_first(plug);
641	while (plugin && frames > 0) {
642		if ((next = plugin->next) != NULL) {
643			if ((err = plugin->client_channels(plugin, frames, &dst_channels)) < 0) {
 
 
644				return err;
645			}
646			frames = err;
647		} else {
648			dst_channels = dst_channels_final;
649		}
650		pdprintf("read plugin: %s, %li\n", plugin->name, frames);
651		if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
 
652			return frames;
653		plugin = next;
654		src_channels = dst_channels;
655	}
656	return frames;
657}
658
659int snd_pcm_area_silence(const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
660			 size_t samples, snd_pcm_format_t format)
661{
662	/* FIXME: sub byte resolution and odd dst_offset */
663	unsigned char *dst;
664	unsigned int dst_step;
665	int width;
666	const unsigned char *silence;
667	if (!dst_area->addr)
668		return 0;
669	dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
670	width = snd_pcm_format_physical_width(format);
671	if (width <= 0)
672		return -EINVAL;
673	if (dst_area->step == (unsigned int) width && width >= 8)
674		return snd_pcm_format_set_silence(format, dst, samples);
675	silence = snd_pcm_format_silence_64(format);
676	if (! silence)
677		return -EINVAL;
678	dst_step = dst_area->step / 8;
679	if (width == 4) {
680		/* Ima ADPCM */
681		int dstbit = dst_area->first % 8;
682		int dstbit_step = dst_area->step % 8;
683		while (samples-- > 0) {
684			if (dstbit)
685				*dst &= 0xf0;
686			else
687				*dst &= 0x0f;
688			dst += dst_step;
689			dstbit += dstbit_step;
690			if (dstbit == 8) {
691				dst++;
692				dstbit = 0;
693			}
694		}
695	} else {
696		width /= 8;
697		while (samples-- > 0) {
698			memcpy(dst, silence, width);
699			dst += dst_step;
700		}
701	}
702	return 0;
703}
704
705int snd_pcm_area_copy(const struct snd_pcm_channel_area *src_area, size_t src_offset,
706		      const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
707		      size_t samples, snd_pcm_format_t format)
708{
709	/* FIXME: sub byte resolution and odd dst_offset */
710	char *src, *dst;
711	int width;
712	int src_step, dst_step;
713	src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
714	if (!src_area->addr)
715		return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
716	dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
717	if (!dst_area->addr)
718		return 0;
719	width = snd_pcm_format_physical_width(format);
720	if (width <= 0)
721		return -EINVAL;
722	if (src_area->step == (unsigned int) width &&
723	    dst_area->step == (unsigned int) width && width >= 8) {
724		size_t bytes = samples * width / 8;
725		memcpy(dst, src, bytes);
726		return 0;
727	}
728	src_step = src_area->step / 8;
729	dst_step = dst_area->step / 8;
730	if (width == 4) {
731		/* Ima ADPCM */
732		int srcbit = src_area->first % 8;
733		int srcbit_step = src_area->step % 8;
734		int dstbit = dst_area->first % 8;
735		int dstbit_step = dst_area->step % 8;
736		while (samples-- > 0) {
737			unsigned char srcval;
738			if (srcbit)
739				srcval = *src & 0x0f;
740			else
741				srcval = (*src & 0xf0) >> 4;
742			if (dstbit)
743				*dst = (*dst & 0xf0) | srcval;
744			else
745				*dst = (*dst & 0x0f) | (srcval << 4);
746			src += src_step;
747			srcbit += srcbit_step;
748			if (srcbit == 8) {
749				src++;
750				srcbit = 0;
751			}
752			dst += dst_step;
753			dstbit += dstbit_step;
754			if (dstbit == 8) {
755				dst++;
756				dstbit = 0;
757			}
758		}
759	} else {
760		width /= 8;
761		while (samples-- > 0) {
762			memcpy(dst, src, width);
763			src += src_step;
764			dst += dst_step;
765		}
766	}
767	return 0;
768}
v5.14.15
  1/*
  2 *  PCM Plug-In shared (kernel/library) code
  3 *  Copyright (c) 1999 by Jaroslav Kysela <perex@perex.cz>
  4 *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
  5 *
  6 *
  7 *   This library is free software; you can redistribute it and/or modify
  8 *   it under the terms of the GNU Library General Public License as
  9 *   published by the Free Software Foundation; either version 2 of
 10 *   the License, or (at your option) any later version.
 11 *
 12 *   This program is distributed in the hope that it will be useful,
 13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 *   GNU Library General Public License for more details.
 16 *
 17 *   You should have received a copy of the GNU Library General Public
 18 *   License along with this library; if not, write to the Free Software
 19 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 20 *
 21 */
 22  
 23#if 0
 24#define PLUGIN_DEBUG
 25#endif
 26
 27#include <linux/slab.h>
 28#include <linux/time.h>
 29#include <linux/vmalloc.h>
 30#include <sound/core.h>
 31#include <sound/pcm.h>
 32#include <sound/pcm_params.h>
 33#include "pcm_plugin.h"
 34
 35#define snd_pcm_plug_first(plug) ((plug)->runtime->oss.plugin_first)
 36#define snd_pcm_plug_last(plug) ((plug)->runtime->oss.plugin_last)
 37
 38/*
 39 *  because some cards might have rates "very close", we ignore
 40 *  all "resampling" requests within +-5%
 41 */
 42static int rate_match(unsigned int src_rate, unsigned int dst_rate)
 43{
 44	unsigned int low = (src_rate * 95) / 100;
 45	unsigned int high = (src_rate * 105) / 100;
 46	return dst_rate >= low && dst_rate <= high;
 47}
 48
 49static int snd_pcm_plugin_alloc(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames)
 50{
 51	struct snd_pcm_plugin_format *format;
 52	ssize_t width;
 53	size_t size;
 54	unsigned int channel;
 55	struct snd_pcm_plugin_channel *c;
 56
 57	if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 58		format = &plugin->src_format;
 59	} else {
 60		format = &plugin->dst_format;
 61	}
 62	width = snd_pcm_format_physical_width(format->format);
 63	if (width < 0)
 64		return width;
 65	size = frames * format->channels * width;
 66	if (snd_BUG_ON(size % 8))
 67		return -ENXIO;
 68	size /= 8;
 69	if (plugin->buf_frames < frames) {
 70		kvfree(plugin->buf);
 71		plugin->buf = kvzalloc(size, GFP_KERNEL);
 72		plugin->buf_frames = frames;
 73	}
 74	if (!plugin->buf) {
 75		plugin->buf_frames = 0;
 76		return -ENOMEM;
 77	}
 78	c = plugin->buf_channels;
 79	if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
 80		for (channel = 0; channel < format->channels; channel++, c++) {
 81			c->frames = frames;
 82			c->enabled = 1;
 83			c->wanted = 0;
 84			c->area.addr = plugin->buf;
 85			c->area.first = channel * width;
 86			c->area.step = format->channels * width;
 87		}
 88	} else if (plugin->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
 89		if (snd_BUG_ON(size % format->channels))
 90			return -EINVAL;
 91		size /= format->channels;
 92		for (channel = 0; channel < format->channels; channel++, c++) {
 93			c->frames = frames;
 94			c->enabled = 1;
 95			c->wanted = 0;
 96			c->area.addr = plugin->buf + (channel * size);
 97			c->area.first = 0;
 98			c->area.step = width;
 99		}
100	} else
101		return -EINVAL;
102	return 0;
103}
104
105int snd_pcm_plug_alloc(struct snd_pcm_substream *plug, snd_pcm_uframes_t frames)
106{
107	int err;
108	if (snd_BUG_ON(!snd_pcm_plug_first(plug)))
109		return -ENXIO;
110	if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) {
111		struct snd_pcm_plugin *plugin = snd_pcm_plug_first(plug);
112		while (plugin->next) {
113			if (plugin->dst_frames)
114				frames = plugin->dst_frames(plugin, frames);
115			if ((snd_pcm_sframes_t)frames <= 0)
116				return -ENXIO;
117			plugin = plugin->next;
118			err = snd_pcm_plugin_alloc(plugin, frames);
119			if (err < 0)
120				return err;
121		}
122	} else {
123		struct snd_pcm_plugin *plugin = snd_pcm_plug_last(plug);
124		while (plugin->prev) {
125			if (plugin->src_frames)
126				frames = plugin->src_frames(plugin, frames);
127			if ((snd_pcm_sframes_t)frames <= 0)
128				return -ENXIO;
129			plugin = plugin->prev;
130			err = snd_pcm_plugin_alloc(plugin, frames);
131			if (err < 0)
132				return err;
133		}
134	}
135	return 0;
136}
137
138
139snd_pcm_sframes_t snd_pcm_plugin_client_channels(struct snd_pcm_plugin *plugin,
140				       snd_pcm_uframes_t frames,
141				       struct snd_pcm_plugin_channel **channels)
142{
143	*channels = plugin->buf_channels;
144	return frames;
145}
146
147int snd_pcm_plugin_build(struct snd_pcm_substream *plug,
148			 const char *name,
149			 struct snd_pcm_plugin_format *src_format,
150			 struct snd_pcm_plugin_format *dst_format,
151			 size_t extra,
152			 struct snd_pcm_plugin **ret)
153{
154	struct snd_pcm_plugin *plugin;
155	unsigned int channels;
156	
157	if (snd_BUG_ON(!plug))
158		return -ENXIO;
159	if (snd_BUG_ON(!src_format || !dst_format))
160		return -ENXIO;
161	plugin = kzalloc(sizeof(*plugin) + extra, GFP_KERNEL);
162	if (plugin == NULL)
163		return -ENOMEM;
164	plugin->name = name;
165	plugin->plug = plug;
166	plugin->stream = snd_pcm_plug_stream(plug);
167	plugin->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
168	plugin->src_format = *src_format;
169	plugin->src_width = snd_pcm_format_physical_width(src_format->format);
170	snd_BUG_ON(plugin->src_width <= 0);
171	plugin->dst_format = *dst_format;
172	plugin->dst_width = snd_pcm_format_physical_width(dst_format->format);
173	snd_BUG_ON(plugin->dst_width <= 0);
174	if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK)
175		channels = src_format->channels;
176	else
177		channels = dst_format->channels;
178	plugin->buf_channels = kcalloc(channels, sizeof(*plugin->buf_channels), GFP_KERNEL);
179	if (plugin->buf_channels == NULL) {
180		snd_pcm_plugin_free(plugin);
181		return -ENOMEM;
182	}
183	plugin->client_channels = snd_pcm_plugin_client_channels;
184	*ret = plugin;
185	return 0;
186}
187
188int snd_pcm_plugin_free(struct snd_pcm_plugin *plugin)
189{
190	if (! plugin)
191		return 0;
192	if (plugin->private_free)
193		plugin->private_free(plugin);
194	kfree(plugin->buf_channels);
195	kvfree(plugin->buf);
196	kfree(plugin);
197	return 0;
198}
199
200static snd_pcm_sframes_t calc_dst_frames(struct snd_pcm_substream *plug,
201					 snd_pcm_sframes_t frames,
202					 bool check_size)
203{
204	struct snd_pcm_plugin *plugin, *plugin_next;
205
206	plugin = snd_pcm_plug_first(plug);
207	while (plugin && frames > 0) {
208		plugin_next = plugin->next;
209		if (check_size && plugin->buf_frames &&
210		    frames > plugin->buf_frames)
211			frames = plugin->buf_frames;
212		if (plugin->dst_frames) {
213			frames = plugin->dst_frames(plugin, frames);
214			if (frames < 0)
215				return frames;
216		}
217		plugin = plugin_next;
218	}
219	return frames;
220}
221
222static snd_pcm_sframes_t calc_src_frames(struct snd_pcm_substream *plug,
223					 snd_pcm_sframes_t frames,
224					 bool check_size)
225{
226	struct snd_pcm_plugin *plugin, *plugin_prev;
 
227
228	plugin = snd_pcm_plug_last(plug);
229	while (plugin && frames > 0) {
230		plugin_prev = plugin->prev;
231		if (plugin->src_frames) {
232			frames = plugin->src_frames(plugin, frames);
233			if (frames < 0)
234				return frames;
235		}
236		if (check_size && plugin->buf_frames &&
237		    frames > plugin->buf_frames)
238			frames = plugin->buf_frames;
239		plugin = plugin_prev;
240	}
241	return frames;
242}
243
244snd_pcm_sframes_t snd_pcm_plug_client_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t drv_frames)
245{
246	if (snd_BUG_ON(!plug))
247		return -ENXIO;
248	switch (snd_pcm_plug_stream(plug)) {
249	case SNDRV_PCM_STREAM_PLAYBACK:
250		return calc_src_frames(plug, drv_frames, false);
251	case SNDRV_PCM_STREAM_CAPTURE:
252		return calc_dst_frames(plug, drv_frames, false);
253	default:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254		snd_BUG();
255		return -EINVAL;
256	}
257}
258
259snd_pcm_sframes_t snd_pcm_plug_slave_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t clt_frames)
260{
 
 
 
 
261	if (snd_BUG_ON(!plug))
262		return -ENXIO;
263	switch (snd_pcm_plug_stream(plug)) {
264	case SNDRV_PCM_STREAM_PLAYBACK:
265		return calc_dst_frames(plug, clt_frames, false);
266	case SNDRV_PCM_STREAM_CAPTURE:
267		return calc_src_frames(plug, clt_frames, false);
268	default:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269		snd_BUG();
270		return -EINVAL;
271	}
272}
273
274static int snd_pcm_plug_formats(const struct snd_mask *mask,
275				snd_pcm_format_t format)
276{
277	struct snd_mask formats = *mask;
278	u64 linfmts = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
279		       SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
280		       SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
281		       SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_S24_LE |
282		       SNDRV_PCM_FMTBIT_U24_BE | SNDRV_PCM_FMTBIT_S24_BE |
283		       SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_S24_3LE |
284		       SNDRV_PCM_FMTBIT_U24_3BE | SNDRV_PCM_FMTBIT_S24_3BE |
285		       SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
286		       SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE);
287	snd_mask_set(&formats, (__force int)SNDRV_PCM_FORMAT_MU_LAW);
288	
289	if (formats.bits[0] & lower_32_bits(linfmts))
290		formats.bits[0] |= lower_32_bits(linfmts);
291	if (formats.bits[1] & upper_32_bits(linfmts))
292		formats.bits[1] |= upper_32_bits(linfmts);
293	return snd_mask_test(&formats, (__force int)format);
294}
295
296static const snd_pcm_format_t preferred_formats[] = {
297	SNDRV_PCM_FORMAT_S16_LE,
298	SNDRV_PCM_FORMAT_S16_BE,
299	SNDRV_PCM_FORMAT_U16_LE,
300	SNDRV_PCM_FORMAT_U16_BE,
301	SNDRV_PCM_FORMAT_S24_3LE,
302	SNDRV_PCM_FORMAT_S24_3BE,
303	SNDRV_PCM_FORMAT_U24_3LE,
304	SNDRV_PCM_FORMAT_U24_3BE,
305	SNDRV_PCM_FORMAT_S24_LE,
306	SNDRV_PCM_FORMAT_S24_BE,
307	SNDRV_PCM_FORMAT_U24_LE,
308	SNDRV_PCM_FORMAT_U24_BE,
309	SNDRV_PCM_FORMAT_S32_LE,
310	SNDRV_PCM_FORMAT_S32_BE,
311	SNDRV_PCM_FORMAT_U32_LE,
312	SNDRV_PCM_FORMAT_U32_BE,
313	SNDRV_PCM_FORMAT_S8,
314	SNDRV_PCM_FORMAT_U8
315};
316
317snd_pcm_format_t snd_pcm_plug_slave_format(snd_pcm_format_t format,
318					   const struct snd_mask *format_mask)
319{
320	int i;
321
322	if (snd_mask_test(format_mask, (__force int)format))
323		return format;
324	if (!snd_pcm_plug_formats(format_mask, format))
325		return (__force snd_pcm_format_t)-EINVAL;
326	if (snd_pcm_format_linear(format)) {
327		unsigned int width = snd_pcm_format_width(format);
328		int unsignd = snd_pcm_format_unsigned(format) > 0;
329		int big = snd_pcm_format_big_endian(format) > 0;
330		unsigned int badness, best = -1;
331		snd_pcm_format_t best_format = (__force snd_pcm_format_t)-1;
332		for (i = 0; i < ARRAY_SIZE(preferred_formats); i++) {
333			snd_pcm_format_t f = preferred_formats[i];
334			unsigned int w;
335			if (!snd_mask_test(format_mask, (__force int)f))
336				continue;
337			w = snd_pcm_format_width(f);
338			if (w >= width)
339				badness = w - width;
340			else
341				badness = width - w + 32;
342			badness += snd_pcm_format_unsigned(f) != unsignd;
343			badness += snd_pcm_format_big_endian(f) != big;
344			if (badness < best) {
345				best_format = f;
346				best = badness;
347			}
348		}
349		if ((__force int)best_format >= 0)
350			return best_format;
351		else
352			return (__force snd_pcm_format_t)-EINVAL;
353	} else {
354		switch (format) {
355		case SNDRV_PCM_FORMAT_MU_LAW:
356			for (i = 0; i < ARRAY_SIZE(preferred_formats); ++i) {
357				snd_pcm_format_t format1 = preferred_formats[i];
358				if (snd_mask_test(format_mask, (__force int)format1))
359					return format1;
360			}
361			fallthrough;
362		default:
363			return (__force snd_pcm_format_t)-EINVAL;
364		}
365	}
366}
367
368int snd_pcm_plug_format_plugins(struct snd_pcm_substream *plug,
369				struct snd_pcm_hw_params *params,
370				struct snd_pcm_hw_params *slave_params)
371{
372	struct snd_pcm_plugin_format tmpformat;
373	struct snd_pcm_plugin_format dstformat;
374	struct snd_pcm_plugin_format srcformat;
375	snd_pcm_access_t src_access, dst_access;
376	struct snd_pcm_plugin *plugin = NULL;
377	int err;
378	int stream = snd_pcm_plug_stream(plug);
379	int slave_interleaved = (params_channels(slave_params) == 1 ||
380				 params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED);
381
382	switch (stream) {
383	case SNDRV_PCM_STREAM_PLAYBACK:
384		dstformat.format = params_format(slave_params);
385		dstformat.rate = params_rate(slave_params);
386		dstformat.channels = params_channels(slave_params);
387		srcformat.format = params_format(params);
388		srcformat.rate = params_rate(params);
389		srcformat.channels = params_channels(params);
390		src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
391		dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
392						  SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
393		break;
394	case SNDRV_PCM_STREAM_CAPTURE:
395		dstformat.format = params_format(params);
396		dstformat.rate = params_rate(params);
397		dstformat.channels = params_channels(params);
398		srcformat.format = params_format(slave_params);
399		srcformat.rate = params_rate(slave_params);
400		srcformat.channels = params_channels(slave_params);
401		src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
402						  SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
403		dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
404		break;
405	default:
406		snd_BUG();
407		return -EINVAL;
408	}
409	tmpformat = srcformat;
410		
411	pdprintf("srcformat: format=%i, rate=%i, channels=%i\n", 
412		 srcformat.format,
413		 srcformat.rate,
414		 srcformat.channels);
415	pdprintf("dstformat: format=%i, rate=%i, channels=%i\n", 
416		 dstformat.format,
417		 dstformat.rate,
418		 dstformat.channels);
419
420	/* Format change (linearization) */
421	if (! rate_match(srcformat.rate, dstformat.rate) &&
422	    ! snd_pcm_format_linear(srcformat.format)) {
423		if (srcformat.format != SNDRV_PCM_FORMAT_MU_LAW)
424			return -EINVAL;
425		tmpformat.format = SNDRV_PCM_FORMAT_S16;
426		err = snd_pcm_plugin_build_mulaw(plug,
427						 &srcformat, &tmpformat,
428						 &plugin);
429		if (err < 0)
430			return err;
431		err = snd_pcm_plugin_append(plugin);
432		if (err < 0) {
433			snd_pcm_plugin_free(plugin);
434			return err;
435		}
436		srcformat = tmpformat;
437		src_access = dst_access;
438	}
439
440	/* channels reduction */
441	if (srcformat.channels > dstformat.channels) {
442		tmpformat.channels = dstformat.channels;
443		err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
444		pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
445		if (err < 0)
446			return err;
447		err = snd_pcm_plugin_append(plugin);
448		if (err < 0) {
449			snd_pcm_plugin_free(plugin);
450			return err;
451		}
452		srcformat = tmpformat;
453		src_access = dst_access;
454	}
455
456	/* rate resampling */
457	if (!rate_match(srcformat.rate, dstformat.rate)) {
458		if (srcformat.format != SNDRV_PCM_FORMAT_S16) {
459			/* convert to S16 for resampling */
460			tmpformat.format = SNDRV_PCM_FORMAT_S16;
461			err = snd_pcm_plugin_build_linear(plug,
462							  &srcformat, &tmpformat,
463							  &plugin);
464			if (err < 0)
465				return err;
466			err = snd_pcm_plugin_append(plugin);
467			if (err < 0) {
468				snd_pcm_plugin_free(plugin);
469				return err;
470			}
471			srcformat = tmpformat;
472			src_access = dst_access;
473		}
474		tmpformat.rate = dstformat.rate;
475        	err = snd_pcm_plugin_build_rate(plug,
476        					&srcformat, &tmpformat,
477						&plugin);
478		pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
479		if (err < 0)
480			return err;
481		err = snd_pcm_plugin_append(plugin);
482		if (err < 0) {
483			snd_pcm_plugin_free(plugin);
484			return err;
485		}
486		srcformat = tmpformat;
487		src_access = dst_access;
488        }
489
490	/* format change */
491	if (srcformat.format != dstformat.format) {
492		tmpformat.format = dstformat.format;
493		if (srcformat.format == SNDRV_PCM_FORMAT_MU_LAW ||
494		    tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) {
495			err = snd_pcm_plugin_build_mulaw(plug,
496							 &srcformat, &tmpformat,
497							 &plugin);
498		}
499		else if (snd_pcm_format_linear(srcformat.format) &&
500			 snd_pcm_format_linear(tmpformat.format)) {
501			err = snd_pcm_plugin_build_linear(plug,
502							  &srcformat, &tmpformat,
503							  &plugin);
504		}
505		else
506			return -EINVAL;
507		pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
508		if (err < 0)
509			return err;
510		err = snd_pcm_plugin_append(plugin);
511		if (err < 0) {
512			snd_pcm_plugin_free(plugin);
513			return err;
514		}
515		srcformat = tmpformat;
516		src_access = dst_access;
517	}
518
519	/* channels extension */
520	if (srcformat.channels < dstformat.channels) {
521		tmpformat.channels = dstformat.channels;
522		err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
523		pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
524		if (err < 0)
525			return err;
526		err = snd_pcm_plugin_append(plugin);
527		if (err < 0) {
528			snd_pcm_plugin_free(plugin);
529			return err;
530		}
531		srcformat = tmpformat;
532		src_access = dst_access;
533	}
534
535	/* de-interleave */
536	if (src_access != dst_access) {
537		err = snd_pcm_plugin_build_copy(plug,
538						&srcformat,
539						&tmpformat,
540						&plugin);
541		pdprintf("interleave change (copy: returns %i)\n", err);
542		if (err < 0)
543			return err;
544		err = snd_pcm_plugin_append(plugin);
545		if (err < 0) {
546			snd_pcm_plugin_free(plugin);
547			return err;
548		}
549	}
550
551	return 0;
552}
553
554snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(struct snd_pcm_substream *plug,
555					 char *buf,
556					 snd_pcm_uframes_t count,
557					 struct snd_pcm_plugin_channel **channels)
558{
559	struct snd_pcm_plugin *plugin;
560	struct snd_pcm_plugin_channel *v;
561	struct snd_pcm_plugin_format *format;
562	int width, nchannels, channel;
563	int stream = snd_pcm_plug_stream(plug);
564
565	if (snd_BUG_ON(!buf))
566		return -ENXIO;
567	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
568		plugin = snd_pcm_plug_first(plug);
569		format = &plugin->src_format;
570	} else {
571		plugin = snd_pcm_plug_last(plug);
572		format = &plugin->dst_format;
573	}
574	v = plugin->buf_channels;
575	*channels = v;
576	width = snd_pcm_format_physical_width(format->format);
577	if (width < 0)
578		return width;
579	nchannels = format->channels;
580	if (snd_BUG_ON(plugin->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
581		       format->channels > 1))
582		return -ENXIO;
583	for (channel = 0; channel < nchannels; channel++, v++) {
584		v->frames = count;
585		v->enabled = 1;
586		v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE);
587		v->area.addr = buf;
588		v->area.first = channel * width;
589		v->area.step = nchannels * width;
590	}
591	return count;
592}
593
594snd_pcm_sframes_t snd_pcm_plug_write_transfer(struct snd_pcm_substream *plug, struct snd_pcm_plugin_channel *src_channels, snd_pcm_uframes_t size)
595{
596	struct snd_pcm_plugin *plugin, *next;
597	struct snd_pcm_plugin_channel *dst_channels;
598	int err;
599	snd_pcm_sframes_t frames = size;
600
601	plugin = snd_pcm_plug_first(plug);
602	while (plugin) {
603		if (frames <= 0)
604			return frames;
605		next = plugin->next;
606		if (next) {
607			snd_pcm_sframes_t frames1 = frames;
608			if (plugin->dst_frames) {
609				frames1 = plugin->dst_frames(plugin, frames);
610				if (frames1 <= 0)
611					return frames1;
612			}
613			err = next->client_channels(next, frames1, &dst_channels);
614			if (err < 0)
615				return err;
 
616			if (err != frames1) {
617				frames = err;
618				if (plugin->src_frames) {
619					frames = plugin->src_frames(plugin, frames1);
620					if (frames <= 0)
621						return frames;
622				}
623			}
624		} else
625			dst_channels = NULL;
626		pdprintf("write plugin: %s, %li\n", plugin->name, frames);
627		frames = plugin->transfer(plugin, src_channels, dst_channels, frames);
628		if (frames < 0)
629			return frames;
630		src_channels = dst_channels;
631		plugin = next;
632	}
633	return calc_src_frames(plug, frames, true);
634}
635
636snd_pcm_sframes_t snd_pcm_plug_read_transfer(struct snd_pcm_substream *plug, struct snd_pcm_plugin_channel *dst_channels_final, snd_pcm_uframes_t size)
637{
638	struct snd_pcm_plugin *plugin, *next;
639	struct snd_pcm_plugin_channel *src_channels, *dst_channels;
640	snd_pcm_sframes_t frames = size;
641	int err;
642
643	frames = calc_src_frames(plug, frames, true);
644	if (frames < 0)
645		return frames;
646
647	src_channels = NULL;
648	plugin = snd_pcm_plug_first(plug);
649	while (plugin && frames > 0) {
650		next = plugin->next;
651		if (next) {
652			err = plugin->client_channels(plugin, frames, &dst_channels);
653			if (err < 0)
654				return err;
 
655			frames = err;
656		} else {
657			dst_channels = dst_channels_final;
658		}
659		pdprintf("read plugin: %s, %li\n", plugin->name, frames);
660		frames = plugin->transfer(plugin, src_channels, dst_channels, frames);
661		if (frames < 0)
662			return frames;
663		plugin = next;
664		src_channels = dst_channels;
665	}
666	return frames;
667}
668
669int snd_pcm_area_silence(const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
670			 size_t samples, snd_pcm_format_t format)
671{
672	/* FIXME: sub byte resolution and odd dst_offset */
673	unsigned char *dst;
674	unsigned int dst_step;
675	int width;
676	const unsigned char *silence;
677	if (!dst_area->addr)
678		return 0;
679	dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
680	width = snd_pcm_format_physical_width(format);
681	if (width <= 0)
682		return -EINVAL;
683	if (dst_area->step == (unsigned int) width && width >= 8)
684		return snd_pcm_format_set_silence(format, dst, samples);
685	silence = snd_pcm_format_silence_64(format);
686	if (! silence)
687		return -EINVAL;
688	dst_step = dst_area->step / 8;
689	if (width == 4) {
690		/* Ima ADPCM */
691		int dstbit = dst_area->first % 8;
692		int dstbit_step = dst_area->step % 8;
693		while (samples-- > 0) {
694			if (dstbit)
695				*dst &= 0xf0;
696			else
697				*dst &= 0x0f;
698			dst += dst_step;
699			dstbit += dstbit_step;
700			if (dstbit == 8) {
701				dst++;
702				dstbit = 0;
703			}
704		}
705	} else {
706		width /= 8;
707		while (samples-- > 0) {
708			memcpy(dst, silence, width);
709			dst += dst_step;
710		}
711	}
712	return 0;
713}
714
715int snd_pcm_area_copy(const struct snd_pcm_channel_area *src_area, size_t src_offset,
716		      const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
717		      size_t samples, snd_pcm_format_t format)
718{
719	/* FIXME: sub byte resolution and odd dst_offset */
720	char *src, *dst;
721	int width;
722	int src_step, dst_step;
723	src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
724	if (!src_area->addr)
725		return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
726	dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
727	if (!dst_area->addr)
728		return 0;
729	width = snd_pcm_format_physical_width(format);
730	if (width <= 0)
731		return -EINVAL;
732	if (src_area->step == (unsigned int) width &&
733	    dst_area->step == (unsigned int) width && width >= 8) {
734		size_t bytes = samples * width / 8;
735		memcpy(dst, src, bytes);
736		return 0;
737	}
738	src_step = src_area->step / 8;
739	dst_step = dst_area->step / 8;
740	if (width == 4) {
741		/* Ima ADPCM */
742		int srcbit = src_area->first % 8;
743		int srcbit_step = src_area->step % 8;
744		int dstbit = dst_area->first % 8;
745		int dstbit_step = dst_area->step % 8;
746		while (samples-- > 0) {
747			unsigned char srcval;
748			if (srcbit)
749				srcval = *src & 0x0f;
750			else
751				srcval = (*src & 0xf0) >> 4;
752			if (dstbit)
753				*dst = (*dst & 0xf0) | srcval;
754			else
755				*dst = (*dst & 0x0f) | (srcval << 4);
756			src += src_step;
757			srcbit += srcbit_step;
758			if (srcbit == 8) {
759				src++;
760				srcbit = 0;
761			}
762			dst += dst_step;
763			dstbit += dstbit_step;
764			if (dstbit == 8) {
765				dst++;
766				dstbit = 0;
767			}
768		}
769	} else {
770		width /= 8;
771		while (samples-- > 0) {
772			memcpy(dst, src, width);
773			src += src_step;
774			dst += dst_step;
775		}
776	}
777	return 0;
778}