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