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(snd_pcm_plugin_t
*plugin
,
43 bitset_t
*vmask
= plugin
->src_vmask
;
44 bitset_copy(vmask
, dst_vmask
, plugin
->src_format
.channels
);
49 static int snd_pcm_plugin_dst_channels_mask(snd_pcm_plugin_t
*plugin
,
53 bitset_t
*vmask
= plugin
->dst_vmask
;
54 bitset_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(snd_pcm_plugin_t
*plugin
, snd_pcm_uframes_t frames
)
72 snd_pcm_plugin_format_t
*format
;
76 snd_pcm_plugin_channel_t
*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(snd_pcm_plug_t
*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 snd_pcm_plugin_t
*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 snd_pcm_plugin_t
*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(snd_pcm_plugin_t
*plugin
,
155 snd_pcm_uframes_t frames
,
156 snd_pcm_plugin_channel_t
**channels
)
158 *channels
= plugin
->buf_channels
;
162 int snd_pcm_plugin_build(snd_pcm_plug_t
*plug
,
164 snd_pcm_plugin_format_t
*src_format
,
165 snd_pcm_plugin_format_t
*dst_format
,
167 snd_pcm_plugin_t
**ret
)
169 snd_pcm_plugin_t
*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
= kcalloc(1, 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
= bitset_alloc(src_format
->channels
);
197 if (plugin
->src_vmask
== NULL
) {
198 snd_pcm_plugin_free(plugin
);
201 plugin
->dst_vmask
= bitset_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(snd_pcm_plugin_t
*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(snd_pcm_plug_t
*plug
, snd_pcm_uframes_t drv_frames
)
229 snd_pcm_plugin_t
*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(snd_pcm_plug_t
*plug
, snd_pcm_uframes_t clt_frames
)
258 snd_pcm_plugin_t
*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(snd_mask_t
*mask
, int format
)
295 snd_mask_t 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
, snd_mask_t
*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(snd_pcm_plug_t
*plug
,
380 snd_pcm_hw_params_t
*params
,
381 snd_pcm_hw_params_t
*slave_params
)
383 snd_pcm_plugin_format_t tmpformat
;
384 snd_pcm_plugin_format_t dstformat
;
385 snd_pcm_plugin_format_t srcformat
;
386 int src_access
, dst_access
;
387 snd_pcm_plugin_t
*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 route_ttable_entry_t
*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 route_ttable_entry_t
*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(snd_pcm_plug_t
*plug
,
619 snd_pcm_uframes_t count
,
620 snd_pcm_plugin_channel_t
**channels
)
622 snd_pcm_plugin_t
*plugin
;
623 snd_pcm_plugin_channel_t
*v
;
624 snd_pcm_plugin_format_t
*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(snd_pcm_plug_t
*plug
,
654 bitset_t
*client_vmask
)
656 snd_pcm_plugin_t
*plugin
= snd_pcm_plug_last(plug
);
657 if (plugin
== NULL
) {
660 int schannels
= plugin
->dst_format
.channels
;
661 bitset_t bs
[bitset_size(schannels
)];
663 bitset_t
*dstmask
= bs
;
665 bitset_one(dstmask
, schannels
);
666 if (plugin
== NULL
) {
667 bitset_and(client_vmask
, dstmask
, schannels
);
671 err
= plugin
->src_channels_mask(plugin
, dstmask
, &srcmask
);
675 if (plugin
->prev
== NULL
)
677 plugin
= plugin
->prev
;
679 bitset_and(client_vmask
, dstmask
, plugin
->src_format
.channels
);
684 static int snd_pcm_plug_playback_disable_useless_channels(snd_pcm_plug_t
*plug
,
685 snd_pcm_plugin_channel_t
*src_channels
)
687 snd_pcm_plugin_t
*plugin
= snd_pcm_plug_first(plug
);
688 unsigned int nchannels
= plugin
->src_format
.channels
;
689 bitset_t bs
[bitset_size(nchannels
)];
690 bitset_t
*srcmask
= bs
;
692 unsigned int channel
;
693 for (channel
= 0; channel
< nchannels
; channel
++) {
694 if (src_channels
[channel
].enabled
)
695 bitset_set(srcmask
, channel
);
697 bitset_reset(srcmask
, channel
);
699 err
= snd_pcm_plug_playback_channels_mask(plug
, srcmask
);
702 for (channel
= 0; channel
< nchannels
; channel
++) {
703 if (!bitset_get(srcmask
, channel
))
704 src_channels
[channel
].enabled
= 0;
709 static int snd_pcm_plug_capture_disable_useless_channels(snd_pcm_plug_t
*plug
,
710 snd_pcm_plugin_channel_t
*src_channels
,
711 snd_pcm_plugin_channel_t
*client_channels
)
713 snd_pcm_plugin_t
*plugin
= snd_pcm_plug_last(plug
);
714 unsigned int nchannels
= plugin
->dst_format
.channels
;
715 bitset_t bs
[bitset_size(nchannels
)];
716 bitset_t
*dstmask
= bs
;
719 unsigned int channel
;
720 for (channel
= 0; channel
< nchannels
; channel
++) {
721 if (client_channels
[channel
].enabled
)
722 bitset_set(dstmask
, channel
);
724 bitset_reset(dstmask
, channel
);
727 err
= plugin
->src_channels_mask(plugin
, dstmask
, &srcmask
);
731 plugin
= plugin
->prev
;
733 plugin
= snd_pcm_plug_first(plug
);
734 nchannels
= plugin
->src_format
.channels
;
735 for (channel
= 0; channel
< nchannels
; channel
++) {
736 if (!bitset_get(dstmask
, channel
))
737 src_channels
[channel
].enabled
= 0;
742 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
)
744 snd_pcm_plugin_t
*plugin
, *next
;
745 snd_pcm_plugin_channel_t
*dst_channels
;
747 snd_pcm_sframes_t frames
= size
;
749 if ((err
= snd_pcm_plug_playback_disable_useless_channels(plug
, src_channels
)) < 0)
752 plugin
= snd_pcm_plug_first(plug
);
753 while (plugin
&& frames
> 0) {
754 if ((next
= plugin
->next
) != NULL
) {
755 snd_pcm_sframes_t frames1
= frames
;
756 if (plugin
->dst_frames
)
757 frames1
= plugin
->dst_frames(plugin
, frames
);
758 if ((err
= next
->client_channels(next
, frames1
, &dst_channels
)) < 0) {
761 if (err
!= frames1
) {
763 if (plugin
->src_frames
)
764 frames
= plugin
->src_frames(plugin
, frames1
);
768 pdprintf("write plugin: %s, %li\n", plugin
->name
, frames
);
769 if ((frames
= plugin
->transfer(plugin
, src_channels
, dst_channels
, frames
)) < 0)
771 src_channels
= dst_channels
;
774 return snd_pcm_plug_client_size(plug
, frames
);
777 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
)
779 snd_pcm_plugin_t
*plugin
, *next
;
780 snd_pcm_plugin_channel_t
*src_channels
, *dst_channels
;
781 snd_pcm_sframes_t frames
= size
;
784 frames
= snd_pcm_plug_slave_size(plug
, frames
);
789 plugin
= snd_pcm_plug_first(plug
);
790 while (plugin
&& frames
> 0) {
791 if ((next
= plugin
->next
) != NULL
) {
792 if ((err
= plugin
->client_channels(plugin
, frames
, &dst_channels
)) < 0) {
797 if ((err
= snd_pcm_plug_capture_disable_useless_channels(plug
, dst_channels
, dst_channels_final
)) < 0)
801 dst_channels
= dst_channels_final
;
803 pdprintf("read plugin: %s, %li\n", plugin
->name
, frames
);
804 if ((frames
= plugin
->transfer(plugin
, src_channels
, dst_channels
, frames
)) < 0)
807 src_channels
= dst_channels
;
812 int snd_pcm_area_silence(const snd_pcm_channel_area_t
*dst_area
, size_t dst_offset
,
813 size_t samples
, int format
)
815 /* FIXME: sub byte resolution and odd dst_offset */
817 unsigned int dst_step
;
819 const unsigned char *silence
;
822 dst
= dst_area
->addr
+ (dst_area
->first
+ dst_area
->step
* dst_offset
) / 8;
823 width
= snd_pcm_format_physical_width(format
);
826 if (dst_area
->step
== (unsigned int) width
&& width
>= 8)
827 return snd_pcm_format_set_silence(format
, dst
, samples
);
828 silence
= snd_pcm_format_silence_64(format
);
831 dst_step
= dst_area
->step
/ 8;
834 int dstbit
= dst_area
->first
% 8;
835 int dstbit_step
= dst_area
->step
% 8;
836 while (samples
-- > 0) {
842 dstbit
+= dstbit_step
;
850 while (samples
-- > 0) {
851 memcpy(dst
, silence
, width
);
858 int snd_pcm_area_copy(const snd_pcm_channel_area_t
*src_area
, size_t src_offset
,
859 const snd_pcm_channel_area_t
*dst_area
, size_t dst_offset
,
860 size_t samples
, int format
)
862 /* FIXME: sub byte resolution and odd dst_offset */
865 int src_step
, dst_step
;
866 src
= src_area
->addr
+ (src_area
->first
+ src_area
->step
* src_offset
) / 8;
868 return snd_pcm_area_silence(dst_area
, dst_offset
, samples
, format
);
869 dst
= dst_area
->addr
+ (dst_area
->first
+ dst_area
->step
* dst_offset
) / 8;
872 width
= snd_pcm_format_physical_width(format
);
875 if (src_area
->step
== (unsigned int) width
&&
876 dst_area
->step
== (unsigned int) width
&& width
>= 8) {
877 size_t bytes
= samples
* width
/ 8;
878 memcpy(dst
, src
, bytes
);
881 src_step
= src_area
->step
/ 8;
882 dst_step
= dst_area
->step
/ 8;
885 int srcbit
= src_area
->first
% 8;
886 int srcbit_step
= src_area
->step
% 8;
887 int dstbit
= dst_area
->first
% 8;
888 int dstbit_step
= dst_area
->step
% 8;
889 while (samples
-- > 0) {
890 unsigned char srcval
;
892 srcval
= *src
& 0x0f;
894 srcval
= (*src
& 0xf0) >> 4;
896 *dst
= (*dst
& 0xf0) | srcval
;
898 *dst
= (*dst
& 0x0f) | (srcval
<< 4);
900 srcbit
+= srcbit_step
;
906 dstbit
+= dstbit_step
;
914 while (samples
-- > 0) {
915 memcpy(dst
, src
, width
);