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>
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
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"
36 #define snd_pcm_plug_first(plug) ((plug)->runtime->oss.plugin_first)
37 #define snd_pcm_plug_last(plug) ((plug)->runtime->oss.plugin_last)
39 static int snd_pcm_plugin_src_channels_mask(struct snd_pcm_plugin
*plugin
,
40 unsigned long *dst_vmask
,
41 unsigned long **src_vmask
)
43 unsigned long *vmask
= plugin
->src_vmask
;
44 bitmap_copy(vmask
, dst_vmask
, plugin
->src_format
.channels
);
49 static int snd_pcm_plugin_dst_channels_mask(struct snd_pcm_plugin
*plugin
,
50 unsigned long *src_vmask
,
51 unsigned long **dst_vmask
)
53 unsigned long *vmask
= plugin
->dst_vmask
;
54 bitmap_copy(vmask
, src_vmask
, plugin
->dst_format
.channels
);
60 * because some cards might have rates "very close", we ignore
61 * all "resampling" requests within +-5%
63 static int rate_match(unsigned int src_rate
, unsigned int dst_rate
)
65 unsigned int low
= (src_rate
* 95) / 100;
66 unsigned int high
= (src_rate
* 105) / 100;
67 return dst_rate
>= low
&& dst_rate
<= high
;
70 static int snd_pcm_plugin_alloc(struct snd_pcm_plugin
*plugin
, snd_pcm_uframes_t frames
)
72 struct snd_pcm_plugin_format
*format
;
76 struct snd_pcm_plugin_channel
*c
;
78 if (plugin
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
79 format
= &plugin
->src_format
;
81 format
= &plugin
->dst_format
;
83 if ((width
= snd_pcm_format_physical_width(format
->format
)) < 0)
85 size
= frames
* format
->channels
* width
;
86 snd_assert((size
% 8) == 0, return -ENXIO
);
88 if (plugin
->buf_frames
< frames
) {
90 plugin
->buf
= vmalloc(size
);
91 plugin
->buf_frames
= frames
;
94 plugin
->buf_frames
= 0;
97 c
= plugin
->buf_channels
;
98 if (plugin
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
) {
99 for (channel
= 0; channel
< format
->channels
; channel
++, c
++) {
103 c
->area
.addr
= plugin
->buf
;
104 c
->area
.first
= channel
* width
;
105 c
->area
.step
= format
->channels
* width
;
107 } else if (plugin
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
) {
108 snd_assert((size
% format
->channels
) == 0,);
109 size
/= format
->channels
;
110 for (channel
= 0; channel
< format
->channels
; channel
++, c
++) {
114 c
->area
.addr
= plugin
->buf
+ (channel
* size
);
116 c
->area
.step
= width
;
123 int snd_pcm_plug_alloc(struct snd_pcm_substream
*plug
, snd_pcm_uframes_t frames
)
126 snd_assert(snd_pcm_plug_first(plug
) != NULL
, return -ENXIO
);
127 if (snd_pcm_plug_stream(plug
) == SNDRV_PCM_STREAM_PLAYBACK
) {
128 struct snd_pcm_plugin
*plugin
= snd_pcm_plug_first(plug
);
129 while (plugin
->next
) {
130 if (plugin
->dst_frames
)
131 frames
= plugin
->dst_frames(plugin
, frames
);
132 snd_assert(frames
> 0, return -ENXIO
);
133 plugin
= plugin
->next
;
134 err
= snd_pcm_plugin_alloc(plugin
, frames
);
139 struct snd_pcm_plugin
*plugin
= snd_pcm_plug_last(plug
);
140 while (plugin
->prev
) {
141 if (plugin
->src_frames
)
142 frames
= plugin
->src_frames(plugin
, frames
);
143 snd_assert(frames
> 0, return -ENXIO
);
144 plugin
= plugin
->prev
;
145 err
= snd_pcm_plugin_alloc(plugin
, frames
);
154 snd_pcm_sframes_t
snd_pcm_plugin_client_channels(struct snd_pcm_plugin
*plugin
,
155 snd_pcm_uframes_t frames
,
156 struct snd_pcm_plugin_channel
**channels
)
158 *channels
= plugin
->buf_channels
;
162 int snd_pcm_plugin_build(struct snd_pcm_substream
*plug
,
164 struct snd_pcm_plugin_format
*src_format
,
165 struct snd_pcm_plugin_format
*dst_format
,
167 struct snd_pcm_plugin
**ret
)
169 struct snd_pcm_plugin
*plugin
;
170 unsigned int channels
;
172 snd_assert(plug
!= NULL
, return -ENXIO
);
173 snd_assert(src_format
!= NULL
&& dst_format
!= NULL
, return -ENXIO
);
174 plugin
= kzalloc(sizeof(*plugin
) + extra
, GFP_KERNEL
);
179 plugin
->stream
= snd_pcm_plug_stream(plug
);
180 plugin
->access
= SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
181 plugin
->src_format
= *src_format
;
182 plugin
->src_width
= snd_pcm_format_physical_width(src_format
->format
);
183 snd_assert(plugin
->src_width
> 0, );
184 plugin
->dst_format
= *dst_format
;
185 plugin
->dst_width
= snd_pcm_format_physical_width(dst_format
->format
);
186 snd_assert(plugin
->dst_width
> 0, );
187 if (plugin
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
188 channels
= src_format
->channels
;
190 channels
= dst_format
->channels
;
191 plugin
->buf_channels
= kcalloc(channels
, sizeof(*plugin
->buf_channels
), GFP_KERNEL
);
192 if (plugin
->buf_channels
== NULL
) {
193 snd_pcm_plugin_free(plugin
);
196 plugin
->src_vmask
= bitmap_alloc(src_format
->channels
);
197 if (plugin
->src_vmask
== NULL
) {
198 snd_pcm_plugin_free(plugin
);
201 plugin
->dst_vmask
= bitmap_alloc(dst_format
->channels
);
202 if (plugin
->dst_vmask
== NULL
) {
203 snd_pcm_plugin_free(plugin
);
206 plugin
->client_channels
= snd_pcm_plugin_client_channels
;
207 plugin
->src_channels_mask
= snd_pcm_plugin_src_channels_mask
;
208 plugin
->dst_channels_mask
= snd_pcm_plugin_dst_channels_mask
;
213 int snd_pcm_plugin_free(struct snd_pcm_plugin
*plugin
)
217 if (plugin
->private_free
)
218 plugin
->private_free(plugin
);
219 kfree(plugin
->buf_channels
);
221 kfree(plugin
->src_vmask
);
222 kfree(plugin
->dst_vmask
);
227 snd_pcm_sframes_t
snd_pcm_plug_client_size(struct snd_pcm_substream
*plug
, snd_pcm_uframes_t drv_frames
)
229 struct snd_pcm_plugin
*plugin
, *plugin_prev
, *plugin_next
;
230 int stream
= snd_pcm_plug_stream(plug
);
232 snd_assert(plug
!= NULL
, return -ENXIO
);
235 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
236 plugin
= snd_pcm_plug_last(plug
);
237 while (plugin
&& drv_frames
> 0) {
238 plugin_prev
= plugin
->prev
;
239 if (plugin
->src_frames
)
240 drv_frames
= plugin
->src_frames(plugin
, drv_frames
);
241 plugin
= plugin_prev
;
243 } else if (stream
== SNDRV_PCM_STREAM_CAPTURE
) {
244 plugin
= snd_pcm_plug_first(plug
);
245 while (plugin
&& drv_frames
> 0) {
246 plugin_next
= plugin
->next
;
247 if (plugin
->dst_frames
)
248 drv_frames
= plugin
->dst_frames(plugin
, drv_frames
);
249 plugin
= plugin_next
;
256 snd_pcm_sframes_t
snd_pcm_plug_slave_size(struct snd_pcm_substream
*plug
, snd_pcm_uframes_t clt_frames
)
258 struct snd_pcm_plugin
*plugin
, *plugin_prev
, *plugin_next
;
259 snd_pcm_sframes_t frames
;
260 int stream
= snd_pcm_plug_stream(plug
);
262 snd_assert(plug
!= NULL
, return -ENXIO
);
266 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
267 plugin
= snd_pcm_plug_first(plug
);
268 while (plugin
&& frames
> 0) {
269 plugin_next
= plugin
->next
;
270 if (plugin
->dst_frames
) {
271 frames
= plugin
->dst_frames(plugin
, frames
);
275 plugin
= plugin_next
;
277 } else if (stream
== SNDRV_PCM_STREAM_CAPTURE
) {
278 plugin
= snd_pcm_plug_last(plug
);
280 plugin_prev
= plugin
->prev
;
281 if (plugin
->src_frames
) {
282 frames
= plugin
->src_frames(plugin
, frames
);
286 plugin
= plugin_prev
;
293 static int snd_pcm_plug_formats(struct snd_mask
*mask
, int format
)
295 struct snd_mask formats
= *mask
;
296 u64 linfmts
= (SNDRV_PCM_FMTBIT_U8
| SNDRV_PCM_FMTBIT_S8
|
297 SNDRV_PCM_FMTBIT_U16_LE
| SNDRV_PCM_FMTBIT_S16_LE
|
298 SNDRV_PCM_FMTBIT_U16_BE
| SNDRV_PCM_FMTBIT_S16_BE
|
299 SNDRV_PCM_FMTBIT_U24_LE
| SNDRV_PCM_FMTBIT_S24_LE
|
300 SNDRV_PCM_FMTBIT_U24_BE
| SNDRV_PCM_FMTBIT_S24_BE
|
301 SNDRV_PCM_FMTBIT_U32_LE
| SNDRV_PCM_FMTBIT_S32_LE
|
302 SNDRV_PCM_FMTBIT_U32_BE
| SNDRV_PCM_FMTBIT_S32_BE
);
303 snd_mask_set(&formats
, SNDRV_PCM_FORMAT_MU_LAW
);
305 if (formats
.bits
[0] & (u32
)linfmts
)
306 formats
.bits
[0] |= (u32
)linfmts
;
307 if (formats
.bits
[1] & (u32
)(linfmts
>> 32))
308 formats
.bits
[1] |= (u32
)(linfmts
>> 32);
309 return snd_mask_test(&formats
, format
);
312 static int preferred_formats
[] = {
313 SNDRV_PCM_FORMAT_S16_LE
,
314 SNDRV_PCM_FORMAT_S16_BE
,
315 SNDRV_PCM_FORMAT_U16_LE
,
316 SNDRV_PCM_FORMAT_U16_BE
,
317 SNDRV_PCM_FORMAT_S24_LE
,
318 SNDRV_PCM_FORMAT_S24_BE
,
319 SNDRV_PCM_FORMAT_U24_LE
,
320 SNDRV_PCM_FORMAT_U24_BE
,
321 SNDRV_PCM_FORMAT_S32_LE
,
322 SNDRV_PCM_FORMAT_S32_BE
,
323 SNDRV_PCM_FORMAT_U32_LE
,
324 SNDRV_PCM_FORMAT_U32_BE
,
329 int snd_pcm_plug_slave_format(int format
, struct snd_mask
*format_mask
)
331 if (snd_mask_test(format_mask
, format
))
333 if (! snd_pcm_plug_formats(format_mask
, format
))
335 if (snd_pcm_format_linear(format
)) {
336 int width
= snd_pcm_format_width(format
);
337 int unsignd
= snd_pcm_format_unsigned(format
);
338 int big
= snd_pcm_format_big_endian(format
);
340 int wid
, width1
=width
;
342 for (wid
= 0; wid
< 4; ++wid
) {
344 for (end
= 0; end
< 2; ++end
) {
345 int sgn
, unsignd1
= unsignd
;
346 for (sgn
= 0; sgn
< 2; ++sgn
) {
347 format1
= snd_pcm_build_linear_format(width1
, unsignd1
, big1
);
349 snd_mask_test(format_mask
, format1
))
351 unsignd1
= !unsignd1
;
367 case SNDRV_PCM_FORMAT_MU_LAW
:
368 for (i
= 0; i
< ARRAY_SIZE(preferred_formats
); ++i
) {
369 int format1
= preferred_formats
[i
];
370 if (snd_mask_test(format_mask
, format1
))
379 int snd_pcm_plug_format_plugins(struct snd_pcm_substream
*plug
,
380 struct snd_pcm_hw_params
*params
,
381 struct snd_pcm_hw_params
*slave_params
)
383 struct snd_pcm_plugin_format tmpformat
;
384 struct snd_pcm_plugin_format dstformat
;
385 struct snd_pcm_plugin_format srcformat
;
386 int src_access
, dst_access
;
387 struct snd_pcm_plugin
*plugin
= NULL
;
389 int stream
= snd_pcm_plug_stream(plug
);
390 int slave_interleaved
= (params_channels(slave_params
) == 1 ||
391 params_access(slave_params
) == SNDRV_PCM_ACCESS_RW_INTERLEAVED
);
394 case SNDRV_PCM_STREAM_PLAYBACK
:
395 dstformat
.format
= params_format(slave_params
);
396 dstformat
.rate
= params_rate(slave_params
);
397 dstformat
.channels
= params_channels(slave_params
);
398 srcformat
.format
= params_format(params
);
399 srcformat
.rate
= params_rate(params
);
400 srcformat
.channels
= params_channels(params
);
401 src_access
= SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
402 dst_access
= (slave_interleaved
? SNDRV_PCM_ACCESS_RW_INTERLEAVED
:
403 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
);
405 case SNDRV_PCM_STREAM_CAPTURE
:
406 dstformat
.format
= params_format(params
);
407 dstformat
.rate
= params_rate(params
);
408 dstformat
.channels
= params_channels(params
);
409 srcformat
.format
= params_format(slave_params
);
410 srcformat
.rate
= params_rate(slave_params
);
411 srcformat
.channels
= params_channels(slave_params
);
412 src_access
= (slave_interleaved
? SNDRV_PCM_ACCESS_RW_INTERLEAVED
:
413 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
);
414 dst_access
= SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
420 tmpformat
= srcformat
;
422 pdprintf("srcformat: format=%i, rate=%i, channels=%i\n",
426 pdprintf("dstformat: format=%i, rate=%i, channels=%i\n",
431 /* Format change (linearization) */
432 if ((srcformat
.format
!= dstformat
.format
||
433 !rate_match(srcformat
.rate
, dstformat
.rate
) ||
434 srcformat
.channels
!= dstformat
.channels
) &&
435 !snd_pcm_format_linear(srcformat
.format
)) {
436 if (snd_pcm_format_linear(dstformat
.format
))
437 tmpformat
.format
= dstformat
.format
;
439 tmpformat
.format
= SNDRV_PCM_FORMAT_S16
;
440 switch (srcformat
.format
) {
441 case SNDRV_PCM_FORMAT_MU_LAW
:
442 err
= snd_pcm_plugin_build_mulaw(plug
,
443 &srcformat
, &tmpformat
,
449 pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat
.format
, tmpformat
.format
, err
);
452 err
= snd_pcm_plugin_append(plugin
);
454 snd_pcm_plugin_free(plugin
);
457 srcformat
= tmpformat
;
458 src_access
= dst_access
;
461 /* channels reduction */
462 if (srcformat
.channels
> dstformat
.channels
) {
463 int sv
= srcformat
.channels
;
464 int dv
= dstformat
.channels
;
465 int *ttable
= kcalloc(dv
* sv
, sizeof(*ttable
), GFP_KERNEL
);
469 if (sv
== 2 && dv
== 1) {
476 for (v
= 0; v
< dv
; ++v
)
477 ttable
[v
* sv
+ v
] = FULL
;
479 tmpformat
.channels
= dstformat
.channels
;
480 if (rate_match(srcformat
.rate
, dstformat
.rate
) &&
481 snd_pcm_format_linear(dstformat
.format
))
482 tmpformat
.format
= dstformat
.format
;
483 err
= snd_pcm_plugin_build_route(plug
,
484 &srcformat
, &tmpformat
,
487 pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat
.channels
, tmpformat
.channels
, err
);
489 snd_pcm_plugin_free(plugin
);
492 err
= snd_pcm_plugin_append(plugin
);
494 snd_pcm_plugin_free(plugin
);
497 srcformat
= tmpformat
;
498 src_access
= dst_access
;
501 /* rate resampling */
502 if (!rate_match(srcformat
.rate
, dstformat
.rate
)) {
503 tmpformat
.rate
= dstformat
.rate
;
504 if (srcformat
.channels
== dstformat
.channels
&&
505 snd_pcm_format_linear(dstformat
.format
))
506 tmpformat
.format
= dstformat
.format
;
507 err
= snd_pcm_plugin_build_rate(plug
,
508 &srcformat
, &tmpformat
,
510 pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat
.rate
, tmpformat
.rate
, err
);
512 snd_pcm_plugin_free(plugin
);
515 err
= snd_pcm_plugin_append(plugin
);
517 snd_pcm_plugin_free(plugin
);
520 srcformat
= tmpformat
;
521 src_access
= dst_access
;
524 /* channels extension */
525 if (srcformat
.channels
< dstformat
.channels
) {
526 int sv
= srcformat
.channels
;
527 int dv
= dstformat
.channels
;
528 int *ttable
= kcalloc(dv
* sv
, sizeof(*ttable
), GFP_KERNEL
);
534 for (v
= 0; v
< sv
; ++v
)
535 ttable
[v
* sv
+ v
] = FULL
;
539 /* Playback is spreaded on all channels */
541 for (vd
= 0, vs
= 0; vd
< dv
; ++vd
) {
542 ttable
[vd
* sv
+ vs
] = FULL
;
549 tmpformat
.channels
= dstformat
.channels
;
550 if (snd_pcm_format_linear(dstformat
.format
))
551 tmpformat
.format
= dstformat
.format
;
552 err
= snd_pcm_plugin_build_route(plug
,
553 &srcformat
, &tmpformat
,
556 pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat
.channels
, tmpformat
.channels
, err
);
558 snd_pcm_plugin_free(plugin
);
561 err
= snd_pcm_plugin_append(plugin
);
563 snd_pcm_plugin_free(plugin
);
566 srcformat
= tmpformat
;
567 src_access
= dst_access
;
571 if (srcformat
.format
!= dstformat
.format
) {
572 tmpformat
.format
= dstformat
.format
;
573 if (tmpformat
.format
== SNDRV_PCM_FORMAT_MU_LAW
) {
574 err
= snd_pcm_plugin_build_mulaw(plug
,
575 &srcformat
, &tmpformat
,
578 else if (snd_pcm_format_linear(srcformat
.format
) &&
579 snd_pcm_format_linear(tmpformat
.format
)) {
580 err
= snd_pcm_plugin_build_linear(plug
,
581 &srcformat
, &tmpformat
,
586 pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat
.format
, tmpformat
.format
, err
);
589 err
= snd_pcm_plugin_append(plugin
);
591 snd_pcm_plugin_free(plugin
);
594 srcformat
= tmpformat
;
595 src_access
= dst_access
;
599 if (src_access
!= dst_access
) {
600 err
= snd_pcm_plugin_build_copy(plug
,
604 pdprintf("interleave change (copy: returns %i)\n", err
);
607 err
= snd_pcm_plugin_append(plugin
);
609 snd_pcm_plugin_free(plugin
);
617 snd_pcm_sframes_t
snd_pcm_plug_client_channels_buf(struct snd_pcm_substream
*plug
,
619 snd_pcm_uframes_t count
,
620 struct snd_pcm_plugin_channel
**channels
)
622 struct snd_pcm_plugin
*plugin
;
623 struct snd_pcm_plugin_channel
*v
;
624 struct snd_pcm_plugin_format
*format
;
625 int width
, nchannels
, channel
;
626 int stream
= snd_pcm_plug_stream(plug
);
628 snd_assert(buf
!= NULL
, return -ENXIO
);
629 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
630 plugin
= snd_pcm_plug_first(plug
);
631 format
= &plugin
->src_format
;
633 plugin
= snd_pcm_plug_last(plug
);
634 format
= &plugin
->dst_format
;
636 v
= plugin
->buf_channels
;
638 if ((width
= snd_pcm_format_physical_width(format
->format
)) < 0)
640 nchannels
= format
->channels
;
641 snd_assert(plugin
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
|| format
->channels
<= 1, return -ENXIO
);
642 for (channel
= 0; channel
< nchannels
; channel
++, v
++) {
645 v
->wanted
= (stream
== SNDRV_PCM_STREAM_CAPTURE
);
647 v
->area
.first
= channel
* width
;
648 v
->area
.step
= nchannels
* width
;
653 static int snd_pcm_plug_playback_channels_mask(struct snd_pcm_substream
*plug
,
654 unsigned long *client_vmask
)
656 struct snd_pcm_plugin
*plugin
= snd_pcm_plug_last(plug
);
657 if (plugin
== NULL
) {
660 int schannels
= plugin
->dst_format
.channels
;
661 DECLARE_BITMAP(bs
, schannels
);
662 unsigned long *srcmask
;
663 unsigned long *dstmask
= bs
;
665 bitmap_fill(dstmask
, schannels
);
668 err
= plugin
->src_channels_mask(plugin
, dstmask
, &srcmask
);
672 if (plugin
->prev
== NULL
)
674 plugin
= plugin
->prev
;
676 bitmap_and(client_vmask
, client_vmask
, dstmask
, plugin
->src_format
.channels
);
681 static int snd_pcm_plug_playback_disable_useless_channels(struct snd_pcm_substream
*plug
,
682 struct snd_pcm_plugin_channel
*src_channels
)
684 struct snd_pcm_plugin
*plugin
= snd_pcm_plug_first(plug
);
685 unsigned int nchannels
= plugin
->src_format
.channels
;
686 DECLARE_BITMAP(bs
, nchannels
);
687 unsigned long *srcmask
= bs
;
689 unsigned int channel
;
690 for (channel
= 0; channel
< nchannels
; channel
++) {
691 if (src_channels
[channel
].enabled
)
692 set_bit(channel
, srcmask
);
694 clear_bit(channel
, srcmask
);
696 err
= snd_pcm_plug_playback_channels_mask(plug
, srcmask
);
699 for (channel
= 0; channel
< nchannels
; channel
++) {
700 if (!test_bit(channel
, srcmask
))
701 src_channels
[channel
].enabled
= 0;
706 static int snd_pcm_plug_capture_disable_useless_channels(struct snd_pcm_substream
*plug
,
707 struct snd_pcm_plugin_channel
*src_channels
,
708 struct snd_pcm_plugin_channel
*client_channels
)
710 struct snd_pcm_plugin
*plugin
= snd_pcm_plug_last(plug
);
711 unsigned int nchannels
= plugin
->dst_format
.channels
;
712 DECLARE_BITMAP(bs
, nchannels
);
713 unsigned long *dstmask
= bs
;
714 unsigned long *srcmask
;
716 unsigned int channel
;
717 for (channel
= 0; channel
< nchannels
; channel
++) {
718 if (client_channels
[channel
].enabled
)
719 set_bit(channel
, dstmask
);
721 clear_bit(channel
, dstmask
);
724 err
= plugin
->src_channels_mask(plugin
, dstmask
, &srcmask
);
728 plugin
= plugin
->prev
;
730 plugin
= snd_pcm_plug_first(plug
);
731 nchannels
= plugin
->src_format
.channels
;
732 for (channel
= 0; channel
< nchannels
; channel
++) {
733 if (!test_bit(channel
, dstmask
))
734 src_channels
[channel
].enabled
= 0;
739 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
)
741 struct snd_pcm_plugin
*plugin
, *next
;
742 struct snd_pcm_plugin_channel
*dst_channels
;
744 snd_pcm_sframes_t frames
= size
;
746 if ((err
= snd_pcm_plug_playback_disable_useless_channels(plug
, src_channels
)) < 0)
749 plugin
= snd_pcm_plug_first(plug
);
750 while (plugin
&& frames
> 0) {
751 if ((next
= plugin
->next
) != NULL
) {
752 snd_pcm_sframes_t frames1
= frames
;
753 if (plugin
->dst_frames
)
754 frames1
= plugin
->dst_frames(plugin
, frames
);
755 if ((err
= next
->client_channels(next
, frames1
, &dst_channels
)) < 0) {
758 if (err
!= frames1
) {
760 if (plugin
->src_frames
)
761 frames
= plugin
->src_frames(plugin
, frames1
);
765 pdprintf("write plugin: %s, %li\n", plugin
->name
, frames
);
766 if ((frames
= plugin
->transfer(plugin
, src_channels
, dst_channels
, frames
)) < 0)
768 src_channels
= dst_channels
;
771 return snd_pcm_plug_client_size(plug
, frames
);
774 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
)
776 struct snd_pcm_plugin
*plugin
, *next
;
777 struct snd_pcm_plugin_channel
*src_channels
, *dst_channels
;
778 snd_pcm_sframes_t frames
= size
;
781 frames
= snd_pcm_plug_slave_size(plug
, frames
);
786 plugin
= snd_pcm_plug_first(plug
);
787 while (plugin
&& frames
> 0) {
788 if ((next
= plugin
->next
) != NULL
) {
789 if ((err
= plugin
->client_channels(plugin
, frames
, &dst_channels
)) < 0) {
794 if ((err
= snd_pcm_plug_capture_disable_useless_channels(plug
, dst_channels
, dst_channels_final
)) < 0)
798 dst_channels
= dst_channels_final
;
800 pdprintf("read plugin: %s, %li\n", plugin
->name
, frames
);
801 if ((frames
= plugin
->transfer(plugin
, src_channels
, dst_channels
, frames
)) < 0)
804 src_channels
= dst_channels
;
809 int snd_pcm_area_silence(const struct snd_pcm_channel_area
*dst_area
, size_t dst_offset
,
810 size_t samples
, int format
)
812 /* FIXME: sub byte resolution and odd dst_offset */
814 unsigned int dst_step
;
816 const unsigned char *silence
;
819 dst
= dst_area
->addr
+ (dst_area
->first
+ dst_area
->step
* dst_offset
) / 8;
820 width
= snd_pcm_format_physical_width(format
);
823 if (dst_area
->step
== (unsigned int) width
&& width
>= 8)
824 return snd_pcm_format_set_silence(format
, dst
, samples
);
825 silence
= snd_pcm_format_silence_64(format
);
828 dst_step
= dst_area
->step
/ 8;
831 int dstbit
= dst_area
->first
% 8;
832 int dstbit_step
= dst_area
->step
% 8;
833 while (samples
-- > 0) {
839 dstbit
+= dstbit_step
;
847 while (samples
-- > 0) {
848 memcpy(dst
, silence
, width
);
855 int snd_pcm_area_copy(const struct snd_pcm_channel_area
*src_area
, size_t src_offset
,
856 const struct snd_pcm_channel_area
*dst_area
, size_t dst_offset
,
857 size_t samples
, int format
)
859 /* FIXME: sub byte resolution and odd dst_offset */
862 int src_step
, dst_step
;
863 src
= src_area
->addr
+ (src_area
->first
+ src_area
->step
* src_offset
) / 8;
865 return snd_pcm_area_silence(dst_area
, dst_offset
, samples
, format
);
866 dst
= dst_area
->addr
+ (dst_area
->first
+ dst_area
->step
* dst_offset
) / 8;
869 width
= snd_pcm_format_physical_width(format
);
872 if (src_area
->step
== (unsigned int) width
&&
873 dst_area
->step
== (unsigned int) width
&& width
>= 8) {
874 size_t bytes
= samples
* width
/ 8;
875 memcpy(dst
, src
, bytes
);
878 src_step
= src_area
->step
/ 8;
879 dst_step
= dst_area
->step
/ 8;
882 int srcbit
= src_area
->first
% 8;
883 int srcbit_step
= src_area
->step
% 8;
884 int dstbit
= dst_area
->first
% 8;
885 int dstbit_step
= dst_area
->step
% 8;
886 while (samples
-- > 0) {
887 unsigned char srcval
;
889 srcval
= *src
& 0x0f;
891 srcval
= (*src
& 0xf0) >> 4;
893 *dst
= (*dst
& 0xf0) | srcval
;
895 *dst
= (*dst
& 0x0f) | (srcval
<< 4);
897 srcbit
+= srcbit_step
;
903 dstbit
+= dstbit_step
;
911 while (samples
-- > 0) {
912 memcpy(dst
, src
, width
);