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 width = snd_pcm_format_physical_width(format->format);
65 size = array3_size(frames, format->channels, width);
66 /* check for too large period size once again */
67 if (size > 1024 * 1024)
69 if (snd_BUG_ON(size % 8))
72 if (plugin->buf_frames < frames) {
74 plugin->buf = kvzalloc(size, GFP_KERNEL);
75 plugin->buf_frames = frames;
78 plugin->buf_frames = 0;
81 c = plugin->buf_channels;
82 if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
83 for (channel = 0; channel < format->channels; channel++, c++) {
87 c->area.addr = plugin->buf;
88 c->area.first = channel * width;
89 c->area.step = format->channels * width;
91 } else if (plugin->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
92 if (snd_BUG_ON(size % format->channels))
94 size /= format->channels;
95 for (channel = 0; channel < format->channels; channel++, c++) {
99 c->area.addr = plugin->buf + (channel * size);
101 c->area.step = width;
108 int snd_pcm_plug_alloc(struct snd_pcm_substream *plug, snd_pcm_uframes_t frames)
111 if (snd_BUG_ON(!snd_pcm_plug_first(plug)))
113 if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) {
114 struct snd_pcm_plugin *plugin = snd_pcm_plug_first(plug);
115 while (plugin->next) {
116 if (plugin->dst_frames)
117 frames = plugin->dst_frames(plugin, frames);
118 if ((snd_pcm_sframes_t)frames <= 0)
120 plugin = plugin->next;
121 err = snd_pcm_plugin_alloc(plugin, frames);
126 struct snd_pcm_plugin *plugin = snd_pcm_plug_last(plug);
127 while (plugin->prev) {
128 if (plugin->src_frames)
129 frames = plugin->src_frames(plugin, frames);
130 if ((snd_pcm_sframes_t)frames <= 0)
132 plugin = plugin->prev;
133 err = snd_pcm_plugin_alloc(plugin, frames);
142 snd_pcm_sframes_t snd_pcm_plugin_client_channels(struct snd_pcm_plugin *plugin,
143 snd_pcm_uframes_t frames,
144 struct snd_pcm_plugin_channel **channels)
146 *channels = plugin->buf_channels;
150 int snd_pcm_plugin_build(struct snd_pcm_substream *plug,
152 struct snd_pcm_plugin_format *src_format,
153 struct snd_pcm_plugin_format *dst_format,
155 struct snd_pcm_plugin **ret)
157 struct snd_pcm_plugin *plugin;
158 unsigned int channels;
160 if (snd_BUG_ON(!plug))
162 if (snd_BUG_ON(!src_format || !dst_format))
164 plugin = kzalloc(sizeof(*plugin) + extra, GFP_KERNEL);
169 plugin->stream = snd_pcm_plug_stream(plug);
170 plugin->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
171 plugin->src_format = *src_format;
172 plugin->src_width = snd_pcm_format_physical_width(src_format->format);
173 snd_BUG_ON(plugin->src_width <= 0);
174 plugin->dst_format = *dst_format;
175 plugin->dst_width = snd_pcm_format_physical_width(dst_format->format);
176 snd_BUG_ON(plugin->dst_width <= 0);
177 if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK)
178 channels = src_format->channels;
180 channels = dst_format->channels;
181 plugin->buf_channels = kcalloc(channels, sizeof(*plugin->buf_channels), GFP_KERNEL);
182 if (plugin->buf_channels == NULL) {
183 snd_pcm_plugin_free(plugin);
186 plugin->client_channels = snd_pcm_plugin_client_channels;
191 int snd_pcm_plugin_free(struct snd_pcm_plugin *plugin)
195 if (plugin->private_free)
196 plugin->private_free(plugin);
197 kfree(plugin->buf_channels);
203 static snd_pcm_sframes_t calc_dst_frames(struct snd_pcm_substream *plug,
204 snd_pcm_sframes_t frames,
207 struct snd_pcm_plugin *plugin, *plugin_next;
209 plugin = snd_pcm_plug_first(plug);
210 while (plugin && frames > 0) {
211 plugin_next = plugin->next;
212 if (check_size && plugin->buf_frames &&
213 frames > plugin->buf_frames)
214 frames = plugin->buf_frames;
215 if (plugin->dst_frames) {
216 frames = plugin->dst_frames(plugin, frames);
220 plugin = plugin_next;
225 static snd_pcm_sframes_t calc_src_frames(struct snd_pcm_substream *plug,
226 snd_pcm_sframes_t frames,
229 struct snd_pcm_plugin *plugin, *plugin_prev;
231 plugin = snd_pcm_plug_last(plug);
232 while (plugin && frames > 0) {
233 plugin_prev = plugin->prev;
234 if (plugin->src_frames) {
235 frames = plugin->src_frames(plugin, frames);
239 if (check_size && plugin->buf_frames &&
240 frames > plugin->buf_frames)
241 frames = plugin->buf_frames;
242 plugin = plugin_prev;
247 snd_pcm_sframes_t snd_pcm_plug_client_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t drv_frames)
249 if (snd_BUG_ON(!plug))
251 switch (snd_pcm_plug_stream(plug)) {
252 case SNDRV_PCM_STREAM_PLAYBACK:
253 return calc_src_frames(plug, drv_frames, false);
254 case SNDRV_PCM_STREAM_CAPTURE:
255 return calc_dst_frames(plug, drv_frames, false);
262 snd_pcm_sframes_t snd_pcm_plug_slave_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t clt_frames)
264 if (snd_BUG_ON(!plug))
266 switch (snd_pcm_plug_stream(plug)) {
267 case SNDRV_PCM_STREAM_PLAYBACK:
268 return calc_dst_frames(plug, clt_frames, false);
269 case SNDRV_PCM_STREAM_CAPTURE:
270 return calc_src_frames(plug, clt_frames, false);
277 static int snd_pcm_plug_formats(const struct snd_mask *mask,
278 snd_pcm_format_t format)
280 struct snd_mask formats = *mask;
281 u64 linfmts = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
282 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
283 SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
284 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_S24_LE |
285 SNDRV_PCM_FMTBIT_U24_BE | SNDRV_PCM_FMTBIT_S24_BE |
286 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_S24_3LE |
287 SNDRV_PCM_FMTBIT_U24_3BE | SNDRV_PCM_FMTBIT_S24_3BE |
288 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
289 SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE);
290 snd_mask_set(&formats, (__force int)SNDRV_PCM_FORMAT_MU_LAW);
292 if (formats.bits[0] & lower_32_bits(linfmts))
293 formats.bits[0] |= lower_32_bits(linfmts);
294 if (formats.bits[1] & upper_32_bits(linfmts))
295 formats.bits[1] |= upper_32_bits(linfmts);
296 return snd_mask_test(&formats, (__force int)format);
299 static const snd_pcm_format_t preferred_formats[] = {
300 SNDRV_PCM_FORMAT_S16_LE,
301 SNDRV_PCM_FORMAT_S16_BE,
302 SNDRV_PCM_FORMAT_U16_LE,
303 SNDRV_PCM_FORMAT_U16_BE,
304 SNDRV_PCM_FORMAT_S24_3LE,
305 SNDRV_PCM_FORMAT_S24_3BE,
306 SNDRV_PCM_FORMAT_U24_3LE,
307 SNDRV_PCM_FORMAT_U24_3BE,
308 SNDRV_PCM_FORMAT_S24_LE,
309 SNDRV_PCM_FORMAT_S24_BE,
310 SNDRV_PCM_FORMAT_U24_LE,
311 SNDRV_PCM_FORMAT_U24_BE,
312 SNDRV_PCM_FORMAT_S32_LE,
313 SNDRV_PCM_FORMAT_S32_BE,
314 SNDRV_PCM_FORMAT_U32_LE,
315 SNDRV_PCM_FORMAT_U32_BE,
320 snd_pcm_format_t snd_pcm_plug_slave_format(snd_pcm_format_t format,
321 const struct snd_mask *format_mask)
325 if (snd_mask_test(format_mask, (__force int)format))
327 if (!snd_pcm_plug_formats(format_mask, format))
328 return (__force snd_pcm_format_t)-EINVAL;
329 if (snd_pcm_format_linear(format)) {
330 unsigned int width = snd_pcm_format_width(format);
331 int unsignd = snd_pcm_format_unsigned(format) > 0;
332 int big = snd_pcm_format_big_endian(format) > 0;
333 unsigned int badness, best = -1;
334 snd_pcm_format_t best_format = (__force snd_pcm_format_t)-1;
335 for (i = 0; i < ARRAY_SIZE(preferred_formats); i++) {
336 snd_pcm_format_t f = preferred_formats[i];
338 if (!snd_mask_test(format_mask, (__force int)f))
340 w = snd_pcm_format_width(f);
344 badness = width - w + 32;
345 badness += snd_pcm_format_unsigned(f) != unsignd;
346 badness += snd_pcm_format_big_endian(f) != big;
347 if (badness < best) {
352 if ((__force int)best_format >= 0)
355 return (__force snd_pcm_format_t)-EINVAL;
358 case SNDRV_PCM_FORMAT_MU_LAW:
359 for (i = 0; i < ARRAY_SIZE(preferred_formats); ++i) {
360 snd_pcm_format_t format1 = preferred_formats[i];
361 if (snd_mask_test(format_mask, (__force int)format1))
366 return (__force snd_pcm_format_t)-EINVAL;
371 int snd_pcm_plug_format_plugins(struct snd_pcm_substream *plug,
372 struct snd_pcm_hw_params *params,
373 struct snd_pcm_hw_params *slave_params)
375 struct snd_pcm_plugin_format tmpformat;
376 struct snd_pcm_plugin_format dstformat;
377 struct snd_pcm_plugin_format srcformat;
378 snd_pcm_access_t src_access, dst_access;
379 struct snd_pcm_plugin *plugin = NULL;
381 int stream = snd_pcm_plug_stream(plug);
382 int slave_interleaved = (params_channels(slave_params) == 1 ||
383 params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED);
386 case SNDRV_PCM_STREAM_PLAYBACK:
387 dstformat.format = params_format(slave_params);
388 dstformat.rate = params_rate(slave_params);
389 dstformat.channels = params_channels(slave_params);
390 srcformat.format = params_format(params);
391 srcformat.rate = params_rate(params);
392 srcformat.channels = params_channels(params);
393 src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
394 dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
395 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
397 case SNDRV_PCM_STREAM_CAPTURE:
398 dstformat.format = params_format(params);
399 dstformat.rate = params_rate(params);
400 dstformat.channels = params_channels(params);
401 srcformat.format = params_format(slave_params);
402 srcformat.rate = params_rate(slave_params);
403 srcformat.channels = params_channels(slave_params);
404 src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
405 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
406 dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
412 tmpformat = srcformat;
414 pdprintf("srcformat: format=%i, rate=%i, channels=%i\n",
418 pdprintf("dstformat: format=%i, rate=%i, channels=%i\n",
423 /* Format change (linearization) */
424 if (! rate_match(srcformat.rate, dstformat.rate) &&
425 ! snd_pcm_format_linear(srcformat.format)) {
426 if (srcformat.format != SNDRV_PCM_FORMAT_MU_LAW)
428 tmpformat.format = SNDRV_PCM_FORMAT_S16;
429 err = snd_pcm_plugin_build_mulaw(plug,
430 &srcformat, &tmpformat,
434 err = snd_pcm_plugin_append(plugin);
436 snd_pcm_plugin_free(plugin);
439 srcformat = tmpformat;
440 src_access = dst_access;
443 /* channels reduction */
444 if (srcformat.channels > dstformat.channels) {
445 tmpformat.channels = dstformat.channels;
446 err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
447 pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
450 err = snd_pcm_plugin_append(plugin);
452 snd_pcm_plugin_free(plugin);
455 srcformat = tmpformat;
456 src_access = dst_access;
459 /* rate resampling */
460 if (!rate_match(srcformat.rate, dstformat.rate)) {
461 if (srcformat.format != SNDRV_PCM_FORMAT_S16) {
462 /* convert to S16 for resampling */
463 tmpformat.format = SNDRV_PCM_FORMAT_S16;
464 err = snd_pcm_plugin_build_linear(plug,
465 &srcformat, &tmpformat,
469 err = snd_pcm_plugin_append(plugin);
471 snd_pcm_plugin_free(plugin);
474 srcformat = tmpformat;
475 src_access = dst_access;
477 tmpformat.rate = dstformat.rate;
478 err = snd_pcm_plugin_build_rate(plug,
479 &srcformat, &tmpformat,
481 pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
484 err = snd_pcm_plugin_append(plugin);
486 snd_pcm_plugin_free(plugin);
489 srcformat = tmpformat;
490 src_access = dst_access;
494 if (srcformat.format != dstformat.format) {
495 tmpformat.format = dstformat.format;
496 if (srcformat.format == SNDRV_PCM_FORMAT_MU_LAW ||
497 tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) {
498 err = snd_pcm_plugin_build_mulaw(plug,
499 &srcformat, &tmpformat,
502 else if (snd_pcm_format_linear(srcformat.format) &&
503 snd_pcm_format_linear(tmpformat.format)) {
504 err = snd_pcm_plugin_build_linear(plug,
505 &srcformat, &tmpformat,
510 pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
513 err = snd_pcm_plugin_append(plugin);
515 snd_pcm_plugin_free(plugin);
518 srcformat = tmpformat;
519 src_access = dst_access;
522 /* channels extension */
523 if (srcformat.channels < dstformat.channels) {
524 tmpformat.channels = dstformat.channels;
525 err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
526 pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
529 err = snd_pcm_plugin_append(plugin);
531 snd_pcm_plugin_free(plugin);
534 srcformat = tmpformat;
535 src_access = dst_access;
539 if (src_access != dst_access) {
540 err = snd_pcm_plugin_build_copy(plug,
544 pdprintf("interleave change (copy: returns %i)\n", err);
547 err = snd_pcm_plugin_append(plugin);
549 snd_pcm_plugin_free(plugin);
557 snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(struct snd_pcm_substream *plug,
559 snd_pcm_uframes_t count,
560 struct snd_pcm_plugin_channel **channels)
562 struct snd_pcm_plugin *plugin;
563 struct snd_pcm_plugin_channel *v;
564 struct snd_pcm_plugin_format *format;
565 int width, nchannels, channel;
566 int stream = snd_pcm_plug_stream(plug);
568 if (snd_BUG_ON(!buf))
570 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
571 plugin = snd_pcm_plug_first(plug);
572 format = &plugin->src_format;
574 plugin = snd_pcm_plug_last(plug);
575 format = &plugin->dst_format;
577 v = plugin->buf_channels;
579 width = snd_pcm_format_physical_width(format->format);
582 nchannels = format->channels;
583 if (snd_BUG_ON(plugin->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
584 format->channels > 1))
586 for (channel = 0; channel < nchannels; channel++, v++) {
589 v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE);
591 v->area.first = channel * width;
592 v->area.step = nchannels * width;
597 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)
599 struct snd_pcm_plugin *plugin, *next;
600 struct snd_pcm_plugin_channel *dst_channels;
602 snd_pcm_sframes_t frames = size;
604 plugin = snd_pcm_plug_first(plug);
610 snd_pcm_sframes_t frames1 = frames;
611 if (plugin->dst_frames) {
612 frames1 = plugin->dst_frames(plugin, frames);
616 err = next->client_channels(next, frames1, &dst_channels);
619 if (err != frames1) {
621 if (plugin->src_frames) {
622 frames = plugin->src_frames(plugin, frames1);
629 pdprintf("write plugin: %s, %li\n", plugin->name, frames);
630 frames = plugin->transfer(plugin, src_channels, dst_channels, frames);
633 src_channels = dst_channels;
636 return calc_src_frames(plug, frames, true);
639 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)
641 struct snd_pcm_plugin *plugin, *next;
642 struct snd_pcm_plugin_channel *src_channels, *dst_channels;
643 snd_pcm_sframes_t frames = size;
646 frames = calc_src_frames(plug, frames, true);
651 plugin = snd_pcm_plug_first(plug);
652 while (plugin && frames > 0) {
655 err = plugin->client_channels(plugin, frames, &dst_channels);
660 dst_channels = dst_channels_final;
662 pdprintf("read plugin: %s, %li\n", plugin->name, frames);
663 frames = plugin->transfer(plugin, src_channels, dst_channels, frames);
667 src_channels = dst_channels;
672 int snd_pcm_area_silence(const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
673 size_t samples, snd_pcm_format_t format)
675 /* FIXME: sub byte resolution and odd dst_offset */
677 unsigned int dst_step;
679 const unsigned char *silence;
682 dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
683 width = snd_pcm_format_physical_width(format);
686 if (dst_area->step == (unsigned int) width && width >= 8)
687 return snd_pcm_format_set_silence(format, dst, samples);
688 silence = snd_pcm_format_silence_64(format);
691 dst_step = dst_area->step / 8;
694 int dstbit = dst_area->first % 8;
695 int dstbit_step = dst_area->step % 8;
696 while (samples-- > 0) {
702 dstbit += dstbit_step;
710 while (samples-- > 0) {
711 memcpy(dst, silence, width);
718 int snd_pcm_area_copy(const struct snd_pcm_channel_area *src_area, size_t src_offset,
719 const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
720 size_t samples, snd_pcm_format_t format)
722 /* FIXME: sub byte resolution and odd dst_offset */
725 int src_step, dst_step;
726 src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
728 return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
729 dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
732 width = snd_pcm_format_physical_width(format);
735 if (src_area->step == (unsigned int) width &&
736 dst_area->step == (unsigned int) width && width >= 8) {
737 size_t bytes = samples * width / 8;
738 memcpy(dst, src, bytes);
741 src_step = src_area->step / 8;
742 dst_step = dst_area->step / 8;
745 int srcbit = src_area->first % 8;
746 int srcbit_step = src_area->step % 8;
747 int dstbit = dst_area->first % 8;
748 int dstbit_step = dst_area->step % 8;
749 while (samples-- > 0) {
750 unsigned char srcval;
752 srcval = *src & 0x0f;
754 srcval = (*src & 0xf0) >> 4;
756 *dst = (*dst & 0xf0) | srcval;
758 *dst = (*dst & 0x0f) | (srcval << 4);
760 srcbit += srcbit_step;
766 dstbit += dstbit_step;
774 while (samples-- > 0) {
775 memcpy(dst, src, width);