xref: /OK3568_Linux_fs/kernel/sound/core/oss/linear.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *  Linear conversion Plug-In
3*4882a593Smuzhiyun  *  Copyright (c) 1999 by Jaroslav Kysela <perex@perex.cz>,
4*4882a593Smuzhiyun  *			  Abramo Bagnara <abramo@alsa-project.org>
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *   This library is free software; you can redistribute it and/or modify
8*4882a593Smuzhiyun  *   it under the terms of the GNU Library General Public License as
9*4882a593Smuzhiyun  *   published by the Free Software Foundation; either version 2 of
10*4882a593Smuzhiyun  *   the License, or (at your option) any later version.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *   This program is distributed in the hope that it will be useful,
13*4882a593Smuzhiyun  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14*4882a593Smuzhiyun  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*4882a593Smuzhiyun  *   GNU Library General Public License for more details.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  *   You should have received a copy of the GNU Library General Public
18*4882a593Smuzhiyun  *   License along with this library; if not, write to the Free Software
19*4882a593Smuzhiyun  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include <linux/time.h>
24*4882a593Smuzhiyun #include <sound/core.h>
25*4882a593Smuzhiyun #include <sound/pcm.h>
26*4882a593Smuzhiyun #include "pcm_plugin.h"
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun  *  Basic linear conversion plugin
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun struct linear_priv {
33*4882a593Smuzhiyun 	int cvt_endian;		/* need endian conversion? */
34*4882a593Smuzhiyun 	unsigned int src_ofs;	/* byte offset in source format */
35*4882a593Smuzhiyun 	unsigned int dst_ofs;	/* byte soffset in destination format */
36*4882a593Smuzhiyun 	unsigned int copy_ofs;	/* byte offset in temporary u32 data */
37*4882a593Smuzhiyun 	unsigned int dst_bytes;		/* byte size of destination format */
38*4882a593Smuzhiyun 	unsigned int copy_bytes;	/* bytes to copy per conversion */
39*4882a593Smuzhiyun 	unsigned int flip; /* MSB flip for signeness, done after endian conv */
40*4882a593Smuzhiyun };
41*4882a593Smuzhiyun 
do_convert(struct linear_priv * data,unsigned char * dst,unsigned char * src)42*4882a593Smuzhiyun static inline void do_convert(struct linear_priv *data,
43*4882a593Smuzhiyun 			      unsigned char *dst, unsigned char *src)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	unsigned int tmp = 0;
46*4882a593Smuzhiyun 	unsigned char *p = (unsigned char *)&tmp;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	memcpy(p + data->copy_ofs, src + data->src_ofs, data->copy_bytes);
49*4882a593Smuzhiyun 	if (data->cvt_endian)
50*4882a593Smuzhiyun 		tmp = swab32(tmp);
51*4882a593Smuzhiyun 	tmp ^= data->flip;
52*4882a593Smuzhiyun 	memcpy(dst, p + data->dst_ofs, data->dst_bytes);
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun 
convert(struct snd_pcm_plugin * plugin,const struct snd_pcm_plugin_channel * src_channels,struct snd_pcm_plugin_channel * dst_channels,snd_pcm_uframes_t frames)55*4882a593Smuzhiyun static void convert(struct snd_pcm_plugin *plugin,
56*4882a593Smuzhiyun 		    const struct snd_pcm_plugin_channel *src_channels,
57*4882a593Smuzhiyun 		    struct snd_pcm_plugin_channel *dst_channels,
58*4882a593Smuzhiyun 		    snd_pcm_uframes_t frames)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	struct linear_priv *data = (struct linear_priv *)plugin->extra_data;
61*4882a593Smuzhiyun 	int channel;
62*4882a593Smuzhiyun 	int nchannels = plugin->src_format.channels;
63*4882a593Smuzhiyun 	for (channel = 0; channel < nchannels; ++channel) {
64*4882a593Smuzhiyun 		char *src;
65*4882a593Smuzhiyun 		char *dst;
66*4882a593Smuzhiyun 		int src_step, dst_step;
67*4882a593Smuzhiyun 		snd_pcm_uframes_t frames1;
68*4882a593Smuzhiyun 		if (!src_channels[channel].enabled) {
69*4882a593Smuzhiyun 			if (dst_channels[channel].wanted)
70*4882a593Smuzhiyun 				snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format);
71*4882a593Smuzhiyun 			dst_channels[channel].enabled = 0;
72*4882a593Smuzhiyun 			continue;
73*4882a593Smuzhiyun 		}
74*4882a593Smuzhiyun 		dst_channels[channel].enabled = 1;
75*4882a593Smuzhiyun 		src = src_channels[channel].area.addr + src_channels[channel].area.first / 8;
76*4882a593Smuzhiyun 		dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
77*4882a593Smuzhiyun 		src_step = src_channels[channel].area.step / 8;
78*4882a593Smuzhiyun 		dst_step = dst_channels[channel].area.step / 8;
79*4882a593Smuzhiyun 		frames1 = frames;
80*4882a593Smuzhiyun 		while (frames1-- > 0) {
81*4882a593Smuzhiyun 			do_convert(data, dst, src);
82*4882a593Smuzhiyun 			src += src_step;
83*4882a593Smuzhiyun 			dst += dst_step;
84*4882a593Smuzhiyun 		}
85*4882a593Smuzhiyun 	}
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
linear_transfer(struct snd_pcm_plugin * plugin,const struct snd_pcm_plugin_channel * src_channels,struct snd_pcm_plugin_channel * dst_channels,snd_pcm_uframes_t frames)88*4882a593Smuzhiyun static snd_pcm_sframes_t linear_transfer(struct snd_pcm_plugin *plugin,
89*4882a593Smuzhiyun 			       const struct snd_pcm_plugin_channel *src_channels,
90*4882a593Smuzhiyun 			       struct snd_pcm_plugin_channel *dst_channels,
91*4882a593Smuzhiyun 			       snd_pcm_uframes_t frames)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun 	if (snd_BUG_ON(!plugin || !src_channels || !dst_channels))
94*4882a593Smuzhiyun 		return -ENXIO;
95*4882a593Smuzhiyun 	if (frames == 0)
96*4882a593Smuzhiyun 		return 0;
97*4882a593Smuzhiyun #ifdef CONFIG_SND_DEBUG
98*4882a593Smuzhiyun 	{
99*4882a593Smuzhiyun 		unsigned int channel;
100*4882a593Smuzhiyun 		for (channel = 0; channel < plugin->src_format.channels; channel++) {
101*4882a593Smuzhiyun 			if (snd_BUG_ON(src_channels[channel].area.first % 8 ||
102*4882a593Smuzhiyun 				       src_channels[channel].area.step % 8))
103*4882a593Smuzhiyun 				return -ENXIO;
104*4882a593Smuzhiyun 			if (snd_BUG_ON(dst_channels[channel].area.first % 8 ||
105*4882a593Smuzhiyun 				       dst_channels[channel].area.step % 8))
106*4882a593Smuzhiyun 				return -ENXIO;
107*4882a593Smuzhiyun 		}
108*4882a593Smuzhiyun 	}
109*4882a593Smuzhiyun #endif
110*4882a593Smuzhiyun 	if (frames > dst_channels[0].frames)
111*4882a593Smuzhiyun 		frames = dst_channels[0].frames;
112*4882a593Smuzhiyun 	convert(plugin, src_channels, dst_channels, frames);
113*4882a593Smuzhiyun 	return frames;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun 
init_data(struct linear_priv * data,snd_pcm_format_t src_format,snd_pcm_format_t dst_format)116*4882a593Smuzhiyun static void init_data(struct linear_priv *data,
117*4882a593Smuzhiyun 		      snd_pcm_format_t src_format, snd_pcm_format_t dst_format)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun 	int src_le, dst_le, src_bytes, dst_bytes;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	src_bytes = snd_pcm_format_width(src_format) / 8;
122*4882a593Smuzhiyun 	dst_bytes = snd_pcm_format_width(dst_format) / 8;
123*4882a593Smuzhiyun 	src_le = snd_pcm_format_little_endian(src_format) > 0;
124*4882a593Smuzhiyun 	dst_le = snd_pcm_format_little_endian(dst_format) > 0;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	data->dst_bytes = dst_bytes;
127*4882a593Smuzhiyun 	data->cvt_endian = src_le != dst_le;
128*4882a593Smuzhiyun 	data->copy_bytes = src_bytes < dst_bytes ? src_bytes : dst_bytes;
129*4882a593Smuzhiyun 	if (src_le) {
130*4882a593Smuzhiyun 		data->copy_ofs = 4 - data->copy_bytes;
131*4882a593Smuzhiyun 		data->src_ofs = src_bytes - data->copy_bytes;
132*4882a593Smuzhiyun 	} else
133*4882a593Smuzhiyun 		data->src_ofs = snd_pcm_format_physical_width(src_format) / 8 -
134*4882a593Smuzhiyun 			src_bytes;
135*4882a593Smuzhiyun 	if (dst_le)
136*4882a593Smuzhiyun 		data->dst_ofs = 4 - data->dst_bytes;
137*4882a593Smuzhiyun 	else
138*4882a593Smuzhiyun 		data->dst_ofs = snd_pcm_format_physical_width(dst_format) / 8 -
139*4882a593Smuzhiyun 			dst_bytes;
140*4882a593Smuzhiyun 	if (snd_pcm_format_signed(src_format) !=
141*4882a593Smuzhiyun 	    snd_pcm_format_signed(dst_format)) {
142*4882a593Smuzhiyun 		if (dst_le)
143*4882a593Smuzhiyun 			data->flip = (__force u32)cpu_to_le32(0x80000000);
144*4882a593Smuzhiyun 		else
145*4882a593Smuzhiyun 			data->flip = (__force u32)cpu_to_be32(0x80000000);
146*4882a593Smuzhiyun 	}
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
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)149*4882a593Smuzhiyun int snd_pcm_plugin_build_linear(struct snd_pcm_substream *plug,
150*4882a593Smuzhiyun 				struct snd_pcm_plugin_format *src_format,
151*4882a593Smuzhiyun 				struct snd_pcm_plugin_format *dst_format,
152*4882a593Smuzhiyun 				struct snd_pcm_plugin **r_plugin)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	int err;
155*4882a593Smuzhiyun 	struct linear_priv *data;
156*4882a593Smuzhiyun 	struct snd_pcm_plugin *plugin;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	if (snd_BUG_ON(!r_plugin))
159*4882a593Smuzhiyun 		return -ENXIO;
160*4882a593Smuzhiyun 	*r_plugin = NULL;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	if (snd_BUG_ON(src_format->rate != dst_format->rate))
163*4882a593Smuzhiyun 		return -ENXIO;
164*4882a593Smuzhiyun 	if (snd_BUG_ON(src_format->channels != dst_format->channels))
165*4882a593Smuzhiyun 		return -ENXIO;
166*4882a593Smuzhiyun 	if (snd_BUG_ON(!snd_pcm_format_linear(src_format->format) ||
167*4882a593Smuzhiyun 		       !snd_pcm_format_linear(dst_format->format)))
168*4882a593Smuzhiyun 		return -ENXIO;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	err = snd_pcm_plugin_build(plug, "linear format conversion",
171*4882a593Smuzhiyun 				   src_format, dst_format,
172*4882a593Smuzhiyun 				   sizeof(struct linear_priv), &plugin);
173*4882a593Smuzhiyun 	if (err < 0)
174*4882a593Smuzhiyun 		return err;
175*4882a593Smuzhiyun 	data = (struct linear_priv *)plugin->extra_data;
176*4882a593Smuzhiyun 	init_data(data, src_format->format, dst_format->format);
177*4882a593Smuzhiyun 	plugin->transfer = linear_transfer;
178*4882a593Smuzhiyun 	*r_plugin = plugin;
179*4882a593Smuzhiyun 	return 0;
180*4882a593Smuzhiyun }
181