mirror of
https://github.com/alsa-project/alsa-lib.git
synced 2025-10-29 05:40:25 -04:00
Uros Bizjak <uros@kss-loka.si>
Sun, 28 Nov 1999 18:42:50 +0100 (CET) mu-Law conversion plugin contains the Sun code now. Added A-Law conversion plugin.
This commit is contained in:
parent
091ee0f71b
commit
3239ca9940
6 changed files with 610 additions and 8250 deletions
|
|
@ -125,6 +125,7 @@ int snd_pcm_plugin_build_mmap(snd_pcm_t *handle, int channel, snd_pcm_plugin_t *
|
|||
int snd_pcm_plugin_build_interleave(int src_interleave, int dst_interleave, int format, snd_pcm_plugin_t **r_plugin);
|
||||
int snd_pcm_plugin_build_linear(int src_format, int dst_format, snd_pcm_plugin_t **r_plugin);
|
||||
int snd_pcm_plugin_build_mulaw(int src_format, int dst_format, snd_pcm_plugin_t **r_plugin);
|
||||
int snd_pcm_plugin_build_alaw(int src_format, int dst_format, snd_pcm_plugin_t **r_plugin);
|
||||
int snd_pcm_plugin_build_rate(int src_format, int src_rate, int src_voices,
|
||||
int dst_format, int dst_rate, int dst_voices,
|
||||
snd_pcm_plugin_t **r_plugin);
|
||||
|
|
|
|||
|
|
@ -248,7 +248,7 @@ double snd_pcm_plugin_hardware_ratio(snd_pcm_t *pcm, int channel)
|
|||
|
||||
static unsigned int snd_pcm_plugin_formats(snd_pcm_t *pcm, unsigned int formats)
|
||||
{
|
||||
formats |= SND_PCM_FMT_MU_LAW;
|
||||
formats |= SND_PCM_FMT_MU_LAW | SND_PCM_FMT_A_LAW;
|
||||
if (formats & (SND_PCM_FMT_U8|SND_PCM_FMT_S8|
|
||||
SND_PCM_FMT_U16_LE|SND_PCM_FMT_S16_LE))
|
||||
formats |= SND_PCM_FMT_U8|SND_PCM_FMT_S8|
|
||||
|
|
@ -381,6 +381,19 @@ int snd_pcm_plugin_params(snd_pcm_t *pcm, snd_pcm_channel_params_t *params)
|
|||
return -EINVAL;
|
||||
}
|
||||
break;
|
||||
case SND_PCM_SFMT_A_LAW:
|
||||
if (sinfo.formats & SND_PCM_FMT_S16_LE) {
|
||||
sparams.format.format = SND_PCM_SFMT_S16_LE;
|
||||
} else if (sinfo.formats & SND_PCM_FMT_U16_LE) {
|
||||
sparams.format.format = SND_PCM_SFMT_U16_LE;
|
||||
} else if (sinfo.formats & SND_PCM_FMT_S8) {
|
||||
sparams.format.format = SND_PCM_SFMT_S8;
|
||||
} else if (sinfo.formats & SND_PCM_FMT_U8) {
|
||||
sparams.format.format = SND_PCM_SFMT_U8;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
|
|
@ -430,6 +443,12 @@ int snd_pcm_plugin_params(snd_pcm_t *pcm, snd_pcm_channel_params_t *params)
|
|||
swap_formats(params->channel, &srcfmt, &dstfmt);
|
||||
err = snd_pcm_plugin_build_mulaw(srcfmt, dstfmt, &plugin);
|
||||
break;
|
||||
case SND_PCM_SFMT_A_LAW:
|
||||
srcfmt = SND_PCM_SFMT_A_LAW;
|
||||
dstfmt = sparams.format.format;
|
||||
swap_formats(params->channel, &srcfmt, &dstfmt);
|
||||
err = snd_pcm_plugin_build_alaw(srcfmt, dstfmt, &plugin);
|
||||
break;
|
||||
default:
|
||||
srcfmt = params->format.format;
|
||||
dstfmt = sparams.format.format;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
EXTRA_LTLIBRARIES = libpcmplugin.la
|
||||
|
||||
libpcmplugin_la_SOURCES = block.c mmap.c stream.c linear.c interleave.c \
|
||||
mulaw.c rate.c voices.c
|
||||
mulaw.c alaw.c rate.c voices.c
|
||||
all: libpcmplugin.la
|
||||
|
||||
|
||||
|
|
|
|||
461
src/pcm/plugin/alaw.c
Normal file
461
src/pcm/plugin/alaw.c
Normal file
|
|
@ -0,0 +1,461 @@
|
|||
/*
|
||||
* A-Law conversion Plug-In Interface
|
||||
* Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
|
||||
* Portions (c) by Sun Microsystems, Inc.
|
||||
*
|
||||
*
|
||||
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <endian.h>
|
||||
#include <byteswap.h>
|
||||
#include "../pcm_local.h"
|
||||
|
||||
#define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
|
||||
#define QUANT_MASK (0xf) /* Quantization field mask. */
|
||||
#define NSEGS (8) /* Number of A-law segments. */
|
||||
#define SEG_SHIFT (4) /* Left shift for segment number. */
|
||||
#define SEG_MASK (0x70) /* Segment field mask. */
|
||||
|
||||
static short alaw_seg_end[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF,
|
||||
0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
|
||||
|
||||
static inline int search(int val, short *table, int size)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < size; i++) {
|
||||
if (val <= *table++)
|
||||
return (i);
|
||||
}
|
||||
return (size);
|
||||
}
|
||||
|
||||
/*
|
||||
* linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law
|
||||
*
|
||||
* linear2alaw() accepts an 16-bit integer and encodes it as A-law data.
|
||||
*
|
||||
* Linear Input Code Compressed Code
|
||||
* ------------------------ ---------------
|
||||
* 0000000wxyza 000wxyz
|
||||
* 0000001wxyza 001wxyz
|
||||
* 000001wxyzab 010wxyz
|
||||
* 00001wxyzabc 011wxyz
|
||||
* 0001wxyzabcd 100wxyz
|
||||
* 001wxyzabcde 101wxyz
|
||||
* 01wxyzabcdef 110wxyz
|
||||
* 1wxyzabcdefg 111wxyz
|
||||
*
|
||||
* For further information see John C. Bellamy's Digital Telephony, 1982,
|
||||
* John Wiley & Sons, pps 98-111 and 472-476.
|
||||
*/
|
||||
static inline unsigned char linear2alaw(int pcm_val) /* 2's complement (16-bit range) */
|
||||
{
|
||||
int mask;
|
||||
int seg;
|
||||
unsigned char aval;
|
||||
|
||||
if (pcm_val >= 0) {
|
||||
mask = 0xD5; /* sign (7th) bit = 1 */
|
||||
} else {
|
||||
mask = 0x55; /* sign bit = 0 */
|
||||
pcm_val = -pcm_val - 8;
|
||||
}
|
||||
|
||||
/* Convert the scaled magnitude to segment number. */
|
||||
seg = search(pcm_val, alaw_seg_end, NSEGS);
|
||||
|
||||
/* Combine the sign, segment, and quantization bits. */
|
||||
|
||||
if (seg >= 8) /* out of range, return maximum value. */
|
||||
return (0x7F ^ mask);
|
||||
else {
|
||||
aval = seg << SEG_SHIFT;
|
||||
if (seg < 2)
|
||||
aval |= (pcm_val >> 4) & QUANT_MASK;
|
||||
else
|
||||
aval |= (pcm_val >> (seg + 3)) & QUANT_MASK;
|
||||
return (aval ^ mask);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* alaw2linear() - Convert an A-law value to 16-bit linear PCM
|
||||
*
|
||||
*/
|
||||
static inline int alaw2linear(unsigned char a_val)
|
||||
{
|
||||
int t;
|
||||
int seg;
|
||||
|
||||
a_val ^= 0x55;
|
||||
|
||||
t = (a_val & QUANT_MASK) << 4;
|
||||
seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
|
||||
switch (seg) {
|
||||
case 0:
|
||||
t += 8;
|
||||
break;
|
||||
case 1:
|
||||
t += 0x108;
|
||||
break;
|
||||
default:
|
||||
t += 0x108;
|
||||
t <<= seg - 1;
|
||||
}
|
||||
return ((a_val & SIGN_BIT) ? t : -t);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Basic A-Law plugin
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
_S8_ALAW,
|
||||
_U8_ALAW,
|
||||
_S16LE_ALAW,
|
||||
_U16LE_ALAW,
|
||||
_S16BE_ALAW,
|
||||
_U16BE_ALAW,
|
||||
_ALAW_S8,
|
||||
_ALAW_U8,
|
||||
_ALAW_S16LE,
|
||||
_ALAW_U16LE,
|
||||
_ALAW_S16BE,
|
||||
_ALAW_U16BE
|
||||
} combination_t;
|
||||
|
||||
struct alaw_private_data {
|
||||
combination_t cmd;
|
||||
};
|
||||
|
||||
static void alaw_conv_u8bit_alaw(unsigned char *src_ptr, unsigned char *dst_ptr, size_t size)
|
||||
{
|
||||
unsigned int pcm;
|
||||
|
||||
while (size-- > 0) {
|
||||
pcm = ((*src_ptr++) ^ 0x80) << 8;
|
||||
*dst_ptr++ = linear2alaw((signed short)(pcm));
|
||||
}
|
||||
}
|
||||
|
||||
static void alaw_conv_s8bit_alaw(unsigned char *src_ptr, unsigned char *dst_ptr, size_t size)
|
||||
{
|
||||
unsigned int pcm;
|
||||
|
||||
while (size-- > 0) {
|
||||
pcm = *src_ptr++ << 8;
|
||||
*dst_ptr++ = linear2alaw((signed short)(pcm));
|
||||
}
|
||||
}
|
||||
|
||||
static void alaw_conv_s16bit_alaw(unsigned short *src_ptr, unsigned char *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = linear2alaw((signed short)(*src_ptr++));
|
||||
}
|
||||
|
||||
static void alaw_conv_s16bit_swap_alaw(unsigned short *src_ptr, unsigned char *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = linear2alaw((signed short)(bswap_16(*src_ptr++)));
|
||||
}
|
||||
|
||||
static void alaw_conv_u16bit_alaw(unsigned short *src_ptr, unsigned char *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = linear2alaw((signed short)((*src_ptr++) ^ 0x8000));
|
||||
}
|
||||
|
||||
static void alaw_conv_u16bit_swap_alaw(unsigned short *src_ptr, unsigned char *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = linear2alaw((signed short)(bswap_16((*src_ptr++) ^ 0x8000)));
|
||||
}
|
||||
|
||||
static void alaw_conv_alaw_u8bit(unsigned char *src_ptr, unsigned char *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = (alaw2linear(*src_ptr++) >> 8) ^ 0x80;
|
||||
}
|
||||
|
||||
static void alaw_conv_alaw_s8bit(unsigned char *src_ptr, unsigned char *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = alaw2linear(*src_ptr++) >> 8;
|
||||
}
|
||||
|
||||
static void alaw_conv_alaw_s16bit(unsigned char *src_ptr, unsigned short *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = alaw2linear(*src_ptr++);
|
||||
}
|
||||
|
||||
static void alaw_conv_alaw_swap_s16bit(unsigned char *src_ptr, unsigned short *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = bswap_16(alaw2linear(*src_ptr++));
|
||||
}
|
||||
|
||||
static void alaw_conv_alaw_u16bit(unsigned char *src_ptr, unsigned short *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = alaw2linear(*src_ptr++) ^ 0x8000;
|
||||
}
|
||||
|
||||
static void alaw_conv_alaw_swap_u16bit(unsigned char *src_ptr, unsigned short *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = bswap_16(alaw2linear(*src_ptr++) ^ 0x8000);
|
||||
}
|
||||
|
||||
static ssize_t alaw_transfer(snd_pcm_plugin_t *plugin,
|
||||
char *src_ptr, size_t src_size,
|
||||
char *dst_ptr, size_t dst_size)
|
||||
{
|
||||
struct alaw_private_data *data;
|
||||
|
||||
if (plugin == NULL || src_ptr == NULL || src_size < 0 ||
|
||||
dst_ptr == NULL || dst_size < 0)
|
||||
return -EINVAL;
|
||||
if (src_size == 0)
|
||||
return 0;
|
||||
data = (struct alaw_private_data *)snd_pcm_plugin_extra_data(plugin);
|
||||
if (data == NULL)
|
||||
return -EINVAL;
|
||||
switch (data->cmd) {
|
||||
case _U8_ALAW:
|
||||
if (dst_size < src_size)
|
||||
return -EINVAL;
|
||||
alaw_conv_u8bit_alaw(src_ptr, dst_ptr, src_size);
|
||||
return src_size;
|
||||
case _S8_ALAW:
|
||||
if (dst_size < src_size)
|
||||
return -EINVAL;
|
||||
alaw_conv_s8bit_alaw(src_ptr, dst_ptr, src_size);
|
||||
return src_size;
|
||||
case _S16LE_ALAW:
|
||||
if ((dst_size << 1) < src_size)
|
||||
return -EINVAL;
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
alaw_conv_s16bit_alaw((short *)src_ptr, dst_ptr, src_size >> 1);
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
alaw_conv_s16bit_swap_alaw((short *)src_ptr, dst_ptr, src_size >> 1);
|
||||
#else
|
||||
#error "Have to be coded..."
|
||||
#endif
|
||||
return src_size >> 1;
|
||||
case _U16LE_ALAW:
|
||||
if ((dst_size << 1) < src_size)
|
||||
return -EINVAL;
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
alaw_conv_u16bit_alaw((short *)src_ptr, dst_ptr, src_size >> 1);
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
alaw_conv_u16bit_swap_alaw((short *)src_ptr, dst_ptr, src_size >> 1);
|
||||
#else
|
||||
#error "Have to be coded..."
|
||||
#endif
|
||||
return src_size >> 1;
|
||||
case _S16BE_ALAW:
|
||||
if ((dst_size << 1) < src_size)
|
||||
return -EINVAL;
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
alaw_conv_s16bit_swap_alaw((short *)src_ptr, dst_ptr, src_size >> 1);
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
alaw_conv_s16bit_alaw((short *)src_ptr, dst_ptr, src_size >> 1);
|
||||
#else
|
||||
#error "Have to be coded..."
|
||||
#endif
|
||||
return src_size >> 1;
|
||||
case _U16BE_ALAW:
|
||||
if ((dst_size << 1) < src_size)
|
||||
return -EINVAL;
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
alaw_conv_u16bit_swap_alaw((short *)src_ptr, dst_ptr, src_size >> 1);
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
alaw_conv_u16bit_alaw((short *)src_ptr, dst_ptr, src_size >> 1);
|
||||
#else
|
||||
#error "Have to be coded..."
|
||||
#endif
|
||||
return src_size >> 1;
|
||||
case _ALAW_U8:
|
||||
if (dst_size < src_size)
|
||||
return -EINVAL;
|
||||
alaw_conv_alaw_u8bit(src_ptr, dst_ptr, src_size);
|
||||
return src_size;
|
||||
case _ALAW_S8:
|
||||
if (dst_size < src_size)
|
||||
return -EINVAL;
|
||||
alaw_conv_alaw_s8bit(src_ptr, dst_ptr, src_size);
|
||||
return src_size;
|
||||
case _ALAW_S16LE:
|
||||
if ((dst_size >> 1) < src_size)
|
||||
return -EINVAL;
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
alaw_conv_alaw_s16bit(src_ptr, (short *)dst_ptr, src_size);
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
alaw_conv_alaw_swap_s16bit(src_ptr, (short *)dst_ptr, src_size);
|
||||
#else
|
||||
#error "Have to be coded..."
|
||||
#endif
|
||||
return src_size << 1;
|
||||
case _ALAW_U16LE:
|
||||
if ((dst_size >> 1) < src_size)
|
||||
return -EINVAL;
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
alaw_conv_alaw_u16bit(src_ptr, (short *)dst_ptr, src_size);
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
alaw_conv_alaw_swap_u16bit(src_ptr, (short *)dst_ptr, src_size);
|
||||
#else
|
||||
#error "Have to be coded..."
|
||||
#endif
|
||||
return src_size << 1;
|
||||
case _ALAW_S16BE:
|
||||
if ((dst_size >> 1) < src_size)
|
||||
return -EINVAL;
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
alaw_conv_alaw_swap_s16bit(src_ptr, (short *)dst_ptr, src_size);
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
alaw_conv_alaw_s16bit(src_ptr, (short *)dst_ptr, src_size);
|
||||
#else
|
||||
#error "Have to be coded..."
|
||||
#endif
|
||||
return src_size << 1;
|
||||
case _ALAW_U16BE:
|
||||
if ((dst_size >> 1) < src_size)
|
||||
return -EINVAL;
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
alaw_conv_alaw_swap_u16bit(src_ptr, (short *)dst_ptr, src_size);
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
alaw_conv_alaw_u16bit(src_ptr, (short *)dst_ptr, src_size);
|
||||
#else
|
||||
#error "Have to be coded..."
|
||||
#endif
|
||||
return dst_size << 1;
|
||||
default:
|
||||
return -EIO;
|
||||
}
|
||||
}
|
||||
|
||||
static ssize_t alaw_src_size(snd_pcm_plugin_t *plugin, size_t size)
|
||||
{
|
||||
struct alaw_private_data *data;
|
||||
|
||||
if (!plugin || size <= 0)
|
||||
return -EINVAL;
|
||||
data = (struct alaw_private_data *)snd_pcm_plugin_extra_data(plugin);
|
||||
switch (data->cmd) {
|
||||
case _U8_ALAW:
|
||||
case _S8_ALAW:
|
||||
case _ALAW_U8:
|
||||
case _ALAW_S8:
|
||||
return size;
|
||||
case _U16LE_ALAW:
|
||||
case _S16LE_ALAW:
|
||||
case _U16BE_ALAW:
|
||||
case _S16BE_ALAW:
|
||||
return size * 2;
|
||||
case _ALAW_U16LE:
|
||||
case _ALAW_S16LE:
|
||||
case _ALAW_U16BE:
|
||||
case _ALAW_S16BE:
|
||||
return size / 2;
|
||||
default:
|
||||
return -EIO;
|
||||
}
|
||||
}
|
||||
|
||||
static ssize_t alaw_dst_size(snd_pcm_plugin_t *plugin, size_t size)
|
||||
{
|
||||
struct alaw_private_data *data;
|
||||
|
||||
if (!plugin || size <= 0)
|
||||
return -EINVAL;
|
||||
data = (struct alaw_private_data *)snd_pcm_plugin_extra_data(plugin);
|
||||
switch (data->cmd) {
|
||||
case _U8_ALAW:
|
||||
case _S8_ALAW:
|
||||
case _ALAW_U8:
|
||||
case _ALAW_S8:
|
||||
return size;
|
||||
case _U16LE_ALAW:
|
||||
case _S16LE_ALAW:
|
||||
case _U16BE_ALAW:
|
||||
case _S16BE_ALAW:
|
||||
return size / 2;
|
||||
case _ALAW_U16LE:
|
||||
case _ALAW_S16LE:
|
||||
case _ALAW_U16BE:
|
||||
case _ALAW_S16BE:
|
||||
return size * 2;
|
||||
default:
|
||||
return -EIO;
|
||||
}
|
||||
}
|
||||
|
||||
int snd_pcm_plugin_build_alaw(int src_format, int dst_format, snd_pcm_plugin_t **r_plugin)
|
||||
{
|
||||
struct alaw_private_data *data;
|
||||
snd_pcm_plugin_t *plugin;
|
||||
combination_t cmd;
|
||||
|
||||
if (!r_plugin)
|
||||
return -EINVAL;
|
||||
*r_plugin = NULL;
|
||||
if (dst_format == SND_PCM_SFMT_A_LAW) {
|
||||
switch (src_format) {
|
||||
case SND_PCM_SFMT_U8: cmd = _U8_ALAW; break;
|
||||
case SND_PCM_SFMT_S8: cmd = _S8_ALAW; break;
|
||||
case SND_PCM_SFMT_U16_LE: cmd = _U16LE_ALAW; break;
|
||||
case SND_PCM_SFMT_S16_LE: cmd = _S16LE_ALAW; break;
|
||||
case SND_PCM_SFMT_U16_BE: cmd = _U16BE_ALAW; break;
|
||||
case SND_PCM_SFMT_S16_BE: cmd = _S16BE_ALAW; break;
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
} else if (src_format == SND_PCM_SFMT_A_LAW) {
|
||||
switch (dst_format) {
|
||||
case SND_PCM_SFMT_U8: cmd = _ALAW_U8; break;
|
||||
case SND_PCM_SFMT_S8: cmd = _ALAW_S8; break;
|
||||
case SND_PCM_SFMT_U16_LE: cmd = _ALAW_U16LE; break;
|
||||
case SND_PCM_SFMT_S16_LE: cmd = _ALAW_S16LE; break;
|
||||
case SND_PCM_SFMT_U16_BE: cmd = _ALAW_U16BE; break;
|
||||
case SND_PCM_SFMT_S16_BE: cmd = _ALAW_S16BE; break;
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
plugin = snd_pcm_plugin_build("A-Law<->linear conversion",
|
||||
sizeof(struct alaw_private_data));
|
||||
if (plugin == NULL)
|
||||
return -ENOMEM;
|
||||
data = (struct alaw_private_data *)snd_pcm_plugin_extra_data(plugin);
|
||||
data->cmd = cmd;
|
||||
plugin->transfer = alaw_transfer;
|
||||
plugin->src_size = alaw_src_size;
|
||||
plugin->dst_size = alaw_dst_size;
|
||||
*r_plugin = plugin;
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
/*
|
||||
* muLaw conversion Plug-In Interface
|
||||
* Mu-Law conversion Plug-In Interface
|
||||
* Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
|
||||
* Portions (c) by Sun Microsystems, Inc.
|
||||
*
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or modify
|
||||
|
|
@ -27,10 +28,116 @@
|
|||
#include <endian.h>
|
||||
#include <byteswap.h>
|
||||
#include "../pcm_local.h"
|
||||
#include "mulaw.h"
|
||||
|
||||
#define SIGN_BIT (0x80) /* Sign bit for a u-law byte. */
|
||||
#define QUANT_MASK (0xf) /* Quantization field mask. */
|
||||
#define NSEGS (8) /* Number of u-law segments. */
|
||||
#define SEG_SHIFT (4) /* Left shift for segment number. */
|
||||
#define SEG_MASK (0x70) /* Segment field mask. */
|
||||
|
||||
static short ulaw_seg_end[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF,
|
||||
0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
|
||||
|
||||
static inline int search(int val, short *table, int size)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < size; i++) {
|
||||
if (val <= *table++)
|
||||
return (i);
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
#define BIAS (0x84) /* Bias for linear code. */
|
||||
|
||||
/*
|
||||
* Basic muLaw plugin
|
||||
* linear2ulaw() - Convert a linear PCM value to u-law
|
||||
*
|
||||
* In order to simplify the encoding process, the original linear magnitude
|
||||
* is biased by adding 33 which shifts the encoding range from (0 - 8158) to
|
||||
* (33 - 8191). The result can be seen in the following encoding table:
|
||||
*
|
||||
* Biased Linear Input Code Compressed Code
|
||||
* ------------------------ ---------------
|
||||
* 00000001wxyza 000wxyz
|
||||
* 0000001wxyzab 001wxyz
|
||||
* 000001wxyzabc 010wxyz
|
||||
* 00001wxyzabcd 011wxyz
|
||||
* 0001wxyzabcde 100wxyz
|
||||
* 001wxyzabcdef 101wxyz
|
||||
* 01wxyzabcdefg 110wxyz
|
||||
* 1wxyzabcdefgh 111wxyz
|
||||
*
|
||||
* Each biased linear code has a leading 1 which identifies the segment
|
||||
* number. The value of the segment number is equal to 7 minus the number
|
||||
* of leading 0's. The quantization interval is directly available as the
|
||||
* four bits wxyz. * The trailing bits (a - h) are ignored.
|
||||
*
|
||||
* Ordinarily the complement of the resulting code word is used for
|
||||
* transmission, and so the code word is complemented before it is returned.
|
||||
*
|
||||
* For further information see John C. Bellamy's Digital Telephony, 1982,
|
||||
* John Wiley & Sons, pps 98-111 and 472-476.
|
||||
*/
|
||||
static inline unsigned char linear2ulaw(int pcm_val) /* 2's complement (16-bit range) */
|
||||
{
|
||||
int mask;
|
||||
int seg;
|
||||
unsigned char uval;
|
||||
|
||||
/* Get the sign and the magnitude of the value. */
|
||||
if (pcm_val < 0) {
|
||||
pcm_val = BIAS - pcm_val;
|
||||
mask = 0x7F;
|
||||
} else {
|
||||
pcm_val += BIAS;
|
||||
mask = 0xFF;
|
||||
}
|
||||
|
||||
/* Convert the scaled magnitude to segment number. */
|
||||
seg = search(pcm_val, ulaw_seg_end, NSEGS);
|
||||
|
||||
/*
|
||||
* Combine the sign, segment, quantization bits;
|
||||
* and complement the code word.
|
||||
*/
|
||||
if (seg >= 8) /* out of range, return maximum value. */
|
||||
return 0x7F ^ mask;
|
||||
else {
|
||||
uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF);
|
||||
return uval ^ mask;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* ulaw2linear() - Convert a u-law value to 16-bit linear PCM
|
||||
*
|
||||
* First, a biased linear code is derived from the code word. An unbiased
|
||||
* output can then be obtained by subtracting 33 from the biased code.
|
||||
*
|
||||
* Note that this function expects to be passed the complement of the
|
||||
* original code word. This is in keeping with ISDN conventions.
|
||||
*/
|
||||
static inline int ulaw2linear(unsigned char u_val)
|
||||
{
|
||||
int t;
|
||||
|
||||
/* Complement to obtain normal u-law value. */
|
||||
u_val = ~u_val;
|
||||
|
||||
/*
|
||||
* Extract and bias the quantization bits. Then
|
||||
* shift up by the segment number and subtract out the bias.
|
||||
*/
|
||||
t = ((u_val & QUANT_MASK) << 3) + BIAS;
|
||||
t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
|
||||
|
||||
return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
|
||||
}
|
||||
|
||||
/*
|
||||
* Basic Mu-Law plugin
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
|
|
@ -54,82 +161,82 @@ struct mulaw_private_data {
|
|||
|
||||
static void mulaw_conv_u8bit_mulaw(unsigned char *src_ptr, unsigned char *dst_ptr, size_t size)
|
||||
{
|
||||
unsigned int idx;
|
||||
unsigned int pcm;
|
||||
|
||||
while (size-- > 0) {
|
||||
idx = ((*src_ptr++) ^ 0x80) << 8;
|
||||
*dst_ptr++ = lintomulaw[idx];
|
||||
pcm = ((*src_ptr++) ^ 0x80) << 8;
|
||||
*dst_ptr++ = linear2ulaw((signed short)(pcm));
|
||||
}
|
||||
}
|
||||
|
||||
static void mulaw_conv_s8bit_mulaw(unsigned char *src_ptr, unsigned char *dst_ptr, size_t size)
|
||||
{
|
||||
unsigned int idx;
|
||||
unsigned int pcm;
|
||||
|
||||
while (size-- > 0) {
|
||||
idx = *src_ptr++ << 8;
|
||||
*dst_ptr++ = lintomulaw[idx];
|
||||
pcm = *src_ptr++ << 8;
|
||||
*dst_ptr++ = linear2ulaw((signed short)(pcm));
|
||||
}
|
||||
}
|
||||
|
||||
static void mulaw_conv_s16bit_mulaw(unsigned short *src_ptr, unsigned char *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = lintomulaw[*src_ptr++];
|
||||
*dst_ptr++ = linear2ulaw((signed short)(*src_ptr++));
|
||||
}
|
||||
|
||||
static void mulaw_conv_s16bit_swap_mulaw(unsigned short *src_ptr, unsigned char *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = lintomulaw[bswap_16(*src_ptr++)];
|
||||
*dst_ptr++ = linear2ulaw((signed short)(bswap_16(*src_ptr++)));
|
||||
}
|
||||
|
||||
static void mulaw_conv_u16bit_mulaw(unsigned short *src_ptr, unsigned char *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = lintomulaw[(*src_ptr++) ^ 0x8000];
|
||||
*dst_ptr++ = linear2ulaw((signed short)((*src_ptr++) ^ 0x8000));
|
||||
}
|
||||
|
||||
static void mulaw_conv_u16bit_swap_mulaw(unsigned short *src_ptr, unsigned char *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = lintomulaw[bswap_16(*src_ptr++) ^ 0x8000];
|
||||
*dst_ptr++ = linear2ulaw((signed short)(bswap_16((*src_ptr++) ^ 0x8000)));
|
||||
}
|
||||
|
||||
static void mulaw_conv_mulaw_u8bit(unsigned char *src_ptr, unsigned char *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = (mulawtolin[*src_ptr++] >> 8) ^ 0x80;
|
||||
*dst_ptr++ = (ulaw2linear(*src_ptr++) >> 8) ^ 0x80;
|
||||
}
|
||||
|
||||
static void mulaw_conv_mulaw_s8bit(unsigned char *src_ptr, unsigned char *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = mulawtolin[*src_ptr++] >> 8;
|
||||
*dst_ptr++ = ulaw2linear(*src_ptr++) >> 8;
|
||||
}
|
||||
|
||||
static void mulaw_conv_mulaw_s16bit(unsigned char *src_ptr, unsigned short *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = mulawtolin[*src_ptr++];
|
||||
*dst_ptr++ = ulaw2linear(*src_ptr++);
|
||||
}
|
||||
|
||||
static void mulaw_conv_mulaw_swap_s16bit(unsigned char *src_ptr, unsigned short *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = bswap_16(mulawtolin[*src_ptr++]);
|
||||
*dst_ptr++ = bswap_16(ulaw2linear(*src_ptr++));
|
||||
}
|
||||
|
||||
static void mulaw_conv_mulaw_u16bit(unsigned char *src_ptr, unsigned short *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = mulawtolin[*src_ptr++] ^ 0x8000;
|
||||
*dst_ptr++ = ulaw2linear(*src_ptr++) ^ 0x8000;
|
||||
}
|
||||
|
||||
static void mulaw_conv_mulaw_swap_u16bit(unsigned char *src_ptr, unsigned short *dst_ptr, size_t size)
|
||||
{
|
||||
while (size-- > 0)
|
||||
*dst_ptr++ = bswap_16(mulawtolin[*src_ptr++] ^ 0x8000);
|
||||
*dst_ptr++ = bswap_16(ulaw2linear(*src_ptr++) ^ 0x8000);
|
||||
}
|
||||
|
||||
static ssize_t mulaw_transfer(snd_pcm_plugin_t *plugin,
|
||||
|
|
@ -350,7 +457,7 @@ int snd_pcm_plugin_build_mulaw(int src_format, int dst_format, snd_pcm_plugin_t
|
|||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
plugin = snd_pcm_plugin_build("muLaw<->linear conversion",
|
||||
plugin = snd_pcm_plugin_build("Mu-Law<->linear conversion",
|
||||
sizeof(struct mulaw_private_data));
|
||||
if (plugin == NULL)
|
||||
return -ENOMEM;
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue