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 <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)
40 * because some cards might have rates "very close", we ignore
41 * all "resampling" requests within +-5%
43 static int rate_match(unsigned int src_rate, unsigned int dst_rate)
45 unsigned int low = (src_rate * 95) / 100;
46 unsigned int high = (src_rate * 105) / 100;
47 return dst_rate >= low && dst_rate <= high;
50 static int snd_pcm_plugin_alloc(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames)
52 struct snd_pcm_plugin_format *format;
56 struct snd_pcm_plugin_channel *c;
58 if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK) {
59 format = &plugin->src_format;
61 format = &plugin->dst_format;
63 if ((width = snd_pcm_format_physical_width(format->format)) < 0)
65 size = frames * format->channels * width;
66 snd_assert((size % 8) == 0, return -ENXIO);
68 if (plugin->buf_frames < frames) {
70 plugin->buf = vmalloc(size);
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 snd_assert((size % format->channels) == 0,);
89 size /= format->channels;
90 for (channel = 0; channel < format->channels; channel++, c++) {
94 c->area.addr = plugin->buf + (channel * size);
103 int snd_pcm_plug_alloc(struct snd_pcm_substream *plug, snd_pcm_uframes_t frames)
106 snd_assert(snd_pcm_plug_first(plug) != NULL, return -ENXIO);
107 if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) {
108 struct snd_pcm_plugin *plugin = snd_pcm_plug_first(plug);
109 while (plugin->next) {
110 if (plugin->dst_frames)
111 frames = plugin->dst_frames(plugin, frames);
112 snd_assert(frames > 0, return -ENXIO);
113 plugin = plugin->next;
114 err = snd_pcm_plugin_alloc(plugin, frames);
119 struct snd_pcm_plugin *plugin = snd_pcm_plug_last(plug);
120 while (plugin->prev) {
121 if (plugin->src_frames)
122 frames = plugin->src_frames(plugin, frames);
123 snd_assert(frames > 0, return -ENXIO);
124 plugin = plugin->prev;
125 err = snd_pcm_plugin_alloc(plugin, frames);
134 snd_pcm_sframes_t snd_pcm_plugin_client_channels(struct snd_pcm_plugin *plugin,
135 snd_pcm_uframes_t frames,
136 struct snd_pcm_plugin_channel **channels)
138 *channels = plugin->buf_channels;
142 int snd_pcm_plugin_build(struct snd_pcm_substream *plug,
144 struct snd_pcm_plugin_format *src_format,
145 struct snd_pcm_plugin_format *dst_format,
147 struct snd_pcm_plugin **ret)
149 struct snd_pcm_plugin *plugin;
150 unsigned int channels;
152 snd_assert(plug != NULL, return -ENXIO);
153 snd_assert(src_format != NULL && dst_format != NULL, return -ENXIO);
154 plugin = kzalloc(sizeof(*plugin) + extra, GFP_KERNEL);
159 plugin->stream = snd_pcm_plug_stream(plug);
160 plugin->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
161 plugin->src_format = *src_format;
162 plugin->src_width = snd_pcm_format_physical_width(src_format->format);
163 snd_assert(plugin->src_width > 0, );
164 plugin->dst_format = *dst_format;
165 plugin->dst_width = snd_pcm_format_physical_width(dst_format->format);
166 snd_assert(plugin->dst_width > 0, );
167 if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK)
168 channels = src_format->channels;
170 channels = dst_format->channels;
171 plugin->buf_channels = kcalloc(channels, sizeof(*plugin->buf_channels), GFP_KERNEL);
172 if (plugin->buf_channels == NULL) {
173 snd_pcm_plugin_free(plugin);
176 plugin->client_channels = snd_pcm_plugin_client_channels;
181 int snd_pcm_plugin_free(struct snd_pcm_plugin *plugin)
185 if (plugin->private_free)
186 plugin->private_free(plugin);
187 kfree(plugin->buf_channels);
193 snd_pcm_sframes_t snd_pcm_plug_client_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t drv_frames)
195 struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
196 int stream = snd_pcm_plug_stream(plug);
198 snd_assert(plug != NULL, return -ENXIO);
201 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
202 plugin = snd_pcm_plug_last(plug);
203 while (plugin && drv_frames > 0) {
204 plugin_prev = plugin->prev;
205 if (plugin->src_frames)
206 drv_frames = plugin->src_frames(plugin, drv_frames);
207 plugin = plugin_prev;
209 } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
210 plugin = snd_pcm_plug_first(plug);
211 while (plugin && drv_frames > 0) {
212 plugin_next = plugin->next;
213 if (plugin->dst_frames)
214 drv_frames = plugin->dst_frames(plugin, drv_frames);
215 plugin = plugin_next;
222 snd_pcm_sframes_t snd_pcm_plug_slave_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t clt_frames)
224 struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
225 snd_pcm_sframes_t frames;
226 int stream = snd_pcm_plug_stream(plug);
228 snd_assert(plug != NULL, return -ENXIO);
232 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
233 plugin = snd_pcm_plug_first(plug);
234 while (plugin && frames > 0) {
235 plugin_next = plugin->next;
236 if (plugin->dst_frames) {
237 frames = plugin->dst_frames(plugin, frames);
241 plugin = plugin_next;
243 } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
244 plugin = snd_pcm_plug_last(plug);
246 plugin_prev = plugin->prev;
247 if (plugin->src_frames) {
248 frames = plugin->src_frames(plugin, frames);
252 plugin = plugin_prev;
259 static int snd_pcm_plug_formats(struct snd_mask *mask, int format)
261 struct snd_mask formats = *mask;
262 u64 linfmts = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
263 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
264 SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
265 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_S24_LE |
266 SNDRV_PCM_FMTBIT_U24_BE | SNDRV_PCM_FMTBIT_S24_BE |
267 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_S24_3LE |
268 SNDRV_PCM_FMTBIT_U24_3BE | SNDRV_PCM_FMTBIT_S24_3BE |
269 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
270 SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE);
271 snd_mask_set(&formats, SNDRV_PCM_FORMAT_MU_LAW);
273 if (formats.bits[0] & (u32)linfmts)
274 formats.bits[0] |= (u32)linfmts;
275 if (formats.bits[1] & (u32)(linfmts >> 32))
276 formats.bits[1] |= (u32)(linfmts >> 32);
277 return snd_mask_test(&formats, format);
280 static int preferred_formats[] = {
281 SNDRV_PCM_FORMAT_S16_LE,
282 SNDRV_PCM_FORMAT_S16_BE,
283 SNDRV_PCM_FORMAT_U16_LE,
284 SNDRV_PCM_FORMAT_U16_BE,
285 SNDRV_PCM_FORMAT_S24_3LE,
286 SNDRV_PCM_FORMAT_S24_3BE,
287 SNDRV_PCM_FORMAT_U24_3LE,
288 SNDRV_PCM_FORMAT_U24_3BE,
289 SNDRV_PCM_FORMAT_S24_LE,
290 SNDRV_PCM_FORMAT_S24_BE,
291 SNDRV_PCM_FORMAT_U24_LE,
292 SNDRV_PCM_FORMAT_U24_BE,
293 SNDRV_PCM_FORMAT_S32_LE,
294 SNDRV_PCM_FORMAT_S32_BE,
295 SNDRV_PCM_FORMAT_U32_LE,
296 SNDRV_PCM_FORMAT_U32_BE,
301 int snd_pcm_plug_slave_format(int format, struct snd_mask *format_mask)
305 if (snd_mask_test(format_mask, format))
307 if (! snd_pcm_plug_formats(format_mask, format))
309 if (snd_pcm_format_linear(format)) {
310 unsigned int width = snd_pcm_format_width(format);
311 int unsignd = snd_pcm_format_unsigned(format) > 0;
312 int big = snd_pcm_format_big_endian(format) > 0;
313 unsigned int badness, best = -1;
314 int best_format = -1;
315 for (i = 0; i < ARRAY_SIZE(preferred_formats); i++) {
316 int f = preferred_formats[i];
318 if (!snd_mask_test(format_mask, f))
320 w = snd_pcm_format_width(f);
324 badness = width - w + 32;
325 badness += snd_pcm_format_unsigned(f) != unsignd;
326 badness += snd_pcm_format_big_endian(f) != big;
327 if (badness < best) {
332 return best_format >= 0 ? best_format : -EINVAL;
335 case SNDRV_PCM_FORMAT_MU_LAW:
336 for (i = 0; i < ARRAY_SIZE(preferred_formats); ++i) {
337 int format1 = preferred_formats[i];
338 if (snd_mask_test(format_mask, format1))
347 int snd_pcm_plug_format_plugins(struct snd_pcm_substream *plug,
348 struct snd_pcm_hw_params *params,
349 struct snd_pcm_hw_params *slave_params)
351 struct snd_pcm_plugin_format tmpformat;
352 struct snd_pcm_plugin_format dstformat;
353 struct snd_pcm_plugin_format srcformat;
354 int src_access, dst_access;
355 struct snd_pcm_plugin *plugin = NULL;
357 int stream = snd_pcm_plug_stream(plug);
358 int slave_interleaved = (params_channels(slave_params) == 1 ||
359 params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED);
362 case SNDRV_PCM_STREAM_PLAYBACK:
363 dstformat.format = params_format(slave_params);
364 dstformat.rate = params_rate(slave_params);
365 dstformat.channels = params_channels(slave_params);
366 srcformat.format = params_format(params);
367 srcformat.rate = params_rate(params);
368 srcformat.channels = params_channels(params);
369 src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
370 dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
371 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
373 case SNDRV_PCM_STREAM_CAPTURE:
374 dstformat.format = params_format(params);
375 dstformat.rate = params_rate(params);
376 dstformat.channels = params_channels(params);
377 srcformat.format = params_format(slave_params);
378 srcformat.rate = params_rate(slave_params);
379 srcformat.channels = params_channels(slave_params);
380 src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
381 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
382 dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
388 tmpformat = srcformat;
390 pdprintf("srcformat: format=%i, rate=%i, channels=%i\n",
394 pdprintf("dstformat: format=%i, rate=%i, channels=%i\n",
399 /* Format change (linearization) */
400 if (! rate_match(srcformat.rate, dstformat.rate) &&
401 ! snd_pcm_format_linear(srcformat.format)) {
402 if (srcformat.format != SNDRV_PCM_FORMAT_MU_LAW)
404 tmpformat.format = SNDRV_PCM_FORMAT_S16;
405 err = snd_pcm_plugin_build_mulaw(plug,
406 &srcformat, &tmpformat,
410 err = snd_pcm_plugin_append(plugin);
412 snd_pcm_plugin_free(plugin);
415 srcformat = tmpformat;
416 src_access = dst_access;
419 /* channels reduction */
420 if (srcformat.channels > dstformat.channels) {
421 tmpformat.channels = dstformat.channels;
422 err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
423 pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
426 err = snd_pcm_plugin_append(plugin);
428 snd_pcm_plugin_free(plugin);
431 srcformat = tmpformat;
432 src_access = dst_access;
435 /* rate resampling */
436 if (!rate_match(srcformat.rate, dstformat.rate)) {
437 if (srcformat.format != SNDRV_PCM_FORMAT_S16) {
438 /* convert to S16 for resampling */
439 tmpformat.format = SNDRV_PCM_FORMAT_S16;
440 err = snd_pcm_plugin_build_linear(plug,
441 &srcformat, &tmpformat,
445 err = snd_pcm_plugin_append(plugin);
447 snd_pcm_plugin_free(plugin);
450 srcformat = tmpformat;
451 src_access = dst_access;
453 tmpformat.rate = dstformat.rate;
454 err = snd_pcm_plugin_build_rate(plug,
455 &srcformat, &tmpformat,
457 pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
460 err = snd_pcm_plugin_append(plugin);
462 snd_pcm_plugin_free(plugin);
465 srcformat = tmpformat;
466 src_access = dst_access;
470 if (srcformat.format != dstformat.format) {
471 tmpformat.format = dstformat.format;
472 if (srcformat.format == SNDRV_PCM_FORMAT_MU_LAW ||
473 tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) {
474 err = snd_pcm_plugin_build_mulaw(plug,
475 &srcformat, &tmpformat,
478 else if (snd_pcm_format_linear(srcformat.format) &&
479 snd_pcm_format_linear(tmpformat.format)) {
480 err = snd_pcm_plugin_build_linear(plug,
481 &srcformat, &tmpformat,
486 pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
489 err = snd_pcm_plugin_append(plugin);
491 snd_pcm_plugin_free(plugin);
494 srcformat = tmpformat;
495 src_access = dst_access;
498 /* channels extension */
499 if (srcformat.channels < dstformat.channels) {
500 tmpformat.channels = dstformat.channels;
501 err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
502 pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
505 err = snd_pcm_plugin_append(plugin);
507 snd_pcm_plugin_free(plugin);
510 srcformat = tmpformat;
511 src_access = dst_access;
515 if (src_access != dst_access) {
516 err = snd_pcm_plugin_build_copy(plug,
520 pdprintf("interleave change (copy: returns %i)\n", err);
523 err = snd_pcm_plugin_append(plugin);
525 snd_pcm_plugin_free(plugin);
533 snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(struct snd_pcm_substream *plug,
535 snd_pcm_uframes_t count,
536 struct snd_pcm_plugin_channel **channels)
538 struct snd_pcm_plugin *plugin;
539 struct snd_pcm_plugin_channel *v;
540 struct snd_pcm_plugin_format *format;
541 int width, nchannels, channel;
542 int stream = snd_pcm_plug_stream(plug);
544 snd_assert(buf != NULL, return -ENXIO);
545 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
546 plugin = snd_pcm_plug_first(plug);
547 format = &plugin->src_format;
549 plugin = snd_pcm_plug_last(plug);
550 format = &plugin->dst_format;
552 v = plugin->buf_channels;
554 if ((width = snd_pcm_format_physical_width(format->format)) < 0)
556 nchannels = format->channels;
557 snd_assert(plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || format->channels <= 1, return -ENXIO);
558 for (channel = 0; channel < nchannels; channel++, v++) {
561 v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE);
563 v->area.first = channel * width;
564 v->area.step = nchannels * width;
569 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)
571 struct snd_pcm_plugin *plugin, *next;
572 struct snd_pcm_plugin_channel *dst_channels;
574 snd_pcm_sframes_t frames = size;
576 plugin = snd_pcm_plug_first(plug);
577 while (plugin && frames > 0) {
578 if ((next = plugin->next) != NULL) {
579 snd_pcm_sframes_t frames1 = frames;
580 if (plugin->dst_frames)
581 frames1 = plugin->dst_frames(plugin, frames);
582 if ((err = next->client_channels(next, frames1, &dst_channels)) < 0) {
585 if (err != frames1) {
587 if (plugin->src_frames)
588 frames = plugin->src_frames(plugin, frames1);
592 pdprintf("write plugin: %s, %li\n", plugin->name, frames);
593 if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
595 src_channels = dst_channels;
598 return snd_pcm_plug_client_size(plug, frames);
601 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)
603 struct snd_pcm_plugin *plugin, *next;
604 struct snd_pcm_plugin_channel *src_channels, *dst_channels;
605 snd_pcm_sframes_t frames = size;
608 frames = snd_pcm_plug_slave_size(plug, frames);
613 plugin = snd_pcm_plug_first(plug);
614 while (plugin && frames > 0) {
615 if ((next = plugin->next) != NULL) {
616 if ((err = plugin->client_channels(plugin, frames, &dst_channels)) < 0) {
621 dst_channels = dst_channels_final;
623 pdprintf("read plugin: %s, %li\n", plugin->name, frames);
624 if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
627 src_channels = dst_channels;
632 int snd_pcm_area_silence(const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
633 size_t samples, int format)
635 /* FIXME: sub byte resolution and odd dst_offset */
637 unsigned int dst_step;
639 const unsigned char *silence;
642 dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
643 width = snd_pcm_format_physical_width(format);
646 if (dst_area->step == (unsigned int) width && width >= 8)
647 return snd_pcm_format_set_silence(format, dst, samples);
648 silence = snd_pcm_format_silence_64(format);
651 dst_step = dst_area->step / 8;
654 int dstbit = dst_area->first % 8;
655 int dstbit_step = dst_area->step % 8;
656 while (samples-- > 0) {
662 dstbit += dstbit_step;
670 while (samples-- > 0) {
671 memcpy(dst, silence, width);
678 int snd_pcm_area_copy(const struct snd_pcm_channel_area *src_area, size_t src_offset,
679 const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
680 size_t samples, int format)
682 /* FIXME: sub byte resolution and odd dst_offset */
685 int src_step, dst_step;
686 src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
688 return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
689 dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
692 width = snd_pcm_format_physical_width(format);
695 if (src_area->step == (unsigned int) width &&
696 dst_area->step == (unsigned int) width && width >= 8) {
697 size_t bytes = samples * width / 8;
698 memcpy(dst, src, bytes);
701 src_step = src_area->step / 8;
702 dst_step = dst_area->step / 8;
705 int srcbit = src_area->first % 8;
706 int srcbit_step = src_area->step % 8;
707 int dstbit = dst_area->first % 8;
708 int dstbit_step = dst_area->step % 8;
709 while (samples-- > 0) {
710 unsigned char srcval;
712 srcval = *src & 0x0f;
714 srcval = (*src & 0xf0) >> 4;
716 *dst = (*dst & 0xf0) | srcval;
718 *dst = (*dst & 0x0f) | (srcval << 4);
720 srcbit += srcbit_step;
726 dstbit += dstbit_step;
734 while (samples-- > 0) {
735 memcpy(dst, src, width);