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