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>
29 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
31 #include <linux/slab.h>
32 #include <linux/time.h>
33 #include <linux/vmalloc.h>
34 #include <sound/core.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
37 #include "pcm_plugin.h"
39 #define snd_pcm_plug_first(plug) ((plug)->runtime->oss.plugin_first)
40 #define snd_pcm_plug_last(plug) ((plug)->runtime->oss.plugin_last)
42 static int snd_pcm_plugin_src_channels_mask(struct snd_pcm_plugin
*plugin
,
43 unsigned long *dst_vmask
,
44 unsigned long **src_vmask
)
46 unsigned long *vmask
= plugin
->src_vmask
;
47 bitmap_copy(vmask
, dst_vmask
, plugin
->src_format
.channels
);
52 static int snd_pcm_plugin_dst_channels_mask(struct snd_pcm_plugin
*plugin
,
53 unsigned long *src_vmask
,
54 unsigned long **dst_vmask
)
56 unsigned long *vmask
= plugin
->dst_vmask
;
57 bitmap_copy(vmask
, src_vmask
, plugin
->dst_format
.channels
);
63 * because some cards might have rates "very close", we ignore
64 * all "resampling" requests within +-5%
66 static int rate_match(unsigned int src_rate
, unsigned int dst_rate
)
68 unsigned int low
= (src_rate
* 95) / 100;
69 unsigned int high
= (src_rate
* 105) / 100;
70 return dst_rate
>= low
&& dst_rate
<= high
;
73 static int snd_pcm_plugin_alloc(struct snd_pcm_plugin
*plugin
, snd_pcm_uframes_t frames
)
75 struct snd_pcm_plugin_format
*format
;
79 struct snd_pcm_plugin_channel
*c
;
81 if (plugin
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
82 format
= &plugin
->src_format
;
84 format
= &plugin
->dst_format
;
86 if ((width
= snd_pcm_format_physical_width(format
->format
)) < 0)
88 size
= frames
* format
->channels
* width
;
89 snd_assert((size
% 8) == 0, return -ENXIO
);
91 if (plugin
->buf_frames
< frames
) {
93 plugin
->buf
= vmalloc(size
);
94 plugin
->buf_frames
= frames
;
97 plugin
->buf_frames
= 0;
100 c
= plugin
->buf_channels
;
101 if (plugin
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
) {
102 for (channel
= 0; channel
< format
->channels
; channel
++, c
++) {
106 c
->area
.addr
= plugin
->buf
;
107 c
->area
.first
= channel
* width
;
108 c
->area
.step
= format
->channels
* width
;
110 } else if (plugin
->access
== SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
) {
111 snd_assert((size
% format
->channels
) == 0,);
112 size
/= format
->channels
;
113 for (channel
= 0; channel
< format
->channels
; channel
++, c
++) {
117 c
->area
.addr
= plugin
->buf
+ (channel
* size
);
119 c
->area
.step
= width
;
126 int snd_pcm_plug_alloc(struct snd_pcm_substream
*plug
, snd_pcm_uframes_t frames
)
129 snd_assert(snd_pcm_plug_first(plug
) != NULL
, return -ENXIO
);
130 if (snd_pcm_plug_stream(plug
) == SNDRV_PCM_STREAM_PLAYBACK
) {
131 struct snd_pcm_plugin
*plugin
= snd_pcm_plug_first(plug
);
132 while (plugin
->next
) {
133 if (plugin
->dst_frames
)
134 frames
= plugin
->dst_frames(plugin
, frames
);
135 snd_assert(frames
> 0, return -ENXIO
);
136 plugin
= plugin
->next
;
137 err
= snd_pcm_plugin_alloc(plugin
, frames
);
142 struct snd_pcm_plugin
*plugin
= snd_pcm_plug_last(plug
);
143 while (plugin
->prev
) {
144 if (plugin
->src_frames
)
145 frames
= plugin
->src_frames(plugin
, frames
);
146 snd_assert(frames
> 0, return -ENXIO
);
147 plugin
= plugin
->prev
;
148 err
= snd_pcm_plugin_alloc(plugin
, frames
);
157 snd_pcm_sframes_t
snd_pcm_plugin_client_channels(struct snd_pcm_plugin
*plugin
,
158 snd_pcm_uframes_t frames
,
159 struct snd_pcm_plugin_channel
**channels
)
161 *channels
= plugin
->buf_channels
;
165 int snd_pcm_plugin_build(struct snd_pcm_substream
*plug
,
167 struct snd_pcm_plugin_format
*src_format
,
168 struct snd_pcm_plugin_format
*dst_format
,
170 struct snd_pcm_plugin
**ret
)
172 struct snd_pcm_plugin
*plugin
;
173 unsigned int channels
;
175 snd_assert(plug
!= NULL
, return -ENXIO
);
176 snd_assert(src_format
!= NULL
&& dst_format
!= NULL
, return -ENXIO
);
177 plugin
= kzalloc(sizeof(*plugin
) + extra
, GFP_KERNEL
);
182 plugin
->stream
= snd_pcm_plug_stream(plug
);
183 plugin
->access
= SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
184 plugin
->src_format
= *src_format
;
185 plugin
->src_width
= snd_pcm_format_physical_width(src_format
->format
);
186 snd_assert(plugin
->src_width
> 0, );
187 plugin
->dst_format
= *dst_format
;
188 plugin
->dst_width
= snd_pcm_format_physical_width(dst_format
->format
);
189 snd_assert(plugin
->dst_width
> 0, );
190 if (plugin
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
191 channels
= src_format
->channels
;
193 channels
= dst_format
->channels
;
194 plugin
->buf_channels
= kcalloc(channels
, sizeof(*plugin
->buf_channels
), GFP_KERNEL
);
195 if (plugin
->buf_channels
== NULL
) {
196 snd_pcm_plugin_free(plugin
);
199 plugin
->src_vmask
= bitmap_alloc(src_format
->channels
);
200 if (plugin
->src_vmask
== NULL
) {
201 snd_pcm_plugin_free(plugin
);
204 plugin
->dst_vmask
= bitmap_alloc(dst_format
->channels
);
205 if (plugin
->dst_vmask
== NULL
) {
206 snd_pcm_plugin_free(plugin
);
209 plugin
->client_channels
= snd_pcm_plugin_client_channels
;
210 plugin
->src_channels_mask
= snd_pcm_plugin_src_channels_mask
;
211 plugin
->dst_channels_mask
= snd_pcm_plugin_dst_channels_mask
;
216 int snd_pcm_plugin_free(struct snd_pcm_plugin
*plugin
)
220 if (plugin
->private_free
)
221 plugin
->private_free(plugin
);
222 kfree(plugin
->buf_channels
);
224 kfree(plugin
->src_vmask
);
225 kfree(plugin
->dst_vmask
);
230 snd_pcm_sframes_t
snd_pcm_plug_client_size(struct snd_pcm_substream
*plug
, snd_pcm_uframes_t drv_frames
)
232 struct snd_pcm_plugin
*plugin
, *plugin_prev
, *plugin_next
;
233 int stream
= snd_pcm_plug_stream(plug
);
235 snd_assert(plug
!= NULL
, return -ENXIO
);
238 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
239 plugin
= snd_pcm_plug_last(plug
);
240 while (plugin
&& drv_frames
> 0) {
241 plugin_prev
= plugin
->prev
;
242 if (plugin
->src_frames
)
243 drv_frames
= plugin
->src_frames(plugin
, drv_frames
);
244 plugin
= plugin_prev
;
246 } else if (stream
== SNDRV_PCM_STREAM_CAPTURE
) {
247 plugin
= snd_pcm_plug_first(plug
);
248 while (plugin
&& drv_frames
> 0) {
249 plugin_next
= plugin
->next
;
250 if (plugin
->dst_frames
)
251 drv_frames
= plugin
->dst_frames(plugin
, drv_frames
);
252 plugin
= plugin_next
;
259 snd_pcm_sframes_t
snd_pcm_plug_slave_size(struct snd_pcm_substream
*plug
, snd_pcm_uframes_t clt_frames
)
261 struct snd_pcm_plugin
*plugin
, *plugin_prev
, *plugin_next
;
262 snd_pcm_sframes_t frames
;
263 int stream
= snd_pcm_plug_stream(plug
);
265 snd_assert(plug
!= NULL
, return -ENXIO
);
269 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
270 plugin
= snd_pcm_plug_first(plug
);
271 while (plugin
&& frames
> 0) {
272 plugin_next
= plugin
->next
;
273 if (plugin
->dst_frames
) {
274 frames
= plugin
->dst_frames(plugin
, frames
);
278 plugin
= plugin_next
;
280 } else if (stream
== SNDRV_PCM_STREAM_CAPTURE
) {
281 plugin
= snd_pcm_plug_last(plug
);
283 plugin_prev
= plugin
->prev
;
284 if (plugin
->src_frames
) {
285 frames
= plugin
->src_frames(plugin
, frames
);
289 plugin
= plugin_prev
;
296 static int snd_pcm_plug_formats(struct snd_mask
*mask
, int format
)
298 struct snd_mask formats
= *mask
;
299 u64 linfmts
= (SNDRV_PCM_FMTBIT_U8
| SNDRV_PCM_FMTBIT_S8
|
300 SNDRV_PCM_FMTBIT_U16_LE
| SNDRV_PCM_FMTBIT_S16_LE
|
301 SNDRV_PCM_FMTBIT_U16_BE
| SNDRV_PCM_FMTBIT_S16_BE
|
302 SNDRV_PCM_FMTBIT_U24_LE
| SNDRV_PCM_FMTBIT_S24_LE
|
303 SNDRV_PCM_FMTBIT_U24_BE
| SNDRV_PCM_FMTBIT_S24_BE
|
304 SNDRV_PCM_FMTBIT_U32_LE
| SNDRV_PCM_FMTBIT_S32_LE
|
305 SNDRV_PCM_FMTBIT_U32_BE
| SNDRV_PCM_FMTBIT_S32_BE
);
306 snd_mask_set(&formats
, SNDRV_PCM_FORMAT_MU_LAW
);
308 if (formats
.bits
[0] & (u32
)linfmts
)
309 formats
.bits
[0] |= (u32
)linfmts
;
310 if (formats
.bits
[1] & (u32
)(linfmts
>> 32))
311 formats
.bits
[1] |= (u32
)(linfmts
>> 32);
312 return snd_mask_test(&formats
, format
);
315 static int preferred_formats
[] = {
316 SNDRV_PCM_FORMAT_S16_LE
,
317 SNDRV_PCM_FORMAT_S16_BE
,
318 SNDRV_PCM_FORMAT_U16_LE
,
319 SNDRV_PCM_FORMAT_U16_BE
,
320 SNDRV_PCM_FORMAT_S24_LE
,
321 SNDRV_PCM_FORMAT_S24_BE
,
322 SNDRV_PCM_FORMAT_U24_LE
,
323 SNDRV_PCM_FORMAT_U24_BE
,
324 SNDRV_PCM_FORMAT_S32_LE
,
325 SNDRV_PCM_FORMAT_S32_BE
,
326 SNDRV_PCM_FORMAT_U32_LE
,
327 SNDRV_PCM_FORMAT_U32_BE
,
332 int snd_pcm_plug_slave_format(int format
, struct snd_mask
*format_mask
)
334 if (snd_mask_test(format_mask
, format
))
336 if (! snd_pcm_plug_formats(format_mask
, format
))
338 if (snd_pcm_format_linear(format
)) {
339 int width
= snd_pcm_format_width(format
);
340 int unsignd
= snd_pcm_format_unsigned(format
);
341 int big
= snd_pcm_format_big_endian(format
);
343 int wid
, width1
=width
;
345 for (wid
= 0; wid
< 4; ++wid
) {
347 for (end
= 0; end
< 2; ++end
) {
348 int sgn
, unsignd1
= unsignd
;
349 for (sgn
= 0; sgn
< 2; ++sgn
) {
350 format1
= snd_pcm_build_linear_format(width1
, unsignd1
, big1
);
352 snd_mask_test(format_mask
, format1
))
354 unsignd1
= !unsignd1
;
370 case SNDRV_PCM_FORMAT_MU_LAW
:
371 for (i
= 0; i
< ARRAY_SIZE(preferred_formats
); ++i
) {
372 int format1
= preferred_formats
[i
];
373 if (snd_mask_test(format_mask
, format1
))
382 int snd_pcm_plug_format_plugins(struct snd_pcm_substream
*plug
,
383 struct snd_pcm_hw_params
*params
,
384 struct snd_pcm_hw_params
*slave_params
)
386 struct snd_pcm_plugin_format tmpformat
;
387 struct snd_pcm_plugin_format dstformat
;
388 struct snd_pcm_plugin_format srcformat
;
389 int src_access
, dst_access
;
390 struct snd_pcm_plugin
*plugin
= NULL
;
392 int stream
= snd_pcm_plug_stream(plug
);
393 int slave_interleaved
= (params_channels(slave_params
) == 1 ||
394 params_access(slave_params
) == SNDRV_PCM_ACCESS_RW_INTERLEAVED
);
397 case SNDRV_PCM_STREAM_PLAYBACK
:
398 dstformat
.format
= params_format(slave_params
);
399 dstformat
.rate
= params_rate(slave_params
);
400 dstformat
.channels
= params_channels(slave_params
);
401 srcformat
.format
= params_format(params
);
402 srcformat
.rate
= params_rate(params
);
403 srcformat
.channels
= params_channels(params
);
404 src_access
= SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
405 dst_access
= (slave_interleaved
? SNDRV_PCM_ACCESS_RW_INTERLEAVED
:
406 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
);
408 case SNDRV_PCM_STREAM_CAPTURE
:
409 dstformat
.format
= params_format(params
);
410 dstformat
.rate
= params_rate(params
);
411 dstformat
.channels
= params_channels(params
);
412 srcformat
.format
= params_format(slave_params
);
413 srcformat
.rate
= params_rate(slave_params
);
414 srcformat
.channels
= params_channels(slave_params
);
415 src_access
= (slave_interleaved
? SNDRV_PCM_ACCESS_RW_INTERLEAVED
:
416 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED
);
417 dst_access
= SNDRV_PCM_ACCESS_RW_INTERLEAVED
;
423 tmpformat
= srcformat
;
425 pdprintf("srcformat: format=%i, rate=%i, channels=%i\n",
429 pdprintf("dstformat: format=%i, rate=%i, channels=%i\n",
434 /* Format change (linearization) */
435 if ((srcformat
.format
!= dstformat
.format
||
436 !rate_match(srcformat
.rate
, dstformat
.rate
) ||
437 srcformat
.channels
!= dstformat
.channels
) &&
438 !snd_pcm_format_linear(srcformat
.format
)) {
439 if (snd_pcm_format_linear(dstformat
.format
))
440 tmpformat
.format
= dstformat
.format
;
442 tmpformat
.format
= SNDRV_PCM_FORMAT_S16
;
443 switch (srcformat
.format
) {
444 case SNDRV_PCM_FORMAT_MU_LAW
:
445 err
= snd_pcm_plugin_build_mulaw(plug
,
446 &srcformat
, &tmpformat
,
452 pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat
.format
, tmpformat
.format
, err
);
455 err
= snd_pcm_plugin_append(plugin
);
457 snd_pcm_plugin_free(plugin
);
460 srcformat
= tmpformat
;
461 src_access
= dst_access
;
464 /* channels reduction */
465 if (srcformat
.channels
> dstformat
.channels
) {
466 int sv
= srcformat
.channels
;
467 int dv
= dstformat
.channels
;
468 int *ttable
= kcalloc(dv
* sv
, sizeof(*ttable
), GFP_KERNEL
);
472 if (sv
== 2 && dv
== 1) {
479 for (v
= 0; v
< dv
; ++v
)
480 ttable
[v
* sv
+ v
] = FULL
;
482 tmpformat
.channels
= dstformat
.channels
;
483 if (rate_match(srcformat
.rate
, dstformat
.rate
) &&
484 snd_pcm_format_linear(dstformat
.format
))
485 tmpformat
.format
= dstformat
.format
;
486 err
= snd_pcm_plugin_build_route(plug
,
487 &srcformat
, &tmpformat
,
490 pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat
.channels
, tmpformat
.channels
, err
);
492 snd_pcm_plugin_free(plugin
);
495 err
= snd_pcm_plugin_append(plugin
);
497 snd_pcm_plugin_free(plugin
);
500 srcformat
= tmpformat
;
501 src_access
= dst_access
;
504 /* rate resampling */
505 if (!rate_match(srcformat
.rate
, dstformat
.rate
)) {
506 tmpformat
.rate
= dstformat
.rate
;
507 if (srcformat
.channels
== dstformat
.channels
&&
508 snd_pcm_format_linear(dstformat
.format
))
509 tmpformat
.format
= dstformat
.format
;
510 err
= snd_pcm_plugin_build_rate(plug
,
511 &srcformat
, &tmpformat
,
513 pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat
.rate
, tmpformat
.rate
, err
);
515 snd_pcm_plugin_free(plugin
);
518 err
= snd_pcm_plugin_append(plugin
);
520 snd_pcm_plugin_free(plugin
);
523 srcformat
= tmpformat
;
524 src_access
= dst_access
;
527 /* channels extension */
528 if (srcformat
.channels
< dstformat
.channels
) {
529 int sv
= srcformat
.channels
;
530 int dv
= dstformat
.channels
;
531 int *ttable
= kcalloc(dv
* sv
, sizeof(*ttable
), GFP_KERNEL
);
537 for (v
= 0; v
< sv
; ++v
)
538 ttable
[v
* sv
+ v
] = FULL
;
542 /* Playback is spreaded on all channels */
544 for (vd
= 0, vs
= 0; vd
< dv
; ++vd
) {
545 ttable
[vd
* sv
+ vs
] = FULL
;
552 tmpformat
.channels
= dstformat
.channels
;
553 if (snd_pcm_format_linear(dstformat
.format
))
554 tmpformat
.format
= dstformat
.format
;
555 err
= snd_pcm_plugin_build_route(plug
,
556 &srcformat
, &tmpformat
,
559 pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat
.channels
, tmpformat
.channels
, err
);
561 snd_pcm_plugin_free(plugin
);
564 err
= snd_pcm_plugin_append(plugin
);
566 snd_pcm_plugin_free(plugin
);
569 srcformat
= tmpformat
;
570 src_access
= dst_access
;
574 if (srcformat
.format
!= dstformat
.format
) {
575 tmpformat
.format
= dstformat
.format
;
576 if (tmpformat
.format
== SNDRV_PCM_FORMAT_MU_LAW
) {
577 err
= snd_pcm_plugin_build_mulaw(plug
,
578 &srcformat
, &tmpformat
,
581 else if (snd_pcm_format_linear(srcformat
.format
) &&
582 snd_pcm_format_linear(tmpformat
.format
)) {
583 err
= snd_pcm_plugin_build_linear(plug
,
584 &srcformat
, &tmpformat
,
589 pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat
.format
, tmpformat
.format
, err
);
592 err
= snd_pcm_plugin_append(plugin
);
594 snd_pcm_plugin_free(plugin
);
597 srcformat
= tmpformat
;
598 src_access
= dst_access
;
602 if (src_access
!= dst_access
) {
603 err
= snd_pcm_plugin_build_copy(plug
,
607 pdprintf("interleave change (copy: returns %i)\n", err
);
610 err
= snd_pcm_plugin_append(plugin
);
612 snd_pcm_plugin_free(plugin
);
620 snd_pcm_sframes_t
snd_pcm_plug_client_channels_buf(struct snd_pcm_substream
*plug
,
622 snd_pcm_uframes_t count
,
623 struct snd_pcm_plugin_channel
**channels
)
625 struct snd_pcm_plugin
*plugin
;
626 struct snd_pcm_plugin_channel
*v
;
627 struct snd_pcm_plugin_format
*format
;
628 int width
, nchannels
, channel
;
629 int stream
= snd_pcm_plug_stream(plug
);
631 snd_assert(buf
!= NULL
, return -ENXIO
);
632 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
633 plugin
= snd_pcm_plug_first(plug
);
634 format
= &plugin
->src_format
;
636 plugin
= snd_pcm_plug_last(plug
);
637 format
= &plugin
->dst_format
;
639 v
= plugin
->buf_channels
;
641 if ((width
= snd_pcm_format_physical_width(format
->format
)) < 0)
643 nchannels
= format
->channels
;
644 snd_assert(plugin
->access
== SNDRV_PCM_ACCESS_RW_INTERLEAVED
|| format
->channels
<= 1, return -ENXIO
);
645 for (channel
= 0; channel
< nchannels
; channel
++, v
++) {
648 v
->wanted
= (stream
== SNDRV_PCM_STREAM_CAPTURE
);
650 v
->area
.first
= channel
* width
;
651 v
->area
.step
= nchannels
* width
;
656 static int snd_pcm_plug_playback_channels_mask(struct snd_pcm_substream
*plug
,
657 unsigned long *client_vmask
)
659 struct snd_pcm_plugin
*plugin
= snd_pcm_plug_last(plug
);
660 if (plugin
== NULL
) {
663 int schannels
= plugin
->dst_format
.channels
;
664 DECLARE_BITMAP(bs
, schannels
);
665 unsigned long *srcmask
;
666 unsigned long *dstmask
= bs
;
668 bitmap_fill(dstmask
, schannels
);
671 err
= plugin
->src_channels_mask(plugin
, dstmask
, &srcmask
);
675 if (plugin
->prev
== NULL
)
677 plugin
= plugin
->prev
;
679 bitmap_and(client_vmask
, client_vmask
, dstmask
, plugin
->src_format
.channels
);
684 static int snd_pcm_plug_playback_disable_useless_channels(struct snd_pcm_substream
*plug
,
685 struct snd_pcm_plugin_channel
*src_channels
)
687 struct snd_pcm_plugin
*plugin
= snd_pcm_plug_first(plug
);
688 unsigned int nchannels
= plugin
->src_format
.channels
;
689 DECLARE_BITMAP(bs
, nchannels
);
690 unsigned long *srcmask
= bs
;
692 unsigned int channel
;
693 for (channel
= 0; channel
< nchannels
; channel
++) {
694 if (src_channels
[channel
].enabled
)
695 set_bit(channel
, srcmask
);
697 clear_bit(channel
, srcmask
);
699 err
= snd_pcm_plug_playback_channels_mask(plug
, srcmask
);
702 for (channel
= 0; channel
< nchannels
; channel
++) {
703 if (!test_bit(channel
, srcmask
))
704 src_channels
[channel
].enabled
= 0;
709 static int snd_pcm_plug_capture_disable_useless_channels(struct snd_pcm_substream
*plug
,
710 struct snd_pcm_plugin_channel
*src_channels
,
711 struct snd_pcm_plugin_channel
*client_channels
)
713 struct snd_pcm_plugin
*plugin
= snd_pcm_plug_last(plug
);
714 unsigned int nchannels
= plugin
->dst_format
.channels
;
715 DECLARE_BITMAP(bs
, nchannels
);
716 unsigned long *dstmask
= bs
;
717 unsigned long *srcmask
;
719 unsigned int channel
;
720 for (channel
= 0; channel
< nchannels
; channel
++) {
721 if (client_channels
[channel
].enabled
)
722 set_bit(channel
, dstmask
);
724 clear_bit(channel
, dstmask
);
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 (!test_bit(channel
, dstmask
))
737 src_channels
[channel
].enabled
= 0;
742 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
)
744 struct snd_pcm_plugin
*plugin
, *next
;
745 struct snd_pcm_plugin_channel
*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(struct snd_pcm_substream
*plug
, struct snd_pcm_plugin_channel
*dst_channels_final
, snd_pcm_uframes_t size
)
779 struct snd_pcm_plugin
*plugin
, *next
;
780 struct snd_pcm_plugin_channel
*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 struct snd_pcm_channel_area
*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 struct snd_pcm_channel_area
*src_area
, size_t src_offset
,
859 const struct snd_pcm_channel_area
*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
);