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