rate.c 9.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 *  Rate conversion Plug-In
 *  Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
 *
 *
 *   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 <sound/driver.h>
23 24 25

#ifdef CONFIG_SND_PCM_OSS_PLUGINS

L
Linus Torvalds 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38
#include <linux/time.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include "pcm_plugin.h"

#define SHIFT	11
#define BITS	(1<<SHIFT)
#define R_MASK	(BITS-1)

/*
 *  Basic rate conversion plugin
 */

39
struct rate_channel {
L
Linus Torvalds 已提交
40 41
	signed short last_S1;
	signed short last_S2;
42
};
L
Linus Torvalds 已提交
43
 
44 45 46
typedef void (*rate_f)(struct snd_pcm_plugin *plugin,
		       const struct snd_pcm_plugin_channel *src_channels,
		       struct snd_pcm_plugin_channel *dst_channels,
L
Linus Torvalds 已提交
47 48
		       int src_frames, int dst_frames);

49
struct rate_priv {
L
Linus Torvalds 已提交
50 51 52 53
	unsigned int pitch;
	unsigned int pos;
	rate_f func;
	snd_pcm_sframes_t old_src_frames, old_dst_frames;
54 55
	struct rate_channel channels[0];
};
L
Linus Torvalds 已提交
56

57
static void rate_init(struct snd_pcm_plugin *plugin)
L
Linus Torvalds 已提交
58 59
{
	unsigned int channel;
60
	struct rate_priv *data = (struct rate_priv *)plugin->extra_data;
L
Linus Torvalds 已提交
61 62 63 64 65 66 67
	data->pos = 0;
	for (channel = 0; channel < plugin->src_format.channels; channel++) {
		data->channels[channel].last_S1 = 0;
		data->channels[channel].last_S2 = 0;
	}
}

68 69 70
static void resample_expand(struct snd_pcm_plugin *plugin,
			    const struct snd_pcm_plugin_channel *src_channels,
			    struct snd_pcm_plugin_channel *dst_channels,
L
Linus Torvalds 已提交
71 72 73 74 75
			    int src_frames, int dst_frames)
{
	unsigned int pos = 0;
	signed int val;
	signed short S1, S2;
T
Takashi Iwai 已提交
76
	signed short *src, *dst;
L
Linus Torvalds 已提交
77 78 79
	unsigned int channel;
	int src_step, dst_step;
	int src_frames1, dst_frames1;
80 81
	struct rate_priv *data = (struct rate_priv *)plugin->extra_data;
	struct rate_channel *rchannels = data->channels;
L
Linus Torvalds 已提交
82 83 84 85 86 87 88 89 90 91 92 93
	
	for (channel = 0; channel < plugin->src_format.channels; channel++) {
		pos = data->pos;
		S1 = rchannels->last_S1;
		S2 = rchannels->last_S2;
		if (!src_channels[channel].enabled) {
			if (dst_channels[channel].wanted)
				snd_pcm_area_silence(&dst_channels[channel].area, 0, dst_frames, plugin->dst_format.format);
			dst_channels[channel].enabled = 0;
			continue;
		}
		dst_channels[channel].enabled = 1;
T
Takashi Iwai 已提交
94 95 96 97 98 99
		src = (signed short *)src_channels[channel].area.addr +
			src_channels[channel].area.first / 8 / 2;
		dst = (signed short *)dst_channels[channel].area.addr +
			dst_channels[channel].area.first / 8 / 2;
		src_step = src_channels[channel].area.step / 8 / 2;
		dst_step = dst_channels[channel].area.step / 8 / 2;
L
Linus Torvalds 已提交
100 101 102 103 104 105 106
		src_frames1 = src_frames;
		dst_frames1 = dst_frames;
		while (dst_frames1-- > 0) {
			if (pos & ~R_MASK) {
				pos &= R_MASK;
				S1 = S2;
				if (src_frames1-- > 0) {
T
Takashi Iwai 已提交
107
					S2 = *src;
L
Linus Torvalds 已提交
108 109 110 111 112 113 114 115
					src += src_step;
				}
			}
			val = S1 + ((S2 - S1) * (signed int)pos) / BITS;
			if (val < -32768)
				val = -32768;
			else if (val > 32767)
				val = 32767;
T
Takashi Iwai 已提交
116
			*dst = val;
L
Linus Torvalds 已提交
117 118 119 120 121 122 123 124 125 126
			dst += dst_step;
			pos += data->pitch;
		}
		rchannels->last_S1 = S1;
		rchannels->last_S2 = S2;
		rchannels++;
	}
	data->pos = pos;
}

127 128 129
static void resample_shrink(struct snd_pcm_plugin *plugin,
			    const struct snd_pcm_plugin_channel *src_channels,
			    struct snd_pcm_plugin_channel *dst_channels,
L
Linus Torvalds 已提交
130 131 132 133 134
			    int src_frames, int dst_frames)
{
	unsigned int pos = 0;
	signed int val;
	signed short S1, S2;
T
Takashi Iwai 已提交
135
	signed short *src, *dst;
L
Linus Torvalds 已提交
136 137 138
	unsigned int channel;
	int src_step, dst_step;
	int src_frames1, dst_frames1;
139 140
	struct rate_priv *data = (struct rate_priv *)plugin->extra_data;
	struct rate_channel *rchannels = data->channels;
L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148 149 150 151 152

	for (channel = 0; channel < plugin->src_format.channels; ++channel) {
		pos = data->pos;
		S1 = rchannels->last_S1;
		S2 = rchannels->last_S2;
		if (!src_channels[channel].enabled) {
			if (dst_channels[channel].wanted)
				snd_pcm_area_silence(&dst_channels[channel].area, 0, dst_frames, plugin->dst_format.format);
			dst_channels[channel].enabled = 0;
			continue;
		}
		dst_channels[channel].enabled = 1;
T
Takashi Iwai 已提交
153 154 155 156 157 158
		src = (signed short *)src_channels[channel].area.addr +
			src_channels[channel].area.first / 8 / 2;
		dst = (signed short *)dst_channels[channel].area.addr +
			dst_channels[channel].area.first / 8 / 2;
		src_step = src_channels[channel].area.step / 8 / 2;
		dst_step = dst_channels[channel].area.step / 8 / 2;
L
Linus Torvalds 已提交
159 160 161 162 163
		src_frames1 = src_frames;
		dst_frames1 = dst_frames;
		while (dst_frames1 > 0) {
			S1 = S2;
			if (src_frames1-- > 0) {
T
Takashi Iwai 已提交
164
				S1 = *src;
L
Linus Torvalds 已提交
165 166 167 168 169 170 171 172 173
				src += src_step;
			}
			if (pos & ~R_MASK) {
				pos &= R_MASK;
				val = S1 + ((S2 - S1) * (signed int)pos) / BITS;
				if (val < -32768)
					val = -32768;
				else if (val > 32767)
					val = 32767;
T
Takashi Iwai 已提交
174
				*dst = val;
L
Linus Torvalds 已提交
175 176 177 178 179 180 181 182 183 184 185 186
				dst += dst_step;
				dst_frames1--;
			}
			pos += data->pitch;
		}
		rchannels->last_S1 = S1;
		rchannels->last_S2 = S2;
		rchannels++;
	}
	data->pos = pos;
}

187
static snd_pcm_sframes_t rate_src_frames(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames)
L
Linus Torvalds 已提交
188
{
189
	struct rate_priv *data;
L
Linus Torvalds 已提交
190 191 192 193 194
	snd_pcm_sframes_t res;

	snd_assert(plugin != NULL, return -ENXIO);
	if (frames == 0)
		return 0;
195
	data = (struct rate_priv *)plugin->extra_data;
L
Linus Torvalds 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	if (plugin->src_format.rate < plugin->dst_format.rate) {
		res = (((frames * data->pitch) + (BITS/2)) >> SHIFT);
	} else {
		res = (((frames << SHIFT) + (data->pitch / 2)) / data->pitch);		
	}
	if (data->old_src_frames > 0) {
		snd_pcm_sframes_t frames1 = frames, res1 = data->old_dst_frames;
		while (data->old_src_frames < frames1) {
			frames1 >>= 1;
			res1 <<= 1;
		}
		while (data->old_src_frames > frames1) {
			frames1 <<= 1;
			res1 >>= 1;
		}
		if (data->old_src_frames == frames1)
			return res1;
	}
	data->old_src_frames = frames;
	data->old_dst_frames = res;
	return res;
}

219
static snd_pcm_sframes_t rate_dst_frames(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames)
L
Linus Torvalds 已提交
220
{
221
	struct rate_priv *data;
L
Linus Torvalds 已提交
222 223 224 225 226
	snd_pcm_sframes_t res;

	snd_assert(plugin != NULL, return -ENXIO);
	if (frames == 0)
		return 0;
227
	data = (struct rate_priv *)plugin->extra_data;
L
Linus Torvalds 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	if (plugin->src_format.rate < plugin->dst_format.rate) {
		res = (((frames << SHIFT) + (data->pitch / 2)) / data->pitch);
	} else {
		res = (((frames * data->pitch) + (BITS/2)) >> SHIFT);
	}
	if (data->old_dst_frames > 0) {
		snd_pcm_sframes_t frames1 = frames, res1 = data->old_src_frames;
		while (data->old_dst_frames < frames1) {
			frames1 >>= 1;
			res1 <<= 1;
		}
		while (data->old_dst_frames > frames1) {
			frames1 <<= 1;
			res1 >>= 1;
		}
		if (data->old_dst_frames == frames1)
			return res1;
	}
	data->old_dst_frames = frames;
	data->old_src_frames = res;
	return res;
}

251 252 253
static snd_pcm_sframes_t rate_transfer(struct snd_pcm_plugin *plugin,
			     const struct snd_pcm_plugin_channel *src_channels,
			     struct snd_pcm_plugin_channel *dst_channels,
L
Linus Torvalds 已提交
254 255 256
			     snd_pcm_uframes_t frames)
{
	snd_pcm_uframes_t dst_frames;
257
	struct rate_priv *data;
L
Linus Torvalds 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

	snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO);
	if (frames == 0)
		return 0;
#ifdef CONFIG_SND_DEBUG
	{
		unsigned int channel;
		for (channel = 0; channel < plugin->src_format.channels; channel++) {
			snd_assert(src_channels[channel].area.first % 8 == 0 &&
				   src_channels[channel].area.step % 8 == 0,
				   return -ENXIO);
			snd_assert(dst_channels[channel].area.first % 8 == 0 &&
				   dst_channels[channel].area.step % 8 == 0,
				   return -ENXIO);
		}
	}
#endif

	dst_frames = rate_dst_frames(plugin, frames);
	if (dst_frames > dst_channels[0].frames)
		dst_frames = dst_channels[0].frames;
279
	data = (struct rate_priv *)plugin->extra_data;
L
Linus Torvalds 已提交
280 281 282 283
	data->func(plugin, src_channels, dst_channels, frames, dst_frames);
	return dst_frames;
}

284 285
static int rate_action(struct snd_pcm_plugin *plugin,
		       enum snd_pcm_plugin_action action,
286
		       unsigned long udata)
L
Linus Torvalds 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299
{
	snd_assert(plugin != NULL, return -ENXIO);
	switch (action) {
	case INIT:
	case PREPARE:
		rate_init(plugin);
		break;
	default:
		break;
	}
	return 0;	/* silenty ignore other actions */
}

300 301 302 303
int snd_pcm_plugin_build_rate(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 已提交
304 305
{
	int err;
306 307
	struct rate_priv *data;
	struct snd_pcm_plugin *plugin;
L
Linus Torvalds 已提交
308 309 310 311 312 313

	snd_assert(r_plugin != NULL, return -ENXIO);
	*r_plugin = NULL;

	snd_assert(src_format->channels == dst_format->channels, return -ENXIO);
	snd_assert(src_format->channels > 0, return -ENXIO);
T
Takashi Iwai 已提交
314 315
	snd_assert(src_format->format == SNDRV_PCM_FORMAT_S16, return -ENXIO);
	snd_assert(dst_format->format == SNDRV_PCM_FORMAT_S16, return -ENXIO);
L
Linus Torvalds 已提交
316 317 318 319
	snd_assert(src_format->rate != dst_format->rate, return -ENXIO);

	err = snd_pcm_plugin_build(plug, "rate conversion",
				   src_format, dst_format,
320 321
				   sizeof(struct rate_priv) +
				   src_format->channels * sizeof(struct rate_channel),
L
Linus Torvalds 已提交
322 323 324
				   &plugin);
	if (err < 0)
		return err;
325
	data = (struct rate_priv *)plugin->extra_data;
L
Linus Torvalds 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	if (src_format->rate < dst_format->rate) {
		data->pitch = ((src_format->rate << SHIFT) + (dst_format->rate >> 1)) / dst_format->rate;
		data->func = resample_expand;
	} else {
		data->pitch = ((dst_format->rate << SHIFT) + (src_format->rate >> 1)) / src_format->rate;
		data->func = resample_shrink;
	}
	data->pos = 0;
	rate_init(plugin);
	data->old_src_frames = data->old_dst_frames = 0;
	plugin->transfer = rate_transfer;
	plugin->src_frames = rate_src_frames;
	plugin->dst_frames = rate_dst_frames;
	plugin->action = rate_action;
	*r_plugin = plugin;
	return 0;
}
343 344

#endif