summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--add.c8
-rw-r--r--macro.h40
-rw-r--r--speex/resample.c23
3 files changed, 57 insertions, 14 deletions
diff --git a/add.c b/add.c
index ec10131..b7714f4 100644
--- a/add.c
+++ b/add.c
@@ -10,9 +10,7 @@ static void add_u8(void *dst, size_t dstr, const void *src1, size_t sstr1, const
for (; bytes > 0; bytes--, d += dstr, s1 += sstr1, s2 += sstr2) {
int16_t v = (int16_t) *s1 + (int16_t) *s2 - 0x80;
- if (v > 0xFF) v = 0xFF;
- if (v < 0) v = 0;
-
+ v = CLAMP(v, 0, 0xFF);
*d = (uint8_t) v;
}
}
@@ -28,9 +26,7 @@ static void add_s32(void *dst, size_t dstr, const void *src1, size_t sstr1, cons
for (; bytes > 0; bytes--, d += dstr/sizeof(int32_t), s1 += sstr1/sizeof(int32_t), s2 += sstr2/sizeof(int32_t)) {
int64_t v = (int64_t) *s1 + (uint64_t) *s2;
- if (v > 0x7FFFFFFF) v = 0x7FFFFFFF;
- if (v < -0x80000000) v = -0x80000000;
-
+ v = CLAMP(v, -0x80000000, 0x7FFFFFFF);
*d = (int32_t) v;
}
}
diff --git a/macro.h b/macro.h
index 5e4be44..d664c3e 100644
--- a/macro.h
+++ b/macro.h
@@ -47,6 +47,13 @@
#define sa_assert assert
+/* An assert which guarantees side effects of x */
+#ifdef NDEBUG
+#define sa_assert_se(x) x
+#else
+#define sa_assert_se(x) sa_assert(x)
+#endif
+
#define sa_assert_not_reached() sa_assert(!"Should not be reached.")
#define sa_assert_success(x) do { \
@@ -56,13 +63,38 @@
#define SA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
-#ifndef SA_MAX
-#define SA_MAX(a, b) ((a) > (b) ? (a) : (b))
+#ifndef MAX
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+#endif
+
+#ifndef MIN
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+#endif
+
+#ifndef CLAMP
+#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
#endif
-#ifndef SA_MIN
-#define SA_MIN(a, b) ((a) < (b) ? (a) : (b))
+
+#ifndef FALSE
+#define FALSE (0)
+#endif
+
+#ifndef TRUE
+#define TRUE (!FALSE)
#endif
+#define SA_PTR_TO_UINT(p) ((unsigned int) (unsigned long) (p))
+#define SA_UINT_TO_PTR(u) ((void*) (unsigned long) (u))
+
+#define SA_PTR_TO_UINT32(p) ((uint32_t) SA_PTR_TO_UINT(p))
+#define SA_UINT32_TO_PTR(u) SA_UINT_TO_PTR((uint32_t) u)
+
+#define SA_PTR_TO_INT(p) ((int) SA_PTR_TO_UINT(p))
+#define SA_INT_TO_PTR(u) SA_UINT_TO_PTR((int) u)
+
+#define SA_PTR_TO_INT32(p) ((int32_t) SA_PTR_TO_UINT(p))
+#define SA_INT32_TO_PTR(u) SA_UINT_TO_PTR((int32_t) u)
+
static inline size_t sa_align(size_t l) {
return (((l + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*));
}
diff --git a/speex/resample.c b/speex/resample.c
index cd01732..1cc4d49 100644
--- a/speex/resample.c
+++ b/speex/resample.c
@@ -84,6 +84,7 @@ static void speex_free (void *ptr) {free(ptr);}
#define OVERSAMPLE 8
#define IMAX(a,b) ((a) > (b) ? (a) : (b))
+#define IMIN(a,b) ((a) < (b) ? (a) : (b))
#ifndef NULL
#define NULL 0
@@ -576,10 +577,10 @@ static void update_filter(SpeexResamplerState *st)
}
for (i=0;i<st->den_rate;i++)
{
- spx_uint32_t j;
+ spx_int32_t j;
for (j=0;j<st->filt_len;j++)
{
- st->sinc_table[i*st->filt_len+j] = sinc(st->cutoff,((j-st->filt_len/2+1)-((float)i)/st->den_rate), st->filt_len, quality_map[st->quality].window_func);
+ st->sinc_table[i*st->filt_len+j] = sinc(st->cutoff,((j-(spx_int32_t)st->filt_len/2+1)-((float)i)/st->den_rate), st->filt_len, quality_map[st->quality].window_func);
}
}
#ifdef FIXED_POINT
@@ -997,16 +998,19 @@ void speex_resampler_get_rate(SpeexResamplerState *st, spx_uint32_t *in_rate, sp
int speex_resampler_set_rate_frac(SpeexResamplerState *st, spx_uint32_t ratio_num, spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate)
{
- int fact;
+ spx_uint32_t fact;
+ spx_uint32_t old_den;
+ spx_uint32_t i;
if (st->in_rate == in_rate && st->out_rate == out_rate && st->num_rate == ratio_num && st->den_rate == ratio_den)
return RESAMPLER_ERR_SUCCESS;
+ old_den = st->den_rate;
st->in_rate = in_rate;
st->out_rate = out_rate;
st->num_rate = ratio_num;
st->den_rate = ratio_den;
/* FIXME: This is terribly inefficient, but who cares (at least for now)? */
- for (fact=2;fact<=sqrt(IMAX(in_rate, out_rate));fact++)
+ for (fact=2;fact<=IMIN(st->num_rate, st->den_rate);fact++)
{
while ((st->num_rate % fact == 0) && (st->den_rate % fact == 0))
{
@@ -1015,6 +1019,17 @@ int speex_resampler_set_rate_frac(SpeexResamplerState *st, spx_uint32_t ratio_nu
}
}
+ if (old_den > 0)
+ {
+ for (i=0;i<st->nb_channels;i++)
+ {
+ st->samp_frac_num[i]=st->samp_frac_num[i]*st->den_rate/old_den;
+ /* Safety net */
+ if (st->samp_frac_num[i] >= st->den_rate)
+ st->samp_frac_num[i] = st->den_rate-1;
+ }
+ }
+
if (st->initialised)
update_filter(st);
return RESAMPLER_ERR_SUCCESS;