Commit 9390ec85 authored by Takashi Iwai's avatar Takashi Iwai Committed by Jaroslav Kysela

[ALSA] Simplify the format conversion in PCM OSS emulation

Simplify the format conversion code in PCM OSS emulation.
This patch also adds the support of 3bytes 24bit formats with linear
and mulaw, but they are not enabled in pcm_plugin.c yet.
Signed-off-by: default avatarTakashi Iwai <tiwai@suse.de>
Signed-off-by: default avatarJaroslav Kysela <perex@suse.cz>
parent 887f9f02
...@@ -31,19 +31,34 @@ ...@@ -31,19 +31,34 @@
*/ */
struct linear_priv { struct linear_priv {
int conv; int cvt_endian; /* need endian conversion? */
unsigned int src_ofs; /* byte offset in source format */
unsigned int dst_ofs; /* byte soffset in destination format */
unsigned int copy_ofs; /* byte offset in temporary u32 data */
unsigned int dst_bytes; /* byte size of destination format */
unsigned int copy_bytes; /* bytes to copy per conversion */
unsigned int flip; /* MSB flip for signeness, done after endian conv */
}; };
static inline void do_convert(struct linear_priv *data,
unsigned char *dst, unsigned char *src)
{
unsigned int tmp = 0;
unsigned char *p = (unsigned char *)&tmp;
memcpy(p + data->copy_ofs, src + data->src_ofs, data->copy_bytes);
if (data->cvt_endian)
tmp = swab32(tmp);
tmp ^= data->flip;
memcpy(dst, p + data->dst_ofs, data->dst_bytes);
}
static void convert(struct snd_pcm_plugin *plugin, static void convert(struct snd_pcm_plugin *plugin,
const struct snd_pcm_plugin_channel *src_channels, const struct snd_pcm_plugin_channel *src_channels,
struct snd_pcm_plugin_channel *dst_channels, struct snd_pcm_plugin_channel *dst_channels,
snd_pcm_uframes_t frames) snd_pcm_uframes_t frames)
{ {
#define CONV_LABELS
#include "plugin_ops.h"
#undef CONV_LABELS
struct linear_priv *data = (struct linear_priv *)plugin->extra_data; struct linear_priv *data = (struct linear_priv *)plugin->extra_data;
void *conv = conv_labels[data->conv];
int channel; int channel;
int nchannels = plugin->src_format.channels; int nchannels = plugin->src_format.channels;
for (channel = 0; channel < nchannels; ++channel) { for (channel = 0; channel < nchannels; ++channel) {
...@@ -64,11 +79,7 @@ static void convert(struct snd_pcm_plugin *plugin, ...@@ -64,11 +79,7 @@ static void convert(struct snd_pcm_plugin *plugin,
dst_step = dst_channels[channel].area.step / 8; dst_step = dst_channels[channel].area.step / 8;
frames1 = frames; frames1 = frames;
while (frames1-- > 0) { while (frames1-- > 0) {
goto *conv; do_convert(data, dst, src);
#define CONV_END after
#include "plugin_ops.h"
#undef CONV_END
after:
src += src_step; src += src_step;
dst += dst_step; dst += dst_step;
} }
...@@ -103,29 +114,36 @@ static snd_pcm_sframes_t linear_transfer(struct snd_pcm_plugin *plugin, ...@@ -103,29 +114,36 @@ static snd_pcm_sframes_t linear_transfer(struct snd_pcm_plugin *plugin,
return frames; return frames;
} }
static int conv_index(int src_format, int dst_format) static void init_data(struct linear_priv *data, int src_format, int dst_format)
{ {
int src_endian, dst_endian, sign, src_width, dst_width; int src_le, dst_le, src_bytes, dst_bytes;
sign = (snd_pcm_format_signed(src_format) != src_bytes = snd_pcm_format_width(src_format) / 8;
snd_pcm_format_signed(dst_format)); dst_bytes = snd_pcm_format_width(dst_format) / 8;
#ifdef SNDRV_LITTLE_ENDIAN src_le = snd_pcm_format_little_endian(src_format) > 0;
src_endian = snd_pcm_format_big_endian(src_format); dst_le = snd_pcm_format_little_endian(dst_format) > 0;
dst_endian = snd_pcm_format_big_endian(dst_format);
#else
src_endian = snd_pcm_format_little_endian(src_format);
dst_endian = snd_pcm_format_little_endian(dst_format);
#endif
if (src_endian < 0)
src_endian = 0;
if (dst_endian < 0)
dst_endian = 0;
src_width = snd_pcm_format_width(src_format) / 8 - 1; data->dst_bytes = dst_bytes;
dst_width = snd_pcm_format_width(dst_format) / 8 - 1; data->cvt_endian = src_le != dst_le;
data->copy_bytes = src_bytes < dst_bytes ? src_bytes : dst_bytes;
return src_width * 32 + src_endian * 16 + sign * 8 + dst_width * 2 + dst_endian; if (src_le) {
data->copy_ofs = 4 - data->copy_bytes;
data->src_ofs = src_bytes - data->copy_bytes;
} else
data->src_ofs = snd_pcm_format_physical_width(src_format) / 8 -
src_bytes;
if (dst_le)
data->dst_ofs = 4 - data->dst_bytes;
else
data->dst_ofs = snd_pcm_format_physical_width(dst_format) / 8 -
dst_bytes;
if (snd_pcm_format_signed(src_format) !=
snd_pcm_format_signed(dst_format)) {
if (dst_le)
data->flip = cpu_to_le32(0x80000000);
else
data->flip = cpu_to_be32(0x80000000);
}
} }
int snd_pcm_plugin_build_linear(struct snd_pcm_substream *plug, int snd_pcm_plugin_build_linear(struct snd_pcm_substream *plug,
...@@ -151,7 +169,7 @@ int snd_pcm_plugin_build_linear(struct snd_pcm_substream *plug, ...@@ -151,7 +169,7 @@ int snd_pcm_plugin_build_linear(struct snd_pcm_substream *plug,
if (err < 0) if (err < 0)
return err; return err;
data = (struct linear_priv *)plugin->extra_data; data = (struct linear_priv *)plugin->extra_data;
data->conv = conv_index(src_format->format, dst_format->format); init_data(data, src_format->format, dst_format->format);
plugin->transfer = linear_transfer; plugin->transfer = linear_transfer;
*r_plugin = plugin; *r_plugin = plugin;
return 0; return 0;
......
...@@ -146,19 +146,32 @@ typedef void (*mulaw_f)(struct snd_pcm_plugin *plugin, ...@@ -146,19 +146,32 @@ typedef void (*mulaw_f)(struct snd_pcm_plugin *plugin,
struct mulaw_priv { struct mulaw_priv {
mulaw_f func; mulaw_f func;
int conv; int cvt_endian; /* need endian conversion? */
unsigned int native_ofs; /* byte offset in native format */
unsigned int copy_ofs; /* byte offset in s16 format */
unsigned int native_bytes; /* byte size of the native format */
unsigned int copy_bytes; /* bytes to copy per conversion */
u16 flip; /* MSB flip for signedness, done after endian conversion */
}; };
static inline void cvt_s16_to_native(struct mulaw_priv *data,
unsigned char *dst, u16 sample)
{
sample ^= data->flip;
if (data->cvt_endian)
sample = swab16(sample);
if (data->native_bytes > data->copy_bytes)
memset(dst, 0, data->native_bytes);
memcpy(dst + data->native_ofs, (char *)&sample + data->copy_ofs,
data->copy_bytes);
}
static void mulaw_decode(struct snd_pcm_plugin *plugin, static void mulaw_decode(struct snd_pcm_plugin *plugin,
const struct snd_pcm_plugin_channel *src_channels, const struct snd_pcm_plugin_channel *src_channels,
struct snd_pcm_plugin_channel *dst_channels, struct snd_pcm_plugin_channel *dst_channels,
snd_pcm_uframes_t frames) snd_pcm_uframes_t frames)
{ {
#define PUT_S16_LABELS
#include "plugin_ops.h"
#undef PUT_S16_LABELS
struct mulaw_priv *data = (struct mulaw_priv *)plugin->extra_data; struct mulaw_priv *data = (struct mulaw_priv *)plugin->extra_data;
void *put = put_s16_labels[data->conv];
int channel; int channel;
int nchannels = plugin->src_format.channels; int nchannels = plugin->src_format.channels;
for (channel = 0; channel < nchannels; ++channel) { for (channel = 0; channel < nchannels; ++channel) {
...@@ -180,30 +193,33 @@ static void mulaw_decode(struct snd_pcm_plugin *plugin, ...@@ -180,30 +193,33 @@ static void mulaw_decode(struct snd_pcm_plugin *plugin,
frames1 = frames; frames1 = frames;
while (frames1-- > 0) { while (frames1-- > 0) {
signed short sample = ulaw2linear(*src); signed short sample = ulaw2linear(*src);
goto *put; cvt_s16_to_native(data, dst, sample);
#define PUT_S16_END after
#include "plugin_ops.h"
#undef PUT_S16_END
after:
src += src_step; src += src_step;
dst += dst_step; dst += dst_step;
} }
} }
} }
static inline signed short cvt_native_to_s16(struct mulaw_priv *data,
unsigned char *src)
{
u16 sample = 0;
memcpy((char *)&sample + data->copy_ofs, src + data->native_ofs,
data->copy_bytes);
if (data->cvt_endian)
sample = swab16(sample);
sample ^= data->flip;
return (signed short)sample;
}
static void mulaw_encode(struct snd_pcm_plugin *plugin, static void mulaw_encode(struct snd_pcm_plugin *plugin,
const struct snd_pcm_plugin_channel *src_channels, const struct snd_pcm_plugin_channel *src_channels,
struct snd_pcm_plugin_channel *dst_channels, struct snd_pcm_plugin_channel *dst_channels,
snd_pcm_uframes_t frames) snd_pcm_uframes_t frames)
{ {
#define GET_S16_LABELS
#include "plugin_ops.h"
#undef GET_S16_LABELS
struct mulaw_priv *data = (struct mulaw_priv *)plugin->extra_data; struct mulaw_priv *data = (struct mulaw_priv *)plugin->extra_data;
void *get = get_s16_labels[data->conv];
int channel; int channel;
int nchannels = plugin->src_format.channels; int nchannels = plugin->src_format.channels;
signed short sample = 0;
for (channel = 0; channel < nchannels; ++channel) { for (channel = 0; channel < nchannels; ++channel) {
char *src; char *src;
char *dst; char *dst;
...@@ -222,11 +238,7 @@ static void mulaw_encode(struct snd_pcm_plugin *plugin, ...@@ -222,11 +238,7 @@ static void mulaw_encode(struct snd_pcm_plugin *plugin,
dst_step = dst_channels[channel].area.step / 8; dst_step = dst_channels[channel].area.step / 8;
frames1 = frames; frames1 = frames;
while (frames1-- > 0) { while (frames1-- > 0) {
goto *get; signed short sample = cvt_native_to_s16(data, src);
#define GET_S16_END after
#include "plugin_ops.h"
#undef GET_S16_END
after:
*dst = linear2ulaw(sample); *dst = linear2ulaw(sample);
src += src_step; src += src_step;
dst += dst_step; dst += dst_step;
...@@ -262,23 +274,25 @@ static snd_pcm_sframes_t mulaw_transfer(struct snd_pcm_plugin *plugin, ...@@ -262,23 +274,25 @@ static snd_pcm_sframes_t mulaw_transfer(struct snd_pcm_plugin *plugin,
return frames; return frames;
} }
static int getput_index(int format) static void init_data(struct mulaw_priv *data, int format)
{ {
int sign, width, endian;
sign = !snd_pcm_format_signed(format);
width = snd_pcm_format_width(format) / 8 - 1;
if (width < 0 || width > 3) {
snd_printk(KERN_ERR "snd-pcm-oss: invalid format %d\n", format);
width = 0;
}
#ifdef SNDRV_LITTLE_ENDIAN #ifdef SNDRV_LITTLE_ENDIAN
endian = snd_pcm_format_big_endian(format); data->cvt_endian = snd_pcm_format_big_endian(format) > 0;
#else #else
endian = snd_pcm_format_little_endian(format); data->cvt_endian = snd_pcm_format_little_endian(format) > 0;
#endif #endif
if (endian < 0) if (!snd_pcm_format_signed(format))
endian = 0; data->flip = 0x8000;
return width * 4 + endian * 2 + sign; data->native_bytes = snd_pcm_format_physical_width(format) / 8;
data->copy_bytes = data->native_bytes < 2 ? 1 : 2;
if (snd_pcm_format_little_endian(format)) {
data->native_ofs = data->native_bytes - data->copy_bytes;
data->copy_ofs = 2 - data->copy_bytes;
} else {
/* S24 in 4bytes need an 1 byte offset */
data->native_ofs = data->native_bytes -
snd_pcm_format_width(format) / 8;
}
} }
int snd_pcm_plugin_build_mulaw(struct snd_pcm_substream *plug, int snd_pcm_plugin_build_mulaw(struct snd_pcm_substream *plug,
...@@ -319,8 +333,7 @@ int snd_pcm_plugin_build_mulaw(struct snd_pcm_substream *plug, ...@@ -319,8 +333,7 @@ int snd_pcm_plugin_build_mulaw(struct snd_pcm_substream *plug,
return err; return err;
data = (struct mulaw_priv *)plugin->extra_data; data = (struct mulaw_priv *)plugin->extra_data;
data->func = func; data->func = func;
data->conv = getput_index(format->format); init_data(data, format->format);
snd_assert(data->conv >= 0 && data->conv < 4*2*2, return -EINVAL);
plugin->transfer = mulaw_transfer; plugin->transfer = mulaw_transfer;
*r_plugin = plugin; *r_plugin = plugin;
return 0; return 0;
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment