Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * PCM Plug-In shared (kernel/library) code |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 3 | * Copyright (c) 1999 by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org> |
| 5 | * |
| 6 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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 |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #if 0 |
| 24 | #define PLUGIN_DEBUG |
| 25 | #endif |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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" |
| 34 | |
| 35 | #define snd_pcm_plug_first(plug) ((plug)->runtime->oss.plugin_first) |
| 36 | #define snd_pcm_plug_last(plug) ((plug)->runtime->oss.plugin_last) |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | /* |
| 39 | * because some cards might have rates "very close", we ignore |
| 40 | * all "resampling" requests within +-5% |
| 41 | */ |
| 42 | static int rate_match(unsigned int src_rate, unsigned int dst_rate) |
| 43 | { |
| 44 | unsigned int low = (src_rate * 95) / 100; |
| 45 | unsigned int high = (src_rate * 105) / 100; |
| 46 | return dst_rate >= low && dst_rate <= high; |
| 47 | } |
| 48 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 49 | static int snd_pcm_plugin_alloc(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 51 | struct snd_pcm_plugin_format *format; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | ssize_t width; |
| 53 | size_t size; |
| 54 | unsigned int channel; |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 55 | struct snd_pcm_plugin_channel *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 58 | format = &plugin->src_format; |
| 59 | } else { |
| 60 | format = &plugin->dst_format; |
| 61 | } |
| 62 | if ((width = snd_pcm_format_physical_width(format->format)) < 0) |
| 63 | return width; |
| 64 | size = frames * format->channels * width; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 65 | if (snd_BUG_ON(size % 8)) |
| 66 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | size /= 8; |
| 68 | if (plugin->buf_frames < frames) { |
| 69 | vfree(plugin->buf); |
| 70 | plugin->buf = vmalloc(size); |
| 71 | plugin->buf_frames = frames; |
| 72 | } |
| 73 | if (!plugin->buf) { |
| 74 | plugin->buf_frames = 0; |
| 75 | return -ENOMEM; |
| 76 | } |
| 77 | c = plugin->buf_channels; |
| 78 | if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) { |
| 79 | for (channel = 0; channel < format->channels; channel++, c++) { |
| 80 | c->frames = frames; |
| 81 | c->enabled = 1; |
| 82 | c->wanted = 0; |
| 83 | c->area.addr = plugin->buf; |
| 84 | c->area.first = channel * width; |
| 85 | c->area.step = format->channels * width; |
| 86 | } |
| 87 | } else if (plugin->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 88 | if (snd_BUG_ON(size % format->channels)) |
| 89 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | size /= format->channels; |
| 91 | for (channel = 0; channel < format->channels; channel++, c++) { |
| 92 | c->frames = frames; |
| 93 | c->enabled = 1; |
| 94 | c->wanted = 0; |
| 95 | c->area.addr = plugin->buf + (channel * size); |
| 96 | c->area.first = 0; |
| 97 | c->area.step = width; |
| 98 | } |
| 99 | } else |
| 100 | return -EINVAL; |
| 101 | return 0; |
| 102 | } |
| 103 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 104 | int snd_pcm_plug_alloc(struct snd_pcm_substream *plug, snd_pcm_uframes_t frames) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
| 106 | int err; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 107 | if (snd_BUG_ON(!snd_pcm_plug_first(plug))) |
| 108 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 110 | struct snd_pcm_plugin *plugin = snd_pcm_plug_first(plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | while (plugin->next) { |
| 112 | if (plugin->dst_frames) |
| 113 | frames = plugin->dst_frames(plugin, frames); |
Takashi Iwai | 51d3d67 | 2020-03-12 16:57:30 +0100 | [diff] [blame] | 114 | if ((snd_pcm_sframes_t)frames <= 0) |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 115 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | plugin = plugin->next; |
| 117 | err = snd_pcm_plugin_alloc(plugin, frames); |
| 118 | if (err < 0) |
| 119 | return err; |
| 120 | } |
| 121 | } else { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 122 | struct snd_pcm_plugin *plugin = snd_pcm_plug_last(plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | while (plugin->prev) { |
| 124 | if (plugin->src_frames) |
| 125 | frames = plugin->src_frames(plugin, frames); |
Takashi Iwai | 51d3d67 | 2020-03-12 16:57:30 +0100 | [diff] [blame] | 126 | if ((snd_pcm_sframes_t)frames <= 0) |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 127 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | plugin = plugin->prev; |
| 129 | err = snd_pcm_plugin_alloc(plugin, frames); |
| 130 | if (err < 0) |
| 131 | return err; |
| 132 | } |
| 133 | } |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 138 | snd_pcm_sframes_t snd_pcm_plugin_client_channels(struct snd_pcm_plugin *plugin, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | snd_pcm_uframes_t frames, |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 140 | struct snd_pcm_plugin_channel **channels) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
| 142 | *channels = plugin->buf_channels; |
| 143 | return frames; |
| 144 | } |
| 145 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 146 | int snd_pcm_plugin_build(struct snd_pcm_substream *plug, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | const char *name, |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 148 | struct snd_pcm_plugin_format *src_format, |
| 149 | struct snd_pcm_plugin_format *dst_format, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | size_t extra, |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 151 | struct snd_pcm_plugin **ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 153 | struct snd_pcm_plugin *plugin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | unsigned int channels; |
| 155 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 156 | if (snd_BUG_ON(!plug)) |
| 157 | return -ENXIO; |
| 158 | if (snd_BUG_ON(!src_format || !dst_format)) |
| 159 | return -ENXIO; |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 160 | plugin = kzalloc(sizeof(*plugin) + extra, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | if (plugin == NULL) |
| 162 | return -ENOMEM; |
| 163 | plugin->name = name; |
| 164 | plugin->plug = plug; |
| 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); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 169 | snd_BUG_ON(plugin->src_width <= 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | plugin->dst_format = *dst_format; |
| 171 | plugin->dst_width = snd_pcm_format_physical_width(dst_format->format); |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 172 | snd_BUG_ON(plugin->dst_width <= 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 174 | channels = src_format->channels; |
| 175 | else |
| 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); |
| 180 | return -ENOMEM; |
| 181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | plugin->client_channels = snd_pcm_plugin_client_channels; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | *ret = plugin; |
| 184 | return 0; |
| 185 | } |
| 186 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 187 | int snd_pcm_plugin_free(struct snd_pcm_plugin *plugin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
| 189 | if (! plugin) |
| 190 | return 0; |
| 191 | if (plugin->private_free) |
| 192 | plugin->private_free(plugin); |
| 193 | kfree(plugin->buf_channels); |
| 194 | vfree(plugin->buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | kfree(plugin); |
| 196 | return 0; |
| 197 | } |
| 198 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 199 | snd_pcm_sframes_t snd_pcm_plug_client_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t drv_frames) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 201 | struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next; |
Xi Wang | 9af4e7f | 2012-11-13 17:12:11 -0500 | [diff] [blame] | 202 | int stream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 204 | if (snd_BUG_ON(!plug)) |
| 205 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | if (drv_frames == 0) |
| 207 | return 0; |
Xi Wang | 9af4e7f | 2012-11-13 17:12:11 -0500 | [diff] [blame] | 208 | stream = snd_pcm_plug_stream(plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 210 | plugin = snd_pcm_plug_last(plug); |
| 211 | while (plugin && drv_frames > 0) { |
Takashi Iwai | e2edae1 | 2020-03-09 09:21:48 +0100 | [diff] [blame] | 212 | if (drv_frames > plugin->buf_frames) |
| 213 | drv_frames = plugin->buf_frames; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | plugin_prev = plugin->prev; |
| 215 | if (plugin->src_frames) |
| 216 | drv_frames = plugin->src_frames(plugin, drv_frames); |
| 217 | plugin = plugin_prev; |
| 218 | } |
| 219 | } else if (stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 220 | plugin = snd_pcm_plug_first(plug); |
| 221 | while (plugin && drv_frames > 0) { |
| 222 | plugin_next = plugin->next; |
| 223 | if (plugin->dst_frames) |
| 224 | drv_frames = plugin->dst_frames(plugin, drv_frames); |
Takashi Iwai | e2edae1 | 2020-03-09 09:21:48 +0100 | [diff] [blame] | 225 | if (drv_frames > plugin->buf_frames) |
| 226 | drv_frames = plugin->buf_frames; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | plugin = plugin_next; |
| 228 | } |
| 229 | } else |
| 230 | snd_BUG(); |
| 231 | return drv_frames; |
| 232 | } |
| 233 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 234 | snd_pcm_sframes_t snd_pcm_plug_slave_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t clt_frames) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 236 | struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | snd_pcm_sframes_t frames; |
Xi Wang | 701ef32 | 2012-11-13 17:12:12 -0500 | [diff] [blame] | 238 | int stream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 240 | if (snd_BUG_ON(!plug)) |
| 241 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | if (clt_frames == 0) |
| 243 | return 0; |
| 244 | frames = clt_frames; |
Xi Wang | 701ef32 | 2012-11-13 17:12:12 -0500 | [diff] [blame] | 245 | stream = snd_pcm_plug_stream(plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 247 | plugin = snd_pcm_plug_first(plug); |
| 248 | while (plugin && frames > 0) { |
| 249 | plugin_next = plugin->next; |
| 250 | if (plugin->dst_frames) { |
| 251 | frames = plugin->dst_frames(plugin, frames); |
| 252 | if (frames < 0) |
| 253 | return frames; |
| 254 | } |
Takashi Iwai | e2edae1 | 2020-03-09 09:21:48 +0100 | [diff] [blame] | 255 | if (frames > plugin->buf_frames) |
| 256 | frames = plugin->buf_frames; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | plugin = plugin_next; |
| 258 | } |
| 259 | } else if (stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 260 | plugin = snd_pcm_plug_last(plug); |
| 261 | while (plugin) { |
Takashi Iwai | e2edae1 | 2020-03-09 09:21:48 +0100 | [diff] [blame] | 262 | if (frames > plugin->buf_frames) |
| 263 | frames = plugin->buf_frames; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | plugin_prev = plugin->prev; |
| 265 | if (plugin->src_frames) { |
| 266 | frames = plugin->src_frames(plugin, frames); |
| 267 | if (frames < 0) |
| 268 | return frames; |
| 269 | } |
| 270 | plugin = plugin_prev; |
| 271 | } |
| 272 | } else |
| 273 | snd_BUG(); |
| 274 | return frames; |
| 275 | } |
| 276 | |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 277 | static int snd_pcm_plug_formats(struct snd_mask *mask, snd_pcm_format_t format) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 279 | struct snd_mask formats = *mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | u64 linfmts = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 | |
| 281 | SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE | |
| 282 | SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE | |
| 283 | SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_S24_LE | |
| 284 | SNDRV_PCM_FMTBIT_U24_BE | SNDRV_PCM_FMTBIT_S24_BE | |
Takashi Iwai | 64d27f9 | 2007-08-08 16:49:08 +0200 | [diff] [blame] | 285 | SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_S24_3LE | |
| 286 | SNDRV_PCM_FMTBIT_U24_3BE | SNDRV_PCM_FMTBIT_S24_3BE | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE | |
| 288 | SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE); |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 289 | snd_mask_set(&formats, (__force int)SNDRV_PCM_FORMAT_MU_LAW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | if (formats.bits[0] & (u32)linfmts) |
| 292 | formats.bits[0] |= (u32)linfmts; |
| 293 | if (formats.bits[1] & (u32)(linfmts >> 32)) |
| 294 | formats.bits[1] |= (u32)(linfmts >> 32); |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 295 | return snd_mask_test(&formats, (__force int)format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 298 | static snd_pcm_format_t preferred_formats[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | SNDRV_PCM_FORMAT_S16_LE, |
| 300 | SNDRV_PCM_FORMAT_S16_BE, |
| 301 | SNDRV_PCM_FORMAT_U16_LE, |
| 302 | SNDRV_PCM_FORMAT_U16_BE, |
Takashi Iwai | 64d27f9 | 2007-08-08 16:49:08 +0200 | [diff] [blame] | 303 | SNDRV_PCM_FORMAT_S24_3LE, |
| 304 | SNDRV_PCM_FORMAT_S24_3BE, |
| 305 | SNDRV_PCM_FORMAT_U24_3LE, |
| 306 | SNDRV_PCM_FORMAT_U24_3BE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | SNDRV_PCM_FORMAT_S24_LE, |
| 308 | SNDRV_PCM_FORMAT_S24_BE, |
| 309 | SNDRV_PCM_FORMAT_U24_LE, |
| 310 | SNDRV_PCM_FORMAT_U24_BE, |
| 311 | SNDRV_PCM_FORMAT_S32_LE, |
| 312 | SNDRV_PCM_FORMAT_S32_BE, |
| 313 | SNDRV_PCM_FORMAT_U32_LE, |
| 314 | SNDRV_PCM_FORMAT_U32_BE, |
| 315 | SNDRV_PCM_FORMAT_S8, |
| 316 | SNDRV_PCM_FORMAT_U8 |
| 317 | }; |
| 318 | |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 319 | snd_pcm_format_t snd_pcm_plug_slave_format(snd_pcm_format_t format, |
| 320 | struct snd_mask *format_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
Takashi Iwai | 64d27f9 | 2007-08-08 16:49:08 +0200 | [diff] [blame] | 322 | int i; |
| 323 | |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 324 | if (snd_mask_test(format_mask, (__force int)format)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | return format; |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 326 | if (!snd_pcm_plug_formats(format_mask, format)) |
| 327 | return (__force snd_pcm_format_t)-EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | if (snd_pcm_format_linear(format)) { |
Takashi Iwai | 64d27f9 | 2007-08-08 16:49:08 +0200 | [diff] [blame] | 329 | unsigned int width = snd_pcm_format_width(format); |
| 330 | int unsignd = snd_pcm_format_unsigned(format) > 0; |
| 331 | int big = snd_pcm_format_big_endian(format) > 0; |
| 332 | unsigned int badness, best = -1; |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 333 | snd_pcm_format_t best_format = (__force snd_pcm_format_t)-1; |
Takashi Iwai | 64d27f9 | 2007-08-08 16:49:08 +0200 | [diff] [blame] | 334 | for (i = 0; i < ARRAY_SIZE(preferred_formats); i++) { |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 335 | snd_pcm_format_t f = preferred_formats[i]; |
Takashi Iwai | 64d27f9 | 2007-08-08 16:49:08 +0200 | [diff] [blame] | 336 | unsigned int w; |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 337 | if (!snd_mask_test(format_mask, (__force int)f)) |
Takashi Iwai | 64d27f9 | 2007-08-08 16:49:08 +0200 | [diff] [blame] | 338 | continue; |
| 339 | w = snd_pcm_format_width(f); |
| 340 | if (w >= width) |
| 341 | badness = w - width; |
| 342 | else |
| 343 | badness = width - w + 32; |
| 344 | badness += snd_pcm_format_unsigned(f) != unsignd; |
| 345 | badness += snd_pcm_format_big_endian(f) != big; |
| 346 | if (badness < best) { |
| 347 | best_format = f; |
| 348 | best = badness; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 351 | if ((__force int)best_format >= 0) |
| 352 | return best_format; |
| 353 | else |
| 354 | return (__force snd_pcm_format_t)-EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | switch (format) { |
| 357 | case SNDRV_PCM_FORMAT_MU_LAW: |
| 358 | for (i = 0; i < ARRAY_SIZE(preferred_formats); ++i) { |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 359 | snd_pcm_format_t format1 = preferred_formats[i]; |
| 360 | if (snd_mask_test(format_mask, (__force int)format1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | return format1; |
| 362 | } |
| 363 | default: |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 364 | return (__force snd_pcm_format_t)-EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 369 | int snd_pcm_plug_format_plugins(struct snd_pcm_substream *plug, |
| 370 | struct snd_pcm_hw_params *params, |
| 371 | struct snd_pcm_hw_params *slave_params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 373 | struct snd_pcm_plugin_format tmpformat; |
| 374 | struct snd_pcm_plugin_format dstformat; |
| 375 | struct snd_pcm_plugin_format srcformat; |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 376 | snd_pcm_access_t src_access, dst_access; |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 377 | struct snd_pcm_plugin *plugin = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | int err; |
| 379 | int stream = snd_pcm_plug_stream(plug); |
| 380 | int slave_interleaved = (params_channels(slave_params) == 1 || |
| 381 | params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED); |
| 382 | |
| 383 | switch (stream) { |
| 384 | case SNDRV_PCM_STREAM_PLAYBACK: |
| 385 | dstformat.format = params_format(slave_params); |
| 386 | dstformat.rate = params_rate(slave_params); |
| 387 | dstformat.channels = params_channels(slave_params); |
| 388 | srcformat.format = params_format(params); |
| 389 | srcformat.rate = params_rate(params); |
| 390 | srcformat.channels = params_channels(params); |
| 391 | src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED; |
| 392 | dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED : |
| 393 | SNDRV_PCM_ACCESS_RW_NONINTERLEAVED); |
| 394 | break; |
| 395 | case SNDRV_PCM_STREAM_CAPTURE: |
| 396 | dstformat.format = params_format(params); |
| 397 | dstformat.rate = params_rate(params); |
| 398 | dstformat.channels = params_channels(params); |
| 399 | srcformat.format = params_format(slave_params); |
| 400 | srcformat.rate = params_rate(slave_params); |
| 401 | srcformat.channels = params_channels(slave_params); |
| 402 | src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED : |
| 403 | SNDRV_PCM_ACCESS_RW_NONINTERLEAVED); |
| 404 | dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED; |
| 405 | break; |
| 406 | default: |
| 407 | snd_BUG(); |
| 408 | return -EINVAL; |
| 409 | } |
| 410 | tmpformat = srcformat; |
| 411 | |
| 412 | pdprintf("srcformat: format=%i, rate=%i, channels=%i\n", |
| 413 | srcformat.format, |
| 414 | srcformat.rate, |
| 415 | srcformat.channels); |
| 416 | pdprintf("dstformat: format=%i, rate=%i, channels=%i\n", |
| 417 | dstformat.format, |
| 418 | dstformat.rate, |
| 419 | dstformat.channels); |
| 420 | |
| 421 | /* Format change (linearization) */ |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 422 | if (! rate_match(srcformat.rate, dstformat.rate) && |
| 423 | ! snd_pcm_format_linear(srcformat.format)) { |
| 424 | if (srcformat.format != SNDRV_PCM_FORMAT_MU_LAW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | return -EINVAL; |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 426 | tmpformat.format = SNDRV_PCM_FORMAT_S16; |
| 427 | err = snd_pcm_plugin_build_mulaw(plug, |
| 428 | &srcformat, &tmpformat, |
| 429 | &plugin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | if (err < 0) |
| 431 | return err; |
| 432 | err = snd_pcm_plugin_append(plugin); |
| 433 | if (err < 0) { |
| 434 | snd_pcm_plugin_free(plugin); |
| 435 | return err; |
| 436 | } |
| 437 | srcformat = tmpformat; |
| 438 | src_access = dst_access; |
| 439 | } |
| 440 | |
| 441 | /* channels reduction */ |
| 442 | if (srcformat.channels > dstformat.channels) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | tmpformat.channels = dstformat.channels; |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 444 | err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err); |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 446 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | err = snd_pcm_plugin_append(plugin); |
| 449 | if (err < 0) { |
| 450 | snd_pcm_plugin_free(plugin); |
| 451 | return err; |
| 452 | } |
| 453 | srcformat = tmpformat; |
| 454 | src_access = dst_access; |
| 455 | } |
| 456 | |
| 457 | /* rate resampling */ |
| 458 | if (!rate_match(srcformat.rate, dstformat.rate)) { |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 459 | if (srcformat.format != SNDRV_PCM_FORMAT_S16) { |
| 460 | /* convert to S16 for resampling */ |
| 461 | tmpformat.format = SNDRV_PCM_FORMAT_S16; |
| 462 | err = snd_pcm_plugin_build_linear(plug, |
| 463 | &srcformat, &tmpformat, |
| 464 | &plugin); |
| 465 | if (err < 0) |
| 466 | return err; |
| 467 | err = snd_pcm_plugin_append(plugin); |
| 468 | if (err < 0) { |
| 469 | snd_pcm_plugin_free(plugin); |
| 470 | return err; |
| 471 | } |
| 472 | srcformat = tmpformat; |
| 473 | src_access = dst_access; |
| 474 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | tmpformat.rate = dstformat.rate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | err = snd_pcm_plugin_build_rate(plug, |
| 477 | &srcformat, &tmpformat, |
| 478 | &plugin); |
| 479 | pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err); |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 480 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | err = snd_pcm_plugin_append(plugin); |
| 483 | if (err < 0) { |
| 484 | snd_pcm_plugin_free(plugin); |
| 485 | return err; |
| 486 | } |
| 487 | srcformat = tmpformat; |
| 488 | src_access = dst_access; |
| 489 | } |
| 490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | /* format change */ |
| 492 | if (srcformat.format != dstformat.format) { |
| 493 | tmpformat.format = dstformat.format; |
Takashi Iwai | c82590d | 2006-01-20 17:13:45 +0100 | [diff] [blame] | 494 | if (srcformat.format == SNDRV_PCM_FORMAT_MU_LAW || |
| 495 | tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | err = snd_pcm_plugin_build_mulaw(plug, |
| 497 | &srcformat, &tmpformat, |
| 498 | &plugin); |
| 499 | } |
| 500 | else if (snd_pcm_format_linear(srcformat.format) && |
| 501 | snd_pcm_format_linear(tmpformat.format)) { |
| 502 | err = snd_pcm_plugin_build_linear(plug, |
| 503 | &srcformat, &tmpformat, |
| 504 | &plugin); |
| 505 | } |
| 506 | else |
| 507 | return -EINVAL; |
| 508 | pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err); |
| 509 | if (err < 0) |
| 510 | return err; |
| 511 | err = snd_pcm_plugin_append(plugin); |
| 512 | if (err < 0) { |
| 513 | snd_pcm_plugin_free(plugin); |
| 514 | return err; |
| 515 | } |
| 516 | srcformat = tmpformat; |
| 517 | src_access = dst_access; |
| 518 | } |
| 519 | |
Takashi Iwai | 0534ab4 | 2006-01-13 12:09:12 +0100 | [diff] [blame] | 520 | /* channels extension */ |
| 521 | if (srcformat.channels < dstformat.channels) { |
| 522 | tmpformat.channels = dstformat.channels; |
| 523 | err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin); |
| 524 | pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err); |
| 525 | if (err < 0) |
| 526 | return err; |
| 527 | err = snd_pcm_plugin_append(plugin); |
| 528 | if (err < 0) { |
| 529 | snd_pcm_plugin_free(plugin); |
| 530 | return err; |
| 531 | } |
| 532 | srcformat = tmpformat; |
| 533 | src_access = dst_access; |
| 534 | } |
| 535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | /* de-interleave */ |
| 537 | if (src_access != dst_access) { |
| 538 | err = snd_pcm_plugin_build_copy(plug, |
| 539 | &srcformat, |
| 540 | &tmpformat, |
| 541 | &plugin); |
| 542 | pdprintf("interleave change (copy: returns %i)\n", err); |
| 543 | if (err < 0) |
| 544 | return err; |
| 545 | err = snd_pcm_plugin_append(plugin); |
| 546 | if (err < 0) { |
| 547 | snd_pcm_plugin_free(plugin); |
| 548 | return err; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 555 | snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(struct snd_pcm_substream *plug, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | char *buf, |
| 557 | snd_pcm_uframes_t count, |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 558 | struct snd_pcm_plugin_channel **channels) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 560 | struct snd_pcm_plugin *plugin; |
| 561 | struct snd_pcm_plugin_channel *v; |
| 562 | struct snd_pcm_plugin_format *format; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | int width, nchannels, channel; |
| 564 | int stream = snd_pcm_plug_stream(plug); |
| 565 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 566 | if (snd_BUG_ON(!buf)) |
| 567 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 569 | plugin = snd_pcm_plug_first(plug); |
| 570 | format = &plugin->src_format; |
| 571 | } else { |
| 572 | plugin = snd_pcm_plug_last(plug); |
| 573 | format = &plugin->dst_format; |
| 574 | } |
| 575 | v = plugin->buf_channels; |
| 576 | *channels = v; |
| 577 | if ((width = snd_pcm_format_physical_width(format->format)) < 0) |
| 578 | return width; |
| 579 | nchannels = format->channels; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 580 | if (snd_BUG_ON(plugin->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED && |
| 581 | format->channels > 1)) |
| 582 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | for (channel = 0; channel < nchannels; channel++, v++) { |
| 584 | v->frames = count; |
| 585 | v->enabled = 1; |
| 586 | v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE); |
| 587 | v->area.addr = buf; |
| 588 | v->area.first = channel * width; |
| 589 | v->area.step = nchannels * width; |
| 590 | } |
| 591 | return count; |
| 592 | } |
| 593 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 594 | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 596 | struct snd_pcm_plugin *plugin, *next; |
| 597 | struct snd_pcm_plugin_channel *dst_channels; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | int err; |
| 599 | snd_pcm_sframes_t frames = size; |
| 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | plugin = snd_pcm_plug_first(plug); |
Takashi Iwai | 8e81425 | 2018-01-04 16:39:27 +0100 | [diff] [blame] | 602 | while (plugin) { |
| 603 | if (frames <= 0) |
| 604 | return frames; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | if ((next = plugin->next) != NULL) { |
| 606 | snd_pcm_sframes_t frames1 = frames; |
Takashi Iwai | 8e81425 | 2018-01-04 16:39:27 +0100 | [diff] [blame] | 607 | if (plugin->dst_frames) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | frames1 = plugin->dst_frames(plugin, frames); |
Takashi Iwai | 8e81425 | 2018-01-04 16:39:27 +0100 | [diff] [blame] | 609 | if (frames1 <= 0) |
| 610 | return frames1; |
| 611 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | if ((err = next->client_channels(next, frames1, &dst_channels)) < 0) { |
| 613 | return err; |
| 614 | } |
| 615 | if (err != frames1) { |
| 616 | frames = err; |
Takashi Iwai | 8e81425 | 2018-01-04 16:39:27 +0100 | [diff] [blame] | 617 | if (plugin->src_frames) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | frames = plugin->src_frames(plugin, frames1); |
Takashi Iwai | 8e81425 | 2018-01-04 16:39:27 +0100 | [diff] [blame] | 619 | if (frames <= 0) |
| 620 | return frames; |
| 621 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
| 623 | } else |
| 624 | dst_channels = NULL; |
| 625 | pdprintf("write plugin: %s, %li\n", plugin->name, frames); |
| 626 | if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0) |
| 627 | return frames; |
| 628 | src_channels = dst_channels; |
| 629 | plugin = next; |
| 630 | } |
| 631 | return snd_pcm_plug_client_size(plug, frames); |
| 632 | } |
| 633 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 634 | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 636 | struct snd_pcm_plugin *plugin, *next; |
| 637 | struct snd_pcm_plugin_channel *src_channels, *dst_channels; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | snd_pcm_sframes_t frames = size; |
| 639 | int err; |
| 640 | |
| 641 | frames = snd_pcm_plug_slave_size(plug, frames); |
| 642 | if (frames < 0) |
| 643 | return frames; |
| 644 | |
| 645 | src_channels = NULL; |
| 646 | plugin = snd_pcm_plug_first(plug); |
| 647 | while (plugin && frames > 0) { |
| 648 | if ((next = plugin->next) != NULL) { |
| 649 | if ((err = plugin->client_channels(plugin, frames, &dst_channels)) < 0) { |
| 650 | return err; |
| 651 | } |
| 652 | frames = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | } else { |
| 654 | dst_channels = dst_channels_final; |
| 655 | } |
| 656 | pdprintf("read plugin: %s, %li\n", plugin->name, frames); |
| 657 | if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0) |
| 658 | return frames; |
| 659 | plugin = next; |
| 660 | src_channels = dst_channels; |
| 661 | } |
| 662 | return frames; |
| 663 | } |
| 664 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 665 | int snd_pcm_area_silence(const struct snd_pcm_channel_area *dst_area, size_t dst_offset, |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 666 | size_t samples, snd_pcm_format_t format) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
| 668 | /* FIXME: sub byte resolution and odd dst_offset */ |
| 669 | unsigned char *dst; |
| 670 | unsigned int dst_step; |
| 671 | int width; |
| 672 | const unsigned char *silence; |
| 673 | if (!dst_area->addr) |
| 674 | return 0; |
| 675 | dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8; |
| 676 | width = snd_pcm_format_physical_width(format); |
| 677 | if (width <= 0) |
| 678 | return -EINVAL; |
| 679 | if (dst_area->step == (unsigned int) width && width >= 8) |
| 680 | return snd_pcm_format_set_silence(format, dst, samples); |
| 681 | silence = snd_pcm_format_silence_64(format); |
| 682 | if (! silence) |
| 683 | return -EINVAL; |
| 684 | dst_step = dst_area->step / 8; |
| 685 | if (width == 4) { |
| 686 | /* Ima ADPCM */ |
| 687 | int dstbit = dst_area->first % 8; |
| 688 | int dstbit_step = dst_area->step % 8; |
| 689 | while (samples-- > 0) { |
| 690 | if (dstbit) |
| 691 | *dst &= 0xf0; |
| 692 | else |
| 693 | *dst &= 0x0f; |
| 694 | dst += dst_step; |
| 695 | dstbit += dstbit_step; |
| 696 | if (dstbit == 8) { |
| 697 | dst++; |
| 698 | dstbit = 0; |
| 699 | } |
| 700 | } |
| 701 | } else { |
| 702 | width /= 8; |
| 703 | while (samples-- > 0) { |
| 704 | memcpy(dst, silence, width); |
| 705 | dst += dst_step; |
| 706 | } |
| 707 | } |
| 708 | return 0; |
| 709 | } |
| 710 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 711 | int snd_pcm_area_copy(const struct snd_pcm_channel_area *src_area, size_t src_offset, |
| 712 | const struct snd_pcm_channel_area *dst_area, size_t dst_offset, |
Clemens Ladisch | fea952e | 2011-02-14 11:00:47 +0100 | [diff] [blame] | 713 | size_t samples, snd_pcm_format_t format) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | { |
| 715 | /* FIXME: sub byte resolution and odd dst_offset */ |
| 716 | char *src, *dst; |
| 717 | int width; |
| 718 | int src_step, dst_step; |
| 719 | src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8; |
| 720 | if (!src_area->addr) |
| 721 | return snd_pcm_area_silence(dst_area, dst_offset, samples, format); |
| 722 | dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8; |
| 723 | if (!dst_area->addr) |
| 724 | return 0; |
| 725 | width = snd_pcm_format_physical_width(format); |
| 726 | if (width <= 0) |
| 727 | return -EINVAL; |
| 728 | if (src_area->step == (unsigned int) width && |
| 729 | dst_area->step == (unsigned int) width && width >= 8) { |
| 730 | size_t bytes = samples * width / 8; |
| 731 | memcpy(dst, src, bytes); |
| 732 | return 0; |
| 733 | } |
| 734 | src_step = src_area->step / 8; |
| 735 | dst_step = dst_area->step / 8; |
| 736 | if (width == 4) { |
| 737 | /* Ima ADPCM */ |
| 738 | int srcbit = src_area->first % 8; |
| 739 | int srcbit_step = src_area->step % 8; |
| 740 | int dstbit = dst_area->first % 8; |
| 741 | int dstbit_step = dst_area->step % 8; |
| 742 | while (samples-- > 0) { |
| 743 | unsigned char srcval; |
| 744 | if (srcbit) |
| 745 | srcval = *src & 0x0f; |
| 746 | else |
| 747 | srcval = (*src & 0xf0) >> 4; |
| 748 | if (dstbit) |
| 749 | *dst = (*dst & 0xf0) | srcval; |
| 750 | else |
| 751 | *dst = (*dst & 0x0f) | (srcval << 4); |
| 752 | src += src_step; |
| 753 | srcbit += srcbit_step; |
| 754 | if (srcbit == 8) { |
| 755 | src++; |
| 756 | srcbit = 0; |
| 757 | } |
| 758 | dst += dst_step; |
| 759 | dstbit += dstbit_step; |
| 760 | if (dstbit == 8) { |
| 761 | dst++; |
| 762 | dstbit = 0; |
| 763 | } |
| 764 | } |
| 765 | } else { |
| 766 | width /= 8; |
| 767 | while (samples-- > 0) { |
| 768 | memcpy(dst, src, width); |
| 769 | src += src_step; |
| 770 | dst += dst_step; |
| 771 | } |
| 772 | } |
| 773 | return 0; |
| 774 | } |