2 * PCM Plug-In shared (kernel/library) code
3 * Copyright (c) 1999 by Jaroslav Kysela <perex@perex.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 <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_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_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 static snd_pcm_sframes_t calc_dst_frames(struct snd_pcm_substream *plug,
200 snd_pcm_sframes_t frames,
203 struct snd_pcm_plugin *plugin, *plugin_next;
205 plugin = snd_pcm_plug_first(plug);
206 while (plugin && frames > 0) {
207 plugin_next = plugin->next;
208 if (check_size && plugin->buf_frames &&
209 frames > plugin->buf_frames)
210 frames = plugin->buf_frames;
211 if (plugin->dst_frames) {
212 frames = plugin->dst_frames(plugin, frames);
216 plugin = plugin_next;
221 static snd_pcm_sframes_t calc_src_frames(struct snd_pcm_substream *plug,
222 snd_pcm_sframes_t frames,
225 struct snd_pcm_plugin *plugin, *plugin_prev;
227 plugin = snd_pcm_plug_last(plug);
228 while (plugin && frames > 0) {
229 plugin_prev = plugin->prev;
230 if (plugin->src_frames) {
231 frames = plugin->src_frames(plugin, frames);
235 if (check_size && plugin->buf_frames &&
236 frames > plugin->buf_frames)
237 frames = plugin->buf_frames;
238 plugin = plugin_prev;
243 snd_pcm_sframes_t snd_pcm_plug_client_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t drv_frames)
245 if (snd_BUG_ON(!plug))
247 switch (snd_pcm_plug_stream(plug)) {
248 case SNDRV_PCM_STREAM_PLAYBACK:
249 return calc_src_frames(plug, drv_frames, false);
250 case SNDRV_PCM_STREAM_CAPTURE:
251 return calc_dst_frames(plug, drv_frames, false);
258 snd_pcm_sframes_t snd_pcm_plug_slave_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t clt_frames)
260 if (snd_BUG_ON(!plug))
262 switch (snd_pcm_plug_stream(plug)) {
263 case SNDRV_PCM_STREAM_PLAYBACK:
264 return calc_dst_frames(plug, clt_frames, false);
265 case SNDRV_PCM_STREAM_CAPTURE:
266 return calc_src_frames(plug, clt_frames, false);
273 static int snd_pcm_plug_formats(const struct snd_mask *mask,
274 snd_pcm_format_t format)
276 struct snd_mask formats = *mask;
277 u64 linfmts = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
278 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
279 SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
280 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_S24_LE |
281 SNDRV_PCM_FMTBIT_U24_BE | SNDRV_PCM_FMTBIT_S24_BE |
282 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_S24_3LE |
283 SNDRV_PCM_FMTBIT_U24_3BE | SNDRV_PCM_FMTBIT_S24_3BE |
284 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
285 SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE);
286 snd_mask_set(&formats, (__force int)SNDRV_PCM_FORMAT_MU_LAW);
288 if (formats.bits[0] & lower_32_bits(linfmts))
289 formats.bits[0] |= lower_32_bits(linfmts);
290 if (formats.bits[1] & upper_32_bits(linfmts))
291 formats.bits[1] |= upper_32_bits(linfmts);
292 return snd_mask_test(&formats, (__force int)format);
295 static const snd_pcm_format_t preferred_formats[] = {
296 SNDRV_PCM_FORMAT_S16_LE,
297 SNDRV_PCM_FORMAT_S16_BE,
298 SNDRV_PCM_FORMAT_U16_LE,
299 SNDRV_PCM_FORMAT_U16_BE,
300 SNDRV_PCM_FORMAT_S24_3LE,
301 SNDRV_PCM_FORMAT_S24_3BE,
302 SNDRV_PCM_FORMAT_U24_3LE,
303 SNDRV_PCM_FORMAT_U24_3BE,
304 SNDRV_PCM_FORMAT_S24_LE,
305 SNDRV_PCM_FORMAT_S24_BE,
306 SNDRV_PCM_FORMAT_U24_LE,
307 SNDRV_PCM_FORMAT_U24_BE,
308 SNDRV_PCM_FORMAT_S32_LE,
309 SNDRV_PCM_FORMAT_S32_BE,
310 SNDRV_PCM_FORMAT_U32_LE,
311 SNDRV_PCM_FORMAT_U32_BE,
316 snd_pcm_format_t snd_pcm_plug_slave_format(snd_pcm_format_t format,
317 const struct snd_mask *format_mask)
321 if (snd_mask_test(format_mask, (__force int)format))
323 if (!snd_pcm_plug_formats(format_mask, format))
324 return (__force snd_pcm_format_t)-EINVAL;
325 if (snd_pcm_format_linear(format)) {
326 unsigned int width = snd_pcm_format_width(format);
327 int unsignd = snd_pcm_format_unsigned(format) > 0;
328 int big = snd_pcm_format_big_endian(format) > 0;
329 unsigned int badness, best = -1;
330 snd_pcm_format_t best_format = (__force snd_pcm_format_t)-1;
331 for (i = 0; i < ARRAY_SIZE(preferred_formats); i++) {
332 snd_pcm_format_t f = preferred_formats[i];
334 if (!snd_mask_test(format_mask, (__force int)f))
336 w = snd_pcm_format_width(f);
340 badness = width - w + 32;
341 badness += snd_pcm_format_unsigned(f) != unsignd;
342 badness += snd_pcm_format_big_endian(f) != big;
343 if (badness < best) {
348 if ((__force int)best_format >= 0)
351 return (__force snd_pcm_format_t)-EINVAL;
354 case SNDRV_PCM_FORMAT_MU_LAW:
355 for (i = 0; i < ARRAY_SIZE(preferred_formats); ++i) {
356 snd_pcm_format_t format1 = preferred_formats[i];
357 if (snd_mask_test(format_mask, (__force int)format1))
362 return (__force snd_pcm_format_t)-EINVAL;
367 int snd_pcm_plug_format_plugins(struct snd_pcm_substream *plug,
368 struct snd_pcm_hw_params *params,
369 struct snd_pcm_hw_params *slave_params)
371 struct snd_pcm_plugin_format tmpformat;
372 struct snd_pcm_plugin_format dstformat;
373 struct snd_pcm_plugin_format srcformat;
374 snd_pcm_access_t src_access, dst_access;
375 struct snd_pcm_plugin *plugin = NULL;
377 int stream = snd_pcm_plug_stream(plug);
378 int slave_interleaved = (params_channels(slave_params) == 1 ||
379 params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED);
382 case SNDRV_PCM_STREAM_PLAYBACK:
383 dstformat.format = params_format(slave_params);
384 dstformat.rate = params_rate(slave_params);
385 dstformat.channels = params_channels(slave_params);
386 srcformat.format = params_format(params);
387 srcformat.rate = params_rate(params);
388 srcformat.channels = params_channels(params);
389 src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
390 dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
391 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
393 case SNDRV_PCM_STREAM_CAPTURE:
394 dstformat.format = params_format(params);
395 dstformat.rate = params_rate(params);
396 dstformat.channels = params_channels(params);
397 srcformat.format = params_format(slave_params);
398 srcformat.rate = params_rate(slave_params);
399 srcformat.channels = params_channels(slave_params);
400 src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
401 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
402 dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
408 tmpformat = srcformat;
410 pdprintf("srcformat: format=%i, rate=%i, channels=%i\n",
414 pdprintf("dstformat: format=%i, rate=%i, channels=%i\n",
419 /* Format change (linearization) */
420 if (! rate_match(srcformat.rate, dstformat.rate) &&
421 ! snd_pcm_format_linear(srcformat.format)) {
422 if (srcformat.format != SNDRV_PCM_FORMAT_MU_LAW)
424 tmpformat.format = SNDRV_PCM_FORMAT_S16;
425 err = snd_pcm_plugin_build_mulaw(plug,
426 &srcformat, &tmpformat,
430 err = snd_pcm_plugin_append(plugin);
432 snd_pcm_plugin_free(plugin);
435 srcformat = tmpformat;
436 src_access = dst_access;
439 /* channels reduction */
440 if (srcformat.channels > dstformat.channels) {
441 tmpformat.channels = dstformat.channels;
442 err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
443 pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
446 err = snd_pcm_plugin_append(plugin);
448 snd_pcm_plugin_free(plugin);
451 srcformat = tmpformat;
452 src_access = dst_access;
455 /* rate resampling */
456 if (!rate_match(srcformat.rate, dstformat.rate)) {
457 if (srcformat.format != SNDRV_PCM_FORMAT_S16) {
458 /* convert to S16 for resampling */
459 tmpformat.format = SNDRV_PCM_FORMAT_S16;
460 err = snd_pcm_plugin_build_linear(plug,
461 &srcformat, &tmpformat,
465 err = snd_pcm_plugin_append(plugin);
467 snd_pcm_plugin_free(plugin);
470 srcformat = tmpformat;
471 src_access = dst_access;
473 tmpformat.rate = dstformat.rate;
474 err = snd_pcm_plugin_build_rate(plug,
475 &srcformat, &tmpformat,
477 pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
480 err = snd_pcm_plugin_append(plugin);
482 snd_pcm_plugin_free(plugin);
485 srcformat = tmpformat;
486 src_access = dst_access;
490 if (srcformat.format != dstformat.format) {
491 tmpformat.format = dstformat.format;
492 if (srcformat.format == SNDRV_PCM_FORMAT_MU_LAW ||
493 tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) {
494 err = snd_pcm_plugin_build_mulaw(plug,
495 &srcformat, &tmpformat,
498 else if (snd_pcm_format_linear(srcformat.format) &&
499 snd_pcm_format_linear(tmpformat.format)) {
500 err = snd_pcm_plugin_build_linear(plug,
501 &srcformat, &tmpformat,
506 pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
509 err = snd_pcm_plugin_append(plugin);
511 snd_pcm_plugin_free(plugin);
514 srcformat = tmpformat;
515 src_access = dst_access;
518 /* channels extension */
519 if (srcformat.channels < dstformat.channels) {
520 tmpformat.channels = dstformat.channels;
521 err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
522 pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
525 err = snd_pcm_plugin_append(plugin);
527 snd_pcm_plugin_free(plugin);
530 srcformat = tmpformat;
531 src_access = dst_access;
535 if (src_access != dst_access) {
536 err = snd_pcm_plugin_build_copy(plug,
540 pdprintf("interleave change (copy: returns %i)\n", err);
543 err = snd_pcm_plugin_append(plugin);
545 snd_pcm_plugin_free(plugin);
553 snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(struct snd_pcm_substream *plug,
555 snd_pcm_uframes_t count,
556 struct snd_pcm_plugin_channel **channels)
558 struct snd_pcm_plugin *plugin;
559 struct snd_pcm_plugin_channel *v;
560 struct snd_pcm_plugin_format *format;
561 int width, nchannels, channel;
562 int stream = snd_pcm_plug_stream(plug);
564 if (snd_BUG_ON(!buf))
566 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
567 plugin = snd_pcm_plug_first(plug);
568 format = &plugin->src_format;
570 plugin = snd_pcm_plug_last(plug);
571 format = &plugin->dst_format;
573 v = plugin->buf_channels;
575 if ((width = snd_pcm_format_physical_width(format->format)) < 0)
577 nchannels = format->channels;
578 if (snd_BUG_ON(plugin->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
579 format->channels > 1))
581 for (channel = 0; channel < nchannels; channel++, v++) {
584 v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE);
586 v->area.first = channel * width;
587 v->area.step = nchannels * width;
592 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)
594 struct snd_pcm_plugin *plugin, *next;
595 struct snd_pcm_plugin_channel *dst_channels;
597 snd_pcm_sframes_t frames = size;
599 plugin = snd_pcm_plug_first(plug);
603 if ((next = plugin->next) != NULL) {
604 snd_pcm_sframes_t frames1 = frames;
605 if (plugin->dst_frames) {
606 frames1 = plugin->dst_frames(plugin, frames);
610 if ((err = next->client_channels(next, frames1, &dst_channels)) < 0) {
613 if (err != frames1) {
615 if (plugin->src_frames) {
616 frames = plugin->src_frames(plugin, frames1);
623 pdprintf("write plugin: %s, %li\n", plugin->name, frames);
624 if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
626 src_channels = dst_channels;
629 return calc_src_frames(plug, frames, true);
632 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)
634 struct snd_pcm_plugin *plugin, *next;
635 struct snd_pcm_plugin_channel *src_channels, *dst_channels;
636 snd_pcm_sframes_t frames = size;
639 frames = calc_src_frames(plug, frames, true);
644 plugin = snd_pcm_plug_first(plug);
645 while (plugin && frames > 0) {
646 if ((next = plugin->next) != NULL) {
647 if ((err = plugin->client_channels(plugin, frames, &dst_channels)) < 0) {
652 dst_channels = dst_channels_final;
654 pdprintf("read plugin: %s, %li\n", plugin->name, frames);
655 if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
658 src_channels = dst_channels;
663 int snd_pcm_area_silence(const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
664 size_t samples, snd_pcm_format_t format)
666 /* FIXME: sub byte resolution and odd dst_offset */
668 unsigned int dst_step;
670 const unsigned char *silence;
673 dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
674 width = snd_pcm_format_physical_width(format);
677 if (dst_area->step == (unsigned int) width && width >= 8)
678 return snd_pcm_format_set_silence(format, dst, samples);
679 silence = snd_pcm_format_silence_64(format);
682 dst_step = dst_area->step / 8;
685 int dstbit = dst_area->first % 8;
686 int dstbit_step = dst_area->step % 8;
687 while (samples-- > 0) {
693 dstbit += dstbit_step;
701 while (samples-- > 0) {
702 memcpy(dst, silence, width);
709 int snd_pcm_area_copy(const struct snd_pcm_channel_area *src_area, size_t src_offset,
710 const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
711 size_t samples, snd_pcm_format_t format)
713 /* FIXME: sub byte resolution and odd dst_offset */
716 int src_step, dst_step;
717 src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
719 return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
720 dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
723 width = snd_pcm_format_physical_width(format);
726 if (src_area->step == (unsigned int) width &&
727 dst_area->step == (unsigned int) width && width >= 8) {
728 size_t bytes = samples * width / 8;
729 memcpy(dst, src, bytes);
732 src_step = src_area->step / 8;
733 dst_step = dst_area->step / 8;
736 int srcbit = src_area->first % 8;
737 int srcbit_step = src_area->step % 8;
738 int dstbit = dst_area->first % 8;
739 int dstbit_step = dst_area->step % 8;
740 while (samples-- > 0) {
741 unsigned char srcval;
743 srcval = *src & 0x0f;
745 srcval = (*src & 0xf0) >> 4;
747 *dst = (*dst & 0xf0) | srcval;
749 *dst = (*dst & 0x0f) | (srcval << 4);
751 srcbit += srcbit_step;
757 dstbit += dstbit_step;
765 while (samples-- > 0) {
766 memcpy(dst, src, width);