summaryrefslogtreecommitdiffstats
path: root/src/format.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/format.c')
-rw-r--r--src/format.c161
1 files changed, 89 insertions, 72 deletions
diff --git a/src/format.c b/src/format.c
index adac878..6faa878 100644
--- a/src/format.c
+++ b/src/format.c
@@ -1,3 +1,23 @@
+/***
+ This file is part of libsydney.
+
+ Copyright 2007-2008 Lennart Poettering
+
+ libsydney is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation, either version 2.1 of the
+ License, or (at your option) any later version.
+
+ libsydney 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with libsydney. If not, see
+ <http://www.gnu.org/licenses/>.
+***/
+
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
@@ -16,9 +36,9 @@
static int format_u8_to_s16(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
int16_t d = -0x80, f = 0x100;
- oil_conv_s16_u8(dst, dstr, src, sstr, bytes);
- oil_scalaradd_s16(dst, dstr, dst, dstr, &d, bytes);
- oil_scalarmult_s16(dst, dstr, dst, dstr, &f, bytes);
+ oil_conv_s16_u8(dst, (int) dstr, src, (int) sstr, (int) bytes);
+ oil_scalaradd_s16(dst, (int) dstr, dst, (int) dstr, &d, (int) bytes);
+ oil_scalarmult_s16(dst, (int) dstr, dst, (int) dstr, &f, (int) bytes);
return SA_SUCCESS;
}
@@ -26,19 +46,19 @@ static int format_u8_to_s16(sa_bbuffer_t *b, void *dst, size_t dstr, const void
static int format_u8_to_s32(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
int32_t d = -0x80, f = 0x1000000;
- oil_conv_s16_u8(dst, dstr, src, sstr, bytes);
- oil_scalaradd_s32(dst, dstr, dst, dstr, &d, bytes);
- oil_scalarmult_s32(dst, dstr, dst, dstr, &f, bytes);
+ oil_conv_s16_u8(dst, (int) dstr, src, (int) sstr, (int) bytes);
+ oil_scalaradd_s32(dst, (int) dstr, dst, (int) dstr, &d, (int) bytes);
+ oil_scalarmult_s32(dst, (int) dstr, dst, (int) dstr, &f, (int) bytes);
return SA_SUCCESS;
}
static int format_u8_to_f32(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
- float d = -0x80, f = 1.0/0x7F;
+ float d = (float) -0x80, f = 1.0f/0x7F;
- oil_conv_f32_u8(dst, dstr, src, sstr, bytes);
- oil_scalaradd_f32(dst, dstr, dst, dstr, &d, bytes);
- oil_scalarmult_f32(dst, dstr, dst, dstr, &f, bytes);
+ oil_conv_f32_u8(dst, (int) dstr, src, (int) sstr, (int) bytes);
+ oil_scalaradd_f32(dst, (int) dstr, dst, (int) dstr, &d, (int) bytes);
+ oil_scalarmult_f32(dst, (int) dstr, dst, (int) dstr, &f, (int) bytes);
return SA_SUCCESS;
}
@@ -70,7 +90,7 @@ static int format_ulaw_to_f32(sa_bbuffer_t *b, void *dst, size_t dstr, const voi
const uint8_t *s = src;
for (; bytes > 0; bytes --, d += dstr/sizeof(float), s += sstr)
- *d = sa_ulaw2linear16(*s * 1.0F / 0x7FFF);
+ *d = (float) sa_ulaw2linear16(*s) / (float) 0x7FFF;
return SA_SUCCESS;
}
@@ -92,7 +112,7 @@ static int format_alaw_to_s32(sa_bbuffer_t *b, void *dst, size_t dstr, const voi
const uint8_t *s = src;
for (; bytes > 0; bytes --, d += dstr/sizeof(int32_t), s += sstr)
- *d = (int32_t) sa_alaw2linear16(*(s++)) * 0x10000;
+ *d = (int32_t) sa_alaw2linear16(*s) * 0x10000;
return SA_SUCCESS;
}
@@ -102,7 +122,7 @@ static int format_alaw_to_f32(sa_bbuffer_t *b, void *dst, size_t dstr, const voi
const uint8_t *s = src;
for (; bytes > 0; bytes --, d += dstr/sizeof(float), s += sstr)
- *d = sa_alaw2linear16(*(s++) * 1.0F / 0x7FFF);
+ *d = (float) sa_alaw2linear16(*s) / (float) 0x7FFF;
return SA_SUCCESS;
}
@@ -112,7 +132,7 @@ static int format_alaw_to_f32(sa_bbuffer_t *b, void *dst, size_t dstr, const voi
static int format_s16_to_u8(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
uint8_t *d = dst;
const int16_t *s = src;
- unsigned n = bytes/sizeof(int16_t);
+ size_t n = bytes/sizeof(int16_t);
for (; n > 0; n--, d += dstr, s += sstr/sizeof(int16_t))
*d = (uint8_t) (*s / 0x100 + 0x80);
@@ -123,7 +143,7 @@ static int format_s16_to_u8(sa_bbuffer_t *b, void *dst, size_t dstr, const void
static int format_s16_to_ulaw(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
uint8_t *d = dst;
const int16_t *s = src;
- unsigned n = bytes/sizeof(int16_t);
+ size_t n = bytes/sizeof(int16_t);
for (; n > 0; n --, d += dstr, s += sstr/sizeof(int16_t))
*d = sa_14linear2ulaw(*s);
@@ -134,7 +154,7 @@ static int format_s16_to_ulaw(sa_bbuffer_t *b, void *dst, size_t dstr, const voi
static int format_s16_to_alaw(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
uint8_t *d = dst;
const int16_t *s = src;
- unsigned n = bytes/sizeof(int16_t);
+ size_t n = bytes/sizeof(int16_t);
for (; n > 0; n --, d += dstr, s += sstr/sizeof(int16_t))
*d = sa_13linear2alaw(*s);
@@ -144,20 +164,20 @@ static int format_s16_to_alaw(sa_bbuffer_t *b, void *dst, size_t dstr, const voi
static int format_s16_to_s32(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
int f = 0x10000;
- unsigned n = bytes/sizeof(int16_t);
+ size_t n = bytes/sizeof(int16_t);
- oil_conv_s32_s16(dst, dstr, src, sstr, n);
- oil_scalarmult_s32(dst, dstr, dst, dstr, &f, n);
+ oil_conv_s32_s16(dst, (int) dstr, src, (int) sstr, (int) n);
+ oil_scalarmult_s32(dst, (int) dstr, dst, (int) dstr, &f, (int) n);
return SA_SUCCESS;
}
static int format_s16_to_f32(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
- float f = 1.0/0x7ffff;
- unsigned n = bytes/sizeof(int16_t);
+ float f = 1.0f/0x7ffff;
+ size_t n = bytes/sizeof(int16_t);
- oil_conv_f32_s16(dst, dstr, src, sstr, n);
- oil_scalarmult_f32(dst, dstr, dst, dstr, &f, n);
+ oil_conv_f32_s16(dst, (int) dstr, src, (int) sstr, (int) n);
+ oil_scalarmult_f32(dst, (int) dstr, dst, (int) dstr, &f, (int) n);
return SA_SUCCESS;
}
@@ -167,13 +187,13 @@ static int format_s16_to_f32(sa_bbuffer_t *b, void *dst, size_t dstr, const void
static int format_s24_to_s32(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
int32_t *d = dst;
const uint8_t *s = src;
- unsigned n = bytes/3;
+ size_t n = bytes/3;
for (; n > 0; n--, d += dstr/sizeof(int32_t), s += sstr/3)
#if defined(SA_LITTLE_ENDIAN)
- *d = (int32_t) ((int8_t) s[2]) * 0x1000000 + s[1] * 0x10000 + s[0] * 0x100;
+ *d = (int32_t) ((int32_t) (int8_t) s[2] * 0x1000000 + (int32_t) s[1] * 0x10000 + (int32_t) s[0] * 0x100);
#elif defined(SA_BIG_ENDIAN)
- *d = (int32_t) ((int8_t) s[0]) * 0x1000000 + s[1] * 0x10000 + s[2] * 0x100;
+ *d = (int32_t) ((int32_t) (int8_t) s[0] * 0x1000000 + (int32_t) s[1] * 0x10000 + (int32_t) s[2] * 0x100);
#else
#error "Unknown byte order"
#endif
@@ -184,13 +204,13 @@ static int format_s24_to_s32(sa_bbuffer_t *b, void *dst, size_t dstr, const void
static int format_s24_to_f32(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
float *d = dst;
const uint8_t *s = src;
- unsigned n = bytes/3;
+ size_t n = bytes/3;
for (; n > 0; n--, d += dstr/sizeof(float), s += sstr/3)
#if defined(SA_LITTLE_ENDIAN)
- *d = ((float) ((int8_t) s[2]) * 0x10000 + s[1] * 0x100 + s[0]) / 0x7fffff;
+ *d = (float) ((int32_t) (int8_t) s[2] * 0x10000 + (int32_t) s[1] * 0x100 + (int32_t) s[0]) / (float) 0x7fffff;
#elif defined(SA_BIG_ENDIAN)
- *d = ((float) ((int8_t) s[0]) * 0x10000 + s[1] * 0x100 + s[2]) / 0x7fffff;
+ *d = (float) ((int32_t) (int8_t) s[0] * 0x10000 + (int32_t) s[1] * 0x100 + (int32_t) s[2]) / (float) 0x7fffff;
#else
#error "Unknown byte order"
#endif
@@ -203,7 +223,7 @@ static int format_s24_to_f32(sa_bbuffer_t *b, void *dst, size_t dstr, const void
static int format_s32_to_u8(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
uint8_t *d = dst;
const int32_t *s = src;
- unsigned n = bytes/sizeof(int32_t);
+ size_t n = bytes/sizeof(int32_t);
for (; n > 0; n--, d += dstr, s += sstr/sizeof(int32_t))
*d = (uint8_t) (*s / 0x1000000 + 0x80);
@@ -214,10 +234,10 @@ static int format_s32_to_u8(sa_bbuffer_t *b, void *dst, size_t dstr, const void
static int format_s32_to_ulaw(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
uint8_t *d = dst;
const int32_t *s = src;
- unsigned n = bytes/sizeof(int32_t);
+ size_t n = bytes/sizeof(int32_t);
for (; n > 0; n--, d += dstr, s += sstr/sizeof(int32_t))
- *d = sa_14linear2ulaw(*s / 0x10000);
+ *d = sa_14linear2ulaw((int16_t) (*s / 0x10000));
return SA_SUCCESS;
}
@@ -225,10 +245,10 @@ static int format_s32_to_ulaw(sa_bbuffer_t *b, void *dst, size_t dstr, const voi
static int format_s32_to_alaw(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
uint8_t *d = dst;
const int32_t *s = src;
- unsigned n = bytes/sizeof(int32_t);
+ size_t n = bytes/sizeof(int32_t);
for (; n > 0; n--, d += dstr, s += sstr/sizeof(int32_t))
- *d = sa_13linear2alaw(*s / 0x10000);
+ *d = sa_13linear2alaw((int16_t) (*s / 0x10000));
return SA_SUCCESS;
}
@@ -236,7 +256,7 @@ static int format_s32_to_alaw(sa_bbuffer_t *b, void *dst, size_t dstr, const voi
static int format_s32_to_s16(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
int16_t *d = dst;
const int32_t *s = src;
- unsigned n = bytes/sizeof(int32_t);
+ size_t n = bytes/sizeof(int32_t);
for (; n > 0; n--, d += dstr/sizeof(int16_t), s += sstr/sizeof(int32_t))
*d = (int16_t) (*s / 0x10000);
@@ -247,19 +267,19 @@ static int format_s32_to_s16(sa_bbuffer_t *b, void *dst, size_t dstr, const void
static int format_s32_to_s24(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
uint8_t *d = dst;
const int32_t *s = src;
- unsigned n = bytes/sizeof(float);
+ size_t n = bytes/sizeof(float);
for (; n > 0; n--, d += dstr/3, s += sstr/sizeof(int32_t)) {
uint32_t j = (uint32_t) (*s) >> 8;
#if defined(SA_LITTLE_ENDIAN)
- d[0] = j & 0xFF;
- d[1] = (j >> 8) & 0xFF;
- d[2] = (j >> 16);
+ d[0] = (uint8_t) j;
+ d[1] = (uint8_t) (j >> 8);
+ d[2] = (uint8_t) (j >> 16);
#elif defined(SA_BIG_ENDIAN)
- d[2] = j & 0xFF;
- d[1] = (j >> 8) & 0xFF;
- d[0] = (j >> 16);
+ d[2] = (uint8_t) j;
+ d[1] = (uint8_t) (j >> 8);
+ d[0] = (uint8_t) (j >> 16);
#else
#error "Unknown byte order"
#endif
@@ -269,11 +289,11 @@ static int format_s32_to_s24(sa_bbuffer_t *b, void *dst, size_t dstr, const void
}
static int format_s32_to_f32(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
- float f = 1.0/0x7fffffff;
- unsigned n = bytes/sizeof(int32_t);
+ float f = 1.0f/(float) 0x7fffffff;
+ size_t n = bytes/sizeof(int32_t);
- oil_conv_f32_s32(dst, dstr, src, sstr, n);
- oil_scalarmult_f32(dst, dstr, dst, dstr, &f, n);
+ oil_conv_f32_s32(dst, (int) dstr, src, (int) sstr, (int) n);
+ oil_scalarmult_f32(dst, (int) dstr, dst, (int) dstr, &f, (int) n);
return SA_SUCCESS;
}
@@ -285,14 +305,14 @@ static int format_f32_to_u8(sa_bbuffer_t *b, void *dst, size_t dstr, const void
const float *s = src;
float *buf;
float f = 0x7F, p = 0x80;
- unsigned n = bytes/sizeof(float);
+ size_t n = bytes/sizeof(float);
if (!(buf = sa_bbuffer_get(b, 0, bytes, 1)))
return SA_ERROR_OOM;
- oil_scalarmult_f32(buf, sizeof(float), s, sstr, &f, n);
- oil_scalaradd_f32(buf, sizeof(float), buf, sizeof(float), &p, n);
- oil_clipconv_u8_f32(d, dstr, buf, sizeof(float), n);
+ oil_scalarmult_f32(buf, sizeof(float), s, (int) sstr, &f, (int) n);
+ oil_scalaradd_f32(buf, sizeof(float), buf, sizeof(float), &p, (int) n);
+ oil_clipconv_u8_f32(d, (int) dstr, buf, sizeof(float), (int) n);
return SA_SUCCESS;
}
@@ -300,12 +320,11 @@ static int format_f32_to_u8(sa_bbuffer_t *b, void *dst, size_t dstr, const void
static int format_f32_to_ulaw(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
uint8_t *d = dst;
const float *s = src;
- unsigned n = bytes/sizeof(float);
+ size_t n = bytes/sizeof(float);
for (; n > 0; n --, d += dstr, s += sstr/sizeof(float)) {
float v = *s * 0x7FFF;
- if (v < -0x8000) v = -0x8000;
- if (v > -0x7FFF) v = -0x7FFF;
+ v = SA_CLAMP(v, (float) -0x8000, (float) 0x7FFF);
*d = sa_13linear2alaw((int16_t) v);
}
@@ -315,12 +334,11 @@ static int format_f32_to_ulaw(sa_bbuffer_t *b, void *dst, size_t dstr, const voi
static int format_f32_to_alaw(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
uint8_t *d = dst;
const float *s = src;
- unsigned n = bytes/sizeof(float);
+ size_t n = bytes/sizeof(float);
for (; n > 0; n --, d += dstr, s += sstr/sizeof(float)) {
float v = *s * 0x7FFF;
- if (v < -0x8000) v = -0x8000;
- if (v > -0x7FFF) v = -0x7FFF;
+ v = SA_CLAMP(v, (float) -0x8000, (float) 0x7FFF);
*d = sa_13linear2alaw((int16_t) v);
}
@@ -332,13 +350,13 @@ static int format_f32_to_s16(sa_bbuffer_t *b, void *dst, size_t dstr, const void
const float *s = src;
float *buf;
float f = 0x7FFF;
- unsigned n = bytes/sizeof(float);
+ size_t n = bytes/sizeof(float);
if (!(buf = sa_bbuffer_get(b, 0, bytes, 1)))
return SA_ERROR_OOM;
- oil_scalarmult_f32(buf, sizeof(float), s, sstr, &f, n);
- oil_clipconv_s16_f32(d, dstr, buf, sizeof(float), n);
+ oil_scalarmult_f32(buf, sizeof(float), s, (int) sstr, &f, (int) n);
+ oil_clipconv_s16_f32(d, (int) dstr, buf, sizeof(float), (int) n);
return SA_SUCCESS;
}
@@ -346,25 +364,24 @@ static int format_f32_to_s16(sa_bbuffer_t *b, void *dst, size_t dstr, const void
static int format_f32_to_s24(sa_bbuffer_t *b, void *dst, size_t dstr, const void *src, size_t sstr, size_t bytes) {
uint8_t *d = dst;
const float *s = src;
- unsigned n = bytes/sizeof(float);
+ size_t n = bytes/sizeof(float);
for (; n > 0; n--, d += dstr/3, s += sstr/sizeof(float)) {
float f = *s / 0x7fffff;
uint32_t j;
- if (f > 0x7fffff) f = 0x7fffff;
- if (f < -0x800000) f = -0x800000;
+ f = SA_CLAMP(f, (float) -0x800000, (float) 0x7FFFFF);
j = (uint32_t) ((int32_t) f);
#if defined(SA_LITTLE_ENDIAN)
- d[0] = j & 0xFF;
- d[1] = (j >> 8) & 0xFF;
- d[2] = (j >> 16);
+ d[0] = (uint8_t) j;
+ d[1] = (uint8_t) (j >> 8);
+ d[2] = (uint8_t) (j >> 16);
#elif defined(SA_BIG_ENDIAN)
- d[2] = j & 0xFF;
- d[1] = (j >> 8) & 0xFF;
- d[0] = (j >> 16);
+ d[2] = (uint8_t) j;
+ d[1] = (uint8_t) (j >> 8);
+ d[0] = (uint8_t) (j >> 16);
#else
#error "Unknown byte order"
#endif
@@ -377,14 +394,14 @@ static int format_f32_to_s32(sa_bbuffer_t *b, void *dst, size_t dstr, const void
int32_t *d = dst;
const float *s = src;
float *buf;
- float f = 0x7FFFFFFF;
- unsigned n = bytes/sizeof(float);
+ float f = (float) 0x7FFFFFFF;
+ size_t n = bytes/sizeof(float);
if (!(buf = sa_bbuffer_get(b, 0, bytes, 1)))
return SA_ERROR_OOM;
- oil_scalarmult_f32(buf, sizeof(float), s, sstr, &f, n);
- oil_clipconv_s32_f32(d, dstr, buf, sizeof(float), n);
+ oil_scalarmult_f32(buf, sizeof(float), s, (int) sstr, &f, (int) n);
+ oil_clipconv_s32_f32(d, (int) dstr, buf, sizeof(float), (int) n);
return SA_SUCCESS;
}