27 #include <linux/slab.h>
28 #include <linux/time.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)
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;
58 format = &plugin->src_format;
60 format = &plugin->dst_format;
64 size = frames * format->channels *
width;
68 if (plugin->buf_frames < frames) {
71 plugin->buf_frames = frames;
74 plugin->buf_frames = 0;
77 c = plugin->buf_channels;
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;
90 size /= format->channels;
91 for (channel = 0; channel < format->channels; channel++, c++) {
95 c->area.addr = plugin->buf + (channel *
size);
111 while (plugin->next) {
112 if (plugin->dst_frames)
113 frames = plugin->dst_frames(plugin, frames);
116 plugin = plugin->next;
117 err = snd_pcm_plugin_alloc(plugin, frames);
123 while (plugin->prev) {
124 if (plugin->src_frames)
125 frames = plugin->src_frames(plugin, frames);
128 plugin = plugin->prev;
129 err = snd_pcm_plugin_alloc(plugin, frames);
140 struct snd_pcm_plugin_channel **
channels)
142 *channels = plugin->buf_channels;
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;
160 plugin = kzalloc(
sizeof(*plugin) + extra,
GFP_KERNEL);
165 plugin->
stream = snd_pcm_plug_stream(plug);
167 plugin->src_format = *src_format;
170 plugin->dst_format = *dst_format;
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) {
191 if (plugin->private_free)
192 plugin->private_free(plugin);
193 kfree(plugin->buf_channels);
201 struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
202 int stream = snd_pcm_plug_stream(plug);
210 while (plugin && drv_frames > 0) {
211 plugin_prev = plugin->prev;
212 if (plugin->src_frames)
213 drv_frames = plugin->src_frames(plugin, drv_frames);
214 plugin = plugin_prev;
218 while (plugin && drv_frames > 0) {
219 plugin_next = plugin->next;
220 if (plugin->dst_frames)
221 drv_frames = plugin->dst_frames(plugin, drv_frames);
222 plugin = plugin_next;
231 struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
233 int stream = snd_pcm_plug_stream(plug);
242 while (plugin && frames > 0) {
243 plugin_next = plugin->next;
244 if (plugin->dst_frames) {
245 frames = plugin->dst_frames(plugin, frames);
249 plugin = plugin_next;
254 plugin_prev = plugin->prev;
255 if (plugin->src_frames) {
256 frames = plugin->src_frames(plugin, frames);
260 plugin = plugin_prev;
281 if (formats.
bits[0] & (
u32)linfmts)
282 formats.
bits[0] |= (
u32)linfmts;
283 if (formats.
bits[1] & (
u32)(linfmts >> 32))
284 formats.
bits[1] |= (
u32)(linfmts >> 32);
285 return snd_mask_test(&formats, (
__force int)format);
314 if (snd_mask_test(format_mask, (
__force int)format))
316 if (!snd_pcm_plug_formats(format_mask, format))
322 unsigned int badness, best = -1;
324 for (i = 0; i <
ARRAY_SIZE(preferred_formats); i++) {
325 snd_pcm_format_t
f = preferred_formats[
i];
327 if (!snd_mask_test(format_mask, (
__force int)f))
333 badness = width - w + 32;
336 if (badness < best) {
341 if ((
__force int)best_format >= 0)
348 for (i = 0; i <
ARRAY_SIZE(preferred_formats); ++
i) {
349 snd_pcm_format_t format1 = preferred_formats[
i];
350 if (snd_mask_test(format_mask, (
__force int)format1))
363 struct snd_pcm_plugin_format tmpformat;
364 struct snd_pcm_plugin_format dstformat;
365 struct snd_pcm_plugin_format srcformat;
367 struct snd_pcm_plugin *plugin =
NULL;
369 int stream = snd_pcm_plug_stream(plug);
400 tmpformat = srcformat;
402 pdprintf(
"srcformat: format=%i, rate=%i, channels=%i\n",
406 pdprintf(
"dstformat: format=%i, rate=%i, channels=%i\n",
412 if (! rate_match(srcformat.rate, dstformat.rate) &&
416 tmpformat.format = SNDRV_PCM_FORMAT_S16;
418 &srcformat, &tmpformat,
422 err = snd_pcm_plugin_append(plugin);
427 srcformat = tmpformat;
428 src_access = dst_access;
432 if (srcformat.channels > dstformat.channels) {
433 tmpformat.channels = dstformat.channels;
435 pdprintf(
"channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
438 err = snd_pcm_plugin_append(plugin);
443 srcformat = tmpformat;
444 src_access = dst_access;
448 if (!rate_match(srcformat.rate, dstformat.rate)) {
449 if (srcformat.format != SNDRV_PCM_FORMAT_S16) {
451 tmpformat.format = SNDRV_PCM_FORMAT_S16;
453 &srcformat, &tmpformat,
457 err = snd_pcm_plugin_append(plugin);
462 srcformat = tmpformat;
463 src_access = dst_access;
465 tmpformat.rate = dstformat.rate;
467 &srcformat, &tmpformat,
469 pdprintf(
"rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
472 err = snd_pcm_plugin_append(plugin);
477 srcformat = tmpformat;
478 src_access = dst_access;
482 if (srcformat.format != dstformat.format) {
483 tmpformat.format = dstformat.format;
487 &srcformat, &tmpformat,
493 &srcformat, &tmpformat,
498 pdprintf(
"format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
501 err = snd_pcm_plugin_append(plugin);
506 srcformat = tmpformat;
507 src_access = dst_access;
511 if (srcformat.channels < dstformat.channels) {
512 tmpformat.channels = dstformat.channels;
514 pdprintf(
"channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
517 err = snd_pcm_plugin_append(plugin);
522 srcformat = tmpformat;
523 src_access = dst_access;
527 if (src_access != dst_access) {
532 pdprintf(
"interleave change (copy: returns %i)\n", err);
535 err = snd_pcm_plugin_append(plugin);
548 struct snd_pcm_plugin_channel **
channels)
550 struct snd_pcm_plugin *plugin;
551 struct snd_pcm_plugin_channel *
v;
552 struct snd_pcm_plugin_format *
format;
554 int stream = snd_pcm_plug_stream(plug);
560 format = &plugin->src_format;
563 format = &plugin->dst_format;
565 v = plugin->buf_channels;
569 nchannels = format->channels;
571 format->channels > 1))
573 for (channel = 0; channel < nchannels; channel++, v++) {
578 v->area.first = channel *
width;
579 v->area.step = nchannels *
width;
586 struct snd_pcm_plugin *plugin, *
next;
587 struct snd_pcm_plugin_channel *dst_channels;
592 while (plugin && frames > 0) {
593 if ((next = plugin->next) !=
NULL) {
595 if (plugin->dst_frames)
596 frames1 = plugin->dst_frames(plugin, frames);
597 if ((err = next->client_channels(next, frames1, &dst_channels)) < 0) {
600 if (err != frames1) {
602 if (plugin->src_frames)
603 frames = plugin->src_frames(plugin, frames1);
607 pdprintf(
"write plugin: %s, %li\n", plugin->name, frames);
608 if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
610 src_channels = dst_channels;
618 struct snd_pcm_plugin *plugin, *
next;
619 struct snd_pcm_plugin_channel *src_channels, *dst_channels;
629 while (plugin && frames > 0) {
630 if ((next = plugin->next) !=
NULL) {
631 if ((err = plugin->client_channels(plugin, frames, &dst_channels)) < 0) {
636 dst_channels = dst_channels_final;
638 pdprintf(
"read plugin: %s, %li\n", plugin->name, frames);
639 if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
642 src_channels = dst_channels;
652 unsigned int dst_step;
654 const unsigned char *silence;
657 dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
661 if (dst_area->step == (
unsigned int) width && width >= 8)
666 dst_step = dst_area->step / 8;
669 int dstbit = dst_area->first % 8;
670 int dstbit_step = dst_area->step % 8;
671 while (samples-- > 0) {
677 dstbit += dstbit_step;
685 while (samples-- > 0) {
686 memcpy(dst, silence, width);
694 const struct snd_pcm_channel_area *dst_area,
size_t dst_offset,
700 int src_step, dst_step;
701 src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
704 dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
710 if (src_area->step == (
unsigned int) width &&
711 dst_area->step == (
unsigned int) width && width >= 8) {
712 size_t bytes = samples * width / 8;
716 src_step = src_area->step / 8;
717 dst_step = dst_area->step / 8;
720 int srcbit = src_area->first % 8;
721 int srcbit_step = src_area->step % 8;
722 int dstbit = dst_area->first % 8;
723 int dstbit_step = dst_area->step % 8;
724 while (samples-- > 0) {
725 unsigned char srcval;
727 srcval = *src & 0x0f;
729 srcval = (*src & 0xf0) >> 4;
731 *dst = (*dst & 0xf0) | srcval;
733 *dst = (*dst & 0x0f) | (srcval << 4);
735 srcbit += srcbit_step;
741 dstbit += dstbit_step;
749 while (samples-- > 0) {