xref: /linux/sound/core/oss/linear.c (revision 4bedea94545165364618d403d03b61d797acba0b)
1 /*
2  *  Linear conversion Plug-In
3  *  Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>,
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 
33 typedef struct linear_private_data {
34 	int conv;
35 } linear_t;
36 
37 static void convert(snd_pcm_plugin_t *plugin,
38 		    const snd_pcm_plugin_channel_t *src_channels,
39 		    snd_pcm_plugin_channel_t *dst_channels,
40 		    snd_pcm_uframes_t frames)
41 {
42 #define CONV_LABELS
43 #include "plugin_ops.h"
44 #undef CONV_LABELS
45 	linear_t *data = (linear_t *)plugin->extra_data;
46 	void *conv = conv_labels[data->conv];
47 	int channel;
48 	int nchannels = plugin->src_format.channels;
49 	for (channel = 0; channel < nchannels; ++channel) {
50 		char *src;
51 		char *dst;
52 		int src_step, dst_step;
53 		snd_pcm_uframes_t frames1;
54 		if (!src_channels[channel].enabled) {
55 			if (dst_channels[channel].wanted)
56 				snd_pcm_area_silence(&dst_channels[channel].area, 0, frames, plugin->dst_format.format);
57 			dst_channels[channel].enabled = 0;
58 			continue;
59 		}
60 		dst_channels[channel].enabled = 1;
61 		src = src_channels[channel].area.addr + src_channels[channel].area.first / 8;
62 		dst = dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
63 		src_step = src_channels[channel].area.step / 8;
64 		dst_step = dst_channels[channel].area.step / 8;
65 		frames1 = frames;
66 		while (frames1-- > 0) {
67 			goto *conv;
68 #define CONV_END after
69 #include "plugin_ops.h"
70 #undef CONV_END
71 		after:
72 			src += src_step;
73 			dst += dst_step;
74 		}
75 	}
76 }
77 
78 static snd_pcm_sframes_t linear_transfer(snd_pcm_plugin_t *plugin,
79 			       const snd_pcm_plugin_channel_t *src_channels,
80 			       snd_pcm_plugin_channel_t *dst_channels,
81 			       snd_pcm_uframes_t frames)
82 {
83 	linear_t *data;
84 
85 	snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO);
86 	data = (linear_t *)plugin->extra_data;
87 	if (frames == 0)
88 		return 0;
89 #ifdef CONFIG_SND_DEBUG
90 	{
91 		unsigned int channel;
92 		for (channel = 0; channel < plugin->src_format.channels; channel++) {
93 			snd_assert(src_channels[channel].area.first % 8 == 0 &&
94 				   src_channels[channel].area.step % 8 == 0,
95 				   return -ENXIO);
96 			snd_assert(dst_channels[channel].area.first % 8 == 0 &&
97 				   dst_channels[channel].area.step % 8 == 0,
98 				   return -ENXIO);
99 		}
100 	}
101 #endif
102 	convert(plugin, src_channels, dst_channels, frames);
103 	return frames;
104 }
105 
106 int conv_index(int src_format, int dst_format)
107 {
108 	int src_endian, dst_endian, sign, src_width, dst_width;
109 
110 	sign = (snd_pcm_format_signed(src_format) !=
111 		snd_pcm_format_signed(dst_format));
112 #ifdef SNDRV_LITTLE_ENDIAN
113 	src_endian = snd_pcm_format_big_endian(src_format);
114 	dst_endian = snd_pcm_format_big_endian(dst_format);
115 #else
116 	src_endian = snd_pcm_format_little_endian(src_format);
117 	dst_endian = snd_pcm_format_little_endian(dst_format);
118 #endif
119 
120 	if (src_endian < 0)
121 		src_endian = 0;
122 	if (dst_endian < 0)
123 		dst_endian = 0;
124 
125 	src_width = snd_pcm_format_width(src_format) / 8 - 1;
126 	dst_width = snd_pcm_format_width(dst_format) / 8 - 1;
127 
128 	return src_width * 32 + src_endian * 16 + sign * 8 + dst_width * 2 + dst_endian;
129 }
130 
131 int snd_pcm_plugin_build_linear(snd_pcm_plug_t *plug,
132 				snd_pcm_plugin_format_t *src_format,
133 				snd_pcm_plugin_format_t *dst_format,
134 				snd_pcm_plugin_t **r_plugin)
135 {
136 	int err;
137 	struct linear_private_data *data;
138 	snd_pcm_plugin_t *plugin;
139 
140 	snd_assert(r_plugin != NULL, return -ENXIO);
141 	*r_plugin = NULL;
142 
143 	snd_assert(src_format->rate == dst_format->rate, return -ENXIO);
144 	snd_assert(src_format->channels == dst_format->channels, return -ENXIO);
145 	snd_assert(snd_pcm_format_linear(src_format->format) &&
146 		   snd_pcm_format_linear(dst_format->format), return -ENXIO);
147 
148 	err = snd_pcm_plugin_build(plug, "linear format conversion",
149 				   src_format, dst_format,
150 				   sizeof(linear_t), &plugin);
151 	if (err < 0)
152 		return err;
153 	data = (linear_t *)plugin->extra_data;
154 	data->conv = conv_index(src_format->format, dst_format->format);
155 	plugin->transfer = linear_transfer;
156 	*r_plugin = plugin;
157 	return 0;
158 }
159