ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / core / oss / pcm_plugin.c
1 /*
2  *  PCM Plug-In shared (kernel/library) code
3  *  Copyright (c) 1999 by Jaroslav Kysela <perex@suse.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 <sound/driver.h>
28 #include <linux/slab.h>
29 #include <linux/time.h>
30 #include <linux/vmalloc.h>
31 #include <sound/core.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include "pcm_plugin.h"
35
36 #define snd_pcm_plug_first(plug) ((plug)->runtime->oss.plugin_first)
37 #define snd_pcm_plug_last(plug) ((plug)->runtime->oss.plugin_last)
38
39 static int snd_pcm_plugin_src_channels_mask(snd_pcm_plugin_t *plugin,
40                                             bitset_t *dst_vmask,
41                                             bitset_t **src_vmask)
42 {
43         bitset_t *vmask = plugin->src_vmask;
44         bitset_copy(vmask, dst_vmask, plugin->src_format.channels);
45         *src_vmask = vmask;
46         return 0;
47 }
48
49 static int snd_pcm_plugin_dst_channels_mask(snd_pcm_plugin_t *plugin,
50                                             bitset_t *src_vmask,
51                                             bitset_t **dst_vmask)
52 {
53         bitset_t *vmask = plugin->dst_vmask;
54         bitset_copy(vmask, src_vmask, plugin->dst_format.channels);
55         *dst_vmask = vmask;
56         return 0;
57 }
58
59 /*
60  *  because some cards might have rates "very close", we ignore
61  *  all "resampling" requests within +-5%
62  */
63 static int rate_match(unsigned int src_rate, unsigned int dst_rate)
64 {
65         unsigned int low = (src_rate * 95) / 100;
66         unsigned int high = (src_rate * 105) / 100;
67         return dst_rate >= low && dst_rate <= high;
68 }
69
70 static int snd_pcm_plugin_alloc(snd_pcm_plugin_t *plugin, snd_pcm_uframes_t frames)
71 {
72         snd_pcm_plugin_format_t *format;
73         ssize_t width;
74         size_t size;
75         unsigned int channel;
76         snd_pcm_plugin_channel_t *c;
77
78         if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK) {
79                 format = &plugin->src_format;
80         } else {
81                 format = &plugin->dst_format;
82         }
83         if ((width = snd_pcm_format_physical_width(format->format)) < 0)
84                 return width;
85         size = frames * format->channels * width;
86         snd_assert((size % 8) == 0, return -ENXIO);
87         size /= 8;
88         if (plugin->buf_frames < frames) {
89                 if (plugin->buf)
90                         vfree(plugin->buf);
91                 plugin->buf = vmalloc(size);
92                 plugin->buf_frames = frames;
93         }
94         if (!plugin->buf) {
95                 plugin->buf_frames = 0;
96                 return -ENOMEM;
97         }
98         c = plugin->buf_channels;
99         if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
100                 for (channel = 0; channel < format->channels; channel++, c++) {
101                         c->frames = frames;
102                         c->enabled = 1;
103                         c->wanted = 0;
104                         c->area.addr = plugin->buf;
105                         c->area.first = channel * width;
106                         c->area.step = format->channels * width;
107                 }
108         } else if (plugin->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
109                 snd_assert((size % format->channels) == 0,);
110                 size /= format->channels;
111                 for (channel = 0; channel < format->channels; channel++, c++) {
112                         c->frames = frames;
113                         c->enabled = 1;
114                         c->wanted = 0;
115                         c->area.addr = plugin->buf + (channel * size);
116                         c->area.first = 0;
117                         c->area.step = width;
118                 }
119         } else
120                 return -EINVAL;
121         return 0;
122 }
123
124 int snd_pcm_plug_alloc(snd_pcm_plug_t *plug, snd_pcm_uframes_t frames)
125 {
126         int err;
127         snd_assert(snd_pcm_plug_first(plug) != NULL, return -ENXIO);
128         if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) {
129                 snd_pcm_plugin_t *plugin = snd_pcm_plug_first(plug);
130                 while (plugin->next) {
131                         if (plugin->dst_frames)
132                                 frames = plugin->dst_frames(plugin, frames);
133                         snd_assert(frames > 0, return -ENXIO);
134                         plugin = plugin->next;
135                         err = snd_pcm_plugin_alloc(plugin, frames);
136                         if (err < 0)
137                                 return err;
138                 }
139         } else {
140                 snd_pcm_plugin_t *plugin = snd_pcm_plug_last(plug);
141                 while (plugin->prev) {
142                         if (plugin->src_frames)
143                                 frames = plugin->src_frames(plugin, frames);
144                         snd_assert(frames > 0, return -ENXIO);
145                         plugin = plugin->prev;
146                         err = snd_pcm_plugin_alloc(plugin, frames);
147                         if (err < 0)
148                                 return err;
149                 }
150         }
151         return 0;
152 }
153
154
155 snd_pcm_sframes_t snd_pcm_plugin_client_channels(snd_pcm_plugin_t *plugin,
156                                        snd_pcm_uframes_t frames,
157                                        snd_pcm_plugin_channel_t **channels)
158 {
159         *channels = plugin->buf_channels;
160         return frames;
161 }
162
163 int snd_pcm_plugin_build(snd_pcm_plug_t *plug,
164                          const char *name,
165                          snd_pcm_plugin_format_t *src_format,
166                          snd_pcm_plugin_format_t *dst_format,
167                          size_t extra,
168                          snd_pcm_plugin_t **ret)
169 {
170         snd_pcm_plugin_t *plugin;
171         unsigned int channels;
172         
173         snd_assert(plug != NULL, return -ENXIO);
174         snd_assert(src_format != NULL && dst_format != NULL, return -ENXIO);
175         plugin = (snd_pcm_plugin_t *)snd_kcalloc(sizeof(*plugin) + extra, GFP_KERNEL);
176         if (plugin == NULL)
177                 return -ENOMEM;
178         plugin->name = name;
179         plugin->plug = plug;
180         plugin->stream = snd_pcm_plug_stream(plug);
181         plugin->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
182         plugin->src_format = *src_format;
183         plugin->src_width = snd_pcm_format_physical_width(src_format->format);
184         snd_assert(plugin->src_width > 0, );
185         plugin->dst_format = *dst_format;
186         plugin->dst_width = snd_pcm_format_physical_width(dst_format->format);
187         snd_assert(plugin->dst_width > 0, );
188         if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK)
189                 channels = src_format->channels;
190         else
191                 channels = dst_format->channels;
192         plugin->buf_channels = snd_kcalloc(channels * sizeof(*plugin->buf_channels), GFP_KERNEL);
193         if (plugin->buf_channels == NULL) {
194                 snd_pcm_plugin_free(plugin);
195                 return -ENOMEM;
196         }
197         plugin->src_vmask = bitset_alloc(src_format->channels);
198         if (plugin->src_vmask == NULL) {
199                 snd_pcm_plugin_free(plugin);
200                 return -ENOMEM;
201         }
202         plugin->dst_vmask = bitset_alloc(dst_format->channels);
203         if (plugin->dst_vmask == NULL) {
204                 snd_pcm_plugin_free(plugin);
205                 return -ENOMEM;
206         }
207         plugin->client_channels = snd_pcm_plugin_client_channels;
208         plugin->src_channels_mask = snd_pcm_plugin_src_channels_mask;
209         plugin->dst_channels_mask = snd_pcm_plugin_dst_channels_mask;
210         *ret = plugin;
211         return 0;
212 }
213
214 int snd_pcm_plugin_free(snd_pcm_plugin_t *plugin)
215 {
216         if (! plugin)
217                 return 0;
218         if (plugin->private_free)
219                 plugin->private_free(plugin);
220         if (plugin->buf_channels)
221                 kfree(plugin->buf_channels);
222         if (plugin->buf)
223                 vfree(plugin->buf);
224         if (plugin->src_vmask)
225                 kfree(plugin->src_vmask);
226         if (plugin->dst_vmask)
227                 kfree(plugin->dst_vmask);
228         kfree(plugin);
229         return 0;
230 }
231
232 snd_pcm_sframes_t snd_pcm_plug_client_size(snd_pcm_plug_t *plug, snd_pcm_uframes_t drv_frames)
233 {
234         snd_pcm_plugin_t *plugin, *plugin_prev, *plugin_next;
235         int stream = snd_pcm_plug_stream(plug);
236
237         snd_assert(plug != NULL, return -ENXIO);
238         if (drv_frames == 0)
239                 return 0;
240         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
241                 plugin = snd_pcm_plug_last(plug);
242                 while (plugin && drv_frames > 0) {
243                         plugin_prev = plugin->prev;
244                         if (plugin->src_frames)
245                                 drv_frames = plugin->src_frames(plugin, drv_frames);
246                         plugin = plugin_prev;
247                 }
248         } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
249                 plugin = snd_pcm_plug_first(plug);
250                 while (plugin && drv_frames > 0) {
251                         plugin_next = plugin->next;
252                         if (plugin->dst_frames)
253                                 drv_frames = plugin->dst_frames(plugin, drv_frames);
254                         plugin = plugin_next;
255                 }
256         } else
257                 snd_BUG();
258         return drv_frames;
259 }
260
261 snd_pcm_sframes_t snd_pcm_plug_slave_size(snd_pcm_plug_t *plug, snd_pcm_uframes_t clt_frames)
262 {
263         snd_pcm_plugin_t *plugin, *plugin_prev, *plugin_next;
264         snd_pcm_sframes_t frames;
265         int stream = snd_pcm_plug_stream(plug);
266         
267         snd_assert(plug != NULL, return -ENXIO);
268         if (clt_frames == 0)
269                 return 0;
270         frames = clt_frames;
271         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
272                 plugin = snd_pcm_plug_first(plug);
273                 while (plugin && frames > 0) {
274                         plugin_next = plugin->next;
275                         if (plugin->dst_frames) {
276                                 frames = plugin->dst_frames(plugin, frames);
277                                 if (frames < 0)
278                                         return frames;
279                         }
280                         plugin = plugin_next;
281                 }
282         } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
283                 plugin = snd_pcm_plug_last(plug);
284                 while (plugin) {
285                         plugin_prev = plugin->prev;
286                         if (plugin->src_frames) {
287                                 frames = plugin->src_frames(plugin, frames);
288                                 if (frames < 0)
289                                         return frames;
290                         }
291                         plugin = plugin_prev;
292                 }
293         } else
294                 snd_BUG();
295         return frames;
296 }
297
298 static int snd_pcm_plug_formats(snd_mask_t *mask, int format)
299 {
300         snd_mask_t 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_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
307                        SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE);
308         snd_mask_set(&formats, SNDRV_PCM_FORMAT_MU_LAW);
309         
310         if (formats.bits[0] & (u32)linfmts)
311                 formats.bits[0] |= (u32)linfmts;
312         if (formats.bits[1] & (u32)(linfmts >> 32))
313                 formats.bits[1] |= (u32)(linfmts >> 32);
314         return snd_mask_test(&formats, format);
315 }
316
317 static int preferred_formats[] = {
318         SNDRV_PCM_FORMAT_S16_LE,
319         SNDRV_PCM_FORMAT_S16_BE,
320         SNDRV_PCM_FORMAT_U16_LE,
321         SNDRV_PCM_FORMAT_U16_BE,
322         SNDRV_PCM_FORMAT_S24_LE,
323         SNDRV_PCM_FORMAT_S24_BE,
324         SNDRV_PCM_FORMAT_U24_LE,
325         SNDRV_PCM_FORMAT_U24_BE,
326         SNDRV_PCM_FORMAT_S32_LE,
327         SNDRV_PCM_FORMAT_S32_BE,
328         SNDRV_PCM_FORMAT_U32_LE,
329         SNDRV_PCM_FORMAT_U32_BE,
330         SNDRV_PCM_FORMAT_S8,
331         SNDRV_PCM_FORMAT_U8
332 };
333
334 int snd_pcm_plug_slave_format(int format, snd_mask_t *format_mask)
335 {
336         if (snd_mask_test(format_mask, format))
337                 return format;
338         if (! snd_pcm_plug_formats(format_mask, format))
339                 return -EINVAL;
340         if (snd_pcm_format_linear(format)) {
341                 int width = snd_pcm_format_width(format);
342                 int unsignd = snd_pcm_format_unsigned(format);
343                 int big = snd_pcm_format_big_endian(format);
344                 int format1;
345                 int wid, width1=width;
346                 int dwidth1 = 8;
347                 for (wid = 0; wid < 4; ++wid) {
348                         int end, big1 = big;
349                         for (end = 0; end < 2; ++end) {
350                                 int sgn, unsignd1 = unsignd;
351                                 for (sgn = 0; sgn < 2; ++sgn) {
352                                         format1 = snd_pcm_build_linear_format(width1, unsignd1, big1);
353                                         if (format1 >= 0 &&
354                                             snd_mask_test(format_mask, format1))
355                                                 goto _found;
356                                         unsignd1 = !unsignd1;
357                                 }
358                                 big1 = !big1;
359                         }
360                         if (width1 == 32) {
361                                 dwidth1 = -dwidth1;
362                                 width1 = width;
363                         }
364                         width1 += dwidth1;
365                 }
366                 return -EINVAL;
367         _found:
368                 return format1;
369         } else {
370                 unsigned int i;
371                 switch (format) {
372                 case SNDRV_PCM_FORMAT_MU_LAW:
373                         for (i = 0; i < sizeof(preferred_formats) / sizeof(preferred_formats[0]); ++i) {
374                                 int format1 = preferred_formats[i];
375                                 if (snd_mask_test(format_mask, format1))
376                                         return format1;
377                         }
378                 default:
379                         return -EINVAL;
380                 }
381         }
382 }
383
384 int snd_pcm_plug_format_plugins(snd_pcm_plug_t *plug,
385                                 snd_pcm_hw_params_t *params,
386                                 snd_pcm_hw_params_t *slave_params)
387 {
388         snd_pcm_plugin_format_t tmpformat;
389         snd_pcm_plugin_format_t dstformat;
390         snd_pcm_plugin_format_t srcformat;
391         int src_access, dst_access;
392         snd_pcm_plugin_t *plugin = NULL;
393         int err, first;
394         int stream = snd_pcm_plug_stream(plug);
395         int slave_interleaved = (params_channels(slave_params) == 1 ||
396                                  params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED);
397
398         switch (stream) {
399         case SNDRV_PCM_STREAM_PLAYBACK:
400                 dstformat.format = params_format(slave_params);
401                 dstformat.rate = params_rate(slave_params);
402                 dstformat.channels = params_channels(slave_params);
403                 srcformat.format = params_format(params);
404                 srcformat.rate = params_rate(params);
405                 srcformat.channels = params_channels(params);
406                 src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
407                 dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
408                                                   SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
409                 break;
410         case SNDRV_PCM_STREAM_CAPTURE:
411                 dstformat.format = params_format(params);
412                 dstformat.rate = params_rate(params);
413                 dstformat.channels = params_channels(params);
414                 srcformat.format = params_format(slave_params);
415                 srcformat.rate = params_rate(slave_params);
416                 srcformat.channels = params_channels(slave_params);
417                 src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
418                                                   SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
419                 dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
420                 break;
421         default:
422                 snd_BUG();
423                 return -EINVAL;
424         }
425         tmpformat = srcformat;
426                 
427         pdprintf("srcformat: format=%i, rate=%i, channels=%i\n", 
428                  srcformat.format,
429                  srcformat.rate,
430                  srcformat.channels);
431         pdprintf("dstformat: format=%i, rate=%i, channels=%i\n", 
432                  dstformat.format,
433                  dstformat.rate,
434                  dstformat.channels);
435
436         /* Format change (linearization) */
437         if ((srcformat.format != dstformat.format ||
438              !rate_match(srcformat.rate, dstformat.rate) ||
439              srcformat.channels != dstformat.channels) &&
440             !snd_pcm_format_linear(srcformat.format)) {
441                 if (snd_pcm_format_linear(dstformat.format))
442                         tmpformat.format = dstformat.format;
443                 else
444                         tmpformat.format = SNDRV_PCM_FORMAT_S16;
445                 first = plugin == NULL;
446                 switch (srcformat.format) {
447                 case SNDRV_PCM_FORMAT_MU_LAW:
448                         err = snd_pcm_plugin_build_mulaw(plug,
449                                                          &srcformat, &tmpformat,
450                                                          &plugin);
451                         break;
452                 default:
453                         return -EINVAL;
454                 }
455                 pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
456                 if (err < 0)
457                         return err;
458                 err = snd_pcm_plugin_append(plugin);
459                 if (err < 0) {
460                         snd_pcm_plugin_free(plugin);
461                         return err;
462                 }
463                 srcformat = tmpformat;
464                 src_access = dst_access;
465         }
466
467         /* channels reduction */
468         if (srcformat.channels > dstformat.channels) {
469                 int sv = srcformat.channels;
470                 int dv = dstformat.channels;
471                 route_ttable_entry_t *ttable = snd_kcalloc(dv*sv*sizeof(*ttable), GFP_KERNEL);
472                 if (ttable == NULL)
473                         return -ENOMEM;
474 #if 1
475                 if (sv == 2 && dv == 1) {
476                         ttable[0] = HALF;
477                         ttable[1] = HALF;
478                 } else
479 #endif
480                 {
481                         int v;
482                         for (v = 0; v < dv; ++v)
483                                 ttable[v * sv + v] = FULL;
484                 }
485                 tmpformat.channels = dstformat.channels;
486                 if (rate_match(srcformat.rate, dstformat.rate) &&
487                     snd_pcm_format_linear(dstformat.format))
488                         tmpformat.format = dstformat.format;
489                 err = snd_pcm_plugin_build_route(plug,
490                                                  &srcformat, &tmpformat,
491                                                  ttable, &plugin);
492                 kfree(ttable);
493                 pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
494                 if (err < 0) {
495                         snd_pcm_plugin_free(plugin);
496                         return err;
497                 }
498                 err = snd_pcm_plugin_append(plugin);
499                 if (err < 0) {
500                         snd_pcm_plugin_free(plugin);
501                         return err;
502                 }
503                 srcformat = tmpformat;
504                 src_access = dst_access;
505         }
506
507         /* rate resampling */
508         if (!rate_match(srcformat.rate, dstformat.rate)) {
509                 tmpformat.rate = dstformat.rate;
510                 if (srcformat.channels == dstformat.channels &&
511                     snd_pcm_format_linear(dstformat.format))
512                         tmpformat.format = dstformat.format;
513                 err = snd_pcm_plugin_build_rate(plug,
514                                                 &srcformat, &tmpformat,
515                                                 &plugin);
516                 pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
517                 if (err < 0) {
518                         snd_pcm_plugin_free(plugin);
519                         return err;
520                 }                                           
521                 err = snd_pcm_plugin_append(plugin);
522                 if (err < 0) {
523                         snd_pcm_plugin_free(plugin);
524                         return err;
525                 }
526                 srcformat = tmpformat;
527                 src_access = dst_access;
528         }
529
530         /* channels extension  */
531         if (srcformat.channels < dstformat.channels) {
532                 int sv = srcformat.channels;
533                 int dv = dstformat.channels;
534                 route_ttable_entry_t *ttable = snd_kcalloc(dv * sv * sizeof(*ttable), GFP_KERNEL);
535                 if (ttable == NULL)
536                         return -ENOMEM;
537 #if 0
538                 {
539                         int v;
540                         for (v = 0; v < sv; ++v)
541                                 ttable[v * sv + v] = FULL;
542                 }
543 #else
544                 {
545                         /* Playback is spreaded on all channels */
546                         int vd, vs;
547                         for (vd = 0, vs = 0; vd < dv; ++vd) {
548                                 ttable[vd * sv + vs] = FULL;
549                                 vs++;
550                                 if (vs == sv)
551                                         vs = 0;
552                         }
553                 }
554 #endif
555                 tmpformat.channels = dstformat.channels;
556                 if (snd_pcm_format_linear(dstformat.format))
557                         tmpformat.format = dstformat.format;
558                 err = snd_pcm_plugin_build_route(plug,
559                                                  &srcformat, &tmpformat,
560                                                  ttable, &plugin);
561                 kfree(ttable);
562                 pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
563                 if (err < 0) {
564                         snd_pcm_plugin_free(plugin);
565                         return err;
566                 }                                           
567                 err = snd_pcm_plugin_append(plugin);
568                 if (err < 0) {
569                         snd_pcm_plugin_free(plugin);
570                         return err;
571                 }
572                 srcformat = tmpformat;
573                 src_access = dst_access;
574         }
575
576         /* format change */
577         if (srcformat.format != dstformat.format) {
578                 tmpformat.format = dstformat.format;
579                 if (tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) {
580                         err = snd_pcm_plugin_build_mulaw(plug,
581                                                          &srcformat, &tmpformat,
582                                                          &plugin);
583                 }
584                 else if (snd_pcm_format_linear(srcformat.format) &&
585                          snd_pcm_format_linear(tmpformat.format)) {
586                         err = snd_pcm_plugin_build_linear(plug,
587                                                           &srcformat, &tmpformat,
588                                                           &plugin);
589                 }
590                 else
591                         return -EINVAL;
592                 pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
593                 if (err < 0)
594                         return err;
595                 err = snd_pcm_plugin_append(plugin);
596                 if (err < 0) {
597                         snd_pcm_plugin_free(plugin);
598                         return err;
599                 }
600                 srcformat = tmpformat;
601                 src_access = dst_access;
602         }
603
604         /* de-interleave */
605         if (src_access != dst_access) {
606                 err = snd_pcm_plugin_build_copy(plug,
607                                                 &srcformat,
608                                                 &tmpformat,
609                                                 &plugin);
610                 pdprintf("interleave change (copy: returns %i)\n", err);
611                 if (err < 0)
612                         return err;
613                 err = snd_pcm_plugin_append(plugin);
614                 if (err < 0) {
615                         snd_pcm_plugin_free(plugin);
616                         return err;
617                 }
618         }
619
620         return 0;
621 }
622
623 snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(snd_pcm_plug_t *plug,
624                                          char *buf,
625                                          snd_pcm_uframes_t count,
626                                          snd_pcm_plugin_channel_t **channels)
627 {
628         snd_pcm_plugin_t *plugin;
629         snd_pcm_plugin_channel_t *v;
630         snd_pcm_plugin_format_t *format;
631         int width, nchannels, channel;
632         int stream = snd_pcm_plug_stream(plug);
633
634         snd_assert(buf != NULL, return -ENXIO);
635         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
636                 plugin = snd_pcm_plug_first(plug);
637                 format = &plugin->src_format;
638         } else {
639                 plugin = snd_pcm_plug_last(plug);
640                 format = &plugin->dst_format;
641         }
642         v = plugin->buf_channels;
643         *channels = v;
644         if ((width = snd_pcm_format_physical_width(format->format)) < 0)
645                 return width;
646         nchannels = format->channels;
647         snd_assert(plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || format->channels <= 1, return -ENXIO);
648         for (channel = 0; channel < nchannels; channel++, v++) {
649                 v->frames = count;
650                 v->enabled = 1;
651                 v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE);
652                 v->area.addr = buf;
653                 v->area.first = channel * width;
654                 v->area.step = nchannels * width;
655         }
656         return count;
657 }
658
659 int snd_pcm_plug_playback_channels_mask(snd_pcm_plug_t *plug,
660                                         bitset_t *client_vmask)
661 {
662         snd_pcm_plugin_t *plugin = snd_pcm_plug_last(plug);
663         if (plugin == NULL) {
664                 return 0;
665         } else {
666                 int schannels = plugin->dst_format.channels;
667                 bitset_t bs[bitset_size(schannels)];
668                 bitset_t *srcmask;
669                 bitset_t *dstmask = bs;
670                 int err;
671                 bitset_one(dstmask, schannels);
672                 if (plugin == NULL) {
673                         bitset_and(client_vmask, dstmask, schannels);
674                         return 0;
675                 }
676                 while (1) {
677                         err = plugin->src_channels_mask(plugin, dstmask, &srcmask);
678                         if (err < 0)
679                                 return err;
680                         dstmask = srcmask;
681                         if (plugin->prev == NULL)
682                                 break;
683                         plugin = plugin->prev;
684                 }
685                 bitset_and(client_vmask, dstmask, plugin->src_format.channels);
686                 return 0;
687         }
688 }
689
690 int snd_pcm_plug_capture_channels_mask(snd_pcm_plug_t *plug,
691                                        bitset_t *client_vmask)
692 {
693         snd_pcm_plugin_t *plugin = snd_pcm_plug_first(plug);
694         if (plugin == NULL) {
695                 return 0;
696         } else {
697                 int schannels = plugin->src_format.channels;
698                 bitset_t bs[bitset_size(schannels)];
699                 bitset_t *srcmask = bs;
700                 bitset_t *dstmask;
701                 int err;
702                 bitset_one(srcmask, schannels);
703                 while (1) {
704                         err = plugin->dst_channels_mask(plugin, srcmask, &dstmask);
705                         if (err < 0)
706                                 return err;
707                         srcmask = dstmask;
708                         if (plugin->next == NULL)
709                                 break;
710                         plugin = plugin->next;
711                 }
712                 bitset_and(client_vmask, srcmask, plugin->dst_format.channels);
713                 return 0;
714         }
715 }
716
717 static int snd_pcm_plug_playback_disable_useless_channels(snd_pcm_plug_t *plug,
718                                                           snd_pcm_plugin_channel_t *src_channels)
719 {
720         snd_pcm_plugin_t *plugin = snd_pcm_plug_first(plug);
721         unsigned int nchannels = plugin->src_format.channels;
722         bitset_t bs[bitset_size(nchannels)];
723         bitset_t *srcmask = bs;
724         int err;
725         unsigned int channel;
726         for (channel = 0; channel < nchannels; channel++) {
727                 if (src_channels[channel].enabled)
728                         bitset_set(srcmask, channel);
729                 else
730                         bitset_reset(srcmask, channel);
731         }
732         err = snd_pcm_plug_playback_channels_mask(plug, srcmask);
733         if (err < 0)
734                 return err;
735         for (channel = 0; channel < nchannels; channel++) {
736                 if (!bitset_get(srcmask, channel))
737                         src_channels[channel].enabled = 0;
738         }
739         return 0;
740 }
741
742 static int snd_pcm_plug_capture_disable_useless_channels(snd_pcm_plug_t *plug,
743                                                          snd_pcm_plugin_channel_t *src_channels,
744                                                          snd_pcm_plugin_channel_t *client_channels)
745 {
746         snd_pcm_plugin_t *plugin = snd_pcm_plug_last(plug);
747         unsigned int nchannels = plugin->dst_format.channels;
748         bitset_t bs[bitset_size(nchannels)];
749         bitset_t *dstmask = bs;
750         bitset_t *srcmask;
751         int err;
752         unsigned int channel;
753         for (channel = 0; channel < nchannels; channel++) {
754                 if (client_channels[channel].enabled)
755                         bitset_set(dstmask, channel);
756                 else
757                         bitset_reset(dstmask, channel);
758         }
759         while (plugin) {
760                 err = plugin->src_channels_mask(plugin, dstmask, &srcmask);
761                 if (err < 0)
762                         return err;
763                 dstmask = srcmask;
764                 plugin = plugin->prev;
765         }
766         plugin = snd_pcm_plug_first(plug);
767         nchannels = plugin->src_format.channels;
768         for (channel = 0; channel < nchannels; channel++) {
769                 if (!bitset_get(dstmask, channel))
770                         src_channels[channel].enabled = 0;
771         }
772         return 0;
773 }
774
775 snd_pcm_sframes_t snd_pcm_plug_write_transfer(snd_pcm_plug_t *plug, snd_pcm_plugin_channel_t *src_channels, snd_pcm_uframes_t size)
776 {
777         snd_pcm_plugin_t *plugin, *next;
778         snd_pcm_plugin_channel_t *dst_channels;
779         int err;
780         snd_pcm_sframes_t frames = size;
781
782         if ((err = snd_pcm_plug_playback_disable_useless_channels(plug, src_channels)) < 0)
783                 return err;
784         
785         plugin = snd_pcm_plug_first(plug);
786         while (plugin && frames > 0) {
787                 if ((next = plugin->next) != NULL) {
788                         snd_pcm_sframes_t frames1 = frames;
789                         if (plugin->dst_frames)
790                                 frames1 = plugin->dst_frames(plugin, frames);
791                         if ((err = next->client_channels(next, frames1, &dst_channels)) < 0) {
792                                 return err;
793                         }
794                         if (err != frames1) {
795                                 frames = err;
796                                 if (plugin->src_frames)
797                                         frames = plugin->src_frames(plugin, frames1);
798                         }
799                 } else
800                         dst_channels = 0;
801                 pdprintf("write plugin: %s, %li\n", plugin->name, frames);
802                 if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
803                         return frames;
804                 src_channels = dst_channels;
805                 plugin = next;
806         }
807         return snd_pcm_plug_client_size(plug, frames);
808 }
809
810 snd_pcm_sframes_t snd_pcm_plug_read_transfer(snd_pcm_plug_t *plug, snd_pcm_plugin_channel_t *dst_channels_final, snd_pcm_uframes_t size)
811 {
812         snd_pcm_plugin_t *plugin, *next;
813         snd_pcm_plugin_channel_t *src_channels, *dst_channels;
814         snd_pcm_sframes_t frames = size;
815         int err;
816
817         frames = snd_pcm_plug_slave_size(plug, frames);
818         if (frames < 0)
819                 return frames;
820
821         src_channels = 0;
822         plugin = snd_pcm_plug_first(plug);
823         while (plugin && frames > 0) {
824                 if ((next = plugin->next) != NULL) {
825                         if ((err = plugin->client_channels(plugin, frames, &dst_channels)) < 0) {
826                                 return err;
827                         }
828                         frames = err;
829                         if (!plugin->prev) {
830                                 if ((err = snd_pcm_plug_capture_disable_useless_channels(plug, dst_channels, dst_channels_final)) < 0)
831                                         return err;
832                         }
833                 } else {
834                         dst_channels = dst_channels_final;
835                 }
836                 pdprintf("read plugin: %s, %li\n", plugin->name, frames);
837                 if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
838                         return frames;
839                 plugin = next;
840                 src_channels = dst_channels;
841         }
842         return frames;
843 }
844
845 int snd_pcm_area_silence(const snd_pcm_channel_area_t *dst_area, size_t dst_offset,
846                          size_t samples, int format)
847 {
848         /* FIXME: sub byte resolution and odd dst_offset */
849         char *dst;
850         unsigned int dst_step;
851         int width;
852         u_int64_t silence;
853         if (!dst_area->addr)
854                 return 0;
855         dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
856         width = snd_pcm_format_physical_width(format);
857         silence = snd_pcm_format_silence_64(format);
858         if (dst_area->step == (unsigned int) width) {
859                 size_t dwords = samples * width / 64;
860                 u_int64_t *dst64 = (u_int64_t *)dst;
861
862                 samples -= dwords * 64 / width;
863                 while (dwords-- > 0)
864                         *dst64++ = silence;
865                 if (samples == 0)
866                         return 0;
867                 dst = (char *)dst64;
868         }
869         dst_step = dst_area->step / 8;
870         switch (width) {
871         case 4: {
872                 u_int8_t s0 = silence & 0xf0;
873                 u_int8_t s1 = silence & 0x0f;
874                 int dstbit = dst_area->first % 8;
875                 int dstbit_step = dst_area->step % 8;
876                 while (samples-- > 0) {
877                         if (dstbit) {
878                                 *dst &= 0xf0;
879                                 *dst |= s1;
880                         } else {
881                                 *dst &= 0x0f;
882                                 *dst |= s0;
883                         }
884                         dst += dst_step;
885                         dstbit += dstbit_step;
886                         if (dstbit == 8) {
887                                 dst++;
888                                 dstbit = 0;
889                         }
890                 }
891                 break;
892         }
893         case 8: {
894                 u_int8_t sil = silence;
895                 while (samples-- > 0) {
896                         *dst = sil;
897                         dst += dst_step;
898                 }
899                 break;
900         }
901         case 16: {
902                 u_int16_t sil = silence;
903                 while (samples-- > 0) {
904                         *(u_int16_t*)dst = sil;
905                         dst += dst_step;
906                 }
907                 break;
908         }
909         case 32: {
910                 u_int32_t sil = silence;
911                 while (samples-- > 0) {
912                         *(u_int32_t*)dst = sil;
913                         dst += dst_step;
914                 }
915                 break;
916         }
917         case 64: {
918                 while (samples-- > 0) {
919                         *(u_int64_t*)dst = silence;
920                         dst += dst_step;
921                 }
922                 break;
923         }
924         default:
925                 snd_BUG();
926         }
927         return 0;
928 }
929
930 int snd_pcm_area_copy(const snd_pcm_channel_area_t *src_area, size_t src_offset,
931                       const snd_pcm_channel_area_t *dst_area, size_t dst_offset,
932                       size_t samples, int format)
933 {
934         /* FIXME: sub byte resolution and odd dst_offset */
935         char *src, *dst;
936         int width;
937         int src_step, dst_step;
938         src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
939         if (!src_area->addr)
940                 return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
941         dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
942         if (!dst_area->addr)
943                 return 0;
944         width = snd_pcm_format_physical_width(format);
945         if (src_area->step == (unsigned int) width &&
946             dst_area->step == (unsigned int) width) {
947                 size_t bytes = samples * width / 8;
948                 samples -= bytes * 8 / width;
949                 memcpy(dst, src, bytes);
950                 if (samples == 0)
951                         return 0;
952         }
953         src_step = src_area->step / 8;
954         dst_step = dst_area->step / 8;
955         switch (width) {
956         case 4: {
957                 int srcbit = src_area->first % 8;
958                 int srcbit_step = src_area->step % 8;
959                 int dstbit = dst_area->first % 8;
960                 int dstbit_step = dst_area->step % 8;
961                 while (samples-- > 0) {
962                         unsigned char srcval;
963                         if (srcbit)
964                                 srcval = *src & 0x0f;
965                         else
966                                 srcval = *src & 0xf0;
967                         if (dstbit)
968                                 *dst &= 0xf0;
969                         else
970                                 *dst &= 0x0f;
971                         *dst |= srcval;
972                         src += src_step;
973                         srcbit += srcbit_step;
974                         if (srcbit == 8) {
975                                 src++;
976                                 srcbit = 0;
977                         }
978                         dst += dst_step;
979                         dstbit += dstbit_step;
980                         if (dstbit == 8) {
981                                 dst++;
982                                 dstbit = 0;
983                         }
984                 }
985                 break;
986         }
987         case 8: {
988                 while (samples-- > 0) {
989                         *dst = *src;
990                         src += src_step;
991                         dst += dst_step;
992                 }
993                 break;
994         }
995         case 16: {
996                 while (samples-- > 0) {
997                         *(u_int16_t*)dst = *(u_int16_t*)src;
998                         src += src_step;
999                         dst += dst_step;
1000                 }
1001                 break;
1002         }
1003         case 32: {
1004                 while (samples-- > 0) {
1005                         *(u_int32_t*)dst = *(u_int32_t*)src;
1006                         src += src_step;
1007                         dst += dst_step;
1008                 }
1009                 break;
1010         }
1011         case 64: {
1012                 while (samples-- > 0) {
1013                         *(u_int64_t*)dst = *(u_int64_t*)src;
1014                         src += src_step;
1015                         dst += dst_step;
1016                 }
1017                 break;
1018         }
1019         default:
1020                 snd_BUG();
1021         }
1022         return 0;
1023 }