Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Linear conversion Plug-In |
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 | * 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 | #include <sound/driver.h> |
| 24 | #include <linux/time.h> |
| 25 | #include <sound/core.h> |
| 26 | #include <sound/pcm.h> |
| 27 | #include "pcm_plugin.h" |
| 28 | |
| 29 | /* |
| 30 | * Basic linear conversion plugin |
| 31 | */ |
| 32 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 33 | struct linear_priv { |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame] | 34 | int cvt_endian; /* need endian conversion? */ |
| 35 | unsigned int src_ofs; /* byte offset in source format */ |
| 36 | unsigned int dst_ofs; /* byte soffset in destination format */ |
| 37 | unsigned int copy_ofs; /* byte offset in temporary u32 data */ |
| 38 | unsigned int dst_bytes; /* byte size of destination format */ |
| 39 | unsigned int copy_bytes; /* bytes to copy per conversion */ |
| 40 | unsigned int flip; /* MSB flip for signeness, done after endian conv */ |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 41 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame] | 43 | static inline void do_convert(struct linear_priv *data, |
| 44 | unsigned char *dst, unsigned char *src) |
| 45 | { |
| 46 | unsigned int tmp = 0; |
| 47 | unsigned char *p = (unsigned char *)&tmp; |
| 48 | |
| 49 | memcpy(p + data->copy_ofs, src + data->src_ofs, data->copy_bytes); |
| 50 | if (data->cvt_endian) |
| 51 | tmp = swab32(tmp); |
| 52 | tmp ^= data->flip; |
| 53 | memcpy(dst, p + data->dst_ofs, data->dst_bytes); |
| 54 | } |
| 55 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 56 | static void convert(struct snd_pcm_plugin *plugin, |
| 57 | const struct snd_pcm_plugin_channel *src_channels, |
| 58 | struct snd_pcm_plugin_channel *dst_channels, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | snd_pcm_uframes_t frames) |
| 60 | { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 61 | struct linear_priv *data = (struct linear_priv *)plugin->extra_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | int channel; |
| 63 | int nchannels = plugin->src_format.channels; |
| 64 | for (channel = 0; channel < nchannels; ++channel) { |
| 65 | char *src; |
| 66 | char *dst; |
| 67 | int src_step, dst_step; |
| 68 | snd_pcm_uframes_t frames1; |
| 69 | if (!src_channels[channel].enabled) { |
| 70 | if (dst_channels[channel].wanted) |
| 71 | snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format); |
| 72 | dst_channels[channel].enabled = 0; |
| 73 | continue; |
| 74 | } |
| 75 | dst_channels[channel].enabled = 1; |
| 76 | src = src_channels[channel].area.addr + src_channels[channel].area.first / 8; |
| 77 | dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8; |
| 78 | src_step = src_channels[channel].area.step / 8; |
| 79 | dst_step = dst_channels[channel].area.step / 8; |
| 80 | frames1 = frames; |
| 81 | while (frames1-- > 0) { |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame] | 82 | do_convert(data, dst, src); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | src += src_step; |
| 84 | dst += dst_step; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 89 | static snd_pcm_sframes_t linear_transfer(struct snd_pcm_plugin *plugin, |
| 90 | const struct snd_pcm_plugin_channel *src_channels, |
| 91 | struct snd_pcm_plugin_channel *dst_channels, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | snd_pcm_uframes_t frames) |
| 93 | { |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 94 | struct linear_priv *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO); |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 97 | data = (struct linear_priv *)plugin->extra_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | if (frames == 0) |
| 99 | return 0; |
| 100 | #ifdef CONFIG_SND_DEBUG |
| 101 | { |
| 102 | unsigned int channel; |
| 103 | for (channel = 0; channel < plugin->src_format.channels; channel++) { |
| 104 | snd_assert(src_channels[channel].area.first % 8 == 0 && |
| 105 | src_channels[channel].area.step % 8 == 0, |
| 106 | return -ENXIO); |
| 107 | snd_assert(dst_channels[channel].area.first % 8 == 0 && |
| 108 | dst_channels[channel].area.step % 8 == 0, |
| 109 | return -ENXIO); |
| 110 | } |
| 111 | } |
| 112 | #endif |
| 113 | convert(plugin, src_channels, dst_channels, frames); |
| 114 | return frames; |
| 115 | } |
| 116 | |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame] | 117 | static void init_data(struct linear_priv *data, int src_format, int dst_format) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame] | 119 | int src_le, dst_le, src_bytes, dst_bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame] | 121 | src_bytes = snd_pcm_format_width(src_format) / 8; |
| 122 | dst_bytes = snd_pcm_format_width(dst_format) / 8; |
| 123 | src_le = snd_pcm_format_little_endian(src_format) > 0; |
| 124 | dst_le = snd_pcm_format_little_endian(dst_format) > 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame] | 126 | data->dst_bytes = dst_bytes; |
| 127 | data->cvt_endian = src_le != dst_le; |
| 128 | data->copy_bytes = src_bytes < dst_bytes ? src_bytes : dst_bytes; |
| 129 | if (src_le) { |
| 130 | data->copy_ofs = 4 - data->copy_bytes; |
| 131 | data->src_ofs = src_bytes - data->copy_bytes; |
| 132 | } else |
| 133 | data->src_ofs = snd_pcm_format_physical_width(src_format) / 8 - |
| 134 | src_bytes; |
| 135 | if (dst_le) |
| 136 | data->dst_ofs = 4 - data->dst_bytes; |
| 137 | else |
| 138 | data->dst_ofs = snd_pcm_format_physical_width(dst_format) / 8 - |
| 139 | dst_bytes; |
| 140 | if (snd_pcm_format_signed(src_format) != |
| 141 | snd_pcm_format_signed(dst_format)) { |
| 142 | if (dst_le) |
| 143 | data->flip = cpu_to_le32(0x80000000); |
| 144 | else |
| 145 | data->flip = cpu_to_be32(0x80000000); |
| 146 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 149 | int snd_pcm_plugin_build_linear(struct snd_pcm_substream *plug, |
| 150 | struct snd_pcm_plugin_format *src_format, |
| 151 | struct snd_pcm_plugin_format *dst_format, |
| 152 | struct snd_pcm_plugin **r_plugin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
| 154 | int err; |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 155 | struct linear_priv *data; |
| 156 | struct snd_pcm_plugin *plugin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | snd_assert(r_plugin != NULL, return -ENXIO); |
| 159 | *r_plugin = NULL; |
| 160 | |
| 161 | snd_assert(src_format->rate == dst_format->rate, return -ENXIO); |
| 162 | snd_assert(src_format->channels == dst_format->channels, return -ENXIO); |
| 163 | snd_assert(snd_pcm_format_linear(src_format->format) && |
| 164 | snd_pcm_format_linear(dst_format->format), return -ENXIO); |
| 165 | |
| 166 | err = snd_pcm_plugin_build(plug, "linear format conversion", |
| 167 | src_format, dst_format, |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 168 | sizeof(struct linear_priv), &plugin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | if (err < 0) |
| 170 | return err; |
Takashi Iwai | 6ac77bc | 2005-11-17 14:01:49 +0100 | [diff] [blame] | 171 | data = (struct linear_priv *)plugin->extra_data; |
Takashi Iwai | 9390ec8 | 2007-08-08 15:50:58 +0200 | [diff] [blame] | 172 | init_data(data, src_format->format, dst_format->format); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | plugin->transfer = linear_transfer; |
| 174 | *r_plugin = plugin; |
| 175 | return 0; |
| 176 | } |