22 #include <linux/time.h>
28 #define BITS (1<<SHIFT)
29 #define R_MASK (BITS-1)
41 const struct snd_pcm_plugin_channel *src_channels,
42 struct snd_pcm_plugin_channel *dst_channels,
43 int src_frames,
int dst_frames);
53 static void rate_init(
struct snd_pcm_plugin *plugin)
58 for (channel = 0; channel < plugin->src_format.channels; channel++) {
64 static void resample_expand(
struct snd_pcm_plugin *plugin,
65 const struct snd_pcm_plugin_channel *src_channels,
66 struct snd_pcm_plugin_channel *dst_channels,
67 int src_frames,
int dst_frames)
74 int src_step, dst_step;
75 int src_frames1, dst_frames1;
79 for (channel = 0; channel < plugin->src_format.channels; channel++) {
81 S1 = rchannels->last_S1;
82 S2 = rchannels->last_S2;
83 if (!src_channels[channel].
enabled) {
84 if (dst_channels[channel].
wanted)
86 dst_channels[
channel].enabled = 0;
89 dst_channels[
channel].enabled = 1;
90 src = (
signed short *)src_channels[channel].area.addr +
91 src_channels[channel].area.first / 8 / 2;
92 dst = (
signed short *)dst_channels[
channel].area.addr +
93 dst_channels[
channel].area.first / 8 / 2;
94 src_step = src_channels[
channel].area.step / 8 / 2;
95 dst_step = dst_channels[
channel].area.step / 8 / 2;
96 src_frames1 = src_frames;
97 dst_frames1 = dst_frames;
98 while (dst_frames1-- > 0) {
102 if (src_frames1-- > 0) {
107 val = S1 + ((S2 -
S1) * (
signed int)
pos) /
BITS;
110 else if (val > 32767)
116 rchannels->last_S1 =
S1;
117 rchannels->last_S2 =
S2;
123 static void resample_shrink(
struct snd_pcm_plugin *plugin,
124 const struct snd_pcm_plugin_channel *src_channels,
125 struct snd_pcm_plugin_channel *dst_channels,
126 int src_frames,
int dst_frames)
128 unsigned int pos = 0;
133 int src_step, dst_step;
134 int src_frames1, dst_frames1;
138 for (channel = 0; channel < plugin->src_format.channels; ++
channel) {
140 S1 = rchannels->last_S1;
141 S2 = rchannels->last_S2;
142 if (!src_channels[channel].enabled) {
143 if (dst_channels[channel].
wanted)
145 dst_channels[
channel].enabled = 0;
148 dst_channels[
channel].enabled = 1;
149 src = (
signed short *)src_channels[channel].area.addr +
150 src_channels[channel].area.first / 8 / 2;
151 dst = (
signed short *)dst_channels[
channel].area.addr +
152 dst_channels[
channel].area.first / 8 / 2;
153 src_step = src_channels[
channel].area.step / 8 / 2;
154 dst_step = dst_channels[
channel].area.step / 8 / 2;
155 src_frames1 = src_frames;
156 dst_frames1 = dst_frames;
157 while (dst_frames1 > 0) {
159 if (src_frames1-- > 0) {
165 val = S1 + ((S2 -
S1) * (
signed int)
pos) /
BITS;
168 else if (val > 32767)
176 rchannels->last_S1 =
S1;
177 rchannels->last_S2 =
S2;
192 data = (
struct rate_priv *)plugin->extra_data;
193 if (plugin->src_format.rate < plugin->dst_format.rate) {
225 data = (
struct rate_priv *)plugin->extra_data;
226 if (plugin->src_format.rate < plugin->dst_format.rate) {
250 const struct snd_pcm_plugin_channel *src_channels,
251 struct snd_pcm_plugin_channel *dst_channels,
257 if (
snd_BUG_ON(!plugin || !src_channels || !dst_channels))
261 #ifdef CONFIG_SND_DEBUG
264 for (channel = 0; channel < plugin->src_format.channels; channel++) {
265 if (
snd_BUG_ON(src_channels[channel].area.first % 8 ||
266 src_channels[channel].area.step % 8))
268 if (
snd_BUG_ON(dst_channels[channel].area.first % 8 ||
269 dst_channels[channel].area.step % 8))
275 dst_frames = rate_dst_frames(plugin, frames);
276 if (dst_frames > dst_channels[0].frames)
277 dst_frames = dst_channels[0].frames;
278 data = (
struct rate_priv *)plugin->extra_data;
279 data->
func(plugin, src_channels, dst_channels, frames, dst_frames);
283 static int rate_action(
struct snd_pcm_plugin *plugin,
284 enum snd_pcm_plugin_action
action,
301 struct snd_pcm_plugin_format *src_format,
302 struct snd_pcm_plugin_format *dst_format,
303 struct snd_pcm_plugin **r_plugin)
307 struct snd_pcm_plugin *plugin;
313 if (
snd_BUG_ON(src_format->channels != dst_format->channels))
317 if (
snd_BUG_ON(src_format->format != SNDRV_PCM_FORMAT_S16))
319 if (
snd_BUG_ON(dst_format->format != SNDRV_PCM_FORMAT_S16))
321 if (
snd_BUG_ON(src_format->rate == dst_format->rate))
325 src_format, dst_format,
331 data = (
struct rate_priv *)plugin->extra_data;
332 if (src_format->rate < dst_format->rate) {
333 data->
pitch = ((src_format->rate <<
SHIFT) + (dst_format->rate >> 1)) / dst_format->rate;
334 data->
func = resample_expand;
336 data->
pitch = ((dst_format->rate <<
SHIFT) + (src_format->rate >> 1)) / src_format->rate;
337 data->
func = resample_shrink;
342 plugin->transfer = rate_transfer;
343 plugin->src_frames = rate_src_frames;
344 plugin->dst_frames = rate_dst_frames;
345 plugin->action = rate_action;