linear.c 5.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  Linear conversion Plug-In
3
 *  Copyright (c) 1999 by Jaroslav Kysela <perex@perex.cz>,
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *			  Abramo Bagnara <abramo@alsa-project.org>
 *
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as
 *   published by the Free Software Foundation; either version 2 of
 *   the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Library General Public License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <linux/time.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include "pcm_plugin.h"

/*
 *  Basic linear conversion plugin
 */
 
32
struct linear_priv {
33 34 35 36 37 38 39
	int cvt_endian;		/* need endian conversion? */
	unsigned int src_ofs;	/* byte offset in source format */
	unsigned int dst_ofs;	/* byte soffset in destination format */
	unsigned int copy_ofs;	/* byte offset in temporary u32 data */
	unsigned int dst_bytes;		/* byte size of destination format */
	unsigned int copy_bytes;	/* bytes to copy per conversion */
	unsigned int flip; /* MSB flip for signeness, done after endian conv */
40
};
L
Linus Torvalds 已提交
41

42 43 44 45 46 47 48 49 50 51 52 53 54
static inline void do_convert(struct linear_priv *data,
			      unsigned char *dst, unsigned char *src)
{
	unsigned int tmp = 0;
	unsigned char *p = (unsigned char *)&tmp;

	memcpy(p + data->copy_ofs, src + data->src_ofs, data->copy_bytes);
	if (data->cvt_endian)
		tmp = swab32(tmp);
	tmp ^= data->flip;
	memcpy(dst, p + data->dst_ofs, data->dst_bytes);
}

55 56 57
static void convert(struct snd_pcm_plugin *plugin,
		    const struct snd_pcm_plugin_channel *src_channels,
		    struct snd_pcm_plugin_channel *dst_channels,
L
Linus Torvalds 已提交
58 59
		    snd_pcm_uframes_t frames)
{
60
	struct linear_priv *data = (struct linear_priv *)plugin->extra_data;
L
Linus Torvalds 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	int channel;
	int nchannels = plugin->src_format.channels;
	for (channel = 0; channel < nchannels; ++channel) {
		char *src;
		char *dst;
		int src_step, dst_step;
		snd_pcm_uframes_t frames1;
		if (!src_channels[channel].enabled) {
			if (dst_channels[channel].wanted)
				snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format);
			dst_channels[channel].enabled = 0;
			continue;
		}
		dst_channels[channel].enabled = 1;
		src = src_channels[channel].area.addr + src_channels[channel].area.first / 8;
		dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
		src_step = src_channels[channel].area.step / 8;
		dst_step = dst_channels[channel].area.step / 8;
		frames1 = frames;
		while (frames1-- > 0) {
81
			do_convert(data, dst, src);
L
Linus Torvalds 已提交
82 83 84 85 86 87
			src += src_step;
			dst += dst_step;
		}
	}
}

88 89 90
static snd_pcm_sframes_t linear_transfer(struct snd_pcm_plugin *plugin,
			       const struct snd_pcm_plugin_channel *src_channels,
			       struct snd_pcm_plugin_channel *dst_channels,
L
Linus Torvalds 已提交
91 92
			       snd_pcm_uframes_t frames)
{
93
	struct linear_priv *data;
L
Linus Torvalds 已提交
94

95 96
	if (snd_BUG_ON(!plugin || !src_channels || !dst_channels))
		return -ENXIO;
97
	data = (struct linear_priv *)plugin->extra_data;
L
Linus Torvalds 已提交
98 99 100 101 102 103
	if (frames == 0)
		return 0;
#ifdef CONFIG_SND_DEBUG
	{
		unsigned int channel;
		for (channel = 0; channel < plugin->src_format.channels; channel++) {
104 105 106 107 108 109
			if (snd_BUG_ON(src_channels[channel].area.first % 8 ||
				       src_channels[channel].area.step % 8))
				return -ENXIO;
			if (snd_BUG_ON(dst_channels[channel].area.first % 8 ||
				       dst_channels[channel].area.step % 8))
				return -ENXIO;
L
Linus Torvalds 已提交
110 111 112 113 114 115 116
		}
	}
#endif
	convert(plugin, src_channels, dst_channels, frames);
	return frames;
}

117
static void init_data(struct linear_priv *data, int src_format, int dst_format)
L
Linus Torvalds 已提交
118
{
119
	int src_le, dst_le, src_bytes, dst_bytes;
L
Linus Torvalds 已提交
120

121 122 123 124
	src_bytes = snd_pcm_format_width(src_format) / 8;
	dst_bytes = snd_pcm_format_width(dst_format) / 8;
	src_le = snd_pcm_format_little_endian(src_format) > 0;
	dst_le = snd_pcm_format_little_endian(dst_format) > 0;
L
Linus Torvalds 已提交
125

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	data->dst_bytes = dst_bytes;
	data->cvt_endian = src_le != dst_le;
	data->copy_bytes = src_bytes < dst_bytes ? src_bytes : dst_bytes;
	if (src_le) {
		data->copy_ofs = 4 - data->copy_bytes;
		data->src_ofs = src_bytes - data->copy_bytes;
	} else
		data->src_ofs = snd_pcm_format_physical_width(src_format) / 8 -
			src_bytes;
	if (dst_le)
		data->dst_ofs = 4 - data->dst_bytes;
	else
		data->dst_ofs = snd_pcm_format_physical_width(dst_format) / 8 -
			dst_bytes;
	if (snd_pcm_format_signed(src_format) !=
	    snd_pcm_format_signed(dst_format)) {
		if (dst_le)
			data->flip = cpu_to_le32(0x80000000);
		else
			data->flip = cpu_to_be32(0x80000000);
	}
L
Linus Torvalds 已提交
147 148
}

149 150 151 152
int snd_pcm_plugin_build_linear(struct snd_pcm_substream *plug,
				struct snd_pcm_plugin_format *src_format,
				struct snd_pcm_plugin_format *dst_format,
				struct snd_pcm_plugin **r_plugin)
L
Linus Torvalds 已提交
153 154
{
	int err;
155 156
	struct linear_priv *data;
	struct snd_pcm_plugin *plugin;
L
Linus Torvalds 已提交
157

158 159
	if (snd_BUG_ON(!r_plugin))
		return -ENXIO;
L
Linus Torvalds 已提交
160 161
	*r_plugin = NULL;

162 163 164 165 166 167 168
	if (snd_BUG_ON(src_format->rate != dst_format->rate))
		return -ENXIO;
	if (snd_BUG_ON(src_format->channels != dst_format->channels))
		return -ENXIO;
	if (snd_BUG_ON(!snd_pcm_format_linear(src_format->format) ||
		       !snd_pcm_format_linear(dst_format->format)))
		return -ENXIO;
L
Linus Torvalds 已提交
169 170 171

	err = snd_pcm_plugin_build(plug, "linear format conversion",
				   src_format, dst_format,
172
				   sizeof(struct linear_priv), &plugin);
L
Linus Torvalds 已提交
173 174
	if (err < 0)
		return err;
175
	data = (struct linear_priv *)plugin->extra_data;
176
	init_data(data, src_format->format, dst_format->format);
L
Linus Torvalds 已提交
177 178 179 180
	plugin->transfer = linear_transfer;
	*r_plugin = plugin;
	return 0;
}