2 * Rate conversion Plug-In
3 * Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Library General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sound/driver.h>
23 #include <linux/time.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include "pcm_plugin.h"
29 #define BITS (1<<SHIFT)
30 #define R_MASK (BITS-1)
33 * Basic rate conversion plugin
41 typedef void (*rate_f)(struct snd_pcm_plugin *plugin,
42 const struct snd_pcm_plugin_channel *src_channels,
43 struct snd_pcm_plugin_channel *dst_channels,
44 int src_frames, int dst_frames);
51 snd_pcm_sframes_t old_src_frames, old_dst_frames;
52 struct rate_channel channels[0];
55 static void rate_init(struct snd_pcm_plugin *plugin)
58 struct rate_priv *data = (struct rate_priv *)plugin->extra_data;
60 for (channel = 0; channel < plugin->src_format.channels; channel++) {
61 data->channels[channel].last_S1 = 0;
62 data->channels[channel].last_S2 = 0;
66 static void resample_expand(struct snd_pcm_plugin *plugin,
67 const struct snd_pcm_plugin_channel *src_channels,
68 struct snd_pcm_plugin_channel *dst_channels,
69 int src_frames, int dst_frames)
76 int src_step, dst_step;
77 int src_frames1, dst_frames1;
78 struct rate_priv *data = (struct rate_priv *)plugin->extra_data;
79 struct rate_channel *rchannels = data->channels;
81 #define GET_S16_LABELS
82 #define PUT_S16_LABELS
83 #include "plugin_ops.h"
86 void *get = get_s16_labels[data->get];
87 void *put = put_s16_labels[data->put];
88 signed short sample = 0;
90 for (channel = 0; channel < plugin->src_format.channels; channel++) {
92 S1 = rchannels->last_S1;
93 S2 = rchannels->last_S2;
94 if (!src_channels[channel].enabled) {
95 if (dst_channels[channel].wanted)
96 snd_pcm_area_silence(&dst_channels[channel].area, 0, dst_frames, plugin->dst_format.format);
97 dst_channels[channel].enabled = 0;
100 dst_channels[channel].enabled = 1;
101 src = (char *)src_channels[channel].area.addr + src_channels[channel].area.first / 8;
102 dst = (char *)dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
103 src_step = src_channels[channel].area.step / 8;
104 dst_step = dst_channels[channel].area.step / 8;
105 src_frames1 = src_frames;
106 dst_frames1 = dst_frames;
107 while (dst_frames1-- > 0) {
111 if (src_frames1-- > 0) {
113 #define GET_S16_END after_get
114 #include "plugin_ops.h"
121 val = S1 + ((S2 - S1) * (signed int)pos) / BITS;
124 else if (val > 32767)
128 #define PUT_S16_END after_put
129 #include "plugin_ops.h"
135 rchannels->last_S1 = S1;
136 rchannels->last_S2 = S2;
142 static void resample_shrink(struct snd_pcm_plugin *plugin,
143 const struct snd_pcm_plugin_channel *src_channels,
144 struct snd_pcm_plugin_channel *dst_channels,
145 int src_frames, int dst_frames)
147 unsigned int pos = 0;
151 unsigned int channel;
152 int src_step, dst_step;
153 int src_frames1, dst_frames1;
154 struct rate_priv *data = (struct rate_priv *)plugin->extra_data;
155 struct rate_channel *rchannels = data->channels;
157 #define GET_S16_LABELS
158 #define PUT_S16_LABELS
159 #include "plugin_ops.h"
160 #undef GET_S16_LABELS
161 #undef PUT_S16_LABELS
162 void *get = get_s16_labels[data->get];
163 void *put = put_s16_labels[data->put];
164 signed short sample = 0;
166 for (channel = 0; channel < plugin->src_format.channels; ++channel) {
168 S1 = rchannels->last_S1;
169 S2 = rchannels->last_S2;
170 if (!src_channels[channel].enabled) {
171 if (dst_channels[channel].wanted)
172 snd_pcm_area_silence(&dst_channels[channel].area, 0, dst_frames, plugin->dst_format.format);
173 dst_channels[channel].enabled = 0;
176 dst_channels[channel].enabled = 1;
177 src = (char *)src_channels[channel].area.addr + src_channels[channel].area.first / 8;
178 dst = (char *)dst_channels[channel].area.addr + dst_channels[channel].area.first / 8;
179 src_step = src_channels[channel].area.step / 8;
180 dst_step = dst_channels[channel].area.step / 8;
181 src_frames1 = src_frames;
182 dst_frames1 = dst_frames;
183 while (dst_frames1 > 0) {
185 if (src_frames1-- > 0) {
187 #define GET_S16_END after_get
188 #include "plugin_ops.h"
196 val = S1 + ((S2 - S1) * (signed int)pos) / BITS;
199 else if (val > 32767)
203 #define PUT_S16_END after_put
204 #include "plugin_ops.h"
212 rchannels->last_S1 = S1;
213 rchannels->last_S2 = S2;
219 static snd_pcm_sframes_t rate_src_frames(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames)
221 struct rate_priv *data;
222 snd_pcm_sframes_t res;
224 snd_assert(plugin != NULL, return -ENXIO);
227 data = (struct rate_priv *)plugin->extra_data;
228 if (plugin->src_format.rate < plugin->dst_format.rate) {
229 res = (((frames * data->pitch) + (BITS/2)) >> SHIFT);
231 res = (((frames << SHIFT) + (data->pitch / 2)) / data->pitch);
233 if (data->old_src_frames > 0) {
234 snd_pcm_sframes_t frames1 = frames, res1 = data->old_dst_frames;
235 while (data->old_src_frames < frames1) {
239 while (data->old_src_frames > frames1) {
243 if (data->old_src_frames == frames1)
246 data->old_src_frames = frames;
247 data->old_dst_frames = res;
251 static snd_pcm_sframes_t rate_dst_frames(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames)
253 struct rate_priv *data;
254 snd_pcm_sframes_t res;
256 snd_assert(plugin != NULL, return -ENXIO);
259 data = (struct rate_priv *)plugin->extra_data;
260 if (plugin->src_format.rate < plugin->dst_format.rate) {
261 res = (((frames << SHIFT) + (data->pitch / 2)) / data->pitch);
263 res = (((frames * data->pitch) + (BITS/2)) >> SHIFT);
265 if (data->old_dst_frames > 0) {
266 snd_pcm_sframes_t frames1 = frames, res1 = data->old_src_frames;
267 while (data->old_dst_frames < frames1) {
271 while (data->old_dst_frames > frames1) {
275 if (data->old_dst_frames == frames1)
278 data->old_dst_frames = frames;
279 data->old_src_frames = res;
283 static snd_pcm_sframes_t rate_transfer(struct snd_pcm_plugin *plugin,
284 const struct snd_pcm_plugin_channel *src_channels,
285 struct snd_pcm_plugin_channel *dst_channels,
286 snd_pcm_uframes_t frames)
288 snd_pcm_uframes_t dst_frames;
289 struct rate_priv *data;
291 snd_assert(plugin != NULL && src_channels != NULL && dst_channels != NULL, return -ENXIO);
294 #ifdef CONFIG_SND_DEBUG
296 unsigned int channel;
297 for (channel = 0; channel < plugin->src_format.channels; channel++) {
298 snd_assert(src_channels[channel].area.first % 8 == 0 &&
299 src_channels[channel].area.step % 8 == 0,
301 snd_assert(dst_channels[channel].area.first % 8 == 0 &&
302 dst_channels[channel].area.step % 8 == 0,
308 dst_frames = rate_dst_frames(plugin, frames);
309 if (dst_frames > dst_channels[0].frames)
310 dst_frames = dst_channels[0].frames;
311 data = (struct rate_priv *)plugin->extra_data;
312 data->func(plugin, src_channels, dst_channels, frames, dst_frames);
316 static int rate_action(struct snd_pcm_plugin *plugin,
317 enum snd_pcm_plugin_action action,
320 snd_assert(plugin != NULL, return -ENXIO);
329 return 0; /* silenty ignore other actions */
332 int snd_pcm_plugin_build_rate(struct snd_pcm_substream *plug,
333 struct snd_pcm_plugin_format *src_format,
334 struct snd_pcm_plugin_format *dst_format,
335 struct snd_pcm_plugin **r_plugin)
338 struct rate_priv *data;
339 struct snd_pcm_plugin *plugin;
341 snd_assert(r_plugin != NULL, return -ENXIO);
344 snd_assert(src_format->channels == dst_format->channels, return -ENXIO);
345 snd_assert(src_format->channels > 0, return -ENXIO);
346 snd_assert(snd_pcm_format_linear(src_format->format) != 0, return -ENXIO);
347 snd_assert(snd_pcm_format_linear(dst_format->format) != 0, return -ENXIO);
348 snd_assert(src_format->rate != dst_format->rate, return -ENXIO);
350 err = snd_pcm_plugin_build(plug, "rate conversion",
351 src_format, dst_format,
352 sizeof(struct rate_priv) +
353 src_format->channels * sizeof(struct rate_channel),
357 data = (struct rate_priv *)plugin->extra_data;
358 data->get = getput_index(src_format->format);
359 snd_assert(data->get >= 0 && data->get < 4*2*2, return -EINVAL);
360 data->put = getput_index(dst_format->format);
361 snd_assert(data->put >= 0 && data->put < 4*2*2, return -EINVAL);
363 if (src_format->rate < dst_format->rate) {
364 data->pitch = ((src_format->rate << SHIFT) + (dst_format->rate >> 1)) / dst_format->rate;
365 data->func = resample_expand;
367 data->pitch = ((dst_format->rate << SHIFT) + (src_format->rate >> 1)) / src_format->rate;
368 data->func = resample_shrink;
372 data->old_src_frames = data->old_dst_frames = 0;
373 plugin->transfer = rate_transfer;
374 plugin->src_frames = rate_src_frames;
375 plugin->dst_frames = rate_dst_frames;
376 plugin->action = rate_action;