cifs: update ctime and mtime during truncate
[linux/fpc-iii.git] / sound / core / oss / pcm_plugin.c
blob6a7cbad902220d825f053f0097366854420ff35e
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>
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.
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.
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
23 #if 0
24 #define PLUGIN_DEBUG
25 #endif
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"
35 #define snd_pcm_plug_first(plug) ((plug)->runtime->oss.plugin_first)
36 #define snd_pcm_plug_last(plug) ((plug)->runtime->oss.plugin_last)
39 * because some cards might have rates "very close", we ignore
40 * all "resampling" requests within +-5%
42 static int rate_match(unsigned int src_rate, unsigned int dst_rate)
44 unsigned int low = (src_rate * 95) / 100;
45 unsigned int high = (src_rate * 105) / 100;
46 return dst_rate >= low && dst_rate <= high;
49 static int snd_pcm_plugin_alloc(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames)
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;
57 if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK) {
58 format = &plugin->src_format;
59 } else {
60 format = &plugin->dst_format;
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;
73 if (!plugin->buf) {
74 plugin->buf_frames = 0;
75 return -ENOMEM;
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;
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;
99 } else
100 return -EINVAL;
101 return 0;
104 int snd_pcm_plug_alloc(struct snd_pcm_substream *plug, snd_pcm_uframes_t frames)
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;
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;
134 return 0;
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)
142 *channels = plugin->buf_channels;
143 return frames;
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)
153 struct snd_pcm_plugin *plugin;
154 unsigned int channels;
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;
182 plugin->client_channels = snd_pcm_plugin_client_channels;
183 *ret = plugin;
184 return 0;
187 int snd_pcm_plugin_free(struct snd_pcm_plugin *plugin)
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;
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)
203 struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
204 int stream;
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;
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;
233 } else
234 snd_BUG();
235 return drv_frames;
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)
242 struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
243 snd_pcm_sframes_t frames;
244 int stream;
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;
264 plugin = plugin_next;
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;
275 if (check_size && plugin->buf_frames &&
276 frames > plugin->buf_frames)
277 frames = plugin->buf_frames;
278 plugin = plugin_prev;
280 } else
281 snd_BUG();
282 return frames;
285 snd_pcm_sframes_t snd_pcm_plug_client_size(struct snd_pcm_substream *plug,
286 snd_pcm_uframes_t drv_frames)
288 return plug_client_size(plug, drv_frames, false);
291 snd_pcm_sframes_t snd_pcm_plug_slave_size(struct snd_pcm_substream *plug,
292 snd_pcm_uframes_t clt_frames)
294 return plug_slave_size(plug, clt_frames, false);
297 static int snd_pcm_plug_formats(struct snd_mask *mask, snd_pcm_format_t format)
299 struct snd_mask formats = *mask;
300 u64 linfmts = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
301 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
302 SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
303 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_S24_LE |
304 SNDRV_PCM_FMTBIT_U24_BE | SNDRV_PCM_FMTBIT_S24_BE |
305 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_S24_3LE |
306 SNDRV_PCM_FMTBIT_U24_3BE | SNDRV_PCM_FMTBIT_S24_3BE |
307 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
308 SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE);
309 snd_mask_set(&formats, (__force int)SNDRV_PCM_FORMAT_MU_LAW);
311 if (formats.bits[0] & (u32)linfmts)
312 formats.bits[0] |= (u32)linfmts;
313 if (formats.bits[1] & (u32)(linfmts >> 32))
314 formats.bits[1] |= (u32)(linfmts >> 32);
315 return snd_mask_test(&formats, (__force int)format);
318 static snd_pcm_format_t preferred_formats[] = {
319 SNDRV_PCM_FORMAT_S16_LE,
320 SNDRV_PCM_FORMAT_S16_BE,
321 SNDRV_PCM_FORMAT_U16_LE,
322 SNDRV_PCM_FORMAT_U16_BE,
323 SNDRV_PCM_FORMAT_S24_3LE,
324 SNDRV_PCM_FORMAT_S24_3BE,
325 SNDRV_PCM_FORMAT_U24_3LE,
326 SNDRV_PCM_FORMAT_U24_3BE,
327 SNDRV_PCM_FORMAT_S24_LE,
328 SNDRV_PCM_FORMAT_S24_BE,
329 SNDRV_PCM_FORMAT_U24_LE,
330 SNDRV_PCM_FORMAT_U24_BE,
331 SNDRV_PCM_FORMAT_S32_LE,
332 SNDRV_PCM_FORMAT_S32_BE,
333 SNDRV_PCM_FORMAT_U32_LE,
334 SNDRV_PCM_FORMAT_U32_BE,
335 SNDRV_PCM_FORMAT_S8,
336 SNDRV_PCM_FORMAT_U8
339 snd_pcm_format_t snd_pcm_plug_slave_format(snd_pcm_format_t format,
340 struct snd_mask *format_mask)
342 int i;
344 if (snd_mask_test(format_mask, (__force int)format))
345 return format;
346 if (!snd_pcm_plug_formats(format_mask, format))
347 return (__force snd_pcm_format_t)-EINVAL;
348 if (snd_pcm_format_linear(format)) {
349 unsigned int width = snd_pcm_format_width(format);
350 int unsignd = snd_pcm_format_unsigned(format) > 0;
351 int big = snd_pcm_format_big_endian(format) > 0;
352 unsigned int badness, best = -1;
353 snd_pcm_format_t best_format = (__force snd_pcm_format_t)-1;
354 for (i = 0; i < ARRAY_SIZE(preferred_formats); i++) {
355 snd_pcm_format_t f = preferred_formats[i];
356 unsigned int w;
357 if (!snd_mask_test(format_mask, (__force int)f))
358 continue;
359 w = snd_pcm_format_width(f);
360 if (w >= width)
361 badness = w - width;
362 else
363 badness = width - w + 32;
364 badness += snd_pcm_format_unsigned(f) != unsignd;
365 badness += snd_pcm_format_big_endian(f) != big;
366 if (badness < best) {
367 best_format = f;
368 best = badness;
371 if ((__force int)best_format >= 0)
372 return best_format;
373 else
374 return (__force snd_pcm_format_t)-EINVAL;
375 } else {
376 switch (format) {
377 case SNDRV_PCM_FORMAT_MU_LAW:
378 for (i = 0; i < ARRAY_SIZE(preferred_formats); ++i) {
379 snd_pcm_format_t format1 = preferred_formats[i];
380 if (snd_mask_test(format_mask, (__force int)format1))
381 return format1;
383 default:
384 return (__force snd_pcm_format_t)-EINVAL;
389 int snd_pcm_plug_format_plugins(struct snd_pcm_substream *plug,
390 struct snd_pcm_hw_params *params,
391 struct snd_pcm_hw_params *slave_params)
393 struct snd_pcm_plugin_format tmpformat;
394 struct snd_pcm_plugin_format dstformat;
395 struct snd_pcm_plugin_format srcformat;
396 snd_pcm_access_t src_access, dst_access;
397 struct snd_pcm_plugin *plugin = NULL;
398 int err;
399 int stream = snd_pcm_plug_stream(plug);
400 int slave_interleaved = (params_channels(slave_params) == 1 ||
401 params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED);
403 switch (stream) {
404 case SNDRV_PCM_STREAM_PLAYBACK:
405 dstformat.format = params_format(slave_params);
406 dstformat.rate = params_rate(slave_params);
407 dstformat.channels = params_channels(slave_params);
408 srcformat.format = params_format(params);
409 srcformat.rate = params_rate(params);
410 srcformat.channels = params_channels(params);
411 src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
412 dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
413 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
414 break;
415 case SNDRV_PCM_STREAM_CAPTURE:
416 dstformat.format = params_format(params);
417 dstformat.rate = params_rate(params);
418 dstformat.channels = params_channels(params);
419 srcformat.format = params_format(slave_params);
420 srcformat.rate = params_rate(slave_params);
421 srcformat.channels = params_channels(slave_params);
422 src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
423 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
424 dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
425 break;
426 default:
427 snd_BUG();
428 return -EINVAL;
430 tmpformat = srcformat;
432 pdprintf("srcformat: format=%i, rate=%i, channels=%i\n",
433 srcformat.format,
434 srcformat.rate,
435 srcformat.channels);
436 pdprintf("dstformat: format=%i, rate=%i, channels=%i\n",
437 dstformat.format,
438 dstformat.rate,
439 dstformat.channels);
441 /* Format change (linearization) */
442 if (! rate_match(srcformat.rate, dstformat.rate) &&
443 ! snd_pcm_format_linear(srcformat.format)) {
444 if (srcformat.format != SNDRV_PCM_FORMAT_MU_LAW)
445 return -EINVAL;
446 tmpformat.format = SNDRV_PCM_FORMAT_S16;
447 err = snd_pcm_plugin_build_mulaw(plug,
448 &srcformat, &tmpformat,
449 &plugin);
450 if (err < 0)
451 return err;
452 err = snd_pcm_plugin_append(plugin);
453 if (err < 0) {
454 snd_pcm_plugin_free(plugin);
455 return err;
457 srcformat = tmpformat;
458 src_access = dst_access;
461 /* channels reduction */
462 if (srcformat.channels > dstformat.channels) {
463 tmpformat.channels = dstformat.channels;
464 err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
465 pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
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;
473 srcformat = tmpformat;
474 src_access = dst_access;
477 /* rate resampling */
478 if (!rate_match(srcformat.rate, dstformat.rate)) {
479 if (srcformat.format != SNDRV_PCM_FORMAT_S16) {
480 /* convert to S16 for resampling */
481 tmpformat.format = SNDRV_PCM_FORMAT_S16;
482 err = snd_pcm_plugin_build_linear(plug,
483 &srcformat, &tmpformat,
484 &plugin);
485 if (err < 0)
486 return err;
487 err = snd_pcm_plugin_append(plugin);
488 if (err < 0) {
489 snd_pcm_plugin_free(plugin);
490 return err;
492 srcformat = tmpformat;
493 src_access = dst_access;
495 tmpformat.rate = dstformat.rate;
496 err = snd_pcm_plugin_build_rate(plug,
497 &srcformat, &tmpformat,
498 &plugin);
499 pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
500 if (err < 0)
501 return err;
502 err = snd_pcm_plugin_append(plugin);
503 if (err < 0) {
504 snd_pcm_plugin_free(plugin);
505 return err;
507 srcformat = tmpformat;
508 src_access = dst_access;
511 /* format change */
512 if (srcformat.format != dstformat.format) {
513 tmpformat.format = dstformat.format;
514 if (srcformat.format == SNDRV_PCM_FORMAT_MU_LAW ||
515 tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) {
516 err = snd_pcm_plugin_build_mulaw(plug,
517 &srcformat, &tmpformat,
518 &plugin);
520 else if (snd_pcm_format_linear(srcformat.format) &&
521 snd_pcm_format_linear(tmpformat.format)) {
522 err = snd_pcm_plugin_build_linear(plug,
523 &srcformat, &tmpformat,
524 &plugin);
526 else
527 return -EINVAL;
528 pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
529 if (err < 0)
530 return err;
531 err = snd_pcm_plugin_append(plugin);
532 if (err < 0) {
533 snd_pcm_plugin_free(plugin);
534 return err;
536 srcformat = tmpformat;
537 src_access = dst_access;
540 /* channels extension */
541 if (srcformat.channels < dstformat.channels) {
542 tmpformat.channels = dstformat.channels;
543 err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
544 pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
545 if (err < 0)
546 return err;
547 err = snd_pcm_plugin_append(plugin);
548 if (err < 0) {
549 snd_pcm_plugin_free(plugin);
550 return err;
552 srcformat = tmpformat;
553 src_access = dst_access;
556 /* de-interleave */
557 if (src_access != dst_access) {
558 err = snd_pcm_plugin_build_copy(plug,
559 &srcformat,
560 &tmpformat,
561 &plugin);
562 pdprintf("interleave change (copy: returns %i)\n", err);
563 if (err < 0)
564 return err;
565 err = snd_pcm_plugin_append(plugin);
566 if (err < 0) {
567 snd_pcm_plugin_free(plugin);
568 return err;
572 return 0;
575 snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(struct snd_pcm_substream *plug,
576 char *buf,
577 snd_pcm_uframes_t count,
578 struct snd_pcm_plugin_channel **channels)
580 struct snd_pcm_plugin *plugin;
581 struct snd_pcm_plugin_channel *v;
582 struct snd_pcm_plugin_format *format;
583 int width, nchannels, channel;
584 int stream = snd_pcm_plug_stream(plug);
586 if (snd_BUG_ON(!buf))
587 return -ENXIO;
588 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
589 plugin = snd_pcm_plug_first(plug);
590 format = &plugin->src_format;
591 } else {
592 plugin = snd_pcm_plug_last(plug);
593 format = &plugin->dst_format;
595 v = plugin->buf_channels;
596 *channels = v;
597 if ((width = snd_pcm_format_physical_width(format->format)) < 0)
598 return width;
599 nchannels = format->channels;
600 if (snd_BUG_ON(plugin->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
601 format->channels > 1))
602 return -ENXIO;
603 for (channel = 0; channel < nchannels; channel++, v++) {
604 v->frames = count;
605 v->enabled = 1;
606 v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE);
607 v->area.addr = buf;
608 v->area.first = channel * width;
609 v->area.step = nchannels * width;
611 return count;
614 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)
616 struct snd_pcm_plugin *plugin, *next;
617 struct snd_pcm_plugin_channel *dst_channels;
618 int err;
619 snd_pcm_sframes_t frames = size;
621 plugin = snd_pcm_plug_first(plug);
622 while (plugin) {
623 if (frames <= 0)
624 return frames;
625 if ((next = plugin->next) != NULL) {
626 snd_pcm_sframes_t frames1 = frames;
627 if (plugin->dst_frames) {
628 frames1 = plugin->dst_frames(plugin, frames);
629 if (frames1 <= 0)
630 return frames1;
632 if ((err = next->client_channels(next, frames1, &dst_channels)) < 0) {
633 return err;
635 if (err != frames1) {
636 frames = err;
637 if (plugin->src_frames) {
638 frames = plugin->src_frames(plugin, frames1);
639 if (frames <= 0)
640 return frames;
643 } else
644 dst_channels = NULL;
645 pdprintf("write plugin: %s, %li\n", plugin->name, frames);
646 if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
647 return frames;
648 src_channels = dst_channels;
649 plugin = next;
651 return plug_client_size(plug, frames, true);
654 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)
656 struct snd_pcm_plugin *plugin, *next;
657 struct snd_pcm_plugin_channel *src_channels, *dst_channels;
658 snd_pcm_sframes_t frames = size;
659 int err;
661 frames = plug_slave_size(plug, frames, true);
662 if (frames < 0)
663 return frames;
665 src_channels = NULL;
666 plugin = snd_pcm_plug_first(plug);
667 while (plugin && frames > 0) {
668 if ((next = plugin->next) != NULL) {
669 if ((err = plugin->client_channels(plugin, frames, &dst_channels)) < 0) {
670 return err;
672 frames = err;
673 } else {
674 dst_channels = dst_channels_final;
676 pdprintf("read plugin: %s, %li\n", plugin->name, frames);
677 if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
678 return frames;
679 plugin = next;
680 src_channels = dst_channels;
682 return frames;
685 int snd_pcm_area_silence(const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
686 size_t samples, snd_pcm_format_t format)
688 /* FIXME: sub byte resolution and odd dst_offset */
689 unsigned char *dst;
690 unsigned int dst_step;
691 int width;
692 const unsigned char *silence;
693 if (!dst_area->addr)
694 return 0;
695 dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
696 width = snd_pcm_format_physical_width(format);
697 if (width <= 0)
698 return -EINVAL;
699 if (dst_area->step == (unsigned int) width && width >= 8)
700 return snd_pcm_format_set_silence(format, dst, samples);
701 silence = snd_pcm_format_silence_64(format);
702 if (! silence)
703 return -EINVAL;
704 dst_step = dst_area->step / 8;
705 if (width == 4) {
706 /* Ima ADPCM */
707 int dstbit = dst_area->first % 8;
708 int dstbit_step = dst_area->step % 8;
709 while (samples-- > 0) {
710 if (dstbit)
711 *dst &= 0xf0;
712 else
713 *dst &= 0x0f;
714 dst += dst_step;
715 dstbit += dstbit_step;
716 if (dstbit == 8) {
717 dst++;
718 dstbit = 0;
721 } else {
722 width /= 8;
723 while (samples-- > 0) {
724 memcpy(dst, silence, width);
725 dst += dst_step;
728 return 0;
731 int snd_pcm_area_copy(const struct snd_pcm_channel_area *src_area, size_t src_offset,
732 const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
733 size_t samples, snd_pcm_format_t format)
735 /* FIXME: sub byte resolution and odd dst_offset */
736 char *src, *dst;
737 int width;
738 int src_step, dst_step;
739 src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
740 if (!src_area->addr)
741 return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
742 dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
743 if (!dst_area->addr)
744 return 0;
745 width = snd_pcm_format_physical_width(format);
746 if (width <= 0)
747 return -EINVAL;
748 if (src_area->step == (unsigned int) width &&
749 dst_area->step == (unsigned int) width && width >= 8) {
750 size_t bytes = samples * width / 8;
751 memcpy(dst, src, bytes);
752 return 0;
754 src_step = src_area->step / 8;
755 dst_step = dst_area->step / 8;
756 if (width == 4) {
757 /* Ima ADPCM */
758 int srcbit = src_area->first % 8;
759 int srcbit_step = src_area->step % 8;
760 int dstbit = dst_area->first % 8;
761 int dstbit_step = dst_area->step % 8;
762 while (samples-- > 0) {
763 unsigned char srcval;
764 if (srcbit)
765 srcval = *src & 0x0f;
766 else
767 srcval = (*src & 0xf0) >> 4;
768 if (dstbit)
769 *dst = (*dst & 0xf0) | srcval;
770 else
771 *dst = (*dst & 0x0f) | (srcval << 4);
772 src += src_step;
773 srcbit += srcbit_step;
774 if (srcbit == 8) {
775 src++;
776 srcbit = 0;
778 dst += dst_step;
779 dstbit += dstbit_step;
780 if (dstbit == 8) {
781 dst++;
782 dstbit = 0;
785 } else {
786 width /= 8;
787 while (samples-- > 0) {
788 memcpy(dst, src, width);
789 src += src_step;
790 dst += dst_step;
793 return 0;