2004-07-16 19:56:36 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2004-07-16 19:56:36 +00:00
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-07-16 19:56:36 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-07-16 19:56:36 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
#include <stdio.h>
|
2004-07-02 18:47:03 +00:00
|
|
|
#include <stdlib.h>
|
2006-01-27 16:25:31 +00:00
|
|
|
|
|
|
|
|
#include <liboil/liboilfuncs.h>
|
|
|
|
|
#include <liboil/liboil.h>
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/g711.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/macro.h>
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2006-02-17 12:10:58 +00:00
|
|
|
#include "endianmacros.h"
|
2004-07-03 23:35:12 +00:00
|
|
|
#include "sconv-s16le.h"
|
|
|
|
|
#include "sconv-s16be.h"
|
|
|
|
|
|
2006-02-17 12:10:58 +00:00
|
|
|
#include "sconv.h"
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/* u8 */
|
|
|
|
|
static void u8_to_float32ne(unsigned n, const uint8_t *a, float *b) {
|
|
|
|
|
static const double add = -1, factor = 1.0/128.0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
2004-07-03 23:35:12 +00:00
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
oil_scaleconv_f32_u8(b, a, (int) n, &add, &factor);
|
2007-01-04 13:43:45 +00:00
|
|
|
}
|
2004-07-03 23:35:12 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void u8_from_float32ne(unsigned n, const float *a, uint8_t *b) {
|
|
|
|
|
static const double add = 128, factor = 127.0;
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
oil_scaleconv_u8_f32(b, a, (int) n, &add, &factor);
|
2004-07-02 18:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void u8_to_s16ne(unsigned n, const uint8_t *a, int16_t *b) {
|
|
|
|
|
static const int16_t add = -0x80, factor = 0x100;
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
|
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
oil_conv_s16_u8(b, 2, a, 1, (int) n);
|
|
|
|
|
oil_scalaradd_s16(b, 2, b, 2, &add, (int) n);
|
|
|
|
|
oil_scalarmult_s16(b, 2, b, 2, &factor, (int) n);
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void u8_from_s16ne(unsigned n, const int16_t *a, uint8_t *b) {
|
|
|
|
|
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
|
|
|
|
|
|
|
|
|
for (; n > 0; n--, a++, b++)
|
|
|
|
|
*b = (uint8_t) (*a / 0x100 + 0x80);
|
2004-07-02 18:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/* float32 */
|
|
|
|
|
|
|
|
|
|
static void float32ne_to_float32ne(unsigned n, const float *a, float *b) {
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
oil_memcpy(b, a, (int) (sizeof(float) * n));
|
2004-07-02 18:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void float32re_to_float32ne(unsigned n, const float *a, float *b) {
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
|
|
|
|
|
|
|
|
|
for (; n > 0; n--, a++, b++)
|
|
|
|
|
*((uint32_t *) b) = PA_UINT32_SWAP(*((uint32_t *) a));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* s16 */
|
2006-11-08 13:03:35 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void s16ne_to_s16ne(unsigned n, const int16_t *a, int16_t *b) {
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
|
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
oil_memcpy(b, a, (int) (sizeof(int16_t) * n));
|
2006-11-08 13:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void s16re_to_s16ne(unsigned n, const int16_t *a, int16_t *b) {
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
2006-11-08 13:03:35 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
for (; n > 0; n--, a++, b++)
|
2008-08-19 22:39:54 +02:00
|
|
|
*b = PA_INT16_SWAP(*a);
|
2006-11-08 13:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/* ulaw */
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void ulaw_to_float32ne(unsigned n, const uint8_t *a, float *b) {
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
for (; n > 0; n--)
|
2007-10-28 19:13:50 +00:00
|
|
|
*(b++) = (float) st_ulaw2linear16(*(a++)) / 0x8000;
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void ulaw_from_float32ne(unsigned n, const float *a, uint8_t *b) {
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
for (; n > 0; n--) {
|
|
|
|
|
float v = *(a++);
|
2008-08-19 22:39:54 +02:00
|
|
|
v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
|
2007-10-28 19:13:50 +00:00
|
|
|
v *= 0x1FFF;
|
2008-10-03 02:34:59 +02:00
|
|
|
*(b++) = st_14linear2ulaw((int16_t) lrintf(v));
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ulaw_to_s16ne(unsigned n, const uint8_t *a, int16_t *b) {
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
|
|
|
|
|
|
|
|
|
for (; n > 0; n--, a++, b++)
|
|
|
|
|
*b = st_ulaw2linear16(*a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ulaw_from_s16ne(unsigned n, const int16_t *a, uint8_t *b) {
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
|
|
|
|
|
|
|
|
|
for (; n > 0; n--, a++, b++)
|
|
|
|
|
*b = st_14linear2ulaw(*a >> 2);
|
|
|
|
|
}
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/* alaw */
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void alaw_to_float32ne(unsigned n, const uint8_t *a, float *b) {
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
for (; n > 0; n--, a++, b++)
|
|
|
|
|
*b = (float) st_alaw2linear16(*a) / 0x8000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void alaw_from_float32ne(unsigned n, const float *a, uint8_t *b) {
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
|
|
|
|
|
|
|
|
|
for (; n > 0; n--, a++, b++) {
|
|
|
|
|
float v = *a;
|
2008-08-19 22:39:54 +02:00
|
|
|
v = PA_CLAMP_UNLIKELY(v, -1.0f, 1.0f);
|
2007-10-28 19:13:50 +00:00
|
|
|
v *= 0xFFF;
|
2008-10-03 02:34:59 +02:00
|
|
|
*b = st_13linear2alaw((int16_t) lrintf(v));
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void alaw_to_s16ne(unsigned n, const int8_t *a, int16_t *b) {
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
for (; n > 0; n--, a++, b++)
|
2008-08-19 22:39:54 +02:00
|
|
|
*b = st_alaw2linear16((uint8_t) *a);
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void alaw_from_s16ne(unsigned n, const int16_t *a, uint8_t *b) {
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
|
|
|
|
|
|
|
|
|
for (; n > 0; n--, a++, b++)
|
|
|
|
|
*b = st_13linear2alaw(*a >> 3);
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_convert_func_t pa_get_convert_to_float32ne_function(pa_sample_format_t f) {
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static const pa_convert_func_t table[] = {
|
|
|
|
|
[PA_SAMPLE_U8] = (pa_convert_func_t) u8_to_float32ne,
|
|
|
|
|
[PA_SAMPLE_ALAW] = (pa_convert_func_t) alaw_to_float32ne,
|
|
|
|
|
[PA_SAMPLE_ULAW] = (pa_convert_func_t) ulaw_to_float32ne,
|
|
|
|
|
[PA_SAMPLE_S16LE] = (pa_convert_func_t) pa_sconv_s16le_to_float32ne,
|
|
|
|
|
[PA_SAMPLE_S16BE] = (pa_convert_func_t) pa_sconv_s16be_to_float32ne,
|
2007-11-09 02:45:07 +00:00
|
|
|
[PA_SAMPLE_S32LE] = (pa_convert_func_t) pa_sconv_s32le_to_float32ne,
|
|
|
|
|
[PA_SAMPLE_S32BE] = (pa_convert_func_t) pa_sconv_s32be_to_float32ne,
|
2009-01-16 03:15:39 +01:00
|
|
|
[PA_SAMPLE_S24LE] = (pa_convert_func_t) pa_sconv_s24le_to_float32ne,
|
|
|
|
|
[PA_SAMPLE_S24BE] = (pa_convert_func_t) pa_sconv_s24be_to_float32ne,
|
2009-01-16 18:39:36 +01:00
|
|
|
[PA_SAMPLE_S24_32LE] = (pa_convert_func_t) pa_sconv_s24_32le_to_float32ne,
|
|
|
|
|
[PA_SAMPLE_S24_32BE] = (pa_convert_func_t) pa_sconv_s24_32be_to_float32ne,
|
2007-10-28 19:13:50 +00:00
|
|
|
[PA_SAMPLE_FLOAT32NE] = (pa_convert_func_t) float32ne_to_float32ne,
|
|
|
|
|
[PA_SAMPLE_FLOAT32RE] = (pa_convert_func_t) float32re_to_float32ne,
|
|
|
|
|
};
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(f >= 0);
|
|
|
|
|
pa_assert(f < PA_SAMPLE_MAX);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return table[f];
|
|
|
|
|
}
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_convert_func_t pa_get_convert_from_float32ne_function(pa_sample_format_t f) {
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static const pa_convert_func_t table[] = {
|
|
|
|
|
[PA_SAMPLE_U8] = (pa_convert_func_t) u8_from_float32ne,
|
|
|
|
|
[PA_SAMPLE_S16LE] = (pa_convert_func_t) pa_sconv_s16le_from_float32ne,
|
|
|
|
|
[PA_SAMPLE_S16BE] = (pa_convert_func_t) pa_sconv_s16be_from_float32ne,
|
2007-11-09 02:45:07 +00:00
|
|
|
[PA_SAMPLE_S32LE] = (pa_convert_func_t) pa_sconv_s32le_from_float32ne,
|
|
|
|
|
[PA_SAMPLE_S32BE] = (pa_convert_func_t) pa_sconv_s32be_from_float32ne,
|
2009-01-16 03:15:39 +01:00
|
|
|
[PA_SAMPLE_S24LE] = (pa_convert_func_t) pa_sconv_s24le_from_float32ne,
|
|
|
|
|
[PA_SAMPLE_S24BE] = (pa_convert_func_t) pa_sconv_s24be_from_float32ne,
|
2009-01-16 18:39:36 +01:00
|
|
|
[PA_SAMPLE_S24_32LE] = (pa_convert_func_t) pa_sconv_s24_32le_from_float32ne,
|
|
|
|
|
[PA_SAMPLE_S24_32BE] = (pa_convert_func_t) pa_sconv_s24_32be_from_float32ne,
|
2007-10-28 19:13:50 +00:00
|
|
|
[PA_SAMPLE_FLOAT32NE] = (pa_convert_func_t) float32ne_to_float32ne,
|
|
|
|
|
[PA_SAMPLE_FLOAT32RE] = (pa_convert_func_t) float32re_to_float32ne,
|
|
|
|
|
[PA_SAMPLE_ALAW] = (pa_convert_func_t) alaw_from_float32ne,
|
|
|
|
|
[PA_SAMPLE_ULAW] = (pa_convert_func_t) ulaw_from_float32ne
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pa_assert(f >= 0);
|
|
|
|
|
pa_assert(f < PA_SAMPLE_MAX);
|
|
|
|
|
|
|
|
|
|
return table[f];
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_convert_func_t pa_get_convert_to_s16ne_function(pa_sample_format_t f) {
|
|
|
|
|
|
|
|
|
|
static const pa_convert_func_t table[] = {
|
|
|
|
|
[PA_SAMPLE_U8] = (pa_convert_func_t) u8_to_s16ne,
|
|
|
|
|
[PA_SAMPLE_S16NE] = (pa_convert_func_t) s16ne_to_s16ne,
|
|
|
|
|
[PA_SAMPLE_S16RE] = (pa_convert_func_t) s16re_to_s16ne,
|
|
|
|
|
[PA_SAMPLE_FLOAT32BE] = (pa_convert_func_t) pa_sconv_float32be_to_s16ne,
|
|
|
|
|
[PA_SAMPLE_FLOAT32LE] = (pa_convert_func_t) pa_sconv_float32le_to_s16ne,
|
2007-11-09 02:45:07 +00:00
|
|
|
[PA_SAMPLE_S32BE] = (pa_convert_func_t) pa_sconv_s32be_to_s16ne,
|
|
|
|
|
[PA_SAMPLE_S32LE] = (pa_convert_func_t) pa_sconv_s32le_to_s16ne,
|
2009-01-16 03:15:39 +01:00
|
|
|
[PA_SAMPLE_S24BE] = (pa_convert_func_t) pa_sconv_s24be_to_s16ne,
|
|
|
|
|
[PA_SAMPLE_S24LE] = (pa_convert_func_t) pa_sconv_s24le_to_s16ne,
|
2009-01-16 18:39:36 +01:00
|
|
|
[PA_SAMPLE_S24_32BE] = (pa_convert_func_t) pa_sconv_s24_32be_to_s16ne,
|
|
|
|
|
[PA_SAMPLE_S24_32LE] = (pa_convert_func_t) pa_sconv_s24_32le_to_s16ne,
|
2007-10-28 19:13:50 +00:00
|
|
|
[PA_SAMPLE_ALAW] = (pa_convert_func_t) alaw_to_s16ne,
|
|
|
|
|
[PA_SAMPLE_ULAW] = (pa_convert_func_t) ulaw_to_s16ne
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pa_assert(f >= 0);
|
|
|
|
|
pa_assert(f < PA_SAMPLE_MAX);
|
|
|
|
|
|
|
|
|
|
return table[f];
|
2004-07-02 18:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_convert_func_t pa_get_convert_from_s16ne_function(pa_sample_format_t f) {
|
|
|
|
|
|
|
|
|
|
static const pa_convert_func_t table[] = {
|
|
|
|
|
[PA_SAMPLE_U8] = (pa_convert_func_t) u8_from_s16ne,
|
|
|
|
|
[PA_SAMPLE_S16NE] = (pa_convert_func_t) s16ne_to_s16ne,
|
|
|
|
|
[PA_SAMPLE_S16RE] = (pa_convert_func_t) s16re_to_s16ne,
|
|
|
|
|
[PA_SAMPLE_FLOAT32BE] = (pa_convert_func_t) pa_sconv_float32be_from_s16ne,
|
|
|
|
|
[PA_SAMPLE_FLOAT32LE] = (pa_convert_func_t) pa_sconv_float32le_from_s16ne,
|
2007-11-09 02:45:07 +00:00
|
|
|
[PA_SAMPLE_S32BE] = (pa_convert_func_t) pa_sconv_s32be_from_s16ne,
|
|
|
|
|
[PA_SAMPLE_S32LE] = (pa_convert_func_t) pa_sconv_s32le_from_s16ne,
|
2009-01-16 03:15:39 +01:00
|
|
|
[PA_SAMPLE_S24BE] = (pa_convert_func_t) pa_sconv_s24be_from_s16ne,
|
|
|
|
|
[PA_SAMPLE_S24LE] = (pa_convert_func_t) pa_sconv_s24le_from_s16ne,
|
2009-01-16 18:39:36 +01:00
|
|
|
[PA_SAMPLE_S24_32BE] = (pa_convert_func_t) pa_sconv_s24_32be_from_s16ne,
|
|
|
|
|
[PA_SAMPLE_S24_32LE] = (pa_convert_func_t) pa_sconv_s24_32le_from_s16ne,
|
2007-10-28 19:13:50 +00:00
|
|
|
[PA_SAMPLE_ALAW] = (pa_convert_func_t) alaw_from_s16ne,
|
|
|
|
|
[PA_SAMPLE_ULAW] = (pa_convert_func_t) ulaw_from_s16ne,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pa_assert(f >= 0);
|
|
|
|
|
pa_assert(f < PA_SAMPLE_MAX);
|
|
|
|
|
|
|
|
|
|
return table[f];
|
2004-07-02 18:47:03 +00:00
|
|
|
}
|