Linux Audio

Check our new training course

Yocto / OpenEmbedded training

Mar 24-27, 2025, special US time zones
Register
Loading...
v4.10.11
  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		vfree(plugin->buf);
 70		plugin->buf = vmalloc(size);
 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(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(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	vfree(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(struct snd_mask *mask, snd_pcm_format_t format)
 
270{
271	struct snd_mask formats = *mask;
272	u64 linfmts = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
273		       SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
274		       SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
275		       SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_S24_LE |
276		       SNDRV_PCM_FMTBIT_U24_BE | SNDRV_PCM_FMTBIT_S24_BE |
277		       SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_S24_3LE |
278		       SNDRV_PCM_FMTBIT_U24_3BE | SNDRV_PCM_FMTBIT_S24_3BE |
279		       SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
280		       SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE);
281	snd_mask_set(&formats, (__force int)SNDRV_PCM_FORMAT_MU_LAW);
282	
283	if (formats.bits[0] & (u32)linfmts)
284		formats.bits[0] |= (u32)linfmts;
285	if (formats.bits[1] & (u32)(linfmts >> 32))
286		formats.bits[1] |= (u32)(linfmts >> 32);
287	return snd_mask_test(&formats, (__force int)format);
288}
289
290static snd_pcm_format_t preferred_formats[] = {
291	SNDRV_PCM_FORMAT_S16_LE,
292	SNDRV_PCM_FORMAT_S16_BE,
293	SNDRV_PCM_FORMAT_U16_LE,
294	SNDRV_PCM_FORMAT_U16_BE,
295	SNDRV_PCM_FORMAT_S24_3LE,
296	SNDRV_PCM_FORMAT_S24_3BE,
297	SNDRV_PCM_FORMAT_U24_3LE,
298	SNDRV_PCM_FORMAT_U24_3BE,
299	SNDRV_PCM_FORMAT_S24_LE,
300	SNDRV_PCM_FORMAT_S24_BE,
301	SNDRV_PCM_FORMAT_U24_LE,
302	SNDRV_PCM_FORMAT_U24_BE,
303	SNDRV_PCM_FORMAT_S32_LE,
304	SNDRV_PCM_FORMAT_S32_BE,
305	SNDRV_PCM_FORMAT_U32_LE,
306	SNDRV_PCM_FORMAT_U32_BE,
307	SNDRV_PCM_FORMAT_S8,
308	SNDRV_PCM_FORMAT_U8
309};
310
311snd_pcm_format_t snd_pcm_plug_slave_format(snd_pcm_format_t format,
312					   struct snd_mask *format_mask)
313{
314	int i;
315
316	if (snd_mask_test(format_mask, (__force int)format))
317		return format;
318	if (!snd_pcm_plug_formats(format_mask, format))
319		return (__force snd_pcm_format_t)-EINVAL;
320	if (snd_pcm_format_linear(format)) {
321		unsigned int width = snd_pcm_format_width(format);
322		int unsignd = snd_pcm_format_unsigned(format) > 0;
323		int big = snd_pcm_format_big_endian(format) > 0;
324		unsigned int badness, best = -1;
325		snd_pcm_format_t best_format = (__force snd_pcm_format_t)-1;
326		for (i = 0; i < ARRAY_SIZE(preferred_formats); i++) {
327			snd_pcm_format_t f = preferred_formats[i];
328			unsigned int w;
329			if (!snd_mask_test(format_mask, (__force int)f))
330				continue;
331			w = snd_pcm_format_width(f);
332			if (w >= width)
333				badness = w - width;
334			else
335				badness = width - w + 32;
336			badness += snd_pcm_format_unsigned(f) != unsignd;
337			badness += snd_pcm_format_big_endian(f) != big;
338			if (badness < best) {
339				best_format = f;
340				best = badness;
341			}
342		}
343		if ((__force int)best_format >= 0)
344			return best_format;
345		else
346			return (__force snd_pcm_format_t)-EINVAL;
347	} else {
348		switch (format) {
349		case SNDRV_PCM_FORMAT_MU_LAW:
350			for (i = 0; i < ARRAY_SIZE(preferred_formats); ++i) {
351				snd_pcm_format_t format1 = preferred_formats[i];
352				if (snd_mask_test(format_mask, (__force int)format1))
353					return format1;
354			}
355		default:
356			return (__force snd_pcm_format_t)-EINVAL;
357		}
358	}
359}
360
361int snd_pcm_plug_format_plugins(struct snd_pcm_substream *plug,
362				struct snd_pcm_hw_params *params,
363				struct snd_pcm_hw_params *slave_params)
364{
365	struct snd_pcm_plugin_format tmpformat;
366	struct snd_pcm_plugin_format dstformat;
367	struct snd_pcm_plugin_format srcformat;
368	snd_pcm_access_t src_access, dst_access;
369	struct snd_pcm_plugin *plugin = NULL;
370	int err;
371	int stream = snd_pcm_plug_stream(plug);
372	int slave_interleaved = (params_channels(slave_params) == 1 ||
373				 params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED);
374
375	switch (stream) {
376	case SNDRV_PCM_STREAM_PLAYBACK:
377		dstformat.format = params_format(slave_params);
378		dstformat.rate = params_rate(slave_params);
379		dstformat.channels = params_channels(slave_params);
380		srcformat.format = params_format(params);
381		srcformat.rate = params_rate(params);
382		srcformat.channels = params_channels(params);
383		src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
384		dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
385						  SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
386		break;
387	case SNDRV_PCM_STREAM_CAPTURE:
388		dstformat.format = params_format(params);
389		dstformat.rate = params_rate(params);
390		dstformat.channels = params_channels(params);
391		srcformat.format = params_format(slave_params);
392		srcformat.rate = params_rate(slave_params);
393		srcformat.channels = params_channels(slave_params);
394		src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
395						  SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
396		dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
397		break;
398	default:
399		snd_BUG();
400		return -EINVAL;
401	}
402	tmpformat = srcformat;
403		
404	pdprintf("srcformat: format=%i, rate=%i, channels=%i\n", 
405		 srcformat.format,
406		 srcformat.rate,
407		 srcformat.channels);
408	pdprintf("dstformat: format=%i, rate=%i, channels=%i\n", 
409		 dstformat.format,
410		 dstformat.rate,
411		 dstformat.channels);
412
413	/* Format change (linearization) */
414	if (! rate_match(srcformat.rate, dstformat.rate) &&
415	    ! snd_pcm_format_linear(srcformat.format)) {
416		if (srcformat.format != SNDRV_PCM_FORMAT_MU_LAW)
417			return -EINVAL;
418		tmpformat.format = SNDRV_PCM_FORMAT_S16;
419		err = snd_pcm_plugin_build_mulaw(plug,
420						 &srcformat, &tmpformat,
421						 &plugin);
422		if (err < 0)
423			return err;
424		err = snd_pcm_plugin_append(plugin);
425		if (err < 0) {
426			snd_pcm_plugin_free(plugin);
427			return err;
428		}
429		srcformat = tmpformat;
430		src_access = dst_access;
431	}
432
433	/* channels reduction */
434	if (srcformat.channels > dstformat.channels) {
435		tmpformat.channels = dstformat.channels;
436		err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
437		pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
438		if (err < 0)
439			return err;
440		err = snd_pcm_plugin_append(plugin);
441		if (err < 0) {
442			snd_pcm_plugin_free(plugin);
443			return err;
444		}
445		srcformat = tmpformat;
446		src_access = dst_access;
447	}
448
449	/* rate resampling */
450	if (!rate_match(srcformat.rate, dstformat.rate)) {
451		if (srcformat.format != SNDRV_PCM_FORMAT_S16) {
452			/* convert to S16 for resampling */
453			tmpformat.format = SNDRV_PCM_FORMAT_S16;
454			err = snd_pcm_plugin_build_linear(plug,
455							  &srcformat, &tmpformat,
456							  &plugin);
457			if (err < 0)
458				return err;
459			err = snd_pcm_plugin_append(plugin);
460			if (err < 0) {
461				snd_pcm_plugin_free(plugin);
462				return err;
463			}
464			srcformat = tmpformat;
465			src_access = dst_access;
466		}
467		tmpformat.rate = dstformat.rate;
468        	err = snd_pcm_plugin_build_rate(plug,
469        					&srcformat, &tmpformat,
470						&plugin);
471		pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
472		if (err < 0)
473			return err;
474		err = snd_pcm_plugin_append(plugin);
475		if (err < 0) {
476			snd_pcm_plugin_free(plugin);
477			return err;
478		}
479		srcformat = tmpformat;
480		src_access = dst_access;
481        }
482
483	/* format change */
484	if (srcformat.format != dstformat.format) {
485		tmpformat.format = dstformat.format;
486		if (srcformat.format == SNDRV_PCM_FORMAT_MU_LAW ||
487		    tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) {
488			err = snd_pcm_plugin_build_mulaw(plug,
489							 &srcformat, &tmpformat,
490							 &plugin);
491		}
492		else if (snd_pcm_format_linear(srcformat.format) &&
493			 snd_pcm_format_linear(tmpformat.format)) {
494			err = snd_pcm_plugin_build_linear(plug,
495							  &srcformat, &tmpformat,
496							  &plugin);
497		}
498		else
499			return -EINVAL;
500		pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
501		if (err < 0)
502			return err;
503		err = snd_pcm_plugin_append(plugin);
504		if (err < 0) {
505			snd_pcm_plugin_free(plugin);
506			return err;
507		}
508		srcformat = tmpformat;
509		src_access = dst_access;
510	}
511
512	/* channels extension */
513	if (srcformat.channels < dstformat.channels) {
514		tmpformat.channels = dstformat.channels;
515		err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
516		pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
517		if (err < 0)
518			return err;
519		err = snd_pcm_plugin_append(plugin);
520		if (err < 0) {
521			snd_pcm_plugin_free(plugin);
522			return err;
523		}
524		srcformat = tmpformat;
525		src_access = dst_access;
526	}
527
528	/* de-interleave */
529	if (src_access != dst_access) {
530		err = snd_pcm_plugin_build_copy(plug,
531						&srcformat,
532						&tmpformat,
533						&plugin);
534		pdprintf("interleave change (copy: returns %i)\n", err);
535		if (err < 0)
536			return err;
537		err = snd_pcm_plugin_append(plugin);
538		if (err < 0) {
539			snd_pcm_plugin_free(plugin);
540			return err;
541		}
542	}
543
544	return 0;
545}
546
547snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(struct snd_pcm_substream *plug,
548					 char *buf,
549					 snd_pcm_uframes_t count,
550					 struct snd_pcm_plugin_channel **channels)
551{
552	struct snd_pcm_plugin *plugin;
553	struct snd_pcm_plugin_channel *v;
554	struct snd_pcm_plugin_format *format;
555	int width, nchannels, channel;
556	int stream = snd_pcm_plug_stream(plug);
557
558	if (snd_BUG_ON(!buf))
559		return -ENXIO;
560	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
561		plugin = snd_pcm_plug_first(plug);
562		format = &plugin->src_format;
563	} else {
564		plugin = snd_pcm_plug_last(plug);
565		format = &plugin->dst_format;
566	}
567	v = plugin->buf_channels;
568	*channels = v;
569	if ((width = snd_pcm_format_physical_width(format->format)) < 0)
570		return width;
571	nchannels = format->channels;
572	if (snd_BUG_ON(plugin->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
573		       format->channels > 1))
574		return -ENXIO;
575	for (channel = 0; channel < nchannels; channel++, v++) {
576		v->frames = count;
577		v->enabled = 1;
578		v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE);
579		v->area.addr = buf;
580		v->area.first = channel * width;
581		v->area.step = nchannels * width;
582	}
583	return count;
584}
585
586snd_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)
587{
588	struct snd_pcm_plugin *plugin, *next;
589	struct snd_pcm_plugin_channel *dst_channels;
590	int err;
591	snd_pcm_sframes_t frames = size;
592
593	plugin = snd_pcm_plug_first(plug);
594	while (plugin && frames > 0) {
 
 
595		if ((next = plugin->next) != NULL) {
596			snd_pcm_sframes_t frames1 = frames;
597			if (plugin->dst_frames)
598				frames1 = plugin->dst_frames(plugin, frames);
 
 
 
599			if ((err = next->client_channels(next, frames1, &dst_channels)) < 0) {
600				return err;
601			}
602			if (err != frames1) {
603				frames = err;
604				if (plugin->src_frames)
605					frames = plugin->src_frames(plugin, frames1);
 
 
 
606			}
607		} else
608			dst_channels = NULL;
609		pdprintf("write plugin: %s, %li\n", plugin->name, frames);
610		if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
611			return frames;
612		src_channels = dst_channels;
613		plugin = next;
614	}
615	return snd_pcm_plug_client_size(plug, frames);
616}
617
618snd_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)
619{
620	struct snd_pcm_plugin *plugin, *next;
621	struct snd_pcm_plugin_channel *src_channels, *dst_channels;
622	snd_pcm_sframes_t frames = size;
623	int err;
624
625	frames = snd_pcm_plug_slave_size(plug, frames);
626	if (frames < 0)
627		return frames;
628
629	src_channels = NULL;
630	plugin = snd_pcm_plug_first(plug);
631	while (plugin && frames > 0) {
632		if ((next = plugin->next) != NULL) {
633			if ((err = plugin->client_channels(plugin, frames, &dst_channels)) < 0) {
634				return err;
635			}
636			frames = err;
637		} else {
638			dst_channels = dst_channels_final;
639		}
640		pdprintf("read plugin: %s, %li\n", plugin->name, frames);
641		if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
642			return frames;
643		plugin = next;
644		src_channels = dst_channels;
645	}
646	return frames;
647}
648
649int snd_pcm_area_silence(const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
650			 size_t samples, snd_pcm_format_t format)
651{
652	/* FIXME: sub byte resolution and odd dst_offset */
653	unsigned char *dst;
654	unsigned int dst_step;
655	int width;
656	const unsigned char *silence;
657	if (!dst_area->addr)
658		return 0;
659	dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
660	width = snd_pcm_format_physical_width(format);
661	if (width <= 0)
662		return -EINVAL;
663	if (dst_area->step == (unsigned int) width && width >= 8)
664		return snd_pcm_format_set_silence(format, dst, samples);
665	silence = snd_pcm_format_silence_64(format);
666	if (! silence)
667		return -EINVAL;
668	dst_step = dst_area->step / 8;
669	if (width == 4) {
670		/* Ima ADPCM */
671		int dstbit = dst_area->first % 8;
672		int dstbit_step = dst_area->step % 8;
673		while (samples-- > 0) {
674			if (dstbit)
675				*dst &= 0xf0;
676			else
677				*dst &= 0x0f;
678			dst += dst_step;
679			dstbit += dstbit_step;
680			if (dstbit == 8) {
681				dst++;
682				dstbit = 0;
683			}
684		}
685	} else {
686		width /= 8;
687		while (samples-- > 0) {
688			memcpy(dst, silence, width);
689			dst += dst_step;
690		}
691	}
692	return 0;
693}
694
695int snd_pcm_area_copy(const struct snd_pcm_channel_area *src_area, size_t src_offset,
696		      const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
697		      size_t samples, snd_pcm_format_t format)
698{
699	/* FIXME: sub byte resolution and odd dst_offset */
700	char *src, *dst;
701	int width;
702	int src_step, dst_step;
703	src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
704	if (!src_area->addr)
705		return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
706	dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
707	if (!dst_area->addr)
708		return 0;
709	width = snd_pcm_format_physical_width(format);
710	if (width <= 0)
711		return -EINVAL;
712	if (src_area->step == (unsigned int) width &&
713	    dst_area->step == (unsigned int) width && width >= 8) {
714		size_t bytes = samples * width / 8;
715		memcpy(dst, src, bytes);
716		return 0;
717	}
718	src_step = src_area->step / 8;
719	dst_step = dst_area->step / 8;
720	if (width == 4) {
721		/* Ima ADPCM */
722		int srcbit = src_area->first % 8;
723		int srcbit_step = src_area->step % 8;
724		int dstbit = dst_area->first % 8;
725		int dstbit_step = dst_area->step % 8;
726		while (samples-- > 0) {
727			unsigned char srcval;
728			if (srcbit)
729				srcval = *src & 0x0f;
730			else
731				srcval = (*src & 0xf0) >> 4;
732			if (dstbit)
733				*dst = (*dst & 0xf0) | srcval;
734			else
735				*dst = (*dst & 0x0f) | (srcval << 4);
736			src += src_step;
737			srcbit += srcbit_step;
738			if (srcbit == 8) {
739				src++;
740				srcbit = 0;
741			}
742			dst += dst_step;
743			dstbit += dstbit_step;
744			if (dstbit == 8) {
745				dst++;
746				dstbit = 0;
747			}
748		}
749	} else {
750		width /= 8;
751		while (samples-- > 0) {
752			memcpy(dst, src, width);
753			src += src_step;
754			dst += dst_step;
755		}
756	}
757	return 0;
758}
v4.17
  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		vfree(plugin->buf);
 70		plugin->buf = vmalloc(size);
 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(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(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	vfree(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] & (u32)linfmts)
285		formats.bits[0] |= (u32)linfmts;
286	if (formats.bits[1] & (u32)(linfmts >> 32))
287		formats.bits[1] |= (u32)(linfmts >> 32);
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		default:
357			return (__force snd_pcm_format_t)-EINVAL;
358		}
359	}
360}
361
362int snd_pcm_plug_format_plugins(struct snd_pcm_substream *plug,
363				struct snd_pcm_hw_params *params,
364				struct snd_pcm_hw_params *slave_params)
365{
366	struct snd_pcm_plugin_format tmpformat;
367	struct snd_pcm_plugin_format dstformat;
368	struct snd_pcm_plugin_format srcformat;
369	snd_pcm_access_t src_access, dst_access;
370	struct snd_pcm_plugin *plugin = NULL;
371	int err;
372	int stream = snd_pcm_plug_stream(plug);
373	int slave_interleaved = (params_channels(slave_params) == 1 ||
374				 params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED);
375
376	switch (stream) {
377	case SNDRV_PCM_STREAM_PLAYBACK:
378		dstformat.format = params_format(slave_params);
379		dstformat.rate = params_rate(slave_params);
380		dstformat.channels = params_channels(slave_params);
381		srcformat.format = params_format(params);
382		srcformat.rate = params_rate(params);
383		srcformat.channels = params_channels(params);
384		src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
385		dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
386						  SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
387		break;
388	case SNDRV_PCM_STREAM_CAPTURE:
389		dstformat.format = params_format(params);
390		dstformat.rate = params_rate(params);
391		dstformat.channels = params_channels(params);
392		srcformat.format = params_format(slave_params);
393		srcformat.rate = params_rate(slave_params);
394		srcformat.channels = params_channels(slave_params);
395		src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
396						  SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
397		dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
398		break;
399	default:
400		snd_BUG();
401		return -EINVAL;
402	}
403	tmpformat = srcformat;
404		
405	pdprintf("srcformat: format=%i, rate=%i, channels=%i\n", 
406		 srcformat.format,
407		 srcformat.rate,
408		 srcformat.channels);
409	pdprintf("dstformat: format=%i, rate=%i, channels=%i\n", 
410		 dstformat.format,
411		 dstformat.rate,
412		 dstformat.channels);
413
414	/* Format change (linearization) */
415	if (! rate_match(srcformat.rate, dstformat.rate) &&
416	    ! snd_pcm_format_linear(srcformat.format)) {
417		if (srcformat.format != SNDRV_PCM_FORMAT_MU_LAW)
418			return -EINVAL;
419		tmpformat.format = SNDRV_PCM_FORMAT_S16;
420		err = snd_pcm_plugin_build_mulaw(plug,
421						 &srcformat, &tmpformat,
422						 &plugin);
423		if (err < 0)
424			return err;
425		err = snd_pcm_plugin_append(plugin);
426		if (err < 0) {
427			snd_pcm_plugin_free(plugin);
428			return err;
429		}
430		srcformat = tmpformat;
431		src_access = dst_access;
432	}
433
434	/* channels reduction */
435	if (srcformat.channels > dstformat.channels) {
436		tmpformat.channels = dstformat.channels;
437		err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
438		pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
439		if (err < 0)
440			return err;
441		err = snd_pcm_plugin_append(plugin);
442		if (err < 0) {
443			snd_pcm_plugin_free(plugin);
444			return err;
445		}
446		srcformat = tmpformat;
447		src_access = dst_access;
448	}
449
450	/* rate resampling */
451	if (!rate_match(srcformat.rate, dstformat.rate)) {
452		if (srcformat.format != SNDRV_PCM_FORMAT_S16) {
453			/* convert to S16 for resampling */
454			tmpformat.format = SNDRV_PCM_FORMAT_S16;
455			err = snd_pcm_plugin_build_linear(plug,
456							  &srcformat, &tmpformat,
457							  &plugin);
458			if (err < 0)
459				return err;
460			err = snd_pcm_plugin_append(plugin);
461			if (err < 0) {
462				snd_pcm_plugin_free(plugin);
463				return err;
464			}
465			srcformat = tmpformat;
466			src_access = dst_access;
467		}
468		tmpformat.rate = dstformat.rate;
469        	err = snd_pcm_plugin_build_rate(plug,
470        					&srcformat, &tmpformat,
471						&plugin);
472		pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
473		if (err < 0)
474			return err;
475		err = snd_pcm_plugin_append(plugin);
476		if (err < 0) {
477			snd_pcm_plugin_free(plugin);
478			return err;
479		}
480		srcformat = tmpformat;
481		src_access = dst_access;
482        }
483
484	/* format change */
485	if (srcformat.format != dstformat.format) {
486		tmpformat.format = dstformat.format;
487		if (srcformat.format == SNDRV_PCM_FORMAT_MU_LAW ||
488		    tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) {
489			err = snd_pcm_plugin_build_mulaw(plug,
490							 &srcformat, &tmpformat,
491							 &plugin);
492		}
493		else if (snd_pcm_format_linear(srcformat.format) &&
494			 snd_pcm_format_linear(tmpformat.format)) {
495			err = snd_pcm_plugin_build_linear(plug,
496							  &srcformat, &tmpformat,
497							  &plugin);
498		}
499		else
500			return -EINVAL;
501		pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
502		if (err < 0)
503			return err;
504		err = snd_pcm_plugin_append(plugin);
505		if (err < 0) {
506			snd_pcm_plugin_free(plugin);
507			return err;
508		}
509		srcformat = tmpformat;
510		src_access = dst_access;
511	}
512
513	/* channels extension */
514	if (srcformat.channels < dstformat.channels) {
515		tmpformat.channels = dstformat.channels;
516		err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
517		pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
518		if (err < 0)
519			return err;
520		err = snd_pcm_plugin_append(plugin);
521		if (err < 0) {
522			snd_pcm_plugin_free(plugin);
523			return err;
524		}
525		srcformat = tmpformat;
526		src_access = dst_access;
527	}
528
529	/* de-interleave */
530	if (src_access != dst_access) {
531		err = snd_pcm_plugin_build_copy(plug,
532						&srcformat,
533						&tmpformat,
534						&plugin);
535		pdprintf("interleave change (copy: returns %i)\n", err);
536		if (err < 0)
537			return err;
538		err = snd_pcm_plugin_append(plugin);
539		if (err < 0) {
540			snd_pcm_plugin_free(plugin);
541			return err;
542		}
543	}
544
545	return 0;
546}
547
548snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(struct snd_pcm_substream *plug,
549					 char *buf,
550					 snd_pcm_uframes_t count,
551					 struct snd_pcm_plugin_channel **channels)
552{
553	struct snd_pcm_plugin *plugin;
554	struct snd_pcm_plugin_channel *v;
555	struct snd_pcm_plugin_format *format;
556	int width, nchannels, channel;
557	int stream = snd_pcm_plug_stream(plug);
558
559	if (snd_BUG_ON(!buf))
560		return -ENXIO;
561	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
562		plugin = snd_pcm_plug_first(plug);
563		format = &plugin->src_format;
564	} else {
565		plugin = snd_pcm_plug_last(plug);
566		format = &plugin->dst_format;
567	}
568	v = plugin->buf_channels;
569	*channels = v;
570	if ((width = snd_pcm_format_physical_width(format->format)) < 0)
571		return width;
572	nchannels = format->channels;
573	if (snd_BUG_ON(plugin->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
574		       format->channels > 1))
575		return -ENXIO;
576	for (channel = 0; channel < nchannels; channel++, v++) {
577		v->frames = count;
578		v->enabled = 1;
579		v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE);
580		v->area.addr = buf;
581		v->area.first = channel * width;
582		v->area.step = nchannels * width;
583	}
584	return count;
585}
586
587snd_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)
588{
589	struct snd_pcm_plugin *plugin, *next;
590	struct snd_pcm_plugin_channel *dst_channels;
591	int err;
592	snd_pcm_sframes_t frames = size;
593
594	plugin = snd_pcm_plug_first(plug);
595	while (plugin) {
596		if (frames <= 0)
597			return frames;
598		if ((next = plugin->next) != NULL) {
599			snd_pcm_sframes_t frames1 = frames;
600			if (plugin->dst_frames) {
601				frames1 = plugin->dst_frames(plugin, frames);
602				if (frames1 <= 0)
603					return frames1;
604			}
605			if ((err = next->client_channels(next, frames1, &dst_channels)) < 0) {
606				return err;
607			}
608			if (err != frames1) {
609				frames = err;
610				if (plugin->src_frames) {
611					frames = plugin->src_frames(plugin, frames1);
612					if (frames <= 0)
613						return frames;
614				}
615			}
616		} else
617			dst_channels = NULL;
618		pdprintf("write plugin: %s, %li\n", plugin->name, frames);
619		if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
620			return frames;
621		src_channels = dst_channels;
622		plugin = next;
623	}
624	return snd_pcm_plug_client_size(plug, frames);
625}
626
627snd_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)
628{
629	struct snd_pcm_plugin *plugin, *next;
630	struct snd_pcm_plugin_channel *src_channels, *dst_channels;
631	snd_pcm_sframes_t frames = size;
632	int err;
633
634	frames = snd_pcm_plug_slave_size(plug, frames);
635	if (frames < 0)
636		return frames;
637
638	src_channels = NULL;
639	plugin = snd_pcm_plug_first(plug);
640	while (plugin && frames > 0) {
641		if ((next = plugin->next) != NULL) {
642			if ((err = plugin->client_channels(plugin, frames, &dst_channels)) < 0) {
643				return err;
644			}
645			frames = err;
646		} else {
647			dst_channels = dst_channels_final;
648		}
649		pdprintf("read plugin: %s, %li\n", plugin->name, frames);
650		if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
651			return frames;
652		plugin = next;
653		src_channels = dst_channels;
654	}
655	return frames;
656}
657
658int snd_pcm_area_silence(const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
659			 size_t samples, snd_pcm_format_t format)
660{
661	/* FIXME: sub byte resolution and odd dst_offset */
662	unsigned char *dst;
663	unsigned int dst_step;
664	int width;
665	const unsigned char *silence;
666	if (!dst_area->addr)
667		return 0;
668	dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
669	width = snd_pcm_format_physical_width(format);
670	if (width <= 0)
671		return -EINVAL;
672	if (dst_area->step == (unsigned int) width && width >= 8)
673		return snd_pcm_format_set_silence(format, dst, samples);
674	silence = snd_pcm_format_silence_64(format);
675	if (! silence)
676		return -EINVAL;
677	dst_step = dst_area->step / 8;
678	if (width == 4) {
679		/* Ima ADPCM */
680		int dstbit = dst_area->first % 8;
681		int dstbit_step = dst_area->step % 8;
682		while (samples-- > 0) {
683			if (dstbit)
684				*dst &= 0xf0;
685			else
686				*dst &= 0x0f;
687			dst += dst_step;
688			dstbit += dstbit_step;
689			if (dstbit == 8) {
690				dst++;
691				dstbit = 0;
692			}
693		}
694	} else {
695		width /= 8;
696		while (samples-- > 0) {
697			memcpy(dst, silence, width);
698			dst += dst_step;
699		}
700	}
701	return 0;
702}
703
704int snd_pcm_area_copy(const struct snd_pcm_channel_area *src_area, size_t src_offset,
705		      const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
706		      size_t samples, snd_pcm_format_t format)
707{
708	/* FIXME: sub byte resolution and odd dst_offset */
709	char *src, *dst;
710	int width;
711	int src_step, dst_step;
712	src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
713	if (!src_area->addr)
714		return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
715	dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
716	if (!dst_area->addr)
717		return 0;
718	width = snd_pcm_format_physical_width(format);
719	if (width <= 0)
720		return -EINVAL;
721	if (src_area->step == (unsigned int) width &&
722	    dst_area->step == (unsigned int) width && width >= 8) {
723		size_t bytes = samples * width / 8;
724		memcpy(dst, src, bytes);
725		return 0;
726	}
727	src_step = src_area->step / 8;
728	dst_step = dst_area->step / 8;
729	if (width == 4) {
730		/* Ima ADPCM */
731		int srcbit = src_area->first % 8;
732		int srcbit_step = src_area->step % 8;
733		int dstbit = dst_area->first % 8;
734		int dstbit_step = dst_area->step % 8;
735		while (samples-- > 0) {
736			unsigned char srcval;
737			if (srcbit)
738				srcval = *src & 0x0f;
739			else
740				srcval = (*src & 0xf0) >> 4;
741			if (dstbit)
742				*dst = (*dst & 0xf0) | srcval;
743			else
744				*dst = (*dst & 0x0f) | (srcval << 4);
745			src += src_step;
746			srcbit += srcbit_step;
747			if (srcbit == 8) {
748				src++;
749				srcbit = 0;
750			}
751			dst += dst_step;
752			dstbit += dstbit_step;
753			if (dstbit == 8) {
754				dst++;
755				dstbit = 0;
756			}
757		}
758	} else {
759		width /= 8;
760		while (samples-- > 0) {
761			memcpy(dst, src, width);
762			src += src_step;
763			dst += dst_step;
764		}
765	}
766	return 0;
767}