2 * PCM Plug-In shared (kernel/library) code
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 <linux/slab.h>
28 #include <linux/time.h>
29 #include <linux/vmalloc.h>
30 #include <sound/core.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include "pcm_plugin.h"
35 #define snd_pcm_plug_first(plug) ((plug)->runtime->oss.plugin_first)
36 #define snd_pcm_plug_last(plug) ((plug)->runtime->oss.plugin_last)
39 * because some cards might have rates "very close", we ignore
40 * all "resampling" requests within +-5%
42 static int rate_match(unsigned int src_rate, unsigned int dst_rate)
44 unsigned int low = (src_rate * 95) / 100;
45 unsigned int high = (src_rate * 105) / 100;
46 return dst_rate >= low && dst_rate <= high;
49 static int snd_pcm_plugin_alloc(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames)
51 struct snd_pcm_plugin_format *format;
55 struct snd_pcm_plugin_channel *c;
57 if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK) {
58 format = &plugin->src_format;
60 format = &plugin->dst_format;
62 if ((width = snd_pcm_format_physical_width(format->format)) < 0)
64 size = frames * format->channels * width;
65 if (snd_BUG_ON(size % 8))
68 if (plugin->buf_frames < frames) {
70 plugin->buf = kvzalloc(size, GFP_KERNEL);
71 plugin->buf_frames = frames;
74 plugin->buf_frames = 0;
77 c = plugin->buf_channels;
78 if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
79 for (channel = 0; channel < format->channels; channel++, c++) {
83 c->area.addr = plugin->buf;
84 c->area.first = channel * width;
85 c->area.step = format->channels * width;
87 } else if (plugin->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
88 if (snd_BUG_ON(size % format->channels))
90 size /= format->channels;
91 for (channel = 0; channel < format->channels; channel++, c++) {
95 c->area.addr = plugin->buf + (channel * size);
104 int snd_pcm_plug_alloc(struct snd_pcm_substream *plug, snd_pcm_uframes_t frames)
107 if (snd_BUG_ON(!snd_pcm_plug_first(plug)))
109 if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) {
110 struct snd_pcm_plugin *plugin = snd_pcm_plug_first(plug);
111 while (plugin->next) {
112 if (plugin->dst_frames)
113 frames = plugin->dst_frames(plugin, frames);
114 if (snd_BUG_ON((snd_pcm_sframes_t)frames <= 0))
116 plugin = plugin->next;
117 err = snd_pcm_plugin_alloc(plugin, frames);
122 struct snd_pcm_plugin *plugin = snd_pcm_plug_last(plug);
123 while (plugin->prev) {
124 if (plugin->src_frames)
125 frames = plugin->src_frames(plugin, frames);
126 if (snd_BUG_ON((snd_pcm_sframes_t)frames <= 0))
128 plugin = plugin->prev;
129 err = snd_pcm_plugin_alloc(plugin, frames);
138 snd_pcm_sframes_t snd_pcm_plugin_client_channels(struct snd_pcm_plugin *plugin,
139 snd_pcm_uframes_t frames,
140 struct snd_pcm_plugin_channel **channels)
142 *channels = plugin->buf_channels;
146 int snd_pcm_plugin_build(struct snd_pcm_substream *plug,
148 struct snd_pcm_plugin_format *src_format,
149 struct snd_pcm_plugin_format *dst_format,
151 struct snd_pcm_plugin **ret)
153 struct snd_pcm_plugin *plugin;
154 unsigned int channels;
156 if (snd_BUG_ON(!plug))
158 if (snd_BUG_ON(!src_format || !dst_format))
160 plugin = kzalloc(sizeof(*plugin) + extra, GFP_KERNEL);
165 plugin->stream = snd_pcm_plug_stream(plug);
166 plugin->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
167 plugin->src_format = *src_format;
168 plugin->src_width = snd_pcm_format_physical_width(src_format->format);
169 snd_BUG_ON(plugin->src_width <= 0);
170 plugin->dst_format = *dst_format;
171 plugin->dst_width = snd_pcm_format_physical_width(dst_format->format);
172 snd_BUG_ON(plugin->dst_width <= 0);
173 if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK)
174 channels = src_format->channels;
176 channels = dst_format->channels;
177 plugin->buf_channels = kcalloc(channels, sizeof(*plugin->buf_channels), GFP_KERNEL);
178 if (plugin->buf_channels == NULL) {
179 snd_pcm_plugin_free(plugin);
182 plugin->client_channels = snd_pcm_plugin_client_channels;
187 int snd_pcm_plugin_free(struct snd_pcm_plugin *plugin)
191 if (plugin->private_free)
192 plugin->private_free(plugin);
193 kfree(plugin->buf_channels);
199 snd_pcm_sframes_t snd_pcm_plug_client_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t drv_frames)
201 struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
204 if (snd_BUG_ON(!plug))
208 stream = snd_pcm_plug_stream(plug);
209 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
210 plugin = snd_pcm_plug_last(plug);
211 while (plugin && drv_frames > 0) {
212 plugin_prev = plugin->prev;
213 if (plugin->src_frames)
214 drv_frames = plugin->src_frames(plugin, drv_frames);
215 plugin = plugin_prev;
217 } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
218 plugin = snd_pcm_plug_first(plug);
219 while (plugin && drv_frames > 0) {
220 plugin_next = plugin->next;
221 if (plugin->dst_frames)
222 drv_frames = plugin->dst_frames(plugin, drv_frames);
223 plugin = plugin_next;
230 snd_pcm_sframes_t snd_pcm_plug_slave_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t clt_frames)
232 struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
233 snd_pcm_sframes_t frames;
236 if (snd_BUG_ON(!plug))
241 stream = snd_pcm_plug_stream(plug);
242 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
243 plugin = snd_pcm_plug_first(plug);
244 while (plugin && frames > 0) {
245 plugin_next = plugin->next;
246 if (plugin->dst_frames) {
247 frames = plugin->dst_frames(plugin, frames);
251 plugin = plugin_next;
253 } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
254 plugin = snd_pcm_plug_last(plug);
256 plugin_prev = plugin->prev;
257 if (plugin->src_frames) {
258 frames = plugin->src_frames(plugin, frames);
262 plugin = plugin_prev;
269 static int snd_pcm_plug_formats(const struct snd_mask *mask,
270 snd_pcm_format_t format)
272 struct snd_mask formats = *mask;
273 u64 linfmts = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
274 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
275 SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
276 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_S24_LE |
277 SNDRV_PCM_FMTBIT_U24_BE | SNDRV_PCM_FMTBIT_S24_BE |
278 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_S24_3LE |
279 SNDRV_PCM_FMTBIT_U24_3BE | SNDRV_PCM_FMTBIT_S24_3BE |
280 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
281 SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE);
282 snd_mask_set(&formats, (__force int)SNDRV_PCM_FORMAT_MU_LAW);
284 if (formats.bits[0] & lower_32_bits(linfmts))
285 formats.bits[0] |= lower_32_bits(linfmts);
286 if (formats.bits[1] & upper_32_bits(linfmts))
287 formats.bits[1] |= upper_32_bits(linfmts);
288 return snd_mask_test(&formats, (__force int)format);
291 static snd_pcm_format_t preferred_formats[] = {
292 SNDRV_PCM_FORMAT_S16_LE,
293 SNDRV_PCM_FORMAT_S16_BE,
294 SNDRV_PCM_FORMAT_U16_LE,
295 SNDRV_PCM_FORMAT_U16_BE,
296 SNDRV_PCM_FORMAT_S24_3LE,
297 SNDRV_PCM_FORMAT_S24_3BE,
298 SNDRV_PCM_FORMAT_U24_3LE,
299 SNDRV_PCM_FORMAT_U24_3BE,
300 SNDRV_PCM_FORMAT_S24_LE,
301 SNDRV_PCM_FORMAT_S24_BE,
302 SNDRV_PCM_FORMAT_U24_LE,
303 SNDRV_PCM_FORMAT_U24_BE,
304 SNDRV_PCM_FORMAT_S32_LE,
305 SNDRV_PCM_FORMAT_S32_BE,
306 SNDRV_PCM_FORMAT_U32_LE,
307 SNDRV_PCM_FORMAT_U32_BE,
312 snd_pcm_format_t snd_pcm_plug_slave_format(snd_pcm_format_t format,
313 const struct snd_mask *format_mask)
317 if (snd_mask_test(format_mask, (__force int)format))
319 if (!snd_pcm_plug_formats(format_mask, format))
320 return (__force snd_pcm_format_t)-EINVAL;
321 if (snd_pcm_format_linear(format)) {
322 unsigned int width = snd_pcm_format_width(format);
323 int unsignd = snd_pcm_format_unsigned(format) > 0;
324 int big = snd_pcm_format_big_endian(format) > 0;
325 unsigned int badness, best = -1;
326 snd_pcm_format_t best_format = (__force snd_pcm_format_t)-1;
327 for (i = 0; i < ARRAY_SIZE(preferred_formats); i++) {
328 snd_pcm_format_t f = preferred_formats[i];
330 if (!snd_mask_test(format_mask, (__force int)f))
332 w = snd_pcm_format_width(f);
336 badness = width - w + 32;
337 badness += snd_pcm_format_unsigned(f) != unsignd;
338 badness += snd_pcm_format_big_endian(f) != big;
339 if (badness < best) {
344 if ((__force int)best_format >= 0)
347 return (__force snd_pcm_format_t)-EINVAL;
350 case SNDRV_PCM_FORMAT_MU_LAW:
351 for (i = 0; i < ARRAY_SIZE(preferred_formats); ++i) {
352 snd_pcm_format_t format1 = preferred_formats[i];
353 if (snd_mask_test(format_mask, (__force int)format1))
358 return (__force snd_pcm_format_t)-EINVAL;
363 int snd_pcm_plug_format_plugins(struct snd_pcm_substream *plug,
364 struct snd_pcm_hw_params *params,
365 struct snd_pcm_hw_params *slave_params)
367 struct snd_pcm_plugin_format tmpformat;
368 struct snd_pcm_plugin_format dstformat;
369 struct snd_pcm_plugin_format srcformat;
370 snd_pcm_access_t src_access, dst_access;
371 struct snd_pcm_plugin *plugin = NULL;
373 int stream = snd_pcm_plug_stream(plug);
374 int slave_interleaved = (params_channels(slave_params) == 1 ||
375 params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED);
378 case SNDRV_PCM_STREAM_PLAYBACK:
379 dstformat.format = params_format(slave_params);
380 dstformat.rate = params_rate(slave_params);
381 dstformat.channels = params_channels(slave_params);
382 srcformat.format = params_format(params);
383 srcformat.rate = params_rate(params);
384 srcformat.channels = params_channels(params);
385 src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
386 dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
387 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
389 case SNDRV_PCM_STREAM_CAPTURE:
390 dstformat.format = params_format(params);
391 dstformat.rate = params_rate(params);
392 dstformat.channels = params_channels(params);
393 srcformat.format = params_format(slave_params);
394 srcformat.rate = params_rate(slave_params);
395 srcformat.channels = params_channels(slave_params);
396 src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
397 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
398 dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
404 tmpformat = srcformat;
406 pdprintf("srcformat: format=%i, rate=%i, channels=%i\n",
410 pdprintf("dstformat: format=%i, rate=%i, channels=%i\n",
415 /* Format change (linearization) */
416 if (! rate_match(srcformat.rate, dstformat.rate) &&
417 ! snd_pcm_format_linear(srcformat.format)) {
418 if (srcformat.format != SNDRV_PCM_FORMAT_MU_LAW)
420 tmpformat.format = SNDRV_PCM_FORMAT_S16;
421 err = snd_pcm_plugin_build_mulaw(plug,
422 &srcformat, &tmpformat,
426 err = snd_pcm_plugin_append(plugin);
428 snd_pcm_plugin_free(plugin);
431 srcformat = tmpformat;
432 src_access = dst_access;
435 /* channels reduction */
436 if (srcformat.channels > dstformat.channels) {
437 tmpformat.channels = dstformat.channels;
438 err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
439 pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
442 err = snd_pcm_plugin_append(plugin);
444 snd_pcm_plugin_free(plugin);
447 srcformat = tmpformat;
448 src_access = dst_access;
451 /* rate resampling */
452 if (!rate_match(srcformat.rate, dstformat.rate)) {
453 if (srcformat.format != SNDRV_PCM_FORMAT_S16) {
454 /* convert to S16 for resampling */
455 tmpformat.format = SNDRV_PCM_FORMAT_S16;
456 err = snd_pcm_plugin_build_linear(plug,
457 &srcformat, &tmpformat,
461 err = snd_pcm_plugin_append(plugin);
463 snd_pcm_plugin_free(plugin);
466 srcformat = tmpformat;
467 src_access = dst_access;
469 tmpformat.rate = dstformat.rate;
470 err = snd_pcm_plugin_build_rate(plug,
471 &srcformat, &tmpformat,
473 pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
476 err = snd_pcm_plugin_append(plugin);
478 snd_pcm_plugin_free(plugin);
481 srcformat = tmpformat;
482 src_access = dst_access;
486 if (srcformat.format != dstformat.format) {
487 tmpformat.format = dstformat.format;
488 if (srcformat.format == SNDRV_PCM_FORMAT_MU_LAW ||
489 tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) {
490 err = snd_pcm_plugin_build_mulaw(plug,
491 &srcformat, &tmpformat,
494 else if (snd_pcm_format_linear(srcformat.format) &&
495 snd_pcm_format_linear(tmpformat.format)) {
496 err = snd_pcm_plugin_build_linear(plug,
497 &srcformat, &tmpformat,
502 pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
505 err = snd_pcm_plugin_append(plugin);
507 snd_pcm_plugin_free(plugin);
510 srcformat = tmpformat;
511 src_access = dst_access;
514 /* channels extension */
515 if (srcformat.channels < dstformat.channels) {
516 tmpformat.channels = dstformat.channels;
517 err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
518 pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
521 err = snd_pcm_plugin_append(plugin);
523 snd_pcm_plugin_free(plugin);
526 srcformat = tmpformat;
527 src_access = dst_access;
531 if (src_access != dst_access) {
532 err = snd_pcm_plugin_build_copy(plug,
536 pdprintf("interleave change (copy: returns %i)\n", err);
539 err = snd_pcm_plugin_append(plugin);
541 snd_pcm_plugin_free(plugin);
549 snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(struct snd_pcm_substream *plug,
551 snd_pcm_uframes_t count,
552 struct snd_pcm_plugin_channel **channels)
554 struct snd_pcm_plugin *plugin;
555 struct snd_pcm_plugin_channel *v;
556 struct snd_pcm_plugin_format *format;
557 int width, nchannels, channel;
558 int stream = snd_pcm_plug_stream(plug);
560 if (snd_BUG_ON(!buf))
562 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
563 plugin = snd_pcm_plug_first(plug);
564 format = &plugin->src_format;
566 plugin = snd_pcm_plug_last(plug);
567 format = &plugin->dst_format;
569 v = plugin->buf_channels;
571 if ((width = snd_pcm_format_physical_width(format->format)) < 0)
573 nchannels = format->channels;
574 if (snd_BUG_ON(plugin->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
575 format->channels > 1))
577 for (channel = 0; channel < nchannels; channel++, v++) {
580 v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE);
582 v->area.first = channel * width;
583 v->area.step = nchannels * width;
588 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)
590 struct snd_pcm_plugin *plugin, *next;
591 struct snd_pcm_plugin_channel *dst_channels;
593 snd_pcm_sframes_t frames = size;
595 plugin = snd_pcm_plug_first(plug);
599 if ((next = plugin->next) != NULL) {
600 snd_pcm_sframes_t frames1 = frames;
601 if (plugin->dst_frames) {
602 frames1 = plugin->dst_frames(plugin, frames);
606 if ((err = next->client_channels(next, frames1, &dst_channels)) < 0) {
609 if (err != frames1) {
611 if (plugin->src_frames) {
612 frames = plugin->src_frames(plugin, frames1);
619 pdprintf("write plugin: %s, %li\n", plugin->name, frames);
620 if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
622 src_channels = dst_channels;
625 return snd_pcm_plug_client_size(plug, frames);
628 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)
630 struct snd_pcm_plugin *plugin, *next;
631 struct snd_pcm_plugin_channel *src_channels, *dst_channels;
632 snd_pcm_sframes_t frames = size;
635 frames = snd_pcm_plug_slave_size(plug, frames);
640 plugin = snd_pcm_plug_first(plug);
641 while (plugin && frames > 0) {
642 if ((next = plugin->next) != NULL) {
643 if ((err = plugin->client_channels(plugin, frames, &dst_channels)) < 0) {
648 dst_channels = dst_channels_final;
650 pdprintf("read plugin: %s, %li\n", plugin->name, frames);
651 if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
654 src_channels = dst_channels;
659 int snd_pcm_area_silence(const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
660 size_t samples, snd_pcm_format_t format)
662 /* FIXME: sub byte resolution and odd dst_offset */
664 unsigned int dst_step;
666 const unsigned char *silence;
669 dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
670 width = snd_pcm_format_physical_width(format);
673 if (dst_area->step == (unsigned int) width && width >= 8)
674 return snd_pcm_format_set_silence(format, dst, samples);
675 silence = snd_pcm_format_silence_64(format);
678 dst_step = dst_area->step / 8;
681 int dstbit = dst_area->first % 8;
682 int dstbit_step = dst_area->step % 8;
683 while (samples-- > 0) {
689 dstbit += dstbit_step;
697 while (samples-- > 0) {
698 memcpy(dst, silence, width);
705 int snd_pcm_area_copy(const struct snd_pcm_channel_area *src_area, size_t src_offset,
706 const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
707 size_t samples, snd_pcm_format_t format)
709 /* FIXME: sub byte resolution and odd dst_offset */
712 int src_step, dst_step;
713 src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
715 return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
716 dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
719 width = snd_pcm_format_physical_width(format);
722 if (src_area->step == (unsigned int) width &&
723 dst_area->step == (unsigned int) width && width >= 8) {
724 size_t bytes = samples * width / 8;
725 memcpy(dst, src, bytes);
728 src_step = src_area->step / 8;
729 dst_step = dst_area->step / 8;
732 int srcbit = src_area->first % 8;
733 int srcbit_step = src_area->step % 8;
734 int dstbit = dst_area->first % 8;
735 int dstbit_step = dst_area->step % 8;
736 while (samples-- > 0) {
737 unsigned char srcval;
739 srcval = *src & 0x0f;
741 srcval = (*src & 0xf0) >> 4;
743 *dst = (*dst & 0xf0) | srcval;
745 *dst = (*dst & 0x0f) | (srcval << 4);
747 srcbit += srcbit_step;
753 dstbit += dstbit_step;
761 while (samples-- > 0) {
762 memcpy(dst, src, width);