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