blob: da3dbd41669eb9556699cfe3e7d1c9cbb20896b3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linear conversion Plug-In
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 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/time.h>
24#include <sound/core.h>
25#include <sound/pcm.h>
26#include "pcm_plugin.h"
27
28/*
29 * Basic linear conversion plugin
30 */
31
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010032struct linear_priv {
Takashi Iwai9390ec82007-08-08 15:50:58 +020033 int cvt_endian; /* need endian conversion? */
34 unsigned int src_ofs; /* byte offset in source format */
35 unsigned int dst_ofs; /* byte soffset in destination format */
36 unsigned int copy_ofs; /* byte offset in temporary u32 data */
37 unsigned int dst_bytes; /* byte size of destination format */
38 unsigned int copy_bytes; /* bytes to copy per conversion */
39 unsigned int flip; /* MSB flip for signeness, done after endian conv */
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010040};
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Takashi Iwai9390ec82007-08-08 15:50:58 +020042static inline void do_convert(struct linear_priv *data,
43 unsigned char *dst, unsigned char *src)
44{
45 unsigned int tmp = 0;
46 unsigned char *p = (unsigned char *)&tmp;
47
48 memcpy(p + data->copy_ofs, src + data->src_ofs, data->copy_bytes);
49 if (data->cvt_endian)
50 tmp = swab32(tmp);
51 tmp ^= data->flip;
52 memcpy(dst, p + data->dst_ofs, data->dst_bytes);
53}
54
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010055static void convert(struct snd_pcm_plugin *plugin,
56 const struct snd_pcm_plugin_channel *src_channels,
57 struct snd_pcm_plugin_channel *dst_channels,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 snd_pcm_uframes_t frames)
59{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010060 struct linear_priv *data = (struct linear_priv *)plugin->extra_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 int channel;
62 int nchannels = plugin->src_format.channels;
63 for (channel = 0; channel < nchannels; ++channel) {
64 char *src;
65 char *dst;
66 int src_step, dst_step;
67 snd_pcm_uframes_t frames1;
68 if (!src_channels[channel].enabled) {
69 if (dst_channels[channel].wanted)
70 snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format);
71 dst_channels[channel].enabled = 0;
72 continue;
73 }
74 dst_channels[channel].enabled = 1;
75 src = src_channels[channel].area.addr + src_channels[channel].area.first / 8;
76 dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
77 src_step = src_channels[channel].area.step / 8;
78 dst_step = dst_channels[channel].area.step / 8;
79 frames1 = frames;
80 while (frames1-- > 0) {
Takashi Iwai9390ec82007-08-08 15:50:58 +020081 do_convert(data, dst, src);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 src += src_step;
83 dst += dst_step;
84 }
85 }
86}
87
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010088static snd_pcm_sframes_t linear_transfer(struct snd_pcm_plugin *plugin,
89 const struct snd_pcm_plugin_channel *src_channels,
90 struct snd_pcm_plugin_channel *dst_channels,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 snd_pcm_uframes_t frames)
92{
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010093 struct linear_priv *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO);
Takashi Iwai6ac77bc2005-11-17 14:01:49 +010096 data = (struct linear_priv *)plugin->extra_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (frames == 0)
98 return 0;
99#ifdef CONFIG_SND_DEBUG
100 {
101 unsigned int channel;
102 for (channel = 0; channel < plugin->src_format.channels; channel++) {
103 snd_assert(src_channels[channel].area.first % 8 == 0 &&
104 src_channels[channel].area.step % 8 == 0,
105 return -ENXIO);
106 snd_assert(dst_channels[channel].area.first % 8 == 0 &&
107 dst_channels[channel].area.step % 8 == 0,
108 return -ENXIO);
109 }
110 }
111#endif
112 convert(plugin, src_channels, dst_channels, frames);
113 return frames;
114}
115
Takashi Iwai9390ec82007-08-08 15:50:58 +0200116static void init_data(struct linear_priv *data, int src_format, int dst_format)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Takashi Iwai9390ec82007-08-08 15:50:58 +0200118 int src_le, dst_le, src_bytes, dst_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Takashi Iwai9390ec82007-08-08 15:50:58 +0200120 src_bytes = snd_pcm_format_width(src_format) / 8;
121 dst_bytes = snd_pcm_format_width(dst_format) / 8;
122 src_le = snd_pcm_format_little_endian(src_format) > 0;
123 dst_le = snd_pcm_format_little_endian(dst_format) > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Takashi Iwai9390ec82007-08-08 15:50:58 +0200125 data->dst_bytes = dst_bytes;
126 data->cvt_endian = src_le != dst_le;
127 data->copy_bytes = src_bytes < dst_bytes ? src_bytes : dst_bytes;
128 if (src_le) {
129 data->copy_ofs = 4 - data->copy_bytes;
130 data->src_ofs = src_bytes - data->copy_bytes;
131 } else
132 data->src_ofs = snd_pcm_format_physical_width(src_format) / 8 -
133 src_bytes;
134 if (dst_le)
135 data->dst_ofs = 4 - data->dst_bytes;
136 else
137 data->dst_ofs = snd_pcm_format_physical_width(dst_format) / 8 -
138 dst_bytes;
139 if (snd_pcm_format_signed(src_format) !=
140 snd_pcm_format_signed(dst_format)) {
141 if (dst_le)
142 data->flip = cpu_to_le32(0x80000000);
143 else
144 data->flip = cpu_to_be32(0x80000000);
145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100148int snd_pcm_plugin_build_linear(struct snd_pcm_substream *plug,
149 struct snd_pcm_plugin_format *src_format,
150 struct snd_pcm_plugin_format *dst_format,
151 struct snd_pcm_plugin **r_plugin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 int err;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100154 struct linear_priv *data;
155 struct snd_pcm_plugin *plugin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 snd_assert(r_plugin != NULL, return -ENXIO);
158 *r_plugin = NULL;
159
160 snd_assert(src_format->rate == dst_format->rate, return -ENXIO);
161 snd_assert(src_format->channels == dst_format->channels, return -ENXIO);
162 snd_assert(snd_pcm_format_linear(src_format->format) &&
163 snd_pcm_format_linear(dst_format->format), return -ENXIO);
164
165 err = snd_pcm_plugin_build(plug, "linear format conversion",
166 src_format, dst_format,
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100167 sizeof(struct linear_priv), &plugin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (err < 0)
169 return err;
Takashi Iwai6ac77bc2005-11-17 14:01:49 +0100170 data = (struct linear_priv *)plugin->extra_data;
Takashi Iwai9390ec82007-08-08 15:50:58 +0200171 init_data(data, src_format->format, dst_format->format);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 plugin->transfer = linear_transfer;
173 *r_plugin = plugin;
174 return 0;
175}