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 snd_assert((size % 8) == 0, return -ENXIO);
67 if (plugin->buf_frames < frames) {
69 plugin->buf = vmalloc(size);
70 plugin->buf_frames = frames;
73 plugin->buf_frames = 0;
76 c = plugin->buf_channels;
77 if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
78 for (channel = 0; channel < format->channels; channel++, c++) {
82 c->area.addr = plugin->buf;
83 c->area.first = channel * width;
84 c->area.step = format->channels * width;
86 } else if (plugin->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
87 snd_assert((size % format->channels) == 0,);
88 size /= format->channels;
89 for (channel = 0; channel < format->channels; channel++, c++) {
93 c->area.addr = plugin->buf + (channel * size);
102 int snd_pcm_plug_alloc(struct snd_pcm_substream *plug, snd_pcm_uframes_t frames)
105 snd_assert(snd_pcm_plug_first(plug) != NULL, return -ENXIO);
106 if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) {
107 struct snd_pcm_plugin *plugin = snd_pcm_plug_first(plug);
108 while (plugin->next) {
109 if (plugin->dst_frames)
110 frames = plugin->dst_frames(plugin, frames);
111 snd_assert(frames > 0, return -ENXIO);
112 plugin = plugin->next;
113 err = snd_pcm_plugin_alloc(plugin, frames);
118 struct snd_pcm_plugin *plugin = snd_pcm_plug_last(plug);
119 while (plugin->prev) {
120 if (plugin->src_frames)
121 frames = plugin->src_frames(plugin, frames);
122 snd_assert(frames > 0, return -ENXIO);
123 plugin = plugin->prev;
124 err = snd_pcm_plugin_alloc(plugin, frames);
133 snd_pcm_sframes_t snd_pcm_plugin_client_channels(struct snd_pcm_plugin *plugin,
134 snd_pcm_uframes_t frames,
135 struct snd_pcm_plugin_channel **channels)
137 *channels = plugin->buf_channels;
141 int snd_pcm_plugin_build(struct snd_pcm_substream *plug,
143 struct snd_pcm_plugin_format *src_format,
144 struct snd_pcm_plugin_format *dst_format,
146 struct snd_pcm_plugin **ret)
148 struct snd_pcm_plugin *plugin;
149 unsigned int channels;
151 snd_assert(plug != NULL, return -ENXIO);
152 snd_assert(src_format != NULL && dst_format != NULL, return -ENXIO);
153 plugin = kzalloc(sizeof(*plugin) + extra, GFP_KERNEL);
158 plugin->stream = snd_pcm_plug_stream(plug);
159 plugin->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
160 plugin->src_format = *src_format;
161 plugin->src_width = snd_pcm_format_physical_width(src_format->format);
162 snd_assert(plugin->src_width > 0, );
163 plugin->dst_format = *dst_format;
164 plugin->dst_width = snd_pcm_format_physical_width(dst_format->format);
165 snd_assert(plugin->dst_width > 0, );
166 if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK)
167 channels = src_format->channels;
169 channels = dst_format->channels;
170 plugin->buf_channels = kcalloc(channels, sizeof(*plugin->buf_channels), GFP_KERNEL);
171 if (plugin->buf_channels == NULL) {
172 snd_pcm_plugin_free(plugin);
175 plugin->client_channels = snd_pcm_plugin_client_channels;
180 int snd_pcm_plugin_free(struct snd_pcm_plugin *plugin)
184 if (plugin->private_free)
185 plugin->private_free(plugin);
186 kfree(plugin->buf_channels);
192 snd_pcm_sframes_t snd_pcm_plug_client_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t drv_frames)
194 struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
195 int stream = snd_pcm_plug_stream(plug);
197 snd_assert(plug != NULL, return -ENXIO);
200 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
201 plugin = snd_pcm_plug_last(plug);
202 while (plugin && drv_frames > 0) {
203 plugin_prev = plugin->prev;
204 if (plugin->src_frames)
205 drv_frames = plugin->src_frames(plugin, drv_frames);
206 plugin = plugin_prev;
208 } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
209 plugin = snd_pcm_plug_first(plug);
210 while (plugin && drv_frames > 0) {
211 plugin_next = plugin->next;
212 if (plugin->dst_frames)
213 drv_frames = plugin->dst_frames(plugin, drv_frames);
214 plugin = plugin_next;
221 snd_pcm_sframes_t snd_pcm_plug_slave_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t clt_frames)
223 struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
224 snd_pcm_sframes_t frames;
225 int stream = snd_pcm_plug_stream(plug);
227 snd_assert(plug != NULL, return -ENXIO);
231 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
232 plugin = snd_pcm_plug_first(plug);
233 while (plugin && frames > 0) {
234 plugin_next = plugin->next;
235 if (plugin->dst_frames) {
236 frames = plugin->dst_frames(plugin, frames);
240 plugin = plugin_next;
242 } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
243 plugin = snd_pcm_plug_last(plug);
245 plugin_prev = plugin->prev;
246 if (plugin->src_frames) {
247 frames = plugin->src_frames(plugin, frames);
251 plugin = plugin_prev;
258 static int snd_pcm_plug_formats(struct snd_mask *mask, int format)
260 struct snd_mask formats = *mask;
261 u64 linfmts = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
262 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
263 SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
264 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_S24_LE |
265 SNDRV_PCM_FMTBIT_U24_BE | SNDRV_PCM_FMTBIT_S24_BE |
266 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_S24_3LE |
267 SNDRV_PCM_FMTBIT_U24_3BE | SNDRV_PCM_FMTBIT_S24_3BE |
268 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
269 SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE);
270 snd_mask_set(&formats, SNDRV_PCM_FORMAT_MU_LAW);
272 if (formats.bits[0] & (u32)linfmts)
273 formats.bits[0] |= (u32)linfmts;
274 if (formats.bits[1] & (u32)(linfmts >> 32))
275 formats.bits[1] |= (u32)(linfmts >> 32);
276 return snd_mask_test(&formats, format);
279 static int preferred_formats[] = {
280 SNDRV_PCM_FORMAT_S16_LE,
281 SNDRV_PCM_FORMAT_S16_BE,
282 SNDRV_PCM_FORMAT_U16_LE,
283 SNDRV_PCM_FORMAT_U16_BE,
284 SNDRV_PCM_FORMAT_S24_3LE,
285 SNDRV_PCM_FORMAT_S24_3BE,
286 SNDRV_PCM_FORMAT_U24_3LE,
287 SNDRV_PCM_FORMAT_U24_3BE,
288 SNDRV_PCM_FORMAT_S24_LE,
289 SNDRV_PCM_FORMAT_S24_BE,
290 SNDRV_PCM_FORMAT_U24_LE,
291 SNDRV_PCM_FORMAT_U24_BE,
292 SNDRV_PCM_FORMAT_S32_LE,
293 SNDRV_PCM_FORMAT_S32_BE,
294 SNDRV_PCM_FORMAT_U32_LE,
295 SNDRV_PCM_FORMAT_U32_BE,
300 int snd_pcm_plug_slave_format(int format, struct snd_mask *format_mask)
304 if (snd_mask_test(format_mask, format))
306 if (! snd_pcm_plug_formats(format_mask, format))
308 if (snd_pcm_format_linear(format)) {
309 unsigned int width = snd_pcm_format_width(format);
310 int unsignd = snd_pcm_format_unsigned(format) > 0;
311 int big = snd_pcm_format_big_endian(format) > 0;
312 unsigned int badness, best = -1;
313 int best_format = -1;
314 for (i = 0; i < ARRAY_SIZE(preferred_formats); i++) {
315 int f = preferred_formats[i];
317 if (!snd_mask_test(format_mask, f))
319 w = snd_pcm_format_width(f);
323 badness = width - w + 32;
324 badness += snd_pcm_format_unsigned(f) != unsignd;
325 badness += snd_pcm_format_big_endian(f) != big;
326 if (badness < best) {
331 return best_format >= 0 ? best_format : -EINVAL;
334 case SNDRV_PCM_FORMAT_MU_LAW:
335 for (i = 0; i < ARRAY_SIZE(preferred_formats); ++i) {
336 int format1 = preferred_formats[i];
337 if (snd_mask_test(format_mask, format1))
346 int snd_pcm_plug_format_plugins(struct snd_pcm_substream *plug,
347 struct snd_pcm_hw_params *params,
348 struct snd_pcm_hw_params *slave_params)
350 struct snd_pcm_plugin_format tmpformat;
351 struct snd_pcm_plugin_format dstformat;
352 struct snd_pcm_plugin_format srcformat;
353 int src_access, dst_access;
354 struct snd_pcm_plugin *plugin = NULL;
356 int stream = snd_pcm_plug_stream(plug);
357 int slave_interleaved = (params_channels(slave_params) == 1 ||
358 params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED);
361 case SNDRV_PCM_STREAM_PLAYBACK:
362 dstformat.format = params_format(slave_params);
363 dstformat.rate = params_rate(slave_params);
364 dstformat.channels = params_channels(slave_params);
365 srcformat.format = params_format(params);
366 srcformat.rate = params_rate(params);
367 srcformat.channels = params_channels(params);
368 src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
369 dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
370 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
372 case SNDRV_PCM_STREAM_CAPTURE:
373 dstformat.format = params_format(params);
374 dstformat.rate = params_rate(params);
375 dstformat.channels = params_channels(params);
376 srcformat.format = params_format(slave_params);
377 srcformat.rate = params_rate(slave_params);
378 srcformat.channels = params_channels(slave_params);
379 src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
380 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
381 dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
387 tmpformat = srcformat;
389 pdprintf("srcformat: format=%i, rate=%i, channels=%i\n",
393 pdprintf("dstformat: format=%i, rate=%i, channels=%i\n",
398 /* Format change (linearization) */
399 if (! rate_match(srcformat.rate, dstformat.rate) &&
400 ! snd_pcm_format_linear(srcformat.format)) {
401 if (srcformat.format != SNDRV_PCM_FORMAT_MU_LAW)
403 tmpformat.format = SNDRV_PCM_FORMAT_S16;
404 err = snd_pcm_plugin_build_mulaw(plug,
405 &srcformat, &tmpformat,
409 err = snd_pcm_plugin_append(plugin);
411 snd_pcm_plugin_free(plugin);
414 srcformat = tmpformat;
415 src_access = dst_access;
418 /* channels reduction */
419 if (srcformat.channels > dstformat.channels) {
420 tmpformat.channels = dstformat.channels;
421 err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
422 pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
425 err = snd_pcm_plugin_append(plugin);
427 snd_pcm_plugin_free(plugin);
430 srcformat = tmpformat;
431 src_access = dst_access;
434 /* rate resampling */
435 if (!rate_match(srcformat.rate, dstformat.rate)) {
436 if (srcformat.format != SNDRV_PCM_FORMAT_S16) {
437 /* convert to S16 for resampling */
438 tmpformat.format = SNDRV_PCM_FORMAT_S16;
439 err = snd_pcm_plugin_build_linear(plug,
440 &srcformat, &tmpformat,
444 err = snd_pcm_plugin_append(plugin);
446 snd_pcm_plugin_free(plugin);
449 srcformat = tmpformat;
450 src_access = dst_access;
452 tmpformat.rate = dstformat.rate;
453 err = snd_pcm_plugin_build_rate(plug,
454 &srcformat, &tmpformat,
456 pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
459 err = snd_pcm_plugin_append(plugin);
461 snd_pcm_plugin_free(plugin);
464 srcformat = tmpformat;
465 src_access = dst_access;
469 if (srcformat.format != dstformat.format) {
470 tmpformat.format = dstformat.format;
471 if (srcformat.format == SNDRV_PCM_FORMAT_MU_LAW ||
472 tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) {
473 err = snd_pcm_plugin_build_mulaw(plug,
474 &srcformat, &tmpformat,
477 else if (snd_pcm_format_linear(srcformat.format) &&
478 snd_pcm_format_linear(tmpformat.format)) {
479 err = snd_pcm_plugin_build_linear(plug,
480 &srcformat, &tmpformat,
485 pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
488 err = snd_pcm_plugin_append(plugin);
490 snd_pcm_plugin_free(plugin);
493 srcformat = tmpformat;
494 src_access = dst_access;
497 /* channels extension */
498 if (srcformat.channels < dstformat.channels) {
499 tmpformat.channels = dstformat.channels;
500 err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
501 pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
504 err = snd_pcm_plugin_append(plugin);
506 snd_pcm_plugin_free(plugin);
509 srcformat = tmpformat;
510 src_access = dst_access;
514 if (src_access != dst_access) {
515 err = snd_pcm_plugin_build_copy(plug,
519 pdprintf("interleave change (copy: returns %i)\n", err);
522 err = snd_pcm_plugin_append(plugin);
524 snd_pcm_plugin_free(plugin);
532 snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(struct snd_pcm_substream *plug,
534 snd_pcm_uframes_t count,
535 struct snd_pcm_plugin_channel **channels)
537 struct snd_pcm_plugin *plugin;
538 struct snd_pcm_plugin_channel *v;
539 struct snd_pcm_plugin_format *format;
540 int width, nchannels, channel;
541 int stream = snd_pcm_plug_stream(plug);
543 snd_assert(buf != NULL, return -ENXIO);
544 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
545 plugin = snd_pcm_plug_first(plug);
546 format = &plugin->src_format;
548 plugin = snd_pcm_plug_last(plug);
549 format = &plugin->dst_format;
551 v = plugin->buf_channels;
553 if ((width = snd_pcm_format_physical_width(format->format)) < 0)
555 nchannels = format->channels;
556 snd_assert(plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED || format->channels <= 1, return -ENXIO);
557 for (channel = 0; channel < nchannels; channel++, v++) {
560 v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE);
562 v->area.first = channel * width;
563 v->area.step = nchannels * width;
568 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)
570 struct snd_pcm_plugin *plugin, *next;
571 struct snd_pcm_plugin_channel *dst_channels;
573 snd_pcm_sframes_t frames = size;
575 plugin = snd_pcm_plug_first(plug);
576 while (plugin && frames > 0) {
577 if ((next = plugin->next) != NULL) {
578 snd_pcm_sframes_t frames1 = frames;
579 if (plugin->dst_frames)
580 frames1 = plugin->dst_frames(plugin, frames);
581 if ((err = next->client_channels(next, frames1, &dst_channels)) < 0) {
584 if (err != frames1) {
586 if (plugin->src_frames)
587 frames = plugin->src_frames(plugin, frames1);
591 pdprintf("write plugin: %s, %li\n", plugin->name, frames);
592 if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
594 src_channels = dst_channels;
597 return snd_pcm_plug_client_size(plug, frames);
600 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)
602 struct snd_pcm_plugin *plugin, *next;
603 struct snd_pcm_plugin_channel *src_channels, *dst_channels;
604 snd_pcm_sframes_t frames = size;
607 frames = snd_pcm_plug_slave_size(plug, frames);
612 plugin = snd_pcm_plug_first(plug);
613 while (plugin && frames > 0) {
614 if ((next = plugin->next) != NULL) {
615 if ((err = plugin->client_channels(plugin, frames, &dst_channels)) < 0) {
620 dst_channels = dst_channels_final;
622 pdprintf("read plugin: %s, %li\n", plugin->name, frames);
623 if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
626 src_channels = dst_channels;
631 int snd_pcm_area_silence(const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
632 size_t samples, int format)
634 /* FIXME: sub byte resolution and odd dst_offset */
636 unsigned int dst_step;
638 const unsigned char *silence;
641 dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
642 width = snd_pcm_format_physical_width(format);
645 if (dst_area->step == (unsigned int) width && width >= 8)
646 return snd_pcm_format_set_silence(format, dst, samples);
647 silence = snd_pcm_format_silence_64(format);
650 dst_step = dst_area->step / 8;
653 int dstbit = dst_area->first % 8;
654 int dstbit_step = dst_area->step % 8;
655 while (samples-- > 0) {
661 dstbit += dstbit_step;
669 while (samples-- > 0) {
670 memcpy(dst, silence, width);
677 int snd_pcm_area_copy(const struct snd_pcm_channel_area *src_area, size_t src_offset,
678 const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
679 size_t samples, int format)
681 /* FIXME: sub byte resolution and odd dst_offset */
684 int src_step, dst_step;
685 src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
687 return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
688 dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
691 width = snd_pcm_format_physical_width(format);
694 if (src_area->step == (unsigned int) width &&
695 dst_area->step == (unsigned int) width && width >= 8) {
696 size_t bytes = samples * width / 8;
697 memcpy(dst, src, bytes);
700 src_step = src_area->step / 8;
701 dst_step = dst_area->step / 8;
704 int srcbit = src_area->first % 8;
705 int srcbit_step = src_area->step % 8;
706 int dstbit = dst_area->first % 8;
707 int dstbit_step = dst_area->step % 8;
708 while (samples-- > 0) {
709 unsigned char srcval;
711 srcval = *src & 0x0f;
713 srcval = (*src & 0xf0) >> 4;
715 *dst = (*dst & 0xf0) | srcval;
717 *dst = (*dst & 0x0f) | (srcval << 4);
719 srcbit += srcbit_step;
725 dstbit += dstbit_step;
733 while (samples-- > 0) {
734 memcpy(dst, src, width);