blob: c6888d76ca5e98734d1fe9bfa10b3e85d2de328c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PCM Plug-In shared (kernel/library) code
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) 1999 by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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 Torvalds1da177e2005-04-16 15:20:36 -070027#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 Torvalds1da177e2005-04-16 15:20:36 -070038/*
39 * because some cards might have rates "very close", we ignore
40 * all "resampling" requests within +-5%
41 */
42static 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 Iwai6ac77bc2005-11-17 14:01:49 +010049static int snd_pcm_plugin_alloc(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010051 struct snd_pcm_plugin_format *format;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 ssize_t width;
53 size_t size;
54 unsigned int channel;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010055 struct snd_pcm_plugin_channel *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
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 Iwai7eaa9432008-08-08 17:09:09 +020065 if (snd_BUG_ON(size % 8))
66 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 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 Iwai7eaa9432008-08-08 17:09:09 +020088 if (snd_BUG_ON(size % format->channels))
89 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 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 Iwai6ac77bc2005-11-17 14:01:49 +0100104int snd_pcm_plug_alloc(struct snd_pcm_substream *plug, snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 int err;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200107 if (snd_BUG_ON(!snd_pcm_plug_first(plug)))
108 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100110 struct snd_pcm_plugin *plugin = snd_pcm_plug_first(plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 while (plugin->next) {
112 if (plugin->dst_frames)
113 frames = plugin->dst_frames(plugin, frames);
Dan Carpenter2ec852b2018-08-27 12:21:45 +0300114 if (snd_BUG_ON((snd_pcm_sframes_t)frames <= 0))
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200115 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 plugin = plugin->next;
117 err = snd_pcm_plugin_alloc(plugin, frames);
118 if (err < 0)
119 return err;
120 }
121 } else {
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100122 struct snd_pcm_plugin *plugin = snd_pcm_plug_last(plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 while (plugin->prev) {
124 if (plugin->src_frames)
125 frames = plugin->src_frames(plugin, frames);
Dan Carpenter2ec852b2018-08-27 12:21:45 +0300126 if (snd_BUG_ON((snd_pcm_sframes_t)frames <= 0))
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200127 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 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 Iwai6ac77bc2005-11-17 14:01:49 +0100138snd_pcm_sframes_t snd_pcm_plugin_client_channels(struct snd_pcm_plugin *plugin,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 snd_pcm_uframes_t frames,
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100140 struct snd_pcm_plugin_channel **channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 *channels = plugin->buf_channels;
143 return frames;
144}
145
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100146int snd_pcm_plugin_build(struct snd_pcm_substream *plug,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 const char *name,
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100148 struct snd_pcm_plugin_format *src_format,
149 struct snd_pcm_plugin_format *dst_format,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 size_t extra,
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100151 struct snd_pcm_plugin **ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100153 struct snd_pcm_plugin *plugin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 unsigned int channels;
155
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200156 if (snd_BUG_ON(!plug))
157 return -ENXIO;
158 if (snd_BUG_ON(!src_format || !dst_format))
159 return -ENXIO;
Takashi Iwaica2c0962005-09-09 14:20:23 +0200160 plugin = kzalloc(sizeof(*plugin) + extra, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 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 Iwai7eaa9432008-08-08 17:09:09 +0200169 snd_BUG_ON(plugin->src_width <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 plugin->dst_format = *dst_format;
171 plugin->dst_width = snd_pcm_format_physical_width(dst_format->format);
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200172 snd_BUG_ON(plugin->dst_width <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 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 Torvalds1da177e2005-04-16 15:20:36 -0700182 plugin->client_channels = snd_pcm_plugin_client_channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 *ret = plugin;
184 return 0;
185}
186
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100187int snd_pcm_plugin_free(struct snd_pcm_plugin *plugin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
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 Torvalds1da177e2005-04-16 15:20:36 -0700195 kfree(plugin);
196 return 0;
197}
198
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100199snd_pcm_sframes_t snd_pcm_plug_client_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t drv_frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100201 struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
Xi Wang9af4e7f2012-11-13 17:12:11 -0500202 int stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200204 if (snd_BUG_ON(!plug))
205 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (drv_frames == 0)
207 return 0;
Xi Wang9af4e7f2012-11-13 17:12:11 -0500208 stream = snd_pcm_plug_stream(plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
210 plugin = snd_pcm_plug_last(plug);
211 while (plugin && drv_frames > 0) {
212 plugin_prev = plugin->prev;
213 if (plugin->src_frames)
214 drv_frames = plugin->src_frames(plugin, drv_frames);
215 plugin = plugin_prev;
216 }
217 } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
218 plugin = snd_pcm_plug_first(plug);
219 while (plugin && drv_frames > 0) {
220 plugin_next = plugin->next;
221 if (plugin->dst_frames)
222 drv_frames = plugin->dst_frames(plugin, drv_frames);
223 plugin = plugin_next;
224 }
225 } else
226 snd_BUG();
227 return drv_frames;
228}
229
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100230snd_pcm_sframes_t snd_pcm_plug_slave_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t clt_frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100232 struct snd_pcm_plugin *plugin, *plugin_prev, *plugin_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 snd_pcm_sframes_t frames;
Xi Wang701ef322012-11-13 17:12:12 -0500234 int stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200236 if (snd_BUG_ON(!plug))
237 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (clt_frames == 0)
239 return 0;
240 frames = clt_frames;
Xi Wang701ef322012-11-13 17:12:12 -0500241 stream = snd_pcm_plug_stream(plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
243 plugin = snd_pcm_plug_first(plug);
244 while (plugin && frames > 0) {
245 plugin_next = plugin->next;
246 if (plugin->dst_frames) {
247 frames = plugin->dst_frames(plugin, frames);
248 if (frames < 0)
249 return frames;
250 }
251 plugin = plugin_next;
252 }
253 } else if (stream == SNDRV_PCM_STREAM_CAPTURE) {
254 plugin = snd_pcm_plug_last(plug);
255 while (plugin) {
256 plugin_prev = plugin->prev;
257 if (plugin->src_frames) {
258 frames = plugin->src_frames(plugin, frames);
259 if (frames < 0)
260 return frames;
261 }
262 plugin = plugin_prev;
263 }
264 } else
265 snd_BUG();
266 return frames;
267}
268
Clemens Ladischfea952e2011-02-14 11:00:47 +0100269static int snd_pcm_plug_formats(struct snd_mask *mask, snd_pcm_format_t format)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100271 struct snd_mask formats = *mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 u64 linfmts = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
273 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
274 SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
275 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_S24_LE |
276 SNDRV_PCM_FMTBIT_U24_BE | SNDRV_PCM_FMTBIT_S24_BE |
Takashi Iwai64d27f92007-08-08 16:49:08 +0200277 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_S24_3LE |
278 SNDRV_PCM_FMTBIT_U24_3BE | SNDRV_PCM_FMTBIT_S24_3BE |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
280 SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE);
Clemens Ladischfea952e2011-02-14 11:00:47 +0100281 snd_mask_set(&formats, (__force int)SNDRV_PCM_FORMAT_MU_LAW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (formats.bits[0] & (u32)linfmts)
284 formats.bits[0] |= (u32)linfmts;
285 if (formats.bits[1] & (u32)(linfmts >> 32))
286 formats.bits[1] |= (u32)(linfmts >> 32);
Clemens Ladischfea952e2011-02-14 11:00:47 +0100287 return snd_mask_test(&formats, (__force int)format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Clemens Ladischfea952e2011-02-14 11:00:47 +0100290static snd_pcm_format_t preferred_formats[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 SNDRV_PCM_FORMAT_S16_LE,
292 SNDRV_PCM_FORMAT_S16_BE,
293 SNDRV_PCM_FORMAT_U16_LE,
294 SNDRV_PCM_FORMAT_U16_BE,
Takashi Iwai64d27f92007-08-08 16:49:08 +0200295 SNDRV_PCM_FORMAT_S24_3LE,
296 SNDRV_PCM_FORMAT_S24_3BE,
297 SNDRV_PCM_FORMAT_U24_3LE,
298 SNDRV_PCM_FORMAT_U24_3BE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 SNDRV_PCM_FORMAT_S24_LE,
300 SNDRV_PCM_FORMAT_S24_BE,
301 SNDRV_PCM_FORMAT_U24_LE,
302 SNDRV_PCM_FORMAT_U24_BE,
303 SNDRV_PCM_FORMAT_S32_LE,
304 SNDRV_PCM_FORMAT_S32_BE,
305 SNDRV_PCM_FORMAT_U32_LE,
306 SNDRV_PCM_FORMAT_U32_BE,
307 SNDRV_PCM_FORMAT_S8,
308 SNDRV_PCM_FORMAT_U8
309};
310
Clemens Ladischfea952e2011-02-14 11:00:47 +0100311snd_pcm_format_t snd_pcm_plug_slave_format(snd_pcm_format_t format,
312 struct snd_mask *format_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Takashi Iwai64d27f92007-08-08 16:49:08 +0200314 int i;
315
Clemens Ladischfea952e2011-02-14 11:00:47 +0100316 if (snd_mask_test(format_mask, (__force int)format))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return format;
Clemens Ladischfea952e2011-02-14 11:00:47 +0100318 if (!snd_pcm_plug_formats(format_mask, format))
319 return (__force snd_pcm_format_t)-EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (snd_pcm_format_linear(format)) {
Takashi Iwai64d27f92007-08-08 16:49:08 +0200321 unsigned int width = snd_pcm_format_width(format);
322 int unsignd = snd_pcm_format_unsigned(format) > 0;
323 int big = snd_pcm_format_big_endian(format) > 0;
324 unsigned int badness, best = -1;
Clemens Ladischfea952e2011-02-14 11:00:47 +0100325 snd_pcm_format_t best_format = (__force snd_pcm_format_t)-1;
Takashi Iwai64d27f92007-08-08 16:49:08 +0200326 for (i = 0; i < ARRAY_SIZE(preferred_formats); i++) {
Clemens Ladischfea952e2011-02-14 11:00:47 +0100327 snd_pcm_format_t f = preferred_formats[i];
Takashi Iwai64d27f92007-08-08 16:49:08 +0200328 unsigned int w;
Clemens Ladischfea952e2011-02-14 11:00:47 +0100329 if (!snd_mask_test(format_mask, (__force int)f))
Takashi Iwai64d27f92007-08-08 16:49:08 +0200330 continue;
331 w = snd_pcm_format_width(f);
332 if (w >= width)
333 badness = w - width;
334 else
335 badness = width - w + 32;
336 badness += snd_pcm_format_unsigned(f) != unsignd;
337 badness += snd_pcm_format_big_endian(f) != big;
338 if (badness < best) {
339 best_format = f;
340 best = badness;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Clemens Ladischfea952e2011-02-14 11:00:47 +0100343 if ((__force int)best_format >= 0)
344 return best_format;
345 else
346 return (__force snd_pcm_format_t)-EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 switch (format) {
349 case SNDRV_PCM_FORMAT_MU_LAW:
350 for (i = 0; i < ARRAY_SIZE(preferred_formats); ++i) {
Clemens Ladischfea952e2011-02-14 11:00:47 +0100351 snd_pcm_format_t format1 = preferred_formats[i];
352 if (snd_mask_test(format_mask, (__force int)format1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return format1;
354 }
355 default:
Clemens Ladischfea952e2011-02-14 11:00:47 +0100356 return (__force snd_pcm_format_t)-EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358 }
359}
360
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100361int snd_pcm_plug_format_plugins(struct snd_pcm_substream *plug,
362 struct snd_pcm_hw_params *params,
363 struct snd_pcm_hw_params *slave_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100365 struct snd_pcm_plugin_format tmpformat;
366 struct snd_pcm_plugin_format dstformat;
367 struct snd_pcm_plugin_format srcformat;
Clemens Ladischfea952e2011-02-14 11:00:47 +0100368 snd_pcm_access_t src_access, dst_access;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100369 struct snd_pcm_plugin *plugin = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 int err;
371 int stream = snd_pcm_plug_stream(plug);
372 int slave_interleaved = (params_channels(slave_params) == 1 ||
373 params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED);
374
375 switch (stream) {
376 case SNDRV_PCM_STREAM_PLAYBACK:
377 dstformat.format = params_format(slave_params);
378 dstformat.rate = params_rate(slave_params);
379 dstformat.channels = params_channels(slave_params);
380 srcformat.format = params_format(params);
381 srcformat.rate = params_rate(params);
382 srcformat.channels = params_channels(params);
383 src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
384 dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
385 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
386 break;
387 case SNDRV_PCM_STREAM_CAPTURE:
388 dstformat.format = params_format(params);
389 dstformat.rate = params_rate(params);
390 dstformat.channels = params_channels(params);
391 srcformat.format = params_format(slave_params);
392 srcformat.rate = params_rate(slave_params);
393 srcformat.channels = params_channels(slave_params);
394 src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
395 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
396 dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
397 break;
398 default:
399 snd_BUG();
400 return -EINVAL;
401 }
402 tmpformat = srcformat;
403
404 pdprintf("srcformat: format=%i, rate=%i, channels=%i\n",
405 srcformat.format,
406 srcformat.rate,
407 srcformat.channels);
408 pdprintf("dstformat: format=%i, rate=%i, channels=%i\n",
409 dstformat.format,
410 dstformat.rate,
411 dstformat.channels);
412
413 /* Format change (linearization) */
Takashi Iwai0534ab42006-01-13 12:09:12 +0100414 if (! rate_match(srcformat.rate, dstformat.rate) &&
415 ! snd_pcm_format_linear(srcformat.format)) {
416 if (srcformat.format != SNDRV_PCM_FORMAT_MU_LAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return -EINVAL;
Takashi Iwai0534ab42006-01-13 12:09:12 +0100418 tmpformat.format = SNDRV_PCM_FORMAT_S16;
419 err = snd_pcm_plugin_build_mulaw(plug,
420 &srcformat, &tmpformat,
421 &plugin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (err < 0)
423 return err;
424 err = snd_pcm_plugin_append(plugin);
425 if (err < 0) {
426 snd_pcm_plugin_free(plugin);
427 return err;
428 }
429 srcformat = tmpformat;
430 src_access = dst_access;
431 }
432
433 /* channels reduction */
434 if (srcformat.channels > dstformat.channels) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 tmpformat.channels = dstformat.channels;
Takashi Iwai0534ab42006-01-13 12:09:12 +0100436 err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
Takashi Iwai0534ab42006-01-13 12:09:12 +0100438 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 err = snd_pcm_plugin_append(plugin);
441 if (err < 0) {
442 snd_pcm_plugin_free(plugin);
443 return err;
444 }
445 srcformat = tmpformat;
446 src_access = dst_access;
447 }
448
449 /* rate resampling */
450 if (!rate_match(srcformat.rate, dstformat.rate)) {
Takashi Iwai0534ab42006-01-13 12:09:12 +0100451 if (srcformat.format != SNDRV_PCM_FORMAT_S16) {
452 /* convert to S16 for resampling */
453 tmpformat.format = SNDRV_PCM_FORMAT_S16;
454 err = snd_pcm_plugin_build_linear(plug,
455 &srcformat, &tmpformat,
456 &plugin);
457 if (err < 0)
458 return err;
459 err = snd_pcm_plugin_append(plugin);
460 if (err < 0) {
461 snd_pcm_plugin_free(plugin);
462 return err;
463 }
464 srcformat = tmpformat;
465 src_access = dst_access;
466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 tmpformat.rate = dstformat.rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 err = snd_pcm_plugin_build_rate(plug,
469 &srcformat, &tmpformat,
470 &plugin);
471 pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
Takashi Iwai0534ab42006-01-13 12:09:12 +0100472 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 err = snd_pcm_plugin_append(plugin);
475 if (err < 0) {
476 snd_pcm_plugin_free(plugin);
477 return err;
478 }
479 srcformat = tmpformat;
480 src_access = dst_access;
481 }
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /* format change */
484 if (srcformat.format != dstformat.format) {
485 tmpformat.format = dstformat.format;
Takashi Iwaic82590d2006-01-20 17:13:45 +0100486 if (srcformat.format == SNDRV_PCM_FORMAT_MU_LAW ||
487 tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 err = snd_pcm_plugin_build_mulaw(plug,
489 &srcformat, &tmpformat,
490 &plugin);
491 }
492 else if (snd_pcm_format_linear(srcformat.format) &&
493 snd_pcm_format_linear(tmpformat.format)) {
494 err = snd_pcm_plugin_build_linear(plug,
495 &srcformat, &tmpformat,
496 &plugin);
497 }
498 else
499 return -EINVAL;
500 pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
501 if (err < 0)
502 return err;
503 err = snd_pcm_plugin_append(plugin);
504 if (err < 0) {
505 snd_pcm_plugin_free(plugin);
506 return err;
507 }
508 srcformat = tmpformat;
509 src_access = dst_access;
510 }
511
Takashi Iwai0534ab42006-01-13 12:09:12 +0100512 /* channels extension */
513 if (srcformat.channels < dstformat.channels) {
514 tmpformat.channels = dstformat.channels;
515 err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
516 pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
517 if (err < 0)
518 return err;
519 err = snd_pcm_plugin_append(plugin);
520 if (err < 0) {
521 snd_pcm_plugin_free(plugin);
522 return err;
523 }
524 srcformat = tmpformat;
525 src_access = dst_access;
526 }
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /* de-interleave */
529 if (src_access != dst_access) {
530 err = snd_pcm_plugin_build_copy(plug,
531 &srcformat,
532 &tmpformat,
533 &plugin);
534 pdprintf("interleave change (copy: returns %i)\n", err);
535 if (err < 0)
536 return err;
537 err = snd_pcm_plugin_append(plugin);
538 if (err < 0) {
539 snd_pcm_plugin_free(plugin);
540 return err;
541 }
542 }
543
544 return 0;
545}
546
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100547snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(struct snd_pcm_substream *plug,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 char *buf,
549 snd_pcm_uframes_t count,
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100550 struct snd_pcm_plugin_channel **channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100552 struct snd_pcm_plugin *plugin;
553 struct snd_pcm_plugin_channel *v;
554 struct snd_pcm_plugin_format *format;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 int width, nchannels, channel;
556 int stream = snd_pcm_plug_stream(plug);
557
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200558 if (snd_BUG_ON(!buf))
559 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
561 plugin = snd_pcm_plug_first(plug);
562 format = &plugin->src_format;
563 } else {
564 plugin = snd_pcm_plug_last(plug);
565 format = &plugin->dst_format;
566 }
567 v = plugin->buf_channels;
568 *channels = v;
569 if ((width = snd_pcm_format_physical_width(format->format)) < 0)
570 return width;
571 nchannels = format->channels;
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200572 if (snd_BUG_ON(plugin->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
573 format->channels > 1))
574 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 for (channel = 0; channel < nchannels; channel++, v++) {
576 v->frames = count;
577 v->enabled = 1;
578 v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE);
579 v->area.addr = buf;
580 v->area.first = channel * width;
581 v->area.step = nchannels * width;
582 }
583 return count;
584}
585
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100586snd_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 Torvalds1da177e2005-04-16 15:20:36 -0700587{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100588 struct snd_pcm_plugin *plugin, *next;
589 struct snd_pcm_plugin_channel *dst_channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 int err;
591 snd_pcm_sframes_t frames = size;
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 plugin = snd_pcm_plug_first(plug);
Takashi Iwai8e814252018-01-04 16:39:27 +0100594 while (plugin) {
595 if (frames <= 0)
596 return frames;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if ((next = plugin->next) != NULL) {
598 snd_pcm_sframes_t frames1 = frames;
Takashi Iwai8e814252018-01-04 16:39:27 +0100599 if (plugin->dst_frames) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 frames1 = plugin->dst_frames(plugin, frames);
Takashi Iwai8e814252018-01-04 16:39:27 +0100601 if (frames1 <= 0)
602 return frames1;
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if ((err = next->client_channels(next, frames1, &dst_channels)) < 0) {
605 return err;
606 }
607 if (err != frames1) {
608 frames = err;
Takashi Iwai8e814252018-01-04 16:39:27 +0100609 if (plugin->src_frames) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 frames = plugin->src_frames(plugin, frames1);
Takashi Iwai8e814252018-01-04 16:39:27 +0100611 if (frames <= 0)
612 return frames;
613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 } else
616 dst_channels = NULL;
617 pdprintf("write plugin: %s, %li\n", plugin->name, frames);
618 if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
619 return frames;
620 src_channels = dst_channels;
621 plugin = next;
622 }
623 return snd_pcm_plug_client_size(plug, frames);
624}
625
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100626snd_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 Torvalds1da177e2005-04-16 15:20:36 -0700627{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100628 struct snd_pcm_plugin *plugin, *next;
629 struct snd_pcm_plugin_channel *src_channels, *dst_channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 snd_pcm_sframes_t frames = size;
631 int err;
632
633 frames = snd_pcm_plug_slave_size(plug, frames);
634 if (frames < 0)
635 return frames;
636
637 src_channels = NULL;
638 plugin = snd_pcm_plug_first(plug);
639 while (plugin && frames > 0) {
640 if ((next = plugin->next) != NULL) {
641 if ((err = plugin->client_channels(plugin, frames, &dst_channels)) < 0) {
642 return err;
643 }
644 frames = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 } else {
646 dst_channels = dst_channels_final;
647 }
648 pdprintf("read plugin: %s, %li\n", plugin->name, frames);
649 if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
650 return frames;
651 plugin = next;
652 src_channels = dst_channels;
653 }
654 return frames;
655}
656
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100657int snd_pcm_area_silence(const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
Clemens Ladischfea952e2011-02-14 11:00:47 +0100658 size_t samples, snd_pcm_format_t format)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 /* FIXME: sub byte resolution and odd dst_offset */
661 unsigned char *dst;
662 unsigned int dst_step;
663 int width;
664 const unsigned char *silence;
665 if (!dst_area->addr)
666 return 0;
667 dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
668 width = snd_pcm_format_physical_width(format);
669 if (width <= 0)
670 return -EINVAL;
671 if (dst_area->step == (unsigned int) width && width >= 8)
672 return snd_pcm_format_set_silence(format, dst, samples);
673 silence = snd_pcm_format_silence_64(format);
674 if (! silence)
675 return -EINVAL;
676 dst_step = dst_area->step / 8;
677 if (width == 4) {
678 /* Ima ADPCM */
679 int dstbit = dst_area->first % 8;
680 int dstbit_step = dst_area->step % 8;
681 while (samples-- > 0) {
682 if (dstbit)
683 *dst &= 0xf0;
684 else
685 *dst &= 0x0f;
686 dst += dst_step;
687 dstbit += dstbit_step;
688 if (dstbit == 8) {
689 dst++;
690 dstbit = 0;
691 }
692 }
693 } else {
694 width /= 8;
695 while (samples-- > 0) {
696 memcpy(dst, silence, width);
697 dst += dst_step;
698 }
699 }
700 return 0;
701}
702
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100703int snd_pcm_area_copy(const struct snd_pcm_channel_area *src_area, size_t src_offset,
704 const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
Clemens Ladischfea952e2011-02-14 11:00:47 +0100705 size_t samples, snd_pcm_format_t format)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 /* FIXME: sub byte resolution and odd dst_offset */
708 char *src, *dst;
709 int width;
710 int src_step, dst_step;
711 src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
712 if (!src_area->addr)
713 return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
714 dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
715 if (!dst_area->addr)
716 return 0;
717 width = snd_pcm_format_physical_width(format);
718 if (width <= 0)
719 return -EINVAL;
720 if (src_area->step == (unsigned int) width &&
721 dst_area->step == (unsigned int) width && width >= 8) {
722 size_t bytes = samples * width / 8;
723 memcpy(dst, src, bytes);
724 return 0;
725 }
726 src_step = src_area->step / 8;
727 dst_step = dst_area->step / 8;
728 if (width == 4) {
729 /* Ima ADPCM */
730 int srcbit = src_area->first % 8;
731 int srcbit_step = src_area->step % 8;
732 int dstbit = dst_area->first % 8;
733 int dstbit_step = dst_area->step % 8;
734 while (samples-- > 0) {
735 unsigned char srcval;
736 if (srcbit)
737 srcval = *src & 0x0f;
738 else
739 srcval = (*src & 0xf0) >> 4;
740 if (dstbit)
741 *dst = (*dst & 0xf0) | srcval;
742 else
743 *dst = (*dst & 0x0f) | (srcval << 4);
744 src += src_step;
745 srcbit += srcbit_step;
746 if (srcbit == 8) {
747 src++;
748 srcbit = 0;
749 }
750 dst += dst_step;
751 dstbit += dstbit_step;
752 if (dstbit == 8) {
753 dst++;
754 dstbit = 0;
755 }
756 }
757 } else {
758 width /= 8;
759 while (samples-- > 0) {
760 memcpy(dst, src, width);
761 src += src_step;
762 dst += dst_step;
763 }
764 }
765 return 0;
766}